You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Error message about immutable binding is not actually caused by a binding #49839
use std::collections::HashMap;structTest{v:u32,}fnmain(){letmut map = HashMap::new();
map.insert("a",Test{v:0});let values = map.values();formut t in values {
t.v += 1;}}
It raises this compiler error:
error[E0594]: cannot assign to field t.v of immutable binding
--> /tmp/test.rs:14:9
|
14 | t.v += 1;
| ^^^^^^^^ cannot mutably borrow field of immutable binding
The fix is to use values_mut() instead of values().
But the error message is confusing as the problem here is not the binding as t itself is mut making it harder to debug for newcomers.
Current output is slightly more misleading, but I think improving on this will be hard:
error[E0594]: cannot assign to `t.v` which is behind a `&` reference
--> src/main.rs:13:9
|
12 | for mut t in values {
| ----- help: consider changing this to be a mutable reference: `&mut Test`
13 | t.v += 1;
| ^^^^^^^^ `t` is a `&` reference, so the data it refers to cannot be written
error[E0594]: cannot assign to `t.v` which is behind a `&` reference
--> src/main.rs:13:9
|
12 | for mut t in values {
| ------ this iterator yields `&` references
13 | t.v += 1;
| ^^^^^^^^ `t` is a `&` reference, so the data it refers to cannot be written
@sasurau4 you might want to first focus on the case where the values() method has been called in the for loop as it will be easier to find the needed expressions and types to verify that values_mut is available. It would also be interesting to account for .iter() vs .iter_mut() as well as part of this work (it just adds another method to check for). For the exact case that is presented in the original report, you'll have to find where the binding that is in the iterator expression of the for loop comes from, which as things are today might be had (but not impossible for the common case) to do. I don't want to discourage you from tackling it if you desire to do so, but I do want you to know that so you don't get discouraged if you find it takes to long or becomes too hard. Handling the simpler cases has a lot of value on their own.
error[E0594]: cannot assign to `t.v` which is behind a `&` reference
--> src/main.rs:12:9
|
11 | for mut t in map.values() {
| ------------
| | |
| | help: use mutable method: `values_mut()`
| this iterator yields `&` references
12 | t.v += 1;
| ^^^^^^^^ `t` is a `&` reference, so the data it refers to cannot be written
The let values = map.values(); case still needs to be handled.
Activity
mbrubeck commentedon Jul 30, 2018
In Rust 1.23 and earlier, the error was:
estebank commentedon May 23, 2019
Current output is slightly more misleading, but I think improving on this will be hard:
estebank commentedon May 25, 2019
This overlaps slightly with #18150, but is more targeted, leaving open.
steveklabnik commentedon May 12, 2020
Triage: no change
sasurau4 commentedon Jan 17, 2021
I'll work on it. @rustbot calim
current output is following.
estebank commentedon Jan 18, 2021
@sasurau4 you might want to first focus on the case where the
values()method has been called in theforloop as it will be easier to find the needed expressions and types to verify thatvalues_mutis available. It would also be interesting to account for.iter()vs.iter_mut()as well as part of this work (it just adds another method to check for). For the exact case that is presented in the original report, you'll have to find where the binding that is in the iterator expression of theforloop comes from, which as things are today might be had (but not impossible for the common case) to do. I don't want to discourage you from tackling it if you desire to do so, but I do want you to know that so you don't get discouraged if you find it takes to long or becomes too hard. Handling the simpler cases has a lot of value on their own.sasurau4 commentedon Jan 18, 2021
@estebank Thanks for your advice! I was stuck on how to get the binding rhs expression
for.I'll focus on the case you suggest 👍
Rollup merge of rust-lang#81466 - sasurau4:fix/enhance-sugget-mut-met…
estebank commentedon Feb 14, 2021
Thanks to @sasurau4 the output for the "inlined" case is now:
The
let values = map.values();case still needs to be handled.Rollup merge of rust-lang#160001 - estebank:issue-49839, r=adwinwhite
Rollup merge of rust-lang#160001 - estebank:issue-49839, r=adwinwhite
Unrolled build for #160001
Rollup merge of #160001 - estebank:issue-49839, r=adwinwhite
Rollup merge of #160001 - estebank:issue-49839, r=adwinwhite