rust-lang / rust-lang/rust

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

Open
#155,151 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-closures A-impl-trait A-patterns C-bug fixed-by-next-solver T-types
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the reproduction in the playground's src/lib.rs and run it on stable 1.94.1 and nightly 1.96 to confirm the diagnostic. Investigate closure capture and opaque-type handling in the old solver; done means the example compiles successfully without the E0381 error or the incorrect capture warning.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.