diff --git a/rust/test/enums.proto b/rust/test/enums.proto index 21182bc175..920992cda0 100644 --- a/rust/test/enums.proto +++ b/rust/test/enums.proto @@ -59,3 +59,14 @@ enum TestEnumValueNameSameAsEnum { TEST_ENUM_VALUE_NAME_SAME_AS_ENUM_UNKNOWN = 0; TEST_ENUM_VALUE_NAME_SAME_AS_ENUM = 1; } + +// Regression test for maps with nested enum as value. +message TestMapWithNestedEnum { + message InnerNested { + enum NestedEnum { + UNKNOWN = 0; + FOO = 1; + } + } + map string_map = 1; +} diff --git a/rust/test/shared/BUILD b/rust/test/shared/BUILD index 275da75035..055c3168ac 100644 --- a/rust/test/shared/BUILD +++ b/rust/test/shared/BUILD @@ -337,6 +337,7 @@ rust_test( "@crate_index//:paste", ], deps = [ + "//rust/test:enums_cc_rust_proto", "//rust/test:map_unittest_cc_rust_proto", "//rust/test:unittest_cc_rust_proto", "@crate_index//:googletest", @@ -350,6 +351,7 @@ rust_test( "@crate_index//:paste", ], deps = [ + "//rust/test:enums_upb_rust_proto", "//rust/test:map_unittest_upb_rust_proto", "//rust/test:unittest_upb_rust_proto", "@crate_index//:googletest", diff --git a/rust/test/shared/accessors_map_test.rs b/rust/test/shared/accessors_map_test.rs index be22aebf69..0fc153f63b 100644 --- a/rust/test/shared/accessors_map_test.rs +++ b/rust/test/shared/accessors_map_test.rs @@ -5,6 +5,7 @@ // license that can be found in the LICENSE file or at // https://developers.google.com/open-source/licenses/bsd +use enums_proto::{test_map_with_nested_enum, TestMapWithNestedEnum}; use googletest::prelude::*; use map_unittest_proto::{MapEnum, TestMap, TestMapWithMessages}; use paste::paste; @@ -125,6 +126,14 @@ fn test_string_maps() { assert_that!(msg.map_string_string().len(), eq(0)); } +#[test] +fn test_nested_enum_maps() { + // Verify that C++ thunks are generated and are with the right name for strings + TestMapWithNestedEnum::new() + .string_map_mut() + .insert("foo", test_map_with_nested_enum::inner_nested::NestedEnum::Foo); +} + #[test] fn test_bytes_and_string_copied() { let mut msg = TestMap::new(); diff --git a/src/google/protobuf/compiler/rust/message.cc b/src/google/protobuf/compiler/rust/message.cc index 87f951bb2d..622bbc608d 100644 --- a/src/google/protobuf/compiler/rust/message.cc +++ b/src/google/protobuf/compiler/rust/message.cc @@ -1164,6 +1164,9 @@ void GenerateThunksCc(Context& ctx, const Descriptor& msg) { for (int i = 0; i < msg.nested_type_count(); ++i) { GenerateThunksCc(ctx, *msg.nested_type(i)); } + for (int i = 0; i < msg.enum_type_count(); ++i) { + GenerateEnumThunksCc(ctx, *msg.enum_type(i)); + } }}, {"accessor_thunks", [&] { diff --git a/src/google/protobuf/compiler/rust/naming.cc b/src/google/protobuf/compiler/rust/naming.cc index 85f1548c09..745f6a6afd 100644 --- a/src/google/protobuf/compiler/rust/naming.cc +++ b/src/google/protobuf/compiler/rust/naming.cc @@ -419,7 +419,7 @@ PROTOBUF_CONSTINIT const MapKeyType kMapKeyTypes[] = { /*cc_key_t=*/"bool", /*cc_ffi_key_t=*/"bool", /*cc_from_ffi_key_expr=*/"key", /*cc_to_ffi_key_expr=*/"cpp_key"}, - {/*thunk_ident=*/"string", + {/*thunk_ident=*/"ProtoStr", /*rs_key_t=*/"$pb$::ProtoStr", /*rs_ffi_key_t=*/"$pbi$::PtrAndLen", /*rs_to_ffi_key_expr=*/"key.as_bytes().into()", @@ -429,16 +429,6 @@ PROTOBUF_CONSTINIT const MapKeyType kMapKeyTypes[] = { /*cc_ffi_key_t=*/"google::protobuf::rust_internal::PtrAndLen", /*cc_from_ffi_key_expr=*/ "std::string(key.ptr, key.len)", /*cc_to_ffi_key_expr=*/ - "google::protobuf::rust_internal::PtrAndLen(cpp_key.data(), cpp_key.size())"}, - {/*thunk_ident=*/"bytes", - /*rs_key_t=*/"[u8]", - /*rs_ffi_key_t=*/"$pbi$::PtrAndLen", - /*rs_to_ffi_key_expr=*/"key.into()", - /*rs_from_ffi_key_expr=*/"ffi_key.as_ref()", - /*cc_key_t=*/"std::string", - /*cc_ffi_key_t=*/"google::protobuf::rust_internal::PtrAndLen", - /*cc_from_ffi_key_expr=*/ - "std::string(key.ptr, key.len)", /*cc_to_ffi_key_expr=*/ "google::protobuf::rust_internal::PtrAndLen(cpp_key.data(), cpp_key.size())"}}; } // namespace rust diff --git a/src/google/protobuf/compiler/rust/naming.h b/src/google/protobuf/compiler/rust/naming.h index 2be35da405..f3d7af797e 100644 --- a/src/google/protobuf/compiler/rust/naming.h +++ b/src/google/protobuf/compiler/rust/naming.h @@ -151,7 +151,7 @@ struct MapKeyType { absl::string_view cc_to_ffi_key_expr; }; -extern const MapKeyType kMapKeyTypes[7]; +extern const MapKeyType kMapKeyTypes[6]; } // namespace rust } // namespace compiler