Skip to content

Commit 2cb786e

Browse files
committed
Include arguments to the precondition check in failure messages
1 parent 124cc92 commit 2cb786e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+153
-75
lines changed

library/core/src/alloc/layout.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ impl Layout {
130130
assert_unsafe_precondition!(
131131
check_library_ub,
132132
"Layout::from_size_align_unchecked requires that align is a power of 2 \
133-
and the rounded-up allocation size does not exceed isize::MAX",
133+
and the rounded-up allocation size does not exceed isize::MAX (size:{size}, align:{align})",
134134
(
135135
size: usize = size,
136136
align: usize = align,

library/core/src/ascii/ascii_char.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -506,7 +506,7 @@ impl AsciiChar {
506506
pub const unsafe fn digit_unchecked(d: u8) -> Self {
507507
assert_unsafe_precondition!(
508508
check_language_ub,
509-
"`ascii::Char::digit_unchecked` input cannot exceed 9.",
509+
"`ascii::Char::digit_unchecked` input cannot exceed 9. (d:{d})",
510510
(d: u8 = d) => d < 10
511511
);
512512

library/core/src/char/convert.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ pub(super) const unsafe fn from_u32_unchecked(i: u32) -> char {
2626
unsafe {
2727
assert_unsafe_precondition!(
2828
check_language_ub,
29-
"invalid value for `char`",
29+
"invalid value for `char` ({i})",
3030
(i: u32 = i) => char_try_from_u32(i).is_ok()
3131
);
3232
transmute(i)

library/core/src/intrinsics/mod.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -4421,7 +4421,8 @@ pub const unsafe fn copy_nonoverlapping<T>(src: *const T, dst: *mut T, count: us
44214421
ub_checks::assert_unsafe_precondition!(
44224422
check_language_ub,
44234423
"ptr::copy_nonoverlapping requires that both pointer arguments are aligned and non-null \
4424-
and the specified memory ranges do not overlap",
4424+
and the specified memory ranges do not overlap \
4425+
(src:{src:?}, dst:{dst:?}, size:{size}, align:{align}, count:{count})",
44254426
(
44264427
src: *const () = src as *const (),
44274428
dst: *mut () = dst as *mut (),
@@ -4530,7 +4531,8 @@ pub const unsafe fn copy<T>(src: *const T, dst: *mut T, count: usize) {
45304531
unsafe {
45314532
ub_checks::assert_unsafe_precondition!(
45324533
check_language_ub,
4533-
"ptr::copy requires that both pointer arguments are aligned and non-null",
4534+
"ptr::copy requires that both pointer arguments are aligned and non-null \
4535+
(src:{src:?}, dst:{dst:?}, align:{align})",
45344536
(
45354537
src: *const () = src as *const (),
45364538
dst: *mut () = dst as *mut (),
@@ -4617,7 +4619,8 @@ pub const unsafe fn write_bytes<T>(dst: *mut T, val: u8, count: usize) {
46174619
unsafe {
46184620
ub_checks::assert_unsafe_precondition!(
46194621
check_language_ub,
4620-
"ptr::write_bytes requires that the destination pointer is aligned and non-null",
4622+
"ptr::write_bytes requires that the destination pointer is aligned and non-null \
4623+
(dst:{addr:?}, align:{align})",
46214624
(
46224625
addr: *const () = dst as *const (),
46234626
align: usize = align_of::<T>(),

library/core/src/num/int_macros.rs

+6
Original file line numberDiff line numberDiff line change
@@ -514,6 +514,7 @@ macro_rules! int_impl {
514514
assert_unsafe_precondition!(
515515
check_language_ub,
516516
concat!(stringify!($SelfT), "::unchecked_add cannot overflow"),
517+
// FIXME: concat! prevents adding formatting
517518
(
518519
lhs: $SelfT = self,
519520
rhs: $SelfT = rhs,
@@ -664,6 +665,7 @@ macro_rules! int_impl {
664665
assert_unsafe_precondition!(
665666
check_language_ub,
666667
concat!(stringify!($SelfT), "::unchecked_sub cannot overflow"),
668+
// FIXME: concat! prevents adding formatting
667669
(
668670
lhs: $SelfT = self,
669671
rhs: $SelfT = rhs,
@@ -814,6 +816,7 @@ macro_rules! int_impl {
814816
assert_unsafe_precondition!(
815817
check_language_ub,
816818
concat!(stringify!($SelfT), "::unchecked_mul cannot overflow"),
819+
// FIXME: concat! prevents adding formatting
817820
(
818821
lhs: $SelfT = self,
819822
rhs: $SelfT = rhs,
@@ -1158,6 +1161,7 @@ macro_rules! int_impl {
11581161
assert_unsafe_precondition!(
11591162
check_language_ub,
11601163
concat!(stringify!($SelfT), "::unchecked_neg cannot overflow"),
1164+
// FIXME: concat! prevents adding formatting
11611165
(
11621166
lhs: $SelfT = self,
11631167
) => !lhs.overflowing_neg().1,
@@ -1286,6 +1290,7 @@ macro_rules! int_impl {
12861290
assert_unsafe_precondition!(
12871291
check_language_ub,
12881292
concat!(stringify!($SelfT), "::unchecked_shl cannot overflow"),
1293+
// FIXME: concat! prevents adding formatting
12891294
(
12901295
rhs: u32 = rhs,
12911296
) => rhs < <$ActualT>::BITS,
@@ -1407,6 +1412,7 @@ macro_rules! int_impl {
14071412
assert_unsafe_precondition!(
14081413
check_language_ub,
14091414
concat!(stringify!($SelfT), "::unchecked_shr cannot overflow"),
1415+
// FIXME: concat! prevents adding formatting
14101416
(
14111417
rhs: u32 = rhs,
14121418
) => rhs < <$ActualT>::BITS,

library/core/src/num/nonzero.rs

+2
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,7 @@ where
394394
ub_checks::assert_unsafe_precondition!(
395395
check_language_ub,
396396
"NonZero::new_unchecked requires the argument to be non-zero",
397+
// FIXME: Can't print n here because of how the check is written
397398
() => false,
398399
);
399400
intrinsics::unreachable()
@@ -434,6 +435,7 @@ where
434435
ub_checks::assert_unsafe_precondition!(
435436
check_library_ub,
436437
"NonZero::from_mut_unchecked requires the argument to dereference as non-zero",
438+
// FIXME: Can't print n here because of how the check is written
437439
() => false,
438440
);
439441
intrinsics::unreachable()

library/core/src/num/uint_macros.rs

+5
Original file line numberDiff line numberDiff line change
@@ -561,6 +561,7 @@ macro_rules! uint_impl {
561561
assert_unsafe_precondition!(
562562
check_language_ub,
563563
concat!(stringify!($SelfT), "::unchecked_add cannot overflow"),
564+
// FIXME: concat! prevents adding formatting
564565
(
565566
lhs: $SelfT = self,
566567
rhs: $SelfT = rhs,
@@ -751,6 +752,7 @@ macro_rules! uint_impl {
751752
assert_unsafe_precondition!(
752753
check_language_ub,
753754
concat!(stringify!($SelfT), "::unchecked_sub cannot overflow"),
755+
// FIXME: concat! prevents adding formatting
754756
(
755757
lhs: $SelfT = self,
756758
rhs: $SelfT = rhs,
@@ -934,6 +936,7 @@ macro_rules! uint_impl {
934936
assert_unsafe_precondition!(
935937
check_language_ub,
936938
concat!(stringify!($SelfT), "::unchecked_mul cannot overflow"),
939+
// FIXME: concat! prevents adding formatting
937940
(
938941
lhs: $SelfT = self,
939942
rhs: $SelfT = rhs,
@@ -1548,6 +1551,7 @@ macro_rules! uint_impl {
15481551
assert_unsafe_precondition!(
15491552
check_language_ub,
15501553
concat!(stringify!($SelfT), "::unchecked_shl cannot overflow"),
1554+
// FIXME: concat! prevents adding formatting
15511555
(
15521556
rhs: u32 = rhs,
15531557
) => rhs < <$ActualT>::BITS,
@@ -1669,6 +1673,7 @@ macro_rules! uint_impl {
16691673
assert_unsafe_precondition!(
16701674
check_language_ub,
16711675
concat!(stringify!($SelfT), "::unchecked_shr cannot overflow"),
1676+
// FIXME: concat! prevents adding formatting
16721677
(
16731678
rhs: u32 = rhs,
16741679
) => rhs < <$ActualT>::BITS,

library/core/src/ops/index_range.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ impl IndexRange {
2121
pub(crate) const unsafe fn new_unchecked(start: usize, end: usize) -> Self {
2222
ub_checks::assert_unsafe_precondition!(
2323
check_library_ub,
24-
"IndexRange::new_unchecked requires `start <= end`",
24+
"IndexRange::new_unchecked requires `start <= end` \
25+
(start:{start}, end:{end})",
2526
(start: usize = start, end: usize = end) => start <= end,
2627
);
2728
IndexRange { start, end }

library/core/src/ptr/alignment.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,8 @@ impl Alignment {
7676
pub const unsafe fn new_unchecked(align: usize) -> Self {
7777
assert_unsafe_precondition!(
7878
check_language_ub,
79-
"Alignment::new_unchecked requires a power of two",
79+
"Alignment::new_unchecked requires a power of two \
80+
(align:{align})",
8081
(align: usize = align) => align.is_power_of_two()
8182
);
8283

library/core/src/ptr/const_ptr.rs

+8-4
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,8 @@ impl<T: ?Sized> *const T {
443443

444444
ub_checks::assert_unsafe_precondition!(
445445
check_language_ub,
446-
"ptr::offset requires the address calculation to not overflow",
446+
"ptr::offset requires the address calculation to not overflow \
447+
(ptr:{this:?}, count:{count}, size:{size})",
447448
(
448449
this: *const () = self as *const (),
449450
count: isize = count,
@@ -789,7 +790,8 @@ impl<T: ?Sized> *const T {
789790

790791
ub_checks::assert_unsafe_precondition!(
791792
check_language_ub,
792-
"ptr::sub_ptr requires `self >= origin`",
793+
"ptr::sub_ptr requires `self >= origin` \
794+
(self:{this:?}, origin:{origin:?})",
793795
(
794796
this: *const () = self as *const (),
795797
origin: *const () = origin as *const (),
@@ -955,7 +957,8 @@ impl<T: ?Sized> *const T {
955957
#[cfg(debug_assertions)] // Expensive, and doesn't catch much in the wild.
956958
ub_checks::assert_unsafe_precondition!(
957959
check_language_ub,
958-
"ptr::add requires that the address calculation does not overflow",
960+
"ptr::add requires that the address calculation does not overflow \
961+
(self:{this:?}, count:{count}, size:{size})",
959962
(
960963
this: *const () = self as *const (),
961964
count: usize = count,
@@ -1060,7 +1063,8 @@ impl<T: ?Sized> *const T {
10601063
#[cfg(debug_assertions)] // Expensive, and doesn't catch much in the wild.
10611064
ub_checks::assert_unsafe_precondition!(
10621065
check_language_ub,
1063-
"ptr::sub requires that the address calculation does not overflow",
1066+
"ptr::sub requires that the address calculation does not overflow \
1067+
(self:{this:?}, count:{count}, size:{size})",
10641068
(
10651069
this: *const () = self as *const (),
10661070
count: usize = count,

library/core/src/ptr/mod.rs

+12-6
Original file line numberDiff line numberDiff line change
@@ -1072,7 +1072,8 @@ pub const unsafe fn swap_nonoverlapping<T>(x: *mut T, y: *mut T, count: usize) {
10721072
ub_checks::assert_unsafe_precondition!(
10731073
check_language_ub,
10741074
"ptr::swap_nonoverlapping requires that both pointer arguments are aligned and non-null \
1075-
and the specified memory ranges do not overlap",
1075+
and the specified memory ranges do not overlap \
1076+
(x:{x:?}, y:{y:?}, size:{size}, align:{align}, count:{count})",
10761077
(
10771078
x: *mut () = x as *mut (),
10781079
y: *mut () = y as *mut (),
@@ -1217,7 +1218,8 @@ pub const unsafe fn replace<T>(dst: *mut T, src: T) -> T {
12171218
unsafe {
12181219
ub_checks::assert_unsafe_precondition!(
12191220
check_language_ub,
1220-
"ptr::replace requires that the pointer argument is aligned and non-null",
1221+
"ptr::replace requires that the pointer argument is aligned and non-null\
1222+
(dst:{addr:?}, (align:{align}))",
12211223
(
12221224
addr: *const () = dst as *const (),
12231225
align: usize = align_of::<T>(),
@@ -1370,7 +1372,8 @@ pub const unsafe fn read<T>(src: *const T) -> T {
13701372
#[cfg(debug_assertions)] // Too expensive to always enable (for now?)
13711373
ub_checks::assert_unsafe_precondition!(
13721374
check_language_ub,
1373-
"ptr::read requires that the pointer argument is aligned and non-null",
1375+
"ptr::read requires that the pointer argument is aligned and non-null \
1376+
(src:{addr:?}, align:{align})",
13741377
(
13751378
addr: *const () = src as *const (),
13761379
align: usize = align_of::<T>(),
@@ -1572,7 +1575,8 @@ pub const unsafe fn write<T>(dst: *mut T, src: T) {
15721575
#[cfg(debug_assertions)] // Too expensive to always enable (for now?)
15731576
ub_checks::assert_unsafe_precondition!(
15741577
check_language_ub,
1575-
"ptr::write requires that the pointer argument is aligned and non-null",
1578+
"ptr::write requires that the pointer argument is aligned and non-null \
1579+
(dst:{addr:?}, align:{align})",
15761580
(
15771581
addr: *mut () = dst as *mut (),
15781582
align: usize = align_of::<T>(),
@@ -1742,7 +1746,8 @@ pub unsafe fn read_volatile<T>(src: *const T) -> T {
17421746
unsafe {
17431747
ub_checks::assert_unsafe_precondition!(
17441748
check_language_ub,
1745-
"ptr::read_volatile requires that the pointer argument is aligned and non-null",
1749+
"ptr::read_volatile requires that the pointer argument is aligned and non-null \
1750+
(src:{addr:?}, align:{align})",
17461751
(
17471752
addr: *const () = src as *const (),
17481753
align: usize = align_of::<T>(),
@@ -1822,7 +1827,8 @@ pub unsafe fn write_volatile<T>(dst: *mut T, src: T) {
18221827
unsafe {
18231828
ub_checks::assert_unsafe_precondition!(
18241829
check_language_ub,
1825-
"ptr::write_volatile requires that the pointer argument is aligned and non-null",
1830+
"ptr::write_volatile requires that the pointer argument is aligned and non-null \
1831+
(dst:{addr:?}, align:{align})",
18261832
(
18271833
addr: *mut () = dst as *mut (),
18281834
align: usize = align_of::<T>(),

library/core/src/ptr/mut_ptr.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,8 @@ impl<T: ?Sized> *mut T {
439439

440440
ub_checks::assert_unsafe_precondition!(
441441
check_language_ub,
442-
"ptr::offset requires the address calculation to not overflow",
442+
"ptr::offset requires the address calculation to not overflow \
443+
(self:{this:?}, count:{count}, size:{size})",
443444
(
444445
this: *const () = self as *const (),
445446
count: isize = count,
@@ -1045,7 +1046,8 @@ impl<T: ?Sized> *mut T {
10451046
#[cfg(debug_assertions)] // Expensive, and doesn't catch much in the wild.
10461047
ub_checks::assert_unsafe_precondition!(
10471048
check_language_ub,
1048-
"ptr::add requires that the address calculation does not overflow",
1049+
"ptr::add requires that the address calculation does not overflow \
1050+
(self:{this:?}, count:{count}, size:{size})",
10491051
(
10501052
this: *const () = self as *const (),
10511053
count: usize = count,
@@ -1150,7 +1152,8 @@ impl<T: ?Sized> *mut T {
11501152
#[cfg(debug_assertions)] // Expensive, and doesn't catch much in the wild.
11511153
ub_checks::assert_unsafe_precondition!(
11521154
check_language_ub,
1153-
"ptr::sub requires that the address calculation does not overflow",
1155+
"ptr::sub requires that the address calculation does not overflow \
1156+
(self:{this:?}, count:{count}, size:{size})",
11541157
(
11551158
this: *const () = self as *const (),
11561159
count: usize = count,

library/core/src/ptr/non_null.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ impl<T: ?Sized> NonNull<T> {
222222
unsafe {
223223
assert_unsafe_precondition!(
224224
check_language_ub,
225-
"NonNull::new_unchecked requires that the pointer is non-null",
225+
"NonNull::new_unchecked requires that the pointer is non-null (ptr:{ptr:?})",
226226
(ptr: *mut () = ptr as *mut ()) => !ptr.is_null()
227227
);
228228
NonNull { pointer: ptr as _ }

library/core/src/slice/index.rs

+14-8
Original file line numberDiff line numberDiff line change
@@ -242,8 +242,9 @@ unsafe impl<T> SliceIndex<[T]> for usize {
242242
unsafe fn get_unchecked(self, slice: *const [T]) -> *const T {
243243
assert_unsafe_precondition!(
244244
check_language_ub,
245-
"slice::get_unchecked requires that the index is within the slice",
246-
(this: usize = self, len: usize = slice.len()) => this < len
245+
"slice::get_unchecked requires that the index is within the slice \
246+
(index:{index}, len:{len})",
247+
(index: usize = self, len: usize = slice.len()) => index < len
247248
);
248249
// SAFETY: the caller guarantees that `slice` is not dangling, so it
249250
// cannot be longer than `isize::MAX`. They also guarantee that
@@ -261,8 +262,9 @@ unsafe impl<T> SliceIndex<[T]> for usize {
261262
unsafe fn get_unchecked_mut(self, slice: *mut [T]) -> *mut T {
262263
assert_unsafe_precondition!(
263264
check_library_ub,
264-
"slice::get_unchecked_mut requires that the index is within the slice",
265-
(this: usize = self, len: usize = slice.len()) => this < len
265+
"slice::get_unchecked_mut requires that the index is within the slice \
266+
(index:{index}, len:{len})",
267+
(index: usize = self, len: usize = slice.len()) => index < len
266268
);
267269
// SAFETY: see comments for `get_unchecked` above.
268270
unsafe { get_mut_noubcheck(slice, self) }
@@ -310,7 +312,8 @@ unsafe impl<T> SliceIndex<[T]> for ops::IndexRange {
310312
unsafe fn get_unchecked(self, slice: *const [T]) -> *const [T] {
311313
assert_unsafe_precondition!(
312314
check_library_ub,
313-
"slice::get_unchecked requires that the index is within the slice",
315+
"slice::get_unchecked requires that the index is within the slice \
316+
(end:{end}, len:{len})",
314317
(end: usize = self.end(), len: usize = slice.len()) => end <= len
315318
);
316319
// SAFETY: the caller guarantees that `slice` is not dangling, so it
@@ -324,7 +327,8 @@ unsafe impl<T> SliceIndex<[T]> for ops::IndexRange {
324327
unsafe fn get_unchecked_mut(self, slice: *mut [T]) -> *mut [T] {
325328
assert_unsafe_precondition!(
326329
check_library_ub,
327-
"slice::get_unchecked_mut requires that the index is within the slice",
330+
"slice::get_unchecked_mut requires that the index is within the slice \
331+
(end:{end}, len:{len})",
328332
(end: usize = self.end(), len: usize = slice.len()) => end <= len
329333
);
330334

@@ -389,7 +393,8 @@ unsafe impl<T> SliceIndex<[T]> for ops::Range<usize> {
389393
unsafe fn get_unchecked(self, slice: *const [T]) -> *const [T] {
390394
assert_unsafe_precondition!(
391395
check_library_ub,
392-
"slice::get_unchecked requires that the range is within the slice",
396+
"slice::get_unchecked requires that the range is within the slice \
397+
(range:{start}..{end}, len:{len})",
393398
(
394399
start: usize = self.start,
395400
end: usize = self.end,
@@ -413,7 +418,8 @@ unsafe impl<T> SliceIndex<[T]> for ops::Range<usize> {
413418
unsafe fn get_unchecked_mut(self, slice: *mut [T]) -> *mut [T] {
414419
assert_unsafe_precondition!(
415420
check_library_ub,
416-
"slice::get_unchecked_mut requires that the range is within the slice",
421+
"slice::get_unchecked_mut requires that the range is within the slice \
422+
(range:{start}..{end}, len:{len})",
417423
(
418424
start: usize = self.start,
419425
end: usize = self.end,

0 commit comments

Comments
 (0)