Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 29 additions & 4 deletions compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
AccessKind::Mutate => {
err = self.cannot_assign(span, &(item_msg + &reason));
act = "assign";
acted_on = "written";
acted_on = "written to";
span
}
AccessKind::MutableBorrow => {
Expand Down Expand Up @@ -542,7 +542,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
self.expected_fn_found_fn_mut_call(&mut err, span, act);
}

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

match opt_source {
Expand All @@ -559,11 +559,36 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
));
self.suggest_map_index_mut_alternatives(ty, &mut err, span);
}
_ => (),
_ => {
let local = &self.body.local_decls[local];
match local.local_info() {
LocalInfo::StaticRef { def_id, .. } => {
let span = self.infcx.tcx.def_span(def_id);
err.span_label(span, format!("this `static` cannot be {acted_on}"));
}
LocalInfo::ConstRef { def_id } => {
let span = self.infcx.tcx.def_span(def_id);
err.span_label(span, format!("this `const` cannot be {acted_on}"));
}
LocalInfo::BlockTailTemp(_) | LocalInfo::Boring
if !local.source_info.span.overlaps(span) =>
{
err.span_label(
local.source_info.span,
format!("this cannot be {acted_on}"),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does not seem to have a testcase.

);
}
_ => {}
}
}
}
}

_ => {
PlaceRef { local, .. } => {
let local = &self.body.local_decls[local];
if !local.source_info.span.overlaps(span) {
err.span_label(local.source_info.span, format!("this cannot be {acted_on}"));
}
err.span_label(span, format!("cannot {act}"));
}
}
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/borrowck/argument_number_mismatch_ice.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ error[E0594]: cannot assign to `*input`, which is behind a `&` reference
--> $DIR/argument_number_mismatch_ice.rs:10:9
|
LL | *input = self.0;
| ^^^^^^^^^^^^^^^ `input` is a `&` reference, so the data it refers to cannot be written
| ^^^^^^^^^^^^^^^ `input` is a `&` reference, so the data it refers to cannot be written to
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is grammatical. I'd expect either "the reference cannot be written to", or "the data cannot be written"; "to" does not work without indirection.

Perhaps rephrase with "input is a & reference, so *input cannot be written to"?

|
help: consider changing this to be a mutable reference in the `impl` method and the `trait` definition
|
Expand Down
3 changes: 3 additions & 0 deletions tests/ui/borrowck/borrowck-access-permissions.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ LL | let mut x = 1;
error[E0596]: cannot borrow immutable static item `static_x` as mutable
--> $DIR/borrowck-access-permissions.rs:16:19
|
LL | static static_x: i32 = 1;
| -------------------- this `static` cannot be borrowed as mutable
...
LL | let _y1 = &mut static_x;
| ^^^^^^^^^^^^^ cannot borrow as mutable

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ error[E0594]: cannot assign to `*s.pointer`, which is behind a `&` reference
--> $DIR/borrowck-assign-to-andmut-in-aliasable-loc.rs:9:5
|
LL | *s.pointer += 1;
| ^^^^^^^^^^^^^^^ `s` is a `&` reference, so the data it refers to cannot be written
| ^^^^^^^^^^^^^^^ `s` is a `&` reference, so the data it refers to cannot be written to
|
help: consider changing this to be a mutable reference
|
Expand All @@ -13,7 +13,7 @@ error[E0594]: cannot assign to `*s.pointer`, which is behind a `&` reference
--> $DIR/borrowck-assign-to-andmut-in-aliasable-loc.rs:17:5
|
LL | *s.pointer += 1;
| ^^^^^^^^^^^^^^^ `s` is a `&` reference, so the data it refers to cannot be written
| ^^^^^^^^^^^^^^^ `s` is a `&` reference, so the data it refers to cannot be written to
|
help: consider changing this to be a mutable reference
|
Expand Down
3 changes: 3 additions & 0 deletions tests/ui/borrowck/borrowck-assign-to-constants.stderr
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
error[E0594]: cannot assign to immutable static item `foo`
--> $DIR/borrowck-assign-to-constants.rs:5:5
|
LL | static foo: isize = 5;
| ----------------- this `static` cannot be written to
...
LL | foo = 6;
| ^^^^^^^ cannot assign

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ error[E0594]: cannot assign to `**t1`, which is behind a `&` reference
--> $DIR/borrowck-borrow-mut-base-ptr-in-aliasable-loc.rs:9:5
|
LL | **t1 = 22;
| ^^^^^^^^^ `t1` is a `&` reference, so the data it refers to cannot be written
| ^^^^^^^^^ `t1` is a `&` reference, so the data it refers to cannot be written to
|
help: consider specifying this binding's type
|
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/borrowck/borrowck-issue-14498.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ error[E0594]: cannot assign to `***p`, which is behind a `&` reference
--> $DIR/borrowck-issue-14498.rs:16:5
|
LL | ***p = 2;
| ^^^^^^^^ `p` is a `&` reference, so the data it refers to cannot be written
| ^^^^^^^^ `p` is a `&` reference, so the data it refers to cannot be written to
|
help: consider changing this to be a mutable reference
|
Expand Down
3 changes: 3 additions & 0 deletions tests/ui/borrowck/issue-42344.stderr
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
error[E0596]: cannot borrow `*TAB[_]` as mutable, as `TAB` is an immutable static item
--> $DIR/issue-42344.rs:4:5
|
LL | static TAB: [&mut [u8]; 0] = [];
| -------------------------- this `static` cannot be borrowed as mutable
...
LL | TAB[0].iter_mut();
| ^^^^^^ cannot borrow as mutable

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ LL | for item in &mut std::iter::empty::<&'static ()>() {
| -------------------------------------- this iterator yields `&` references
LL |
LL | *item = ();
| ^^^^^^^^^^ `item` is a `&` reference, so the data it refers to cannot be written
| ^^^^^^^^^^ `item` is a `&` reference, so the data it refers to cannot be written to

