Skip to content

Commit dc630aa

Browse files
committed
Provide more context when mutably borrowing an imutable borrow
Point at statics and consts being mutable borrowed or written to: ``` error[E0594]: cannot assign to immutable static item `NUM` --> $DIR/E0594.rs:4:5 | LL | static NUM: i32 = 18; | --------------- this `static` cannot be written to ... LL | NUM = 20; | ^^^^^^^^ cannot assign ``` Point at the expression that couldn't be mutably borrowed from a pattern: ``` error[E0596]: cannot borrow data in a `&` reference as mutable --> $DIR/mut-pattern-of-immutable-borrow.rs:19:14 | LL | match &arg.field { | ---------- this cannot be borrowed as mutable LL | Some(ref mut s) => s.push('a'), | ^^^^^^^^^ cannot borrow as mutable ```
1 parent e5efc33 commit dc630aa

File tree

60 files changed

+284
-79
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

60 files changed

+284
-79
lines changed

compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
213213
AccessKind::Mutate => {
214214
err = self.cannot_assign(span, &(item_msg + &reason));
215215
act = "assign";
216-
acted_on = "written";
216+
acted_on = "written to";
217217
span
218218
}
219219
AccessKind::MutableBorrow => {
@@ -542,7 +542,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
542542
self.expected_fn_found_fn_mut_call(&mut err, span, act);
543543
}
544544

545-
PlaceRef { local: _, projection: [.., ProjectionElem::Deref] } => {
545+
PlaceRef { local, projection: [.., ProjectionElem::Deref] } => {
546546
err.span_label(span, format!("cannot {act}"));
547547

548548
match opt_source {
@@ -559,11 +559,36 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
559559
));
560560
self.suggest_map_index_mut_alternatives(ty, &mut err, span);
561561
}
562-
_ => (),
562+
_ => {
563+
let local = &self.body.local_decls[local];
564+
match local.local_info() {
565+
LocalInfo::StaticRef { def_id, .. } => {
566+
let span = self.infcx.tcx.def_span(def_id);
567+
err.span_label(span, format!("this `static` cannot be {acted_on}"));
568+
}
569+
LocalInfo::ConstRef { def_id } => {
570+
let span = self.infcx.tcx.def_span(def_id);
571+
err.span_label(span, format!("this `const` cannot be {acted_on}"));
572+
}
573+
LocalInfo::BlockTailTemp(_) | LocalInfo::Boring
574+
if !local.source_info.span.overlaps(span) =>
575+
{
576+
err.span_label(
577+
local.source_info.span,
578+
format!("this cannot be {acted_on}"),
579+
);
580+
}
581+
_ => {}
582+
}
583+
}
563584
}
564585
}
565586

566-
_ => {
587+
PlaceRef { local, .. } => {
588+
let local = &self.body.local_decls[local];
589+
if !local.source_info.span.overlaps(span) {
590+
err.span_label(local.source_info.span, format!("this cannot be {acted_on}"));
591+
}
567592
err.span_label(span, format!("cannot {act}"));
568593
}
569594
}

tests/ui/borrowck/argument_number_mismatch_ice.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ error[E0594]: cannot assign to `*input`, which is behind a `&` reference
1111
--> $DIR/argument_number_mismatch_ice.rs:10:9
1212
|
1313
LL | *input = self.0;
14-
| ^^^^^^^^^^^^^^^ `input` is a `&` reference, so the data it refers to cannot be written
14+
| ^^^^^^^^^^^^^^^ `input` is a `&` reference, so the data it refers to cannot be written to
1515
|
1616
help: consider changing this to be a mutable reference in the `impl` method and the `trait` definition
1717
|

tests/ui/borrowck/borrowck-access-permissions.stderr

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ LL | let mut x = 1;
1212
error[E0596]: cannot borrow immutable static item `static_x` as mutable
1313
--> $DIR/borrowck-access-permissions.rs:16:19
1414
|
15+
LL | static static_x: i32 = 1;
16+
| -------------------- this `static` cannot be borrowed as mutable
17+
...
1518
LL | let _y1 = &mut static_x;
1619
| ^^^^^^^^^^^^^ cannot borrow as mutable
1720

