From f329d78382500143262967cbaa094da9c1e35874 Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 18 Oct 2026 10:34:05 +0000 Subject: [PATCH 1/2] Write C# operators public static, noting a dropped visibility or virtual [patch] C# requires a user-defined operator to be public and static, and allows neither virtual nor abstract on one outside an interface. The generator wrote a private or protected operator's visibility unchanged (CS0558) and a virtual or abstract one's modifier unchanged (CS0106). Operators and conversion operators are now always public static, with a note above the declaration naming what C++ allowed and C# cannot say. Fixes #260 Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_0175VbFC9TAs3FcivXVHTFum --- .../CSharpGeneratedSourceCompilesTests.cs | 44 +++++++++++++++++++ .../Languages/CSharpMemberModifierTests.cs | 26 ++++++++++- Coder/Languages/CSharpGenerator.cs | 41 +++++++++++++++-- 3 files changed, 106 insertions(+), 5 deletions(-) diff --git a/Coder.Test/Languages/CSharpGeneratedSourceCompilesTests.cs b/Coder.Test/Languages/CSharpGeneratedSourceCompilesTests.cs index 9182329..8dc3857 100644 --- a/Coder.Test/Languages/CSharpGeneratedSourceCompilesTests.cs +++ b/Coder.Test/Languages/CSharpGeneratedSourceCompilesTests.cs @@ -141,4 +141,48 @@ public void Operators_Compile() Assert.AreEqual(0, exitCode, $"C# rejected the generated source:{Environment.NewLine}{output}{Environment.NewLine}{source}"); }); } + + /// + /// Tests that operators declared private, protected or virtual, which C++ allows, still compile + /// as the public static operators C# requires, with what was dropped written down (#260). + /// + [TestMethod] + public void OperatorsDeclaredPrivateOrVirtual_Compile() + { + if (ToolchainHarness.FindOnPath("--version", "dotnet") is null) + { + Assert.Inconclusive("No dotnet on the path, so nothing was compiled."); + return; + } + + FunctionDeclaration plus = CompiledExemplar.Plus(); + plus.Visibility = Visibility.Private; + FunctionDeclaration negate = CompiledExemplar.Negate(); + negate.IsVirtual = true; + FunctionDeclaration toDouble = CompiledExemplar.ToDouble("self.x"); + toDouble.Visibility = Visibility.Protected; + + ClassDeclaration point = CompiledExemplar.Point(); + point.Members.Add(plus); + point.Members.Add(negate); + point.Members.Add(toDouble); + + SourceFile file = new("Generated"); + file.Members.Add(point); + + ToolchainHarness.InTemporaryDirectory(directory => + { + string source = new CSharpGenerator().Generate(file); + Assert.Contains("// private: operator + is public, as every C# operator is", source, StringComparison.Ordinal, source); + Assert.Contains("// virtual: operator - is static", source, StringComparison.Ordinal, source); + Assert.Contains("// protected: ", source, StringComparison.Ordinal, source); + + File.WriteAllText(Path.Combine(directory, "Generated.csproj"), Project); + File.WriteAllText(Path.Combine(directory, "Driver.cs"), OperatorDriver); + File.WriteAllText(Path.Combine(directory, "Generated.cs"), source); + + (int exitCode, string output) = ToolchainHarness.Run("dotnet", "build -nologo", directory); + Assert.AreEqual(0, exitCode, $"C# rejected the generated source:{Environment.NewLine}{output}{Environment.NewLine}{source}"); + }); + } } diff --git a/Coder.Test/Languages/CSharpMemberModifierTests.cs b/Coder.Test/Languages/CSharpMemberModifierTests.cs index af5e867..4999c1f 100644 --- a/Coder.Test/Languages/CSharpMemberModifierTests.cs +++ b/Coder.Test/Languages/CSharpMemberModifierTests.cs @@ -1,4 +1,4 @@ -// Copyright (c) 2023-2026 ktsu-dev contributors +// Copyright (c) 2023-2026 ktsu-dev contributors namespace ktsu.Coder.Test.Languages; @@ -136,4 +136,28 @@ public void StructDestructor_IsANote() Assert.Contains("// ~Vec is a destructor, which a C# struct cannot declare.", code, StringComparison.Ordinal, code); Assert.DoesNotContain("~Vec()", code, StringComparison.Ordinal, code); } + + /// + /// Tests that an abstract operator in a class is written as the public static operator C# + /// requires, with neither abstract nor its declared visibility, and a note saying so (#260). + /// + [TestMethod] + public void AbstractProtectedOperatorInAClass_IsPublicStaticWithANote() + { + ClassDeclaration shape = new("Shape"); + FunctionDeclaration plus = CompiledExemplar.Plus(); + plus.ReturnType = "Shape"; + plus.Parameters[0].Type = "Shape"; + plus.IsAbstract = true; + plus.Visibility = Visibility.Protected; + shape.Members.Add(plus); + + string code = new CSharpGenerator().Generate(shape); + + Assert.Contains("public static Shape operator +(", code, StringComparison.Ordinal, code); + Assert.DoesNotContain("abstract Shape", code, StringComparison.Ordinal, code); + Assert.DoesNotContain("protected static", code, StringComparison.Ordinal, code); + Assert.Contains("// abstract: operator + is static", code, StringComparison.Ordinal, code); + Assert.Contains("// protected: operator + is public", code, StringComparison.Ordinal, code); + } } diff --git a/Coder/Languages/CSharpGenerator.cs b/Coder/Languages/CSharpGenerator.cs index bb2a428..f9b37b7 100644 --- a/Coder/Languages/CSharpGenerator.cs +++ b/Coder/Languages/CSharpGenerator.cs @@ -973,9 +973,33 @@ private bool WriteMemberNotes(FunctionDeclaration function, string name, ClassDe WriteInexpressible(code, $"readonly: {name} does not modify its instance"); } + // C++ lets a member operator be private, protected or virtual; a C# operator is public and + // static and nothing else, so what it asked for is written down rather than lost. + if (IsOperator(function)) + { + if (function.Visibility is not (Visibility.Unspecified or Visibility.Public)) + { + WriteInexpressible(code, $"{SpellVisibility(function.Visibility)}: {name} is public, as every C# operator is"); + } + + if (enclosingType?.Kind != TypeDeclarationKind.Interface && (function.IsVirtual || function.IsAbstract)) + { + string modifier = function.IsAbstract ? "abstract" : "virtual"; + WriteInexpressible(code, $"{modifier}: {name} is static, which a C# operator outside an interface cannot override"); + } + } + return true; } + /// + /// Gets whether a declaration is one C# requires to be public static. + /// + /// The declaration being emitted. + /// True for an operator or a conversion operator. + private static bool IsOperator(FunctionDeclaration function) => + function.Kind is FunctionKind.Operator or FunctionKind.ConversionOperator; + /// /// Writes the attributes a declaration earns. /// @@ -1010,28 +1034,37 @@ private void WriteFunctionModifiers(FunctionDeclaration function, CodeBlocker co // A function nobody has given a visibility to is public: an inaccessible method is not what // someone who wrote no modifier meant. A finalizer is the exception, because it takes no // access modifier at all (CS0106): only the runtime calls it. - if (function.Kind != FunctionKind.Destructor) + // A user-defined operator must be public as well as static (CS0558), whatever it declared. + if (IsOperator(function)) + { + code.Write("public "); + } + else if (function.Kind != FunctionKind.Destructor) { code.Write($"{SpellVisibility(function.Visibility) ?? DefaultVisibility} "); } if (function.IsStatic || ReferenceEquals(function, heldMember) - || function.Kind is FunctionKind.Operator or FunctionKind.ConversionOperator) + || IsOperator(function)) { // A C# operator is always static, whether or not the declaration thought to say so. code.Write("static "); } + // An operator outside an interface can be neither virtual nor abstract (CS0106); the note + // written above the declaration says what was dropped. + bool canOverride = !IsOperator(function) || enclosingType?.Kind == TypeDeclarationKind.Interface; + // An interface member with no body is abstract already, and says so by having none. if (function.IsAbstract) { - if (enclosingType?.Kind != TypeDeclarationKind.Interface) + if (canOverride && enclosingType?.Kind != TypeDeclarationKind.Interface) { code.Write("abstract "); } } - else if (function.IsVirtual) + else if (function.IsVirtual && canOverride) { code.Write("virtual "); } From 57497a18552f36caf29d7cc418d4a83dfbd5f9bf Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 18 Oct 2026 10:40:16 +0000 Subject: [PATCH 2/2] Keep CSharpMemberModifierTests.cs without a byte order mark Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_0175VbFC9TAs3FcivXVHTFum --- Coder.Test/Languages/CSharpMemberModifierTests.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Coder.Test/Languages/CSharpMemberModifierTests.cs b/Coder.Test/Languages/CSharpMemberModifierTests.cs index 4999c1f..ee3faf6 100644 --- a/Coder.Test/Languages/CSharpMemberModifierTests.cs +++ b/Coder.Test/Languages/CSharpMemberModifierTests.cs @@ -1,4 +1,4 @@ -// Copyright (c) 2023-2026 ktsu-dev contributors +// Copyright (c) 2023-2026 ktsu-dev contributors namespace ktsu.Coder.Test.Languages;