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 1 commit
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
12 changes: 12 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,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)
}
13 changes: 13 additions & 0 deletions src/test/ui/consts/const-eval/const_fn_panic_libcore.rs
Original file line number Diff line number Diff line change
@@ -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)
}