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

Nonsensical suggestion when trying to move value in 'for' expression into closure #64559

Closed
Aaron1011 opened this issue Sep 17, 2019 · 0 comments · Fixed by #64642
Closed

Nonsensical suggestion when trying to move value in 'for' expression into closure #64559

Aaron1011 opened this issue Sep 17, 2019 · 0 comments · Fixed by #64642
Labels
A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix`. C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@Aaron1011
Copy link
Member

This code:

fn main() {
    let orig = vec![true];
    for _val in orig {}
    let _closure = || orig;
}

gives the following error:

error[E0382]: use of moved value: `orig`
 --> src/main.rs:4:20
  |
2 |     let orig = vec![true];
  |         ---- move occurs because `orig` has type `std::vec::Vec<bool>`, which does not implement the `Copy` trait
3 |     for _val in orig {}
  |                 ----
  |                 |
  |                 value moved here
  |                 help: consider borrowing to avoid moving into the for loop: `&||`
4 |     let _closure = || orig;
  |                    ^^ ---- use occurs due to use in closure
  |                    |
  |                    value used here after move

error: aborting due to previous error

Rust suggests writing &|| to avoid borrowing. This suggestion is completely wrong - it is orig that should be borrowed (i.e. &orig). Attempting to follow the suggestion might lead a user to write let _closure = &|| orig;, which is still wrong.

Rust should properly suggest &orig in this scenario.

@jonas-schievink jonas-schievink added A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix`. C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Sep 17, 2019
Centril added a commit to Centril/rust that referenced this issue Sep 21, 2019
…rkor

Fix the span used to suggest avoiding for-loop moves

It was using the snippet from the "use" span, which often renders the
same, but with closures that snippet is on the start of the closure
where the value is captured. We should be using the snippet from the
span where it was moved into the `for` loop, which is `move_span`.

Fixes rust-lang#64559.
@bors bors closed this as completed in 97ca073 Sep 21, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix`. 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

Successfully merging a pull request may close this issue.

2 participants