@@ -2,7 +2,7 @@ use std::path::{Path, PathBuf};
22
33use rustc_data_structures:: fx:: { FxHashMap , FxIndexMap } ;
44use rustc_hir:: def:: { DefKind , Res } ;
5- use rustc_hir:: def_id:: { DefId , LOCAL_CRATE } ;
5+ use rustc_hir:: def_id:: { DefId , LOCAL_CRATE , LocalDefId } ;
66use rustc_hir:: intravisit:: { self , Visitor , VisitorExt } ;
77use rustc_hir:: { ExprKind , HirId , Item , ItemKind , Mod , Node , QPath } ;
88use rustc_middle:: hir:: nested_filter;
@@ -201,6 +201,17 @@ impl SpanMapVisitor<'_> {
201201 }
202202}
203203
204+ // This is a reimplementation of `hir_enclosing_body_owner` which allows to fail without
205+ // panicking.
206+ fn hir_enclosing_body_owner ( tcx : TyCtxt < ' _ > , hir_id : HirId ) -> Option < LocalDefId > {
207+ for ( _, node) in tcx. hir_parent_iter ( hir_id) {
208+ if let Some ( ( def_id, _) ) = node. associated_body ( ) {
209+ return Some ( def_id) ;
210+ }
211+ }
212+ None
213+ }
214+
204215impl < ' tcx > Visitor < ' tcx > for SpanMapVisitor < ' tcx > {
205216 type NestedFilter = nested_filter:: All ;
206217
@@ -221,15 +232,16 @@ impl<'tcx> Visitor<'tcx> for SpanMapVisitor<'tcx> {
221232 QPath :: TypeRelative ( qself, path) => {
222233 if matches ! ( path. res, Res :: Err ) {
223234 let tcx = self . tcx ;
224- let body_id = tcx. hir_enclosing_body_owner ( id) ;
225- let typeck_results = tcx. typeck_body ( tcx. hir_body_owned_by ( body_id) . id ( ) ) ;
226- let path = rustc_hir:: Path {
227- // We change the span to not include parens.
228- span : path. ident . span ,
229- res : typeck_results. qpath_res ( qpath, id) ,
230- segments : & [ ] ,
231- } ;
232- self . handle_path ( & path, false ) ;
235+ if let Some ( body_id) = hir_enclosing_body_owner ( tcx, id) {
236+ let typeck_results = tcx. typeck_body ( tcx. hir_body_owned_by ( body_id) . id ( ) ) ;
237+ let path = rustc_hir:: Path {
238+ // We change the span to not include parens.
239+ span : path. ident . span ,
240+ res : typeck_results. qpath_res ( qpath, id) ,
241+ segments : & [ ] ,
242+ } ;
243+ self . handle_path ( & path, false ) ;
244+ }
233245 } else {
234246 self . infer_id ( path. hir_id , Some ( id) , path. ident . span ) ;
235247 }
0 commit comments