-
Notifications
You must be signed in to change notification settings - Fork 13.6k
Improve MIR match generation for ranges #56810
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
Merged
+339
−69
Merged
Changes from all commits
Commits
Show all changes
6 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
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
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,85 @@ | ||
// Make sure redundant testing paths in `match` expressions are sorted out. | ||
|
||
#![feature(exclusive_range_pattern)] | ||
|
||
fn main() { | ||
let x = 3; | ||
let b = true; | ||
|
||
// When `(0..=10).contains(x) && !b`, we should jump to the last arm | ||
// without testing two other candidates. | ||
match x { | ||
0..10 if b => 0, | ||
10..=20 => 1, | ||
-1 => 2, | ||
_ => 3, | ||
}; | ||
} | ||
|
||
// END RUST SOURCE | ||
// START rustc.main.SimplifyCfg-initial.after.mir | ||
// bb0: { | ||
// ... | ||
// _4 = Le(const 0i32, _1); | ||
// switchInt(move _4) -> [false: bb10, otherwise: bb11]; | ||
// } | ||
// bb1: { | ||
// _3 = const 0i32; | ||
// goto -> bb16; | ||
// } | ||
// bb2: { | ||
// _3 = const 1i32; | ||
// goto -> bb16; | ||
// } | ||
// bb3: { | ||
// _3 = const 2i32; | ||
// goto -> bb16; | ||
// } | ||
// bb4: { | ||
// _3 = const 3i32; | ||
// goto -> bb16; | ||
// } | ||
// bb5: { | ||
// falseEdges -> [real: bb12, imaginary: bb6]; | ||
// } | ||
// bb6: { | ||
// falseEdges -> [real: bb2, imaginary: bb7]; | ||
// } | ||
// bb7: { | ||
// falseEdges -> [real: bb3, imaginary: bb8]; | ||
// } | ||
// bb8: { | ||
// falseEdges -> [real: bb4, imaginary: bb9]; | ||
// } | ||
// bb9: { | ||
// unreachable; | ||
// } | ||
// bb10: { | ||
// _7 = Le(const 10i32, _1); | ||
// switchInt(move _7) -> [false: bb14, otherwise: bb15]; | ||
// } | ||
// bb11: { | ||
// _5 = Lt(_1, const 10i32); | ||
// switchInt(move _5) -> [false: bb10, otherwise: bb5]; | ||
// } | ||
// bb12: { | ||
// StorageLive(_6); | ||
// _6 = _2; | ||
// switchInt(move _6) -> [false: bb13, otherwise: bb1]; | ||
// } | ||
// bb13: { | ||
// falseEdges -> [real: bb8, imaginary: bb6]; | ||
// } | ||
// bb14: { | ||
// switchInt(_1) -> [-1i32: bb7, otherwise: bb8]; | ||
// } | ||
// bb15: { | ||
// _8 = Le(_1, const 20i32); | ||
// switchInt(move _8) -> [false: bb14, otherwise: bb6]; | ||
// } | ||
// bb16: { | ||
// StorageDead(_6); | ||
// ... | ||
// return; | ||
// } | ||
// END rustc.main.SimplifyCfg-initial.after.mir |
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,83 @@ | ||
#![feature(exclusive_range_pattern)] | ||
|
||
// run-pass | ||
|
||
fn main() { | ||
let incl_range = |x, b| { | ||
match x { | ||
0..=5 if b => 0, | ||
5..=10 if b => 1, | ||
1..=4 if !b => 2, | ||
_ => 3, | ||
} | ||
}; | ||
assert_eq!(incl_range(3, false), 2); | ||
assert_eq!(incl_range(3, true), 0); | ||
assert_eq!(incl_range(5, false), 3); | ||
assert_eq!(incl_range(5, true), 0); | ||
|
||
let excl_range = |x, b| { | ||
match x { | ||
0..5 if b => 0, | ||
5..10 if b => 1, | ||
1..4 if !b => 2, | ||
_ => 3, | ||
} | ||
}; | ||
assert_eq!(excl_range(3, false), 2); | ||
assert_eq!(excl_range(3, true), 0); | ||
assert_eq!(excl_range(5, false), 3); | ||
assert_eq!(excl_range(5, true), 1); | ||
|
||
let incl_range_vs_const = |x, b| { | ||
match x { | ||
0..=5 if b => 0, | ||
7 => 1, | ||
3 => 2, | ||
_ => 3, | ||
} | ||
}; | ||
assert_eq!(incl_range_vs_const(5, false), 3); | ||
assert_eq!(incl_range_vs_const(5, true), 0); | ||
assert_eq!(incl_range_vs_const(3, false), 2); | ||
assert_eq!(incl_range_vs_const(3, true), 0); | ||
assert_eq!(incl_range_vs_const(7, false), 1); | ||
assert_eq!(incl_range_vs_const(7, true), 1); | ||
|
||
let excl_range_vs_const = |x, b| { | ||
match x { | ||
0..5 if b => 0, | ||
7 => 1, | ||
3 => 2, | ||
_ => 3, | ||
} | ||
}; | ||
assert_eq!(excl_range_vs_const(5, false), 3); | ||
assert_eq!(excl_range_vs_const(5, true), 3); | ||
assert_eq!(excl_range_vs_const(3, false), 2); | ||
assert_eq!(excl_range_vs_const(3, true), 0); | ||
assert_eq!(excl_range_vs_const(7, false), 1); | ||
assert_eq!(excl_range_vs_const(7, true), 1); | ||
|
||
let const_vs_incl_range = |x, b| { | ||
match x { | ||
3 if b => 0, | ||
5..=7 => 2, | ||
1..=4 => 1, | ||
_ => 3, | ||
} | ||
}; | ||
assert_eq!(const_vs_incl_range(3, false), 1); | ||
assert_eq!(const_vs_incl_range(3, true), 0); | ||
|
||
let const_vs_excl_range = |x, b| { | ||
match x { | ||
3 if b => 0, | ||
5..7 => 2, | ||
1..4 => 1, | ||
_ => 3, | ||
} | ||
}; | ||
assert_eq!(const_vs_excl_range(3, false), 1); | ||
assert_eq!(const_vs_excl_range(3, true), 0); | ||
} |
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.
we can use the
try
syntax inside the compiler.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.
try
syntax seems to require not only#![feature(try_block)]
but also--edition 2018
fortry
keyword.