-
Notifications
You must be signed in to change notification settings - Fork 13.3k
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Point to let when modifying field of immutable variable #40445
Conversation
r? @eddyb (rust_highfive has picked a reviewer for you, use r? to override) |
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 ```
I think this should read
or
|
@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.
|
Please also add a test for argument binding. |
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 |
👍 |
We already do that 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` as mutable
--> file.rs:7:13
|
6 | let f = Foo { v: Vec::new() };
| - consider changing this to `mut f`
7 | f.v.push("cat".to_string());
| ^^^ |
I agree with these changes. Why did travis fail on only one suite? |
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
90e69d6
Compare
@jonathandturner does the following wording make sense? error: cannot borrow immutable field `z.x` as mutable
--> $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 |
Seems close. How about...
|
@jonathandturner LGTM. Done. |
@jonathandturner ping. |
@bors r+ |
📌 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 ``` Fix rust-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 ``` Fix rust-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 ``` Fix rust-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 ``` Fix rust-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. Fix rust-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.