Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect explanation for upvar move in FnMut closure #100896

Open
digama0 opened this issue Aug 23, 2022 · 0 comments
Open

Incorrect explanation for upvar move in FnMut closure #100896

digama0 opened this issue Aug 23, 2022 · 0 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@digama0
Copy link
Contributor

digama0 commented Aug 23, 2022

Given the following code (playground):

fn use_mut(_: impl FnMut()) {}
fn main() {
    let mut upvar = Some(String::new());
    use_mut(move || {
        || {
            || {
                upvar.take();
                if let Some(_s) = upvar {}
            };
        };
    });
}

The current output is:

error[E0507]: cannot move out of `upvar`, a captured variable in an `FnMut` closure
  --> src/main.rs:5:9
   |
3  |       let mut upvar = Some(String::new());
   |           --------- captured outer variable
4  |       use_mut(move || {
   |  _____________-
5  | |         || {
   | |         ^^ move out of `upvar` occurs here
6  | |             || {
7  | |                 upvar.take();
   | |                 -----
   | |                 |
   | |                 variable moved due to use in closure
   | |                 move occurs because `upvar` has type `Option<String>`, which does not implement the `Copy` trait
...  |
10 | |         };
11 | |     });
   | |_____- captured by this `FnMut` closure

The error is correct, but the diagnostic points to upvar.take() as the reason why the value is moved, while the real culprit is the match expression if let Some(_s) = upvar {}, which is not indicated.

Occurs in 1.63.0 stable and 1.65.0-nightly (2022-08-16 86c6ebee8fa0a5ad1e18).

@digama0 digama0 added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Aug 23, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

1 participant