-
Notifications
You must be signed in to change notification settings - Fork 12.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make char and u8 methods const #79549
Conversation
`escape_unicode`, `escape_default`, `len_utf8`, `len_utf16`, to_ascii_lowercase`, `eq_ignore_ascii_case` `u8` methods `to_ascii_lowercase`, `to_ascii_uppercase` also must be made const
Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @sfackler (or someone else) soon. If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes. Please see the contribution instructions for more information. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM but someone from T-libs should make sure the standard library wants to commit to these being const.
#[inline] | ||
pub fn escape_unicode(self) -> EscapeUnicode { | ||
pub const fn escape_unicode(self) -> EscapeUnicode { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When would this ever be called in a const context given that it returns an iterator?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point, I didn't really consider the practicality for making it const. I'm not sure about the downsides of making it callable in const context, but I'm totally fine with removing it. It'll probably be a good while before const iterators happen anyway.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure about the downsides of making it callable in const context, but I'm totally fine with removing it.
Yeah, please remove the iterator functions.
cc @rust-lang/wg-const-eval |
#[inline] | ||
pub fn escape_default(self) -> EscapeDefault { | ||
pub const fn escape_default(self) -> EscapeDefault { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same here. I think we should not make functions const fn
for now unless their result can actually be used for anything during const eval or there's a good reason to have a constant version of it. Unfortunately we are not yet at the "because we can" stage of making things const
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This concern seems to still be outstanding.
In case you folks are looking for a use case for getting this PR over the line, The main part of this implementation is a giant match statement that maps |
This commit does everything except add the `const` annotation to the function signature. Adding `const` is blocked on rust-lang/rust#79549. See #49.
Nominating for stabilization (except for the iterator constructor functions). These functions can all be written in user code today by copy pasting the libcore implementation into user code and adding |
FWIW, have we done insta-stabilizations before for const things? |
@YenForYang Can you remove the
Not that I know. But having things unstable first is mostly useful to see if there is a better interface, better names, or there turn out to be some unforseen problems with it, or anything else that we could only learn from having some experience with it first. But I think none of that really applies for making simple functions like this const. |
@YenForYang Ping from triage! Would you mind address the comments above? Thanks! |
Would it be permissible for me to submit a new PR based on this one with the concerns addressed so this can get merged? |
@lopopolo Sure. Note that we're at 1.52.0 now (for the |
I've rebased, squashed, and made the suggested changes in a new PR in the interests of getting this merged: #82078. |
Make char and u8 methods const char methods `len_utf8`, `len_utf16`, `to_ascii_lowercase`, `eq_ignore_ascii_case` can be made const. `u8` methods `to_ascii_lowercase`, `to_ascii_uppercase` are required to be const as well. `u8::eq_ignore_ascii_case` was additionally made const. Rebase of rust-lang#79549 originally authored by `@YenForYang.` Changes from that PR: - Squashed all commits from rust-lang#79549. - rebased to latest upstream master. - Removed const attributes for `char::escape_unicode` and `char::escape_default`. - Updated `since` attributes for `const` stabilization to 1.52.0. cc `@m-ou-se.`
Make char and u8 methods const char methods `len_utf8`, `len_utf16`, `to_ascii_lowercase`, `eq_ignore_ascii_case` can be made const. `u8` methods `to_ascii_lowercase`, `to_ascii_uppercase` are required to be const as well. `u8::eq_ignore_ascii_case` was additionally made const. Rebase of rust-lang#79549 originally authored by ``@YenForYang.`` Changes from that PR: - Squashed all commits from rust-lang#79549. - rebased to latest upstream master. - Removed const attributes for `char::escape_unicode` and `char::escape_default`. - Updated `since` attributes for `const` stabilization to 1.52.0. cc ``@m-ou-se.``
char methods
escape_unicode
,escape_default
,len_utf8
,len_utf16
,to_ascii_lowercase
,eq_ignore_ascii_case
can be made const.u8
methodsto_ascii_lowercase
,to_ascii_uppercase
are required to be const as well.u8::eq_ignore_ascii_case
was additionally made const.