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

Wrong error message with closures when variable moved #63405

Closed
shengsheng opened this issue Aug 9, 2019 · 3 comments
Closed

Wrong error message with closures when variable moved #63405

shengsheng opened this issue Aug 9, 2019 · 3 comments
Labels
A-closures Area: Closures (`|…| { … }`) A-diagnostics Area: Messages for errors, warnings, and lints C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@shengsheng
Copy link

shengsheng commented Aug 9, 2019

https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=c4b92f189e40cc2a6e8a6d13a24e4ee8

struct T(u32);

fn by_value(_: T) {}
fn by_ref(_: &T) {}
    
fn main() {
    let z = T(123);
    
    let closure = || {
        by_ref(&z);
        by_value(z);
    };
    
    closure();
    
    by_value(z);
}

Output:

   Compiling playground v0.0.1 (/playground)
error[E0382]: use of moved value: `z`
  --> src/main.rs:16:14
   |
7  |     let z = T(123);
   |         - move occurs because `z` has type `T`, which does not implement the `Copy` trait
8  |     
9  |     let closure = || {
   |                   -- value moved into closure here
10 |         by_ref(&z);
   |                 - variable moved due to use in closure
...
16 |     by_value(z);
   |              ^ value used here after move

error: aborting due to previous error

For more information about this error, try `rustc --explain E0382`.
error: Could not compile `playground`.

To learn more, run the command again with --verbose.

The move actually happen in line 11: by_value(z); not line 10.

Expect output:

11 |         by_value(z);
   |                  - variable moved due to use in closure
@jonas-schievink jonas-schievink added A-closures Area: Closures (`|…| { … }`) A-diagnostics Area: Messages for errors, warnings, and lints C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Aug 9, 2019
@shengsheng
Copy link
Author

Another example:

#[derive(Debug)]
struct V(u32);

fn main() {
    let a = Some(456);
    let v = V(123);
    
    a.map(|x| {
        let _ = x + v.0;
        drop(v);
    });
    
    println!("v: {:?}", v);
}

Output:

   Compiling playground v0.0.1 (/playground)
error[E0382]: borrow of moved value: `v`
  --> src/main.rs:13:25
   |
6  |     let v = V(123);
   |         - move occurs because `v` has type `V`, which does not implement the `Copy` trait
7  |     
8  |     a.map(|x| {
   |           --- value moved into closure here
9  |         let _ = x + v.0;
   |                     - variable moved due to use in closure
...
13 |     println!("v: {:?}", v);
   |                         ^ value borrowed here after move

error: aborting due to previous error

For more information about this error, try `rustc --explain E0382`.
error: Could not compile `playground`.

To learn more, run the command again with --verbose.

@shengsheng
Copy link
Author

Also confusing with move keyword:

#[derive(Debug)]
struct V(u32);

fn main() {
    let a = Some(456);
    let v = V(123);
    
    a.map(move |x| {
        let _ = x + v.0;
    });
    
    println!("v: {:?}", v);
}

Output:

   Compiling playground v0.0.1 (/playground)
error[E0382]: borrow of moved value: `v`
  --> src/main.rs:12:25
   |
6  |     let v = V(123);
   |         - move occurs because `v` has type `V`, which does not implement the `Copy` trait
7  |     
8  |     a.map(move |x| {
   |           -------- value moved into closure here
9  |         let _ = x + v.0;
   |                     - variable moved due to use in closure
...
12 |     println!("v: {:?}", v);
   |                         ^ value borrowed here after move

error: aborting due to previous error

For more information about this error, try `rustc --explain E0382`.
error: Could not compile `playground`.

To learn more, run the command again with --verbose.

@arora-aman
Copy link
Member

arora-aman commented Nov 6, 2020

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-closures Area: Closures (`|…| { … }`) A-diagnostics Area: Messages for errors, warnings, and lints C-bug Category: This is a bug. 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

3 participants