Skip to content

A closure returning a captured non-Copy local by value double-frees it (§2.3) #1481

Closed

Description

@ehartford

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.)

Activity

  1. ehartford commented on Sep 23, 2026

    @ehartford
    ContributorAuthor

    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.

    The two consistent fixes:

    1. By place (§12.4 text + example): f() moves out of c through the capture pointer and blanks it; c is consumed at the call, a second f() is use-after-move, main drops 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 * 10 gives garbage once mk is not inlined (§12.2 escaping closures) #1567).
    2. 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.

  2. ehartford commented on Sep 23, 2026

    @ehartford
    ContributorAuthor

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions