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