Skip to content

Rust: generated enums derive nothing, so comparing two enum values fails (E0369) and a record holding an enum field fails (E0277) #255

Closed

Description

@matt-edmondson

What's wrong

RustGenerator.GenerateEnumDeclaration (Coder/Languages/RustGenerator.cs ~L1677-L1702) writes at most a #[repr(...)] and never a #[derive(...)]. Record structs, by contrast, get #[derive(Clone, Debug, PartialEq)] (~L591-L595), which requires every field type to implement those traits.

The comment at ~L414-L416 explains why plain structs derive nothing: one of their fields might not implement the traits. That reason can't apply to a fieldless C-like enum, which can always derive Clone, Copy, Debug, PartialEq, Eq, Hash.

RustGeneratedSourceCompilesTests declares CompiledExemplar.Colour() (~L503) but never compares it, copies it, or holds it in a record, so the compile test passes.

Failure scenarios (portable AST, accepted by the other six targets)

  1. bool same(Colour a, Colour b) { return a == b; }, built from VariableReferences and BinaryOperator.Equal, fails with E0369: binary operation == cannot be applied to type Colour.
  2. A ClassDeclaration { IsRecord = true } with a Colour field fails with E0277: Colour doesn't implement Clone/Debug/PartialEq, which the record's derive needs.
  3. A method reading an enum field by value from &self fails with E0507 (cannot move out of self.colour), because the enum isn't Copy.

Suggested fix

  • Write #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] on every generated enum, placed directly above the enum line, after the preamble and #[repr].
  • Extend RustGeneratedSourceCompilesTests with:
    • a function that compares two enum values
    • a record holding an enum field
    • a method returning an enum field from &self

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 Rust output fails to compile for ordinary uses of an enum: comparing values, holding one in a record, or reading one from &self. The failure is at compile time, so it isn't silent.
    • Area / suggested assignee: RustGenerator.GenerateEnumDeclaration and RustGeneratedSourceCompilesTests. Owner: @matt-edmondson
    • Duplicates / in progress: none found. No open PR covers it.
    • Next step: emit #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] on every fieldless enum. Extend the compile test with the three scenarios in the issue.

    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