@@ -6,9 +6,8 @@ use rustc_data_structures::fx::FxHashMap;
6
6
use rustc_hir:: def:: { DefKind , Res } ;
7
7
use rustc_hir:: def_id:: { DefId , LOCAL_CRATE } ;
8
8
use rustc_hir:: intravisit:: { self , Visitor } ;
9
- use rustc_hir:: { ExprKind , HirId , Mod , Node } ;
9
+ use rustc_hir:: { ExprKind , HirId , Item , ItemKind , Mod , Node } ;
10
10
use rustc_middle:: hir:: nested_filter;
11
- use rustc_hir:: { ExprKind , GenericParam , GenericParamKind , HirId , Item , ItemKind , Mod , Node } ;
12
11
use rustc_middle:: ty:: TyCtxt ;
13
12
use rustc_span:: Span ;
14
13
@@ -24,7 +23,7 @@ use std::path::{Path, PathBuf};
24
23
#[ derive( Debug ) ]
25
24
pub ( crate ) enum LinkFromSrc {
26
25
Local ( clean:: Span ) ,
27
- External ( DefId ) ,
26
+ External ( clean :: Span ) ,
28
27
Primitive ( PrimitiveType ) ,
29
28
Doc ( DefId ) ,
30
29
}
@@ -67,36 +66,42 @@ struct SpanMapVisitor<'tcx> {
67
66
impl < ' tcx > SpanMapVisitor < ' tcx > {
68
67
/// This function is where we handle `hir::Path` elements and add them into the "span map".
69
68
fn handle_path ( & mut self , path : & rustc_hir:: Path < ' _ > , path_span : Option < Span > ) {
70
- let info = match path. res {
69
+ match path. res {
71
70
// FIXME: For now, we only handle `DefKind` if it's not `DefKind::TyParam` or
72
71
// `DefKind::Macro`. Would be nice to support them too alongside the other `DefKind`
73
72
// (such as primitive types!).
74
73
Res :: Def ( kind, def_id) if kind != DefKind :: TyParam => {
75
74
if matches ! ( kind, DefKind :: Macro ( _) ) {
76
75
return ;
77
76
}
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
+ }
79
92
}
80
- Res :: Local ( _) => None ,
81
93
Res :: PrimTy ( p) => {
82
94
// FIXME: Doesn't handle "path-like" primitives like arrays or tuples.
83
95
let span = path_span. unwrap_or ( path. span ) ;
84
96
self . matches . insert ( span, LinkFromSrc :: Primitive ( PrimitiveType :: from ( p) ) ) ;
85
- return ;
86
97
}
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
+ _ => { }
95
100
}
96
101
}
97
102
98
103
/// 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 ) {
100
105
if let Some ( def_id) = self . tcx . hir ( ) . opt_local_def_id ( hir_id) {
101
106
if let Some ( span) = self . tcx . def_ident_span ( def_id) {
102
107
let cspan = clean:: Span :: new ( span) ;
@@ -155,13 +160,13 @@ impl<'tcx> Visitor<'tcx> for SpanMapVisitor<'tcx> {
155
160
hir. maybe_body_owned_by ( body_id) . expect ( "a body which isn't a body" ) ,
156
161
) ;
157
162
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 ) ;
165
170
}
166
171
}
167
172
}
@@ -178,7 +183,7 @@ impl<'tcx> Visitor<'tcx> for SpanMapVisitor<'tcx> {
178
183
ItemKind :: Static ( _, _, _)
179
184
| ItemKind :: Const ( _, _)
180
185
| ItemKind :: Fn ( _, _, _)
181
- | ItemKind :: Macro ( _)
186
+ | ItemKind :: Macro ( _, _ )
182
187
| ItemKind :: TyAlias ( _, _)
183
188
| ItemKind :: Enum ( _, _)
184
189
| ItemKind :: Struct ( _, _)
0 commit comments