about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMatthias Krüger <matthias.krueger@famsik.de>2024-01-09 05:33:21 +0100
committerGitHub <noreply@github.com>2024-01-09 05:33:21 +0100
commitb0c492cd6e5237ab64adcdc229293ab127ecc3ab (patch)
treea8dd411ed7d02f21a74109f8907a31cf6853f08a
parent1974f5cba97b846cef6a83f5164cc1e138d97f6b (diff)
parentab716d0635b7eb0a829f79ac394f1c7a0a05a64a (diff)
downloadrust-b0c492cd6e5237ab64adcdc229293ab127ecc3ab.tar.gz
rust-b0c492cd6e5237ab64adcdc229293ab127ecc3ab.zip
Rollup merge of #118979 - ChrisDenton:unwrap-const, r=Nilstrieb,dtolnay
Use `assert_unsafe_precondition` for `char::from_u32_unchecked`

Use `assert_unsafe_precondition` in `char::from_u32_unchecked` so that it can be stabilized as `const`.
-rw-r--r--library/core/src/char/convert.rs9
1 files changed, 8 insertions, 1 deletions
diff --git a/library/core/src/char/convert.rs b/library/core/src/char/convert.rs
index 453de9754be..177f39b59ae 100644
--- a/library/core/src/char/convert.rs
+++ b/library/core/src/char/convert.rs
@@ -4,6 +4,7 @@ use crate::char::TryFromCharError;
 use crate::convert::TryFrom;
 use crate::error::Error;
 use crate::fmt;
+use crate::intrinsics::assert_unsafe_precondition;
 use crate::mem::transmute;
 use crate::str::FromStr;
 
@@ -23,7 +24,13 @@ pub(super) const fn from_u32(i: u32) -> Option<char> {
 #[must_use]
 pub(super) const unsafe fn from_u32_unchecked(i: u32) -> char {
     // SAFETY: the caller must guarantee that `i` is a valid char value.
-    if cfg!(debug_assertions) { char::from_u32(i).unwrap() } else { unsafe { transmute(i) } }
+    unsafe {
+        assert_unsafe_precondition!(
+            "invalid value for `char`",
+            (i: u32) => char_try_from_u32(i).is_ok()
+        );
+        transmute(i)
+    }
 }
 
 #[stable(feature = "char_convert", since = "1.13.0")]