-
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
Tracking issue for const char conversions #89259
Comments
This comment was marked as off-topic.
This comment was marked as off-topic.
Make char conversion functions unstably const The char conversion functions like `char::from_u32` do trivial computations and can easily be converted into const fns. Only smaller tricks are needed to avoid non-const standard library functions like `Result::ok` or `bool::then_some`. Tracking issue: rust-lang#89259
Make char conversion functions unstably const The char conversion functions like `char::from_u32` do trivial computations and can easily be converted into const fns. Only smaller tricks are needed to avoid non-const standard library functions like `Result::ok` or `bool::then_some`. Tracking issue: rust-lang#89259
Hi! Is there any plan of stabilization? |
…, r=joshtriplett Stabilize const char convert Split out `const_char_from_u32_unchecked` from `const_char_convert` and stabilize the rest, i.e. stabilize the following functions: ```Rust impl char { pub const fn from_u32(self, i: u32) -> Option<char>; pub const fn from_digit(self, num: u32, radix: u32) -> Option<char>; pub const fn to_digit(self, radix: u32) -> Option<u32>; } // Available through core::char and std::char mod char { pub const fn from_u32(i: u32) -> Option<char>; pub const fn from_digit(num: u32, radix: u32) -> Option<char>; } ``` And put the following under the `from_u32_unchecked` const stability gate as it needs `Option::unwrap` which isn't const-stable (yet): ```Rust impl char { pub const unsafe fn from_u32_unchecked(i: u32) -> char; } // Available through core::char and std::char mod char { pub const unsafe fn from_u32_unchecked(i: u32) -> char; } ``` cc the tracking issue rust-lang#89259 (which I'd like to keep open for `const_char_from_u32_unchecked`).
…, r=joshtriplett Stabilize const char convert Split out `const_char_from_u32_unchecked` from `const_char_convert` and stabilize the rest, i.e. stabilize the following functions: ```Rust impl char { pub const fn from_u32(self, i: u32) -> Option<char>; pub const fn from_digit(self, num: u32, radix: u32) -> Option<char>; pub const fn to_digit(self, radix: u32) -> Option<u32>; } // Available through core::char and std::char mod char { pub const fn from_u32(i: u32) -> Option<char>; pub const fn from_digit(num: u32, radix: u32) -> Option<char>; } ``` And put the following under the `from_u32_unchecked` const stability gate as it needs `Option::unwrap` which isn't const-stable (yet): ```Rust impl char { pub const unsafe fn from_u32_unchecked(i: u32) -> char; } // Available through core::char and std::char mod char { pub const unsafe fn from_u32_unchecked(i: u32) -> char; } ``` cc the tracking issue rust-lang#89259 (which I'd like to keep open for `const_char_from_u32_unchecked`).
…, r=joshtriplett Stabilize const char convert Split out `const_char_from_u32_unchecked` from `const_char_convert` and stabilize the rest, i.e. stabilize the following functions: ```Rust impl char { pub const fn from_u32(self, i: u32) -> Option<char>; pub const fn from_digit(self, num: u32, radix: u32) -> Option<char>; pub const fn to_digit(self, radix: u32) -> Option<u32>; } // Available through core::char and std::char mod char { pub const fn from_u32(i: u32) -> Option<char>; pub const fn from_digit(num: u32, radix: u32) -> Option<char>; } ``` And put the following under the `from_u32_unchecked` const stability gate as it needs `Option::unwrap` which isn't const-stable (yet): ```Rust impl char { pub const unsafe fn from_u32_unchecked(i: u32) -> char; } // Available through core::char and std::char mod char { pub const unsafe fn from_u32_unchecked(i: u32) -> char; } ``` cc the tracking issue rust-lang#89259 (which I'd like to keep open for `const_char_from_u32_unchecked`).
I think the idea was that there should be no extra hacks done just to get things compatible with const fn, but I don't remember it fully any more. |
I've opened #118979 to see what T-libs thinks about it. |
#118979 is now merged so I think there's no blocker for |
@rust-lang/libs-api: I propose stabilizing These functions were left out of the initial set of If fn main() {
let _ = const { unsafe { char::from_u32_unchecked(0xd800) } };
} error[E0080]: it is undefined behavior to use this value
--> src/main.rs:2:19
|
2 | let _ = const { unsafe { char::from_u32_unchecked(0xd800) } };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value: encountered 0x0000d800, but expected a valid unicode scalar value (in `0..=0x10FFFF` but not in `0xD800..=0xDFFF`)
|
= note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior.
= note: the raw bytes of the constant (size: 4, align: 4) {
00 d8 00 00 │ ....
}
note: erroneous constant encountered
--> src/main.rs:2:13
|
2 | let _ = const { unsafe { char::from_u32_unchecked(0xd800) } };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
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. |
🔔 This is now entering its final comment period, as per the review above. 🔔 |
Note that this is the error you get when the final value of the const is an invalid If you ignore the return value of |
The final comment period, with a disposition to merge, as per the review above, 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. |
Stabilize const unchecked conversion from u32 to char Closes rust-lang/rust#89259. The functions in this PR were left out of the initial set of `feature(const_char_convert)` stabilizations in rust-lang/rust#102470, but have since been unblocked by rust-lang/rust#118979. If `unsafe { from_u32_unchecked(u) }` is called in const with a value for which `from_u32(u)` returns None, we get the following compile error. ```rust fn main() { let _ = const { unsafe { char::from_u32_unchecked(0xd800) } }; } ``` ```console error[E0080]: it is undefined behavior to use this value --> src/main.rs:2:19 | 2 | let _ = const { unsafe { char::from_u32_unchecked(0xd800) } }; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value: encountered 0x0000d800, but expected a valid unicode scalar value (in `0..=0x10FFFF` but not in `0xD800..=0xDFFF`) | = note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior. = note: the raw bytes of the constant (size: 4, align: 4) { 00 d8 00 00 │ .... } note: erroneous constant encountered --> src/main.rs:2:13 | 2 | let _ = const { unsafe { char::from_u32_unchecked(0xd800) } }; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ```
Stabilize const unchecked conversion from u32 to char Closes rust-lang/rust#89259. The functions in this PR were left out of the initial set of `feature(const_char_convert)` stabilizations in rust-lang/rust#102470, but have since been unblocked by rust-lang/rust#118979. If `unsafe { from_u32_unchecked(u) }` is called in const with a value for which `from_u32(u)` returns None, we get the following compile error. ```rust fn main() { let _ = const { unsafe { char::from_u32_unchecked(0xd800) } }; } ``` ```console error[E0080]: it is undefined behavior to use this value --> src/main.rs:2:19 | 2 | let _ = const { unsafe { char::from_u32_unchecked(0xd800) } }; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value: encountered 0x0000d800, but expected a valid unicode scalar value (in `0..=0x10FFFF` but not in `0xD800..=0xDFFF`) | = note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior. = note: the raw bytes of the constant (size: 4, align: 4) { 00 d8 00 00 │ .... } note: erroneous constant encountered --> src/main.rs:2:13 | 2 | let _ = const { unsafe { char::from_u32_unchecked(0xd800) } }; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ```
This is a tracking issue for const char conversions. List of functions belonging to
!#[feature(const_char_from_u32_unchecked)]
:List of functions belonging to stable#![feature(const_char_convert)]
:Steps:
const_char_from_u32_unchecked
is still unstable.Stabilization is probably blocked by const panicing.Edit: const panic is stable now!Stabilization is blocked by Option::unwrap const stability, which is blocked by Tracking issue for#![feature(const_precise_live_drops)]
#73255.The text was updated successfully, but these errors were encountered: