Skip to content

Commit b45f840

Browse files
committed
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 <[email protected]>
1 parent 94b4de0 commit b45f840

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed

compiler/rustc_mir/src/transform/qualify_min_const_fn.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,10 @@ fn check_terminator(
407407
} => {
408408
let fn_ty = func.ty(body, tcx);
409409
if let ty::FnDef(fn_def_id, _) = *fn_ty.kind() {
410+
// Panic functions (with one argument) might be const fn.
411+
if super::check_consts::is_lang_panic_fn(tcx, fn_def_id) {
412+
return Ok(());
413+
}
410414
// Allow unstable const if we opt in by using #[allow_internal_unstable]
411415
// on function or macro declaration.
412416
if !crate::const_eval::is_min_const_fn(tcx, fn_def_id)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// check-pass
2+
3+
#![crate_type = "lib"]
4+
#![feature(const_panic)]
5+
6+
pub const fn always_panic() {
7+
panic!("always")
8+
}
9+
10+
pub const fn assert_truth() {
11+
assert_eq!(2 + 2, 4)
12+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// check-pass
2+
3+
#![no_std]
4+
#![crate_type = "lib"]
5+
#![feature(const_panic)]
6+
7+
pub const fn always_panic() {
8+
panic!("always")
9+
}
10+
11+
pub const fn assert_truth() {
12+
assert_eq!(2 + 2, 4)
13+
}

0 commit comments

Comments
 (0)