You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
A closure returning a captured non-Copy local by value double-frees it (§2.3) #1481
Found while testing D60 closure tails; unrelated to assignments. Reproduced on origin/main 5848ae9 (stage1), run --debug-alloc:
fn main:
var c = "a".clone()
let f: fn() -> str = () => c
print(f())
prints a, then debug-alloc: DOUBLE FREE ... origin=unknown first_drop=drop#struct __drop_struct_16 second_drop=drop#struct __drop_struct_16.
The closure's body returns the captured str by value, and main still drops c at scope exit. Either the closure's return moves out of the capture (then the capture must be by move and c not dropped again, and a second call is use-after-move), or it is an error as returning a captured place by value; today it is neither. (§2.3: transport never produces a second live value from one.)
Stopped, not fixed, in the closure stack (#1583/#1584): the fix direction depends on a ruling the spec does not give.
What the spec says:
§12.3: "A closure is non-escaping if and only if it appears as a direct argument to a function call. All other closures are escaping." — let f: fn() -> str = () => c is escaping.
§12.4: "For non-Copy values, default capture is by place: the closure observes or mutates the original place according to its body." and "Invoking a closure is checked exactly like invoking a function: if a closure consumes, mutates, returns, or returns a view derived from a capture, those effects apply to the originating captured place." Its example is a let-bound (escaping by §12.3) closure captured by place: let f = || xs.push(1); f() // mutates xs.
The implementation (SemaCheck check_closure, "Escaping closures (including move closures) consume non-Copy captures") moves c into the closure at creation; codegen copies the bytes into the environment while main keeps and drops c. Bug: closure capture modes diverge from §12.4 #420's plan item 3 asked for exactly this ruling and it was never given.
By move at creation (today's sema rule): the closure owns c; f() moves it out of the environment; the second call must be rejected; main must not drop c (today it does — the double free); and the environment must be dropped when the closure value dies, which fn values (Copy) cannot do today.
Question for the ruling: does a let-bound (escaping) closure capture a non-Copy local by place or by move? §12.3 and the §12.4 example pull opposite ways.
Implemented per the §12.4 decision (let-bound closures capture non-Copy locals by place; a consuming call moves the capture out once, a second call is use-after-move): PR #1591 in stack #1585 (on #1583 → #1584). The earlier stop question above is resolved by that decision.
Activity
ehartford commentedon Sep 23, 2026
Stopped, not fixed, in the closure stack (#1583/#1584): the fix direction depends on a ruling the spec does not give.
What the spec says:
let f: fn() -> str = () => cis escaping.Copyvalues, default capture is by place: the closure observes or mutates the original place according to its body." and "Invoking a closure is checked exactly like invoking a function: if a closure consumes, mutates, returns, or returns a view derived from a capture, those effects apply to the originating captured place." Its example is a let-bound (escaping by §12.3) closure captured by place:let f = || xs.push(1); f() // mutates xs.cinto the closure at creation; codegen copies the bytes into the environment whilemainkeeps and dropsc. Bug: closure capture modes diverge from §12.4 #420's plan item 3 asked for exactly this ruling and it was never given.The two consistent fixes:
f()moves out ofcthrough the capture pointer and blanks it;cis consumed at the call, a secondf()is use-after-move,maindrops nothing. Returned/stored closures then need the environment-ownership answer (A closure returned from a function reads its environment from the dead callee frame:fn mk(x: i32) -> fn() -> i32: () => x * 10gives garbage once mk is not inlined (§12.2 escaping closures) #1567).c;f()moves it out of the environment; the second call must be rejected;mainmust not dropc(today it does — the double free); and the environment must be dropped when the closure value dies, whichfnvalues (Copy) cannot do today.Question for the ruling: does a let-bound (escaping) closure capture a non-Copy local by place or by move? §12.3 and the §12.4 example pull opposite ways.
ehartford commentedon Sep 23, 2026
Implemented per the §12.4 decision (let-bound closures capture non-Copy locals by place; a consuming call moves the capture out once, a second call is use-after-move): PR #1591 in stack #1585 (on #1583 → #1584). The earlier stop question above is resolved by that decision.
Sema/Codegen: every capture of a non-move closure is by place, Copy o…
Sema/Codegen: every capture of a non-move closure is by place, Copy o…
Sema/Codegen: every capture of a non-move closure is by place, Copy o…
Sema/Codegen: every capture of a non-move closure is by place, Copy o…