1
Fork 0

Refactor the non-transient cell borrow error diagnostic

This commit is contained in:
oli 2021-01-03 14:46:19 +00:00
parent 8968c8a103
commit d3992f36ad
10 changed files with 46 additions and 25 deletions

View file

@ -238,13 +238,32 @@ impl NonConstOp for TransientCellBorrow {
pub struct CellBorrow; pub struct CellBorrow;
impl NonConstOp for CellBorrow { impl NonConstOp for CellBorrow {
fn build_error(&self, ccx: &ConstCx<'_, 'tcx>, span: Span) -> DiagnosticBuilder<'tcx> { fn build_error(&self, ccx: &ConstCx<'_, 'tcx>, span: Span) -> DiagnosticBuilder<'tcx> {
struct_span_err!( let mut err = struct_span_err!(
ccx.tcx.sess, ccx.tcx.sess,
span, span,
E0492, E0492,
"this borrow to an interior mutable value may end up in the final value of this {}", "{}s cannot refer to interior mutable data",
ccx.const_kind(), ccx.const_kind(),
) );
err.span_label(
span,
format!("this borrow of an interior mutable value may end up in the final value"),
);
if let hir::ConstContext::Static(_) = ccx.const_kind() {
err.help(
"To fix this, the value can be extracted to separate \
`static` and then referenced.",
);
}
if ccx.tcx.sess.teach(&err.get_code().unwrap()) {
err.note(
"A constant containing interior mutable data behind a reference can allow you
to modify that data. This would make multiple uses of a constant to be able to
see different values and allow one to escape the `Send` and `Sync` requirements
for shared mutable data, which is unsound.",
);
}
err
} }
} }

View file

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

View file

@ -1,8 +1,8 @@
error[E0492]: this borrow to an interior mutable value may end up in the final value of this constant error[E0492]: constants cannot refer to interior mutable data
--> $DIR/partial_qualif.rs:6:5 --> $DIR/partial_qualif.rs:6:5
| |
LL | &{a} LL | &{a}
| ^^^^ | ^^^^ this borrow of an interior mutable value may end up in the final value
error: aborting due to previous error error: aborting due to previous error

View file

@ -7,7 +7,7 @@ use std::cell::Cell;
const FOO: &Option<Cell<usize>> = { const FOO: &Option<Cell<usize>> = {
let mut a = Some(Cell::new(0)); let mut a = Some(Cell::new(0));
a = None; // sets `qualif(a)` to `qualif(a) | qualif(None)` a = None; // sets `qualif(a)` to `qualif(a) | qualif(None)`
&{a} //~ ERROR borrow to an interior mutable value may end up in the final value &{a} //~ ERROR cannot refer to interior mutable
}; };
fn main() {} fn main() {}

View file

@ -1,8 +1,8 @@
error[E0492]: this borrow to an interior mutable value may end up in the final value of this constant error[E0492]: constants cannot refer to interior mutable data
--> $DIR/qualif_overwrite.rs:10:5 --> $DIR/qualif_overwrite.rs:10:5
| |
LL | &{a} LL | &{a}
| ^^^^ | ^^^^ this borrow of an interior mutable value may end up in the final value
error: aborting due to previous error error: aborting due to previous error

View file

@ -5,7 +5,7 @@ use std::cell::Cell;
const FOO: &Option<Cell<usize>> = { const FOO: &Option<Cell<usize>> = {
let mut a = (Some(Cell::new(0)),); let mut a = (Some(Cell::new(0)),);
a.0 = None; // sets `qualif(a)` to `qualif(a) | qualif(None)` a.0 = None; // sets `qualif(a)` to `qualif(a) | qualif(None)`
&{a.0} //~ ERROR borrow to an interior mutable value may end up in the final value &{a.0} //~ ERROR cannot refer to interior mutable
}; };
fn main() {} fn main() {}

View file

