Skip to content
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

Closed
wants to merge 9 commits into from
Closed

Conversation

YenForYang
Copy link
Contributor

@YenForYang YenForYang commented Nov 30, 2020

char methods escape_unicode, escape_default, 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.

`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
@rust-highfive
Copy link
Collaborator

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.

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Nov 30, 2020
@jyn514 jyn514 added A-const-fn Area: const fn foo(..) {..}. Pure functions which can be applied at compile time. needs-fcp This change is insta-stable, so needs a completed FCP to proceed. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. labels Nov 30, 2020
@YenForYang YenForYang marked this pull request as ready for review December 1, 2020 21:28
Copy link
Member

@jyn514 jyn514 left a 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 {
Copy link
Member

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?

Copy link
Contributor Author

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.

Copy link
Member

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.

@oli-obk
Copy link
Contributor

oli-obk commented Dec 2, 2020

cc @rust-lang/wg-const-eval

#[inline]
pub fn escape_default(self) -> EscapeDefault {
pub const fn escape_default(self) -> EscapeDefault {
Copy link
Contributor

@oli-obk oli-obk Dec 2, 2020

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

Copy link
Member

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.

@lopopolo
Copy link
Contributor

In case you folks are looking for a use case for getting this PR over the line, focaccia is a crate on crates.io that implements several Unicode case folding strategies.

The main part of this implementation is a giant match statement that maps chars to byte sequences representing the case folded result. This function is almost able to be made const, but I'm missing support for char::to_ascii_lowercase: artichoke/focaccia#49.

lopopolo added a commit to artichoke/focaccia that referenced this pull request Dec 30, 2020
This commit does everything except add the `const` annotation to the
function signature. Adding `const` is blocked on rust-lang/rust#79549.

See #49.
@oli-obk
Copy link
Contributor

oli-obk commented Dec 30, 2020

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 const to them. Thus they don't really need any deeper analysis for whether we'll always want to keep them as const, even if some internal feature does not end up getting stabilized.

@RalfJung
Copy link
Member

RalfJung commented Dec 30, 2020

FWIW, have we done insta-stabilizations before for const things?

@m-ou-se
Copy link
Member

m-ou-se commented Jan 6, 2021

@YenForYang Can you remove the const from escape_default and escape_unicode, as suggested above? Then I'll kick off the stabilization process.

FWIW, have we done insta-stabilizations before for const things?

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.

@m-ou-se m-ou-se assigned m-ou-se and unassigned sfackler Jan 6, 2021
@m-ou-se m-ou-se added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jan 6, 2021
@crlf0710
Copy link
Member

@YenForYang Ping from triage! Would you mind address the comments above? Thanks!

@lopopolo
Copy link
Contributor

Would it be permissible for me to submit a new PR based on this one with the concerns addressed so this can get merged?

@m-ou-se
Copy link
Member

m-ou-se commented Feb 13, 2021

@lopopolo Sure. Note that we're at 1.52.0 now (for the since = ".." attribute).

@lopopolo
Copy link
Contributor

I've rebased, squashed, and made the suggested changes in a new PR in the interests of getting this merged: #82078.

@m-ou-se m-ou-se closed this Feb 13, 2021
Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this pull request Feb 24, 2021
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.`
Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this pull request Feb 25, 2021
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.``
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-const-fn Area: const fn foo(..) {..}. Pure functions which can be applied at compile time. needs-fcp This change is insta-stable, so needs a completed FCP to proceed. S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants