Skip to content

Sema: contextual generic enum arg (slot.set(Some(x))) typed as payload T, not Option[T] → codegen trap (§4.4) #669

Closed

Description

@ehartford

Spec reference

§4.4 enum variant shorthand / contextual enum construction: a variant expression in a context whose expected type is a known enum must type as the enum. This holds for the typed-binding spelling but not for the direct generic method-argument spelling.

Minimal repro (self-contained, checked)

fn run[T](x: T) -> T:
    let values: Vec[Option[T]] = Vec.new()
    let empty: Option[T] = None
    values.push(empty)
    with values.slot(0) as mut slot:
        slot.set(Some(x))
    values.get(0).unwrap()

fn main:
    assert(run(5) == 5)
    print("ok")

Expected vs actual

Expected: prints ok (as the typed-intermediate spelling does).

Actual (committed v0.15.1-era compiler and current main):

error: aggregate rvalue missing destination struct type fn=run__sema__3:37:4=3 count=1 dest_sema=3 current_ret=i32
error: code generation failed

Evidence chain (exact)

  • Codegen trap site: src/CodegenDispatch.w:3536 — fires because struct_ty == 0 for the aggregate destination.

  • MIR (--dump-mir on the repro, specialized body run__sema__3:37:4=3):

    _9: ty3 [mut]                                   // WRONG: ty3 = concrete T (i32)
    _9 = aggregate(kind=1, tag=0, fields=[copy _1]) // Some(x)
    call const fn sym14(copy _7, copy _9)           // slot.set(...)
    

    Contrast the typed-binding None in the same body: _5: ty232 (Option[i32]) — correct.

  • So Sema's contextual variant typing for a generic method-call argument records the payload type T instead of Option[T]; MirLower gives the aggregate temp that sema type; codegen resolves no struct type and traps with no source location.

Workaround in tree

lib/std/task.w (fallible await_all / await_any) uses typed intermediates:

let value: Option[T] = Some(result.unwrap())
slot.set(value)

Remove these when fixed.

Provenance

Pre-existing: reproduces on the committed release compiler and /tmp seed; unrelated to the #489 worktree slice.

Activity

  1. added 2 commits that reference this issue on Jul 15, 2026
  2. ehartford commented on Jul 15, 2026

    @ehartford
    ContributorAuthor

    Fixed in 6518916.

    Root cause (one level deeper than the original report): the trap was not generic-body-specific. Storing builtins (VecSlot.set, VecRange.set, SlotMapSlot.set, HashMapEntry.set, SlotMap.insert) published no expected argument type from Sema.method_expected_arg_type, so a contextual variant-constructor argument never learned its enum — in any context. The non-generic spelling slot.set(Some(5)) trapped identically (dest_sema=14 = void). The generic-body report was just the first place it was noticed.

    Fix: method_expected_arg_type now returns the stored element type for the storing-method family, mirroring the case list method_arg_stores_value already enumerates. This feeds both check-time contextual typing (check_expr_with_expected in the method-arg loop) and MirLower's intrinsic argument lowering, so the variant aggregate gets its real enum destination.

    Verified matrix (all ok post-fix; each trapped or was unexercised pre-fix): generic + non-generic VecSlot.set, VecRange.set value arg, SlotMapSlot.set, HashMapEntry.set, SlotMap.insert, plus the already-working controls (Vec.push, HashMap/HashSet.insert, or_insert, replace, plain generic fn args). Pinned by test/behavior/behav_contextual_enum_storing_args.w.

    The typed-intermediate workarounds in lib/std/task.w are removed; the #489 combinator fixtures and the drop-bearing-payload allocator differential are unchanged-green with the direct spelling.

    Split: Sender.send(Some(x)) shows the same symptom but a different mechanism (send is not a MirLower intrinsic; its argument lowers through the sig path, and the check-time expected observably never lands) — moved to #671 with the LLDB/MIR evidence.

    Gates on 6518916: build 0, FIXPOINT, audit:all violations=0, :test GATES EXIT 0, test-green/last-green recorded.

  3. added a commit that references this issue on Jul 15, 2026
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