@@ -97,26 +97,26 @@ where
9797 root_items. chain ( impl_items) . collect ( )
9898}
9999
100- /// Use a predicate to find `const` declarations, then extract all closures from those declarations
100+ /// Use a predicate to find `const` declarations, then extract all items reachable from them.
101101///
102102/// Probably only specifically useful with a predicate to find `TestDescAndFn` const declarations from
103103/// tests and extract the closures from them.
104- pub fn filter_closures_in_const_crate_items < F > ( tcx : TyCtxt , mut predicate : F ) -> Vec < MonoItem >
104+ pub fn filter_const_crate_items < F > ( tcx : TyCtxt , mut predicate : F ) -> Vec < MonoItem >
105105where
106106 F : FnMut ( TyCtxt , DefId ) -> bool ,
107107{
108108 let mut roots = Vec :: new ( ) ;
109109 for hir_id in tcx. hir_crate_items ( ( ) ) . items ( ) {
110110 let def_id = hir_id. owner_id . def_id . to_def_id ( ) ;
111- if predicate ( tcx , def_id) {
112- // The predicate should only ever apply to monomorphic items
111+ let def_kind = tcx . def_kind ( def_id) ;
112+ if matches ! ( def_kind , DefKind :: Const ) && predicate ( tcx , def_id ) {
113113 let instance = Instance :: mono ( tcx, def_id) ;
114114 let body = tcx. instance_mir ( InstanceDef :: Item ( WithOptConstParam :: unknown ( def_id) ) ) ;
115- let mut extrator =
116- ConstMonoItemExtractor { tcx, body, instance, collected : FxHashSet :: default ( ) } ;
117- extrator . visit_body ( body) ;
115+ let mut collector =
116+ MonoItemsFnCollector { tcx, body, instance, collected : FxHashSet :: default ( ) } ;
117+ collector . visit_body ( body) ;
118118
119- roots. extend ( extrator . collected ) ;
119+ roots. extend ( collector . collected ) ;
120120 }
121121 }
122122 roots
@@ -602,53 +602,6 @@ fn collect_alloc_items(tcx: TyCtxt, alloc_id: AllocId) -> Vec<MonoItem> {
602602 items
603603}
604604
605- /// This MIR Visitor is intended for one specific purpose:
606- /// Find the closure that exist inside a top-level const declaration generated by
607- /// test declarations. This allows us to treat this closure instance as a root for
608- /// the reachability analysis.
609- ///
610- /// Entry into this visitor will be via `visit_body`
611- struct ConstMonoItemExtractor < ' a , ' tcx > {
612- tcx : TyCtxt < ' tcx > ,
613- collected : FxHashSet < MonoItem < ' tcx > > ,
614- instance : Instance < ' tcx > ,
615- body : & ' a Body < ' tcx > ,
616- }
617-
618- impl < ' a , ' tcx > MirVisitor < ' tcx > for ConstMonoItemExtractor < ' a , ' tcx > {
619- #[ allow( clippy:: single_match) ]
620- fn visit_rvalue ( & mut self , rvalue : & Rvalue < ' tcx > , location : Location ) {
621- trace ! ( rvalue=?* rvalue, "visit_rvalue" ) ;
622-
623- match * rvalue {
624- Rvalue :: Cast ( CastKind :: Pointer ( PointerCast :: ClosureFnPointer ( _) ) , ref operand, _) => {
625- let source_ty = operand. ty ( self . body , self . tcx ) ;
626- let source_ty = self . instance . subst_mir_and_normalize_erasing_regions (
627- self . tcx ,
628- ParamEnv :: reveal_all ( ) ,
629- source_ty,
630- ) ;
631- match * source_ty. kind ( ) {
632- Closure ( def_id, substs) => {
633- let instance = Instance :: resolve_closure (
634- self . tcx ,
635- def_id,
636- substs,
637- ClosureKind :: FnOnce ,
638- )
639- . expect ( "failed to normalize and resolve closure during codegen" ) ;
640- self . collected . insert ( MonoItem :: Fn ( instance. polymorphize ( self . tcx ) ) ) ;
641- }
642- _ => unreachable ! ( "Unexpected type: {:?}" , source_ty) ,
643- }
644- }
645- _ => { /* not interesting */ }
646- }
647-
648- self . super_rvalue ( rvalue, location) ;
649- }
650- }
651-
652605#[ cfg( debug_assertions) ]
653606mod debug {
654607 #![ allow( dead_code) ]
0 commit comments