rust/tests/ui/pattern/deref-patterns/branch.rs
Nadrieril c623319a30 Lower deref patterns to MIR
This handles using deref patterns to choose the correct match arm. This
does not handle bindings or guards.

Co-authored-by: Deadbeef <ent3rm4n@gmail.com>
2024-04-20 15:59:54 +02:00

41 lines
1.1 KiB
Rust

//@ run-pass
// Test the execution of deref patterns.
#![feature(deref_patterns)]
#![allow(incomplete_features)]
fn branch(vec: Vec<u32>) -> u32 {
match vec {
deref!([]) => 0,
deref!([1, _, 3]) => 1,
deref!([2, ..]) => 2,
_ => 1000,
}
}
fn nested(vec: Vec<Vec<u32>>) -> u32 {
match vec {
deref!([deref!([]), ..]) => 1,
deref!([deref!([0, ..]), deref!([1, ..])]) => 2,
_ => 1000,
}
}
fn main() {
assert!(matches!(Vec::<u32>::new(), deref!([])));
assert!(matches!(vec![1], deref!([1])));
assert!(matches!(&vec![1], deref!([1])));
assert!(matches!(vec![&1], deref!([1])));
assert!(matches!(vec![vec![1]], deref!([deref!([1])])));
assert_eq!(branch(vec![]), 0);
assert_eq!(branch(vec![1, 2, 3]), 1);
assert_eq!(branch(vec![3, 2, 1]), 1000);
assert_eq!(branch(vec![2]), 2);
assert_eq!(branch(vec![2, 3]), 2);
assert_eq!(branch(vec![3, 2]), 1000);
assert_eq!(nested(vec![vec![], vec![2]]), 1);
assert_eq!(nested(vec![vec![0], vec![1]]), 2);
assert_eq!(nested(vec![vec![0, 2], vec![1, 2]]), 2);
}