Skip to content

Commit d3992f3

Browse files
committed
Refactor the non-transient cell borrow error diagnostic
1 parent 8968c8a commit d3992f3

File tree

10 files changed

+46
-25
lines changed

10 files changed

+46
-25
lines changed

compiler/rustc_mir/src/transform/check_consts/ops.rs

+22-3
Original file line numberDiff line numberDiff line change
@@ -238,13 +238,32 @@ impl NonConstOp for TransientCellBorrow {
238238
pub struct CellBorrow;
239239
impl NonConstOp for CellBorrow {
240240
fn build_error(&self, ccx: &ConstCx<'_, 'tcx>, span: Span) -> DiagnosticBuilder<'tcx> {
241-
struct_span_err!(
241+
let mut err = struct_span_err!(
242242
ccx.tcx.sess,
243243
span,
244244
E0492,
245-
"this borrow to an interior mutable value may end up in the final value of this {}",
245+
"{}s cannot refer to interior mutable data",
246246
ccx.const_kind(),
247-
)
247+
);
248+
err.span_label(
249+
span,
250+
format!("this borrow of an interior mutable value may end up in the final value"),
251+
);
252+
if let hir::ConstContext::Static(_) = ccx.const_kind() {
253+
err.help(
254+
"To fix this, the value can be extracted to separate \
255+
`static` and then referenced.",
256+
);
257+
}
258+
if ccx.tcx.sess.teach(&err.get_code().unwrap()) {
259+
err.note(
260+
"A constant containing interior mutable data behind a reference can allow you
261+
to modify that data. This would make multiple uses of a constant to be able to
262+
see different values and allow one to escape the `Send` and `Sync` requirements
263+
for shared mutable data, which is unsound.",
264+
);
265+
}
266+
err
248267
}
249268
}
250269

src/test/ui/consts/partial_qualif.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::cell::Cell;
33
const FOO: &(Cell<usize>, bool) = {
44
let mut a = (Cell::new(0), false);
55
a.1 = true; // sets `qualif(a)` to `qualif(a) | qualif(true)`
6-
&{a} //~ ERROR borrow to an interior mutable value may end up in the final value
6+
&{a} //~ ERROR cannot refer to interior mutable
77
};
88

99
fn main() {}

src/test/ui/consts/partial_qualif.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
error[E0492]: this borrow to an interior mutable value may end up in the final value of this constant
1+
error[E0492]: constants cannot refer to interior mutable data
22
--> $DIR/partial_qualif.rs:6:5
33
|
44
LL | &{a}
5-
| ^^^^
5+
| ^^^^ this borrow of an interior mutable value may end up in the final value
66

77
error: aborting due to previous error
88

src/test/ui/consts/qualif_overwrite.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use std::cell::Cell;
77
const FOO: &Option<Cell<usize>> = {
88
let mut a = Some(Cell::new(0));
99
a = None; // sets `qualif(a)` to `qualif(a) | qualif(None)`
10-
&{a} //~ ERROR borrow to an interior mutable value may end up in the final value
10+
&{a} //~ ERROR cannot refer to interior mutable
1111
};
1212

1313
fn main() {}

src/test/ui/consts/qualif_overwrite.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
error[E0492]: this borrow to an interior mutable value may end up in the final value of this constant
1+
error[E0492]: constants cannot refer to interior mutable data
22
--> $DIR/qualif_overwrite.rs:10:5
33
|
44
LL | &{a}
5-
| ^^^^
5+
| ^^^^ this borrow of an interior mutable value may end up in the final value
66

77
error: aborting due to previous error
88

src/test/ui/consts/qualif_overwrite_2.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::cell::Cell;
55
const FOO: &Option<Cell<usize>> = {
66
let mut a = (Some(Cell::new(0)),);
77
a.0 = None; // sets `qualif(a)` to `qualif(a) | qualif(None)`
8-
&{a.0} //~ ERROR borrow to an interior mutable value may end up in the final value
8+
&{a.0} //~ ERROR cannot refer to interior mutable
99
};
1010

1111
fn main() {}

src/test/ui/consts/qualif_overwrite_2.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
error[E0492]: this borrow to an interior mutable value may end up in the final value of this constant
1+
error[E0492]: constants cannot refer to interior mutable data
22
--> $DIR/qualif_overwrite_2.rs:8:5
33
|
44
LL | &{a.0}
5-
| ^^^^^^
5+
| ^^^^^^ this borrow of an interior mutable value may end up in the final value
66

77
error: aborting due to previous error
88

src/test/ui/error-codes/E0492.stderr

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
1-
error[E0492]: this borrow to an interior mutable value may end up in the final value of this constant
1+
error[E0492]: constants cannot refer to interior mutable data
22
--> $DIR/E0492.rs:4:33
33
|
44
LL | const B: &'static AtomicUsize = &A;
5-
| ^^
5+
| ^^ this borrow of an interior mutable value may end up in the final value
66

7-
error[E0492]: this borrow to an interior mutable value may end up in the final value of this static
7+
error[E0492]: statics cannot refer to interior mutable data
88
--> $DIR/E0492.rs:5:34
99
|
1010
LL | static C: &'static AtomicUsize = &A;
11-
| ^^
11+
| ^^ this borrow of an interior mutable value may end up in the final value
12+
|
13+
= help: To fix this, the value can be extracted to separate `static` and then referenced.
1214

1315
error: aborting due to 2 previous errors
1416

src/test/ui/issues/issue-17718-const-borrow.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ use std::cell::UnsafeCell;
22

33
const A: UnsafeCell<usize> = UnsafeCell::new(1);
44
const B: &'static UnsafeCell<usize> = &A;
5-
//~^ ERROR: borrow to an interior mutable value
5+
//~^ ERROR: cannot refer to interior mutable
66

77
struct C { a: UnsafeCell<usize> }
88
const D: C = C { a: UnsafeCell::new(1) };
99
const E: &'static UnsafeCell<usize> = &D.a;
10-
//~^ ERROR: borrow to an interior mutable value
10+
//~^ ERROR: cannot refer to interior mutable
1111
const F: &'static C = &D;
12-
//~^ ERROR: borrow to an interior mutable value
12+
//~^ ERROR: cannot refer to interior mutable
1313

1414
fn main() {}

src/test/ui/issues/issue-17718-const-borrow.stderr

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
1-
error[E0492]: this borrow to an interior mutable value may end up in the final value of this constant
1+
error[E0492]: constants cannot refer to interior mutable data
22
--> $DIR/issue-17718-const-borrow.rs:4:39
33
|
44
LL | const B: &'static UnsafeCell<usize> = &A;
5-
| ^^
5+
| ^^ this borrow of an interior mutable value may end up in the final value
66

7-
error[E0492]: this borrow to an interior mutable value may end up in the final value of this constant
7+
error[E0492]: constants cannot refer to interior mutable data
88
--> $DIR/issue-17718-const-borrow.rs:9:39
99
|
1010
LL | const E: &'static UnsafeCell<usize> = &D.a;
11-
| ^^^^
11+
| ^^^^ this borrow of an interior mutable value may end up in the final value
1212

13-
error[E0492]: this borrow to an interior mutable value may end up in the final value of this constant
13+
error[E0492]: constants cannot refer to interior mutable data
1414
--> $DIR/issue-17718-const-borrow.rs:11:23
1515
|
1616
LL | const F: &'static C = &D;
17-
| ^^
17+
| ^^ this borrow of an interior mutable value may end up in the final value
1818

1919
error: aborting due to 3 previous errors
2020

0 commit comments

Comments
 (0)