]> git.proxmox.com Git - rustc.git/blob - src/tools/rust-analyzer/crates/hir-ty/src/tests/method_resolution.rs
New upstream version 1.65.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 struct A;
168 impl A { pub fn thing() -> A { A {} }}
169 }
170
171 mod b {
172 struct B;
173 impl B { pub fn thing() -> u32 { 99 }}
174
175 mod c {
176 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 55..63 '{ A {} }': A
190 57..61 'A {}': A
191 125..131 '{ 99 }': u32
192 127..129 '99': u32
193 201..209 '{ C {} }': C
194 203..207 'C {}': C
195 240..324 '{ ...g(); }': ()
196 250..251 'x': A
197 254..265 'a::A::thing': fn thing() -> A
198 254..267 'a::A::thing()': A
199 277..278 'y': u32
200 281..292 'b::B::thing': fn thing() -> u32
201 281..294 'b::B::thing()': u32
202 304..305 'z': C
203 308..319 'c::C::thing': fn thing() -> C
204 308..321 '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 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 method_resolution_encountering_fn_type() {
990 check_types(
991 r#"
992 //- /main.rs
993 fn foo() {}
994 trait FnOnce { fn call(self); }
995 fn test() { foo.call(); }
996 //^^^^^^^^^^ {unknown}
997 "#,
998 );
999 }
1000
1001 #[test]
1002 fn super_trait_impl_return_trait_method_resolution() {
1003 check_infer(
1004 r#"
1005 //- minicore: sized
1006 trait Base {
1007 fn foo(self) -> usize;
1008 }
1009
1010 trait Super : Base {}
1011
1012 fn base1() -> impl Base { loop {} }
1013 fn super1() -> impl Super { loop {} }
1014
1015 fn test(base2: impl Base, super2: impl Super) {
1016 base1().foo();
1017 super1().foo();
1018 base2.foo();
1019 super2.foo();
1020 }
1021 "#,
1022 expect![[r#"
1023 24..28 'self': Self
1024 90..101 '{ loop {} }': !
1025 92..99 'loop {}': !
1026 97..99 '{}': ()
1027 128..139 '{ loop {} }': !
1028 130..137 'loop {}': !
1029 135..137 '{}': ()
1030 149..154 'base2': impl Base
1031 167..173 'super2': impl Super
1032 187..264 '{ ...o(); }': ()
1033 193..198 'base1': fn base1() -> impl Base
1034 193..200 'base1()': impl Base
1035 193..206 'base1().foo()': usize
1036 212..218 'super1': fn super1() -> impl Super
1037 212..220 'super1()': impl Super
1038 212..226 'super1().foo()': usize
1039 232..237 'base2': impl Base
1040 232..243 'base2.foo()': usize
1041 249..255 'super2': impl Super
1042 249..261 'super2.foo()': usize
1043 "#]],
1044 );
1045 }
1046
1047 #[test]
1048 fn method_resolution_non_parameter_type() {
1049 check_types(
1050 r#"
1051 mod a {
1052 pub trait Foo {
1053 fn foo(&self);
1054 }
1055 }
1056
1057 struct Wrapper<T>(T);
1058 fn foo<T>(t: Wrapper<T>)
1059 where
1060 Wrapper<T>: a::Foo,
1061 {
1062 t.foo();
1063 } //^^^^^^^ {unknown}
1064 "#,
1065 );
1066 }
1067
1068 #[test]
1069 fn method_resolution_3373() {
1070 check_types(
1071 r#"
1072 struct A<T>(T);
1073
1074 impl A<i32> {
1075 fn from(v: i32) -> A<i32> { A(v) }
1076 }
1077
1078 fn main() {
1079 A::from(3);
1080 } //^^^^^^^^^^ A<i32>
1081 "#,
1082 );
1083 }
1084
1085 #[test]
1086 fn method_resolution_slow() {
1087 // this can get quite slow if we set the solver size limit too high
1088 check_types(
1089 r#"
1090 trait SendX {}
1091
1092 struct S1; impl SendX for S1 {}
1093 struct S2; impl SendX for S2 {}
1094 struct U1;
1095
1096 trait Trait { fn method(self); }
1097
1098 struct X1<A, B> {}
1099 impl<A, B> SendX for X1<A, B> where A: SendX, B: SendX {}
1100
1101 struct S<B, C> {}
1102
1103 trait FnX {}
1104
1105 impl<B, C> Trait for S<B, C> where C: FnX, B: SendX {}
1106
1107 fn test() { (S {}).method(); }
1108 //^^^^^^^^^^^^^^^ ()
1109 "#,
1110 );
1111 }
1112
1113 #[test]
1114 fn dyn_trait_super_trait_not_in_scope() {
1115 check_infer(
1116 r#"
1117 mod m {
1118 pub trait SuperTrait {
1119 fn foo(&self) -> u32 { 0 }
1120 }
1121 }
1122 trait Trait: m::SuperTrait {}
1123
1124 struct S;
1125 impl m::SuperTrait for S {}
1126 impl Trait for S {}
1127
1128 fn test(d: &dyn Trait) {
1129 d.foo();
1130 }
1131 "#,
1132 expect![[r#"
1133 51..55 'self': &Self
1134 64..69 '{ 0 }': u32
1135 66..67 '0': u32
1136 176..177 'd': &dyn Trait
1137 191..207 '{ ...o(); }': ()
1138 197..198 'd': &dyn Trait
1139 197..204 'd.foo()': u32
1140 "#]],
1141 );
1142 }
1143
1144 #[test]
1145 fn method_resolution_foreign_opaque_type() {
1146 check_infer(
1147 r#"
1148 extern "C" {
1149 type S;
1150 fn f() -> &'static S;
1151 }
1152
1153 impl S {
1154 fn foo(&self) -> bool {
1155 true
1156 }
1157 }
1158
1159 fn test() {
1160 let s = unsafe { f() };
1161 s.foo();
1162 }
1163 "#,
1164 expect![[r#"
1165 75..79 'self': &S
1166 89..109 '{ ... }': bool
1167 99..103 'true': bool
1168 123..167 '{ ...o(); }': ()
1169 133..134 's': &S
1170 137..151 'unsafe { f() }': &S
1171 137..151 'unsafe { f() }': &S
1172 146..147 'f': fn f() -> &S
1173 146..149 'f()': &S
1174 157..158 's': &S
1175 157..164 's.foo()': bool
1176 "#]],
1177 );
1178 }
1179
1180 #[test]
1181 fn method_with_allocator_box_self_type() {
1182 check_types(
1183 r#"
1184 struct Slice<T> {}
1185 struct Box<T, A> {}
1186
1187 impl<T> Slice<T> {
1188 pub fn into_vec<A>(self: Box<Self, A>) { }
1189 }
1190
1191 fn main() {
1192 let foo: Slice<u32>;
1193 foo.into_vec(); // we shouldn't crash on this at least
1194 } //^^^^^^^^^^^^^^ {unknown}
1195 "#,
1196 );
1197 }
1198
1199 #[test]
1200 fn method_on_dyn_impl() {
1201 check_types(
1202 r#"
1203 trait Foo {}
1204
1205 impl Foo for u32 {}
1206 impl dyn Foo + '_ {
1207 pub fn dyn_foo(&self) -> u32 {
1208 0
1209 }
1210 }
1211
1212 fn main() {
1213 let f = &42u32 as &dyn Foo;
1214 f.dyn_foo();
1215 // ^^^^^^^^^^^ u32
1216 }
1217 "#,
1218 );
1219 }
1220
1221 #[test]
1222 fn autoderef_visibility_field() {
1223 check(
1224 r#"
1225 //- minicore: deref
1226 mod a {
1227 pub struct Foo(pub char);
1228 pub struct Bar(i32);
1229 impl Bar {
1230 pub fn new() -> Self {
1231 Self(0)
1232 }
1233 }
1234 impl core::ops::Deref for Bar {
1235 type Target = Foo;
1236 fn deref(&self) -> &Foo {
1237 &Foo('z')
1238 }
1239 }
1240 }
1241 mod b {
1242 fn foo() {
1243 let x = super::a::Bar::new().0;
1244 // ^^^^^^^^^^^^^^^^^^^^ adjustments: Deref(Some(OverloadedDeref(Not)))
1245 // ^^^^^^^^^^^^^^^^^^^^^^ type: char
1246 }
1247 }
1248 "#,
1249 )
1250 }
1251
1252 #[test]
1253 fn autoderef_visibility_method() {
1254 cov_mark::check!(autoderef_candidate_not_visible);
1255 check(
1256 r#"
1257 //- minicore: deref
1258 mod a {
1259 pub struct Foo(pub char);
1260 impl Foo {
1261 pub fn mango(&self) -> char {
1262 self.0
1263 }
1264 }
1265 pub struct Bar(i32);
1266 impl Bar {
1267 pub fn new() -> Self {
1268 Self(0)
1269 }
1270 fn mango(&self) -> i32 {
1271 self.0
1272 }
1273 }
1274 impl core::ops::Deref for Bar {
1275 type Target = Foo;
1276 fn deref(&self) -> &Foo {
1277 &Foo('z')
1278 }
1279 }
1280 }
1281 mod b {
1282 fn foo() {
1283 let x = super::a::Bar::new().mango();
1284 // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type: char
1285 }
1286 }
1287 "#,
1288 )
1289 }
1290
1291 #[test]
1292 fn trait_vs_private_inherent_const() {
1293 cov_mark::check!(const_candidate_not_visible);
1294 check(
1295 r#"
1296 mod a {
1297 pub struct Foo;
1298 impl Foo {
1299 const VALUE: u32 = 2;
1300 }
1301 pub trait Trait {
1302 const VALUE: usize;
1303 }
1304 impl Trait for Foo {
1305 const VALUE: usize = 3;
1306 }
1307
1308 fn foo() {
1309 let x = Foo::VALUE;
1310 // ^^^^^^^^^^ type: u32
1311 }
1312 }
1313 use a::Trait;
1314 fn foo() {
1315 let x = a::Foo::VALUE;
1316 // ^^^^^^^^^^^^^ type: usize
1317 }
1318 "#,
1319 )
1320 }
1321
1322 #[test]
1323 fn trait_impl_in_unnamed_const() {
1324 check_types(
1325 r#"
1326 struct S;
1327
1328 trait Tr {
1329 fn method(&self) -> u16;
1330 }
1331
1332 const _: () = {
1333 impl Tr for S {}
1334 };
1335
1336 fn f() {
1337 S.method();
1338 //^^^^^^^^^^ u16
1339 }
1340 "#,
1341 );
1342 }
1343
1344 #[test]
1345 fn trait_impl_in_synstructure_const() {
1346 check_types(
1347 r#"
1348 struct S;
1349
1350 trait Tr {
1351 fn method(&self) -> u16;
1352 }
1353
1354 const _DERIVE_Tr_: () = {
1355 impl Tr for S {}
1356 };
1357
1358 fn f() {
1359 S.method();
1360 //^^^^^^^^^^ u16
1361 }
1362 "#,
1363 );
1364 }
1365
1366 #[test]
1367 fn inherent_impl_in_unnamed_const() {
1368 check_types(
1369 r#"
1370 struct S;
1371
1372 const _: () = {
1373 impl S {
1374 fn method(&self) -> u16 { 0 }
1375
1376 pub(super) fn super_method(&self) -> u16 { 0 }
1377
1378 pub(crate) fn crate_method(&self) -> u16 { 0 }
1379
1380 pub fn pub_method(&self) -> u16 { 0 }
1381 }
1382 };
1383
1384 fn f() {
1385 S.method();
1386 //^^^^^^^^^^ u16
1387
1388 S.super_method();
1389 //^^^^^^^^^^^^^^^^ u16
1390
1391 S.crate_method();
1392 //^^^^^^^^^^^^^^^^ u16
1393
1394 S.pub_method();
1395 //^^^^^^^^^^^^^^ u16
1396 }
1397 "#,
1398 );
1399 }
1400
1401 #[test]
1402 fn resolve_const_generic_array_methods() {
1403 check_types(
1404 r#"
1405 #[lang = "array"]
1406 impl<T, const N: usize> [T; N] {
1407 pub fn map<F, U>(self, f: F) -> [U; N]
1408 where
1409 F: FnMut(T) -> U,
1410 { loop {} }
1411 }
1412
1413 #[lang = "slice"]
1414 impl<T> [T] {
1415 pub fn map<F, U>(self, f: F) -> &[U]
1416 where
1417 F: FnMut(T) -> U,
1418 { loop {} }
1419 }
1420
1421 fn f() {
1422 let v = [1, 2].map::<_, usize>(|x| -> x * 2);
1423 v;
1424 //^ [usize; 2]
1425 }
1426 "#,
1427 );
1428 }
1429
1430 #[test]
1431 fn resolve_const_generic_method() {
1432 check_types(
1433 r#"
1434 struct Const<const N: usize>;
1435
1436 #[lang = "array"]
1437 impl<T, const N: usize> [T; N] {
1438 pub fn my_map<F, U, const X: usize>(self, f: F, c: Const<X>) -> [U; X]
1439 where
1440 F: FnMut(T) -> U,
1441 { loop {} }
1442 }
1443
1444 #[lang = "slice"]
1445 impl<T> [T] {
1446 pub fn my_map<F, const X: usize, U>(self, f: F, c: Const<X>) -> &[U]
1447 where
1448 F: FnMut(T) -> U,
1449 { loop {} }
1450 }
1451
1452 fn f<const C: usize, P>() {
1453 let v = [1, 2].my_map::<_, (), 12>(|x| -> x * 2, Const::<12>);
1454 v;
1455 //^ [(); 12]
1456 let v = [1, 2].my_map::<_, P, C>(|x| -> x * 2, Const::<C>);
1457 v;
1458 //^ [P; C]
1459 }
1460 "#,
1461 );
1462 }
1463
1464 #[test]
1465 fn const_generic_type_alias() {
1466 check_types(
1467 r#"
1468 struct Const<const N: usize>;
1469 type U2 = Const<2>;
1470 type U5 = Const<5>;
1471
1472 impl U2 {
1473 fn f(self) -> Const<12> {
1474 loop {}
1475 }
1476 }
1477
1478 impl U5 {
1479 fn f(self) -> Const<15> {
1480 loop {}
1481 }
1482 }
1483
1484 fn f(x: U2) {
1485 let y = x.f();
1486 //^ Const<12>
1487 }
1488 "#,
1489 );
1490 }
1491
1492 #[test]
1493 fn skip_array_during_method_dispatch() {
1494 check_types(
1495 r#"
1496 //- /main2018.rs crate:main2018 deps:core
1497 use core::IntoIterator;
1498
1499 fn f() {
1500 let v = [4].into_iter();
1501 v;
1502 //^ &i32
1503
1504 let a = [0, 1].into_iter();
1505 a;
1506 //^ &i32
1507 }
1508
1509 //- /main2021.rs crate:main2021 deps:core edition:2021
1510 use core::IntoIterator;
1511
1512 fn f() {
1513 let v = [4].into_iter();
1514 v;
1515 //^ i32
1516
1517 let a = [0, 1].into_iter();
1518 a;
1519 //^ &i32
1520 }
1521
1522 //- /core.rs crate:core
1523 #[rustc_skip_array_during_method_dispatch]
1524 pub trait IntoIterator {
1525 type Out;
1526 fn into_iter(self) -> Self::Out;
1527 }
1528
1529 impl<T> IntoIterator for [T; 1] {
1530 type Out = T;
1531 fn into_iter(self) -> Self::Out { loop {} }
1532 }
1533 impl<'a, T> IntoIterator for &'a [T] {
1534 type Out = &'a T;
1535 fn into_iter(self) -> Self::Out { loop {} }
1536 }
1537 "#,
1538 );
1539 }
1540
1541 #[test]
1542 fn sized_blanket_impl() {
1543 check_infer(
1544 r#"
1545 //- minicore: sized
1546 trait Foo { fn foo() -> u8; }
1547 impl<T: Sized> Foo for T {}
1548 fn f<S: Sized, T, U: ?Sized>() {
1549 u32::foo;
1550 S::foo;
1551 T::foo;
1552 U::foo;
1553 <[u32]>::foo;
1554 }
1555 "#,
1556 expect![[r#"
1557 89..160 '{ ...foo; }': ()
1558 95..103 'u32::foo': fn foo<u32>() -> u8
1559 109..115 'S::foo': fn foo<S>() -> u8
1560 121..127 'T::foo': fn foo<T>() -> u8
1561 133..139 'U::foo': {unknown}
1562 145..157 '<[u32]>::foo': {unknown}
1563 "#]],
1564 );
1565 }
1566
1567 #[test]
1568 fn local_impl() {
1569 check_types(
1570 r#"
1571 fn main() {
1572 struct SomeStruct(i32);
1573
1574 impl SomeStruct {
1575 fn is_even(&self) -> bool {
1576 self.0 % 2 == 0
1577 }
1578 }
1579
1580 let o = SomeStruct(3);
1581 let is_even = o.is_even();
1582 // ^^^^^^^ bool
1583 }
1584 "#,
1585 );
1586 }
1587
1588 #[test]
1589 fn deref_fun_1() {
1590 check_types(
1591 r#"
1592 //- minicore: deref
1593
1594 struct A<T, U>(T, U);
1595 struct B<T>(T);
1596 struct C<T>(T);
1597
1598 impl<T> core::ops::Deref for A<B<T>, u32> {
1599 type Target = B<T>;
1600 fn deref(&self) -> &B<T> { &self.0 }
1601 }
1602 impl core::ops::Deref for B<isize> {
1603 type Target = C<isize>;
1604 fn deref(&self) -> &C<isize> { loop {} }
1605 }
1606
1607 impl<T: Copy> C<T> {
1608 fn thing(&self) -> T { self.0 }
1609 }
1610
1611 fn make<T>() -> T { loop {} }
1612
1613 fn test() {
1614 let a1 = A(make(), make());
1615 let _: usize = (*a1).0;
1616 a1;
1617 //^^ A<B<usize>, u32>
1618
1619 let a2 = A(make(), make());
1620 a2.thing();
1621 //^^^^^^^^^^ isize
1622 a2;
1623 //^^ A<B<isize>, u32>
1624 }
1625 "#,
1626 );
1627 }
1628
1629 #[test]
1630 fn deref_fun_2() {
1631 check_types(
1632 r#"
1633 //- minicore: deref
1634
1635 struct A<T, U>(T, U);
1636 struct B<T>(T);
1637 struct C<T>(T);
1638
1639 impl<T> core::ops::Deref for A<B<T>, u32> {
1640 type Target = B<T>;
1641 fn deref(&self) -> &B<T> { &self.0 }
1642 }
1643 impl core::ops::Deref for B<isize> {
1644 type Target = C<isize>;
1645 fn deref(&self) -> &C<isize> { loop {} }
1646 }
1647
1648 impl<T> core::ops::Deref for A<C<T>, i32> {
1649 type Target = C<T>;
1650 fn deref(&self) -> &C<T> { &self.0 }
1651 }
1652
1653 impl<T: Copy> C<T> {
1654 fn thing(&self) -> T { self.0 }
1655 }
1656
1657 fn make<T>() -> T { loop {} }
1658
1659 fn test() {
1660 let a1 = A(make(), 1u32);
1661 a1.thing();
1662 a1;
1663 //^^ A<B<isize>, u32>
1664
1665 let a2 = A(make(), 1i32);
1666 let _: &str = a2.thing();
1667 a2;
1668 //^^ A<C<&str>, i32>
1669 }
1670 "#,
1671 );
1672 }
1673
1674 #[test]
1675 fn receiver_adjustment_autoref() {
1676 check(
1677 r#"
1678 struct Foo;
1679 impl Foo {
1680 fn foo(&self) {}
1681 }
1682 fn test() {
1683 Foo.foo();
1684 //^^^ adjustments: Borrow(Ref(Not))
1685 (&Foo).foo();
1686 // ^^^^ adjustments: ,
1687 }
1688 "#,
1689 );
1690 }
1691
1692 #[test]
1693 fn receiver_adjustment_unsize_array() {
1694 // FIXME not quite correct
1695 check(
1696 r#"
1697 //- minicore: slice
1698 fn test() {
1699 let a = [1, 2, 3];
1700 a.len();
1701 } //^ adjustments: Pointer(Unsize), Borrow(Ref(Not))
1702 "#,
1703 );
1704 }
1705
1706 #[test]
1707 fn bad_inferred_reference_1() {
1708 check_no_mismatches(
1709 r#"
1710 //- minicore: sized
1711 pub trait Into<T>: Sized {
1712 fn into(self) -> T;
1713 }
1714 impl<T> Into<T> for T {
1715 fn into(self) -> T { self }
1716 }
1717
1718 trait ExactSizeIterator {
1719 fn len(&self) -> usize;
1720 }
1721
1722 pub struct Foo;
1723 impl Foo {
1724 fn len(&self) -> usize { 0 }
1725 }
1726
1727 pub fn test(generic_args: impl Into<Foo>) {
1728 let generic_args = generic_args.into();
1729 generic_args.len();
1730 let _: Foo = generic_args;
1731 }
1732 "#,
1733 );
1734 }
1735
1736 #[test]
1737 fn bad_inferred_reference_2() {
1738 check_no_mismatches(
1739 r#"
1740 //- minicore: deref
1741 trait ExactSizeIterator {
1742 fn len(&self) -> usize;
1743 }
1744
1745 pub struct Foo;
1746 impl Foo {
1747 fn len(&self) -> usize { 0 }
1748 }
1749
1750 pub fn test() {
1751 let generic_args;
1752 generic_args.len();
1753 let _: Foo = generic_args;
1754 }
1755 "#,
1756 );
1757 }
1758
1759 #[test]
1760 fn resolve_minicore_iterator() {
1761 check_types(
1762 r#"
1763 //- minicore: iterators, sized
1764 fn foo() {
1765 let m = core::iter::repeat(()).filter_map(|()| Some(92)).next();
1766 } //^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Option<i32>
1767 "#,
1768 );
1769 }
1770
1771 #[test]
1772 fn primitive_assoc_fn_shadowed_by_use() {
1773 check_types(
1774 r#"
1775 //- /lib.rs crate:lib deps:core
1776 use core::u16;
1777
1778 fn f() -> u16 {
1779 let x = u16::from_le_bytes();
1780 x
1781 //^ u16
1782 }
1783
1784 //- /core.rs crate:core
1785 pub mod u16 {}
1786
1787 impl u16 {
1788 pub fn from_le_bytes() -> Self { 0 }
1789 }
1790 "#,
1791 )
1792 }
1793
1794 #[test]
1795 fn with_impl_bounds() {
1796 check_types(
1797 r#"
1798 trait Trait {}
1799 struct Foo<T>(T);
1800 impl Trait for isize {}
1801
1802 impl<T: Trait> Foo<T> {
1803 fn foo() -> isize { 0 }
1804 fn bar(&self) -> isize { 0 }
1805 }
1806
1807 impl Foo<()> {
1808 fn foo() {}
1809 fn bar(&self) {}
1810 }
1811
1812 fn f() {
1813 let _ = Foo::<isize>::foo();
1814 //^isize
1815 let _ = Foo(0isize).bar();
1816 //^isize
1817 let _ = Foo::<()>::foo();
1818 //^()
1819 let _ = Foo(()).bar();
1820 //^()
1821 let _ = Foo::<usize>::foo();
1822 //^{unknown}
1823 let _ = Foo(0usize).bar();
1824 //^{unknown}
1825 }
1826
1827 fn g<T: Trait>(a: T) {
1828 let _ = Foo::<T>::foo();
1829 //^isize
1830 let _ = Foo(a).bar();
1831 //^isize
1832 }
1833 "#,
1834 );
1835 }