Skip to content

Commit 71b0755

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 71b0755

File tree

92 files changed

+355
-150
lines changed

Some content is hidden

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

92 files changed

+355
-150
lines changed

compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs

Lines changed: 31 additions & 6 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 => {
@@ -518,8 +518,8 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
518518
err.span_label(
519519
span,
520520
format!(
521-
"`{name}` is a `{pointer_sigil}` {pointer_desc}, \
522-
so the data it refers to cannot be {acted_on}",
521+
"`{name}` is a `{pointer_sigil}` {pointer_desc}, so it cannot be \
522+
{acted_on}",
523523
),
524524
);
525525

@@ -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/array-slice-vec/slice-mut-2.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0596]: cannot borrow `*x` as mutable, as it is behind a `&` reference
22
--> $DIR/slice-mut-2.rs:7:18
33
|
44
LL | let _ = &mut x[2..4];
5-
| ^ `x` is a `&` reference, so the data it refers to cannot be borrowed as mutable
5+
| ^ `x` is a `&` reference, so it cannot be borrowed as mutable
66
|
77
help: consider changing this binding's type
88
|

tests/ui/borrowck/accidentally-cloning-ref-borrow-error.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0596]: cannot borrow `sm.x` as mutable, as it is behind a `&` reference
22
--> $DIR/accidentally-cloning-ref-borrow-error.rs:32:9
33
|
44
LL | foo(&mut sm.x);
5-
| ^^^^^^^^^ `sm` is a `&` reference, so the data it refers to cannot be borrowed as mutable
5+
| ^^^^^^^^^ `sm` is a `&` reference, so it cannot be borrowed as mutable
66
|
77
help: `Str` doesn't implement `Clone`, so this call clones the reference `&Str`
88
--> $DIR/accidentally-cloning-ref-borrow-error.rs:31:21

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 it 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/borrow-raw-address-of-deref-mutability.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0596]: cannot borrow `*x` as mutable, as it is behind a `&` reference
22
--> $DIR/borrow-raw-address-of-deref-mutability.rs:6:13
33
|
44
LL | let q = &raw mut *x;
5-
| ^^^^^^^^^^^ `x` is a `&` reference, so the data it refers to cannot be borrowed as mutable
5+
| ^^^^^^^^^^^ `x` is a `&` reference, so it cannot be borrowed as mutable
66
|
77
help: consider changing this to be a mutable reference
88
|
@@ -13,7 +13,7 @@ error[E0596]: cannot borrow `*x` as mutable, as it is behind a `*const` pointer
1313
--> $DIR/borrow-raw-address-of-deref-mutability.rs:12:13
1414
|
1515
LL | let q = &raw mut *x;
16-
| ^^^^^^^^^^^ `x` is a `*const` pointer, so the data it refers to cannot be borrowed as mutable
16+
| ^^^^^^^^^^^ `x` is a `*const` pointer, so it cannot be borrowed as mutable
1717
|
1818
help: consider specifying this binding's type
1919
|

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

Lines changed: 6 additions & 3 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

@@ -30,7 +33,7 @@ error[E0596]: cannot borrow `*ref_x` as mutable, as it is behind a `&` reference
3033
--> $DIR/borrowck-access-permissions.rs:36:19
3134
|
3235
LL | let _y1 = &mut *ref_x;
33-
| ^^^^^^^^^^^ `ref_x` is a `&` reference, so the data it refers to cannot be borrowed as mutable
36+
| ^^^^^^^^^^^ `ref_x` is a `&` reference, so it cannot be borrowed as mutable
3437
|
3538
help: consider changing this to be a mutable reference
3639
|
@@ -41,7 +44,7 @@ error[E0596]: cannot borrow `*ptr_x` as mutable, as it is behind a `*const` poin
4144
--> $DIR/borrowck-access-permissions.rs:46:23
4245
|
4346
LL | let _y1 = &mut *ptr_x;
44-
| ^^^^^^^^^^^ `ptr_x` is a `*const` pointer, so the data it refers to cannot be borrowed as mutable
47+
| ^^^^^^^^^^^ `ptr_x` is a `*const` pointer, so it cannot be borrowed as mutable
4548
|
4649
help: consider changing this binding's type
4750
|
@@ -53,7 +56,7 @@ error[E0596]: cannot borrow `*foo_ref.f` as mutable, as it is behind a `&` refer
5356
--> $DIR/borrowck-access-permissions.rs:59:18
5457
|
5558
LL | let _y = &mut *foo_ref.f;
56-
| ^^^^^^^^^^^^^^^ `foo_ref` is a `&` reference, so the data it refers to cannot be borrowed as mutable
59+
| ^^^^^^^^^^^^^^^ `foo_ref` is a `&` reference, so it cannot be borrowed as mutable
5760
|
5861
help: consider changing this to be a mutable reference
5962
|

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 it 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 it 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: 2 additions & 2 deletions
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 it cannot be written to
66
|
77
help: consider specifying this binding's type
88
|
@@ -23,7 +23,7 @@ error[E0596]: cannot borrow `**t0` as mutable, as it is behind a `&` reference
2323
--> $DIR/borrowck-borrow-mut-base-ptr-in-aliasable-loc.rs:19:26
2424
|
2525
LL | let x: &mut isize = &mut **t0;
26-
| ^^^^^^^^^ `t0` is a `&` reference, so the data it refers to cannot be borrowed as mutable
26+
| ^^^^^^^^^ `t0` is a `&` reference, so it cannot be borrowed as mutable
2727
|
2828
help: consider changing this to be a mutable reference
2929
|

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 it cannot be written to
66
|
77
help: consider changing this to be a mutable reference
88
|

0 commit comments

Comments
 (0)