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 }})

estebank

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.

@rust-highfive

r? @eddyb

(rust_highfive has picked a reviewer for you, use r? to override)

@eddyb

@estebank

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

@Emilgardis

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`

@nagisa

@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

@nagisa

Please also add a test for argument binding.

@sophiajt

@nagisa

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"

@sophiajt

consider changing this to `mut f`

👍

@estebank

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()); | ^^^

@Emilgardis

I agree with these changes.

Why did travis fail on only one suite?

@sophiajt

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.

@estebank

Change the wording of mutable borrow on immutable binding from "this should be mut" to "consider changing this to mut f".

@estebank

@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

@sophiajt

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

@estebank

@estebank

@estebank

@sophiajt

@bors

📌 Commit 9ac628d has been approved by jonathandturner

frewsxcv added a commit to frewsxcv/rust that referenced this pull request

Mar 18, 2017

@frewsxcv

…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

Mar 18, 2017

…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

Mar 18, 2017

@bors

Rollup of 22 pull requests

arielb1 pushed a commit to arielb1/rust that referenced this pull request

Mar 19, 2017

…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

Mar 19, 2017

@frewsxcv

…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

Mar 19, 2017

@bors

Rollup of 13 pull requests

estebank added a commit to estebank/rust that referenced this pull request

Mar 24, 2017

@estebank

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.