Repository navigation
Sema: a lazy fallback returning a bare generic returns the join's type (#1378) - #1404
Merged
Merged
Conversation
ehartford
force-pushed
the
fix-1363-unwrap-or-str
branch
from
September 23, 2026 02:37
b1f82c6 to
55fc9e8
Compare
ehartford
force-pushed
the
fix-1363-unwrap-or-str
branch
from
September 23, 2026 03:42
55fc9e8 to
dab175b
Compare
…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
force-pushed
the
fix-1378-unwrap-or-else-closure-type
branch
from
September 23, 2026 04:36
3a27144 to
e4fd257
Compare
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. |
ehartford
marked this pull request as ready for review
September 23, 2026 04:36
This was referenced Sep 23, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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_llvmof it is 0.Vec[i32]merged with the fallback'sVectoVec[i32].unwrap_or_elsebranch ofoption_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), soVec.new()infers bareVec. Two places kept it: the closure's function type, and the join's recorded lazy-arm type, whichlower_unwrap_or_else_methoduses as the call's result type.Fix
Per §10 (
unwrap_or_else[U]((fn(E) -> U)) -> Join[T, U]), once the join settles onVec[i32], bareVeccan only meanVec[i32].resolve_contextual_joinsets a LAZY_RESULT arm whose type is the bare generic base of the final type to the final type.complete_lazy_fallback_resultgives the closure the typefn(E) -> <join type>.After the fix, the same query reads
type=fn(str) -> Vec[i32]and the repro prints0.Matrix
test/debug_alloc/da_unwrap_or_else_bare_generic_fallback.w(leak count 0, exact stdout):&call argument, a fallback that uses the Err payload, a loopOn the base stage1 it hits the same codegen BUG. Out of scope, and rejected consistently:
Box.new()) fails before the join with "cannot infer generic method type parameter", the same as inunwrap_or(Box.new()).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: okunwrap_or_elsepasses, excepttest/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.