From b45f84029bb62d7b32f12b684b1357bcd3649842 Mon Sep 17 00:00:00 2001 From: Joe Richey Date: Fri, 11 Sep 2020 05:35:57 -0700 Subject: [PATCH 1/3] const_panic: Allow panicking in const fn We need to make sure certain panic lang items are skipped when qualifying min_const_fn. Signed-off-by: Joe Richey --- .../rustc_mir/src/transform/qualify_min_const_fn.rs | 4 ++++ src/test/ui/consts/const-eval/const_fn_panic.rs | 12 ++++++++++++ .../ui/consts/const-eval/const_fn_panic_libcore.rs | 13 +++++++++++++ 3 files changed, 29 insertions(+) create mode 100644 src/test/ui/consts/const-eval/const_fn_panic.rs create mode 100644 src/test/ui/consts/const-eval/const_fn_panic_libcore.rs diff --git a/compiler/rustc_mir/src/transform/qualify_min_const_fn.rs b/compiler/rustc_mir/src/transform/qualify_min_const_fn.rs index 7d9611e07311d..f8724f054cf20 100644 --- a/compiler/rustc_mir/src/transform/qualify_min_const_fn.rs +++ b/compiler/rustc_mir/src/transform/qualify_min_const_fn.rs @@ -407,6 +407,10 @@ fn check_terminator( } => { let fn_ty = func.ty(body, tcx); if let ty::FnDef(fn_def_id, _) = *fn_ty.kind() { + // Panic functions (with one argument) might be const fn. + if super::check_consts::is_lang_panic_fn(tcx, fn_def_id) { + return Ok(()); + } // Allow unstable const if we opt in by using #[allow_internal_unstable] // on function or macro declaration. if !crate::const_eval::is_min_const_fn(tcx, fn_def_id) diff --git a/src/test/ui/consts/const-eval/const_fn_panic.rs b/src/test/ui/consts/const-eval/const_fn_panic.rs new file mode 100644 index 0000000000000..89db8f5ac5846 --- /dev/null +++ b/src/test/ui/consts/const-eval/const_fn_panic.rs @@ -0,0 +1,12 @@ +// check-pass + +#![crate_type = "lib"] +#![feature(const_panic)] + +pub const fn always_panic() { + panic!("always") +} + +pub const fn assert_truth() { + assert_eq!(2 + 2, 4) +} diff --git a/src/test/ui/consts/const-eval/const_fn_panic_libcore.rs b/src/test/ui/consts/const-eval/const_fn_panic_libcore.rs new file mode 100644 index 0000000000000..69c29aaefe1f6 --- /dev/null +++ b/src/test/ui/consts/const-eval/const_fn_panic_libcore.rs @@ -0,0 +1,13 @@ +// check-pass + +#![no_std] +#![crate_type = "lib"] +#![feature(const_panic)] + +pub const fn always_panic() { + panic!("always") +} + +pub const fn assert_truth() { + assert_eq!(2 + 2, 4) +} From 2637cd3c7d992dc1275d8cd4b05580161a6bf24b Mon Sep 17 00:00:00 2001 From: Joe Richey Date: Sat, 12 Sep 2020 01:31:02 -0700 Subject: [PATCH 2/3] const_panic: Update/merge tests We don't need separate test files for std and core, we can just test everything in one file using the full paths. We also note why we use assert! instead of assert_eq! or assert_ne! Signed-off-by: Joe Richey --- .../ui/consts/const-eval/const_fn_panic.rs | 26 +++++++++-- .../const-eval/const_fn_panic_libcore.rs | 13 ------ src/test/ui/consts/const-eval/const_panic.rs | 15 ++++-- .../ui/consts/const-eval/const_panic.stderr | 46 +++++++++++++++---- .../consts/const-eval/const_panic_libcore.rs | 12 ----- .../const-eval/const_panic_libcore.stderr | 33 ------------- .../const-eval/feature-gate-const_panic.rs | 15 ++++++ .../feature-gate-const_panic.stderr | 32 ++++++++++++- 8 files changed, 118 insertions(+), 74 deletions(-) delete mode 100644 src/test/ui/consts/const-eval/const_fn_panic_libcore.rs delete mode 100644 src/test/ui/consts/const-eval/const_panic_libcore.rs delete mode 100644 src/test/ui/consts/const-eval/const_panic_libcore.stderr diff --git a/src/test/ui/consts/const-eval/const_fn_panic.rs b/src/test/ui/consts/const-eval/const_fn_panic.rs index 89db8f5ac5846..5c3f493459e05 100644 --- a/src/test/ui/consts/const-eval/const_fn_panic.rs +++ b/src/test/ui/consts/const-eval/const_fn_panic.rs @@ -3,10 +3,28 @@ #![crate_type = "lib"] #![feature(const_panic)] -pub const fn always_panic() { - panic!("always") +// Can't use assert_{eq, ne}!() yet as panic!() only supports a single argument. + +pub const fn always_panic_std() { + std::panic!("always"); +} + +pub const fn assert_truth_std() { + std::assert!(2 + 2 == 4); +} + +pub const fn assert_false_std() { + std::assert!(2 + 2 != 4); +} + +pub const fn always_panic_core() { + core::panic!("always"); } -pub const fn assert_truth() { - assert_eq!(2 + 2, 4) +pub const fn assert_truth_core() { + core::assert!(2 + 2 == 4); } + +pub const fn assert_false_core() { + core::assert!(2 + 2 != 4); +} \ No newline at end of file diff --git a/src/test/ui/consts/const-eval/const_fn_panic_libcore.rs b/src/test/ui/consts/const-eval/const_fn_panic_libcore.rs deleted file mode 100644 index 69c29aaefe1f6..0000000000000 --- a/src/test/ui/consts/const-eval/const_fn_panic_libcore.rs +++ /dev/null @@ -1,13 +0,0 @@ -// check-pass - -#![no_std] -#![crate_type = "lib"] -#![feature(const_panic)] - -pub const fn always_panic() { - panic!("always") -} - -pub const fn assert_truth() { - assert_eq!(2 + 2, 4) -} diff --git a/src/test/ui/consts/const-eval/const_panic.rs b/src/test/ui/consts/const-eval/const_panic.rs index 3e5112b0b145f..8116a3848444d 100644 --- a/src/test/ui/consts/const-eval/const_panic.rs +++ b/src/test/ui/consts/const-eval/const_panic.rs @@ -1,11 +1,20 @@ #![feature(const_panic)] #![crate_type = "lib"] -pub const Z: () = panic!("cheese"); +pub const Z: () = std::panic!("cheese"); //~^ ERROR any use of this value will cause an error -pub const Y: () = unreachable!(); +pub const Y: () = std::assert!(1 == 2); //~^ ERROR any use of this value will cause an error -pub const X: () = unimplemented!(); +pub const X: () = std::unimplemented!(); +//~^ ERROR any use of this value will cause an error + +pub const W: () = core::panic!("cheese"); +//~^ ERROR any use of this value will cause an error + +pub const V: () = core::assert!(1.2 < 1.0); +//~^ ERROR any use of this value will cause an error + +pub const U: () = core::unimplemented!(); //~^ ERROR any use of this value will cause an error diff --git a/src/test/ui/consts/const-eval/const_panic.stderr b/src/test/ui/consts/const-eval/const_panic.stderr index 679d8f280cc60..fbc21e64139a4 100644 --- a/src/test/ui/consts/const-eval/const_panic.stderr +++ b/src/test/ui/consts/const-eval/const_panic.stderr @@ -1,8 +1,8 @@ error: any use of this value will cause an error --> $DIR/const_panic.rs:4:19 | -LL | pub const Z: () = panic!("cheese"); - | ------------------^^^^^^^^^^^^^^^^- +LL | pub const Z: () = std::panic!("cheese"); + | ------------------^^^^^^^^^^^^^^^^^^^^^- | | | the evaluated program panicked at 'cheese', $DIR/const_panic.rs:4:19 | @@ -12,22 +12,52 @@ LL | pub const Z: () = panic!("cheese"); error: any use of this value will cause an error --> $DIR/const_panic.rs:7:19 | -LL | pub const Y: () = unreachable!(); - | ------------------^^^^^^^^^^^^^^- +LL | pub const Y: () = std::assert!(1 == 2); + | ------------------^^^^^^^^^^^^^^^^^^^^- | | - | the evaluated program panicked at 'internal error: entered unreachable code', $DIR/const_panic.rs:7:19 + | the evaluated program panicked at 'assertion failed: 1 == 2', $DIR/const_panic.rs:7:19 | = note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info) error: any use of this value will cause an error --> $DIR/const_panic.rs:10:19 | -LL | pub const X: () = unimplemented!(); - | ------------------^^^^^^^^^^^^^^^^- +LL | pub const X: () = std::unimplemented!(); + | ------------------^^^^^^^^^^^^^^^^^^^^^- | | | the evaluated program panicked at 'not implemented', $DIR/const_panic.rs:10:19 | = note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info) -error: aborting due to 3 previous errors +error: any use of this value will cause an error + --> $DIR/const_panic.rs:13:19 + | +LL | pub const W: () = core::panic!("cheese"); + | ------------------^^^^^^^^^^^^^^^^^^^^^^- + | | + | the evaluated program panicked at 'cheese', $DIR/const_panic.rs:13:19 + | + = note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info) + +error: any use of this value will cause an error + --> $DIR/const_panic.rs:16:19 + | +LL | pub const V: () = core::assert!(1.2 < 1.0); + | ------------------^^^^^^^^^^^^^^^^^^^^^^^^- + | | + | the evaluated program panicked at 'assertion failed: 1.2 < 1.0', $DIR/const_panic.rs:16:19 + | + = note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info) + +error: any use of this value will cause an error + --> $DIR/const_panic.rs:19:19 + | +LL | pub const U: () = core::unimplemented!(); + | ------------------^^^^^^^^^^^^^^^^^^^^^^- + | | + | the evaluated program panicked at 'not implemented', $DIR/const_panic.rs:19:19 + | + = note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info) + +error: aborting due to 6 previous errors diff --git a/src/test/ui/consts/const-eval/const_panic_libcore.rs b/src/test/ui/consts/const-eval/const_panic_libcore.rs deleted file mode 100644 index e42685e9c76b0..0000000000000 --- a/src/test/ui/consts/const-eval/const_panic_libcore.rs +++ /dev/null @@ -1,12 +0,0 @@ -#![no_std] -#![crate_type = "lib"] -#![feature(const_panic)] - -const Z: () = panic!("cheese"); -//~^ ERROR any use of this value will cause an error - -const Y: () = unreachable!(); -//~^ ERROR any use of this value will cause an error - -const X: () = unimplemented!(); -//~^ ERROR any use of this value will cause an error diff --git a/src/test/ui/consts/const-eval/const_panic_libcore.stderr b/src/test/ui/consts/const-eval/const_panic_libcore.stderr deleted file mode 100644 index 2abf158aade54..0000000000000 --- a/src/test/ui/consts/const-eval/const_panic_libcore.stderr +++ /dev/null @@ -1,33 +0,0 @@ -error: any use of this value will cause an error - --> $DIR/const_panic_libcore.rs:5:15 - | -LL | const Z: () = panic!("cheese"); - | --------------^^^^^^^^^^^^^^^^- - | | - | the evaluated program panicked at 'cheese', $DIR/const_panic_libcore.rs:5:15 - | - = note: `#[deny(const_err)]` on by default - = note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info) - -error: any use of this value will cause an error - --> $DIR/const_panic_libcore.rs:8:15 - | -LL | const Y: () = unreachable!(); - | --------------^^^^^^^^^^^^^^- - | | - | the evaluated program panicked at 'internal error: entered unreachable code', $DIR/const_panic_libcore.rs:8:15 - | - = note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info) - -error: any use of this value will cause an error - --> $DIR/const_panic_libcore.rs:11:15 - | -LL | const X: () = unimplemented!(); - | --------------^^^^^^^^^^^^^^^^- - | | - | the evaluated program panicked at 'not implemented', $DIR/const_panic_libcore.rs:11:15 - | - = note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info) - -error: aborting due to 3 previous errors - diff --git a/src/test/ui/consts/const-eval/feature-gate-const_panic.rs b/src/test/ui/consts/const-eval/feature-gate-const_panic.rs index ba5b07239a200..a7fe013749a2d 100644 --- a/src/test/ui/consts/const-eval/feature-gate-const_panic.rs +++ b/src/test/ui/consts/const-eval/feature-gate-const_panic.rs @@ -8,3 +8,18 @@ const Y: () = unreachable!(); const X: () = unimplemented!(); //~^ ERROR panicking in constants is unstable + +const fn a() { + assert!(2 + 2 == 4); + //~^ ERROR panicking in constant functions is unstable +} + +const fn b() { + panic!("oh no"); + //~^ ERROR panicking in constant functions is unstable +} + +const fn c() { + unimplemented!(); + //~^ ERROR panicking in constant functions is unstable +} diff --git a/src/test/ui/consts/const-eval/feature-gate-const_panic.stderr b/src/test/ui/consts/const-eval/feature-gate-const_panic.stderr index 56746c04f5cd7..b92bbe403ab5c 100644 --- a/src/test/ui/consts/const-eval/feature-gate-const_panic.stderr +++ b/src/test/ui/consts/const-eval/feature-gate-const_panic.stderr @@ -1,3 +1,33 @@ +error[E0658]: panicking in constant functions is unstable + --> $DIR/feature-gate-const_panic.rs:13:5 + | +LL | assert!(2 + 2 == 4); + | ^^^^^^^^^^^^^^^^^^^^ + | + = note: see issue #51999 for more information + = help: add `#![feature(const_panic)]` to the crate attributes to enable + = note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info) + +error[E0658]: panicking in constant functions is unstable + --> $DIR/feature-gate-const_panic.rs:18:5 + | +LL | panic!("oh no"); + | ^^^^^^^^^^^^^^^^ + | + = note: see issue #51999 for more information + = help: add `#![feature(const_panic)]` to the crate attributes to enable + = note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info) + +error[E0658]: panicking in constant functions is unstable + --> $DIR/feature-gate-const_panic.rs:23:5 + | +LL | unimplemented!(); + | ^^^^^^^^^^^^^^^^^ + | + = note: see issue #51999 for more information + = help: add `#![feature(const_panic)]` to the crate attributes to enable + = note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info) + error[E0658]: panicking in constants is unstable --> $DIR/feature-gate-const_panic.rs:3:15 | @@ -28,6 +58,6 @@ LL | const Y: () = unreachable!(); = help: add `#![feature(const_panic)]` to the crate attributes to enable = note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info) -error: aborting due to 3 previous errors +error: aborting due to 6 previous errors For more information about this error, try `rustc --explain E0658`. From 56510340c1693746634e2f6e6659ba95df6f9c74 Mon Sep 17 00:00:00 2001 From: Joe Richey Date: Sat, 12 Sep 2020 01:54:07 -0700 Subject: [PATCH 3/3] const_panic: Add trailing newline Signed-off-by: Joe Richey --- src/test/ui/consts/const-eval/const_fn_panic.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/ui/consts/const-eval/const_fn_panic.rs b/src/test/ui/consts/const-eval/const_fn_panic.rs index 5c3f493459e05..7eed77991f807 100644 --- a/src/test/ui/consts/const-eval/const_fn_panic.rs +++ b/src/test/ui/consts/const-eval/const_fn_panic.rs @@ -27,4 +27,4 @@ pub const fn assert_truth_core() { pub const fn assert_false_core() { core::assert!(2 + 2 != 4); -} \ No newline at end of file +}