@@ -346,35 +346,37 @@ fn extract_hole_spans_from_hir<'tcx>(
346346 body_span : Span , // Usually `hir_body.value.span`, but not always
347347 hir_body : & hir:: Body < ' tcx > ,
348348) -> Vec < Span > {
349- struct HolesVisitor < ' hir , F > {
350- tcx : TyCtxt < ' hir > ,
351- visit_hole_span : F ,
349+ struct HolesVisitor < ' tcx > {
350+ tcx : TyCtxt < ' tcx > ,
351+ body_span : Span ,
352+ hole_spans : Vec < Span > ,
352353 }
353354
354- impl < ' hir , F : FnMut ( Span ) > Visitor < ' hir > for HolesVisitor < ' hir , F > {
355- /// - We need `NestedFilter::INTRA = true` so that `visit_item` will be called.
356- /// - Bodies of nested items don't actually get visited, because of the
357- /// `visit_item` override.
358- /// - For nested bodies that are not part of an item, we do want to visit any
359- /// items contained within them.
360- type NestedFilter = nested_filter:: All ;
355+ impl < ' tcx > Visitor < ' tcx > for HolesVisitor < ' tcx > {
356+ /// We have special handling for nested items, but we still want to
357+ /// traverse into nested bodies of things that are not considered items,
358+ /// such as "anon consts" (e.g. array lengths).
359+ type NestedFilter = nested_filter:: OnlyBodies ;
361360
362- fn maybe_tcx ( & mut self ) -> Self :: MaybeTyCtxt {
361+ fn maybe_tcx ( & mut self ) -> TyCtxt < ' tcx > {
363362 self . tcx
364363 }
365364
366- fn visit_item ( & mut self , item : & ' hir hir:: Item < ' hir > ) {
367- ( self . visit_hole_span ) ( item. span ) ;
365+ /// We override `visit_nested_item` instead of `visit_item` because we
366+ /// only need the item's span, not the item itself.
367+ fn visit_nested_item ( & mut self , id : hir:: ItemId ) -> Self :: Result {
368+ let span = self . tcx . def_span ( id. owner_id . def_id ) ;
369+ self . visit_hole_span ( span) ;
368370 // Having visited this item, we don't care about its children,
369371 // so don't call `walk_item`.
370372 }
371373
372374 // We override `visit_expr` instead of the more specific expression
373375 // visitors, so that we have direct access to the expression span.
374- fn visit_expr ( & mut self , expr : & ' hir hir:: Expr < ' hir > ) {
376+ fn visit_expr ( & mut self , expr : & ' tcx hir:: Expr < ' tcx > ) {
375377 match expr. kind {
376378 hir:: ExprKind :: Closure ( _) | hir:: ExprKind :: ConstBlock ( _) => {
377- ( self . visit_hole_span ) ( expr. span ) ;
379+ self . visit_hole_span ( expr. span ) ;
378380 // Having visited this expression, we don't care about its
379381 // children, so don't call `walk_expr`.
380382 }
@@ -384,18 +386,17 @@ fn extract_hole_spans_from_hir<'tcx>(
384386 }
385387 }
386388 }
387-
388- let mut hole_spans = vec ! [ ] ;
389- let mut visitor = HolesVisitor {
390- tcx,
391- visit_hole_span : |hole_span| {
389+ impl HolesVisitor < ' _ > {
390+ fn visit_hole_span ( & mut self , hole_span : Span ) {
392391 // Discard any holes that aren't directly visible within the body span.
393- if body_span. contains ( hole_span) && body_span. eq_ctxt ( hole_span) {
394- hole_spans. push ( hole_span) ;
392+ if self . body_span . contains ( hole_span) && self . body_span . eq_ctxt ( hole_span) {
393+ self . hole_spans . push ( hole_span) ;
395394 }
396- } ,
397- } ;
395+ }
396+ }
397+
398+ let mut visitor = HolesVisitor { tcx, body_span, hole_spans : vec ! [ ] } ;
398399
399400 visitor. visit_body ( hir_body) ;
400- hole_spans
401+ visitor . hole_spans
401402}
0 commit comments