Rollup merge of #131463 - bjoernager:const-char-encode-utf8, r=RalfJung

Stabilise `const_char_encode_utf8`.

Closes: #130512

This PR stabilises the `const_char_encode_utf8` feature gate (i.e. support for `char::encode_utf8` in const scenarios).

Note that the linked tracking issue is currently awaiting FCP.
This commit is contained in:
Trevor Gross 2024-10-11 16:53:49 -05:00 committed by GitHub
commit 8ea41b903f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 5 additions and 3 deletions

View File

@ -674,8 +674,9 @@ impl char {
/// 'ß'.encode_utf8(&mut b); /// 'ß'.encode_utf8(&mut b);
/// ``` /// ```
#[stable(feature = "unicode_encode_char", since = "1.15.0")] #[stable(feature = "unicode_encode_char", since = "1.15.0")]
#[rustc_const_unstable(feature = "const_char_encode_utf8", issue = "130512")] #[rustc_const_stable(feature = "const_char_encode_utf8", since = "CURRENT_RUSTC_VERSION")]
#[inline] #[inline]
#[cfg_attr(bootstrap, rustc_allow_const_fn_unstable(const_mut_refs))]
pub const fn encode_utf8(self, dst: &mut [u8]) -> &mut str { pub const fn encode_utf8(self, dst: &mut [u8]) -> &mut str {
// SAFETY: `char` is not a surrogate, so this is valid UTF-8. // SAFETY: `char` is not a surrogate, so this is valid UTF-8.
unsafe { from_utf8_unchecked_mut(encode_utf8_raw(self as u32, dst)) } unsafe { from_utf8_unchecked_mut(encode_utf8_raw(self as u32, dst)) }
@ -1770,9 +1771,11 @@ const fn len_utf16(code: u32) -> usize {
/// Panics if the buffer is not large enough. /// Panics if the buffer is not large enough.
/// A buffer of length four is large enough to encode any `char`. /// A buffer of length four is large enough to encode any `char`.
#[unstable(feature = "char_internals", reason = "exposed only for libstd", issue = "none")] #[unstable(feature = "char_internals", reason = "exposed only for libstd", issue = "none")]
#[rustc_const_unstable(feature = "const_char_encode_utf8", issue = "130512")] #[rustc_const_stable(feature = "const_char_encode_utf8", since = "CURRENT_RUSTC_VERSION")]
#[doc(hidden)] #[doc(hidden)]
#[inline] #[inline]
#[rustc_allow_const_fn_unstable(const_eval_select)]
#[cfg_attr(bootstrap, rustc_allow_const_fn_unstable(const_mut_refs))]
pub const fn encode_utf8_raw(code: u32, dst: &mut [u8]) -> &mut [u8] { 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) { const fn panic_at_const(_code: u32, _len: usize, _dst_len: usize) {
// Note that we cannot format in constant expressions. // Note that we cannot format in constant expressions.

View File

@ -118,7 +118,6 @@
#![feature(const_bigint_helper_methods)] #![feature(const_bigint_helper_methods)]
#![feature(const_black_box)] #![feature(const_black_box)]
#![feature(const_char_encode_utf16)] #![feature(const_char_encode_utf16)]
#![feature(const_char_encode_utf8)]
#![feature(const_eval_select)] #![feature(const_eval_select)]
#![feature(const_exact_div)] #![feature(const_exact_div)]
#![feature(const_fmt_arguments_new)] #![feature(const_fmt_arguments_new)]