tests/ui/borrowck/borrowck-assign-to-andmut-in-aliasable-loc.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0594]: cannot assign to `*s.pointer`, which is behind a `&` reference
22
--> $DIR/borrowck-assign-to-andmut-in-aliasable-loc.rs:9:5
33
|
44
LL | *s.pointer += 1;
5-
| ^^^^^^^^^^^^^^^ `s` is a `&` reference, so the data it refers to cannot be written
5+
| ^^^^^^^^^^^^^^^ `s` is a `&` reference, so the data it refers to cannot be written to
66
|
77
help: consider changing this to be a mutable reference
88
|
@@ -13,7 +13,7 @@ error[E0594]: cannot assign to `*s.pointer`, which is behind a `&` reference
1313
--> $DIR/borrowck-assign-to-andmut-in-aliasable-loc.rs:17:5
1414
|
1515
LL | *s.pointer += 1;
16-
| ^^^^^^^^^^^^^^^ `s` is a `&` reference, so the data it refers to cannot be written
16+
| ^^^^^^^^^^^^^^^ `s` is a `&` reference, so the data it refers to cannot be written to
1717
|
1818
help: consider changing this to be a mutable reference
1919
|

tests/ui/borrowck/borrowck-assign-to-constants.stderr

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
error[E0594]: cannot assign to immutable static item `foo`
22
--> $DIR/borrowck-assign-to-constants.rs:5:5
33
|
4+
LL | static foo: isize = 5;
5+
| ----------------- this `static` cannot be written to
6+
...
47
LL | foo = 6;
58
| ^^^^^^^ cannot assign
69

tests/ui/borrowck/borrowck-borrow-mut-base-ptr-in-aliasable-loc.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0594]: cannot assign to `**t1`, which is behind a `&` reference
22
--> $DIR/borrowck-borrow-mut-base-ptr-in-aliasable-loc.rs:9:5
33
|
44
LL | **t1 = 22;
5-
| ^^^^^^^^^ `t1` is a `&` reference, so the data it refers to cannot be written
5+
| ^^^^^^^^^ `t1` is a `&` reference, so the data it refers to cannot be written to
66
|
77
help: consider specifying this binding's type
88
|

tests/ui/borrowck/borrowck-issue-14498.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0594]: cannot assign to `***p`, which is behind a `&` reference
22
--> $DIR/borrowck-issue-14498.rs:16:5
33
|
44
LL | ***p = 2;
5-
| ^^^^^^^^ `p` is a `&` reference, so the data it refers to cannot be written
5+
| ^^^^^^^^ `p` is a `&` reference, so the data it refers to cannot be written to
66
|
77
help: consider changing this to be a mutable reference
88
|

tests/ui/borrowck/issue-42344.stderr

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
error[E0596]: cannot borrow `*TAB[_]` as mutable, as `TAB` is an immutable static item
22
--> $DIR/issue-42344.rs:4:5
33
|
4+
LL | static TAB: [&mut [u8]; 0] = [];
5+
| -------------------------- this `static` cannot be borrowed as mutable
6+
...
47
LL | TAB[0].iter_mut();
58
| ^^^^^^ cannot borrow as mutable
69

tests/ui/borrowck/issue-69789-iterator-mut-suggestion.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ LL | for item in &mut std::iter::empty::<&'static ()>() {
55
| -------------------------------------- this iterator yields `&` references
66
LL |
77
LL | *item = ();
8-
| ^^^^^^^^^^ `item` is a `&` reference, so the data it refers to cannot be written
8+
| ^^^^^^^^^^ `item` is a `&` reference, so the data it refers to cannot be written to
99

1010
error: aborting due to 1 previous error
1111

tests/ui/borrowck/issue-83309-ice-immut-in-for-loop.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ LL | for v in Query.iter_mut() {
55
| ---------------- this iterator yields `&` references
66
LL |
77
LL | *v -= 1;
8-
| ^^^^^^^ `v` is a `&` reference, so the data it refers to cannot be written
8+
| ^^^^^^^ `v` is a `&` reference, so the data it refers to cannot be written to
99

1010
error: aborting due to 1 previous error
1111

0 commit comments

Comments
 (0)