]> git.lizzy.rs Git - rust.git/blob - crates/hir_ty/src/tests/regression.rs
Update tests with expected changes
[rust.git] / crates / hir_ty / src / tests / regression.rs
1 use expect_test::expect;
2
3 use super::{check_infer, 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 infer_std_crash_1() {
139     // caused stack overflow, taken from std
140     check_infer(
141         r#"
142         enum Maybe<T> {
143             Real(T),
144             Fake,
145         }
146
147         fn write() {
148             match something_unknown {
149                 Maybe::Real(ref mut something) => (),
150             }
151         }
152         "#,
153         expect![[r#"
154             53..138 '{     ...   } }': ()
155             59..136 'match ...     }': ()
156             65..82 'someth...nknown': Maybe<{unknown}>
157             93..123 'Maybe:...thing)': Maybe<{unknown}>
158             105..122 'ref mu...ething': &mut {unknown}
159             127..129 '()': ()
160         "#]],
161     );
162 }
163
164 #[test]
165 fn infer_std_crash_2() {
166     // caused "equating two type variables, ...", taken from std
167     check_infer(
168         r#"
169         fn test_line_buffer() {
170             &[0, b'\n', 1, b'\n'];
171         }
172         "#,
173         expect![[r#"
174             22..52 '{     ...n']; }': ()
175             28..49 '&[0, b...b'\n']': &[u8; 4]
176             29..49 '[0, b'...b'\n']': [u8; 4]
177             30..31 '0': u8
178             33..38 'b'\n'': u8
179             40..41 '1': u8
180             43..48 'b'\n'': u8
181         "#]],
182     );
183 }
184
185 #[test]
186 fn infer_std_crash_3() {
187     // taken from rustc
188     check_infer(
189         r#"
190         pub fn compute() {
191             match nope!() {
192                 SizeSkeleton::Pointer { non_zero: true, tail } => {}
193             }
194         }
195         "#,
196         expect![[r#"
197             17..107 '{     ...   } }': ()
198             23..105 'match ...     }': ()
199             29..36 'nope!()': {unknown}
200             47..93 'SizeSk...tail }': {unknown}
201             81..85 'true': bool
202             81..85 'true': bool
203             87..91 'tail': {unknown}
204             97..99 '{}': ()
205         "#]],
206     );
207 }
208
209 #[test]
210 fn infer_std_crash_4() {
211     // taken from rustc
212     check_infer(
213         r#"
214         pub fn primitive_type() {
215             match *self {
216                 BorrowedRef { type_: Primitive(p), ..} => {},
217             }
218         }
219         "#,
220         expect![[r#"
221             24..105 '{     ...   } }': ()
222             30..103 'match ...     }': ()
223             36..41 '*self': {unknown}
224             37..41 'self': {unknown}
225             52..90 'Borrow...), ..}': {unknown}
226             73..85 'Primitive(p)': {unknown}
227             83..84 'p': {unknown}
228             94..96 '{}': ()
229         "#]],
230     );
231 }
232
233 #[test]
234 fn infer_std_crash_5() {
235     // taken from rustc
236     check_infer(
237         r#"
238         fn extra_compiler_flags() {
239             for content in doesnt_matter {
240                 let name = if doesnt_matter {
241                     first
242                 } else {
243                     &content
244                 };
245
246                 let content = if ICE_REPORT_COMPILER_FLAGS_STRIP_VALUE.contains(&name) {
247                     name
248                 } else {
249                     content
250                 };
251             }
252         }
253         "#,
254         expect![[r#"
255             26..322 '{     ...   } }': ()
256             32..320 'for co...     }': ()
257             36..43 'content': {unknown}
258             47..60 'doesnt_matter': {unknown}
259             61..320 '{     ...     }': ()
260             75..79 'name': &{unknown}
261             82..166 'if doe...     }': &{unknown}
262             85..98 'doesnt_matter': bool
263             99..128 '{     ...     }': &{unknown}
264             113..118 'first': &{unknown}
265             134..166 '{     ...     }': &{unknown}
266             148..156 '&content': &{unknown}
267             149..156 'content': {unknown}
268             181..188 'content': &{unknown}
269             191..313 'if ICE...     }': &{unknown}
270             194..231 'ICE_RE..._VALUE': {unknown}
271             194..247 'ICE_RE...&name)': bool
272             241..246 '&name': &&{unknown}
273             242..246 'name': &{unknown}
274             248..276 '{     ...     }': &{unknown}
275             262..266 'name': &{unknown}
276             282..313 '{     ...     }': {unknown}
277             296..303 'content': {unknown}
278         "#]],
279     );
280 }
281
282 #[test]
283 fn infer_nested_generics_crash() {
284     // another crash found typechecking rustc
285     check_infer(
286         r#"
287         struct Canonical<V> {
288             value: V,
289         }
290         struct QueryResponse<V> {
291             value: V,
292         }
293         fn test<R>(query_response: Canonical<QueryResponse<R>>) {
294             &query_response.value;
295         }
296         "#,
297         expect![[r#"
298             91..105 'query_response': Canonical<QueryResponse<R>>
299             136..166 '{     ...lue; }': ()
300             142..163 '&query....value': &QueryResponse<R>
301             143..157 'query_response': Canonical<QueryResponse<R>>
302             143..163 'query_....value': QueryResponse<R>
303         "#]],
304     );
305 }
306
307 #[test]
308 fn infer_paren_macro_call() {
309     check_infer(
310         r#"
311         macro_rules! bar { () => {0u32} }
312         fn test() {
313             let a = (bar!());
314         }
315         "#,
316         expect![[r#"
317             !0..4 '0u32': u32
318             44..69 '{     ...()); }': ()
319             54..55 'a': u32
320         "#]],
321     );
322 }
323
324 #[test]
325 fn infer_array_macro_call() {
326     check_infer(
327         r#"
328         macro_rules! bar { () => {0u32} }
329         fn test() {
330             let a = [bar!()];
331         }
332         "#,
333         expect![[r#"
334             !0..4 '0u32': u32
335             44..69 '{     ...()]; }': ()
336             54..55 'a': [u32; 1]
337             58..66 '[bar!()]': [u32; 1]
338         "#]],
339     );
340 }
341
342 #[test]
343 fn bug_1030() {
344     check_infer(
345         r#"
346         struct HashSet<T, H>;
347         struct FxHasher;
348         type FxHashSet<T> = HashSet<T, FxHasher>;
349
350         impl<T, H> HashSet<T, H> {
351             fn default() -> HashSet<T, H> {}
352         }
353
354         pub fn main_loop() {
355             FxHashSet::default();
356         }
357         "#,
358         expect![[r#"
359             143..145 '{}': ()
360             168..197 '{     ...t(); }': ()
361             174..192 'FxHash...efault': fn default<{unknown}, FxHasher>() -> HashSet<{unknown}, FxHasher>
362             174..194 'FxHash...ault()': HashSet<{unknown}, FxHasher>
363         "#]],
364     );
365 }
366
367 #[test]
368 fn issue_2669() {
369     check_infer(
370         r#"
371         trait A {}
372         trait Write {}
373         struct Response<T> {}
374
375         trait D {
376             fn foo();
377         }
378
379         impl<T:A> D for Response<T> {
380             fn foo() {
381                 end();
382                 fn end<W: Write>() {
383                     let _x: T =  loop {};
384                 }
385             }
386         }
387         "#,
388         expect![[r#"
389             119..214 '{     ...     }': ()
390             129..132 'end': fn end<{unknown}>()
391             129..134 'end()': ()
392             163..208 '{     ...     }': ()
393             181..183 '_x': !
394             190..197 'loop {}': !
395             195..197 '{}': ()
396         "#]],
397     )
398 }
399
400 #[test]
401 fn issue_2705() {
402     check_infer(
403         r#"
404         trait Trait {}
405         fn test() {
406             <Trait<u32>>::foo()
407         }
408         "#,
409         expect![[r#"
410             25..52 '{     ...oo() }': ()
411             31..48 '<Trait...>::foo': {unknown}
412             31..50 '<Trait...:foo()': ()
413         "#]],
414     );
415 }
416
417 #[test]
418 fn issue_2683_chars_impl() {
419     check_types(
420         r#"
421 //- /main.rs crate:main deps:std
422 fn test() {
423     let chars: std::str::Chars<'_>;
424     (chars.next(), chars.nth(1));
425 } //^ (Option<char>, Option<char>)
426
427 //- /std.rs crate:std
428 #[prelude_import]
429 use prelude::*;
430
431 pub mod prelude {
432     pub use crate::iter::Iterator;
433     pub use crate::option::Option;
434 }
435
436 pub mod iter {
437     pub use self::traits::Iterator;
438     pub mod traits {
439         pub use self::iterator::Iterator;
440
441         pub mod iterator {
442             pub trait Iterator {
443                 type Item;
444                 fn next(&mut self) -> Option<Self::Item>;
445                 fn nth(&mut self, n: usize) -> Option<Self::Item> {}
446             }
447         }
448     }
449 }
450
451 pub mod option {
452     pub enum Option<T> {}
453 }
454
455 pub mod str {
456     pub struct Chars<'a> {}
457     impl<'a> Iterator for Chars<'a> {
458         type Item = char;
459         fn next(&mut self) -> Option<char> {}
460     }
461 }
462 "#,
463     );
464 }
465
466 #[test]
467 fn issue_3642_bad_macro_stackover() {
468     check_types(
469         r#"
470 #[macro_export]
471 macro_rules! match_ast {
472     (match $node:ident { $($tt:tt)* }) => { match_ast!(match ($node) { $($tt)* }) };
473
474     (match ($node:expr) {
475         $( ast::$ast:ident($it:ident) => $res:expr, )*
476         _ => $catch_all:expr $(,)?
477     }) => {{
478         $( if let Some($it) = ast::$ast::cast($node.clone()) { $res } else )*
479         { $catch_all }
480     }};
481 }
482
483 fn main() {
484     let anchor = match_ast! {
485        //^ ()
486         match parent {
487             as => {},
488             _ => return None
489         }
490     };
491 }"#,
492     );
493 }
494
495 #[test]
496 fn issue_3999_slice() {
497     check_infer(
498         r#"
499         fn foo(params: &[usize]) {
500             match params {
501                 [ps @ .., _] => {}
502             }
503         }
504         "#,
505         expect![[r#"
506             7..13 'params': &[usize]
507             25..80 '{     ...   } }': ()
508             31..78 'match ...     }': ()
509             37..43 'params': &[usize]
510             54..66 '[ps @ .., _]': [usize]
511             55..62 'ps @ ..': &[usize]
512             60..62 '..': [usize]
513             64..65 '_': usize
514             70..72 '{}': ()
515         "#]],
516     );
517 }
518
519 #[test]
520 fn issue_3999_struct() {
521     // rust-analyzer should not panic on seeing this malformed
522     // record pattern.
523     check_infer(
524         r#"
525         struct Bar {
526             a: bool,
527         }
528         fn foo(b: Bar) {
529             match b {
530                 Bar { a: .. } => {},
531             }
532         }
533         "#,
534         expect![[r#"
535             35..36 'b': Bar
536             43..95 '{     ...   } }': ()
537             49..93 'match ...     }': ()
538             55..56 'b': Bar
539             67..80 'Bar { a: .. }': Bar
540             76..78 '..': bool
541             84..86 '{}': ()
542         "#]],
543     );
544 }
545
546 #[test]
547 fn issue_4235_name_conflicts() {
548     check_infer(
549         r#"
550         struct FOO {}
551         static FOO:FOO = FOO {};
552
553         impl FOO {
554             fn foo(&self) {}
555         }
556
557         fn main() {
558             let a = &FOO;
559             a.foo();
560         }
561         "#,
562         expect![[r#"
563             31..37 'FOO {}': FOO
564             63..67 'self': &FOO
565             69..71 '{}': ()
566             85..119 '{     ...o(); }': ()
567             95..96 'a': &FOO
568             99..103 '&FOO': &FOO
569             100..103 'FOO': FOO
570             109..110 'a': &FOO
571             109..116 'a.foo()': ()
572         "#]],
573     );
574 }
575
576 #[test]
577 fn issue_4465_dollar_crate_at_type() {
578     check_infer(
579         r#"
580         pub struct Foo {}
581         pub fn anything<T>() -> T {
582             loop {}
583         }
584         macro_rules! foo {
585             () => {{
586                 let r: $crate::Foo = anything();
587                 r
588             }};
589         }
590         fn main() {
591             let _a = foo!();
592         }
593         "#,
594         expect![[r#"
595             44..59 '{     loop {} }': T
596             50..57 'loop {}': !
597             55..57 '{}': ()
598             !0..31 '{letr:...g();r}': Foo
599             !4..5 'r': Foo
600             !18..26 'anything': fn anything<Foo>() -> Foo
601             !18..28 'anything()': Foo
602             !29..30 'r': Foo
603             163..187 '{     ...!(); }': ()
604             173..175 '_a': Foo
605         "#]],
606     );
607 }
608
609 #[test]
610 fn issue_6811() {
611     check_infer(
612         r#"
613         macro_rules! profile_function {
614             () => {
615                 let _a = 1;
616                 let _b = 1;
617             };
618         }
619         fn main() {
620             profile_function!();
621         }
622         "#,
623         expect![[r#"
624             !3..5 '_a': i32
625             !6..7 '1': i32
626             !11..13 '_b': i32
627             !14..15 '1': i32
628             103..131 '{     ...!(); }': ()
629         "#]],
630     );
631 }
632
633 #[test]
634 fn issue_4053_diesel_where_clauses() {
635     check_infer(
636         r#"
637         trait BoxedDsl<DB> {
638             type Output;
639             fn internal_into_boxed(self) -> Self::Output;
640         }
641
642         struct SelectStatement<From, Select, Distinct, Where, Order, LimitOffset, GroupBy, Locking> {
643             order: Order,
644         }
645
646         trait QueryFragment<DB: Backend> {}
647
648         trait Into<T> { fn into(self) -> T; }
649
650         impl<F, S, D, W, O, LOf, DB> BoxedDsl<DB>
651             for SelectStatement<F, S, D, W, O, LOf, G>
652         where
653             O: Into<dyn QueryFragment<DB>>,
654         {
655             type Output = XXX;
656
657             fn internal_into_boxed(self) -> Self::Output {
658                 self.order.into();
659             }
660         }
661         "#,
662         expect![[r#"
663             65..69 'self': Self
664             267..271 'self': Self
665             466..470 'self': SelectStatement<F, S, D, W, O, LOf, {unknown}, {unknown}>
666             488..522 '{     ...     }': ()
667             498..502 'self': SelectStatement<F, S, D, W, O, LOf, {unknown}, {unknown}>
668             498..508 'self.order': O
669             498..515 'self.o...into()': dyn QueryFragment<DB>
670         "#]],
671     );
672 }
673
674 #[test]
675 fn issue_4953() {
676     check_infer(
677         r#"
678         pub struct Foo(pub i64);
679         impl Foo {
680             fn test() -> Self { Self(0i64) }
681         }
682         "#,
683         expect![[r#"
684             58..72 '{ Self(0i64) }': Foo
685             60..64 'Self': Foo(i64) -> Foo
686             60..70 'Self(0i64)': Foo
687             65..69 '0i64': i64
688         "#]],
689     );
690     check_infer(
691         r#"
692         pub struct Foo<T>(pub T);
693         impl Foo<i64> {
694             fn test() -> Self { Self(0i64) }
695         }
696         "#,
697         expect![[r#"
698             64..78 '{ Self(0i64) }': Foo<i64>
699             66..70 'Self': Foo<i64>(i64) -> Foo<i64>
700             66..76 'Self(0i64)': Foo<i64>
701             71..75 '0i64': i64
702         "#]],
703     );
704 }
705
706 #[test]
707 fn issue_4931() {
708     check_infer(
709         r#"
710         trait Div<T> {
711             type Output;
712         }
713
714         trait CheckedDiv: Div<()> {}
715
716         trait PrimInt: CheckedDiv<Output = ()> {
717             fn pow(self);
718         }
719
720         fn check<T: PrimInt>(i: T) {
721             i.pow();
722         }
723         "#,
724         expect![[r#"
725             117..121 'self': Self
726             148..149 'i': T
727             154..170 '{     ...w(); }': ()
728             160..161 'i': T
729             160..167 'i.pow()': ()
730         "#]],
731     );
732 }
733
734 #[test]
735 fn issue_4885() {
736     check_infer(
737         r#"
738         #[lang = "coerce_unsized"]
739         pub trait CoerceUnsized<T> {}
740
741         trait Future {
742             type Output;
743         }
744         trait Foo<R> {
745             type Bar;
746         }
747         fn foo<R, K>(key: &K) -> impl Future<Output = K::Bar>
748         where
749             K: Foo<R>,
750         {
751             bar(key)
752         }
753         fn bar<R, K>(key: &K) -> impl Future<Output = K::Bar>
754         where
755             K: Foo<R>,
756         {
757         }
758         "#,
759         expect![[r#"
760             136..139 'key': &K
761             198..214 '{     ...key) }': {unknown}
762             204..207 'bar': fn bar<R, K>(&K) -> impl Future<Output = <K as Foo<R>>::Bar>
763             204..212 'bar(key)': impl Future<Output = <K as Foo<R>>::Bar>
764             208..211 'key': &K
765             228..231 'key': &K
766             290..293 '{ }': ()
767         "#]],
768     );
769 }
770
771 #[test]
772 fn issue_4800() {
773     check_infer(
774         r#"
775         trait Debug {}
776
777         struct Foo<T>;
778
779         type E1<T> = (T, T, T);
780         type E2<T> = E1<E1<E1<(T, T, T)>>>;
781
782         impl Debug for Foo<E2<()>> {}
783
784         struct Request;
785
786         pub trait Future {
787             type Output;
788         }
789
790         pub struct PeerSet<D>;
791
792         impl<D> Service<Request> for PeerSet<D>
793         where
794             D: Discover,
795             D::Key: Debug,
796         {
797             type Error = ();
798             type Future = dyn Future<Output = Self::Error>;
799
800             fn call(&mut self) -> Self::Future {
801                 loop {}
802             }
803         }
804
805         pub trait Discover {
806             type Key;
807         }
808
809         pub trait Service<Request> {
810             type Error;
811             type Future: Future<Output = Self::Error>;
812             fn call(&mut self) -> Self::Future;
813         }
814         "#,
815         expect![[r#"
816             379..383 'self': &mut PeerSet<D>
817             401..424 '{     ...     }': dyn Future<Output = ()>
818             411..418 'loop {}': !
819             416..418 '{}': ()
820             575..579 'self': &mut Self
821         "#]],
822     );
823 }
824
825 #[test]
826 fn issue_4966() {
827     check_infer(
828         r#"
829         pub trait IntoIterator {
830             type Item;
831         }
832
833         struct Repeat<A> { element: A }
834
835         struct Map<F> { f: F }
836
837         struct Vec<T> {}
838
839         #[lang = "deref"]
840         pub trait Deref {
841             type Target;
842         }
843
844         impl<T> Deref for Vec<T> {
845             type Target = [T];
846         }
847
848         fn from_iter<A, T: IntoIterator<Item = A>>(iter: T) -> Vec<A> {}
849
850         fn main() {
851             let inner = Map { f: |_: &f64| 0.0 };
852
853             let repeat = Repeat { element: inner };
854
855             let vec = from_iter(repeat);
856
857             vec.foo_bar();
858         }
859         "#,
860         expect![[r#"
861             270..274 'iter': T
862             289..291 '{}': ()
863             303..447 '{     ...r(); }': ()
864             313..318 'inner': Map<|&f64| -> f64>
865             321..345 'Map { ... 0.0 }': Map<|&f64| -> f64>
866             330..343 '|_: &f64| 0.0': |&f64| -> f64
867             331..332 '_': &f64
868             340..343 '0.0': f64
869             356..362 'repeat': Repeat<Map<|&f64| -> f64>>
870             365..390 'Repeat...nner }': Repeat<Map<|&f64| -> f64>>
871             383..388 'inner': Map<|&f64| -> f64>
872             401..404 'vec': Vec<IntoIterator::Item<Repeat<Map<|&f64| -> f64>>>>
873             407..416 '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>>>>
874             407..424 'from_i...epeat)': Vec<IntoIterator::Item<Repeat<Map<|&f64| -> f64>>>>
875             417..423 'repeat': Repeat<Map<|&f64| -> f64>>
876             431..434 'vec': Vec<IntoIterator::Item<Repeat<Map<|&f64| -> f64>>>>
877             431..444 'vec.foo_bar()': {unknown}
878         "#]],
879     );
880 }
881
882 #[test]
883 fn issue_6628() {
884     check_infer(
885         r#"
886         #[lang = "fn_once"]
887         pub trait FnOnce<Args> {
888             type Output;
889         }
890
891         struct S<T>();
892         impl<T> S<T> {
893             fn f(&self, _t: T) {}
894             fn g<F: FnOnce(&T)>(&self, _f: F) {}
895         }
896         fn main() {
897             let s = S();
898             s.g(|_x| {});
899             s.f(10);
900         }
901         "#,
902         expect![[r#"
903             105..109 'self': &S<T>
904             111..113 '_t': T
905             118..120 '{}': ()
906             146..150 'self': &S<T>
907             152..154 '_f': F
908             159..161 '{}': ()
909             174..225 '{     ...10); }': ()
910             184..185 's': S<i32>
911             188..189 'S': S<i32>() -> S<i32>
912             188..191 'S()': S<i32>
913             197..198 's': S<i32>
914             197..209 's.g(|_x| {})': ()
915             201..208 '|_x| {}': |&i32| -> ()
916             202..204 '_x': &i32
917             206..208 '{}': ()
918             215..216 's': S<i32>
919             215..222 's.f(10)': ()
920             219..221 '10': i32
921         "#]],
922     );
923 }
924
925 #[test]
926 fn issue_6852() {
927     check_infer(
928         r#"
929         #[lang = "deref"]
930         pub trait Deref {
931             type Target;
932         }
933
934         struct BufWriter {}
935
936         struct Mutex<T> {}
937         struct MutexGuard<'a, T> {}
938         impl<T> Mutex<T> {
939             fn lock(&self) -> MutexGuard<'_, T> {}
940         }
941         impl<'a, T: 'a> Deref for MutexGuard<'a, T> {
942             type Target = T;
943         }
944         fn flush(&self) {
945             let w: &Mutex<BufWriter>;
946             *(w.lock());
947         }
948         "#,
949         expect![[r#"
950             156..160 'self': &Mutex<T>
951             183..185 '{}': ()
952             267..271 'self': &{unknown}
953             273..323 '{     ...()); }': ()
954             283..284 'w': &Mutex<BufWriter>
955             309..320 '*(w.lock())': BufWriter
956             311..312 'w': &Mutex<BufWriter>
957             311..319 'w.lock()': MutexGuard<BufWriter>
958         "#]],
959     );
960 }
961
962 #[test]
963 fn param_overrides_fn() {
964     check_types(
965         r#"
966         fn example(example: i32) {
967             fn f() {}
968             example;
969           //^^^^^^^ i32
970         }
971         "#,
972     )
973 }
974
975 #[test]
976 fn lifetime_from_chalk_during_deref() {
977     check_types(
978         r#"
979         #[lang = "deref"]
980         pub trait Deref {
981             type Target;
982         }
983
984         struct Box<T: ?Sized> {}
985         impl<T> Deref for Box<T> {
986             type Target = T;
987
988             fn deref(&self) -> &Self::Target {
989                 loop {}
990             }
991         }
992
993         trait Iterator {
994             type Item;
995         }
996
997         pub struct Iter<'a, T: 'a> {
998             inner: Box<dyn IterTrait<'a, T, Item = &'a T> + 'a>,
999         }
1000
1001         trait IterTrait<'a, T: 'a>: Iterator<Item = &'a T> {
1002             fn clone_box(&self);
1003         }
1004
1005         fn clone_iter<T>(s: Iter<T>) {
1006             s.inner.clone_box();
1007           //^^^^^^^^^^^^^^^^^^^ ()
1008         }
1009         "#,
1010     )
1011 }
1012
1013 #[test]
1014 fn issue_8686() {
1015     check_infer(
1016         r#"
1017 pub trait Try: FromResidual {
1018     type Output;
1019     type Residual;
1020 }
1021 pub trait FromResidual<R = <Self as Try>::Residual> {
1022      fn from_residual(residual: R) -> Self;
1023 }
1024
1025 struct ControlFlow<B, C>;
1026 impl<B, C> Try for ControlFlow<B, C> {
1027     type Output = C;
1028     type Residual = ControlFlow<B, !>;
1029 }
1030 impl<B, C> FromResidual for ControlFlow<B, C> {
1031     fn from_residual(r: ControlFlow<B, !>) -> Self { ControlFlow }
1032 }
1033
1034 fn test() {
1035     ControlFlow::from_residual(ControlFlow::<u32, !>);
1036 }
1037         "#,
1038         expect![[r#"
1039             144..152 'residual': R
1040             365..366 'r': ControlFlow<B, !>
1041             395..410 '{ ControlFlow }': ControlFlow<B, C>
1042             397..408 'ControlFlow': ControlFlow<B, C>
1043             424..482 '{     ...!>); }': ()
1044             430..456 'Contro...sidual': fn from_residual<ControlFlow<u32, {unknown}>, ControlFlow<u32, !>>(ControlFlow<u32, !>) -> ControlFlow<u32, {unknown}>
1045             430..479 'Contro...2, !>)': ControlFlow<u32, {unknown}>
1046             457..478 'Contro...32, !>': ControlFlow<u32, !>
1047         "#]],
1048     );
1049 }
1050
1051 #[test]
1052 fn cfg_tail() {
1053     // https://github.com/rust-analyzer/rust-analyzer/issues/8378
1054     check_infer(
1055         r#"
1056         fn fake_tail(){
1057             { "first" }
1058             #[cfg(never)] 9
1059         }
1060         fn multiple_fake(){
1061             { "fake" }
1062             { "fake" }
1063             { "second" }
1064             #[cfg(never)] { 11 }
1065             #[cfg(never)] 12;
1066             #[cfg(never)] 13
1067         }
1068         fn no_normal_tail(){
1069             { "third" }
1070             #[cfg(never)] 14;
1071             #[cfg(never)] 15;
1072         }
1073         fn no_actual_tail(){
1074             { "fourth" };
1075             #[cfg(never)] 14;
1076             #[cfg(never)] 15
1077         }
1078         "#,
1079         expect![[r#"
1080             14..53 '{     ...)] 9 }': &str
1081             20..31 '{ "first" }': &str
1082             22..29 '"first"': &str
1083             72..190 '{     ...] 13 }': &str
1084             78..88 '{ "fake" }': &str
1085             80..86 '"fake"': &str
1086             93..103 '{ "fake" }': &str
1087             95..101 '"fake"': &str
1088             108..120 '{ "second" }': &str
1089             110..118 '"second"': &str
1090             210..273 '{     ... 15; }': &str
1091             216..227 '{ "third" }': &str
1092             218..225 '"third"': &str
1093             293..357 '{     ...] 15 }': ()
1094             299..311 '{ "fourth" }': &str
1095             301..309 '"fourth"': &str
1096         "#]],
1097     )
1098 }