Skip to content

use structured suggestion for "missing mut" label #54157

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

Merged
merged 1 commit into from
Sep 16, 2018
Merged
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
8 changes: 5 additions & 3 deletions src/librustc_borrowck/borrowck/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ use rustc_data_structures::sync::Lrc;
use std::hash::{Hash, Hasher};
use syntax::ast;
use syntax_pos::{MultiSpan, Span};
use errors::{DiagnosticBuilder, DiagnosticId};
use errors::{Applicability, DiagnosticBuilder, DiagnosticId};

use rustc::hir;
use rustc::hir::intravisit::{self, Visitor};
Expand Down Expand Up @@ -1299,9 +1299,11 @@ impl<'a, 'tcx> BorrowckCtxt<'a, 'tcx> {
snippet
);
} else {
db.span_label(
db.span_suggestion_with_applicability(
let_span,
format!("consider changing this to `mut {}`", snippet),
"make this binding mutable",
format!("mut {}", snippet),
Applicability::MachineApplicable,
);
}
}
Expand Down
8 changes: 5 additions & 3 deletions src/librustc_mir/borrow_check/error_reporting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use rustc::mir::{ProjectionElem, Rvalue, Statement, StatementKind};
use rustc::ty;
use rustc_data_structures::fx::FxHashSet;
use rustc_data_structures::sync::Lrc;
use rustc_errors::DiagnosticBuilder;
use rustc_errors::{Applicability, DiagnosticBuilder};
use syntax_pos::Span;

