Repository navigation
Escape keyword-named identifiers in the Rust and C++ generators - #330
Open
matt-edmondson wants to merge 3 commits into
Open
matt-edmondson wants to merge 3 commits into
matt-edmondson wants to merge 3 commits into
Conversation
A property called Type or Default, or a field, parameter or local named after a keyword of the target, came out verbatim and failed to parse. Rust now writes a raw identifier (r#type), falling back to an underscore for self, Self, crate and super; C++ appends an underscore. Declarations and references go through the same hook, so they agree, and a leading receiver in a reference (self.x, this->x) is left as written. The base generator's reserved set is empty, so the other targets are unchanged. Part of #106. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_018tZnrna9oooMZHDaLyXumk
|
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.



Requested by Matt · project thread
Before: the Rust and C++ generators wrote every name as given. A property called
Typecame out aspub fn type(&self)in Rust, and one calledDefaultasint default() constin C++, both of which fail to parse. The same happened to fields, parameters, locals, designated initialisers and member initialisers namedwhere,match,loop,virtual,delete,newand the rest.After: each generator escapes a name that is a keyword of its target, where the name is declared and wherever it is referred to, so the two spellings agree.
r#type,r#match).self,Self,crateandsupercannot be raw identifiers, so they get an underscore instead (super_).default_,virtual_). C++ has no escape for a keyword, so this is a rename, and a caller of the generated header uses the renamed name.How:
LanguageGeneratorBasegains aReservedWordsset, anEscapeIdentifierhook (default: append_to a reserved word), andEscapeReference, which escapes each dotted segment of aVariableReferencewhile leaving a leading receiver (self,this, …) as written, soself.typebecomesself.r#type. The set is empty by default, so the other five generators are unchanged. Rust and C++ supply their keyword lists and route their declaration sites (fields, constants, functions, parameters, locals, member and designated initialisers, field-init shorthand) through the hook.ReservedWordIdentifierTestsadds four tests: two compile and run a struct with keyword-named fields, getters, parameters and locals throughrustc --edition 2021andg++ -std=c++20 -Wall -Wextra -pedantic, and two pin the Rust receiver and path-keyword rules. All four fail without the fix.Left for follow-ups, so this is part of the issue rather than all of it:
CallExpression.Calleeis verbatim text by design, so a call to a keyword-named getter has to be written already escaped by whoever builds the AST.Part of #106
Generated by Claude Code