Skip to content

Rust and JavaScript: an enum value that shifts or negates a sibling member fails to compile (Perm::Write as isize << 1, --1) #396

Open

Description

@matt-edmondson

What's wrong

Rust

RustGenerator writes enum members (~L2150–2161 of Coder/Languages/RustGenerator.cs) by replacing each sibling reference through RenameSiblingMembers with {name}::{sibling} as {discriminant}, and adds no parentheses. The same file's WriteWidened (~L1427, L1458–1460) does parenthesise as, because, as its comment says, as binds tighter than any operator around it. The enum path breaks in both directions:

  • Shift: members Read = 1, Write = 2, All = "(Write << 1) - 1" (the common "all flags" idiom) generate
    All = (Perm::Write as isize << 1) - 1,
    rustc rejects this: "<< is interpreted as a start of generic arguments for isize, not a shift".
  • Negation: A = 1, B = "-A" generates B = -Perm::A as isize,. Rust parses that as (-Perm::A) as isize, which is E0600, "cannot apply unary operator - to type Perm".

Expected output: (Perm::Write as isize) << 1 and -(Perm::A as isize).

JavaScript

WriteEnumMembers (Coder/Languages/JavaScriptGenerator.cs ~L164–183) remembers each earlier member's value so later members can use it. A value that parses as a long is stored unparenthesised, and that includes negative numbers. So A = -1, B = "-A" generates B: --1,. That is a SyntaxError ("Invalid left-hand side expression in prefix operation"), so the module fails to load.

Suggested fix / acceptance criteria

  • Rust spells a sibling reference ({name}::{sibling} as {discriminant}).
  • JavaScript parenthesises a remembered value that is negative. For example, store it bare only when it parses and is non-negative.
  • Rust and JavaScript compile/load tests cover an enum with All = (Write << 1) - 1 and one with B = -A where A is negative.

Related: #258 (closed) introduced sibling renaming. #388 is the separate E0081 duplicate-discriminant problem.

Activity

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

    @matt-edmondson
    ContributorAuthor

    Triage


    Generated by Claude Code

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions