Skip to content

Commit 026add5

Browse files
authored
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
2 parents acce384 + 1a5456b commit 026add5

File tree

9 files changed

+130
-11
lines changed

9 files changed

+130
-11
lines changed

src/librustc_borrowck/borrowck/mod.rs

+15-8
Original file line numberDiff line numberDiff line change
@@ -993,16 +993,23 @@ impl<'a, 'tcx> BorrowckCtxt<'a, 'tcx> {
993993
if let Categorization::Local(local_id) = err.cmt.cat {
994994
let span = self.tcx.map.span(local_id);
995995
if let Ok(snippet) = self.tcx.sess.codemap().span_to_snippet(span) {
996-
if snippet.starts_with("ref ") {
997-
db.span_label(span,
998-
&format!("use `{}` here to make mutable",
999-
snippet.replace("ref ", "ref mut ")));
1000-
} else if snippet != "self" {
1001-
db.span_label(span,
1002-
&format!("use `mut {}` here to make mutable", snippet));
996+
if snippet.starts_with("ref mut ") || snippet.starts_with("&mut ") {
997+
db.span_label(error_span, &format!("cannot reborrow mutably"));
998+
db.span_label(error_span, &format!("try removing `&mut` here"));
999+
} else {
1000+
if snippet.starts_with("ref ") {
1001+
db.span_label(span,
1002+
&format!("use `{}` here to make mutable",
1003+
snippet.replace("ref ", "ref mut ")));
1004+
} else if snippet != "self" {
1005+
db.span_label(span,
1006+
&format!("use `mut {}` here to make mutable", snippet));
1007+
}
1008+
db.span_label(error_span, &format!("cannot borrow mutably"));
10031009
}
1010+
} else {
1011+
db.span_label(error_span, &format!("cannot borrow mutably"));
10041012
}
1005-
db.span_label(error_span, &format!("cannot borrow mutably"));
10061013
}
10071014
}
10081015
}

src/test/compile-fail/issue-31424.rs renamed to src/test/ui/did_you_mean/issue-31424.rs

-3
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,12 @@ struct Struct;
1515
impl Struct {
1616
fn foo(&mut self) {
1717
(&mut self).bar();
18-
//~^ ERROR cannot borrow immutable argument `self` as mutable
19-
// ... and no SUGGESTION that suggests `&mut mut self`
2018
}
2119

2220
// In this case we could keep the suggestion, but to distinguish the
2321
// two cases is pretty hard. It's an obscure case anyway.
2422
fn bar(self: &mut Self) {
2523
(&mut self).bar();
26-
//~^ ERROR cannot borrow immutable argument `self` as mutable
2724
}
2825
}
2926

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
error: cannot borrow immutable argument `self` as mutable
2+
--> $DIR/issue-31424.rs:17:15
3+
|
4+
17 | (&mut self).bar();
5+
| ^^^^
6+
| |
7+
| try removing `&mut` here
8+
| cannot reborrow mutably
9+
10+
error: cannot borrow immutable argument `self` as mutable
11+
--> $DIR/issue-31424.rs:23:15
12+
|
13+
23 | (&mut self).bar();
14+
| ^^^^ cannot borrow mutably
15+
16+
error: aborting due to 2 previous errors
17+
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
struct Z { }
12+
13+
impl Z {
14+
fn run(&self, z: &mut Z) { }
15+
fn start(&mut self) {
16+
self.run(&mut self);
17+
}
18+
}
19+
20+
fn main() {
21+
let mut z = Z {};
22+
z.start();
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
error: cannot borrow immutable argument `self` as mutable
2+
--> $DIR/issue-34126.rs:16:23
3+
|
4+
16 | self.run(&mut self);
5+
| ^^^^
6+
| |
7+
| try removing `&mut` here
8+
| cannot reborrow mutably
9+
10+
error: aborting due to previous error
11+
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
fn get(key: &mut String) { }
12+
13+
fn main() {
14+
let mut v: Vec<String> = Vec::new();
15+
let ref mut key = v[0];
16+
get(&mut key);
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
error: cannot borrow immutable local variable `key` as mutable
2+
--> $DIR/issue-34337.rs:16:14
3+
|
4+
16 | get(&mut key);
5+
| ^^^
6+
| |
7+
| try removing `&mut` here
8+
| cannot reborrow mutably
9+
10+
error: aborting due to previous error
11+
+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
enum TestEnum {
12+
Item(i32),
13+
}
14+
15+
fn test(_: &mut i32) {
16+
}
17+
18+
fn main() {
19+
let mut x = TestEnum::Item(10);
20+
match x {
21+
TestEnum::Item(ref mut x) => {
22+
test(&mut x);
23+
}
24+
}
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
error: cannot borrow immutable local variable `x` as mutable
2+
--> $DIR/issue-37139.rs:22:23
3+
|
4+
22 | test(&mut x);
5+
| ^
6+
| |
7+
| try removing `&mut` here
8+
| cannot reborrow mutably
9+
10+
error: aborting due to previous error
11+

0 commit comments

Comments
 (0)