Skip to content

Commit c883edf

Browse files
committed
Include lifetime in mutability suggestion in NLL messages
1 parent 70cac59 commit c883edf

6 files changed

+53
-21
lines changed

Diff for: src/librustc_mir/borrow_check/mutability_errors.rs

+43-11
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ impl<'a, 'gcx, 'tcx> MirBorrowckCtxt<'a, 'gcx, 'tcx> {
307307
let local_decl = &self.mir.local_decls[*local];
308308
let suggestion = match local_decl.is_user_variable.as_ref().unwrap() {
309309
ClearCrossCrate::Set(mir::BindingForm::ImplicitSelf) => {
310-
Some(suggest_ampmut_self(local_decl))
310+
Some(suggest_ampmut_self(self.tcx, local_decl))
311311
}
312312

313313
ClearCrossCrate::Set(mir::BindingForm::Var(mir::VarBindingForm {
@@ -418,8 +418,22 @@ impl<'a, 'gcx, 'tcx> MirBorrowckCtxt<'a, 'gcx, 'tcx> {
418418
}
419419
}
420420

421-
fn suggest_ampmut_self<'cx, 'gcx, 'tcx>(local_decl: &mir::LocalDecl<'tcx>) -> (Span, String) {
422-
(local_decl.source_info.span, "&mut self".to_string())
421+
fn suggest_ampmut_self<'cx, 'gcx, 'tcx>(
422+
tcx: TyCtxt<'cx, 'gcx, 'tcx>,
423+
local_decl: &mir::LocalDecl<'tcx>,
424+
) -> (Span, String) {
425+
let sp = local_decl.source_info.span;
426+
(sp, match tcx.sess.codemap().span_to_snippet(sp) {
427+
Ok(snippet) => {
428+
let lt_pos = snippet.find('\'');
429+
if let Some(lt_pos) = lt_pos {
430+
format!("&{}mut self", &snippet[lt_pos..snippet.len() - 4])
431+
} else {
432+
"&mut self".to_string()
433+
}
434+
}
435+
_ => "&mut self".to_string()
436+
})
423437
}
424438

425439
// When we want to suggest a user change a local variable to be a `&mut`, there
@@ -447,9 +461,15 @@ fn suggest_ampmut<'cx, 'gcx, 'tcx>(
447461
let locations = mir.find_assignments(local);
448462
if locations.len() > 0 {
449463
let assignment_rhs_span = mir.source_info(locations[0]).span;
450-
let snippet = tcx.sess.codemap().span_to_snippet(assignment_rhs_span);
451-
if let Ok(src) = snippet {
452-
if src.starts_with('&') {
464+
if let Ok(src) = tcx.sess.codemap().span_to_snippet(assignment_rhs_span) {
465+
if let (true, Some(ws_pos)) = (
466+
src.starts_with("&'"),
467+
src.find(|c: char| -> bool { c.is_whitespace() }),
468+
) {
469+
let lt_name = &src[1..ws_pos];
470+
let ty = &src[ws_pos..];
471+
return (assignment_rhs_span, format!("&{} mut {}", lt_name, ty));
472+
} else if src.starts_with('&') {
453473
let borrowed_expr = src[1..].to_string();
454474
return (assignment_rhs_span, format!("&mut {}", borrowed_expr));
455475
}
@@ -466,13 +486,25 @@ fn suggest_ampmut<'cx, 'gcx, 'tcx>(
466486
None => local_decl.source_info.span,
467487
};
468488

489+
if let Ok(src) = tcx.sess.codemap().span_to_snippet(highlight_span) {
490+
if let (true, Some(ws_pos)) = (
491+
src.starts_with("&'"),
492+
src.find(|c: char| -> bool { c.is_whitespace() }),
493+
) {
494+
let lt_name = &src[1..ws_pos];
495+
let ty = &src[ws_pos..];
496+
return (highlight_span, format!("&{} mut{}", lt_name, ty));
497+
}
498+
}
499+
469500
let ty_mut = local_decl.ty.builtin_deref(true).unwrap();
470501
assert_eq!(ty_mut.mutbl, hir::MutImmutable);
471-
if local_decl.ty.is_region_ptr() {
472-
(highlight_span, format!("&mut {}", ty_mut.ty))
473-
} else {
474-
(highlight_span, format!("*mut {}", ty_mut.ty))
475-
}
502+
(highlight_span,
503+
if local_decl.ty.is_region_ptr() {
504+
format!("&mut {}", ty_mut.ty)
505+
} else {
506+
format!("*mut {}", ty_mut.ty)
507+
})
476508
}
477509

478510
fn is_closure_or_generator(ty: ty::Ty) -> bool {

Diff for: src/test/ui/did_you_mean/issue-39544.nll.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ error[E0596]: cannot borrow `self.x` as mutable, as it is behind a `&` reference
1010
--> $DIR/issue-39544.rs:26:17
1111
|
1212
LL | fn foo<'z>(&'z self) {
13-
| -------- help: consider changing this to be a mutable reference: `&mut self`
13+
| -------- help: consider changing this to be a mutable reference: `&'z mut self`
1414
LL | let _ = &mut self.x; //~ ERROR cannot borrow
1515
| ^^^^^^^^^^^ `self` is a `&` reference, so the data it refers to cannot be borrowed as mutable
1616

@@ -35,7 +35,7 @@ error[E0596]: cannot borrow `self.x` as mutable, as it is behind a `&` reference
3535
--> $DIR/issue-39544.rs:35:17
3636
|
3737
LL | fn foo2<'a>(&'a self, other: &Z) {
38-
| -------- help: consider changing this to be a mutable reference: `&mut self`
38+
| -------- help: consider changing this to be a mutable reference: `&'a mut self`
3939
LL | let _ = &mut self.x; //~ ERROR cannot borrow
4040
| ^^^^^^^^^^^ `self` is a `&` reference, so the data it refers to cannot be borrowed as mutable
4141

@@ -52,7 +52,7 @@ error[E0596]: cannot borrow `self.x` as mutable, as it is behind a `&` reference
5252
--> $DIR/issue-39544.rs:40:17
5353
|
5454
LL | fn foo3<'a>(self: &'a Self, other: &Z) {
55-
| -------- help: consider changing this to be a mutable reference: `&mut Z`
55+
| -------- help: consider changing this to be a mutable reference: `&'a mut Self`
5656
LL | let _ = &mut self.x; //~ ERROR cannot borrow
5757
| ^^^^^^^^^^^ `self` is a `&` reference, so the data it refers to cannot be borrowed as mutable
5858

Diff for: src/test/ui/span/borrowck-borrow-overloaded-auto-deref-mut.nll.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ error[E0596]: cannot borrow `*x` as mutable, as it is behind a `&` reference
2626
--> $DIR/borrowck-borrow-overloaded-auto-deref-mut.rs:102:5
2727
|
2828
LL | fn assign_field2<'a>(x: &'a Own<Point>) {
29-
| -------------- help: consider changing this to be a mutable reference: `&mut Own<Point>`
29+
| -------------- help: consider changing this to be a mutable reference: `&'a mut Own<Point>`
3030
LL | x.y = 3; //~ ERROR cannot borrow
3131
| ^ `x` is a `&` reference, so the data it refers to cannot be borrowed as mutable
3232

@@ -58,7 +58,7 @@ error[E0596]: cannot borrow `*x` as mutable, as it is behind a `&` reference
5858
--> $DIR/borrowck-borrow-overloaded-auto-deref-mut.rs:143:6
5959
|
6060
LL | fn assign_method2<'a>(x: &'a Own<Point>) {
61-
| -------------- help: consider changing this to be a mutable reference: `&mut Own<Point>`
61+
| -------------- help: consider changing this to be a mutable reference: `&'a mut Own<Point>`
6262
LL | *x.y_mut() = 3; //~ ERROR cannot borrow
6363
| ^ `x` is a `&` reference, so the data it refers to cannot be borrowed as mutable
6464

Diff for: src/test/ui/span/borrowck-borrow-overloaded-deref-mut.nll.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ error[E0596]: cannot borrow `*x` as mutable, as it is behind a `&` reference
1010
--> $DIR/borrowck-borrow-overloaded-deref-mut.rs:51:11
1111
|
1212
LL | fn deref_extend_mut1<'a>(x: &'a Own<isize>) -> &'a mut isize {
13-
| -------------- help: consider changing this to be a mutable reference: `&mut Own<isize>`
13+
| -------------- help: consider changing this to be a mutable reference: `&'a mut Own<isize>`
1414
LL | &mut **x //~ ERROR cannot borrow
1515
| ^^ `x` is a `&` reference, so the data it refers to cannot be borrowed as mutable
1616

@@ -26,7 +26,7 @@ error[E0596]: cannot borrow `*x` as mutable, as it is behind a `&` reference
2626
--> $DIR/borrowck-borrow-overloaded-deref-mut.rs:63:6
2727
|
2828
LL | fn assign2<'a>(x: &'a Own<isize>) {
29-
| -------------- help: consider changing this to be a mutable reference: `&mut Own<isize>`
29+
| -------------- help: consider changing this to be a mutable reference: `&'a mut Own<isize>`
3030
LL | **x = 3; //~ ERROR cannot borrow
3131
| ^^ `x` is a `&` reference, so the data it refers to cannot be borrowed as mutable
3232

Diff for: src/test/ui/span/mut-arg-hint.nll.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ error[E0596]: cannot borrow `*a` as mutable, as it is behind a `&` reference
1010
--> $DIR/mut-arg-hint.rs:18:5
1111
|
1212
LL | pub fn foo<'a>(mut a: &'a String) {
13-
| ---------- help: consider changing this to be a mutable reference: `&mut std::string::String`
13+
| ---------- help: consider changing this to be a mutable reference: `&'a mut String`
1414
LL | a.push_str("foo"); //~ ERROR cannot borrow immutable borrowed content
1515
| ^ `a` is a `&` reference, so the data it refers to cannot be borrowed as mutable
1616

Diff for: src/test/ui/trivial-bounds-inconsistent-copy-reborrow.nll.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@ error[E0596]: cannot borrow `**t` as mutable, as it is behind a `&` reference
22
--> $DIR/trivial-bounds-inconsistent-copy-reborrow.rs:16:5
33
|
44
LL | fn reborrow_mut<'a>(t: &'a &'a mut i32) -> &'a mut i32 where &'a mut i32: Copy {
5-
| --------------- help: consider changing this to be a mutable reference: `&mut &mut i32`
5+
| --------------- help: consider changing this to be a mutable reference: `&'a mut &'a mut i32`
66
LL | *t //~ ERROR
77
| ^^ `t` is a `&` reference, so the data it refers to cannot be borrowed as mutable
88

99
error[E0596]: cannot borrow `**t` as mutable, as it is behind a `&` reference
1010
--> $DIR/trivial-bounds-inconsistent-copy-reborrow.rs:20:6
1111
|
1212
LL | fn copy_reborrow_mut<'a>(t: &'a &'a mut i32) -> &'a mut i32 where &'a mut i32: Copy {
13-
| --------------- help: consider changing this to be a mutable reference: `&mut &mut i32`
13+
| --------------- help: consider changing this to be a mutable reference: `&'a mut &'a mut i32`
1414
LL | {*t} //~ ERROR
1515
| ^^ `t` is a `&` reference, so the data it refers to cannot be borrowed as mutable
1616

0 commit comments

Comments
 (0)