Skip to content

C#: a private/protected or virtual/abstract operator generates code that fails to compile (CS0558, CS0106) #260

Closed

Description

@matt-edmondson

What's wrong

CSharpGenerator.WriteFunctionModifiers (Coder/Languages/CSharpGenerator.cs:1008-1049) writes a function's declared visibility (around :1015) and its virtual/abstract flags (around :1034) unchanged, even for Operator and ConversionOperator functions. C# requires user-defined operators to be public static and does not allow virtual or abstract on them. #126 made operators static, but it neither forces public nor drops virtual/abstract.

Reproduction (main @ 440ef35, output compiled with dotnet build)

  • Struct Point with an operator + (Kind = Operator, Visibility = Private, parameter rhs: Point):
    • Generated: private static Point operator +(Point self, Point rhs)
    • Result: CS0558, "User-defined operator must be declared static and public".
  • The same operator with IsVirtual = true on a class Shape:
    • Generated: public static virtual Shape operator +(...)
    • Result: CS0106, "The modifier 'virtual' is not valid for this item".

C++ allows private and virtual member operators, so an AST written in C++ terms produces C# that doesn't build.

Suggested fix / acceptance criteria

  • For Operator and ConversionOperator, always write public static. Write a WriteInexpressible-style note when the declared visibility is not public, or when IsVirtual or IsAbstract is set.
  • Add compile tests: a private operator and a virtual operator both produce C# that builds, with the note present.

Related: #126 and #236 (other C# operator generation issues).

Activity

  1. added
    bug
    on Oct 18, 2026
  2. matt-edmondson commented on Oct 18, 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

bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions