Skip to content

Spell C# access modifiers C# allows where each declaration stands [patch] - #455

Merged
matt-edmondson merged 5 commits into
mainfrom
fix/csharp-forbidden-visibility-428
Nov 11, 2026
Merged

matt-edmondson merged 5 commits into
mainfrom
fix/csharp-forbidden-visibility-428

Conversation

@matt-edmondson

Copy link
Copy Markdown
Contributor

Fixes #428

What was wrong

CSharpGenerator wrote each declaration's Visibility exactly as given, wherever the declaration sat. ASTs that are valid for C++ therefore produced C# that doesn't compile:

  • A private/protected type or enum directly in a namespace or file: CS1527.
  • A protected field or function moved into the generated public static class holder: CS1057.
  • A protected member of a struct: CS0666.

Change

There is now one rule, ForbiddenAccess, that decides by position. SpellAccess writes the modifier it allows, and WriteForbiddenAccessNote writes a WriteInexpressible note above the declaration, the same way operators already do (#260).

Position Given Written
Type/enum in a namespace or file private, protected internal
Member of the static holder protected internal, so types beside it in the namespace still reach it
Member of a struct (including a nested type) protected private, which is all protected means where nothing can derive
  • The rule is applied at every site the issue lists: type, enum, property, field, function and variable member.
  • A new typeMemberOwner, saved and restored next to typeMember, lets non-function members know which type encloses them.
  • Members of classes and interfaces are emitted as before.
  • Operators, destructors and the private-virtual-to-protected rule keep their existing handling.

Tests

  • CSharpGeneratedSourceCompilesTests.ForbiddenAccessModifiers_Compile builds every row of the issue's table plus a nested type in a struct. It runs dotnet build with a driver that calls the holder's members, and checks the notes. It also checks that a protected member of a class stays protected.
  • With the generator change reverted, the test fails. With it, the test passes.
  • Full suite: 1665 passed, 0 failed.

Generated by Claude Code

…tch]

A private or protected type in a namespace or file is internal (CS1527), a
protected member of the static holder class is internal (CS1057), and a
protected member of a struct is private (CS0666). Each changed modifier is
written down in a note above the declaration. Members of classes and
interfaces are unchanged.

Fixes #428

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017vfJW58CbYAhSAZyBsuJtr
Keeps main's typeless-list fallback in GenerateVariableDeclaration ahead of
the positioned access modifier, and both sides' appended tests.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017vfJW58CbYAhSAZyBsuJtr
Keeps #454's appended C# compile test beside this branch's.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017vfJW58CbYAhSAZyBsuJtr
Keeps the C# compile tests main appended (#451) beside this branch's.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017vfJW58CbYAhSAZyBsuJtr
@sonarqubecloud

Copy link
Copy Markdown

@matt-edmondson
matt-edmondson merged commit 6f7beef into main Nov 11, 2026
16 checks passed
@matt-edmondson
matt-edmondson deleted the fix/csharp-forbidden-visibility-428 branch November 11, 2026 02:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

C# emits private/protected where C# forbids them: namespace-level types (CS1527), the static holder class (CS1057), struct members (CS0666)

2 participants