diff options
| author | Gabriel Bjørnager Jensen <gabriel@achernar.io> | 2024-09-20 13:34:09 +0200 |
|---|---|---|
| committer | Gabriel Bjørnager Jensen <gabriel@achernar.io> | 2024-09-20 15:53:57 +0200 |
| commit | bfadadf78fc483aef5d24be97afb51997d5bf156 (patch) | |
| tree | 6f7fb0fa3a69b0773737efa76927145b36c0aacc | |
| parent | fb475e47594d089f1b670009ffcd38cea1544fb3 (diff) | |
| download | rust-bfadadf78fc483aef5d24be97afb51997d5bf156.tar.gz rust-bfadadf78fc483aef5d24be97afb51997d5bf156.zip | |
Address diagnostics regression for 'const_char_encode_utf8';
| -rw-r--r-- | library/core/src/char/methods.rs | 14 |
1 files changed, 12 insertions, 2 deletions
diff --git a/library/core/src/char/methods.rs b/library/core/src/char/methods.rs index fcaa91184d3..092d427ecea 100644 --- a/library/core/src/char/methods.rs +++ b/library/core/src/char/methods.rs @@ -1,6 +1,7 @@ //! impl char {} use super::*; +use crate::intrinsics::const_eval_select; use crate::slice; use crate::str::from_utf8_unchecked_mut; use crate::unicode::printable::is_printable; @@ -1762,6 +1763,15 @@ const fn len_utf8(code: u32) -> usize { #[doc(hidden)] #[inline] pub const fn encode_utf8_raw(code: u32, dst: &mut [u8]) -> &mut [u8] { + const fn panic_at_const(_code: u32, _len: usize, _dst_len: usize) { + // Note that we cannot format in constant expressions. + panic!("encode_utf8: buffer does not have enough bytes to encode code point"); + } + fn panic_at_rt(code: u32, len: usize, dst_len: usize) { + panic!( + "encode_utf8: need {len} bytes to encode U+{code:04X} but buffer has just {dst_len}", + ); + } let len = len_utf8(code); match (len, &mut *dst) { (1, [a, ..]) => { @@ -1782,8 +1792,8 @@ pub const fn encode_utf8_raw(code: u32, dst: &mut [u8]) -> &mut [u8] { *c = (code >> 6 & 0x3F) as u8 | TAG_CONT; *d = (code & 0x3F) as u8 | TAG_CONT; } - // Note that we cannot format in constant expressions. - _ => panic!("encode_utf8: buffer does not have enough bytes to encode code point"), + // FIXME(const-hack): We would prefer to have streamlined panics when formatters become const-friendly. + _ => const_eval_select((code, len, dst.len()), panic_at_const, panic_at_rt), }; // SAFETY: `<&mut [u8]>::as_mut_ptr` is guaranteed to return a valid pointer and `len` has been tested to be within bounds. unsafe { slice::from_raw_parts_mut(dst.as_mut_ptr(), len) } |
