@@ -124,18 +124,21 @@ impl<'tcx> SpanMapVisitor<'tcx> {
124124 Some ( * results)
125125 }
126126
127+ fn link_for_def ( & self , def_id : DefId ) -> LinkFromSrc {
128+ if def_id. is_local ( ) {
129+ LinkFromSrc :: Local ( rustc_span ( def_id, self . tcx ) )
130+ } else {
131+ LinkFromSrc :: External ( def_id)
132+ }
133+ }
134+
127135 /// This function is where we handle `hir::Path` elements and add them into the "span map".
128136 fn handle_path ( & mut self , path : & hir:: Path < ' _ > , only_use_last_segment : bool ) {
129137 match path. res {
130138 // FIXME: For now, we handle `DefKind` if it's not a `DefKind::TyParam`.
131139 // Would be nice to support them too alongside the other `DefKind`
132140 // (such as primitive types!).
133141 Res :: Def ( kind, def_id) if kind != DefKind :: TyParam => {
134- let link = if def_id. as_local ( ) . is_some ( ) {
135- LinkFromSrc :: Local ( rustc_span ( def_id, self . tcx ) )
136- } else {
137- LinkFromSrc :: External ( def_id)
138- } ;
139142 // In case the path ends with generics, we remove them from the span.
140143 let span = if only_use_last_segment
141144 && let Some ( path_span) = path. segments . last ( ) . map ( |segment| segment. ident . span )
@@ -156,7 +159,7 @@ impl<'tcx> SpanMapVisitor<'tcx> {
156159 } )
157160 . unwrap_or ( path. span )
158161 } ;
159- self . matches . insert ( span. into ( ) , link ) ;
162+ self . matches . insert ( span. into ( ) , self . link_for_def ( def_id ) ) ;
160163 }
161164 Res :: Local ( _) if let Some ( span) = self . tcx . hir_res_span ( path. res ) => {
162165 let path_span = if only_use_last_segment
@@ -240,21 +243,6 @@ impl<'tcx> SpanMapVisitor<'tcx> {
240243 self . matches . insert ( new_span. into ( ) , link_from_src) ;
241244 true
242245 }
243-
244- fn infer_id ( & mut self , hir_id : HirId , expr_hir_id : Option < HirId > , span : Span ) {
245- let Some ( typeck_results) = self . maybe_typeck_results ( ) else { return } ;
246-
247- // Interestingly enough, for method calls, we need the whole expression whereas for static
248- // method/function calls, we need the call expression specifically.
249- if let Some ( def_id) = typeck_results. type_dependent_def_id ( expr_hir_id. unwrap_or ( hir_id) ) {
250- let link = if def_id. as_local ( ) . is_some ( ) {
251- LinkFromSrc :: Local ( rustc_span ( def_id, self . tcx ) )
252- } else {
253- LinkFromSrc :: External ( def_id)
254- } ;
255- self . matches . insert ( span, link) ;
256- }
257- }
258246}
259247
260248impl < ' tcx > Visitor < ' tcx > for SpanMapVisitor < ' tcx > {
@@ -341,23 +329,26 @@ impl<'tcx> Visitor<'tcx> for SpanMapVisitor<'tcx> {
341329 }
342330
343331 fn visit_expr ( & mut self , expr : & ' tcx hir:: Expr < ' tcx > ) {
344- match expr. kind {
345- ExprKind :: MethodCall ( segment, ..) => {
346- self . infer_id ( segment. hir_id , Some ( expr. hir_id ) , segment. ident . span . into ( ) )
332+ let mut handle_type_dependent_def = |hir_id : HirId , span : rustc_span:: Span | {
333+ // Exprs *have* to exist in a body, so typeck results should always be available.
334+ let typeck_results = self . maybe_typeck_results ( ) . unwrap ( ) ;
335+ if let Some ( def_id) = typeck_results. type_dependent_def_id ( hir_id) {
336+ self . matches . insert ( span. into ( ) , self . link_for_def ( def_id) ) ;
347337 }
338+ } ;
339+
340+ match expr. kind {
341+ ExprKind :: MethodCall ( seg, ..) => handle_type_dependent_def ( expr. hir_id , seg. ident . span ) ,
348342 // FIXME(fmease): We needlessly request `TypeckResults` even if the callee isn't
349343 // type-relative. In the majority of cases, it's just gonna be a
350344 // `Resolved` path meaning we can end up unnecessarily
351345 // `typeck`'ing the body which is super costly!
352346 // Moreover, if it actually is a type-relative path, we end up
353347 // "resolving" it twice (with slightly different spans).
354- ExprKind :: Call ( call, ..) => self . infer_id ( call. hir_id , None , call. span . into ( ) ) ,
355- _ => {
356- if self . handle_macro ( expr. span ) {
357- // We don't want to go deeper into the macro.
358- return ;
359- }
360- }
348+ ExprKind :: Call ( callee, ..) => handle_type_dependent_def ( callee. hir_id , callee. span ) ,
349+ // We don't want to go deeper into the macro.
350+ _ if self . handle_macro ( expr. span ) => return ,
351+ _ => { }
361352 }
362353 intravisit:: walk_expr ( self , expr) ;
363354 }
0 commit comments