Skip to content

Closures: every capture is found, a stored view is refused, a call through &fn calls (§12.4, D62/D63; #1698) - #1701

Merged
ehartford merged 2 commits into
mainfrom
closure-soundness
Sep 26, 2026
Merged

ehartford merged 2 commits into
mainfrom
closure-soundness

Conversation

@ehartford

Copy link
Copy Markdown
Contributor

First slice of #1698. Fixes #1570, #1598, #1594, #1638, #1603 and the container form of #1566 (its direct-argument form was already diagnosed); #1567 and #1605 were verified fixed on main and closed.

  • expr_uses_symbol is exhaustive over every node kind with a loud fallthrough; MirLower's expr_mentions_symbol answers conservatively for unknown forms.
  • Index projection through a by-place capture loads the pointer first (D65: Sema's type decides, not the LLVM ptr).
  • A non-move closure with a by-place capture is an ephemeral value: stores into containers and literals propagate its origins; returning a literal holding one is refused.
  • &f typed &fn(A) -> R materializes the callable and references it.

Ownership/drop-scheduling change: alone in its battery with :drop-audit and :move-audit. Draft until the battery is posted.

Eric Hartford added 2 commits September 25, 2026 22:26
…rough &fn calls (§12.4, D62/D63; #1698: #1570 #1598 #1594 #1638 #1566 #1603)

Capture-set completeness. Sema's expr_uses_symbol was a whitelist with a
silent 0 for any node kind it did not name, so a local used only inside a
`.Variant(args)` shorthand, an `unsafe` block, a `move`/`copy` argument, a
multi-index, a `with` body or an asm operand was never captured and the
closure read a zero or its own return slot (#1570, #1598). The walk now
names every kind: the missing forms are walked, the leaves are listed, and
an unknown kind is an internal error, not a 0. MirLower's own
expr_mentions_symbol, which guards the string move-first rewrite, had the
same shape; an unknown form there now answers "may mention" so the rewrite
stays off.

Indexing through a by-place capture. Codegen's index projection keyed on the
LLVM `ptr` of the capture slot and treated `xs[1]` as raw-pointer indexing —
it printed the high half of the Vec's data pointer. Sema owns the meaning
(D65): only a raw-pointer Sema type is pointer indexing; a pointer-typed
slot whose Sema type is a Vec/str/slice loads through the pointer first, as
the field projection already did.

Stored views (#1594, #1638, #1566's container form). A non-`move` closure
holding a place of its frame is an ephemeral VALUE though `fn(A) -> R` the
type is not (D63), so expr_is_ephemeral_value says so, a struct/array/map
literal holding one is ephemeral, the #625 viral store (`fs.push(closure)`)
propagates the captures' origins onto the container, and a returned literal
holding one goes through the returned-ephemeral origin check. `fs.push(()
=> n * 10)` in a loop is now "view `fs` may originate from `n`, which no
longer lives here"; `Cnt { f: x => x + extra }` returned is "returned
ephemeral value may outlive its origin 'extra'"; the `move` spellings run.

Calls through `&fn(A) -> R` (#1603). `&shout` lowered through
lower_fn_address to the bare code pointer for every `&` op, so the callee's
`read.*(s)` dereferenced code and ran nothing (rc 138). When Sema typed the
expression `&fn`, the callable is materialized in a temp and the reference
points at it; raw-ref ops and a `&f` Sema typed as the callable itself keep
the code address.

Verified on a stage1 of this tree: the nine new tests pass, every existing
closure behavior/compile-error test is unchanged, and `check src/main.w`
reaches no unlisted node kind.
…nly a `&fn(A) -> R` expectation references the callable (#1603 follow-up)

The first cut materialized every `&f` Sema typed as a reference, so a raw C
function pointer (`let fp: *const fn(i64) -> i64 = &take_i64`, a struct
field, a call argument) received a pointer to a temp and the three fn-pointer
behavior tests segfaulted in the battery. The expected type decides: a raw
pointer expectation keeps the address.
@ehartford

Copy link
Copy Markdown
Contributor Author

Seed-driven battery at 9366af2 (seed nightly-20260925-local-1, WITH=src/main), alone with the drop/move audits, this host:

9366af26 closures /Users/eric/.local/with-staging/closures
closures build  rc=0
closures build :fixpoint rc=0
closures build :drop-audit rc=0
closures build :move-audit rc=0
closures build :test rc=0
closures build :test-green rc=0
closures build :last-green rc=0
closures build :user-programs-safe rc=0
BATTERY_DONE

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.

Sema: a closure does not capture a local used inside a .Variant(args) shorthand — () => .Some(n) reads 0/null (§12.4)

1 participant