Skip to content

Commit 32dc5a0

Browse files
committed
Auto merge of #54157 - euclio:structured-suggestion, r=estebank
use structured suggestion for "missing mut" label Fixes #54133 for both NLL and non-NLL. r? @estebank I'm not super happy with the existing wording here, since it's now a suggestion. I wonder if the message would work better as something like "help: make binding mutable: `mut foo`"? Also, are the `HELP` and `SUGGESTION` comments necessary?
2 parents f481987 + d871b8a commit 32dc5a0

File tree

77 files changed

+179
-164
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

77 files changed

+179
-164
lines changed

src/librustc_borrowck/borrowck/mod.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ use rustc_data_structures::sync::Lrc;
4545
use std::hash::{Hash, Hasher};
4646
use syntax::ast;
4747
use syntax_pos::{MultiSpan, Span};
48-
use errors::{DiagnosticBuilder, DiagnosticId};
48+
use errors::{Applicability, DiagnosticBuilder, DiagnosticId};
4949

5050
use rustc::hir;
5151
use rustc::hir::intravisit::{self, Visitor};
@@ -1299,9 +1299,11 @@ impl<'a, 'tcx> BorrowckCtxt<'a, 'tcx> {
12991299
snippet
13001300
);
13011301
} else {
1302-
db.span_label(
1302+
db.span_suggestion_with_applicability(
13031303
let_span,
1304-
format!("consider changing this to `mut {}`", snippet),
1304+
"make this binding mutable",
1305+
format!("mut {}", snippet),
1306+
Applicability::MachineApplicable,
13051307
);
13061308
}
13071309
}

src/librustc_mir/borrow_check/error_reporting.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use rustc::mir::{ProjectionElem, Rvalue, Statement, StatementKind};
1717
use rustc::ty;
1818
use rustc_data_structures::fx::FxHashSet;
1919
use rustc_data_structures::sync::Lrc;
20-
use rustc_errors::DiagnosticBuilder;
20+
use rustc_errors::{Applicability, DiagnosticBuilder};
2121
use syntax_pos::Span;
2222

2323
use super::borrow_set::BorrowData;
@@ -702,9 +702,11 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
702702
if let Some(decl) = local_decl {
703703
if let Some(name) = decl.name {
704704
if decl.can_be_made_mutable() {
705-
err.span_label(
705+
err.span_suggestion_with_applicability(
706706
decl.source_info.span,
707-
format!("consider changing this to `mut {}`", name),
707+
"make this binding mutable",
708+
format!("mut {}", name),
709+
Applicability::MachineApplicable,
708710
);
709711
}
710712
}

src/test/ui/E0596.ast.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0596]: cannot borrow immutable local variable `x` as mutable
22
--> $DIR/E0596.rs:16:18
33
|
44
LL | let x = 1;
5-
| - consider changing this to `mut x`
5+
| - help: make this binding mutable: `mut x`
66
LL | let y = &mut x; //[ast]~ ERROR [E0596]
77
| ^ cannot borrow mutably
88

src/test/ui/asm/asm-out-assign-imm.nll.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0384]: cannot assign twice to immutable variable `x`
22
--> $DIR/asm-out-assign-imm.rs:34:9
33
|
44
LL | let x: isize;
5-
| - consider changing this to `mut x`
5+
| - help: make this binding mutable: `mut x`
66
LL | x = 1;
77
| ----- first assignment to `x`
88
...

src/test/ui/assign-imm-local-twice.ast.nll.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
error[E0384]: cannot assign twice to immutable variable `v`
2-
--> $DIR/assign-imm-local-twice.rs:20:5
2+
--> $DIR/assign-imm-local-twice.rs:21:5
33
|
44
LL | let v: isize;
5-
| - consider changing this to `mut v`
6-
LL | //[mir]~^ NOTE consider changing this to `mut v`
5+
| - help: make this binding mutable: `mut v`
6+
...
77
LL | v = 1; //[ast]~ NOTE first assignment
88
| ----- first assignment to `v`
99
...

