Skip to content

Commit e81ebff

Browse files
committed
Implement panic_if_any_invalid and panic_if_zero_invalid intrinsics
1 parent 881e65c commit e81ebff

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

src/shims/intrinsics.rs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,32 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
449449
let layout = this.layout_of(ty)?;
450450
if layout.abi.is_uninhabited() {
451451
// 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);
452+
return this.start_panic(&format!("attempted to instantiate uninhabited type {}", ty), unwind);
453+
}
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() {
461+
// Return here because we paniced instead of returning normally from the intrinsic.
462+
return this.start_panic(&format!("attempted to zero-initialize type `{}`, which is invalid", ty), unwind);
463+
}
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() {
476+
// Return here because we paniced instead of returning normally from the intrinsic.
477+
return this.start_panic(&format!("attempted to leave type `{}` uninitialized, which is invalid", ty), unwind);
453478
}
454479
}
455480

0 commit comments

Comments
 (0)