@ -1,8 +1,8 @@
error[E0492]: this borrow to an interior mutable value may end up in the final value of this constant error[E0492]: constants cannot refer to interior mutable data
--> $DIR/qualif_overwrite_2.rs:8:5 --> $DIR/qualif_overwrite_2.rs:8:5
| |
LL | &{a.0} LL | &{a.0}
| ^^^^^^ | ^^^^^^ this borrow of an interior mutable value may end up in the final value
error: aborting due to previous error error: aborting due to previous error

View file

@ -1,14 +1,16 @@
error[E0492]: this borrow to an interior mutable value may end up in the final value of this constant error[E0492]: constants cannot refer to interior mutable data
--> $DIR/E0492.rs:4:33 --> $DIR/E0492.rs:4:33
| |
LL | const B: &'static AtomicUsize = &A; LL | const B: &'static AtomicUsize = &A;
| ^^ | ^^ this borrow of an interior mutable value may end up in the final value
error[E0492]: this borrow to an interior mutable value may end up in the final value of this static error[E0492]: statics cannot refer to interior mutable data
--> $DIR/E0492.rs:5:34 --> $DIR/E0492.rs:5:34
| |
LL | static C: &'static AtomicUsize = &A; LL | static C: &'static AtomicUsize = &A;
| ^^ | ^^ this borrow of an interior mutable value may end up in the final value
|
= help: To fix this, the value can be extracted to separate `static` and then referenced.
error: aborting due to 2 previous errors error: aborting due to 2 previous errors

View file

@ -2,13 +2,13 @@ use std::cell::UnsafeCell;
const A: UnsafeCell<usize> = UnsafeCell::new(1); const A: UnsafeCell<usize> = UnsafeCell::new(1);
const B: &'static UnsafeCell<usize> = &A; const B: &'static UnsafeCell<usize> = &A;
//~^ ERROR: borrow to an interior mutable value //~^ ERROR: cannot refer to interior mutable
struct C { a: UnsafeCell<usize> } struct C { a: UnsafeCell<usize> }
const D: C = C { a: UnsafeCell::new(1) }; const D: C = C { a: UnsafeCell::new(1) };
const E: &'static UnsafeCell<usize> = &D.a; const E: &'static UnsafeCell<usize> = &D.a;
//~^ ERROR: borrow to an interior mutable value //~^ ERROR: cannot refer to interior mutable
const F: &'static C = &D; const F: &'static C = &D;
//~^ ERROR: borrow to an interior mutable value //~^ ERROR: cannot refer to interior mutable
fn main() {} fn main() {}

View file

@ -1,20 +1,20 @@
error[E0492]: this borrow to an interior mutable value may end up in the final value of this constant error[E0492]: constants cannot refer to interior mutable data
--> $DIR/issue-17718-const-borrow.rs:4:39 --> $DIR/issue-17718-const-borrow.rs:4:39
| |
LL | const B: &'static UnsafeCell<usize> = &A; LL | const B: &'static UnsafeCell<usize> = &A;
| ^^ | ^^ this borrow of an interior mutable value may end up in the final value
error[E0492]: this borrow to an interior mutable value may end up in the final value of this constant error[E0492]: constants cannot refer to interior mutable data
--> $DIR/issue-17718-const-borrow.rs:9:39 --> $DIR/issue-17718-const-borrow.rs:9:39
| |
LL | const E: &'static UnsafeCell<usize> = &D.a; LL | const E: &'static UnsafeCell<usize> = &D.a;
| ^^^^ | ^^^^ this borrow of an interior mutable value may end up in the final value
error[E0492]: this borrow to an interior mutable value may end up in the final value of this constant error[E0492]: constants cannot refer to interior mutable data
--> $DIR/issue-17718-const-borrow.rs:11:23 --> $DIR/issue-17718-const-borrow.rs:11:23
| |
LL | const F: &'static C = &D; LL | const F: &'static C = &D;
| ^^ | ^^ this borrow of an interior mutable value may end up in the final value
error: aborting due to 3 previous errors error: aborting due to 3 previous errors