Skip to content

Type.from_int on a discriminant enum with payload variants: what does it return? (Option[Type] cannot hold a payload variant) #1497

Closed

Description

@ehartford

Summary

Type.from_int(n) on a discriminant enum whose variants carry payloads returns the repr's Option — Some(7) — because no Msg value exists for a payload variant's discriminant. The spec says from_int returns Option[Type], which a payload variant cannot satisfy. Needs a ruling.

Repro (installed nightly v0.15.2.1; origin/main stage1)

enum Msg: i32:
    Quit = 0
    Move(i32, i32) = 7

fn main:
    let o = Msg.from_int(7)
    match o:
        Some(v) => print(v)
        None => print("none")

Prints 7 (o: Option[i32]).

Spec text

§4.4a: "Conversion: Type.from_int(n) converts an integer to Option[Type], returning .None for values that don't match any defined discriminant". §4.4a also allows payload variants with explicit discriminants ("Discriminant enums with payloads: Variants can carry associated data just like regular enums, combined with explicit discriminant values").

The question

For an enum with payload variants: is from_int rejected (it cannot build a payload), or Option[Type] that is Some only for payloadless variants and None for a payload variant's discriminant, or something else? The #1453 fix makes from_int an Option[Type] for fieldless discriminant enums (the spec's Color.from_int(2) example) and leaves the payload case as it was until this is ruled.

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions