@@ -77,23 +77,36 @@ pub const fn panic_fmt(fmt: fmt::Arguments<'_>) -> ! {
77
77
// and unwinds anyway, we will hit the "unwinding out of nounwind function" guard,
78
78
// which causes a "panic in a function that cannot unwind".
79
79
#[ rustc_nounwind]
80
- pub fn panic_nounwind_fmt ( fmt : fmt:: Arguments < ' _ > ) -> ! {
81
- if cfg ! ( feature = "panic_immediate_abort" ) {
82
- super :: intrinsics:: abort ( )
83
- }
80
+ #[ rustc_const_unstable( feature = "core_panic" , issue = "none" ) ]
81
+ pub const fn panic_nounwind_fmt ( fmt : fmt:: Arguments < ' _ > ) -> ! {
82
+ fn runtime ( fmt : fmt:: Arguments < ' _ > ) -> ! {
83
+ if cfg ! ( feature = "panic_immediate_abort" ) {
84
+ super :: intrinsics:: abort ( )
85
+ }
84
86
85
- // NOTE This function never crosses the FFI boundary; it's a Rust-to-Rust call
86
- // that gets resolved to the `#[panic_handler]` function.
87
- extern "Rust" {
88
- #[ lang = "panic_impl" ]
89
- fn panic_impl ( pi : & PanicInfo < ' _ > ) -> !;
87
+ // NOTE This function never crosses the FFI boundary; it's a Rust-to-Rust call
88
+ // that gets resolved to the `#[panic_handler]` function.
89
+ extern "Rust" {
90
+ #[ lang = "panic_impl" ]
91
+ fn panic_impl ( pi : & PanicInfo < ' _ > ) -> !;
92
+ }
93
+
94
+ // PanicInfo with the `can_unwind` flag set to false forces an abort.
95
+ let pi = PanicInfo :: internal_constructor ( Some ( & fmt) , Location :: caller ( ) , false ) ;
96
+
97
+ // SAFETY: `panic_impl` is defined in safe Rust code and thus is safe to call.
98
+ unsafe { panic_impl ( & pi) }
90
99
}
91
100
92
- // PanicInfo with the `can_unwind` flag set to false forces an abort.
93
- let pi = PanicInfo :: internal_constructor ( Some ( & fmt) , Location :: caller ( ) , false ) ;
101
+ #[ inline]
102
+ const fn comptime ( fmt : fmt:: Arguments < ' _ > ) -> ! {
103
+ panic_fmt ( fmt) ;
104
+ }
94
105
95
- // SAFETY: `panic_impl` is defined in safe Rust code and thus is safe to call.
96
- unsafe { panic_impl ( & pi) }
106
+ // SAFETY: const panic does not care about unwinding
107
+ unsafe {
108
+ super :: intrinsics:: const_eval_select ( ( fmt, ) , comptime, runtime) ;
109
+ }
97
110
}
98
111
99
112
// Next we define a bunch of higher-level wrappers that all bottom out in the two core functions
@@ -122,7 +135,8 @@ pub const fn panic(expr: &'static str) -> ! {
122
135
#[ cfg_attr( feature = "panic_immediate_abort" , inline) ]
123
136
#[ lang = "panic_nounwind" ] // needed by codegen for non-unwinding panics
124
137
#[ rustc_nounwind]
125
- pub fn panic_nounwind ( expr : & ' static str ) -> ! {
138
+ #[ rustc_const_unstable( feature = "core_panic" , issue = "none" ) ]
139
+ pub const fn panic_nounwind ( expr : & ' static str ) -> ! {
126
140
panic_nounwind_fmt ( fmt:: Arguments :: new_const ( & [ expr] ) ) ;
127
141
}
128
142
0 commit comments