Rollup merge of #69954 - RalfJung:panic_if-assert, r=Centril,eddyb
rename panic_if_ intrinsics to assert_ [Suggested by @eddyb](https://github.com/rust-lang/miri/issues/1222#issuecomment-598087523)
This commit is contained in:
commit
77263db96f
|
@ -1005,17 +1005,23 @@ extern "rust-intrinsic" {
|
||||||
|
|
||||||
/// A guard for unsafe functions that cannot ever be executed if `T` is uninhabited:
|
/// 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.
|
||||||
|
#[cfg(bootstrap)]
|
||||||
pub fn panic_if_uninhabited<T>();
|
pub fn panic_if_uninhabited<T>();
|
||||||
|
|
||||||
|
/// A guard for unsafe functions that cannot ever be executed if `T` is uninhabited:
|
||||||
|
/// This will statically either panic, or do nothing.
|
||||||
|
#[cfg(not(bootstrap))]
|
||||||
|
pub fn assert_inhabited<T>();
|
||||||
|
|
||||||
/// A guard for unsafe functions that cannot ever be executed if `T` does not permit
|
/// 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.
|
||||||
#[cfg(not(bootstrap))]
|
#[cfg(not(bootstrap))]
|
||||||
pub fn panic_if_zero_invalid<T>();
|
pub fn assert_zero_valid<T>();
|
||||||
|
|
||||||
/// A guard for unsafe functions that cannot ever be executed if `T` has invalid
|
/// A guard for unsafe functions that cannot ever be executed if `T` has invalid
|
||||||
/// bit patterns: This will statically either panic, or do nothing.
|
/// bit patterns: This will statically either panic, or do nothing.
|
||||||
#[cfg(not(bootstrap))]
|
#[cfg(not(bootstrap))]
|
||||||
pub fn panic_if_any_invalid<T>();
|
pub fn assert_uninit_valid<T>();
|
||||||
|
|
||||||
/// Gets a reference to a static `Location` indicating where it was called.
|
/// Gets a reference to a static `Location` indicating where it was called.
|
||||||
#[rustc_const_unstable(feature = "const_caller_location", issue = "47809")]
|
#[rustc_const_unstable(feature = "const_caller_location", issue = "47809")]
|
||||||
|
|
|
@ -495,7 +495,10 @@ impl<T> MaybeUninit<T> {
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
#[rustc_diagnostic_item = "assume_init"]
|
#[rustc_diagnostic_item = "assume_init"]
|
||||||
pub unsafe fn assume_init(self) -> T {
|
pub unsafe fn assume_init(self) -> T {
|
||||||
|
#[cfg(bootstrap)]
|
||||||
intrinsics::panic_if_uninhabited::<T>();
|
intrinsics::panic_if_uninhabited::<T>();
|
||||||
|
#[cfg(not(bootstrap))]
|
||||||
|
intrinsics::assert_inhabited::<T>();
|
||||||
ManuallyDrop::into_inner(self.value)
|
ManuallyDrop::into_inner(self.value)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -559,7 +562,10 @@ impl<T> MaybeUninit<T> {
|
||||||
#[unstable(feature = "maybe_uninit_extra", issue = "63567")]
|
#[unstable(feature = "maybe_uninit_extra", issue = "63567")]
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
pub unsafe fn read(&self) -> T {
|
pub unsafe fn read(&self) -> T {
|
||||||
|
#[cfg(bootstrap)]
|
||||||
intrinsics::panic_if_uninhabited::<T>();
|
intrinsics::panic_if_uninhabited::<T>();
|
||||||
|
#[cfg(not(bootstrap))]
|
||||||
|
intrinsics::assert_inhabited::<T>();
|
||||||
self.as_ptr().read()
|
self.as_ptr().read()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -621,7 +627,10 @@ impl<T> MaybeUninit<T> {
|
||||||
#[unstable(feature = "maybe_uninit_ref", issue = "63568")]
|
#[unstable(feature = "maybe_uninit_ref", issue = "63568")]
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
pub unsafe fn get_ref(&self) -> &T {
|
pub unsafe fn get_ref(&self) -> &T {
|
||||||
|
#[cfg(bootstrap)]
|
||||||
intrinsics::panic_if_uninhabited::<T>();
|
intrinsics::panic_if_uninhabited::<T>();
|
||||||
|
#[cfg(not(bootstrap))]
|
||||||
|
intrinsics::assert_inhabited::<T>();
|
||||||
&*self.value
|
&*self.value
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -739,7 +748,10 @@ impl<T> MaybeUninit<T> {
|
||||||
#[unstable(feature = "maybe_uninit_ref", issue = "63568")]
|
#[unstable(feature = "maybe_uninit_ref", issue = "63568")]
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
pub unsafe fn get_mut(&mut self) -> &mut T {
|
pub unsafe fn get_mut(&mut self) -> &mut T {
|
||||||
|
#[cfg(bootstrap)]
|
||||||
intrinsics::panic_if_uninhabited::<T>();
|
intrinsics::panic_if_uninhabited::<T>();
|
||||||
|
#[cfg(not(bootstrap))]
|
||||||
|
intrinsics::assert_inhabited::<T>();
|
||||||
&mut *self.value
|
&mut *self.value
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -497,7 +497,7 @@ pub const fn needs_drop<T>() -> bool {
|
||||||
#[rustc_diagnostic_item = "mem_zeroed"]
|
#[rustc_diagnostic_item = "mem_zeroed"]
|
||||||
pub unsafe fn zeroed<T>() -> T {
|
pub unsafe fn zeroed<T>() -> T {
|
||||||
#[cfg(not(bootstrap))]
|
#[cfg(not(bootstrap))]
|
||||||
intrinsics::panic_if_zero_invalid::<T>();
|
intrinsics::assert_zero_valid::<T>();
|
||||||
#[cfg(bootstrap)]
|
#[cfg(bootstrap)]
|
||||||
intrinsics::panic_if_uninhabited::<T>();
|
intrinsics::panic_if_uninhabited::<T>();
|
||||||
intrinsics::init()
|
intrinsics::init()
|
||||||
|
@ -533,7 +533,7 @@ pub unsafe fn zeroed<T>() -> T {
|
||||||
#[rustc_diagnostic_item = "mem_uninitialized"]
|
#[rustc_diagnostic_item = "mem_uninitialized"]
|
||||||
pub unsafe fn uninitialized<T>() -> T {
|
pub unsafe fn uninitialized<T>() -> T {
|
||||||
#[cfg(not(bootstrap))]
|
#[cfg(not(bootstrap))]
|
||||||
intrinsics::panic_if_any_invalid::<T>();
|
intrinsics::assert_uninit_valid::<T>();
|
||||||
#[cfg(bootstrap)]
|
#[cfg(bootstrap)]
|
||||||
intrinsics::panic_if_uninhabited::<T>();
|
intrinsics::panic_if_uninhabited::<T>();
|
||||||
intrinsics::uninit()
|
intrinsics::uninit()
|
||||||
|
|
|
@ -449,38 +449,38 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
|
||||||
destination: &Option<(mir::Place<'tcx>, mir::BasicBlock)>,
|
destination: &Option<(mir::Place<'tcx>, mir::BasicBlock)>,
|
||||||
cleanup: Option<mir::BasicBlock>,
|
cleanup: Option<mir::BasicBlock>,
|
||||||
) -> bool {
|
) -> bool {
|
||||||
// Emit a panic or a no-op for `panic_if_uninhabited`.
|
// Emit a panic or a no-op for `assert_*` intrinsics.
|
||||||
// These are intrinsics that compile to panics so that we can get a message
|
// These are intrinsics that compile to panics so that we can get a message
|
||||||
// which mentions the offending type, even from a const context.
|
// which mentions the offending type, even from a const context.
|
||||||
#[derive(Debug, PartialEq)]
|
#[derive(Debug, PartialEq)]
|
||||||
enum PanicIntrinsic {
|
enum AssertIntrinsic {
|
||||||
IfUninhabited,
|
Inhabited,
|
||||||
IfZeroInvalid,
|
ZeroValid,
|
||||||
IfAnyInvalid,
|
UninitValid,
|
||||||
};
|
};
|
||||||
let panic_intrinsic = intrinsic.and_then(|i| match i {
|
let panic_intrinsic = intrinsic.and_then(|i| match i {
|
||||||
// FIXME: Move to symbols instead of strings.
|
// FIXME: Move to symbols instead of strings.
|
||||||
"panic_if_uninhabited" => Some(PanicIntrinsic::IfUninhabited),
|
"assert_inhabited" => Some(AssertIntrinsic::Inhabited),
|
||||||
"panic_if_zero_invalid" => Some(PanicIntrinsic::IfZeroInvalid),
|
"assert_zero_valid" => Some(AssertIntrinsic::ZeroValid),
|
||||||
"panic_if_any_invalid" => Some(PanicIntrinsic::IfAnyInvalid),
|
"assert_uninit_valid" => Some(AssertIntrinsic::UninitValid),
|
||||||
_ => None,
|
_ => None,
|
||||||
});
|
});
|
||||||
if let Some(intrinsic) = panic_intrinsic {
|
if let Some(intrinsic) = panic_intrinsic {
|
||||||
use PanicIntrinsic::*;
|
use AssertIntrinsic::*;
|
||||||
let ty = instance.unwrap().substs.type_at(0);
|
let ty = instance.unwrap().substs.type_at(0);
|
||||||
let layout = bx.layout_of(ty);
|
let layout = bx.layout_of(ty);
|
||||||
let do_panic = match intrinsic {
|
let do_panic = match intrinsic {
|
||||||
IfUninhabited => layout.abi.is_uninhabited(),
|
Inhabited => layout.abi.is_uninhabited(),
|
||||||
// We unwrap as the error type is `!`.
|
// We unwrap as the error type is `!`.
|
||||||
IfZeroInvalid => !layout.might_permit_raw_init(bx, /*zero:*/ true).unwrap(),
|
ZeroValid => !layout.might_permit_raw_init(bx, /*zero:*/ true).unwrap(),
|
||||||
// We unwrap as the error type is `!`.
|
// We unwrap as the error type is `!`.
|
||||||
IfAnyInvalid => !layout.might_permit_raw_init(bx, /*zero:*/ false).unwrap(),
|
UninitValid => !layout.might_permit_raw_init(bx, /*zero:*/ false).unwrap(),
|
||||||
};
|
};
|
||||||
if do_panic {
|
if do_panic {
|
||||||
let msg_str = if layout.abi.is_uninhabited() {
|
let msg_str = if layout.abi.is_uninhabited() {
|
||||||
// Use this error even for the other intrinsics as it is more precise.
|
// Use this error even for the other intrinsics as it is more precise.
|
||||||
format!("attempted to instantiate uninhabited type `{}`", ty)
|
format!("attempted to instantiate uninhabited type `{}`", ty)
|
||||||
} else if intrinsic == IfZeroInvalid {
|
} else if intrinsic == ZeroValid {
|
||||||
format!("attempted to zero-initialize type `{}`, which is invalid", ty)
|
format!("attempted to zero-initialize type `{}`, which is invalid", ty)
|
||||||
} else {
|
} else {
|
||||||
format!("attempted to leave type `{}` uninitialized, which is invalid", ty)
|
format!("attempted to leave type `{}` uninitialized, which is invalid", ty)
|
||||||
|
|
|
@ -147,7 +147,7 @@ pub fn check_intrinsic_type(tcx: TyCtxt<'_>, it: &hir::ForeignItem<'_>) {
|
||||||
),
|
),
|
||||||
"rustc_peek" => (1, vec![param(0)], param(0)),
|
"rustc_peek" => (1, vec![param(0)], param(0)),
|
||||||
"caller_location" => (0, vec![], tcx.caller_location_ty()),
|
"caller_location" => (0, vec![], tcx.caller_location_ty()),
|
||||||
"panic_if_uninhabited" | "panic_if_zero_invalid" | "panic_if_any_invalid" => {
|
"assert_inhabited" | "assert_zero_valid" | "assert_uninit_valid" => {
|
||||||
(1, Vec::new(), tcx.mk_unit())
|
(1, Vec::new(), tcx.mk_unit())
|
||||||
}
|
}
|
||||||
"init" => (1, Vec::new(), param(0)),
|
"init" => (1, Vec::new(), param(0)),
|
||||||
|
|
Loading…
Reference in New Issue