internal/order: fix sorting of synthetic oneofs to be deterministic

Before this change, synthetic oneofs were ignored in the inOneof() helper
function, but not in the remainder of the LegacyFieldOrder function.

Change-Id: Ia60e5244ae1000e98bbba9dd26f1d1583337fab4
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/497935
Reviewed-by: Damien Neil <dneil@google.com>
Reviewed-by: Lasse Folger <lassefolger@google.com>
Reviewed-by: Cassondra Foesch <cfoesch@gmail.com>
This commit is contained in:
Michael Stapelberg 2023-05-24 16:50:36 +02:00
parent b8fc770601
commit cc524c9cf1
2 changed files with 3 additions and 3 deletions

View File

@ -33,7 +33,7 @@ var (
return !inOneof(ox) && inOneof(oy)
}
// Fields in disjoint oneof sets are sorted by declaration index.
if ox != nil && oy != nil && ox != oy {
if inOneof(ox) && inOneof(oy) && ox != oy {
return ox.Index() < oy.Index()
}
// Fields sorted by field number.

View File

@ -65,9 +65,9 @@ func TestFieldOrder(t *testing.T) {
// Non-extension fields that are not within a oneof
// sorted next by field number.
{number: 1},
{number: 5, oneofIndex: -9}, // synthetic oneof
{number: 5, oneofIndex: -10}, // synthetic oneof
{number: 10},
{number: 11, oneofIndex: -10}, // synthetic oneof
{number: 11, oneofIndex: -9}, // synthetic oneof
{number: 12},
// Non-synthetic oneofs sorted last by index.