Repository navigation
Hold a C# namespace's fields and functions in a static class [patch] - #226
Merged
Merged
Conversation
C# has no member outside a type, but CSharpGenerator wrote a namespace's fields, functions and variables straight into the namespace (CS0116), and a file's straight into the file (CS0106). Every other target has somewhere to put them, so the AST allows them. They are now gathered into a public static class, written where the first of them was declared and named after the namespace's last segment, or Globals at file scope. A type of the same name beside it gets the Globals suffix instead. Each held member is written static, except a const, which already is; locals in a held function's body are untouched. Types, enums, aliases and nested namespaces stay put. The file-scope loop moves from LanguageGeneratorBase.GenerateSourceFile into an overridable GenerateFileMembers so C# can do the same there. CSharpGeneratedSourceCompilesTests builds the output with dotnet build, like the C, C++, Rust and Go compile tests. Fixes #155 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_018FP1DMG4WYMABYwTfyb2Xd
|
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 #155
What was wrong
CSharpGenerator.GenerateNamespacewrote every member straight into the namespace, so the issue's repro came out as:C# has no member outside a type, so this fails with CS0116. The same members directly in a
SourceFilefail with CS0106.Change
FieldDeclaration,FunctionDeclarationandVariableDeclarationmembers are gathered into apublic static class, placed where the first of them was declared:Globalsat file scope. If a type, enum or alias beside it already has that name, the class gets aGlobalssuffix to avoid CS0101.static(CS0708). A heldconstvariable is left as plainconst, becausestatic constis CS0504. Locals inside a held function are untouched: the generator tracks the member being held by reference, not with a flag, so nothing in its body is affected.LanguageGeneratorBase.GenerateSourceFileinto an overridableGenerateFileMembers, so C# can do the same at file scope. Other generators behave the same as before.Tests
CSharpScopeMemberTests(6 tests): the namespace holder and its ordering, locals staying locals,constwithoutstatic, the name-collision fallback, file-scope members, and no holder when a namespace holds only types.CSharpGeneratedSourceCompilesTests, matching the C/C++/Rust/Go ones. It writes the generated file next to a driver that uses every declaration from outside, runsdotnet build, and is inconclusive when nodotnetis on the path.Coder.Testsuite: 1207 passed, 0 failed.dotnet format --verify-no-changesis clean on the touched files.Not covered
The compile test builds the issue's shapes, not all of
CompiledExemplar. Compiling the whole exemplar as C# still hits #127 (abstract readonlyonShape, fixed separately in #225) and other gaps tracked in #162.ExemplarReflectionTableTestsuses C++ type names such asholo::reflect::FieldInfo, so it can't be compiled as C# however its members are placed.This PR and #225 both edit
CSharpGenerator's function-modifier code, so whichever merges second will need a small merge.🤖 Generated with Claude Code
https://claude.ai/code/session_018FP1DMG4WYMABYwTfyb2Xd
Generated by Claude Code