Point to let when modifying field of immutable variable by estebank · Pull Request #40445 · rust-lang/rust (original) (raw)
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 andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Conversation16 Commits3 Checks0 Files changed
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
[ Show hidden characters]({{ revealButtonHref }})
Point at the immutable local variable when trying to modify one of its
fields.
Given a file:
struct Foo { pub v: Vec }
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 #27593.
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:
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
error: cannot borrow immutable field `f.v` as mutable
--> file.rs:7:13
|
6 | let f = Foo { v: Vec::new() };
| ^ - consider adding `mut` here
7 | f.v.push("cat".to_string());
| ^^^
or
consider changing this to `mut f`
@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.
6 | let f = Foo { v: Vec::new() };
| - consider making this binding mutable
Please also add a test for argument binding.
6 | let f = Foo { v: Vec::new() };
| - consider making this binding mutable
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 mut
keyword, they have something they can do to fix the error. Your suggestion has the unfortunate side effect of saying "go read the manual"
consider changing this to `mut f`
👍
Please also add a test for argument binding.
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.
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 mut keyword, they have something they can do to fix the error. Your suggestion has the unfortunate side effect of saying "go read the manual"
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?
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());
| ^^^
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
".
@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...
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 mutably borrow immutable field
📌 Commit 9ac628d has been approved by jonathandturner
frewsxcv added a commit to frewsxcv/rust that referenced this pull request
…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:
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.
arielb1 pushed a commit to arielb1/rust that referenced this pull request
…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:
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.
bors added a commit that referenced this pull request
Rollup of 22 pull requests
- Successful merges: #40241, #40346, #40348, #40377, #40398, #40409, #40441, #40445, #40509, #40521, #40523, #40532, #40538, #40564, #40581, #40583, #40588, #40589, #40590, #40603, #40611, #40621
- Failed merges: #40501, #40541
arielb1 pushed a commit to arielb1/rust that referenced this pull request
…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:
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.
frewsxcv added a commit to frewsxcv/rust that referenced this pull request
…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:
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.
bors added a commit that referenced this pull request
Rollup of 13 pull requests
- Successful merges: #40441, #40445, #40562, #40564, #40583, #40588, #40589, #40590, #40603, #40611, #40621, #40646, #40648
- Failed merges:
estebank added a commit to estebank/rust that referenced this pull request
Given a file
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:
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.