diff --git a/src/librustc_borrowck/borrowck/mod.rs b/src/librustc_borrowck/borrowck/mod.rs index 7ad5e8fc9ee3..237656087922 100644 --- a/src/librustc_borrowck/borrowck/mod.rs +++ b/src/librustc_borrowck/borrowck/mod.rs @@ -993,16 +993,23 @@ impl<'a, 'tcx> BorrowckCtxt<'a, 'tcx> { if let Categorization::Local(local_id) = err.cmt.cat { let span = self.tcx.map.span(local_id); if let Ok(snippet) = self.tcx.sess.codemap().span_to_snippet(span) { - if snippet.starts_with("ref ") { - db.span_label(span, - &format!("use `{}` here to make mutable", - snippet.replace("ref ", "ref mut "))); - } else if snippet != "self" { - db.span_label(span, - &format!("use `mut {}` here to make mutable", snippet)); + if snippet.starts_with("ref mut ") || snippet.starts_with("&mut ") { + db.span_label(error_span, &format!("cannot reborrow mutably")); + db.span_label(error_span, &format!("try removing `&mut` here")); + } else { + if snippet.starts_with("ref ") { + db.span_label(span, + &format!("use `{}` here to make mutable", + snippet.replace("ref ", "ref mut "))); + } else if snippet != "self" { + db.span_label(span, + &format!("use `mut {}` here to make mutable", snippet)); + } + db.span_label(error_span, &format!("cannot borrow mutably")); } + } else { + db.span_label(error_span, &format!("cannot borrow mutably")); } - db.span_label(error_span, &format!("cannot borrow mutably")); } } } diff --git a/src/test/compile-fail/issue-31424.rs b/src/test/ui/did_you_mean/issue-31424.rs similarity index 79% rename from src/test/compile-fail/issue-31424.rs rename to src/test/ui/did_you_mean/issue-31424.rs index 262efab22a29..374d06bb71d2 100644 --- a/src/test/compile-fail/issue-31424.rs +++ b/src/test/ui/did_you_mean/issue-31424.rs @@ -15,15 +15,12 @@ struct Struct; impl Struct { fn foo(&mut self) { (&mut self).bar(); - //~^ ERROR cannot borrow immutable argument `self` as mutable - // ... and no SUGGESTION that suggests `&mut mut self` } // In this case we could keep the suggestion, but to distinguish the // two cases is pretty hard. It's an obscure case anyway. fn bar(self: &mut Self) { (&mut self).bar(); - //~^ ERROR cannot borrow immutable argument `self` as mutable } } diff --git a/src/test/ui/did_you_mean/issue-31424.stderr b/src/test/ui/did_you_mean/issue-31424.stderr new file mode 100644 index 000000000000..4873acf551eb --- /dev/null +++ b/src/test/ui/did_you_mean/issue-31424.stderr @@ -0,0 +1,17 @@ +error: cannot borrow immutable argument `self` as mutable + --> $DIR/issue-31424.rs:17:15 + | +17 | (&mut self).bar(); + | ^^^^ + | | + | try removing `&mut` here + | cannot reborrow mutably + +error: cannot borrow immutable argument `self` as mutable + --> $DIR/issue-31424.rs:23:15 + | +23 | (&mut self).bar(); + | ^^^^ cannot borrow mutably + +error: aborting due to 2 previous errors + diff --git a/src/test/ui/did_you_mean/issue-34126.rs b/src/test/ui/did_you_mean/issue-34126.rs new file mode 100644 index 000000000000..9523e6bbf383 --- /dev/null +++ b/src/test/ui/did_you_mean/issue-34126.rs @@ -0,0 +1,23 @@ +// Copyright 2016 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 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +struct Z { } + +impl Z { + fn run(&self, z: &mut Z) { } + fn start(&mut self) { + self.run(&mut self); + } +} + +fn main() { + let mut z = Z {}; + z.start(); +} diff --git a/src/test/ui/did_you_mean/issue-34126.stderr b/src/test/ui/did_you_mean/issue-34126.stderr new file mode 100644 index 000000000000..8011298c80cd --- /dev/null +++ b/src/test/ui/did_you_mean/issue-34126.stderr @@ -0,0 +1,11 @@ +error: cannot borrow immutable argument `self` as mutable + --> $DIR/issue-34126.rs:16:23 + | +16 | self.run(&mut self); + | ^^^^ + | | + | try removing `&mut` here + | cannot reborrow mutably + +error: aborting due to previous error + diff --git a/src/test/ui/did_you_mean/issue-34337.rs b/src/test/ui/did_you_mean/issue-34337.rs new file mode 100644 index 000000000000..42853a5d83db --- /dev/null +++ b/src/test/ui/did_you_mean/issue-34337.rs @@ -0,0 +1,17 @@ +// Copyright 2016 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 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +fn get(key: &mut String) { } + +fn main() { + let mut v: Vec = Vec::new(); + let ref mut key = v[0]; + get(&mut key); +} diff --git a/src/test/ui/did_you_mean/issue-34337.stderr b/src/test/ui/did_you_mean/issue-34337.stderr new file mode 100644 index 000000000000..d658912835b9 --- /dev/null +++ b/src/test/ui/did_you_mean/issue-34337.stderr @@ -0,0 +1,11 @@ +error: cannot borrow immutable local variable `key` as mutable + --> $DIR/issue-34337.rs:16:14 + | +16 | get(&mut key); + | ^^^ + | | + | try removing `&mut` here + | cannot reborrow mutably + +error: aborting due to previous error + diff --git a/src/test/ui/did_you_mean/issue-37139.rs b/src/test/ui/did_you_mean/issue-37139.rs new file mode 100644 index 000000000000..65181768053c --- /dev/null +++ b/src/test/ui/did_you_mean/issue-37139.rs @@ -0,0 +1,25 @@ +// Copyright 2016 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 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +enum TestEnum { + Item(i32), +} + +fn test(_: &mut i32) { +} + +fn main() { + let mut x = TestEnum::Item(10); + match x { + TestEnum::Item(ref mut x) => { + test(&mut x); + } + } +} diff --git a/src/test/ui/did_you_mean/issue-37139.stderr b/src/test/ui/did_you_mean/issue-37139.stderr new file mode 100644 index 000000000000..b1a8231fdb6c --- /dev/null +++ b/src/test/ui/did_you_mean/issue-37139.stderr @@ -0,0 +1,11 @@ +error: cannot borrow immutable local variable `x` as mutable + --> $DIR/issue-37139.rs:22:23 + | +22 | test(&mut x); + | ^ + | | + | try removing `&mut` here + | cannot reborrow mutably + +error: aborting due to previous error +