From d193deedd106bb5d1e02ceebe48210d8512c2543 Mon Sep 17 00:00:00 2001 From: Santiago Pastorino Date: Thu, 31 May 2018 19:10:37 -0300 Subject: [PATCH] Suggest use mut when mutating a non mutable local --- src/librustc_mir/borrow_check/mod.rs | 9 +++---- src/test/ui/augmented-assignments.nll.stderr | 3 +++ src/test/ui/borrowck/issue-45983.nll.stderr | 2 ++ .../huge_multispan_highlight.nll.stderr | 3 +++ .../ui/did_you_mean/issue-34337.nll.stderr | 2 ++ .../ui/did_you_mean/issue-35937.nll.stderr | 4 ++-- .../ui/did_you_mean/issue-37139.nll.stderr | 2 ++ .../ui/did_you_mean/issue-38147-1.nll.stderr | 2 -- .../ui/did_you_mean/issue-38147-4.nll.stderr | 2 -- .../ui/did_you_mean/issue-39544.nll.stderr | 24 ++----------------- src/test/ui/issue-36400.nll.stderr | 4 ++-- .../span-covering-argument-1.nll.stderr | 2 ++ ...owck-call-is-borrow-issue-12224.nll.stderr | 2 -- .../borrowck-object-mutability.nll.stderr | 2 -- 14 files changed, 25 insertions(+), 38 deletions(-) diff --git a/src/librustc_mir/borrow_check/mod.rs b/src/librustc_mir/borrow_check/mod.rs index 5efbdeafd1bbf..29423c1d1688a 100644 --- a/src/librustc_mir/borrow_check/mod.rs +++ b/src/librustc_mir/borrow_check/mod.rs @@ -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, + format!("consider changing this to `mut {}`", local_decl.name.unwrap())); } } diff --git a/src/test/ui/augmented-assignments.nll.stderr b/src/test/ui/augmented-assignments.nll.stderr index deb2e7ed4a33d..4967e7d473e7a 100644 --- a/src/test/ui/augmented-assignments.nll.stderr +++ b/src/test/ui/augmented-assignments.nll.stderr @@ -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 diff --git a/src/test/ui/borrowck/issue-45983.nll.stderr b/src/test/ui/borrowck/issue-45983.nll.stderr index 1aec71fee347b..b1b7bd9a2ee69 100644 --- a/src/test/ui/borrowck/issue-45983.nll.stderr +++ b/src/test/ui/borrowck/issue-45983.nll.stderr @@ -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 diff --git a/src/test/ui/codemap_tests/huge_multispan_highlight.nll.stderr b/src/test/ui/codemap_tests/huge_multispan_highlight.nll.stderr index 4526616e48899..1042f089b18ee 100644 --- a/src/test/ui/codemap_tests/huge_multispan_highlight.nll.stderr +++ b/src/test/ui/codemap_tests/huge_multispan_highlight.nll.stderr @@ -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 diff --git a/src/test/ui/did_you_mean/issue-34337.nll.stderr b/src/test/ui/did_you_mean/issue-34337.nll.stderr index 258e1bb1ad7da..1a3c67613e45b 100644 --- a/src/test/ui/did_you_mean/issue-34337.nll.stderr +++ b/src/test/ui/did_you_mean/issue-34337.nll.stderr @@ -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 diff --git a/src/test/ui/did_you_mean/issue-35937.nll.stderr b/src/test/ui/did_you_mean/issue-35937.nll.stderr index 40b640b63cf32..dba52255c2f1b 100644 --- a/src/test/ui/did_you_mean/issue-35937.nll.stderr +++ b/src/test/ui/did_you_mean/issue-35937.nll.stderr @@ -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 diff --git a/src/test/ui/did_you_mean/issue-37139.nll.stderr b/src/test/ui/did_you_mean/issue-37139.nll.stderr index 29c7192a98bc6..f923173760f01 100644 --- a/src/test/ui/did_you_mean/issue-37139.nll.stderr +++ b/src/test/ui/did_you_mean/issue-37139.nll.stderr @@ -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` LL | test(&mut x); //~ ERROR cannot borrow immutable | ^^^^^^ cannot borrow as mutable diff --git a/src/test/ui/did_you_mean/issue-38147-1.nll.stderr b/src/test/ui/did_you_mean/issue-38147-1.nll.stderr index 8e4426779517c..564c132a68b49 100644 --- a/src/test/ui/did_you_mean/issue-38147-1.nll.stderr +++ b/src/test/ui/did_you_mean/issue-38147-1.nll.stderr @@ -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 diff --git a/src/test/ui/did_you_mean/issue-38147-4.nll.stderr b/src/test/ui/did_you_mean/issue-38147-4.nll.stderr index 6808222cc3241..2960af4917214 100644 --- a/src/test/ui/did_you_mean/issue-38147-4.nll.stderr +++ b/src/test/ui/did_you_mean/issue-38147-4.nll.stderr @@ -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 diff --git a/src/test/ui/did_you_mean/issue-39544.nll.stderr b/src/test/ui/did_you_mean/issue-39544.nll.stderr index f5f5b675e7727..056cfc1665ada 100644 --- a/src/test/ui/did_you_mean/issue-39544.nll.stderr +++ b/src/test/ui/did_you_mean/issue-39544.nll.stderr @@ -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 diff --git a/src/test/ui/issue-36400.nll.stderr b/src/test/ui/issue-36400.nll.stderr index 8045993747934..41fce83735c0c 100644 --- a/src/test/ui/issue-36400.nll.stderr +++ b/src/test/ui/issue-36400.nll.stderr @@ -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 diff --git a/src/test/ui/macros/span-covering-argument-1.nll.stderr b/src/test/ui/macros/span-covering-argument-1.nll.stderr index a12baab415907..06408bddef5ef 100644 --- a/src/test/ui/macros/span-covering-argument-1.nll.stderr +++ b/src/test/ui/macros/span-covering-argument-1.nll.stderr @@ -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 ... diff --git a/src/test/ui/span/borrowck-call-is-borrow-issue-12224.nll.stderr b/src/test/ui/span/borrowck-call-is-borrow-issue-12224.nll.stderr index 26e9ea4dc0bc8..2077d9598166e 100644 --- a/src/test/ui/span/borrowck-call-is-borrow-issue-12224.nll.stderr +++ b/src/test/ui/span/borrowck-call-is-borrow-issue-12224.nll.stderr @@ -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 diff --git a/src/test/ui/span/borrowck-object-mutability.nll.stderr b/src/test/ui/span/borrowck-object-mutability.nll.stderr index 9b5e084bd3751..7c5b1ddb234fc 100644 --- a/src/test/ui/span/borrowck-object-mutability.nll.stderr +++ b/src/test/ui/span/borrowck-object-mutability.nll.stderr @@ -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