@@ -16,6 +16,7 @@ use crate::{ResolutionError, Determinacy, PathResult, CrateLint};
1616use rustc:: bug;
1717use rustc:: hir:: def:: { self , * } ;
1818use rustc:: hir:: def_id:: { CRATE_DEF_INDEX , LOCAL_CRATE , DefId } ;
19+ use rustc:: hir:: map:: DefCollector ;
1920use rustc:: ty;
2021use rustc:: middle:: cstore:: CrateStore ;
2122use rustc_metadata:: cstore:: LoadedMacro ;
@@ -159,33 +160,34 @@ impl<'a> Resolver<'a> {
159160 Some ( ext)
160161 }
161162
162- /// Ensures that the reduced graph rooted at the given external module
163- /// is built, building it if it is not.
164- crate fn populate_module_if_necessary ( & mut self , module : Module < ' a > ) {
165- if module. populated . get ( ) { return }
166- let def_id = module. def_id ( ) . unwrap ( ) ;
167- for child in self . cstore . item_children_untracked ( def_id, self . session ) {
168- let child = child. map_id ( |_| panic ! ( "unexpected id" ) ) ;
169- BuildReducedGraphVisitor { parent_scope : ParentScope :: module ( module) , r : self }
170- . build_reduced_graph_for_external_crate_res ( child) ;
171- }
172- module. populated . set ( true )
173- }
174-
175163 crate fn build_reduced_graph (
176164 & mut self , fragment : & AstFragment , parent_scope : ParentScope < ' a >
177165 ) -> LegacyScope < ' a > {
166+ fragment. visit_with ( & mut DefCollector :: new ( & mut self . definitions , parent_scope. expansion ) ) ;
178167 let mut visitor = BuildReducedGraphVisitor { r : self , parent_scope } ;
179168 fragment. visit_with ( & mut visitor) ;
180169 visitor. parent_scope . legacy
181170 }
171+
172+ crate fn build_reduced_graph_external ( & mut self , module : Module < ' a > ) {
173+ let def_id = module. def_id ( ) . expect ( "unpopulated module without a def-id" ) ;
174+ for child in self . cstore . item_children_untracked ( def_id, self . session ) {
175+ let child = child. map_id ( |_| panic ! ( "unexpected id" ) ) ;
176+ BuildReducedGraphVisitor { r : self , parent_scope : ParentScope :: module ( module) }
177+ . build_reduced_graph_for_external_crate_res ( child) ;
178+ }
179+ }
182180}
183181
184182struct BuildReducedGraphVisitor < ' a , ' b > {
185183 r : & ' b mut Resolver < ' a > ,
186184 parent_scope : ParentScope < ' a > ,
187185}
188186
187+ impl < ' a > AsMut < Resolver < ' a > > for BuildReducedGraphVisitor < ' a , ' _ > {
188+ fn as_mut ( & mut self ) -> & mut Resolver < ' a > { self . r }
189+ }
190+
189191impl < ' a , ' b > BuildReducedGraphVisitor < ' a , ' b > {
190192 fn resolve_visibility ( & mut self , vis : & ast:: Visibility ) -> ty:: Visibility {
191193 let parent_scope = & self . parent_scope ;
@@ -603,8 +605,6 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
603605 self . r . get_module ( DefId { krate : crate_id, index : CRATE_DEF_INDEX } )
604606 } ;
605607
606- self . r . populate_module_if_necessary ( module) ;
607-
608608 let used = self . process_legacy_macro_imports ( item, module) ;
609609 let binding =
610610 ( module, ty:: Visibility :: Public , sp, expansion) . to_name_binding ( self . r . arenas ) ;
@@ -879,80 +879,67 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
879879 // This is only a guess, two equivalent idents may incorrectly get different gensyms here.
880880 let ident = ident. gensym_if_underscore ( ) ;
881881 let expansion = ExpnId :: root ( ) ; // FIXME(jseyfried) intercrate hygiene
882+ // Record primary definitions.
882883 match res {
883884 Res :: Def ( kind @ DefKind :: Mod , def_id)
884- | Res :: Def ( kind @ DefKind :: Enum , def_id) => {
885+ | Res :: Def ( kind @ DefKind :: Enum , def_id)
886+ | Res :: Def ( kind @ DefKind :: Trait , def_id) => {
885887 let module = self . r . new_module ( parent,
886888 ModuleKind :: Def ( kind, def_id, ident. name ) ,
887889 def_id,
888890 expansion,
889891 span) ;
890892 self . r . define ( parent, ident, TypeNS , ( module, vis, DUMMY_SP , expansion) ) ;
891893 }
892- Res :: Def ( DefKind :: Variant , _)
894+ Res :: Def ( DefKind :: Struct , _)
895+ | Res :: Def ( DefKind :: Union , _)
896+ | Res :: Def ( DefKind :: Variant , _)
893897 | Res :: Def ( DefKind :: TyAlias , _)
894898 | Res :: Def ( DefKind :: ForeignTy , _)
895899 | Res :: Def ( DefKind :: OpaqueTy , _)
896900 | Res :: Def ( DefKind :: TraitAlias , _)
901+ | Res :: Def ( DefKind :: AssocTy , _)
902+ | Res :: Def ( DefKind :: AssocOpaqueTy , _)
897903 | Res :: PrimTy ( ..)
898- | Res :: ToolMod => {
899- self . r . define ( parent, ident, TypeNS , ( res, vis, DUMMY_SP , expansion) ) ;
900- }
904+ | Res :: ToolMod =>
905+ self . r . define ( parent, ident, TypeNS , ( res, vis, DUMMY_SP , expansion) ) ,
901906 Res :: Def ( DefKind :: Fn , _)
907+ | Res :: Def ( DefKind :: Method , _)
902908 | Res :: Def ( DefKind :: Static , _)
903909 | Res :: Def ( DefKind :: Const , _)
904- | Res :: Def ( DefKind :: Ctor ( CtorOf :: Variant , ..) , _) => {
905- self . r . define ( parent, ident, ValueNS , ( res, vis, DUMMY_SP , expansion) ) ;
906- }
907- Res :: Def ( DefKind :: Ctor ( CtorOf :: Struct , ..) , def_id) => {
908- self . r . define ( parent, ident, ValueNS , ( res, vis, DUMMY_SP , expansion) ) ;
909-
910- if let Some ( struct_def_id) =
911- self . r . cstore . def_key ( def_id) . parent
912- . map ( |index| DefId { krate : def_id. krate , index : index } ) {
913- self . r . struct_constructors . insert ( struct_def_id, ( res, vis) ) ;
914- }
915- }
916- Res :: Def ( DefKind :: Trait , def_id) => {
917- let module_kind = ModuleKind :: Def ( DefKind :: Trait , def_id, ident. name ) ;
918- let module = self . r . new_module ( parent,
919- module_kind,
920- parent. normal_ancestor_id ,
921- expansion,
922- span) ;
923- self . r . define ( parent, ident, TypeNS , ( module, vis, DUMMY_SP , expansion) ) ;
924-
925- for child in self . r . cstore . item_children_untracked ( def_id, self . r . session ) {
926- let res = child. res . map_id ( |_| panic ! ( "unexpected id" ) ) ;
927- let ns = if let Res :: Def ( DefKind :: AssocTy , _) = res {
928- TypeNS
929- } else { ValueNS } ;
930- self . r . define ( module, child. ident , ns,
931- ( res, ty:: Visibility :: Public , DUMMY_SP , expansion) ) ;
932-
933- if self . r . cstore . associated_item_cloned_untracked ( child. res . def_id ( ) )
934- . method_has_self_argument {
935- self . r . has_self . insert ( res. def_id ( ) ) ;
936- }
937- }
938- module. populated . set ( true ) ;
939- }
910+ | Res :: Def ( DefKind :: AssocConst , _)
911+ | Res :: Def ( DefKind :: Ctor ( ..) , _) =>
912+ self . r . define ( parent, ident, ValueNS , ( res, vis, DUMMY_SP , expansion) ) ,
913+ Res :: Def ( DefKind :: Macro ( ..) , _)
914+ | Res :: NonMacroAttr ( ..) =>
915+ self . r . define ( parent, ident, MacroNS , ( res, vis, DUMMY_SP , expansion) ) ,
916+ Res :: Def ( DefKind :: TyParam , _) | Res :: Def ( DefKind :: ConstParam , _)
917+ | Res :: Local ( ..) | Res :: SelfTy ( ..) | Res :: SelfCtor ( ..) | Res :: Err =>
918+ bug ! ( "unexpected resolution: {:?}" , res)
919+ }
920+ // Record some extra data for better diagnostics.
921+ match res {
940922 Res :: Def ( DefKind :: Struct , def_id) | Res :: Def ( DefKind :: Union , def_id) => {
941- self . r . define ( parent, ident, TypeNS , ( res, vis, DUMMY_SP , expansion) ) ;
942-
943- // Record field names for error reporting.
944923 let field_names = self . r . cstore . struct_field_names_untracked ( def_id) ;
945924 self . insert_field_names ( def_id, field_names) ;
946925 }
947- Res :: Def ( DefKind :: Macro ( ..) , _) | Res :: NonMacroAttr ( ..) => {
948- self . r . define ( parent, ident, MacroNS , ( res, vis, DUMMY_SP , expansion) ) ;
926+ Res :: Def ( DefKind :: Method , def_id) => {
927+ if self . r . cstore . associated_item_cloned_untracked ( def_id) . method_has_self_argument {
928+ self . r . has_self . insert ( def_id) ;
929+ }
930+ }
931+ Res :: Def ( DefKind :: Ctor ( CtorOf :: Struct , ..) , def_id) => {
932+ let parent = self . r . cstore . def_key ( def_id) . parent ;
933+ if let Some ( struct_def_id) = parent. map ( |index| DefId { index, ..def_id } ) {
934+ self . r . struct_constructors . insert ( struct_def_id, ( res, vis) ) ;
935+ }
949936 }
950- _ => bug ! ( "unexpected resolution: {:?}" , res )
937+ _ => { }
951938 }
952939 }
953940
954941 fn legacy_import_macro ( & mut self ,
955- name : Name ,
942+ name : ast :: Name ,
956943 binding : & ' a NameBinding < ' a > ,
957944 span : Span ,
958945 allow_shadowing : bool ) {
@@ -1021,9 +1008,9 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
10211008 if let Some ( span) = import_all {
10221009 let directive = macro_use_directive ( self , span) ;
10231010 self . r . potentially_unused_imports . push ( directive) ;
1024- module. for_each_child ( | ident, ns, binding| if ns == MacroNS {
1025- let imported_binding = self . r . import ( binding, directive) ;
1026- self . legacy_import_macro ( ident. name , imported_binding, span, allow_shadowing) ;
1011+ module. for_each_child ( self , |this , ident, ns, binding| if ns == MacroNS {
1012+ let imported_binding = this . r . import ( binding, directive) ;
1013+ this . legacy_import_macro ( ident. name , imported_binding, span, allow_shadowing) ;
10271014 } ) ;
10281015 } else {
10291016 for ident in single_imports. iter ( ) . cloned ( ) {
0 commit comments