aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir_ty/src/tests
diff options
context:
space:
mode:
authorSeivan Heidari <[email protected]>2019-12-23 14:35:31 +0000
committerSeivan Heidari <[email protected]>2019-12-23 14:35:31 +0000
commitb21d9337d9200e2cfdc90b386591c72c302dc03e (patch)
treef81f5c08f821115cee26fa4d3ceaae88c7807fd5 /crates/ra_hir_ty/src/tests
parent18a0937585b836ec5ed054b9ae48e0156ab6d9ef (diff)
parentce07a2daa9e53aa86a769f8641b14c2878444fbc (diff)
Merge branch 'master' into feature/themes
Diffstat (limited to 'crates/ra_hir_ty/src/tests')
-rw-r--r--crates/ra_hir_ty/src/tests/coercion.rs159
-rw-r--r--crates/ra_hir_ty/src/tests/macros.rs390
-rw-r--r--crates/ra_hir_ty/src/tests/method_resolution.rs1005
-rw-r--r--crates/ra_hir_ty/src/tests/patterns.rs238
-rw-r--r--crates/ra_hir_ty/src/tests/regression.rs333
-rw-r--r--crates/ra_hir_ty/src/tests/simple.rs1663
-rw-r--r--crates/ra_hir_ty/src/tests/traits.rs1598
7 files changed, 5386 insertions, 0 deletions
diff --git a/crates/ra_hir_ty/src/tests/coercion.rs b/crates/ra_hir_ty/src/tests/coercion.rs
index 1530fcc63..7e99a42ed 100644
--- a/crates/ra_hir_ty/src/tests/coercion.rs
+++ b/crates/ra_hir_ty/src/tests/coercion.rs
@@ -1,3 +1,4 @@
1use super::infer_with_mismatches;
1use insta::assert_snapshot; 2use insta::assert_snapshot;
2use test_utils::covers; 3use test_utils::covers;
3 4
@@ -367,3 +368,161 @@ fn test() {
367 "### 368 "###
368 ); 369 );
369} 370}
371
372#[test]
373fn return_coerce_unknown() {
374 assert_snapshot!(
375 infer_with_mismatches(r#"
376fn foo() -> u32 {
377 return unknown;
378}
379"#, true),
380 @r###"
381 [17; 40) '{ ...own; }': !
382 [23; 37) 'return unknown': !
383 [30; 37) 'unknown': u32
384 "###
385 );
386}
387
388#[test]
389fn coerce_autoderef() {
390 assert_snapshot!(
391 infer_with_mismatches(r#"
392struct Foo;
393fn takes_ref_foo(x: &Foo) {}
394fn test() {
395 takes_ref_foo(&Foo);
396 takes_ref_foo(&&Foo);
397 takes_ref_foo(&&&Foo);
398}
399"#, true),
400 @r###"
401 [30; 31) 'x': &Foo
402 [39; 41) '{}': ()
403 [52; 133) '{ ...oo); }': ()
404 [58; 71) 'takes_ref_foo': fn takes_ref_foo(&Foo) -> ()
405 [58; 77) 'takes_...(&Foo)': ()
406 [72; 76) '&Foo': &Foo
407 [73; 76) 'Foo': Foo
408 [83; 96) 'takes_ref_foo': fn takes_ref_foo(&Foo) -> ()
409 [83; 103) 'takes_...&&Foo)': ()
410 [97; 102) '&&Foo': &&Foo
411 [98; 102) '&Foo': &Foo
412 [99; 102) 'Foo': Foo
413 [109; 122) 'takes_ref_foo': fn takes_ref_foo(&Foo) -> ()
414 [109; 130) 'takes_...&&Foo)': ()
415 [123; 129) '&&&Foo': &&&Foo
416 [124; 129) '&&Foo': &&Foo
417 [125; 129) '&Foo': &Foo
418 [126; 129) 'Foo': Foo
419 "###
420 );
421}
422
423#[test]
424fn coerce_autoderef_generic() {
425 assert_snapshot!(
426 infer_with_mismatches(r#"
427struct Foo;
428fn takes_ref<T>(x: &T) -> T { *x }
429fn test() {
430 takes_ref(&Foo);
431 takes_ref(&&Foo);
432 takes_ref(&&&Foo);
433}
434"#, true),
435 @r###"
436 [29; 30) 'x': &T
437 [41; 47) '{ *x }': T
438 [43; 45) '*x': T
439 [44; 45) 'x': &T
440 [58; 127) '{ ...oo); }': ()
441 [64; 73) 'takes_ref': fn takes_ref<Foo>(&T) -> T
442 [64; 79) 'takes_ref(&Foo)': Foo
443 [74; 78) '&Foo': &Foo
444 [75; 78) 'Foo': Foo
445 [85; 94) 'takes_ref': fn takes_ref<&Foo>(&T) -> T
446 [85; 101) 'takes_...&&Foo)': &Foo
447 [95; 100) '&&Foo': &&Foo
448 [96; 100) '&Foo': &Foo
449 [97; 100) 'Foo': Foo
450 [107; 116) 'takes_ref': fn takes_ref<&&Foo>(&T) -> T
451 [107; 124) 'takes_...&&Foo)': &&Foo
452 [117; 123) '&&&Foo': &&&Foo
453 [118; 123) '&&Foo': &&Foo
454 [119; 123) '&Foo': &Foo
455 [120; 123) 'Foo': Foo
456 "###
457 );
458}
459
460#[test]
461fn closure_return_coerce() {
462 assert_snapshot!(
463 infer_with_mismatches(r#"
464fn foo() {
465 let x = || {
466 if true {
467 return &1u32;
468 }
469 &&1u32
470 };
471}
472"#, true),
473 @r###"
474 [10; 106) '{ ... }; }': ()
475 [20; 21) 'x': || -> &u32
476 [24; 103) '|| { ... }': || -> &u32
477 [27; 103) '{ ... }': &u32
478 [37; 82) 'if tru... }': ()
479 [40; 44) 'true': bool
480 [45; 82) '{ ... }': !
481 [59; 71) 'return &1u32': !
482 [66; 71) '&1u32': &u32
483 [67; 71) '1u32': u32
484 [91; 97) '&&1u32': &&u32
485 [92; 97) '&1u32': &u32
486 [93; 97) '1u32': u32
487 "###
488 );
489}
490
491#[test]
492fn coerce_fn_item_to_fn_ptr() {
493 assert_snapshot!(
494 infer_with_mismatches(r#"
495fn foo(x: u32) -> isize { 1 }
496fn test() {
497 let f: fn(u32) -> isize = foo;
498}
499"#, true),
500 @r###"
501 [8; 9) 'x': u32
502 [25; 30) '{ 1 }': isize
503 [27; 28) '1': isize
504 [41; 79) '{ ...foo; }': ()
505 [51; 52) 'f': fn(u32) -> isize
506 [73; 76) 'foo': fn foo(u32) -> isize
507 "###
508 );
509}
510
511#[test]
512fn coerce_closure_to_fn_ptr() {
513 assert_snapshot!(
514 infer_with_mismatches(r#"
515fn test() {
516 let f: fn(u32) -> isize = |x| { 1 };
517}
518"#, true),
519 @r###"
520 [11; 55) '{ ...1 }; }': ()
521 [21; 22) 'f': fn(u32) -> isize
522 [43; 52) '|x| { 1 }': |u32| -> isize
523 [44; 45) 'x': u32
524 [47; 52) '{ 1 }': isize
525 [49; 50) '1': isize
526 "###
527 );
528}
diff --git a/crates/ra_hir_ty/src/tests/macros.rs b/crates/ra_hir_ty/src/tests/macros.rs
new file mode 100644
index 000000000..69c695cc8
--- /dev/null
+++ b/crates/ra_hir_ty/src/tests/macros.rs
@@ -0,0 +1,390 @@
1use super::{infer, type_at, type_at_pos};
2use crate::test_db::TestDB;
3use insta::assert_snapshot;
4use ra_db::fixture::WithFixture;
5
6#[test]
7fn cfg_impl_block() {
8 let (db, pos) = TestDB::with_position(
9 r#"
10//- /main.rs crate:main deps:foo cfg:test
11use foo::S as T;
12struct S;
13
14#[cfg(test)]
15impl S {
16 fn foo1(&self) -> i32 { 0 }
17}
18
19#[cfg(not(test))]
20impl S {
21 fn foo2(&self) -> i32 { 0 }
22}
23
24fn test() {
25 let t = (S.foo1(), S.foo2(), T.foo3(), T.foo4());
26 t<|>;
27}
28
29//- /foo.rs crate:foo
30struct S;
31
32#[cfg(not(test))]
33impl S {
34 fn foo3(&self) -> i32 { 0 }
35}
36
37#[cfg(test)]
38impl S {
39 fn foo4(&self) -> i32 { 0 }
40}
41"#,
42 );
43 assert_eq!("(i32, {unknown}, i32, {unknown})", type_at_pos(&db, pos));
44}
45
46#[test]
47fn infer_macros_expanded() {
48 assert_snapshot!(
49 infer(r#"
50struct Foo(Vec<i32>);
51
52macro_rules! foo {
53 ($($item:expr),*) => {
54 {
55 Foo(vec![$($item,)*])
56 }
57 };
58}
59
60fn main() {
61 let x = foo!(1,2);
62}
63"#),
64 @r###"
65 ![0; 17) '{Foo(v...,2,])}': Foo
66 ![1; 4) 'Foo': Foo({unknown}) -> Foo
67 ![1; 16) 'Foo(vec![1,2,])': Foo
68 ![5; 15) 'vec![1,2,]': {unknown}
69 [156; 182) '{ ...,2); }': ()
70 [166; 167) 'x': Foo
71 "###
72 );
73}
74
75#[test]
76fn infer_legacy_textual_scoped_macros_expanded() {
77 assert_snapshot!(
78 infer(r#"
79struct Foo(Vec<i32>);
80
81#[macro_use]
82mod m {
83 macro_rules! foo {
84 ($($item:expr),*) => {
85 {
86 Foo(vec![$($item,)*])
87 }
88 };
89 }
90}
91
92fn main() {
93 let x = foo!(1,2);
94 let y = crate::foo!(1,2);
95}
96"#),
97 @r###"
98 ![0; 17) '{Foo(v...,2,])}': Foo
99 ![1; 4) 'Foo': Foo({unknown}) -> Foo
100 ![1; 16) 'Foo(vec![1,2,])': Foo
101 ![5; 15) 'vec![1,2,]': {unknown}
102 [195; 251) '{ ...,2); }': ()
103 [205; 206) 'x': Foo
104 [228; 229) 'y': {unknown}
105 [232; 248) 'crate:...!(1,2)': {unknown}
106 "###
107 );
108}
109
110#[test]
111fn infer_path_qualified_macros_expanded() {
112 assert_snapshot!(
113 infer(r#"
114#[macro_export]
115macro_rules! foo {
116 () => { 42i32 }
117}
118
119mod m {
120 pub use super::foo as bar;
121}
122
123fn main() {
124 let x = crate::foo!();
125 let y = m::bar!();
126}
127"#),
128 @r###"
129 ![0; 5) '42i32': i32
130 ![0; 5) '42i32': i32
131 [111; 164) '{ ...!(); }': ()
132 [121; 122) 'x': i32
133 [148; 149) 'y': i32
134 "###
135 );
136}
137
138#[test]
139fn infer_type_value_macro_having_same_name() {
140 assert_snapshot!(
141 infer(r#"
142#[macro_export]
143macro_rules! foo {
144 () => {
145 mod foo {
146 pub use super::foo;
147 }
148 };
149 ($x:tt) => {
150 $x
151 };
152}
153
154foo!();
155
156fn foo() {
157 let foo = foo::foo!(42i32);
158}
159"#),
160 @r###"
161 ![0; 5) '42i32': i32
162 [171; 206) '{ ...32); }': ()
163 [181; 184) 'foo': i32
164 "###
165 );
166}
167
168#[test]
169fn processes_impls_generated_by_macros() {
170 let t = type_at(
171 r#"
172//- /main.rs
173macro_rules! m {
174 ($ident:ident) => (impl Trait for $ident {})
175}
176trait Trait { fn foo(self) -> u128 {} }
177struct S;
178m!(S);
179fn test() { S.foo()<|>; }
180"#,
181 );
182 assert_eq!(t, "u128");
183}
184
185#[test]
186fn infer_impl_items_generated_by_macros() {
187 let t = type_at(
188 r#"
189//- /main.rs
190macro_rules! m {
191 () => (fn foo(&self) -> u128 {0})
192}
193struct S;
194impl S {
195 m!();
196}
197
198fn test() { S.foo()<|>; }
199"#,
200 );
201 assert_eq!(t, "u128");
202}
203
204#[test]
205fn infer_impl_items_generated_by_macros_chain() {
206 let t = type_at(
207 r#"
208//- /main.rs
209macro_rules! m_inner {
210 () => {fn foo(&self) -> u128 {0}}
211}
212macro_rules! m {
213 () => {m_inner!();}
214}
215
216struct S;
217impl S {
218 m!();
219}
220
221fn test() { S.foo()<|>; }
222"#,
223 );
224 assert_eq!(t, "u128");
225}
226
227#[test]
228fn infer_macro_with_dollar_crate_is_correct_in_expr() {
229 let (db, pos) = TestDB::with_position(
230 r#"
231//- /main.rs crate:main deps:foo
232fn test() {
233 let x = (foo::foo!(1), foo::foo!(2));
234 x<|>;
235}
236
237//- /lib.rs crate:foo
238#[macro_export]
239macro_rules! foo {
240 (1) => { $crate::bar!() };
241 (2) => { 1 + $crate::baz() };
242}
243
244#[macro_export]
245macro_rules! bar {
246 () => { 42 }
247}
248
249pub fn baz() -> usize { 31usize }
250"#,
251 );
252 assert_eq!("(i32, usize)", type_at_pos(&db, pos));
253}
254
255#[test]
256fn infer_type_value_non_legacy_macro_use_as() {
257 assert_snapshot!(
258 infer(r#"
259mod m {
260 macro_rules! _foo {
261 ($x:ident) => { type $x = u64; }
262 }
263 pub(crate) use _foo as foo;
264}
265
266m::foo!(foo);
267use foo as bar;
268fn f() -> bar { 0 }
269fn main() {
270 let _a = f();
271}
272"#),
273 @r###"
274 [159; 164) '{ 0 }': u64
275 [161; 162) '0': u64
276 [175; 199) '{ ...f(); }': ()
277 [187; 189) '_a': u64
278 [193; 194) 'f': fn f() -> u64
279 [193; 196) 'f()': u64
280 "###
281 );
282}
283
284#[test]
285fn infer_builtin_macros_line() {
286 assert_snapshot!(
287 infer(r#"
288#[rustc_builtin_macro]
289macro_rules! line {() => {}}
290
291fn main() {
292 let x = line!();
293}
294"#),
295 @r###"
296 ![0; 1) '6': i32
297 [64; 88) '{ ...!(); }': ()
298 [74; 75) 'x': i32
299 "###
300 );
301}
302
303#[test]
304fn infer_builtin_macros_file() {
305 assert_snapshot!(
306 infer(r#"
307#[rustc_builtin_macro]
308macro_rules! file {() => {}}
309
310fn main() {
311 let x = file!();
312}
313"#),
314 @r###"
315 ![0; 2) '""': &str
316 [64; 88) '{ ...!(); }': ()
317 [74; 75) 'x': &str
318 "###
319 );
320}
321
322#[test]
323fn infer_builtin_macros_column() {
324 assert_snapshot!(
325 infer(r#"
326#[rustc_builtin_macro]
327macro_rules! column {() => {}}
328
329fn main() {
330 let x = column!();
331}
332"#),
333 @r###"
334 ![0; 2) '13': i32
335 [66; 92) '{ ...!(); }': ()
336 [76; 77) 'x': i32
337 "###
338 );
339}
340
341#[test]
342fn infer_derive_clone_simple() {
343 let (db, pos) = TestDB::with_position(
344 r#"
345//- /main.rs crate:main deps:std
346#[derive(Clone)]
347struct S;
348fn test() {
349 S.clone()<|>;
350}
351
352//- /lib.rs crate:std
353#[prelude_import]
354use clone::*;
355mod clone {
356 trait Clone {
357 fn clone(&self) -> Self;
358 }
359}
360"#,
361 );
362 assert_eq!("S", type_at_pos(&db, pos));
363}
364
365#[test]
366fn infer_derive_clone_with_params() {
367 let (db, pos) = TestDB::with_position(
368 r#"
369//- /main.rs crate:main deps:std
370#[derive(Clone)]
371struct S;
372#[derive(Clone)]
373struct Wrapper<T>(T);
374struct NonClone;
375fn test() {
376 (Wrapper(S).clone(), Wrapper(NonClone).clone())<|>;
377}
378
379//- /lib.rs crate:std
380#[prelude_import]
381use clone::*;
382mod clone {
383 trait Clone {
384 fn clone(&self) -> Self;
385 }
386}
387"#,
388 );
389 assert_eq!("(Wrapper<S>, {unknown})", type_at_pos(&db, pos));
390}
diff --git a/crates/ra_hir_ty/src/tests/method_resolution.rs b/crates/ra_hir_ty/src/tests/method_resolution.rs
new file mode 100644
index 000000000..ce9a06fde
--- /dev/null
+++ b/crates/ra_hir_ty/src/tests/method_resolution.rs
@@ -0,0 +1,1005 @@
1use super::{infer, type_at, type_at_pos};
2use crate::test_db::TestDB;
3use insta::assert_snapshot;
4use ra_db::fixture::WithFixture;
5
6#[test]
7fn infer_slice_method() {
8 assert_snapshot!(
9 infer(r#"
10#[lang = "slice"]
11impl<T> [T] {
12 fn foo(&self) -> T {
13 loop {}
14 }
15}
16
17#[lang = "slice_alloc"]
18impl<T> [T] {}
19
20fn test() {
21 <[_]>::foo(b"foo");
22}
23"#),
24 @r###"
25 [45; 49) 'self': &[T]
26 [56; 79) '{ ... }': T
27 [66; 73) 'loop {}': !
28 [71; 73) '{}': ()
29 [133; 160) '{ ...o"); }': ()
30 [139; 149) '<[_]>::foo': fn foo<u8>(&[T]) -> T
31 [139; 157) '<[_]>:..."foo")': u8
32 [150; 156) 'b"foo"': &[u8]
33 "###
34 );
35}
36
37#[test]
38fn infer_associated_method_struct() {
39 assert_snapshot!(
40 infer(r#"
41struct A { x: u32 }
42
43impl A {
44 fn new() -> A {
45 A { x: 0 }
46 }
47}
48fn test() {
49 let a = A::new();
50 a.x;
51}
52"#),
53 @r###"
54 [49; 75) '{ ... }': A
55 [59; 69) 'A { x: 0 }': A
56 [66; 67) '0': u32
57 [88; 122) '{ ...a.x; }': ()
58 [98; 99) 'a': A
59 [102; 108) 'A::new': fn new() -> A
60 [102; 110) 'A::new()': A
61 [116; 117) 'a': A
62 [116; 119) 'a.x': u32
63 "###
64 );
65}
66
67#[test]
68fn infer_associated_method_enum() {
69 assert_snapshot!(
70 infer(r#"
71enum A { B, C }
72
73impl A {
74 pub fn b() -> A {
75 A::B
76 }
77 pub fn c() -> A {
78 A::C
79 }
80}
81fn test() {
82 let a = A::b();
83 a;
84 let c = A::c();
85 c;
86}
87"#),
88 @r###"
89 [47; 67) '{ ... }': A
90 [57; 61) 'A::B': A
91 [88; 108) '{ ... }': A
92 [98; 102) 'A::C': A
93 [121; 178) '{ ... c; }': ()
94 [131; 132) 'a': A
95 [135; 139) 'A::b': fn b() -> A
96 [135; 141) 'A::b()': A
97 [147; 148) 'a': A
98 [158; 159) 'c': A
99 [162; 166) 'A::c': fn c() -> A
100 [162; 168) 'A::c()': A
101 [174; 175) 'c': A
102 "###
103 );
104}
105
106#[test]
107fn infer_associated_method_with_modules() {
108 assert_snapshot!(
109 infer(r#"
110mod a {
111 struct A;
112 impl A { pub fn thing() -> A { A {} }}
113}
114
115mod b {
116 struct B;
117 impl B { pub fn thing() -> u32 { 99 }}
118
119 mod c {
120 struct C;
121 impl C { pub fn thing() -> C { C {} }}
122 }
123}
124use b::c;
125
126fn test() {
127 let x = a::A::thing();
128 let y = b::B::thing();
129 let z = c::C::thing();
130}
131"#),
132 @r###"
133 [56; 64) '{ A {} }': A
134 [58; 62) 'A {}': A
135 [126; 132) '{ 99 }': u32
136 [128; 130) '99': u32
137 [202; 210) '{ C {} }': C
138 [204; 208) 'C {}': C
139 [241; 325) '{ ...g(); }': ()
140 [251; 252) 'x': A
141 [255; 266) 'a::A::thing': fn thing() -> A
142 [255; 268) 'a::A::thing()': A
143 [278; 279) 'y': u32
144 [282; 293) 'b::B::thing': fn thing() -> u32
145 [282; 295) 'b::B::thing()': u32
146 [305; 306) 'z': C
147 [309; 320) 'c::C::thing': fn thing() -> C
148 [309; 322) 'c::C::thing()': C
149 "###
150 );
151}
152
153#[test]
154fn infer_associated_method_generics() {
155 assert_snapshot!(
156 infer(r#"
157struct Gen<T> {
158 val: T
159}
160
161impl<T> Gen<T> {
162 pub fn make(val: T) -> Gen<T> {
163 Gen { val }
164 }
165}
166
167fn test() {
168 let a = Gen::make(0u32);
169}
170"#),
171 @r###"
172 [64; 67) 'val': T
173 [82; 109) '{ ... }': Gen<T>
174 [92; 103) 'Gen { val }': Gen<T>
175 [98; 101) 'val': T
176 [123; 155) '{ ...32); }': ()
177 [133; 134) 'a': Gen<u32>
178 [137; 146) 'Gen::make': fn make<u32>(T) -> Gen<T>
179 [137; 152) 'Gen::make(0u32)': Gen<u32>
180 [147; 151) '0u32': u32
181 "###
182 );
183}
184
185#[test]
186fn infer_associated_method_generics_with_default_param() {
187 assert_snapshot!(
188 infer(r#"
189struct Gen<T=u32> {
190 val: T
191}
192
193impl<T> Gen<T> {
194 pub fn make() -> Gen<T> {
195 loop { }
196 }
197}
198
199fn test() {
200 let a = Gen::make();
201}
202"#),
203 @r###"
204 [80; 104) '{ ... }': Gen<T>
205 [90; 98) 'loop { }': !
206 [95; 98) '{ }': ()
207 [118; 146) '{ ...e(); }': ()
208 [128; 129) 'a': Gen<u32>
209 [132; 141) 'Gen::make': fn make<u32>() -> Gen<T>
210 [132; 143) 'Gen::make()': Gen<u32>
211 "###
212 );
213}
214
215#[test]
216fn infer_associated_method_generics_with_default_tuple_param() {
217 let t = type_at(
218 r#"
219//- /main.rs
220struct Gen<T=()> {
221 val: T
222}
223
224impl<T> Gen<T> {
225 pub fn make() -> Gen<T> {
226 loop { }
227 }
228}
229
230fn test() {
231 let a = Gen::make();
232 a.val<|>;
233}
234"#,
235 );
236 assert_eq!(t, "()");
237}
238
239#[test]
240fn infer_associated_method_generics_without_args() {
241 assert_snapshot!(
242 infer(r#"
243struct Gen<T> {
244 val: T
245}
246
247impl<T> Gen<T> {
248 pub fn make() -> Gen<T> {
249 loop { }
250 }
251}
252
253fn test() {
254 let a = Gen::<u32>::make();
255}
256"#),
257 @r###"
258 [76; 100) '{ ... }': Gen<T>
259 [86; 94) 'loop { }': !
260 [91; 94) '{ }': ()
261 [114; 149) '{ ...e(); }': ()
262 [124; 125) 'a': Gen<u32>
263 [128; 144) 'Gen::<...::make': fn make<u32>() -> Gen<T>
264 [128; 146) 'Gen::<...make()': Gen<u32>
265 "###
266 );
267}
268
269#[test]
270fn infer_associated_method_generics_2_type_params_without_args() {
271 assert_snapshot!(
272 infer(r#"
273struct Gen<T, U> {
274 val: T,
275 val2: U,
276}
277
278impl<T> Gen<u32, T> {
279 pub fn make() -> Gen<u32,T> {
280 loop { }
281 }
282}
283
284fn test() {
285 let a = Gen::<u32, u64>::make();
286}
287"#),
288 @r###"
289 [102; 126) '{ ... }': Gen<u32, T>
290 [112; 120) 'loop { }': !
291 [117; 120) '{ }': ()
292 [140; 180) '{ ...e(); }': ()
293 [150; 151) 'a': Gen<u32, u64>
294 [154; 175) 'Gen::<...::make': fn make<u64>() -> Gen<u32, T>
295 [154; 177) 'Gen::<...make()': Gen<u32, u64>
296 "###
297 );
298}
299
300#[test]
301fn cross_crate_associated_method_call() {
302 let (db, pos) = TestDB::with_position(
303 r#"
304//- /main.rs crate:main deps:other_crate
305fn test() {
306 let x = other_crate::foo::S::thing();
307 x<|>;
308}
309
310//- /lib.rs crate:other_crate
311mod foo {
312 struct S;
313 impl S {
314 fn thing() -> i128 {}
315 }
316}
317"#,
318 );
319 assert_eq!("i128", type_at_pos(&db, pos));
320}
321
322#[test]
323fn infer_trait_method_simple() {
324 // the trait implementation is intentionally incomplete -- it shouldn't matter
325 assert_snapshot!(
326 infer(r#"
327trait Trait1 {
328 fn method(&self) -> u32;
329}
330struct S1;
331impl Trait1 for S1 {}
332trait Trait2 {
333 fn method(&self) -> i128;
334}
335struct S2;
336impl Trait2 for S2 {}
337fn test() {
338 S1.method(); // -> u32
339 S2.method(); // -> i128
340}
341"#),
342 @r###"
343 [31; 35) 'self': &Self
344 [110; 114) 'self': &Self
345 [170; 228) '{ ...i128 }': ()
346 [176; 178) 'S1': S1
347 [176; 187) 'S1.method()': u32
348 [203; 205) 'S2': S2
349 [203; 214) 'S2.method()': i128
350 "###
351 );
352}
353
354#[test]
355fn infer_trait_method_scoped() {
356 // the trait implementation is intentionally incomplete -- it shouldn't matter
357 assert_snapshot!(
358 infer(r#"
359struct S;
360mod foo {
361 pub trait Trait1 {
362 fn method(&self) -> u32;
363 }
364 impl Trait1 for super::S {}
365}
366mod bar {
367 pub trait Trait2 {
368 fn method(&self) -> i128;
369 }
370 impl Trait2 for super::S {}
371}
372
373mod foo_test {
374 use super::S;
375 use super::foo::Trait1;
376 fn test() {
377 S.method(); // -> u32
378 }
379}
380
381mod bar_test {
382 use super::S;
383 use super::bar::Trait2;
384 fn test() {
385 S.method(); // -> i128
386 }
387}
388"#),
389 @r###"
390 [63; 67) 'self': &Self
391 [169; 173) 'self': &Self
392 [300; 337) '{ ... }': ()
393 [310; 311) 'S': S
394 [310; 320) 'S.method()': u32
395 [416; 454) '{ ... }': ()
396 [426; 427) 'S': S
397 [426; 436) 'S.method()': i128
398 "###
399 );
400}
401
402#[test]
403fn infer_trait_method_generic_1() {
404 // the trait implementation is intentionally incomplete -- it shouldn't matter
405 assert_snapshot!(
406 infer(r#"
407trait Trait<T> {
408 fn method(&self) -> T;
409}
410struct S;
411impl Trait<u32> for S {}
412fn test() {
413 S.method();
414}
415"#),
416 @r###"
417 [33; 37) 'self': &Self
418 [92; 111) '{ ...d(); }': ()
419 [98; 99) 'S': S
420 [98; 108) 'S.method()': u32
421 "###
422 );
423}
424
425#[test]
426fn infer_trait_method_generic_more_params() {
427 // the trait implementation is intentionally incomplete -- it shouldn't matter
428 assert_snapshot!(
429 infer(r#"
430trait Trait<T1, T2, T3> {
431 fn method1(&self) -> (T1, T2, T3);
432 fn method2(&self) -> (T3, T2, T1);
433}
434struct S1;
435impl Trait<u8, u16, u32> for S1 {}
436struct S2;
437impl<T> Trait<i8, i16, T> for S2 {}
438fn test() {
439 S1.method1(); // u8, u16, u32
440 S1.method2(); // u32, u16, u8
441 S2.method1(); // i8, i16, {unknown}
442 S2.method2(); // {unknown}, i16, i8
443}
444"#),
445 @r###"
446 [43; 47) 'self': &Self
447 [82; 86) 'self': &Self
448 [210; 361) '{ ..., i8 }': ()
449 [216; 218) 'S1': S1
450 [216; 228) 'S1.method1()': (u8, u16, u32)
451 [250; 252) 'S1': S1
452 [250; 262) 'S1.method2()': (u32, u16, u8)
453 [284; 286) 'S2': S2
454 [284; 296) 'S2.method1()': (i8, i16, {unknown})
455 [324; 326) 'S2': S2
456 [324; 336) 'S2.method2()': ({unknown}, i16, i8)
457 "###
458 );
459}
460
461#[test]
462fn infer_trait_method_generic_2() {
463 // the trait implementation is intentionally incomplete -- it shouldn't matter
464 assert_snapshot!(
465 infer(r#"
466trait Trait<T> {
467 fn method(&self) -> T;
468}
469struct S<T>(T);
470impl<U> Trait<U> for S<U> {}
471fn test() {
472 S(1u32).method();
473}
474"#),
475 @r###"
476 [33; 37) 'self': &Self
477 [102; 127) '{ ...d(); }': ()
478 [108; 109) 'S': S<u32>(T) -> S<T>
479 [108; 115) 'S(1u32)': S<u32>
480 [108; 124) 'S(1u32...thod()': u32
481 [110; 114) '1u32': u32
482 "###
483 );
484}
485
486#[test]
487fn infer_trait_assoc_method() {
488 assert_snapshot!(
489 infer(r#"
490trait Default {
491 fn default() -> Self;
492}
493struct S;
494impl Default for S {}
495fn test() {
496 let s1: S = Default::default();
497 let s2 = S::default();
498 let s3 = <S as Default>::default();
499}
500"#),
501 @r###"
502 [87; 193) '{ ...t(); }': ()
503 [97; 99) 's1': S
504 [105; 121) 'Defaul...efault': fn default<S>() -> Self
505 [105; 123) 'Defaul...ault()': S
506 [133; 135) 's2': S
507 [138; 148) 'S::default': fn default<S>() -> Self
508 [138; 150) 'S::default()': S
509 [160; 162) 's3': S
510 [165; 188) '<S as ...efault': fn default<S>() -> Self
511 [165; 190) '<S as ...ault()': S
512 "###
513 );
514}
515
516#[test]
517fn infer_trait_assoc_method_generics_1() {
518 assert_snapshot!(
519 infer(r#"
520trait Trait<T> {
521 fn make() -> T;
522}
523struct S;
524impl Trait<u32> for S {}
525struct G<T>;
526impl<T> Trait<T> for G<T> {}
527fn test() {
528 let a = S::make();
529 let b = G::<u64>::make();
530 let c: f64 = G::make();
531}
532"#),
533 @r###"
534 [127; 211) '{ ...e(); }': ()
535 [137; 138) 'a': u32
536 [141; 148) 'S::make': fn make<S, u32>() -> T
537 [141; 150) 'S::make()': u32
538 [160; 161) 'b': u64
539 [164; 178) 'G::<u64>::make': fn make<G<u64>, u64>() -> T
540 [164; 180) 'G::<u6...make()': u64
541 [190; 191) 'c': f64
542 [199; 206) 'G::make': fn make<G<f64>, f64>() -> T
543 [199; 208) 'G::make()': f64
544 "###
545 );
546}
547
548#[test]
549fn infer_trait_assoc_method_generics_2() {
550 assert_snapshot!(
551 infer(r#"
552trait Trait<T> {
553 fn make<U>() -> (T, U);
554}
555struct S;
556impl Trait<u32> for S {}
557struct G<T>;
558impl<T> Trait<T> for G<T> {}
559fn test() {
560 let a = S::make::<i64>();
561 let b: (_, i64) = S::make();
562 let c = G::<u32>::make::<i64>();
563 let d: (u32, _) = G::make::<i64>();
564 let e: (u32, i64) = G::make();
565}
566"#),
567 @r###"
568 [135; 313) '{ ...e(); }': ()
569 [145; 146) 'a': (u32, i64)
570 [149; 163) 'S::make::<i64>': fn make<S, u32, i64>() -> (T, U)
571 [149; 165) 'S::mak...i64>()': (u32, i64)
572 [175; 176) 'b': (u32, i64)
573 [189; 196) 'S::make': fn make<S, u32, i64>() -> (T, U)
574 [189; 198) 'S::make()': (u32, i64)
575 [208; 209) 'c': (u32, i64)
576 [212; 233) 'G::<u3...:<i64>': fn make<G<u32>, u32, i64>() -> (T, U)
577 [212; 235) 'G::<u3...i64>()': (u32, i64)
578 [245; 246) 'd': (u32, i64)
579 [259; 273) 'G::make::<i64>': fn make<G<u32>, u32, i64>() -> (T, U)
580 [259; 275) 'G::mak...i64>()': (u32, i64)
581 [285; 286) 'e': (u32, i64)
582 [301; 308) 'G::make': fn make<G<u32>, u32, i64>() -> (T, U)
583 [301; 310) 'G::make()': (u32, i64)
584 "###
585 );
586}
587
588#[test]
589fn infer_trait_assoc_method_generics_3() {
590 assert_snapshot!(
591 infer(r#"
592trait Trait<T> {
593 fn make() -> (Self, T);
594}
595struct S<T>;
596impl Trait<i64> for S<i32> {}
597fn test() {
598 let a = S::make();
599}
600"#),
601 @r###"
602 [101; 127) '{ ...e(); }': ()
603 [111; 112) 'a': (S<i32>, i64)
604 [115; 122) 'S::make': fn make<S<i32>, i64>() -> (Self, T)
605 [115; 124) 'S::make()': (S<i32>, i64)
606 "###
607 );
608}
609
610#[test]
611fn infer_trait_assoc_method_generics_4() {
612 assert_snapshot!(
613 infer(r#"
614trait Trait<T> {
615 fn make() -> (Self, T);
616}
617struct S<T>;
618impl Trait<i64> for S<u64> {}
619impl Trait<i32> for S<u32> {}
620fn test() {
621 let a: (S<u64>, _) = S::make();
622 let b: (_, i32) = S::make();
623}
624"#),
625 @r###"
626 [131; 203) '{ ...e(); }': ()
627 [141; 142) 'a': (S<u64>, i64)
628 [158; 165) 'S::make': fn make<S<u64>, i64>() -> (Self, T)
629 [158; 167) 'S::make()': (S<u64>, i64)
630 [177; 178) 'b': (S<u32>, i32)
631 [191; 198) 'S::make': fn make<S<u32>, i32>() -> (Self, T)
632 [191; 200) 'S::make()': (S<u32>, i32)
633 "###
634 );
635}
636
637#[test]
638fn infer_trait_assoc_method_generics_5() {
639 assert_snapshot!(
640 infer(r#"
641trait Trait<T> {
642 fn make<U>() -> (Self, T, U);
643}
644struct S<T>;
645impl Trait<i64> for S<u64> {}
646fn test() {
647 let a = <S as Trait<i64>>::make::<u8>();
648 let b: (S<u64>, _, _) = Trait::<i64>::make::<u8>();
649}
650"#),
651 @r###"
652 [107; 211) '{ ...>(); }': ()
653 [117; 118) 'a': (S<u64>, i64, u8)
654 [121; 150) '<S as ...::<u8>': fn make<S<u64>, i64, u8>() -> (Self, T, U)
655 [121; 152) '<S as ...<u8>()': (S<u64>, i64, u8)
656 [162; 163) 'b': (S<u64>, i64, u8)
657 [182; 206) 'Trait:...::<u8>': fn make<S<u64>, i64, u8>() -> (Self, T, U)
658 [182; 208) 'Trait:...<u8>()': (S<u64>, i64, u8)
659 "###
660 );
661}
662
663#[test]
664fn infer_call_trait_method_on_generic_param_1() {
665 assert_snapshot!(
666 infer(r#"
667trait Trait {
668 fn method(&self) -> u32;
669}
670fn test<T: Trait>(t: T) {
671 t.method();
672}
673"#),
674 @r###"
675 [30; 34) 'self': &Self
676 [64; 65) 't': T
677 [70; 89) '{ ...d(); }': ()
678 [76; 77) 't': T
679 [76; 86) 't.method()': u32
680 "###
681 );
682}
683
684#[test]
685fn infer_call_trait_method_on_generic_param_2() {
686 assert_snapshot!(
687 infer(r#"
688trait Trait<T> {
689 fn method(&self) -> T;
690}
691fn test<U, T: Trait<U>>(t: T) {
692 t.method();
693}
694"#),
695 @r###"
696 [33; 37) 'self': &Self
697 [71; 72) 't': T
698 [77; 96) '{ ...d(); }': ()
699 [83; 84) 't': T
700 [83; 93) 't.method()': [missing name]
701 "###
702 );
703}
704
705#[test]
706fn infer_with_multiple_trait_impls() {
707 assert_snapshot!(
708 infer(r#"
709trait Into<T> {
710 fn into(self) -> T;
711}
712struct S;
713impl Into<u32> for S {}
714impl Into<u64> for S {}
715fn test() {
716 let x: u32 = S.into();
717 let y: u64 = S.into();
718 let z = Into::<u64>::into(S);
719}
720"#),
721 @r###"
722 [29; 33) 'self': Self
723 [111; 202) '{ ...(S); }': ()
724 [121; 122) 'x': u32
725 [130; 131) 'S': S
726 [130; 138) 'S.into()': u32
727 [148; 149) 'y': u64
728 [157; 158) 'S': S
729 [157; 165) 'S.into()': u64
730 [175; 176) 'z': u64
731 [179; 196) 'Into::...::into': fn into<S, u64>(Self) -> T
732 [179; 199) 'Into::...nto(S)': u64
733 [197; 198) 'S': S
734 "###
735 );
736}
737
738#[test]
739fn method_resolution_unify_impl_self_type() {
740 let t = type_at(
741 r#"
742//- /main.rs
743struct S<T>;
744impl S<u32> { fn foo(&self) -> u8 {} }
745impl S<i32> { fn foo(&self) -> i8 {} }
746fn test() { (S::<u32>.foo(), S::<i32>.foo())<|>; }
747"#,
748 );
749 assert_eq!(t, "(u8, i8)");
750}
751
752#[test]
753fn method_resolution_trait_before_autoref() {
754 let t = type_at(
755 r#"
756//- /main.rs
757trait Trait { fn foo(self) -> u128; }
758struct S;
759impl S { fn foo(&self) -> i8 { 0 } }
760impl Trait for S { fn foo(self) -> u128 { 0 } }
761fn test() { S.foo()<|>; }
762"#,
763 );
764 assert_eq!(t, "u128");
765}
766
767#[test]
768fn method_resolution_by_value_before_autoref() {
769 let t = type_at(
770 r#"
771//- /main.rs
772trait Clone { fn clone(&self) -> Self; }
773struct S;
774impl Clone for S {}
775impl Clone for &S {}
776fn test() { (S.clone(), (&S).clone(), (&&S).clone())<|>; }
777"#,
778 );
779 assert_eq!(t, "(S, S, &S)");
780}
781
782#[test]
783fn method_resolution_trait_before_autoderef() {
784 let t = type_at(
785 r#"
786//- /main.rs
787trait Trait { fn foo(self) -> u128; }
788struct S;
789impl S { fn foo(self) -> i8 { 0 } }
790impl Trait for &S { fn foo(self) -> u128 { 0 } }
791fn test() { (&S).foo()<|>; }
792"#,
793 );
794 assert_eq!(t, "u128");
795}
796
797#[test]
798fn method_resolution_impl_before_trait() {
799 let t = type_at(
800 r#"
801//- /main.rs
802trait Trait { fn foo(self) -> u128; }
803struct S;
804impl S { fn foo(self) -> i8 { 0 } }
805impl Trait for S { fn foo(self) -> u128 { 0 } }
806fn test() { S.foo()<|>; }
807"#,
808 );
809 assert_eq!(t, "i8");
810}
811
812#[test]
813fn method_resolution_impl_ref_before_trait() {
814 let t = type_at(
815 r#"
816//- /main.rs
817trait Trait { fn foo(self) -> u128; }
818struct S;
819impl S { fn foo(&self) -> i8 { 0 } }
820impl Trait for &S { fn foo(self) -> u128 { 0 } }
821fn test() { S.foo()<|>; }
822"#,
823 );
824 assert_eq!(t, "i8");
825}
826
827#[test]
828fn method_resolution_trait_autoderef() {
829 let t = type_at(
830 r#"
831//- /main.rs
832trait Trait { fn foo(self) -> u128; }
833struct S;
834impl Trait for S { fn foo(self) -> u128 { 0 } }
835fn test() { (&S).foo()<|>; }
836"#,
837 );
838 assert_eq!(t, "u128");
839}
840
841#[test]
842fn method_resolution_trait_from_prelude() {
843 let (db, pos) = TestDB::with_position(
844 r#"
845//- /main.rs crate:main deps:other_crate
846struct S;
847impl Clone for S {}
848
849fn test() {
850 S.clone()<|>;
851}
852
853//- /lib.rs crate:other_crate
854#[prelude_import] use foo::*;
855
856mod foo {
857 trait Clone {
858 fn clone(&self) -> Self;
859 }
860}
861"#,
862 );
863 assert_eq!("S", type_at_pos(&db, pos));
864}
865
866#[test]
867fn method_resolution_where_clause_for_unknown_trait() {
868 // The blanket impl currently applies because we ignore the unresolved where clause
869 let t = type_at(
870 r#"
871//- /main.rs
872trait Trait { fn foo(self) -> u128; }
873struct S;
874impl<T> Trait for T where T: UnknownTrait {}
875fn test() { (&S).foo()<|>; }
876"#,
877 );
878 assert_eq!(t, "u128");
879}
880
881#[test]
882fn method_resolution_where_clause_not_met() {
883 // The blanket impl shouldn't apply because we can't prove S: Clone
884 let t = type_at(
885 r#"
886//- /main.rs
887trait Clone {}
888trait Trait { fn foo(self) -> u128; }
889struct S;
890impl<T> Trait for T where T: Clone {}
891fn test() { (&S).foo()<|>; }
892"#,
893 );
894 // This is also to make sure that we don't resolve to the foo method just
895 // because that's the only method named foo we can find, which would make
896 // the below tests not work
897 assert_eq!(t, "{unknown}");
898}
899
900#[test]
901fn method_resolution_where_clause_inline_not_met() {
902 // The blanket impl shouldn't apply because we can't prove S: Clone
903 let t = type_at(
904 r#"
905//- /main.rs
906trait Clone {}
907trait Trait { fn foo(self) -> u128; }
908struct S;
909impl<T: Clone> Trait for T {}
910fn test() { (&S).foo()<|>; }
911"#,
912 );
913 assert_eq!(t, "{unknown}");
914}
915
916#[test]
917fn method_resolution_where_clause_1() {
918 let t = type_at(
919 r#"
920//- /main.rs
921trait Clone {}
922trait Trait { fn foo(self) -> u128; }
923struct S;
924impl Clone for S {}
925impl<T> Trait for T where T: Clone {}
926fn test() { S.foo()<|>; }
927"#,
928 );
929 assert_eq!(t, "u128");
930}
931
932#[test]
933fn method_resolution_where_clause_2() {
934 let t = type_at(
935 r#"
936//- /main.rs
937trait Into<T> { fn into(self) -> T; }
938trait From<T> { fn from(other: T) -> Self; }
939struct S1;
940struct S2;
941impl From<S2> for S1 {}
942impl<T, U> Into<U> for T where U: From<T> {}
943fn test() { S2.into()<|>; }
944"#,
945 );
946 assert_eq!(t, "{unknown}");
947}
948
949#[test]
950fn method_resolution_where_clause_inline() {
951 let t = type_at(
952 r#"
953//- /main.rs
954trait Into<T> { fn into(self) -> T; }
955trait From<T> { fn from(other: T) -> Self; }
956struct S1;
957struct S2;
958impl From<S2> for S1 {}
959impl<T, U: From<T>> Into<U> for T {}
960fn test() { S2.into()<|>; }
961"#,
962 );
963 assert_eq!(t, "{unknown}");
964}
965
966#[test]
967fn method_resolution_encountering_fn_type() {
968 type_at(
969 r#"
970//- /main.rs
971fn foo() {}
972trait FnOnce { fn call(self); }
973fn test() { foo.call()<|>; }
974"#,
975 );
976}
977
978#[test]
979fn method_resolution_slow() {
980 // this can get quite slow if we set the solver size limit too high
981 let t = type_at(
982 r#"
983//- /main.rs
984trait SendX {}
985
986struct S1; impl SendX for S1 {}
987struct S2; impl SendX for S2 {}
988struct U1;
989
990trait Trait { fn method(self); }
991
992struct X1<A, B> {}
993impl<A, B> SendX for X1<A, B> where A: SendX, B: SendX {}
994
995struct S<B, C> {}
996
997trait FnX {}
998
999impl<B, C> Trait for S<B, C> where C: FnX, B: SendX {}
1000
1001fn test() { (S {}).method()<|>; }
1002"#,
1003 );
1004 assert_eq!(t, "()");
1005}
diff --git a/crates/ra_hir_ty/src/tests/patterns.rs b/crates/ra_hir_ty/src/tests/patterns.rs
new file mode 100644
index 000000000..cb3890b42
--- /dev/null
+++ b/crates/ra_hir_ty/src/tests/patterns.rs
@@ -0,0 +1,238 @@
1use super::infer;
2use insta::assert_snapshot;
3use test_utils::covers;
4
5#[test]
6fn infer_pattern() {
7 assert_snapshot!(
8 infer(r#"
9fn test(x: &i32) {
10 let y = x;
11 let &z = x;
12 let a = z;
13 let (c, d) = (1, "hello");
14
15 for (e, f) in some_iter {
16 let g = e;
17 }
18
19 if let [val] = opt {
20 let h = val;
21 }
22
23 let lambda = |a: u64, b, c: i32| { a + b; c };
24
25 let ref ref_to_x = x;
26 let mut mut_x = x;
27 let ref mut mut_ref_to_x = x;
28 let k = mut_ref_to_x;
29}
30"#),
31 @r###"
32 [9; 10) 'x': &i32
33 [18; 369) '{ ...o_x; }': ()
34 [28; 29) 'y': &i32
35 [32; 33) 'x': &i32
36 [43; 45) '&z': &i32
37 [44; 45) 'z': i32
38 [48; 49) 'x': &i32
39 [59; 60) 'a': i32
40 [63; 64) 'z': i32
41 [74; 80) '(c, d)': (i32, &str)
42 [75; 76) 'c': i32
43 [78; 79) 'd': &str
44 [83; 95) '(1, "hello")': (i32, &str)
45 [84; 85) '1': i32
46 [87; 94) '"hello"': &str
47 [102; 152) 'for (e... }': ()
48 [106; 112) '(e, f)': ({unknown}, {unknown})
49 [107; 108) 'e': {unknown}
50 [110; 111) 'f': {unknown}
51 [116; 125) 'some_iter': {unknown}
52 [126; 152) '{ ... }': ()
53 [140; 141) 'g': {unknown}
54 [144; 145) 'e': {unknown}
55 [158; 205) 'if let... }': ()
56 [165; 170) '[val]': {unknown}
57 [173; 176) 'opt': {unknown}
58 [177; 205) '{ ... }': ()
59 [191; 192) 'h': {unknown}
60 [195; 198) 'val': {unknown}
61 [215; 221) 'lambda': |u64, u64, i32| -> i32
62 [224; 256) '|a: u6...b; c }': |u64, u64, i32| -> i32
63 [225; 226) 'a': u64
64 [233; 234) 'b': u64
65 [236; 237) 'c': i32
66 [244; 256) '{ a + b; c }': i32
67 [246; 247) 'a': u64
68 [246; 251) 'a + b': u64
69 [250; 251) 'b': u64
70 [253; 254) 'c': i32
71 [267; 279) 'ref ref_to_x': &&i32
72 [282; 283) 'x': &i32
73 [293; 302) 'mut mut_x': &i32
74 [305; 306) 'x': &i32
75 [316; 336) 'ref mu...f_to_x': &mut &i32
76 [339; 340) 'x': &i32
77 [350; 351) 'k': &mut &i32
78 [354; 366) 'mut_ref_to_x': &mut &i32
79 "###
80 );
81}
82
83#[test]
84fn infer_pattern_match_ergonomics() {
85 assert_snapshot!(
86 infer(r#"
87struct A<T>(T);
88
89fn test() {
90 let A(n) = &A(1);
91 let A(n) = &mut A(1);
92}
93"#),
94 @r###"
95 [28; 79) '{ ...(1); }': ()
96 [38; 42) 'A(n)': A<i32>
97 [40; 41) 'n': &i32
98 [45; 50) '&A(1)': &A<i32>
99 [46; 47) 'A': A<i32>(T) -> A<T>
100 [46; 50) 'A(1)': A<i32>
101 [48; 49) '1': i32
102 [60; 64) 'A(n)': A<i32>
103 [62; 63) 'n': &mut i32
104 [67; 76) '&mut A(1)': &mut A<i32>
105 [72; 73) 'A': A<i32>(T) -> A<T>
106 [72; 76) 'A(1)': A<i32>
107 [74; 75) '1': i32
108 "###
109 );
110}
111
112#[test]
113fn infer_pattern_match_ergonomics_ref() {
114 covers!(match_ergonomics_ref);
115 assert_snapshot!(
116 infer(r#"
117fn test() {
118 let v = &(1, &2);
119 let (_, &w) = v;
120}
121"#),
122 @r###"
123 [11; 57) '{ ...= v; }': ()
124 [21; 22) 'v': &(i32, &i32)
125 [25; 33) '&(1, &2)': &(i32, &i32)
126 [26; 33) '(1, &2)': (i32, &i32)
127 [27; 28) '1': i32
128 [30; 32) '&2': &i32
129 [31; 32) '2': i32
130 [43; 50) '(_, &w)': (i32, &i32)
131 [44; 45) '_': i32
132 [47; 49) '&w': &i32
133 [48; 49) 'w': i32
134 [53; 54) 'v': &(i32, &i32)
135 "###
136 );
137}
138
139#[test]
140fn infer_adt_pattern() {
141 assert_snapshot!(
142 infer(r#"
143enum E {
144 A { x: usize },
145 B
146}
147
148struct S(u32, E);
149
150fn test() {
151 let e = E::A { x: 3 };
152
153 let S(y, z) = foo;
154 let E::A { x: new_var } = e;
155
156 match e {
157 E::A { x } => x,
158 E::B if foo => 1,
159 E::B => 10,
160 };
161
162 let ref d @ E::A { .. } = e;
163 d;
164}
165"#),
166 @r###"
167 [68; 289) '{ ... d; }': ()
168 [78; 79) 'e': E
169 [82; 95) 'E::A { x: 3 }': E
170 [92; 93) '3': usize
171 [106; 113) 'S(y, z)': S
172 [108; 109) 'y': u32
173 [111; 112) 'z': E
174 [116; 119) 'foo': S
175 [129; 148) 'E::A {..._var }': E
176 [139; 146) 'new_var': usize
177 [151; 152) 'e': E
178 [159; 245) 'match ... }': usize
179 [165; 166) 'e': E
180 [177; 187) 'E::A { x }': E
181 [184; 185) 'x': usize
182 [191; 192) 'x': usize
183 [202; 206) 'E::B': E
184 [210; 213) 'foo': bool
185 [217; 218) '1': usize
186 [228; 232) 'E::B': E
187 [236; 238) '10': usize
188 [256; 275) 'ref d ...{ .. }': &E
189 [264; 275) 'E::A { .. }': E
190 [278; 279) 'e': E
191 [285; 286) 'd': &E
192 "###
193 );
194}
195
196#[test]
197fn infer_generics_in_patterns() {
198 assert_snapshot!(
199 infer(r#"
200struct A<T> {
201 x: T,
202}
203
204enum Option<T> {
205 Some(T),
206 None,
207}
208
209fn test(a1: A<u32>, o: Option<u64>) {
210 let A { x: x2 } = a1;
211 let A::<i64> { x: x3 } = A { x: 1 };
212 match o {
213 Option::Some(t) => t,
214 _ => 1,
215 };
216}
217"#),
218 @r###"
219 [79; 81) 'a1': A<u32>
220 [91; 92) 'o': Option<u64>
221 [107; 244) '{ ... }; }': ()
222 [117; 128) 'A { x: x2 }': A<u32>
223 [124; 126) 'x2': u32
224 [131; 133) 'a1': A<u32>
225 [143; 161) 'A::<i6...: x3 }': A<i64>
226 [157; 159) 'x3': i64
227 [164; 174) 'A { x: 1 }': A<i64>
228 [171; 172) '1': i64
229 [180; 241) 'match ... }': u64
230 [186; 187) 'o': Option<u64>
231 [198; 213) 'Option::Some(t)': Option<u64>
232 [211; 212) 't': u64
233 [217; 218) 't': u64
234 [228; 229) '_': Option<u64>
235 [233; 234) '1': u64
236 "###
237 );
238}
diff --git a/crates/ra_hir_ty/src/tests/regression.rs b/crates/ra_hir_ty/src/tests/regression.rs
new file mode 100644
index 000000000..09d684ac2
--- /dev/null
+++ b/crates/ra_hir_ty/src/tests/regression.rs
@@ -0,0 +1,333 @@
1use super::infer;
2use insta::assert_snapshot;
3use test_utils::covers;
4
5#[test]
6fn bug_484() {
7 assert_snapshot!(
8 infer(r#"
9fn test() {
10 let x = if true {};
11}
12"#),
13 @r###"
14 [11; 37) '{ l... {}; }': ()
15 [20; 21) 'x': ()
16 [24; 34) 'if true {}': ()
17 [27; 31) 'true': bool
18 [32; 34) '{}': ()
19 "###
20 );
21}
22
23#[test]
24fn no_panic_on_field_of_enum() {
25 assert_snapshot!(
26 infer(r#"
27enum X {}
28
29fn test(x: X) {
30 x.some_field;
31}
32"#),
33 @r###"
34 [20; 21) 'x': X
35 [26; 47) '{ ...eld; }': ()
36 [32; 33) 'x': X
37 [32; 44) 'x.some_field': {unknown}
38 "###
39 );
40}
41
42#[test]
43fn bug_585() {
44 assert_snapshot!(
45 infer(r#"
46fn test() {
47 X {};
48 match x {
49 A::B {} => (),
50 A::Y() => (),
51 }
52}
53"#),
54 @r###"
55 [11; 89) '{ ... } }': ()
56 [17; 21) 'X {}': {unknown}
57 [27; 87) 'match ... }': ()
58 [33; 34) 'x': {unknown}
59 [45; 52) 'A::B {}': {unknown}
60 [56; 58) '()': ()
61 [68; 74) 'A::Y()': {unknown}
62 [78; 80) '()': ()
63 "###
64 );
65}
66
67#[test]
68fn bug_651() {
69 assert_snapshot!(
70 infer(r#"
71fn quux() {
72 let y = 92;
73 1 + y;
74}
75"#),
76 @r###"
77 [11; 41) '{ ...+ y; }': ()
78 [21; 22) 'y': i32
79 [25; 27) '92': i32
80 [33; 34) '1': i32
81 [33; 38) '1 + y': i32
82 [37; 38) 'y': i32
83 "###
84 );
85}
86
87#[test]
88fn recursive_vars() {
89 covers!(type_var_cycles_resolve_completely);
90 covers!(type_var_cycles_resolve_as_possible);
91 assert_snapshot!(
92 infer(r#"
93fn test() {
94 let y = unknown;
95 [y, &y];
96}
97"#),
98 @r###"
99 [11; 48) '{ ...&y]; }': ()
100 [21; 22) 'y': &{unknown}
101 [25; 32) 'unknown': &{unknown}
102 [38; 45) '[y, &y]': [&&{unknown};_]
103 [39; 40) 'y': &{unknown}
104 [42; 44) '&y': &&{unknown}
105 [43; 44) 'y': &{unknown}
106 "###
107 );
108}
109
110#[test]
111fn recursive_vars_2() {
112 covers!(type_var_cycles_resolve_completely);
113 covers!(type_var_cycles_resolve_as_possible);
114 assert_snapshot!(
115 infer(r#"
116fn test() {
117 let x = unknown;
118 let y = unknown;
119 [(x, y), (&y, &x)];
120}
121"#),
122 @r###"
123 [11; 80) '{ ...x)]; }': ()
124 [21; 22) 'x': &&{unknown}
125 [25; 32) 'unknown': &&{unknown}
126 [42; 43) 'y': &&{unknown}
127 [46; 53) 'unknown': &&{unknown}
128 [59; 77) '[(x, y..., &x)]': [(&&&{unknown}, &&&{unknown});_]
129 [60; 66) '(x, y)': (&&&{unknown}, &&&{unknown})
130 [61; 62) 'x': &&{unknown}
131 [64; 65) 'y': &&{unknown}
132 [68; 76) '(&y, &x)': (&&&{unknown}, &&&{unknown})
133 [69; 71) '&y': &&&{unknown}
134 [70; 71) 'y': &&{unknown}
135 [73; 75) '&x': &&&{unknown}
136 [74; 75) 'x': &&{unknown}
137 "###
138 );
139}
140
141#[test]
142fn infer_std_crash_1() {
143 // caused stack overflow, taken from std
144 assert_snapshot!(
145 infer(r#"
146enum Maybe<T> {
147 Real(T),
148 Fake,
149}
150
151fn write() {
152 match something_unknown {
153 Maybe::Real(ref mut something) => (),
154 }
155}
156"#),
157 @r###"
158 [54; 139) '{ ... } }': ()
159 [60; 137) 'match ... }': ()
160 [66; 83) 'someth...nknown': Maybe<{unknown}>
161 [94; 124) 'Maybe:...thing)': Maybe<{unknown}>
162 [106; 123) 'ref mu...ething': &mut {unknown}
163 [128; 130) '()': ()
164 "###
165 );
166}
167
168#[test]
169fn infer_std_crash_2() {
170 covers!(type_var_resolves_to_int_var);
171 // caused "equating two type variables, ...", taken from std
172 assert_snapshot!(
173 infer(r#"
174fn test_line_buffer() {
175 &[0, b'\n', 1, b'\n'];
176}
177"#),
178 @r###"
179 [23; 53) '{ ...n']; }': ()
180 [29; 50) '&[0, b...b'\n']': &[u8;_]
181 [30; 50) '[0, b'...b'\n']': [u8;_]
182 [31; 32) '0': u8
183 [34; 39) 'b'\n'': u8
184 [41; 42) '1': u8
185 [44; 49) 'b'\n'': u8
186 "###
187 );
188}
189
190#[test]
191fn infer_std_crash_3() {
192 // taken from rustc
193 assert_snapshot!(
194 infer(r#"
195pub fn compute() {
196 match nope!() {
197 SizeSkeleton::Pointer { non_zero: true, tail } => {}
198 }
199}
200"#),
201 @r###"
202 [18; 108) '{ ... } }': ()
203 [24; 106) 'match ... }': ()
204 [30; 37) 'nope!()': {unknown}
205 [48; 94) 'SizeSk...tail }': {unknown}
206 [82; 86) 'true': {unknown}
207 [88; 92) 'tail': {unknown}
208 [98; 100) '{}': ()
209 "###
210 );
211}
212
213#[test]
214fn infer_std_crash_4() {
215 // taken from rustc
216 assert_snapshot!(
217 infer(r#"
218pub fn primitive_type() {
219 match *self {
220 BorrowedRef { type_: Primitive(p), ..} => {},
221 }
222}
223"#),
224 @r###"
225 [25; 106) '{ ... } }': ()
226 [31; 104) 'match ... }': ()
227 [37; 42) '*self': {unknown}
228 [38; 42) 'self': {unknown}
229 [53; 91) 'Borrow...), ..}': {unknown}
230 [74; 86) 'Primitive(p)': {unknown}
231 [84; 85) 'p': {unknown}
232 [95; 97) '{}': ()
233 "###
234 );
235}
236
237#[test]
238fn infer_std_crash_5() {
239 // taken from rustc
240 assert_snapshot!(
241 infer(r#"
242fn extra_compiler_flags() {
243 for content in doesnt_matter {
244 let name = if doesnt_matter {
245 first
246 } else {
247 &content
248 };
249
250 let content = if ICE_REPORT_COMPILER_FLAGS_STRIP_VALUE.contains(&name) {
251 name
252 } else {
253 content
254 };
255 }
256}
257"#),
258 @r###"
259 [27; 323) '{ ... } }': ()
260 [33; 321) 'for co... }': ()
261 [37; 44) 'content': &{unknown}
262 [48; 61) 'doesnt_matter': {unknown}
263 [62; 321) '{ ... }': ()
264 [76; 80) 'name': &&{unknown}
265 [83; 167) 'if doe... }': &&{unknown}
266 [86; 99) 'doesnt_matter': bool
267 [100; 129) '{ ... }': &&{unknown}
268 [114; 119) 'first': &&{unknown}
269 [135; 167) '{ ... }': &&{unknown}
270 [149; 157) '&content': &&{unknown}
271 [150; 157) 'content': &{unknown}
272 [182; 189) 'content': &{unknown}
273 [192; 314) 'if ICE... }': &{unknown}
274 [195; 232) 'ICE_RE..._VALUE': {unknown}
275 [195; 248) 'ICE_RE...&name)': bool
276 [242; 247) '&name': &&&{unknown}
277 [243; 247) 'name': &&{unknown}
278 [249; 277) '{ ... }': &&{unknown}
279 [263; 267) 'name': &&{unknown}
280 [283; 314) '{ ... }': &{unknown}
281 [297; 304) 'content': &{unknown}
282 "###
283 );
284}
285
286#[test]
287fn infer_nested_generics_crash() {
288 // another crash found typechecking rustc
289 assert_snapshot!(
290 infer(r#"
291struct Canonical<V> {
292 value: V,
293}
294struct QueryResponse<V> {
295 value: V,
296}
297fn test<R>(query_response: Canonical<QueryResponse<R>>) {
298 &query_response.value;
299}
300"#),
301 @r###"
302 [92; 106) 'query_response': Canonical<QueryResponse<R>>
303 [137; 167) '{ ...lue; }': ()
304 [143; 164) '&query....value': &QueryResponse<R>
305 [144; 158) 'query_response': Canonical<QueryResponse<R>>
306 [144; 164) 'query_....value': QueryResponse<R>
307 "###
308 );
309}
310
311#[test]
312fn bug_1030() {
313 assert_snapshot!(infer(r#"
314struct HashSet<T, H>;
315struct FxHasher;
316type FxHashSet<T> = HashSet<T, FxHasher>;
317
318impl<T, H> HashSet<T, H> {
319 fn default() -> HashSet<T, H> {}
320}
321
322pub fn main_loop() {
323 FxHashSet::default();
324}
325"#),
326 @r###"
327 [144; 146) '{}': ()
328 [169; 198) '{ ...t(); }': ()
329 [175; 193) 'FxHash...efault': fn default<{unknown}, FxHasher>() -> HashSet<T, H>
330 [175; 195) 'FxHash...ault()': HashSet<{unknown}, FxHasher>
331 "###
332 );
333}
diff --git a/crates/ra_hir_ty/src/tests/simple.rs b/crates/ra_hir_ty/src/tests/simple.rs
new file mode 100644
index 000000000..3e5e163e3
--- /dev/null
+++ b/crates/ra_hir_ty/src/tests/simple.rs
@@ -0,0 +1,1663 @@
1use super::{infer, type_at, type_at_pos};
2use crate::test_db::TestDB;
3use insta::assert_snapshot;
4use ra_db::fixture::WithFixture;
5
6#[test]
7fn infer_box() {
8 let (db, pos) = TestDB::with_position(
9 r#"
10//- /main.rs crate:main deps:std
11
12fn test() {
13 let x = box 1;
14 let t = (x, box x, box &1, box [1]);
15 t<|>;
16}
17
18//- /std.rs crate:std
19#[prelude_import] use prelude::*;
20mod prelude {}
21
22mod boxed {
23 pub struct Box<T: ?Sized> {
24 inner: *mut T,
25 }
26}
27
28"#,
29 );
30 assert_eq!("(Box<i32>, Box<Box<i32>>, Box<&i32>, Box<[i32;_]>)", type_at_pos(&db, pos));
31}
32
33#[test]
34fn infer_adt_self() {
35 let (db, pos) = TestDB::with_position(
36 r#"
37//- /main.rs
38enum Nat { Succ(Self), Demo(Nat), Zero }
39
40fn test() {
41 let foo: Nat = Nat::Zero;
42 if let Nat::Succ(x) = foo {
43 x<|>
44 }
45}
46
47"#,
48 );
49 assert_eq!("Nat", type_at_pos(&db, pos));
50}
51
52#[test]
53fn infer_ranges() {
54 let (db, pos) = TestDB::with_position(
55 r#"
56//- /main.rs crate:main deps:std
57fn test() {
58 let a = ..;
59 let b = 1..;
60 let c = ..2u32;
61 let d = 1..2usize;
62 let e = ..=10;
63 let f = 'a'..='z';
64
65 let t = (a, b, c, d, e, f);
66 t<|>;
67}
68
69//- /std.rs crate:std
70#[prelude_import] use prelude::*;
71mod prelude {}
72
73pub mod ops {
74 pub struct Range<Idx> {
75 pub start: Idx,
76 pub end: Idx,
77 }
78 pub struct RangeFrom<Idx> {
79 pub start: Idx,
80 }
81 struct RangeFull;
82 pub struct RangeInclusive<Idx> {
83 start: Idx,
84 end: Idx,
85 is_empty: u8,
86 }
87 pub struct RangeTo<Idx> {
88 pub end: Idx,
89 }
90 pub struct RangeToInclusive<Idx> {
91 pub end: Idx,
92 }
93}
94"#,
95 );
96 assert_eq!(
97 "(RangeFull, RangeFrom<i32>, RangeTo<u32>, Range<usize>, RangeToInclusive<i32>, RangeInclusive<char>)",
98 type_at_pos(&db, pos),
99 );
100}
101
102#[test]
103fn infer_while_let() {
104 let (db, pos) = TestDB::with_position(
105 r#"
106//- /main.rs
107enum Option<T> { Some(T), None }
108
109fn test() {
110 let foo: Option<f32> = None;
111 while let Option::Some(x) = foo {
112 <|>x
113 }
114}
115
116"#,
117 );
118 assert_eq!("f32", type_at_pos(&db, pos));
119}
120
121#[test]
122fn infer_basics() {
123 assert_snapshot!(
124 infer(r#"
125fn test(a: u32, b: isize, c: !, d: &str) {
126 a;
127 b;
128 c;
129 d;
130 1usize;
131 1isize;
132 "test";
133 1.0f32;
134}"#),
135 @r###"
136 [9; 10) 'a': u32
137 [17; 18) 'b': isize
138 [27; 28) 'c': !
139 [33; 34) 'd': &str
140 [42; 121) '{ ...f32; }': !
141 [48; 49) 'a': u32
142 [55; 56) 'b': isize
143 [62; 63) 'c': !
144 [69; 70) 'd': &str
145 [76; 82) '1usize': usize
146 [88; 94) '1isize': isize
147 [100; 106) '"test"': &str
148 [112; 118) '1.0f32': f32
149 "###
150 );
151}
152
153#[test]
154fn infer_let() {
155 assert_snapshot!(
156 infer(r#"
157fn test() {
158 let a = 1isize;
159 let b: usize = 1;
160 let c = b;
161 let d: u32;
162 let e;
163 let f: i32 = e;
164}
165"#),
166 @r###"
167 [11; 118) '{ ...= e; }': ()
168 [21; 22) 'a': isize
169 [25; 31) '1isize': isize
170 [41; 42) 'b': usize
171 [52; 53) '1': usize
172 [63; 64) 'c': usize
173 [67; 68) 'b': usize
174 [78; 79) 'd': u32
175 [94; 95) 'e': i32
176 [105; 106) 'f': i32
177 [114; 115) 'e': i32
178 "###
179 );
180}
181
182#[test]
183fn infer_paths() {
184 assert_snapshot!(
185 infer(r#"
186fn a() -> u32 { 1 }
187
188mod b {
189 fn c() -> u32 { 1 }
190}
191
192fn test() {
193 a();
194 b::c();
195}
196"#),
197 @r###"
198 [15; 20) '{ 1 }': u32
199 [17; 18) '1': u32
200 [48; 53) '{ 1 }': u32
201 [50; 51) '1': u32
202 [67; 91) '{ ...c(); }': ()
203 [73; 74) 'a': fn a() -> u32
204 [73; 76) 'a()': u32
205 [82; 86) 'b::c': fn c() -> u32
206 [82; 88) 'b::c()': u32
207 "###
208 );
209}
210
211#[test]
212fn infer_path_type() {
213 assert_snapshot!(
214 infer(r#"
215struct S;
216
217impl S {
218 fn foo() -> i32 { 1 }
219}
220
221fn test() {
222 S::foo();
223 <S>::foo();
224}
225"#),
226 @r###"
227 [41; 46) '{ 1 }': i32
228 [43; 44) '1': i32
229 [60; 93) '{ ...o(); }': ()
230 [66; 72) 'S::foo': fn foo() -> i32
231 [66; 74) 'S::foo()': i32
232 [80; 88) '<S>::foo': fn foo() -> i32
233 [80; 90) '<S>::foo()': i32
234 "###
235 );
236}
237
238#[test]
239fn infer_struct() {
240 assert_snapshot!(
241 infer(r#"
242struct A {
243 b: B,
244 c: C,
245}
246struct B;
247struct C(usize);
248
249fn test() {
250 let c = C(1);
251 B;
252 let a: A = A { b: B, c: C(1) };
253 a.b;
254 a.c;
255}
256"#),
257 @r###"
258 [72; 154) '{ ...a.c; }': ()
259 [82; 83) 'c': C
260 [86; 87) 'C': C(usize) -> C
261 [86; 90) 'C(1)': C
262 [88; 89) '1': usize
263 [96; 97) 'B': B
264 [107; 108) 'a': A
265 [114; 133) 'A { b:...C(1) }': A
266 [121; 122) 'B': B
267 [127; 128) 'C': C(usize) -> C
268 [127; 131) 'C(1)': C
269 [129; 130) '1': usize
270 [139; 140) 'a': A
271 [139; 142) 'a.b': B
272 [148; 149) 'a': A
273 [148; 151) 'a.c': C
274 "###
275 );
276}
277
278#[test]
279fn infer_enum() {
280 assert_snapshot!(
281 infer(r#"
282enum E {
283 V1 { field: u32 },
284 V2
285}
286fn test() {
287 E::V1 { field: 1 };
288 E::V2;
289}"#),
290 @r###"
291 [48; 82) '{ E:...:V2; }': ()
292 [52; 70) 'E::V1 ...d: 1 }': E
293 [67; 68) '1': u32
294 [74; 79) 'E::V2': E
295 "###
296 );
297}
298
299#[test]
300fn infer_refs() {
301 assert_snapshot!(
302 infer(r#"
303fn test(a: &u32, b: &mut u32, c: *const u32, d: *mut u32) {
304 a;
305 *a;
306 &a;
307 &mut a;
308 b;
309 *b;
310 &b;
311 c;
312 *c;
313 d;
314 *d;
315}
316"#),
317 @r###"
318 [9; 10) 'a': &u32
319 [18; 19) 'b': &mut u32
320 [31; 32) 'c': *const u32
321 [46; 47) 'd': *mut u32
322 [59; 150) '{ ... *d; }': ()
323 [65; 66) 'a': &u32
324 [72; 74) '*a': u32
325 [73; 74) 'a': &u32
326 [80; 82) '&a': &&u32
327 [81; 82) 'a': &u32
328 [88; 94) '&mut a': &mut &u32
329 [93; 94) 'a': &u32
330 [100; 101) 'b': &mut u32
331 [107; 109) '*b': u32
332 [108; 109) 'b': &mut u32
333 [115; 117) '&b': &&mut u32
334 [116; 117) 'b': &mut u32
335 [123; 124) 'c': *const u32
336 [130; 132) '*c': u32
337 [131; 132) 'c': *const u32
338 [138; 139) 'd': *mut u32
339 [145; 147) '*d': u32
340 [146; 147) 'd': *mut u32
341 "###
342 );
343}
344
345#[test]
346fn infer_literals() {
347 assert_snapshot!(
348 infer(r##"
349fn test() {
350 5i32;
351 5f32;
352 5f64;
353 "hello";
354 b"bytes";
355 'c';
356 b'b';
357 3.14;
358 5000;
359 false;
360 true;
361 r#"
362 //! doc
363 // non-doc
364 mod foo {}
365 "#;
366 br#"yolo"#;
367}
368"##),
369 @r###"
370 [11; 221) '{ ...o"#; }': ()
371 [17; 21) '5i32': i32
372 [27; 31) '5f32': f32
373 [37; 41) '5f64': f64
374 [47; 54) '"hello"': &str
375 [60; 68) 'b"bytes"': &[u8]
376 [74; 77) ''c'': char
377 [83; 87) 'b'b'': u8
378 [93; 97) '3.14': f64
379 [103; 107) '5000': i32
380 [113; 118) 'false': bool
381 [124; 128) 'true': bool
382 [134; 202) 'r#" ... "#': &str
383 [208; 218) 'br#"yolo"#': &[u8]
384 "###
385 );
386}
387
388#[test]
389fn infer_unary_op() {
390 assert_snapshot!(
391 infer(r#"
392enum SomeType {}
393
394fn test(x: SomeType) {
395 let b = false;
396 let c = !b;
397 let a = 100;
398 let d: i128 = -a;
399 let e = -100;
400 let f = !!!true;
401 let g = !42;
402 let h = !10u32;
403 let j = !a;
404 -3.14;
405 !3;
406 -x;
407 !x;
408 -"hello";
409 !"hello";
410}
411"#),
412 @r###"
413 [27; 28) 'x': SomeType
414 [40; 272) '{ ...lo"; }': ()
415 [50; 51) 'b': bool
416 [54; 59) 'false': bool
417 [69; 70) 'c': bool
418 [73; 75) '!b': bool
419 [74; 75) 'b': bool
420 [85; 86) 'a': i128
421 [89; 92) '100': i128
422 [102; 103) 'd': i128
423 [112; 114) '-a': i128
424 [113; 114) 'a': i128
425 [124; 125) 'e': i32
426 [128; 132) '-100': i32
427 [129; 132) '100': i32
428 [142; 143) 'f': bool
429 [146; 153) '!!!true': bool
430 [147; 153) '!!true': bool
431 [148; 153) '!true': bool
432 [149; 153) 'true': bool
433 [163; 164) 'g': i32
434 [167; 170) '!42': i32
435 [168; 170) '42': i32
436 [180; 181) 'h': u32
437 [184; 190) '!10u32': u32
438 [185; 190) '10u32': u32
439 [200; 201) 'j': i128
440 [204; 206) '!a': i128
441 [205; 206) 'a': i128
442 [212; 217) '-3.14': f64
443 [213; 217) '3.14': f64
444 [223; 225) '!3': i32
445 [224; 225) '3': i32
446 [231; 233) '-x': {unknown}
447 [232; 233) 'x': SomeType
448 [239; 241) '!x': {unknown}
449 [240; 241) 'x': SomeType
450 [247; 255) '-"hello"': {unknown}
451 [248; 255) '"hello"': &str
452 [261; 269) '!"hello"': {unknown}
453 [262; 269) '"hello"': &str
454 "###
455 );
456}
457
458#[test]
459fn infer_backwards() {
460 assert_snapshot!(
461 infer(r#"
462fn takes_u32(x: u32) {}
463
464struct S { i32_field: i32 }
465
466fn test() -> &mut &f64 {
467 let a = unknown_function();
468 takes_u32(a);
469 let b = unknown_function();
470 S { i32_field: b };
471 let c = unknown_function();
472 &mut &c
473}
474"#),
475 @r###"
476 [14; 15) 'x': u32
477 [22; 24) '{}': ()
478 [78; 231) '{ ...t &c }': &mut &f64
479 [88; 89) 'a': u32
480 [92; 108) 'unknow...nction': {unknown}
481 [92; 110) 'unknow...tion()': u32
482 [116; 125) 'takes_u32': fn takes_u32(u32) -> ()
483 [116; 128) 'takes_u32(a)': ()
484 [126; 127) 'a': u32
485 [138; 139) 'b': i32
486 [142; 158) 'unknow...nction': {unknown}
487 [142; 160) 'unknow...tion()': i32
488 [166; 184) 'S { i3...d: b }': S
489 [181; 182) 'b': i32
490 [194; 195) 'c': f64
491 [198; 214) 'unknow...nction': {unknown}
492 [198; 216) 'unknow...tion()': f64
493 [222; 229) '&mut &c': &mut &f64
494 [227; 229) '&c': &f64
495 [228; 229) 'c': f64
496 "###
497 );
498}
499
500#[test]
501fn infer_self() {
502 assert_snapshot!(
503 infer(r#"
504struct S;
505
506impl S {
507 fn test(&self) {
508 self;
509 }
510 fn test2(self: &Self) {
511 self;
512 }
513 fn test3() -> Self {
514 S {}
515 }
516 fn test4() -> Self {
517 Self {}
518 }
519}
520"#),
521 @r###"
522 [34; 38) 'self': &S
523 [40; 61) '{ ... }': ()
524 [50; 54) 'self': &S
525 [75; 79) 'self': &S
526 [88; 109) '{ ... }': ()
527 [98; 102) 'self': &S
528 [133; 153) '{ ... }': S
529 [143; 147) 'S {}': S
530 [177; 200) '{ ... }': S
531 [187; 194) 'Self {}': S
532 "###
533 );
534}
535
536#[test]
537fn infer_binary_op() {
538 assert_snapshot!(
539 infer(r#"
540fn f(x: bool) -> i32 {
541 0i32
542}
543
544fn test() -> bool {
545 let x = a && b;
546 let y = true || false;
547 let z = x == y;
548 let t = x != y;
549 let minus_forty: isize = -40isize;
550 let h = minus_forty <= CONST_2;
551 let c = f(z || y) + 5;
552 let d = b;
553 let g = minus_forty ^= i;
554 let ten: usize = 10;
555 let ten_is_eleven = ten == some_num;
556
557 ten < 3
558}
559"#),
560 @r###"
561 [6; 7) 'x': bool
562 [22; 34) '{ 0i32 }': i32
563 [28; 32) '0i32': i32
564 [54; 370) '{ ... < 3 }': bool
565 [64; 65) 'x': bool
566 [68; 69) 'a': bool
567 [68; 74) 'a && b': bool
568 [73; 74) 'b': bool
569 [84; 85) 'y': bool
570 [88; 92) 'true': bool
571 [88; 101) 'true || false': bool
572 [96; 101) 'false': bool
573 [111; 112) 'z': bool
574 [115; 116) 'x': bool
575 [115; 121) 'x == y': bool
576 [120; 121) 'y': bool
577 [131; 132) 't': bool
578 [135; 136) 'x': bool
579 [135; 141) 'x != y': bool
580 [140; 141) 'y': bool
581 [151; 162) 'minus_forty': isize
582 [172; 180) '-40isize': isize
583 [173; 180) '40isize': isize
584 [190; 191) 'h': bool
585 [194; 205) 'minus_forty': isize
586 [194; 216) 'minus_...ONST_2': bool
587 [209; 216) 'CONST_2': isize
588 [226; 227) 'c': i32
589 [230; 231) 'f': fn f(bool) -> i32
590 [230; 239) 'f(z || y)': i32
591 [230; 243) 'f(z || y) + 5': i32
592 [232; 233) 'z': bool
593 [232; 238) 'z || y': bool
594 [237; 238) 'y': bool
595 [242; 243) '5': i32
596 [253; 254) 'd': {unknown}
597 [257; 258) 'b': {unknown}
598 [268; 269) 'g': ()
599 [272; 283) 'minus_forty': isize
600 [272; 288) 'minus_...y ^= i': ()
601 [287; 288) 'i': isize
602 [298; 301) 'ten': usize
603 [311; 313) '10': usize
604 [323; 336) 'ten_is_eleven': bool
605 [339; 342) 'ten': usize
606 [339; 354) 'ten == some_num': bool
607 [346; 354) 'some_num': usize
608 [361; 364) 'ten': usize
609 [361; 368) 'ten < 3': bool
610 [367; 368) '3': usize
611 "###
612 );
613}
614
615#[test]
616fn infer_field_autoderef() {
617 assert_snapshot!(
618 infer(r#"
619struct A {
620 b: B,
621}
622struct B;
623
624fn test1(a: A) {
625 let a1 = a;
626 a1.b;
627 let a2 = &a;
628 a2.b;
629 let a3 = &mut a;
630 a3.b;
631 let a4 = &&&&&&&a;
632 a4.b;
633 let a5 = &mut &&mut &&mut a;
634 a5.b;
635}
636
637fn test2(a1: *const A, a2: *mut A) {
638 a1.b;
639 a2.b;
640}
641"#),
642 @r###"
643 [44; 45) 'a': A
644 [50; 213) '{ ...5.b; }': ()
645 [60; 62) 'a1': A
646 [65; 66) 'a': A
647 [72; 74) 'a1': A
648 [72; 76) 'a1.b': B
649 [86; 88) 'a2': &A
650 [91; 93) '&a': &A
651 [92; 93) 'a': A
652 [99; 101) 'a2': &A
653 [99; 103) 'a2.b': B
654 [113; 115) 'a3': &mut A
655 [118; 124) '&mut a': &mut A
656 [123; 124) 'a': A
657 [130; 132) 'a3': &mut A
658 [130; 134) 'a3.b': B
659 [144; 146) 'a4': &&&&&&&A
660 [149; 157) '&&&&&&&a': &&&&&&&A
661 [150; 157) '&&&&&&a': &&&&&&A
662 [151; 157) '&&&&&a': &&&&&A
663 [152; 157) '&&&&a': &&&&A
664 [153; 157) '&&&a': &&&A
665 [154; 157) '&&a': &&A
666 [155; 157) '&a': &A
667 [156; 157) 'a': A
668 [163; 165) 'a4': &&&&&&&A
669 [163; 167) 'a4.b': B
670 [177; 179) 'a5': &mut &&mut &&mut A
671 [182; 200) '&mut &...&mut a': &mut &&mut &&mut A
672 [187; 200) '&&mut &&mut a': &&mut &&mut A
673 [188; 200) '&mut &&mut a': &mut &&mut A
674 [193; 200) '&&mut a': &&mut A
675 [194; 200) '&mut a': &mut A
676 [199; 200) 'a': A
677 [206; 208) 'a5': &mut &&mut &&mut A
678 [206; 210) 'a5.b': B
679 [224; 226) 'a1': *const A
680 [238; 240) 'a2': *mut A
681 [250; 273) '{ ...2.b; }': ()
682 [256; 258) 'a1': *const A
683 [256; 260) 'a1.b': B
684 [266; 268) 'a2': *mut A
685 [266; 270) 'a2.b': B
686 "###
687 );
688}
689
690#[test]
691fn infer_argument_autoderef() {
692 assert_snapshot!(
693 infer(r#"
694#[lang = "deref"]
695pub trait Deref {
696 type Target;
697 fn deref(&self) -> &Self::Target;
698}
699
700struct A<T>(T);
701
702impl<T> A<T> {
703 fn foo(&self) -> &T {
704 &self.0
705 }
706}
707
708struct B<T>(T);
709
710impl<T> Deref for B<T> {
711 type Target = T;
712 fn deref(&self) -> &Self::Target {
713 &self.0
714 }
715}
716
717fn test() {
718 let t = A::foo(&&B(B(A(42))));
719}
720"#),
721 @r###"
722 [68; 72) 'self': &Self
723 [139; 143) 'self': &A<T>
724 [151; 174) '{ ... }': &T
725 [161; 168) '&self.0': &T
726 [162; 166) 'self': &A<T>
727 [162; 168) 'self.0': T
728 [255; 259) 'self': &B<T>
729 [278; 301) '{ ... }': &T
730 [288; 295) '&self.0': &T
731 [289; 293) 'self': &B<T>
732 [289; 295) 'self.0': T
733 [315; 353) '{ ...))); }': ()
734 [325; 326) 't': &i32
735 [329; 335) 'A::foo': fn foo<i32>(&A<T>) -> &T
736 [329; 350) 'A::foo...42))))': &i32
737 [336; 349) '&&B(B(A(42)))': &&B<B<A<i32>>>
738 [337; 349) '&B(B(A(42)))': &B<B<A<i32>>>
739 [338; 339) 'B': B<B<A<i32>>>(T) -> B<T>
740 [338; 349) 'B(B(A(42)))': B<B<A<i32>>>
741 [340; 341) 'B': B<A<i32>>(T) -> B<T>
742 [340; 348) 'B(A(42))': B<A<i32>>
743 [342; 343) 'A': A<i32>(T) -> A<T>
744 [342; 347) 'A(42)': A<i32>
745 [344; 346) '42': i32
746 "###
747 );
748}
749
750#[test]
751fn infer_method_argument_autoderef() {
752 assert_snapshot!(
753 infer(r#"
754#[lang = "deref"]
755pub trait Deref {
756 type Target;
757 fn deref(&self) -> &Self::Target;
758}
759
760struct A<T>(*mut T);
761
762impl<T> A<T> {
763 fn foo(&self, x: &A<T>) -> &T {
764 &*x.0
765 }
766}
767
768struct B<T>(T);
769
770impl<T> Deref for B<T> {
771 type Target = T;
772 fn deref(&self) -> &Self::Target {
773 &self.0
774 }
775}
776
777fn test(a: A<i32>) {
778 let t = A(0 as *mut _).foo(&&B(B(a)));
779}
780"#),
781 @r###"
782 [68; 72) 'self': &Self
783 [144; 148) 'self': &A<T>
784 [150; 151) 'x': &A<T>
785 [166; 187) '{ ... }': &T
786 [176; 181) '&*x.0': &T
787 [177; 181) '*x.0': T
788 [178; 179) 'x': &A<T>
789 [178; 181) 'x.0': *mut T
790 [268; 272) 'self': &B<T>
791 [291; 314) '{ ... }': &T
792 [301; 308) '&self.0': &T
793 [302; 306) 'self': &B<T>
794 [302; 308) 'self.0': T
795 [326; 327) 'a': A<i32>
796 [337; 383) '{ ...))); }': ()
797 [347; 348) 't': &i32
798 [351; 352) 'A': A<i32>(*mut T) -> A<T>
799 [351; 365) 'A(0 as *mut _)': A<i32>
800 [351; 380) 'A(0 as...B(a)))': &i32
801 [353; 354) '0': i32
802 [353; 364) '0 as *mut _': *mut i32
803 [370; 379) '&&B(B(a))': &&B<B<A<i32>>>
804 [371; 379) '&B(B(a))': &B<B<A<i32>>>
805 [372; 373) 'B': B<B<A<i32>>>(T) -> B<T>
806 [372; 379) 'B(B(a))': B<B<A<i32>>>
807 [374; 375) 'B': B<A<i32>>(T) -> B<T>
808 [374; 378) 'B(a)': B<A<i32>>
809 [376; 377) 'a': A<i32>
810 "###
811 );
812}
813
814#[test]
815fn infer_in_elseif() {
816 assert_snapshot!(
817 infer(r#"
818struct Foo { field: i32 }
819fn main(foo: Foo) {
820 if true {
821
822 } else if false {
823 foo.field
824 }
825}
826"#),
827 @r###"
828 [35; 38) 'foo': Foo
829 [45; 109) '{ ... } }': ()
830 [51; 107) 'if tru... }': ()
831 [54; 58) 'true': bool
832 [59; 67) '{ }': ()
833 [73; 107) 'if fal... }': ()
834 [76; 81) 'false': bool
835 [82; 107) '{ ... }': i32
836 [92; 95) 'foo': Foo
837 [92; 101) 'foo.field': i32
838 "###
839 )
840}
841
842#[test]
843fn infer_if_match_with_return() {
844 assert_snapshot!(
845 infer(r#"
846fn foo() {
847 let _x1 = if true {
848 1
849 } else {
850 return;
851 };
852 let _x2 = if true {
853 2
854 } else {
855 return
856 };
857 let _x3 = match true {
858 true => 3,
859 _ => {
860 return;
861 }
862 };
863 let _x4 = match true {
864 true => 4,
865 _ => return
866 };
867}"#),
868 @r###"
869 [10; 323) '{ ... }; }': ()
870 [20; 23) '_x1': i32
871 [26; 80) 'if tru... }': i32
872 [29; 33) 'true': bool
873 [34; 51) '{ ... }': i32
874 [44; 45) '1': i32
875 [57; 80) '{ ... }': !
876 [67; 73) 'return': !
877 [90; 93) '_x2': i32
878 [96; 149) 'if tru... }': i32
879 [99; 103) 'true': bool
880 [104; 121) '{ ... }': i32
881 [114; 115) '2': i32
882 [127; 149) '{ ... }': !
883 [137; 143) 'return': !
884 [159; 162) '_x3': i32
885 [165; 247) 'match ... }': i32
886 [171; 175) 'true': bool
887 [186; 190) 'true': bool
888 [194; 195) '3': i32
889 [205; 206) '_': bool
890 [210; 241) '{ ... }': !
891 [224; 230) 'return': !
892 [257; 260) '_x4': i32
893 [263; 320) 'match ... }': i32
894 [269; 273) 'true': bool
895 [284; 288) 'true': bool
896 [292; 293) '4': i32
897 [303; 304) '_': bool
898 [308; 314) 'return': !
899 "###
900 )
901}
902
903#[test]
904fn infer_inherent_method() {
905 assert_snapshot!(
906 infer(r#"
907struct A;
908
909impl A {
910 fn foo(self, x: u32) -> i32 {}
911}
912
913mod b {
914 impl super::A {
915 fn bar(&self, x: u64) -> i64 {}
916 }
917}
918
919fn test(a: A) {
920 a.foo(1);
921 (&a).bar(1);
922 a.bar(1);
923}
924"#),
925 @r###"
926 [32; 36) 'self': A
927 [38; 39) 'x': u32
928 [53; 55) '{}': ()
929 [103; 107) 'self': &A
930 [109; 110) 'x': u64
931 [124; 126) '{}': ()
932 [144; 145) 'a': A
933 [150; 198) '{ ...(1); }': ()
934 [156; 157) 'a': A
935 [156; 164) 'a.foo(1)': i32
936 [162; 163) '1': u32
937 [170; 181) '(&a).bar(1)': i64
938 [171; 173) '&a': &A
939 [172; 173) 'a': A
940 [179; 180) '1': u64
941 [187; 188) 'a': A
942 [187; 195) 'a.bar(1)': i64
943 [193; 194) '1': u64
944 "###
945 );
946}
947
948#[test]
949fn infer_inherent_method_str() {
950 assert_snapshot!(
951 infer(r#"
952#[lang = "str"]
953impl str {
954 fn foo(&self) -> i32 {}
955}
956
957fn test() {
958 "foo".foo();
959}
960"#),
961 @r###"
962 [40; 44) 'self': &str
963 [53; 55) '{}': ()
964 [69; 89) '{ ...o(); }': ()
965 [75; 80) '"foo"': &str
966 [75; 86) '"foo".foo()': i32
967 "###
968 );
969}
970
971#[test]
972fn infer_tuple() {
973 assert_snapshot!(
974 infer(r#"
975fn test(x: &str, y: isize) {
976 let a: (u32, &str) = (1, "a");
977 let b = (a, x);
978 let c = (y, x);
979 let d = (c, x);
980 let e = (1, "e");
981 let f = (e, "d");
982}
983"#),
984 @r###"
985 [9; 10) 'x': &str
986 [18; 19) 'y': isize
987 [28; 170) '{ ...d"); }': ()
988 [38; 39) 'a': (u32, &str)
989 [55; 63) '(1, "a")': (u32, &str)
990 [56; 57) '1': u32
991 [59; 62) '"a"': &str
992 [73; 74) 'b': ((u32, &str), &str)
993 [77; 83) '(a, x)': ((u32, &str), &str)
994 [78; 79) 'a': (u32, &str)
995 [81; 82) 'x': &str
996 [93; 94) 'c': (isize, &str)
997 [97; 103) '(y, x)': (isize, &str)
998 [98; 99) 'y': isize
999 [101; 102) 'x': &str
1000 [113; 114) 'd': ((isize, &str), &str)
1001 [117; 123) '(c, x)': ((isize, &str), &str)
1002 [118; 119) 'c': (isize, &str)
1003 [121; 122) 'x': &str
1004 [133; 134) 'e': (i32, &str)
1005 [137; 145) '(1, "e")': (i32, &str)
1006 [138; 139) '1': i32
1007 [141; 144) '"e"': &str
1008 [155; 156) 'f': ((i32, &str), &str)
1009 [159; 167) '(e, "d")': ((i32, &str), &str)
1010 [160; 161) 'e': (i32, &str)
1011 [163; 166) '"d"': &str
1012 "###
1013 );
1014}
1015
1016#[test]
1017fn infer_array() {
1018 assert_snapshot!(
1019 infer(r#"
1020fn test(x: &str, y: isize) {
1021 let a = [x];
1022 let b = [a, a];
1023 let c = [b, b];
1024
1025 let d = [y, 1, 2, 3];
1026 let d = [1, y, 2, 3];
1027 let e = [y];
1028 let f = [d, d];
1029 let g = [e, e];
1030
1031 let h = [1, 2];
1032 let i = ["a", "b"];
1033
1034 let b = [a, ["b"]];
1035 let x: [u8; 0] = [];
1036}
1037"#),
1038 @r###"
1039 [9; 10) 'x': &str
1040 [18; 19) 'y': isize
1041 [28; 293) '{ ... []; }': ()
1042 [38; 39) 'a': [&str;_]
1043 [42; 45) '[x]': [&str;_]
1044 [43; 44) 'x': &str
1045 [55; 56) 'b': [[&str;_];_]
1046 [59; 65) '[a, a]': [[&str;_];_]
1047 [60; 61) 'a': [&str;_]
1048 [63; 64) 'a': [&str;_]
1049 [75; 76) 'c': [[[&str;_];_];_]
1050 [79; 85) '[b, b]': [[[&str;_];_];_]
1051 [80; 81) 'b': [[&str;_];_]
1052 [83; 84) 'b': [[&str;_];_]
1053 [96; 97) 'd': [isize;_]
1054 [100; 112) '[y, 1, 2, 3]': [isize;_]
1055 [101; 102) 'y': isize
1056 [104; 105) '1': isize
1057 [107; 108) '2': isize
1058 [110; 111) '3': isize
1059 [122; 123) 'd': [isize;_]
1060 [126; 138) '[1, y, 2, 3]': [isize;_]
1061 [127; 128) '1': isize
1062 [130; 131) 'y': isize
1063 [133; 134) '2': isize
1064 [136; 137) '3': isize
1065 [148; 149) 'e': [isize;_]
1066 [152; 155) '[y]': [isize;_]
1067 [153; 154) 'y': isize
1068 [165; 166) 'f': [[isize;_];_]
1069 [169; 175) '[d, d]': [[isize;_];_]
1070 [170; 171) 'd': [isize;_]
1071 [173; 174) 'd': [isize;_]
1072 [185; 186) 'g': [[isize;_];_]
1073 [189; 195) '[e, e]': [[isize;_];_]
1074 [190; 191) 'e': [isize;_]
1075 [193; 194) 'e': [isize;_]
1076 [206; 207) 'h': [i32;_]
1077 [210; 216) '[1, 2]': [i32;_]
1078 [211; 212) '1': i32
1079 [214; 215) '2': i32
1080 [226; 227) 'i': [&str;_]
1081 [230; 240) '["a", "b"]': [&str;_]
1082 [231; 234) '"a"': &str
1083 [236; 239) '"b"': &str
1084 [251; 252) 'b': [[&str;_];_]
1085 [255; 265) '[a, ["b"]]': [[&str;_];_]
1086 [256; 257) 'a': [&str;_]
1087 [259; 264) '["b"]': [&str;_]
1088 [260; 263) '"b"': &str
1089 [275; 276) 'x': [u8;_]
1090 [288; 290) '[]': [u8;_]
1091 "###
1092 );
1093}
1094
1095#[test]
1096fn infer_struct_generics() {
1097 assert_snapshot!(
1098 infer(r#"
1099struct A<T> {
1100 x: T,
1101}
1102
1103fn test(a1: A<u32>, i: i32) {
1104 a1.x;
1105 let a2 = A { x: i };
1106 a2.x;
1107 let a3 = A::<i128> { x: 1 };
1108 a3.x;
1109}
1110"#),
1111 @r###"
1112 [36; 38) 'a1': A<u32>
1113 [48; 49) 'i': i32
1114 [56; 147) '{ ...3.x; }': ()
1115 [62; 64) 'a1': A<u32>
1116 [62; 66) 'a1.x': u32
1117 [76; 78) 'a2': A<i32>
1118 [81; 91) 'A { x: i }': A<i32>
1119 [88; 89) 'i': i32
1120 [97; 99) 'a2': A<i32>
1121 [97; 101) 'a2.x': i32
1122 [111; 113) 'a3': A<i128>
1123 [116; 134) 'A::<i1...x: 1 }': A<i128>
1124 [131; 132) '1': i128
1125 [140; 142) 'a3': A<i128>
1126 [140; 144) 'a3.x': i128
1127 "###
1128 );
1129}
1130
1131#[test]
1132fn infer_tuple_struct_generics() {
1133 assert_snapshot!(
1134 infer(r#"
1135struct A<T>(T);
1136enum Option<T> { Some(T), None }
1137use Option::*;
1138
1139fn test() {
1140 A(42);
1141 A(42u128);
1142 Some("x");
1143 Option::Some("x");
1144 None;
1145 let x: Option<i64> = None;
1146}
1147"#),
1148 @r###"
1149 [76; 184) '{ ...one; }': ()
1150 [82; 83) 'A': A<i32>(T) -> A<T>
1151 [82; 87) 'A(42)': A<i32>
1152 [84; 86) '42': i32
1153 [93; 94) 'A': A<u128>(T) -> A<T>
1154 [93; 102) 'A(42u128)': A<u128>
1155 [95; 101) '42u128': u128
1156 [108; 112) 'Some': Some<&str>(T) -> Option<T>
1157 [108; 117) 'Some("x")': Option<&str>
1158 [113; 116) '"x"': &str
1159 [123; 135) 'Option::Some': Some<&str>(T) -> Option<T>
1160 [123; 140) 'Option...e("x")': Option<&str>
1161 [136; 139) '"x"': &str
1162 [146; 150) 'None': Option<{unknown}>
1163 [160; 161) 'x': Option<i64>
1164 [177; 181) 'None': Option<i64>
1165 "###
1166 );
1167}
1168
1169#[test]
1170fn infer_function_generics() {
1171 assert_snapshot!(
1172 infer(r#"
1173fn id<T>(t: T) -> T { t }
1174
1175fn test() {
1176 id(1u32);
1177 id::<i128>(1);
1178 let x: u64 = id(1);
1179}
1180"#),
1181 @r###"
1182 [10; 11) 't': T
1183 [21; 26) '{ t }': T
1184 [23; 24) 't': T
1185 [38; 98) '{ ...(1); }': ()
1186 [44; 46) 'id': fn id<u32>(T) -> T
1187 [44; 52) 'id(1u32)': u32
1188 [47; 51) '1u32': u32
1189 [58; 68) 'id::<i128>': fn id<i128>(T) -> T
1190 [58; 71) 'id::<i128>(1)': i128
1191 [69; 70) '1': i128
1192 [81; 82) 'x': u64
1193 [90; 92) 'id': fn id<u64>(T) -> T
1194 [90; 95) 'id(1)': u64
1195 [93; 94) '1': u64
1196 "###
1197 );
1198}
1199
1200#[test]
1201fn infer_impl_generics() {
1202 assert_snapshot!(
1203 infer(r#"
1204struct A<T1, T2> {
1205 x: T1,
1206 y: T2,
1207}
1208impl<Y, X> A<X, Y> {
1209 fn x(self) -> X {
1210 self.x
1211 }
1212 fn y(self) -> Y {
1213 self.y
1214 }
1215 fn z<T>(self, t: T) -> (X, Y, T) {
1216 (self.x, self.y, t)
1217 }
1218}
1219
1220fn test() -> i128 {
1221 let a = A { x: 1u64, y: 1i64 };
1222 a.x();
1223 a.y();
1224 a.z(1i128);
1225 a.z::<u128>(1);
1226}
1227"#),
1228 @r###"
1229 [74; 78) 'self': A<X, Y>
1230 [85; 107) '{ ... }': X
1231 [95; 99) 'self': A<X, Y>
1232 [95; 101) 'self.x': X
1233 [117; 121) 'self': A<X, Y>
1234 [128; 150) '{ ... }': Y
1235 [138; 142) 'self': A<X, Y>
1236 [138; 144) 'self.y': Y
1237 [163; 167) 'self': A<X, Y>
1238 [169; 170) 't': T
1239 [188; 223) '{ ... }': (X, Y, T)
1240 [198; 217) '(self.....y, t)': (X, Y, T)
1241 [199; 203) 'self': A<X, Y>
1242 [199; 205) 'self.x': X
1243 [207; 211) 'self': A<X, Y>
1244 [207; 213) 'self.y': Y
1245 [215; 216) 't': T
1246 [245; 342) '{ ...(1); }': ()
1247 [255; 256) 'a': A<u64, i64>
1248 [259; 281) 'A { x:...1i64 }': A<u64, i64>
1249 [266; 270) '1u64': u64
1250 [275; 279) '1i64': i64
1251 [287; 288) 'a': A<u64, i64>
1252 [287; 292) 'a.x()': u64
1253 [298; 299) 'a': A<u64, i64>
1254 [298; 303) 'a.y()': i64
1255 [309; 310) 'a': A<u64, i64>
1256 [309; 319) 'a.z(1i128)': (u64, i64, i128)
1257 [313; 318) '1i128': i128
1258 [325; 326) 'a': A<u64, i64>
1259 [325; 339) 'a.z::<u128>(1)': (u64, i64, u128)
1260 [337; 338) '1': u128
1261 "###
1262 );
1263}
1264
1265#[test]
1266fn infer_impl_generics_with_autoderef() {
1267 assert_snapshot!(
1268 infer(r#"
1269enum Option<T> {
1270 Some(T),
1271 None,
1272}
1273impl<T> Option<T> {
1274 fn as_ref(&self) -> Option<&T> {}
1275}
1276fn test(o: Option<u32>) {
1277 (&o).as_ref();
1278 o.as_ref();
1279}
1280"#),
1281 @r###"
1282 [78; 82) 'self': &Option<T>
1283 [98; 100) '{}': ()
1284 [111; 112) 'o': Option<u32>
1285 [127; 165) '{ ...f(); }': ()
1286 [133; 146) '(&o).as_ref()': Option<&u32>
1287 [134; 136) '&o': &Option<u32>
1288 [135; 136) 'o': Option<u32>
1289 [152; 153) 'o': Option<u32>
1290 [152; 162) 'o.as_ref()': Option<&u32>
1291 "###
1292 );
1293}
1294
1295#[test]
1296fn infer_generic_chain() {
1297 assert_snapshot!(
1298 infer(r#"
1299struct A<T> {
1300 x: T,
1301}
1302impl<T2> A<T2> {
1303 fn x(self) -> T2 {
1304 self.x
1305 }
1306}
1307fn id<T>(t: T) -> T { t }
1308
1309fn test() -> i128 {
1310 let x = 1;
1311 let y = id(x);
1312 let a = A { x: id(y) };
1313 let z = id(a.x);
1314 let b = A { x: z };
1315 b.x()
1316}
1317"#),
1318 @r###"
1319 [53; 57) 'self': A<T2>
1320 [65; 87) '{ ... }': T2
1321 [75; 79) 'self': A<T2>
1322 [75; 81) 'self.x': T2
1323 [99; 100) 't': T
1324 [110; 115) '{ t }': T
1325 [112; 113) 't': T
1326 [135; 261) '{ ....x() }': i128
1327 [146; 147) 'x': i128
1328 [150; 151) '1': i128
1329 [162; 163) 'y': i128
1330 [166; 168) 'id': fn id<i128>(T) -> T
1331 [166; 171) 'id(x)': i128
1332 [169; 170) 'x': i128
1333 [182; 183) 'a': A<i128>
1334 [186; 200) 'A { x: id(y) }': A<i128>
1335 [193; 195) 'id': fn id<i128>(T) -> T
1336 [193; 198) 'id(y)': i128
1337 [196; 197) 'y': i128
1338 [211; 212) 'z': i128
1339 [215; 217) 'id': fn id<i128>(T) -> T
1340 [215; 222) 'id(a.x)': i128
1341 [218; 219) 'a': A<i128>
1342 [218; 221) 'a.x': i128
1343 [233; 234) 'b': A<i128>
1344 [237; 247) 'A { x: z }': A<i128>
1345 [244; 245) 'z': i128
1346 [254; 255) 'b': A<i128>
1347 [254; 259) 'b.x()': i128
1348 "###
1349 );
1350}
1351
1352#[test]
1353fn infer_associated_const() {
1354 assert_snapshot!(
1355 infer(r#"
1356struct Struct;
1357
1358impl Struct {
1359 const FOO: u32 = 1;
1360}
1361
1362enum Enum {}
1363
1364impl Enum {
1365 const BAR: u32 = 2;
1366}
1367
1368trait Trait {
1369 const ID: u32;
1370}
1371
1372struct TraitTest;
1373
1374impl Trait for TraitTest {
1375 const ID: u32 = 5;
1376}
1377
1378fn test() {
1379 let x = Struct::FOO;
1380 let y = Enum::BAR;
1381 let z = TraitTest::ID;
1382}
1383"#),
1384 @r###"
1385 [52; 53) '1': u32
1386 [105; 106) '2': u32
1387 [213; 214) '5': u32
1388 [229; 307) '{ ...:ID; }': ()
1389 [239; 240) 'x': u32
1390 [243; 254) 'Struct::FOO': u32
1391 [264; 265) 'y': u32
1392 [268; 277) 'Enum::BAR': u32
1393 [287; 288) 'z': u32
1394 [291; 304) 'TraitTest::ID': u32
1395 "###
1396 );
1397}
1398
1399#[test]
1400fn infer_type_alias() {
1401 assert_snapshot!(
1402 infer(r#"
1403struct A<X, Y> { x: X, y: Y }
1404type Foo = A<u32, i128>;
1405type Bar<T> = A<T, u128>;
1406type Baz<U, V> = A<V, U>;
1407fn test(x: Foo, y: Bar<&str>, z: Baz<i8, u8>) {
1408 x.x;
1409 x.y;
1410 y.x;
1411 y.y;
1412 z.x;
1413 z.y;
1414}
1415"#),
1416 @r###"
1417 [116; 117) 'x': A<u32, i128>
1418 [124; 125) 'y': A<&str, u128>
1419 [138; 139) 'z': A<u8, i8>
1420 [154; 211) '{ ...z.y; }': ()
1421 [160; 161) 'x': A<u32, i128>
1422 [160; 163) 'x.x': u32
1423 [169; 170) 'x': A<u32, i128>
1424 [169; 172) 'x.y': i128
1425 [178; 179) 'y': A<&str, u128>
1426 [178; 181) 'y.x': &str
1427 [187; 188) 'y': A<&str, u128>
1428 [187; 190) 'y.y': u128
1429 [196; 197) 'z': A<u8, i8>
1430 [196; 199) 'z.x': u8
1431 [205; 206) 'z': A<u8, i8>
1432 [205; 208) 'z.y': i8
1433 "###
1434 )
1435}
1436
1437#[test]
1438fn recursive_type_alias() {
1439 assert_snapshot!(
1440 infer(r#"
1441struct A<X> {}
1442type Foo = Foo;
1443type Bar = A<Bar>;
1444fn test(x: Foo) {}
1445"#),
1446 @r###"
1447 [59; 60) 'x': {unknown}
1448 [67; 69) '{}': ()
1449 "###
1450 )
1451}
1452
1453#[test]
1454fn infer_type_param() {
1455 assert_snapshot!(
1456 infer(r#"
1457fn id<T>(x: T) -> T {
1458 x
1459}
1460
1461fn clone<T>(x: &T) -> T {
1462 *x
1463}
1464
1465fn test() {
1466 let y = 10u32;
1467 id(y);
1468 let x: bool = clone(z);
1469 id::<i128>(1);
1470}
1471"#),
1472 @r###"
1473 [10; 11) 'x': T
1474 [21; 30) '{ x }': T
1475 [27; 28) 'x': T
1476 [44; 45) 'x': &T
1477 [56; 66) '{ *x }': T
1478 [62; 64) '*x': T
1479 [63; 64) 'x': &T
1480 [78; 158) '{ ...(1); }': ()
1481 [88; 89) 'y': u32
1482 [92; 97) '10u32': u32
1483 [103; 105) 'id': fn id<u32>(T) -> T
1484 [103; 108) 'id(y)': u32
1485 [106; 107) 'y': u32
1486 [118; 119) 'x': bool
1487 [128; 133) 'clone': fn clone<bool>(&T) -> T
1488 [128; 136) 'clone(z)': bool
1489 [134; 135) 'z': &bool
1490 [142; 152) 'id::<i128>': fn id<i128>(T) -> T
1491 [142; 155) 'id::<i128>(1)': i128
1492 [153; 154) '1': i128
1493 "###
1494 );
1495}
1496
1497#[test]
1498fn infer_const() {
1499 assert_snapshot!(
1500 infer(r#"
1501struct Foo;
1502impl Foo { const ASSOC_CONST: u32 = 0; }
1503const GLOBAL_CONST: u32 = 101;
1504fn test() {
1505 const LOCAL_CONST: u32 = 99;
1506 let x = LOCAL_CONST;
1507 let z = GLOBAL_CONST;
1508 let id = Foo::ASSOC_CONST;
1509}
1510"#),
1511 @r###"
1512 [49; 50) '0': u32
1513 [80; 83) '101': u32
1514 [95; 213) '{ ...NST; }': ()
1515 [138; 139) 'x': u32
1516 [142; 153) 'LOCAL_CONST': u32
1517 [163; 164) 'z': u32
1518 [167; 179) 'GLOBAL_CONST': u32
1519 [189; 191) 'id': u32
1520 [194; 210) 'Foo::A..._CONST': u32
1521 "###
1522 );
1523}
1524
1525#[test]
1526fn infer_static() {
1527 assert_snapshot!(
1528 infer(r#"
1529static GLOBAL_STATIC: u32 = 101;
1530static mut GLOBAL_STATIC_MUT: u32 = 101;
1531fn test() {
1532 static LOCAL_STATIC: u32 = 99;
1533 static mut LOCAL_STATIC_MUT: u32 = 99;
1534 let x = LOCAL_STATIC;
1535 let y = LOCAL_STATIC_MUT;
1536 let z = GLOBAL_STATIC;
1537 let w = GLOBAL_STATIC_MUT;
1538}
1539"#),
1540 @r###"
1541 [29; 32) '101': u32
1542 [70; 73) '101': u32
1543 [85; 280) '{ ...MUT; }': ()
1544 [173; 174) 'x': u32
1545 [177; 189) 'LOCAL_STATIC': u32
1546 [199; 200) 'y': u32
1547 [203; 219) 'LOCAL_...IC_MUT': u32
1548 [229; 230) 'z': u32
1549 [233; 246) 'GLOBAL_STATIC': u32
1550 [256; 257) 'w': u32
1551 [260; 277) 'GLOBAL...IC_MUT': u32
1552 "###
1553 );
1554}
1555
1556#[test]
1557fn shadowing_primitive() {
1558 let t = type_at(
1559 r#"
1560//- /main.rs
1561struct i32;
1562struct Foo;
1563
1564impl i32 { fn foo(&self) -> Foo { Foo } }
1565
1566fn main() {
1567 let x: i32 = i32;
1568 x.foo()<|>;
1569}"#,
1570 );
1571 assert_eq!(t, "Foo");
1572}
1573
1574#[test]
1575fn not_shadowing_primitive_by_module() {
1576 let t = type_at(
1577 r#"
1578//- /str.rs
1579fn foo() {}
1580
1581//- /main.rs
1582mod str;
1583fn foo() -> &'static str { "" }
1584
1585fn main() {
1586 foo()<|>;
1587}"#,
1588 );
1589 assert_eq!(t, "&str");
1590}
1591
1592#[test]
1593fn not_shadowing_module_by_primitive() {
1594 let t = type_at(
1595 r#"
1596//- /str.rs
1597fn foo() -> u32 {0}
1598
1599//- /main.rs
1600mod str;
1601fn foo() -> &'static str { "" }
1602
1603fn main() {
1604 str::foo()<|>;
1605}"#,
1606 );
1607 assert_eq!(t, "u32");
1608}
1609
1610#[test]
1611fn closure_return() {
1612 assert_snapshot!(
1613 infer(r#"
1614fn foo() -> u32 {
1615 let x = || -> usize { return 1; };
1616}
1617"#),
1618 @r###"
1619 [17; 59) '{ ...; }; }': ()
1620 [27; 28) 'x': || -> usize
1621 [31; 56) '|| -> ...n 1; }': || -> usize
1622 [43; 56) '{ return 1; }': !
1623 [45; 53) 'return 1': !
1624 [52; 53) '1': usize
1625 "###
1626 );
1627}
1628
1629#[test]
1630fn closure_return_unit() {
1631 assert_snapshot!(
1632 infer(r#"
1633fn foo() -> u32 {
1634 let x = || { return; };
1635}
1636"#),
1637 @r###"
1638 [17; 48) '{ ...; }; }': ()
1639 [27; 28) 'x': || -> ()
1640 [31; 45) '|| { return; }': || -> ()
1641 [34; 45) '{ return; }': !
1642 [36; 42) 'return': !
1643 "###
1644 );
1645}
1646
1647#[test]
1648fn closure_return_inferred() {
1649 assert_snapshot!(
1650 infer(r#"
1651fn foo() -> u32 {
1652 let x = || { "test" };
1653}
1654"#),
1655 @r###"
1656 [17; 47) '{ ..." }; }': ()
1657 [27; 28) 'x': || -> &str
1658 [31; 44) '|| { "test" }': || -> &str
1659 [34; 44) '{ "test" }': &str
1660 [36; 42) '"test"': &str
1661 "###
1662 );
1663}
diff --git a/crates/ra_hir_ty/src/tests/traits.rs b/crates/ra_hir_ty/src/tests/traits.rs
new file mode 100644
index 000000000..ae316922b
--- /dev/null
+++ b/crates/ra_hir_ty/src/tests/traits.rs
@@ -0,0 +1,1598 @@
1use insta::assert_snapshot;
2
3use ra_db::fixture::WithFixture;
4use test_utils::covers;
5
6use super::{infer, infer_with_mismatches, type_at, type_at_pos};
7use crate::test_db::TestDB;
8
9#[test]
10fn infer_await() {
11 let (db, pos) = TestDB::with_position(
12 r#"
13//- /main.rs crate:main deps:std
14
15struct IntFuture;
16
17impl Future for IntFuture {
18 type Output = u64;
19}
20
21fn test() {
22 let r = IntFuture;
23 let v = r.await;
24 v<|>;
25}
26
27//- /std.rs crate:std
28#[prelude_import] use future::*;
29mod future {
30 trait Future {
31 type Output;
32 }
33}
34
35"#,
36 );
37 assert_eq!("u64", type_at_pos(&db, pos));
38}
39
40#[test]
41fn infer_try() {
42 let (db, pos) = TestDB::with_position(
43 r#"
44//- /main.rs crate:main deps:std
45
46fn test() {
47 let r: Result<i32, u64> = Result::Ok(1);
48 let v = r?;
49 v<|>;
50}
51
52//- /std.rs crate:std
53
54#[prelude_import] use ops::*;
55mod ops {
56 trait Try {
57 type Ok;
58 type Error;
59 }
60}
61
62#[prelude_import] use result::*;
63mod result {
64 enum Result<O, E> {
65 Ok(O),
66 Err(E)
67 }
68
69 impl<O, E> crate::ops::Try for Result<O, E> {
70 type Ok = O;
71 type Error = E;
72 }
73}
74
75"#,
76 );
77 assert_eq!("i32", type_at_pos(&db, pos));
78}
79
80#[test]
81fn infer_for_loop() {
82 let (db, pos) = TestDB::with_position(
83 r#"
84//- /main.rs crate:main deps:std
85
86use std::collections::Vec;
87
88fn test() {
89 let v = Vec::new();
90 v.push("foo");
91 for x in v {
92 x<|>;
93 }
94}
95
96//- /std.rs crate:std
97
98#[prelude_import] use iter::*;
99mod iter {
100 trait IntoIterator {
101 type Item;
102 }
103}
104
105mod collections {
106 struct Vec<T> {}
107 impl<T> Vec<T> {
108 fn new() -> Self { Vec {} }
109 fn push(&mut self, t: T) { }
110 }
111
112 impl<T> crate::iter::IntoIterator for Vec<T> {
113 type Item=T;
114 }
115}
116"#,
117 );
118 assert_eq!("&str", type_at_pos(&db, pos));
119}
120
121#[test]
122fn infer_ops_neg() {
123 let (db, pos) = TestDB::with_position(
124 r#"
125//- /main.rs crate:main deps:std
126
127struct Bar;
128struct Foo;
129
130impl std::ops::Neg for Bar {
131 type Output = Foo;
132}
133
134fn test() {
135 let a = Bar;
136 let b = -a;
137 b<|>;
138}
139
140//- /std.rs crate:std
141
142#[prelude_import] use ops::*;
143mod ops {
144 pub trait Neg {
145 type Output;
146 }
147}
148"#,
149 );
150 assert_eq!("Foo", type_at_pos(&db, pos));
151}
152
153#[test]
154fn infer_ops_not() {
155 let (db, pos) = TestDB::with_position(
156 r#"
157//- /main.rs crate:main deps:std
158
159struct Bar;
160struct Foo;
161
162impl std::ops::Not for Bar {
163 type Output = Foo;
164}
165
166fn test() {
167 let a = Bar;
168 let b = !a;
169 b<|>;
170}
171
172//- /std.rs crate:std
173
174#[prelude_import] use ops::*;
175mod ops {
176 pub trait Not {
177 type Output;
178 }
179}
180"#,
181 );
182 assert_eq!("Foo", type_at_pos(&db, pos));
183}
184
185#[test]
186fn infer_from_bound_1() {
187 assert_snapshot!(
188 infer(r#"
189trait Trait<T> {}
190struct S<T>(T);
191impl<U> Trait<U> for S<U> {}
192fn foo<T: Trait<u32>>(t: T) {}
193fn test() {
194 let s = S(unknown);
195 foo(s);
196}
197"#),
198 @r###"
199 [86; 87) 't': T
200 [92; 94) '{}': ()
201 [105; 144) '{ ...(s); }': ()
202 [115; 116) 's': S<u32>
203 [119; 120) 'S': S<u32>(T) -> S<T>
204 [119; 129) 'S(unknown)': S<u32>
205 [121; 128) 'unknown': u32
206 [135; 138) 'foo': fn foo<S<u32>>(T) -> ()
207 [135; 141) 'foo(s)': ()
208 [139; 140) 's': S<u32>
209 "###
210 );
211}
212
213#[test]
214fn infer_from_bound_2() {
215 assert_snapshot!(
216 infer(r#"
217trait Trait<T> {}
218struct S<T>(T);
219impl<U> Trait<U> for S<U> {}
220fn foo<U, T: Trait<U>>(t: T) -> U {}
221fn test() {
222 let s = S(unknown);
223 let x: u32 = foo(s);
224}
225"#),
226 @r###"
227 [87; 88) 't': T
228 [98; 100) '{}': ()
229 [111; 163) '{ ...(s); }': ()
230 [121; 122) 's': S<u32>
231 [125; 126) 'S': S<u32>(T) -> S<T>
232 [125; 135) 'S(unknown)': S<u32>
233 [127; 134) 'unknown': u32
234 [145; 146) 'x': u32
235 [154; 157) 'foo': fn foo<u32, S<u32>>(T) -> U
236 [154; 160) 'foo(s)': u32
237 [158; 159) 's': S<u32>
238 "###
239 );
240}
241
242#[test]
243fn infer_project_associated_type() {
244 // y, z, a don't yet work because of https://github.com/rust-lang/chalk/issues/234
245 assert_snapshot!(
246 infer(r#"
247trait Iterable {
248 type Item;
249}
250struct S;
251impl Iterable for S { type Item = u32; }
252fn test<T: Iterable>() {
253 let x: <S as Iterable>::Item = 1;
254 let y: <T as Iterable>::Item = no_matter;
255 let z: T::Item = no_matter;
256 let a: <T>::Item = no_matter;
257}
258"#),
259 @r###"
260 [108; 261) '{ ...ter; }': ()
261 [118; 119) 'x': u32
262 [145; 146) '1': u32
263 [156; 157) 'y': {unknown}
264 [183; 192) 'no_matter': {unknown}
265 [202; 203) 'z': {unknown}
266 [215; 224) 'no_matter': {unknown}
267 [234; 235) 'a': {unknown}
268 [249; 258) 'no_matter': {unknown}
269 "###
270 );
271}
272
273#[test]
274fn infer_return_associated_type() {
275 assert_snapshot!(
276 infer(r#"
277trait Iterable {
278 type Item;
279}
280struct S;
281impl Iterable for S { type Item = u32; }
282fn foo1<T: Iterable>(t: T) -> T::Item {}
283fn foo2<T: Iterable>(t: T) -> <T as Iterable>::Item {}
284fn foo3<T: Iterable>(t: T) -> <T>::Item {}
285fn test() {
286 let x = foo1(S);
287 let y = foo2(S);
288 let z = foo3(S);
289}
290"#),
291 @r###"
292 [106; 107) 't': T
293 [123; 125) '{}': ()
294 [147; 148) 't': T
295 [178; 180) '{}': ()
296 [202; 203) 't': T
297 [221; 223) '{}': ()
298 [234; 300) '{ ...(S); }': ()
299 [244; 245) 'x': u32
300 [248; 252) 'foo1': fn foo1<S>(T) -> <T as Iterable>::Item
301 [248; 255) 'foo1(S)': u32
302 [253; 254) 'S': S
303 [265; 266) 'y': u32
304 [269; 273) 'foo2': fn foo2<S>(T) -> <T as Iterable>::Item
305 [269; 276) 'foo2(S)': u32
306 [274; 275) 'S': S
307 [286; 287) 'z': u32
308 [290; 294) 'foo3': fn foo3<S>(T) -> <T as Iterable>::Item
309 [290; 297) 'foo3(S)': u32
310 [295; 296) 'S': S
311 "###
312 );
313}
314
315#[test]
316fn infer_associated_type_bound() {
317 assert_snapshot!(
318 infer(r#"
319trait Iterable {
320 type Item;
321}
322fn test<T: Iterable<Item=u32>>() {
323 let y: T::Item = unknown;
324}
325"#),
326 @r###"
327 [67; 100) '{ ...own; }': ()
328 [77; 78) 'y': {unknown}
329 [90; 97) 'unknown': {unknown}
330 "###
331 );
332}
333
334#[test]
335fn infer_const_body() {
336 assert_snapshot!(
337 infer(r#"
338const A: u32 = 1 + 1;
339static B: u64 = { let x = 1; x };
340"#),
341 @r###"
342 [16; 17) '1': u32
343 [16; 21) '1 + 1': u32
344 [20; 21) '1': u32
345 [39; 55) '{ let ...1; x }': u64
346 [45; 46) 'x': u64
347 [49; 50) '1': u64
348 [52; 53) 'x': u64
349 "###
350 );
351}
352
353#[test]
354fn tuple_struct_fields() {
355 assert_snapshot!(
356 infer(r#"
357struct S(i32, u64);
358fn test() -> u64 {
359 let a = S(4, 6);
360 let b = a.0;
361 a.1
362}
363"#),
364 @r###"
365 [38; 87) '{ ... a.1 }': u64
366 [48; 49) 'a': S
367 [52; 53) 'S': S(i32, u64) -> S
368 [52; 59) 'S(4, 6)': S
369 [54; 55) '4': i32
370 [57; 58) '6': u64
371 [69; 70) 'b': i32
372 [73; 74) 'a': S
373 [73; 76) 'a.0': i32
374 [82; 83) 'a': S
375 [82; 85) 'a.1': u64
376 "###
377 );
378}
379
380#[test]
381fn tuple_struct_with_fn() {
382 assert_snapshot!(
383 infer(r#"
384struct S(fn(u32) -> u64);
385fn test() -> u64 {
386 let a = S(|i| 2*i);
387 let b = a.0(4);
388 a.0(2)
389}
390"#),
391 @r###"
392 [44; 102) '{ ...0(2) }': u64
393 [54; 55) 'a': S
394 [58; 59) 'S': S(fn(u32) -> u64) -> S
395 [58; 68) 'S(|i| 2*i)': S
396 [60; 67) '|i| 2*i': |u32| -> u64
397 [61; 62) 'i': u32
398 [64; 65) '2': u32
399 [64; 67) '2*i': u32
400 [66; 67) 'i': u32
401 [78; 79) 'b': u64
402 [82; 83) 'a': S
403 [82; 85) 'a.0': fn(u32) -> u64
404 [82; 88) 'a.0(4)': u64
405 [86; 87) '4': u32
406 [94; 95) 'a': S
407 [94; 97) 'a.0': fn(u32) -> u64
408 [94; 100) 'a.0(2)': u64
409 [98; 99) '2': u32
410 "###
411 );
412}
413
414#[test]
415fn indexing_arrays() {
416 assert_snapshot!(
417 infer("fn main() { &mut [9][2]; }"),
418 @r###"
419 [10; 26) '{ &mut...[2]; }': ()
420 [12; 23) '&mut [9][2]': &mut {unknown}
421 [17; 20) '[9]': [i32;_]
422 [17; 23) '[9][2]': {unknown}
423 [18; 19) '9': i32
424 [21; 22) '2': i32
425 "###
426 )
427}
428
429#[test]
430fn infer_ops_index() {
431 let (db, pos) = TestDB::with_position(
432 r#"
433//- /main.rs crate:main deps:std
434
435struct Bar;
436struct Foo;
437
438impl std::ops::Index<u32> for Bar {
439 type Output = Foo;
440}
441
442fn test() {
443 let a = Bar;
444 let b = a[1];
445 b<|>;
446}
447
448//- /std.rs crate:std
449
450#[prelude_import] use ops::*;
451mod ops {
452 pub trait Index<Idx> {
453 type Output;
454 }
455}
456"#,
457 );
458 assert_eq!("Foo", type_at_pos(&db, pos));
459}
460
461#[test]
462fn deref_trait() {
463 let t = type_at(
464 r#"
465//- /main.rs
466#[lang = "deref"]
467trait Deref {
468 type Target;
469 fn deref(&self) -> &Self::Target;
470}
471
472struct Arc<T>;
473impl<T> Deref for Arc<T> {
474 type Target = T;
475}
476
477struct S;
478impl S {
479 fn foo(&self) -> u128 {}
480}
481
482fn test(s: Arc<S>) {
483 (*s, s.foo())<|>;
484}
485"#,
486 );
487 assert_eq!(t, "(S, u128)");
488}
489
490#[test]
491fn deref_trait_with_inference_var() {
492 let t = type_at(
493 r#"
494//- /main.rs
495#[lang = "deref"]
496trait Deref {
497 type Target;
498 fn deref(&self) -> &Self::Target;
499}
500
501struct Arc<T>;
502fn new_arc<T>() -> Arc<T> {}
503impl<T> Deref for Arc<T> {
504 type Target = T;
505}
506
507struct S;
508fn foo(a: Arc<S>) {}
509
510fn test() {
511 let a = new_arc();
512 let b = (*a)<|>;
513 foo(a);
514}
515"#,
516 );
517 assert_eq!(t, "S");
518}
519
520#[test]
521fn deref_trait_infinite_recursion() {
522 let t = type_at(
523 r#"
524//- /main.rs
525#[lang = "deref"]
526trait Deref {
527 type Target;
528 fn deref(&self) -> &Self::Target;
529}
530
531struct S;
532
533impl Deref for S {
534 type Target = S;
535}
536
537fn test(s: S) {
538 s.foo()<|>;
539}
540"#,
541 );
542 assert_eq!(t, "{unknown}");
543}
544
545#[test]
546fn deref_trait_with_question_mark_size() {
547 let t = type_at(
548 r#"
549//- /main.rs
550#[lang = "deref"]
551trait Deref {
552 type Target;
553 fn deref(&self) -> &Self::Target;
554}
555
556struct Arc<T>;
557impl<T> Deref for Arc<T> {
558 type Target = T;
559}
560
561struct S;
562impl S {
563 fn foo(&self) -> u128 {}
564}
565
566fn test(s: Arc<S>) {
567 (*s, s.foo())<|>;
568}
569"#,
570 );
571 assert_eq!(t, "(S, u128)");
572}
573
574#[test]
575fn obligation_from_function_clause() {
576 let t = type_at(
577 r#"
578//- /main.rs
579struct S;
580
581trait Trait<T> {}
582impl Trait<u32> for S {}
583
584fn foo<T: Trait<U>, U>(t: T) -> U {}
585
586fn test(s: S) {
587 foo(s)<|>;
588}
589"#,
590 );
591 assert_eq!(t, "u32");
592}
593
594#[test]
595fn obligation_from_method_clause() {
596 let t = type_at(
597 r#"
598//- /main.rs
599struct S;
600
601trait Trait<T> {}
602impl Trait<isize> for S {}
603
604struct O;
605impl O {
606 fn foo<T: Trait<U>, U>(&self, t: T) -> U {}
607}
608
609fn test() {
610 O.foo(S)<|>;
611}
612"#,
613 );
614 assert_eq!(t, "isize");
615}
616
617#[test]
618fn obligation_from_self_method_clause() {
619 let t = type_at(
620 r#"
621//- /main.rs
622struct S;
623
624trait Trait<T> {}
625impl Trait<i64> for S {}
626
627impl S {
628 fn foo<U>(&self) -> U where Self: Trait<U> {}
629}
630
631fn test() {
632 S.foo()<|>;
633}
634"#,
635 );
636 assert_eq!(t, "i64");
637}
638
639#[test]
640fn obligation_from_impl_clause() {
641 let t = type_at(
642 r#"
643//- /main.rs
644struct S;
645
646trait Trait<T> {}
647impl Trait<&str> for S {}
648
649struct O<T>;
650impl<U, T: Trait<U>> O<T> {
651 fn foo(&self) -> U {}
652}
653
654fn test(o: O<S>) {
655 o.foo()<|>;
656}
657"#,
658 );
659 assert_eq!(t, "&str");
660}
661
662#[test]
663fn generic_param_env_1() {
664 let t = type_at(
665 r#"
666//- /main.rs
667trait Clone {}
668trait Trait { fn foo(self) -> u128; }
669struct S;
670impl Clone for S {}
671impl<T> Trait for T where T: Clone {}
672fn test<T: Clone>(t: T) { t.foo()<|>; }
673"#,
674 );
675 assert_eq!(t, "u128");
676}
677
678#[test]
679fn generic_param_env_1_not_met() {
680 let t = type_at(
681 r#"
682//- /main.rs
683trait Clone {}
684trait Trait { fn foo(self) -> u128; }
685struct S;
686impl Clone for S {}
687impl<T> Trait for T where T: Clone {}
688fn test<T>(t: T) { t.foo()<|>; }
689"#,
690 );
691 assert_eq!(t, "{unknown}");
692}
693
694#[test]
695fn generic_param_env_2() {
696 let t = type_at(
697 r#"
698//- /main.rs
699trait Trait { fn foo(self) -> u128; }
700struct S;
701impl Trait for S {}
702fn test<T: Trait>(t: T) { t.foo()<|>; }
703"#,
704 );
705 assert_eq!(t, "u128");
706}
707
708#[test]
709fn generic_param_env_2_not_met() {
710 let t = type_at(
711 r#"
712//- /main.rs
713trait Trait { fn foo(self) -> u128; }
714struct S;
715impl Trait for S {}
716fn test<T>(t: T) { t.foo()<|>; }
717"#,
718 );
719 assert_eq!(t, "{unknown}");
720}
721
722#[test]
723fn generic_param_env_deref() {
724 let t = type_at(
725 r#"
726//- /main.rs
727#[lang = "deref"]
728trait Deref {
729 type Target;
730}
731trait Trait {}
732impl<T> Deref for T where T: Trait {
733 type Target = i128;
734}
735fn test<T: Trait>(t: T) { (*t)<|>; }
736"#,
737 );
738 assert_eq!(t, "i128");
739}
740
741#[test]
742fn associated_type_placeholder() {
743 let t = type_at(
744 r#"
745//- /main.rs
746pub trait ApplyL {
747 type Out;
748}
749
750pub struct RefMutL<T>;
751
752impl<T> ApplyL for RefMutL<T> {
753 type Out = <T as ApplyL>::Out;
754}
755
756fn test<T: ApplyL>() {
757 let y: <RefMutL<T> as ApplyL>::Out = no_matter;
758 y<|>;
759}
760"#,
761 );
762 // inside the generic function, the associated type gets normalized to a placeholder `ApplL::Out<T>` [https://rust-lang.github.io/rustc-guide/traits/associated-types.html#placeholder-associated-types].
763 // FIXME: fix type parameter names going missing when going through Chalk
764 assert_eq!(t, "ApplyL::Out<[missing name]>");
765}
766
767#[test]
768fn associated_type_placeholder_2() {
769 let t = type_at(
770 r#"
771//- /main.rs
772pub trait ApplyL {
773 type Out;
774}
775fn foo<T: ApplyL>(t: T) -> <T as ApplyL>::Out;
776
777fn test<T: ApplyL>(t: T) {
778 let y = foo(t);
779 y<|>;
780}
781"#,
782 );
783 // FIXME here Chalk doesn't normalize the type to a placeholder. I think we
784 // need to add a rule like Normalize(<T as ApplyL>::Out -> ApplyL::Out<T>)
785 // to the trait env ourselves here; probably Chalk can't do this by itself.
786 // assert_eq!(t, "ApplyL::Out<[missing name]>");
787 assert_eq!(t, "{unknown}");
788}
789
790#[test]
791fn impl_trait() {
792 assert_snapshot!(
793 infer(r#"
794trait Trait<T> {
795 fn foo(&self) -> T;
796 fn foo2(&self) -> i64;
797}
798fn bar() -> impl Trait<u64> {}
799
800fn test(x: impl Trait<u64>, y: &impl Trait<u64>) {
801 x;
802 y;
803 let z = bar();
804 x.foo();
805 y.foo();
806 z.foo();
807 x.foo2();
808 y.foo2();
809 z.foo2();
810}
811"#),
812 @r###"
813 [30; 34) 'self': &Self
814 [55; 59) 'self': &Self
815 [99; 101) '{}': ()
816 [111; 112) 'x': impl Trait<u64>
817 [131; 132) 'y': &impl Trait<u64>
818 [152; 269) '{ ...2(); }': ()
819 [158; 159) 'x': impl Trait<u64>
820 [165; 166) 'y': &impl Trait<u64>
821 [176; 177) 'z': impl Trait<u64>
822 [180; 183) 'bar': fn bar() -> impl Trait<u64>
823 [180; 185) 'bar()': impl Trait<u64>
824 [191; 192) 'x': impl Trait<u64>
825 [191; 198) 'x.foo()': u64
826 [204; 205) 'y': &impl Trait<u64>
827 [204; 211) 'y.foo()': u64
828 [217; 218) 'z': impl Trait<u64>
829 [217; 224) 'z.foo()': u64
830 [230; 231) 'x': impl Trait<u64>
831 [230; 238) 'x.foo2()': i64
832 [244; 245) 'y': &impl Trait<u64>
833 [244; 252) 'y.foo2()': i64
834 [258; 259) 'z': impl Trait<u64>
835 [258; 266) 'z.foo2()': i64
836 "###
837 );
838}
839
840#[test]
841fn dyn_trait() {
842 assert_snapshot!(
843 infer(r#"
844trait Trait<T> {
845 fn foo(&self) -> T;
846 fn foo2(&self) -> i64;
847}
848fn bar() -> dyn Trait<u64> {}
849
850fn test(x: dyn Trait<u64>, y: &dyn Trait<u64>) {
851 x;
852 y;
853 let z = bar();
854 x.foo();
855 y.foo();
856 z.foo();
857 x.foo2();
858 y.foo2();
859 z.foo2();
860}
861"#),
862 @r###"
863 [30; 34) 'self': &Self
864 [55; 59) 'self': &Self
865 [98; 100) '{}': ()
866 [110; 111) 'x': dyn Trait<u64>
867 [129; 130) 'y': &dyn Trait<u64>
868 [149; 266) '{ ...2(); }': ()
869 [155; 156) 'x': dyn Trait<u64>
870 [162; 163) 'y': &dyn Trait<u64>
871 [173; 174) 'z': dyn Trait<u64>
872 [177; 180) 'bar': fn bar() -> dyn Trait<u64>
873 [177; 182) 'bar()': dyn Trait<u64>
874 [188; 189) 'x': dyn Trait<u64>
875 [188; 195) 'x.foo()': u64
876 [201; 202) 'y': &dyn Trait<u64>
877 [201; 208) 'y.foo()': u64
878 [214; 215) 'z': dyn Trait<u64>
879 [214; 221) 'z.foo()': u64
880 [227; 228) 'x': dyn Trait<u64>
881 [227; 235) 'x.foo2()': i64
882 [241; 242) 'y': &dyn Trait<u64>
883 [241; 249) 'y.foo2()': i64
884 [255; 256) 'z': dyn Trait<u64>
885 [255; 263) 'z.foo2()': i64
886 "###
887 );
888}
889
890#[test]
891fn dyn_trait_bare() {
892 assert_snapshot!(
893 infer(r#"
894trait Trait {
895 fn foo(&self) -> u64;
896}
897fn bar() -> Trait {}
898
899fn test(x: Trait, y: &Trait) -> u64 {
900 x;
901 y;
902 let z = bar();
903 x.foo();
904 y.foo();
905 z.foo();
906}
907"#),
908 @r###"
909 [27; 31) 'self': &Self
910 [61; 63) '{}': ()
911 [73; 74) 'x': dyn Trait
912 [83; 84) 'y': &dyn Trait
913 [101; 176) '{ ...o(); }': ()
914 [107; 108) 'x': dyn Trait
915 [114; 115) 'y': &dyn Trait
916 [125; 126) 'z': dyn Trait
917 [129; 132) 'bar': fn bar() -> dyn Trait
918 [129; 134) 'bar()': dyn Trait
919 [140; 141) 'x': dyn Trait
920 [140; 147) 'x.foo()': u64
921 [153; 154) 'y': &dyn Trait
922 [153; 160) 'y.foo()': u64
923 [166; 167) 'z': dyn Trait
924 [166; 173) 'z.foo()': u64
925 "###
926 );
927}
928
929#[test]
930fn weird_bounds() {
931 assert_snapshot!(
932 infer(r#"
933trait Trait {}
934fn test() {
935 let a: impl Trait + 'lifetime = foo;
936 let b: impl 'lifetime = foo;
937 let b: impl (Trait) = foo;
938 let b: impl ('lifetime) = foo;
939 let d: impl ?Sized = foo;
940 let e: impl Trait + ?Sized = foo;
941}
942"#),
943 @r###"
944 [26; 237) '{ ...foo; }': ()
945 [36; 37) 'a': impl Trait + {error}
946 [64; 67) 'foo': impl Trait + {error}
947 [77; 78) 'b': impl {error}
948 [97; 100) 'foo': impl {error}
949 [110; 111) 'b': impl Trait
950 [128; 131) 'foo': impl Trait
951 [141; 142) 'b': impl {error}
952 [163; 166) 'foo': impl {error}
953 [176; 177) 'd': impl {error}
954 [193; 196) 'foo': impl {error}
955 [206; 207) 'e': impl Trait + {error}
956 [231; 234) 'foo': impl Trait + {error}
957 "###
958 );
959}
960
961#[test]
962fn error_bound_chalk() {
963 let t = type_at(
964 r#"
965//- /main.rs
966trait Trait {
967 fn foo(&self) -> u32 {}
968}
969
970fn test(x: (impl Trait + UnknownTrait)) {
971 x.foo()<|>;
972}
973"#,
974 );
975 assert_eq!(t, "u32");
976}
977
978#[test]
979fn assoc_type_bindings() {
980 assert_snapshot!(
981 infer(r#"
982trait Trait {
983 type Type;
984}
985
986fn get<T: Trait>(t: T) -> <T as Trait>::Type {}
987fn get2<U, T: Trait<Type = U>>(t: T) -> U {}
988fn set<T: Trait<Type = u64>>(t: T) -> T {t}
989
990struct S<T>;
991impl<T> Trait for S<T> { type Type = T; }
992
993fn test<T: Trait<Type = u32>>(x: T, y: impl Trait<Type = i64>) {
994 get(x);
995 get2(x);
996 get(y);
997 get2(y);
998 get(set(S));
999 get2(set(S));
1000 get2(S::<str>);
1001}
1002"#),
1003 @r###"
1004 [50; 51) 't': T
1005 [78; 80) '{}': ()
1006 [112; 113) 't': T
1007 [123; 125) '{}': ()
1008 [155; 156) 't': T
1009 [166; 169) '{t}': T
1010 [167; 168) 't': T
1011 [257; 258) 'x': T
1012 [263; 264) 'y': impl Trait<Type = i64>
1013 [290; 398) '{ ...r>); }': ()
1014 [296; 299) 'get': fn get<T>(T) -> <T as Trait>::Type
1015 [296; 302) 'get(x)': {unknown}
1016 [300; 301) 'x': T
1017 [308; 312) 'get2': fn get2<{unknown}, T>(T) -> U
1018 [308; 315) 'get2(x)': {unknown}
1019 [313; 314) 'x': T
1020 [321; 324) 'get': fn get<impl Trait<Type = i64>>(T) -> <T as Trait>::Type
1021 [321; 327) 'get(y)': {unknown}
1022 [325; 326) 'y': impl Trait<Type = i64>
1023 [333; 337) 'get2': fn get2<{unknown}, impl Trait<Type = i64>>(T) -> U
1024 [333; 340) 'get2(y)': {unknown}
1025 [338; 339) 'y': impl Trait<Type = i64>
1026 [346; 349) 'get': fn get<S<u64>>(T) -> <T as Trait>::Type
1027 [346; 357) 'get(set(S))': u64
1028 [350; 353) 'set': fn set<S<u64>>(T) -> T
1029 [350; 356) 'set(S)': S<u64>
1030 [354; 355) 'S': S<u64>
1031 [363; 367) 'get2': fn get2<u64, S<u64>>(T) -> U
1032 [363; 375) 'get2(set(S))': u64
1033 [368; 371) 'set': fn set<S<u64>>(T) -> T
1034 [368; 374) 'set(S)': S<u64>
1035 [372; 373) 'S': S<u64>
1036 [381; 385) 'get2': fn get2<str, S<str>>(T) -> U
1037 [381; 395) 'get2(S::<str>)': str
1038 [386; 394) 'S::<str>': S<str>
1039 "###
1040 );
1041}
1042
1043#[test]
1044fn impl_trait_assoc_binding_projection_bug() {
1045 let (db, pos) = TestDB::with_position(
1046 r#"
1047//- /main.rs crate:main deps:std
1048pub trait Language {
1049 type Kind;
1050}
1051pub enum RustLanguage {}
1052impl Language for RustLanguage {
1053 type Kind = SyntaxKind;
1054}
1055struct SyntaxNode<L> {}
1056fn foo() -> impl Iterator<Item = SyntaxNode<RustLanguage>> {}
1057
1058trait Clone {
1059 fn clone(&self) -> Self;
1060}
1061
1062fn api_walkthrough() {
1063 for node in foo() {
1064 node.clone()<|>;
1065 }
1066}
1067
1068//- /std.rs crate:std
1069#[prelude_import] use iter::*;
1070mod iter {
1071 trait IntoIterator {
1072 type Item;
1073 }
1074 trait Iterator {
1075 type Item;
1076 }
1077 impl<T: Iterator> IntoIterator for T {
1078 type Item = <T as Iterator>::Item;
1079 }
1080}
1081"#,
1082 );
1083 assert_eq!("{unknown}", type_at_pos(&db, pos));
1084}
1085
1086#[test]
1087fn projection_eq_within_chalk() {
1088 // std::env::set_var("CHALK_DEBUG", "1");
1089 assert_snapshot!(
1090 infer(r#"
1091trait Trait1 {
1092 type Type;
1093}
1094trait Trait2<T> {
1095 fn foo(self) -> T;
1096}
1097impl<T, U> Trait2<T> for U where U: Trait1<Type = T> {}
1098
1099fn test<T: Trait1<Type = u32>>(x: T) {
1100 x.foo();
1101}
1102"#),
1103 @r###"
1104 [62; 66) 'self': Self
1105 [164; 165) 'x': T
1106 [170; 186) '{ ...o(); }': ()
1107 [176; 177) 'x': T
1108 [176; 183) 'x.foo()': {unknown}
1109 "###
1110 );
1111}
1112
1113#[test]
1114fn where_clause_trait_in_scope_for_method_resolution() {
1115 let t = type_at(
1116 r#"
1117//- /main.rs
1118mod foo {
1119 trait Trait {
1120 fn foo(&self) -> u32 {}
1121 }
1122}
1123
1124fn test<T: foo::Trait>(x: T) {
1125 x.foo()<|>;
1126}
1127"#,
1128 );
1129 assert_eq!(t, "u32");
1130}
1131
1132#[test]
1133fn super_trait_method_resolution() {
1134 assert_snapshot!(
1135 infer(r#"
1136mod foo {
1137 trait SuperTrait {
1138 fn foo(&self) -> u32 {}
1139 }
1140}
1141trait Trait1: foo::SuperTrait {}
1142trait Trait2 where Self: foo::SuperTrait {}
1143
1144fn test<T: Trait1, U: Trait2>(x: T, y: U) {
1145 x.foo();
1146 y.foo();
1147}
1148"#),
1149 @r###"
1150 [50; 54) 'self': &Self
1151 [63; 65) '{}': ()
1152 [182; 183) 'x': T
1153 [188; 189) 'y': U
1154 [194; 223) '{ ...o(); }': ()
1155 [200; 201) 'x': T
1156 [200; 207) 'x.foo()': u32
1157 [213; 214) 'y': U
1158 [213; 220) 'y.foo()': u32
1159 "###
1160 );
1161}
1162
1163#[test]
1164fn super_trait_cycle() {
1165 // This just needs to not crash
1166 assert_snapshot!(
1167 infer(r#"
1168trait A: B {}
1169trait B: A {}
1170
1171fn test<T: A>(x: T) {
1172 x.foo();
1173}
1174"#),
1175 @r###"
1176 [44; 45) 'x': T
1177 [50; 66) '{ ...o(); }': ()
1178 [56; 57) 'x': T
1179 [56; 63) 'x.foo()': {unknown}
1180 "###
1181 );
1182}
1183
1184#[test]
1185fn super_trait_assoc_type_bounds() {
1186 assert_snapshot!(
1187 infer(r#"
1188trait SuperTrait { type Type; }
1189trait Trait where Self: SuperTrait {}
1190
1191fn get2<U, T: Trait<Type = U>>(t: T) -> U {}
1192fn set<T: Trait<Type = u64>>(t: T) -> T {t}
1193
1194struct S<T>;
1195impl<T> SuperTrait for S<T> { type Type = T; }
1196impl<T> Trait for S<T> {}
1197
1198fn test() {
1199 get2(set(S));
1200}
1201"#),
1202 @r###"
1203 [103; 104) 't': T
1204 [114; 116) '{}': ()
1205 [146; 147) 't': T
1206 [157; 160) '{t}': T
1207 [158; 159) 't': T
1208 [259; 280) '{ ...S)); }': ()
1209 [265; 269) 'get2': fn get2<u64, S<u64>>(T) -> U
1210 [265; 277) 'get2(set(S))': u64
1211 [270; 273) 'set': fn set<S<u64>>(T) -> T
1212 [270; 276) 'set(S)': S<u64>
1213 [274; 275) 'S': S<u64>
1214 "###
1215 );
1216}
1217
1218#[test]
1219fn fn_trait() {
1220 assert_snapshot!(
1221 infer(r#"
1222trait FnOnce<Args> {
1223 type Output;
1224
1225 fn call_once(self, args: Args) -> <Self as FnOnce<Args>>::Output;
1226}
1227
1228fn test<F: FnOnce(u32, u64) -> u128>(f: F) {
1229 f.call_once((1, 2));
1230}
1231"#),
1232 @r###"
1233 [57; 61) 'self': Self
1234 [63; 67) 'args': Args
1235 [150; 151) 'f': F
1236 [156; 184) '{ ...2)); }': ()
1237 [162; 163) 'f': F
1238 [162; 181) 'f.call...1, 2))': {unknown}
1239 [174; 180) '(1, 2)': (u32, u64)
1240 [175; 176) '1': u32
1241 [178; 179) '2': u64
1242 "###
1243 );
1244}
1245
1246#[test]
1247fn closure_1() {
1248 assert_snapshot!(
1249 infer(r#"
1250#[lang = "fn_once"]
1251trait FnOnce<Args> {
1252 type Output;
1253}
1254
1255enum Option<T> { Some(T), None }
1256impl<T> Option<T> {
1257 fn map<U, F: FnOnce(T) -> U>(self, f: F) -> Option<U> {}
1258}
1259
1260fn test() {
1261 let x = Option::Some(1u32);
1262 x.map(|v| v + 1);
1263 x.map(|_v| 1u64);
1264 let y: Option<i64> = x.map(|_v| 1);
1265}
1266"#),
1267 @r###"
1268 [148; 152) 'self': Option<T>
1269 [154; 155) 'f': F
1270 [173; 175) '{}': ()
1271 [189; 308) '{ ... 1); }': ()
1272 [199; 200) 'x': Option<u32>
1273 [203; 215) 'Option::Some': Some<u32>(T) -> Option<T>
1274 [203; 221) 'Option...(1u32)': Option<u32>
1275 [216; 220) '1u32': u32
1276 [227; 228) 'x': Option<u32>
1277 [227; 243) 'x.map(...v + 1)': Option<u32>
1278 [233; 242) '|v| v + 1': |u32| -> u32
1279 [234; 235) 'v': u32
1280 [237; 238) 'v': u32
1281 [237; 242) 'v + 1': u32
1282 [241; 242) '1': u32
1283 [249; 250) 'x': Option<u32>
1284 [249; 265) 'x.map(... 1u64)': Option<u64>
1285 [255; 264) '|_v| 1u64': |u32| -> u64
1286 [256; 258) '_v': u32
1287 [260; 264) '1u64': u64
1288 [275; 276) 'y': Option<i64>
1289 [292; 293) 'x': Option<u32>
1290 [292; 305) 'x.map(|_v| 1)': Option<i64>
1291 [298; 304) '|_v| 1': |u32| -> i64
1292 [299; 301) '_v': u32
1293 [303; 304) '1': i64
1294 "###
1295 );
1296}
1297
1298#[test]
1299fn closure_2() {
1300 assert_snapshot!(
1301 infer(r#"
1302trait FnOnce<Args> {
1303 type Output;
1304}
1305
1306fn test<F: FnOnce(u32) -> u64>(f: F) {
1307 f(1);
1308 let g = |v| v + 1;
1309 g(1u64);
1310 let h = |v| 1u128 + v;
1311}
1312"#),
1313 @r###"
1314 [73; 74) 'f': F
1315 [79; 155) '{ ...+ v; }': ()
1316 [85; 86) 'f': F
1317 [85; 89) 'f(1)': {unknown}
1318 [87; 88) '1': i32
1319 [99; 100) 'g': |u64| -> i32
1320 [103; 112) '|v| v + 1': |u64| -> i32
1321 [104; 105) 'v': u64
1322 [107; 108) 'v': u64
1323 [107; 112) 'v + 1': i32
1324 [111; 112) '1': i32
1325 [118; 119) 'g': |u64| -> i32
1326 [118; 125) 'g(1u64)': i32
1327 [120; 124) '1u64': u64
1328 [135; 136) 'h': |u128| -> u128
1329 [139; 152) '|v| 1u128 + v': |u128| -> u128
1330 [140; 141) 'v': u128
1331 [143; 148) '1u128': u128
1332 [143; 152) '1u128 + v': u128
1333 [151; 152) 'v': u128
1334 "###
1335 );
1336}
1337
1338#[test]
1339fn closure_as_argument_inference_order() {
1340 assert_snapshot!(
1341 infer(r#"
1342#[lang = "fn_once"]
1343trait FnOnce<Args> {
1344 type Output;
1345}
1346
1347fn foo1<T, U, F: FnOnce(T) -> U>(x: T, f: F) -> U {}
1348fn foo2<T, U, F: FnOnce(T) -> U>(f: F, x: T) -> U {}
1349
1350struct S;
1351impl S {
1352 fn method(self) -> u64;
1353
1354 fn foo1<T, U, F: FnOnce(T) -> U>(self, x: T, f: F) -> U {}
1355 fn foo2<T, U, F: FnOnce(T) -> U>(self, f: F, x: T) -> U {}
1356}
1357
1358fn test() {
1359 let x1 = foo1(S, |s| s.method());
1360 let x2 = foo2(|s| s.method(), S);
1361 let x3 = S.foo1(S, |s| s.method());
1362 let x4 = S.foo2(|s| s.method(), S);
1363}
1364"#),
1365 @r###"
1366 [95; 96) 'x': T
1367 [101; 102) 'f': F
1368 [112; 114) '{}': ()
1369 [148; 149) 'f': F
1370 [154; 155) 'x': T
1371 [165; 167) '{}': ()
1372 [202; 206) 'self': S
1373 [254; 258) 'self': S
1374 [260; 261) 'x': T
1375 [266; 267) 'f': F
1376 [277; 279) '{}': ()
1377 [317; 321) 'self': S
1378 [323; 324) 'f': F
1379 [329; 330) 'x': T
1380 [340; 342) '{}': ()
1381 [356; 515) '{ ... S); }': ()
1382 [366; 368) 'x1': u64
1383 [371; 375) 'foo1': fn foo1<S, u64, |S| -> u64>(T, F) -> U
1384 [371; 394) 'foo1(S...hod())': u64
1385 [376; 377) 'S': S
1386 [379; 393) '|s| s.method()': |S| -> u64
1387 [380; 381) 's': S
1388 [383; 384) 's': S
1389 [383; 393) 's.method()': u64
1390 [404; 406) 'x2': u64
1391 [409; 413) 'foo2': fn foo2<S, u64, |S| -> u64>(F, T) -> U
1392 [409; 432) 'foo2(|...(), S)': u64
1393 [414; 428) '|s| s.method()': |S| -> u64
1394 [415; 416) 's': S
1395 [418; 419) 's': S
1396 [418; 428) 's.method()': u64
1397 [430; 431) 'S': S
1398 [442; 444) 'x3': u64
1399 [447; 448) 'S': S
1400 [447; 472) 'S.foo1...hod())': u64
1401 [454; 455) 'S': S
1402 [457; 471) '|s| s.method()': |S| -> u64
1403 [458; 459) 's': S
1404 [461; 462) 's': S
1405 [461; 471) 's.method()': u64
1406 [482; 484) 'x4': u64
1407 [487; 488) 'S': S
1408 [487; 512) 'S.foo2...(), S)': u64
1409 [494; 508) '|s| s.method()': |S| -> u64
1410 [495; 496) 's': S
1411 [498; 499) 's': S
1412 [498; 508) 's.method()': u64
1413 [510; 511) 'S': S
1414 "###
1415 );
1416}
1417
1418#[test]
1419fn unselected_projection_in_trait_env_1() {
1420 let t = type_at(
1421 r#"
1422//- /main.rs
1423trait Trait {
1424 type Item;
1425}
1426
1427trait Trait2 {
1428 fn foo(&self) -> u32;
1429}
1430
1431fn test<T: Trait>() where T::Item: Trait2 {
1432 let x: T::Item = no_matter;
1433 x.foo()<|>;
1434}
1435"#,
1436 );
1437 assert_eq!(t, "u32");
1438}
1439
1440#[test]
1441fn unselected_projection_in_trait_env_2() {
1442 let t = type_at(
1443 r#"
1444//- /main.rs
1445trait Trait<T> {
1446 type Item;
1447}
1448
1449trait Trait2 {
1450 fn foo(&self) -> u32;
1451}
1452
1453fn test<T, U>() where T::Item: Trait2, T: Trait<U::Item>, U: Trait<()> {
1454 let x: T::Item = no_matter;
1455 x.foo()<|>;
1456}
1457"#,
1458 );
1459 assert_eq!(t, "u32");
1460}
1461
1462#[test]
1463fn trait_impl_self_ty() {
1464 let t = type_at(
1465 r#"
1466//- /main.rs
1467trait Trait<T> {
1468 fn foo(&self);
1469}
1470
1471struct S;
1472
1473impl Trait<Self> for S {}
1474
1475fn test() {
1476 S.foo()<|>;
1477}
1478"#,
1479 );
1480 assert_eq!(t, "()");
1481}
1482
1483#[test]
1484fn trait_impl_self_ty_cycle() {
1485 let t = type_at(
1486 r#"
1487//- /main.rs
1488trait Trait {
1489 fn foo(&self);
1490}
1491
1492struct S<T>;
1493
1494impl Trait for S<Self> {}
1495
1496fn test() {
1497 S.foo()<|>;
1498}
1499"#,
1500 );
1501 assert_eq!(t, "{unknown}");
1502}
1503
1504#[test]
1505fn unselected_projection_in_trait_env_cycle_1() {
1506 let t = type_at(
1507 r#"
1508//- /main.rs
1509trait Trait {
1510 type Item;
1511}
1512
1513trait Trait2<T> {}
1514
1515fn test<T: Trait>() where T: Trait2<T::Item> {
1516 let x: T::Item = no_matter<|>;
1517}
1518"#,
1519 );
1520 // this is a legitimate cycle
1521 assert_eq!(t, "{unknown}");
1522}
1523
1524#[test]
1525fn unselected_projection_in_trait_env_cycle_2() {
1526 let t = type_at(
1527 r#"
1528//- /main.rs
1529trait Trait<T> {
1530 type Item;
1531}
1532
1533fn test<T, U>() where T: Trait<U::Item>, U: Trait<T::Item> {
1534 let x: T::Item = no_matter<|>;
1535}
1536"#,
1537 );
1538 // this is a legitimate cycle
1539 assert_eq!(t, "{unknown}");
1540}
1541
1542#[test]
1543fn unify_impl_trait() {
1544 covers!(insert_vars_for_impl_trait);
1545 assert_snapshot!(
1546 infer_with_mismatches(r#"
1547trait Trait<T> {}
1548
1549fn foo(x: impl Trait<u32>) { loop {} }
1550fn bar<T>(x: impl Trait<T>) -> T { loop {} }
1551
1552struct S<T>(T);
1553impl<T> Trait<T> for S<T> {}
1554
1555fn default<T>() -> T { loop {} }
1556
1557fn test() -> impl Trait<i32> {
1558 let s1 = S(default());
1559 foo(s1);
1560 let x: i32 = bar(S(default()));
1561 S(default())
1562}
1563"#, true),
1564 @r###"
1565 [27; 28) 'x': impl Trait<u32>
1566 [47; 58) '{ loop {} }': ()
1567 [49; 56) 'loop {}': !
1568 [54; 56) '{}': ()
1569 [69; 70) 'x': impl Trait<T>
1570 [92; 103) '{ loop {} }': T
1571 [94; 101) 'loop {}': !
1572 [99; 101) '{}': ()
1573 [172; 183) '{ loop {} }': T
1574 [174; 181) 'loop {}': !
1575 [179; 181) '{}': ()
1576 [214; 310) '{ ...t()) }': S<i32>
1577 [224; 226) 's1': S<u32>
1578 [229; 230) 'S': S<u32>(T) -> S<T>
1579 [229; 241) 'S(default())': S<u32>
1580 [231; 238) 'default': fn default<u32>() -> T
1581 [231; 240) 'default()': u32
1582 [247; 250) 'foo': fn foo(impl Trait<u32>) -> ()
1583 [247; 254) 'foo(s1)': ()
1584 [251; 253) 's1': S<u32>
1585 [264; 265) 'x': i32
1586 [273; 276) 'bar': fn bar<i32>(impl Trait<T>) -> T
1587 [273; 290) 'bar(S(...lt()))': i32
1588 [277; 278) 'S': S<i32>(T) -> S<T>
1589 [277; 289) 'S(default())': S<i32>
1590 [279; 286) 'default': fn default<i32>() -> T
1591 [279; 288) 'default()': i32
1592 [296; 297) 'S': S<i32>(T) -> S<T>
1593 [296; 308) 'S(default())': S<i32>
1594 [298; 305) 'default': fn default<i32>() -> T
1595 [298; 307) 'default()': i32
1596 "###
1597 );
1598}