]> git.lizzy.rs Git - rust.git/blob - src/tools/rust-analyzer/crates/hir-ty/src/tests/regression.rs
Add 'src/tools/rust-analyzer/' from commit '977e12a0bdc3e329af179ef3a9d466af9eb613bb'
[rust.git] / src / tools / rust-analyzer / crates / hir-ty / src / tests / regression.rs
1 use expect_test::expect;
2
3 use super::{check_infer, check_no_mismatches, check_types};
4
5 #[test]
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]
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]
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]
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]
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]
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]
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]
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]
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]
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]
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]
245 fn infer_std_crash_5() {
246     // taken from rustc
247     check_infer(
248         r#"
249         fn extra_compiler_flags() {
250             for content in doesnt_matter {
251                 let name = if doesnt_matter {
252                     first
253                 } else {
254                     &content
255                 };
256
257                 let content = if ICE_REPORT_COMPILER_FLAGS_STRIP_VALUE.contains(&name) {
258                     name
259                 } else {
260                     content
261                 };
262             }
263         }
264         "#,
265         expect![[r#"
266             26..322 '{     ...   } }': ()
267             32..320 'for co...     }': ()
268             36..43 'content': {unknown}
269             47..60 'doesnt_matter': {unknown}
270             61..320 '{     ...     }': ()
271             75..79 'name': &{unknown}
272             82..166 'if doe...     }': &{unknown}
273             85..98 'doesnt_matter': bool
274             99..128 '{     ...     }': &{unknown}
275             113..118 'first': &{unknown}
276             134..166 '{     ...     }': &{unknown}
277             148..156 '&content': &{unknown}
278             149..156 'content': {unknown}
279             181..188 'content': &{unknown}
280             191..313 'if ICE...     }': &{unknown}
281             194..231 'ICE_RE..._VALUE': {unknown}
282             194..247 'ICE_RE...&name)': bool
283             241..246 '&name': &&{unknown}
284             242..246 'name': &{unknown}
285             248..276 '{     ...     }': &{unknown}
286             262..266 'name': &{unknown}
287             282..313 '{     ...     }': {unknown}
288             296..303 'content': {unknown}
289         "#]],
290     );
291 }
292
293 #[test]
294 fn infer_nested_generics_crash() {
295     // another crash found typechecking rustc
296     check_infer(
297         r#"
298         struct Canonical<V> {
299             value: V,
300         }
301         struct QueryResponse<V> {
302             value: V,
303         }
304         fn test<R>(query_response: Canonical<QueryResponse<R>>) {
305             &query_response.value;
306         }
307         "#,
308         expect![[r#"
309             91..105 'query_response': Canonical<QueryResponse<R>>
310             136..166 '{     ...lue; }': ()
311             142..163 '&query....value': &QueryResponse<R>
312             143..157 'query_response': Canonical<QueryResponse<R>>
313             143..163 'query_....value': QueryResponse<R>
314         "#]],
315     );
316 }
317
318 #[test]
319 fn infer_paren_macro_call() {
320     check_infer(
321         r#"
322         macro_rules! bar { () => {0u32} }
323         fn test() {
324             let a = (bar!());
325         }
326         "#,
327         expect![[r#"
328             !0..4 '0u32': u32
329             44..69 '{     ...()); }': ()
330             54..55 'a': u32
331         "#]],
332     );
333 }
334
335 #[test]
336 fn infer_array_macro_call() {
337     check_infer(
338         r#"
339         macro_rules! bar { () => {0u32} }
340         fn test() {
341             let a = [bar!()];
342         }
343         "#,
344         expect![[r#"
345             !0..4 '0u32': u32
346             44..69 '{     ...()]; }': ()
347             54..55 'a': [u32; 1]
348             58..66 '[bar!()]': [u32; 1]
349         "#]],
350     );
351 }
352
353 #[test]
354 fn bug_1030() {
355     check_infer(
356         r#"
357         struct HashSet<T, H>;
358         struct FxHasher;
359         type FxHashSet<T> = HashSet<T, FxHasher>;
360
361         impl<T, H> HashSet<T, H> {
362             fn default() -> HashSet<T, H> {}
363         }
364
365         pub fn main_loop() {
366             FxHashSet::default();
367         }
368         "#,
369         expect![[r#"
370             143..145 '{}': HashSet<T, H>
371             168..197 '{     ...t(); }': ()
372             174..192 'FxHash...efault': fn default<{unknown}, FxHasher>() -> HashSet<{unknown}, FxHasher>
373             174..194 'FxHash...ault()': HashSet<{unknown}, FxHasher>
374         "#]],
375     );
376 }
377
378 #[test]
379 fn issue_2669() {
380     check_infer(
381         r#"
382         trait A {}
383         trait Write {}
384         struct Response<T> {}
385
386         trait D {
387             fn foo();
388         }
389
390         impl<T:A> D for Response<T> {
391             fn foo() {
392                 end();
393                 fn end<W: Write>() {
394                     let _x: T =  loop {};
395                 }
396             }
397         }
398         "#,
399         expect![[r#"
400             119..214 '{     ...     }': ()
401             129..132 'end': fn end<{unknown}>()
402             129..134 'end()': ()
403             163..208 '{     ...     }': ()
404             181..183 '_x': !
405             190..197 'loop {}': !
406             195..197 '{}': ()
407         "#]],
408     )
409 }
410
411 #[test]
412 fn issue_2705() {
413     check_infer(
414         r#"
415         trait Trait {}
416         fn test() {
417             <Trait<u32>>::foo()
418         }
419         "#,
420         expect![[r#"
421             25..52 '{     ...oo() }': ()
422             31..48 '<Trait...>::foo': {unknown}
423             31..50 '<Trait...:foo()': ()
424         "#]],
425     );
426 }
427
428 #[test]
429 fn issue_2683_chars_impl() {
430     check_types(
431         r#"
432 //- minicore: iterator
433 pub struct Chars<'a> {}
434 impl<'a> Iterator for Chars<'a> {
435     type Item = char;
436     fn next(&mut self) -> Option<char> { loop {} }
437 }
438
439 fn test() {
440     let chars: Chars<'_>;
441     (chars.next(), chars.nth(1));
442 } //^^^^^^^^^^^^^^^^^^^^^^^^^^^^ (Option<char>, Option<char>)
443 "#,
444     );
445 }
446
447 #[test]
448 fn issue_3999_slice() {
449     check_infer(
450         r#"
451         fn foo(params: &[usize]) {
452             match params {
453                 [ps @ .., _] => {}
454             }
455         }
456         "#,
457         expect![[r#"
458             7..13 'params': &[usize]
459             25..80 '{     ...   } }': ()
460             31..78 'match ...     }': ()
461             37..43 'params': &[usize]
462             54..66 '[ps @ .., _]': [usize]
463             55..62 'ps @ ..': &[usize]
464             60..62 '..': [usize]
465             64..65 '_': usize
466             70..72 '{}': ()
467         "#]],
468     );
469 }
470
471 #[test]
472 fn issue_3999_struct() {
473     // rust-analyzer should not panic on seeing this malformed
474     // record pattern.
475     check_infer(
476         r#"
477         struct Bar {
478             a: bool,
479         }
480         fn foo(b: Bar) {
481             match b {
482                 Bar { a: .. } => {},
483             }
484         }
485         "#,
486         expect![[r#"
487             35..36 'b': Bar
488             43..95 '{     ...   } }': ()
489             49..93 'match ...     }': ()
490             55..56 'b': Bar
491             67..80 'Bar { a: .. }': Bar
492             76..78 '..': bool
493             84..86 '{}': ()
494         "#]],
495     );
496 }
497
498 #[test]
499 fn issue_4235_name_conflicts() {
500     check_infer(
501         r#"
502         struct FOO {}
503         static FOO:FOO = FOO {};
504
505         impl FOO {
506             fn foo(&self) {}
507         }
508
509         fn main() {
510             let a = &FOO;
511             a.foo();
512         }
513         "#,
514         expect![[r#"
515             31..37 'FOO {}': FOO
516             63..67 'self': &FOO
517             69..71 '{}': ()
518             85..119 '{     ...o(); }': ()
519             95..96 'a': &FOO
520             99..103 '&FOO': &FOO
521             100..103 'FOO': FOO
522             109..110 'a': &FOO
523             109..116 'a.foo()': ()
524         "#]],
525     );
526 }
527
528 #[test]
529 fn issue_4465_dollar_crate_at_type() {
530     check_infer(
531         r#"
532         pub struct Foo {}
533         pub fn anything<T>() -> T {
534             loop {}
535         }
536         macro_rules! foo {
537             () => {{
538                 let r: $crate::Foo = anything();
539                 r
540             }};
541         }
542         fn main() {
543             let _a = foo!();
544         }
545         "#,
546         expect![[r#"
547             44..59 '{     loop {} }': T
548             50..57 'loop {}': !
549             55..57 '{}': ()
550             !0..31 '{letr:...g();r}': Foo
551             !4..5 'r': Foo
552             !18..26 'anything': fn anything<Foo>() -> Foo
553             !18..28 'anything()': Foo
554             !29..30 'r': Foo
555             163..187 '{     ...!(); }': ()
556             173..175 '_a': Foo
557         "#]],
558     );
559 }
560
561 #[test]
562 fn issue_6811() {
563     check_infer(
564         r#"
565         macro_rules! profile_function {
566             () => {
567                 let _a = 1;
568                 let _b = 1;
569             };
570         }
571         fn main() {
572             profile_function!();
573         }
574         "#,
575         expect![[r#"
576             !0..16 'let_a=...t_b=1;': ()
577             !3..5 '_a': i32
578             !6..7 '1': i32
579             !11..13 '_b': i32
580             !14..15 '1': i32
581             103..131 '{     ...!(); }': ()
582         "#]],
583     );
584 }
585
586 #[test]
587 fn issue_4053_diesel_where_clauses() {
588     check_infer(
589         r#"
590         trait BoxedDsl<DB> {
591             type Output;
592             fn internal_into_boxed(self) -> Self::Output;
593         }
594
595         struct SelectStatement<From, Select, Distinct, Where, Order, LimitOffset, GroupBy, Locking> {
596             order: Order,
597         }
598
599         trait QueryFragment<DB: Backend> {}
600
601         trait Into<T> { fn into(self) -> T; }
602
603         impl<F, S, D, W, O, LOf, DB> BoxedDsl<DB>
604             for SelectStatement<F, S, D, W, O, LOf, G>
605         where
606             O: Into<dyn QueryFragment<DB>>,
607         {
608             type Output = XXX;
609
610             fn internal_into_boxed(self) -> Self::Output {
611                 self.order.into();
612             }
613         }
614         "#,
615         expect![[r#"
616             65..69 'self': Self
617             267..271 'self': Self
618             466..470 'self': SelectStatement<F, S, D, W, O, LOf, {unknown}, {unknown}>
619             488..522 '{     ...     }': ()
620             498..502 'self': SelectStatement<F, S, D, W, O, LOf, {unknown}, {unknown}>
621             498..508 'self.order': O
622             498..515 'self.o...into()': dyn QueryFragment<DB>
623         "#]],
624     );
625 }
626
627 #[test]
628 fn issue_4953() {
629     check_infer(
630         r#"
631         pub struct Foo(pub i64);
632         impl Foo {
633             fn test() -> Self { Self(0i64) }
634         }
635         "#,
636         expect![[r#"
637             58..72 '{ Self(0i64) }': Foo
638             60..64 'Self': Foo(i64) -> Foo
639             60..70 'Self(0i64)': Foo
640             65..69 '0i64': i64
641         "#]],
642     );
643     check_infer(
644         r#"
645         pub struct Foo<T>(pub T);
646         impl Foo<i64> {
647             fn test() -> Self { Self(0i64) }
648         }
649         "#,
650         expect![[r#"
651             64..78 '{ Self(0i64) }': Foo<i64>
652             66..70 'Self': Foo<i64>(i64) -> Foo<i64>
653             66..76 'Self(0i64)': Foo<i64>
654             71..75 '0i64': i64
655         "#]],
656     );
657 }
658
659 #[test]
660 fn issue_4931() {
661     check_infer(
662         r#"
663         trait Div<T> {
664             type Output;
665         }
666
667         trait CheckedDiv: Div<()> {}
668
669         trait PrimInt: CheckedDiv<Output = ()> {
670             fn pow(self);
671         }
672
673         fn check<T: PrimInt>(i: T) {
674             i.pow();
675         }
676         "#,
677         expect![[r#"
678             117..121 'self': Self
679             148..149 'i': T
680             154..170 '{     ...w(); }': ()
681             160..161 'i': T
682             160..167 'i.pow()': ()
683         "#]],
684     );
685 }
686
687 #[test]
688 fn issue_4885() {
689     check_infer(
690         r#"
691         //- minicore: coerce_unsized, future
692         use core::future::Future;
693         trait Foo<R> {
694             type Bar;
695         }
696         fn foo<R, K>(key: &K) -> impl Future<Output = K::Bar>
697         where
698             K: Foo<R>,
699         {
700             bar(key)
701         }
702         fn bar<R, K>(key: &K) -> impl Future<Output = K::Bar>
703         where
704             K: Foo<R>,
705         {
706         }
707         "#,
708         expect![[r#"
709             70..73 'key': &K
710             132..148 '{     ...key) }': impl Future<Output = <K as Foo<R>>::Bar>
711             138..141 'bar': fn bar<R, K>(&K) -> impl Future<Output = <K as Foo<R>>::Bar>
712             138..146 'bar(key)': impl Future<Output = <K as Foo<R>>::Bar>
713             142..145 'key': &K
714             162..165 'key': &K
715             224..227 '{ }': ()
716         "#]],
717     );
718 }
719
720 #[test]
721 fn issue_4800() {
722     check_infer(
723         r#"
724         trait Debug {}
725
726         struct Foo<T>;
727
728         type E1<T> = (T, T, T);
729         type E2<T> = E1<E1<E1<(T, T, T)>>>;
730
731         impl Debug for Foo<E2<()>> {}
732
733         struct Request;
734
735         pub trait Future {
736             type Output;
737         }
738
739         pub struct PeerSet<D>;
740
741         impl<D> Service<Request> for PeerSet<D>
742         where
743             D: Discover,
744             D::Key: Debug,
745         {
746             type Error = ();
747             type Future = dyn Future<Output = Self::Error>;
748
749             fn call(&mut self) -> Self::Future {
750                 loop {}
751             }
752         }
753
754         pub trait Discover {
755             type Key;
756         }
757
758         pub trait Service<Request> {
759             type Error;
760             type Future: Future<Output = Self::Error>;
761             fn call(&mut self) -> Self::Future;
762         }
763         "#,
764         expect![[r#"
765             379..383 'self': &mut PeerSet<D>
766             401..424 '{     ...     }': dyn Future<Output = ()>
767             411..418 'loop {}': !
768             416..418 '{}': ()
769             575..579 'self': &mut Self
770         "#]],
771     );
772 }
773
774 #[test]
775 fn issue_4966() {
776     check_infer(
777         r#"
778         //- minicore: deref
779         pub trait IntoIterator {
780             type Item;
781         }
782
783         struct Repeat<A> { element: A }
784
785         struct Map<F> { f: F }
786
787         struct Vec<T> {}
788
789         impl<T> core::ops::Deref for Vec<T> {
790             type Target = [T];
791         }
792
793         fn from_iter<A, T: IntoIterator<Item = A>>(iter: T) -> Vec<A> {}
794
795         fn main() {
796             let inner = Map { f: |_: &f64| 0.0 };
797
798             let repeat = Repeat { element: inner };
799
800             let vec = from_iter(repeat);
801
802             vec.foo_bar();
803         }
804         "#,
805         expect![[r#"
806             225..229 'iter': T
807             244..246 '{}': Vec<A>
808             258..402 '{     ...r(); }': ()
809             268..273 'inner': Map<|&f64| -> f64>
810             276..300 'Map { ... 0.0 }': Map<|&f64| -> f64>
811             285..298 '|_: &f64| 0.0': |&f64| -> f64
812             286..287 '_': &f64
813             295..298 '0.0': f64
814             311..317 'repeat': Repeat<Map<|&f64| -> f64>>
815             320..345 'Repeat...nner }': Repeat<Map<|&f64| -> f64>>
816             338..343 'inner': Map<|&f64| -> f64>
817             356..359 'vec': Vec<IntoIterator::Item<Repeat<Map<|&f64| -> f64>>>>
818             362..371 'from_iter': fn from_iter<IntoIterator::Item<Repeat<Map<|&f64| -> f64>>>, Repeat<Map<|&f64| -> f64>>>(Repeat<Map<|&f64| -> f64>>) -> Vec<IntoIterator::Item<Repeat<Map<|&f64| -> f64>>>>
819             362..379 'from_i...epeat)': Vec<IntoIterator::Item<Repeat<Map<|&f64| -> f64>>>>
820             372..378 'repeat': Repeat<Map<|&f64| -> f64>>
821             386..389 'vec': Vec<IntoIterator::Item<Repeat<Map<|&f64| -> f64>>>>
822             386..399 'vec.foo_bar()': {unknown}
823         "#]],
824     );
825 }
826
827 #[test]
828 fn issue_6628() {
829     check_infer(
830         r#"
831 //- minicore: fn
832 struct S<T>();
833 impl<T> S<T> {
834     fn f(&self, _t: T) {}
835     fn g<F: FnOnce(&T)>(&self, _f: F) {}
836 }
837 fn main() {
838     let s = S();
839     s.g(|_x| {});
840     s.f(10);
841 }
842 "#,
843         expect![[r#"
844             40..44 'self': &S<T>
845             46..48 '_t': T
846             53..55 '{}': ()
847             81..85 'self': &S<T>
848             87..89 '_f': F
849             94..96 '{}': ()
850             109..160 '{     ...10); }': ()
851             119..120 's': S<i32>
852             123..124 'S': S<i32>() -> S<i32>
853             123..126 'S()': S<i32>
854             132..133 's': S<i32>
855             132..144 's.g(|_x| {})': ()
856             136..143 '|_x| {}': |&i32| -> ()
857             137..139 '_x': &i32
858             141..143 '{}': ()
859             150..151 's': S<i32>
860             150..157 's.f(10)': ()
861             154..156 '10': i32
862         "#]],
863     );
864 }
865
866 #[test]
867 fn issue_6852() {
868     check_infer(
869         r#"
870 //- minicore: deref
871 use core::ops::Deref;
872
873 struct BufWriter {}
874
875 struct Mutex<T> {}
876 struct MutexGuard<'a, T> {}
877 impl<T> Mutex<T> {
878     fn lock(&self) -> MutexGuard<'_, T> {}
879 }
880 impl<'a, T: 'a> Deref for MutexGuard<'a, T> {
881     type Target = T;
882 }
883 fn flush(&self) {
884     let w: &Mutex<BufWriter>;
885     *(w.lock());
886 }
887 "#,
888         expect![[r#"
889             123..127 'self': &Mutex<T>
890             150..152 '{}': MutexGuard<T>
891             234..238 'self': &{unknown}
892             240..290 '{     ...()); }': ()
893             250..251 'w': &Mutex<BufWriter>
894             276..287 '*(w.lock())': BufWriter
895             278..279 'w': &Mutex<BufWriter>
896             278..286 'w.lock()': MutexGuard<BufWriter>
897         "#]],
898     );
899 }
900
901 #[test]
902 fn param_overrides_fn() {
903     check_types(
904         r#"
905         fn example(example: i32) {
906             fn f() {}
907             example;
908           //^^^^^^^ i32
909         }
910         "#,
911     )
912 }
913
914 #[test]
915 fn lifetime_from_chalk_during_deref() {
916     check_types(
917         r#"
918 //- minicore: deref
919 struct Box<T: ?Sized> {}
920 impl<T: ?Sized> core::ops::Deref for Box<T> {
921     type Target = T;
922
923     fn deref(&self) -> &Self::Target {
924         loop {}
925     }
926 }
927
928 trait Iterator {
929     type Item;
930 }
931
932 pub struct Iter<'a, T: 'a> {
933     inner: Box<dyn IterTrait<'a, T, Item = &'a T> + 'a>,
934 }
935
936 trait IterTrait<'a, T: 'a>: Iterator<Item = &'a T> {
937     fn clone_box(&self);
938 }
939
940 fn clone_iter<T>(s: Iter<T>) {
941     s.inner.clone_box();
942   //^^^^^^^^^^^^^^^^^^^ ()
943 }
944 "#,
945     )
946 }
947
948 #[test]
949 fn issue_8686() {
950     check_infer(
951         r#"
952 pub trait Try: FromResidual {
953     type Output;
954     type Residual;
955 }
956 pub trait FromResidual<R = <Self as Try>::Residual> {
957      fn from_residual(residual: R) -> Self;
958 }
959
960 struct ControlFlow<B, C>;
961 impl<B, C> Try for ControlFlow<B, C> {
962     type Output = C;
963     type Residual = ControlFlow<B, !>;
964 }
965 impl<B, C> FromResidual for ControlFlow<B, C> {
966     fn from_residual(r: ControlFlow<B, !>) -> Self { ControlFlow }
967 }
968
969 fn test() {
970     ControlFlow::from_residual(ControlFlow::<u32, !>);
971 }
972         "#,
973         expect![[r#"
974             144..152 'residual': R
975             365..366 'r': ControlFlow<B, !>
976             395..410 '{ ControlFlow }': ControlFlow<B, C>
977             397..408 'ControlFlow': ControlFlow<B, C>
978             424..482 '{     ...!>); }': ()
979             430..456 'Contro...sidual': fn from_residual<ControlFlow<u32, {unknown}>, ControlFlow<u32, !>>(ControlFlow<u32, !>) -> ControlFlow<u32, {unknown}>
980             430..479 'Contro...2, !>)': ControlFlow<u32, {unknown}>
981             457..478 'Contro...32, !>': ControlFlow<u32, !>
982         "#]],
983     );
984 }
985
986 #[test]
987 fn cfg_tail() {
988     // https://github.com/rust-lang/rust-analyzer/issues/8378
989     check_infer(
990         r#"
991         fn fake_tail(){
992             { "first" }
993             #[cfg(never)] 9
994         }
995         fn multiple_fake(){
996             { "fake" }
997             { "fake" }
998             { "second" }
999             #[cfg(never)] { 11 }
1000             #[cfg(never)] 12;
1001             #[cfg(never)] 13
1002         }
1003         fn no_normal_tail(){
1004             { "third" }
1005             #[cfg(never)] 14;
1006             #[cfg(never)] 15;
1007         }
1008         fn no_actual_tail(){
1009             { "fourth" };
1010             #[cfg(never)] 14;
1011             #[cfg(never)] 15
1012         }
1013         "#,
1014         expect![[r#"
1015             14..53 '{     ...)] 9 }': ()
1016             20..31 '{ "first" }': ()
1017             22..29 '"first"': &str
1018             72..190 '{     ...] 13 }': ()
1019             78..88 '{ "fake" }': &str
1020             80..86 '"fake"': &str
1021             93..103 '{ "fake" }': &str
1022             95..101 '"fake"': &str
1023             108..120 '{ "second" }': ()
1024             110..118 '"second"': &str
1025             210..273 '{     ... 15; }': ()
1026             216..227 '{ "third" }': ()
1027             218..225 '"third"': &str
1028             293..357 '{     ...] 15 }': ()
1029             299..311 '{ "fourth" }': &str
1030             301..309 '"fourth"': &str
1031         "#]],
1032     )
1033 }
1034
1035 #[test]
1036 fn impl_trait_in_option_9530() {
1037     check_types(
1038         r#"
1039 //- minicore: sized
1040 struct Option<T>;
1041 impl<T> Option<T> {
1042     fn unwrap(self) -> T { loop {} }
1043 }
1044 fn make() -> Option<impl Copy> { Option }
1045 trait Copy {}
1046 fn test() {
1047     let o = make();
1048     o.unwrap();
1049   //^^^^^^^^^^ impl Copy
1050 }
1051         "#,
1052     )
1053 }
1054
1055 #[test]
1056 fn bare_dyn_trait_binders_9639() {
1057     check_no_mismatches(
1058         r#"
1059 //- minicore: fn, coerce_unsized
1060 fn infix_parse<T, S>(_state: S, _level_code: &Fn(S)) -> T {
1061     loop {}
1062 }
1063
1064 fn parse_arule() {
1065     infix_parse((), &(|_recurse| ()))
1066 }
1067         "#,
1068     )
1069 }
1070
1071 #[test]
1072 fn call_expected_type_closure() {
1073     check_types(
1074         r#"
1075 //- minicore: fn, option
1076
1077 fn map<T, U>(o: Option<T>, f: impl FnOnce(T) -> U) -> Option<U> { loop {} }
1078 struct S {
1079     field: u32
1080 }
1081
1082 fn test() {
1083     let o = Some(S { field: 2 });
1084     let _: Option<()> = map(o, |s| { s.field; });
1085                                   // ^^^^^^^ u32
1086 }
1087         "#,
1088     );
1089 }
1090
1091 #[test]
1092 fn coerce_diesel_panic() {
1093     check_no_mismatches(
1094         r#"
1095 //- minicore: option
1096
1097 trait TypeMetadata {
1098     type MetadataLookup;
1099 }
1100
1101 pub struct Output<'a, T, DB>
1102 where
1103     DB: TypeMetadata,
1104     DB::MetadataLookup: 'a,
1105 {
1106     out: T,
1107     metadata_lookup: Option<&'a DB::MetadataLookup>,
1108 }
1109
1110 impl<'a, T, DB: TypeMetadata> Output<'a, T, DB> {
1111     pub fn new(out: T, metadata_lookup: &'a DB::MetadataLookup) -> Self {
1112         Output {
1113             out,
1114             metadata_lookup: Some(metadata_lookup),
1115         }
1116     }
1117 }
1118         "#,
1119     );
1120 }
1121
1122 #[test]
1123 fn bitslice_panic() {
1124     check_no_mismatches(
1125         r#"
1126 //- minicore: option, deref
1127
1128 pub trait BitView {
1129     type Store;
1130 }
1131
1132 pub struct Lsb0;
1133
1134 pub struct BitArray<V: BitView> { }
1135
1136 pub struct BitSlice<T> { }
1137
1138 impl<V: BitView> core::ops::Deref for BitArray<V> {
1139     type Target = BitSlice<V::Store>;
1140 }
1141
1142 impl<T> BitSlice<T> {
1143     pub fn split_first(&self) -> Option<(T, &Self)> { loop {} }
1144 }
1145
1146 fn multiexp_inner() {
1147     let exp: &BitArray<Foo>;
1148     exp.split_first();
1149 }
1150         "#,
1151     );
1152 }
1153
1154 #[test]
1155 fn macro_expands_to_impl_trait() {
1156     check_no_mismatches(
1157         r#"
1158 trait Foo {}
1159
1160 macro_rules! ty {
1161     () => {
1162         impl Foo
1163     }
1164 }
1165
1166 fn foo(_: ty!()) {}
1167
1168 fn bar() {
1169     foo(());
1170 }
1171     "#,
1172     )
1173 }
1174
1175 #[test]
1176 fn nested_macro_in_fn_params() {
1177     check_no_mismatches(
1178         r#"
1179 macro_rules! U32Inner {
1180     () => {
1181         u32
1182     };
1183 }
1184
1185 macro_rules! U32 {
1186     () => {
1187         U32Inner!()
1188     };
1189 }
1190
1191 fn mamba(a: U32!(), p: u32) -> u32 {
1192     a
1193 }
1194     "#,
1195     )
1196 }
1197
1198 #[test]
1199 fn for_loop_block_expr_iterable() {
1200     check_infer(
1201         r#"
1202 fn test() {
1203     for _ in { let x = 0; } {
1204         let y = 0;
1205     }
1206 }
1207         "#,
1208         expect![[r#"
1209             10..68 '{     ...   } }': ()
1210             16..66 'for _ ...     }': ()
1211             20..21 '_': {unknown}
1212             25..39 '{ let x = 0; }': ()
1213             31..32 'x': i32
1214             35..36 '0': i32
1215             40..66 '{     ...     }': ()
1216             54..55 'y': i32
1217             58..59 '0': i32
1218         "#]],
1219     );
1220 }
1221
1222 #[test]
1223 fn while_loop_block_expr_iterable() {
1224     check_infer(
1225         r#"
1226 fn test() {
1227     while { true } {
1228         let y = 0;
1229     }
1230 }
1231         "#,
1232         expect![[r#"
1233             10..59 '{     ...   } }': ()
1234             16..57 'while ...     }': ()
1235             22..30 '{ true }': bool
1236             24..28 'true': bool
1237             31..57 '{     ...     }': ()
1238             45..46 'y': i32
1239             49..50 '0': i32
1240         "#]],
1241     );
1242 }
1243
1244 #[test]
1245 fn bug_11242() {
1246     // FIXME: wrong, should be u32
1247     check_types(
1248         r#"
1249 fn foo<A, B>()
1250 where
1251     A: IntoIterator<Item = u32>,
1252     B: IntoIterator<Item = usize>,
1253 {
1254     let _x: <A as IntoIterator>::Item;
1255      // ^^ {unknown}
1256 }
1257
1258 pub trait Iterator {
1259     type Item;
1260 }
1261
1262 pub trait IntoIterator {
1263     type Item;
1264     type IntoIter: Iterator<Item = Self::Item>;
1265 }
1266
1267 impl<I: Iterator> IntoIterator for I {
1268     type Item = I::Item;
1269     type IntoIter = I;
1270 }
1271 "#,
1272     );
1273 }
1274
1275 #[test]
1276 fn bug_11659() {
1277     check_no_mismatches(
1278         r#"
1279 struct LinkArray<const N: usize, LD>(LD);
1280 fn f<const N: usize, LD>(x: LD) -> LinkArray<N, LD> {
1281     let r = LinkArray::<N, LD>(x);
1282     r
1283 }
1284
1285 fn test() {
1286     let x = f::<2, i32>(5);
1287     let y = LinkArray::<52, LinkArray<2, i32>>(x);
1288 }
1289         "#,
1290     );
1291     check_no_mismatches(
1292         r#"
1293 struct LinkArray<LD, const N: usize>(LD);
1294 fn f<const N: usize, LD>(x: LD) -> LinkArray<LD, N> {
1295     let r = LinkArray::<LD, N>(x);
1296     r
1297 }
1298
1299 fn test() {
1300     let x = f::<i32, 2>(5);
1301     let y = LinkArray::<LinkArray<i32, 2>, 52>(x);
1302 }
1303         "#,
1304     );
1305 }
1306
1307 #[test]
1308 fn const_generic_error_tolerance() {
1309     check_no_mismatches(
1310         r#"
1311 #[lang = "sized"]
1312 pub trait Sized {}
1313
1314 struct CT<const N: usize, T>(T);
1315 struct TC<T, const N: usize>(T);
1316 fn f<const N: usize, T>(x: T) -> (CT<N, T>, TC<T, N>) {
1317     let l = CT::<N, T>(x);
1318     let r = TC::<N, T>(x);
1319     (l, r)
1320 }
1321
1322 trait TR1<const N: usize>;
1323 trait TR2<const N: usize>;
1324
1325 impl<const N: usize, T> TR1<N> for CT<N, T>;
1326 impl<const N: usize, T> TR1<5> for TC<T, N>;
1327 impl<const N: usize, T> TR2<N> for CT<T, N>;
1328
1329 trait TR3<const N: usize> {
1330     fn tr3(&self) -> &Self;
1331 }
1332
1333 impl<const N: usize, T> TR3<5> for TC<T, N> {
1334     fn tr3(&self) -> &Self {
1335         self
1336     }
1337 }
1338
1339 impl<const N: usize, T> TR3<Item = 5> for TC<T, N> {}
1340 impl<const N: usize, T> TR3<T> for TC<T, N> {}
1341
1342 fn impl_trait<const N: usize>(inp: impl TR1<N>) {}
1343 fn dyn_trait<const N: usize>(inp: &dyn TR2<N>) {}
1344 fn impl_trait_bad<'a, const N: usize>(inp: impl TR1<i32>) -> impl TR1<'a, i32> {}
1345 fn impl_trait_very_bad<const N: usize>(inp: impl TR1<Item = i32>) -> impl TR1<'a, Item = i32, 5, Foo = N> {}
1346
1347 fn test() {
1348     f::<2, i32>(5);
1349     f::<2, 2>(5);
1350     f(5);
1351     f::<i32>(5);
1352     CT::<52, CT<2, i32>>(x);
1353     CT::<CT<2, i32>>(x);
1354     impl_trait_bad(5);
1355     impl_trait_bad(12);
1356     TR3<5>::tr3();
1357     TR3<{ 2+3 }>::tr3();
1358     TC::<i32, 10>(5).tr3();
1359     TC::<i32, 20>(5).tr3();
1360     TC::<i32, i32>(5).tr3();
1361     TC::<i32, { 7 + 3 }>(5).tr3();
1362 }
1363         "#,
1364     );
1365 }
1366
1367 #[test]
1368 fn const_generic_impl_trait() {
1369     check_no_mismatches(
1370         r#"
1371         //- minicore: from
1372
1373         struct Foo<T, const M: usize>;
1374
1375         trait Tr<T> {
1376             fn f(T) -> Self;
1377         }
1378
1379         impl<T, const M: usize> Tr<[T; M]> for Foo<T, M> {
1380             fn f(_: [T; M]) -> Self {
1381                 Self
1382             }
1383         }
1384
1385         fn test() {
1386             Foo::f([1, 2, 7, 10]);
1387         }
1388         "#,
1389     );
1390 }
1391
1392 #[test]
1393 fn nalgebra_factorial() {
1394     check_no_mismatches(
1395         r#"
1396         const FACTORIAL: [u128; 4] = [1, 1, 2, 6];
1397
1398         fn factorial(n: usize) -> u128 {
1399             match FACTORIAL.get(n) {
1400                 Some(f) => *f,
1401                 None => panic!("{}! is greater than u128::MAX", n),
1402             }
1403         }
1404         "#,
1405     )
1406 }
1407
1408 #[test]
1409 fn regression_11688_1() {
1410     check_no_mismatches(
1411         r#"
1412         pub struct Buffer<T>(T);
1413         type Writer = Buffer<u8>;
1414         impl<T> Buffer<T> {
1415             fn extend_from_array<const N: usize>(&mut self, xs: &[T; N]) {
1416                 loop {}
1417             }
1418         }
1419         trait Encode<S> {
1420             fn encode(self, w: &mut Writer, s: &mut S);
1421         }
1422         impl<S> Encode<S> for u8 {
1423             fn encode(self, w: &mut Writer, _: &mut S) {
1424                 w.extend_from_array(&self.to_le_bytes());
1425             }
1426         }
1427         "#,
1428     );
1429 }
1430
1431 #[test]
1432 fn regression_11688_2() {
1433     check_types(
1434         r#"
1435         union MaybeUninit<T> {
1436             uninit: (),
1437             value: T,
1438         }
1439
1440         impl<T> MaybeUninit<T> {
1441             fn uninit_array<const LEN: usize>() -> [Self; LEN] {
1442                 loop {}
1443             }
1444         }
1445
1446         fn main() {
1447             let x = MaybeUninit::<i32>::uninit_array::<1>();
1448               //^ [MaybeUninit<i32>; 1]
1449         }
1450         "#,
1451     );
1452 }
1453
1454 #[test]
1455 fn regression_11688_3() {
1456     check_types(
1457         r#"
1458         //- minicore: iterator
1459         struct Ar<T, const N: u8>(T);
1460         fn f<const LEN: usize, T, const BASE: u8>(
1461             num_zeros: usize,
1462         ) -> dyn Iterator<Item = [Ar<T, BASE>; LEN]> {
1463             loop {}
1464         }
1465         fn dynamic_programming() {
1466             for board in f::<9, u8, 7>(1) {
1467               //^^^^^ [Ar<u8, 7>; 9]
1468             }
1469         }
1470         "#,
1471     );
1472 }
1473
1474 #[test]
1475 fn regression_11688_4() {
1476     check_types(
1477         r#"
1478         trait Bar<const C: usize> {
1479             fn baz(&self) -> [i32; C];
1480         }
1481
1482         fn foo(x: &dyn Bar<2>) {
1483             x.baz();
1484           //^^^^^^^ [i32; 2]
1485         }
1486         "#,
1487     )
1488 }
1489
1490 #[test]
1491 fn gat_crash_1() {
1492     cov_mark::check!(ignore_gats);
1493     check_no_mismatches(
1494         r#"
1495 trait ATrait {}
1496
1497 trait Crash {
1498     type Member<const N: usize>: ATrait;
1499     fn new<const N: usize>() -> Self::Member<N>;
1500 }
1501
1502 fn test<T: Crash>() {
1503     T::new();
1504 }
1505 "#,
1506     );
1507 }
1508
1509 #[test]
1510 fn gat_crash_2() {
1511     check_no_mismatches(
1512         r#"
1513 pub struct InlineStorage {}
1514
1515 pub struct InlineStorageHandle<T: ?Sized> {}
1516
1517 pub unsafe trait Storage {
1518     type Handle<T: ?Sized>;
1519     fn create<T: ?Sized>() -> Self::Handle<T>;
1520 }
1521
1522 unsafe impl Storage for InlineStorage {
1523     type Handle<T: ?Sized> = InlineStorageHandle<T>;
1524 }
1525 "#,
1526     );
1527 }
1528
1529 #[test]
1530 fn cfgd_out_self_param() {
1531     cov_mark::check!(cfgd_out_self_param);
1532     check_no_mismatches(
1533         r#"
1534 struct S;
1535 impl S {
1536     fn f(#[cfg(never)] &self) {}
1537 }
1538
1539 fn f(s: S) {
1540     s.f();
1541 }
1542 "#,
1543     );
1544 }
1545
1546 #[test]
1547 fn rust_161_option_clone() {
1548     check_types(
1549         r#"
1550 //- minicore: option, drop
1551
1552 fn test(o: &Option<i32>) {
1553     o.my_clone();
1554   //^^^^^^^^^^^^ Option<i32>
1555 }
1556
1557 pub trait MyClone: Sized {
1558     fn my_clone(&self) -> Self;
1559 }
1560
1561 impl<T> const MyClone for Option<T>
1562 where
1563     T: ~const MyClone + ~const Drop + ~const Destruct,
1564 {
1565     fn my_clone(&self) -> Self {
1566         match self {
1567             Some(x) => Some(x.my_clone()),
1568             None => None,
1569         }
1570     }
1571 }
1572
1573 impl const MyClone for i32 {
1574     fn my_clone(&self) -> Self {
1575         *self
1576     }
1577 }
1578
1579 pub trait Destruct {}
1580
1581 impl<T: ?Sized> const Destruct for T {}
1582 "#,
1583     );
1584 }
1585
1586 #[test]
1587 fn rust_162_option_clone() {
1588     check_types(
1589         r#"
1590 //- minicore: option, drop
1591
1592 fn test(o: &Option<i32>) {
1593     o.my_clone();
1594   //^^^^^^^^^^^^ Option<i32>
1595 }
1596
1597 pub trait MyClone: Sized {
1598     fn my_clone(&self) -> Self;
1599 }
1600
1601 impl<T> const MyClone for Option<T>
1602 where
1603     T: ~const MyClone + ~const Destruct,
1604 {
1605     fn my_clone(&self) -> Self {
1606         match self {
1607             Some(x) => Some(x.my_clone()),
1608             None => None,
1609         }
1610     }
1611 }
1612
1613 impl const MyClone for i32 {
1614     fn my_clone(&self) -> Self {
1615         *self
1616     }
1617 }
1618
1619 #[lang = "destruct"]
1620 pub trait Destruct {}
1621 "#,
1622     );
1623 }
1624
1625 #[test]
1626 fn tuple_struct_pattern_with_unmatched_args_crash() {
1627     check_infer(
1628         r#"
1629 struct S(usize);
1630 fn main() {
1631     let S(.., a, b) = S(1);
1632     let (.., a, b) = (1,);
1633 }
1634         "#,
1635         expect![[r#"
1636         27..85 '{     ...1,); }': ()
1637         37..48 'S(.., a, b)': S
1638         43..44 'a': usize
1639         46..47 'b': {unknown}
1640         51..52 'S': S(usize) -> S
1641         51..55 'S(1)': S
1642         53..54 '1': usize
1643         65..75 '(.., a, b)': (i32, {unknown})
1644         70..71 'a': i32
1645         73..74 'b': {unknown}
1646         78..82 '(1,)': (i32,)
1647         79..80 '1': i32
1648         "#]],
1649     );
1650 }