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