diff options
| -rw-r--r-- | library/core/src/char/methods.rs | 7 | ||||
| -rw-r--r-- | library/core/src/num/mod.rs | 6 | ||||
| -rw-r--r-- | library/core/src/str/mod.rs | 7 |
3 files changed, 20 insertions, 0 deletions
diff --git a/library/core/src/char/methods.rs b/library/core/src/char/methods.rs index 3c25ab4fdd7..af2edf141b2 100644 --- a/library/core/src/char/methods.rs +++ b/library/core/src/char/methods.rs @@ -4,6 +4,7 @@ use super::*; use crate::panic::const_panic; use crate::slice; use crate::str::from_utf8_unchecked_mut; +use crate::ub_checks::assert_unsafe_precondition; use crate::unicode::printable::is_printable; use crate::unicode::{self, conversions}; @@ -1212,6 +1213,12 @@ impl char { #[unstable(feature = "ascii_char", issue = "110998")] #[inline] pub const unsafe fn as_ascii_unchecked(&self) -> ascii::Char { + assert_unsafe_precondition!( + check_library_ub, + "as_ascii_unchecked requires that the char is valid ASCII", + (it: &char = self) => it.is_ascii() + ); + // SAFETY: the caller promised that this char is ASCII. unsafe { ascii::Char::from_u8_unchecked(*self as u8) } } diff --git a/library/core/src/num/mod.rs b/library/core/src/num/mod.rs index 9332908398c..a82b2aa61ce 100644 --- a/library/core/src/num/mod.rs +++ b/library/core/src/num/mod.rs @@ -502,6 +502,12 @@ impl u8 { #[unstable(feature = "ascii_char", issue = "110998")] #[inline] pub const unsafe fn as_ascii_unchecked(&self) -> ascii::Char { + assert_unsafe_precondition!( + check_library_ub, + "as_ascii_unchecked requires that the byte is valid ASCII", + (it: &u8 = self) => it.is_ascii() + ); + // SAFETY: the caller promised that this byte is ASCII. unsafe { ascii::Char::from_u8_unchecked(*self) } } diff --git a/library/core/src/str/mod.rs b/library/core/src/str/mod.rs index 2eb8f496085..e505e228095 100644 --- a/library/core/src/str/mod.rs +++ b/library/core/src/str/mod.rs @@ -17,6 +17,7 @@ use self::pattern::{DoubleEndedSearcher, Pattern, ReverseSearcher, Searcher}; use crate::char::{self, EscapeDebugExtArgs}; use crate::ops::Range; use crate::slice::{self, SliceIndex}; +use crate::ub_checks::assert_unsafe_precondition; use crate::{ascii, mem}; pub mod pattern; @@ -2644,6 +2645,12 @@ impl str { #[must_use] #[inline] pub const unsafe fn as_ascii_unchecked(&self) -> &[ascii::Char] { + assert_unsafe_precondition!( + check_library_ub, + "as_ascii_unchecked requires that the string is valid ASCII", + (it: &str = self) => it.is_ascii() + ); + // SAFETY: the caller promised that every byte of this string slice // is ASCII. unsafe { self.as_bytes().as_ascii_unchecked() } |
