]> git.lizzy.rs Git - rust.git/blob - crates/hir_ty/src/tests/method_resolution.rs
11bdfa85a9f2746eba7d31c3f5340e385653205c
[rust.git] / crates / hir_ty / src / tests / method_resolution.rs
1 use expect_test::expect;
2
3 use crate::tests::check;
4
5 use super::{check_infer, check_types};
6
7 #[test]
8 fn infer_slice_method() {
9     check_infer(
10         r#"
11         #[lang = "slice"]
12         impl<T> [T] {
13             fn foo(&self) -> T {
14                 loop {}
15             }
16         }
17
18         #[lang = "slice_alloc"]
19         impl<T> [T] {}
20
21         fn test(x: &[u8]) {
22             <[_]>::foo(x);
23         }
24         "#,
25         expect![[r#"
26             44..48 'self': &[T]
27             55..78 '{     ...     }': T
28             65..72 'loop {}': !
29             70..72 '{}': ()
30             130..131 'x': &[u8]
31             140..162 '{     ...(x); }': ()
32             146..156 '<[_]>::foo': fn foo<u8>(&[u8]) -> u8
33             146..159 '<[_]>::foo(x)': u8
34             157..158 'x': &[u8]
35         "#]],
36     );
37 }
38
39 #[test]
40 fn infer_array_inherent_impl() {
41     check_types(
42         r#"
43         #[lang = "array"]
44         impl<T, const N: usize> [T; N] {
45             fn foo(&self) -> T {
46                 loop {}
47             }
48         }
49         fn test(x: &[u8; 0]) {
50             <[_; 0]>::foo(x);
51           //^^^^^^^^^^^^^^^^ u8
52         }
53         "#,
54     );
55 }
56
57 #[test]
58 fn infer_associated_method_struct() {
59     check_infer(
60         r#"
61         struct A { x: u32 }
62
63         impl A {
64             fn new() -> A {
65                 A { x: 0 }
66             }
67         }
68         fn test() {
69             let a = A::new();
70             a.x;
71         }
72         "#,
73         expect![[r#"
74             48..74 '{     ...     }': A
75             58..68 'A { x: 0 }': A
76             65..66 '0': u32
77             87..121 '{     ...a.x; }': ()
78             97..98 'a': A
79             101..107 'A::new': fn new() -> A
80             101..109 'A::new()': A
81             115..116 'a': A
82             115..118 'a.x': u32
83         "#]],
84     );
85 }
86
87 #[test]
88 fn infer_associated_method_struct_in_local_scope() {
89     check_infer(
90         r#"
91         fn mismatch() {
92             struct A;
93
94             impl A {
95                 fn from(_: i32, _: i32) -> Self {
96                     A
97                 }
98             }
99
100             let _a = A::from(1, 2);
101         }
102         "#,
103         expect![[r#"
104             14..146 '{     ... 2); }': ()
105             125..127 '_a': A
106             130..137 'A::from': fn from(i32, i32) -> A
107             130..143 'A::from(1, 2)': A
108             138..139 '1': i32
109             141..142 '2': i32
110             60..61 '_': i32
111             68..69 '_': i32
112             84..109 '{     ...     }': A
113             98..99 'A': A
114         "#]],
115     );
116 }
117
118 #[test]
119 fn infer_associated_method_enum() {
120     check_infer(
121         r#"
122         enum A { B, C }
123
124         impl A {
125             pub fn b() -> A {
126                 A::B
127             }
128             pub fn c() -> A {
129                 A::C
130             }
131         }
132         fn test() {
133             let a = A::b();
134             a;
135             let c = A::c();
136             c;
137         }
138         "#,
139         expect![[r#"
140             46..66 '{     ...     }': A
141             56..60 'A::B': A
142             87..107 '{     ...     }': A
143             97..101 'A::C': A
144             120..177 '{     ...  c; }': ()
145             130..131 'a': A
146             134..138 'A::b': fn b() -> A
147             134..140 'A::b()': A
148             146..147 'a': A
149             157..158 'c': A
150             161..165 'A::c': fn c() -> A
151             161..167 'A::c()': A
152             173..174 'c': A
153         "#]],
154     );
155 }
156
157 #[test]
158 fn infer_associated_method_with_modules() {
159     check_infer(
160         r#"
161         mod a {
162             struct A;
163             impl A { pub fn thing() -> A { A {} }}
164         }
165
166         mod b {
167             struct B;
168             impl B { pub fn thing() -> u32 { 99 }}
169
170             mod c {
171                 struct C;
172                 impl C { pub fn thing() -> C { C {} }}
173             }
174         }
175         use b::c;
176
177         fn test() {
178             let x = a::A::thing();
179             let y = b::B::thing();
180             let z = c::C::thing();
181         }
182         "#,
183         expect![[r#"
184             55..63 '{ A {} }': A
185             57..61 'A {}': A
186             125..131 '{ 99 }': u32
187             127..129 '99': u32
188             201..209 '{ C {} }': C
189             203..207 'C {}': C
190             240..324 '{     ...g(); }': ()
191             250..251 'x': A
192             254..265 'a::A::thing': fn thing() -> A
193             254..267 'a::A::thing()': A
194             277..278 'y': u32
195             281..292 'b::B::thing': fn thing() -> u32
196             281..294 'b::B::thing()': u32
197             304..305 'z': C
198             308..319 'c::C::thing': fn thing() -> C
199             308..321 'c::C::thing()': C
200         "#]],
201     );
202 }
203
204 #[test]
205 fn infer_associated_method_generics() {
206     check_infer(
207         r#"
208         struct Gen<T> {
209             val: T
210         }
211
212         impl<T> Gen<T> {
213             pub fn make(val: T) -> Gen<T> {
214                 Gen { val }
215             }
216         }
217
218         fn test() {
219             let a = Gen::make(0u32);
220         }
221         "#,
222         expect![[r#"
223             63..66 'val': T
224             81..108 '{     ...     }': Gen<T>
225             91..102 'Gen { val }': Gen<T>
226             97..100 'val': T
227             122..154 '{     ...32); }': ()
228             132..133 'a': Gen<u32>
229             136..145 'Gen::make': fn make<u32>(u32) -> Gen<u32>
230             136..151 'Gen::make(0u32)': Gen<u32>
231             146..150 '0u32': u32
232         "#]],
233     );
234 }
235
236 #[test]
237 fn infer_associated_method_generics_without_args() {
238     check_infer(
239         r#"
240         struct Gen<T> {
241             val: T
242         }
243
244         impl<T> Gen<T> {
245             pub fn make() -> Gen<T> {
246                 loop { }
247             }
248         }
249
250         fn test() {
251             let a = Gen::<u32>::make();
252         }
253         "#,
254         expect![[r#"
255             75..99 '{     ...     }': Gen<T>
256             85..93 'loop { }': !
257             90..93 '{ }': ()
258             113..148 '{     ...e(); }': ()
259             123..124 'a': Gen<u32>
260             127..143 'Gen::<...::make': fn make<u32>() -> Gen<u32>
261             127..145 'Gen::<...make()': Gen<u32>
262         "#]],
263     );
264 }
265
266 #[test]
267 fn infer_associated_method_generics_2_type_params_without_args() {
268     check_infer(
269         r#"
270         struct Gen<T, U> {
271             val: T,
272             val2: U,
273         }
274
275         impl<T> Gen<u32, T> {
276             pub fn make() -> Gen<u32,T> {
277                 loop { }
278             }
279         }
280
281         fn test() {
282             let a = Gen::<u32, u64>::make();
283         }
284         "#,
285         expect![[r#"
286             101..125 '{     ...     }': Gen<u32, T>
287             111..119 'loop { }': !
288             116..119 '{ }': ()
289             139..179 '{     ...e(); }': ()
290             149..150 'a': Gen<u32, u64>
291             153..174 'Gen::<...::make': fn make<u64>() -> Gen<u32, u64>
292             153..176 'Gen::<...make()': Gen<u32, u64>
293         "#]],
294     );
295 }
296
297 #[test]
298 fn cross_crate_associated_method_call() {
299     check_types(
300         r#"
301 //- /main.rs crate:main deps:other_crate
302 fn test() {
303     let x = other_crate::foo::S::thing();
304     x;
305 } //^ i128
306
307 //- /lib.rs crate:other_crate
308 pub mod foo {
309     pub struct S;
310     impl S {
311         fn thing() -> i128 { 0 }
312     }
313 }
314 "#,
315     );
316 }
317
318 #[test]
319 fn infer_trait_method_simple() {
320     // the trait implementation is intentionally incomplete -- it shouldn't matter
321     check_types(
322         r#"
323 trait Trait1 {
324     fn method(&self) -> u32;
325 }
326 struct S1;
327 impl Trait1 for S1 {}
328 trait Trait2 {
329     fn method(&self) -> i128;
330 }
331 struct S2;
332 impl Trait2 for S2 {}
333 fn test() {
334     S1.method();
335   //^^^^^^^^^^^ u32
336     S2.method(); // -> i128
337   //^^^^^^^^^^^ i128
338 }
339         "#,
340     );
341 }
342
343 #[test]
344 fn infer_trait_method_scoped() {
345     // the trait implementation is intentionally incomplete -- it shouldn't matter
346     check_types(
347         r#"
348 struct S;
349 mod foo {
350     pub trait Trait1 {
351         fn method(&self) -> u32;
352     }
353     impl Trait1 for super::S {}
354 }
355 mod bar {
356     pub trait Trait2 {
357         fn method(&self) -> i128;
358     }
359     impl Trait2 for super::S {}
360 }
361
362 mod foo_test {
363     use super::S;
364     use super::foo::Trait1;
365     fn test() {
366         S.method();
367       //^^^^^^^^^^ u32
368     }
369 }
370
371 mod bar_test {
372     use super::S;
373     use super::bar::Trait2;
374     fn test() {
375         S.method();
376       //^^^^^^^^^^ i128
377     }
378 }
379         "#,
380     );
381 }
382
383 #[test]
384 fn infer_trait_method_generic_1() {
385     // the trait implementation is intentionally incomplete -- it shouldn't matter
386     check_types(
387         r#"
388 trait Trait<T> {
389     fn method(&self) -> T;
390 }
391 struct S;
392 impl Trait<u32> for S {}
393 fn test() {
394     S.method();
395   //^^^^^^^^^^ u32
396 }
397         "#,
398     );
399 }
400
401 #[test]
402 fn infer_trait_method_generic_more_params() {
403     // the trait implementation is intentionally incomplete -- it shouldn't matter
404     check_types(
405         r#"
406 trait Trait<T1, T2, T3> {
407     fn method1(&self) -> (T1, T2, T3);
408     fn method2(&self) -> (T3, T2, T1);
409 }
410 struct S1;
411 impl Trait<u8, u16, u32> for S1 {}
412 struct S2;
413 impl<T> Trait<i8, i16, T> for S2 {}
414 fn test() {
415     S1.method1();
416   //^^^^^^^^^^^^ (u8, u16, u32)
417     S1.method2();
418   //^^^^^^^^^^^^ (u32, u16, u8)
419     S2.method1();
420   //^^^^^^^^^^^^ (i8, i16, {unknown})
421     S2.method2();
422   //^^^^^^^^^^^^ ({unknown}, i16, i8)
423 }
424         "#,
425     );
426 }
427
428 #[test]
429 fn infer_trait_method_generic_2() {
430     // the trait implementation is intentionally incomplete -- it shouldn't matter
431     check_types(
432         r#"
433 trait Trait<T> {
434     fn method(&self) -> T;
435 }
436 struct S<T>(T);
437 impl<U> Trait<U> for S<U> {}
438 fn test() {
439     S(1u32).method();
440   //^^^^^^^^^^^^^^^^ u32
441 }
442         "#,
443     );
444 }
445
446 #[test]
447 fn infer_trait_assoc_method() {
448     check_infer(
449         r#"
450         trait Default {
451             fn default() -> Self;
452         }
453         struct S;
454         impl Default for S {}
455         fn test() {
456             let s1: S = Default::default();
457             let s2 = S::default();
458             let s3 = <S as Default>::default();
459         }
460         "#,
461         expect![[r#"
462             86..192 '{     ...t(); }': ()
463             96..98 's1': S
464             104..120 'Defaul...efault': fn default<S>() -> S
465             104..122 'Defaul...ault()': S
466             132..134 's2': S
467             137..147 'S::default': fn default<S>() -> S
468             137..149 'S::default()': S
469             159..161 's3': S
470             164..187 '<S as ...efault': fn default<S>() -> S
471             164..189 '<S as ...ault()': S
472         "#]],
473     );
474 }
475
476 #[test]
477 fn infer_trait_assoc_method_generics_1() {
478     check_infer(
479         r#"
480         trait Trait<T> {
481             fn make() -> T;
482         }
483         struct S;
484         impl Trait<u32> for S {}
485         struct G<T>;
486         impl<T> Trait<T> for G<T> {}
487         fn test() {
488             let a = S::make();
489             let b = G::<u64>::make();
490             let c: f64 = G::make();
491         }
492         "#,
493         expect![[r#"
494             126..210 '{     ...e(); }': ()
495             136..137 'a': u32
496             140..147 'S::make': fn make<S, u32>() -> u32
497             140..149 'S::make()': u32
498             159..160 'b': u64
499             163..177 'G::<u64>::make': fn make<G<u64>, u64>() -> u64
500             163..179 'G::<u6...make()': u64
501             189..190 'c': f64
502             198..205 'G::make': fn make<G<f64>, f64>() -> f64
503             198..207 'G::make()': f64
504         "#]],
505     );
506 }
507
508 #[test]
509 fn infer_trait_assoc_method_generics_2() {
510     check_infer(
511         r#"
512         trait Trait<T> {
513             fn make<U>() -> (T, U);
514         }
515         struct S;
516         impl Trait<u32> for S {}
517         struct G<T>;
518         impl<T> Trait<T> for G<T> {}
519         fn test() {
520             let a = S::make::<i64>();
521             let b: (_, i64) = S::make();
522             let c = G::<u32>::make::<i64>();
523             let d: (u32, _) = G::make::<i64>();
524             let e: (u32, i64) = G::make();
525         }
526         "#,
527         expect![[r#"
528             134..312 '{     ...e(); }': ()
529             144..145 'a': (u32, i64)
530             148..162 'S::make::<i64>': fn make<S, u32, i64>() -> (u32, i64)
531             148..164 'S::mak...i64>()': (u32, i64)
532             174..175 'b': (u32, i64)
533             188..195 'S::make': fn make<S, u32, i64>() -> (u32, i64)
534             188..197 'S::make()': (u32, i64)
535             207..208 'c': (u32, i64)
536             211..232 'G::<u3...:<i64>': fn make<G<u32>, u32, i64>() -> (u32, i64)
537             211..234 'G::<u3...i64>()': (u32, i64)
538             244..245 'd': (u32, i64)
539             258..272 'G::make::<i64>': fn make<G<u32>, u32, i64>() -> (u32, i64)
540             258..274 'G::mak...i64>()': (u32, i64)
541             284..285 'e': (u32, i64)
542             300..307 'G::make': fn make<G<u32>, u32, i64>() -> (u32, i64)
543             300..309 'G::make()': (u32, i64)
544         "#]],
545     );
546 }
547
548 #[test]
549 fn infer_trait_assoc_method_generics_3() {
550     check_infer(
551         r#"
552         trait Trait<T> {
553             fn make() -> (Self, T);
554         }
555         struct S<T>;
556         impl Trait<i64> for S<i32> {}
557         fn test() {
558             let a = S::make();
559         }
560         "#,
561         expect![[r#"
562             100..126 '{     ...e(); }': ()
563             110..111 'a': (S<i32>, i64)
564             114..121 'S::make': fn make<S<i32>, i64>() -> (S<i32>, i64)
565             114..123 'S::make()': (S<i32>, i64)
566         "#]],
567     );
568 }
569
570 #[test]
571 fn infer_trait_assoc_method_generics_4() {
572     check_infer(
573         r#"
574         trait Trait<T> {
575             fn make() -> (Self, T);
576         }
577         struct S<T>;
578         impl Trait<i64> for S<u64> {}
579         impl Trait<i32> for S<u32> {}
580         fn test() {
581             let a: (S<u64>, _) = S::make();
582             let b: (_, i32) = S::make();
583         }
584         "#,
585         expect![[r#"
586             130..202 '{     ...e(); }': ()
587             140..141 'a': (S<u64>, i64)
588             157..164 'S::make': fn make<S<u64>, i64>() -> (S<u64>, i64)
589             157..166 'S::make()': (S<u64>, i64)
590             176..177 'b': (S<u32>, i32)
591             190..197 'S::make': fn make<S<u32>, i32>() -> (S<u32>, i32)
592             190..199 'S::make()': (S<u32>, i32)
593         "#]],
594     );
595 }
596
597 #[test]
598 fn infer_trait_assoc_method_generics_5() {
599     check_infer(
600         r#"
601         trait Trait<T> {
602             fn make<U>() -> (Self, T, U);
603         }
604         struct S<T>;
605         impl Trait<i64> for S<u64> {}
606         fn test() {
607             let a = <S as Trait<i64>>::make::<u8>();
608             let b: (S<u64>, _, _) = Trait::<i64>::make::<u8>();
609         }
610         "#,
611         expect![[r#"
612             106..210 '{     ...>(); }': ()
613             116..117 'a': (S<u64>, i64, u8)
614             120..149 '<S as ...::<u8>': fn make<S<u64>, i64, u8>() -> (S<u64>, i64, u8)
615             120..151 '<S as ...<u8>()': (S<u64>, i64, u8)
616             161..162 'b': (S<u64>, i64, u8)
617             181..205 'Trait:...::<u8>': fn make<S<u64>, i64, u8>() -> (S<u64>, i64, u8)
618             181..207 'Trait:...<u8>()': (S<u64>, i64, u8)
619         "#]],
620     );
621 }
622
623 #[test]
624 fn infer_call_trait_method_on_generic_param_1() {
625     check_infer(
626         r#"
627         trait Trait {
628             fn method(&self) -> u32;
629         }
630         fn test<T: Trait>(t: T) {
631             t.method();
632         }
633         "#,
634         expect![[r#"
635             29..33 'self': &Self
636             63..64 't': T
637             69..88 '{     ...d(); }': ()
638             75..76 't': T
639             75..85 't.method()': u32
640         "#]],
641     );
642 }
643
644 #[test]
645 fn infer_call_trait_method_on_generic_param_2() {
646     check_infer(
647         r#"
648         trait Trait<T> {
649             fn method(&self) -> T;
650         }
651         fn test<U, T: Trait<U>>(t: T) {
652             t.method();
653         }
654         "#,
655         expect![[r#"
656             32..36 'self': &Self
657             70..71 't': T
658             76..95 '{     ...d(); }': ()
659             82..83 't': T
660             82..92 't.method()': U
661         "#]],
662     );
663 }
664
665 #[test]
666 fn infer_with_multiple_trait_impls() {
667     check_infer(
668         r#"
669         trait Into<T> {
670             fn into(self) -> T;
671         }
672         struct S;
673         impl Into<u32> for S {}
674         impl Into<u64> for S {}
675         fn test() {
676             let x: u32 = S.into();
677             let y: u64 = S.into();
678             let z = Into::<u64>::into(S);
679         }
680         "#,
681         expect![[r#"
682             28..32 'self': Self
683             110..201 '{     ...(S); }': ()
684             120..121 'x': u32
685             129..130 'S': S
686             129..137 'S.into()': u32
687             147..148 'y': u64
688             156..157 'S': S
689             156..164 'S.into()': u64
690             174..175 'z': u64
691             178..195 'Into::...::into': fn into<S, u64>(S) -> u64
692             178..198 'Into::...nto(S)': u64
693             196..197 'S': S
694         "#]],
695     );
696 }
697
698 #[test]
699 fn method_resolution_unify_impl_self_type() {
700     check_types(
701         r#"
702 struct S<T>;
703 impl S<u32> { fn foo(&self) -> u8 { 0 } }
704 impl S<i32> { fn foo(&self) -> i8 { 0 } }
705 fn test() { (S::<u32>.foo(), S::<i32>.foo()); }
706           //^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ (u8, i8)
707 "#,
708     );
709 }
710
711 #[test]
712 fn method_resolution_trait_before_autoref() {
713     check_types(
714         r#"
715 trait Trait { fn foo(self) -> u128; }
716 struct S;
717 impl S { fn foo(&self) -> i8 { 0 } }
718 impl Trait for S { fn foo(self) -> u128 { 0 } }
719 fn test() { S.foo(); }
720           //^^^^^^^ u128
721 "#,
722     );
723 }
724
725 #[test]
726 fn method_resolution_by_value_before_autoref() {
727     check_types(
728         r#"
729 trait Clone { fn clone(&self) -> Self; }
730 struct S;
731 impl Clone for S {}
732 impl Clone for &S {}
733 fn test() { (S.clone(), (&S).clone(), (&&S).clone()); }
734           //^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ (S, S, &S)
735 "#,
736     );
737 }
738
739 #[test]
740 fn method_resolution_trait_before_autoderef() {
741     check_types(
742         r#"
743 trait Trait { fn foo(self) -> u128; }
744 struct S;
745 impl S { fn foo(self) -> i8 { 0 } }
746 impl Trait for &S { fn foo(self) -> u128 { 0 } }
747 fn test() { (&S).foo(); }
748           //^^^^^^^^^^ u128
749 "#,
750     );
751 }
752
753 #[test]
754 fn method_resolution_impl_before_trait() {
755     check_types(
756         r#"
757 trait Trait { fn foo(self) -> u128; }
758 struct S;
759 impl S { fn foo(self) -> i8 { 0 } }
760 impl Trait for S { fn foo(self) -> u128 { 0 } }
761 fn test() { S.foo(); }
762           //^^^^^^^ i8
763 "#,
764     );
765 }
766
767 #[test]
768 fn method_resolution_impl_ref_before_trait() {
769     check_types(
770         r#"
771 trait Trait { fn foo(self) -> u128; }
772 struct S;
773 impl S { fn foo(&self) -> i8 { 0 } }
774 impl Trait for &S { fn foo(self) -> u128 { 0 } }
775 fn test() { S.foo(); }
776           //^^^^^^^ i8
777 "#,
778     );
779 }
780
781 #[test]
782 fn method_resolution_trait_autoderef() {
783     check_types(
784         r#"
785 trait Trait { fn foo(self) -> u128; }
786 struct S;
787 impl Trait for S { fn foo(self) -> u128 { 0 } }
788 fn test() { (&S).foo(); }
789           //^^^^^^^^^^ u128
790 "#,
791     );
792 }
793
794 #[test]
795 fn method_resolution_unsize_array() {
796     check_types(
797         r#"
798 //- minicore: slice
799 fn test() {
800     let a = [1, 2, 3];
801     a.len();
802 } //^^^^^^^ usize
803 "#,
804     );
805 }
806
807 #[test]
808 fn method_resolution_trait_from_prelude() {
809     check_types(
810         r#"
811 //- /main.rs crate:main deps:core
812 struct S;
813 impl Clone for S {}
814
815 fn test() {
816     S.clone();
817   //^^^^^^^^^ S
818 }
819
820 //- /lib.rs crate:core
821 pub mod prelude {
822     pub mod rust_2018 {
823         pub trait Clone {
824             fn clone(&self) -> Self;
825         }
826     }
827 }
828 "#,
829     );
830 }
831
832 #[test]
833 fn method_resolution_where_clause_for_unknown_trait() {
834     // The blanket impl currently applies because we ignore the unresolved where clause
835     check_types(
836         r#"
837 trait Trait { fn foo(self) -> u128; }
838 struct S;
839 impl<T> Trait for T where T: UnknownTrait {}
840 fn test() { (&S).foo(); }
841           //^^^^^^^^^^ u128
842 "#,
843     );
844 }
845
846 #[test]
847 fn method_resolution_where_clause_not_met() {
848     // The blanket impl shouldn't apply because we can't prove S: Clone
849     // This is also to make sure that we don't resolve to the foo method just
850     // because that's the only method named foo we can find, which would make
851     // the below tests not work
852     check_types(
853         r#"
854 trait Clone {}
855 trait Trait { fn foo(self) -> u128; }
856 struct S;
857 impl<T> Trait for T where T: Clone {}
858 fn test() { (&S).foo(); }
859           //^^^^^^^^^^ {unknown}
860 "#,
861     );
862 }
863
864 #[test]
865 fn method_resolution_where_clause_inline_not_met() {
866     // The blanket impl shouldn't apply because we can't prove S: Clone
867     check_types(
868         r#"
869 trait Clone {}
870 trait Trait { fn foo(self) -> u128; }
871 struct S;
872 impl<T: Clone> Trait for T {}
873 fn test() { (&S).foo(); }
874           //^^^^^^^^^^ {unknown}
875 "#,
876     );
877 }
878
879 #[test]
880 fn method_resolution_where_clause_1() {
881     check_types(
882         r#"
883 trait Clone {}
884 trait Trait { fn foo(self) -> u128; }
885 struct S;
886 impl Clone for S {}
887 impl<T> Trait for T where T: Clone {}
888 fn test() { S.foo(); }
889           //^^^^^^^ u128
890 "#,
891     );
892 }
893
894 #[test]
895 fn method_resolution_where_clause_2() {
896     check_types(
897         r#"
898 trait Into<T> { fn into(self) -> T; }
899 trait From<T> { fn from(other: T) -> Self; }
900 struct S1;
901 struct S2;
902 impl From<S2> for S1 {}
903 impl<T, U> Into<U> for T where U: From<T> {}
904 fn test() { S2.into(); }
905           //^^^^^^^^^ {unknown}
906 "#,
907     );
908 }
909
910 #[test]
911 fn method_resolution_where_clause_inline() {
912     check_types(
913         r#"
914 trait Into<T> { fn into(self) -> T; }
915 trait From<T> { fn from(other: T) -> Self; }
916 struct S1;
917 struct S2;
918 impl From<S2> for S1 {}
919 impl<T, U: From<T>> Into<U> for T {}
920 fn test() { S2.into(); }
921           //^^^^^^^^^ {unknown}
922 "#,
923     );
924 }
925
926 #[test]
927 fn method_resolution_overloaded_method() {
928     cov_mark::check!(impl_self_type_match_without_receiver);
929     check_types(
930         r#"
931 struct Wrapper<T>(T);
932 struct Foo<T>(T);
933 struct Bar<T>(T);
934
935 impl<T> Wrapper<Foo<T>> {
936     pub fn new(foo_: T) -> Self {
937         Wrapper(Foo(foo_))
938     }
939 }
940
941 impl<T> Wrapper<Bar<T>> {
942     pub fn new(bar_: T) -> Self {
943         Wrapper(Bar(bar_))
944     }
945 }
946
947 fn main() {
948     let a = Wrapper::<Foo<f32>>::new(1.0);
949     let b = Wrapper::<Bar<f32>>::new(1.0);
950     (a, b);
951   //^^^^^^ (Wrapper<Foo<f32>>, Wrapper<Bar<f32>>)
952 }
953 "#,
954     );
955 }
956
957 #[test]
958 fn method_resolution_encountering_fn_type() {
959     check_types(
960         r#"
961 //- /main.rs
962 fn foo() {}
963 trait FnOnce { fn call(self); }
964 fn test() { foo.call(); }
965           //^^^^^^^^^^ {unknown}
966 "#,
967     );
968 }
969
970 #[test]
971 fn super_trait_impl_return_trait_method_resolution() {
972     check_infer(
973         r#"
974         //- minicore: sized
975         trait Base {
976             fn foo(self) -> usize;
977         }
978
979         trait Super : Base {}
980
981         fn base1() -> impl Base { loop {} }
982         fn super1() -> impl Super { loop {} }
983
984         fn test(base2: impl Base, super2: impl Super) {
985             base1().foo();
986             super1().foo();
987             base2.foo();
988             super2.foo();
989         }
990         "#,
991         expect![[r#"
992             24..28 'self': Self
993             90..101 '{ loop {} }': !
994             92..99 'loop {}': !
995             97..99 '{}': ()
996             128..139 '{ loop {} }': !
997             130..137 'loop {}': !
998             135..137 '{}': ()
999             149..154 'base2': impl Base
1000             167..173 'super2': impl Super
1001             187..264 '{     ...o(); }': ()
1002             193..198 'base1': fn base1() -> impl Base
1003             193..200 'base1()': impl Base
1004             193..206 'base1().foo()': usize
1005             212..218 'super1': fn super1() -> impl Super
1006             212..220 'super1()': impl Super
1007             212..226 'super1().foo()': usize
1008             232..237 'base2': impl Base
1009             232..243 'base2.foo()': usize
1010             249..255 'super2': impl Super
1011             249..261 'super2.foo()': usize
1012         "#]],
1013     );
1014 }
1015
1016 #[test]
1017 fn method_resolution_non_parameter_type() {
1018     check_types(
1019         r#"
1020 mod a {
1021     pub trait Foo {
1022         fn foo(&self);
1023     }
1024 }
1025
1026 struct Wrapper<T>(T);
1027 fn foo<T>(t: Wrapper<T>)
1028 where
1029     Wrapper<T>: a::Foo,
1030 {
1031     t.foo();
1032 } //^^^^^^^ {unknown}
1033 "#,
1034     );
1035 }
1036
1037 #[test]
1038 fn method_resolution_3373() {
1039     check_types(
1040         r#"
1041 struct A<T>(T);
1042
1043 impl A<i32> {
1044     fn from(v: i32) -> A<i32> { A(v) }
1045 }
1046
1047 fn main() {
1048     A::from(3);
1049 } //^^^^^^^^^^ A<i32>
1050 "#,
1051     );
1052 }
1053
1054 #[test]
1055 fn method_resolution_slow() {
1056     // this can get quite slow if we set the solver size limit too high
1057     check_types(
1058         r#"
1059 trait SendX {}
1060
1061 struct S1; impl SendX for S1 {}
1062 struct S2; impl SendX for S2 {}
1063 struct U1;
1064
1065 trait Trait { fn method(self); }
1066
1067 struct X1<A, B> {}
1068 impl<A, B> SendX for X1<A, B> where A: SendX, B: SendX {}
1069
1070 struct S<B, C> {}
1071
1072 trait FnX {}
1073
1074 impl<B, C> Trait for S<B, C> where C: FnX, B: SendX {}
1075
1076 fn test() { (S {}).method(); }
1077           //^^^^^^^^^^^^^^^ ()
1078 "#,
1079     );
1080 }
1081
1082 #[test]
1083 fn dyn_trait_super_trait_not_in_scope() {
1084     check_infer(
1085         r#"
1086         mod m {
1087             pub trait SuperTrait {
1088                 fn foo(&self) -> u32 { 0 }
1089             }
1090         }
1091         trait Trait: m::SuperTrait {}
1092
1093         struct S;
1094         impl m::SuperTrait for S {}
1095         impl Trait for S {}
1096
1097         fn test(d: &dyn Trait) {
1098             d.foo();
1099         }
1100         "#,
1101         expect![[r#"
1102             51..55 'self': &Self
1103             64..69 '{ 0 }': u32
1104             66..67 '0': u32
1105             176..177 'd': &dyn Trait
1106             191..207 '{     ...o(); }': ()
1107             197..198 'd': &dyn Trait
1108             197..204 'd.foo()': u32
1109         "#]],
1110     );
1111 }
1112
1113 #[test]
1114 fn method_resolution_foreign_opaque_type() {
1115     check_infer(
1116         r#"
1117 extern "C" {
1118     type S;
1119     fn f() -> &'static S;
1120 }
1121
1122 impl S {
1123     fn foo(&self) -> bool {
1124         true
1125     }
1126 }
1127
1128 fn test() {
1129     let s = unsafe { f() };
1130     s.foo();
1131 }
1132 "#,
1133         expect![[r#"
1134             75..79 'self': &S
1135             89..109 '{     ...     }': bool
1136             99..103 'true': bool
1137             123..167 '{     ...o(); }': ()
1138             133..134 's': &S
1139             137..151 'unsafe { f() }': &S
1140             137..151 'unsafe { f() }': &S
1141             146..147 'f': fn f() -> &S
1142             146..149 'f()': &S
1143             157..158 's': &S
1144             157..164 's.foo()': bool
1145         "#]],
1146     );
1147 }
1148
1149 #[test]
1150 fn method_with_allocator_box_self_type() {
1151     check_types(
1152         r#"
1153 struct Slice<T> {}
1154 struct Box<T, A> {}
1155
1156 impl<T> Slice<T> {
1157     pub fn into_vec<A>(self: Box<Self, A>) { }
1158 }
1159
1160 fn main() {
1161     let foo: Slice<u32>;
1162     foo.into_vec(); // we shouldn't crash on this at least
1163 } //^^^^^^^^^^^^^^ {unknown}
1164 "#,
1165     );
1166 }
1167
1168 #[test]
1169 fn method_on_dyn_impl() {
1170     check_types(
1171         r#"
1172 trait Foo {}
1173
1174 impl Foo for u32 {}
1175 impl dyn Foo + '_ {
1176     pub fn dyn_foo(&self) -> u32 {
1177         0
1178     }
1179 }
1180
1181 fn main() {
1182     let f = &42u32 as &dyn Foo;
1183     f.dyn_foo();
1184  // ^^^^^^^^^^^ u32
1185 }
1186 "#,
1187     );
1188 }
1189
1190 #[test]
1191 fn autoderef_visibility_field() {
1192     check(
1193         r#"
1194 //- minicore: deref
1195 mod a {
1196     pub struct Foo(pub char);
1197     pub struct Bar(i32);
1198     impl Bar {
1199         pub fn new() -> Self {
1200             Self(0)
1201         }
1202     }
1203     impl core::ops::Deref for Bar {
1204         type Target = Foo;
1205         fn deref(&self) -> &Foo {
1206             &Foo('z')
1207         }
1208     }
1209 }
1210 mod b {
1211     fn foo() {
1212         let x = super::a::Bar::new().0;
1213              // ^^^^^^^^^^^^^^^^^^^^ adjustments: Deref(Some(OverloadedDeref(Not)))
1214              // ^^^^^^^^^^^^^^^^^^^^^^ type: char
1215     }
1216 }
1217 "#,
1218     )
1219 }
1220
1221 #[test]
1222 fn autoderef_visibility_method() {
1223     cov_mark::check!(autoderef_candidate_not_visible);
1224     check(
1225         r#"
1226 //- minicore: deref
1227 mod a {
1228     pub struct Foo(pub char);
1229     impl Foo {
1230         pub fn mango(&self) -> char {
1231             self.0
1232         }
1233     }
1234     pub struct Bar(i32);
1235     impl Bar {
1236         pub fn new() -> Self {
1237             Self(0)
1238         }
1239         fn mango(&self) -> i32 {
1240             self.0
1241         }
1242     }
1243     impl core::ops::Deref for Bar {
1244         type Target = Foo;
1245         fn deref(&self) -> &Foo {
1246             &Foo('z')
1247         }
1248     }
1249 }
1250 mod b {
1251     fn foo() {
1252         let x = super::a::Bar::new().mango();
1253              // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type: char
1254     }
1255 }
1256 "#,
1257     )
1258 }
1259
1260 #[test]
1261 fn trait_impl_in_unnamed_const() {
1262     check_types(
1263         r#"
1264 struct S;
1265
1266 trait Tr {
1267     fn method(&self) -> u16;
1268 }
1269
1270 const _: () = {
1271     impl Tr for S {}
1272 };
1273
1274 fn f() {
1275     S.method();
1276   //^^^^^^^^^^ u16
1277 }
1278     "#,
1279     );
1280 }
1281
1282 #[test]
1283 fn trait_impl_in_synstructure_const() {
1284     check_types(
1285         r#"
1286 struct S;
1287
1288 trait Tr {
1289     fn method(&self) -> u16;
1290 }
1291
1292 const _DERIVE_Tr_: () = {
1293     impl Tr for S {}
1294 };
1295
1296 fn f() {
1297     S.method();
1298   //^^^^^^^^^^ u16
1299 }
1300     "#,
1301     );
1302 }
1303
1304 #[test]
1305 fn inherent_impl_in_unnamed_const() {
1306     check_types(
1307         r#"
1308 struct S;
1309
1310 const _: () = {
1311     impl S {
1312         fn method(&self) -> u16 { 0 }
1313
1314         pub(super) fn super_method(&self) -> u16 { 0 }
1315
1316         pub(crate) fn crate_method(&self) -> u16 { 0 }
1317
1318         pub fn pub_method(&self) -> u16 { 0 }
1319     }
1320 };
1321
1322 fn f() {
1323     S.method();
1324   //^^^^^^^^^^ u16
1325
1326     S.super_method();
1327   //^^^^^^^^^^^^^^^^ u16
1328
1329     S.crate_method();
1330   //^^^^^^^^^^^^^^^^ u16
1331
1332     S.pub_method();
1333   //^^^^^^^^^^^^^^ u16
1334 }
1335     "#,
1336     );
1337 }
1338
1339 #[test]
1340 fn resolve_const_generic_array_methods() {
1341     check_types(
1342         r#"
1343 #[lang = "array"]
1344 impl<T, const N: usize> [T; N] {
1345     pub fn map<F, U>(self, f: F) -> [U; N]
1346     where
1347         F: FnMut(T) -> U,
1348     { loop {} }
1349 }
1350
1351 #[lang = "slice"]
1352 impl<T> [T] {
1353     pub fn map<F, U>(self, f: F) -> &[U]
1354     where
1355         F: FnMut(T) -> U,
1356     { loop {} }
1357 }
1358
1359 fn f() {
1360     let v = [1, 2].map::<_, usize>(|x| -> x * 2);
1361     v;
1362   //^ [usize; _]
1363 }
1364     "#,
1365     );
1366 }
1367
1368 #[test]
1369 fn skip_array_during_method_dispatch() {
1370     check_types(
1371         r#"
1372 //- /main2018.rs crate:main2018 deps:core
1373 use core::IntoIterator;
1374
1375 fn f() {
1376     let v = [4].into_iter();
1377     v;
1378   //^ &i32
1379
1380     let a = [0, 1].into_iter();
1381     a;
1382   //^ &i32
1383 }
1384
1385 //- /main2021.rs crate:main2021 deps:core edition:2021
1386 use core::IntoIterator;
1387
1388 fn f() {
1389     let v = [4].into_iter();
1390     v;
1391   //^ i32
1392
1393     let a = [0, 1].into_iter();
1394     a;
1395   //^ &i32
1396 }
1397
1398 //- /core.rs crate:core
1399 #[rustc_skip_array_during_method_dispatch]
1400 pub trait IntoIterator {
1401     type Out;
1402     fn into_iter(self) -> Self::Out;
1403 }
1404
1405 impl<T> IntoIterator for [T; 1] {
1406     type Out = T;
1407     fn into_iter(self) -> Self::Out { loop {} }
1408 }
1409 impl<'a, T> IntoIterator for &'a [T] {
1410     type Out = &'a T;
1411     fn into_iter(self) -> Self::Out { loop {} }
1412 }
1413     "#,
1414     );
1415 }
1416
1417 #[test]
1418 fn sized_blanket_impl() {
1419     check_infer(
1420         r#"
1421 //- minicore: sized
1422 trait Foo { fn foo() -> u8; }
1423 impl<T: Sized> Foo for T {}
1424 fn f<S: Sized, T, U: ?Sized>() {
1425     u32::foo;
1426     S::foo;
1427     T::foo;
1428     U::foo;
1429     <[u32]>::foo;
1430 }
1431 "#,
1432         expect![[r#"
1433             89..160 '{     ...foo; }': ()
1434             95..103 'u32::foo': fn foo<u32>() -> u8
1435             109..115 'S::foo': fn foo<S>() -> u8
1436             121..127 'T::foo': fn foo<T>() -> u8
1437             133..139 'U::foo': {unknown}
1438             145..157 '<[u32]>::foo': {unknown}
1439         "#]],
1440     );
1441 }
1442
1443 #[test]
1444 fn local_impl() {
1445     check_types(
1446         r#"
1447 fn main() {
1448     struct SomeStruct(i32);
1449
1450     impl SomeStruct {
1451         fn is_even(&self) -> bool {
1452             self.0 % 2 == 0
1453         }
1454     }
1455
1456     let o = SomeStruct(3);
1457     let is_even = o.is_even();
1458      // ^^^^^^^ bool
1459 }
1460     "#,
1461     );
1462 }