Change:
Feature gate: This is a tracking issue for supporting in const scenarios. [x] Implementation: [x] Address diagnostics: [x] Final comment period (FCP) [x] Stabilization PRNone yet.
fcp merge for
Team member has proposed to merge this. The next step is review by the rest of the tagged team members: [x] [x] [x] [ ] [ ] No concerns currently listed. Once a majority of reviewers approve (and at most 2 approvals are outstanding), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up! See for info about what commands tagged team members can give me.
I'll note that in the , making this const seems to require making the panic error message worse (even in the case where isn't used in a context) and also introducing more uses of . I'm not especially happy about that.
The use of unsafe code cannot be avoided as long as slice indexes aren't const-friendly (AFAIK). But I would argue that this use is properly documented and (relatively) invulnerable to UB, although I am open to other views on this. has also noted that could be used to regain full diagnostics in non-const calls – a feature which I would be more than happy trying to implement.
We could have a safe "const fn subslice" that does the bounds check and then uses unsafe fromrawparts. That would avoid introducing new unsafe reasoning in encode_utf8 itself.
resolves the diagnostics question. Can this be without conflicting with the FCP?
Yeah that should be fine. Thank you! I appreciate that. :-)
Plesse add a FIXME(const-hack) next to the const-eval-select to document that it should be removed again in the future, when possible.
Done. :)
:bell: This is now entering its final comment period, as per the . :bell:
Am I supposed to draft a stabilisation PR or is that outside my domain?
If you want to then yes you can draft a stabilization PR. :) Please link it to this issue and mention that it must only land once FCP has completed.
stabilises this.
The final comment period, with a disposition to merge, as per the , is now complete. As the automated representative of the governance process, I would like to thank the author for their work and everyone else who contributed. This will be merged soon.

Discussion:
/// 'ß'.encode_utf8(&mut b); /// ``` #[stable(feature = "unicode_encode_char", since = "1.15.0")] <del> #[rustc_const_unstable(feature = "const_char_encode_utf8", issue = "130512")] </del> <ins> #[rustc_const_stable(feature = "const_char_encode_utf8", since = "CURRENT_RUSTC_VERSION")] </ins> #[inline] <ins> #[cfg_attr(bootstrap, rustc_allow_const_fn_unstable(const_mut_refs))] </ins> pub const fn encode_utf8(self, dst: &mut [u8]) -> &mut str { // SAFETY: `char` is not a surrogate, so this is valid UTF-8. unsafe { from_utf8_unchecked_mut(encode_utf8_raw(self as u32, dst)) }

/// Panics if the buffer is not large enough. /// A buffer of length four is large enough to encode any `char`. #[unstable(feature = "char_internals", reason = "exposed only for libstd", issue = "none")] <del> #[rustc_const_unstable(feature = "const_char_encode_utf8", issue = "130512")] </del> <ins> #[rustc_const_stable(feature = "const_char_encode_utf8", since = "CURRENT_RUSTC_VERSION")] </ins> #[doc(hidden)] #[inline] <ins> #[rustc_allow_const_fn_unstable(const_eval_select)] #[cfg_attr(bootstrap, rustc_allow_const_fn_unstable(const_mut_refs))] </ins> 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.

#![feature(const_bigint_helper_methods)] #![feature(const_black_box)] #![feature(const_char_encode_utf16)] <del> #![feature(const_char_encode_utf8)] </del> #![feature(const_eval_select)] #![feature(const_exact_div)] #![feature(const_float_classify)]