]> git.lizzy.rs Git - rust.git/blob - crates/hir_ty/src/tests/regression.rs
Add const generics
[rust.git] / 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 '{}': ()
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_3642_bad_macro_stackover() {
449     check_no_mismatches(
450         r#"
451 #[macro_export]
452 macro_rules! match_ast {
453     (match $node:ident { $($tt:tt)* }) => { match_ast!(match ($node) { $($tt)* }) };
454
455     (match ($node:expr) {
456         $( ast::$ast:ident($it:ident) => $res:expr, )*
457         _ => $catch_all:expr $(,)?
458     }) => {{
459         $( if let Some($it) = ast::$ast::cast($node.clone()) { $res } else )*
460         { $catch_all }
461     }};
462 }
463
464 fn main() {
465     let anchor = match_ast! {
466         match parent {
467             as => {},
468             _ => return None
469         }
470     };
471 }"#,
472     );
473 }
474
475 #[test]
476 fn issue_3999_slice() {
477     check_infer(
478         r#"
479         fn foo(params: &[usize]) {
480             match params {
481                 [ps @ .., _] => {}
482             }
483         }
484         "#,
485         expect![[r#"
486             7..13 'params': &[usize]
487             25..80 '{     ...   } }': ()
488             31..78 'match ...     }': ()
489             37..43 'params': &[usize]
490             54..66 '[ps @ .., _]': [usize]
491             55..62 'ps @ ..': &[usize]
492             60..62 '..': [usize]
493             64..65 '_': usize
494             70..72 '{}': ()
495         "#]],
496     );
497 }
498
499 #[test]
500 fn issue_3999_struct() {
501     // rust-analyzer should not panic on seeing this malformed
502     // record pattern.
503     check_infer(
504         r#"
505         struct Bar {
506             a: bool,
507         }
508         fn foo(b: Bar) {
509             match b {
510                 Bar { a: .. } => {},
511             }
512         }
513         "#,
514         expect![[r#"
515             35..36 'b': Bar
516             43..95 '{     ...   } }': ()
517             49..93 'match ...     }': ()
518             55..56 'b': Bar
519             67..80 'Bar { a: .. }': Bar
520             76..78 '..': bool
521             84..86 '{}': ()
522         "#]],
523     );
524 }
525
526 #[test]
527 fn issue_4235_name_conflicts() {
528     check_infer(
529         r#"
530         struct FOO {}
531         static FOO:FOO = FOO {};
532
533         impl FOO {
534             fn foo(&self) {}
535         }
536
537         fn main() {
538             let a = &FOO;
539             a.foo();
540         }
541         "#,
542         expect![[r#"
543             31..37 'FOO {}': FOO
544             63..67 'self': &FOO
545             69..71 '{}': ()
546             85..119 '{     ...o(); }': ()
547             95..96 'a': &FOO
548             99..103 '&FOO': &FOO
549             100..103 'FOO': FOO
550             109..110 'a': &FOO
551             109..116 'a.foo()': ()
552         "#]],
553     );
554 }
555
556 #[test]
557 fn issue_4465_dollar_crate_at_type() {
558     check_infer(
559         r#"
560         pub struct Foo {}
561         pub fn anything<T>() -> T {
562             loop {}
563         }
564         macro_rules! foo {
565             () => {{
566                 let r: $crate::Foo = anything();
567                 r
568             }};
569         }
570         fn main() {
571             let _a = foo!();
572         }
573         "#,
574         expect![[r#"
575             44..59 '{     loop {} }': T
576             50..57 'loop {}': !
577             55..57 '{}': ()
578             !0..31 '{letr:...g();r}': Foo
579             !4..5 'r': Foo
580             !18..26 'anything': fn anything<Foo>() -> Foo
581             !18..28 'anything()': Foo
582             !29..30 'r': Foo
583             163..187 '{     ...!(); }': ()
584             173..175 '_a': Foo
585         "#]],
586     );
587 }
588
589 #[test]
590 fn issue_6811() {
591     check_infer(
592         r#"
593         macro_rules! profile_function {
594             () => {
595                 let _a = 1;
596                 let _b = 1;
597             };
598         }
599         fn main() {
600             profile_function!();
601         }
602         "#,
603         expect![[r#"
604             !3..5 '_a': i32
605             !6..7 '1': i32
606             !11..13 '_b': i32
607             !14..15 '1': i32
608             103..131 '{     ...!(); }': ()
609         "#]],
610     );
611 }
612
613 #[test]
614 fn issue_4053_diesel_where_clauses() {
615     check_infer(
616         r#"
617         trait BoxedDsl<DB> {
618             type Output;
619             fn internal_into_boxed(self) -> Self::Output;
620         }
621
622         struct SelectStatement<From, Select, Distinct, Where, Order, LimitOffset, GroupBy, Locking> {
623             order: Order,
624         }
625
626         trait QueryFragment<DB: Backend> {}
627
628         trait Into<T> { fn into(self) -> T; }
629
630         impl<F, S, D, W, O, LOf, DB> BoxedDsl<DB>
631             for SelectStatement<F, S, D, W, O, LOf, G>
632         where
633             O: Into<dyn QueryFragment<DB>>,
634         {
635             type Output = XXX;
636
637             fn internal_into_boxed(self) -> Self::Output {
638                 self.order.into();
639             }
640         }
641         "#,
642         expect![[r#"
643             65..69 'self': Self
644             267..271 'self': Self
645             466..470 'self': SelectStatement<F, S, D, W, O, LOf, {unknown}, {unknown}>
646             488..522 '{     ...     }': ()
647             498..502 'self': SelectStatement<F, S, D, W, O, LOf, {unknown}, {unknown}>
648             498..508 'self.order': O
649             498..515 'self.o...into()': dyn QueryFragment<DB>
650         "#]],
651     );
652 }
653
654 #[test]
655 fn issue_4953() {
656     check_infer(
657         r#"
658         pub struct Foo(pub i64);
659         impl Foo {
660             fn test() -> Self { Self(0i64) }
661         }
662         "#,
663         expect![[r#"
664             58..72 '{ Self(0i64) }': Foo
665             60..64 'Self': Foo(i64) -> Foo
666             60..70 'Self(0i64)': Foo
667             65..69 '0i64': i64
668         "#]],
669     );
670     check_infer(
671         r#"
672         pub struct Foo<T>(pub T);
673         impl Foo<i64> {
674             fn test() -> Self { Self(0i64) }
675         }
676         "#,
677         expect![[r#"
678             64..78 '{ Self(0i64) }': Foo<i64>
679             66..70 'Self': Foo<i64>(i64) -> Foo<i64>
680             66..76 'Self(0i64)': Foo<i64>
681             71..75 '0i64': i64
682         "#]],
683     );
684 }
685
686 #[test]
687 fn issue_4931() {
688     check_infer(
689         r#"
690         trait Div<T> {
691             type Output;
692         }
693
694         trait CheckedDiv: Div<()> {}
695
696         trait PrimInt: CheckedDiv<Output = ()> {
697             fn pow(self);
698         }
699
700         fn check<T: PrimInt>(i: T) {
701             i.pow();
702         }
703         "#,
704         expect![[r#"
705             117..121 'self': Self
706             148..149 'i': T
707             154..170 '{     ...w(); }': ()
708             160..161 'i': T
709             160..167 'i.pow()': ()
710         "#]],
711     );
712 }
713
714 #[test]
715 fn issue_4885() {
716     check_infer(
717         r#"
718         //- minicore: coerce_unsized, future
719         use core::future::Future;
720         trait Foo<R> {
721             type Bar;
722         }
723         fn foo<R, K>(key: &K) -> impl Future<Output = K::Bar>
724         where
725             K: Foo<R>,
726         {
727             bar(key)
728         }
729         fn bar<R, K>(key: &K) -> impl Future<Output = K::Bar>
730         where
731             K: Foo<R>,
732         {
733         }
734         "#,
735         expect![[r#"
736             70..73 'key': &K
737             132..148 '{     ...key) }': impl Future<Output = <K as Foo<R>>::Bar>
738             138..141 'bar': fn bar<R, K>(&K) -> impl Future<Output = <K as Foo<R>>::Bar>
739             138..146 'bar(key)': impl Future<Output = <K as Foo<R>>::Bar>
740             142..145 'key': &K
741             162..165 'key': &K
742             224..227 '{ }': ()
743         "#]],
744     );
745 }
746
747 #[test]
748 fn issue_4800() {
749     check_infer(
750         r#"
751         trait Debug {}
752
753         struct Foo<T>;
754
755         type E1<T> = (T, T, T);
756         type E2<T> = E1<E1<E1<(T, T, T)>>>;
757
758         impl Debug for Foo<E2<()>> {}
759
760         struct Request;
761
762         pub trait Future {
763             type Output;
764         }
765
766         pub struct PeerSet<D>;
767
768         impl<D> Service<Request> for PeerSet<D>
769         where
770             D: Discover,
771             D::Key: Debug,
772         {
773             type Error = ();
774             type Future = dyn Future<Output = Self::Error>;
775
776             fn call(&mut self) -> Self::Future {
777                 loop {}
778             }
779         }
780
781         pub trait Discover {
782             type Key;
783         }
784
785         pub trait Service<Request> {
786             type Error;
787             type Future: Future<Output = Self::Error>;
788             fn call(&mut self) -> Self::Future;
789         }
790         "#,
791         expect![[r#"
792             379..383 'self': &mut PeerSet<D>
793             401..424 '{     ...     }': dyn Future<Output = ()>
794             411..418 'loop {}': !
795             416..418 '{}': ()
796             575..579 'self': &mut Self
797         "#]],
798     );
799 }
800
801 #[test]
802 fn issue_4966() {
803     check_infer(
804         r#"
805         //- minicore: deref
806         pub trait IntoIterator {
807             type Item;
808         }
809
810         struct Repeat<A> { element: A }
811
812         struct Map<F> { f: F }
813
814         struct Vec<T> {}
815
816         impl<T> core::ops::Deref for Vec<T> {
817             type Target = [T];
818         }
819
820         fn from_iter<A, T: IntoIterator<Item = A>>(iter: T) -> Vec<A> {}
821
822         fn main() {
823             let inner = Map { f: |_: &f64| 0.0 };
824
825             let repeat = Repeat { element: inner };
826
827             let vec = from_iter(repeat);
828
829             vec.foo_bar();
830         }
831         "#,
832         expect![[r#"
833             225..229 'iter': T
834             244..246 '{}': ()
835             258..402 '{     ...r(); }': ()
836             268..273 'inner': Map<|&f64| -> f64>
837             276..300 'Map { ... 0.0 }': Map<|&f64| -> f64>
838             285..298 '|_: &f64| 0.0': |&f64| -> f64
839             286..287 '_': &f64
840             295..298 '0.0': f64
841             311..317 'repeat': Repeat<Map<|&f64| -> f64>>
842             320..345 'Repeat...nner }': Repeat<Map<|&f64| -> f64>>
843             338..343 'inner': Map<|&f64| -> f64>
844             356..359 'vec': Vec<IntoIterator::Item<Repeat<Map<|&f64| -> f64>>>>
845             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>>>>
846             362..379 'from_i...epeat)': Vec<IntoIterator::Item<Repeat<Map<|&f64| -> f64>>>>
847             372..378 'repeat': Repeat<Map<|&f64| -> f64>>
848             386..389 'vec': Vec<IntoIterator::Item<Repeat<Map<|&f64| -> f64>>>>
849             386..399 'vec.foo_bar()': {unknown}
850         "#]],
851     );
852 }
853
854 #[test]
855 fn issue_6628() {
856     check_infer(
857         r#"
858 //- minicore: fn
859 struct S<T>();
860 impl<T> S<T> {
861     fn f(&self, _t: T) {}
862     fn g<F: FnOnce(&T)>(&self, _f: F) {}
863 }
864 fn main() {
865     let s = S();
866     s.g(|_x| {});
867     s.f(10);
868 }
869 "#,
870         expect![[r#"
871             40..44 'self': &S<T>
872             46..48 '_t': T
873             53..55 '{}': ()
874             81..85 'self': &S<T>
875             87..89 '_f': F
876             94..96 '{}': ()
877             109..160 '{     ...10); }': ()
878             119..120 's': S<i32>
879             123..124 'S': S<i32>() -> S<i32>
880             123..126 'S()': S<i32>
881             132..133 's': S<i32>
882             132..144 's.g(|_x| {})': ()
883             136..143 '|_x| {}': |&i32| -> ()
884             137..139 '_x': &i32
885             141..143 '{}': ()
886             150..151 's': S<i32>
887             150..157 's.f(10)': ()
888             154..156 '10': i32
889         "#]],
890     );
891 }
892
893 #[test]
894 fn issue_6852() {
895     check_infer(
896         r#"
897 //- minicore: deref
898 use core::ops::Deref;
899
900 struct BufWriter {}
901
902 struct Mutex<T> {}
903 struct MutexGuard<'a, T> {}
904 impl<T> Mutex<T> {
905     fn lock(&self) -> MutexGuard<'_, T> {}
906 }
907 impl<'a, T: 'a> Deref for MutexGuard<'a, T> {
908     type Target = T;
909 }
910 fn flush(&self) {
911     let w: &Mutex<BufWriter>;
912     *(w.lock());
913 }
914 "#,
915         expect![[r#"
916             123..127 'self': &Mutex<T>
917             150..152 '{}': ()
918             234..238 'self': &{unknown}
919             240..290 '{     ...()); }': ()
920             250..251 'w': &Mutex<BufWriter>
921             276..287 '*(w.lock())': BufWriter
922             278..279 'w': &Mutex<BufWriter>
923             278..286 'w.lock()': MutexGuard<BufWriter>
924         "#]],
925     );
926 }
927
928 #[test]
929 fn param_overrides_fn() {
930     check_types(
931         r#"
932         fn example(example: i32) {
933             fn f() {}
934             example;
935           //^^^^^^^ i32
936         }
937         "#,
938     )
939 }
940
941 #[test]
942 fn lifetime_from_chalk_during_deref() {
943     check_types(
944         r#"
945 //- minicore: deref
946 struct Box<T: ?Sized> {}
947 impl<T: ?Sized> core::ops::Deref for Box<T> {
948     type Target = T;
949
950     fn deref(&self) -> &Self::Target {
951         loop {}
952     }
953 }
954
955 trait Iterator {
956     type Item;
957 }
958
959 pub struct Iter<'a, T: 'a> {
960     inner: Box<dyn IterTrait<'a, T, Item = &'a T> + 'a>,
961 }
962
963 trait IterTrait<'a, T: 'a>: Iterator<Item = &'a T> {
964     fn clone_box(&self);
965 }
966
967 fn clone_iter<T>(s: Iter<T>) {
968     s.inner.clone_box();
969   //^^^^^^^^^^^^^^^^^^^ ()
970 }
971 "#,
972     )
973 }
974
975 #[test]
976 fn issue_8686() {
977     check_infer(
978         r#"
979 pub trait Try: FromResidual {
980     type Output;
981     type Residual;
982 }
983 pub trait FromResidual<R = <Self as Try>::Residual> {
984      fn from_residual(residual: R) -> Self;
985 }
986
987 struct ControlFlow<B, C>;
988 impl<B, C> Try for ControlFlow<B, C> {
989     type Output = C;
990     type Residual = ControlFlow<B, !>;
991 }
992 impl<B, C> FromResidual for ControlFlow<B, C> {
993     fn from_residual(r: ControlFlow<B, !>) -> Self { ControlFlow }
994 }
995
996 fn test() {
997     ControlFlow::from_residual(ControlFlow::<u32, !>);
998 }
999         "#,
1000         expect![[r#"
1001             144..152 'residual': R
1002             365..366 'r': ControlFlow<B, !>
1003             395..410 '{ ControlFlow }': ControlFlow<B, C>
1004             397..408 'ControlFlow': ControlFlow<B, C>
1005             424..482 '{     ...!>); }': ()
1006             430..456 'Contro...sidual': fn from_residual<ControlFlow<u32, {unknown}>, ControlFlow<u32, !>>(ControlFlow<u32, !>) -> ControlFlow<u32, {unknown}>
1007             430..479 'Contro...2, !>)': ControlFlow<u32, {unknown}>
1008             457..478 'Contro...32, !>': ControlFlow<u32, !>
1009         "#]],
1010     );
1011 }
1012
1013 #[test]
1014 fn cfg_tail() {
1015     // https://github.com/rust-analyzer/rust-analyzer/issues/8378
1016     check_infer(
1017         r#"
1018         fn fake_tail(){
1019             { "first" }
1020             #[cfg(never)] 9
1021         }
1022         fn multiple_fake(){
1023             { "fake" }
1024             { "fake" }
1025             { "second" }
1026             #[cfg(never)] { 11 }
1027             #[cfg(never)] 12;
1028             #[cfg(never)] 13
1029         }
1030         fn no_normal_tail(){
1031             { "third" }
1032             #[cfg(never)] 14;
1033             #[cfg(never)] 15;
1034         }
1035         fn no_actual_tail(){
1036             { "fourth" };
1037             #[cfg(never)] 14;
1038             #[cfg(never)] 15
1039         }
1040         "#,
1041         expect![[r#"
1042             14..53 '{     ...)] 9 }': &str
1043             20..31 '{ "first" }': &str
1044             22..29 '"first"': &str
1045             72..190 '{     ...] 13 }': &str
1046             78..88 '{ "fake" }': &str
1047             80..86 '"fake"': &str
1048             93..103 '{ "fake" }': &str
1049             95..101 '"fake"': &str
1050             108..120 '{ "second" }': &str
1051             110..118 '"second"': &str
1052             210..273 '{     ... 15; }': &str
1053             216..227 '{ "third" }': &str
1054             218..225 '"third"': &str
1055             293..357 '{     ...] 15 }': ()
1056             299..311 '{ "fourth" }': &str
1057             301..309 '"fourth"': &str
1058         "#]],
1059     )
1060 }
1061
1062 #[test]
1063 fn impl_trait_in_option_9530() {
1064     check_types(
1065         r#"
1066 //- minicore: sized
1067 struct Option<T>;
1068 impl<T> Option<T> {
1069     fn unwrap(self) -> T { loop {} }
1070 }
1071 fn make() -> Option<impl Copy> { Option }
1072 trait Copy {}
1073 fn test() {
1074     let o = make();
1075     o.unwrap();
1076   //^^^^^^^^^^ impl Copy
1077 }
1078         "#,
1079     )
1080 }
1081
1082 #[test]
1083 fn bare_dyn_trait_binders_9639() {
1084     check_no_mismatches(
1085         r#"
1086 //- minicore: fn, coerce_unsized
1087 fn infix_parse<T, S>(_state: S, _level_code: &Fn(S)) -> T {
1088     loop {}
1089 }
1090
1091 fn parse_arule() {
1092     infix_parse((), &(|_recurse| ()))
1093 }
1094         "#,
1095     )
1096 }
1097
1098 #[test]
1099 fn call_expected_type_closure() {
1100     check_types(
1101         r#"
1102 //- minicore: fn, option
1103
1104 fn map<T, U>(o: Option<T>, f: impl FnOnce(T) -> U) -> Option<U> { loop {} }
1105 struct S {
1106     field: u32
1107 }
1108
1109 fn test() {
1110     let o = Some(S { field: 2 });
1111     let _: Option<()> = map(o, |s| { s.field; });
1112                                   // ^^^^^^^ u32
1113 }
1114         "#,
1115     );
1116 }
1117
1118 #[test]
1119 fn coerce_diesel_panic() {
1120     check_no_mismatches(
1121         r#"
1122 //- minicore: option
1123
1124 trait TypeMetadata {
1125     type MetadataLookup;
1126 }
1127
1128 pub struct Output<'a, T, DB>
1129 where
1130     DB: TypeMetadata,
1131     DB::MetadataLookup: 'a,
1132 {
1133     out: T,
1134     metadata_lookup: Option<&'a DB::MetadataLookup>,
1135 }
1136
1137 impl<'a, T, DB: TypeMetadata> Output<'a, T, DB> {
1138     pub fn new(out: T, metadata_lookup: &'a DB::MetadataLookup) -> Self {
1139         Output {
1140             out,
1141             metadata_lookup: Some(metadata_lookup),
1142         }
1143     }
1144 }
1145         "#,
1146     );
1147 }
1148
1149 #[test]
1150 fn bitslice_panic() {
1151     check_no_mismatches(
1152         r#"
1153 //- minicore: option, deref
1154
1155 pub trait BitView {
1156     type Store;
1157 }
1158
1159 pub struct Lsb0;
1160
1161 pub struct BitArray<V: BitView> { }
1162
1163 pub struct BitSlice<T> { }
1164
1165 impl<V: BitView> core::ops::Deref for BitArray<V> {
1166     type Target = BitSlice<V::Store>;
1167 }
1168
1169 impl<T> BitSlice<T> {
1170     pub fn split_first(&self) -> Option<(T, &Self)> { loop {} }
1171 }
1172
1173 fn multiexp_inner() {
1174     let exp: &BitArray<Foo>;
1175     exp.split_first();
1176 }
1177         "#,
1178     );
1179 }
1180
1181 #[test]
1182 fn macro_expands_to_impl_trait() {
1183     check_no_mismatches(
1184         r#"
1185 trait Foo {}
1186
1187 macro_rules! ty {
1188     () => {
1189         impl Foo
1190     }
1191 }
1192
1193 fn foo(_: ty!()) {}
1194
1195 fn bar() {
1196     foo(());
1197 }
1198     "#,
1199     )
1200 }
1201
1202 #[test]
1203 fn nested_macro_in_fn_params() {
1204     check_no_mismatches(
1205         r#"
1206 macro_rules! U32Inner {
1207     () => {
1208         u32
1209     };
1210 }
1211
1212 macro_rules! U32 {
1213     () => {
1214         U32Inner!()
1215     };
1216 }
1217
1218 fn mamba(a: U32!(), p: u32) -> u32 {
1219     a
1220 }
1221     "#,
1222     )
1223 }
1224
1225 #[test]
1226 fn for_loop_block_expr_iterable() {
1227     check_infer(
1228         r#"
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 _ ...     }': ()
1238             20..21 '_': {unknown}
1239             25..39 '{ let x = 0; }': ()
1240             31..32 'x': i32
1241             35..36 '0': i32
1242             40..66 '{     ...     }': ()
1243             54..55 'y': i32
1244             58..59 '0': i32
1245         "#]],
1246     );
1247 }
1248
1249 #[test]
1250 fn while_loop_block_expr_iterable() {
1251     check_infer(
1252         r#"
1253 fn test() {
1254     while { true } {
1255         let y = 0;
1256     }
1257 }
1258         "#,
1259         expect![[r#"
1260             10..59 '{     ...   } }': ()
1261             16..57 'while ...     }': ()
1262             22..30 '{ true }': bool
1263             24..28 'true': bool
1264             31..57 '{     ...     }': ()
1265             45..46 'y': i32
1266             49..50 '0': i32
1267         "#]],
1268     );
1269 }
1270
1271 #[test]
1272 fn bug_11242() {
1273     // FIXME: wrong, should be u32
1274     check_types(
1275         r#"
1276 fn foo<A, B>()
1277 where
1278     A: IntoIterator<Item = u32>,
1279     B: IntoIterator<Item = usize>,
1280 {
1281     let _x: <A as IntoIterator>::Item;
1282      // ^^ {unknown}
1283 }
1284
1285 pub trait Iterator {
1286     type Item;
1287 }
1288
1289 pub trait IntoIterator {
1290     type Item;
1291     type IntoIter: Iterator<Item = Self::Item>;
1292 }
1293
1294 impl<I: Iterator> IntoIterator for I {
1295     type Item = I::Item;
1296     type IntoIter = I;
1297 }
1298 "#,
1299     );
1300 }
1301
1302 #[test]
1303 fn bug_11659() {
1304     check_no_mismatches(
1305         r#"
1306 struct LinkArray<const N: usize, LD>(LD);
1307 fn f<const N: usize, LD>(x: LD) -> LinkArray<N, LD> {
1308     let r = LinkArray::<N, LD>(x);
1309     r
1310 }
1311
1312 fn test() {
1313     let x = f::<2, i32>(5);
1314     let y = LinkArray::<52, LinkArray<2, i32>>(x);
1315 }
1316         "#,
1317     );
1318     check_no_mismatches(
1319         r#"
1320 struct LinkArray<LD, const N: usize>(LD);
1321 fn f<const N: usize, LD>(x: LD) -> LinkArray<LD, N> {
1322     let r = LinkArray::<LD, N>(x);
1323     r
1324 }
1325
1326 fn test() {
1327     let x = f::<i32, 2>(5);
1328     let y = LinkArray::<LinkArray<i32, 2>, 52>(x);
1329 }
1330         "#,
1331     );
1332 }
1333
1334 #[test]
1335 fn const_generic_error_tolerance() {
1336     check_no_mismatches(
1337         r#"
1338 #[lang = "sized"]
1339 pub trait Sized {}
1340
1341 struct CT<const N: usize, T>(T);
1342 struct TC<T, const N: usize>(T);
1343 fn f<const N: usize, T>(x: T) -> (CT<N, T>, TC<T, N>) {
1344     let l = CT::<N, T>(x);
1345     let r = TC::<N, T>(x);
1346     (l, r)
1347 }
1348
1349 trait TR1<const N: usize>;
1350 trait TR2<const N: usize>;
1351
1352 impl<const N: usize, T> TR1<N> for CT<N, T>;
1353 impl<const N: usize, T> TR1<5> for TC<T, N>;
1354 impl<const N: usize, T> TR2<N> for CT<T, N>;
1355
1356 trait TR3<const N: usize> {
1357     fn tr3(&self) -> &Self;
1358 }
1359
1360 impl<const N: usize, T> TR3<5> for TC<T, N> {
1361     fn tr3(&self) -> &Self {
1362         self
1363     }
1364 }
1365
1366 impl<const N: usize, T> TR3<Item = 5> for TC<T, N> {}
1367 impl<const N: usize, T> TR3<T> for TC<T, N> {}
1368
1369 fn impl_trait<const N: usize>(inp: impl TR1<N>) {}
1370 fn dyn_trait<const N: usize>(inp: &dyn TR2<N>) {}
1371 fn impl_trait_bad<'a, const N: usize>(inp: impl TR1<i32>) -> impl TR1<'a, i32> {}
1372 fn impl_trait_very_bad<const N: usize>(inp: impl TR1<Item = i32>) -> impl TR1<'a, Item = i32, 5, Foo = N> {}
1373
1374 fn test() {
1375     f::<2, i32>(5);
1376     f::<2, 2>(5);
1377     f(5);
1378     f::<i32>(5);
1379     CT::<52, CT<2, i32>>(x);
1380     CT::<CT<2, i32>>(x);
1381     impl_trait_bad(5);
1382     impl_trait_bad(12);
1383     TR3<5>::tr3();
1384     TR3<{ 2+3 }>::tr3();
1385     TC::<i32, 10>(5).tr3();
1386     TC::<i32, 20>(5).tr3();
1387     TC::<i32, i32>(5).tr3();
1388     TC::<i32, { 7 + 3 }>(5).tr3();
1389 }
1390         "#,
1391     );
1392 }
1393
1394 #[test]
1395 fn const_generic_impl_trait() {
1396     check_no_mismatches(
1397         r#"
1398         //- minicore: from
1399
1400         struct Foo<T, const M: usize>;
1401
1402         trait Tr<T> {
1403             fn f(T) -> Self;
1404         }
1405
1406         impl<T, const M: usize> Tr<[T; M]> for Foo<T, M> {
1407             fn f(_: [T; M]) -> Self {
1408                 Self
1409             }
1410         }
1411
1412         fn test() {
1413             Foo::f([1, 2, 7, 10]);
1414         }
1415         "#,
1416     );
1417 }
1418
1419 #[test]
1420 fn nalgebra_factorial() {
1421     check_no_mismatches(
1422         r#"
1423         const FACTORIAL: [u128; 4] = [1, 1, 2, 6];
1424
1425         fn factorial(n: usize) -> u128 {
1426             match FACTORIAL.get(n) {
1427                 Some(f) => *f,
1428                 None => panic!("{}! is greater than u128::MAX", n),
1429             }
1430         }
1431         "#,
1432     )
1433 }