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

Unhelpful help for E0387 "consider changing this closure to take self by mutable reference" #53905

Open
killercup opened this issue Sep 2, 2018 · 2 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix`. C-enhancement Category: An issue proposing an enhancement or a PR with one. D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@killercup
Copy link
Member

I just got this error message (on current stable, beta, and the nightly mentioned below):

error[E0387]: cannot borrow data mutably in a captured outer variable in an `Fn` closure
  --> src/main.rs:30:57
   |
30 |         .for_each(|filename| tally_words(filename, &mut words).unwrap());
   |                                                         ^^^^^
   |
help: consider changing this closure to take self by mutable reference
  --> src/main.rs:30:19
   |
30 |         .for_each(|filename| tally_words(filename, &mut words).unwrap());
   |                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

As you can see it suggests to do no change at all:

-          .for_each(|filename| tally_words(filename, &mut words).unwrap());
+          .for_each(|filename| tally_words(filename, &mut words).unwrap());

You can reproduce this by checking out hello-rust/show@d495abe and running cd episode/9/rust/pascal/; cargo build.

Rust version
rustc 1.30.0-nightly (f8d34596f 2018-08-30)
binary: rustc
commit-hash: f8d34596ff74da91e0877212a9979cb9ca13eb7e
commit-date: 2018-08-30
host: x86_64-apple-darwin
release: 1.30.0-nightly
LLVM version: 7.0
@killercup killercup added the A-diagnostics Area: Messages for errors, warnings, and lints label Sep 2, 2018
@hcs64
Copy link

hcs64 commented Nov 17, 2018

As noted in #31701 the text of the message isn't particularly helpful, either.

@estebank estebank added A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix`. D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Oct 18, 2019
@crlf0710 crlf0710 added the C-enhancement Category: An issue proposing an enhancement or a PR with one. label Jun 11, 2020
@ibraheemdev
Copy link
Member

I think this was fixed:

use rayon::prelude::*;

fn main() {
    let mut y = 0;
    (0..10).into_par_iter().for_each(|x| println!("{}{}", x, &mut y));
}
error[E0596]: cannot borrow `y` as mutable, as it is a captured variable in a `Fn` closure
 --> src/main.rs:5:62
  |
5 |     (0..10).into_par_iter().for_each(|x| println!("{}{}", x, &mut y));
  |                                                              ^^^^^^ cannot borrow as mutable

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 A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix`. C-enhancement Category: An issue proposing an enhancement or a PR with one. D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. 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

5 participants