Add missing map thunks for nested enums.

While at it, remove the code for emitting map thunks for bytes map keys - bytes are not supported map keys.

PiperOrigin-RevId: 621806332
pull/16371/head
Marcel Hlopko 10 months ago committed by Copybara-Service
parent 00caffaf00
commit b4bf6b22e5
  1. 11
      rust/test/enums.proto
  2. 2
      rust/test/shared/BUILD
  3. 9
      rust/test/shared/accessors_map_test.rs
  4. 3
      src/google/protobuf/compiler/rust/message.cc
  5. 12
      src/google/protobuf/compiler/rust/naming.cc
  6. 2
      src/google/protobuf/compiler/rust/naming.h

@ -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, InnerNested.NestedEnum> string_map = 1;
}

@ -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",

@ -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();

@ -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",
[&] {

@ -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

@ -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

Loading…
Cancel
Save