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

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
@@ -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

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

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

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

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

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

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,
@@ -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,

0 commit comments

Comments
 (0)