Uh oh!
There was an error while loading. Please reload this page.
Point to let when modifying field of immutable variable - #40445
Conversation
rust-highfive
commented
Mar 11, 2017
r? @eddyb (rust_highfive has picked a reviewer for you, use r? to override) |
eddyb
commented
Mar 11, 2017
Point at the immutable local variable when trying to modify one of its
fields.
Given a file:
```rust
struct Foo {
pub v: Vec<String>
}
fn main() {
let f = Foo { v: Vec::new() };
f.v.push("cat".to_string());
}
```
present the following output:
```
error: cannot borrow immutable field `f.v` as mutable
--> file.rs:7:13
|
6 | let f = Foo { v: Vec::new() };
| - this should be `mut`
7 | f.v.push("cat".to_string());
| ^^^
error: aborting due to previous error
```Emilgardis
commented
Mar 11, 2017
I think this should read or |
nagisa
commented
Mar 12, 2017
@Emilgardis I disagree, because with complex patterns there’s multiple ways to make some binding mutable. IME it should read simply, without suggesting any specific syntax. |
nagisa
commented
Mar 12, 2017
Please also add a test for argument binding. |
sophiajt
commented
Mar 12, 2017
This is a useful suggestion to more experienced Rust programmers, but one of the nice things about making a suggestion is that the errors can help teach you the language. By teaching them about the |
sophiajt
commented
Mar 12, 2017
👍 |
estebank
commented
Mar 12, 2017
Wealreadydothat since #39139, and it isn't affected by this change because here it's checking wether it is an argument to only add this message if it isn't.
I have to agree with these points. I'll change it, as proposed, to error: cannot borrow immutable field `f.v` asmutable
--> file.rs:7:13
|
6 | let f = Foo{v:Vec::new()};
| - consider changing this to `mut f`
7 | f.v.push("cat".to_string());
| ^^^ |
Emilgardis
commented
Mar 12, 2017
I agree with these changes. Why did travis fail on only one suite? |
sophiajt
commented
Mar 12, 2017
The ^^^ needs a label. |
Change the wording of mutable borrow on immutable binding from "this should be `mut`" to "consider changing this to `mut f`".
e339c17 to
90e69d6Compareestebank
commented
Mar 13, 2017
@jonathandturner does the following wording make sense? error: cannot borrow immutable field `z.x` asmutable
--> $DIR/issue-39544.rs:21:18
|
20 | let z = Z{x:X::Y};
| - consider changing this to `mut z`
21 | let _ = &mut z.x;
| ^^^ cannot borrow mutably field of immutable binding |
sophiajt
commented
Mar 14, 2017
Seems close. How about... |
estebank
commented
Mar 14, 2017
@jonathandturner LGTM. Done. |
estebank
commented
Mar 16, 2017
@jonathandturner ping. |
sophiajt
commented
Mar 16, 2017
@bors r+ |
bors
commented
Mar 16, 2017
📌 Commit 9ac628d has been approved by |
…rner
Point to let when modifying field of immutable variable
Point at the immutable local variable when trying to modify one of its
fields.
Given a file:
```rust
struct Foo {
pub v: Vec<String>
}
fn main() {
let f = Foo { v: Vec::new() };
f.v.push("cat".to_string());
}
```
present the following output:
```
error: cannot borrow immutable field `f.v` as mutable
--> file.rs:7:13
|
6 | let f = Foo { v: Vec::new() };
| - this should be `mut`
7 | f.v.push("cat".to_string());
| ^^^
error: aborting due to previous error
```
Fixrust-lang#27593.…rner
Point to let when modifying field of immutable variable
Point at the immutable local variable when trying to modify one of its
fields.
Given a file:
```rust
struct Foo {
pub v: Vec<String>
}
fn main() {
let f = Foo { v: Vec::new() };
f.v.push("cat".to_string());
}
```
present the following output:
```
error: cannot borrow immutable field `f.v` as mutable
--> file.rs:7:13
|
6 | let f = Foo { v: Vec::new() };
| - this should be `mut`
7 | f.v.push("cat".to_string());
| ^^^
error: aborting due to previous error
```
Fixrust-lang#27593.…rner
Point to let when modifying field of immutable variable
Point at the immutable local variable when trying to modify one of its
fields.
Given a file:
```rust
struct Foo {
pub v: Vec<String>
}
fn main() {
let f = Foo { v: Vec::new() };
f.v.push("cat".to_string());
}
```
present the following output:
```
error: cannot borrow immutable field `f.v` as mutable
--> file.rs:7:13
|
6 | let f = Foo { v: Vec::new() };
| - this should be `mut`
7 | f.v.push("cat".to_string());
| ^^^
error: aborting due to previous error
```
Fixrust-lang#27593.…rner
Point to let when modifying field of immutable variable
Point at the immutable local variable when trying to modify one of its
fields.
Given a file:
```rust
struct Foo {
pub v: Vec<String>
}
fn main() {
let f = Foo { v: Vec::new() };
f.v.push("cat".to_string());
}
```
present the following output:
```
error: cannot borrow immutable field `f.v` as mutable
--> file.rs:7:13
|
6 | let f = Foo { v: Vec::new() };
| - this should be `mut`
7 | f.v.push("cat".to_string());
| ^^^
error: aborting due to previous error
```
Fixrust-lang#27593.Given a file
```rust
struct S {
x: i32,
}
fn foo() {
let s = S { x: 42 };
s.x += 1;
}
fn bar(s: S) {
s.x += 1;
}
```
Provide the following output:
```rust
error: cannot assign to immutable field `s.x`
--> $DIR/issue-35937.rs:16:5
|
5 | let s = S { x: 42 };
| - consider changing this to `mut s`
6 | s.x += 1;
| ^^^^^^^^ cannot mutably borrow immutable field
error: cannot assign to immutable field `s.x`
--> $DIR/issue-35937.rs:20:5
|
8 | fn bar(s: S) {
| - consider changing this to `mut s`
9 | s.x += 1;
| ^^^^^^^^ cannot mutably borrow immutable field
```
Follow up to rust-lang#40445. Fixrust-lang#35937.
Point at the immutable local variable when trying to modify one of its
fields.
Given a file:
present the following output:
Fix#27593.