use super::borrow_set::BorrowData;
Expand Down Expand Up @@ -690,9 +690,11 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
if let Some(decl) = local_decl {
if let Some(name) = decl.name {
if decl.can_be_made_mutable() {
err.span_label(
err.span_suggestion_with_applicability(
decl.source_info.span,
format!("consider changing this to `mut {}`", name),
"make this binding mutable",
format!("mut {}", name),
Applicability::MachineApplicable,
);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/E0596.ast.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ error[E0596]: cannot borrow immutable local variable `x` as mutable
--> $DIR/E0596.rs:16:18
|
LL | let x = 1;
| - consider changing this to `mut x`
| - help: make this binding mutable: `mut x`
LL | let y = &mut x; //[ast]~ ERROR [E0596]
| ^ cannot borrow mutably

Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/asm/asm-out-assign-imm.nll.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ error[E0384]: cannot assign twice to immutable variable `x`
--> $DIR/asm-out-assign-imm.rs:34:9
|
LL | let x: isize;
| - consider changing this to `mut x`
| - help: make this binding mutable: `mut x`
LL | x = 1;
| ----- first assignment to `x`
...
Expand Down
6 changes: 3 additions & 3 deletions src/test/ui/assign-imm-local-twice.ast.nll.stderr
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
error[E0384]: cannot assign twice to immutable variable `v`
--> $DIR/assign-imm-local-twice.rs:20:5
--> $DIR/assign-imm-local-twice.rs:21:5
|
LL | let v: isize;
| - consider changing this to `mut v`
LL | //[mir]~^ NOTE consider changing this to `mut v`
| - help: make this binding mutable: `mut v`
...
LL | v = 1; //[ast]~ NOTE first assignment
| ----- first assignment to `v`
...
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/assign-imm-local-twice.ast.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error[E0384]: cannot assign twice to immutable variable `v`
--> $DIR/assign-imm-local-twice.rs:20:5
--> $DIR/assign-imm-local-twice.rs:21:5
|
LL | v = 1; //[ast]~ NOTE first assignment
| ----- first assignment to `v`
Expand Down
6 changes: 3 additions & 3 deletions src/test/ui/assign-imm-local-twice.mir.stderr
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
error[E0384]: cannot assign twice to immutable variable `v`
--> $DIR/assign-imm-local-twice.rs:20:5
--> $DIR/assign-imm-local-twice.rs:21:5
|
LL | let v: isize;
| - consider changing this to `mut v`
LL | //[mir]~^ NOTE consider changing this to `mut v`
| - help: make this binding mutable: `mut v`
...
LL | v = 1; //[ast]~ NOTE first assignment
| ----- first assignment to `v`
...
Expand Down
3 changes: 2 additions & 1 deletion src/test/ui/assign-imm-local-twice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@

fn test() {
let v: isize;
//[mir]~^ NOTE consider changing this to `mut v`
//[mir]~^ HELP make this binding mutable
//[mir]~| SUGGESTION mut v
v = 1; //[ast]~ NOTE first assignment
//[mir]~^ NOTE first assignment
println!("v={}", v);
Expand Down
4 changes: 2 additions & 2 deletions src/test/ui/augmented-assignments.nll.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ LL | | x; //~ value moved here
| borrow later used here

error[E0596]: cannot borrow `y` as mutable, as it is not declared as mutable
--> $DIR/augmented-assignments.rs:30:5
--> $DIR/augmented-assignments.rs:31:5
|
LL | let y = Int(2);
| - help: consider changing this to be mutable: `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
3 changes: 2 additions & 1 deletion src/test/ui/augmented-assignments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ fn main() {
x; //~ value moved here

let y = Int(2);
//~^ consider changing this to `mut y`
//~^ HELP make this binding mutable
//~| SUGGESTION mut y
y //~ error: cannot borrow immutable local variable `y` as mutable
//~| cannot borrow
+=
Expand Down
6 changes: 3 additions & 3 deletions src/test/ui/augmented-assignments.stderr
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
error[E0596]: cannot borrow immutable local variable `y` as mutable
--> $DIR/augmented-assignments.rs:30:5
--> $DIR/augmented-assignments.rs:31:5
|
LL | let y = Int(2);
| - consider changing this to `mut y`
LL | //~^ consider changing this to `mut y`
| - help: make this binding mutable: `mut y`
...
LL | y //~ error: cannot borrow immutable local variable `y` as mutable
| ^ cannot borrow mutably

Expand Down
4 changes: 2 additions & 2 deletions src/test/ui/borrowck/borrowck-access-permissions.ast.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ error[E0596]: cannot borrow immutable local variable `x` as mutable
--> $DIR/borrowck-access-permissions.rs:22:24
|
LL | let x = 1;
| - consider changing this to `mut x`
| - help: make this binding mutable: `mut x`
...
LL | let _y1 = &mut x; //[ast]~ ERROR [E0596]
| ^ cannot borrow mutably
Expand All @@ -17,7 +17,7 @@ error[E0596]: cannot borrow immutable `Box` content `*box_x` as mutable
--> $DIR/borrowck-access-permissions.rs:37:24
|
LL | let box_x = Box::new(1);
| ----- consider changing this to `mut box_x`
| ----- help: make this binding mutable: `mut box_x`
...
LL | let _y1 = &mut *box_x; //[ast]~ ERROR [E0596]
| ^^^^^^ cannot borrow as mutable
Expand Down
8 changes: 4 additions & 4 deletions src/test/ui/borrowck/borrowck-argument.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,23 @@ error[E0596]: cannot borrow immutable argument `arg` as mutable
--> $DIR/borrowck-argument.rs:20:5
|
LL | fn func(arg: S) {
| --- consider changing this to `mut arg`
| --- help: make this binding mutable: `mut arg`
LL | arg.mutate(); //~ ERROR: cannot borrow immutable argument
| ^^^ cannot borrow mutably

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

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

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

error: aborting due to 4 previous errors

Expand Down
4 changes: 2 additions & 2 deletions src/test/ui/borrowck/borrowck-asm.ast.nll.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ LL | let x = 3;
| -
| |
| first assignment to `x`
| consider changing this to `mut x`
| help: make this binding mutable: `mut x`
LL | unsafe {
LL | asm!("nop" : "=r"(x)); //[ast]~ ERROR cannot assign twice
| ^^^^^^^^^^^^^^^^^^^^^^ cannot assign twice to immutable variable
Expand All @@ -40,7 +40,7 @@ LL | let x = 3;
| -
| |
| first assignment to `x`
| consider changing this to `mut x`
| help: make this binding mutable: `mut x`
LL | unsafe {
LL | asm!("nop" : "+r"(x)); //[ast]~ ERROR cannot assign twice
| ^^^^^^^^^^^^^^^^^^^^^^ cannot assign twice to immutable variable
Expand Down
4 changes: 2 additions & 2 deletions src/test/ui/borrowck/borrowck-asm.mir.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ LL | let x = 3;
| -
| |
| first assignment to `x`
| consider changing this to `mut x`
| help: make this binding mutable: `mut x`
LL | unsafe {
LL | asm!("nop" : "=r"(x)); //[ast]~ ERROR cannot assign twice
| ^^^^^^^^^^^^^^^^^^^^^^ cannot assign twice to immutable variable
Expand All @@ -40,7 +40,7 @@ LL | let x = 3;
| -
| |
| first assignment to `x`
| consider changing this to `mut x`
| help: make this binding mutable: `mut x`
LL | unsafe {
LL | asm!("nop" : "+r"(x)); //[ast]~ ERROR cannot assign twice
| ^^^^^^^^^^^^^^^^^^^^^^ cannot assign twice to immutable variable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ error[E0596]: cannot borrow immutable local variable `x` as mutable
--> $DIR/borrowck-auto-mut-ref-to-immut-var.rs:25:5
|
LL | let x = Foo { x: 3 };
| - consider changing this to `mut x`
| - help: make this binding mutable: `mut x`
LL | x.printme(); //~ ERROR cannot borrow
| ^ cannot borrow mutably

Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/borrowck/borrowck-borrow-from-owned-ptr.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ error[E0596]: cannot borrow field `foo.bar1` of immutable binding as mutable
--> $DIR/borrowck-borrow-from-owned-ptr.rs:132:21
|
LL | let foo = make_foo();
| --- consider changing this to `mut foo`
| --- help: make this binding mutable: `mut foo`
LL | let bar1 = &mut foo.bar1; //~ ERROR cannot borrow
| ^^^^^^^^ cannot mutably borrow field of immutable binding

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

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ LL | x => {
| -
| |
| first assignment to `x`
| consider changing this to `mut x`
| help: make this binding mutable: `mut x`
LL | x += 1; //[ast]~ ERROR cannot assign twice to immutable variable `x`
| ^^^^^^ cannot assign twice to immutable variable

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

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

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

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ LL | x => {
| -
| |
| first assignment to `x`
| consider changing this to `mut x`
| help: make this binding mutable: `mut x`
LL | x += 1; //[ast]~ ERROR cannot assign twice to immutable variable `x`
| ^^^^^^ cannot assign twice to immutable variable

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

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

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

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

Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/borrowck/borrowck-mut-addr-of-imm-var.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ error[E0596]: cannot borrow immutable local variable `x` as mutable
--> $DIR/borrowck-mut-addr-of-imm-var.rs:13:30
|
LL | let x: isize = 3;
| - consider changing this to `mut x`
| - help: make this binding mutable: `mut x`
LL | let y: &mut isize = &mut x; //~ ERROR cannot borrow
| ^ cannot borrow mutably

Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/borrowck/borrowck-mut-slice-of-imm-vec.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ error[E0596]: cannot borrow immutable local variable `v` as mutable
--> $DIR/borrowck-mut-slice-of-imm-vec.rs:17:16
|
LL | let v = vec![1, 2, 3];
| - consider changing this to `mut v`
| - help: make this binding mutable: `mut v`
LL | write(&mut v); //~ ERROR cannot borrow
| ^ cannot borrow mutably

Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/borrowck/borrowck-overloaded-call.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ error[E0596]: cannot borrow immutable local variable `s` as mutable
--> $DIR/borrowck-overloaded-call.rs:77:5
|
LL | let s = SFnMut {
| - consider changing this to `mut s`
| - help: make this binding mutable: `mut s`
...
LL | s(3); //~ ERROR cannot borrow immutable local variable `s` as mutable
| ^ cannot borrow mutably
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/borrowck/borrowck-ref-mut-of-imm.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ error[E0596]: cannot borrow field `(x as std::prelude::v1::Some).0` of immutable
--> $DIR/borrowck-ref-mut-of-imm.rs:14:12
|
LL | fn destructure(x: Option<isize>) -> isize {
| - consider changing this to `mut x`
| - help: make this binding mutable: `mut x`
...
LL | Some(ref mut v) => *v //~ ERROR cannot borrow
| ^^^^^^^^^ cannot mutably borrow field of immutable binding
Expand Down
Loading