Skip to content

Commit 9ac628d

Browse files
committed
Add label to primary span for mutable access of immutable struct error
1 parent 38b5b29 commit 9ac628d

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

src/librustc_borrowck/borrowck/mod.rs

+8-3
Original file line numberDiff line numberDiff line change
@@ -747,9 +747,9 @@ impl<'a, 'tcx> BorrowckCtxt<'a, 'tcx> {
747747
if let Some((span, msg)) = immutable_field {
748748
db.span_label(span, &msg);
749749
}
750-
if let Some(span) = local_def {
751-
if let Ok(snippet) = self.tcx.sess.codemap().span_to_snippet(span) {
752-
db.span_label(span, &format!("consider changing this to `mut {}`", snippet));
750+
if let Some(let_span) = local_def {
751+
if let Ok(snippet) = self.tcx.sess.codemap().span_to_snippet(let_span) {
752+
db.span_label(let_span, &format!("consider changing this to `mut {}`", snippet));
753753
}
754754
}
755755
db
@@ -1120,6 +1120,11 @@ before rustc 1.16, this temporary lived longer - see issue #39283 \
11201120
} else {
11211121
db.span_label(*error_span, &format!("cannot borrow mutably"));
11221122
}
1123+
} else if let Categorization::Interior(ref cmt, _) = err.cmt.cat {
1124+
if let mc::MutabilityCategory::McImmutable = cmt.mutbl {
1125+
db.span_label(*error_span,
1126+
&"cannot mutably borrow immutable field");
1127+
}
11231128
}
11241129
}
11251130
}

src/test/ui/did_you_mean/issue-39544.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error: cannot borrow immutable field `z.x` as mutable
44
20 | let z = Z { x: X::Y };
55
| - consider changing this to `mut z`
66
21 | let _ = &mut z.x;
7-
| ^^^
7+
| ^^^ cannot mutably borrow immutable field
88

99
error: aborting due to previous error
1010

0 commit comments

Comments
 (0)