Skip to content

Rename member references in C, Go, Rust and JavaScript enum values [patch] - #265

Merged
matt-edmondson merged 1 commit into
mainfrom
fix/258-enum-sibling-references
Oct 18, 2026
Merged

matt-edmondson merged 1 commit into
mainfrom
fix/258-enum-sibling-references

Conversation

@matt-edmondson

Copy link
Copy Markdown
Contributor

Fixes #258

Before: EnumMember.Value is documented as allowing a reference to another member (ReadWrite = Read | Write). The C, Go, Rust and JavaScript generators copied that text as-is, but each of them renames or scopes its members. The output therefore referred to Read, which was never declared:

  • C: gcc reports 'Read' undeclared
  • Go: undefined: Read
  • Rust: rustc E0425
  • JavaScript: ReferenceError on load

After: the new helper LanguageGeneratorBase.RenameSiblingMembers rewrites each whole-word reference to a sibling member using the name that generator gave the member:

  • C: Perm_Read, via SpellEnumMember
  • Go: PermRead, via Prefixed
  • Rust: Perm::Read as i32, cast to the #[repr] type, or to isize when no representation is fixed
  • JavaScript: the value already written for that member, because an object literal can't read its own properties. ReadWrite: 1 | 2, Next: (1 | 2) + 1

A word that only contains a member's name (ReadMask) is left alone, as is a reference someone already qualified (Other.Read, X::Read). C#, C++ and Python already handled this correctly and are unchanged.

Tests: EnumSiblingReferenceTests builds and runs the issue's Perm enum with gcc, go run, rustc (with #[repr(i32)] and with no repr) and node, and checks that the output is ReadWrite == 3, Next == 4. It also has one unit test for the qualified-reference and longer-word cases. With the generator changes reverted, all 6 tests fail with the errors the issue lists. With the changes, all pass. Full suite: 1289 passed, 0 failed.

Note: #263 also edits RustGenerator.GenerateEnumDeclaration (it adds the #[derive] line). The hunks are next to each other but don't overlap.


Generated by Claude Code

…atch]

An enum member's value may name another member (ReadWrite = Read | Write),
but C and Go prefix their members, Rust scopes them and reads a variant
as a value only through a cast, and a JavaScript object literal cannot
read its own properties. The value text was copied verbatim, so the
reference named something that was never declared. Whole-word sibling
references are now rewritten to the generator's own spelling:
Perm_Read in C, PermRead in Go, Perm::Read as i32 (or isize) in Rust,
and the referenced member's value in JavaScript.

Fixes #258

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

Copy link
Copy Markdown

@matt-edmondson
matt-edmondson merged commit 54c36bd into main Oct 18, 2026
16 checks passed
@matt-edmondson
matt-edmondson deleted the fix/258-enum-sibling-references branch October 18, 2026 12:33
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.

Enum member values that name other members don't compile in C, Go, Rust or JavaScript

2 participants