Skip to content

Silent wrong arm: match on a u8-repr discriminant enum picks the first arm for in-range values #1444

Closed

Description

@ehartford

Summary

match on a u8-repr discriminant enum selects the wrong arm for in-range values — silent wrong code. i32 repr is correct. (Distinct from #1003, which is about out-of-range discriminant values.)

Repro (origin/main 1c76062)

enum Kind: u8:
    Red = 1
    Blue = 2

fn kname(k: Kind) -> i32:
    match k:
        .Red => 10
        .Blue => 20

fn main:
    print(f"{kname(Kind.Blue)} {kname(Kind.Red)}")

Prints 10 10; expected 20 10. With str arms (.Red => "red", .Blue => "blue") kname(Kind.Blue) returns an empty string; the qualified pattern spelling Kind.Red => behaves the same. enum Kind: i32: prints 20 10. An if k == Kind.Red comparison on the u8 enum is correct, so the fault is in match lowering of a sub-i32 repr.

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