Skip to content

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

Closed

Description

@matt-edmondson

What's wrong

CSharpGenerator (Coder/Languages/CSharpGenerator.cs, operator signature around lines 1110-1125) writes each operator declaration independently. C# requires comparison operators to be declared in pairs: == with !=, < with >, and <= with >=. Nothing in the generator completes a pair.

Reproduction

Give struct Point a single operator: FunctionDeclaration("==") { Kind = Operator, ReturnType = bool }, with parameter rhs.

Generated C#:

public static bool operator ==(Point self, Point rhs) { ... }

dotnet build then fails:

error CS0216: The operator 'Point.operator ==(Point, Point)' requires a matching operator '!=' to also be defined

Declaring < without > fails the same way. Both cases were verified.

This is a natural AST to write. C++20 and Go accept a single comparison operator, and in Rust PartialEq supplies !=. Only C# needs the partner.

Suggested fix / acceptance criteria

  • When a type declares one side of ==/!=, </> or <=/>=, the C# generator also writes the missing partner:
    • != as !(self == rhs), and == as !(self != rhs)
    • > as rhs < self, and the reverse for <
    • <= and >= likewise
  • Add cases to CSharpGeneratedSourceCompilesTests that declare only == and only <, and confirm both compile.

This is separate from #231.

Activity

  1. added
    bug
    on Oct 17, 2026
  2. matt-edmondson commented on Oct 17, 2026

    @matt-edmondson
    ContributorAuthor

    Triage

    Notes: The generated code fails to compile with CS0216. The fix is well scoped: synthesize the missing partner operator, then add compile tests. Consider whether C# also needs Equals/GetHashCode overrides once == is synthesized; those produce warnings CS0660/CS0661, not errors.


    Generated by Claude Code

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

Metadata

Metadata

Labels

bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions