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..7eed77991f807 --- /dev/null +++ b/src/test/ui/consts/const-eval/const_fn_panic.rs @@ -0,0 +1,30 @@ +// check-pass + +#![crate_type = "lib"] +#![feature(const_panic)] + +// 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_core() { + core::assert!(2 + 2 == 4); +} + +pub const fn assert_false_core() { + core::assert!(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`.