Skip to content

Commit

Permalink
Add label to primary span for mutable access of immutable struct error
Browse files Browse the repository at this point in the history
  • Loading branch information
estebank committed Mar 14, 2017
1 parent 38b5b29 commit 9ac628d
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
11 changes: 8 additions & 3 deletions src/librustc_borrowck/borrowck/mod.rs
Expand Up @@ -747,9 +747,9 @@ impl<'a, 'tcx> BorrowckCtxt<'a, 'tcx> {
if let Some((span, msg)) = immutable_field {
db.span_label(span, &msg);
}
if let Some(span) = local_def {
if let Ok(snippet) = self.tcx.sess.codemap().span_to_snippet(span) {
db.span_label(span, &format!("consider changing this to `mut {}`", snippet));
if let Some(let_span) = local_def {
if let Ok(snippet) = self.tcx.sess.codemap().span_to_snippet(let_span) {
db.span_label(let_span, &format!("consider changing this to `mut {}`", snippet));
}
}
db
Expand Down Expand Up @@ -1120,6 +1120,11 @@ before rustc 1.16, this temporary lived longer - see issue #39283 \
} else {
db.span_label(*error_span, &format!("cannot borrow mutably"));
}
} else if let Categorization::Interior(ref cmt, _) = err.cmt.cat {
if let mc::MutabilityCategory::McImmutable = cmt.mutbl {
db.span_label(*error_span,
&"cannot mutably borrow immutable field");
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/did_you_mean/issue-39544.stderr
Expand Up @@ -4,7 +4,7 @@ error: cannot borrow immutable field `z.x` as mutable
20 | let z = Z { x: X::Y };
| - consider changing this to `mut z`
21 | let _ = &mut z.x;
| ^^^
| ^^^ cannot mutably borrow immutable field

error: aborting due to previous error

0 comments on commit 9ac628d

Please sign in to comment.