Skip to content

Commit 6e0a8bf

Browse files
committed
Auto merge of #86998 - m-ou-se:const-panic-fmt-as-str, r=oli-obk
Make const panic!("..") work in Rust 2021. During const eval, this replaces calls to core::panicking::panic_fmt and std::panicking::being_panic_fmt with a call to a new const fn: core::panicking::const_panic_fmt. That function uses fmt::Arguments::as_str() to get the str and calls panic_str with that instead. panic!() invocations with formatting arguments are still not accepted, as the creation of such a fmt::Arguments cannot be done in constant functions right now. r? `@RalfJung`
2 parents 581b166 + 312bf8e commit 6e0a8bf

File tree

17 files changed

+203
-21
lines changed

17 files changed

+203
-21
lines changed

compiler/rustc_builtin_macros/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ pub fn register_builtin_macros(resolver: &mut dyn ResolverExpand) {
7272
file: source_util::expand_file,
7373
format_args_nl: format::expand_format_args_nl,
7474
format_args: format::expand_format_args,
75+
const_format_args: format::expand_format_args,
7576
global_asm: asm::expand_global_asm,
7677
include_bytes: source_util::expand_include_bytes,
7778
include_str: source_util::expand_include_str,

compiler/rustc_hir/src/lang_items.rs

+3
Original file line numberDiff line numberDiff line change
@@ -276,13 +276,16 @@ language_item_table! {
276276
// is required to define it somewhere. Additionally, there are restrictions on crates that use
277277
// a weak lang item, but do not have it defined.
278278
Panic, sym::panic, panic_fn, Target::Fn;
279+
PanicFmt, sym::panic_fmt, panic_fmt, Target::Fn;
279280
PanicStr, sym::panic_str, panic_str, Target::Fn;
281+
ConstPanicFmt, sym::const_panic_fmt, const_panic_fmt, Target::Fn;
280282
PanicBoundsCheck, sym::panic_bounds_check, panic_bounds_check_fn, Target::Fn;
281283
PanicInfo, sym::panic_info, panic_info, Target::Struct;
282284
PanicLocation, sym::panic_location, panic_location, Target::Struct;
283285
PanicImpl, sym::panic_impl, panic_impl, Target::Fn;
284286
/// libstd panic entry point. Necessary for const eval to be able to catch it
285287
BeginPanic, sym::begin_panic, begin_panic_fn, Target::Fn;
288+
BeginPanicFmt, sym::begin_panic_fmt, begin_panic_fmt, Target::Fn;
286289

287290
ExchangeMalloc, sym::exchange_malloc, exchange_malloc_fn, Target::Fn;
288291
BoxFree, sym::box_free, box_free_fn, Target::Fn;

compiler/rustc_mir/src/const_eval/machine.rs

+36-8
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ impl<'mir, 'tcx> InterpCx<'mir, 'tcx, CompileTimeInterpreter<'mir, 'tcx>> {
3030
&mut self,
3131
instance: ty::Instance<'tcx>,
3232
args: &[OpTy<'tcx>],
33-
) -> InterpResult<'tcx> {
33+
) -> InterpResult<'tcx, Option<ty::Instance<'tcx>>> {
34+
// The list of functions we handle here must be in sync with
35+
// `is_lang_panic_fn` in `transform/check_consts/mod.rs`.
3436
let def_id = instance.def_id();
3537
if Some(def_id) == self.tcx.lang_items().panic_fn()
3638
|| Some(def_id) == self.tcx.lang_items().panic_str()
@@ -43,10 +45,25 @@ impl<'mir, 'tcx> InterpCx<'mir, 'tcx, CompileTimeInterpreter<'mir, 'tcx>> {
4345
let msg = Symbol::intern(self.read_str(&msg_place)?);
4446
let span = self.find_closest_untracked_caller_location();
4547
let (file, line, col) = self.location_triple_for_span(span);
46-
Err(ConstEvalErrKind::Panic { msg, file, line, col }.into())
47-
} else {
48-
Ok(())
48+
return Err(ConstEvalErrKind::Panic { msg, file, line, col }.into());
49+
} else if Some(def_id) == self.tcx.lang_items().panic_fmt()
50+
|| Some(def_id) == self.tcx.lang_items().begin_panic_fmt()
51+
{
52+
// For panic_fmt, call const_panic_fmt instead.
53+
if let Some(const_panic_fmt) = self.tcx.lang_items().const_panic_fmt() {
54+
return Ok(Some(
55+
ty::Instance::resolve(
56+
*self.tcx,
57+
ty::ParamEnv::reveal_all(),
58+
const_panic_fmt,
59+
self.tcx.intern_substs(&[]),
60+
)
61+
.unwrap()
62+
.unwrap(),
63+
));
64+
}
4965
}
66+
Ok(None)
5067
}
5168
}
5269

@@ -241,10 +258,21 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for CompileTimeInterpreter<'mir,
241258
if !ecx.tcx.has_attr(def.did, sym::default_method_body_is_const) {
242259
// Some functions we support even if they are non-const -- but avoid testing
243260
// that for const fn!
244-
ecx.hook_panic_fn(instance, args)?;
245-
// We certainly do *not* want to actually call the fn
246-
// though, so be sure we return here.
247-
throw_unsup_format!("calling non-const function `{}`", instance)
261+
if let Some(new_instance) = ecx.hook_panic_fn(instance, args)? {
262+
// We call another const fn instead.
263+
return Self::find_mir_or_eval_fn(
264+
ecx,
265+
new_instance,
266+
_abi,
267+
args,
268+
_ret,
269+
_unwind,
270+
);
271+
} else {
272+
// We certainly do *not* want to actually call the fn
273+
// though, so be sure we return here.
274+
throw_unsup_format!("calling non-const function `{}`", instance)
275+
}
248276
}
249277
}
250278
}

