]> git.proxmox.com Git - rustc.git/blob - src/tools/rust-analyzer/crates/hir-ty/src/tests/method_resolution.rs
New upstream version 1.69.0+dfsg1
[rustc.git] / src / tools / rust-analyzer / 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_no_mismatches, check_types};
6
7 #[test]
8 fn infer_slice_method() {
9 check_types(
10 r#"
11 impl<T> [T] {
12 fn foo(&self) -> T {
13 loop {}
14 }
15 }
16
17 fn test(x: &[u8]) {
18 <[_]>::foo(x);
19 //^^^^^^^^^^^^^ u8
20 }
21 "#,
22 );
23 }
24
25 #[test]
26 fn cross_crate_primitive_method() {
27 check_types(
28 r#"
29 //- /main.rs crate:main deps:other_crate
30 fn test() {
31 let x = 1f32;
32 x.foo();
33 } //^^^^^^^ f32
34
35 //- /lib.rs crate:other_crate
36 mod foo {
37 impl f32 {
38 pub fn foo(self) -> f32 { 0. }
39 }
40 }
41 "#,
42 );
43 }
44
45 #[test]
46 fn infer_array_inherent_impl() {
47 check_types(
48 r#"
49 impl<T, const N: usize> [T; N] {
50 fn foo(&self) -> T {
51 loop {}
52 }
53 }
54 fn test(x: &[u8; 0]) {
55 <[_; 0]>::foo(x);
56 //^^^^^^^^^^^^^^^^ u8
57 }
58 "#,
59 );
60 }
61
62 #[test]
63 fn infer_associated_method_struct() {
64 check_infer(
65 r#"
66 struct A { x: u32 }
67
68 impl A {
69 fn new() -> A {
70 A { x: 0 }
71 }
72 }
73 fn test() {
74 let a = A::new();
75 a.x;
76 }
77 "#,
78 expect![[r#"
79 48..74 '{ ... }': A
80 58..68 'A { x: 0 }': A
81 65..66 '0': u32
82 87..121 '{ ...a.x; }': ()
83 97..98 'a': A
84 101..107 'A::new': fn new() -> A
85 101..109 'A::new()': A
86 115..116 'a': A
87 115..118 'a.x': u32
88 "#]],
89 );
90 }
91
92 #[test]
93 fn infer_associated_method_struct_in_local_scope() {
94 check_infer(
95 r#"
96 fn mismatch() {
97 struct A;
98
99 impl A {
100 fn from(_: i32, _: i32) -> Self {
101 A
102 }
103 }
104
105 let _a = A::from(1, 2);
106 }
107 "#,
108 expect![[r#"
109 14..146 '{ ... 2); }': ()
110 125..127 '_a': A
111 130..137 'A::from': fn from(i32, i32) -> A
112 130..143 'A::from(1, 2)': A
113 138..139 '1': i32
114 141..142 '2': i32
115 60..61 '_': i32
116 68..69 '_': i32
117 84..109 '{ ... }': A
118 98..99 'A': A
119 "#]],
120 );
121 }
122
123 #[test]
124 fn infer_associated_method_enum() {
125 check_infer(
126 r#"
127 enum A { B, C }
128
129 impl A {
130 pub fn b() -> A {
131 A::B
132 }
133 pub fn c() -> A {
134 A::C
135 }
136 }
137 fn test() {
138 let a = A::b();
139 a;
140 let c = A::c();
141 c;
142 }
143 "#,
144 expect![[r#"
145 46..66 '{ ... }': A
146 56..60 'A::B': A
147 87..107 '{ ... }': A
148 97..101 'A::C': A
149 120..177 '{ ... c; }': ()
150 130..131 'a': A
151 134..138 'A::b': fn b() -> A
152 134..140 'A::b()': A
153 146..147 'a': A
154 157..158 'c': A
155 161..165 'A::c': fn c() -> A
156 161..167 'A::c()': A
157 173..174 'c': A
158 "#]],
159 );
160 }
161
162 #[test]
163 fn infer_associated_method_with_modules() {
164 check_infer(
165 r#"
166 mod a {
167 pub struct A;
168 impl A { pub fn thing() -> A { A {} }}
169 }
170
171 mod b {
172 pub struct B;
173 impl B { pub fn thing() -> u32 { 99 }}
174
175 pub mod c {
176 pub struct C;
177 impl C { pub fn thing() -> C { C {} }}
178 }
179 }
180 use b::c;
181
182 fn test() {
183 let x = a::A::thing();
184 let y = b::B::thing();
185 let z = c::C::thing();
186 }
187 "#,
188 expect![[r#"
189 59..67 '{ A {} }': A
190 61..65 'A {}': A
191 133..139 '{ 99 }': u32
192 135..137 '99': u32
193 217..225 '{ C {} }': C
194 219..223 'C {}': C
195 256..340 '{ ...g(); }': ()
196 266..267 'x': A
197 270..281 'a::A::thing': fn thing() -> A
198 270..283 'a::A::thing()': A
199 293..294 'y': u32
200 297..308 'b::B::thing': fn thing() -> u32
201 297..310 'b::B::thing()': u32
202 320..321 'z': C
203 324..335 'c::C::thing': fn thing() -> C
204 324..337 'c::C::thing()': C
205 "#]],
206 );
207 }
208
209 #[test]
210 fn infer_associated_method_generics() {
211 check_infer(
212 r#"
213 struct Gen<T> {
214 val: T
215 }
216
217 impl<T> Gen<T> {
218 pub fn make(val: T) -> Gen<T> {
219 Gen { val }
220 }
221 }
222
223 fn test() {
224 let a = Gen::make(0u32);
225 }
226 "#,
227 expect![[r#"
228 63..66 'val': T
229 81..108 '{ ... }': Gen<T>
230 91..102 'Gen { val }': Gen<T>
231 97..100 'val': T
232 122..154 '{ ...32); }': ()
233 132..133 'a': Gen<u32>
234 136..145 'Gen::make': fn make<u32>(u32) -> Gen<u32>
235 136..151 'Gen::make(0u32)': Gen<u32>
236 146..150 '0u32': u32
237 "#]],
238 );
239 }
240
241 #[test]
242 fn infer_associated_method_generics_without_args() {
243 check_infer(
244 r#"
245 struct Gen<T> {
246 val: T
247 }
248
249 impl<T> Gen<T> {
250 pub fn make() -> Gen<T> {
251 loop { }
252 }
253 }
254
255 fn test() {
256 let a = Gen::<u32>::make();
257 }
258 "#,
259 expect![[r#"
260 75..99 '{ ... }': Gen<T>
261 85..93 'loop { }': !
262 90..93 '{ }': ()
263 113..148 '{ ...e(); }': ()
264 123..124 'a': Gen<u32>
265 127..143 'Gen::<...::make': fn make<u32>() -> Gen<u32>
266 127..145 'Gen::<...make()': Gen<u32>
267 "#]],
268 );
269 }
270
271 #[test]
272 fn infer_associated_method_generics_2_type_params_without_args() {
273 check_infer(
274 r#"
275 struct Gen<T, U> {
276 val: T,
277 val2: U,
278 }
279
280 impl<T> Gen<u32, T> {
281 pub fn make() -> Gen<u32,T> {
282 loop { }
283 }
284 }
285
286 fn test() {
287 let a = Gen::<u32, u64>::make();
288 }
289 "#,
290 expect![[r#"
291 101..125 '{ ... }': Gen<u32, T>
292 111..119 'loop { }': !
293 116..119 '{ }': ()
294 139..179 '{ ...e(); }': ()
295 149..150 'a': Gen<u32, u64>
296 153..174 'Gen::<...::make': fn make<u64>() -> Gen<u32, u64>
297 153..176 'Gen::<...make()': Gen<u32, u64>
298 "#]],
299 );
300 }
301
302 #[test]
303 fn cross_crate_associated_method_call() {
304 check_types(
305 r#"
306 //- /main.rs crate:main deps:other_crate
307 fn test() {
308 let x = other_crate::foo::S::thing();
309 x;
310 } //^ i128
311
312 //- /lib.rs crate:other_crate
313 pub mod foo {
314 pub struct S;
315 impl S {
316 pub fn thing() -> i128 { 0 }
317 }
318 }
319 "#,
320 );
321 }
322
323 #[test]
324 fn infer_trait_method_simple() {
325 // the trait implementation is intentionally incomplete -- it shouldn't matter
326 check_types(
327 r#"
328 trait Trait1 {
329 fn method(&self) -> u32;
330 }
331 struct S1;
332 impl Trait1 for S1 {}
333 trait Trait2 {
334 fn method(&self) -> i128;
335 }
336 struct S2;
337 impl Trait2 for S2 {}
338 fn test() {
339 S1.method();
340 //^^^^^^^^^^^ u32
341 S2.method(); // -> i128
342 //^^^^^^^^^^^ i128
343 }
344 "#,
345 );
346 }
347
348 #[test]
349 fn infer_trait_method_scoped() {
350 // the trait implementation is intentionally incomplete -- it shouldn't matter
351 check_types(
352 r#"
353 struct S;
354 mod foo {
355 pub trait Trait1 {
356 fn method(&self) -> u32;
357 }
358 impl Trait1 for super::S {}
359 }
360 mod bar {
361 pub trait Trait2 {
362 fn method(&self) -> i128;
363 }
364 impl Trait2 for super::S {}
365 }
366
367 mod foo_test {
368 use super::S;
369 use super::foo::Trait1;
370 fn test() {
371 S.method();
372 //^^^^^^^^^^ u32
373 }
374 }
375
376 mod bar_test {
377 use super::S;
378 use super::bar::Trait2;
379 fn test() {
380 S.method();
381 //^^^^^^^^^^ i128
382 }
383 }
384 "#,
385 );
386 }
387
388 #[test]
389 fn infer_trait_method_generic_1() {
390 // the trait implementation is intentionally incomplete -- it shouldn't matter
391 check_types(
392 r#"
393 trait Trait<T> {
394 fn method(&self) -> T;
395 }
396 struct S;
397 impl Trait<u32> for S {}
398 fn test() {
399 S.method();
400 //^^^^^^^^^^ u32
401 }
402 "#,
403 );
404 }
405
406 #[test]
407 fn infer_trait_method_generic_more_params() {
408 // the trait implementation is intentionally incomplete -- it shouldn't matter
409 check_types(
410 r#"
411 trait Trait<T1, T2, T3> {
412 fn method1(&self) -> (T1, T2, T3);
413 fn method2(&self) -> (T3, T2, T1);
414 }
415 struct S1;
416 impl Trait<u8, u16, u32> for S1 {}
417 struct S2;
418 impl<T> Trait<i8, i16, T> for S2 {}
419 fn test() {
420 S1.method1();
421 //^^^^^^^^^^^^ (u8, u16, u32)
422 S1.method2();
423 //^^^^^^^^^^^^ (u32, u16, u8)
424 S2.method1();
425 //^^^^^^^^^^^^ (i8, i16, {unknown})
426 S2.method2();
427 //^^^^^^^^^^^^ ({unknown}, i16, i8)
428 }
429 "#,
430 );
431 }
432
433 #[test]
434 fn infer_trait_method_generic_2() {
435 // the trait implementation is intentionally incomplete -- it shouldn't matter
436 check_types(
437 r#"
438 trait Trait<T> {
439 fn method(&self) -> T;
440 }
441 struct S<T>(T);
442 impl<U> Trait<U> for S<U> {}
443 fn test() {
444 S(1u32).method();
445 //^^^^^^^^^^^^^^^^ u32
446 }
447 "#,
448 );
449 }
450
451 #[test]
452 fn infer_trait_assoc_method() {
453 check_infer(
454 r#"
455 trait Default {
456 fn default() -> Self;
457 }
458 struct S;
459 impl Default for S {}
460 fn test() {
461 let s1: S = Default::default();
462 let s2 = S::default();
463 let s3 = <S as Default>::default();
464 }
465 "#,
466 expect![[r#"
467 86..192 '{ ...t(); }': ()
468 96..98 's1': S
469 104..120 'Defaul...efault': fn default<S>() -> S
470 104..122 'Defaul...ault()': S
471 132..134 's2': S
472 137..147 'S::default': fn default<S>() -> S
473 137..149 'S::default()': S
474 159..161 's3': S
475 164..187 '<S as ...efault': fn default<S>() -> S
476 164..189 '<S as ...ault()': S
477 "#]],
478 );
479 }
480
481 #[test]
482 fn infer_trait_assoc_method_generics_1() {
483 check_infer(
484 r#"
485 trait Trait<T> {
486 fn make() -> T;
487 }
488 struct S;
489 impl Trait<u32> for S {}
490 struct G<T>;
491 impl<T> Trait<T> for G<T> {}
492 fn test() {
493 let a = S::make();
494 let b = G::<u64>::make();
495 let c: f64 = G::make();
496 }
497 "#,
498 expect![[r#"
499 126..210 '{ ...e(); }': ()
500 136..137 'a': u32
501 140..147 'S::make': fn make<S, u32>() -> u32
502 140..149 'S::make()': u32
503 159..160 'b': u64
504 163..177 'G::<u64>::make': fn make<G<u64>, u64>() -> u64
505 163..179 'G::<u6...make()': u64
506 189..190 'c': f64
507 198..205 'G::make': fn make<G<f64>, f64>() -> f64
508 198..207 'G::make()': f64
509 "#]],
510 );
511 }
512
513 #[test]
514 fn infer_trait_assoc_method_generics_2() {
515 check_infer(
516 r#"
517 trait Trait<T> {
518 fn make<U>() -> (T, U);
519 }
520 struct S;
521 impl Trait<u32> for S {}
522 struct G<T>;
523 impl<T> Trait<T> for G<T> {}
524 fn test() {
525 let a = S::make::<i64>();
526 let b: (_, i64) = S::make();
527 let c = G::<u32>::make::<i64>();
528 let d: (u32, _) = G::make::<i64>();
529 let e: (u32, i64) = G::make();
530 }
531 "#,
532 expect![[r#"
533 134..312 '{ ...e(); }': ()
534 144..145 'a': (u32, i64)
535 148..162 'S::make::<i64>': fn make<S, u32, i64>() -> (u32, i64)
536 148..164 'S::mak...i64>()': (u32, i64)
537 174..175 'b': (u32, i64)
538 188..195 'S::make': fn make<S, u32, i64>() -> (u32, i64)
539 188..197 'S::make()': (u32, i64)
540 207..208 'c': (u32, i64)
541 211..232 'G::<u3...:<i64>': fn make<G<u32>, u32, i64>() -> (u32, i64)
542 211..234 'G::<u3...i64>()': (u32, i64)
543 244..245 'd': (u32, i64)
544 258..272 'G::make::<i64>': fn make<G<u32>, u32, i64>() -> (u32, i64)
545 258..274 'G::mak...i64>()': (u32, i64)
546 284..285 'e': (u32, i64)
547 300..307 'G::make': fn make<G<u32>, u32, i64>() -> (u32, i64)
548 300..309 'G::make()': (u32, i64)
549 "#]],
550 );
551 }
552
553 #[test]
554 fn infer_trait_assoc_method_generics_3() {
555 check_infer(
556 r#"
557 trait Trait<T> {
558 fn make() -> (Self, T);
559 }
560 struct S<T>;
561 impl Trait<i64> for S<i32> {}
562 fn test() {
563 let a = S::make();
564 }
565 "#,
566 expect![[r#"
567 100..126 '{ ...e(); }': ()
568 110..111 'a': (S<i32>, i64)
569 114..121 'S::make': fn make<S<i32>, i64>() -> (S<i32>, i64)
570 114..123 'S::make()': (S<i32>, i64)
571 "#]],
572 );
573 }
574
575 #[test]
576 fn infer_trait_assoc_method_generics_4() {
577 check_infer(
578 r#"
579 trait Trait<T> {
580 fn make() -> (Self, T);
581 }
582 struct S<T>;
583 impl Trait<i64> for S<u64> {}
584 impl Trait<i32> for S<u32> {}
585 fn test() {
586 let a: (S<u64>, _) = S::make();
587 let b: (_, i32) = S::make();
588 }
589 "#,
590 expect![[r#"
591 130..202 '{ ...e(); }': ()
592 140..141 'a': (S<u64>, i64)
593 157..164 'S::make': fn make<S<u64>, i64>() -> (S<u64>, i64)
594 157..166 'S::make()': (S<u64>, i64)
595 176..177 'b': (S<u32>, i32)
596 190..197 'S::make': fn make<S<u32>, i32>() -> (S<u32>, i32)
597 190..199 'S::make()': (S<u32>, i32)
598 "#]],
599 );
600 }
601
602 #[test]
603 fn infer_trait_assoc_method_generics_5() {
604 check_infer(
605 r#"
606 trait Trait<T> {
607 fn make<U>() -> (Self, T, U);
608 }
609 struct S<T>;
610 impl Trait<i64> for S<u64> {}
611 fn test() {
612 let a = <S as Trait<i64>>::make::<u8>();
613 let b: (S<u64>, _, _) = Trait::<i64>::make::<u8>();
614 }
615 "#,
616 expect![[r#"
617 106..210 '{ ...>(); }': ()
618 116..117 'a': (S<u64>, i64, u8)
619 120..149 '<S as ...::<u8>': fn make<S<u64>, i64, u8>() -> (S<u64>, i64, u8)
620 120..151 '<S as ...<u8>()': (S<u64>, i64, u8)
621 161..162 'b': (S<u64>, i64, u8)
622 181..205 'Trait:...::<u8>': fn make<S<u64>, i64, u8>() -> (S<u64>, i64, u8)
623 181..207 'Trait:...<u8>()': (S<u64>, i64, u8)
624 "#]],
625 );
626 }
627
628 #[test]
629 fn infer_call_trait_method_on_generic_param_1() {
630 check_infer(
631 r#"
632 trait Trait {
633 fn method(&self) -> u32;
634 }
635 fn test<T: Trait>(t: T) {
636 t.method();
637 }
638 "#,
639 expect![[r#"
640 29..33 'self': &Self
641 63..64 't': T
642 69..88 '{ ...d(); }': ()
643 75..76 't': T
644 75..85 't.method()': u32
645 "#]],
646 );
647 }
648
649 #[test]
650 fn infer_call_trait_method_on_generic_param_2() {
651 check_infer(
652 r#"
653 trait Trait<T> {
654 fn method(&self) -> T;
655 }
656 fn test<U, T: Trait<U>>(t: T) {
657 t.method();
658 }
659 "#,
660 expect![[r#"
661 32..36 'self': &Self
662 70..71 't': T
663 76..95 '{ ...d(); }': ()
664 82..83 't': T
665 82..92 't.method()': U
666 "#]],
667 );
668 }
669
670 #[test]
671 fn infer_with_multiple_trait_impls() {
672 check_infer(
673 r#"
674 trait Into<T> {
675 fn into(self) -> T;
676 }
677 struct S;
678 impl Into<u32> for S {}
679 impl Into<u64> for S {}
680 fn test() {
681 let x: u32 = S.into();
682 let y: u64 = S.into();
683 let z = Into::<u64>::into(S);
684 }
685 "#,
686 expect![[r#"
687 28..32 'self': Self
688 110..201 '{ ...(S); }': ()
689 120..121 'x': u32
690 129..130 'S': S
691 129..137 'S.into()': u32
692 147..148 'y': u64
693 156..157 'S': S
694 156..164 'S.into()': u64
695 174..175 'z': u64
696 178..195 'Into::...::into': fn into<S, u64>(S) -> u64
697 178..198 'Into::...nto(S)': u64
698 196..197 'S': S
699 "#]],
700 );
701 }
702
703 #[test]
704 fn method_resolution_unify_impl_self_type() {
705 check_types(
706 r#"
707 struct S<T>;
708 impl S<u32> { fn foo(&self) -> u8 { 0 } }
709 impl S<i32> { fn foo(&self) -> i8 { 0 } }
710 fn test() { (S::<u32>.foo(), S::<i32>.foo()); }
711 //^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ (u8, i8)
712 "#,
713 );
714 }
715
716 #[test]
717 fn method_resolution_trait_before_autoref() {
718 check_types(
719 r#"
720 trait Trait { fn foo(self) -> u128; }
721 struct S;
722 impl S { fn foo(&self) -> i8 { 0 } }
723 impl Trait for S { fn foo(self) -> u128 { 0 } }
724 fn test() { S.foo(); }
725 //^^^^^^^ u128
726 "#,
727 );
728 }
729
730 #[test]
731 fn method_resolution_by_value_before_autoref() {
732 check_types(
733 r#"
734 trait Clone { fn clone(&self) -> Self; }
735 struct S;
736 impl Clone for S {}
737 impl Clone for &S {}
738 fn test() { (S.clone(), (&S).clone(), (&&S).clone()); }
739 //^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ (S, S, &S)
740 "#,
741 );
742 }
743
744 #[test]
745 fn method_resolution_trait_before_autoderef() {
746 check_types(
747 r#"
748 trait Trait { fn foo(self) -> u128; }
749 struct S;
750 impl S { fn foo(self) -> i8 { 0 } }
751 impl Trait for &S { fn foo(self) -> u128 { 0 } }
752 fn test() { (&S).foo(); }
753 //^^^^^^^^^^ u128
754 "#,
755 );
756 }
757
758 #[test]
759 fn method_resolution_impl_before_trait() {
760 check_types(
761 r#"
762 trait Trait { fn foo(self) -> u128; }
763 struct S;
764 impl S { fn foo(self) -> i8 { 0 } }
765 impl Trait for S { fn foo(self) -> u128 { 0 } }
766 fn test() { S.foo(); }
767 //^^^^^^^ i8
768 "#,
769 );
770 }
771
772 #[test]
773 fn method_resolution_impl_ref_before_trait() {
774 check_types(
775 r#"
776 trait Trait { fn foo(self) -> u128; }
777 struct S;
778 impl S { fn foo(&self) -> i8 { 0 } }
779 impl Trait for &S { fn foo(self) -> u128 { 0 } }
780 fn test() { S.foo(); }
781 //^^^^^^^ i8
782 "#,
783 );
784 }
785
786 #[test]
787 fn method_resolution_trait_autoderef() {
788 check_types(
789 r#"
790 trait Trait { fn foo(self) -> u128; }
791 struct S;
792 impl Trait for S { fn foo(self) -> u128 { 0 } }
793 fn test() { (&S).foo(); }
794 //^^^^^^^^^^ u128
795 "#,
796 );
797 }
798
799 #[test]
800 fn method_resolution_unsize_array() {
801 check_types(
802 r#"
803 //- minicore: slice
804 fn test() {
805 let a = [1, 2, 3];
806 a.len();
807 } //^^^^^^^ usize
808 "#,
809 );
810 }
811
812 #[test]
813 fn method_resolution_trait_from_prelude() {
814 check_types(
815 r#"
816 //- /main.rs edition:2018 crate:main deps:core
817 struct S;
818 impl Clone for S {}
819
820 fn test() {
821 S.clone();
822 //^^^^^^^^^ S
823 }
824
825 //- /lib.rs crate:core
826 pub mod prelude {
827 pub mod rust_2018 {
828 pub trait Clone {
829 fn clone(&self) -> Self;
830 }
831 }
832 }
833 "#,
834 );
835 }
836
837 #[test]
838 fn method_resolution_where_clause_for_unknown_trait() {
839 // The blanket impl currently applies because we ignore the unresolved where clause
840 check_types(
841 r#"
842 trait Trait { fn foo(self) -> u128; }
843 struct S;
844 impl<T> Trait for T where T: UnknownTrait {}
845 fn test() { (&S).foo(); }
846 //^^^^^^^^^^ u128
847 "#,
848 );
849 }
850
851 #[test]
852 fn method_resolution_where_clause_not_met() {
853 // The blanket impl shouldn't apply because we can't prove S: Clone
854 // This is also to make sure that we don't resolve to the foo method just
855 // because that's the only method named foo we can find, which would make
856 // the below tests not work
857 check_types(
858 r#"
859 trait Clone {}
860 trait Trait { fn foo(self) -> u128; }
861 struct S;
862 impl<T> Trait for T where T: Clone {}
863 fn test() { (&S).foo(); }
864 //^^^^^^^^^^ {unknown}
865 "#,
866 );
867 }
868
869 #[test]
870 fn method_resolution_where_clause_inline_not_met() {
871 // The blanket impl shouldn't apply because we can't prove S: Clone
872 check_types(
873 r#"
874 trait Clone {}
875 trait Trait { fn foo(self) -> u128; }
876 struct S;
877 impl<T: Clone> Trait for T {}
878 fn test() { (&S).foo(); }
879 //^^^^^^^^^^ {unknown}
880 "#,
881 );
882 }
883
884 #[test]
885 fn method_resolution_where_clause_1() {
886 check_types(
887 r#"
888 trait Clone {}
889 trait Trait { fn foo(self) -> u128; }
890 struct S;
891 impl Clone for S {}
892 impl<T> Trait for T where T: Clone {}
893 fn test() { S.foo(); }
894 //^^^^^^^ u128
895 "#,
896 );
897 }
898
899 #[test]
900 fn method_resolution_where_clause_2() {
901 check_types(
902 r#"
903 trait Into<T> { fn into(self) -> T; }
904 trait From<T> { fn from(other: T) -> Self; }
905 struct S1;
906 struct S2;
907 impl From<S2> for S1 {}
908 impl<T, U> Into<U> for T where U: From<T> {}
909 fn test() { S2.into(); }
910 //^^^^^^^^^ {unknown}
911 "#,
912 );
913 }
914
915 #[test]
916 fn method_resolution_where_clause_inline() {
917 check_types(
918 r#"
919 trait Into<T> { fn into(self) -> T; }
920 trait From<T> { fn from(other: T) -> Self; }
921 struct S1;
922 struct S2;
923 impl From<S2> for S1 {}
924 impl<T, U: From<T>> Into<U> for T {}
925 fn test() { S2.into(); }
926 //^^^^^^^^^ {unknown}
927 "#,
928 );
929 }
930
931 #[test]
932 fn method_resolution_overloaded_method() {
933 check_types(
934 r#"
935 struct Wrapper<T>(T);
936 struct Foo<T>(T);
937 struct Bar<T>(T);
938
939 impl<T> Wrapper<Foo<T>> {
940 pub fn new(foo_: T) -> Self {
941 Wrapper(Foo(foo_))
942 }
943 }
944
945 impl<T> Wrapper<Bar<T>> {
946 pub fn new(bar_: T) -> Self {
947 Wrapper(Bar(bar_))
948 }
949 }
950
951 fn main() {
952 let a = Wrapper::<Foo<f32>>::new(1.0);
953 let b = Wrapper::<Bar<f32>>::new(1.0);
954 (a, b);
955 //^^^^^^ (Wrapper<Foo<f32>>, Wrapper<Bar<f32>>)
956 }
957 "#,
958 );
959 }
960
961 #[test]
962 fn method_resolution_overloaded_const() {
963 cov_mark::check!(const_candidate_self_type_mismatch);
964 check_types(
965 r#"
966 struct Wrapper<T>(T);
967 struct Foo<T>(T);
968 struct Bar<T>(T);
969
970 impl<T> Wrapper<Foo<T>> {
971 pub const VALUE: Foo<T>;
972 }
973
974 impl<T> Wrapper<Bar<T>> {
975 pub const VALUE: Bar<T>;
976 }
977
978 fn main() {
979 let a = Wrapper::<Foo<f32>>::VALUE;
980 let b = Wrapper::<Bar<f32>>::VALUE;
981 (a, b);
982 //^^^^^^ (Foo<f32>, Bar<f32>)
983 }
984 "#,
985 );
986 }
987
988 #[test]
989 fn explicit_fn_once_call_fn_item() {
990 check_types(
991 r#"
992 //- minicore: fn
993 fn foo() {}
994 fn test() { foo.call_once(); }
995 //^^^^^^^^^^^^^^^ ()
996 "#,
997 );
998 }
999
1000 #[test]
1001 fn super_trait_impl_return_trait_method_resolution() {
1002 check_infer(
1003 r#"
1004 //- minicore: sized
1005 trait Base {
1006 fn foo(self) -> usize;
1007 }
1008
1009 trait Super : Base {}
1010
1011 fn base1() -> impl Base { loop {} }
1012 fn super1() -> impl Super { loop {} }
1013
1014 fn test(base2: impl Base, super2: impl Super) {
1015 base1().foo();
1016 super1().foo();
1017 base2.foo();
1018 super2.foo();
1019 }
1020 "#,
1021 expect![[r#"
1022 24..28 'self': Self
1023 90..101 '{ loop {} }': !
1024 92..99 'loop {}': !
1025 97..99 '{}': ()
1026 128..139 '{ loop {} }': !
1027 130..137 'loop {}': !
1028 135..137 '{}': ()
1029 149..154 'base2': impl Base
1030 167..173 'super2': impl Super
1031 187..264 '{ ...o(); }': ()
1032 193..198 'base1': fn base1() -> impl Base
1033 193..200 'base1()': impl Base
1034 193..206 'base1().foo()': usize
1035 212..218 'super1': fn super1() -> impl Super
1036 212..220 'super1()': impl Super
1037 212..226 'super1().foo()': usize
1038 232..237 'base2': impl Base
1039 232..243 'base2.foo()': usize
1040 249..255 'super2': impl Super
1041 249..261 'super2.foo()': usize
1042 "#]],
1043 );
1044 }
1045
1046 #[test]
1047 fn method_resolution_non_parameter_type() {
1048 check_types(
1049 r#"
1050 mod a {
1051 pub trait Foo {
1052 fn foo(&self);
1053 }
1054 }
1055
1056 struct Wrapper<T>(T);
1057 fn foo<T>(t: Wrapper<T>)
1058 where
1059 Wrapper<T>: a::Foo,
1060 {
1061 t.foo();
1062 } //^^^^^^^ {unknown}
1063 "#,
1064 );
1065 }
1066
1067 #[test]
1068 fn method_resolution_3373() {
1069 check_types(
1070 r#"
1071 struct A<T>(T);
1072
1073 impl A<i32> {
1074 fn from(v: i32) -> A<i32> { A(v) }
1075 }
1076
1077 fn main() {
1078 A::from(3);
1079 } //^^^^^^^^^^ A<i32>
1080 "#,
1081 );
1082 }
1083
1084 #[test]
1085 fn method_resolution_slow() {
1086 // this can get quite slow if we set the solver size limit too high
1087 check_types(
1088 r#"
1089 trait SendX {}
1090
1091 struct S1; impl SendX for S1 {}
1092 struct S2; impl SendX for S2 {}
1093 struct U1;
1094
1095 trait Trait { fn method(self); }
1096
1097 struct X1<A, B> {}
1098 impl<A, B> SendX for X1<A, B> where A: SendX, B: SendX {}
1099
1100 struct S<B, C> {}
1101
1102 trait FnX {}
1103
1104 impl<B, C> Trait for S<B, C> where C: FnX, B: SendX {}
1105
1106 fn test() { (S {}).method(); }
1107 //^^^^^^^^^^^^^^^ ()
1108 "#,
1109 );
1110 }
1111
1112 #[test]
1113 fn dyn_trait_super_trait_not_in_scope() {
1114 check_infer(
1115 r#"
1116 mod m {
1117 pub trait SuperTrait {
1118 fn foo(&self) -> u32 { 0 }
1119 }
1120 }
1121 trait Trait: m::SuperTrait {}
1122
1123 struct S;
1124 impl m::SuperTrait for S {}
1125 impl Trait for S {}
1126
1127 fn test(d: &dyn Trait) {
1128 d.foo();
1129 }
1130 "#,
1131 expect![[r#"
1132 51..55 'self': &Self
1133 64..69 '{ 0 }': u32
1134 66..67 '0': u32
1135 176..177 'd': &dyn Trait
1136 191..207 '{ ...o(); }': ()
1137 197..198 'd': &dyn Trait
1138 197..204 'd.foo()': u32
1139 "#]],
1140 );
1141 }
1142
1143 #[test]
1144 fn method_resolution_foreign_opaque_type() {
1145 check_infer(
1146 r#"
1147 extern "C" {
1148 type S;
1149 fn f() -> &'static S;
1150 }
1151
1152 impl S {
1153 fn foo(&self) -> bool {
1154 true
1155 }
1156 }
1157
1158 fn test() {
1159 let s = unsafe { f() };
1160 s.foo();
1161 }
1162 "#,
1163 expect![[r#"
1164 75..79 'self': &S
1165 89..109 '{ ... }': bool
1166 99..103 'true': bool
1167 123..167 '{ ...o(); }': ()
1168 133..134 's': &S
1169 137..151 'unsafe { f() }': &S
1170 137..151 'unsafe { f() }': &S
1171 146..147 'f': fn f() -> &S
1172 146..149 'f()': &S
1173 157..158 's': &S
1174 157..164 's.foo()': bool
1175 "#]],
1176 );
1177 }
1178
1179 #[test]
1180 fn method_with_allocator_box_self_type() {
1181 check_types(
1182 r#"
1183 struct Slice<T> {}
1184 struct Box<T, A> {}
1185
1186 impl<T> Slice<T> {
1187 pub fn into_vec<A>(self: Box<Self, A>) { }
1188 }
1189
1190 fn main() {
1191 let foo: Slice<u32>;
1192 foo.into_vec(); // we shouldn't crash on this at least
1193 } //^^^^^^^^^^^^^^ {unknown}
1194 "#,
1195 );
1196 }
1197
1198 #[test]
1199 fn method_on_dyn_impl() {
1200 check_types(
1201 r#"
1202 trait Foo {}
1203
1204 impl Foo for u32 {}
1205 impl dyn Foo + '_ {
1206 pub fn dyn_foo(&self) -> u32 {
1207 0
1208 }
1209 }
1210
1211 fn main() {
1212 let f = &42u32 as &dyn Foo;
1213 f.dyn_foo();
1214 // ^^^^^^^^^^^ u32
1215 }
1216 "#,
1217 );
1218 }
1219
1220 #[test]
1221 fn dyn_trait_method_priority() {
1222 check_types(
1223 r#"
1224 //- minicore: from
1225 trait Trait {
1226 fn into(&self) -> usize { 0 }
1227 }
1228
1229 fn foo(a: &dyn Trait) {
1230 let _ = a.into();
1231 //^usize
1232 }
1233 "#,
1234 );
1235 }
1236
1237 #[test]
1238 fn trait_method_priority_for_placeholder_type() {
1239 check_types(
1240 r#"
1241 //- minicore: from
1242 trait Trait {
1243 fn into(&self) -> usize { 0 }
1244 }
1245
1246 fn foo<T: Trait>(a: &T) {
1247 let _ = a.into();
1248 //^usize
1249 }
1250 "#,
1251 );
1252 }
1253
1254 #[test]
1255 fn autoderef_visibility_field() {
1256 check(
1257 r#"
1258 //- minicore: deref
1259 mod a {
1260 pub struct Foo(pub char);
1261 pub struct Bar(i32);
1262 impl Bar {
1263 pub fn new() -> Self {
1264 Self(0)
1265 }
1266 }
1267 impl core::ops::Deref for Bar {
1268 type Target = Foo;
1269 fn deref(&self) -> &Foo {
1270 &Foo('z')
1271 }
1272 }
1273 }
1274 mod b {
1275 fn foo() {
1276 let x = super::a::Bar::new().0;
1277 // ^^^^^^^^^^^^^^^^^^^^ adjustments: Deref(Some(OverloadedDeref(Not)))
1278 // ^^^^^^^^^^^^^^^^^^^^^^ type: char
1279 }
1280 }
1281 "#,
1282 )
1283 }
1284
1285 #[test]
1286 fn autoderef_visibility_method() {
1287 cov_mark::check!(autoderef_candidate_not_visible);
1288 check(
1289 r#"
1290 //- minicore: deref
1291 mod a {
1292 pub struct Foo(pub char);
1293 impl Foo {
1294 pub fn mango(&self) -> char {
1295 self.0
1296 }
1297 }
1298 pub struct Bar(i32);
1299 impl Bar {
1300 pub fn new() -> Self {
1301 Self(0)
1302 }
1303 fn mango(&self) -> i32 {
1304 self.0
1305 }
1306 }
1307 impl core::ops::Deref for Bar {
1308 type Target = Foo;
1309 fn deref(&self) -> &Foo {
1310 &Foo('z')
1311 }
1312 }
1313 }
1314 mod b {
1315 fn foo() {
1316 let x = super::a::Bar::new().mango();
1317 // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type: char
1318 }
1319 }
1320 "#,
1321 )
1322 }
1323
1324 #[test]
1325 fn trait_vs_private_inherent_const() {
1326 cov_mark::check!(const_candidate_not_visible);
1327 check(
1328 r#"
1329 mod a {
1330 pub struct Foo;
1331 impl Foo {
1332 const VALUE: u32 = 2;
1333 }
1334 pub trait Trait {
1335 const VALUE: usize;
1336 }
1337 impl Trait for Foo {
1338 const VALUE: usize = 3;
1339 }
1340
1341 fn foo() {
1342 let x = Foo::VALUE;
1343 // ^^^^^^^^^^ type: u32
1344 }
1345 }
1346 use a::Trait;
1347 fn foo() {
1348 let x = a::Foo::VALUE;
1349 // ^^^^^^^^^^^^^ type: usize
1350 }
1351 "#,
1352 )
1353 }
1354
1355 #[test]
1356 fn trait_impl_in_unnamed_const() {
1357 check_types(
1358 r#"
1359 struct S;
1360
1361 trait Tr {
1362 fn method(&self) -> u16;
1363 }
1364
1365 const _: () = {
1366 impl Tr for S {}
1367 };
1368
1369 fn f() {
1370 S.method();
1371 //^^^^^^^^^^ u16
1372 }
1373 "#,
1374 );
1375 }
1376
1377 #[test]
1378 fn trait_impl_in_synstructure_const() {
1379 check_types(
1380 r#"
1381 struct S;
1382
1383 trait Tr {
1384 fn method(&self) -> u16;
1385 }
1386
1387 const _DERIVE_Tr_: () = {
1388 impl Tr for S {}
1389 };
1390
1391 fn f() {
1392 S.method();
1393 //^^^^^^^^^^ u16
1394 }
1395 "#,
1396 );
1397 }
1398
1399 #[test]
1400 fn inherent_impl_in_unnamed_const() {
1401 check_types(
1402 r#"
1403 struct S;
1404
1405 const _: () = {
1406 impl S {
1407 fn method(&self) -> u16 { 0 }
1408
1409 pub(super) fn super_method(&self) -> u16 { 0 }
1410
1411 pub(crate) fn crate_method(&self) -> u16 { 0 }
1412
1413 pub fn pub_method(&self) -> u16 { 0 }
1414 }
1415 };
1416
1417 fn f() {
1418 S.method();
1419 //^^^^^^^^^^ u16
1420
1421 S.super_method();
1422 //^^^^^^^^^^^^^^^^ u16
1423
1424 S.crate_method();
1425 //^^^^^^^^^^^^^^^^ u16
1426
1427 S.pub_method();
1428 //^^^^^^^^^^^^^^ u16
1429 }
1430 "#,
1431 );
1432 }
1433
1434 #[test]
1435 fn resolve_const_generic_array_methods() {
1436 check_types(
1437 r#"
1438 #[lang = "array"]
1439 impl<T, const N: usize> [T; N] {
1440 pub fn map<F, U>(self, f: F) -> [U; N]
1441 where
1442 F: FnMut(T) -> U,
1443 { loop {} }
1444 }
1445
1446 #[lang = "slice"]
1447 impl<T> [T] {
1448 pub fn map<F, U>(self, f: F) -> &[U]
1449 where
1450 F: FnMut(T) -> U,
1451 { loop {} }
1452 }
1453
1454 fn f() {
1455 let v = [1, 2].map::<_, usize>(|x| -> x * 2);
1456 v;
1457 //^ [usize; 2]
1458 }
1459 "#,
1460 );
1461 }
1462
1463 #[test]
1464 fn resolve_const_generic_method() {
1465 check_types(
1466 r#"
1467 struct Const<const N: usize>;
1468
1469 #[lang = "array"]
1470 impl<T, const N: usize> [T; N] {
1471 pub fn my_map<F, U, const X: usize>(self, f: F, c: Const<X>) -> [U; X]
1472 where
1473 F: FnMut(T) -> U,
1474 { loop {} }
1475 }
1476
1477 #[lang = "slice"]
1478 impl<T> [T] {
1479 pub fn my_map<F, const X: usize, U>(self, f: F, c: Const<X>) -> &[U]
1480 where
1481 F: FnMut(T) -> U,
1482 { loop {} }
1483 }
1484
1485 fn f<const C: usize, P>() {
1486 let v = [1, 2].my_map::<_, (), 12>(|x| -> x * 2, Const::<12>);
1487 v;
1488 //^ [(); 12]
1489 let v = [1, 2].my_map::<_, P, C>(|x| -> x * 2, Const::<C>);
1490 v;
1491 //^ [P; C]
1492 }
1493 "#,
1494 );
1495 }
1496
1497 #[test]
1498 fn const_generic_type_alias() {
1499 check_types(
1500 r#"
1501 struct Const<const N: usize>;
1502 type U2 = Const<2>;
1503 type U5 = Const<5>;
1504
1505 impl U2 {
1506 fn f(self) -> Const<12> {
1507 loop {}
1508 }
1509 }
1510
1511 impl U5 {
1512 fn f(self) -> Const<15> {
1513 loop {}
1514 }
1515 }
1516
1517 fn f(x: U2) {
1518 let y = x.f();
1519 //^ Const<12>
1520 }
1521 "#,
1522 );
1523 }
1524
1525 #[test]
1526 fn skip_array_during_method_dispatch() {
1527 check_types(
1528 r#"
1529 //- /main2018.rs crate:main2018 deps:core edition:2018
1530 use core::IntoIterator;
1531
1532 fn f() {
1533 let v = [4].into_iter();
1534 v;
1535 //^ &i32
1536
1537 let a = [0, 1].into_iter();
1538 a;
1539 //^ &i32
1540 }
1541
1542 //- /main2021.rs crate:main2021 deps:core edition:2021
1543 use core::IntoIterator;
1544
1545 fn f() {
1546 let v = [4].into_iter();
1547 v;
1548 //^ i32
1549
1550 let a = [0, 1].into_iter();
1551 a;
1552 //^ &i32
1553 }
1554
1555 //- /core.rs crate:core
1556 #[rustc_skip_array_during_method_dispatch]
1557 pub trait IntoIterator {
1558 type Out;
1559 fn into_iter(self) -> Self::Out;
1560 }
1561
1562 impl<T> IntoIterator for [T; 1] {
1563 type Out = T;
1564 fn into_iter(self) -> Self::Out { loop {} }
1565 }
1566 impl<'a, T> IntoIterator for &'a [T] {
1567 type Out = &'a T;
1568 fn into_iter(self) -> Self::Out { loop {} }
1569 }
1570 "#,
1571 );
1572 }
1573
1574 #[test]
1575 fn sized_blanket_impl() {
1576 check_infer(
1577 r#"
1578 //- minicore: sized
1579 trait Foo { fn foo() -> u8; }
1580 impl<T: Sized> Foo for T {}
1581 fn f<S: Sized, T, U: ?Sized>() {
1582 u32::foo;
1583 S::foo;
1584 T::foo;
1585 U::foo;
1586 <[u32]>::foo;
1587 }
1588 "#,
1589 expect![[r#"
1590 89..160 '{ ...foo; }': ()
1591 95..103 'u32::foo': fn foo<u32>() -> u8
1592 109..115 'S::foo': fn foo<S>() -> u8
1593 121..127 'T::foo': fn foo<T>() -> u8
1594 133..139 'U::foo': {unknown}
1595 145..157 '<[u32]>::foo': {unknown}
1596 "#]],
1597 );
1598 }
1599
1600 #[test]
1601 fn local_impl() {
1602 check_types(
1603 r#"
1604 fn main() {
1605 struct SomeStruct(i32);
1606
1607 impl SomeStruct {
1608 fn is_even(&self) -> bool {
1609 self.0 % 2 == 0
1610 }
1611 }
1612
1613 let o = SomeStruct(3);
1614 let is_even = o.is_even();
1615 // ^^^^^^^ bool
1616 }
1617 "#,
1618 );
1619 }
1620
1621 #[test]
1622 fn deref_fun_1() {
1623 check_types(
1624 r#"
1625 //- minicore: deref
1626
1627 struct A<T, U>(T, U);
1628 struct B<T>(T);
1629 struct C<T>(T);
1630
1631 impl<T> core::ops::Deref for A<B<T>, u32> {
1632 type Target = B<T>;
1633 fn deref(&self) -> &B<T> { &self.0 }
1634 }
1635 impl core::ops::Deref for B<isize> {
1636 type Target = C<isize>;
1637 fn deref(&self) -> &C<isize> { loop {} }
1638 }
1639
1640 impl<T: Copy> C<T> {
1641 fn thing(&self) -> T { self.0 }
1642 }
1643
1644 fn make<T>() -> T { loop {} }
1645
1646 fn test() {
1647 let a1 = A(make(), make());
1648 let _: usize = (*a1).0;
1649 a1;
1650 //^^ A<B<usize>, u32>
1651
1652 let a2 = A(make(), make());
1653 a2.thing();
1654 //^^^^^^^^^^ isize
1655 a2;
1656 //^^ A<B<isize>, u32>
1657 }
1658 "#,
1659 );
1660 }
1661
1662 #[test]
1663 fn deref_fun_2() {
1664 check_types(
1665 r#"
1666 //- minicore: deref
1667
1668 struct A<T, U>(T, U);
1669 struct B<T>(T);
1670 struct C<T>(T);
1671
1672 impl<T> core::ops::Deref for A<B<T>, u32> {
1673 type Target = B<T>;
1674 fn deref(&self) -> &B<T> { &self.0 }
1675 }
1676 impl core::ops::Deref for B<isize> {
1677 type Target = C<isize>;
1678 fn deref(&self) -> &C<isize> { loop {} }
1679 }
1680
1681 impl<T> core::ops::Deref for A<C<T>, i32> {
1682 type Target = C<T>;
1683 fn deref(&self) -> &C<T> { &self.0 }
1684 }
1685
1686 impl<T: Copy> C<T> {
1687 fn thing(&self) -> T { self.0 }
1688 }
1689
1690 fn make<T>() -> T { loop {} }
1691
1692 fn test() {
1693 let a1 = A(make(), 1u32);
1694 a1.thing();
1695 a1;
1696 //^^ A<B<isize>, u32>
1697
1698 let a2 = A(make(), 1i32);
1699 let _: &str = a2.thing();
1700 a2;
1701 //^^ A<C<&str>, i32>
1702 }
1703 "#,
1704 );
1705 }
1706
1707 #[test]
1708 fn receiver_adjustment_autoref() {
1709 check(
1710 r#"
1711 struct Foo;
1712 impl Foo {
1713 fn foo(&self) {}
1714 }
1715 fn test() {
1716 Foo.foo();
1717 //^^^ adjustments: Borrow(Ref(Not))
1718 (&Foo).foo();
1719 // ^^^^ adjustments: ,
1720 }
1721 "#,
1722 );
1723 }
1724
1725 #[test]
1726 fn receiver_adjustment_unsize_array() {
1727 check(
1728 r#"
1729 //- minicore: slice
1730 fn test() {
1731 let a = [1, 2, 3];
1732 a.len();
1733 } //^ adjustments: Borrow(Ref(Not)), Pointer(Unsize)
1734 "#,
1735 );
1736 }
1737
1738 #[test]
1739 fn bad_inferred_reference_1() {
1740 check_no_mismatches(
1741 r#"
1742 //- minicore: sized
1743 pub trait Into<T>: Sized {
1744 fn into(self) -> T;
1745 }
1746 impl<T> Into<T> for T {
1747 fn into(self) -> T { self }
1748 }
1749
1750 trait ExactSizeIterator {
1751 fn len(&self) -> usize;
1752 }
1753
1754 pub struct Foo;
1755 impl Foo {
1756 fn len(&self) -> usize { 0 }
1757 }
1758
1759 pub fn test(generic_args: impl Into<Foo>) {
1760 let generic_args = generic_args.into();
1761 generic_args.len();
1762 let _: Foo = generic_args;
1763 }
1764 "#,
1765 );
1766 }
1767
1768 #[test]
1769 fn bad_inferred_reference_2() {
1770 check_no_mismatches(
1771 r#"
1772 //- minicore: deref
1773 trait ExactSizeIterator {
1774 fn len(&self) -> usize;
1775 }
1776
1777 pub struct Foo;
1778 impl Foo {
1779 fn len(&self) -> usize { 0 }
1780 }
1781
1782 pub fn test() {
1783 let generic_args;
1784 generic_args.len();
1785 let _: Foo = generic_args;
1786 }
1787 "#,
1788 );
1789 }
1790
1791 #[test]
1792 fn resolve_minicore_iterator() {
1793 check_types(
1794 r#"
1795 //- minicore: iterators, sized
1796 fn foo() {
1797 let m = core::iter::repeat(()).filter_map(|()| Some(92)).next();
1798 } //^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Option<i32>
1799 "#,
1800 );
1801 }
1802
1803 #[test]
1804 fn primitive_assoc_fn_shadowed_by_use() {
1805 check_types(
1806 r#"
1807 //- /lib.rs crate:lib deps:core
1808 use core::u16;
1809
1810 fn f() -> u16 {
1811 let x = u16::from_le_bytes();
1812 x
1813 //^ u16
1814 }
1815
1816 //- /core.rs crate:core
1817 pub mod u16 {}
1818
1819 impl u16 {
1820 pub fn from_le_bytes() -> Self { 0 }
1821 }
1822 "#,
1823 )
1824 }
1825
1826 #[test]
1827 fn with_impl_bounds() {
1828 check_types(
1829 r#"
1830 trait Trait {}
1831 struct Foo<T>(T);
1832 impl Trait for isize {}
1833
1834 impl<T: Trait> Foo<T> {
1835 fn foo() -> isize { 0 }
1836 fn bar(&self) -> isize { 0 }
1837 }
1838
1839 impl Foo<()> {
1840 fn foo() {}
1841 fn bar(&self) {}
1842 }
1843
1844 fn f() {
1845 let _ = Foo::<isize>::foo();
1846 //^isize
1847 let _ = Foo(0isize).bar();
1848 //^isize
1849 let _ = Foo::<()>::foo();
1850 //^()
1851 let _ = Foo(()).bar();
1852 //^()
1853 let _ = Foo::<usize>::foo();
1854 //^{unknown}
1855 let _ = Foo(0usize).bar();
1856 //^{unknown}
1857 }
1858
1859 fn g<T: Trait>(a: T) {
1860 let _ = Foo::<T>::foo();
1861 //^isize
1862 let _ = Foo(a).bar();
1863 //^isize
1864 }
1865 "#,
1866 );
1867 }
1868
1869 #[test]
1870 fn incoherent_impls() {
1871 check(
1872 r#"
1873 //- minicore: error, send
1874 pub struct Box<T>(T);
1875 use core::error::Error;
1876
1877 #[rustc_allow_incoherent_impl]
1878 impl dyn Error {
1879 pub fn downcast<T: Error + 'static>(self: Box<Self>) -> Result<Box<T>, Box<dyn Error>> {
1880 loop {}
1881 }
1882 }
1883 #[rustc_allow_incoherent_impl]
1884 impl dyn Error + Send {
1885 /// Attempts to downcast the box to a concrete type.
1886 pub fn downcast<T: Error + 'static>(self: Box<Self>) -> Result<Box<T>, Box<dyn Error + Send>> {
1887 let err: Box<dyn Error> = self;
1888 // ^^^^ expected Box<dyn Error>, got Box<dyn Error + Send>
1889 // FIXME, type mismatch should not occur
1890 <dyn Error>::downcast(err).map_err(|_| loop {})
1891 //^^^^^^^^^^^^^^^^^^^^^ type: fn downcast<{unknown}>(Box<dyn Error>) -> Result<Box<{unknown}>, Box<dyn Error>>
1892 }
1893 }
1894 "#,
1895 );
1896 }
1897
1898 #[test]
1899 fn fallback_private_methods() {
1900 check(
1901 r#"
1902 mod module {
1903 pub struct Struct;
1904
1905 impl Struct {
1906 fn func(&self) {}
1907 }
1908 }
1909
1910 fn foo() {
1911 let s = module::Struct;
1912 s.func();
1913 //^^^^^^^^ type: ()
1914 }
1915 "#,
1916 );
1917 }