Repository navigation
Sema: an element view keeps its origin through joins, carriers and projections (#1406) - #1459
Merged
ehartford merged 1 commit intoSep 23, 2026
Conversation
…ojections; only the body block's tail is a return (#1406) `let x = if c: v[0] else: v[1]` then `v.push(...)` while `x` was live compiled and read freed memory (debug allocator with scribble prints an empty line for "a"). Route: wrong ownership verdict, lldb on stage2. Root cause. check_index records an element view's origins on the index node (SemaCheck.w record_view_producer_origins at the Vec/array/slice branches: the base's own origins, else the base's root `v`), but collect_expr_view_deps grouped NK_INDEX with field access and only recursed into the base (SemaCheck.w:10194), which finds nothing for an owned `v`. The recorded origin was dropped wherever the view flowed, and only a bare `let x = v[i]` was rescued by the #887 fallback. lldb on with-stage2 check of the issue repro: SemaCheck.w:10416 is entered for `x` (deps empty, mask 0, view-bound) and 10423 is never reached, because the peeled initializer is NK_IF_EXPR; for `let x = v[0]` 10423 and 10425 are hit and the push is rejected. Fix. collect_expr_view_deps reads the index node's recorded origins (§3.4: construction, projection, elimination and control-flow joins preserve the origins of every view that flows into the value; §3.8 join rule 3; §21.1 rule 10). That exposed the block-tail check treating every block's ref-typed tail as a return (`let x = if c: { let i = 0; &v[i] } else: &v[1]` was already rejected with "returned view may outlive its origin 'v'"): check_block now runs the return check only for the body block (body_tail_block); an inner block's tail is rejected only for an origin declared inside that block ("view may outlive its origin 'w', which is dropped at the end of this block"). Matrix (19 join/carrier/projection shapes, 12 return/escape shapes; base = stage2 of 1c76062, new = stage1 with the fix). Newly rejected, all accepted on base: if, match (incl. an arm indexed by a pattern binding), two vecs, join mixed with a `&str` parameter, arm block tail, grouped, else-if chain, field element `s.items[i]`, `v[0] = ...`, `v.clear()`, moving `v`, loop pushes, Copy element (`Vec[i32]`), tuple `(v[0], v[1])` (bound and destructured), `Some(v[0])` through a join, `o ?? v[0]`, `o.unwrap_or(v[0])`, `var x` reassigned to a join, `v[0].name`, owned array `a[0]`. Returns now rejected that base accepted and that dangle: `let v = mkv(); if c: v[0] else: v[1]` from `-> &str`, the same through an arm block, through `return`, and from a consumed `v: Vec[str]`. Still accepted: use-then-mutate (NLL) for if and match, clone joins, owned `i32` join, arithmetic join, `str` byte join, `&Vec` parameter joins, inner block tails of outer bindings (previously a false error for `&v[i]`). The two sites in MirLower that viewed `self.defer_nodes[i]` across a `mut fn` call on `self` were accepted only because MirBuilder is ephemeral and its parameter mask hid the element root; they now pass the element directly, as the identical non-ephemeral shape already required. Stage1 check of src/main.w: ok. Fixtures: 11 compile_errors, behav_join_element_view_origins (exact stdout), da_join_element_view (leak count=0).
ehartford
force-pushed
the
fix-1406-join-view-origin
branch
from
September 23, 2026 09:57
7e13eb7 to
dcddc3b
Compare
ehartford
changed the base branch from
main
to
fix-1362-bundle-internals-scope
September 23, 2026 09:58
This was referenced Sep 23, 2026
Contributor
Author
|
Stack #1466 battery (seed-driven, new seed, cold c_import cache, with :drop-audit/:move-audit) on main 1c76062 + #1418 → #1419 → #1425 → #1428 → #1449 → #1377 → #1459 → #1460 → #1461 → #1462 (head cf36211, including the #1449/#1462 Codegen resolution and the fixed #1460): build ok, :fixpoint ok, :drop-audit ok, :move-audit ok, :test ok (no reds), :test-green ok, :last-green ok. Each branch holds exactly its tested commits on the layer below. Merge the whole stack: |
ehartford
marked this pull request as ready for review
September 23, 2026 09:58
This was referenced Sep 23, 2026
Merged
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.
Closes #1406. Bottom of stack #1463 (#1459 → #1460 → #1461 → #1462).
Bug.
let x = if c: v[0] else: v[1]followed byv.push(...)whilexwas live compiled and read freed memory (debug allocator with scribble:print(x)prints an empty line).Root cause (lldb on stage2).
check_indexrecords an element view's origins on the index node (record_view_producer_origins: the base's origins, else its rootv), butcollect_expr_view_depshandledNK_INDEXlike a field access and only recursed into the base (SemaCheck.w:10194), which finds nothing for an ownedv. Only a barelet x = v[i]was rescued, by the #887 fallback. For the join, SemaCheck.w:10416 is entered forx(deps empty) and 10423 is never reached (the initializer isNK_IF_EXPR).Fix.
collect_expr_view_depsreads the index node's recorded origins (§3.4, §3.8 join rule 3, §21.1 rule 10). That exposed the block-tail check treating every block's tail as a return (let x = if c: { let i = 0; &v[i] } else: &v[1]was already a false "returned view may outlive its origin"). Only the body block's tail is now a return; an inner block rejects only origins declared inside it. Two MirLower sites viewedself.defer_nodes[i]across amut fncall onself; they were accepted only because MirBuilder is ephemeral, and now pass the element directly.Matrix. 19 join/carrier/projection shapes are now rejected (all accepted on base): if, match (including a pattern-bound index), two vecs, mixed with a
&strparam, arm block, grouped, else-if,s.items[i],v[0] = …,v.clear(), movingv, loop pushes, Copy elements, tuple,Some(v[0]),??,unwrap_or,varreassign,v[0].name, owned arrays. Four dangling returns base accepted are now rejected. The NLL, clone, owned-Copy, arithmetic and&Vec-param shapes still compile.Fixtures. 11 compile_errors,
behav_join_element_view_origins(exact stdout),da_join_element_view(leak count=0).Checks run. Stage1 check of src/main.w passes. A check sweep over lib/, tools/, build/ and examples/ plus 292 view-related tests, base vs new, shows only the intended new rejections. Stage1-only lib/std diffs match a base stage1 (#1116). Drop and move audits ran on the stack top; see #1462.
Draft until the batch battery is green.