Skip to content

Commit 947d5c8

Browse files
committed
Address diagnostics regression for 'const_char_encode_utf8';
1 parent fb475e4 commit 947d5c8

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

library/core/src/char/methods.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -1762,6 +1762,13 @@ const fn len_utf8(code: u32) -> usize {
17621762
#[doc(hidden)]
17631763
#[inline]
17641764
pub const fn encode_utf8_raw(code: u32, dst: &mut [u8]) -> &mut [u8] {
1765+
const fn panic_at_const(_code: u32, _len: usize, _dst_len: usize) {
1766+
// Note that we cannot format in constant expressions.
1767+
panic!("encode_utf8: buffer does not have enough bytes to encode code point")
1768+
}
1769+
fn panic_at_rt(code: u32, len: usize, dst_len: usize) {
1770+
panic!("encode_utf8: need {len} bytes to encode U+{code:04X} but buffer has just {dst_len}")
1771+
}
17651772
let len = len_utf8(code);
17661773
match (len, &mut *dst) {
17671774
(1, [a, ..]) => {
@@ -1782,8 +1789,7 @@ pub const fn encode_utf8_raw(code: u32, dst: &mut [u8]) -> &mut [u8] {
17821789
*c = (code >> 6 & 0x3F) as u8 | TAG_CONT;
17831790
*d = (code & 0x3F) as u8 | TAG_CONT;
17841791
}
1785-
// Note that we cannot format in constant expressions.
1786-
_ => panic!("encode_utf8: buffer does not have enough bytes to encode code point"),
1792+
_ => const_eval_select((code, len, dst.len()), panic_at_const, panic_at_rt),
17871793
};
17881794
// SAFETY: `<&mut [u8]>::as_mut_ptr` is guaranteed to return a valid pointer and `len` has been tested to be within bounds.
17891795
unsafe { slice::from_raw_parts_mut(dst.as_mut_ptr(), len) }

0 commit comments

Comments
 (0)