Repository navigation
Write the missing side of a C# comparison operator pair [patch] - #246
Merged
Merged
Conversation
C# requires == with !=, < with > and <= with >= (CS0216). A type that declares one side now gets the other: an equality partner negates the declared operator, and an ordering partner swaps its operands when the right-hand operand is the type itself. Fixes #236 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01CYLouPcEwkKsAvho4nRqQK
|
This was referenced Oct 18, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



Fixes #236
Before: A type declaring only
==, or only<, generated C# that failed with CS0216, because C# requires==/!=,</>and<=/>=to be declared in pairs.After:
CSharpGenerator.GenerateClasswrites each missing partner after the type's own members, using the issue's spellings:!=is written as!(self == rhs), and==as!(self != rhs).>is written asrhs < self, and the same swap applies to the other ordering partners.Partners are synthesized only for binary, non-friend operators that are provided rather than abstract or deleted, and never on an interface. A pair the type already declares in full is left alone.
One limit worth a look: an ordering partner swaps the operands, so it only resolves when the right-hand operand is the type itself. An operator like
Point < intwould need anint < Pointthat nobody declared. That case is left as declared and still hits CS0216. Equality partners don't have this problem, because negation works whatever the operand type.Not done here:
==withoutEquals/GetHashCodeoverrides still produces CS0660/CS0661 warnings. Those are warnings, not errors, so they stay outside this issue's scope.Tests: new
CSharpComparisonPairTests.dotnet buildof a type declaring only==, and only<, with a driver that uses both sides of each pair.8 of the 10 fail on
main. The 2 that pass there are the cases where nothing should be synthesized. All 10 pass with the change, and the full suite passes: 1293 of 1293.Generated by Claude Code