]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/mir_refs_correct.rs
Auto merge of #35856 - phimuemue:master, r=brson
[rust.git] / src / test / run-pass / mir_refs_correct.rs
1 // Copyright 2015 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 // aux-build:mir_external_refs.rs
12
13 extern crate mir_external_refs as ext;
14
15 struct S(u8);
16 #[derive(Debug, PartialEq, Eq)]
17 struct Unit;
18
19 impl S {
20     fn hey() -> u8 { 42 }
21     fn hey2(&self) -> u8 { 44 }
22 }
23
24 trait X {
25     fn hoy(&self) -> u8 { 43 }
26     fn hoy2() -> u8 { 45 }
27 }
28
29 trait F<U> {
30     fn f(self, other: U) -> u64;
31 }
32
33 impl F<u32> for u32 {
34     fn f(self, other: u32) -> u64 { self as u64 + other as u64 }
35 }
36
37 impl F<u64> for u32 {
38     fn f(self, other: u64) -> u64 { self as u64 - other }
39 }
40
41 impl F<u64> for u64 {
42     fn f(self, other: u64) -> u64 { self * other }
43 }
44
45 impl F<u32> for u64 {
46     fn f(self, other: u32) -> u64 { self ^ other as u64 }
47 }
48
49 trait T<I, O> {
50     fn staticmeth(i: I, o: O) -> (I, O) { (i, o) }
51 }
52
53 impl<I, O> T<I, O> for O {}
54
55 impl X for S {}
56
57 enum E {
58     U(u8)
59 }
60
61 #[derive(PartialEq, Debug, Eq)]
62 enum CEnum {
63     A = 0x321,
64     B = 0x123
65 }
66
67 const C: u8 = 84;
68 const C2: [u8; 5] = [42; 5];
69 const C3: [u8; 3] = [42, 41, 40];
70 const C4: fn(u8) -> S = S;
71
72 fn regular() -> u8 {
73     21
74 }
75
76 fn parametric<T>(u: T) -> T {
77     u
78 }
79
80 fn t1() -> fn()->u8 {
81     regular
82 }
83
84 fn t2() -> fn(u8)->E {
85     E::U
86 }
87
88 fn t3() -> fn(u8)->S {
89     S
90 }
91
92 fn t4() -> fn()->u8 {
93     S::hey
94 }
95
96 fn t5() -> fn(&S)-> u8 {
97     <S as X>::hoy
98 }
99
100
101 fn t6() -> fn()->u8{
102     ext::regular_fn
103 }
104
105 fn t7() -> fn(u8)->ext::E {
106     ext::E::U
107 }
108
109 fn t8() -> fn(u8)->ext::S {
110     ext::S
111 }
112
113 fn t9() -> fn()->u8 {
114     ext::S::hey
115 }
116
117 fn t10() -> fn(&ext::S)->u8 {
118     <ext::S as ext::X>::hoy
119 }
120
121 fn t11() -> fn(u8)->u8 {
122     parametric
123 }
124
125 fn t12() -> u8 {
126     C
127 }
128
129 fn t13() -> [u8; 5] {
130     C2
131 }
132
133 fn t13_2() -> [u8; 3] {
134     C3
135 }
136
137 fn t14() -> fn()-> u8 {
138     <S as X>::hoy2
139 }
140
141 fn t15() -> fn(&S)-> u8 {
142     S::hey2
143 }
144
145 fn t16() -> fn(u32, u32)->u64 {
146     F::f
147 }
148
149 fn t17() -> fn(u32, u64)->u64 {
150     F::f
151 }
152
153 fn t18() -> fn(u64, u64)->u64 {
154     F::f
155 }
156
157 fn t19() -> fn(u64, u32)->u64 {
158     F::f
159 }
160
161 fn t20() -> fn(u64, u32)->(u64, u32) {
162     <u32 as T<_, _>>::staticmeth
163 }
164
165 fn t21() -> Unit {
166     Unit
167 }
168
169 fn t22() -> Option<u8> {
170     None
171 }
172
173 fn t23() -> (CEnum, CEnum) {
174     (CEnum::A, CEnum::B)
175 }
176
177 fn t24() -> fn(u8) -> S {
178     C4
179 }
180
181 fn main() {
182     assert_eq!(t1()(), regular());
183
184     assert_eq!(t2() as *mut (), E::U as *mut ());
185     assert_eq!(t3() as *mut (), S as *mut ());
186
187     assert_eq!(t4()(), S::hey());
188     let s = S(42);
189     assert_eq!(t5()(&s), <S as X>::hoy(&s));
190
191
192     assert_eq!(t6()(), ext::regular_fn());
193     assert_eq!(t7() as *mut (), ext::E::U as *mut ());
194     assert_eq!(t8() as *mut (), ext::S as *mut ());
195
196     assert_eq!(t9()(), ext::S::hey());
197     let sext = ext::S(6);
198     assert_eq!(t10()(&sext), <ext::S as ext::X>::hoy(&sext));
199
200     let p = parametric::<u8>;
201     assert_eq!(t11() as *mut (), p as *mut ());
202
203     assert_eq!(t12(), C);
204     assert_eq!(t13(), C2);
205     assert_eq!(t13_2(), C3);
206
207     assert_eq!(t14()(), <S as X>::hoy2());
208     assert_eq!(t15()(&s), S::hey2(&s));
209     assert_eq!(t16()(10u32, 20u32), F::f(10u32, 20u32));
210     assert_eq!(t17()(30u32, 10u64), F::f(30u32, 10u64));
211     assert_eq!(t18()(50u64, 5u64), F::f(50u64, 5u64));
212     assert_eq!(t19()(322u64, 2u32), F::f(322u64, 2u32));
213     assert_eq!(t20()(123u64, 38u32), <u32 as T<_, _>>::staticmeth(123, 38));
214     assert_eq!(t21(), Unit);
215     assert_eq!(t22(), None);
216     assert_eq!(t23(), (CEnum::A, CEnum::B));
217     assert_eq!(t24(), C4);
218 }