src/test/ui/assign-imm-local-twice.ast.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error[E0384]: cannot assign twice to immutable variable `v`
2-
--> $DIR/assign-imm-local-twice.rs:20:5
2+
--> $DIR/assign-imm-local-twice.rs:21:5
33
|
44
LL | v = 1; //[ast]~ NOTE first assignment
55
| ----- first assignment to `v`

src/test/ui/assign-imm-local-twice.mir.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
error[E0384]: cannot assign twice to immutable variable `v`
2-
--> $DIR/assign-imm-local-twice.rs:20:5
2+
--> $DIR/assign-imm-local-twice.rs:21:5
33
|
44
LL | let v: isize;
5-
| - consider changing this to `mut v`
6-
LL | //[mir]~^ NOTE consider changing this to `mut v`
5+
| - help: make this binding mutable: `mut v`
6+
...
77
LL | v = 1; //[ast]~ NOTE first assignment
88
| ----- first assignment to `v`
99
...

src/test/ui/assign-imm-local-twice.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313

1414
fn test() {
1515
let v: isize;
16-
//[mir]~^ NOTE consider changing this to `mut v`
16+
//[mir]~^ HELP make this binding mutable
17+
//[mir]~| SUGGESTION mut v
1718
v = 1; //[ast]~ NOTE first assignment
1819
//[mir]~^ NOTE first assignment
1920
println!("v={}", v);

src/test/ui/augmented-assignments.nll.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ LL | | x; //~ value moved here
1515
| borrow later used here
1616

1717
error[E0596]: cannot borrow `y` as mutable, as it is not declared as mutable
18-
--> $DIR/augmented-assignments.rs:30:5
18+
--> $DIR/augmented-assignments.rs:31:5
1919
|
2020
LL | let y = Int(2);
2121
| - help: consider changing this to be mutable: `mut y`
22-
LL | //~^ consider changing this to `mut y`
22+
...
2323
LL | y //~ error: cannot borrow immutable local variable `y` as mutable
2424
| ^ cannot borrow as mutable
2525

src/test/ui/augmented-assignments.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ fn main() {
2626
x; //~ value moved here
2727

2828
let y = Int(2);
29-
//~^ consider changing this to `mut y`
29+
//~^ HELP make this binding mutable
30+
//~| SUGGESTION mut y
3031
y //~ error: cannot borrow immutable local variable `y` as mutable
3132
//~| cannot borrow
3233
+=

src/test/ui/augmented-assignments.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
error[E0596]: cannot borrow immutable local variable `y` as mutable
2-
--> $DIR/augmented-assignments.rs:30:5
2+
--> $DIR/augmented-assignments.rs:31:5
33
|
44
LL | let y = Int(2);
5-
| - consider changing this to `mut y`
6-
LL | //~^ consider changing this to `mut y`
5+
| - help: make this binding mutable: `mut y`
6+
...
77
LL | y //~ error: cannot borrow immutable local variable `y` as mutable
88
| ^ cannot borrow mutably
99

src/test/ui/borrowck/borrowck-access-permissions.ast.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0596]: cannot borrow immutable local variable `x` as mutable
22
--> $DIR/borrowck-access-permissions.rs:22:24
33
|
44
LL | let x = 1;
5-
| - consider changing this to `mut x`
5+
| - help: make this binding mutable: `mut x`
66
...
77
LL | let _y1 = &mut x; //[ast]~ ERROR [E0596]
88
| ^ cannot borrow mutably
@@ -17,7 +17,7 @@ error[E0596]: cannot borrow immutable `Box` content `*box_x` as mutable
1717
--> $DIR/borrowck-access-permissions.rs:37:24
1818
|
1919
LL | let box_x = Box::new(1);
20-
| ----- consider changing this to `mut box_x`
20+
| ----- help: make this binding mutable: `mut box_x`
2121
...
2222
LL | let _y1 = &mut *box_x; //[ast]~ ERROR [E0596]
2323
| ^^^^^^ cannot borrow as mutable

src/test/ui/borrowck/borrowck-argument.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,23 @@ error[E0596]: cannot borrow immutable argument `arg` as mutable
22
--> $DIR/borrowck-argument.rs:20:5
33
|
44
LL | fn func(arg: S) {
5-
| --- consider changing this to `mut arg`
5+
| --- help: make this binding mutable: `mut arg`
66
LL | arg.mutate(); //~ ERROR: cannot borrow immutable argument
77
| ^^^ cannot borrow mutably
88

99
error[E0596]: cannot borrow immutable argument `arg` as mutable
1010
--> $DIR/borrowck-argument.rs:25:9
1111
|
1212
LL | fn method(&self, arg: S) {
13-
| --- consider changing this to `mut arg`
13+
| --- help: make this binding mutable: `mut arg`
1414
LL | arg.mutate(); //~ ERROR: cannot borrow immutable argument
1515
| ^^^ cannot borrow mutably
1616

1717
error[E0596]: cannot borrow immutable argument `arg` as mutable
1818
--> $DIR/borrowck-argument.rs:31:9
1919
|
2020
LL | fn default(&self, arg: S) {
21-
| --- consider changing this to `mut arg`
21+
| --- help: make this binding mutable: `mut arg`
2222
LL | arg.mutate(); //~ ERROR: cannot borrow immutable argument
2323
| ^^^ cannot borrow mutably
2424

@@ -28,7 +28,7 @@ error[E0596]: cannot borrow immutable argument `arg` as mutable
2828
LL | (|arg: S| { arg.mutate() })(s); //~ ERROR: cannot borrow immutable argument
2929
| --- ^^^ cannot borrow mutably
3030
| |
31-
| consider changing this to `mut arg`
31+
| help: make this binding mutable: `mut arg`
3232

3333
error: aborting due to 4 previous errors
3434

src/test/ui/borrowck/borrowck-asm.ast.nll.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ LL | let x = 3;
2828
| -
2929
| |
3030
| first assignment to `x`
31-
| consider changing this to `mut x`
31+
| help: make this binding mutable: `mut x`
3232
LL | unsafe {
3333
LL | asm!("nop" : "=r"(x)); //[ast]~ ERROR cannot assign twice
3434
| ^^^^^^^^^^^^^^^^^^^^^^ cannot assign twice to immutable variable
@@ -40,7 +40,7 @@ LL | let x = 3;
4040
| -
4141
| |
4242
| first assignment to `x`
43-
| consider changing this to `mut x`
43+
| help: make this binding mutable: `mut x`
4444
LL | unsafe {
4545
LL | asm!("nop" : "+r"(x)); //[ast]~ ERROR cannot assign twice
4646
| ^^^^^^^^^^^^^^^^^^^^^^ cannot assign twice to immutable variable

src/test/ui/borrowck/borrowck-asm.mir.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ LL | let x = 3;
2828
| -
2929
| |
3030
| first assignment to `x`
31-
| consider changing this to `mut x`
31+
| help: make this binding mutable: `mut x`
3232
LL | unsafe {
3333
LL | asm!("nop" : "=r"(x)); //[ast]~ ERROR cannot assign twice
3434
| ^^^^^^^^^^^^^^^^^^^^^^ cannot assign twice to immutable variable
@@ -40,7 +40,7 @@ LL | let x = 3;
4040
| -
4141
| |
4242
| first assignment to `x`
43-
| consider changing this to `mut x`
43+
| help: make this binding mutable: `mut x`
4444
LL | unsafe {
4545
LL | asm!("nop" : "+r"(x)); //[ast]~ ERROR cannot assign twice
4646
| ^^^^^^^^^^^^^^^^^^^^^^ cannot assign twice to immutable variable

src/test/ui/borrowck/borrowck-auto-mut-ref-to-immut-var.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0596]: cannot borrow immutable local variable `x` as mutable
22
--> $DIR/borrowck-auto-mut-ref-to-immut-var.rs:25:5
33
|
44
LL | let x = Foo { x: 3 };
5-
| - consider changing this to `mut x`
5+
| - help: make this binding mutable: `mut x`
66
LL | x.printme(); //~ ERROR cannot borrow
77
| ^ cannot borrow mutably
88

src/test/ui/borrowck/borrowck-borrow-from-owned-ptr.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ error[E0596]: cannot borrow field `foo.bar1` of immutable binding as mutable
147147
--> $DIR/borrowck-borrow-from-owned-ptr.rs:132:21
148148
|
149149
LL | let foo = make_foo();
150-
| --- consider changing this to `mut foo`
150+
| --- help: make this binding mutable: `mut foo`
151151
LL | let bar1 = &mut foo.bar1; //~ ERROR cannot borrow
152152
| ^^^^^^^^ cannot mutably borrow field of immutable binding
153153

src/test/ui/borrowck/borrowck-borrow-from-stack-variable.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ error[E0596]: cannot borrow field `foo.bar1` of immutable binding as mutable
114114
--> $DIR/borrowck-borrow-from-stack-variable.rs:130:21
115115
|
116116
LL | let foo = make_foo();
117-
| --- consider changing this to `mut foo`
117+
| --- help: make this binding mutable: `mut foo`
118118
LL | let bar1 = &mut foo.bar1; //~ ERROR cannot borrow
119119
| ^^^^^^^^ cannot mutably borrow field of immutable binding
120120

src/test/ui/borrowck/borrowck-borrow-immut-deref-of-box-as-mut.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0596]: cannot borrow immutable `Box` content `*a` as mutable
22
--> $DIR/borrowck-borrow-immut-deref-of-box-as-mut.rs:22:5
33
|
44
LL | let a: Box<_> = box A;
5-
| - consider changing this to `mut a`
5+
| - help: make this binding mutable: `mut a`
66
LL | a.foo();
77
| ^ cannot borrow as mutable
88

src/test/ui/borrowck/borrowck-match-binding-is-assignment.ast.nll.stderr

+5-5
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ LL | x => {
55
| -
66
| |
77
| first assignment to `x`
8-
| consider changing this to `mut x`
8+
| help: make this binding mutable: `mut x`
99
LL | x += 1; //[ast]~ ERROR cannot assign twice to immutable variable `x`
1010
| ^^^^^^ cannot assign twice to immutable variable
1111

@@ -16,7 +16,7 @@ LL | E::Foo(x) => {
1616
| -
1717
| |
1818
| first assignment to `x`
19-
| consider changing this to `mut x`
19+
| help: make this binding mutable: `mut x`
2020
LL | x += 1; //[ast]~ ERROR cannot assign twice to immutable variable `x`
2121
| ^^^^^^ cannot assign twice to immutable variable
2222

@@ -27,7 +27,7 @@ LL | S { bar: x } => {
2727
| -
2828
| |
2929
| first assignment to `x`
30-
| consider changing this to `mut x`
30+
| help: make this binding mutable: `mut x`
3131
LL | x += 1; //[ast]~ ERROR cannot assign twice to immutable variable `x`
3232
| ^^^^^^ cannot assign twice to immutable variable
3333

@@ -38,7 +38,7 @@ LL | (x,) => {
3838
| -
3939
| |
4040
| first assignment to `x`
41-
| consider changing this to `mut x`
41+
| help: make this binding mutable: `mut x`
4242
LL | x += 1; //[ast]~ ERROR cannot assign twice to immutable variable `x`
4343
| ^^^^^^ cannot assign twice to immutable variable
4444

@@ -49,7 +49,7 @@ LL | [x,_,_] => {
4949
| -
5050
| |
5151
| first assignment to `x`
52-
| consider changing this to `mut x`
52+
| help: make this binding mutable: `mut x`
5353
LL | x += 1; //[ast]~ ERROR cannot assign twice to immutable variable `x`
5454
| ^^^^^^ cannot assign twice to immutable variable
5555

src/test/ui/borrowck/borrowck-match-binding-is-assignment.mir.stderr

+5-5
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ LL | x => {
55
| -
66
| |
77
| first assignment to `x`
8-
| consider changing this to `mut x`
8+
| help: make this binding mutable: `mut x`
99
LL | x += 1; //[ast]~ ERROR cannot assign twice to immutable variable `x`
1010
| ^^^^^^ cannot assign twice to immutable variable
1111

@@ -16,7 +16,7 @@ LL | E::Foo(x) => {
1616
| -
1717
| |
1818
| first assignment to `x`
19-
| consider changing this to `mut x`
19+
| help: make this binding mutable: `mut x`
2020
LL | x += 1; //[ast]~ ERROR cannot assign twice to immutable variable `x`
2121
| ^^^^^^ cannot assign twice to immutable variable
2222

@@ -27,7 +27,7 @@ LL | S { bar: x } => {
2727
| -
2828
| |
2929
| first assignment to `x`
30-
| consider changing this to `mut x`
30+
| help: make this binding mutable: `mut x`
3131
LL | x += 1; //[ast]~ ERROR cannot assign twice to immutable variable `x`
3232
| ^^^^^^ cannot assign twice to immutable variable
3333

@@ -38,7 +38,7 @@ LL | (x,) => {
3838
| -
3939
| |
4040
| first assignment to `x`
41-
| consider changing this to `mut x`
41+
| help: make this binding mutable: `mut x`
4242
LL | x += 1; //[ast]~ ERROR cannot assign twice to immutable variable `x`
4343
| ^^^^^^ cannot assign twice to immutable variable
4444

@@ -49,7 +49,7 @@ LL | [x,_,_] => {
4949
| -
5050
| |
5151
| first assignment to `x`
52-
| consider changing this to `mut x`
52+
| help: make this binding mutable: `mut x`
5353
LL | x += 1; //[ast]~ ERROR cannot assign twice to immutable variable `x`
5454
| ^^^^^^ cannot assign twice to immutable variable
5555

src/test/ui/borrowck/borrowck-mut-addr-of-imm-var.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0596]: cannot borrow immutable local variable `x` as mutable
22
--> $DIR/borrowck-mut-addr-of-imm-var.rs:13:30
33
|
44
LL | let x: isize = 3;
5-
| - consider changing this to `mut x`
5+
| - help: make this binding mutable: `mut x`
66
LL | let y: &mut isize = &mut x; //~ ERROR cannot borrow
77
| ^ cannot borrow mutably
88

src/test/ui/borrowck/borrowck-mut-slice-of-imm-vec.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0596]: cannot borrow immutable local variable `v` as mutable
22
--> $DIR/borrowck-mut-slice-of-imm-vec.rs:17:16
33
|
44
LL | let v = vec![1, 2, 3];
5-
| - consider changing this to `mut v`
5+
| - help: make this binding mutable: `mut v`
66
LL | write(&mut v); //~ ERROR cannot borrow
77
| ^ cannot borrow mutably
88

src/test/ui/borrowck/borrowck-overloaded-call.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ error[E0596]: cannot borrow immutable local variable `s` as mutable
1212
--> $DIR/borrowck-overloaded-call.rs:77:5
1313
|
1414
LL | let s = SFnMut {
15-
| - consider changing this to `mut s`
15+
| - help: make this binding mutable: `mut s`
1616
...
1717
LL | s(3); //~ ERROR cannot borrow immutable local variable `s` as mutable
1818
| ^ cannot borrow mutably

src/test/ui/borrowck/borrowck-ref-mut-of-imm.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0596]: cannot borrow field `(x as std::prelude::v1::Some).0` of immutable
22
--> $DIR/borrowck-ref-mut-of-imm.rs:14:12
33
|
44
LL | fn destructure(x: Option<isize>) -> isize {
5-
| - consider changing this to `mut x`
5+
| - help: make this binding mutable: `mut x`
66
...
77
LL | Some(ref mut v) => *v //~ ERROR cannot borrow
88
| ^^^^^^^^^ cannot mutably borrow field of immutable binding

0 commit comments

Comments
 (0)