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

Use assert_unsafe_precondition for char::from_u32_unchecked #118979

Merged
merged 1 commit into from
Jan 9, 2024

Conversation

ChrisDenton
Copy link
Member

@ChrisDenton ChrisDenton commented Dec 15, 2023

Use assert_unsafe_precondition in char::from_u32_unchecked so that it can be stabilized as const.

@rustbot
Copy link
Collaborator

rustbot commented Dec 15, 2023

r? @cuviper

(rustbot has picked a reviewer for you, use r? to override)

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Dec 15, 2023
@ChrisDenton
Copy link
Member Author

The context for this is #89259 (comment) (in the issue linked above this post).

Copy link
Member

@joboet joboet left a 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.

library/core/src/char/convert.rs Outdated Show resolved Hide resolved
@ChrisDenton ChrisDenton changed the title Use const_unwrap for char::from_u32_unchecked Use assert_unsafe_precondition for char::from_u32_unchecked Dec 15, 2023
@ChrisDenton
Copy link
Member Author

Updated the PR. Also update the OP because it appears in the merge commit. The original can be seen in the edit history.

Copy link
Member

@dtolnay dtolnay left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dtolnay
Copy link
Member

dtolnay commented Jan 8, 2024

@bors r=Nilstrieb,dtolnay

@bors
Copy link
Contributor

bors commented Jan 8, 2024

📌 Commit ab716d0 has been approved by Nilstrieb,dtolnay

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jan 8, 2024
@dtolnay dtolnay assigned dtolnay and unassigned cuviper Jan 8, 2024
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Jan 8, 2024
…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`.
bors added a commit to rust-lang-ci/rust that referenced this pull request Jan 9, 2024
…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
bors added a commit to rust-lang-ci/rust that referenced this pull request Jan 9, 2024
…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
@bors bors merged commit b0c492c into rust-lang:master Jan 9, 2024
11 checks passed
@rustbot rustbot added this to the 1.77.0 milestone Jan 9, 2024
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request Jan 9, 2024
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`.
@ChrisDenton ChrisDenton deleted the unwrap-const branch January 9, 2024 18:18
bors added a commit to rust-lang-ci/rust that referenced this pull request Jul 13, 2024
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) } };
  |             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
```
github-actions bot pushed a commit to rust-lang/miri that referenced this pull request Jul 16, 2024
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) } };
  |             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
```
lnicola pushed a commit to lnicola/rust-analyzer that referenced this pull request Jul 16, 2024
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) } };
  |             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants