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