Skip to content

Commit 762a6eb

Browse files
authored
Rollup merge of rust-lang#143327 - RalfJung:miri-type-validity-error, r=oli-obk
miri: improve errors for type validity assertion failures Miri has pretty nice errors for type validity violations, printing which field in the type the problem occurs at and so on. However, we don't see these errors when using e.g. `mem::zeroed` as that uses `assert_zero_valid` to bail out before Miri can detect the UB. Similar to what we did with `@saethlin's` UB checks, I think we should disable such language UB checks in Miri so that we can get better error messages. If we go for this we should probably say this in the intrinsic docs as well so that people don't think they can rely on these intrinsics catching anything. Furthermore, I slightly changed `MaybeUninit::assume_init` so that the `.value` field does not show up in error messages any more. `@rust-lang/miri` what do you think?
2 parents 4d66ef3 + c06a746 commit 762a6eb

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

core/src/intrinsics/mod.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -472,7 +472,8 @@ pub fn select_unpredictable<T>(b: bool, true_val: T, false_val: T) -> T {
472472
}
473473

474474
/// A guard for unsafe functions that cannot ever be executed if `T` is uninhabited:
475-
/// This will statically either panic, or do nothing.
475+
/// This will statically either panic, or do nothing. It does not *guarantee* to ever panic,
476+
/// and should only be called if an assertion failure will imply language UB in the following code.
476477
///
477478
/// This intrinsic does not have a stable counterpart.
478479
#[rustc_intrinsic_const_stable_indirect]
@@ -481,15 +482,19 @@ pub fn select_unpredictable<T>(b: bool, true_val: T, false_val: T) -> T {
481482
pub const fn assert_inhabited<T>();
482483

483484
/// A guard for unsafe functions that cannot ever be executed if `T` does not permit
484-
/// zero-initialization: This will statically either panic, or do nothing.
485+
/// zero-initialization: This will statically either panic, or do nothing. It does not *guarantee*
486+
/// to ever panic, and should only be called if an assertion failure will imply language UB in the
487+
/// following code.
485488
///
486489
/// This intrinsic does not have a stable counterpart.
487490
#[rustc_intrinsic_const_stable_indirect]
488491
#[rustc_nounwind]
489492
#[rustc_intrinsic]
490493
pub const fn assert_zero_valid<T>();
491494

492-
/// A guard for `std::mem::uninitialized`. This will statically either panic, or do nothing.
495+
/// A guard for `std::mem::uninitialized`. This will statically either panic, or do nothing. It does
496+
/// not *guarantee* to ever panic, and should only be called if an assertion failure will imply
497+
/// language UB in the following code.
493498
///
494499
/// This intrinsic does not have a stable counterpart.
495500
#[rustc_intrinsic_const_stable_indirect]

core/src/mem/maybe_uninit.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -616,7 +616,9 @@ impl<T> MaybeUninit<T> {
616616
// This also means that `self` must be a `value` variant.
617617
unsafe {
618618
intrinsics::assert_inhabited::<T>();
619-
ManuallyDrop::into_inner(self.value)
619+
// We do this via a raw ptr read instead of `ManuallyDrop::into_inner` so that there's
620+
// no trace of `ManuallyDrop` in Miri's error messages here.
621+
(&raw const self.value).cast::<T>().read()
620622
}
621623
}
622624

0 commit comments

Comments
 (0)