Skip to content

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

Closed

Description

@matt-edmondson

What's wrong

The docs for EnumMember.Value (Coder/Ast/EnumMember.cs:28-36) say a value may be "a literal, another member, an expression over them". The C, Go, Rust and JavaScript generators copy the value text verbatim, but they rename or scope the members it refers to. The generated reference then points at a name that doesn't exist.

Reproduction

Use enum Perm with members Read = 1, Write = 2, ReadWrite = "Read | Write" and Next (no value), generated inside a SourceFile. Each output below was checked with the real compiler or runtime.

Language Generated Result
C Perm_ReadWrite = Read | Write (CGenerator.cs:990-997; members renamed by SpellEnumMember at :1018-1022) gcc: 'Read' undeclared
Go PermReadWrite Perm = Read | Write (GoGenerator.cs:1503-1515; Prefixed renames constants, not the value) undefined: Read
Rust ReadWrite = Read | Write (RustGenerator.cs:1696-1698) E0425 cannot find value Read
JavaScript ReadWrite: Read | Write and Next: (Read | Write) + 1 inside Object.freeze({...}) (JavaScriptGenerator.cs:159, LanguageGeneratorBase.EnumMemberValues at :1296) ReferenceError: Read is not defined on load

C#, C++ and Python already compile and run correctly. In Python, ReadWrite is 3 and Next is 4.

Suggested fix

When writing a member's value, replace whole-word references to sibling members with the name the generator gave those members:

  • C: Perm_Read
  • Go: PermRead
  • Rust: Perm::Read as isize. rustc accepts ReadWrite = Perm::Read as isize | Perm::Write as isize.
  • JavaScript: either substitute the values already computed by EnumMemberValues, or declare the members as consts before the frozen object.

The renaming is the generator's own, so carrying it into the values can't conflict with any user names.

Acceptance criteria

  • The Perm enum above compiles or runs in the C, Go, Rust and JavaScript compile tests.
  • ReadWrite == 3 and Next == 4 in each of those languages.

Activity

  1. added
    bug
    on Oct 18, 2026
  2. matt-edmondson commented on Oct 18, 2026

    @matt-edmondson
    ContributorAuthor

    Triage

    • Category: Bug
    • Priority: Medium. The form EnumMember.Value documents (a value that refers to another member) breaks the C, Go and Rust compiles and makes the JavaScript throw on load. It only affects enums whose values reference siblings, and C#, C++ and Python are correct.
    • Area / suggested assignee: the enum-member value writers in CGenerator, GoGenerator, RustGenerator and JavaScriptGenerator (LanguageGeneratorBase.EnumMemberValues). Owner: @matt-edmondson
    • Duplicates / in progress: not a duplicate, and no open PR covers it. It's the same family as Identifiers that are target-language keywords are emitted verbatim: a property named Type or Default generates fn type(...) (Rust) / int default() (C++) that fails to compile #106 (identifiers emitted verbatim), but the cause is separate: the generator renames a member and the value text doesn't follow the rename.
    • Next step: rewrite whole-word sibling references in the value using the generator's own member spelling. For JavaScript, substitute the values EnumMemberValues already computes. Add the Perm enum to the compile tests.

    Generated by Claude Code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions