Skip to content

const_panic: Allow panicking in const fn #76602

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions compiler/rustc_mir/src/transform/qualify_min_const_fn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
30 changes: 30 additions & 0 deletions src/test/ui/consts/const-eval/const_fn_panic.rs
Original file line number Diff line number Diff line change
@@ -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);
}
15 changes: 12 additions & 3 deletions src/test/ui/consts/const-eval/const_panic.rs
Original file line number Diff line number Diff line change
@@ -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
46 changes: 38 additions & 8 deletions src/test/ui/consts/const-eval/const_panic.stderr
Original file line number Diff line number Diff line change
@@ -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
|
Expand All @@ -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

12 changes: 0 additions & 12 deletions src/test/ui/consts/const-eval/const_panic_libcore.rs

This file was deleted.

33 changes: 0 additions & 33 deletions src/test/ui/consts/const-eval/const_panic_libcore.stderr

This file was deleted.

15 changes: 15 additions & 0 deletions src/test/ui/consts/const-eval/feature-gate-const_panic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
32 changes: 31 additions & 1 deletion src/test/ui/consts/const-eval/feature-gate-const_panic.stderr
Original file line number Diff line number Diff line change
@@ -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 <https://github.com/rust-lang/rust/issues/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 <https://github.com/rust-lang/rust/issues/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 <https://github.com/rust-lang/rust/issues/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
|
Expand Down Expand Up @@ -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`.