Skip to content

Commit 4dc2e6c

Browse files
committed
Include arguments to the precondition check in failure messages
1 parent 64feb9b commit 4dc2e6c

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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4354,7 +4354,8 @@ pub const unsafe fn copy_nonoverlapping<T>(src: *const T, dst: *mut T, count: us
43544354
ub_checks::assert_unsafe_precondition!(
43554355
check_language_ub,
43564356
"ptr::copy_nonoverlapping requires that both pointer arguments are aligned and non-null \
4357-
and the specified memory ranges do not overlap",
4357+
and the specified memory ranges do not overlap \
4358+
(src:{src:?}, dst:{dst:?}, size:{size}, align:{align}, count:{count})",
43584359
(
43594360
src: *const () = src as *const (),
43604361
dst: *mut () = dst as *mut (),
@@ -4459,7 +4460,8 @@ pub const unsafe fn copy<T>(src: *const T, dst: *mut T, count: usize) {
44594460
unsafe {
44604461
ub_checks::assert_unsafe_precondition!(
44614462
check_language_ub,
4462-
"ptr::copy requires that both pointer arguments are aligned and non-null",
4463+
"ptr::copy requires that both pointer arguments are aligned and non-null \
4464+
(src:{src:?}, dst:{dst:?}, align:{align})",
44634465
(
44644466
src: *const () = src as *const (),
44654467
dst: *mut () = dst as *mut (),
@@ -4542,7 +4544,8 @@ pub const unsafe fn write_bytes<T>(dst: *mut T, val: u8, count: usize) {
45424544
unsafe {
45434545
ub_checks::assert_unsafe_precondition!(
45444546
check_language_ub,
4545-
"ptr::write_bytes requires that the destination pointer is aligned and non-null",
4547+
"ptr::write_bytes requires that the destination pointer is aligned and non-null \
4548+
(dst:{addr:?}, align:{align})",
45464549
(
45474550
addr: *const () = dst as *const (),
45484551
align: usize = align_of::<T>(),

library/core/src/num/int_macros.rs

Lines changed: 6 additions & 0 deletions
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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,7 @@ where
403403
ub_checks::assert_unsafe_precondition!(
404404
check_language_ub,
405405
"NonZero::new_unchecked requires the argument to be non-zero",
406+
// FIXME: Can't print n here because of how the check is written
406407
() => false,
407408
);
408409
intrinsics::unreachable()
@@ -443,6 +444,7 @@ where
443444
ub_checks::assert_unsafe_precondition!(
444445
check_library_ub,
445446
"NonZero::from_mut_unchecked requires the argument to dereference as non-zero",
447+
// FIXME: Can't print n here because of how the check is written
446448
() => false,
447449
);
448450
intrinsics::unreachable()

library/core/src/num/uint_macros.rs

Lines changed: 5 additions & 0 deletions
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,
@@ -1504,6 +1507,7 @@ macro_rules! uint_impl {
15041507
assert_unsafe_precondition!(
15051508
check_language_ub,
15061509
concat!(stringify!($SelfT), "::unchecked_shl cannot overflow"),
1510+
// FIXME: concat! prevents adding formatting
15071511
(
15081512
rhs: u32 = rhs,
15091513
) => rhs < <$ActualT>::BITS,
@@ -1625,6 +1629,7 @@ macro_rules! uint_impl {
16251629
assert_unsafe_precondition!(
16261630
check_language_ub,
16271631
concat!(stringify!($SelfT), "::unchecked_shr cannot overflow"),
1632+
// FIXME: concat! prevents adding formatting
16281633
(
16291634
rhs: u32 = rhs,
16301635
) => rhs < <$ActualT>::BITS,

library/core/src/ops/index_range.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ impl IndexRange {
2121
pub 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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,8 @@ impl Alignment {
7575
pub const unsafe fn new_unchecked(align: usize) -> Self {
7676
assert_unsafe_precondition!(
7777
check_language_ub,
78-
"Alignment::new_unchecked requires a power of two",
78+
"Alignment::new_unchecked requires a power of two \
79+
(align:{align})",
7980
(align: usize = align) => align.is_power_of_two()
8081
);
8182

library/core/src/ptr/const_ptr.rs

Lines changed: 8 additions & 4 deletions
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

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1078,7 +1078,8 @@ pub const unsafe fn swap_nonoverlapping<T>(x: *mut T, y: *mut T, count: usize) {
10781078
ub_checks::assert_unsafe_precondition!(
10791079
check_language_ub,
10801080
"ptr::swap_nonoverlapping requires that both pointer arguments are aligned and non-null \
1081-
and the specified memory ranges do not overlap",
1081+
and the specified memory ranges do not overlap \
1082+
(x:{x:?}, y:{y:?}, size:{size}, align:{align}, count:{count})",
10821083
(
10831084
x: *mut () = x as *mut (),
10841085
y: *mut () = y as *mut (),
@@ -1223,7 +1224,8 @@ pub const unsafe fn replace<T>(dst: *mut T, src: T) -> T {
12231224
unsafe {
12241225
ub_checks::assert_unsafe_precondition!(
12251226
check_language_ub,
1226-
"ptr::replace requires that the pointer argument is aligned and non-null",
1227+
"ptr::replace requires that the pointer argument is aligned and non-null\
1228+
(dst:{addr:?}, (align:{align}))",
12271229
(
12281230
addr: *const () = dst as *const (),
12291231
align: usize = align_of::<T>(),
@@ -1376,7 +1378,8 @@ pub const unsafe fn read<T>(src: *const T) -> T {
13761378
#[cfg(debug_assertions)] // Too expensive to always enable (for now?)
13771379
ub_checks::assert_unsafe_precondition!(
13781380
check_language_ub,
1379-
"ptr::read requires that the pointer argument is aligned and non-null",
1381+
"ptr::read requires that the pointer argument is aligned and non-null \
1382+
(src:{addr:?}, align:{align})",
13801383
(
13811384
addr: *const () = src as *const (),
13821385
align: usize = align_of::<T>(),
@@ -1580,7 +1583,8 @@ pub const unsafe fn write<T>(dst: *mut T, src: T) {
15801583
#[cfg(debug_assertions)] // Too expensive to always enable (for now?)
15811584
ub_checks::assert_unsafe_precondition!(
15821585
check_language_ub,
1583-
"ptr::write requires that the pointer argument is aligned and non-null",
1586+
"ptr::write requires that the pointer argument is aligned and non-null \
1587+
(dst:{addr:?}, align:{align})",
15841588
(
15851589
addr: *mut () = dst as *mut (),
15861590
align: usize = align_of::<T>(),
@@ -1752,7 +1756,8 @@ pub unsafe fn read_volatile<T>(src: *const T) -> T {
17521756
unsafe {
17531757
ub_checks::assert_unsafe_precondition!(
17541758
check_language_ub,
1755-
"ptr::read_volatile requires that the pointer argument is aligned and non-null",
1759+
"ptr::read_volatile requires that the pointer argument is aligned and non-null \
1760+
(src:{addr:?}, align:{align})",
17561761
(
17571762
addr: *const () = src as *const (),
17581763
align: usize = align_of::<T>(),
@@ -1832,7 +1837,8 @@ pub unsafe fn write_volatile<T>(dst: *mut T, src: T) {
18321837
unsafe {
18331838
ub_checks::assert_unsafe_precondition!(
18341839
check_language_ub,
1835-
"ptr::write_volatile requires that the pointer argument is aligned and non-null",
1840+
"ptr::write_volatile requires that the pointer argument is aligned and non-null \
1841+
(dst:{addr:?}, align:{align})",
18361842
(
18371843
addr: *mut () = dst as *mut (),
18381844
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
@@ -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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ impl<T: ?Sized> NonNull<T> {
195195
unsafe {
196196
assert_unsafe_precondition!(
197197
check_language_ub,
198-
"NonNull::new_unchecked requires that the pointer is non-null",
198+
"NonNull::new_unchecked requires that the pointer is non-null (ptr:{ptr:?})",
199199
(ptr: *mut () = ptr as *mut ()) => !ptr.is_null()
200200
);
201201
NonNull { pointer: ptr as _ }

library/core/src/slice/index.rs

Lines changed: 14 additions & 8 deletions
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)