Skip to content

Commit 91fbe5f

Browse files
authored
Unrolled build for rust-lang#136212
Rollup merge of rust-lang#136212 - estebank:span-tweak, r=petrochenkov Tweak `&mut self` suggestion span ``` error[E0596]: cannot borrow `*self.s` as mutable, as it is behind a `&` reference --> $DIR/issue-38147-1.rs:17:9 | LL | self.s.push('x'); | ^^^^^^ `self` is a `&` reference, so the data it refers to cannot be borrowed as mutable | help: consider changing this to be a mutable reference | LL | fn f(&mut self) { | +++ ``` Note the suggestion to add `mut` instead of replacing the entire `&self` with `&mut self`.
2 parents a1d7676 + 130b0d2 commit 91fbe5f

9 files changed

+21
-26
lines changed

compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs

+10-15
Original file line numberDiff line numberDiff line change
@@ -1140,10 +1140,9 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
11401140

11411141
let amp_mut_sugg = match *local_decl.local_info() {
11421142
LocalInfo::User(mir::BindingForm::ImplicitSelf(_)) => {
1143-
let suggestion = suggest_ampmut_self(self.infcx.tcx, decl_span);
1144-
let additional =
1145-
local_trait.map(|span| (span, suggest_ampmut_self(self.infcx.tcx, span)));
1146-
Some(AmpMutSugg { has_sugg: true, span: decl_span, suggestion, additional })
1143+
let (span, suggestion) = suggest_ampmut_self(self.infcx.tcx, decl_span);
1144+
let additional = local_trait.map(|span| suggest_ampmut_self(self.infcx.tcx, span));
1145+
Some(AmpMutSugg { has_sugg: true, span, suggestion, additional })
11471146
}
11481147

11491148
LocalInfo::User(mir::BindingForm::Var(mir::VarBindingForm {
@@ -1202,10 +1201,11 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
12021201
opt_ty_info: None,
12031202
..
12041203
})) => {
1205-
let sugg = suggest_ampmut_self(self.infcx.tcx, decl_span);
1204+
let (span, sugg) =
1205+
suggest_ampmut_self(self.infcx.tcx, decl_span);
12061206
Some(AmpMutSugg {
12071207
has_sugg: true,
1208-
span: decl_span,
1208+
span,
12091209
suggestion: sugg,
12101210
additional: None,
12111211
})
@@ -1461,17 +1461,12 @@ fn mut_borrow_of_mutable_ref(local_decl: &LocalDecl<'_>, local_name: Option<Symb
14611461
}
14621462
}
14631463

1464-
fn suggest_ampmut_self<'tcx>(tcx: TyCtxt<'tcx>, span: Span) -> String {
1464+
fn suggest_ampmut_self(tcx: TyCtxt<'_>, span: Span) -> (Span, String) {
14651465
match tcx.sess.source_map().span_to_snippet(span) {
1466-
Ok(snippet) => {
1467-
let lt_pos = snippet.find('\'');
1468-
if let Some(lt_pos) = lt_pos {
1469-
format!("&{}mut self", &snippet[lt_pos..snippet.len() - 4])
1470-
} else {
1471-
"&mut self".to_string()
1472-
}
1466+
Ok(snippet) if snippet.ends_with("self") => {
1467+
(span.with_hi(span.hi() - BytePos(4)).shrink_to_hi(), "mut ".to_string())
14731468
}
1474-
_ => "&mut self".to_string(),
1469+
_ => (span, "&mut self".to_string()),
14751470
}
14761471
}
14771472

tests/ui/borrowck/issue-93093.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ struct S {
44
}
55
impl S {
66
async fn bar(&self) { //~ HELP consider changing this to be a mutable reference
7-
//~| SUGGESTION &mut self
7+
//~| SUGGESTION mut
88
self.foo += 1; //~ ERROR cannot assign to `self.foo`, which is behind a `&` reference [E0594]
99
}
1010
}

tests/ui/borrowck/issue-93093.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ LL | self.foo += 1;
77
help: consider changing this to be a mutable reference
88
|
99
LL | async fn bar(&mut self) {
10-
| ~~~~~~~~~
10+
| +++
1111

1212
error: aborting due to 1 previous error
1313

tests/ui/borrowck/trait-impl-argument-difference-ice.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ LL | let a16 = self.read_word() as u16;
4141
help: consider changing this to be a mutable reference in the `impl` method and the `trait` definition
4242
|
4343
LL | extern "C" fn read_dword(&'_ mut self) -> u16 {
44-
| ~~~~~~~~~~~~
44+
| +++
4545

4646
error[E0596]: cannot borrow `*self` as mutable, as it is behind a `&` reference
4747
--> $DIR/trait-impl-argument-difference-ice.rs:18:19
@@ -52,7 +52,7 @@ LL | let b16 = self.read_word() as u16;
5252
help: consider changing this to be a mutable reference in the `impl` method and the `trait` definition
5353
|
5454
LL | extern "C" fn read_dword(&'_ mut self) -> u16 {
55-
| ~~~~~~~~~~~~
55+
| +++
5656

5757
error: aborting due to 5 previous errors; 1 warning emitted
5858

tests/ui/did_you_mean/issue-38147-1.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ LL | self.s.push('x');
77
help: consider changing this to be a mutable reference
88
|
99
LL | fn f(&mut self) {
10-
| ~~~~~~~~~
10+
| +++
1111

1212
error: aborting due to 1 previous error
1313

tests/ui/did_you_mean/issue-39544.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ LL | let _ = &mut self.x;
1818
help: consider changing this to be a mutable reference
1919
|
2020
LL | fn foo<'z>(&'z mut self) {
21-
| ~~~~~~~~~~~~
21+
| +++
2222

2323
error[E0596]: cannot borrow `self.x` as mutable, as it is behind a `&` reference
2424
--> $DIR/issue-39544.rs:20:17
@@ -29,7 +29,7 @@ LL | let _ = &mut self.x;
2929
help: consider changing this to be a mutable reference
3030
|
3131
LL | fn foo1(&mut self, other: &Z) {
32-
| ~~~~~~~~~
32+
| +++
3333

3434
error[E0596]: cannot borrow `other.x` as mutable, as it is behind a `&` reference
3535
--> $DIR/issue-39544.rs:21:17
@@ -51,7 +51,7 @@ LL | let _ = &mut self.x;
5151
help: consider changing this to be a mutable reference
5252
|
5353
LL | fn foo2<'a>(&'a mut self, other: &Z) {
54-
| ~~~~~~~~~~~~
54+
| +++
5555

5656
error[E0596]: cannot borrow `other.x` as mutable, as it is behind a `&` reference
5757
--> $DIR/issue-39544.rs:26:17

tests/ui/mut/mutable-class-fields-2.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ LL | self.how_hungry -= 5;
77
help: consider changing this to be a mutable reference
88
|
99
LL | pub fn eat(&mut self) {
10-
| ~~~~~~~~~
10+
| +++
1111

1212
error: aborting due to 1 previous error
1313

tests/ui/suggestions/suggest-ref-mut.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ struct X(usize);
33
impl X {
44
fn zap(&self) {
55
//~^ HELP
6-
//~| SUGGESTION &mut self
6+
//~| SUGGESTION mut
77
self.0 = 32;
88
//~^ ERROR
99
}

tests/ui/suggestions/suggest-ref-mut.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ LL | self.0 = 32;
77
help: consider changing this to be a mutable reference
88
|
99
LL | fn zap(&mut self) {
10-
| ~~~~~~~~~
10+
| +++
1111

1212
error[E0594]: cannot assign to `*foo`, which is behind a `&` reference
1313
--> $DIR/suggest-ref-mut.rs:15:5

0 commit comments

Comments
 (0)