]> git.lizzy.rs Git - rust.git/blob - src/tools/rust-analyzer/crates/ide/src/goto_definition.rs
Auto merge of #97581 - AngelicosPhosphoros:improve_calloc_check_in_vec_macro_for_tupl...
[rust.git] / src / tools / rust-analyzer / crates / ide / src / goto_definition.rs
1 use std::{convert::TryInto, mem::discriminant};
2
3 use crate::{doc_links::token_as_doc_comment, FilePosition, NavigationTarget, RangeInfo, TryToNav};
4 use hir::{AsAssocItem, AssocItem, Semantics};
5 use ide_db::{
6     base_db::{AnchoredPath, FileId, FileLoader},
7     defs::{Definition, IdentClass},
8     helpers::pick_best_token,
9     RootDatabase,
10 };
11 use itertools::Itertools;
12 use syntax::{ast, AstNode, AstToken, SyntaxKind::*, SyntaxToken, TextRange, T};
13
14 // Feature: Go to Definition
15 //
16 // Navigates to the definition of an identifier.
17 //
18 // For outline modules, this will navigate to the source file of the module.
19 //
20 // |===
21 // | Editor  | Shortcut
22 //
23 // | VS Code | kbd:[F12]
24 // |===
25 //
26 // image::https://user-images.githubusercontent.com/48062697/113065563-025fbe00-91b1-11eb-83e4-a5a703610b23.gif[]
27 pub(crate) fn goto_definition(
28     db: &RootDatabase,
29     position: FilePosition,
30 ) -> Option<RangeInfo<Vec<NavigationTarget>>> {
31     let sema = &Semantics::new(db);
32     let file = sema.parse(position.file_id).syntax().clone();
33     let original_token =
34         pick_best_token(file.token_at_offset(position.offset), |kind| match kind {
35             IDENT
36             | INT_NUMBER
37             | LIFETIME_IDENT
38             | T![self]
39             | T![super]
40             | T![crate]
41             | T![Self]
42             | COMMENT => 2,
43             kind if kind.is_trivia() => 0,
44             _ => 1,
45         })?;
46     if let Some(doc_comment) = token_as_doc_comment(&original_token) {
47         return doc_comment.get_definition_with_descend_at(sema, position.offset, |def, _, _| {
48             let nav = def.try_to_nav(db)?;
49             Some(RangeInfo::new(original_token.text_range(), vec![nav]))
50         });
51     }
52     let navs = sema
53         .descend_into_macros(original_token.clone())
54         .into_iter()
55         .filter_map(|token| {
56             let parent = token.parent()?;
57             if let Some(tt) = ast::TokenTree::cast(parent) {
58                 if let Some(x) = try_lookup_include_path(sema, tt, token.clone(), position.file_id)
59                 {
60                     return Some(vec![x]);
61                 }
62             }
63             Some(
64                 IdentClass::classify_token(sema, &token)?
65                     .definitions()
66                     .into_iter()
67                     .flat_map(|def| {
68                         try_filter_trait_item_definition(sema, &def)
69                             .unwrap_or_else(|| def_to_nav(sema.db, def))
70                     })
71                     .collect(),
72             )
73         })
74         .flatten()
75         .unique()
76         .collect::<Vec<NavigationTarget>>();
77
78     Some(RangeInfo::new(original_token.text_range(), navs))
79 }
80
81 fn try_lookup_include_path(
82     sema: &Semantics<'_, RootDatabase>,
83     tt: ast::TokenTree,
84     token: SyntaxToken,
85     file_id: FileId,
86 ) -> Option<NavigationTarget> {
87     let token = ast::String::cast(token)?;
88     let path = token.value()?.into_owned();
89     let macro_call = tt.syntax().parent().and_then(ast::MacroCall::cast)?;
90     let name = macro_call.path()?.segment()?.name_ref()?;
91     if !matches!(&*name.text(), "include" | "include_str" | "include_bytes") {
92         return None;
93     }
94     let file_id = sema.db.resolve_path(AnchoredPath { anchor: file_id, path: &path })?;
95     let size = sema.db.file_text(file_id).len().try_into().ok()?;
96     Some(NavigationTarget {
97         file_id,
98         full_range: TextRange::new(0.into(), size),
99         name: path.into(),
100         focus_range: None,
101         kind: None,
102         container_name: None,
103         description: None,
104         docs: None,
105     })
106 }
107 /// finds the trait definition of an impl'd item, except function
108 /// e.g.
109 /// ```rust
110 /// trait A { type a; }
111 /// struct S;
112 /// impl A for S { type a = i32; } // <-- on this associate type, will get the location of a in the trait
113 /// ```
114 fn try_filter_trait_item_definition(
115     sema: &Semantics<'_, RootDatabase>,
116     def: &Definition,
117 ) -> Option<Vec<NavigationTarget>> {
118     let db = sema.db;
119     let assoc = def.as_assoc_item(db)?;
120     match assoc {
121         AssocItem::Function(..) => None,
122         AssocItem::Const(..) | AssocItem::TypeAlias(..) => {
123             let imp = match assoc.container(db) {
124                 hir::AssocItemContainer::Impl(imp) => imp,
125                 _ => return None,
126             };
127             let trait_ = imp.trait_(db)?;
128             let name = def.name(db)?;
129             let discri_value = discriminant(&assoc);
130             trait_
131                 .items(db)
132                 .iter()
133                 .filter(|itm| discriminant(*itm) == discri_value)
134                 .find_map(|itm| (itm.name(db)? == name).then(|| itm.try_to_nav(db)).flatten())
135                 .map(|it| vec![it])
136         }
137     }
138 }
139
140 fn def_to_nav(db: &RootDatabase, def: Definition) -> Vec<NavigationTarget> {
141     def.try_to_nav(db).map(|it| vec![it]).unwrap_or_default()
142 }
143
144 #[cfg(test)]
145 mod tests {
146     use ide_db::base_db::FileRange;
147     use itertools::Itertools;
148
149     use crate::fixture;
150
151     #[track_caller]
152     fn check(ra_fixture: &str) {
153         let (analysis, position, expected) = fixture::annotations(ra_fixture);
154         let navs = analysis.goto_definition(position).unwrap().expect("no definition found").info;
155         if navs.is_empty() {
156             panic!("unresolved reference")
157         }
158
159         let cmp = |&FileRange { file_id, range }: &_| (file_id, range.start());
160         let navs = navs
161             .into_iter()
162             .map(|nav| FileRange { file_id: nav.file_id, range: nav.focus_or_full_range() })
163             .sorted_by_key(cmp)
164             .collect::<Vec<_>>();
165         let expected = expected
166             .into_iter()
167             .map(|(FileRange { file_id, range }, _)| FileRange { file_id, range })
168             .sorted_by_key(cmp)
169             .collect::<Vec<_>>();
170         assert_eq!(expected, navs);
171     }
172
173     fn check_unresolved(ra_fixture: &str) {
174         let (analysis, position) = fixture::position(ra_fixture);
175         let navs = analysis.goto_definition(position).unwrap().expect("no definition found").info;
176
177         assert!(navs.is_empty(), "didn't expect this to resolve anywhere: {:?}", navs)
178     }
179
180     #[test]
181     fn goto_def_if_items_same_name() {
182         check(
183             r#"
184 trait Trait {
185     type A;
186     const A: i32;
187         //^
188 }
189
190 struct T;
191 impl Trait for T {
192     type A = i32;
193     const A$0: i32 = -9;
194 }"#,
195         );
196     }
197     #[test]
198     fn goto_def_in_mac_call_in_attr_invoc() {
199         check(
200             r#"
201 //- proc_macros: identity
202 pub struct Struct {
203         // ^^^^^^
204     field: i32,
205 }
206
207 macro_rules! identity {
208     ($($tt:tt)*) => {$($tt)*};
209 }
210
211 #[proc_macros::identity]
212 fn function() {
213     identity!(Struct$0 { field: 0 });
214 }
215
216 "#,
217         )
218     }
219
220     #[test]
221     fn goto_def_for_extern_crate() {
222         check(
223             r#"
224 //- /main.rs crate:main deps:std
225 extern crate std$0;
226 //- /std/lib.rs crate:std
227 // empty
228 //^file
229 "#,
230         )
231     }
232
233     #[test]
234     fn goto_def_for_renamed_extern_crate() {
235         check(
236             r#"
237 //- /main.rs crate:main deps:std
238 extern crate std as abc$0;
239 //- /std/lib.rs crate:std
240 // empty
241 //^file
242 "#,
243         )
244     }
245
246     #[test]
247     fn goto_def_in_items() {
248         check(
249             r#"
250 struct Foo;
251      //^^^
252 enum E { X(Foo$0) }
253 "#,
254         );
255     }
256
257     #[test]
258     fn goto_def_at_start_of_item() {
259         check(
260             r#"
261 struct Foo;
262      //^^^
263 enum E { X($0Foo) }
264 "#,
265         );
266     }
267
268     #[test]
269     fn goto_definition_resolves_correct_name() {
270         check(
271             r#"
272 //- /lib.rs
273 use a::Foo;
274 mod a;
275 mod b;
276 enum E { X(Foo$0) }
277
278 //- /a.rs
279 struct Foo;
280      //^^^
281 //- /b.rs
282 struct Foo;
283 "#,
284         );
285     }
286
287     #[test]
288     fn goto_def_for_module_declaration() {
289         check(
290             r#"
291 //- /lib.rs
292 mod $0foo;
293
294 //- /foo.rs
295 // empty
296 //^file
297 "#,
298         );
299
300         check(
301             r#"
302 //- /lib.rs
303 mod $0foo;
304
305 //- /foo/mod.rs
306 // empty
307 //^file
308 "#,
309         );
310     }
311
312     #[test]
313     fn goto_def_for_macros() {
314         check(
315             r#"
316 macro_rules! foo { () => { () } }
317            //^^^
318 fn bar() {
319     $0foo!();
320 }
321 "#,
322         );
323     }
324
325     #[test]
326     fn goto_def_for_macros_from_other_crates() {
327         check(
328             r#"
329 //- /lib.rs crate:main deps:foo
330 use foo::foo;
331 fn bar() {
332     $0foo!();
333 }
334
335 //- /foo/lib.rs crate:foo
336 #[macro_export]
337 macro_rules! foo { () => { () } }
338            //^^^
339 "#,
340         );
341     }
342
343     #[test]
344     fn goto_def_for_macros_in_use_tree() {
345         check(
346             r#"
347 //- /lib.rs crate:main deps:foo
348 use foo::foo$0;
349
350 //- /foo/lib.rs crate:foo
351 #[macro_export]
352 macro_rules! foo { () => { () } }
353            //^^^
354 "#,
355         );
356     }
357
358     #[test]
359     fn goto_def_for_macro_defined_fn_with_arg() {
360         check(
361             r#"
362 //- /lib.rs
363 macro_rules! define_fn {
364     ($name:ident) => (fn $name() {})
365 }
366
367 define_fn!(foo);
368          //^^^
369
370 fn bar() {
371    $0foo();
372 }
373 "#,
374         );
375     }
376
377     #[test]
378     fn goto_def_for_macro_defined_fn_no_arg() {
379         check(
380             r#"
381 //- /lib.rs
382 macro_rules! define_fn {
383     () => (fn foo() {})
384 }
385
386   define_fn!();
387 //^^^^^^^^^^^^^
388
389 fn bar() {
390    $0foo();
391 }
392 "#,
393         );
394     }
395
396     #[test]
397     fn goto_definition_works_for_macro_inside_pattern() {
398         check(
399             r#"
400 //- /lib.rs
401 macro_rules! foo {() => {0}}
402            //^^^
403
404 fn bar() {
405     match (0,1) {
406         ($0foo!(), _) => {}
407     }
408 }
409 "#,
410         );
411     }
412
413     #[test]
414     fn goto_definition_works_for_macro_inside_match_arm_lhs() {
415         check(
416             r#"
417 //- /lib.rs
418 macro_rules! foo {() => {0}}
419            //^^^
420 fn bar() {
421     match 0 {
422         $0foo!() => {}
423     }
424 }
425 "#,
426         );
427     }
428
429     #[test]
430     fn goto_def_for_use_alias() {
431         check(
432             r#"
433 //- /lib.rs crate:main deps:foo
434 use foo as bar$0;
435
436 //- /foo/lib.rs crate:foo
437 // empty
438 //^file
439 "#,
440         );
441     }
442
443     #[test]
444     fn goto_def_for_use_alias_foo_macro() {
445         check(
446             r#"
447 //- /lib.rs crate:main deps:foo
448 use foo::foo as bar$0;
449
450 //- /foo/lib.rs crate:foo
451 #[macro_export]
452 macro_rules! foo { () => { () } }
453            //^^^
454 "#,
455         );
456     }
457
458     #[test]
459     fn goto_def_for_methods() {
460         check(
461             r#"
462 struct Foo;
463 impl Foo {
464     fn frobnicate(&self) { }
465      //^^^^^^^^^^
466 }
467
468 fn bar(foo: &Foo) {
469     foo.frobnicate$0();
470 }
471 "#,
472         );
473     }
474
475     #[test]
476     fn goto_def_for_fields() {
477         check(
478             r#"
479 struct Foo {
480     spam: u32,
481 } //^^^^
482
483 fn bar(foo: &Foo) {
484     foo.spam$0;
485 }
486 "#,
487         );
488     }
489
490     #[test]
491     fn goto_def_for_record_fields() {
492         check(
493             r#"
494 //- /lib.rs
495 struct Foo {
496     spam: u32,
497 } //^^^^
498
499 fn bar() -> Foo {
500     Foo {
501         spam$0: 0,
502     }
503 }
504 "#,
505         );
506     }
507
508     #[test]
509     fn goto_def_for_record_pat_fields() {
510         check(
511             r#"
512 //- /lib.rs
513 struct Foo {
514     spam: u32,
515 } //^^^^
516
517 fn bar(foo: Foo) -> Foo {
518     let Foo { spam$0: _, } = foo
519 }
520 "#,
521         );
522     }
523
524     #[test]
525     fn goto_def_for_record_fields_macros() {
526         check(
527             r"
528 macro_rules! m { () => { 92 };}
529 struct Foo { spam: u32 }
530            //^^^^
531
532 fn bar() -> Foo {
533     Foo { spam$0: m!() }
534 }
535 ",
536         );
537     }
538
539     #[test]
540     fn goto_for_tuple_fields() {
541         check(
542             r#"
543 struct Foo(u32);
544          //^^^
545
546 fn bar() {
547     let foo = Foo(0);
548     foo.$00;
549 }
550 "#,
551         );
552     }
553
554     #[test]
555     fn goto_def_for_ufcs_inherent_methods() {
556         check(
557             r#"
558 struct Foo;
559 impl Foo {
560     fn frobnicate() { }
561 }    //^^^^^^^^^^
562
563 fn bar(foo: &Foo) {
564     Foo::frobnicate$0();
565 }
566 "#,
567         );
568     }
569
570     #[test]
571     fn goto_def_for_ufcs_trait_methods_through_traits() {
572         check(
573             r#"
574 trait Foo {
575     fn frobnicate();
576 }    //^^^^^^^^^^
577
578 fn bar() {
579     Foo::frobnicate$0();
580 }
581 "#,
582         );
583     }
584
585     #[test]
586     fn goto_def_for_ufcs_trait_methods_through_self() {
587         check(
588             r#"
589 struct Foo;
590 trait Trait {
591     fn frobnicate();
592 }    //^^^^^^^^^^
593 impl Trait for Foo {}
594
595 fn bar() {
596     Foo::frobnicate$0();
597 }
598 "#,
599         );
600     }
601
602     #[test]
603     fn goto_definition_on_self() {
604         check(
605             r#"
606 struct Foo;
607 impl Foo {
608    //^^^
609     pub fn new() -> Self {
610         Self$0 {}
611     }
612 }
613 "#,
614         );
615         check(
616             r#"
617 struct Foo;
618 impl Foo {
619    //^^^
620     pub fn new() -> Self$0 {
621         Self {}
622     }
623 }
624 "#,
625         );
626
627         check(
628             r#"
629 enum Foo { A }
630 impl Foo {
631    //^^^
632     pub fn new() -> Self$0 {
633         Foo::A
634     }
635 }
636 "#,
637         );
638
639         check(
640             r#"
641 enum Foo { A }
642 impl Foo {
643    //^^^
644     pub fn thing(a: &Self$0) {
645     }
646 }
647 "#,
648         );
649     }
650
651     #[test]
652     fn goto_definition_on_self_in_trait_impl() {
653         check(
654             r#"
655 struct Foo;
656 trait Make {
657     fn new() -> Self;
658 }
659 impl Make for Foo {
660             //^^^
661     fn new() -> Self {
662         Self$0 {}
663     }
664 }
665 "#,
666         );
667
668         check(
669             r#"
670 struct Foo;
671 trait Make {
672     fn new() -> Self;
673 }
674 impl Make for Foo {
675             //^^^
676     fn new() -> Self$0 {
677         Self {}
678     }
679 }
680 "#,
681         );
682     }
683
684     #[test]
685     fn goto_def_when_used_on_definition_name_itself() {
686         check(
687             r#"
688 struct Foo$0 { value: u32 }
689      //^^^
690             "#,
691         );
692
693         check(
694             r#"
695 struct Foo {
696     field$0: string,
697 } //^^^^^
698 "#,
699         );
700
701         check(
702             r#"
703 fn foo_test$0() { }
704  //^^^^^^^^
705 "#,
706         );
707
708         check(
709             r#"
710 enum Foo$0 { Variant }
711    //^^^
712 "#,
713         );
714
715         check(
716             r#"
717 enum Foo {
718     Variant1,
719     Variant2$0,
720   //^^^^^^^^
721     Variant3,
722 }
723 "#,
724         );
725
726         check(
727             r#"
728 static INNER$0: &str = "";
729      //^^^^^
730 "#,
731         );
732
733         check(
734             r#"
735 const INNER$0: &str = "";
736     //^^^^^
737 "#,
738         );
739
740         check(
741             r#"
742 type Thing$0 = Option<()>;
743    //^^^^^
744 "#,
745         );
746
747         check(
748             r#"
749 trait Foo$0 { }
750     //^^^
751 "#,
752         );
753
754         check(
755             r#"
756 mod bar$0 { }
757   //^^^
758 "#,
759         );
760     }
761
762     #[test]
763     fn goto_from_macro() {
764         check(
765             r#"
766 macro_rules! id {
767     ($($tt:tt)*) => { $($tt)* }
768 }
769 fn foo() {}
770  //^^^
771 id! {
772     fn bar() {
773         fo$0o();
774     }
775 }
776 mod confuse_index { fn foo(); }
777 "#,
778         );
779     }
780
781     #[test]
782     fn goto_through_format() {
783         check(
784             r#"
785 #[macro_export]
786 macro_rules! format {
787     ($($arg:tt)*) => ($crate::fmt::format($crate::__export::format_args!($($arg)*)))
788 }
789 #[rustc_builtin_macro]
790 #[macro_export]
791 macro_rules! format_args {
792     ($fmt:expr) => ({ /* compiler built-in */ });
793     ($fmt:expr, $($args:tt)*) => ({ /* compiler built-in */ })
794 }
795 pub mod __export {
796     pub use crate::format_args;
797     fn foo() {} // for index confusion
798 }
799 fn foo() -> i8 {}
800  //^^^
801 fn test() {
802     format!("{}", fo$0o())
803 }
804 "#,
805         );
806     }
807
808     #[test]
809     fn goto_through_included_file() {
810         check(
811             r#"
812 //- /main.rs
813 #[rustc_builtin_macro]
814 macro_rules! include {}
815
816   include!("foo.rs");
817 //^^^^^^^^^^^^^^^^^^^
818
819 fn f() {
820     foo$0();
821 }
822
823 mod confuse_index {
824     pub fn foo() {}
825 }
826
827 //- /foo.rs
828 fn foo() {}
829         "#,
830         );
831     }
832
833     #[test]
834     fn goto_for_type_param() {
835         check(
836             r#"
837 struct Foo<T: Clone> { t: $0T }
838          //^
839 "#,
840         );
841     }
842
843     #[test]
844     fn goto_within_macro() {
845         check(
846             r#"
847 macro_rules! id {
848     ($($tt:tt)*) => ($($tt)*)
849 }
850
851 fn foo() {
852     let x = 1;
853       //^
854     id!({
855         let y = $0x;
856         let z = y;
857     });
858 }
859 "#,
860         );
861
862         check(
863             r#"
864 macro_rules! id {
865     ($($tt:tt)*) => ($($tt)*)
866 }
867
868 fn foo() {
869     let x = 1;
870     id!({
871         let y = x;
872           //^
873         let z = $0y;
874     });
875 }
876 "#,
877         );
878     }
879
880     #[test]
881     fn goto_def_in_local_fn() {
882         check(
883             r#"
884 fn main() {
885     fn foo() {
886         let x = 92;
887           //^
888         $0x;
889     }
890 }
891 "#,
892         );
893     }
894
895     #[test]
896     fn goto_def_in_local_macro() {
897         check(
898             r#"
899 fn bar() {
900     macro_rules! foo { () => { () } }
901                //^^^
902     $0foo!();
903 }
904 "#,
905         );
906     }
907
908     #[test]
909     fn goto_def_for_field_init_shorthand() {
910         check(
911             r#"
912 struct Foo { x: i32 }
913            //^
914 fn main() {
915     let x = 92;
916       //^
917     Foo { x$0 };
918 }
919 "#,
920         )
921     }
922
923     #[test]
924     fn goto_def_for_enum_variant_field() {
925         check(
926             r#"
927 enum Foo {
928     Bar { x: i32 }
929         //^
930 }
931 fn baz(foo: Foo) {
932     match foo {
933         Foo::Bar { x$0 } => x
934                  //^
935     };
936 }
937 "#,
938         );
939     }
940
941     #[test]
942     fn goto_def_for_enum_variant_self_pattern_const() {
943         check(
944             r#"
945 enum Foo { Bar }
946          //^^^
947 impl Foo {
948     fn baz(self) {
949         match self { Self::Bar$0 => {} }
950     }
951 }
952 "#,
953         );
954     }
955
956     #[test]
957     fn goto_def_for_enum_variant_self_pattern_record() {
958         check(
959             r#"
960 enum Foo { Bar { val: i32 } }
961          //^^^
962 impl Foo {
963     fn baz(self) -> i32 {
964         match self { Self::Bar$0 { val } => {} }
965     }
966 }
967 "#,
968         );
969     }
970
971     #[test]
972     fn goto_def_for_enum_variant_self_expr_const() {
973         check(
974             r#"
975 enum Foo { Bar }
976          //^^^
977 impl Foo {
978     fn baz(self) { Self::Bar$0; }
979 }
980 "#,
981         );
982     }
983
984     #[test]
985     fn goto_def_for_enum_variant_self_expr_record() {
986         check(
987             r#"
988 enum Foo { Bar { val: i32 } }
989          //^^^
990 impl Foo {
991     fn baz(self) { Self::Bar$0 {val: 4}; }
992 }
993 "#,
994         );
995     }
996
997     #[test]
998     fn goto_def_for_type_alias_generic_parameter() {
999         check(
1000             r#"
1001 type Alias<T> = T$0;
1002          //^
1003 "#,
1004         )
1005     }
1006
1007     #[test]
1008     fn goto_def_for_macro_container() {
1009         check(
1010             r#"
1011 //- /lib.rs crate:main deps:foo
1012 foo::module$0::mac!();
1013
1014 //- /foo/lib.rs crate:foo
1015 pub mod module {
1016       //^^^^^^
1017     #[macro_export]
1018     macro_rules! _mac { () => { () } }
1019     pub use crate::_mac as mac;
1020 }
1021 "#,
1022         );
1023     }
1024
1025     #[test]
1026     fn goto_def_for_assoc_ty_in_path() {
1027         check(
1028             r#"
1029 trait Iterator {
1030     type Item;
1031        //^^^^
1032 }
1033
1034 fn f() -> impl Iterator<Item$0 = u8> {}
1035 "#,
1036         );
1037     }
1038
1039     #[test]
1040     fn goto_def_for_super_assoc_ty_in_path() {
1041         check(
1042             r#"
1043 trait Super {
1044     type Item;
1045        //^^^^
1046 }
1047
1048 trait Sub: Super {}
1049
1050 fn f() -> impl Sub<Item$0 = u8> {}
1051 "#,
1052         );
1053     }
1054
1055     #[test]
1056     fn unknown_assoc_ty() {
1057         check_unresolved(
1058             r#"
1059 trait Iterator { type Item; }
1060 fn f() -> impl Iterator<Invalid$0 = u8> {}
1061 "#,
1062         )
1063     }
1064
1065     #[test]
1066     fn goto_def_for_assoc_ty_in_path_multiple() {
1067         check(
1068             r#"
1069 trait Iterator {
1070     type A;
1071        //^
1072     type B;
1073 }
1074
1075 fn f() -> impl Iterator<A$0 = u8, B = ()> {}
1076 "#,
1077         );
1078         check(
1079             r#"
1080 trait Iterator {
1081     type A;
1082     type B;
1083        //^
1084 }
1085
1086 fn f() -> impl Iterator<A = u8, B$0 = ()> {}
1087 "#,
1088         );
1089     }
1090
1091     #[test]
1092     fn goto_def_for_assoc_ty_ufcs() {
1093         check(
1094             r#"
1095 trait Iterator {
1096     type Item;
1097        //^^^^
1098 }
1099
1100 fn g() -> <() as Iterator<Item$0 = ()>>::Item {}
1101 "#,
1102         );
1103     }
1104
1105     #[test]
1106     fn goto_def_for_assoc_ty_ufcs_multiple() {
1107         check(
1108             r#"
1109 trait Iterator {
1110     type A;
1111        //^
1112     type B;
1113 }
1114
1115 fn g() -> <() as Iterator<A$0 = (), B = u8>>::B {}
1116 "#,
1117         );
1118         check(
1119             r#"
1120 trait Iterator {
1121     type A;
1122     type B;
1123        //^
1124 }
1125
1126 fn g() -> <() as Iterator<A = (), B$0 = u8>>::A {}
1127 "#,
1128         );
1129     }
1130
1131     #[test]
1132     fn goto_self_param_ty_specified() {
1133         check(
1134             r#"
1135 struct Foo {}
1136
1137 impl Foo {
1138     fn bar(self: &Foo) {
1139          //^^^^
1140         let foo = sel$0f;
1141     }
1142 }"#,
1143         )
1144     }
1145
1146     #[test]
1147     fn goto_self_param_on_decl() {
1148         check(
1149             r#"
1150 struct Foo {}
1151
1152 impl Foo {
1153     fn bar(&self$0) {
1154           //^^^^
1155     }
1156 }"#,
1157         )
1158     }
1159
1160     #[test]
1161     fn goto_lifetime_param_on_decl() {
1162         check(
1163             r#"
1164 fn foo<'foobar$0>(_: &'foobar ()) {
1165      //^^^^^^^
1166 }"#,
1167         )
1168     }
1169
1170     #[test]
1171     fn goto_lifetime_param_decl() {
1172         check(
1173             r#"
1174 fn foo<'foobar>(_: &'foobar$0 ()) {
1175      //^^^^^^^
1176 }"#,
1177         )
1178     }
1179
1180     #[test]
1181     fn goto_lifetime_param_decl_nested() {
1182         check(
1183             r#"
1184 fn foo<'foobar>(_: &'foobar ()) {
1185     fn foo<'foobar>(_: &'foobar$0 ()) {}
1186          //^^^^^^^
1187 }"#,
1188         )
1189     }
1190
1191     #[test]
1192     fn goto_lifetime_hrtb() {
1193         // FIXME: requires the HIR to somehow track these hrtb lifetimes
1194         check_unresolved(
1195             r#"
1196 trait Foo<T> {}
1197 fn foo<T>() where for<'a> T: Foo<&'a$0 (u8, u16)>, {}
1198                     //^^
1199 "#,
1200         );
1201         check_unresolved(
1202             r#"
1203 trait Foo<T> {}
1204 fn foo<T>() where for<'a$0> T: Foo<&'a (u8, u16)>, {}
1205                     //^^
1206 "#,
1207         );
1208     }
1209
1210     #[test]
1211     fn goto_lifetime_hrtb_for_type() {
1212         // FIXME: requires ForTypes to be implemented
1213         check_unresolved(
1214             r#"trait Foo<T> {}
1215 fn foo<T>() where T: for<'a> Foo<&'a$0 (u8, u16)>, {}
1216                        //^^
1217 "#,
1218         );
1219     }
1220
1221     #[test]
1222     fn goto_label() {
1223         check(
1224             r#"
1225 fn foo<'foo>(_: &'foo ()) {
1226     'foo: {
1227   //^^^^
1228         'bar: loop {
1229             break 'foo$0;
1230         }
1231     }
1232 }"#,
1233         )
1234     }
1235
1236     #[test]
1237     fn goto_def_for_intra_doc_link_same_file() {
1238         check(
1239             r#"
1240 /// Blah, [`bar`](bar) .. [`foo`](foo$0) has [`bar`](bar)
1241 pub fn bar() { }
1242
1243 /// You might want to see [`std::fs::read()`] too.
1244 pub fn foo() { }
1245      //^^^
1246
1247 }"#,
1248         )
1249     }
1250
1251     #[test]
1252     fn goto_def_for_intra_doc_link_inner() {
1253         check(
1254             r#"
1255 //- /main.rs
1256 mod m;
1257 struct S;
1258      //^
1259
1260 //- /m.rs
1261 //! [`super::S$0`]
1262 "#,
1263         )
1264     }
1265
1266     #[test]
1267     fn goto_incomplete_field() {
1268         check(
1269             r#"
1270 struct A { a: u32 }
1271          //^
1272 fn foo() { A { a$0: }; }
1273 "#,
1274         )
1275     }
1276
1277     #[test]
1278     fn goto_proc_macro() {
1279         check(
1280             r#"
1281 //- /main.rs crate:main deps:mac
1282 use mac::fn_macro;
1283
1284 fn_macro$0!();
1285
1286 //- /mac.rs crate:mac
1287 #![crate_type="proc-macro"]
1288 #[proc_macro]
1289 fn fn_macro() {}
1290  //^^^^^^^^
1291             "#,
1292         )
1293     }
1294
1295     #[test]
1296     fn goto_intra_doc_links() {
1297         check(
1298             r#"
1299
1300 pub mod theitem {
1301     /// This is the item. Cool!
1302     pub struct TheItem;
1303              //^^^^^^^
1304 }
1305
1306 /// Gives you a [`TheItem$0`].
1307 ///
1308 /// [`TheItem`]: theitem::TheItem
1309 pub fn gimme() -> theitem::TheItem {
1310     theitem::TheItem
1311 }
1312 "#,
1313         );
1314     }
1315
1316     #[test]
1317     fn goto_ident_from_pat_macro() {
1318         check(
1319             r#"
1320 macro_rules! pat {
1321     ($name:ident) => { Enum::Variant1($name) }
1322 }
1323
1324 enum Enum {
1325     Variant1(u8),
1326     Variant2,
1327 }
1328
1329 fn f(e: Enum) {
1330     match e {
1331         pat!(bind) => {
1332            //^^^^
1333             bind$0
1334         }
1335         Enum::Variant2 => {}
1336     }
1337 }
1338 "#,
1339         );
1340     }
1341
1342     #[test]
1343     fn goto_include() {
1344         check(
1345             r#"
1346 //- /main.rs
1347 fn main() {
1348     let str = include_str!("foo.txt$0");
1349 }
1350 //- /foo.txt
1351 // empty
1352 //^file
1353 "#,
1354         );
1355     }
1356     #[cfg(test)]
1357     mod goto_impl_of_trait_fn {
1358         use super::check;
1359         #[test]
1360         fn cursor_on_impl() {
1361             check(
1362                 r#"
1363 trait Twait {
1364     fn a();
1365 }
1366
1367 struct Stwuct;
1368
1369 impl Twait for Stwuct {
1370     fn a$0();
1371      //^
1372 }
1373         "#,
1374             );
1375         }
1376         #[test]
1377         fn method_call() {
1378             check(
1379                 r#"
1380 trait Twait {
1381     fn a(&self);
1382 }
1383
1384 struct Stwuct;
1385
1386 impl Twait for Stwuct {
1387     fn a(&self){};
1388      //^
1389 }
1390 fn f() {
1391     let s = Stwuct;
1392     s.a$0();
1393 }
1394         "#,
1395             );
1396         }
1397         #[test]
1398         fn path_call() {
1399             check(
1400                 r#"
1401 trait Twait {
1402     fn a(&self);
1403 }
1404
1405 struct Stwuct;
1406
1407 impl Twait for Stwuct {
1408     fn a(&self){};
1409      //^
1410 }
1411 fn f() {
1412     let s = Stwuct;
1413     Stwuct::a$0(&s);
1414 }
1415         "#,
1416             );
1417         }
1418         #[test]
1419         fn where_clause_can_work() {
1420             check(
1421                 r#"
1422 trait G {
1423     fn g(&self);
1424 }
1425 trait Bound{}
1426 trait EA{}
1427 struct Gen<T>(T);
1428 impl <T:EA> G for Gen<T> {
1429     fn g(&self) {
1430     }
1431 }
1432 impl <T> G for Gen<T>
1433 where T : Bound
1434 {
1435     fn g(&self){
1436      //^
1437     }
1438 }
1439 struct A;
1440 impl Bound for A{}
1441 fn f() {
1442     let gen = Gen::<A>(A);
1443     gen.g$0();
1444 }
1445                 "#,
1446             );
1447         }
1448         #[test]
1449         fn wc_case_is_ok() {
1450             check(
1451                 r#"
1452 trait G {
1453     fn g(&self);
1454 }
1455 trait BParent{}
1456 trait Bound: BParent{}
1457 struct Gen<T>(T);
1458 impl <T> G for Gen<T>
1459 where T : Bound
1460 {
1461     fn g(&self){
1462      //^
1463     }
1464 }
1465 struct A;
1466 impl Bound for A{}
1467 fn f() {
1468     let gen = Gen::<A>(A);
1469     gen.g$0();
1470 }
1471 "#,
1472             );
1473         }
1474
1475         #[test]
1476         fn method_call_defaulted() {
1477             check(
1478                 r#"
1479 trait Twait {
1480     fn a(&self) {}
1481      //^
1482 }
1483
1484 struct Stwuct;
1485
1486 impl Twait for Stwuct {
1487 }
1488 fn f() {
1489     let s = Stwuct;
1490     s.a$0();
1491 }
1492         "#,
1493             );
1494         }
1495
1496         #[test]
1497         fn method_call_on_generic() {
1498             check(
1499                 r#"
1500 trait Twait {
1501     fn a(&self) {}
1502      //^
1503 }
1504
1505 fn f<T: Twait>(s: T) {
1506     s.a$0();
1507 }
1508         "#,
1509             );
1510         }
1511     }
1512
1513     #[test]
1514     fn goto_def_of_trait_impl_const() {
1515         check(
1516             r#"
1517 trait Twait {
1518     const NOMS: bool;
1519        // ^^^^
1520 }
1521
1522 struct Stwuct;
1523
1524 impl Twait for Stwuct {
1525     const NOMS$0: bool = true;
1526 }
1527 "#,
1528         );
1529     }
1530
1531     #[test]
1532     fn goto_def_of_trait_impl_type_alias() {
1533         check(
1534             r#"
1535 trait Twait {
1536     type IsBad;
1537       // ^^^^^
1538 }
1539
1540 struct Stwuct;
1541
1542 impl Twait for Stwuct {
1543     type IsBad$0 = !;
1544 }
1545 "#,
1546         );
1547     }
1548
1549     #[test]
1550     fn goto_def_derive_input() {
1551         check(
1552             r#"
1553         //- minicore:derive
1554         #[rustc_builtin_macro]
1555         pub macro Copy {}
1556                // ^^^^
1557         #[derive(Copy$0)]
1558         struct Foo;
1559                     "#,
1560         );
1561         check(
1562             r#"
1563 //- minicore:derive
1564 #[rustc_builtin_macro]
1565 pub macro Copy {}
1566        // ^^^^
1567 #[cfg_attr(feature = "false", derive)]
1568 #[derive(Copy$0)]
1569 struct Foo;
1570             "#,
1571         );
1572         check(
1573             r#"
1574 //- minicore:derive
1575 mod foo {
1576     #[rustc_builtin_macro]
1577     pub macro Copy {}
1578            // ^^^^
1579 }
1580 #[derive(foo::Copy$0)]
1581 struct Foo;
1582             "#,
1583         );
1584         check(
1585             r#"
1586 //- minicore:derive
1587 mod foo {
1588  // ^^^
1589     #[rustc_builtin_macro]
1590     pub macro Copy {}
1591 }
1592 #[derive(foo$0::Copy)]
1593 struct Foo;
1594             "#,
1595         );
1596     }
1597
1598     #[test]
1599     fn goto_def_in_macro_multi() {
1600         check(
1601             r#"
1602 struct Foo {
1603     foo: ()
1604   //^^^
1605 }
1606 macro_rules! foo {
1607     ($ident:ident) => {
1608         fn $ident(Foo { $ident }: Foo) {}
1609     }
1610 }
1611 foo!(foo$0);
1612    //^^^
1613    //^^^
1614 "#,
1615         );
1616         check(
1617             r#"
1618 fn bar() {}
1619  //^^^
1620 struct bar;
1621      //^^^
1622 macro_rules! foo {
1623     ($ident:ident) => {
1624         fn foo() {
1625             let _: $ident = $ident;
1626         }
1627     }
1628 }
1629
1630 foo!(bar$0);
1631 "#,
1632         );
1633     }
1634 }