Skip to content

Escape keyword-named enum members in C++ and Rust [patch] - #365

Merged
matt-edmondson merged 1 commit into
mainfrom
fix/364-escape-enum-members
Nov 4, 2026
Merged

matt-edmondson merged 1 commit into
mainfrom
fix/364-escape-enum-members

Conversation

@matt-edmondson

Copy link
Copy Markdown
Contributor

Fixes #364

What changed

  • CppGenerator and RustGenerator now write each enumeration member through EscapeIdentifier. A member named public in C++ becomes public_, and one named type in Rust becomes r#type.
  • A member's value that names a keyword-named sibling is rewritten to the same spelling. In C++ that is Hidden = private_ + 1, and in Rust Other = Kind::r#type as isize + 1. Before this change the Rust path passed the sibling through unescaped.
  • LanguageGeneratorBase.EscapeReference now escapes the last word of a :: path, so Kind::type gives Kind::r#type and Access::public gives Access::public_. The words before the last :: are left as written, so path keywords like Self:: and crate:: are not escaped.

Tests

ReservedWordIdentifierTests has two new tests, Rust_KeywordNamedEnumMembersCompileAndResolve and Cpp_KeywordNamedEnumMembersCompileAndResolve. They declare keyword-named members, reference them through a :: path and from a sibling's value, compile the output with rustc and g++, and run a driver.

  • Without the source changes, both tests fail: rustc reports expected identifier, found keyword `type` and g++ reports expected identifier before 'public'.
  • With the changes, the full suite passes locally (1553 tests, 0 failures).

Generated by Claude Code

CppGenerator and RustGenerator wrote enumeration members verbatim, so a
member named `public` (C++) or `type` (Rust) produced output that failed
to compile. Both now write each member through EscapeIdentifier, and a
sibling's value that names it is rewritten to the same spelling.

EscapeReference also escapes the last word of a `::` path, so a
reference written as Kind::type or Access::public matches the
declaration. The words before it stay as written.

Fixes #364

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

Copy link
Copy Markdown

@matt-edmondson
matt-edmondson merged commit 982d073 into main Nov 4, 2026
16 checks passed
@matt-edmondson
matt-edmondson deleted the fix/364-escape-enum-members branch November 4, 2026 00:54
matt-edmondson pushed a commit that referenced this pull request Nov 4, 2026
Resolves the conflict in ReservedWordIdentifierTests.cs with #365, which
added its enum tests at the same place. Both sets of tests are kept.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017vfJW5wGJLhri3KXBeZJwH
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++ and Rust write keyword-named enum members unescaped (public, / type,), so the output fails to compile

2 participants