Skip to content

Commit 9ef2ef0

Browse files
committed
Auto merge of #1241 - RalfJung:dont-panic, r=RalfJung
whitelist platforms where panicking should work @CAD97 [proposed](#1059 (comment)) trying to get a better error for failed panics on Windows. Could you test if this works for you?
2 parents ee71b2e + 7a10c9d commit 9ef2ef0

File tree

8 files changed

+48
-0
lines changed

8 files changed

+48
-0
lines changed

src/shims/foreign_items.rs

+1
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
132132
// This matches calls to the foreign item `panic_impl`.
133133
// The implementation is provided by the function with the `#[panic_handler]` attribute.
134134
"panic_impl" => {
135+
this.check_panic_supported()?;
135136
let panic_impl_id = this.tcx.lang_items().panic_impl().unwrap();
136137
let panic_impl_instance = ty::Instance::mono(*this.tcx, panic_impl_id);
137138
return Ok(Some(&*this.load_mir(panic_impl_instance.def, None)?));

src/shims/mod.rs

+8
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,14 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
4242
return this.emulate_foreign_item(instance.def_id(), args, ret, unwind);
4343
}
4444

45+
// Better error message for panics on Windows.
46+
let def_id = instance.def_id();
47+
if Some(def_id) == this.tcx.lang_items().begin_panic_fn() ||
48+
Some(def_id) == this.tcx.lang_items().panic_impl()
49+
{
50+
this.check_panic_supported()?;
51+
}
52+
4553
// Otherwise, load the MIR.
4654
Ok(Some(&*this.load_mir(instance.def, None)?))
4755
}

src/shims/panic.rs

+8
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,14 @@ pub struct CatchUnwindData<'tcx> {
3232

3333
impl<'mir, 'tcx> EvalContextExt<'mir, 'tcx> for crate::MiriEvalContext<'mir, 'tcx> {}
3434
pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx> {
35+
/// Check if panicking is supported on this platform, and give a good error otherwise.
36+
fn check_panic_supported(&self) -> InterpResult<'tcx> {
37+
match self.eval_context_ref().tcx.sess.target.target.target_os.as_str() {
38+
"linux" | "macos" => Ok(()),
39+
_ => throw_unsup_format!("panicking is not supported on this platform"),
40+
}
41+
}
42+
3543
/// Handles the special `miri_start_panic` intrinsic, which is called
3644
/// by libpanic_unwind to delegate the actual unwinding process to Miri.
3745
fn handle_miri_start_panic(

tests/compile-fail/abort-terminator.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// error-pattern: the evaluated program aborted
2+
// ignored-windows (panics dont work on Windows)
23
#![feature(unwind_attributes)]
34

45
#[unwind(aborts)]

tests/compile-fail/double_panic.rs renamed to tests/compile-fail/panic/double_panic.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
// error-pattern: the evaluated program aborted
2+
// ignored-windows (panics dont work on Windows)
3+
24
struct Foo;
35
impl Drop for Foo {
46
fn drop(&mut self) {

tests/compile-fail/panic/windows1.rs

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// ignore-linux
2+
// ignore-macos
3+
4+
// Test that panics on Windows give a reasonable error message.
5+
6+
// error-pattern: panicking is not supported on this platform
7+
fn main() {
8+
core::panic!("this is {}", "Windows");
9+
}

tests/compile-fail/panic/windows2.rs

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// ignore-linux
2+
// ignore-macos
3+
4+
// Test that panics on Windows give a reasonable error message.
5+
6+
// error-pattern: panicking is not supported on this platform
7+
fn main() {
8+
std::panic!("this is Windows");
9+
}

tests/compile-fail/panic/windows3.rs

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// ignore-linux
2+
// ignore-macos
3+
4+
// Test that panics on Windows give a reasonable error message.
5+
6+
// error-pattern: panicking is not supported on this platform
7+
#[allow(unconditional_panic)]
8+
fn main() {
9+
let _val = 1/0;
10+
}

0 commit comments

Comments
 (0)