@@ -6,9 +6,8 @@ use rustc_data_structures::fx::FxHashMap;
66use rustc_hir:: def:: { DefKind , Res } ;
77use rustc_hir:: def_id:: { DefId , LOCAL_CRATE } ;
88use rustc_hir:: intravisit:: { self , Visitor } ;
9- use rustc_hir:: { ExprKind , HirId , Mod , Node } ;
9+ use rustc_hir:: { ExprKind , HirId , Item , ItemKind , Mod , Node } ;
1010use rustc_middle:: hir:: nested_filter;
11- use rustc_hir:: { ExprKind , GenericParam , GenericParamKind , HirId , Item , ItemKind , Mod , Node } ;
1211use rustc_middle:: ty:: TyCtxt ;
1312use rustc_span:: Span ;
1413
@@ -24,7 +23,7 @@ use std::path::{Path, PathBuf};
2423#[ derive( Debug ) ]
2524pub ( crate ) enum LinkFromSrc {
2625 Local ( clean:: Span ) ,
27- External ( DefId ) ,
26+ External ( clean :: Span ) ,
2827 Primitive ( PrimitiveType ) ,
2928 Doc ( DefId ) ,
3029}
@@ -67,36 +66,42 @@ struct SpanMapVisitor<'tcx> {
6766impl < ' tcx > SpanMapVisitor < ' tcx > {
6867 /// This function is where we handle `hir::Path` elements and add them into the "span map".
6968 fn handle_path ( & mut self , path : & rustc_hir:: Path < ' _ > , path_span : Option < Span > ) {
70- let info = match path. res {
69+ match path. res {
7170 // FIXME: For now, we only handle `DefKind` if it's not `DefKind::TyParam` or
7271 // `DefKind::Macro`. Would be nice to support them too alongside the other `DefKind`
7372 // (such as primitive types!).
7473 Res :: Def ( kind, def_id) if kind != DefKind :: TyParam => {
7574 if matches ! ( kind, DefKind :: Macro ( _) ) {
7675 return ;
7776 }
78- Some ( def_id)
77+ let span = rustc_span ( def_id, self . tcx ) ;
78+ let link = if def_id. as_local ( ) . is_some ( ) {
79+ LinkFromSrc :: Local ( span)
80+ } else {
81+ LinkFromSrc :: External ( span)
82+ } ;
83+ self . matches . insert ( path_span. unwrap_or ( path. span ) , link) ;
84+ }
85+ Res :: Local ( _) => {
86+ if let Some ( span) = self . tcx . hir ( ) . res_span ( path. res ) {
87+ self . matches . insert (
88+ path_span. unwrap_or ( path. span ) ,
89+ LinkFromSrc :: Local ( clean:: Span :: new ( span) ) ,
90+ ) ;
91+ }
7992 }
80- Res :: Local ( _) => None ,
8193 Res :: PrimTy ( p) => {
8294 // FIXME: Doesn't handle "path-like" primitives like arrays or tuples.
8395 let span = path_span. unwrap_or ( path. span ) ;
8496 self . matches . insert ( span, LinkFromSrc :: Primitive ( PrimitiveType :: from ( p) ) ) ;
85- return ;
8697 }
87- Res :: Err => return ,
88- _ => return ,
89- } ;
90- if let Some ( span) = self . tcx . hir ( ) . res_span ( path. res ) {
91- self . matches
92- . insert ( path_span. unwrap_or ( path. span ) , LinkFromSrc :: Local ( clean:: Span :: new ( span) ) ) ;
93- } else if let Some ( def_id) = info {
94- self . matches . insert ( path_span. unwrap_or ( path. span ) , LinkFromSrc :: External ( def_id) ) ;
98+ Res :: Err => { }
99+ _ => { }
95100 }
96101 }
97102
98103 /// Used to generate links on items' definition to go to their documentation page.
99- crate fn extract_info_from_hir_id ( & mut self , hir_id : HirId ) {
104+ pub ( crate ) fn extract_info_from_hir_id ( & mut self , hir_id : HirId ) {
100105 if let Some ( def_id) = self . tcx . hir ( ) . opt_local_def_id ( hir_id) {
101106 if let Some ( span) = self . tcx . def_ident_span ( def_id) {
102107 let cspan = clean:: Span :: new ( span) ;
@@ -155,13 +160,13 @@ impl<'tcx> Visitor<'tcx> for SpanMapVisitor<'tcx> {
155160 hir. maybe_body_owned_by ( body_id) . expect ( "a body which isn't a body" ) ,
156161 ) ;
157162 if let Some ( def_id) = typeck_results. type_dependent_def_id ( expr. hir_id ) {
158- self . matches . insert (
159- segment . ident . span ,
160- match hir . span_if_local ( def_id ) {
161- Some ( span ) => LinkFromSrc :: Local ( clean :: Span :: new ( span ) ) ,
162- None => LinkFromSrc :: External ( def_id ) ,
163- } ,
164- ) ;
163+ let span = rustc_span ( def_id , self . tcx ) ;
164+ let link = if def_id . as_local ( ) . is_some ( ) {
165+ LinkFromSrc :: Local ( span )
166+ } else {
167+ LinkFromSrc :: External ( span )
168+ } ;
169+ self . matches . insert ( segment . ident . span , link ) ;
165170 }
166171 }
167172 }
@@ -178,7 +183,7 @@ impl<'tcx> Visitor<'tcx> for SpanMapVisitor<'tcx> {
178183 ItemKind :: Static ( _, _, _)
179184 | ItemKind :: Const ( _, _)
180185 | ItemKind :: Fn ( _, _, _)
181- | ItemKind :: Macro ( _)
186+ | ItemKind :: Macro ( _, _ )
182187 | ItemKind :: TyAlias ( _, _)
183188 | ItemKind :: Enum ( _, _)
184189 | ItemKind :: Struct ( _, _)
0 commit comments