Generate safe stable code for derives on empty enums#113770
Merged
bors merged 2 commits intorust-lang:masterfrom Jul 17, 2023
Merged
Generate safe stable code for derives on empty enums#113770bors merged 2 commits intorust-lang:masterfrom
bors merged 2 commits intorust-lang:masterfrom
Conversation
Generate `match *self {}` instead of `unsafe { core::intrinsics::unreachable() }`.
This is:
1. safe
2. stable
for the benefit of everyone looking at these derived impls through `cargo expand`.
Both expansions compile to the same code at all optimization levels (including `0`).
Collaborator
|
r? @b-naber (rustbot has picked a reviewer for you, use r? to override) |
Collaborator
|
Changes to the code generated for builtin derived traits. cc @nnethercote |
compiler-errors
approved these changes
Jul 16, 2023
Contributor
|
very nice r? @compiler-errors @bors r+ |
Collaborator
Contributor
|
I was just about to request a tiny change. @bors r- |
nnethercote
reviewed
Jul 16, 2023
| // There is no sensible code to be generated for *any* deriving on a | ||
| // zero-variant enum. So we just generate a failing expression. | ||
| // For zero-variant enum, this function body is unreachable. | ||
| // Generate `match *self {}`. |
Contributor
There was a problem hiding this comment.
Can you add something like this: "This results in the same generated code as unsafe { core::intrinsics::unreachable() } while also being safe and stable."
Thanks.
@bors delegate=dtolnay
Collaborator
|
✌️ @dtolnay, you can now approve this pull request! If @nnethercote told you to " |
Member
Author
|
@bors r=compiler-errors,nnethercote |
Collaborator
Contributor
|
@bors rollup |
matthiaskrgr
added a commit
to matthiaskrgr/rust
that referenced
this pull request
Jul 17, 2023
…ors,nnethercote
Generate safe stable code for derives on empty enums
Generate `match *self {}` instead of `unsafe { core::intrinsics::unreachable() }`.
This is:
1. safe
2. stable
for the benefit of everyone looking at these derived impls through `cargo expand`.
[Both expansions compile to the same code at all optimization levels (including `0`).](https://rust.godbolt.org/z/P79joGMh3)
bors
added a commit
to rust-lang-ci/rust
that referenced
this pull request
Jul 17, 2023
…iaskrgr Rollup of 4 pull requests Successful merges: - rust-lang#112741 (fix typo in `rustdoc/src/what-is-rustdoc.md`) - rust-lang#113535 (Add a sparc-unknown-none-elf target.) - rust-lang#113651 (self type param infer, avoid ICE) - rust-lang#113770 (Generate safe stable code for derives on empty enums) r? `@ghost` `@rustbot` modify labels: rollup
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Generate
match *self {}instead ofunsafe { core::intrinsics::unreachable() }.This is:
for the benefit of everyone looking at these derived impls through
cargo expand.Both expansions compile to the same code at all optimization levels (including
0).