Skip to content
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 and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Suggest use mut when mutating a non mutable local #51260

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions src/librustc_mir/borrow_check/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1716,10 +1716,11 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
.cannot_borrow_path_as_mutable(span, &item_msg, Origin::Mir);
err.span_label(span, "cannot borrow as mutable");

if place != place_err {
if let Some(name) = self.describe_place(place_err) {
err.note(&format!("the value which is causing this path not to be \
mutable is...: `{}`", name));
if let &Place::Local(local) = place_err {
let local_decl = &self.mir.local_decls[local];
if local_decl.is_user_variable && local_decl.mutability == Mutability::Not {
err.span_label(local_decl.source_info.span,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here we should use the span_suggestion method. Something like this (I took the prompt etc from AST borrowck):

let local_name = local_decl.name.unwrap();
err.span_suggestion(
    local_decl.source_info.span,
    &format!("consider making `{}` mutable", local_name),
    format!("mut {}", local_name),
);

This will encode the suggestion into the JSON etc so that tools like rustfix or IDEs can apply it automatically.

format!("consider changing this to `mut {}`", local_decl.name.unwrap()));
}
}
Copy link
Contributor

@nikomatsakis nikomatsakis Jun 1, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, I was noting on Zulip at 4:40pm (ET) yesterday, I think we want to move this suggestion code so that it comes after the match. That will require restructuring this method somewhat. The problem with the current setup is that your suggestion will only fire when you are trying to borrow something, but not — I think — in tests like let x = 3; x = 4; (try it!). I would combine it with some of the other suggestion-like code in the method too, yielding something like this:

let mut err = match kind {
  Reservation(...) | Write(...) => {
    if no error { return } else { construct_err_object() }
  }
  Reservation(...) | Write(...) => /* some variant of the preivous arm */,
  Reservation(...) | Write(...) => ...,
};

// If we get here, then there *is* an error
match place_err {
    Place::Local(l) => {
        // suggest changing to `mut` variable
    }
    Place::Projection(...) => {
        // suggest changing to `&mut` borrow (see [link 1])
    }
}

err.emit()

link 1 is

let err_info = if let Place::Projection(
box Projection {
base: Place::Local(local),
elem: ProjectionElem::Deref
}
) = *place_err {

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are also going to want to watch out for "upvars" — those are cases where the place_err will not be a Place::Local but we want to make a similar suggestion. We can discuss on Zulip though, I'm just going to edit your PR description to have a check-list so things don't get forgotten.


Expand Down
3 changes: 3 additions & 0 deletions src/test/ui/augmented-assignments.nll.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ LL | | x; //~ value moved here
error[E0596]: cannot borrow immutable item `y` as mutable
--> $DIR/augmented-assignments.rs:30:5
|
LL | let y = Int(2);
| - consider changing this to `mut y`
LL | //~^ consider changing this to `mut y`
LL | y //~ error: cannot borrow immutable local variable `y` as mutable
| ^ cannot borrow as mutable

Expand Down
2 changes: 2 additions & 0 deletions src/test/ui/borrowck/issue-45983.nll.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ LL | give_any(|y| x = Some(y));
error[E0596]: cannot borrow immutable item `x` as mutable
--> $DIR/issue-45983.rs:17:14
|
LL | let x = None;
| - consider changing this to `mut x`
LL | give_any(|y| x = Some(y));
| ^^^^^^^^^^^^^^^ cannot borrow as mutable

Expand Down
3 changes: 3 additions & 0 deletions src/test/ui/codemap_tests/huge_multispan_highlight.nll.stderr
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
error[E0596]: cannot borrow immutable item `x` as mutable
--> $DIR/huge_multispan_highlight.rs:100:13
|
LL | let x = "foo";
| - consider changing this to `mut x`
...
LL | let y = &mut x; //~ ERROR cannot borrow
| ^^^^^^ cannot borrow as mutable

Expand Down
2 changes: 2 additions & 0 deletions src/test/ui/did_you_mean/issue-34337.nll.stderr
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
error[E0596]: cannot borrow immutable item `key` as mutable
--> $DIR/issue-34337.rs:16:9
|
LL | let ref mut key = v[0];
| ----------- consider changing this to `mut key`
LL | get(&mut key); //~ ERROR cannot borrow
| ^^^^^^^^ cannot borrow as mutable

Expand Down
4 changes: 2 additions & 2 deletions src/test/ui/did_you_mean/issue-35937.nll.stderr
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
error[E0596]: cannot borrow immutable item `f.v` as mutable
--> $DIR/issue-35937.rs:17:5
|
LL | let f = Foo { v: Vec::new() };
| - consider changing this to `mut f`
LL | f.v.push("cat".to_string()); //~ ERROR cannot borrow
| ^^^ cannot borrow as mutable
|
= note: the value which is causing this path not to be mutable is...: `f`

error[E0384]: cannot assign twice to immutable variable `s.x`
--> $DIR/issue-35937.rs:26:5
Expand Down
2 changes: 2 additions & 0 deletions src/test/ui/did_you_mean/issue-37139.nll.stderr
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
error[E0596]: cannot borrow immutable item `x` as mutable
--> $DIR/issue-37139.rs:22:18
|
LL | TestEnum::Item(ref mut x) => {
| --------- consider changing this to `mut x`
Copy link
Member

@pnkfelix pnkfelix May 31, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah we definitely don't want to suggest this...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, need to look at this more carefully.

LL | test(&mut x); //~ ERROR cannot borrow immutable
| ^^^^^^ cannot borrow as mutable

Expand Down
2 changes: 0 additions & 2 deletions src/test/ui/did_you_mean/issue-38147-1.nll.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ error[E0596]: cannot borrow immutable item `*self.s` as mutable
|
LL | self.s.push('x'); //~ ERROR cannot borrow data mutably
| ^^^^^^ cannot borrow as mutable
|
= note: the value which is causing this path not to be mutable is...: `*self`

error: aborting due to previous error

Expand Down
2 changes: 0 additions & 2 deletions src/test/ui/did_you_mean/issue-38147-4.nll.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ error[E0596]: cannot borrow immutable item `*f.s` as mutable
|
LL | f.s.push('x'); //~ ERROR cannot borrow data mutably
| ^^^ cannot borrow as mutable
|
= note: the value which is causing this path not to be mutable is...: `*f`

error: aborting due to previous error

Expand Down
24 changes: 2 additions & 22 deletions src/test/ui/did_you_mean/issue-39544.nll.stderr
Original file line number Diff line number Diff line change
@@ -1,90 +1,70 @@
error[E0596]: cannot borrow immutable item `z.x` as mutable
--> $DIR/issue-39544.rs:21:13
|
LL | let z = Z { x: X::Y };
| - consider changing this to `mut z`
LL | let _ = &mut z.x; //~ ERROR cannot borrow
| ^^^^^^^^ cannot borrow as mutable
|
= note: the value which is causing this path not to be mutable is...: `z`

error[E0596]: cannot borrow immutable item `self.x` as mutable
--> $DIR/issue-39544.rs:26:17
|
LL | let _ = &mut self.x; //~ ERROR cannot borrow
| ^^^^^^^^^^^ cannot borrow as mutable
|
= note: the value which is causing this path not to be mutable is...: `*self`

error[E0596]: cannot borrow immutable item `self.x` as mutable
--> $DIR/issue-39544.rs:30:17
|
LL | let _ = &mut self.x; //~ ERROR cannot borrow
| ^^^^^^^^^^^ cannot borrow as mutable
|
= note: the value which is causing this path not to be mutable is...: `*self`

error[E0596]: cannot borrow immutable item `other.x` as mutable
--> $DIR/issue-39544.rs:31:17
|
LL | let _ = &mut other.x; //~ ERROR cannot borrow
| ^^^^^^^^^^^^ cannot borrow as mutable
|
= note: the value which is causing this path not to be mutable is...: `*other`

error[E0596]: cannot borrow immutable item `self.x` as mutable
--> $DIR/issue-39544.rs:35:17
|
LL | let _ = &mut self.x; //~ ERROR cannot borrow
| ^^^^^^^^^^^ cannot borrow as mutable
|
= note: the value which is causing this path not to be mutable is...: `*self`

error[E0596]: cannot borrow immutable item `other.x` as mutable
--> $DIR/issue-39544.rs:36:17
|
LL | let _ = &mut other.x; //~ ERROR cannot borrow
| ^^^^^^^^^^^^ cannot borrow as mutable
|
= note: the value which is causing this path not to be mutable is...: `*other`

error[E0596]: cannot borrow immutable item `self.x` as mutable
--> $DIR/issue-39544.rs:40:17
|
LL | let _ = &mut self.x; //~ ERROR cannot borrow
| ^^^^^^^^^^^ cannot borrow as mutable
|
= note: the value which is causing this path not to be mutable is...: `*self`

error[E0596]: cannot borrow immutable item `other.x` as mutable
--> $DIR/issue-39544.rs:41:17
|
LL | let _ = &mut other.x; //~ ERROR cannot borrow
| ^^^^^^^^^^^^ cannot borrow as mutable
|
= note: the value which is causing this path not to be mutable is...: `*other`

error[E0596]: cannot borrow immutable item `other.x` as mutable
--> $DIR/issue-39544.rs:45:17
|
LL | let _ = &mut other.x; //~ ERROR cannot borrow
| ^^^^^^^^^^^^ cannot borrow as mutable
|
= note: the value which is causing this path not to be mutable is...: `*other`

error[E0596]: cannot borrow immutable item `z.x` as mutable
--> $DIR/issue-39544.rs:51:13
|
LL | let _ = &mut z.x; //~ ERROR cannot borrow
| ^^^^^^^^ cannot borrow as mutable
|
= note: the value which is causing this path not to be mutable is...: `z`

error[E0596]: cannot borrow immutable item `w.x` as mutable
--> $DIR/issue-39544.rs:52:13
|
LL | let _ = &mut w.x; //~ ERROR cannot borrow
| ^^^^^^^^ cannot borrow as mutable
|
= note: the value which is causing this path not to be mutable is...: `*w`

error[E0594]: cannot assign to immutable item `*x.0`
--> $DIR/issue-39544.rs:58:5
Expand Down
4 changes: 2 additions & 2 deletions src/test/ui/issue-36400.nll.stderr
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
error[E0596]: cannot borrow immutable item `*x` as mutable
--> $DIR/issue-36400.rs:15:7
|
LL | let x = Box::new(3);
| - consider changing this to `mut x`
LL | f(&mut *x); //~ ERROR cannot borrow immutable
| ^^^^^^^ cannot borrow as mutable
|
= note: the value which is causing this path not to be mutable is...: `x`

error: aborting due to previous error

Expand Down
2 changes: 2 additions & 0 deletions src/test/ui/macros/span-covering-argument-1.nll.stderr
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
error[E0596]: cannot borrow immutable item `foo` as mutable
--> $DIR/span-covering-argument-1.rs:15:14
|
LL | let $s = 0;
| -- consider changing this to `mut foo`
LL | *&mut $s = 0;
| ^^^^^^^ cannot borrow as mutable
...
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ error[E0596]: cannot borrow immutable item `*f.f` as mutable
|
LL | f.f.call_mut(())
| ^^^ cannot borrow as mutable
|
= note: the value which is causing this path not to be mutable is...: `*f`

error[E0507]: cannot move out of borrowed content
--> $DIR/borrowck-call-is-borrow-issue-12224.rs:66:13
Expand Down
2 changes: 0 additions & 2 deletions src/test/ui/span/borrowck-object-mutability.nll.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ error[E0596]: cannot borrow immutable item `*x` as mutable
|
LL | x.borrowed_mut(); //~ ERROR cannot borrow
| ^ cannot borrow as mutable
|
= note: the value which is causing this path not to be mutable is...: `x`

error: aborting due to 2 previous errors

Expand Down