Skip to content

Commit a1c1f54

Browse files
committed
Include arguments to the precondition check in failure messages
1 parent f7b4354 commit a1c1f54

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
@@ -3718,7 +3718,8 @@ pub const unsafe fn copy_nonoverlapping<T>(src: *const T, dst: *mut T, count: us
37183718
ub_checks::assert_unsafe_precondition!(
37193719
check_language_ub,
37203720
"ptr::copy_nonoverlapping requires that both pointer arguments are aligned and non-null \
3721-
and the specified memory ranges do not overlap",
3721+
and the specified memory ranges do not overlap \
3722+
(src:{src:?}, dst:{dst:?}, size:{size}, align:{align}, count:{count})",
37223723
(
37233724
src: *const () = src as *const (),
37243725
dst: *mut () = dst as *mut (),
@@ -3820,7 +3821,8 @@ pub const unsafe fn copy<T>(src: *const T, dst: *mut T, count: usize) {
38203821
unsafe {
38213822
ub_checks::assert_unsafe_precondition!(
38223823
check_language_ub,
3823-
"ptr::copy requires that both pointer arguments are aligned and non-null",
3824+
"ptr::copy requires that both pointer arguments are aligned and non-null \
3825+
(src:{src:?}, dst:{dst:?}, align:{align})",
38243826
(
38253827
src: *const () = src as *const (),
38263828
dst: *mut () = dst as *mut (),
@@ -3900,7 +3902,8 @@ pub const unsafe fn write_bytes<T>(dst: *mut T, val: u8, count: usize) {
39003902
unsafe {
39013903
ub_checks::assert_unsafe_precondition!(
39023904
check_language_ub,
3903-
"ptr::write_bytes requires that the destination pointer is aligned and non-null",
3905+
"ptr::write_bytes requires that the destination pointer is aligned and non-null \
3906+
(dst:{addr:?}, align:{align})",
39043907
(
39053908
addr: *const () = dst as *const (),
39063909
align: usize = align_of::<T>(),

library/core/src/num/int_macros.rs

+6
Original file line numberDiff line numberDiff line change
@@ -560,6 +560,7 @@ macro_rules! int_impl {
560560
assert_unsafe_precondition!(
561561
check_language_ub,
562562
concat!(stringify!($SelfT), "::unchecked_add cannot overflow"),
563+
// FIXME: concat! prevents adding formatting
563564
(
564565
lhs: $SelfT = self,
565566
rhs: $SelfT = rhs,
@@ -710,6 +711,7 @@ macro_rules! int_impl {
710711
assert_unsafe_precondition!(
711712
check_language_ub,
712713
concat!(stringify!($SelfT), "::unchecked_sub cannot overflow"),
714+
// FIXME: concat! prevents adding formatting
713715
(
714716
lhs: $SelfT = self,
715717
rhs: $SelfT = rhs,
@@ -860,6 +862,7 @@ macro_rules! int_impl {
860862
assert_unsafe_precondition!(
861863
check_language_ub,
862864
concat!(stringify!($SelfT), "::unchecked_mul cannot overflow"),
865+
// FIXME: concat! prevents adding formatting
863866
(
864867
lhs: $SelfT = self,
865868
rhs: $SelfT = rhs,
@@ -1204,6 +1207,7 @@ macro_rules! int_impl {
12041207
assert_unsafe_precondition!(
12051208
check_language_ub,
12061209
concat!(stringify!($SelfT), "::unchecked_neg cannot overflow"),
1210+
// FIXME: concat! prevents adding formatting
12071211
(
12081212
lhs: $SelfT = self,
12091213
) => !lhs.overflowing_neg().1,
@@ -1332,6 +1336,7 @@ macro_rules! int_impl {
13321336
assert_unsafe_precondition!(
13331337
check_language_ub,
13341338
concat!(stringify!($SelfT), "::unchecked_shl cannot overflow"),
1339+
// FIXME: concat! prevents adding formatting
13351340
(
13361341
rhs: u32 = rhs,
13371342
) => rhs < <$ActualT>::BITS,
@@ -1453,6 +1458,7 @@ macro_rules! int_impl {
14531458
assert_unsafe_precondition!(
14541459
check_language_ub,
14551460
concat!(stringify!($SelfT), "::unchecked_shr cannot overflow"),
1461+
// FIXME: concat! prevents adding formatting
14561462
(
14571463
rhs: u32 = rhs,
14581464
) => rhs < <$ActualT>::BITS,

library/core/src/num/nonzero.rs

+2
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,7 @@ where
397397
ub_checks::assert_unsafe_precondition!(
398398
check_language_ub,
399399
"NonZero::new_unchecked requires the argument to be non-zero",
400+
// FIXME: Can't print n here because of how the check is written
400401
() => false,
401402
);
402403
intrinsics::unreachable()
@@ -437,6 +438,7 @@ where
437438
ub_checks::assert_unsafe_precondition!(
438439
check_library_ub,
439440
"NonZero::from_mut_unchecked requires the argument to dereference as non-zero",
441+
// FIXME: Can't print n here because of how the check is written
440442
() => false,
441443
);
442444
intrinsics::unreachable()

library/core/src/num/uint_macros.rs

+5
Original file line numberDiff line numberDiff line change
@@ -606,6 +606,7 @@ macro_rules! uint_impl {
606606
assert_unsafe_precondition!(
607607
check_language_ub,
608608
concat!(stringify!($SelfT), "::unchecked_add cannot overflow"),
609+
// FIXME: concat! prevents adding formatting
609610
(
610611
lhs: $SelfT = self,
611612
rhs: $SelfT = rhs,
@@ -796,6 +797,7 @@ macro_rules! uint_impl {
796797
assert_unsafe_precondition!(
797798
check_language_ub,
798799
concat!(stringify!($SelfT), "::unchecked_sub cannot overflow"),
800+
// FIXME: concat! prevents adding formatting
799801
(
800802
lhs: $SelfT = self,
801803
rhs: $SelfT = rhs,
@@ -979,6 +981,7 @@ macro_rules! uint_impl {
979981
assert_unsafe_precondition!(
980982
check_language_ub,
981983
concat!(stringify!($SelfT), "::unchecked_mul cannot overflow"),
984+
// FIXME: concat! prevents adding formatting
982985
(
983986
lhs: $SelfT = self,
984987
rhs: $SelfT = rhs,
@@ -1593,6 +1596,7 @@ macro_rules! uint_impl {
15931596
assert_unsafe_precondition!(
15941597
check_language_ub,
15951598
concat!(stringify!($SelfT), "::unchecked_shl cannot overflow"),
1599+
// FIXME: concat! prevents adding formatting
15961600
(
15971601
rhs: u32 = rhs,
15981602
) => rhs < <$ActualT>::BITS,
@@ -1714,6 +1718,7 @@ macro_rules! uint_impl {
17141718
assert_unsafe_precondition!(
17151719
check_language_ub,
17161720
concat!(stringify!($SelfT), "::unchecked_shr cannot overflow"),
1721+
// FIXME: concat! prevents adding formatting
17171722
(
17181723
rhs: u32 = rhs,
17191724
) => 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,
@@ -786,7 +787,8 @@ impl<T: ?Sized> *const T {
786787

787788
ub_checks::assert_unsafe_precondition!(
788789
check_language_ub,
789-
"ptr::offset_from_unsigned requires `self >= origin`",
790+
"ptr::offset_from_unsigned requires `self >= origin` \
791+
(self:{this:?}, origin:{origin:?})",
790792
(
791793
this: *const () = self as *const (),
792794
origin: *const () = origin as *const (),
@@ -952,7 +954,8 @@ impl<T: ?Sized> *const T {
952954
#[cfg(debug_assertions)] // Expensive, and doesn't catch much in the wild.
953955
ub_checks::assert_unsafe_precondition!(
954956
check_language_ub,
955-
"ptr::add requires that the address calculation does not overflow",
957+
"ptr::add requires that the address calculation does not overflow \
958+
(self:{this:?}, count:{count}, size:{size})",
956959
(
957960
this: *const () = self as *const (),
958961
count: usize = count,
@@ -1057,7 +1060,8 @@ impl<T: ?Sized> *const T {
10571060
#[cfg(debug_assertions)] // Expensive, and doesn't catch much in the wild.
10581061
ub_checks::assert_unsafe_precondition!(
10591062
check_language_ub,
1060-
"ptr::sub requires that the address calculation does not overflow",
1063+
"ptr::sub requires that the address calculation does not overflow \
1064+
(self:{this:?}, count:{count}, size:{size})",
10611065
(
10621066
this: *const () = self as *const (),
10631067
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_library_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>(),
@@ -1570,7 +1573,8 @@ pub const unsafe fn write<T>(dst: *mut T, src: T) {
15701573
#[cfg(debug_assertions)] // Too expensive to always enable (for now?)
15711574
ub_checks::assert_unsafe_precondition!(
15721575
check_language_ub,
1573-
"ptr::write requires that the pointer argument is aligned and non-null",
1576+
"ptr::write requires that the pointer argument is aligned and non-null \
1577+
(dst:{addr:?}, align:{align})",
15741578
(
15751579
addr: *mut () = dst as *mut (),
15761580
align: usize = align_of::<T>(),
@@ -1738,7 +1742,8 @@ pub unsafe fn read_volatile<T>(src: *const T) -> T {
17381742
unsafe {
17391743
ub_checks::assert_unsafe_precondition!(
17401744
check_language_ub,
1741-
"ptr::read_volatile requires that the pointer argument is aligned and non-null",
1745+
"ptr::read_volatile requires that the pointer argument is aligned and non-null \
1746+
(src:{addr:?}, align:{align})",
17421747
(
17431748
addr: *const () = src as *const (),
17441749
align: usize = align_of::<T>(),
@@ -1818,7 +1823,8 @@ pub unsafe fn write_volatile<T>(dst: *mut T, src: T) {
18181823
unsafe {
18191824
ub_checks::assert_unsafe_precondition!(
18201825
check_language_ub,
1821-
"ptr::write_volatile requires that the pointer argument is aligned and non-null",
1826+
"ptr::write_volatile requires that the pointer argument is aligned and non-null \
1827+
(dst:{addr:?}, align:{align})",
18221828
(
18231829
addr: *mut () = dst as *mut (),
18241830
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,
@@ -1042,7 +1043,8 @@ impl<T: ?Sized> *mut T {
10421043
#[cfg(debug_assertions)] // Expensive, and doesn't catch much in the wild.
10431044
ub_checks::assert_unsafe_precondition!(
10441045
check_language_ub,
1045-
"ptr::add requires that the address calculation does not overflow",
1046+
"ptr::add requires that the address calculation does not overflow \
1047+
(self:{this:?}, count:{count}, size:{size})",
10461048
(
10471049
this: *const () = self as *const (),
10481050
count: usize = count,
@@ -1147,7 +1149,8 @@ impl<T: ?Sized> *mut T {
11471149
#[cfg(debug_assertions)] // Expensive, and doesn't catch much in the wild.
11481150
ub_checks::assert_unsafe_precondition!(
11491151
check_language_ub,
1150-
"ptr::sub requires that the address calculation does not overflow",
1152+
"ptr::sub requires that the address calculation does not overflow \
1153+
(self:{this:?}, count:{count}, size:{size})",
11511154
(
11521155
this: *const () = self as *const (),
11531156
count: usize = count,

library/core/src/ptr/non_null.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ impl<T: ?Sized> NonNull<T> {
221221
unsafe {
222222
assert_unsafe_precondition!(
223223
check_language_ub,
224-
"NonNull::new_unchecked requires that the pointer is non-null",
224+
"NonNull::new_unchecked requires that the pointer is non-null (ptr:{ptr:?})",
225225
(ptr: *mut () = ptr as *mut ()) => !ptr.is_null()
226226
);
227227
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)