Skip to content

Commit bf39266

Browse files
committed
Add const_panic_extra feature gate for allowing panicking with format
1 parent f220866 commit bf39266

File tree

3 files changed

+6
-6
lines changed

3 files changed

+6
-6
lines changed

library/core/src/fmt/mod.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,8 @@ macro_rules! arg_new {
312312
($f: ident, $t: ident) => {
313313
#[doc(hidden)]
314314
#[unstable(feature = "fmt_internals", reason = "internal to format_args!", issue = "none")]
315-
pub fn $f<'b, T: $t>(x: &'b T) -> ArgumentV1<'_> {
315+
#[rustc_const_unstable(feature = "const_panic_extra", issue = "none")]
316+
pub const fn $f<'b, T: $t>(x: &'b T) -> ArgumentV1<'_> {
316317
Self::new(x, $t::fmt)
317318
}
318319
};
@@ -321,7 +322,8 @@ macro_rules! arg_new {
321322
impl<'a> ArgumentV1<'a> {
322323
#[doc(hidden)]
323324
#[unstable(feature = "fmt_internals", reason = "internal to format_args!", issue = "none")]
324-
pub fn new<'b, T>(x: &'b T, f: fn(&T, &mut Formatter<'_>) -> Result) -> ArgumentV1<'b> {
325+
#[rustc_const_unstable(feature = "const_panic_extra", issue = "none")]
326+
pub const fn new<'b, T>(x: &'b T, f: fn(&T, &mut Formatter<'_>) -> Result) -> ArgumentV1<'b> {
325327
// SAFETY: `mem::transmute(x)` is safe because
326328
// 1. `&'b T` keeps the lifetime it originated with `'b`
327329
// (so as to not have an unbounded lifetime)

library/core/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@
120120
#![feature(const_num_from_num)]
121121
#![feature(const_ops)]
122122
#![feature(const_option)]
123+
#![feature(const_panic_extra)]
123124
#![feature(const_pin)]
124125
#![feature(const_replace)]
125126
#![feature(const_ptr_offset)]

library/core/src/panicking.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,10 +112,7 @@ pub const fn const_panic_fmt(fmt: fmt::Arguments<'_>) -> ! {
112112
if let Some(msg) = fmt.as_str() {
113113
panic_str(msg);
114114
} else {
115-
// SAFETY: This is only evaluated at compile time, which reliably
116-
// handles this UB (in case this branch turns out to be reachable
117-
// somehow).
118-
unsafe { crate::hint::unreachable_unchecked() };
115+
panic_str("<failed to format Arguments>");
119116
}
120117
}
121118

0 commit comments

Comments
 (0)