-
-
Notifications
You must be signed in to change notification settings - Fork 14.9k
When matching on enums, read the discriminant even if there's only a single variant #154756
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
Open
meithecatte
wants to merge
3
commits into
rust-lang:main
Choose a base branch
from
meithecatte:always-always-discriminate
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
13 changes: 13 additions & 0 deletions
13
src/tools/miri/tests/fail/issue-120337-irrefutable-let-ice.stderr
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| error: Undefined Behavior: read discriminant of an uninhabited enum variant | ||
| --> tests/fail/issue-120337-irrefutable-let-ice.rs:LL:CC | ||
| | | ||
| LL | let E::A(ref _a) = unsafe { &(&U { u: () }).e }; | ||
| | ^^^^^^^^^^^^ Undefined Behavior occurred here | ||
| | | ||
| = help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior | ||
| = help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information | ||
|
|
||
| note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace | ||
|
|
||
| error: aborting due to 1 previous error | ||
|
|
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,32 +1,21 @@ | ||
| // Ideally, this would be UB regardless of #[non_exhaustive]. For now, | ||
| // at least the semantics don't depend on the crate you're in. | ||
| // | ||
| // See: rust-lang/rust#147722 | ||
| // | ||
| // This UB should be detected even with validation disabled. | ||
| //@compile-flags: -Zmiri-disable-validation | ||
| #![allow(dead_code)] | ||
|
|
||
| #[repr(u8)] | ||
| enum Exhaustive { | ||
| A(u8) = 42, | ||
| } | ||
|
|
||
| #[repr(u8)] | ||
| #[non_exhaustive] | ||
| enum NonExhaustive { | ||
| A(u8) = 42, | ||
| } | ||
|
|
||
| fn main() { | ||
| unsafe { | ||
| let x: &[u8; 2] = &[21, 37]; | ||
| let y: &Exhaustive = std::mem::transmute(x); | ||
| match y { | ||
| Exhaustive::A(_) => {} | ||
| } | ||
|
|
||
| let y: &NonExhaustive = std::mem::transmute(x); | ||
| match y { | ||
| //~^ ERROR: enum value has invalid tag | ||
| NonExhaustive::A(_) => {} | ||
| Exhaustive::A(_) => {} | ||
| } | ||
| } | ||
| } |
25 changes: 25 additions & 0 deletions
25
src/tools/miri/tests/fail/match/single_variant_non_exhaustive.rs
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| // Like single_variant.rs, but with a non_exhaustive enum, as the generated MIR used to differ | ||
| // between these cases. | ||
| // | ||
| // See: rust-lang/rust#147722 | ||
| // | ||
| // This UB should be detected even with validation disabled. | ||
| //@compile-flags: -Zmiri-disable-validation | ||
| #![allow(dead_code)] | ||
|
|
||
| #[repr(u8)] | ||
| #[non_exhaustive] | ||
| enum NonExhaustive { | ||
| A(u8) = 42, | ||
| } | ||
|
|
||
| fn main() { | ||
| unsafe { | ||
| let x: &[u8; 2] = &[21, 37]; | ||
| let y: &NonExhaustive = std::mem::transmute(x); | ||
| match y { | ||
| //~^ ERROR: enum value has invalid tag | ||
| NonExhaustive::A(_) => {} | ||
| } | ||
| } | ||
| } | ||
13 changes: 13 additions & 0 deletions
13
src/tools/miri/tests/fail/match/single_variant_non_exhaustive.stderr
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| error: Undefined Behavior: enum value has invalid tag: 0x15 | ||
| --> tests/fail/match/single_variant_non_exhaustive.rs:LL:CC | ||
| | | ||
| LL | match y { | ||
| | ^ Undefined Behavior occurred here | ||
| | | ||
| = help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior | ||
| = help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information | ||
|
|
||
| note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace | ||
|
|
||
| error: aborting due to 1 previous error | ||
|
|
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
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
29 changes: 29 additions & 0 deletions
29
src/tools/miri/tests/fail/match/single_variant_uninit_non_exhaustive.rs
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| // Like single_variant_uninit.rs, but with a non_exhaustive enum, as the generated MIR used to | ||
| // differ between these cases. | ||
| // | ||
| // See: rust-lang/rust#147722 | ||
| // | ||
| // This UB should be detected even with validation disabled. | ||
| //@compile-flags: -Zmiri-disable-validation | ||
| #![allow(dead_code)] | ||
| #![allow(unreachable_patterns)] | ||
|
|
||
| #[repr(u8)] | ||
| #[non_exhaustive] | ||
| enum NonExhaustive { | ||
| A(u8) = 0, | ||
| } | ||
|
|
||
| use std::mem::MaybeUninit; | ||
|
|
||
| fn main() { | ||
| let buffer: [MaybeUninit<u8>; 2] = [MaybeUninit::uninit(), MaybeUninit::new(0u8)]; | ||
| let nexh: *const NonExhaustive = (&raw const buffer).cast(); | ||
| unsafe { | ||
| match *nexh { | ||
| //~^ ERROR: memory is uninitialized | ||
| NonExhaustive::A(ref _val) => {} | ||
| _ => {} | ||
| } | ||
| } | ||
| } |
18 changes: 18 additions & 0 deletions
18
src/tools/miri/tests/fail/match/single_variant_uninit_non_exhaustive.stderr
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| error: Undefined Behavior: reading memory at ALLOC[0x0..0x1], but memory is uninitialized at [0x0..0x1], and this operation requires initialized memory | ||
| --> tests/fail/match/single_variant_uninit_non_exhaustive.rs:LL:CC | ||
| | | ||
| LL | match *nexh { | ||
| | ^^^^^ Undefined Behavior occurred here | ||
| | | ||
| = help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior | ||
| = help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information | ||
|
|
||
| Uninitialized memory occurred at ALLOC[0x0..0x1], in this allocation: | ||
| ALLOC (stack variable, size: 2, align: 1) { | ||
| __ 00 │ ░. | ||
| } | ||
|
|
||
| note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace | ||
|
|
||
| error: aborting due to 1 previous error | ||
|
|
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
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
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
Oops, something went wrong.
Oops, something went wrong.
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.
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.
This seems like it could use revisions to avoid duplicating the code? Same for "single_variant_uninit".
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.
Ah, I wasn't aware that revisions are a thing! I would've done that in the first place had I known about it.