Skip to content

Commit 548c90e

Browse files
committed
share some code between panic intrinsics, and fix the message
1 parent 2802c3c commit 548c90e

File tree

3 files changed

+12
-27
lines changed

3 files changed

+12
-27
lines changed

src/shims/intrinsics.rs

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -444,35 +444,20 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
444444
this.write_scalar(result_ptr, dest)?;
445445
}
446446

447-
"panic_if_uninhabited" => {
447+
"panic_if_uninhabited" |
448+
"panic_if_zero_invalid" |
449+
"panic_if_any_invalid" => {
448450
let ty = substs.type_at(0);
449451
let layout = this.layout_of(ty)?;
450452
if layout.abi.is_uninhabited() {
451453
// Return here because we paniced instead of returning normally from the intrinsic.
452-
return this.start_panic(&format!("attempted to instantiate uninhabited type {}", ty), unwind);
454+
return this.start_panic(&format!("attempted to instantiate uninhabited type `{}`", ty), unwind);
453455
}
454-
}
455-
456-
"panic_if_zero_invalid" => {
457-
let ty = substs.type_at(0);
458-
let layout = this.layout_of(ty)?;
459-
// Check if it permits zeroed raw initialization
460-
if !layout.might_permit_raw_init(this, /*zero:*/ true).unwrap() {
456+
if intrinsic_name == "panic_if_zero_invalid" && !layout.might_permit_raw_init(this, /*zero:*/ true).unwrap() {
461457
// Return here because we paniced instead of returning normally from the intrinsic.
462458
return this.start_panic(&format!("attempted to zero-initialize type `{}`, which is invalid", ty), unwind);
463459
}
464-
}
465-
466-
"panic_if_any_invalid" => {
467-
let ty = substs.type_at(0);
468-
let layout = this.layout_of(ty)?;
469-
// rustc handles all these in a single function, but we don't so we need to make sure `mem::uninitialized::<!>()` returns the right error.
470-
// So we check for `is_uninhabited` here too.
471-
if layout.abi.is_uninhabited() {
472-
return this.start_panic(&format!("attempted to instantiate uninhabited type {}", ty), unwind);
473-
}
474-
// Check if it permits any raw initialization
475-
if !layout.might_permit_raw_init(this, /*zero:*/ false).unwrap() {
460+
if intrinsic_name == "panic_if_any_invalid" && !layout.might_permit_raw_init(this, /*zero:*/ false).unwrap() {
476461
// Return here because we paniced instead of returning normally from the intrinsic.
477462
return this.start_panic(&format!("attempted to leave type `{}` uninitialized, which is invalid", ty), unwind);
478463
}

tests/run-pass/panic/catch_panic.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,11 @@ fn main() {
7171
#[allow(deprecated, invalid_value)]
7272
{
7373
test(
74-
Some("attempted to instantiate uninhabited type !"),
74+
Some("attempted to instantiate uninhabited type `!`"),
7575
|_old_val| unsafe { std::mem::uninitialized::<!>() },
7676
);
7777
test(
78-
Some("attempted to zero-initialize type `!`, which is invalid"),
78+
Some("attempted to instantiate uninhabited type `!`"),
7979
|_old_val| unsafe { std::mem::zeroed::<!>() },
8080
);
8181
test(

tests/run-pass/panic/catch_panic.stderr

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ thread 'main' panicked at 'index out of bounds: the len is 3 but the index is 4'
1616
Caught panic message (String): index out of bounds: the len is 3 but the index is 4
1717
thread 'main' panicked at 'attempt to divide by zero', $DIR/catch_panic.rs:67:33
1818
Caught panic message (String): attempt to divide by zero
19-
thread 'main' panicked at 'attempted to instantiate uninhabited type !', $LOC
20-
Caught panic message (String): attempted to instantiate uninhabited type !
21-
thread 'main' panicked at 'attempted to zero-initialize type `!`, which is invalid', $LOC
22-
Caught panic message (String): attempted to zero-initialize type `!`, which is invalid
19+
thread 'main' panicked at 'attempted to instantiate uninhabited type `!`', $LOC
20+
Caught panic message (String): attempted to instantiate uninhabited type `!`
21+
thread 'main' panicked at 'attempted to instantiate uninhabited type `!`', $LOC
22+
Caught panic message (String): attempted to instantiate uninhabited type `!`
2323
thread 'main' panicked at 'attempted to leave type `fn()` uninitialized, which is invalid', $LOC
2424
Caught panic message (String): attempted to leave type `fn()` uninitialized, which is invalid
2525
thread 'main' panicked at 'attempted to zero-initialize type `fn()`, which is invalid', $LOC

0 commit comments

Comments
 (0)