compiler/rustc_mir/src/transform/check_consts/mod.rs

+5
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,14 @@ impl ConstCx<'mir, 'tcx> {
7474

7575
/// Returns `true` if this `DefId` points to one of the official `panic` lang items.
7676
pub fn is_lang_panic_fn(tcx: TyCtxt<'tcx>, def_id: DefId) -> bool {
77+
// We can allow calls to these functions because `hook_panic_fn` in
78+
// `const_eval/machine.rs` ensures the calls are handled specially.
79+
// Keep in sync with what that function handles!
7780
Some(def_id) == tcx.lang_items().panic_fn()
7881
|| Some(def_id) == tcx.lang_items().panic_str()
7982
|| Some(def_id) == tcx.lang_items().begin_panic_fn()
83+
|| Some(def_id) == tcx.lang_items().panic_fmt()
84+
|| Some(def_id) == tcx.lang_items().begin_panic_fmt()
8085
}
8186

8287
pub fn rustc_allow_const_fn_unstable(

compiler/rustc_span/src/symbol.rs

+5
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,7 @@ symbols! {
323323
await_macro,
324324
bang,
325325
begin_panic,
326+
begin_panic_fmt,
326327
bench,
327328
bin,
328329
bind_by_move_pattern_guards,
@@ -409,6 +410,7 @@ symbols! {
409410
const_fn_transmute,
410411
const_fn_union,
411412
const_fn_unsize,
413+
const_format_args,
412414
const_generic_defaults,
413415
const_generics,
414416
const_generics_defaults,
@@ -420,6 +422,7 @@ symbols! {
420422
const_loop,
421423
const_mut_refs,
422424
const_panic,
425+
const_panic_fmt,
423426
const_precise_live_drops,
424427
const_ptr,
425428
const_raw_ptr_deref,
@@ -586,6 +589,7 @@ symbols! {
586589
fmaf32,
587590
fmaf64,
588591
fmt,
592+
fmt_as_str,
589593
fmt_internals,
590594
fmul_fast,
591595
fn_align,
@@ -881,6 +885,7 @@ symbols! {
881885
panic_2021,
882886
panic_abort,
883887
panic_bounds_check,
888+
panic_fmt,
884889
panic_handler,
885890
panic_impl,
886891
panic_implementation,

library/core/src/fmt/mod.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,8 @@ impl<'a> Arguments<'a> {
337337
#[doc(hidden)]
338338
#[inline]
339339
#[unstable(feature = "fmt_internals", reason = "internal to format_args!", issue = "none")]
340-
pub fn new_v1(pieces: &'a [&'static str], args: &'a [ArgumentV1<'a>]) -> Arguments<'a> {
340+
#[rustc_const_unstable(feature = "const_fmt_arguments_new", issue = "none")]
341+
pub const fn new_v1(pieces: &'a [&'static str], args: &'a [ArgumentV1<'a>]) -> Arguments<'a> {
341342
Arguments { pieces, fmt: None, args }
342343
}
343344

@@ -350,7 +351,8 @@ impl<'a> Arguments<'a> {
350351
#[doc(hidden)]
351352
#[inline]
352353
#[unstable(feature = "fmt_internals", reason = "internal to format_args!", issue = "none")]
353-
pub fn new_v1_formatted(
354+
#[rustc_const_unstable(feature = "const_fmt_arguments_new", issue = "none")]
355+
pub const fn new_v1_formatted(
354356
pieces: &'a [&'static str],
355357
args: &'a [ArgumentV1<'a>],
356358
fmt: &'a [rt::v1::Argument],

library/core/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373
#![feature(cfg_target_has_atomic)]
7474
#![feature(const_heap)]
7575
#![feature(const_alloc_layout)]
76+
#![feature(const_arguments_as_str)]
7677
#![feature(const_assert_type)]
7778
#![feature(const_discriminant)]
7879
#![feature(const_cell_into_inner)]

library/core/src/macros/mod.rs

+25
Original file line numberDiff line numberDiff line change
@@ -837,6 +837,31 @@ pub(crate) mod builtin {
837837
($fmt:expr, $($args:tt)*) => {{ /* compiler built-in */ }};
838838
}
839839

840+
/// Same as `format_args`, but can be used in some const contexts.
841+
///
842+
/// This macro is used by the panic macros for the `const_panic` feature.
843+
///
844+
/// This macro will be removed once `format_args` is allowed in const contexts.
845+
#[cfg(not(bootstrap))]
846+
#[unstable(feature = "const_format_args", issue = "none")]
847+
#[allow_internal_unstable(fmt_internals, const_fmt_arguments_new)]
848+
#[rustc_builtin_macro]
849+
#[macro_export]
850+
macro_rules! const_format_args {
851+
($fmt:expr) => {{ /* compiler built-in */ }};
852+
($fmt:expr, $($args:tt)*) => {{ /* compiler built-in */ }};
853+
}
854+
855+
/// Same as `format_args`, but can be used in some const contexts.
856+
#[cfg(bootstrap)]
857+
#[unstable(feature = "const_format_args", issue = "none")]
858+
#[macro_export]
859+
macro_rules! const_format_args {
860+
($($t:tt)*) => {
861+
$crate::format_args!($($t)*)
862+
}
863+
}
864+
840865
/// Same as `format_args`, but adds a newline in the end.
841866
#[unstable(
842867
feature = "format_args_nl",

library/core/src/panic.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use crate::fmt;
77

88
#[doc(hidden)]
99
#[unstable(feature = "edition_panic", issue = "none", reason = "use panic!() instead")]
10-
#[allow_internal_unstable(core_panic)]
10+
#[allow_internal_unstable(core_panic, const_format_args)]
1111
#[rustc_diagnostic_item = "core_panic_2015_macro"]
1212
#[rustc_macro_transparency = "semitransparent"]
1313
pub macro panic_2015 {
@@ -21,21 +21,21 @@ pub macro panic_2015 {
2121
$crate::panicking::panic_str($msg)
2222
),
2323
($fmt:expr, $($arg:tt)+) => (
24-
$crate::panicking::panic_fmt($crate::format_args!($fmt, $($arg)+))
24+
$crate::panicking::panic_fmt($crate::const_format_args!($fmt, $($arg)+))
2525
),
2626
}
2727

2828
#[doc(hidden)]
2929
#[unstable(feature = "edition_panic", issue = "none", reason = "use panic!() instead")]
30-
#[allow_internal_unstable(core_panic)]
30+
#[allow_internal_unstable(core_panic, const_format_args)]
3131
#[rustc_diagnostic_item = "core_panic_2021_macro"]
3232
#[rustc_macro_transparency = "semitransparent"]
3333
pub macro panic_2021 {
3434
() => (
3535
$crate::panicking::panic("explicit panic")
3636
),
3737
($($t:tt)+) => (
38-
$crate::panicking::panic_fmt($crate::format_args!($($t)+))
38+
$crate::panicking::panic_fmt($crate::const_format_args!($($t)+))
3939
),
4040
}
4141

library/core/src/panicking.rs

+15
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ fn panic_bounds_check(index: usize, len: usize) -> ! {
7474
#[cfg_attr(not(feature = "panic_immediate_abort"), inline(never))]
7575
#[cfg_attr(feature = "panic_immediate_abort", inline)]
7676
#[track_caller]
77+
#[cfg_attr(not(bootstrap), lang = "panic_fmt")] // needed for const-evaluated panics
7778
pub fn panic_fmt(fmt: fmt::Arguments<'_>) -> ! {
7879
if cfg!(feature = "panic_immediate_abort") {
7980
super::intrinsics::abort()
@@ -92,6 +93,20 @@ pub fn panic_fmt(fmt: fmt::Arguments<'_>) -> ! {
9293
unsafe { panic_impl(&pi) }
9394
}
9495

96+
/// This function is used instead of panic_fmt in const eval.
97+
#[cfg(not(bootstrap))]
98+
#[lang = "const_panic_fmt"]
99+
pub const fn const_panic_fmt(fmt: fmt::Arguments<'_>) -> ! {
100+
if let Some(msg) = fmt.as_str() {
101+
panic_str(msg);
102+
} else {
103+
// SAFETY: This is only evaluated at compile time, which reliably
104+
// handles this UB (in case this branch turns out to be reachable
105+
// somehow).
106+
unsafe { crate::hint::unreachable_unchecked() };
107+
}
108+
}
109+
95110
#[derive(Debug)]
96111
#[doc(hidden)]
97112
pub enum AssertKind {

library/std/src/lib.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,7 @@
247247
#![feature(const_fn_floating_point_arithmetic)]
248248
#![feature(const_fn_fn_ptr_basics)]
249249
#![cfg_attr(bootstrap, feature(const_fn_transmute))]
250+
#![feature(const_format_args)]
250251
#![feature(const_io_structs)]
251252
#![feature(const_ip)]
252253
#![feature(const_ipv4)]
@@ -555,9 +556,9 @@ pub use core::{
555556
#[stable(feature = "builtin_macro_prelude", since = "1.38.0")]
556557
#[allow(deprecated)]
557558
pub use core::{
558-
assert, assert_matches, cfg, column, compile_error, concat, concat_idents, env, file,
559-
format_args, format_args_nl, include, include_bytes, include_str, line, llvm_asm, log_syntax,
560-
module_path, option_env, stringify, trace_macros,
559+
assert, assert_matches, cfg, column, compile_error, concat, concat_idents, const_format_args,
560+
env, file, format_args, format_args_nl, include, include_bytes, include_str, line, llvm_asm,
561+
log_syntax, module_path, option_env, stringify, trace_macros,
561562
};
562563

563564
#[stable(feature = "core_primitive", since = "1.43.0")]

library/std/src/panic.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use crate::thread::Result;
2020

2121
#[doc(hidden)]
2222
#[unstable(feature = "edition_panic", issue = "none", reason = "use panic!() instead")]
23-
#[allow_internal_unstable(libstd_sys_internals)]
23+
#[allow_internal_unstable(libstd_sys_internals, const_format_args)]
2424
#[cfg_attr(not(test), rustc_diagnostic_item = "std_panic_2015_macro")]
2525
#[rustc_macro_transparency = "semitransparent"]
2626
pub macro panic_2015 {
@@ -31,7 +31,7 @@ pub macro panic_2015 {
3131
$crate::rt::begin_panic($msg)
3232
}),
3333
($fmt:expr, $($arg:tt)+) => ({
34-
$crate::rt::begin_panic_fmt(&$crate::format_args!($fmt, $($arg)+))
34+
$crate::rt::begin_panic_fmt(&$crate::const_format_args!($fmt, $($arg)+))
3535
}),
3636
}
3737

library/std/src/panicking.rs

+1
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,7 @@ pub fn panicking() -> bool {
448448
#[cfg_attr(not(feature = "panic_immediate_abort"), track_caller)]
449449
#[cfg_attr(not(feature = "panic_immediate_abort"), inline(never))]
450450
#[cfg_attr(feature = "panic_immediate_abort", inline)]
451+
#[cfg_attr(all(not(bootstrap), not(test)), lang = "begin_panic_fmt")]
451452
pub fn begin_panic_fmt(msg: &fmt::Arguments<'_>) -> ! {
452453
if cfg!(feature = "panic_immediate_abort") {
453454
intrinsics::abort()

src/test/ui/borrowck/issue-64453.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ struct Value;
33

44
static settings_dir: String = format!("");
55
//~^ ERROR calls in statics are limited to constant functions
6-
//~| ERROR calls in statics are limited to constant functions
6+
//~| ERROR is not yet stable as a const
77

88
fn from_string(_: String) -> Value {
99
Value

src/test/ui/borrowck/issue-64453.stderr

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@ error[E0507]: cannot move out of static item `settings_dir`
44
LL | let settings_data = from_string(settings_dir);
55
| ^^^^^^^^^^^^ move occurs because `settings_dir` has type `String`, which does not implement the `Copy` trait
66

7-
error[E0015]: calls in statics are limited to constant functions, tuple structs and tuple variants
7+
error: `Arguments::<'a>::new_v1` is not yet stable as a const fn
88
--> $DIR/issue-64453.rs:4:31
99
|
1010
LL | static settings_dir: String = format!("");
1111
| ^^^^^^^^^^^
1212
|
13+
= help: add `#![feature(const_fmt_arguments_new)]` to the crate attributes to enable
1314
= note: this error originates in the macro `$crate::__export::format_args` (in Nightly builds, run with -Z macro-backtrace for more info)
1415

1516
error[E0015]: calls in statics are limited to constant functions, tuple structs and tuple variants
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// edition:2021
2+
#![feature(const_panic)]
3+
#![crate_type = "lib"]
4+
5+
const A: () = std::panic!("blåhaj");
6+
//~^ ERROR evaluation of constant value failed
7+
8+
const B: () = std::panic!();
9+
//~^ ERROR evaluation of constant value failed
10+
11+
const C: () = std::unreachable!();
12+
//~^ ERROR evaluation of constant value failed
13+
14+
const D: () = std::unimplemented!();
15+
//~^ ERROR evaluation of constant value failed
16+
17+
const E: () = core::panic!("shark");
18+
//~^ ERROR evaluation of constant value failed
19+
20+
const F: () = core::panic!();
21+
//~^ ERROR evaluation of constant value failed
22+
23+
const G: () = core::unreachable!();
24+
//~^ ERROR evaluation of constant value failed
25+
26+
const H: () = core::unimplemented!();
27+
//~^ ERROR evaluation of constant value failed

0 commit comments

Comments
 (0)