Skip to content

Commit 4188f21

Browse files
committed
add runtime behavior testing for guard patterns (1)
1 parent 4d88621 commit 4188f21

4 files changed

Lines changed: 104 additions & 20 deletions

File tree

tests/ui/pattern/rfc-3637-guard-patterns/correct-mir.rs

Lines changed: 0 additions & 20 deletions
This file was deleted.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//! Check for correct runtime behavior when using guard patterns combined with arm guards
2+
3+
//@ compile-flags: -Zvalidate-mir -Zlint-mir
4+
//@ run-pass
5+
6+
#![feature(guard_patterns)]
7+
#![allow(incomplete_features)]
8+
9+
fn main() {
10+
assert!(guard_arm_pats(true, false, true));
11+
assert!(guard_arm_pats(false, true, true));
12+
}
13+
14+
fn guard_arm_pats(x: bool, y: bool, z: bool) -> bool {
15+
match (x, y) {
16+
// (true, false, true)
17+
(true if x, false if x) if z => true,
18+
// (false, true, true)
19+
(false if z, true if !x) if y => true,
20+
_ => false
21+
}
22+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
//! Check for correct runtime behavior when using guard patterns with or-patterns
2+
3+
//@ compile-flags: -Zvalidate-mir -Zlint-mir
4+
//@ run-pass
5+
6+
#![feature(guard_patterns)]
7+
#![allow(incomplete_features)]
8+
9+
fn main() {
10+
assert!(arr_with_or_pats_and_arm_guard([1, 2, 4], true, true, false, true, true));
11+
assert!(arr_with_or_pats_and_arm_guard([1, 3, 4], true, false, true, true, true));
12+
13+
assert!(arr_with_or_pats([1, 2, 4], true, true, false, true));
14+
assert!(arr_with_or_pats([1, 3, 4], true, false, true, true));
15+
}
16+
17+
fn arr_with_or_pats_and_arm_guard(
18+
arr: [u8; 3],
19+
a: bool,
20+
b: bool,
21+
c: bool,
22+
d: bool,
23+
e: bool,
24+
) -> bool {
25+
const A: u8 = 1;
26+
const B: u8 = 2;
27+
const C: u8 = 3;
28+
const D: u8 = 4;
29+
30+
match arr {
31+
// [1, 2 | 3, 4]
32+
[A if a, (B if b) | (C if c), D if d] if e => true,
33+
_ => false
34+
}
35+
}
36+
37+
fn arr_with_or_pats(arr: [u8; 3], a: bool, b: bool, c: bool, d: bool) -> bool {
38+
const A: u8 = 1;
39+
const B: u8 = 2;
40+
const C: u8 = 3;
41+
const D: u8 = 4;
42+
43+
match arr {
44+
// [1, 2 | 3, 4]
45+
[A if a, (B if b) | (C if c), D if d] => true,
46+
_ => false
47+
}
48+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//! Check for correct runtime behavior when using bools in guard patterns
2+
3+
//@ compile-flags: -Zvalidate-mir -Zlint-mir
4+
//@ run-pass
5+
6+
#![feature(guard_patterns)]
7+
#![allow(incomplete_features)]
8+
9+
fn main() {
10+
assert!(generic_usage(true, false, true));
11+
assert!(!generic_usage(false, true, true));
12+
assert!(with_ops(true, false, true));
13+
assert!(with_ops(false, true, true))
14+
}
15+
16+
fn generic_usage(x: bool, y: bool, z: bool) -> bool {
17+
match (x, y) {
18+
// (true, false, true)
19+
(true if z, false if z) => true,
20+
// (false, true, true)
21+
(false if z, true if z) => false,
22+
_ => false
23+
}
24+
}
25+
26+
fn with_ops(x: bool, y: bool, z: bool) -> bool {
27+
match (x, y) {
28+
// (true, false, true)
29+
(true if y || z, false if x && z) => true,
30+
// (false, true, true)
31+
(false if y && z, true if y || z) => true,
32+
_ => false
33+
}
34+
}

0 commit comments

Comments
 (0)