Skip to content

Commit e7df7ba

Browse files
authoredAug 22, 2024
Rollup merge of #129374 - ChaiTRex:digit_unchecked_assert_unsafe_precondition, r=scottmcm
Use `assert_unsafe_precondition!` in `AsciiChar::digit_unchecked`
2 parents 8f2c4d1 + 191862d commit e7df7ba

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed
 

‎library/core/src/ascii/ascii_char.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
//! suggestions from rustc if you get anything slightly wrong in here, and overall
44
//! helps with clarity as we're also referring to `char` intentionally in here.
55
6-
use crate::fmt;
76
use crate::mem::transmute;
7+
use crate::{assert_unsafe_precondition, fmt};
88

99
/// One of the 128 Unicode characters from U+0000 through U+007F,
1010
/// often known as the [ASCII] subset.
@@ -497,14 +497,18 @@ impl AsciiChar {
497497
/// Notably, it should not be expected to return hex digits, or any other
498498
/// reasonable extension of the decimal digits.
499499
///
500-
/// (This lose safety condition is intended to simplify soundness proofs
500+
/// (This loose safety condition is intended to simplify soundness proofs
501501
/// when writing code using this method, since the implementation doesn't
502502
/// need something really specific, not to make those other arguments do
503503
/// something useful. It might be tightened before stabilization.)
504504
#[unstable(feature = "ascii_char", issue = "110998")]
505505
#[inline]
506506
pub const unsafe fn digit_unchecked(d: u8) -> Self {
507-
debug_assert!(d < 10);
507+
assert_unsafe_precondition!(
508+
check_language_ub,
509+
"`AsciiChar::digit_unchecked` input cannot exceed 9.",
510+
(d: u8 = d) => d < 10
511+
);
508512

509513
// SAFETY: `'0'` through `'9'` are U+00030 through U+0039,
510514
// so because `d` must be 64 or less the addition can return at most

0 commit comments

Comments
 (0)