]> git.lizzy.rs Git - rust.git/blob - src/test/ui/coerce/coerce-unify.rs
f1818f9bb5a6df7fa791c747f106e0c7a957d3c5
[rust.git] / src / test / ui / coerce / coerce-unify.rs
1 // run-pass
2 // Check that coercions can unify if-else, match arms and array elements.
3
4 // Try to construct if-else chains, matches and arrays out of given expressions.
5 macro_rules! check {
6     ($last:expr $(, $rest:expr)+) => {
7         // Last expression comes first because of whacky ifs and matches.
8         let _ = $(if false { $rest })else+ else { $last };
9
10         let _ = match 0 { $(_ if false => $rest,)+ _ => $last };
11
12         let _ = [$($rest,)+ $last];
13     }
14 }
15
16 // Check all non-uniform cases of 2 and 3 expressions of 2 types.
17 macro_rules! check2 {
18     ($a:expr, $b:expr) => {
19         check!($a, $b);
20         check!($b, $a);
21
22         check!($a, $a, $b);
23         check!($a, $b, $a);
24         check!($a, $b, $b);
25
26         check!($b, $a, $a);
27         check!($b, $a, $b);
28         check!($b, $b, $a);
29     }
30 }
31
32 // Check all non-uniform cases of 2 and 3 expressions of 3 types.
33 macro_rules! check3 {
34     ($a:expr, $b:expr, $c:expr) => {
35         // Delegate to check2 for cases where a type repeats.
36         check2!($a, $b);
37         check2!($b, $c);
38         check2!($a, $c);
39
40         // Check the remaining cases, i.e., permutations of ($a, $b, $c).
41         check!($a, $b, $c);
42         check!($a, $c, $b);
43         check!($b, $a, $c);
44         check!($b, $c, $a);
45         check!($c, $a, $b);
46         check!($c, $b, $a);
47     }
48 }
49
50 use std::mem::size_of;
51
52 fn foo() {}
53 fn bar() {}
54
55 pub fn main() {
56     check3!(foo, bar, foo as fn());
57     check3!(size_of::<u8>, size_of::<u16>, size_of::<usize> as fn() -> usize);
58
59     let s = String::from("bar");
60     check2!("foo", &s);
61
62     let a = [1, 2, 3];
63     let v = vec![1, 2, 3];
64     check2!(&a[..], &v);
65
66     // Make sure in-array coercion still works.
67     let _ = [("a", Default::default()), (Default::default(), "b"), (&s, &s)];
68 }