Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix const core::panic!(non_literal_str). #78069

Merged
merged 2 commits into from
Oct 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions compiler/rustc_hir/src/lang_items.rs
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
8 changes: 8 additions & 0 deletions src/test/ui/consts/const-eval/const_panic.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#![feature(const_panic)]
#![crate_type = "lib"]

const MSG: &str = "hello";

const Z: () = std::panic!("cheese");
//~^ ERROR any use of this value will cause an error

Expand All @@ -12,6 +14,9 @@ const Y: () = std::unreachable!();

const X: () = std::unimplemented!();
//~^ ERROR any use of this value will cause an error
//
const W: () = std::panic!(MSG);
//~^ ERROR any use of this value will cause an error

const Z_CORE: () = core::panic!("cheese");
//~^ ERROR any use of this value will cause an error
Expand All @@ -24,3 +29,6 @@ const Y_CORE: () = core::unreachable!();

const X_CORE: () = core::unimplemented!();
//~^ ERROR any use of this value will cause an error

const W_CORE: () = core::panic!(MSG);
//~^ ERROR any use of this value will cause an error
54 changes: 37 additions & 17 deletions src/test/ui/consts/const-eval/const_panic.stderr
Original file line number Diff line number Diff line change
@@ -1,83 +1,103 @@
error: any use of this value will cause an error
--> $DIR/const_panic.rs:4:15
--> $DIR/const_panic.rs:6:15
|
LL | const Z: () = std::panic!("cheese");
| --------------^^^^^^^^^^^^^^^^^^^^^-
| |
| the evaluated program panicked at 'cheese', $DIR/const_panic.rs:4:15
| the evaluated program panicked at 'cheese', $DIR/const_panic.rs:6:15
|
= note: `#[deny(const_err)]` on by default
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)

error: any use of this value will cause an error
--> $DIR/const_panic.rs:7:16
--> $DIR/const_panic.rs:9:16
|
LL | const Z2: () = std::panic!();
| ---------------^^^^^^^^^^^^^-
| |
| the evaluated program panicked at 'explicit panic', $DIR/const_panic.rs:7:16
| the evaluated program panicked at 'explicit panic', $DIR/const_panic.rs:9:16
|
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)

error: any use of this value will cause an error
--> $DIR/const_panic.rs:10:15
--> $DIR/const_panic.rs:12:15
|
LL | const Y: () = std::unreachable!();
| --------------^^^^^^^^^^^^^^^^^^^-
| |
| the evaluated program panicked at 'internal error: entered unreachable code', $DIR/const_panic.rs:10:15
| the evaluated program panicked at 'internal error: entered unreachable code', $DIR/const_panic.rs:12:15
|
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)

error: any use of this value will cause an error
--> $DIR/const_panic.rs:13:15
--> $DIR/const_panic.rs:15:15
|
LL | const X: () = std::unimplemented!();
| --------------^^^^^^^^^^^^^^^^^^^^^-
| |
| the evaluated program panicked at 'not implemented', $DIR/const_panic.rs:13:15
| the evaluated program panicked at 'not implemented', $DIR/const_panic.rs:15:15
|
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)

error: any use of this value will cause an error
--> $DIR/const_panic.rs:16:20
--> $DIR/const_panic.rs:18:15
|
LL | const W: () = std::panic!(MSG);
| --------------^^^^^^^^^^^^^^^^-
| |
| the evaluated program panicked at 'hello', $DIR/const_panic.rs:18:15
|
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)

error: any use of this value will cause an error
--> $DIR/const_panic.rs:21:20
|
LL | const Z_CORE: () = core::panic!("cheese");
| -------------------^^^^^^^^^^^^^^^^^^^^^^-
| |
| the evaluated program panicked at 'cheese', $DIR/const_panic.rs:16:20
| the evaluated program panicked at 'cheese', $DIR/const_panic.rs:21:20
|
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)

error: any use of this value will cause an error
--> $DIR/const_panic.rs:19:21
--> $DIR/const_panic.rs:24:21
|
LL | const Z2_CORE: () = core::panic!();
| --------------------^^^^^^^^^^^^^^-
| |
| the evaluated program panicked at 'explicit panic', $DIR/const_panic.rs:19:21
| the evaluated program panicked at 'explicit panic', $DIR/const_panic.rs:24:21
|
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)

error: any use of this value will cause an error
--> $DIR/const_panic.rs:22:20
--> $DIR/const_panic.rs:27:20
|
LL | const Y_CORE: () = core::unreachable!();
| -------------------^^^^^^^^^^^^^^^^^^^^-
| |
| the evaluated program panicked at 'internal error: entered unreachable code', $DIR/const_panic.rs:22:20
| the evaluated program panicked at 'internal error: entered unreachable code', $DIR/const_panic.rs:27:20
|
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)

error: any use of this value will cause an error
--> $DIR/const_panic.rs:25:20
--> $DIR/const_panic.rs:30:20
|
LL | const X_CORE: () = core::unimplemented!();
| -------------------^^^^^^^^^^^^^^^^^^^^^^-
| |
| the evaluated program panicked at 'not implemented', $DIR/const_panic.rs:25:20
| the evaluated program panicked at 'not implemented', $DIR/const_panic.rs:30:20
|
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)

error: any use of this value will cause an error
--> $DIR/const_panic.rs:33:20
|
LL | const W_CORE: () = core::panic!(MSG);
| -------------------^^^^^^^^^^^^^^^^^-
| |
| the evaluated program panicked at 'hello', $DIR/const_panic.rs:33:20
|
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)

error: aborting due to 8 previous errors
error: aborting due to 10 previous errors