Auto merge of #37531 - estebank:fix-ref-mut-mut, r=jonathandturner
Fix invalid "ref mut mut" sugestion Change output from: ```nocode error: cannot borrow immutable local variable `x` as mutable --> <anon>:12:23 | 11 | TestEnum::Item(ref mut x) => { | --------- use `ref mut mut x` here to make mutable 12 | test(&mut x); | ^ cannot borrow mutably ``` to ```nocode error: cannot borrow immutable local variable `x` as mutable --> <anon>:12:23 | 12 | test(&mut x); | ^ | | | cannot reborrow mutably | try removing `&mut` here ``` Fixes #37139, #34337, #34126
This commit is contained in:
commit
026add5f06
@ -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"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
}
|
||||
|
17
src/test/ui/did_you_mean/issue-31424.stderr
Normal file
17
src/test/ui/did_you_mean/issue-31424.stderr
Normal file
@ -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
|
||||
|
23
src/test/ui/did_you_mean/issue-34126.rs
Normal file
23
src/test/ui/did_you_mean/issue-34126.rs
Normal file
@ -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 <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.
|
||||
|
||||
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();
|
||||
}
|
11
src/test/ui/did_you_mean/issue-34126.stderr
Normal file
11
src/test/ui/did_you_mean/issue-34126.stderr
Normal file
@ -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
|
||||
|
17
src/test/ui/did_you_mean/issue-34337.rs
Normal file
17
src/test/ui/did_you_mean/issue-34337.rs
Normal file
@ -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 <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 get(key: &mut String) { }
|
||||
|
||||
fn main() {
|
||||
let mut v: Vec<String> = Vec::new();
|
||||
let ref mut key = v[0];
|
||||
get(&mut key);
|
||||
}
|
11
src/test/ui/did_you_mean/issue-34337.stderr
Normal file
11
src/test/ui/did_you_mean/issue-34337.stderr
Normal file
@ -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
|
||||
|
25
src/test/ui/did_you_mean/issue-37139.rs
Normal file
25
src/test/ui/did_you_mean/issue-37139.rs
Normal file
@ -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 <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.
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
11
src/test/ui/did_you_mean/issue-37139.stderr
Normal file
11
src/test/ui/did_you_mean/issue-37139.stderr
Normal file
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user