diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2019-09-26 09:19:18 +0100 |
---|---|---|
committer | GitHub <[email protected]> | 2019-09-26 09:19:18 +0100 |
commit | 79376abb43e3e1e607b5307e85dd43251ad6c5e3 (patch) | |
tree | 03ab755672e9b973bbcbb9fd86f9d2fdf4f0e1ba /crates/ra_hir/src/ty/tests/never_type.rs | |
parent | 6c1878009db50b81fd70b7eb93a76d57a366c7ba (diff) | |
parent | 29e56b8ee480828b81011cfa16c055fa0c9c89fe (diff) |
Merge #1823
1823: Support type coercion r=flodiebold a=uHOOCCOOHu
Co-authored-by: uHOOCCOOHu <[email protected]>
Diffstat (limited to 'crates/ra_hir/src/ty/tests/never_type.rs')
-rw-r--r-- | crates/ra_hir/src/ty/tests/never_type.rs | 246 |
1 files changed, 246 insertions, 0 deletions
diff --git a/crates/ra_hir/src/ty/tests/never_type.rs b/crates/ra_hir/src/ty/tests/never_type.rs new file mode 100644 index 000000000..c202f545a --- /dev/null +++ b/crates/ra_hir/src/ty/tests/never_type.rs | |||
@@ -0,0 +1,246 @@ | |||
1 | use super::type_at; | ||
2 | |||
3 | #[test] | ||
4 | fn infer_never1() { | ||
5 | let t = type_at( | ||
6 | r#" | ||
7 | //- /main.rs | ||
8 | fn test() { | ||
9 | let t = return; | ||
10 | t<|>; | ||
11 | } | ||
12 | "#, | ||
13 | ); | ||
14 | assert_eq!(t, "!"); | ||
15 | } | ||
16 | |||
17 | #[test] | ||
18 | fn infer_never2() { | ||
19 | let t = type_at( | ||
20 | r#" | ||
21 | //- /main.rs | ||
22 | fn gen<T>() -> T { loop {} } | ||
23 | |||
24 | fn test() { | ||
25 | let a = gen(); | ||
26 | if false { a } else { loop {} }; | ||
27 | a<|>; | ||
28 | } | ||
29 | "#, | ||
30 | ); | ||
31 | assert_eq!(t, "!"); | ||
32 | } | ||
33 | |||
34 | #[test] | ||
35 | fn infer_never3() { | ||
36 | let t = type_at( | ||
37 | r#" | ||
38 | //- /main.rs | ||
39 | fn gen<T>() -> T { loop {} } | ||
40 | |||
41 | fn test() { | ||
42 | let a = gen(); | ||
43 | if false { loop {} } else { a }; | ||
44 | a<|>; | ||
45 | } | ||
46 | "#, | ||
47 | ); | ||
48 | assert_eq!(t, "!"); | ||
49 | } | ||
50 | |||
51 | #[test] | ||
52 | fn never_type_in_generic_args() { | ||
53 | let t = type_at( | ||
54 | r#" | ||
55 | //- /main.rs | ||
56 | enum Option<T> { None, Some(T) } | ||
57 | |||
58 | fn test() { | ||
59 | let a = if true { Option::None } else { Option::Some(return) }; | ||
60 | a<|>; | ||
61 | } | ||
62 | "#, | ||
63 | ); | ||
64 | assert_eq!(t, "Option<!>"); | ||
65 | } | ||
66 | |||
67 | #[test] | ||
68 | fn never_type_can_be_reinferred1() { | ||
69 | let t = type_at( | ||
70 | r#" | ||
71 | //- /main.rs | ||
72 | fn gen<T>() -> T { loop {} } | ||
73 | |||
74 | fn test() { | ||
75 | let a = gen(); | ||
76 | if false { loop {} } else { a }; | ||
77 | a<|>; | ||
78 | if false { a }; | ||
79 | } | ||
80 | "#, | ||
81 | ); | ||
82 | assert_eq!(t, "()"); | ||
83 | } | ||
84 | |||
85 | #[test] | ||
86 | fn never_type_can_be_reinferred2() { | ||
87 | let t = type_at( | ||
88 | r#" | ||
89 | //- /main.rs | ||
90 | enum Option<T> { None, Some(T) } | ||
91 | |||
92 | fn test() { | ||
93 | let a = if true { Option::None } else { Option::Some(return) }; | ||
94 | a<|>; | ||
95 | match 42 { | ||
96 | 42 => a, | ||
97 | _ => Option::Some(42), | ||
98 | }; | ||
99 | } | ||
100 | "#, | ||
101 | ); | ||
102 | assert_eq!(t, "Option<i32>"); | ||
103 | } | ||
104 | #[test] | ||
105 | fn never_type_can_be_reinferred3() { | ||
106 | let t = type_at( | ||
107 | r#" | ||
108 | //- /main.rs | ||
109 | enum Option<T> { None, Some(T) } | ||
110 | |||
111 | fn test() { | ||
112 | let a = if true { Option::None } else { Option::Some(return) }; | ||
113 | a<|>; | ||
114 | match 42 { | ||
115 | 42 => a, | ||
116 | _ => Option::Some("str"), | ||
117 | }; | ||
118 | } | ||
119 | "#, | ||
120 | ); | ||
121 | assert_eq!(t, "Option<&str>"); | ||
122 | } | ||
123 | |||
124 | #[test] | ||
125 | fn match_no_arm() { | ||
126 | let t = type_at( | ||
127 | r#" | ||
128 | //- /main.rs | ||
129 | enum Void {} | ||
130 | |||
131 | fn test(a: Void) { | ||
132 | let t = match a {}; | ||
133 | t<|>; | ||
134 | } | ||
135 | "#, | ||
136 | ); | ||
137 | assert_eq!(t, "!"); | ||
138 | } | ||
139 | |||
140 | #[test] | ||
141 | fn if_never() { | ||
142 | let t = type_at( | ||
143 | r#" | ||
144 | //- /main.rs | ||
145 | fn test() { | ||
146 | let i = if true { | ||
147 | loop {} | ||
148 | } else { | ||
149 | 3.0 | ||
150 | }; | ||
151 | i<|>; | ||
152 | } | ||
153 | "#, | ||
154 | ); | ||
155 | assert_eq!(t, "f64"); | ||
156 | } | ||
157 | |||
158 | #[test] | ||
159 | fn if_else_never() { | ||
160 | let t = type_at( | ||
161 | r#" | ||
162 | //- /main.rs | ||
163 | fn test(input: bool) { | ||
164 | let i = if input { | ||
165 | 2.0 | ||
166 | } else { | ||
167 | return | ||
168 | }; | ||
169 | i<|>; | ||
170 | } | ||
171 | "#, | ||
172 | ); | ||
173 | assert_eq!(t, "f64"); | ||
174 | } | ||
175 | |||
176 | #[test] | ||
177 | fn match_first_arm_never() { | ||
178 | let t = type_at( | ||
179 | r#" | ||
180 | //- /main.rs | ||
181 | fn test(a: i32) { | ||
182 | let i = match a { | ||
183 | 1 => return, | ||
184 | 2 => 2.0, | ||
185 | 3 => loop {}, | ||
186 | _ => 3.0, | ||
187 | }; | ||
188 | i<|>; | ||
189 | } | ||
190 | "#, | ||
191 | ); | ||
192 | assert_eq!(t, "f64"); | ||
193 | } | ||
194 | |||
195 | #[test] | ||
196 | fn match_second_arm_never() { | ||
197 | let t = type_at( | ||
198 | r#" | ||
199 | //- /main.rs | ||
200 | fn test(a: i32) { | ||
201 | let i = match a { | ||
202 | 1 => 3.0, | ||
203 | 2 => loop {}, | ||
204 | 3 => 3.0, | ||
205 | _ => return, | ||
206 | }; | ||
207 | i<|>; | ||
208 | } | ||
209 | "#, | ||
210 | ); | ||
211 | assert_eq!(t, "f64"); | ||
212 | } | ||
213 | |||
214 | #[test] | ||
215 | fn match_all_arms_never() { | ||
216 | let t = type_at( | ||
217 | r#" | ||
218 | //- /main.rs | ||
219 | fn test(a: i32) { | ||
220 | let i = match a { | ||
221 | 2 => return, | ||
222 | _ => loop {}, | ||
223 | }; | ||
224 | i<|>; | ||
225 | } | ||
226 | "#, | ||
227 | ); | ||
228 | assert_eq!(t, "!"); | ||
229 | } | ||
230 | |||
231 | #[test] | ||
232 | fn match_no_never_arms() { | ||
233 | let t = type_at( | ||
234 | r#" | ||
235 | //- /main.rs | ||
236 | fn test(a: i32) { | ||
237 | let i = match a { | ||
238 | 2 => 2.0, | ||
239 | _ => 3.0, | ||
240 | }; | ||
241 | i<|>; | ||
242 | } | ||
243 | "#, | ||
244 | ); | ||
245 | assert_eq!(t, "f64"); | ||
246 | } | ||