Update set_alias to use FindFieldByNumber

PiperOrigin-RevId: 688120291
pull/18894/head
Hong Shin 1 month ago committed by Copybara-Service
parent b8d356701a
commit 377b5e4863
  1. 4
      hpb_generator/gen_accessors.cc
  2. 5
      hpb_generator/tests/set_alias.proto
  3. 12
      hpb_generator/tests/test_generated.cc

@ -285,7 +285,7 @@ void WriteAccessorsInSource(const protobuf::Descriptor* desc, Context& ctx) {
ABSL_CHECK_EQ(arena_, hpb::interop::upb::GetArena(target)); ABSL_CHECK_EQ(arena_, hpb::interop::upb::GetArena(target));
upb_Message_SetBaseFieldMessage( upb_Message_SetBaseFieldMessage(
UPB_UPCAST(msg_), UPB_UPCAST(msg_),
upb_MiniTable_GetFieldByIndex($7::minitable(), $8), upb_MiniTable_FindFieldByNumber($7::minitable(), $8),
hpb::interop::upb::GetMessage(target)); hpb::interop::upb::GetMessage(target));
} }
)cc", )cc",
@ -293,7 +293,7 @@ void WriteAccessorsInSource(const protobuf::Descriptor* desc, Context& ctx) {
resolved_field_name, resolved_field_name,
upb::generator::CApiMessageType(desc->full_name()), upb::generator::CApiMessageType(desc->full_name()),
MessageBaseType(field, /* maybe_const */ false), resolved_upbc_name, MessageBaseType(field, /* maybe_const */ false), resolved_upbc_name,
arena_expression, ClassName(desc), field->index()); arena_expression, ClassName(desc), field->number());
} }
} }
} }

@ -14,8 +14,9 @@ message Child {
} }
message Parent { message Parent {
optional int32 x = 1; optional int32 x = 2;
optional Child child = 2; optional Child child = 3;
optional int64 out_of_order = 1; // set_alias must work regardless of order
} }
message ParentWithRepeated { message ParentWithRepeated {

@ -1284,6 +1284,18 @@ TEST(CppGeneratedCode, SetAlias) {
hpb::interop::upb::GetMessage(parent1.child())); hpb::interop::upb::GetMessage(parent1.child()));
} }
TEST(CppGeneratedCode, SetAliasFieldsOutofOrder) {
hpb::Arena arena;
auto child = hpb::CreateMessage<Child>(arena);
child.set_peeps(12);
auto parent1 = hpb::CreateMessage<Parent>(arena);
auto parent2 = hpb::CreateMessage<Parent>(arena);
parent1.set_alias_child(child);
parent2.set_alias_child(child);
ASSERT_EQ(parent1.child()->peeps(), parent2.child()->peeps());
ASSERT_EQ(parent1.child()->peeps(), 12);
}
TEST(CppGeneratedCode, SetAliasFailsForDifferentArena) { TEST(CppGeneratedCode, SetAliasFailsForDifferentArena) {
hpb::Arena arena; hpb::Arena arena;
auto child = hpb::CreateMessage<Child>(arena); auto child = hpb::CreateMessage<Child>(arena);

Loading…
Cancel
Save