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.
This commit is contained in:
parent
eba3228b2a
commit
f827d3e285
8 changed files with 53 additions and 11 deletions
|
@ -276,13 +276,16 @@ language_item_table! {
|
||||||
// is required to define it somewhere. Additionally, there are restrictions on crates that use
|
// is required to define it somewhere. Additionally, there are restrictions on crates that use
|
||||||
// a weak lang item, but do not have it defined.
|
// a weak lang item, but do not have it defined.
|
||||||
Panic, sym::panic, panic_fn, Target::Fn;
|
Panic, sym::panic, panic_fn, Target::Fn;
|
||||||
|
PanicFmt, sym::panic_fmt, panic_fmt, Target::Fn;
|
||||||
PanicStr, sym::panic_str, panic_str, Target::Fn;
|
PanicStr, sym::panic_str, panic_str, Target::Fn;
|
||||||
|
ConstPanicFmt, sym::const_panic_fmt, const_panic_fmt, Target::Fn;
|
||||||
PanicBoundsCheck, sym::panic_bounds_check, panic_bounds_check_fn, Target::Fn;
|
PanicBoundsCheck, sym::panic_bounds_check, panic_bounds_check_fn, Target::Fn;
|
||||||
PanicInfo, sym::panic_info, panic_info, Target::Struct;
|
PanicInfo, sym::panic_info, panic_info, Target::Struct;
|
||||||
PanicLocation, sym::panic_location, panic_location, Target::Struct;
|
PanicLocation, sym::panic_location, panic_location, Target::Struct;
|
||||||
PanicImpl, sym::panic_impl, panic_impl, Target::Fn;
|
PanicImpl, sym::panic_impl, panic_impl, Target::Fn;
|
||||||
/// libstd panic entry point. Necessary for const eval to be able to catch it
|
/// libstd panic entry point. Necessary for const eval to be able to catch it
|
||||||
BeginPanic, sym::begin_panic, begin_panic_fn, Target::Fn;
|
BeginPanic, sym::begin_panic, begin_panic_fn, Target::Fn;
|
||||||
|
BeginPanicFmt, sym::begin_panic_fmt, begin_panic_fmt, Target::Fn;
|
||||||
|
|
||||||
ExchangeMalloc, sym::exchange_malloc, exchange_malloc_fn, Target::Fn;
|
ExchangeMalloc, sym::exchange_malloc, exchange_malloc_fn, Target::Fn;
|
||||||
BoxFree, sym::box_free, box_free_fn, Target::Fn;
|
BoxFree, sym::box_free, box_free_fn, Target::Fn;
|
||||||
|
|
|
@ -30,7 +30,7 @@ impl<'mir, 'tcx> InterpCx<'mir, 'tcx, CompileTimeInterpreter<'mir, 'tcx>> {
|
||||||
&mut self,
|
&mut self,
|
||||||
instance: ty::Instance<'tcx>,
|
instance: ty::Instance<'tcx>,
|
||||||
args: &[OpTy<'tcx>],
|
args: &[OpTy<'tcx>],
|
||||||
) -> InterpResult<'tcx> {
|
) -> InterpResult<'tcx, Option<ty::Instance<'tcx>>> {
|
||||||
let def_id = instance.def_id();
|
let def_id = instance.def_id();
|
||||||
if Some(def_id) == self.tcx.lang_items().panic_fn()
|
if Some(def_id) == self.tcx.lang_items().panic_fn()
|
||||||
|| Some(def_id) == self.tcx.lang_items().panic_str()
|
|| Some(def_id) == self.tcx.lang_items().panic_str()
|
||||||
|
@ -43,11 +43,26 @@ impl<'mir, 'tcx> InterpCx<'mir, 'tcx, CompileTimeInterpreter<'mir, 'tcx>> {
|
||||||
let msg = Symbol::intern(self.read_str(&msg_place)?);
|
let msg = Symbol::intern(self.read_str(&msg_place)?);
|
||||||
let span = self.find_closest_untracked_caller_location();
|
let span = self.find_closest_untracked_caller_location();
|
||||||
let (file, line, col) = self.location_triple_for_span(span);
|
let (file, line, col) = self.location_triple_for_span(span);
|
||||||
Err(ConstEvalErrKind::Panic { msg, file, line, col }.into())
|
return Err(ConstEvalErrKind::Panic { msg, file, line, col }.into());
|
||||||
} else {
|
} else if Some(def_id) == self.tcx.lang_items().panic_fmt()
|
||||||
Ok(())
|
|| Some(def_id) == self.tcx.lang_items().begin_panic_fmt()
|
||||||
|
{
|
||||||
|
// For panic_fmt, call const_panic_fmt instead.
|
||||||
|
if let Some(const_panic_fmt) = self.tcx.lang_items().const_panic_fmt() {
|
||||||
|
return Ok(Some(
|
||||||
|
ty::Instance::resolve(
|
||||||
|
*self.tcx,
|
||||||
|
ty::ParamEnv::reveal_all(),
|
||||||
|
const_panic_fmt,
|
||||||
|
self.tcx.intern_substs(&[]),
|
||||||
|
)
|
||||||
|
.unwrap()
|
||||||
|
.unwrap(),
|
||||||
|
));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Ok(None)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Extra machine state for CTFE, and the Machine instance
|
/// Extra machine state for CTFE, and the Machine instance
|
||||||
|
@ -223,7 +238,7 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for CompileTimeInterpreter<'mir,
|
||||||
|
|
||||||
fn find_mir_or_eval_fn(
|
fn find_mir_or_eval_fn(
|
||||||
ecx: &mut InterpCx<'mir, 'tcx, Self>,
|
ecx: &mut InterpCx<'mir, 'tcx, Self>,
|
||||||
instance: ty::Instance<'tcx>,
|
mut instance: ty::Instance<'tcx>,
|
||||||
_abi: Abi,
|
_abi: Abi,
|
||||||
args: &[OpTy<'tcx>],
|
args: &[OpTy<'tcx>],
|
||||||
_ret: Option<(&PlaceTy<'tcx>, mir::BasicBlock)>,
|
_ret: Option<(&PlaceTy<'tcx>, mir::BasicBlock)>,
|
||||||
|
@ -241,13 +256,17 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for CompileTimeInterpreter<'mir,
|
||||||
if !ecx.tcx.has_attr(def.did, sym::default_method_body_is_const) {
|
if !ecx.tcx.has_attr(def.did, sym::default_method_body_is_const) {
|
||||||
// Some functions we support even if they are non-const -- but avoid testing
|
// Some functions we support even if they are non-const -- but avoid testing
|
||||||
// that for const fn!
|
// that for const fn!
|
||||||
ecx.hook_panic_fn(instance, args)?;
|
if let Some(new_instance) = ecx.hook_panic_fn(instance, args)? {
|
||||||
|
// We call another const fn instead.
|
||||||
|
instance = new_instance;
|
||||||
|
} else {
|
||||||
// We certainly do *not* want to actually call the fn
|
// We certainly do *not* want to actually call the fn
|
||||||
// though, so be sure we return here.
|
// though, so be sure we return here.
|
||||||
throw_unsup_format!("calling non-const function `{}`", instance)
|
throw_unsup_format!("calling non-const function `{}`", instance)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
// This is a const fn. Call it.
|
// This is a const fn. Call it.
|
||||||
Ok(Some(ecx.load_mir(instance.def, None)?))
|
Ok(Some(ecx.load_mir(instance.def, None)?))
|
||||||
}
|
}
|
||||||
|
|
|
@ -77,6 +77,8 @@ pub fn is_lang_panic_fn(tcx: TyCtxt<'tcx>, def_id: DefId) -> bool {
|
||||||
Some(def_id) == tcx.lang_items().panic_fn()
|
Some(def_id) == tcx.lang_items().panic_fn()
|
||||||
|| Some(def_id) == tcx.lang_items().panic_str()
|
|| Some(def_id) == tcx.lang_items().panic_str()
|
||||||
|| Some(def_id) == tcx.lang_items().begin_panic_fn()
|
|| Some(def_id) == tcx.lang_items().begin_panic_fn()
|
||||||
|
|| Some(def_id) == tcx.lang_items().panic_fmt()
|
||||||
|
|| Some(def_id) == tcx.lang_items().begin_panic_fmt()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn rustc_allow_const_fn_unstable(
|
pub fn rustc_allow_const_fn_unstable(
|
||||||
|
|
|
@ -323,6 +323,7 @@ symbols! {
|
||||||
await_macro,
|
await_macro,
|
||||||
bang,
|
bang,
|
||||||
begin_panic,
|
begin_panic,
|
||||||
|
begin_panic_fmt,
|
||||||
bench,
|
bench,
|
||||||
bin,
|
bin,
|
||||||
bind_by_move_pattern_guards,
|
bind_by_move_pattern_guards,
|
||||||
|
@ -420,6 +421,7 @@ symbols! {
|
||||||
const_loop,
|
const_loop,
|
||||||
const_mut_refs,
|
const_mut_refs,
|
||||||
const_panic,
|
const_panic,
|
||||||
|
const_panic_fmt,
|
||||||
const_precise_live_drops,
|
const_precise_live_drops,
|
||||||
const_ptr,
|
const_ptr,
|
||||||
const_raw_ptr_deref,
|
const_raw_ptr_deref,
|
||||||
|
@ -586,6 +588,7 @@ symbols! {
|
||||||
fmaf32,
|
fmaf32,
|
||||||
fmaf64,
|
fmaf64,
|
||||||
fmt,
|
fmt,
|
||||||
|
fmt_as_str,
|
||||||
fmt_internals,
|
fmt_internals,
|
||||||
fmul_fast,
|
fmul_fast,
|
||||||
fn_align,
|
fn_align,
|
||||||
|
@ -881,6 +884,7 @@ symbols! {
|
||||||
panic_2021,
|
panic_2021,
|
||||||
panic_abort,
|
panic_abort,
|
||||||
panic_bounds_check,
|
panic_bounds_check,
|
||||||
|
panic_fmt,
|
||||||
panic_handler,
|
panic_handler,
|
||||||
panic_impl,
|
panic_impl,
|
||||||
panic_implementation,
|
panic_implementation,
|
||||||
|
|
|
@ -337,7 +337,7 @@ impl<'a> Arguments<'a> {
|
||||||
#[doc(hidden)]
|
#[doc(hidden)]
|
||||||
#[inline]
|
#[inline]
|
||||||
#[unstable(feature = "fmt_internals", reason = "internal to format_args!", issue = "none")]
|
#[unstable(feature = "fmt_internals", reason = "internal to format_args!", issue = "none")]
|
||||||
pub fn new_v1(pieces: &'a [&'static str], args: &'a [ArgumentV1<'a>]) -> Arguments<'a> {
|
pub const fn new_v1(pieces: &'a [&'static str], args: &'a [ArgumentV1<'a>]) -> Arguments<'a> {
|
||||||
Arguments { pieces, fmt: None, args }
|
Arguments { pieces, fmt: None, args }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -350,7 +350,7 @@ impl<'a> Arguments<'a> {
|
||||||
#[doc(hidden)]
|
#[doc(hidden)]
|
||||||
#[inline]
|
#[inline]
|
||||||
#[unstable(feature = "fmt_internals", reason = "internal to format_args!", issue = "none")]
|
#[unstable(feature = "fmt_internals", reason = "internal to format_args!", issue = "none")]
|
||||||
pub fn new_v1_formatted(
|
pub const fn new_v1_formatted(
|
||||||
pieces: &'a [&'static str],
|
pieces: &'a [&'static str],
|
||||||
args: &'a [ArgumentV1<'a>],
|
args: &'a [ArgumentV1<'a>],
|
||||||
fmt: &'a [rt::v1::Argument],
|
fmt: &'a [rt::v1::Argument],
|
||||||
|
|
|
@ -73,6 +73,7 @@
|
||||||
#![feature(cfg_target_has_atomic)]
|
#![feature(cfg_target_has_atomic)]
|
||||||
#![feature(const_heap)]
|
#![feature(const_heap)]
|
||||||
#![feature(const_alloc_layout)]
|
#![feature(const_alloc_layout)]
|
||||||
|
#![feature(const_arguments_as_str)]
|
||||||
#![feature(const_assert_type)]
|
#![feature(const_assert_type)]
|
||||||
#![feature(const_discriminant)]
|
#![feature(const_discriminant)]
|
||||||
#![feature(const_cell_into_inner)]
|
#![feature(const_cell_into_inner)]
|
||||||
|
|
|
@ -74,6 +74,7 @@ fn panic_bounds_check(index: usize, len: usize) -> ! {
|
||||||
#[cfg_attr(not(feature = "panic_immediate_abort"), inline(never))]
|
#[cfg_attr(not(feature = "panic_immediate_abort"), inline(never))]
|
||||||
#[cfg_attr(feature = "panic_immediate_abort", inline)]
|
#[cfg_attr(feature = "panic_immediate_abort", inline)]
|
||||||
#[track_caller]
|
#[track_caller]
|
||||||
|
#[cfg_attr(not(bootstrap), lang = "panic_fmt")] // needed for const-evaluated panics
|
||||||
pub fn panic_fmt(fmt: fmt::Arguments<'_>) -> ! {
|
pub fn panic_fmt(fmt: fmt::Arguments<'_>) -> ! {
|
||||||
if cfg!(feature = "panic_immediate_abort") {
|
if cfg!(feature = "panic_immediate_abort") {
|
||||||
super::intrinsics::abort()
|
super::intrinsics::abort()
|
||||||
|
@ -92,6 +93,17 @@ pub fn panic_fmt(fmt: fmt::Arguments<'_>) -> ! {
|
||||||
unsafe { panic_impl(&pi) }
|
unsafe { panic_impl(&pi) }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// This function is used instead of panic_fmt in const eval.
|
||||||
|
#[cfg(not(bootstrap))]
|
||||||
|
#[lang = "const_panic_fmt"]
|
||||||
|
pub const fn const_panic_fmt(fmt: fmt::Arguments<'_>) -> ! {
|
||||||
|
if let Some(msg) = fmt.as_str() {
|
||||||
|
panic_str(msg);
|
||||||
|
} else {
|
||||||
|
panic_str("???");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
#[doc(hidden)]
|
#[doc(hidden)]
|
||||||
pub enum AssertKind {
|
pub enum AssertKind {
|
||||||
|
|
|
@ -448,6 +448,7 @@ pub fn panicking() -> bool {
|
||||||
#[cfg_attr(not(feature = "panic_immediate_abort"), track_caller)]
|
#[cfg_attr(not(feature = "panic_immediate_abort"), track_caller)]
|
||||||
#[cfg_attr(not(feature = "panic_immediate_abort"), inline(never))]
|
#[cfg_attr(not(feature = "panic_immediate_abort"), inline(never))]
|
||||||
#[cfg_attr(feature = "panic_immediate_abort", inline)]
|
#[cfg_attr(feature = "panic_immediate_abort", inline)]
|
||||||
|
#[cfg_attr(all(not(bootstrap), not(test)), lang = "begin_panic_fmt")]
|
||||||
pub fn begin_panic_fmt(msg: &fmt::Arguments<'_>) -> ! {
|
pub fn begin_panic_fmt(msg: &fmt::Arguments<'_>) -> ! {
|
||||||
if cfg!(feature = "panic_immediate_abort") {
|
if cfg!(feature = "panic_immediate_abort") {
|
||||||
intrinsics::abort()
|
intrinsics::abort()
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue