Skip to content

Commit

Permalink
Fix const core::panic!(non_literal_str).
Browse files Browse the repository at this point in the history
  • Loading branch information
m-ou-se committed Oct 22, 2020
1 parent 500ddc5 commit 4f7ffbf
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 3 deletions.
1 change: 1 addition & 0 deletions compiler/rustc_hir/src/lang_items.rs
Expand Up @@ -263,6 +263,7 @@ language_item_table! {
// is required to define it somewhere. Additionally, there are restrictions on crates that use
// a weak lang item, but do not have it defined.
Panic, sym::panic, panic_fn, Target::Fn;
PanicStr, sym::panic_str, panic_str, Target::Fn;
PanicBoundsCheck, sym::panic_bounds_check, panic_bounds_check_fn, Target::Fn;
PanicInfo, sym::panic_info, panic_info, Target::Struct;
PanicLocation, sym::panic_location, panic_location, Target::Struct;
Expand Down
3 changes: 2 additions & 1 deletion compiler/rustc_mir/src/const_eval/machine.rs
Expand Up @@ -70,9 +70,10 @@ impl<'mir, 'tcx> InterpCx<'mir, 'tcx, CompileTimeInterpreter<'mir, 'tcx>> {
) -> InterpResult<'tcx> {
let def_id = instance.def_id();
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().begin_panic_fn()
{
// &'static str
// &str
assert!(args.len() == 1);

let msg_place = self.deref_operand(args[0])?;
Expand Down
4 changes: 3 additions & 1 deletion compiler/rustc_mir/src/transform/check_consts/mod.rs
Expand Up @@ -74,7 +74,9 @@ impl ConstCx<'mir, 'tcx> {

/// Returns `true` if this `DefId` points to one of the official `panic` lang items.
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().begin_panic_fn()
Some(def_id) == tcx.lang_items().panic_fn()
|| Some(def_id) == tcx.lang_items().panic_str()
|| Some(def_id) == tcx.lang_items().begin_panic_fn()
}

pub fn allow_internal_unstable(tcx: TyCtxt<'tcx>, def_id: DefId, feature_gate: Symbol) -> bool {
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_span/src/symbol.rs
Expand Up @@ -777,6 +777,7 @@ symbols! {
panic_info,
panic_location,
panic_runtime,
panic_str,
panic_unwind,
param_attrs,
parent_trait,
Expand Down
2 changes: 1 addition & 1 deletion library/core/src/macros/mod.rs
Expand Up @@ -10,7 +10,7 @@ macro_rules! panic {
$crate::panicking::panic($msg)
);
($msg:expr) => (
$crate::panic!("{}", $crate::convert::identity::<&str>($msg))
$crate::panicking::panic_str($msg)
);
($msg:expr,) => (
$crate::panic!($msg)
Expand Down
7 changes: 7 additions & 0 deletions library/core/src/panicking.rs
Expand Up @@ -50,6 +50,13 @@ pub fn panic(expr: &'static str) -> ! {
panic_fmt(fmt::Arguments::new_v1(&[expr], &[]));
}

#[inline]
#[track_caller]
#[cfg_attr(not(bootstrap), lang = "panic_str")] // needed for const-evaluated panics
pub fn panic_str(expr: &str) -> ! {
panic_fmt(format_args!("{}", expr));
}

#[cold]
#[cfg_attr(not(feature = "panic_immediate_abort"), inline(never))]
#[track_caller]
Expand Down

0 comments on commit 4f7ffbf

Please sign in to comment.