1 use expect_test::expect;
2
3 use super::{check_infer, check_no_mismatches, check_types};
4
5 #[test]
bug_484()6 fn bug_484() {
7 check_infer(
8 r#"
9 fn test() {
10 let x = if true {};
11 }
12 "#,
13 expect![[r#"
14 10..37 '{ ... {}; }': ()
15 20..21 'x': ()
16 24..34 'if true {}': ()
17 27..31 'true': bool
18 32..34 '{}': ()
19 "#]],
20 );
21 }
22
23 #[test]
no_panic_on_field_of_enum()24 fn no_panic_on_field_of_enum() {
25 check_infer(
26 r#"
27 enum X {}
28
29 fn test(x: X) {
30 x.some_field;
31 }
32 "#,
33 expect![[r#"
34 19..20 'x': X
35 25..46 '{ ...eld; }': ()
36 31..32 'x': X
37 31..43 'x.some_field': {unknown}
38 "#]],
39 );
40 }
41
42 #[test]
bug_585()43 fn bug_585() {
44 check_infer(
45 r#"
46 fn test() {
47 X {};
48 match x {
49 A::B {} => (),
50 A::Y() => (),
51 }
52 }
53 "#,
54 expect![[r#"
55 10..88 '{ ... } }': ()
56 16..20 'X {}': {unknown}
57 26..86 'match ... }': ()
58 32..33 'x': {unknown}
59 44..51 'A::B {}': {unknown}
60 55..57 '()': ()
61 67..73 'A::Y()': {unknown}
62 77..79 '()': ()
63 "#]],
64 );
65 }
66
67 #[test]
bug_651()68 fn bug_651() {
69 check_infer(
70 r#"
71 fn quux() {
72 let y = 92;
73 1 + y;
74 }
75 "#,
76 expect![[r#"
77 10..40 '{ ...+ y; }': ()
78 20..21 'y': i32
79 24..26 '92': i32
80 32..33 '1': i32
81 32..37 '1 + y': i32
82 36..37 'y': i32
83 "#]],
84 );
85 }
86
87 #[test]
recursive_vars()88 fn recursive_vars() {
89 check_infer(
90 r#"
91 fn test() {
92 let y = unknown;
93 [y, &y];
94 }
95 "#,
96 expect![[r#"
97 10..47 '{ ...&y]; }': ()
98 20..21 'y': {unknown}
99 24..31 'unknown': {unknown}
100 37..44 '[y, &y]': [{unknown}; 2]
101 38..39 'y': {unknown}
102 41..43 '&y': &{unknown}
103 42..43 'y': {unknown}
104 "#]],
105 );
106 }
107
108 #[test]
recursive_vars_2()109 fn recursive_vars_2() {
110 check_infer(
111 r#"
112 fn test() {
113 let x = unknown;
114 let y = unknown;
115 [(x, y), (&y, &x)];
116 }
117 "#,
118 expect![[r#"
119 10..79 '{ ...x)]; }': ()
120 20..21 'x': &{unknown}
121 24..31 'unknown': &{unknown}
122 41..42 'y': {unknown}
123 45..52 'unknown': {unknown}
124 58..76 '[(x, y..., &x)]': [(&{unknown}, {unknown}); 2]
125 59..65 '(x, y)': (&{unknown}, {unknown})
126 60..61 'x': &{unknown}
127 63..64 'y': {unknown}
128 67..75 '(&y, &x)': (&{unknown}, {unknown})
129 68..70 '&y': &{unknown}
130 69..70 'y': {unknown}
131 72..74 '&x': &&{unknown}
132 73..74 'x': &{unknown}
133 "#]],
134 );
135 }
136
137 #[test]
array_elements_expected_type()138 fn array_elements_expected_type() {
139 check_no_mismatches(
140 r#"
141 fn test() {
142 let x: [[u32; 2]; 2] = [[1, 2], [3, 4]];
143 }
144 "#,
145 );
146 }
147
148 #[test]
infer_std_crash_1()149 fn infer_std_crash_1() {
150 // caused stack overflow, taken from std
151 check_infer(
152 r#"
153 enum Maybe<T> {
154 Real(T),
155 Fake,
156 }
157
158 fn write() {
159 match something_unknown {
160 Maybe::Real(ref mut something) => (),
161 }
162 }
163 "#,
164 expect![[r#"
165 53..138 '{ ... } }': ()
166 59..136 'match ... }': ()
167 65..82 'someth...nknown': Maybe<{unknown}>
168 93..123 'Maybe:...thing)': Maybe<{unknown}>
169 105..122 'ref mu...ething': &mut {unknown}
170 127..129 '()': ()
171 "#]],
172 );
173 }
174
175 #[test]
infer_std_crash_2()176 fn infer_std_crash_2() {
177 // caused "equating two type variables, ...", taken from std
178 check_infer(
179 r#"
180 fn test_line_buffer() {
181 &[0, b'\n', 1, b'\n'];
182 }
183 "#,
184 expect![[r#"
185 22..52 '{ ...n']; }': ()
186 28..49 '&[0, b...b'\n']': &[u8; 4]
187 29..49 '[0, b'...b'\n']': [u8; 4]
188 30..31 '0': u8
189 33..38 'b'\n'': u8
190 40..41 '1': u8
191 43..48 'b'\n'': u8
192 "#]],
193 );
194 }
195
196 #[test]
infer_std_crash_3()197 fn infer_std_crash_3() {
198 // taken from rustc
199 check_infer(
200 r#"
201 pub fn compute() {
202 match nope!() {
203 SizeSkeleton::Pointer { non_zero: true, tail } => {}
204 }
205 }
206 "#,
207 expect![[r#"
208 17..107 '{ ... } }': ()
209 23..105 'match ... }': ()
210 29..36 'nope!()': {unknown}
211 47..93 'SizeSk...tail }': {unknown}
212 81..85 'true': bool
213 81..85 'true': bool
214 87..91 'tail': {unknown}
215 97..99 '{}': ()
216 "#]],
217 );
218 }
219
220 #[test]
infer_std_crash_4()221 fn infer_std_crash_4() {
222 // taken from rustc
223 check_infer(
224 r#"
225 pub fn primitive_type() {
226 match *self {
227 BorrowedRef { type_: Primitive(p), ..} => {},
228 }
229 }
230 "#,
231 expect![[r#"
232 24..105 '{ ... } }': ()
233 30..103 'match ... }': ()
234 36..41 '*self': {unknown}
235 37..41 'self': {unknown}
236 52..90 'Borrow...), ..}': {unknown}
237 73..85 'Primitive(p)': {unknown}
238 83..84 'p': {unknown}
239 94..96 '{}': ()
240 "#]],
241 );
242 }
243
244 #[test]
infer_std_crash_5()245 fn infer_std_crash_5() {
246 // taken from rustc
247 check_infer(
248 r#"
249 //- minicore: iterator
250 fn extra_compiler_flags() {
251 for content in doesnt_matter {
252 let name = if doesnt_matter {
253 first
254 } else {
255 &content
256 };
257
258 let content = if ICE_REPORT_COMPILER_FLAGS_STRIP_VALUE.contains(&name) {
259 name
260 } else {
261 content
262 };
263 }
264 }
265 "#,
266 expect![[r#"
267 26..322 '{ ... } }': ()
268 32..320 'for co... }': fn into_iter<{unknown}>({unknown}) -> <{unknown} as IntoIterator>::IntoIter
269 32..320 'for co... }': {unknown}
270 32..320 'for co... }': !
271 32..320 'for co... }': {unknown}
272 32..320 'for co... }': &mut {unknown}
273 32..320 'for co... }': fn next<{unknown}>(&mut {unknown}) -> Option<<{unknown} as Iterator>::Item>
274 32..320 'for co... }': Option<{unknown}>
275 32..320 'for co... }': ()
276 32..320 'for co... }': ()
277 32..320 'for co... }': ()
278 36..43 'content': {unknown}
279 47..60 'doesnt_matter': {unknown}
280 61..320 '{ ... }': ()
281 75..79 'name': &{unknown}
282 82..166 'if doe... }': &{unknown}
283 85..98 'doesnt_matter': bool
284 99..128 '{ ... }': &{unknown}
285 113..118 'first': &{unknown}
286 134..166 '{ ... }': &{unknown}
287 148..156 '&content': &{unknown}
288 149..156 'content': {unknown}
289 181..188 'content': &{unknown}
290 191..313 'if ICE... }': &{unknown}
291 194..231 'ICE_RE..._VALUE': {unknown}
292 194..247 'ICE_RE...&name)': bool
293 241..246 '&name': &&{unknown}
294 242..246 'name': &{unknown}
295 248..276 '{ ... }': &{unknown}
296 262..266 'name': &{unknown}
297 282..313 '{ ... }': {unknown}
298 296..303 'content': {unknown}
299 "#]],
300 );
301 }
302
303 #[test]
infer_nested_generics_crash()304 fn infer_nested_generics_crash() {
305 // another crash found typechecking rustc
306 check_infer(
307 r#"
308 struct Canonical<V> {
309 value: V,
310 }
311 struct QueryResponse<V> {
312 value: V,
313 }
314 fn test<R>(query_response: Canonical<QueryResponse<R>>) {
315 &query_response.value;
316 }
317 "#,
318 expect![[r#"
319 91..105 'query_response': Canonical<QueryResponse<R>>
320 136..166 '{ ...lue; }': ()
321 142..163 '&query....value': &QueryResponse<R>
322 143..157 'query_response': Canonical<QueryResponse<R>>
323 143..163 'query_....value': QueryResponse<R>
324 "#]],
325 );
326 }
327
328 #[test]
infer_paren_macro_call()329 fn infer_paren_macro_call() {
330 check_infer(
331 r#"
332 macro_rules! bar { () => {0u32} }
333 fn test() {
334 let a = (bar!());
335 }
336 "#,
337 expect![[r#"
338 !0..4 '0u32': u32
339 44..69 '{ ...()); }': ()
340 54..55 'a': u32
341 "#]],
342 );
343 }
344
345 #[test]
infer_array_macro_call()346 fn infer_array_macro_call() {
347 check_infer(
348 r#"
349 macro_rules! bar { () => {0u32} }
350 fn test() {
351 let a = [bar!()];
352 }
353 "#,
354 expect![[r#"
355 !0..4 '0u32': u32
356 44..69 '{ ...()]; }': ()
357 54..55 'a': [u32; 1]
358 58..66 '[bar!()]': [u32; 1]
359 "#]],
360 );
361 }
362
363 #[test]
bug_1030()364 fn bug_1030() {
365 check_infer(
366 r#"
367 struct HashSet<T, H>;
368 struct FxHasher;
369 type FxHashSet<T> = HashSet<T, FxHasher>;
370
371 impl<T, H> HashSet<T, H> {
372 fn default() -> HashSet<T, H> {}
373 }
374
375 pub fn main_loop() {
376 FxHashSet::default();
377 }
378 "#,
379 expect![[r#"
380 143..145 '{}': HashSet<T, H>
381 168..197 '{ ...t(); }': ()
382 174..192 'FxHash...efault': fn default<{unknown}, FxHasher>() -> HashSet<{unknown}, FxHasher>
383 174..194 'FxHash...ault()': HashSet<{unknown}, FxHasher>
384 "#]],
385 );
386 }
387
388 #[test]
issue_2669()389 fn issue_2669() {
390 check_infer(
391 r#"
392 trait A {}
393 trait Write {}
394 struct Response<T> {}
395
396 trait D {
397 fn foo();
398 }
399
400 impl<T:A> D for Response<T> {
401 fn foo() {
402 end();
403 fn end<W: Write>() {
404 let _x: T = loop {};
405 }
406 }
407 }
408 "#,
409 expect![[r#"
410 119..214 '{ ... }': ()
411 129..132 'end': fn end<{unknown}>()
412 129..134 'end()': ()
413 163..208 '{ ... }': ()
414 181..183 '_x': !
415 190..197 'loop {}': !
416 195..197 '{}': ()
417 "#]],
418 )
419 }
420
421 #[test]
issue_2705()422 fn issue_2705() {
423 check_infer(
424 r#"
425 trait Trait {}
426 fn test() {
427 <Trait<u32>>::foo()
428 }
429 "#,
430 expect![[r#"
431 25..52 '{ ...oo() }': ()
432 31..48 '<Trait...>::foo': {unknown}
433 31..50 '<Trait...:foo()': ()
434 "#]],
435 );
436 }
437
438 #[test]
issue_2683_chars_impl()439 fn issue_2683_chars_impl() {
440 check_types(
441 r#"
442 //- minicore: iterator
443 pub struct Chars<'a> {}
444 impl<'a> Iterator for Chars<'a> {
445 type Item = char;
446 fn next(&mut self) -> Option<char> { loop {} }
447 }
448
449 fn test() {
450 let chars: Chars<'_>;
451 (chars.next(), chars.nth(1));
452 } //^^^^^^^^^^^^^^^^^^^^^^^^^^^^ (Option<char>, Option<char>)
453 "#,
454 );
455 }
456
457 #[test]
issue_3999_slice()458 fn issue_3999_slice() {
459 check_infer(
460 r#"
461 fn foo(params: &[usize]) {
462 match params {
463 [ps @ .., _] => {}
464 }
465 }
466 "#,
467 expect![[r#"
468 7..13 'params': &[usize]
469 25..80 '{ ... } }': ()
470 31..78 'match ... }': ()
471 37..43 'params': &[usize]
472 54..66 '[ps @ .., _]': [usize]
473 55..62 'ps @ ..': &[usize]
474 60..62 '..': [usize]
475 64..65 '_': usize
476 70..72 '{}': ()
477 "#]],
478 );
479 }
480
481 #[test]
issue_3999_struct()482 fn issue_3999_struct() {
483 // rust-analyzer should not panic on seeing this malformed
484 // record pattern.
485 check_infer(
486 r#"
487 struct Bar {
488 a: bool,
489 }
490 fn foo(b: Bar) {
491 match b {
492 Bar { a: .. } => {},
493 }
494 }
495 "#,
496 expect![[r#"
497 35..36 'b': Bar
498 43..95 '{ ... } }': ()
499 49..93 'match ... }': ()
500 55..56 'b': Bar
501 67..80 'Bar { a: .. }': Bar
502 76..78 '..': bool
503 84..86 '{}': ()
504 "#]],
505 );
506 }
507
508 #[test]
issue_4235_name_conflicts()509 fn issue_4235_name_conflicts() {
510 check_infer(
511 r#"
512 struct FOO {}
513 static FOO:FOO = FOO {};
514
515 impl FOO {
516 fn foo(&self) {}
517 }
518
519 fn main() {
520 let a = &FOO;
521 a.foo();
522 }
523 "#,
524 expect![[r#"
525 31..37 'FOO {}': FOO
526 63..67 'self': &FOO
527 69..71 '{}': ()
528 85..119 '{ ...o(); }': ()
529 95..96 'a': &FOO
530 99..103 '&FOO': &FOO
531 100..103 'FOO': FOO
532 109..110 'a': &FOO
533 109..116 'a.foo()': ()
534 "#]],
535 );
536 }
537
538 #[test]
issue_4465_dollar_crate_at_type()539 fn issue_4465_dollar_crate_at_type() {
540 check_infer(
541 r#"
542 pub struct Foo {}
543 pub fn anything<T>() -> T {
544 loop {}
545 }
546 macro_rules! foo {
547 () => {{
548 let r: $crate::Foo = anything();
549 r
550 }};
551 }
552 fn main() {
553 let _a = foo!();
554 }
555 "#,
556 expect![[r#"
557 44..59 '{ loop {} }': T
558 50..57 'loop {}': !
559 55..57 '{}': ()
560 !0..31 '{letr:...g();r}': Foo
561 !4..5 'r': Foo
562 !18..26 'anything': fn anything<Foo>() -> Foo
563 !18..28 'anything()': Foo
564 !29..30 'r': Foo
565 163..187 '{ ...!(); }': ()
566 173..175 '_a': Foo
567 "#]],
568 );
569 }
570
571 #[test]
issue_6811()572 fn issue_6811() {
573 check_infer(
574 r#"
575 macro_rules! profile_function {
576 () => {
577 let _a = 1;
578 let _b = 1;
579 };
580 }
581 fn main() {
582 profile_function!();
583 }
584 "#,
585 expect![[r#"
586 !3..5 '_a': i32
587 !6..7 '1': i32
588 !11..13 '_b': i32
589 !14..15 '1': i32
590 103..131 '{ ...!(); }': ()
591 "#]],
592 );
593 }
594
595 #[test]
issue_4053_diesel_where_clauses()596 fn issue_4053_diesel_where_clauses() {
597 check_infer(
598 r#"
599 trait BoxedDsl<DB> {
600 type Output;
601 fn internal_into_boxed(self) -> Self::Output;
602 }
603
604 struct SelectStatement<From, Select, Distinct, Where, Order, LimitOffset, GroupBy, Locking> {
605 order: Order,
606 }
607
608 trait QueryFragment<DB: Backend> {}
609
610 trait Into<T> { fn into(self) -> T; }
611
612 impl<F, S, D, W, O, LOf, DB> BoxedDsl<DB>
613 for SelectStatement<F, S, D, W, O, LOf, G>
614 where
615 O: Into<dyn QueryFragment<DB>>,
616 {
617 type Output = XXX;
618
619 fn internal_into_boxed(self) -> Self::Output {
620 self.order.into();
621 }
622 }
623 "#,
624 expect![[r#"
625 65..69 'self': Self
626 267..271 'self': Self
627 466..470 'self': SelectStatement<F, S, D, W, O, LOf, {unknown}, {unknown}>
628 488..522 '{ ... }': ()
629 498..502 'self': SelectStatement<F, S, D, W, O, LOf, {unknown}, {unknown}>
630 498..508 'self.order': O
631 498..515 'self.o...into()': dyn QueryFragment<DB>
632 "#]],
633 );
634 }
635
636 #[test]
issue_4953()637 fn issue_4953() {
638 check_infer(
639 r#"
640 pub struct Foo(pub i64);
641 impl Foo {
642 fn test() -> Self { Self(0i64) }
643 }
644 "#,
645 expect![[r#"
646 58..72 '{ Self(0i64) }': Foo
647 60..64 'Self': Foo(i64) -> Foo
648 60..70 'Self(0i64)': Foo
649 65..69 '0i64': i64
650 "#]],
651 );
652 check_infer(
653 r#"
654 pub struct Foo<T>(pub T);
655 impl Foo<i64> {
656 fn test() -> Self { Self(0i64) }
657 }
658 "#,
659 expect![[r#"
660 64..78 '{ Self(0i64) }': Foo<i64>
661 66..70 'Self': Foo<i64>(i64) -> Foo<i64>
662 66..76 'Self(0i64)': Foo<i64>
663 71..75 '0i64': i64
664 "#]],
665 );
666 }
667
668 #[test]
issue_4931()669 fn issue_4931() {
670 check_infer(
671 r#"
672 trait Div<T> {
673 type Output;
674 }
675
676 trait CheckedDiv: Div<()> {}
677
678 trait PrimInt: CheckedDiv<Output = ()> {
679 fn pow(self);
680 }
681
682 fn check<T: PrimInt>(i: T) {
683 i.pow();
684 }
685 "#,
686 expect![[r#"
687 117..121 'self': Self
688 148..149 'i': T
689 154..170 '{ ...w(); }': ()
690 160..161 'i': T
691 160..167 'i.pow()': ()
692 "#]],
693 );
694 }
695
696 #[test]
issue_4885()697 fn issue_4885() {
698 check_infer(
699 r#"
700 //- minicore: coerce_unsized, future
701 use core::future::Future;
702 trait Foo<R> {
703 type Bar;
704 }
705 fn foo<R, K>(key: &K) -> impl Future<Output = K::Bar>
706 where
707 K: Foo<R>,
708 {
709 bar(key)
710 }
711 fn bar<R, K>(key: &K) -> impl Future<Output = K::Bar>
712 where
713 K: Foo<R>,
714 {
715 }
716 "#,
717 expect![[r#"
718 70..73 'key': &K
719 132..148 '{ ...key) }': impl Future<Output = <K as Foo<R>>::Bar>
720 138..141 'bar': fn bar<R, K>(&K) -> impl Future<Output = <K as Foo<R>>::Bar>
721 138..146 'bar(key)': impl Future<Output = <K as Foo<R>>::Bar>
722 142..145 'key': &K
723 162..165 'key': &K
724 224..227 '{ }': ()
725 "#]],
726 );
727 }
728
729 #[test]
issue_4800()730 fn issue_4800() {
731 check_infer(
732 r#"
733 trait Debug {}
734
735 struct Foo<T>;
736
737 type E1<T> = (T, T, T);
738 type E2<T> = E1<E1<E1<(T, T, T)>>>;
739
740 impl Debug for Foo<E2<()>> {}
741
742 struct Request;
743
744 pub trait Future {
745 type Output;
746 }
747
748 pub struct PeerSet<D>;
749
750 impl<D> Service<Request> for PeerSet<D>
751 where
752 D: Discover,
753 D::Key: Debug,
754 {
755 type Error = ();
756 type Future = dyn Future<Output = Self::Error>;
757
758 fn call(&mut self) -> Self::Future {
759 loop {}
760 }
761 }
762
763 pub trait Discover {
764 type Key;
765 }
766
767 pub trait Service<Request> {
768 type Error;
769 type Future: Future<Output = Self::Error>;
770 fn call(&mut self) -> Self::Future;
771 }
772 "#,
773 expect![[r#"
774 379..383 'self': &mut PeerSet<D>
775 401..424 '{ ... }': dyn Future<Output = ()>
776 411..418 'loop {}': !
777 416..418 '{}': ()
778 575..579 'self': &mut Self
779 "#]],
780 );
781 }
782
783 #[test]
issue_4966()784 fn issue_4966() {
785 check_infer(
786 r#"
787 //- minicore: deref
788 pub trait IntoIterator {
789 type Item;
790 }
791
792 struct Repeat<A> { element: A }
793
794 struct Map<F> { f: F }
795
796 struct Vec<T> {}
797
798 impl<T> core::ops::Deref for Vec<T> {
799 type Target = [T];
800 }
801
802 fn from_iter<A, T: IntoIterator<Item = A>>(iter: T) -> Vec<A> {}
803
804 fn main() {
805 let inner = Map { f: |_: &f64| 0.0 };
806
807 let repeat = Repeat { element: inner };
808
809 let vec = from_iter(repeat);
810
811 vec.foo_bar();
812 }
813 "#,
814 expect![[r#"
815 225..229 'iter': T
816 244..246 '{}': Vec<A>
817 258..402 '{ ...r(); }': ()
818 268..273 'inner': Map<impl Fn(&f64) -> f64>
819 276..300 'Map { ... 0.0 }': Map<impl Fn(&f64) -> f64>
820 285..298 '|_: &f64| 0.0': impl Fn(&f64) -> f64
821 286..287 '_': &f64
822 295..298 '0.0': f64
823 311..317 'repeat': Repeat<Map<impl Fn(&f64) -> f64>>
824 320..345 'Repeat...nner }': Repeat<Map<impl Fn(&f64) -> f64>>
825 338..343 'inner': Map<impl Fn(&f64) -> f64>
826 356..359 'vec': Vec<IntoIterator::Item<Repeat<Map<impl Fn(&f64) -> f64>>>>
827 362..371 'from_iter': fn from_iter<IntoIterator::Item<Repeat<Map<impl Fn(&f64) -> f64>>>, Repeat<Map<impl Fn(&f64) -> f64>>>(Repeat<Map<impl Fn(&f64) -> f64>>) -> Vec<IntoIterator::Item<Repeat<Map<impl Fn(&f64) -> f64>>>>
828 362..379 'from_i...epeat)': Vec<IntoIterator::Item<Repeat<Map<impl Fn(&f64) -> f64>>>>
829 372..378 'repeat': Repeat<Map<impl Fn(&f64) -> f64>>
830 386..389 'vec': Vec<IntoIterator::Item<Repeat<Map<impl Fn(&f64) -> f64>>>>
831 386..399 'vec.foo_bar()': {unknown}
832 "#]],
833 );
834 }
835
836 #[test]
issue_6628()837 fn issue_6628() {
838 check_infer(
839 r#"
840 //- minicore: fn
841 struct S<T>();
842 impl<T> S<T> {
843 fn f(&self, _t: T) {}
844 fn g<F: FnOnce(&T)>(&self, _f: F) {}
845 }
846 fn main() {
847 let s = S();
848 s.g(|_x| {});
849 s.f(10);
850 }
851 "#,
852 expect![[r#"
853 40..44 'self': &S<T>
854 46..48 '_t': T
855 53..55 '{}': ()
856 81..85 'self': &S<T>
857 87..89 '_f': F
858 94..96 '{}': ()
859 109..160 '{ ...10); }': ()
860 119..120 's': S<i32>
861 123..124 'S': S<i32>() -> S<i32>
862 123..126 'S()': S<i32>
863 132..133 's': S<i32>
864 132..144 's.g(|_x| {})': ()
865 136..143 '|_x| {}': impl Fn(&i32)
866 137..139 '_x': &i32
867 141..143 '{}': ()
868 150..151 's': S<i32>
869 150..157 's.f(10)': ()
870 154..156 '10': i32
871 "#]],
872 );
873 }
874
875 #[test]
issue_6852()876 fn issue_6852() {
877 check_infer(
878 r#"
879 //- minicore: deref
880 use core::ops::Deref;
881
882 struct BufWriter {}
883
884 struct Mutex<T> {}
885 struct MutexGuard<'a, T> {}
886 impl<T> Mutex<T> {
887 fn lock(&self) -> MutexGuard<'_, T> {}
888 }
889 impl<'a, T: 'a> Deref for MutexGuard<'a, T> {
890 type Target = T;
891 }
892 fn flush(&self) {
893 let w: &Mutex<BufWriter>;
894 *(w.lock());
895 }
896 "#,
897 expect![[r#"
898 123..127 'self': &Mutex<T>
899 150..152 '{}': MutexGuard<'_, T>
900 234..238 'self': &{unknown}
901 240..290 '{ ...()); }': ()
902 250..251 'w': &Mutex<BufWriter>
903 276..287 '*(w.lock())': BufWriter
904 278..279 'w': &Mutex<BufWriter>
905 278..286 'w.lock()': MutexGuard<'_, BufWriter>
906 "#]],
907 );
908 }
909
910 #[test]
param_overrides_fn()911 fn param_overrides_fn() {
912 check_types(
913 r#"
914 fn example(example: i32) {
915 fn f() {}
916 example;
917 //^^^^^^^ i32
918 }
919 "#,
920 )
921 }
922
923 #[test]
lifetime_from_chalk_during_deref()924 fn lifetime_from_chalk_during_deref() {
925 check_types(
926 r#"
927 //- minicore: deref
928 struct Box<T: ?Sized> {}
929 impl<T: ?Sized> core::ops::Deref for Box<T> {
930 type Target = T;
931
932 fn deref(&self) -> &Self::Target {
933 loop {}
934 }
935 }
936
937 trait Iterator {
938 type Item;
939 }
940
941 pub struct Iter<'a, T: 'a> {
942 inner: Box<dyn IterTrait<'a, T, Item = &'a T> + 'a>,
943 }
944
945 trait IterTrait<'a, T: 'a>: Iterator<Item = &'a T> {
946 fn clone_box(&self);
947 }
948
949 fn clone_iter<T>(s: Iter<T>) {
950 s.inner.clone_box();
951 //^^^^^^^^^^^^^^^^^^^ ()
952 }
953 "#,
954 )
955 }
956
957 #[test]
issue_8686()958 fn issue_8686() {
959 check_infer(
960 r#"
961 pub trait Try: FromResidual {
962 type Output;
963 type Residual;
964 }
965 pub trait FromResidual<R = <Self as Try>::Residual> {
966 fn from_residual(residual: R) -> Self;
967 }
968
969 struct ControlFlow<B, C>;
970 impl<B, C> Try for ControlFlow<B, C> {
971 type Output = C;
972 type Residual = ControlFlow<B, !>;
973 }
974 impl<B, C> FromResidual for ControlFlow<B, C> {
975 fn from_residual(r: ControlFlow<B, !>) -> Self { ControlFlow }
976 }
977
978 fn test() {
979 ControlFlow::from_residual(ControlFlow::<u32, !>);
980 }
981 "#,
982 expect![[r#"
983 144..152 'residual': R
984 365..366 'r': ControlFlow<B, !>
985 395..410 '{ ControlFlow }': ControlFlow<B, C>
986 397..408 'ControlFlow': ControlFlow<B, C>
987 424..482 '{ ...!>); }': ()
988 430..456 'Contro...sidual': fn from_residual<ControlFlow<u32, {unknown}>, ControlFlow<u32, !>>(ControlFlow<u32, !>) -> ControlFlow<u32, {unknown}>
989 430..479 'Contro...2, !>)': ControlFlow<u32, {unknown}>
990 457..478 'Contro...32, !>': ControlFlow<u32, !>
991 "#]],
992 );
993 }
994
995 #[test]
cfg_tail()996 fn cfg_tail() {
997 // https://github.com/rust-lang/rust-analyzer/issues/8378
998 check_infer(
999 r#"
1000 fn fake_tail(){
1001 { "first" }
1002 #[cfg(never)] 9
1003 }
1004 fn multiple_fake(){
1005 { "fake" }
1006 { "fake" }
1007 { "second" }
1008 #[cfg(never)] { 11 }
1009 #[cfg(never)] 12;
1010 #[cfg(never)] 13
1011 }
1012 fn no_normal_tail(){
1013 { "third" }
1014 #[cfg(never)] 14;
1015 #[cfg(never)] 15;
1016 }
1017 fn no_actual_tail(){
1018 { "fourth" };
1019 #[cfg(never)] 14;
1020 #[cfg(never)] 15
1021 }
1022 "#,
1023 expect![[r#"
1024 14..53 '{ ...)] 9 }': ()
1025 20..31 '{ "first" }': ()
1026 22..29 '"first"': &str
1027 72..190 '{ ...] 13 }': ()
1028 78..88 '{ "fake" }': ()
1029 80..86 '"fake"': &str
1030 93..103 '{ "fake" }': ()
1031 95..101 '"fake"': &str
1032 108..120 '{ "second" }': ()
1033 110..118 '"second"': &str
1034 210..273 '{ ... 15; }': ()
1035 216..227 '{ "third" }': ()
1036 218..225 '"third"': &str
1037 293..357 '{ ...] 15 }': ()
1038 299..311 '{ "fourth" }': &str
1039 301..309 '"fourth"': &str
1040 "#]],
1041 )
1042 }
1043
1044 #[test]
impl_trait_in_option_9530()1045 fn impl_trait_in_option_9530() {
1046 check_types(
1047 r#"
1048 //- minicore: sized
1049 struct Option<T>;
1050 impl<T> Option<T> {
1051 fn unwrap(self) -> T { loop {} }
1052 }
1053 fn make() -> Option<impl Copy> { Option }
1054 trait Copy {}
1055 fn test() {
1056 let o = make();
1057 o.unwrap();
1058 //^^^^^^^^^^ impl Copy
1059 }
1060 "#,
1061 )
1062 }
1063
1064 #[test]
bare_dyn_trait_binders_9639()1065 fn bare_dyn_trait_binders_9639() {
1066 check_no_mismatches(
1067 r#"
1068 //- minicore: fn, coerce_unsized
1069 fn infix_parse<T, S>(_state: S, _level_code: &Fn(S)) -> T {
1070 loop {}
1071 }
1072
1073 fn parse_a_rule() {
1074 infix_parse((), &(|_recurse| ()))
1075 }
1076 "#,
1077 )
1078 }
1079
1080 #[test]
nested_closure()1081 fn nested_closure() {
1082 check_types(
1083 r#"
1084 //- minicore: fn, option
1085
1086 fn map<T, U>(o: Option<T>, f: impl FnOnce(T) -> U) -> Option<U> { loop {} }
1087
1088 fn test() {
1089 let o = Some(Some(2));
1090 map(o, |s| map(s, |x| x));
1091 // ^ i32
1092 }
1093 "#,
1094 );
1095 }
1096
1097 #[test]
call_expected_type_closure()1098 fn call_expected_type_closure() {
1099 check_types(
1100 r#"
1101 //- minicore: fn, option
1102
1103 fn map<T, U>(o: Option<T>, f: impl FnOnce(T) -> U) -> Option<U> { loop {} }
1104 struct S {
1105 field: u32
1106 }
1107
1108 fn test() {
1109 let o = Some(S { field: 2 });
1110 let _: Option<()> = map(o, |s| { s.field; });
1111 // ^^^^^^^ u32
1112 }
1113 "#,
1114 );
1115 }
1116
1117 #[test]
coerce_diesel_panic()1118 fn coerce_diesel_panic() {
1119 check_no_mismatches(
1120 r#"
1121 //- minicore: option
1122
1123 trait TypeMetadata {
1124 type MetadataLookup;
1125 }
1126
1127 pub struct Output<'a, T, DB>
1128 where
1129 DB: TypeMetadata,
1130 DB::MetadataLookup: 'a,
1131 {
1132 out: T,
1133 metadata_lookup: Option<&'a DB::MetadataLookup>,
1134 }
1135
1136 impl<'a, T, DB: TypeMetadata> Output<'a, T, DB> {
1137 pub fn new(out: T, metadata_lookup: &'a DB::MetadataLookup) -> Self {
1138 Output {
1139 out,
1140 metadata_lookup: Some(metadata_lookup),
1141 }
1142 }
1143 }
1144 "#,
1145 );
1146 }
1147
1148 #[test]
bitslice_panic()1149 fn bitslice_panic() {
1150 check_no_mismatches(
1151 r#"
1152 //- minicore: option, deref
1153
1154 pub trait BitView {
1155 type Store;
1156 }
1157
1158 pub struct Lsb0;
1159
1160 pub struct BitArray<V: BitView> { }
1161
1162 pub struct BitSlice<T> { }
1163
1164 impl<V: BitView> core::ops::Deref for BitArray<V> {
1165 type Target = BitSlice<V::Store>;
1166 }
1167
1168 impl<T> BitSlice<T> {
1169 pub fn split_first(&self) -> Option<(T, &Self)> { loop {} }
1170 }
1171
1172 fn multiexp_inner() {
1173 let exp: &BitArray<Foo>;
1174 exp.split_first();
1175 }
1176 "#,
1177 );
1178 }
1179
1180 #[test]
macro_expands_to_impl_trait()1181 fn macro_expands_to_impl_trait() {
1182 check_no_mismatches(
1183 r#"
1184 trait Foo {}
1185
1186 macro_rules! ty {
1187 () => {
1188 impl Foo
1189 }
1190 }
1191
1192 fn foo(_: ty!()) {}
1193
1194 fn bar() {
1195 foo(());
1196 }
1197 "#,
1198 )
1199 }
1200
1201 #[test]
nested_macro_in_fn_params()1202 fn nested_macro_in_fn_params() {
1203 check_no_mismatches(
1204 r#"
1205 macro_rules! U32Inner {
1206 () => {
1207 u32
1208 };
1209 }
1210
1211 macro_rules! U32 {
1212 () => {
1213 U32Inner!()
1214 };
1215 }
1216
1217 fn mamba(a: U32!(), p: u32) -> u32 {
1218 a
1219 }
1220 "#,
1221 )
1222 }
1223
1224 #[test]
for_loop_block_expr_iterable()1225 fn for_loop_block_expr_iterable() {
1226 check_infer(
1227 r#"
1228 //- minicore: iterator
1229 fn test() {
1230 for _ in { let x = 0; } {
1231 let y = 0;
1232 }
1233 }
1234 "#,
1235 expect![[r#"
1236 10..68 '{ ... } }': ()
1237 16..66 'for _ ... }': fn into_iter<()>(()) -> <() as IntoIterator>::IntoIter
1238 16..66 'for _ ... }': IntoIterator::IntoIter<()>
1239 16..66 'for _ ... }': !
1240 16..66 'for _ ... }': IntoIterator::IntoIter<()>
1241 16..66 'for _ ... }': &mut IntoIterator::IntoIter<()>
1242 16..66 'for _ ... }': fn next<IntoIterator::IntoIter<()>>(&mut IntoIterator::IntoIter<()>) -> Option<<IntoIterator::IntoIter<()> as Iterator>::Item>
1243 16..66 'for _ ... }': Option<Iterator::Item<IntoIterator::IntoIter<()>>>
1244 16..66 'for _ ... }': ()
1245 16..66 'for _ ... }': ()
1246 16..66 'for _ ... }': ()
1247 20..21 '_': Iterator::Item<IntoIterator::IntoIter<()>>
1248 25..39 '{ let x = 0; }': ()
1249 31..32 'x': i32
1250 35..36 '0': i32
1251 40..66 '{ ... }': ()
1252 54..55 'y': i32
1253 58..59 '0': i32
1254 "#]],
1255 );
1256 }
1257
1258 #[test]
while_loop_block_expr_iterable()1259 fn while_loop_block_expr_iterable() {
1260 check_infer(
1261 r#"
1262 fn test() {
1263 while { true } {
1264 let y = 0;
1265 }
1266 }
1267 "#,
1268 expect![[r#"
1269 10..59 '{ ... } }': ()
1270 16..57 'while ... }': ()
1271 22..30 '{ true }': bool
1272 24..28 'true': bool
1273 31..57 '{ ... }': ()
1274 45..46 'y': i32
1275 49..50 '0': i32
1276 "#]],
1277 );
1278 }
1279
1280 #[test]
bug_11242()1281 fn bug_11242() {
1282 // FIXME: wrong, should be u32
1283 check_types(
1284 r#"
1285 fn foo<A, B>()
1286 where
1287 A: IntoIterator<Item = u32>,
1288 B: IntoIterator<Item = usize>,
1289 {
1290 let _x: <A as IntoIterator>::Item;
1291 // ^^ {unknown}
1292 }
1293
1294 pub trait Iterator {
1295 type Item;
1296 }
1297
1298 pub trait IntoIterator {
1299 type Item;
1300 type IntoIter: Iterator<Item = Self::Item>;
1301 }
1302
1303 impl<I: Iterator> IntoIterator for I {
1304 type Item = I::Item;
1305 type IntoIter = I;
1306 }
1307 "#,
1308 );
1309 }
1310
1311 #[test]
bug_11659()1312 fn bug_11659() {
1313 check_no_mismatches(
1314 r#"
1315 struct LinkArray<const N: usize, LD>(LD);
1316 fn f<const N: usize, LD>(x: LD) -> LinkArray<N, LD> {
1317 let r = LinkArray::<N, LD>(x);
1318 r
1319 }
1320
1321 fn test() {
1322 let x = f::<2, i32>(5);
1323 let y = LinkArray::<52, LinkArray<2, i32>>(x);
1324 }
1325 "#,
1326 );
1327 check_no_mismatches(
1328 r#"
1329 struct LinkArray<LD, const N: usize>(LD);
1330 fn f<const N: usize, LD>(x: LD) -> LinkArray<LD, N> {
1331 let r = LinkArray::<LD, N>(x);
1332 r
1333 }
1334
1335 fn test() {
1336 let x = f::<i32, 2>(5);
1337 let y = LinkArray::<LinkArray<i32, 2>, 52>(x);
1338 }
1339 "#,
1340 );
1341 }
1342
1343 #[test]
const_generic_error_tolerance()1344 fn const_generic_error_tolerance() {
1345 check_no_mismatches(
1346 r#"
1347 #[lang = "sized"]
1348 pub trait Sized {}
1349
1350 struct CT<const N: usize, T>(T);
1351 struct TC<T, const N: usize>(T);
1352 fn f<const N: usize, T>(x: T) -> (CT<N, T>, TC<T, N>) {
1353 let l = CT::<N, T>(x);
1354 let r = TC::<N, T>(x);
1355 (l, r)
1356 }
1357
1358 trait TR1<const N: usize>;
1359 trait TR2<const N: usize>;
1360
1361 impl<const N: usize, T> TR1<N> for CT<N, T>;
1362 impl<const N: usize, T> TR1<5> for TC<T, N>;
1363 impl<const N: usize, T> TR2<N> for CT<T, N>;
1364
1365 trait TR3<const N: usize> {
1366 fn tr3(&self) -> &Self;
1367 }
1368
1369 impl<const N: usize, T> TR3<5> for TC<T, N> {
1370 fn tr3(&self) -> &Self {
1371 self
1372 }
1373 }
1374
1375 impl<const N: usize, T> TR3<Item = 5> for TC<T, N> {}
1376 impl<const N: usize, T> TR3<T> for TC<T, N> {}
1377
1378 fn impl_trait<const N: usize>(inp: impl TR1<N>) {}
1379 fn dyn_trait<const N: usize>(inp: &dyn TR2<N>) {}
1380 fn impl_trait_bad<'a, const N: usize>(inp: impl TR1<i32>) -> impl TR1<'a, i32> {}
1381 fn impl_trait_very_bad<const N: usize>(inp: impl TR1<Item = i32>) -> impl TR1<'a, Item = i32, 5, Foo = N> {}
1382
1383 fn test() {
1384 f::<2, i32>(5);
1385 f::<2, 2>(5);
1386 f(5);
1387 f::<i32>(5);
1388 CT::<52, CT<2, i32>>(x);
1389 CT::<CT<2, i32>>(x);
1390 impl_trait_bad(5);
1391 impl_trait_bad(12);
1392 TR3<5>::tr3();
1393 TR3<{ 2+3 }>::tr3();
1394 TC::<i32, 10>(5).tr3();
1395 TC::<i32, 20>(5).tr3();
1396 TC::<i32, i32>(5).tr3();
1397 TC::<i32, { 7 + 3 }>(5).tr3();
1398 }
1399 "#,
1400 );
1401 }
1402
1403 #[test]
const_generic_impl_trait()1404 fn const_generic_impl_trait() {
1405 check_no_mismatches(
1406 r#"
1407 //- minicore: from
1408
1409 struct Foo<T, const M: usize>;
1410
1411 trait Tr<T> {
1412 fn f(T) -> Self;
1413 }
1414
1415 impl<T, const M: usize> Tr<[T; M]> for Foo<T, M> {
1416 fn f(_: [T; M]) -> Self {
1417 Self
1418 }
1419 }
1420
1421 fn test() {
1422 Foo::f([1, 2, 7, 10]);
1423 }
1424 "#,
1425 );
1426 }
1427
1428 #[test]
nalgebra_factorial()1429 fn nalgebra_factorial() {
1430 check_no_mismatches(
1431 r#"
1432 const FACTORIAL: [u128; 4] = [1, 1, 2, 6];
1433
1434 fn factorial(n: usize) -> u128 {
1435 match FACTORIAL.get(n) {
1436 Some(f) => *f,
1437 None => panic!("{}! is greater than u128::MAX", n),
1438 }
1439 }
1440 "#,
1441 )
1442 }
1443
1444 #[test]
regression_11688_1()1445 fn regression_11688_1() {
1446 check_no_mismatches(
1447 r#"
1448 pub struct Buffer<T>(T);
1449 type Writer = Buffer<u8>;
1450 impl<T> Buffer<T> {
1451 fn extend_from_array<const N: usize>(&mut self, xs: &[T; N]) {
1452 loop {}
1453 }
1454 }
1455 trait Encode<S> {
1456 fn encode(self, w: &mut Writer, s: &mut S);
1457 }
1458 impl<S> Encode<S> for u8 {
1459 fn encode(self, w: &mut Writer, _: &mut S) {
1460 w.extend_from_array(&self.to_le_bytes());
1461 }
1462 }
1463 "#,
1464 );
1465 }
1466
1467 #[test]
regression_11688_2()1468 fn regression_11688_2() {
1469 check_types(
1470 r#"
1471 union MaybeUninit<T> {
1472 uninit: (),
1473 value: T,
1474 }
1475
1476 impl<T> MaybeUninit<T> {
1477 fn uninit_array<const LEN: usize>() -> [Self; LEN] {
1478 loop {}
1479 }
1480 }
1481
1482 fn main() {
1483 let x = MaybeUninit::<i32>::uninit_array::<1>();
1484 //^ [MaybeUninit<i32>; 1]
1485 }
1486 "#,
1487 );
1488 }
1489
1490 #[test]
regression_11688_3()1491 fn regression_11688_3() {
1492 check_types(
1493 r#"
1494 //- minicore: iterator
1495 struct Ar<T, const N: u8>(T);
1496 fn f<const LEN: usize, T, const BASE: u8>(
1497 num_zeros: usize,
1498 ) -> &dyn Iterator<Item = [Ar<T, BASE>; LEN]> {
1499 loop {}
1500 }
1501 fn dynamic_programming() {
1502 let board = f::<9, u8, 7>(1).next();
1503 //^^^^^ Option<[Ar<u8, 7>; 9]>
1504 }
1505 "#,
1506 );
1507 }
1508
1509 #[test]
regression_11688_4()1510 fn regression_11688_4() {
1511 check_types(
1512 r#"
1513 trait Bar<const C: usize> {
1514 fn baz(&self) -> [i32; C];
1515 }
1516
1517 fn foo(x: &dyn Bar<2>) {
1518 x.baz();
1519 //^^^^^^^ [i32; 2]
1520 }
1521 "#,
1522 )
1523 }
1524
1525 #[test]
gat_crash_1()1526 fn gat_crash_1() {
1527 check_no_mismatches(
1528 r#"
1529 trait ATrait {}
1530
1531 trait Crash {
1532 type Member<const N: usize>: ATrait;
1533 fn new<const N: usize>() -> Self::Member<N>;
1534 }
1535
1536 fn test<T: Crash>() {
1537 T::new();
1538 }
1539 "#,
1540 );
1541 }
1542
1543 #[test]
gat_crash_2()1544 fn gat_crash_2() {
1545 check_no_mismatches(
1546 r#"
1547 pub struct InlineStorage {}
1548
1549 pub struct InlineStorageHandle<T: ?Sized> {}
1550
1551 pub unsafe trait Storage {
1552 type Handle<T: ?Sized>;
1553 fn create<T: ?Sized>() -> Self::Handle<T>;
1554 }
1555
1556 unsafe impl Storage for InlineStorage {
1557 type Handle<T: ?Sized> = InlineStorageHandle<T>;
1558 }
1559 "#,
1560 );
1561 }
1562
1563 #[test]
gat_crash_3()1564 fn gat_crash_3() {
1565 check_no_mismatches(
1566 r#"
1567 trait Collection {
1568 type Item;
1569 type Member<T>: Collection<Item = T>;
1570 fn add(&mut self, value: Self::Item) -> Result<(), Self::Error>;
1571 }
1572 struct ConstGen<T, const N: usize> {
1573 data: [T; N],
1574 }
1575 impl<T, const N: usize> Collection for ConstGen<T, N> {
1576 type Item = T;
1577 type Member<U> = ConstGen<U, N>;
1578 }
1579 "#,
1580 );
1581 }
1582
1583 #[test]
cfgd_out_self_param()1584 fn cfgd_out_self_param() {
1585 cov_mark::check!(cfgd_out_self_param);
1586 check_no_mismatches(
1587 r#"
1588 struct S;
1589 impl S {
1590 fn f(#[cfg(never)] &self) {}
1591 }
1592
1593 fn f(s: S) {
1594 s.f();
1595 }
1596 "#,
1597 );
1598 }
1599
1600 #[test]
rust_161_option_clone()1601 fn rust_161_option_clone() {
1602 check_types(
1603 r#"
1604 //- minicore: option, drop
1605
1606 fn test(o: &Option<i32>) {
1607 o.my_clone();
1608 //^^^^^^^^^^^^ Option<i32>
1609 }
1610
1611 pub trait MyClone: Sized {
1612 fn my_clone(&self) -> Self;
1613 }
1614
1615 impl<T> const MyClone for Option<T>
1616 where
1617 T: ~const MyClone + ~const Drop + ~const Destruct,
1618 {
1619 fn my_clone(&self) -> Self {
1620 match self {
1621 Some(x) => Some(x.my_clone()),
1622 None => None,
1623 }
1624 }
1625 }
1626
1627 impl const MyClone for i32 {
1628 fn my_clone(&self) -> Self {
1629 *self
1630 }
1631 }
1632
1633 pub trait Destruct {}
1634
1635 impl<T: ?Sized> const Destruct for T {}
1636 "#,
1637 );
1638 }
1639
1640 #[test]
rust_162_option_clone()1641 fn rust_162_option_clone() {
1642 check_types(
1643 r#"
1644 //- minicore: option, drop
1645
1646 fn test(o: &Option<i32>) {
1647 o.my_clone();
1648 //^^^^^^^^^^^^ Option<i32>
1649 }
1650
1651 pub trait MyClone: Sized {
1652 fn my_clone(&self) -> Self;
1653 }
1654
1655 impl<T> const MyClone for Option<T>
1656 where
1657 T: ~const MyClone + ~const Destruct,
1658 {
1659 fn my_clone(&self) -> Self {
1660 match self {
1661 Some(x) => Some(x.my_clone()),
1662 None => None,
1663 }
1664 }
1665 }
1666
1667 impl const MyClone for i32 {
1668 fn my_clone(&self) -> Self {
1669 *self
1670 }
1671 }
1672
1673 #[lang = "destruct"]
1674 pub trait Destruct {}
1675 "#,
1676 );
1677 }
1678
1679 #[test]
tuple_struct_pattern_with_unmatched_args_crash()1680 fn tuple_struct_pattern_with_unmatched_args_crash() {
1681 check_infer(
1682 r#"
1683 struct S(usize);
1684 fn main() {
1685 let S(.., a, b) = S(1);
1686 let (.., a, b) = (1,);
1687 }
1688 "#,
1689 expect![[r#"
1690 27..85 '{ ...1,); }': ()
1691 37..48 'S(.., a, b)': S
1692 43..44 'a': usize
1693 46..47 'b': {unknown}
1694 51..52 'S': S(usize) -> S
1695 51..55 'S(1)': S
1696 53..54 '1': usize
1697 65..75 '(.., a, b)': (i32, {unknown})
1698 70..71 'a': i32
1699 73..74 'b': {unknown}
1700 78..82 '(1,)': (i32,)
1701 79..80 '1': i32
1702 "#]],
1703 );
1704 }
1705
1706 #[test]
trailing_empty_macro()1707 fn trailing_empty_macro() {
1708 check_no_mismatches(
1709 r#"
1710 macro_rules! m2 {
1711 ($($t:tt)*) => {$($t)*};
1712 }
1713
1714 fn macrostmts() -> u8 {
1715 m2! { 0 }
1716 m2! {}
1717 }
1718 "#,
1719 );
1720 }
1721
1722 #[test]
dyn_with_unresolved_trait()1723 fn dyn_with_unresolved_trait() {
1724 check_types(
1725 r#"
1726 fn foo(a: &dyn DoesNotExist) {
1727 a.bar();
1728 //^&{unknown}
1729 }
1730 "#,
1731 );
1732 }
1733
1734 #[test]
self_assoc_with_const_generics_crash()1735 fn self_assoc_with_const_generics_crash() {
1736 check_no_mismatches(
1737 r#"
1738 trait Trait { type Item; }
1739 impl<T, const N: usize> Trait for [T; N] {
1740 type Item = ();
1741 fn f<U>(_: Self::Item) {}
1742 }
1743 "#,
1744 );
1745 }
1746
1747 #[test]
unsize_array_with_inference_variable()1748 fn unsize_array_with_inference_variable() {
1749 check_types(
1750 r#"
1751 //- minicore: try, slice
1752 use core::ops::ControlFlow;
1753 fn foo() -> ControlFlow<(), [usize; 1]> { loop {} }
1754 fn bar() -> ControlFlow<(), ()> {
1755 let a = foo()?.len();
1756 //^ usize
1757 ControlFlow::Continue(())
1758 }
1759 "#,
1760 );
1761 }
1762
1763 #[test]
assoc_type_shorthand_with_gats_in_binders()1764 fn assoc_type_shorthand_with_gats_in_binders() {
1765 // c.f. test `issue_4885()`
1766 check_no_mismatches(
1767 r#"
1768 trait Gats {
1769 type Assoc<T>;
1770 }
1771 trait Foo<T> {}
1772
1773 struct Bar<'a, B: Gats, A> {
1774 field: &'a dyn Foo<B::Assoc<A>>,
1775 }
1776
1777 fn foo(b: Bar) {
1778 let _ = b.field;
1779 }
1780 "#,
1781 );
1782 }
1783
1784 #[test]
regression_14305()1785 fn regression_14305() {
1786 check_no_mismatches(
1787 r#"
1788 //- minicore: add
1789 trait Tr {}
1790 impl Tr for [u8; C] {}
1791 const C: usize = 2 + 2;
1792 "#,
1793 );
1794 }
1795
1796 #[test]
regression_14456()1797 fn regression_14456() {
1798 check_types(
1799 r#"
1800 //- minicore: future
1801 async fn x() {}
1802 fn f() {
1803 let fut = x();
1804 let t = [0u8; { let a = 2 + 2; a }];
1805 //^ [u8; 4]
1806 }
1807 "#,
1808 );
1809 }
1810
1811 #[test]
regression_14164()1812 fn regression_14164() {
1813 check_types(
1814 r#"
1815 trait Rec {
1816 type K;
1817 type Rebind<Tok>: Rec<K = Tok>;
1818 }
1819
1820 trait Expr<K> {
1821 type Part: Rec<K = K>;
1822 fn foo(_: <Self::Part as Rec>::Rebind<i32>) {}
1823 }
1824
1825 struct Head<K>(K);
1826 impl<K> Rec for Head<K> {
1827 type K = K;
1828 type Rebind<Tok> = Head<Tok>;
1829 }
1830
1831 fn test<E>()
1832 where
1833 E: Expr<usize, Part = Head<usize>>,
1834 {
1835 let head;
1836 //^^^^ Head<i32>
1837 E::foo(head);
1838 }
1839 "#,
1840 );
1841 }
1842
1843 #[test]
match_ergonomics_with_binding_modes_interaction()1844 fn match_ergonomics_with_binding_modes_interaction() {
1845 check_types(
1846 r"
1847 enum E { A }
1848 fn foo() {
1849 match &E::A {
1850 b @ (x @ E::A | x) => {
1851 b;
1852 //^ &E
1853 x;
1854 //^ &E
1855 }
1856 }
1857 }",
1858 );
1859 }
1860
1861 #[test]
regression_14844()1862 fn regression_14844() {
1863 check_no_mismatches(
1864 r#"
1865 pub type Ty = Unknown;
1866
1867 pub struct Inner<T>();
1868
1869 pub struct Outer {
1870 pub inner: Inner<Ty>,
1871 }
1872
1873 fn main() {
1874 _ = Outer {
1875 inner: Inner::<i32>(),
1876 };
1877 }
1878 "#,
1879 );
1880 check_no_mismatches(
1881 r#"
1882 pub const ONE: usize = 1;
1883
1884 pub struct Inner<const P: usize>();
1885
1886 pub struct Outer {
1887 pub inner: Inner<ONE>,
1888 }
1889
1890 fn main() {
1891 _ = Outer {
1892 inner: Inner::<1>(),
1893 };
1894 }
1895 "#,
1896 );
1897 check_no_mismatches(
1898 r#"
1899 pub const ONE: usize = unknown();
1900
1901 pub struct Inner<const P: usize>();
1902
1903 pub struct Outer {
1904 pub inner: Inner<ONE>,
1905 }
1906
1907 fn main() {
1908 _ = Outer {
1909 inner: Inner::<1>(),
1910 };
1911 }
1912 "#,
1913 );
1914 check_no_mismatches(
1915 r#"
1916 pub const N: usize = 2 + 2;
1917
1918 fn f(t: [u8; N]) {}
1919
1920 fn main() {
1921 let a = [1, 2, 3, 4];
1922 f(a);
1923 let b = [1; 4];
1924 let c: [u8; N] = b;
1925 let d = [1; N];
1926 let e: [u8; N] = d;
1927 let f = [1; N];
1928 let g = match f {
1929 [a, b, c, d] => a + b + c + d,
1930 };
1931 }
1932 "#,
1933 );
1934 }
1935
1936 #[test]
regression_14844_2()1937 fn regression_14844_2() {
1938 check_no_mismatches(
1939 r#"
1940 //- minicore: fn
1941 pub const ONE: usize = 1;
1942
1943 pub type MyInner = Inner<ONE>;
1944
1945 pub struct Inner<const P: usize>();
1946
1947 impl Inner<1> {
1948 fn map<F>(&self, func: F) -> bool
1949 where
1950 F: Fn(&MyInner) -> bool,
1951 {
1952 func(self)
1953 }
1954 }
1955 "#,
1956 );
1957 }
1958
1959 #[test]
dont_crash_on_slice_unsizing()1960 fn dont_crash_on_slice_unsizing() {
1961 check_no_mismatches(
1962 r#"
1963 //- minicore: slice, unsize, coerce_unsized
1964 trait Tr {
1965 fn f(self);
1966 }
1967
1968 impl Tr for [i32] {
1969 fn f(self) {
1970 let t;
1971 x(t);
1972 }
1973 }
1974
1975 fn x(a: [i32; 4]) {
1976 let b = a.f();
1977 }
1978 "#,
1979 );
1980 }
1981