Skip to content

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

@Zalathar

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

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
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions