Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve
match
MIR generation for ranges
Makes testing a range rule out ranges/constant covered by the range that is being tested
- Loading branch information
Showing
3 changed files
with
207 additions
and
5 deletions.
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,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); | ||
} |