Skip to content

Codegen trap: .Some(v.get(0)) with v: &Vec[T] returning Option[&T] — the call result local is typed unit and aggregated into the Option #933

Closed

Description

@ehartford

Repro

fn f(v: &Vec[i32]) -> Option[&i32]: .Some(v.get(0))
fn main:
    print("ok")

with check passes. with build exits 133 (SIGTRAP) inside llvm::DataLayout::getTypeSizeInBits — the compiler itself, even though f is never called. Same with Vec[str] / Option[&str].

Not triggered by any of: .None; .Some(s) from a &str parameter; returning v.get(0) directly as &T; or binding first (let s = v.get(0) then .Some(s)).

Evidence (--dump-mir)

fn sym301(f) {
  locals:
    _0: ty334  // return
    _1: ty333  // v
    _2: ty14 [mut]          <- v.get(0) result: ty14 is Unit
    _3: ty334 [mut]
  bb0: {
    call const fn sym187(copy _1.*, const 0ty3) -> [return: _2, next: bb1];
  }
  bb1: {
    _3 = aggregate(kind=1, tag=0, fields=[copy _2]);   <- unit payload into Option[&i32]
    _0 = copy _3;
    return;
  }
}

The call node inside the variant constructor's argument gets no Sema expression type when the expected payload is &T (a let in between records one), so MirLower's temp for the call result defaults to unit and codegen asks LLVM to size a void payload. Two defects: the Sema typing hole for NK_ENUM_VARIANT arguments checked against an expected &T payload, and the silent unit default in MirLower where an untyped call result should be a loud phase bug (§No Silent Fallbacks).

Found while building the #925 acceptance matrix; a fixture defining this shape trapped the compiler before main ran.

Activity

  1. ehartford commented on Sep 2, 2026

    @ehartford
    ContributorAuthor

    Fixed in 784b429. Two defects, as filed:

    1. Sema's .Variant(args) shorthand branch recorded the enum type from the expected type and returned without checking the payload arguments, so a generic-method-call payload (v.get(0) on &Vec[T]) never got an instantiated type; MirLower defaulted the call result to unit, aggregated a void payload into the Option, and LLVM trapped sizing it. check_variant_shorthand_args now checks each payload against the variant's payload types with check_expr_with_expected, exactly as the bare Variant(args) call path does, on both the expected-type and the variant-lookup paths. As a consequence .Some(v.get(0).no_such()) is a real diagnostic instead of an abort.
    2. lower_call now fails loudly (phase bug naming the call) when a call result has no type at all, instead of lowering it as unit.

    Found and fixed on the way: the shorthand's MIR lowering never applied Sema's recorded adjustments, so .Some(v.get(0)) into Option[i32] compiled but stored the view's address and read back 0. It now applies them in lower_call_arg's order (auto-reference, contextual copy, auto-deref).

    Fixtures: err_variant_shorthand_payload_checked, behav_variant_shorthand_generic_payload (view, owned, str, and auto-referenced payloads). Battery with audits green; reseeded.

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