Skip to content

Friend operator (IsFriend, two operands) generates a three-operand operator in C# (CS1534) and Rust (E0050) #231

Closed

Description

@matt-edmondson

What's wrong

FunctionDeclaration.IsFriend is documented as "declared inside a type but is not a member of it ... how a symmetric operator is written beside the type" (Coder/Ast/FunctionDeclaration.cs:~146-155). A friend operator therefore declares both of its operands itself. Two generators ignore that:

Reproduction

A struct Point { int x } with an operator +, Kind = Operator, IsFriend = true, parameters a: Point, b: Point, returning a:

// C++ (correct)
friend Point operator+(Point a, Point b)
// C#
public static Point operator +(Point self, Point a, Point b)   // error CS1534: Overloaded binary operator '+' takes two parameters
// Rust
impl std::ops::Add for Point { type Output = Point; fn add(self, a: Point, b: Point) -> Point {...} }   // E0050

Both errors were confirmed with dotnet build and rustc. C and Go also gain a spurious receiver (Point_add(Point* self, Point a, Point b) and func (self *Point) Add(a, b)). Those compile, but don't match the declared arity.

Suggested fix

  • C#: return false from TakesImplicitOperand when function.IsFriend. The operator is already static.
  • Rust: when IsFriend, bind the first declared parameter as self (fn add(self, rhs: Point), rebinding the first parameter's name in the body), or write the operator out as unsupported.
  • C and Go: skip the receiver for friends, as WriteReceiver already does for IsStatic.
  • Add a friend + to CompiledExemplar and compile it in the C# and Rust compile tests.

Activity

  1. matt-edmondson commented on Oct 17, 2026

    @matt-edmondson
    ContributorAuthor

    Triage


    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

bugSomething isn't working

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions