Skip to content

Commit

Permalink
Update set_alias to use FindFieldByNumber
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 688120291
  • Loading branch information
honglooker authored and copybara-github committed Oct 21, 2024
1 parent b8d3567 commit 377b5e4
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
4 changes: 2 additions & 2 deletions hpb_generator/gen_accessors.cc
Original file line number Diff line number Diff line change
Expand Up @@ -285,15 +285,15 @@ 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",
class_name, MessagePtrConstType(field, /* is_const */ false),
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());
}
}
}
Expand Down
5 changes: 3 additions & 2 deletions hpb_generator/tests/set_alias.proto
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
12 changes: 12 additions & 0 deletions hpb_generator/tests/test_generated.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1284,6 +1284,18 @@ TEST(CppGeneratedCode, SetAlias) {
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) {
hpb::Arena arena;
auto child = hpb::CreateMessage<Child>(arena);
Expand Down

0 comments on commit 377b5e4

Please sign in to comment.