Skip to content

Backing-less enum with a payload variant: an explicit discriminant = N is parsed and silently dropped #1769

Closed

Description

@ehartford

A backing-less enum with a payload variant accepts an explicit discriminant = N and drops it: the enum stays a plain ADT enum whose tags are the variant indexes.

Repro (stage1 of main 09a0efb)

enum M:
    A = 5
    B(i32)

fn mt(m: M) -> i32: m as i32
fn main:
    print(mt(M.A))

Prints 0. No diagnostic.

Where

Parser.parse_enum_variants_block parses the = N (parse_disc_value_node) for every variant, but with a payload present it emits the ADT extras format [count, (name, pcount, payloads...)*], which has no discriminant slot, so the value node is never seen by Sema. The comment there says the discriminant is "meaningful only for fieldless enums".

Spec

§4.4a: "Enums can specify an integer representation type and explicit discriminant values for each variant. Discriminant enums use the enum keyword with a representation type"; "Discriminant enums with payloads" are shown with a representation type (enum Msg: i32:). A fieldless enum without one infers i32 (#309), and D71/#1482 makes an @[flags] one a discriminant enum in its default integer representation even with payloads. Whether an explicit = N on a backing-less payload enum makes it a discriminant enum in the inferred i32 (as @[flags] now does) or is an error naming the missing representation type is not written; silently dropping it is neither (No Silent Fallbacks).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions