Skip to content

Sema: a lazy fallback returning a bare generic returns the join's type (#1378) - #1404

Merged
ehartford merged 1 commit into
mainfrom
fix-1378-unwrap-or-else-closure-type
Sep 23, 2026
Merged

ehartford merged 1 commit into
mainfrom
fix-1378-unwrap-or-else-closure-type

Conversation

@ehartford

Copy link
Copy Markdown
Contributor

Fixes #1378. Stacked on #1398 (branch fix-1363-unwrap-or-str), which rewrites the unwrap_or_else lowering this touches.

Root cause

rv(false).unwrap_or_else((_) => Vec.new()) died in codegen: BUG: closure result lacks LLVM type [src/CodegenDispatch.w:17259 Codegen.gen_closure].

  • with analyze repro.w explain:node:577325 (the closure), base stage1: type=fn(str) -> Vec, the uninstantiated generic. sema_type_to_llvm of it is 0.
  • The join itself was right: the payload Vec[i32] merged with the fallback's Vec to Vec[i32].
  • Exact line: the unwrap_or_else branch of option_combinator_return_type / result_combinator_return_type (src/SemaCheck.w) returns the join result without feeding it back to the closure. The closure is checked with no expected return (the fallback is a join input), so Vec.new() infers bare Vec. Two places kept it: the closure's function type, and the join's recorded lazy-arm type, which lower_unwrap_or_else_method uses as the call's result type.

Fix

Per §10 (unwrap_or_else[U]((fn(E) -> U)) -> Join[T, U]), once the join settles on Vec[i32], bare Vec can only mean Vec[i32].

  • resolve_contextual_join sets a LAZY_RESULT arm whose type is the bare generic base of the final type to the final type.
  • complete_lazy_fallback_result gives the closure the type fn(E) -> <join type>.

After the fix, the same query reads type=fn(str) -> Vec[i32] and the repro prints 0.

Matrix

test/debug_alloc/da_unwrap_or_else_bare_generic_fallback.w (leak count 0, exact stdout):

  • carrier and path: Result/Option × Err/None and Ok/Some
  • fallback: Vec, HashMap, Option
  • also: an annotated binding, a & call argument, a fallback that uses the Err payload, a loop

On the base stage1 it hits the same codegen BUG. Out of scope, and rejected consistently:

  • A user generic constructor (Box.new()) fails before the join with "cannot infer generic method type parameter", the same as in unwrap_or(Box.new()).
  • An Option[&Vec[i32]] with an owned fallback is rejected by D22 ("would need to copy").

Local evidence (iterate tier, stage1 from this branch)

  • with check src/main.w: ok
  • Every test/ fixture that uses unwrap_or_else passes, except test/non_compliant/d22/origin_unwrap_or_else_union.w. That one fails the same way on the base.

This changes typing only. It does not change drop scheduling or ownership lowering. Full battery not run, so this stays a draft.

…eturns the join's type (#1378)

Route: codegen crash — analyze facts for the closure node, then the Sema
branch that typed it.

`rv(false).unwrap_or_else((_) => Vec.new())` died in codegen:
`BUG: closure result lacks LLVM type: node=577325
[src/CodegenDispatch.w:17259 Codegen.gen_closure]`.

Evidence:
- `with analyze repro.w explain:node:577325` (the closure) on the base
  stage1: `type=fn(str) -> Vec` — the uninstantiated generic declaration.
  gen_closure's `sema_type_to_llvm(closure_fn_ret_tid)` is 0 for it (the
  closure body's return local is `local_type_ids[0]`, set from that type in
  MirLower.prepare_anonymous_body, whose `ret_ty == 0` check a bare base
  passes).
- The join itself was right: resolve_contextual_join merged the payload
  `Vec[i32]` with the fallback's `Vec` to `Vec[i32]` (the
  merge_contextual_owned_join_types base rule).

Exact line: SemaCheck option_combinator_return_type /
result_combinator_return_type, the `unwrap_or_else` branch, returns the
join result without feeding it back to the closure. The closure was checked
with no expected return (method_arg_expected_type: "the fallback result is
a join input"), so `Vec.new()` inferred the bare `Vec`, and nothing
completed it: the closure's function type and the join's recorded
lazy-arm type (which MirLower's lower_unwrap_or_else_method uses as the
call's result type) both stayed `Vec`.

Fix (§10: `unwrap_or_else[U]((fn(E) -> U)) -> Join[T, U]`): once the join
has settled on `Vec[i32]`, the bare `Vec` has one meaning left.
- resolve_contextual_join completes a LAZY_RESULT arm whose type is the bare
  generic base of the final type to the final type.
- complete_lazy_fallback_result gives the closure node the function type
  `fn(E) -> <join type>`, so MIR builds the body against it.
After: `explain:node:577325` reads `type=fn(str) -> Vec[i32]`; the repro
prints 0.

A user generic constructor (`Box.new()`) is not a bare-base arm: it is
rejected before the join ("cannot infer generic method type parameter"),
as it is in `unwrap_or(Box.new())`. An `Option[&Vec[i32]]` with an owned
`Vec.new()` fallback is rejected by D22 ("would need to copy a `Vec[i32]`").

Matrix (test/debug_alloc/da_unwrap_or_else_bare_generic_fallback.w, leak
count 0, exact stdout): Result/Option × Err/None and Ok/Some, Vec / HashMap
/ Option fallbacks, an annotated binding, a `&` call argument, a fallback
using the Err payload, a loop. Base stage1 (fix-1363-unwrap-or-str): the
same codegen BUG. The other unwrap_or_else fixtures (spec §10.5/§10.6,
d22 default joins, #1363's debug_alloc and behavior fixtures) pass on
stage1; test/non_compliant/d22/origin_unwrap_or_else_union.w fails the
same way on the base.

Typing change only: no drop scheduling or ownership lowering changed.

Fixes #1378.
@ehartford

Copy link
Copy Markdown
Contributor Author

Batch battery X2 (seed-driven, NEW seed, with :drop-audit/:move-audit) on main 977e667 + #1404 → #1418 → #1419 → #1425 (head ee4e2e4, including the #1404/#1418 join-arm merge resolution): build ok, :fixpoint ok, :drop-audit ok, :move-audit ok, :test ok (no reds), :test-green ok, :last-green ok. #1424's four repros run clean on this build. Branches hold exactly the tested chain; merge in order #1404, #1418, #1419, #1425 — then batch Y (#1405 … #1360), then #1428.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

unwrap_or_else((_) => Vec.new()) crashes codegen: "BUG: closure result lacks LLVM type"

1 participant