-
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
Use assert_unsafe_precondition
for char::from_u32_unchecked
#118979
Conversation
r? @cuviper (rustbot has picked a reviewer for you, use r? to override) |
The context for this is #89259 (comment) (in the issue linked above this post). |
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.
Why don't we use assert_unsafe_precondition
for this? IIUC, it was made for these purposes.
Co-Authored-By: joboet <jonasboettiger@icloud.com>
218883a
to
ab716d0
Compare
const_unwrap
for char::from_u32_unchecked
assert_unsafe_precondition
for char::from_u32_unchecked
Updated the PR. Also update the OP because it appears in the merge commit. The original can be seen in the edit history. |
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.
Thank you!
@bors r=Nilstrieb,dtolnay |
…ieb,dtolnay Use `assert_unsafe_precondition` for `char::from_u32_unchecked` Use `assert_unsafe_precondition` in `char::from_u32_unchecked` so that it can be stabilized as `const`.
…iaskrgr Rollup of 8 pull requests Successful merges: - rust-lang#117744 (Add -Zuse-sync-unwind) - rust-lang#118649 (Make inductive cycles in coherence ambiguous always) - rust-lang#118979 (Use `assert_unsafe_precondition` for `char::from_u32_unchecked`) - rust-lang#119562 (Rename `pointer` field on `Pin`) - rust-lang#119619 (mir-opt and custom target fixes) - rust-lang#119632 (Fix broken build for ESP IDF due to rust-lang#119026) - rust-lang#119712 (Adding alignment to the cases to test for specific error messages.) - rust-lang#119734 (Miri subtree update) r? `@ghost` `@rustbot` modify labels: rollup
…iaskrgr Rollup of 7 pull requests Successful merges: - rust-lang#117744 (Add -Zuse-sync-unwind) - rust-lang#118649 (Make inductive cycles in coherence ambiguous always) - rust-lang#118979 (Use `assert_unsafe_precondition` for `char::from_u32_unchecked`) - rust-lang#119619 (mir-opt and custom target fixes) - rust-lang#119632 (Fix broken build for ESP IDF due to rust-lang#119026) - rust-lang#119712 (Adding alignment to the cases to test for specific error messages.) - rust-lang#119734 (Miri subtree update) r? `@ghost` `@rustbot` modify labels: rollup
Rollup merge of rust-lang#118979 - ChrisDenton:unwrap-const, r=Nilstrieb,dtolnay Use `assert_unsafe_precondition` for `char::from_u32_unchecked` Use `assert_unsafe_precondition` in `char::from_u32_unchecked` so that it can be stabilized as `const`.
Stabilize const unchecked conversion from u32 to char Closes rust-lang#89259. The functions in this PR were left out of the initial set of `feature(const_char_convert)` stabilizations in rust-lang#102470, but have since been unblocked by rust-lang#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) } }; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ```
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) } }; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ```
Use
assert_unsafe_precondition
inchar::from_u32_unchecked
so that it can be stabilized asconst
.