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