Repository navigation
Escape keyword-named identifiers in the C# generator [patch] - #441
Merged
Merged
Conversation
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
|
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.



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
inoroutwas enough to break it (int total(int in, int out)).Change
CSharpGeneratornow declares C#'s reserved keywords asReservedWords. It overridesEscapeIdentifierto write a verbatim identifier (@default), so nothing gets renamed and a C# caller uses the same name.EscapeReferencetoo, so declaration and use stay in step.thisandbaseas receivers, the literalstrue,falseandnull, and a built-in type keyword at the start of a member access (int.MaxValue,string.Empty).var,value,where,record, …) are legal names, so they aren't escaped.Tests
CSharp_KeywordNamesCompileAndResolvegenerates a class, an enum and free functions with keyword names, builds them withdotnet runagainst a driver, and checks the values that come back.CSharp_ReferenceKeepsTheReceiverAndLiteralsAndEscapesTheMemberchecks thatthis.@baseis escaped whilenullandstring.Emptyare left alone.Generated by Claude Code