@@ -2,7 +2,7 @@ use std::path::{Path, PathBuf};
2
2
3
3
use rustc_data_structures:: fx:: { FxHashMap , FxIndexMap } ;
4
4
use rustc_hir:: def:: { DefKind , Res } ;
5
- use rustc_hir:: def_id:: { DefId , LOCAL_CRATE } ;
5
+ use rustc_hir:: def_id:: { DefId , LOCAL_CRATE , LocalDefId } ;
6
6
use rustc_hir:: intravisit:: { self , Visitor , VisitorExt } ;
7
7
use rustc_hir:: { ExprKind , HirId , Item , ItemKind , Mod , Node , QPath } ;
8
8
use rustc_middle:: hir:: nested_filter;
@@ -201,6 +201,17 @@ impl SpanMapVisitor<'_> {
201
201
}
202
202
}
203
203
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
+
204
215
impl < ' tcx > Visitor < ' tcx > for SpanMapVisitor < ' tcx > {
205
216
type NestedFilter = nested_filter:: All ;
206
217
@@ -221,15 +232,16 @@ impl<'tcx> Visitor<'tcx> for SpanMapVisitor<'tcx> {
221
232
QPath :: TypeRelative ( qself, path) => {
222
233
if matches ! ( path. res, Res :: Err ) {
223
234
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
+ }
233
245
} else {
234
246
self . infer_id ( path. hir_id , Some ( id) , path. ident . span ) ;
235
247
}
0 commit comments