diff --git a/library/core/src/alloc/layout.rs b/library/core/src/alloc/layout.rs index e6f39db9dced3..a6778351ee087 100644 --- a/library/core/src/alloc/layout.rs +++ b/library/core/src/alloc/layout.rs @@ -130,7 +130,7 @@ impl Layout { assert_unsafe_precondition!( check_library_ub, "Layout::from_size_align_unchecked requires that align is a power of 2 \ - and the rounded-up allocation size does not exceed isize::MAX", + and the rounded-up allocation size does not exceed isize::MAX (size:{size}, align:{align})", ( size: usize = size, align: usize = align, diff --git a/library/core/src/ascii/ascii_char.rs b/library/core/src/ascii/ascii_char.rs index 48de4f17b1b3a..7ea19028c1b29 100644 --- a/library/core/src/ascii/ascii_char.rs +++ b/library/core/src/ascii/ascii_char.rs @@ -506,7 +506,7 @@ impl AsciiChar { pub const unsafe fn digit_unchecked(d: u8) -> Self { assert_unsafe_precondition!( check_language_ub, - "`ascii::Char::digit_unchecked` input cannot exceed 9.", + "`ascii::Char::digit_unchecked` input cannot exceed 9. (d:{d})", (d: u8 = d) => d < 10 ); diff --git a/library/core/src/char/convert.rs b/library/core/src/char/convert.rs index 73ab4f1e52ade..5c2cffbac60f1 100644 --- a/library/core/src/char/convert.rs +++ b/library/core/src/char/convert.rs @@ -26,7 +26,7 @@ pub(super) const unsafe fn from_u32_unchecked(i: u32) -> char { unsafe { assert_unsafe_precondition!( check_language_ub, - "invalid value for `char`", + "invalid value for `char` ({i})", (i: u32 = i) => char_try_from_u32(i).is_ok() ); transmute(i) diff --git a/library/core/src/intrinsics/mod.rs b/library/core/src/intrinsics/mod.rs index 65e2dcbc7cc31..2818c7fefd69f 100644 --- a/library/core/src/intrinsics/mod.rs +++ b/library/core/src/intrinsics/mod.rs @@ -4354,7 +4354,8 @@ pub const unsafe fn copy_nonoverlapping(src: *const T, dst: *mut T, count: us ub_checks::assert_unsafe_precondition!( check_language_ub, "ptr::copy_nonoverlapping requires that both pointer arguments are aligned and non-null \ - and the specified memory ranges do not overlap", + and the specified memory ranges do not overlap \ + (src:{src:?}, dst:{dst:?}, size:{size}, align:{align}, count:{count})", ( src: *const () = src as *const (), dst: *mut () = dst as *mut (), @@ -4459,7 +4460,8 @@ pub const unsafe fn copy(src: *const T, dst: *mut T, count: usize) { unsafe { ub_checks::assert_unsafe_precondition!( check_language_ub, - "ptr::copy requires that both pointer arguments are aligned and non-null", + "ptr::copy requires that both pointer arguments are aligned and non-null \ + (src:{src:?}, dst:{dst:?}, align:{align})", ( src: *const () = src as *const (), dst: *mut () = dst as *mut (), @@ -4542,7 +4544,8 @@ pub const unsafe fn write_bytes(dst: *mut T, val: u8, count: usize) { unsafe { ub_checks::assert_unsafe_precondition!( check_language_ub, - "ptr::write_bytes requires that the destination pointer is aligned and non-null", + "ptr::write_bytes requires that the destination pointer is aligned and non-null \ + (dst:{addr:?}, align:{align})", ( addr: *const () = dst as *const (), align: usize = align_of::(), diff --git a/library/core/src/num/int_macros.rs b/library/core/src/num/int_macros.rs index 9a202600988c4..85e9808f7e745 100644 --- a/library/core/src/num/int_macros.rs +++ b/library/core/src/num/int_macros.rs @@ -514,6 +514,7 @@ macro_rules! int_impl { assert_unsafe_precondition!( check_language_ub, concat!(stringify!($SelfT), "::unchecked_add cannot overflow"), + // FIXME: concat! prevents adding formatting ( lhs: $SelfT = self, rhs: $SelfT = rhs, @@ -664,6 +665,7 @@ macro_rules! int_impl { assert_unsafe_precondition!( check_language_ub, concat!(stringify!($SelfT), "::unchecked_sub cannot overflow"), + // FIXME: concat! prevents adding formatting ( lhs: $SelfT = self, rhs: $SelfT = rhs, @@ -814,6 +816,7 @@ macro_rules! int_impl { assert_unsafe_precondition!( check_language_ub, concat!(stringify!($SelfT), "::unchecked_mul cannot overflow"), + // FIXME: concat! prevents adding formatting ( lhs: $SelfT = self, rhs: $SelfT = rhs, @@ -1158,6 +1161,7 @@ macro_rules! int_impl { assert_unsafe_precondition!( check_language_ub, concat!(stringify!($SelfT), "::unchecked_neg cannot overflow"), + // FIXME: concat! prevents adding formatting ( lhs: $SelfT = self, ) => !lhs.overflowing_neg().1, @@ -1286,6 +1290,7 @@ macro_rules! int_impl { assert_unsafe_precondition!( check_language_ub, concat!(stringify!($SelfT), "::unchecked_shl cannot overflow"), + // FIXME: concat! prevents adding formatting ( rhs: u32 = rhs, ) => rhs < <$ActualT>::BITS, @@ -1407,6 +1412,7 @@ macro_rules! int_impl { assert_unsafe_precondition!( check_language_ub, concat!(stringify!($SelfT), "::unchecked_shr cannot overflow"), + // FIXME: concat! prevents adding formatting ( rhs: u32 = rhs, ) => rhs < <$ActualT>::BITS, diff --git a/library/core/src/num/nonzero.rs b/library/core/src/num/nonzero.rs index a9294306b1b61..cc4aa1d78a896 100644 --- a/library/core/src/num/nonzero.rs +++ b/library/core/src/num/nonzero.rs @@ -403,6 +403,7 @@ where ub_checks::assert_unsafe_precondition!( check_language_ub, "NonZero::new_unchecked requires the argument to be non-zero", + // FIXME: Can't print n here because of how the check is written () => false, ); intrinsics::unreachable() @@ -443,6 +444,7 @@ where ub_checks::assert_unsafe_precondition!( check_library_ub, "NonZero::from_mut_unchecked requires the argument to dereference as non-zero", + // FIXME: Can't print n here because of how the check is written () => false, ); intrinsics::unreachable() diff --git a/library/core/src/num/uint_macros.rs b/library/core/src/num/uint_macros.rs index 4a5fdbfb0ea2c..6962e67e0c054 100644 --- a/library/core/src/num/uint_macros.rs +++ b/library/core/src/num/uint_macros.rs @@ -561,6 +561,7 @@ macro_rules! uint_impl { assert_unsafe_precondition!( check_language_ub, concat!(stringify!($SelfT), "::unchecked_add cannot overflow"), + // FIXME: concat! prevents adding formatting ( lhs: $SelfT = self, rhs: $SelfT = rhs, @@ -751,6 +752,7 @@ macro_rules! uint_impl { assert_unsafe_precondition!( check_language_ub, concat!(stringify!($SelfT), "::unchecked_sub cannot overflow"), + // FIXME: concat! prevents adding formatting ( lhs: $SelfT = self, rhs: $SelfT = rhs, @@ -934,6 +936,7 @@ macro_rules! uint_impl { assert_unsafe_precondition!( check_language_ub, concat!(stringify!($SelfT), "::unchecked_mul cannot overflow"), + // FIXME: concat! prevents adding formatting ( lhs: $SelfT = self, rhs: $SelfT = rhs, @@ -1504,6 +1507,7 @@ macro_rules! uint_impl { assert_unsafe_precondition!( check_language_ub, concat!(stringify!($SelfT), "::unchecked_shl cannot overflow"), + // FIXME: concat! prevents adding formatting ( rhs: u32 = rhs, ) => rhs < <$ActualT>::BITS, @@ -1625,6 +1629,7 @@ macro_rules! uint_impl { assert_unsafe_precondition!( check_language_ub, concat!(stringify!($SelfT), "::unchecked_shr cannot overflow"), + // FIXME: concat! prevents adding formatting ( rhs: u32 = rhs, ) => rhs < <$ActualT>::BITS, diff --git a/library/core/src/ops/index_range.rs b/library/core/src/ops/index_range.rs index dce3514a1595b..0f88d7a892fed 100644 --- a/library/core/src/ops/index_range.rs +++ b/library/core/src/ops/index_range.rs @@ -21,7 +21,8 @@ impl IndexRange { pub const unsafe fn new_unchecked(start: usize, end: usize) -> Self { ub_checks::assert_unsafe_precondition!( check_library_ub, - "IndexRange::new_unchecked requires `start <= end`", + "IndexRange::new_unchecked requires `start <= end` \ + (start:{start}, end:{end})", (start: usize = start, end: usize = end) => start <= end, ); IndexRange { start, end } diff --git a/library/core/src/ptr/alignment.rs b/library/core/src/ptr/alignment.rs index 74a1d40f4e734..99855e0c67d11 100644 --- a/library/core/src/ptr/alignment.rs +++ b/library/core/src/ptr/alignment.rs @@ -75,7 +75,8 @@ impl Alignment { pub const unsafe fn new_unchecked(align: usize) -> Self { assert_unsafe_precondition!( check_language_ub, - "Alignment::new_unchecked requires a power of two", + "Alignment::new_unchecked requires a power of two \ + (align:{align})", (align: usize = align) => align.is_power_of_two() ); diff --git a/library/core/src/ptr/const_ptr.rs b/library/core/src/ptr/const_ptr.rs index ec569291853a5..9e3308b14f452 100644 --- a/library/core/src/ptr/const_ptr.rs +++ b/library/core/src/ptr/const_ptr.rs @@ -443,7 +443,8 @@ impl *const T { ub_checks::assert_unsafe_precondition!( check_language_ub, - "ptr::offset requires the address calculation to not overflow", + "ptr::offset requires the address calculation to not overflow \ + (ptr:{this:?}, count:{count}, size:{size})", ( this: *const () = self as *const (), count: isize = count, @@ -789,7 +790,8 @@ impl *const T { ub_checks::assert_unsafe_precondition!( check_language_ub, - "ptr::sub_ptr requires `self >= origin`", + "ptr::sub_ptr requires `self >= origin` \ + (self:{this:?}, origin:{origin:?})", ( this: *const () = self as *const (), origin: *const () = origin as *const (), @@ -955,7 +957,8 @@ impl *const T { #[cfg(debug_assertions)] // Expensive, and doesn't catch much in the wild. ub_checks::assert_unsafe_precondition!( check_language_ub, - "ptr::add requires that the address calculation does not overflow", + "ptr::add requires that the address calculation does not overflow \ + (self:{this:?}, count:{count}, size:{size})", ( this: *const () = self as *const (), count: usize = count, @@ -1060,7 +1063,8 @@ impl *const T { #[cfg(debug_assertions)] // Expensive, and doesn't catch much in the wild. ub_checks::assert_unsafe_precondition!( check_language_ub, - "ptr::sub requires that the address calculation does not overflow", + "ptr::sub requires that the address calculation does not overflow \ + (self:{this:?}, count:{count}, size:{size})", ( this: *const () = self as *const (), count: usize = count, diff --git a/library/core/src/ptr/mod.rs b/library/core/src/ptr/mod.rs index e6e13eaff7b0f..d7e0de54ece63 100644 --- a/library/core/src/ptr/mod.rs +++ b/library/core/src/ptr/mod.rs @@ -1078,7 +1078,8 @@ pub const unsafe fn swap_nonoverlapping(x: *mut T, y: *mut T, count: usize) { ub_checks::assert_unsafe_precondition!( check_language_ub, "ptr::swap_nonoverlapping requires that both pointer arguments are aligned and non-null \ - and the specified memory ranges do not overlap", + and the specified memory ranges do not overlap \ + (x:{x:?}, y:{y:?}, size:{size}, align:{align}, count:{count})", ( x: *mut () = x as *mut (), y: *mut () = y as *mut (), @@ -1223,7 +1224,8 @@ pub const unsafe fn replace(dst: *mut T, src: T) -> T { unsafe { ub_checks::assert_unsafe_precondition!( check_language_ub, - "ptr::replace requires that the pointer argument is aligned and non-null", + "ptr::replace requires that the pointer argument is aligned and non-null\ + (dst:{addr:?}, (align:{align}))", ( addr: *const () = dst as *const (), align: usize = align_of::(), @@ -1376,7 +1378,8 @@ pub const unsafe fn read(src: *const T) -> T { #[cfg(debug_assertions)] // Too expensive to always enable (for now?) ub_checks::assert_unsafe_precondition!( check_language_ub, - "ptr::read requires that the pointer argument is aligned and non-null", + "ptr::read requires that the pointer argument is aligned and non-null \ + (src:{addr:?}, align:{align})", ( addr: *const () = src as *const (), align: usize = align_of::(), @@ -1580,7 +1583,8 @@ pub const unsafe fn write(dst: *mut T, src: T) { #[cfg(debug_assertions)] // Too expensive to always enable (for now?) ub_checks::assert_unsafe_precondition!( check_language_ub, - "ptr::write requires that the pointer argument is aligned and non-null", + "ptr::write requires that the pointer argument is aligned and non-null \ + (dst:{addr:?}, align:{align})", ( addr: *mut () = dst as *mut (), align: usize = align_of::(), @@ -1752,7 +1756,8 @@ pub unsafe fn read_volatile(src: *const T) -> T { unsafe { ub_checks::assert_unsafe_precondition!( check_language_ub, - "ptr::read_volatile requires that the pointer argument is aligned and non-null", + "ptr::read_volatile requires that the pointer argument is aligned and non-null \ + (src:{addr:?}, align:{align})", ( addr: *const () = src as *const (), align: usize = align_of::(), @@ -1832,7 +1837,8 @@ pub unsafe fn write_volatile(dst: *mut T, src: T) { unsafe { ub_checks::assert_unsafe_precondition!( check_language_ub, - "ptr::write_volatile requires that the pointer argument is aligned and non-null", + "ptr::write_volatile requires that the pointer argument is aligned and non-null \ + (dst:{addr:?}, align:{align})", ( addr: *mut () = dst as *mut (), align: usize = align_of::(), diff --git a/library/core/src/ptr/mut_ptr.rs b/library/core/src/ptr/mut_ptr.rs index 34567917b523a..1dd73fbe12021 100644 --- a/library/core/src/ptr/mut_ptr.rs +++ b/library/core/src/ptr/mut_ptr.rs @@ -439,7 +439,8 @@ impl *mut T { ub_checks::assert_unsafe_precondition!( check_language_ub, - "ptr::offset requires the address calculation to not overflow", + "ptr::offset requires the address calculation to not overflow \ + (self:{this:?}, count:{count}, size:{size})", ( this: *const () = self as *const (), count: isize = count, @@ -1045,7 +1046,8 @@ impl *mut T { #[cfg(debug_assertions)] // Expensive, and doesn't catch much in the wild. ub_checks::assert_unsafe_precondition!( check_language_ub, - "ptr::add requires that the address calculation does not overflow", + "ptr::add requires that the address calculation does not overflow \ + (self:{this:?}, count:{count}, size:{size})", ( this: *const () = self as *const (), count: usize = count, @@ -1150,7 +1152,8 @@ impl *mut T { #[cfg(debug_assertions)] // Expensive, and doesn't catch much in the wild. ub_checks::assert_unsafe_precondition!( check_language_ub, - "ptr::sub requires that the address calculation does not overflow", + "ptr::sub requires that the address calculation does not overflow \ + (self:{this:?}, count:{count}, size:{size})", ( this: *const () = self as *const (), count: usize = count, diff --git a/library/core/src/ptr/non_null.rs b/library/core/src/ptr/non_null.rs index e0ba469272ed1..e809fe94e303e 100644 --- a/library/core/src/ptr/non_null.rs +++ b/library/core/src/ptr/non_null.rs @@ -195,7 +195,7 @@ impl NonNull { unsafe { assert_unsafe_precondition!( check_language_ub, - "NonNull::new_unchecked requires that the pointer is non-null", + "NonNull::new_unchecked requires that the pointer is non-null (ptr:{ptr:?})", (ptr: *mut () = ptr as *mut ()) => !ptr.is_null() ); NonNull { pointer: ptr as _ } diff --git a/library/core/src/slice/index.rs b/library/core/src/slice/index.rs index aafa19c0dd3d3..386e273bafc8b 100644 --- a/library/core/src/slice/index.rs +++ b/library/core/src/slice/index.rs @@ -242,8 +242,9 @@ unsafe impl SliceIndex<[T]> for usize { unsafe fn get_unchecked(self, slice: *const [T]) -> *const T { assert_unsafe_precondition!( check_language_ub, - "slice::get_unchecked requires that the index is within the slice", - (this: usize = self, len: usize = slice.len()) => this < len + "slice::get_unchecked requires that the index is within the slice \ + (index:{index}, len:{len})", + (index: usize = self, len: usize = slice.len()) => index < len ); // SAFETY: the caller guarantees that `slice` is not dangling, so it // cannot be longer than `isize::MAX`. They also guarantee that @@ -261,8 +262,9 @@ unsafe impl SliceIndex<[T]> for usize { unsafe fn get_unchecked_mut(self, slice: *mut [T]) -> *mut T { assert_unsafe_precondition!( check_library_ub, - "slice::get_unchecked_mut requires that the index is within the slice", - (this: usize = self, len: usize = slice.len()) => this < len + "slice::get_unchecked_mut requires that the index is within the slice \ + (index:{index}, len:{len})", + (index: usize = self, len: usize = slice.len()) => index < len ); // SAFETY: see comments for `get_unchecked` above. unsafe { get_mut_noubcheck(slice, self) } @@ -310,7 +312,8 @@ unsafe impl SliceIndex<[T]> for ops::IndexRange { unsafe fn get_unchecked(self, slice: *const [T]) -> *const [T] { assert_unsafe_precondition!( check_library_ub, - "slice::get_unchecked requires that the index is within the slice", + "slice::get_unchecked requires that the index is within the slice \ + (end:{end}, len:{len})", (end: usize = self.end(), len: usize = slice.len()) => end <= len ); // SAFETY: the caller guarantees that `slice` is not dangling, so it @@ -324,7 +327,8 @@ unsafe impl SliceIndex<[T]> for ops::IndexRange { unsafe fn get_unchecked_mut(self, slice: *mut [T]) -> *mut [T] { assert_unsafe_precondition!( check_library_ub, - "slice::get_unchecked_mut requires that the index is within the slice", + "slice::get_unchecked_mut requires that the index is within the slice \ + (end:{end}, len:{len})", (end: usize = self.end(), len: usize = slice.len()) => end <= len ); @@ -389,7 +393,8 @@ unsafe impl SliceIndex<[T]> for ops::Range { unsafe fn get_unchecked(self, slice: *const [T]) -> *const [T] { assert_unsafe_precondition!( check_library_ub, - "slice::get_unchecked requires that the range is within the slice", + "slice::get_unchecked requires that the range is within the slice \ + (range:{start}..{end}, len:{len})", ( start: usize = self.start, end: usize = self.end, @@ -413,7 +418,8 @@ unsafe impl SliceIndex<[T]> for ops::Range { unsafe fn get_unchecked_mut(self, slice: *mut [T]) -> *mut [T] { assert_unsafe_precondition!( check_library_ub, - "slice::get_unchecked_mut requires that the range is within the slice", + "slice::get_unchecked_mut requires that the range is within the slice \ + (range:{start}..{end}, len:{len})", ( start: usize = self.start, end: usize = self.end, diff --git a/library/core/src/slice/mod.rs b/library/core/src/slice/mod.rs index ab65f9d6d2fcc..9c575e16aebfc 100644 --- a/library/core/src/slice/mod.rs +++ b/library/core/src/slice/mod.rs @@ -962,7 +962,8 @@ impl [T] { pub const unsafe fn swap_unchecked(&mut self, a: usize, b: usize) { assert_unsafe_precondition!( check_library_ub, - "slice::swap_unchecked requires that the indices are within the slice", + "slice::swap_unchecked requires that the indices are within the slice \ + (a:{a}, b:{b}, len:{len})", ( len: usize = self.len(), a: usize = a, @@ -1309,7 +1310,8 @@ impl [T] { pub const unsafe fn as_chunks_unchecked(&self) -> &[[T; N]] { assert_unsafe_precondition!( check_language_ub, - "slice::as_chunks_unchecked requires `N != 0` and the slice to split exactly into `N`-element chunks", + "slice::as_chunks_unchecked requires `N != 0` and the slice to split exactly into `N`-element chunks \ + (N:{n}, len:{len})", (n: usize = N, len: usize = self.len()) => n != 0 && len % n == 0, ); // SAFETY: Caller must guarantee that `N` is nonzero and exactly divides the slice length @@ -1469,7 +1471,8 @@ impl [T] { pub const unsafe fn as_chunks_unchecked_mut(&mut self) -> &mut [[T; N]] { assert_unsafe_precondition!( check_language_ub, - "slice::as_chunks_unchecked requires `N != 0` and the slice to split exactly into `N`-element chunks", + "slice::as_chunks_unchecked requires `N != 0` and the slice to split exactly into `N`-element chunks \ + (N:{n}, len:{len})", (n: usize = N, len: usize = self.len()) => n != 0 && len % n == 0 ); // SAFETY: Caller must guarantee that `N` is nonzero and exactly divides the slice length @@ -2003,7 +2006,8 @@ impl [T] { assert_unsafe_precondition!( check_library_ub, - "slice::split_at_unchecked requires the index to be within the slice", + "slice::split_at_unchecked requires the index to be within the slice \ + (mid:{mid}, len:{len})", (mid: usize = mid, len: usize = len) => mid <= len, ); @@ -2052,7 +2056,8 @@ impl [T] { assert_unsafe_precondition!( check_library_ub, - "slice::split_at_mut_unchecked requires the index to be within the slice", + "slice::split_at_mut_unchecked requires the index to be within the slice \ + (mid:{mid}, len:{len})", (mid: usize = mid, len: usize = len) => mid <= len, ); diff --git a/library/core/src/slice/raw.rs b/library/core/src/slice/raw.rs index 319b76899bf8e..6f16ee65738d1 100644 --- a/library/core/src/slice/raw.rs +++ b/library/core/src/slice/raw.rs @@ -125,7 +125,8 @@ pub const unsafe fn from_raw_parts<'a, T>(data: *const T, len: usize) -> &'a [T] unsafe { ub_checks::assert_unsafe_precondition!( check_language_ub, - "slice::from_raw_parts requires the pointer to be aligned and non-null, and the total size of the slice not to exceed `isize::MAX`", + "slice::from_raw_parts requires the pointer to be aligned and non-null, and the total size of the slice not to exceed `isize::MAX` \ + (data:{data:?}, size:{size}, align:{align}, len:{len})", ( data: *mut () = data as *mut (), size: usize = size_of::(), @@ -179,7 +180,8 @@ pub const unsafe fn from_raw_parts_mut<'a, T>(data: *mut T, len: usize) -> &'a m unsafe { ub_checks::assert_unsafe_precondition!( check_language_ub, - "slice::from_raw_parts_mut requires the pointer to be aligned and non-null, and the total size of the slice not to exceed `isize::MAX`", + "slice::from_raw_parts_mut requires the pointer to be aligned and non-null, and the total size of the slice not to exceed `isize::MAX` \ + (data:{data:?}, size:{size}, align:{align}, len:{len})", ( data: *mut () = data as *mut (), size: usize = size_of::(), diff --git a/library/core/src/str/traits.rs b/library/core/src/str/traits.rs index 77c70b978fd15..bfe711dab2a5f 100644 --- a/library/core/src/str/traits.rs +++ b/library/core/src/str/traits.rs @@ -197,7 +197,8 @@ unsafe impl SliceIndex for ops::Range { // `str::get_unchecked` without adding a special function // to `SliceIndex` just for this. check_library_ub, - "str::get_unchecked requires that the range is within the string slice", + "str::get_unchecked requires that the range is within the string slice \ + (range:{start}..{end}, len:{len})", ( start: usize = self.start, end: usize = self.end, @@ -218,7 +219,8 @@ unsafe impl SliceIndex for ops::Range { assert_unsafe_precondition!( check_library_ub, - "str::get_unchecked_mut requires that the range is within the string slice", + "str::get_unchecked_mut requires that the range is within the string slice \ + (range:{start}..{end}, len:{len})", ( start: usize = self.start, end: usize = self.end, @@ -299,7 +301,8 @@ unsafe impl SliceIndex for range::Range { // `str::get_unchecked` without adding a special function // to `SliceIndex` just for this. check_library_ub, - "str::get_unchecked requires that the range is within the string slice", + "str::get_unchecked requires that the range is within the string slice \ + (range:{start}..{end}, len:{len})", ( start: usize = self.start, end: usize = self.end, @@ -320,7 +323,8 @@ unsafe impl SliceIndex for range::Range { assert_unsafe_precondition!( check_library_ub, - "str::get_unchecked_mut requires that the range is within the string slice", + "str::get_unchecked_mut requires that the range is within the string slice \ + (range:{start}..{end}, len:{len})", ( start: usize = self.start, end: usize = self.end, diff --git a/library/core/src/ub_checks.rs b/library/core/src/ub_checks.rs index b289f6026ffcb..8042cfbd8df75 100644 --- a/library/core/src/ub_checks.rs +++ b/library/core/src/ub_checks.rs @@ -63,12 +63,17 @@ macro_rules! assert_unsafe_precondition { #[rustc_no_mir_inline] #[inline] #[rustc_nounwind] + #[rustc_allow_const_fn_unstable(const_eval_select)] const fn precondition_check($($name:$ty),*) { - if !$e { - ::core::panicking::panic_nounwind( - concat!("unsafe precondition(s) violated: ", $message) - ); - } + if $e { return; } + crate::intrinsics::const_eval_select!( + @capture { $($name: $ty),* }: + if const { + ::core::panicking::panic_nounwind($message); + } else #[allow(unused)] { + ::core::panicking::panic_nounwind_fmt(format_args!($message), false); + } + ) } if ::core::ub_checks::$kind() { diff --git a/tests/ui/precondition-checks/alignment.rs b/tests/ui/precondition-checks/alignment.rs index 92400528fa07f..1e9048b6b9838 100644 --- a/tests/ui/precondition-checks/alignment.rs +++ b/tests/ui/precondition-checks/alignment.rs @@ -1,6 +1,6 @@ //@ run-fail //@ compile-flags: -Copt-level=3 -Cdebug-assertions=no -Zub-checks=yes -//@ error-pattern: unsafe precondition(s) violated: Alignment::new_unchecked requires +//@ error-pattern: Alignment::new_unchecked requires #![feature(ptr_alignment_type)] diff --git a/tests/ui/precondition-checks/ascii-char-digit_unchecked.rs b/tests/ui/precondition-checks/ascii-char-digit_unchecked.rs index 30c6f79fb08f0..c0007edba00ab 100644 --- a/tests/ui/precondition-checks/ascii-char-digit_unchecked.rs +++ b/tests/ui/precondition-checks/ascii-char-digit_unchecked.rs @@ -1,6 +1,6 @@ //@ run-fail //@ compile-flags: -Copt-level=3 -Cdebug-assertions=no -Zub-checks=yes -//@ error-pattern: unsafe precondition(s) violated: `ascii::Char::digit_unchecked` input cannot exceed 9 +//@ error-pattern: `ascii::Char::digit_unchecked` input cannot exceed 9 #![feature(ascii_char)] diff --git a/tests/ui/precondition-checks/assert_unchecked.rs b/tests/ui/precondition-checks/assert_unchecked.rs index 22b2b41455021..805c43d9ce8b3 100644 --- a/tests/ui/precondition-checks/assert_unchecked.rs +++ b/tests/ui/precondition-checks/assert_unchecked.rs @@ -1,6 +1,6 @@ //@ run-fail //@ compile-flags: -Copt-level=3 -Cdebug-assertions=no -Zub-checks=yes -//@ error-pattern: unsafe precondition(s) violated: hint::assert_unchecked must never be called when the condition is false +//@ error-pattern: hint::assert_unchecked must never be called when the condition is false fn main() { unsafe { diff --git a/tests/ui/precondition-checks/char-from_u32_unchecked.rs b/tests/ui/precondition-checks/char-from_u32_unchecked.rs index d950f20c77208..90a0323f0581a 100644 --- a/tests/ui/precondition-checks/char-from_u32_unchecked.rs +++ b/tests/ui/precondition-checks/char-from_u32_unchecked.rs @@ -1,6 +1,6 @@ //@ run-fail //@ compile-flags: -Copt-level=3 -Cdebug-assertions=no -Zub-checks=yes -//@ error-pattern: unsafe precondition(s) violated: invalid value for `char` +//@ error-pattern: invalid value for `char` fn main() { unsafe { diff --git a/tests/ui/precondition-checks/copy-nonoverlapping.rs b/tests/ui/precondition-checks/copy-nonoverlapping.rs index 81018e4bff3e5..cf54ade090c97 100644 --- a/tests/ui/precondition-checks/copy-nonoverlapping.rs +++ b/tests/ui/precondition-checks/copy-nonoverlapping.rs @@ -1,6 +1,6 @@ //@ run-fail //@ compile-flags: -Copt-level=3 -Cdebug-assertions=no -Zub-checks=yes -//@ error-pattern: unsafe precondition(s) violated: ptr::copy_nonoverlapping requires +//@ error-pattern: ptr::copy_nonoverlapping requires //@ revisions: null_src null_dst misaligned_src misaligned_dst overlapping use std::ptr; diff --git a/tests/ui/precondition-checks/copy.rs b/tests/ui/precondition-checks/copy.rs index 694853f950ab5..1c91718f373c8 100644 --- a/tests/ui/precondition-checks/copy.rs +++ b/tests/ui/precondition-checks/copy.rs @@ -1,6 +1,6 @@ //@ run-fail //@ compile-flags: -Copt-level=3 -Cdebug-assertions=no -Zub-checks=yes -//@ error-pattern: unsafe precondition(s) violated: ptr::copy requires +//@ error-pattern: ptr::copy requires //@ revisions: null_src null_dst misaligned_src misaligned_dst use std::ptr; diff --git a/tests/ui/precondition-checks/layout.rs b/tests/ui/precondition-checks/layout.rs index 4ee66cc932886..5b068487d92e0 100644 --- a/tests/ui/precondition-checks/layout.rs +++ b/tests/ui/precondition-checks/layout.rs @@ -1,6 +1,6 @@ //@ run-fail //@ compile-flags: -Copt-level=3 -Cdebug-assertions=no -Zub-checks=yes -//@ error-pattern: unsafe precondition(s) violated: Layout::from_size_align_unchecked requires +//@ error-pattern: Layout::from_size_align_unchecked requires //@ revisions: toolarge badalign fn main() { diff --git a/tests/ui/precondition-checks/nonnull.rs b/tests/ui/precondition-checks/nonnull.rs index 6b8edd4e5825e..1be965000bd7d 100644 --- a/tests/ui/precondition-checks/nonnull.rs +++ b/tests/ui/precondition-checks/nonnull.rs @@ -1,6 +1,6 @@ //@ run-fail //@ compile-flags: -Copt-level=3 -Cdebug-assertions=no -Zub-checks=yes -//@ error-pattern: unsafe precondition(s) violated: NonNull::new_unchecked requires +//@ error-pattern: NonNull::new_unchecked requires fn main() { unsafe { diff --git a/tests/ui/precondition-checks/nonzero-from_mut_unchecked.rs b/tests/ui/precondition-checks/nonzero-from_mut_unchecked.rs index 46ce7dc356fe2..4239e5aede2d5 100644 --- a/tests/ui/precondition-checks/nonzero-from_mut_unchecked.rs +++ b/tests/ui/precondition-checks/nonzero-from_mut_unchecked.rs @@ -1,6 +1,6 @@ //@ run-fail //@ compile-flags: -Copt-level=3 -Cdebug-assertions=no -Zub-checks=yes -//@ error-pattern: unsafe precondition(s) violated: NonZero::from_mut_unchecked requires +//@ error-pattern: NonZero::from_mut_unchecked requires #![feature(nonzero_from_mut)] diff --git a/tests/ui/precondition-checks/nonzero-new_unchecked.rs b/tests/ui/precondition-checks/nonzero-new_unchecked.rs index 7827a42844fd4..bd327376cd774 100644 --- a/tests/ui/precondition-checks/nonzero-new_unchecked.rs +++ b/tests/ui/precondition-checks/nonzero-new_unchecked.rs @@ -1,6 +1,6 @@ //@ run-fail //@ compile-flags: -Copt-level=3 -Cdebug-assertions=no -Zub-checks=yes -//@ error-pattern: unsafe precondition(s) violated: NonZero::new_unchecked requires +//@ error-pattern: NonZero::new_unchecked requires fn main() { unsafe { diff --git a/tests/ui/precondition-checks/read.rs b/tests/ui/precondition-checks/read.rs index ab9921a0ceebe..fd6d900b019c8 100644 --- a/tests/ui/precondition-checks/read.rs +++ b/tests/ui/precondition-checks/read.rs @@ -1,6 +1,6 @@ //@ run-fail //@ compile-flags: -Copt-level=3 -Cdebug-assertions=no -Zub-checks=yes -//@ error-pattern: unsafe precondition(s) violated: ptr::read requires +//@ error-pattern: ptr::read requires //@ revisions: null misaligned //@ ignore-test diff --git a/tests/ui/precondition-checks/read_volatile.rs b/tests/ui/precondition-checks/read_volatile.rs index e14881d029037..b9eeb01f825be 100644 --- a/tests/ui/precondition-checks/read_volatile.rs +++ b/tests/ui/precondition-checks/read_volatile.rs @@ -1,6 +1,6 @@ //@ run-fail //@ compile-flags: -Copt-level=3 -Cdebug-assertions=no -Zub-checks=yes -//@ error-pattern: unsafe precondition(s) violated: ptr::read_volatile requires +//@ error-pattern: ptr::read_volatile requires //@ revisions: null misaligned use std::ptr; diff --git a/tests/ui/precondition-checks/replace.rs b/tests/ui/precondition-checks/replace.rs index 2808cee7b64b1..77f8e2356b72f 100644 --- a/tests/ui/precondition-checks/replace.rs +++ b/tests/ui/precondition-checks/replace.rs @@ -1,6 +1,6 @@ //@ run-fail //@ compile-flags: -Copt-level=3 -Cdebug-assertions=no -Zub-checks=yes -//@ error-pattern: unsafe precondition(s) violated: ptr::replace requires +//@ error-pattern: ptr::replace requires //@ revisions: null misaligned use std::ptr; diff --git a/tests/ui/precondition-checks/slice-from-raw-parts-mut.rs b/tests/ui/precondition-checks/slice-from-raw-parts-mut.rs index 3801639e2551b..145b16bb71cf0 100644 --- a/tests/ui/precondition-checks/slice-from-raw-parts-mut.rs +++ b/tests/ui/precondition-checks/slice-from-raw-parts-mut.rs @@ -1,6 +1,6 @@ //@ run-fail //@ compile-flags: -Copt-level=3 -Cdebug-assertions=no -Zub-checks=yes -//@ error-pattern: unsafe precondition(s) violated: slice::from_raw_parts_mut requires +//@ error-pattern: slice::from_raw_parts_mut requires //@ revisions: null misaligned toolarge fn main() { diff --git a/tests/ui/precondition-checks/slice-from-raw-parts.rs b/tests/ui/precondition-checks/slice-from-raw-parts.rs index a3690fa045eb7..ad4021d17a3ea 100644 --- a/tests/ui/precondition-checks/slice-from-raw-parts.rs +++ b/tests/ui/precondition-checks/slice-from-raw-parts.rs @@ -1,6 +1,6 @@ //@ run-fail //@ compile-flags: -Copt-level=3 -Cdebug-assertions=no -Zub-checks=yes -//@ error-pattern: unsafe precondition(s) violated: slice::from_raw_parts requires +//@ error-pattern: slice::from_raw_parts requires //@ revisions: null misaligned toolarge fn main() { diff --git a/tests/ui/precondition-checks/slice-get_unchecked.rs b/tests/ui/precondition-checks/slice-get_unchecked.rs index 1d8188fb9531a..7285c97162c5a 100644 --- a/tests/ui/precondition-checks/slice-get_unchecked.rs +++ b/tests/ui/precondition-checks/slice-get_unchecked.rs @@ -1,6 +1,6 @@ //@ run-fail //@ compile-flags: -Copt-level=3 -Cdebug-assertions=no -Zub-checks=yes -//@ error-pattern: unsafe precondition(s) violated: slice::get_unchecked requires +//@ error-pattern: slice::get_unchecked requires //@ revisions: usize range range_to range_from backwards_range fn main() { diff --git a/tests/ui/precondition-checks/slice-get_unchecked_mut.rs b/tests/ui/precondition-checks/slice-get_unchecked_mut.rs index 34c1454af438d..3500d84602e73 100644 --- a/tests/ui/precondition-checks/slice-get_unchecked_mut.rs +++ b/tests/ui/precondition-checks/slice-get_unchecked_mut.rs @@ -1,6 +1,6 @@ //@ run-fail //@ compile-flags: -Copt-level=3 -Cdebug-assertions=no -Zub-checks=yes -//@ error-pattern: unsafe precondition(s) violated: slice::get_unchecked_mut requires +//@ error-pattern: slice::get_unchecked_mut requires //@ revisions: usize range range_to range_from backwards_range fn main() { diff --git a/tests/ui/precondition-checks/str-get_unchecked.rs b/tests/ui/precondition-checks/str-get_unchecked.rs index 14d17f997ec9b..df3699fd019ed 100644 --- a/tests/ui/precondition-checks/str-get_unchecked.rs +++ b/tests/ui/precondition-checks/str-get_unchecked.rs @@ -1,6 +1,6 @@ //@ run-fail //@ compile-flags: -Copt-level=3 -Cdebug-assertions=no -Zub-checks=yes -//@ error-pattern: unsafe precondition(s) violated: str::get_unchecked requires +//@ error-pattern: str::get_unchecked requires //@ revisions: range range_to range_from backwards_range fn main() { diff --git a/tests/ui/precondition-checks/str-get_unchecked_mut.rs b/tests/ui/precondition-checks/str-get_unchecked_mut.rs index ca1b169005559..353c5dd5b93f0 100644 --- a/tests/ui/precondition-checks/str-get_unchecked_mut.rs +++ b/tests/ui/precondition-checks/str-get_unchecked_mut.rs @@ -1,6 +1,6 @@ //@ run-fail //@ compile-flags: -Copt-level=3 -Cdebug-assertions=no -Zub-checks=yes -//@ error-pattern: unsafe precondition(s) violated: str::get_unchecked_mut requires +//@ error-pattern: str::get_unchecked_mut requires //@ revisions: range range_to range_from backwards_range fn main() { diff --git a/tests/ui/precondition-checks/swap-nonoverlapping.rs b/tests/ui/precondition-checks/swap-nonoverlapping.rs index 52e4a3c870be5..1b59133122036 100644 --- a/tests/ui/precondition-checks/swap-nonoverlapping.rs +++ b/tests/ui/precondition-checks/swap-nonoverlapping.rs @@ -1,6 +1,6 @@ //@ run-fail //@ compile-flags: -Copt-level=3 -Cdebug-assertions=no -Zub-checks=yes -//@ error-pattern: unsafe precondition(s) violated: ptr::swap_nonoverlapping requires +//@ error-pattern: ptr::swap_nonoverlapping requires //@ revisions: null_src null_dst misaligned_src misaligned_dst overlapping use std::ptr; diff --git a/tests/ui/precondition-checks/unchecked_add.rs b/tests/ui/precondition-checks/unchecked_add.rs index f44a6ea32ad8f..a669f625444d9 100644 --- a/tests/ui/precondition-checks/unchecked_add.rs +++ b/tests/ui/precondition-checks/unchecked_add.rs @@ -1,6 +1,6 @@ //@ run-fail //@ compile-flags: -Copt-level=3 -Cdebug-assertions=no -Zub-checks=yes -//@ error-pattern: unsafe precondition(s) violated: u8::unchecked_add cannot overflow +//@ error-pattern: u8::unchecked_add cannot overflow fn main() { unsafe { diff --git a/tests/ui/precondition-checks/unchecked_mul.rs b/tests/ui/precondition-checks/unchecked_mul.rs index 66655dda136e9..eae371b417916 100644 --- a/tests/ui/precondition-checks/unchecked_mul.rs +++ b/tests/ui/precondition-checks/unchecked_mul.rs @@ -1,6 +1,6 @@ //@ run-fail //@ compile-flags: -Copt-level=3 -Cdebug-assertions=no -Zub-checks=yes -//@ error-pattern: unsafe precondition(s) violated: u8::unchecked_add cannot overflow +//@ error-pattern: u8::unchecked_add cannot overflow fn main() { unsafe { diff --git a/tests/ui/precondition-checks/unchecked_shl.rs b/tests/ui/precondition-checks/unchecked_shl.rs index 1c96db0b1ec71..27031219bbd8f 100644 --- a/tests/ui/precondition-checks/unchecked_shl.rs +++ b/tests/ui/precondition-checks/unchecked_shl.rs @@ -1,6 +1,6 @@ //@ run-fail //@ compile-flags: -Copt-level=3 -Cdebug-assertions=no -Zub-checks=yes -//@ error-pattern: unsafe precondition(s) violated: u8::unchecked_shl cannot overflow +//@ error-pattern: u8::unchecked_shl cannot overflow #![feature(unchecked_shifts)] diff --git a/tests/ui/precondition-checks/unchecked_shr.rs b/tests/ui/precondition-checks/unchecked_shr.rs index 4a6d9ffb1d35b..1c43edc105389 100644 --- a/tests/ui/precondition-checks/unchecked_shr.rs +++ b/tests/ui/precondition-checks/unchecked_shr.rs @@ -1,6 +1,6 @@ //@ run-fail //@ compile-flags: -Copt-level=3 -Cdebug-assertions=no -Zub-checks=yes -//@ error-pattern: unsafe precondition(s) violated: u8::unchecked_shr cannot overflow +//@ error-pattern: u8::unchecked_shr cannot overflow #![feature(unchecked_shifts)] diff --git a/tests/ui/precondition-checks/unchecked_sub.rs b/tests/ui/precondition-checks/unchecked_sub.rs index 545dde0e27809..a8d18ec10d4f0 100644 --- a/tests/ui/precondition-checks/unchecked_sub.rs +++ b/tests/ui/precondition-checks/unchecked_sub.rs @@ -1,6 +1,6 @@ //@ run-fail //@ compile-flags: -Copt-level=3 -Cdebug-assertions=no -Zub-checks=yes -//@ error-pattern: unsafe precondition(s) violated: u8::unchecked_sub cannot overflow +//@ error-pattern: u8::unchecked_sub cannot overflow fn main() { unsafe { diff --git a/tests/ui/precondition-checks/unreachable_unchecked.rs b/tests/ui/precondition-checks/unreachable_unchecked.rs index 2435450c4b5a1..19b12cfe9b2e2 100644 --- a/tests/ui/precondition-checks/unreachable_unchecked.rs +++ b/tests/ui/precondition-checks/unreachable_unchecked.rs @@ -1,6 +1,6 @@ //@ run-fail //@ compile-flags: -Copt-level=3 -Cdebug-assertions=no -Zub-checks=yes -//@ error-pattern: unsafe precondition(s) violated: hint::unreachable_unchecked must never be reached +//@ error-pattern: hint::unreachable_unchecked must never be reached fn main() { unsafe { diff --git a/tests/ui/precondition-checks/write.rs b/tests/ui/precondition-checks/write.rs index f76e776fcf35d..36014c4e6132b 100644 --- a/tests/ui/precondition-checks/write.rs +++ b/tests/ui/precondition-checks/write.rs @@ -1,6 +1,6 @@ //@ run-fail //@ compile-flags: -Copt-level=3 -Cdebug-assertions=no -Zub-checks=yes -//@ error-pattern: unsafe precondition(s) violated: ptr::write requires +//@ error-pattern: ptr::write requires //@ revisions: null misaligned //@ ignore-test diff --git a/tests/ui/precondition-checks/write_bytes.rs b/tests/ui/precondition-checks/write_bytes.rs index 3f64be9d1ee14..76a97a9beaa78 100644 --- a/tests/ui/precondition-checks/write_bytes.rs +++ b/tests/ui/precondition-checks/write_bytes.rs @@ -1,6 +1,6 @@ //@ run-fail //@ compile-flags: -Copt-level=3 -Cdebug-assertions=no -Zub-checks=yes -//@ error-pattern: unsafe precondition(s) violated: ptr::write requires +//@ error-pattern: ptr::write requires //@ revisions: null misaligned //@ ignore-test diff --git a/tests/ui/precondition-checks/write_volatile.rs b/tests/ui/precondition-checks/write_volatile.rs index ac0b89b5ecf2a..0b3fb8cd1ea21 100644 --- a/tests/ui/precondition-checks/write_volatile.rs +++ b/tests/ui/precondition-checks/write_volatile.rs @@ -1,6 +1,6 @@ //@ run-fail //@ compile-flags: -Copt-level=3 -Cdebug-assertions=no -Zub-checks=yes -//@ error-pattern: unsafe precondition(s) violated: ptr::write_volatile requires +//@ error-pattern: ptr::write_volatile requires //@ revisions: null misaligned use std::ptr; diff --git a/tests/ui/print_type_sizes/niche-filling.stdout b/tests/ui/print_type_sizes/niche-filling.stdout index eeb5de5324121..13983b69366c1 100644 --- a/tests/ui/print_type_sizes/niche-filling.stdout +++ b/tests/ui/print_type_sizes/niche-filling.stdout @@ -1,3 +1,25 @@ +print-type-size type: `std::fmt::Arguments<'_>`: 48 bytes, alignment: 8 bytes +print-type-size field `.pieces`: 16 bytes +print-type-size field `.args`: 16 bytes +print-type-size field `.fmt`: 16 bytes +print-type-size type: `std::panic::Location<'_>`: 24 bytes, alignment: 8 bytes +print-type-size field `.file`: 16 bytes +print-type-size field `.line`: 4 bytes +print-type-size field `.col`: 4 bytes +print-type-size type: `core::fmt::rt::Argument<'_>`: 16 bytes, alignment: 8 bytes +print-type-size field `.ty`: 16 bytes +print-type-size type: `core::fmt::rt::ArgumentType<'_>`: 16 bytes, alignment: 8 bytes +print-type-size variant `Placeholder`: 16 bytes +print-type-size field `.value`: 8 bytes +print-type-size field `.formatter`: 8 bytes +print-type-size field `._lifetime`: 0 bytes +print-type-size variant `Count`: 16 bytes +print-type-size padding: 8 bytes +print-type-size field `.0`: 8 bytes, alignment: 8 bytes +print-type-size type: `std::option::Option<&[core::fmt::rt::Placeholder]>`: 16 bytes, alignment: 8 bytes +print-type-size variant `Some`: 16 bytes +print-type-size field `.0`: 16 bytes +print-type-size variant `None`: 0 bytes print-type-size type: `IndirectNonZero`: 12 bytes, alignment: 4 bytes print-type-size field `.nested`: 8 bytes print-type-size field `.post`: 2 bytes @@ -34,6 +56,8 @@ print-type-size field `.val`: 4 bytes print-type-size field `.post`: 2 bytes print-type-size field `.pre`: 1 bytes print-type-size end padding: 1 bytes +print-type-size type: `std::ptr::NonNull<()>`: 8 bytes, alignment: 8 bytes +print-type-size field `.pointer`: 8 bytes print-type-size type: `Enum4<(), char, (), ()>`: 4 bytes, alignment: 4 bytes print-type-size variant `Two`: 4 bytes print-type-size field `.0`: 4 bytes @@ -116,3 +140,4 @@ print-type-size discriminant: 1 bytes print-type-size variant `Less`: 0 bytes print-type-size variant `Equal`: 0 bytes print-type-size variant `Greater`: 0 bytes +print-type-size type: `std::marker::PhantomData<&()>`: 0 bytes, alignment: 1 bytes