Skip to content

Write the missing side of a C# comparison operator pair [patch] - #246

Merged
matt-edmondson merged 1 commit into
mainfrom
fix/236-csharp-comparison-pairs
Oct 18, 2026
Merged

matt-edmondson merged 1 commit into
mainfrom
fix/236-csharp-comparison-pairs

Conversation

@matt-edmondson

Copy link
Copy Markdown
Contributor

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.GenerateClass writes 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 as rhs < 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 < int would need an int < Point that 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: == without Equals/GetHashCode overrides still produces CS0660/CS0661 warnings. Those are warnings, not errors, so they stay outside this issue's scope.

Tests: new CSharpComparisonPairTests.

  • Partner spelling for all six operators.
  • No duplicates when both sides are declared.
  • No swapped partner for an ordering against another type.
  • dotnet build of 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

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
@sonarqubecloud

Copy link
Copy Markdown

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

C#: declaring only one of a comparison operator pair (==/!=, </>, <=/>=) generates code that fails with CS0216

2 participants