Repository navigation
Write C# member modifiers only where C# allows them [patch] - #225
Merged
Merged
Conversation
WriteFunctionModifiers wrote the AST's modifiers without looking at the enclosing type, so ordinary C++-shaped declarations produced C# that does not compile: - IsReadOnly became `readonly` on class and interface members (CS0106). It is now spelled only on an instance member of a struct; anywhere else the claim is written down in a note, as a readonly class's is. - A destructor got an access modifier (`public ~Vec()`, CS0106). It now gets none, and inside a struct, which cannot have a finalizer (CS0575), it is written down rather than declared. - An abstract member left its class unmarked (CS0513). A class with an abstract member is now `abstract`, and an interface member drops the `abstract` it already has implicitly. GenerateFunction now takes the enclosing ClassDeclaration rather than its name so the modifiers can see what kind of type they are in. Fixes #127 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_018FP1DMG4WYMABYwTfyb2Xd
Sonar flagged GenerateFunction's cognitive complexity (S3776, 19 of 15) after the destructor and readonly notes were added to it. They now live in WriteMemberNotes; the output is unchanged. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_018FP1DMG4WYMABYwTfyb2Xd
Sonar still scored GenerateFunction at 16 of 15 (S3776) after the member notes moved out. The initialiser assignments now live in WriteInitialisers; the output is unchanged. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_018FP1DMG4WYMABYwTfyb2Xd
…-modifiers # Conflicts: # Coder/Languages/CSharpGenerator.cs
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



Fixes #127
What was wrong
CSharpGenerator.WriteFunctionModifierswrote modifiers without checking what kind of type the member was in. Each of the issue's repros failed to build:IsReadOnlypublic readonly int get()IsReadOnly+IsAbstract(CompiledExemplar.Shape())public abstract readonly double area();IsAbstractmember in aKind = Classtypepublic class Animal { public abstract string speak(); }public ~Vec()Change
GenerateFunctiontakes the enclosingClassDeclarationinstead of its name, soWriteFunctionModifierscan see the type's kind.IsReadOnlySpelled). Anywhere else the member still makes the claim, so it gets a note:// readonly: get does not modify its instance. This follows the existingWriteTypePromiseshandling of a readonly class.IsAbstractmember is writtenabstract class(IsAbstractClass). Interface members dropabstract, because a bodiless interface member is already abstract.WriteInexpressiblenote instead of being declared.Tests
CSharpMemberModifierTestshas seven tests: a classreadonlymethod, struct instance and static methods,CompiledExemplar.Shape()as an interface, abstract and non-abstract classes, and class and struct destructors.FunctionShapeTests.ReadOnly_IsSpelledWhereALanguageHasItused to pinreadonlyon a free-standing function, which C# doesn't allow either. It now pinsreadonlyon a struct member, the one place C# allows it.readonlymethod and a destructor) through the changed generator and built the output withdotnet buildon net10.0. It succeeded with no errors.Coder.Testsuite: 1207 passed, 0 failed.dotnet format --verify-no-changesis clean on the touched files.🤖 Generated with Claude Code
https://claude.ai/code/session_018FP1DMG4WYMABYwTfyb2Xd
Generated by Claude Code