diff --git a/tests/crashes/122548.rs b/tests/crashes/122548.rs deleted file mode 100644 index 232ce5d441313..0000000000000 --- a/tests/crashes/122548.rs +++ /dev/null @@ -1,17 +0,0 @@ -//@ known-bug: #122548 -#![feature(const_mut_refs)] -#![feature(const_refs_to_static)] - -use std::cell::UnsafeCell; - -struct Meh { - x: &'static UnsafeCell, -} - -const MUH: Meh = Meh { - x: &mut *(&READONLY as *const _ as *mut _), -}; - -static READONLY: i32 = 0; - -pub fn main() {} diff --git a/tests/ui/statics/mutable_memory_validation.rs b/tests/ui/statics/mutable_memory_validation.rs new file mode 100644 index 0000000000000..fcf6ad1627772 --- /dev/null +++ b/tests/ui/statics/mutable_memory_validation.rs @@ -0,0 +1,21 @@ +//issue: rust-lang/rust#122548 + +// Strip out raw byte dumps to make comparison platform-independent: +//@ normalize-stderr-test "(the raw bytes of the constant) \(size: [0-9]*, align: [0-9]*\)" -> "$1 (size: $$SIZE, align: $$ALIGN)" +//@ normalize-stderr-test "([0-9a-f][0-9a-f] |╾─*A(LLOC)?[0-9]+(\+[a-z0-9]+)?()?─*╼ )+ *│.*" -> "HEX_DUMP" + +#![feature(const_mut_refs)] +#![feature(const_refs_to_static)] + +use std::cell::UnsafeCell; + +struct Meh { + x: &'static UnsafeCell, +} + +const MUH: Meh = Meh { x: unsafe { &mut *(&READONLY as *const _ as *mut _) } }; +//~^ ERROR: it is undefined behavior to use this value + +static READONLY: i32 = 0; + +pub fn main() {} diff --git a/tests/ui/statics/mutable_memory_validation.stderr b/tests/ui/statics/mutable_memory_validation.stderr new file mode 100644 index 0000000000000..f21269235e9b8 --- /dev/null +++ b/tests/ui/statics/mutable_memory_validation.stderr @@ -0,0 +1,14 @@ +error[E0080]: it is undefined behavior to use this value + --> $DIR/mutable_memory_validation.rs:16:1 + | +LL | const MUH: Meh = Meh { x: unsafe { &mut *(&READONLY as *const _ as *mut _) } }; + | ^^^^^^^^^^^^^^ constructing invalid value at .x.: encountered `UnsafeCell` in read-only memory + | + = 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. + = note: the raw bytes of the constant (size: $SIZE, align: $ALIGN) { + HEX_DUMP + } + +error: aborting due to 1 previous error + +For more information about this error, try `rustc --explain E0080`.