Skip to content

Codegen: a by-place capture of a read receiver is a slot holding its address (#2025) - #2040

Merged
ehartford merged 1 commit into
impl-1930from
fix-2025
Oct 4, 2026
Merged

ehartford merged 1 commit into
impl-1930from
fix-2025

Conversation

@ehartford

@ehartford ehartford commented Oct 3, 2026 •

Copy link
Copy Markdown
Contributor

Stacked on #2034. Fixes #2025.

A closure in a read-mode fn method that reads the receiver segfaulted when called through a fn(..) parameter or a map stage (mut fn / move fn were fine).

Route (CLAUDE.md, codegen bug): lldb on the built repro stopped at the inlined __closure with EXC_BAD_ACCESS address=0x5 — the receiver's count field read as an address. --dump-mir showed the cause: the read receiver local is typed &C and marked [caller-place], and the closure body reads _1.*.count through its by-place capture. Codegen holds that receiver as the caller's place (mir_indirect_value_local_types), so mir_capture_source_ptr handed the closure the address of the C itself rather than a slot holding the &C.

Fix (codegen owns representation, D65): mir_capture_source_ptr gives a reference-typed indirect source a slot holding its address. Capture modes are unchanged, so this is independent of #2029's Sema capture record (that branch leaves this function as it is).

Evidence: new test/behavior/behav_closure_captures_read_receiver.w segfaulted (rc=139) on the #2034 stage1 and passes after the fix. It covers self.count, a bare count (§9.5), a |> map(it + count) stage, and the mut fn case. --debug-alloc reports leak count=0, --validate-all is ok, the 46 closure/capture behavior tests pass, :gate is green, and :drop-audit shows 254 cells with 0 regressions.

@ehartford
ehartford added this pull request to stack #2041 October 3, 2026 11:37
@ehartford ehartford changed the title fix 2025 Codegen: a by-place capture of a read receiver is a slot holding its address (#2025) Oct 3, 2026
@ehartford
ehartford removed this pull request from stack #2041 October 4, 2026 00:52
@ehartford
ehartford added this pull request to stack #2072 October 4, 2026 00:52
…address (#2025)

A read-mode `fn` receiver is typed `&Self` but held as the caller's
place, so mir_capture_source_ptr returned that place as the capture's
storage; the closure loaded the receiver's own bytes as the `&Self` and
faulted (lldb: EXC_BAD_ACCESS address=0x5, the receiver's count field, at
the inlined __closure). A reference-typed source gets a slot holding the
address, as any reference local has.
@ehartford
ehartford marked this pull request as ready for review October 4, 2026 14:20
@ehartford
ehartford merged commit 7218e85 into main Oct 4, 2026
@ehartford
ehartford deleted the fix-2025 branch October 4, 2026 14:21
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.

Closure capturing self in a read-mode fn method segfaults when called

1 participant