diff options
| author | Matthias Krüger <476013+matthiaskrgr@users.noreply.github.com> | 2025-07-03 05:21:36 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-07-03 05:21:36 +0200 |
| commit | bc0262d0f1bf9165e28c01b64fb0dd1a24534968 (patch) | |
| tree | 9e275fd8466532f354e5e3496cc74659f15d82ca /library | |
| parent | 5aa7dd81d80db2a7e58344587a6e6ecbd2690510 (diff) | |
| parent | 8362508989c8941ccbba26aaa8c787cc492c6fef (diff) | |
| download | rust-bc0262d0f1bf9165e28c01b64fb0dd1a24534968.tar.gz rust-bc0262d0f1bf9165e28c01b64fb0dd1a24534968.zip | |
Rollup merge of #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?
Diffstat (limited to 'library')
| -rw-r--r-- | library/core/src/intrinsics/mod.rs | 11 | ||||
| -rw-r--r-- | library/core/src/mem/maybe_uninit.rs | 4 |
2 files changed, 11 insertions, 4 deletions
diff --git a/library/core/src/intrinsics/mod.rs b/library/core/src/intrinsics/mod.rs index 4250de9fb2b..5827fa93bfb 100644 --- a/library/core/src/intrinsics/mod.rs +++ b/library/core/src/intrinsics/mod.rs @@ -472,7 +472,8 @@ pub fn select_unpredictable<T>(b: bool, true_val: T, false_val: T) -> T { } /// A guard for unsafe functions that cannot ever be executed if `T` is uninhabited: -/// This will statically either panic, or do nothing. +/// This will statically either panic, or do nothing. It does not *guarantee* to ever panic, +/// and should only be called if an assertion failure will imply language UB in the following code. /// /// This intrinsic does not have a stable counterpart. #[rustc_intrinsic_const_stable_indirect] @@ -481,7 +482,9 @@ pub fn select_unpredictable<T>(b: bool, true_val: T, false_val: T) -> T { pub const fn assert_inhabited<T>(); /// A guard for unsafe functions that cannot ever be executed if `T` does not permit -/// zero-initialization: This will statically either panic, or do nothing. +/// zero-initialization: This will statically either panic, or do nothing. It does not *guarantee* +/// to ever panic, and should only be called if an assertion failure will imply language UB in the +/// following code. /// /// This intrinsic does not have a stable counterpart. #[rustc_intrinsic_const_stable_indirect] @@ -489,7 +492,9 @@ pub const fn assert_inhabited<T>(); #[rustc_intrinsic] pub const fn assert_zero_valid<T>(); -/// A guard for `std::mem::uninitialized`. This will statically either panic, or do nothing. +/// A guard for `std::mem::uninitialized`. This will statically either panic, or do nothing. It does +/// not *guarantee* to ever panic, and should only be called if an assertion failure will imply +/// language UB in the following code. /// /// This intrinsic does not have a stable counterpart. #[rustc_intrinsic_const_stable_indirect] diff --git a/library/core/src/mem/maybe_uninit.rs b/library/core/src/mem/maybe_uninit.rs index 63a479ed8dd..fc35e54bb0d 100644 --- a/library/core/src/mem/maybe_uninit.rs +++ b/library/core/src/mem/maybe_uninit.rs @@ -616,7 +616,9 @@ impl<T> MaybeUninit<T> { // This also means that `self` must be a `value` variant. unsafe { intrinsics::assert_inhabited::<T>(); - ManuallyDrop::into_inner(self.value) + // We do this via a raw ptr read instead of `ManuallyDrop::into_inner` so that there's + // no trace of `ManuallyDrop` in Miri's error messages here. + (&raw const self.value).cast::<T>().read() } } |
