Skip to content

Commit a1b85c5

Browse files
authored
Unrolled build for rust-lang#118979
Rollup merge of rust-lang#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`.
2 parents a399117 + ab716d0 commit a1b85c5

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

Diff for: library/core/src/char/convert.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use crate::char::TryFromCharError;
44
use crate::convert::TryFrom;
55
use crate::error::Error;
66
use crate::fmt;
7+
use crate::intrinsics::assert_unsafe_precondition;
78
use crate::mem::transmute;
89
use crate::str::FromStr;
910

@@ -23,7 +24,13 @@ pub(super) const fn from_u32(i: u32) -> Option<char> {
2324
#[must_use]
2425
pub(super) const unsafe fn from_u32_unchecked(i: u32) -> char {
2526
// SAFETY: the caller must guarantee that `i` is a valid char value.
26-
if cfg!(debug_assertions) { char::from_u32(i).unwrap() } else { unsafe { transmute(i) } }
27+
unsafe {
28+
assert_unsafe_precondition!(
29+
"invalid value for `char`",
30+
(i: u32) => char_try_from_u32(i).is_ok()
31+
);
32+
transmute(i)
33+
}
2734
}
2835

2936
#[stable(feature = "char_convert", since = "1.13.0")]

0 commit comments

Comments
 (0)