about summary refs log tree commit diff
diff options
context:
space:
mode:
authorsam skeoch <djscythe@noreply.codeberg.org>2025-05-13 14:55:03 +0100
committersam skeoch <djscythe@noreply.codeberg.org>2025-05-16 13:54:02 +0100
commit101e24a223f818e17d2c68298d48d4e6008c02aa (patch)
treeaa544b24f193f39f97b4f610a0487740b620bc55
parent7d9f437f993e213c1f9aeb9c07454f6fd1648821 (diff)
downloadrust-101e24a223f818e17d2c68298d48d4e6008c02aa.tar.gz
rust-101e24a223f818e17d2c68298d48d4e6008c02aa.zip
Add assert_unsafe_precondition!()s to as_ascii_unchecked() methods
-rw-r--r--library/core/src/char/methods.rs7
-rw-r--r--library/core/src/num/mod.rs6
-rw-r--r--library/core/src/str/mod.rs7
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() }