Open bjoernager opened 2 weeks ago
@rfcbot fcp merge
const
for https://doc.rust-lang.org/nightly/core/primitive.char.html#method.encode_utf8
Team member @dtolnay has proposed to merge this. The next step is review by the rest of the tagged team members:
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 this document for info about what commands tagged team members can give me.
I'll note that in the implementation PR, making this const seems to require making the panic error message worse (even in the case where encode_utf8
isn't used in a const
context) and also introducing more uses of unsafe
. 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.
@RalfJung has also noted that core::intrinsics::const_eval_select
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 from_raw_parts. That would avoid introducing new unsafe reasoning in encode_utf8 itself.
@bjoernager 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 review above. :bell:
Feature gate:
#![feature(const_char_encode_utf8)]
This is a tracking issue for supporting
char::encode_utf8
in const scenarios.Public API
Steps / History
Unresolved Questions