Skip to content

Commit

Permalink
Rollup merge of rust-lang#136212 - estebank:span-tweak, r=petrochenkov
Browse files Browse the repository at this point in the history
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`.
  • Loading branch information
GuillaumeGomez authored Jan 29, 2025
2 parents e26fe50 + 130b0d2 commit 0da81af
Show file tree
Hide file tree
Showing 9 changed files with 21 additions and 26 deletions.
25 changes: 10 additions & 15 deletions compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1140,10 +1140,9 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {

let amp_mut_sugg = match *local_decl.local_info() {
LocalInfo::User(mir::BindingForm::ImplicitSelf(_)) => {
let suggestion = suggest_ampmut_self(self.infcx.tcx, decl_span);
let additional =
local_trait.map(|span| (span, suggest_ampmut_self(self.infcx.tcx, span)));
Some(AmpMutSugg { has_sugg: true, span: decl_span, suggestion, additional })
let (span, suggestion) = suggest_ampmut_self(self.infcx.tcx, decl_span);
let additional = local_trait.map(|span| suggest_ampmut_self(self.infcx.tcx, span));
Some(AmpMutSugg { has_sugg: true, span, suggestion, additional })
}

LocalInfo::User(mir::BindingForm::Var(mir::VarBindingForm {
Expand Down Expand Up @@ -1202,10 +1201,11 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
opt_ty_info: None,
..
})) => {
let sugg = suggest_ampmut_self(self.infcx.tcx, decl_span);
let (span, sugg) =
suggest_ampmut_self(self.infcx.tcx, decl_span);
Some(AmpMutSugg {
has_sugg: true,
span: decl_span,
span,
suggestion: sugg,
additional: None,
})
Expand Down Expand Up @@ -1461,17 +1461,12 @@ fn mut_borrow_of_mutable_ref(local_decl: &LocalDecl<'_>, local_name: Option<Symb
}
}

fn suggest_ampmut_self<'tcx>(tcx: TyCtxt<'tcx>, span: Span) -> String {
fn suggest_ampmut_self(tcx: TyCtxt<'_>, span: Span) -> (Span, String) {
match tcx.sess.source_map().span_to_snippet(span) {
Ok(snippet) => {
let lt_pos = snippet.find('\'');
if let Some(lt_pos) = lt_pos {
format!("&{}mut self", &snippet[lt_pos..snippet.len() - 4])
} else {
"&mut self".to_string()
}
Ok(snippet) if snippet.ends_with("self") => {
(span.with_hi(span.hi() - BytePos(4)).shrink_to_hi(), "mut ".to_string())
}
_ => "&mut self".to_string(),
_ => (span, "&mut self".to_string()),
}
}

Expand Down
2 changes: 1 addition & 1 deletion tests/ui/borrowck/issue-93093.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ struct S {
}
impl S {
async fn bar(&self) { //~ HELP consider changing this to be a mutable reference
//~| SUGGESTION &mut self
//~| SUGGESTION mut
self.foo += 1; //~ ERROR cannot assign to `self.foo`, which is behind a `&` reference [E0594]
}
}
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/borrowck/issue-93093.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ LL | self.foo += 1;
help: consider changing this to be a mutable reference
|
LL | async fn bar(&mut self) {
| ~~~~~~~~~
| +++

error: aborting due to 1 previous error

Expand Down
4 changes: 2 additions & 2 deletions tests/ui/borrowck/trait-impl-argument-difference-ice.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ LL | let a16 = self.read_word() as u16;
help: consider changing this to be a mutable reference in the `impl` method and the `trait` definition
|
LL | extern "C" fn read_dword(&'_ mut self) -> u16 {
| ~~~~~~~~~~~~
| +++

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

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

Expand Down
2 changes: 1 addition & 1 deletion tests/ui/did_you_mean/issue-38147-1.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ LL | self.s.push('x');
help: consider changing this to be a mutable reference
|
LL | fn f(&mut self) {
| ~~~~~~~~~
| +++

error: aborting due to 1 previous error

Expand Down
6 changes: 3 additions & 3 deletions tests/ui/did_you_mean/issue-39544.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ LL | let _ = &mut self.x;
help: consider changing this to be a mutable reference
|
LL | fn foo<'z>(&'z mut self) {
| ~~~~~~~~~~~~
| +++

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

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

error[E0596]: cannot borrow `other.x` as mutable, as it is behind a `&` reference
--> $DIR/issue-39544.rs:26:17
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/mut/mutable-class-fields-2.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ LL | self.how_hungry -= 5;
help: consider changing this to be a mutable reference
|
LL | pub fn eat(&mut self) {
| ~~~~~~~~~
| +++

error: aborting due to 1 previous error

Expand Down
2 changes: 1 addition & 1 deletion tests/ui/suggestions/suggest-ref-mut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ struct X(usize);
impl X {
fn zap(&self) {
//~^ HELP
//~| SUGGESTION &mut self
//~| SUGGESTION mut
self.0 = 32;
//~^ ERROR
}
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/suggestions/suggest-ref-mut.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ LL | self.0 = 32;
help: consider changing this to be a mutable reference
|
LL | fn zap(&mut self) {
| ~~~~~~~~~
| +++

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

0 comments on commit 0da81af

Please sign in to comment.