Skip to content

A closure passed to a call is judged by its capture facts, against the receiver and the caller's live views (#615) - #3451

Merged
ehartford merged 3 commits into
mainfrom
closure-capture-facts
Nov 19, 2026
Merged

ehartford merged 3 commits into
mainfrom
closure-capture-facts

Conversation

@ehartford

Copy link
Copy Markdown
Contributor

Part of #615 (D218). It closes a use-after-free in safe code and does not close the issue: the refusal of a read-only capture of a mut receiver, and the closure type with a mut parameter that its fix-it needs, come with spec words.

The hole. A callee holds a view into self.items across the call of its closure; the closure pushes to x.items; the list reallocates and the view reads freed memory (it printed 0 for an element that was 7). The same through a read-only receiver, a &T parameter of a free function, and a view the caller holds across a call that takes a writing closure.

Two causes.

  • The same-call check and the argument closure's borrow asked a syntax scan whether the closure writes its capture; the scan missed a mutating call through a field (y.items.push(1)). Both now read the capture summary the body's own check computed (D65). The method-call path passes the receiver, which it never did.
  • check_mutation_against_views removed a borrow it judged dead while checking a closure body, where a view of the enclosing function has no later use. Those borrows now survive the body (closure_body_borrow_floor), and the closure's writes are judged where it is passed (check_capture_write_against_views).

Still accepted: a closure that only reads the place another argument borrows; one that writes a field while a view of a sibling field is live; one that writes a place nothing else holds.

Also: the error cites §3.2 (it said §15.7); WITH_DEBUG_BORROWS traces the closure's captures; tools/verdict.w reads the battery's stage reds (RED (census): … printed green); Zipped.next drops a redundant Some the census counted once the compiler used zip.

Allowance: index-loop in src 1561 → 1562: the new check walks the borrow table, parallel lists read by one index.

Five tests (four refusals, one for what stays legal). Battery green on the branch tip.

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.

1 participant