Skip to content

Non-move closure capturing a local by reference, stored in a returned struct field: garbage / segfault, no diagnostic #1638

Closed

Description

@ehartford

Found while verifying #1635 (the alias-call fix) under --debug-alloc. Independent of it: the DIRECT call c.f(1) is wrong too, on the pinned seed as well as a stage1 built from main + the #1635 fix.

Repro

type Cnt { name: str, f: fn(i32) -> i32 }
fn mk(k: i32) -> Cnt:
    let extra = k
    Cnt { name: "n", f: x => x + extra }
fn direct(c: &Cnt) -> i32: c.f(1) + c.f(2)
fn main:
    let c = mk(100)
    print(direct(&c))

Expected: either 203, or a Sema diagnostic that the closure captures extra by reference and escapes its frame (§12.4 / closure capture rules).
Actual: prints garbage (1033 on a main stage1, -189500189 on the pinned seed); with more calls (let r = &c.f; r(1)) rc=139. --debug-alloc is silent (the dangling capture is a stack slot, not a heap block).

Spelling the closure move x => x + k gives 203 through every call shape (direct, let r = &c.f, let r = c.f, owned), leak count=0.

Why it matters

A non-move closure that captures a local by reference and is stored into a returned aggregate is a use-after-return that neither Sema nor the allocator catches. Either the escape must be diagnosed (a view into the frame cannot leave it — the same rule §21.1 applies to returned &T), or the capture must be by copy for a Copy local. Which one is Eric's ruling; the compiler must not stay silent.

Activity

  1. ehartford commented on Sep 26, 2026

    @ehartford
    ContributorAuthor

    Fixed on main by #1701 (3d5dc47): capture-set completeness, stored-view refusal, calls through &fn; battery green and pinned by the tests named in the PR.

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