Skip to content

THRIFT-6493: Compare and hash netstd union values by the member they hold - #4112

Open
Jens-G wants to merge 1 commit into
apache:masterfrom
Jens-G:THRIFT-6493
Open

Jens-G wants to merge 1 commit into
apache:masterfrom
Jens-G:THRIFT-6493

Conversation

@Jens-G

@Jens-G Jens-G commented Nov 5, 2026

Copy link
Copy Markdown
Member

THRIFT-6493

With the union option, each member class of a generated union compared only the values in Equals, against other.As_<member>. That property returns the default value when another member is set. So U.x(0).Equals(U.y(0)) was true, and U.x(0).Equals(U.s("text")) was true while the reverse was false. GetHashCode() ignored the member as well.

Changes

  • t_netstd_generator.cc:
    • the Equals of the member classes first checks which member is set, as the base class already does;
    • their GetHashCode() adds the member's field ID before the value, and the base class computes the same value. This applies to both the net and the netstandard code path.
  • Thrift6493.thrift, added to the four Thrift.Compile projects, and DataModel/UnionEquality.cs. The tests cover:
    • equality across members, and the asymmetric pair;
    • hashing across members, and a HashSet;
    • equal values still hashing equally.

Compatibility

Union values of different members that hold equal values are no longer equal, and the hash codes of union values change.

Verification

  • With the master compiler, 4 of the 5 tests fail:
    • Assert.IsFalse(new U.x(0).Equals(new U.y(0))) gets true;
    • x(5) and y(5) both hash to 5;
    • a HashSet keeps only 4 of 5 values.
  • With the fix all 5 pass.
  • Thrift.Tests passes 157 of 157, IntegrationTests 57 of 57, and the compiler tests pass.
  • Over the test IDLs, only the generated code of unions changes.

This is the union counterpart of THRIFT-5056 (#4104), which covers structs. The two PRs change different generator code and merge in either order.

🤖 Generated with Claude Code

…hold

Client: netstd

The classes of the union members compared only the values in Equals, so values
of different members could be equal, and GetHashCode() ignored the member.
Equals now checks which member is set, and the hash code adds its field ID.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@mergeable mergeable Bot added c# Pull requests that update C# code Pull requests that update .NET code compiler labels Nov 5, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

c# Pull requests that update C# code Pull requests that update .NET code compiler

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant