Skip to content

Commit 65a3d91

Browse files
committed
Report arguments from assert_unsafe_precondition
1 parent 9725c4b commit 65a3d91

Some content is hidden

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

55 files changed

+289
-87
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
@@ -516,7 +516,7 @@ impl AsciiChar {
516516
pub const unsafe fn digit_unchecked(d: u8) -> Self {
517517
assert_unsafe_precondition!(
518518
check_library_ub,
519-
"`ascii::Char::digit_unchecked` input cannot exceed 9.",
519+
"`ascii::Char::digit_unchecked` input cannot exceed 9. (d:{d})",
520520
(d: u8 = d) => d < 10
521521
);
522522

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/char/methods.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use super::*;
44
use crate::panic::const_panic;
55
use crate::slice;
66
use crate::str::from_utf8_unchecked_mut;
7-
use crate::ub_checks::assert_unsafe_precondition;
7+
//use crate::ub_checks::assert_unsafe_precondition;
88
use crate::unicode::printable::is_printable;
99
use crate::unicode::{self, conversions};
1010

@@ -1258,11 +1258,13 @@ impl char {
12581258
#[unstable(feature = "ascii_char", issue = "110998")]
12591259
#[inline]
12601260
pub const unsafe fn as_ascii_unchecked(&self) -> ascii::Char {
1261+
/*
12611262
assert_unsafe_precondition!(
12621263
check_library_ub,
12631264
"as_ascii_unchecked requires that the char is valid ASCII",
12641265
(it: &char = self) => it.is_ascii()
12651266
);
1267+
*/
12661268

12671269
// SAFETY: the caller promised that this char is ASCII.
12681270
unsafe { ascii::Char::from_u8_unchecked(*self as u8) }

library/core/src/displaywrapper.rs

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
use core::fmt::{Display, Formatter, Result};
2+
3+
#[allow(missing_debug_implementations)]
4+
#[unstable(feature = "ub_checks", issue = "none")]
5+
pub struct DisplayWrapper<T>(#[unstable(feature = "ub_checks", issue = "none")] pub T);
6+
7+
trait Displayable: Sized + Clone + Copy {
8+
const IS_POINTER: bool;
9+
const SIGNED: bool;
10+
#[inline]
11+
fn addr(self) -> usize {
12+
unimplemented!()
13+
}
14+
#[inline]
15+
fn as_u128(self) -> u128 {
16+
unimplemented!()
17+
}
18+
#[inline]
19+
fn as_i128(self) -> i128 {
20+
unimplemented!()
21+
}
22+
}
23+
24+
impl<T> Displayable for *const T {
25+
const IS_POINTER: bool = true;
26+
const SIGNED: bool = false;
27+
#[inline]
28+
fn addr(self) -> usize {
29+
self.addr()
30+
}
31+
}
32+
impl<T> Displayable for *mut T {
33+
const IS_POINTER: bool = true;
34+
const SIGNED: bool = false;
35+
#[inline]
36+
fn addr(self) -> usize {
37+
self.addr()
38+
}
39+
}
40+
impl Displayable for u8 {
41+
const IS_POINTER: bool = false;
42+
const SIGNED: bool = false;
43+
#[inline]
44+
fn as_u128(self) -> u128 {
45+
self as u128
46+
}
47+
}
48+
impl Displayable for u32 {
49+
const IS_POINTER: bool = false;
50+
const SIGNED: bool = false;
51+
#[inline]
52+
fn as_u128(self) -> u128 {
53+
self as u128
54+
}
55+
}
56+
impl Displayable for u64 {
57+
const IS_POINTER: bool = false;
58+
const SIGNED: bool = false;
59+
#[inline]
60+
fn as_u128(self) -> u128 {
61+
self as u128
62+
}
63+
}
64+
impl Displayable for usize {
65+
const IS_POINTER: bool = false;
66+
const SIGNED: bool = false;
67+
#[inline]
68+
fn as_u128(self) -> u128 {
69+
self as u128
70+
}
71+
}
72+
impl Displayable for u128 {
73+
const IS_POINTER: bool = false;
74+
const SIGNED: bool = false;
75+
#[inline]
76+
fn as_u128(self) -> u128 {
77+
self
78+
}
79+
}
80+
81+
impl Displayable for isize {
82+
const IS_POINTER: bool = false;
83+
const SIGNED: bool = true;
84+
#[inline]
85+
fn as_i128(self) -> i128 {
86+
self as i128
87+
}
88+
}
89+
impl Displayable for i128 {
90+
const IS_POINTER: bool = false;
91+
const SIGNED: bool = true;
92+
#[inline]
93+
fn as_i128(self) -> i128 {
94+
self
95+
}
96+
}
97+
#[unstable(feature = "ub_checks", issue = "none")]
98+
impl<T: Displayable> Display for DisplayWrapper<T> {
99+
#[inline]
100+
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
101+
const HEX: [u8; 16] = *b"0123456789abcdef";
102+
let mut buf = [0u8; 42];
103+
let mut cur = buf.len();
104+
if T::IS_POINTER {
105+
let mut n = self.0.addr();
106+
while n >= 16 {
107+
let d = n % 16;
108+
n /= 16;
109+
cur -= 1;
110+
buf[cur] = HEX[d];
111+
}
112+
cur -= 1;
113+
buf[cur] = HEX[n];
114+
115+
cur -= 1;
116+
buf[cur] = b'x';
117+
cur -= 1;
118+
buf[cur] = b'0';
119+
} else {
120+
let mut is_negative = false;
121+
let mut n = if T::SIGNED {
122+
let signed = self.0.as_i128();
123+
is_negative = signed < 0;
124+
(!(signed as u128)).wrapping_add(1)
125+
} else {
126+
self.0.as_u128()
127+
};
128+
while n >= 10 {
129+
let d = n % 10;
130+
n /= 10;
131+
cur -= 1;
132+
buf[cur] = (d as u8) + b'0';
133+
}
134+
cur -= 1;
135+
buf[cur] = (n as u8) + b'0';
136+
if is_negative {
137+
cur -= 1;
138+
buf[cur] = b'-';
139+
}
140+
}
141+
let s = unsafe { core::str::from_utf8_unchecked(&buf[cur..]) };
142+
f.write_str(s)
143+
}
144+
}

library/core/src/fmt/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1883,6 +1883,7 @@ impl<'a> Formatter<'a> {
18831883
/// assert_eq!(format!("{Foo:0>8}"), "Foo");
18841884
/// ```
18851885
#[stable(feature = "rust1", since = "1.0.0")]
1886+
#[inline]
18861887
pub fn write_str(&mut self, data: &str) -> Result {
18871888
self.buf.write_str(data)
18881889
}

library/core/src/intrinsics/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2388,7 +2388,7 @@ where
23882388
/// marked as `#[inline]`.
23892389
///
23902390
/// See [`const_eval_select()`] for the rules and requirements around that intrinsic.
2391-
pub(crate) macro const_eval_select {
2391+
pub macro const_eval_select {
23922392
(
23932393
@capture$([$($binders:tt)*])? { $($arg:ident : $ty:ty = $val:expr),* $(,)? } $( -> $ret:ty )? :
23942394
if const

library/core/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,9 @@ mod bool;
350350
mod escape;
351351
mod tuple;
352352
mod unit;
353+
#[doc(hidden)]
354+
#[unstable(feature = "ub_checks", issue = "none")]
355+
pub mod displaywrapper;
353356

354357
#[stable(feature = "core_primitive", since = "1.43.0")]
355358
pub mod primitive;

library/core/src/num/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -506,11 +506,13 @@ impl u8 {
506506
#[unstable(feature = "ascii_char", issue = "110998")]
507507
#[inline]
508508
pub const unsafe fn as_ascii_unchecked(&self) -> ascii::Char {
509+
/*
509510
assert_unsafe_precondition!(
510511
check_library_ub,
511512
"as_ascii_unchecked requires that the byte is valid ASCII",
512513
(it: &u8 = self) => it.is_ascii()
513514
);
515+
*/
514516

515517
// SAFETY: the caller promised that this byte is ASCII.
516518
unsafe { ascii::Char::from_u8_unchecked(*self) }

library/core/src/num/nonzero.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,7 @@ where
420420
ub_checks::assert_unsafe_precondition!(
421421
check_language_ub,
422422
"NonZero::new_unchecked requires the argument to be non-zero",
423+
// FIXME: Can't print n here because of how the check is written
423424
() => false,
424425
);
425426
intrinsics::unreachable()
@@ -461,6 +462,7 @@ where
461462
ub_checks::assert_unsafe_precondition!(
462463
check_library_ub,
463464
"NonZero::from_mut_unchecked requires the argument to dereference as non-zero",
465+
// FIXME: Can't print n here because of how the check is written
464466
() => false,
465467
);
466468
intrinsics::unreachable()

0 commit comments

Comments
 (0)