From db20f248ebcd2a6cb5230b3bde7f400a380b0b87 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Tue, 24 Dec 2019 12:20:36 +0100 Subject: [PATCH 1/4] add test for dropping in const --- src/test/ui/consts/miri_unleashed/drop.rs | 19 +++++++++++++++ src/test/ui/consts/miri_unleashed/drop.stderr | 24 +++++++++++++++++++ 2 files changed, 43 insertions(+) create mode 100644 src/test/ui/consts/miri_unleashed/drop.rs create mode 100644 src/test/ui/consts/miri_unleashed/drop.stderr diff --git a/src/test/ui/consts/miri_unleashed/drop.rs b/src/test/ui/consts/miri_unleashed/drop.rs new file mode 100644 index 0000000000000..ab9d874d579d8 --- /dev/null +++ b/src/test/ui/consts/miri_unleashed/drop.rs @@ -0,0 +1,19 @@ +// compile-flags: -Zunleash-the-miri-inside-of-you +#![deny(const_err)] + +use std::mem::ManuallyDrop; + +fn main() {} + +static TEST_OK: () = { + let v: Vec = Vec::new(); + let _v = ManuallyDrop::new(v); +}; + +// Make sure we catch executing bad drop functions. +// The actual error is located in `real_drop_in_place` so we can't capture it with the +// error annotations here. +static TEST_BAD: () = { + let _v: Vec = Vec::new(); + //~^ WARN skipping const check +}; diff --git a/src/test/ui/consts/miri_unleashed/drop.stderr b/src/test/ui/consts/miri_unleashed/drop.stderr new file mode 100644 index 0000000000000..b8fc6a0271d77 --- /dev/null +++ b/src/test/ui/consts/miri_unleashed/drop.stderr @@ -0,0 +1,24 @@ +warning: skipping const checks + --> $DIR/drop.rs:17:9 + | +LL | let _v: Vec = Vec::new(); + | ^^ + +error[E0080]: could not evaluate static initializer + --> $SRC_DIR/libcore/ptr/mod.rs:LL:COL + | +LL | / unsafe fn real_drop_in_place(to_drop: &mut T) { +LL | | // Code here does not matter - this is replaced by the +LL | | // real drop glue by the compiler. +LL | | real_drop_in_place(to_drop) +LL | | } + | |_^ calling non-const function ` as std::ops::Drop>::drop` + | + ::: $DIR/drop.rs:19:1 + | +LL | }; + | - inside call to `std::ptr::real_drop_in_place::> - shim(Some(std::vec::Vec))` at $DIR/drop.rs:19:1 + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0080`. From 072676023ee1ca5f69d071f85995c929e091eb52 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Tue, 24 Dec 2019 12:24:32 +0100 Subject: [PATCH 2/4] better variable names --- .../miri_unleashed/const_refers_to_static.rs | 10 ++++---- .../const_refers_to_static.stderr | 24 +++++++++---------- 2 files changed, 17 insertions(+), 17 deletions(-) diff --git a/src/test/ui/consts/miri_unleashed/const_refers_to_static.rs b/src/test/ui/consts/miri_unleashed/const_refers_to_static.rs index 55f3f1c848855..e07db3ffba25e 100644 --- a/src/test/ui/consts/miri_unleashed/const_refers_to_static.rs +++ b/src/test/ui/consts/miri_unleashed/const_refers_to_static.rs @@ -6,31 +6,31 @@ use std::sync::atomic::AtomicUsize; use std::sync::atomic::Ordering; -const BOO: &usize = { //~ ERROR undefined behavior to use this value +const REF_INTERIOR_MUT: &usize = { //~ ERROR undefined behavior to use this value static FOO: AtomicUsize = AtomicUsize::new(0); unsafe { &*(&FOO as *const _ as *const usize) } //~^ WARN skipping const checks }; -const FOO: usize = { +const MUTATE_INTERIOR_MUT: usize = { static FOO: AtomicUsize = AtomicUsize::new(0); FOO.fetch_add(1, Ordering::Relaxed) //~ WARN any use of this value will cause an error //~^ WARN skipping const checks //~| WARN skipping const checks }; -const BAR: usize = { +const READ_INTERIOR_MUT: usize = { static FOO: AtomicUsize = AtomicUsize::new(0); unsafe { *(&FOO as *const _ as *const usize) } //~ WARN any use of this value will cause an err //~^ WARN skipping const checks }; static mut MUTABLE: u32 = 0; -const BAD: u32 = unsafe { MUTABLE }; //~ WARN any use of this value will cause an error +const READ_MUT: u32 = unsafe { MUTABLE }; //~ WARN any use of this value will cause an error //~^ WARN skipping const checks // ok some day perhaps -const BOO_OK: &usize = { //~ ERROR it is undefined behavior to use this value +const READ_IMMUT: &usize = { //~ ERROR it is undefined behavior to use this value static FOO: usize = 0; &FOO //~^ WARN skipping const checks diff --git a/src/test/ui/consts/miri_unleashed/const_refers_to_static.stderr b/src/test/ui/consts/miri_unleashed/const_refers_to_static.stderr index 6ae88558d7003..eae76c1389b62 100644 --- a/src/test/ui/consts/miri_unleashed/const_refers_to_static.stderr +++ b/src/test/ui/consts/miri_unleashed/const_refers_to_static.stderr @@ -23,10 +23,10 @@ LL | unsafe { *(&FOO as *const _ as *const usize) } | ^^^ warning: skipping const checks - --> $DIR/const_refers_to_static.rs:29:27 + --> $DIR/const_refers_to_static.rs:29:32 | -LL | const BAD: u32 = unsafe { MUTABLE }; - | ^^^^^^^ +LL | const READ_MUT: u32 = unsafe { MUTABLE }; + | ^^^^^^^ warning: skipping const checks --> $DIR/const_refers_to_static.rs:35:6 @@ -37,7 +37,7 @@ LL | &FOO error[E0080]: it is undefined behavior to use this value --> $DIR/const_refers_to_static.rs:9:1 | -LL | / const BOO: &usize = { +LL | / const REF_INTERIOR_MUT: &usize = { LL | | static FOO: AtomicUsize = AtomicUsize::new(0); LL | | unsafe { &*(&FOO as *const _ as *const usize) } LL | | @@ -49,7 +49,7 @@ LL | | }; warning: any use of this value will cause an error --> $DIR/const_refers_to_static.rs:17:5 | -LL | / const FOO: usize = { +LL | / const MUTATE_INTERIOR_MUT: usize = { LL | | static FOO: AtomicUsize = AtomicUsize::new(0); LL | | FOO.fetch_add(1, Ordering::Relaxed) | | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ calling non-const function `std::sync::atomic::AtomicUsize::fetch_add` @@ -67,7 +67,7 @@ LL | #![warn(const_err)] warning: any use of this value will cause an error --> $DIR/const_refers_to_static.rs:24:14 | -LL | / const BAR: usize = { +LL | / const READ_INTERIOR_MUT: usize = { LL | | static FOO: AtomicUsize = AtomicUsize::new(0); LL | | unsafe { *(&FOO as *const _ as *const usize) } | | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ constant accesses static @@ -76,17 +76,17 @@ LL | | }; | |__- warning: any use of this value will cause an error - --> $DIR/const_refers_to_static.rs:29:27 + --> $DIR/const_refers_to_static.rs:29:32 | -LL | const BAD: u32 = unsafe { MUTABLE }; - | --------------------------^^^^^^^--- - | | - | constant accesses static +LL | const READ_MUT: u32 = unsafe { MUTABLE }; + | -------------------------------^^^^^^^--- + | | + | constant accesses static error[E0080]: it is undefined behavior to use this value --> $DIR/const_refers_to_static.rs:33:1 | -LL | / const BOO_OK: &usize = { +LL | / const READ_IMMUT: &usize = { LL | | static FOO: usize = 0; LL | | &FOO LL | | From ffb6aa1288e77c4251043ec08b5731a3a65d92fb Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Tue, 24 Dec 2019 12:28:24 +0100 Subject: [PATCH 3/4] this has sysroot spans to let's ignore it the usual way --- src/test/ui/consts/miri_unleashed/drop.rs | 1 + src/test/ui/consts/miri_unleashed/drop.stderr | 6 +++--- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/test/ui/consts/miri_unleashed/drop.rs b/src/test/ui/consts/miri_unleashed/drop.rs index ab9d874d579d8..99b317cb0d988 100644 --- a/src/test/ui/consts/miri_unleashed/drop.rs +++ b/src/test/ui/consts/miri_unleashed/drop.rs @@ -1,4 +1,5 @@ // compile-flags: -Zunleash-the-miri-inside-of-you +// ignore-x86 FIXME: missing sysroot spans (#53081) #![deny(const_err)] use std::mem::ManuallyDrop; diff --git a/src/test/ui/consts/miri_unleashed/drop.stderr b/src/test/ui/consts/miri_unleashed/drop.stderr index b8fc6a0271d77..5603eeb6dc037 100644 --- a/src/test/ui/consts/miri_unleashed/drop.stderr +++ b/src/test/ui/consts/miri_unleashed/drop.stderr @@ -1,5 +1,5 @@ warning: skipping const checks - --> $DIR/drop.rs:17:9 + --> $DIR/drop.rs:18:9 | LL | let _v: Vec = Vec::new(); | ^^ @@ -14,10 +14,10 @@ LL | | real_drop_in_place(to_drop) LL | | } | |_^ calling non-const function ` as std::ops::Drop>::drop` | - ::: $DIR/drop.rs:19:1 + ::: $DIR/drop.rs:20:1 | LL | }; - | - inside call to `std::ptr::real_drop_in_place::> - shim(Some(std::vec::Vec))` at $DIR/drop.rs:19:1 + | - inside call to `std::ptr::real_drop_in_place::> - shim(Some(std::vec::Vec))` at $DIR/drop.rs:20:1 error: aborting due to previous error From 40b8b7c338d2a55a5852fbe2da5b70edfee7dc90 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Wed, 25 Dec 2019 11:18:39 +0100 Subject: [PATCH 4/4] use error-pattern --- src/test/ui/consts/miri_unleashed/drop.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/test/ui/consts/miri_unleashed/drop.rs b/src/test/ui/consts/miri_unleashed/drop.rs index 99b317cb0d988..d66ca53dfb8d3 100644 --- a/src/test/ui/consts/miri_unleashed/drop.rs +++ b/src/test/ui/consts/miri_unleashed/drop.rs @@ -1,5 +1,6 @@ // compile-flags: -Zunleash-the-miri-inside-of-you // ignore-x86 FIXME: missing sysroot spans (#53081) +// error-pattern: calling non-const function ` as std::ops::Drop>::drop` #![deny(const_err)] use std::mem::ManuallyDrop; @@ -12,8 +13,7 @@ static TEST_OK: () = { }; // Make sure we catch executing bad drop functions. -// The actual error is located in `real_drop_in_place` so we can't capture it with the -// error annotations here. +// The actual error is tested by the error-pattern above. static TEST_BAD: () = { let _v: Vec = Vec::new(); //~^ WARN skipping const check