Skip to content

Field within opaque type is not captured by closure under old solver #155151

Description

@Zalathar

I tried this code:

pub fn wut() -> impl Sized {    
    struct Foo { x: u32 }

    if false {
        let foo = wut();
        let _closure = move || {
            // let foo = foo; // Works if this is uncommented
            let Foo { x } = foo;
            let _y = x;
        };
    }
    
    Foo { x: 7 }
}

(Thanks to @theemathas for a minimization not requiring TAIT.)

I expected to see this happen: Compiles successfully.

Instead, this happened:

   Compiling playground v0.0.1 (/playground)
error[E0381]: used binding `x` isn't initialized
 --> src/lib.rs:9:22
  |
8 |             let Foo { x } = foo;
  |                       - binding declared here but left uninitialized
9 |             let _y = x;
  |                      ^ `x` used here but it isn't initialized

warning: value captured by `foo.x` is never read
 --> src/lib.rs:8:29
  |
8 |             let Foo { x } = foo;
  |                             ^^^
  |
  = help: did you mean to capture by reference instead?
  = note: `#[warn(unused_assignments)]` (part of `#[warn(unused)]`) on by default

For more information about this error, try `rustc --explain E0381`.
warning: `playground` (lib) generated 1 warning
error: could not compile `playground` (lib) due to 1 previous error; 1 warning emitted

Meta

(Tested on playground with stable 1.94.1 and nightly 1.96.)

Activity

  1. added
    needs-triageThis issue may need triage. Remove when done. See docs forge.rust-lang.org/release/issue-triaging
    on Apr 11, 2026
  2. Zalathar commented on Apr 11, 2026

    @Zalathar
    MemberAuthor

    The failure only occurs under edition 2021 or later (since it requires precise-capture), and does not occur under -Znext-solver.

  3. Zalathar commented on Apr 11, 2026

    @Zalathar
    MemberAuthor

    Triggering this issue without TAIT requires some pretty contrived code, but with TAIT it's a lot more plausible:

    fn upvar() {
    #[derive(Copy, Clone)]
    struct Foo((u32, u32));
    type T = impl Copy;
    let foo: T = Foo((1u32, 2u32));
    let x = move || {
    let Foo((a, b)) = foo;
    };
    }

    This existing test currently doesn't trigger the bug, but actually using a or b in the closure would produce a similar error.

  4. theemathas commented on Apr 11, 2026

    @theemathas
    Contributor

    Possibly related to #119786. cc @meithecatte

  5. added
    A-closuresArea: Closures (`|…| { … }`)
    A-impl-traitArea: `impl Trait`. Universally / existentially quantified anonymous types with static dispatch.
    A-patternsRelating to patterns and pattern matching
    T-typesRelevant to the types team, which will review and decide on the PR/issue.
    fixed-by-next-solverFixed by the next-generation trait solver, `-Znext-solver`.
    and removed
    needs-triageThis issue may need triage. Remove when done. See docs forge.rust-lang.org/release/issue-triaging
    on Apr 11, 2026
  6. Zalathar commented on Apr 12, 2026

    @Zalathar
    MemberAuthor

    For additional background, I noticed the issue while looking into situations where this code fails to create a binding for a PatKind::Binding node, because place is None:

    // Then push this binding, after any bindings in the subpattern.
    if let Some(source) = place {
    extra_data.bindings.push(super::SubpatternBindings::One(super::Binding {
    span: pattern.span,
    source,
    var_id: var,
    binding_mode: mode,
    is_shorthand,
    }));
    }

  7. added a commit that references this issue on Apr 14, 2026
  8. added a commit that references this issue on Apr 15, 2026
  9. added a commit that references this issue on Apr 15, 2026
  10. added a commit that references this issue on Sep 20, 2026
    f47f080
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

    A-closuresArea: Closures (`|…| { … }`)A-impl-traitArea: `impl Trait`. Universally / existentially quantified anonymous types with static dispatch.A-patternsRelating to patterns and pattern matchingC-bugCategory: This is a bug.T-typesRelevant to the types team, which will review and decide on the PR/issue.fixed-by-next-solverFixed by the next-generation trait solver, `-Znext-solver`.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions