-
Notifications
You must be signed in to change notification settings - Fork 13.2k
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
WIP: Initial implementation of or-pattern handling in MIR #63688
Closed
Closed
Changes from all commits
Commits
Show all changes
2 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 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 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 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 @@ | ||
// Test basic or-patterns when the target pattern type will be lowered to a | ||
// `SwitchInt` (an `enum`). | ||
// run-pass | ||
dlrobertson marked this conversation as resolved.
Show resolved
Hide resolved
|
||
#![feature(or_patterns)] | ||
//~^ WARN the feature `or_patterns` is incomplete and may cause the compiler to crash | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you |
||
|
||
#[derive(Debug)] | ||
enum Test { | ||
Foo, | ||
Bar, | ||
Baz, | ||
Qux | ||
} | ||
|
||
fn test(x: Option<Test>) -> bool { | ||
match x { | ||
// most simple case | ||
Some(Test::Bar | Test::Qux) => true, | ||
// wild case | ||
Some(_) => false, | ||
// empty case | ||
None => false, | ||
} | ||
} | ||
|
||
fn main() { | ||
assert!(!test(Some(Test::Foo))); | ||
assert!(test(Some(Test::Bar))); | ||
assert!(!test(Some(Test::Baz))); | ||
assert!(test(Some(Test::Qux))); | ||
assert!(!test(None)) | ||
} |
This file contains 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,8 @@ | ||
warning: the feature `or_patterns` is incomplete and may cause the compiler to crash | ||
--> $DIR/basic-switch.rs:4:12 | ||
| | ||
LL | #![feature(or_patterns)] | ||
| ^^^^^^^^^^^ | ||
| | ||
= note: `#[warn(incomplete_features)]` on by default | ||
|
This file contains 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,56 @@ | ||
// Test basic or-patterns when the target pattern type will be lowered to | ||
// a `Switch`. This will happen when the target type is an integer. | ||
// run-pass | ||
dlrobertson marked this conversation as resolved.
Show resolved
Hide resolved
|
||
#![feature(or_patterns)] | ||
//~^ WARN the feature `or_patterns` is incomplete and may cause the compiler to crash | ||
|
||
#[derive(Debug, PartialEq)] | ||
enum MatchArm { | ||
Arm(usize), | ||
Wild | ||
} | ||
|
||
#[derive(Debug)] | ||
enum Foo { | ||
One(usize), | ||
Two(usize, usize), | ||
} | ||
|
||
fn test_foo(x: Foo) -> MatchArm { | ||
match x { | ||
// normal pattern. | ||
Foo::One(0) | Foo::One(1) | Foo::One(2) => MatchArm::Arm(0), | ||
// most simple or-pattern. | ||
Foo::One(42 | 255) => MatchArm::Arm(1), | ||
// multiple or-patterns for one structure. | ||
Foo::Two(42 | 255, 1024 | 2048) => MatchArm::Arm(2), | ||
// mix of pattern types in one or-pattern (range). | ||
// | ||
// FIXME(dlrobertson | Nadrieril): Fix or-pattern completeness and | ||
Centril marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// unreachabilitychecks for ranges. | ||
Foo::One(100 | 110..=120 | 210..=220) => MatchArm::Arm(3), | ||
// multiple or-patterns with wild. | ||
Foo::Two(0..=10 | 100..=110, 0 | _) => MatchArm::Arm(4), | ||
// wild | ||
_ => MatchArm::Wild | ||
} | ||
} | ||
|
||
fn main() { | ||
// `Foo` tests. | ||
assert_eq!(test_foo(Foo::One(0)), MatchArm::Arm(0)); | ||
assert_eq!(test_foo(Foo::One(42)), MatchArm::Arm(1)); | ||
assert_eq!(test_foo(Foo::One(43)), MatchArm::Wild); | ||
assert_eq!(test_foo(Foo::One(255)), MatchArm::Arm(1)); | ||
assert_eq!(test_foo(Foo::One(256)), MatchArm::Wild); | ||
assert_eq!(test_foo(Foo::Two(42, 1023)), MatchArm::Wild); | ||
assert_eq!(test_foo(Foo::Two(255, 2048)), MatchArm::Arm(2)); | ||
assert_eq!(test_foo(Foo::One(100)), MatchArm::Arm(3)); | ||
assert_eq!(test_foo(Foo::One(115)), MatchArm::Arm(3)); | ||
assert_eq!(test_foo(Foo::One(105)), MatchArm::Wild); | ||
assert_eq!(test_foo(Foo::One(215)), MatchArm::Arm(3)); | ||
assert_eq!(test_foo(Foo::One(121)), MatchArm::Wild); | ||
assert_eq!(test_foo(Foo::Two(0, 42)), MatchArm::Arm(4)); | ||
assert_eq!(test_foo(Foo::Two(100, 0)), MatchArm::Arm(4)); | ||
assert_eq!(test_foo(Foo::Two(42, 0)), MatchArm::Wild); | ||
} |
This file contains 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,22 @@ | ||
warning: the feature `or_patterns` is incomplete and may cause the compiler to crash | ||
--> $DIR/basic-switchint.rs:4:12 | ||
| | ||
LL | #![feature(or_patterns)] | ||
| ^^^^^^^^^^^ | ||
| | ||
= note: `#[warn(incomplete_features)]` on by default | ||
|
||
warning: unreachable pattern | ||
--> $DIR/basic-switchint.rs:31:9 | ||
| | ||
LL | Foo::One(100 | 110..=120 | 210..=220) => MatchArm::Arm(3), | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= note: `#[warn(unreachable_patterns)]` on by default | ||
|
||
warning: unreachable pattern | ||
--> $DIR/basic-switchint.rs:33:9 | ||
| | ||
LL | Foo::Two(0..=10 | 100..=110, 0 | _) => MatchArm::Arm(4), | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
This file contains 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,20 @@ | ||
// Test that an or-pattern works with a wild pattern. This tests two things: | ||
// | ||
// 1) The Wild pattern should cause the pattern to always succeed. | ||
// 2) or-patterns should work with simplifyable patterns. | ||
// | ||
// run-pass | ||
dlrobertson marked this conversation as resolved.
Show resolved
Hide resolved
|
||
#![feature(or_patterns)] | ||
//~^ WARN the feature `or_patterns` is incomplete and may cause the compiler to crash | ||
|
||
pub fn test(x: Option<usize>) -> bool { | ||
match x { | ||
Some(0 | _) => true, | ||
_ => false | ||
} | ||
} | ||
|
||
fn main() { | ||
assert!(test(Some(42))); | ||
assert!(!test(None)); | ||
} |
This file contains 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,8 @@ | ||
warning: the feature `or_patterns` is incomplete and may cause the compiler to crash | ||
--> $DIR/mix-with-wild.rs:7:12 | ||
| | ||
LL | #![feature(or_patterns)] | ||
| ^^^^^^^^^^^ | ||
| | ||
= note: `#[warn(incomplete_features)]` on by default | ||
|
This file contains 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,48 @@ | ||
// run-pass | ||
#![feature(or_patterns)] | ||
//~^ WARN the feature `or_patterns` is incomplete and may cause the compiler to crash | ||
|
||
#[derive(Debug)] | ||
enum Other { | ||
One, | ||
Two, | ||
Three, | ||
} | ||
|
||
#[derive(Debug)] | ||
enum Test { | ||
Foo { first: usize, second: usize }, | ||
Bar { other: Option<Other> }, | ||
Baz, | ||
} | ||
|
||
fn test(x: Option<Test>) -> bool { | ||
match x { | ||
Some( | ||
Test::Foo { | ||
first: 1024 | 2048, | ||
second: 2048 | 4096 | ||
} | | ||
Test::Bar { other: Some( | ||
Other::One | | ||
Other::Two | ||
)} | ||
) => true, | ||
// wild case | ||
Some(_) => false, | ||
// empty case | ||
None => false, | ||
} | ||
} | ||
|
||
fn main() { | ||
assert!(test(Some(Test::Foo { first: 1024, second: 4096 }))); | ||
assert!(!test(Some(Test::Foo { first: 2048, second: 8192 }))); | ||
assert!(!test(Some(Test::Foo { first: 42, second: 2048 }))); | ||
assert!(test(Some(Test::Bar { other: Some(Other::One) }))); | ||
assert!(test(Some(Test::Bar { other: Some(Other::Two) }))); | ||
assert!(!test(Some(Test::Bar { other: Some(Other::Three) }))); | ||
assert!(!test(Some(Test::Bar { other: None }))); | ||
assert!(!test(Some(Test::Baz))); | ||
assert!(!test(None)); | ||
} |
This file contains 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,8 @@ | ||
warning: the feature `or_patterns` is incomplete and may cause the compiler to crash | ||
--> $DIR/struct-like.rs:2:12 | ||
| | ||
LL | #![feature(or_patterns)] | ||
| ^^^^^^^^^^^ | ||
| | ||
= note: `#[warn(incomplete_features)]` on by default | ||
|
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.
I was expecting these to be an entirely new set of candidates, containing only the match pairs that come from the or pattern. So this function would call
match_candidates
3 times: once for the first candidate, once for the remaining candidates and once for the new candidates from the or pattern.