Auto merge of #52242 - ashtneoi:suggest-ref-mut, r=pnkfelix

NLL: Suggest `ref mut` and `&mut self`

Fixes #51244. Supersedes #51249, I think.

Under the old lexical lifetimes, the compiler provided helpful suggestions about adding `mut` when you tried to mutate a variable bound as `&self` or (explicit) `ref`. NLL doesn't have those suggestions yet. This pull request adds them.

I didn't bother making the help text exactly the same as without NLL, but I can if that's important.

(Originally this was supposed to be part of #51612, but I got bogged down trying to fit everything in one PR.)
This commit is contained in:
bors 2018-07-13 17:08:39 +00:00
commit fe29a4cda5
17 changed files with 239 additions and 60 deletions

View file

@ -2,7 +2,7 @@ error[E0596]: cannot borrow immutable item `*self.s` as mutable
--> $DIR/issue-38147-1.rs:27:9
|
LL | fn f(&self) {
| ----- help: consider changing this to be a mutable reference: `&mut Foo<'_>`
| ----- help: consider changing this to be a mutable reference: `&mut self`
LL | self.s.push('x'); //~ ERROR cannot borrow data mutably
| ^^^^^^ `self` is a `&` reference, so the data it refers to cannot be borrowed as mutable

View file

@ -10,7 +10,7 @@ error[E0596]: cannot borrow immutable item `self.x` as mutable
--> $DIR/issue-39544.rs:26:17
|
LL | fn foo<'z>(&'z self) {
| -------- help: consider changing this to be a mutable reference: `&mut Z`
| -------- help: consider changing this to be a mutable reference: `&mut self`
LL | let _ = &mut self.x; //~ ERROR cannot borrow
| ^^^^^^^^^^^ `self` is a `&` reference, so the data it refers to cannot be borrowed as mutable
@ -18,7 +18,7 @@ error[E0596]: cannot borrow immutable item `self.x` as mutable
--> $DIR/issue-39544.rs:30:17
|
LL | fn foo1(&self, other: &Z) {
| ----- help: consider changing this to be a mutable reference: `&mut Z`
| ----- help: consider changing this to be a mutable reference: `&mut self`
LL | let _ = &mut self.x; //~ ERROR cannot borrow
| ^^^^^^^^^^^ `self` is a `&` reference, so the data it refers to cannot be borrowed as mutable
@ -35,7 +35,7 @@ error[E0596]: cannot borrow immutable item `self.x` as mutable
--> $DIR/issue-39544.rs:35:17
|
LL | fn foo2<'a>(&'a self, other: &Z) {
| -------- help: consider changing this to be a mutable reference: `&mut Z`
| -------- help: consider changing this to be a mutable reference: `&mut self`
LL | let _ = &mut self.x; //~ ERROR cannot borrow
| ^^^^^^^^^^^ `self` is a `&` reference, so the data it refers to cannot be borrowed as mutable

View file

@ -0,0 +1,17 @@
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
#![feature(nll)]
fn main() {
let ref my_ref @ _ = 0;
*my_ref = 0;
//~^ ERROR cannot assign to `*my_ref` which is behind a `&` reference [E0594]
}

View file

@ -0,0 +1,11 @@
error[E0594]: cannot assign to `*my_ref` which is behind a `&` reference
--> $DIR/issue-51244.rs:15:5
|
LL | let ref my_ref @ _ = 0;
| -------------- help: consider changing this to be a mutable reference: `ref mut my_ref @ _`
LL | *my_ref = 0;
| ^^^^^^^^^^^ `my_ref` is a `&` reference, so the data it refers to cannot be written
error: aborting due to previous error
For more information about this error, try `rustc --explain E0594`.

View file

@ -2,19 +2,19 @@ error[E0594]: cannot assign to `*x` which is behind a `&` reference
--> $DIR/enum.rs:19:5
|
LL | *x += 1; //~ ERROR cannot assign to immutable
| ^^^^^^^ cannot assign
| ^^^^^^^ `x` is a `&` reference, so the data it refers to cannot be written
error[E0594]: cannot assign to `*x` which is behind a `&` reference
--> $DIR/enum.rs:23:9
|
LL | *x += 1; //~ ERROR cannot assign to immutable
| ^^^^^^^ cannot assign
| ^^^^^^^ `x` is a `&` reference, so the data it refers to cannot be written
error[E0594]: cannot assign to `*x` which is behind a `&` reference
--> $DIR/enum.rs:29:9
|
LL | *x += 1; //~ ERROR cannot assign to immutable
| ^^^^^^^ cannot assign
| ^^^^^^^ `x` is a `&` reference, so the data it refers to cannot be written
error: aborting due to 3 previous errors

View file

@ -1,24 +1,18 @@
error[E0594]: cannot assign to immutable borrowed content `*x`
--> $DIR/enum.rs:19:5
|
LL | let Wrap(x) = &Wrap(3);
| - consider changing this to `x`
LL | *x += 1; //~ ERROR cannot assign to immutable
| ^^^^^^^ cannot borrow as mutable
error[E0594]: cannot assign to immutable borrowed content `*x`
--> $DIR/enum.rs:23:9
|
LL | if let Some(x) = &Some(3) {
| - consider changing this to `x`
LL | *x += 1; //~ ERROR cannot assign to immutable
| ^^^^^^^ cannot borrow as mutable
error[E0594]: cannot assign to immutable borrowed content `*x`
--> $DIR/enum.rs:29:9
|
LL | while let Some(x) = &Some(3) {
| - consider changing this to `x`
LL | *x += 1; //~ ERROR cannot assign to immutable
| ^^^^^^^ cannot borrow as mutable

View file

@ -2,19 +2,19 @@ error[E0594]: cannot assign to `*n` which is behind a `&` reference
--> $DIR/explicit-mut.rs:17:13
|
LL | *n += 1; //~ ERROR cannot assign to immutable
| ^^^^^^^ cannot assign
| ^^^^^^^ `n` is a `&` reference, so the data it refers to cannot be written
error[E0594]: cannot assign to `*n` which is behind a `&` reference
--> $DIR/explicit-mut.rs:25:13
|
LL | *n += 1; //~ ERROR cannot assign to immutable
| ^^^^^^^ cannot assign
| ^^^^^^^ `n` is a `&` reference, so the data it refers to cannot be written
error[E0594]: cannot assign to `*n` which is behind a `&` reference
--> $DIR/explicit-mut.rs:33:13
|
LL | *n += 1; //~ ERROR cannot assign to immutable
| ^^^^^^^ cannot assign
| ^^^^^^^ `n` is a `&` reference, so the data it refers to cannot be written
error: aborting due to 3 previous errors

View file

@ -1,24 +1,18 @@
error[E0594]: cannot assign to immutable borrowed content `*n`
--> $DIR/explicit-mut.rs:17:13
|
LL | Some(n) => {
| - consider changing this to `n`
LL | *n += 1; //~ ERROR cannot assign to immutable
| ^^^^^^^ cannot borrow as mutable
error[E0594]: cannot assign to immutable borrowed content `*n`
--> $DIR/explicit-mut.rs:25:13
|
LL | Some(n) => {
| - consider changing this to `n`
LL | *n += 1; //~ ERROR cannot assign to immutable
| ^^^^^^^ cannot borrow as mutable
error[E0594]: cannot assign to immutable borrowed content `*n`
--> $DIR/explicit-mut.rs:33:13
|
LL | Some(n) => {
| - consider changing this to `n`
LL | *n += 1; //~ ERROR cannot assign to immutable
| ^^^^^^^ cannot borrow as mutable

View file

@ -0,0 +1,11 @@
error[E0594]: cannot assign to `*my_ref` which is behind a `&` reference
--> $DIR/issue-51244.rs:13:5
|
LL | let ref my_ref @ _ = 0;
| -------------- help: consider changing this to be a mutable reference: `ref mut my_ref @ _`
LL | *my_ref = 0; //~ ERROR cannot assign to immutable borrowed content `*my_ref` [E0594]
| ^^^^^^^^^^^ `my_ref` is a `&` reference, so the data it refers to cannot be written
error: aborting due to previous error
For more information about this error, try `rustc --explain E0594`.

View file

@ -0,0 +1,14 @@
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
fn main() {
let ref my_ref @ _ = 0;
*my_ref = 0; //~ ERROR cannot assign to immutable borrowed content `*my_ref` [E0594]
}

View file

@ -0,0 +1,11 @@
error[E0594]: cannot assign to immutable borrowed content `*my_ref`
--> $DIR/issue-51244.rs:13:5
|
LL | let ref my_ref @ _ = 0;
| -------------- help: use a mutable reference instead: `ref mut my_ref @ _`
LL | *my_ref = 0; //~ ERROR cannot assign to immutable borrowed content `*my_ref` [E0594]
| ^^^^^^^^^^^ cannot borrow as mutable
error: aborting due to previous error
For more information about this error, try `rustc --explain E0594`.

View file

@ -0,0 +1,42 @@
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
#![feature(nll)]
struct X(usize);
impl X {
fn zap(&self) {
//~^ HELP
//~| SUGGESTION &mut self
self.0 = 32;
//~^ ERROR
}
}
fn main() {
let ref foo = 16;
//~^ HELP
//~| SUGGESTION ref mut foo
*foo = 32;
//~^ ERROR
if let Some(ref bar) = Some(16) {
//~^ HELP
//~| SUGGESTION ref mut bar
*bar = 32;
//~^ ERROR
}
match 16 {
ref quo => { *quo = 32; },
//~^ ERROR
//~| HELP
//~| SUGGESTION ref mut quo
}
}

View file

@ -0,0 +1,38 @@
error[E0594]: cannot assign to `self.0` which is behind a `&` reference
--> $DIR/suggest-ref-mut.rs:19:9
|
LL | fn zap(&self) {
| ----- help: consider changing this to be a mutable reference: `&mut self`
...
LL | self.0 = 32;
| ^^^^^^^^^^^ `self` is a `&` reference, so the data it refers to cannot be written
error[E0594]: cannot assign to `*foo` which is behind a `&` reference
--> $DIR/suggest-ref-mut.rs:28:5
|
LL | let ref foo = 16;
| ------- help: consider changing this to be a mutable reference: `ref mut foo`
...
LL | *foo = 32;
| ^^^^^^^^^ `foo` is a `&` reference, so the data it refers to cannot be written
error[E0594]: cannot assign to `*bar` which is behind a `&` reference
--> $DIR/suggest-ref-mut.rs:33:9
|
LL | if let Some(ref bar) = Some(16) {
| ------- help: consider changing this to be a mutable reference: `ref mut bar`
...
LL | *bar = 32;
| ^^^^^^^^^ `bar` is a `&` reference, so the data it refers to cannot be written
error[E0594]: cannot assign to `*quo` which is behind a `&` reference
--> $DIR/suggest-ref-mut.rs:37:22
|
LL | ref quo => { *quo = 32; },
| ------- ^^^^^^^^^ `quo` is a `&` reference, so the data it refers to cannot be written
| |
| help: consider changing this to be a mutable reference: `ref mut quo`
error: aborting due to 4 previous errors
For more information about this error, try `rustc --explain E0594`.