]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/coerce/coerce-unify.rs
575d672e092a043c51e1188a9e83bd1d16d32b92
[rust.git] / src / test / run-pass / coerce / coerce-unify.rs
1 // Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 // run-pass
12 // Check that coercions can unify if-else, match arms and array elements.
13
14 // Try to construct if-else chains, matches and arrays out of given expressions.
15 macro_rules! check {
16     ($last:expr $(, $rest:expr)+) => {
17         // Last expression comes first because of whacky ifs and matches.
18         let _ = $(if false { $rest })else+ else { $last };
19
20         let _ = match 0 { $(_ if false => $rest,)+ _ => $last };
21
22         let _ = [$($rest,)+ $last];
23     }
24 }
25
26 // Check all non-uniform cases of 2 and 3 expressions of 2 types.
27 macro_rules! check2 {
28     ($a:expr, $b:expr) => {
29         check!($a, $b);
30         check!($b, $a);
31
32         check!($a, $a, $b);
33         check!($a, $b, $a);
34         check!($a, $b, $b);
35
36         check!($b, $a, $a);
37         check!($b, $a, $b);
38         check!($b, $b, $a);
39     }
40 }
41
42 // Check all non-uniform cases of 2 and 3 expressions of 3 types.
43 macro_rules! check3 {
44     ($a:expr, $b:expr, $c:expr) => {
45         // Delegate to check2 for cases where a type repeats.
46         check2!($a, $b);
47         check2!($b, $c);
48         check2!($a, $c);
49
50         // Check the remaining cases, i.e. permutations of ($a, $b, $c).
51         check!($a, $b, $c);
52         check!($a, $c, $b);
53         check!($b, $a, $c);
54         check!($b, $c, $a);
55         check!($c, $a, $b);
56         check!($c, $b, $a);
57     }
58 }
59
60 use std::mem::size_of;
61
62 fn foo() {}
63 fn bar() {}
64
65 pub fn main() {
66     check3!(foo, bar, foo as fn());
67     check3!(size_of::<u8>, size_of::<u16>, size_of::<usize> as fn() -> usize);
68
69     let s = String::from("bar");
70     check2!("foo", &s);
71
72     let a = [1, 2, 3];
73     let v = vec![1, 2, 3];
74     check2!(&a[..], &v);
75
76     // Make sure in-array coercion still works.
77     let _ = [("a", Default::default()), (Default::default(), "b"), (&s, &s)];
78 }