Skip to content

Commit b3a56be

Browse files
committed
Include arguments to the precondition check in failure messages
1 parent 556d20a commit b3a56be

Some content is hidden

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

46 files changed

+128
-76
lines changed

library/core/src/alloc/layout.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ impl Layout {
131131
assert_unsafe_precondition!(
132132
check_library_ub,
133133
"Layout::from_size_align_unchecked requires that align is a power of 2 \
134-
and the rounded-up allocation size does not exceed isize::MAX",
134+
and the rounded-up allocation size does not exceed isize::MAX (size:{size}, align:{align})",
135135
(
136136
size: usize = size,
137137
align: usize = align,

library/core/src/ascii/ascii_char.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -507,7 +507,7 @@ impl AsciiChar {
507507
pub const unsafe fn digit_unchecked(d: u8) -> Self {
508508
assert_unsafe_precondition!(
509509
check_language_ub,
510-
"`ascii::Char::digit_unchecked` input cannot exceed 9.",
510+
"`ascii::Char::digit_unchecked` input cannot exceed 9. (d:{d})",
511511
(d: u8 = d) => d < 10
512512
);
513513

library/core/src/char/convert.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ pub(super) const unsafe fn from_u32_unchecked(i: u32) -> char {
2828
unsafe {
2929
assert_unsafe_precondition!(
3030
check_language_ub,
31-
"invalid value for `char`",
31+
"invalid value for `char` ({i})",
3232
(i: u32 = i) => char_try_from_u32(i).is_ok()
3333
);
3434
transmute(i)

library/core/src/num/int_macros.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -517,6 +517,7 @@ macro_rules! int_impl {
517517
assert_unsafe_precondition!(
518518
check_language_ub,
519519
concat!(stringify!($SelfT), "::unchecked_add cannot overflow"),
520+
// FIXME: concat! prevents adding formatting
520521
(
521522
lhs: $SelfT = self,
522523
rhs: $SelfT = rhs,
@@ -659,6 +660,7 @@ macro_rules! int_impl {
659660
assert_unsafe_precondition!(
660661
check_language_ub,
661662
concat!(stringify!($SelfT), "::unchecked_sub cannot overflow"),
663+
// FIXME: concat! prevents adding formatting
662664
(
663665
lhs: $SelfT = self,
664666
rhs: $SelfT = rhs,
@@ -801,6 +803,7 @@ macro_rules! int_impl {
801803
assert_unsafe_precondition!(
802804
check_language_ub,
803805
concat!(stringify!($SelfT), "::unchecked_mul cannot overflow"),
806+
// FIXME: concat! prevents adding formatting
804807
(
805808
lhs: $SelfT = self,
806809
rhs: $SelfT = rhs,
@@ -1227,6 +1230,7 @@ macro_rules! int_impl {
12271230
assert_unsafe_precondition!(
12281231
check_language_ub,
12291232
concat!(stringify!($SelfT), "::unchecked_neg cannot overflow"),
1233+
// FIXME: concat! prevents adding formatting
12301234
(
12311235
lhs: $SelfT = self,
12321236
) => !lhs.overflowing_neg().1,
@@ -1349,6 +1353,7 @@ macro_rules! int_impl {
13491353
assert_unsafe_precondition!(
13501354
check_language_ub,
13511355
concat!(stringify!($SelfT), "::unchecked_shl cannot overflow"),
1356+
// FIXME: concat! prevents adding formatting
13521357
(
13531358
rhs: u32 = rhs,
13541359
) => rhs < <$ActualT>::BITS,
@@ -1465,6 +1470,7 @@ macro_rules! int_impl {
14651470
assert_unsafe_precondition!(
14661471
check_language_ub,
14671472
concat!(stringify!($SelfT), "::unchecked_shr cannot overflow"),
1473+
// FIXME: concat! prevents adding formatting
14681474
(
14691475
rhs: u32 = rhs,
14701476
) => rhs < <$ActualT>::BITS,

library/core/src/num/nonzero.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,7 @@ where
399399
ub_checks::assert_unsafe_precondition!(
400400
check_language_ub,
401401
"NonZero::new_unchecked requires the argument to be non-zero",
402+
// FIXME: Can't print n here because of how the check is written
402403
() => false,
403404
);
404405
intrinsics::unreachable()
@@ -440,6 +441,7 @@ where
440441
ub_checks::assert_unsafe_precondition!(
441442
check_library_ub,
442443
"NonZero::from_mut_unchecked requires the argument to dereference as non-zero",
444+
// FIXME: Can't print n here because of how the check is written
443445
() => false,
444446
);
445447
intrinsics::unreachable()

library/core/src/num/uint_macros.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -586,6 +586,7 @@ macro_rules! uint_impl {
586586
assert_unsafe_precondition!(
587587
check_language_ub,
588588
concat!(stringify!($SelfT), "::unchecked_add cannot overflow"),
589+
// FIXME: concat! prevents adding formatting
589590
(
590591
lhs: $SelfT = self,
591592
rhs: $SelfT = rhs,
@@ -768,6 +769,7 @@ macro_rules! uint_impl {
768769
assert_unsafe_precondition!(
769770
check_language_ub,
770771
concat!(stringify!($SelfT), "::unchecked_sub cannot overflow"),
772+
// FIXME: concat! prevents adding formatting
771773
(
772774
lhs: $SelfT = self,
773775
rhs: $SelfT = rhs,
@@ -943,6 +945,7 @@ macro_rules! uint_impl {
943945
assert_unsafe_precondition!(
944946
check_language_ub,
945947
concat!(stringify!($SelfT), "::unchecked_mul cannot overflow"),
948+
// FIXME: concat! prevents adding formatting
946949
(
947950
lhs: $SelfT = self,
948951
rhs: $SelfT = rhs,
@@ -1631,6 +1634,7 @@ macro_rules! uint_impl {
16311634
assert_unsafe_precondition!(
16321635
check_language_ub,
16331636
concat!(stringify!($SelfT), "::unchecked_shl cannot overflow"),
1637+
// FIXME: concat! prevents adding formatting
16341638
(
16351639
rhs: u32 = rhs,
16361640
) => rhs < <$ActualT>::BITS,
@@ -1747,6 +1751,7 @@ macro_rules! uint_impl {
17471751
assert_unsafe_precondition!(
17481752
check_language_ub,
17491753
concat!(stringify!($SelfT), "::unchecked_shr cannot overflow"),
1754+
// FIXME: concat! prevents adding formatting
17501755
(
17511756
rhs: u32 = rhs,
17521757
) => rhs < <$ActualT>::BITS,

library/core/src/ops/index_range.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ impl IndexRange {
2323
pub(crate) const unsafe fn new_unchecked(start: usize, end: usize) -> Self {
2424
ub_checks::assert_unsafe_precondition!(
2525
check_library_ub,
26-
"IndexRange::new_unchecked requires `start <= end`",
26+
"IndexRange::new_unchecked requires `start <= end` \
27+
(start:{start}, end:{end})",
2728
(start: usize = start, end: usize = end) => start <= end,
2829
);
2930
IndexRange { start, end }

library/core/src/ptr/alignment.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,8 @@ impl Alignment {
7777
pub const unsafe fn new_unchecked(align: usize) -> Self {
7878
assert_unsafe_precondition!(
7979
check_language_ub,
80-
"Alignment::new_unchecked requires a power of two",
80+
"Alignment::new_unchecked requires a power of two \
81+
(align:{align})",
8182
(align: usize = align) => align.is_power_of_two()
8283
);
8384

library/core/src/ptr/const_ptr.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,8 @@ impl<T: PointeeSized> *const T {
424424

425425
ub_checks::assert_unsafe_precondition!(
426426
check_language_ub,
427-
"ptr::offset requires the address calculation to not overflow",
427+
"ptr::offset requires the address calculation to not overflow \
428+
(ptr:{this:?}, count:{count}, size:{size})",
428429
(
429430
this: *const () = self as *const (),
430431
count: isize = count,
@@ -768,7 +769,8 @@ impl<T: PointeeSized> *const T {
768769

769770
ub_checks::assert_unsafe_precondition!(
770771
check_language_ub,
771-
"ptr::offset_from_unsigned requires `self >= origin`",
772+
"ptr::offset_from_unsigned requires `self >= origin` \
773+
(self:{this:?}, origin:{origin:?})",
772774
(
773775
this: *const () = self as *const (),
774776
origin: *const () = origin as *const (),
@@ -903,7 +905,8 @@ impl<T: PointeeSized> *const T {
903905
#[cfg(debug_assertions)] // Expensive, and doesn't catch much in the wild.
904906
ub_checks::assert_unsafe_precondition!(
905907
check_language_ub,
906-
"ptr::add requires that the address calculation does not overflow",
908+
"ptr::add requires that the address calculation does not overflow \
909+
(self:{this:?}, count:{count}, size:{size})",
907910
(
908911
this: *const () = self as *const (),
909912
count: usize = count,
@@ -1008,7 +1011,8 @@ impl<T: PointeeSized> *const T {
10081011
#[cfg(debug_assertions)] // Expensive, and doesn't catch much in the wild.
10091012
ub_checks::assert_unsafe_precondition!(
10101013
check_language_ub,
1011-
"ptr::sub requires that the address calculation does not overflow",
1014+
"ptr::sub requires that the address calculation does not overflow \
1015+
(self:{this:?}, count:{count}, size:{size})",
10121016
(
10131017
this: *const () = self as *const (),
10141018
count: usize = count,

library/core/src/ptr/mod.rs

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -522,7 +522,8 @@ pub const unsafe fn copy_nonoverlapping<T>(src: *const T, dst: *mut T, count: us
522522
ub_checks::assert_unsafe_precondition!(
523523
check_language_ub,
524524
"ptr::copy_nonoverlapping requires that both pointer arguments are aligned and non-null \
525-
and the specified memory ranges do not overlap",
525+
and the specified memory ranges do not overlap \
526+
(src{src:?}, dst:{dst:?}, size:{size:?}, align:{align:?}, count:{count:?})",
526527
(
527528
src: *const () = src as *const (),
528529
dst: *mut () = dst as *mut (),
@@ -620,7 +621,8 @@ pub const unsafe fn copy<T>(src: *const T, dst: *mut T, count: usize) {
620621
unsafe {
621622
ub_checks::assert_unsafe_precondition!(
622623
check_language_ub,
623-
"ptr::copy requires that both pointer arguments are aligned and non-null",
624+
"ptr::copy requires that both pointer arguments are aligned and non-null \
625+
(src{src:?}, dst:{dst:?}, align:{align:?})",
624626
(
625627
src: *const () = src as *const (),
626628
dst: *mut () = dst as *mut (),
@@ -694,7 +696,8 @@ pub const unsafe fn write_bytes<T>(dst: *mut T, val: u8, count: usize) {
694696
unsafe {
695697
ub_checks::assert_unsafe_precondition!(
696698
check_language_ub,
697-
"ptr::write_bytes requires that the destination pointer is aligned and non-null",
699+
"ptr::write_bytes requires that the destination pointer is aligned and non-null \
700+
(dst:{addr:?}, align:{align})",
698701
(
699702
addr: *const () = dst as *const (),
700703
align: usize = align_of::<T>(),
@@ -1384,7 +1387,8 @@ pub const unsafe fn swap_nonoverlapping<T>(x: *mut T, y: *mut T, count: usize) {
13841387
ub_checks::assert_unsafe_precondition!(
13851388
check_library_ub,
13861389
"ptr::swap_nonoverlapping requires that both pointer arguments are aligned and non-null \
1387-
and the specified memory ranges do not overlap",
1390+
and the specified memory ranges do not overlap \
1391+
(x:{x:?}, y:{y:?}, size:{size}, align:{align}, count:{count})",
13881392
(
13891393
x: *mut () = x as *mut (),
13901394
y: *mut () = y as *mut (),
@@ -1569,7 +1573,8 @@ pub const unsafe fn replace<T>(dst: *mut T, src: T) -> T {
15691573
unsafe {
15701574
ub_checks::assert_unsafe_precondition!(
15711575
check_language_ub,
1572-
"ptr::replace requires that the pointer argument is aligned and non-null",
1576+
"ptr::replace requires that the pointer argument is aligned and non-null\
1577+
(dst:{addr:?}, (align:{align}))",
15731578
(
15741579
addr: *const () = dst as *const (),
15751580
align: usize = align_of::<T>(),
@@ -1722,7 +1727,8 @@ pub const unsafe fn read<T>(src: *const T) -> T {
17221727
#[cfg(debug_assertions)] // Too expensive to always enable (for now?)
17231728
ub_checks::assert_unsafe_precondition!(
17241729
check_language_ub,
1725-
"ptr::read requires that the pointer argument is aligned and non-null",
1730+
"ptr::read requires that the pointer argument is aligned and non-null \
1731+
(src:{addr:?}, align:{align})",
17261732
(
17271733
addr: *const () = src as *const (),
17281734
align: usize = align_of::<T>(),
@@ -1922,7 +1928,8 @@ pub const unsafe fn write<T>(dst: *mut T, src: T) {
19221928
#[cfg(debug_assertions)] // Too expensive to always enable (for now?)
19231929
ub_checks::assert_unsafe_precondition!(
19241930
check_language_ub,
1925-
"ptr::write requires that the pointer argument is aligned and non-null",
1931+
"ptr::write requires that the pointer argument is aligned and non-null \
1932+
(dst:{addr:?}, align:{align})",
19261933
(
19271934
addr: *mut () = dst as *mut (),
19281935
align: usize = align_of::<T>(),
@@ -2090,7 +2097,8 @@ pub unsafe fn read_volatile<T>(src: *const T) -> T {
20902097
unsafe {
20912098
ub_checks::assert_unsafe_precondition!(
20922099
check_language_ub,
2093-
"ptr::read_volatile requires that the pointer argument is aligned and non-null",
2100+
"ptr::read_volatile requires that the pointer argument is aligned and non-null \
2101+
(src:{addr:?}, align:{align})",
20942102
(
20952103
addr: *const () = src as *const (),
20962104
align: usize = align_of::<T>(),
@@ -2170,7 +2178,8 @@ pub unsafe fn write_volatile<T>(dst: *mut T, src: T) {
21702178
unsafe {
21712179
ub_checks::assert_unsafe_precondition!(
21722180
check_language_ub,
2173-
"ptr::write_volatile requires that the pointer argument is aligned and non-null",
2181+
"ptr::write_volatile requires that the pointer argument is aligned and non-null \
2182+
(dst:{addr:?}, align:{align})",
21742183
(
21752184
addr: *mut () = dst as *mut (),
21762185
align: usize = align_of::<T>(),

library/core/src/ptr/mut_ptr.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,8 @@ impl<T: PointeeSized> *mut T {
422422

423423
ub_checks::assert_unsafe_precondition!(
424424
check_language_ub,
425-
"ptr::offset requires the address calculation to not overflow",
425+
"ptr::offset requires the address calculation to not overflow \
426+
(self:{this:?}, count:{count}, size:{size})",
426427
(
427428
this: *const () = self as *const (),
428429
count: isize = count,
@@ -996,7 +997,8 @@ impl<T: PointeeSized> *mut T {
996997
#[cfg(debug_assertions)] // Expensive, and doesn't catch much in the wild.
997998
ub_checks::assert_unsafe_precondition!(
998999
check_language_ub,
999-
"ptr::add requires that the address calculation does not overflow",
1000+
"ptr::add requires that the address calculation does not overflow \
1001+
(self:{this:?}, count:{count}, size:{size})",
10001002
(
10011003
this: *const () = self as *const (),
10021004
count: usize = count,
@@ -1101,7 +1103,8 @@ impl<T: PointeeSized> *mut T {
11011103
#[cfg(debug_assertions)] // Expensive, and doesn't catch much in the wild.
11021104
ub_checks::assert_unsafe_precondition!(
11031105
check_language_ub,
1104-
"ptr::sub requires that the address calculation does not overflow",
1106+
"ptr::sub requires that the address calculation does not overflow \
1107+
(self:{this:?}, count:{count}, size:{size})",
11051108
(
11061109
this: *const () = self as *const (),
11071110
count: usize = count,

library/core/src/ptr/non_null.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ impl<T: PointeeSized> NonNull<T> {
228228
unsafe {
229229
assert_unsafe_precondition!(
230230
check_language_ub,
231-
"NonNull::new_unchecked requires that the pointer is non-null",
231+
"NonNull::new_unchecked requires that the pointer is non-null (ptr:{ptr:?})",
232232
(ptr: *mut () = ptr as *mut ()) => !ptr.is_null()
233233
);
234234
NonNull { pointer: ptr as _ }

library/core/src/slice/index.rs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -236,8 +236,9 @@ unsafe impl<T> SliceIndex<[T]> for usize {
236236
unsafe fn get_unchecked(self, slice: *const [T]) -> *const T {
237237
assert_unsafe_precondition!(
238238
check_language_ub,
239-
"slice::get_unchecked requires that the index is within the slice",
240-
(this: usize = self, len: usize = slice.len()) => this < len
239+
"slice::get_unchecked requires that the index is within the slice \
240+
(index:{index}, len:{len})",
241+
(index: usize = self, len: usize = slice.len()) => index < len
241242
);
242243
// SAFETY: the caller guarantees that `slice` is not dangling, so it
243244
// cannot be longer than `isize::MAX`. They also guarantee that
@@ -256,8 +257,9 @@ unsafe impl<T> SliceIndex<[T]> for usize {
256257
unsafe fn get_unchecked_mut(self, slice: *mut [T]) -> *mut T {
257258
assert_unsafe_precondition!(
258259
check_library_ub,
259-
"slice::get_unchecked_mut requires that the index is within the slice",
260-
(this: usize = self, len: usize = slice.len()) => this < len
260+
"slice::get_unchecked_mut requires that the index is within the slice \
261+
(index:{index}, len:{len})",
262+
(index: usize = self, len: usize = slice.len()) => index < len
261263
);
262264
// SAFETY: see comments for `get_unchecked` above.
263265
unsafe { slice_get_unchecked(slice, self) }
@@ -306,7 +308,8 @@ unsafe impl<T> SliceIndex<[T]> for ops::IndexRange {
306308
unsafe fn get_unchecked(self, slice: *const [T]) -> *const [T] {
307309
assert_unsafe_precondition!(
308310
check_library_ub,
309-
"slice::get_unchecked requires that the index is within the slice",
311+
"slice::get_unchecked requires that the index is within the slice \
312+
(end:{end}, len:{len})",
310313
(end: usize = self.end(), len: usize = slice.len()) => end <= len
311314
);
312315
// SAFETY: the caller guarantees that `slice` is not dangling, so it
@@ -321,7 +324,8 @@ unsafe impl<T> SliceIndex<[T]> for ops::IndexRange {
321324
unsafe fn get_unchecked_mut(self, slice: *mut [T]) -> *mut [T] {
322325
assert_unsafe_precondition!(
323326
check_library_ub,
324-
"slice::get_unchecked_mut requires that the index is within the slice",
327+
"slice::get_unchecked_mut requires that the index is within the slice \
328+
(end:{end}, len:{len})",
325329
(end: usize = self.end(), len: usize = slice.len()) => end <= len
326330
);
327331

@@ -387,7 +391,8 @@ unsafe impl<T> SliceIndex<[T]> for ops::Range<usize> {
387391
unsafe fn get_unchecked(self, slice: *const [T]) -> *const [T] {
388392
assert_unsafe_precondition!(
389393
check_library_ub,
390-
"slice::get_unchecked requires that the range is within the slice",
394+
"slice::get_unchecked requires that the range is within the slice \
395+
(range:{start}..{end}, len:{len})",
391396
(
392397
start: usize = self.start,
393398
end: usize = self.end,
@@ -412,7 +417,8 @@ unsafe impl<T> SliceIndex<[T]> for ops::Range<usize> {
412417
unsafe fn get_unchecked_mut(self, slice: *mut [T]) -> *mut [T] {
413418
assert_unsafe_precondition!(
414419
check_library_ub,
415-
"slice::get_unchecked_mut requires that the range is within the slice",
420+
"slice::get_unchecked_mut requires that the range is within the slice \
421+
(range:{start}..{end}, len:{len})",
416422
(
417423
start: usize = self.start,
418424
end: usize = self.end,

0 commit comments

Comments
 (0)