From 377b5e4863b0d3948211f518050e7bdab40c4c4e Mon Sep 17 00:00:00 2001 From: Hong Shin Date: Mon, 21 Oct 2024 06:53:56 -0700 Subject: [PATCH] Update set_alias to use FindFieldByNumber PiperOrigin-RevId: 688120291 --- hpb_generator/gen_accessors.cc | 4 ++-- hpb_generator/tests/set_alias.proto | 5 +++-- hpb_generator/tests/test_generated.cc | 12 ++++++++++++ 3 files changed, 17 insertions(+), 4 deletions(-) diff --git a/hpb_generator/gen_accessors.cc b/hpb_generator/gen_accessors.cc index a1846d8013..9a70f6aef0 100644 --- a/hpb_generator/gen_accessors.cc +++ b/hpb_generator/gen_accessors.cc @@ -285,7 +285,7 @@ void WriteAccessorsInSource(const protobuf::Descriptor* desc, Context& ctx) { ABSL_CHECK_EQ(arena_, hpb::interop::upb::GetArena(target)); upb_Message_SetBaseFieldMessage( UPB_UPCAST(msg_), - upb_MiniTable_GetFieldByIndex($7::minitable(), $8), + upb_MiniTable_FindFieldByNumber($7::minitable(), $8), hpb::interop::upb::GetMessage(target)); } )cc", @@ -293,7 +293,7 @@ void WriteAccessorsInSource(const protobuf::Descriptor* desc, Context& ctx) { resolved_field_name, upb::generator::CApiMessageType(desc->full_name()), MessageBaseType(field, /* maybe_const */ false), resolved_upbc_name, - arena_expression, ClassName(desc), field->index()); + arena_expression, ClassName(desc), field->number()); } } } diff --git a/hpb_generator/tests/set_alias.proto b/hpb_generator/tests/set_alias.proto index a096e6d2b1..b279c0c6b4 100644 --- a/hpb_generator/tests/set_alias.proto +++ b/hpb_generator/tests/set_alias.proto @@ -14,8 +14,9 @@ message Child { } message Parent { - optional int32 x = 1; - optional Child child = 2; + optional int32 x = 2; + optional Child child = 3; + optional int64 out_of_order = 1; // set_alias must work regardless of order } message ParentWithRepeated { diff --git a/hpb_generator/tests/test_generated.cc b/hpb_generator/tests/test_generated.cc index 318c0c19d3..90ddc99410 100644 --- a/hpb_generator/tests/test_generated.cc +++ b/hpb_generator/tests/test_generated.cc @@ -1284,6 +1284,18 @@ TEST(CppGeneratedCode, SetAlias) { hpb::interop::upb::GetMessage(parent1.child())); } +TEST(CppGeneratedCode, SetAliasFieldsOutofOrder) { + hpb::Arena arena; + auto child = hpb::CreateMessage(arena); + child.set_peeps(12); + auto parent1 = hpb::CreateMessage(arena); + auto parent2 = hpb::CreateMessage(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) { hpb::Arena arena; auto child = hpb::CreateMessage(arena);