Skip to content

Sema: an element view keeps its origin through joins, carriers and projections (#1406) - #1459

Merged
ehartford merged 1 commit into
fix-1362-bundle-internals-scopefrom
fix-1406-join-view-origin
Sep 23, 2026
Merged

ehartford merged 1 commit into
fix-1362-bundle-internals-scopefrom
fix-1406-join-view-origin

Conversation

@ehartford

@ehartford ehartford commented Sep 23, 2026 •

Copy link
Copy Markdown
Contributor

Closes #1406. Bottom of stack #1463 (#1459 → #1460 → #1461 → #1462).

Bug. let x = if c: v[0] else: v[1] followed by v.push(...) while x was live compiled and read freed memory (debug allocator with scribble: print(x) prints an empty line).

Root cause (lldb on stage2). check_index records an element view's origins on the index node (record_view_producer_origins: the base's origins, else its root v), but collect_expr_view_deps handled NK_INDEX like a field access and only recursed into the base (SemaCheck.w:10194), which finds nothing for an owned v. Only a bare let x = v[i] was rescued, by the #887 fallback. For the join, SemaCheck.w:10416 is entered for x (deps empty) and 10423 is never reached (the initializer is NK_IF_EXPR).

Fix. collect_expr_view_deps reads 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 viewed self.defer_nodes[i] across a mut fn call on self; 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 &str param, arm block, grouped, else-if, s.items[i], v[0] = …, v.clear(), moving v, loop pushes, Copy elements, tuple, Some(v[0]), ??, unwrap_or, var reassign, 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.

@ehartford
ehartford added this pull request to stack #1463 September 23, 2026 08:41
@ehartford ehartford changed the title fix 1406 join view origin Sema: an element view keeps its origin through joins, carriers and projections (#1406) Sep 23, 2026
…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

Copy link
Copy Markdown
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: gh stack merge 1462 --squash.

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.

Element views through an if/match join lose their origin: mutating the Vec while the joined view is live is accepted (use-after-free)

1 participant