Skip to content

Commit 5139a6d

Browse files
authored
Rollup merge of rust-lang#90687 - jhpratt:const_panic, r=oli-obk
Permit const panics in stable const contexts in stdlib Without this change, it is not possible to use `panic!` and similar (including `assert!`) in stable const contexts inside of stdlib. See rust-lang#89542 for a real-world case that currently fails for this reason. This does _not_ affect any user code. For example, this snippet currently fails to compile: ```rust #[stable(feature = "foo", since = "1.0.0")] #[rustc_const_stable(feature = "foo", since = "1.0.0")] const fn foo() { assert!(false); assert!(false, "foo"); } ``` With the addition of `#[rustc_const_unstable]` to `core::panicking::panic`, the error no longer occurs. This snippet has been added verbatim in this PR as a UI test. To avoid needing to add `#![feature(core_panic)]` to libcore, the two instances of direct calls to `core::panicking::panic` have been switched to use the `panic!` macro. I am requesting prioritization because this is holding up other stabilizations such as rust-lang#89542 (which is otherwise ready to merge and succeeds with this change)
2 parents 76dca16 + 0505d62 commit 5139a6d

File tree

2 files changed

+3
-2
lines changed

2 files changed

+3
-2
lines changed

core/src/panicking.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ use crate::panic::{Location, PanicInfo};
3636
#[cfg_attr(not(feature = "panic_immediate_abort"), inline(never))]
3737
#[cfg_attr(feature = "panic_immediate_abort", inline)]
3838
#[track_caller]
39+
#[rustc_const_unstable(feature = "core_panic", issue = "none")]
3940
#[lang = "panic"] // needed by codegen for panic on overflow and other `Assert` MIR terminators
4041
pub const fn panic(expr: &'static str) -> ! {
4142
// Use Arguments::new_v1 instead of format_args!("{}", expr) to potentially

core/src/time.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -727,7 +727,7 @@ impl Duration {
727727
pub const fn from_secs_f64(secs: f64) -> Duration {
728728
match Duration::try_from_secs_f64(secs) {
729729
Ok(v) => v,
730-
Err(e) => crate::panicking::panic(e.description()),
730+
Err(e) => panic!("{}", e.description()),
731731
}
732732
}
733733

@@ -788,7 +788,7 @@ impl Duration {
788788
pub const fn from_secs_f32(secs: f32) -> Duration {
789789
match Duration::try_from_secs_f32(secs) {
790790
Ok(v) => v,
791-
Err(e) => crate::panicking::panic(e.description()),
791+
Err(e) => panic!("{}", e.description()),
792792
}
793793
}
794794

0 commit comments

Comments
 (0)