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