move UnsafeCell-in-const check from interning to validation

This commit is contained in:
Ralf Jung 2020-10-24 20:49:17 +02:00
parent d0a23e613d
commit 0e014be359
7 changed files with 74 additions and 53 deletions

View file

@ -13,7 +13,7 @@ unsafe impl Sync for Meh {}
// the following will never be ok! no interior mut behind consts, because
// all allocs interned here will be marked immutable.
const MUH: Meh = Meh { //~ ERROR: mutable memory (`UnsafeCell`) is not allowed in constant
const MUH: Meh = Meh { //~ ERROR: it is undefined behavior to use this value
x: &UnsafeCell::new(42),
};
@ -24,7 +24,7 @@ unsafe impl Sync for Synced {}
// Make sure we also catch this behind a type-erased `dyn Trait` reference.
const SNEAKY: &dyn Sync = &Synced { x: UnsafeCell::new(42) };
//~^ ERROR: mutable memory (`UnsafeCell`) is not allowed in constant
//~^ ERROR: it is undefined behavior to use this value
// Make sure we also catch mutable references.
const BLUNT: &mut i32 = &mut 42;

View file

@ -1,16 +1,20 @@
error: mutable memory (`UnsafeCell`) is not allowed in constant
error[E0080]: it is undefined behavior to use this value
--> $DIR/mutable_references_err.rs:16:1
|
LL | / const MUH: Meh = Meh {
LL | | x: &UnsafeCell::new(42),
LL | | };
| |__^
| |__^ type validation failed: encountered `UnsafeCell` in a `const` at .x.<deref>
|
= note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior.
error: mutable memory (`UnsafeCell`) is not allowed in constant
error[E0080]: it is undefined behavior to use this value
--> $DIR/mutable_references_err.rs:26:1
|
LL | const SNEAKY: &dyn Sync = &Synced { x: UnsafeCell::new(42) };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered `UnsafeCell` in a `const` at .<deref>.<dyn-downcast>.x
|
= note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior.
error: mutable memory (`&mut`) is not allowed in constant
--> $DIR/mutable_references_err.rs:30:1
@ -38,3 +42,4 @@ LL | const BLUNT: &mut i32 = &mut 42;
error: aborting due to 3 previous errors; 1 warning emitted
For more information about this error, try `rustc --explain E0080`.