2018-08-30 07:18:55 -05:00
|
|
|
// run-pass
|
2014-03-22 07:55:46 -05:00
|
|
|
|
|
|
|
// Tests that match expression handles overlapped literal and range
|
|
|
|
// properly in the presence of guard function.
|
|
|
|
|
2015-03-25 19:06:52 -05:00
|
|
|
fn val() -> usize { 1 }
|
2014-03-22 07:55:46 -05:00
|
|
|
|
2015-03-25 19:06:52 -05:00
|
|
|
static CONST: usize = 1;
|
2014-03-22 07:55:46 -05:00
|
|
|
|
|
|
|
pub fn main() {
|
|
|
|
lit_shadow_range();
|
|
|
|
range_shadow_lit();
|
|
|
|
range_shadow_range();
|
|
|
|
multi_pats_shadow_lit();
|
|
|
|
multi_pats_shadow_range();
|
|
|
|
lit_shadow_multi_pats();
|
|
|
|
range_shadow_multi_pats();
|
2014-05-04 17:16:16 -05:00
|
|
|
misc();
|
2014-03-22 07:55:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn lit_shadow_range() {
|
2015-01-25 15:05:03 -06:00
|
|
|
assert_eq!(2, match 1 {
|
|
|
|
1 if false => 1,
|
2018-05-28 21:42:11 -05:00
|
|
|
1..=2 => 2,
|
2014-03-22 07:55:46 -05:00
|
|
|
_ => 3
|
|
|
|
});
|
|
|
|
|
2015-01-25 15:05:03 -06:00
|
|
|
let x = 0;
|
|
|
|
assert_eq!(2, match x+1 {
|
|
|
|
0 => 0,
|
2014-03-22 07:55:46 -05:00
|
|
|
1 if false => 1,
|
2018-05-28 21:42:11 -05:00
|
|
|
1..=2 => 2,
|
2014-03-22 07:55:46 -05:00
|
|
|
_ => 3
|
|
|
|
});
|
|
|
|
|
2015-01-25 15:05:03 -06:00
|
|
|
assert_eq!(2, match val() {
|
|
|
|
1 if false => 1,
|
2018-05-28 21:42:11 -05:00
|
|
|
1..=2 => 2,
|
2014-03-22 07:55:46 -05:00
|
|
|
_ => 3
|
|
|
|
});
|
|
|
|
|
2015-01-25 15:05:03 -06:00
|
|
|
assert_eq!(2, match CONST {
|
|
|
|
0 => 0,
|
2014-03-22 07:55:46 -05:00
|
|
|
1 if false => 1,
|
2018-05-28 21:42:11 -05:00
|
|
|
1..=2 => 2,
|
2014-03-22 07:55:46 -05:00
|
|
|
_ => 3
|
|
|
|
});
|
|
|
|
|
|
|
|
// value is out of the range of second arm, should match wildcard pattern
|
2015-01-25 15:05:03 -06:00
|
|
|
assert_eq!(3, match 3 {
|
|
|
|
1 if false => 1,
|
2018-05-28 21:42:11 -05:00
|
|
|
1..=2 => 2,
|
2014-03-22 07:55:46 -05:00
|
|
|
_ => 3
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
fn range_shadow_lit() {
|
2015-01-25 15:05:03 -06:00
|
|
|
assert_eq!(2, match 1 {
|
2018-05-28 21:42:11 -05:00
|
|
|
1..=2 if false => 1,
|
2014-03-22 07:55:46 -05:00
|
|
|
1 => 2,
|
|
|
|
_ => 3
|
|
|
|
});
|
|
|
|
|
2015-01-25 15:05:03 -06:00
|
|
|
let x = 0;
|
|
|
|
assert_eq!(2, match x+1 {
|
|
|
|
0 => 0,
|
2018-05-28 21:42:11 -05:00
|
|
|
1..=2 if false => 1,
|
2014-03-22 07:55:46 -05:00
|
|
|
1 => 2,
|
|
|
|
_ => 3
|
|
|
|
});
|
|
|
|
|
2015-01-25 15:05:03 -06:00
|
|
|
assert_eq!(2, match val() {
|
2018-05-28 21:42:11 -05:00
|
|
|
1..=2 if false => 1,
|
2014-03-22 07:55:46 -05:00
|
|
|
1 => 2,
|
|
|
|
_ => 3
|
|
|
|
});
|
|
|
|
|
2015-01-25 15:05:03 -06:00
|
|
|
assert_eq!(2, match CONST {
|
|
|
|
0 => 0,
|
2018-05-28 21:42:11 -05:00
|
|
|
1..=2 if false => 1,
|
2014-03-22 07:55:46 -05:00
|
|
|
1 => 2,
|
|
|
|
_ => 3
|
|
|
|
});
|
|
|
|
|
|
|
|
// ditto
|
2015-01-25 15:05:03 -06:00
|
|
|
assert_eq!(3, match 3 {
|
2018-05-28 21:42:11 -05:00
|
|
|
1..=2 if false => 1,
|
2014-03-22 07:55:46 -05:00
|
|
|
1 => 2,
|
|
|
|
_ => 3
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
fn range_shadow_range() {
|
2015-01-25 15:05:03 -06:00
|
|
|
assert_eq!(2, match 1 {
|
2018-05-28 21:42:11 -05:00
|
|
|
0..=2 if false => 1,
|
|
|
|
1..=3 => 2,
|
2014-03-22 07:55:46 -05:00
|
|
|
_ => 3,
|
|
|
|
});
|
|
|
|
|
2015-01-25 15:05:03 -06:00
|
|
|
let x = 0;
|
|
|
|
assert_eq!(2, match x+1 {
|
2014-03-22 07:55:46 -05:00
|
|
|
100 => 0,
|
2018-05-28 21:42:11 -05:00
|
|
|
0..=2 if false => 1,
|
|
|
|
1..=3 => 2,
|
2014-03-22 07:55:46 -05:00
|
|
|
_ => 3,
|
|
|
|
});
|
|
|
|
|
2015-01-25 15:05:03 -06:00
|
|
|
assert_eq!(2, match val() {
|
2018-05-28 21:42:11 -05:00
|
|
|
0..=2 if false => 1,
|
|
|
|
1..=3 => 2,
|
2014-03-22 07:55:46 -05:00
|
|
|
_ => 3,
|
|
|
|
});
|
|
|
|
|
2015-01-25 15:05:03 -06:00
|
|
|
assert_eq!(2, match CONST {
|
2014-03-22 07:55:46 -05:00
|
|
|
100 => 0,
|
2018-05-28 21:42:11 -05:00
|
|
|
0..=2 if false => 1,
|
|
|
|
1..=3 => 2,
|
2014-03-22 07:55:46 -05:00
|
|
|
_ => 3,
|
|
|
|
});
|
|
|
|
|
|
|
|
// ditto
|
2015-01-25 15:05:03 -06:00
|
|
|
assert_eq!(3, match 5 {
|
2018-05-28 21:42:11 -05:00
|
|
|
0..=2 if false => 1,
|
|
|
|
1..=3 => 2,
|
2014-03-22 07:55:46 -05:00
|
|
|
_ => 3,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
fn multi_pats_shadow_lit() {
|
2015-01-25 15:05:03 -06:00
|
|
|
assert_eq!(2, match 1 {
|
|
|
|
100 => 0,
|
2018-05-28 21:42:11 -05:00
|
|
|
0 | 1..=10 if false => 1,
|
2014-03-22 07:55:46 -05:00
|
|
|
1 => 2,
|
|
|
|
_ => 3,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
fn multi_pats_shadow_range() {
|
2015-01-25 15:05:03 -06:00
|
|
|
assert_eq!(2, match 1 {
|
|
|
|
100 => 0,
|
2018-05-28 21:42:11 -05:00
|
|
|
0 | 1..=10 if false => 1,
|
|
|
|
1..=3 => 2,
|
2014-03-22 07:55:46 -05:00
|
|
|
_ => 3,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
fn lit_shadow_multi_pats() {
|
2015-01-25 15:05:03 -06:00
|
|
|
assert_eq!(2, match 1 {
|
|
|
|
100 => 0,
|
2014-03-22 07:55:46 -05:00
|
|
|
1 if false => 1,
|
2018-05-28 21:42:11 -05:00
|
|
|
0 | 1..=10 => 2,
|
2014-03-22 07:55:46 -05:00
|
|
|
_ => 3,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
fn range_shadow_multi_pats() {
|
2015-01-25 15:05:03 -06:00
|
|
|
assert_eq!(2, match 1 {
|
|
|
|
100 => 0,
|
2018-05-28 21:42:11 -05:00
|
|
|
1..=3 if false => 1,
|
|
|
|
0 | 1..=10 => 2,
|
2014-03-22 07:55:46 -05:00
|
|
|
_ => 3,
|
|
|
|
});
|
|
|
|
}
|
2014-05-04 17:16:16 -05:00
|
|
|
|
|
|
|
fn misc() {
|
|
|
|
enum Foo {
|
2022-07-25 15:36:03 -05:00
|
|
|
Bar(#[allow(unused_tuple_struct_fields)] usize, bool)
|
2014-05-04 17:16:16 -05:00
|
|
|
}
|
|
|
|
// This test basically mimics how trace_macros! macro is implemented,
|
|
|
|
// which is a rare combination of vector patterns, multiple wild-card
|
|
|
|
// patterns and guard functions.
|
2015-02-01 20:53:25 -06:00
|
|
|
let r = match [Foo::Bar(0, false)] {
|
2015-01-25 15:05:03 -06:00
|
|
|
[Foo::Bar(_, pred)] if pred => 1,
|
|
|
|
[Foo::Bar(_, pred)] if !pred => 2,
|
|
|
|
_ => 0,
|
2014-05-04 17:16:16 -05:00
|
|
|
};
|
2015-01-25 15:05:03 -06:00
|
|
|
assert_eq!(2, r);
|
2014-05-04 17:16:16 -05:00
|
|
|
}
|