Skip to content

Escape keyword-named identifiers in the C# generator [patch] - #441

Merged
matt-edmondson merged 1 commit into
mainfrom
fix/106-csharp-keyword-identifiers
Nov 4, 2026
Merged

matt-edmondson merged 1 commit into
mainfrom
fix/106-csharp-keyword-identifiers

Conversation

@matt-edmondson

Copy link
Copy Markdown
Contributor

Part of #106, the C# step. Go and JavaScript are still left after this, so the PR doesn't close the issue.

What was wrong

The C# generator wrote declared names verbatim. Any field, property, parameter, local, method, enum member or designated member named after a C# keyword produced a file that failed to compile. A parameter called in or out was enough to break it (int total(int in, int out)).

Change

  • CSharpGenerator now declares C#'s reserved keywords as ReservedWords. It overrides EscapeIdentifier to write a verbatim identifier (@default), so nothing gets renamed and a C# caller uses the same name.
  • Every place that writes a declared member name now goes through the hook. That covers fields, properties, parameters, locals, method names, enum members, object-initialiser designators and constructor member initialisers. References use EscapeReference too, so declaration and use stay in step.
  • Some words are left as written: this and base as receivers, the literals true, false and null, and a built-in type keyword at the start of a member access (int.MaxValue, string.Empty).
  • Contextual keywords (var, value, where, record, …) are legal names, so they aren't escaped.
  • Type declaration names (class, enum, alias) are out of scope. Type references are spelled through a separate static path, and escaping only the declaration would leave it out of step with its uses.

Tests

  • CSharp_KeywordNamesCompileAndResolve generates a class, an enum and free functions with keyword names, builds them with dotnet run against a driver, and checks the values that come back.
  • CSharp_ReferenceKeepsTheReceiverAndLiteralsAndEscapesTheMember checks that this.@base is escaped while null and string.Empty are left alone.
  • Both new tests fail with the generator change reverted and pass with it. The full suite passes: 1554 of 1554.

Generated by Claude Code

A field, property, parameter, local, method, enumeration member or
designated member named after a C# keyword came out verbatim, so the
file failed to compile: a parameter called in or out, or a property
called default, was enough. C# now writes a verbatim identifier
(@default), which renames nothing, at each declaration, and references
go through the same hook so they agree. this and base, the literals
true, false and null, and a built-in type's keyword leading a member
access (int.MaxValue, string.Empty) are left as written. Contextual
keywords (var, value, where, record) are legal names and are not
escaped.

Part of #106.

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

sonarqubecloud Bot commented Nov 3, 2026

Copy link
Copy Markdown

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.

2 participants