-
-
Notifications
You must be signed in to change notification settings - Fork 14.4k
Make operational semantics of pattern matching independent of crate and module #150681
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
8
commits into
rust-lang:main
Choose a base branch
from
meithecatte: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.
+1,031
−255
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
8536979
Move some match edge case tests
meithecatte 4e09007
capture-enums.rs: get rid of feature gate noise
meithecatte 940a489
non-exhaustive-match.rs: actually test what the comments say
meithecatte a28b279
Make some mir-opt tests more resilient
meithecatte 628baca
add tests for emergent behavior of partial captures
meithecatte 3c17508
add tests for discriminant read borrowck
meithecatte ee1a6f4
match in closure: capture non_exhaustive even if defined in current c…
meithecatte af302a6
discriminant reads: make semantics independent of module/crate
meithecatte 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
25 changes: 25 additions & 0 deletions
25
src/tools/miri/tests/fail/match/all_variants_uninhabited.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 @@ | ||
| #![allow(deref_nullptr)] | ||
|
|
||
| enum Never {} | ||
|
|
||
| fn main() { | ||
| unsafe { | ||
| match *std::ptr::null::<Result<Never, Never>>() { | ||
| //~^ ERROR: read discriminant of an uninhabited enum variant | ||
| Ok(_) => { | ||
| lol(); | ||
| } | ||
| Err(_) => { | ||
| wut(); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| fn lol() { | ||
| println!("lol"); | ||
| } | ||
|
|
||
| fn wut() { | ||
| println!("wut"); | ||
| } |
13 changes: 13 additions & 0 deletions
13
src/tools/miri/tests/fail/match/all_variants_uninhabited.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/match/all_variants_uninhabited.rs:LL:CC | ||
| | | ||
| LL | match *std::ptr::null::<Result<Never, Never>>() { | ||
| | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 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 | ||
|
|
File renamed without changes.
2 changes: 1 addition & 1 deletion
2
...sts/fail/closures/deref-in-pattern.stderr → ...il/match/closures/deref-in-pattern.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
File renamed without changes.
2 changes: 1 addition & 1 deletion
2
...ests/fail/closures/partial-pattern.stderr → ...ail/match/closures/partial-pattern.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
File renamed without changes.
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
32 changes: 32 additions & 0 deletions
32
src/tools/miri/tests/fail/match/closures/uninhabited-variant2.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,32 @@ | ||
| // Motivated by rust-lang/rust#138961, this shows how invalid discriminants interact with | ||
| // closure captures. | ||
| // | ||
| // Test case with only one inhabited variant, for which rustc used to not emit | ||
| // a discriminant read in the first place. See: rust-lang/miri#4778 | ||
| #![feature(never_type)] | ||
|
|
||
| #[repr(C)] | ||
| #[allow(dead_code)] | ||
| enum E { | ||
| V0, // discriminant: 0 | ||
| V1(!), // 1 | ||
| } | ||
|
|
||
| fn main() { | ||
| assert_eq!(std::mem::size_of::<E>(), 4); | ||
|
|
||
| let val = 1u32; | ||
| let ptr = (&raw const val).cast::<E>(); | ||
| let r = unsafe { &*ptr }; | ||
| let f = || { | ||
| // After rust-lang/rust#138961, constructing the closure performs a reborrow of r. | ||
| // Nevertheless, the discriminant is only actually inspected when the closure | ||
| // is called. | ||
| match r { //~ ERROR: read discriminant of an uninhabited enum variant | ||
| E::V0 => {} | ||
| E::V1(_) => {} | ||
| } | ||
| }; | ||
|
|
||
| f(); | ||
| } |
18 changes: 18 additions & 0 deletions
18
src/tools/miri/tests/fail/match/closures/uninhabited-variant2.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: read discriminant of an uninhabited enum variant | ||
| --> tests/fail/match/closures/uninhabited-variant2.rs:LL:CC | ||
| | | ||
| LL | match r { | ||
| | ^ 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: stack backtrace: | ||
| 0: main::{closure#0} | ||
| at tests/fail/match/closures/uninhabited-variant2.rs:LL:CC | ||
| 1: main | ||
| at tests/fail/match/closures/uninhabited-variant2.rs:LL:CC | ||
|
|
||
| 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| // rust-lang/miri#4778 | ||
| #![feature(never_type)] | ||
|
|
||
| #[repr(C)] | ||
| #[allow(dead_code)] | ||
| enum E { | ||
| V0, // discriminant: 0 | ||
| V1(!), // 1 | ||
| } | ||
|
|
||
| fn main() { | ||
| assert_eq!(std::mem::size_of::<E>(), 4); | ||
|
|
||
| let val = 1u32; | ||
| let ptr = (&raw const val).cast::<E>(); | ||
| let r = unsafe { &*ptr }; | ||
| match r { //~ ERROR: read discriminant of an uninhabited enum variant | ||
| E::V0 => {} | ||
| E::V1(_) => {} | ||
| } | ||
| } |
13 changes: 13 additions & 0 deletions
13
src/tools/miri/tests/fail/match/only_inhabited_variant.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/match/only_inhabited_variant.rs:LL:CC | ||
| | | ||
| LL | match r { | ||
| | ^ 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| // 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 | ||
| #![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(_) => {}, | ||
| } | ||
| } | ||
| } |
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.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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| // 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 | ||
| #![allow(dead_code)] | ||
| #![allow(unreachable_patterns)] | ||
|
|
||
| #[repr(u8)] | ||
| enum Exhaustive { | ||
| A(u8) = 0, | ||
| } | ||
|
|
||
| #[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 exh: *const Exhaustive = (&raw const buffer).cast(); | ||
| let nexh: *const NonExhaustive = (&raw const buffer).cast(); | ||
| unsafe { | ||
| match *exh { | ||
| Exhaustive::A(ref _val) => {} | ||
| _ => {} | ||
| } | ||
|
|
||
| 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.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.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
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.
The "validity" folder is about validity invariants, which is not what these tests are about.
I'd say let's make a new folder "match" for these tests. And probably the "closures" tests should all go there since really they are about pattern matching on closure captures.