error: aborting due to 1 previous error

Expand Down
2 changes: 1 addition & 1 deletion tests/ui/borrowck/issue-83309-ice-immut-in-for-loop.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ LL | for v in Query.iter_mut() {
| ---------------- this iterator yields `&` references
LL |
LL | *v -= 1;
| ^^^^^^^ `v` is a `&` reference, so the data it refers to cannot be written
| ^^^^^^^ `v` is a `&` reference, so the data it refers to cannot be written to

error: aborting due to 1 previous error

Expand Down
6 changes: 3 additions & 3 deletions tests/ui/borrowck/issue-85765-closure.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ error[E0594]: cannot assign to `*r`, which is behind a `&` reference
--> $DIR/issue-85765-closure.rs:13:9
|
LL | *r = 0;
| ^^^^^^ `r` is a `&` reference, so the data it refers to cannot be written
| ^^^^^^ `r` is a `&` reference, so the data it refers to cannot be written to
|
help: consider changing this to be a mutable reference
|
Expand All @@ -24,7 +24,7 @@ error[E0594]: cannot assign to `*x`, which is behind a `&` reference
--> $DIR/issue-85765-closure.rs:20:9
|
LL | *x = 1;
| ^^^^^^ `x` is a `&` reference, so the data it refers to cannot be written
| ^^^^^^ `x` is a `&` reference, so the data it refers to cannot be written to
|
help: consider changing this binding's type
|
Expand All @@ -35,7 +35,7 @@ error[E0594]: cannot assign to `*y`, which is behind a `&` reference
--> $DIR/issue-85765-closure.rs:27:9
|
LL | *y = 1;
| ^^^^^^ `y` is a `&` reference, so the data it refers to cannot be written
| ^^^^^^ `y` is a `&` reference, so the data it refers to cannot be written to
|
help: consider changing this binding's type
|
Expand Down
6 changes: 3 additions & 3 deletions tests/ui/borrowck/issue-85765.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ error[E0594]: cannot assign to `*r`, which is behind a `&` reference
--> $DIR/issue-85765.rs:12:5
|
LL | *r = 0;
| ^^^^^^ `r` is a `&` reference, so the data it refers to cannot be written
| ^^^^^^ `r` is a `&` reference, so the data it refers to cannot be written to
|
help: consider changing this to be a mutable reference
|
Expand All @@ -24,7 +24,7 @@ error[E0594]: cannot assign to `*x`, which is behind a `&` reference
--> $DIR/issue-85765.rs:19:5
|
LL | *x = 1;
| ^^^^^^ `x` is a `&` reference, so the data it refers to cannot be written
| ^^^^^^ `x` is a `&` reference, so the data it refers to cannot be written to
|
help: consider changing this binding's type
|
Expand All @@ -35,7 +35,7 @@ error[E0594]: cannot assign to `*y`, which is behind a `&` reference
--> $DIR/issue-85765.rs:26:5
|
LL | *y = 1;
| ^^^^^^ `y` is a `&` reference, so the data it refers to cannot be written
| ^^^^^^ `y` is a `&` reference, so the data it refers to cannot be written to
|
help: consider changing this binding's type
|
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/borrowck/issue-92015.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ error[E0594]: cannot assign to `*foo`, which is behind a `&` reference
--> $DIR/issue-92015.rs:6:5
|
LL | *foo = 1;
| ^^^^^^^^ `foo` is a `&` reference, so the data it refers to cannot be written
| ^^^^^^^^ `foo` is a `&` reference, so the data it refers to cannot be written to
|
help: consider specifying this binding's type
|
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/borrowck/issue-93093.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ error[E0594]: cannot assign to `self.foo`, which is behind a `&` reference
--> $DIR/issue-93093.rs:8:9
|
LL | self.foo += 1;
| ^^^^^^^^^^^^^ `self` is a `&` reference, so the data it refers to cannot be written
| ^^^^^^^^^^^^^ `self` is a `&` reference, so the data it refers to cannot be written to
|
help: consider changing this to be a mutable reference
|
Expand Down
20 changes: 16 additions & 4 deletions tests/ui/borrowck/mutability-errors.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ error[E0594]: cannot assign to `*x`, which is behind a `&` reference
--> $DIR/mutability-errors.rs:9:5
|
LL | *x = (1,);
| ^^^^^^^^^ `x` is a `&` reference, so the data it refers to cannot be written
| ^^^^^^^^^ `x` is a `&` reference, so the data it refers to cannot be written to
|
help: consider changing this to be a mutable reference
|
Expand All @@ -13,7 +13,7 @@ error[E0594]: cannot assign to `x.0`, which is behind a `&` reference
--> $DIR/mutability-errors.rs:10:5
|
LL | x.0 = 1;
| ^^^^^^^ `x` is a `&` reference, so the data it refers to cannot be written
| ^^^^^^^ `x` is a `&` reference, so the data it refers to cannot be written to
|
help: consider changing this to be a mutable reference
|
Expand Down Expand Up @@ -70,7 +70,7 @@ error[E0594]: cannot assign to `*x`, which is behind a `*const` pointer
--> $DIR/mutability-errors.rs:23:5
|
LL | *x = (1,);
| ^^^^^^^^^ `x` is a `*const` pointer, so the data it refers to cannot be written
| ^^^^^^^^^ `x` is a `*const` pointer, so the data it refers to cannot be written to
|
help: consider changing this to be a mutable pointer
|
Expand All @@ -81,7 +81,7 @@ error[E0594]: cannot assign to `x.0`, which is behind a `*const` pointer
--> $DIR/mutability-errors.rs:24:5
|
LL | (*x).0 = 1;
| ^^^^^^^^^^ `x` is a `*const` pointer, so the data it refers to cannot be written
| ^^^^^^^^^^ `x` is a `*const` pointer, so the data it refers to cannot be written to
|
help: consider changing this to be a mutable pointer
|
Expand Down Expand Up @@ -358,24 +358,36 @@ LL | fn imm_capture(mut x: (i32,)) {
error[E0594]: cannot assign to immutable static item `X`
--> $DIR/mutability-errors.rs:76:5
|
LL | static X: (i32,) = (0,);
| ---------------- this `static` cannot be written to
...
LL | X = (1,);
| ^^^^^^^^ cannot assign

error[E0594]: cannot assign to `X.0`, as `X` is an immutable static item
--> $DIR/mutability-errors.rs:77:5
|
LL | static X: (i32,) = (0,);
| ---------------- this `static` cannot be written to
...
LL | X.0 = 1;
| ^^^^^^^ cannot assign

error[E0596]: cannot borrow immutable static item `X` as mutable
--> $DIR/mutability-errors.rs:78:5
|
LL | static X: (i32,) = (0,);
| ---------------- this `static` cannot be borrowed as mutable
...
LL | &mut X;
| ^^^^^^ cannot borrow as mutable

error[E0596]: cannot borrow `X.0` as mutable, as `X` is an immutable static item
--> $DIR/mutability-errors.rs:79:5
|
LL | static X: (i32,) = (0,);
| ---------------- this `static` cannot be borrowed as mutable
...
LL | &mut X.0;
| ^^^^^^^^ cannot borrow as mutable

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ error[E0594]: cannot assign to `*ptr`, which is behind a `*const` pointer
--> $DIR/no-invalid-mut-suggestion-for-raw-pointer-issue-127562.rs:7:14
|
LL | unsafe { *ptr = 3; }
| ^^^^^^^^ `ptr` is a `*const` pointer, so the data it refers to cannot be written
| ^^^^^^^^ `ptr` is a `*const` pointer, so the data it refers to cannot be written to
|
help: consider changing this to be a mutable pointer
|
Expand Down
3 changes: 3 additions & 0 deletions tests/ui/consts/miri_unleashed/mutable_references.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,9 @@ LL | const RAW_MUT_COERCE: SyncPtr<i32> = SyncPtr { x: &mut 0 };
error[E0594]: cannot assign to `*OH_YES`, as `OH_YES` is an immutable static item
--> $DIR/mutable_references.rs:109:5
|
LL | static OH_YES: &mut i32 = &mut 42;
| ----------------------- this `static` cannot be written to
...
LL | *OH_YES = 99;
| ^^^^^^^^^^^^ cannot assign

Expand Down
3 changes: 3 additions & 0 deletions tests/ui/consts/write_to_static_via_mut_ref.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ LL | static OH_NO: &mut i32 = &mut 42;
error[E0594]: cannot assign to `*OH_NO`, as `OH_NO` is an immutable static item
--> $DIR/write_to_static_via_mut_ref.rs:4:5
|
LL | static OH_NO: &mut i32 = &mut 42;
| ---------------------- this `static` cannot be written to
...
LL | *OH_NO = 43;
| ^^^^^^^^^^^ cannot assign

Expand Down
3 changes: 3 additions & 0 deletions tests/ui/error-codes/E0017.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ LL | const CR: &'static mut i32 = &mut C;
error[E0596]: cannot borrow immutable static item `X` as mutable
--> $DIR/E0017.rs:11:39
|
LL | static X: i32 = 1;
| ------------- this `static` cannot be borrowed as mutable
...
LL | static STATIC_REF: &'static mut i32 = &mut X;
| ^^^^^^ cannot borrow as mutable

Expand Down
2 changes: 1 addition & 1 deletion tests/ui/error-codes/E0389.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ error[E0594]: cannot assign to `fancy_ref.num`, which is behind a `&` reference
--> $DIR/E0389.rs:8:5
|
LL | fancy_ref.num = 6;
| ^^^^^^^^^^^^^^^^^ `fancy_ref` is a `&` reference, so the data it refers to cannot be written
| ^^^^^^^^^^^^^^^^^ `fancy_ref` is a `&` reference, so the data it refers to cannot be written to
|
help: consider changing this to be a mutable reference
|
Expand Down
3 changes: 3 additions & 0 deletions tests/ui/error-codes/E0594.stderr
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
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

Expand Down
3 changes: 3 additions & 0 deletions tests/ui/issues/issue-46604.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ LL | static buf: &mut [u8] = &mut [1u8,2,3,4,5,7];
error[E0594]: cannot assign to `buf[_]`, as `buf` is an immutable static item
--> $DIR/issue-46604.rs:6:5
|
LL | static buf: &mut [u8] = &mut [1u8,2,3,4,5,7];
| --------------------- this `static` cannot be written to
...
LL | buf[0]=2;
| ^^^^^^^^ cannot assign

Expand Down
4 changes: 2 additions & 2 deletions tests/ui/issues/issue-51515.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ error[E0594]: cannot assign to `*foo`, which is behind a `&` reference
--> $DIR/issue-51515.rs:4:5
|
LL | *foo = 32;
| ^^^^^^^^^ `foo` is a `&` reference, so the data it refers to cannot be written
| ^^^^^^^^^ `foo` is a `&` reference, so the data it refers to cannot be written to
|
help: consider changing this to be a mutable reference
|
Expand All @@ -13,7 +13,7 @@ error[E0594]: cannot assign to `*bar`, which is behind a `&` reference
--> $DIR/issue-51515.rs:8:5
|
LL | *bar = 64;
| ^^^^^^^^^ `bar` is a `&` reference, so the data it refers to cannot be written
| ^^^^^^^^^ `bar` is a `&` reference, so the data it refers to cannot be written to
|
help: consider specifying this binding's type
|
Expand Down
Loading
Loading