Skip to content

Commit

Permalink
Add missing map thunks for nested enums.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 619936338
  • Loading branch information
hlopko authored and copybara-github committed Apr 2, 2024
1 parent d8251eb commit 7905893
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 12 deletions.
9 changes: 9 additions & 0 deletions rust/test/enums.proto
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,12 @@ 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 {
enum NestedEnum {
UNKNOWN = 0;
FOO = 1;
}
map<string, NestedEnum> string_map = 1;
}
2 changes: 2 additions & 0 deletions rust/test/shared/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down
9 changes: 9 additions & 0 deletions rust/test/shared/accessors_map_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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::NestedEnum::Foo);
}

#[test]
fn test_bytes_and_string_copied() {
let mut msg = TestMap::new();
Expand Down
3 changes: 3 additions & 0 deletions src/google/protobuf/compiler/rust/message.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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",
[&] {
Expand Down
12 changes: 1 addition & 11 deletions src/google/protobuf/compiler/rust/naming.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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()",
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/google/protobuf/compiler/rust/naming.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 7905893

Please sign in to comment.