Skip to content

Rust suggests me to make a mutable variable mutable? #46834

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

Closed
jD91mZM2 opened this issue Dec 19, 2017 · 5 comments
Closed

Rust suggests me to make a mutable variable mutable? #46834

jD91mZM2 opened this issue Dec 19, 2017 · 5 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@jD91mZM2
Copy link
Contributor

error[E0594]: cannot assign to captured outer variable in an `Fn` closure
   --> main.rs:232:9
    |
225 |     let mut typing_last = Instant::now();
    |         --------------- help: consider making `mut typing_last` mutable: `mut mut typing_last`
...
232 |         typing_last = Instant::now();
    |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |
help: consider changing this closure to take self by mutable reference
   --> main.rs:228:40
    |
228 |       input.connect_property_text_notify(move |input| {
    |  ________________________________________^
229 | |         if typing_last.elapsed() < typing_duration {
230 | |             return;
231 | |         }
...   |
234 | |         println!("{:?}", text);
235 | |     });
    | |_____^

help: consider making mut typing_last mutable: mut mut typing_last

Sadly I can't seems to find a minimal reproduction example where this appears, but I commited all my code to a separate branch so you can look at the code (or try to compile it yourself): rust-weird-error.

I'm sorry, but that's all I have on this error.

@sfackler sfackler added the A-diagnostics Area: Messages for errors, warnings, and lints label Dec 19, 2017
@matthewjasper
Copy link
Contributor

matthewjasper commented Dec 19, 2017

Minified:

pub fn bar<F: Fn()>(_f: F) {}

pub fn foo() {
    let mut x = 0;
    bar(move || x = 1); // Removing `move` produces a different, unhelpful error message
}

If the help message showed anything if should point to the Fn() bound.

cc #46020

@jD91mZM2
Copy link
Contributor Author

@matthewjasper That doesn't seem to give the hint in the playground. Ooooh, maybe the reason I couldn't make a minimal example is because the playground has a different rust version?

@matthewjasper
Copy link
Contributor

Looks like this is only an issue on beta/nightly.

@jD91mZM2
Copy link
Contributor Author

jD91mZM2 commented Dec 20, 2017

When I encoutered this I wasn't using nightly.
Ugh never mind, when trying to install redox my toolchain was switched to nightly by default.

GuillaumeGomez added a commit to GuillaumeGomez/rust that referenced this issue Jan 17, 2018
Do not suggest to make `mut` binding external to `Fn` closure

Re rust-lang#46834.
@XAMPPRocky XAMPPRocky added C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Mar 26, 2018
@estebank
Copy link
Contributor

estebank commented Apr 20, 2019

Current output:

error[E0594]: cannot assign to captured outer variable in an `Fn` closure
 --> src/main.rs:5:17
  |
5 |     bar(move || x = 1);
  |                 ^^^^^
  |
  = note: `Fn` closures cannot capture their enclosing environment for modifications
help: consider changing this closure to take self by mutable reference
 --> src/main.rs:5:9
  |
5 |     bar(move || x = 1);
  |         ^^^^^^^^^^^^^

warning: variable does not need to be mutable
 --> src/main.rs:4:9
  |
4 |     let mut x = 0;
  |         ----^
  |         |
  |         help: remove this `mut`
  |
  = note: #[warn(unused_mut)] on by default

error: aborting due to previous error

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 C-enhancement Category: An issue proposing an enhancement or a PR with one. 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