Skip to content

Commit

Permalink
Rollup merge of rust-lang#49931 - csmoe:end_span, r=estebank
Browse files Browse the repository at this point in the history
Fix incorrect span in `&mut` suggestion

Fixes rust-lang#49859
  • Loading branch information
kennytm committed Apr 16, 2018
2 parents e4991b2 + 6f5a16b commit 73ea893
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
22 changes: 18 additions & 4 deletions src/librustc_mir/borrow_check/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1639,10 +1639,18 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
} else {
self.get_default_err_msg(place)
};
let sp = self.mir.source_info(locations[0]).span;
let mut to_suggest_span = String::new();
if let Ok(src) =
self.tcx.sess.codemap().span_to_snippet(sp) {
to_suggest_span = src[1..].to_string();
};
err_info = Some((
self.mir.source_info(locations[0]).span,
sp,
"consider changing this to be a \
mutable reference: `&mut`", item_msg,
mutable reference",
to_suggest_span,
item_msg,
self.get_primary_err_msg(base)));
}
},
Expand All @@ -1652,9 +1660,15 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
_ => {},
}

if let Some((err_help_span, err_help_stmt, item_msg, sec_span)) = err_info {
if let Some((err_help_span,
err_help_stmt,
to_suggest_span,
item_msg,
sec_span)) = err_info {
let mut err = self.tcx.cannot_assign(span, &item_msg, Origin::Mir);
err.span_suggestion(err_help_span, err_help_stmt, format!(""));
err.span_suggestion(err_help_span,
err_help_stmt,
format!("&mut {}", to_suggest_span));
if place != place_err {
err.span_label(span, sec_span);
}
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/nll/issue-47388.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ error[E0594]: cannot assign to data in a `&` reference
--> $DIR/issue-47388.rs:18:5
|
LL | let fancy_ref = &(&mut fancy);
| ------------- help: consider changing this to be a mutable reference: `&mut`
| ------------- help: consider changing this to be a mutable reference: `&mut (&mut fancy)`
LL | fancy_ref.num = 6; //~ ERROR E0594
| ^^^^^^^^^^^^^^^^^ `fancy_ref` is a `&` reference, so the data it refers to cannot be written

Expand Down

0 comments on commit 73ea893

Please sign in to comment.