Skip to content

as i32 on a payload enum value returns the aggregate: invalid LLVM, or the variant printed instead of its discriminant #1770

Closed

Description

@ehartford

as i32 on an enum whose representation is an aggregate (any enum with a payload variant, with or without a representation type) is not lowered as a discriminant read. Through a parameter it happens to work; on a variant value it yields the enum aggregate itself.

Repro (stage1 of main 09a0efb)

enum R:
    A
    B(i32)
    C

fn main:
    print(f"{R.C as i32}")
    let a = R.C as i32
    print(a + 1)
Call parameter type does not match function signature!
i32 1
 %R = type { i32, [4 x i8] }  %17 = call { %R, i1 } @llvm.sadd.with.overflow.s_Rs(%R %.fca.1.3.insert151, i32 1)
===== INVALID LLVM FUNCTION main =====

With only the f-string line it compiles and prints C — the variant, not 2. enum Q: i32: with the same variants behaves the same. fn ra(r: R) -> i32: r as i32 returns the right tag (2; the discriminant under an explicit or @[flags] repr).

Where

MirLower.lower_cast emits RK_CAST(enum -> i32) (MIR: _3 = cast(copy _2 as ty3) with _2 = aggregate(kind=1, variant=2, fields=[])). CodegenDispatch's RK_CAST has no enum-to-integer arm: a struct-typed value falls to coerce_value_to_type (Codegen.w), whose last line returns the value unchanged — a silent fallback. MIR already has the operation: lower_enum_discriminant (RK_DISCRIMINANT, typed by enum_discriminant_type). Not yet observed in lldb.

Spec

§4.4a: "Casting: value as i32 extracts the underlying integer (identity cast)." For an enum with a payload the underlying integer is its discriminant (the tag).

Activity

  1. added 3 commits that reference this issue on Sep 28, 2026
    1867ae8
    3c85b87
    c2a566d
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