@@ -16,6 +16,7 @@ use crate::{ResolutionError, Determinacy, PathResult, CrateLint};
16
16
use rustc:: bug;
17
17
use rustc:: hir:: def:: { self , * } ;
18
18
use rustc:: hir:: def_id:: { CRATE_DEF_INDEX , LOCAL_CRATE , DefId } ;
19
+ use rustc:: hir:: map:: DefCollector ;
19
20
use rustc:: ty;
20
21
use rustc:: middle:: cstore:: CrateStore ;
21
22
use rustc_metadata:: cstore:: LoadedMacro ;
@@ -159,33 +160,34 @@ impl<'a> Resolver<'a> {
159
160
Some ( ext)
160
161
}
161
162
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
-
175
163
crate fn build_reduced_graph (
176
164
& mut self , fragment : & AstFragment , parent_scope : ParentScope < ' a >
177
165
) -> LegacyScope < ' a > {
166
+ fragment. visit_with ( & mut DefCollector :: new ( & mut self . definitions , parent_scope. expansion ) ) ;
178
167
let mut visitor = BuildReducedGraphVisitor { r : self , parent_scope } ;
179
168
fragment. visit_with ( & mut visitor) ;
180
169
visitor. parent_scope . legacy
181
170
}
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
+ }
182
180
}
183
181
184
182
struct BuildReducedGraphVisitor < ' a , ' b > {
185
183
r : & ' b mut Resolver < ' a > ,
186
184
parent_scope : ParentScope < ' a > ,
187
185
}
188
186
187
+ impl < ' a > AsMut < Resolver < ' a > > for BuildReducedGraphVisitor < ' a , ' _ > {
188
+ fn as_mut ( & mut self ) -> & mut Resolver < ' a > { self . r }
189
+ }
190
+
189
191
impl < ' a , ' b > BuildReducedGraphVisitor < ' a , ' b > {
190
192
fn resolve_visibility ( & mut self , vis : & ast:: Visibility ) -> ty:: Visibility {
191
193
let parent_scope = & self . parent_scope ;
@@ -603,8 +605,6 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
603
605
self . r . get_module ( DefId { krate : crate_id, index : CRATE_DEF_INDEX } )
604
606
} ;
605
607
606
- self . r . populate_module_if_necessary ( module) ;
607
-
608
608
let used = self . process_legacy_macro_imports ( item, module) ;
609
609
let binding =
610
610
( module, ty:: Visibility :: Public , sp, expansion) . to_name_binding ( self . r . arenas ) ;
@@ -879,80 +879,67 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
879
879
// This is only a guess, two equivalent idents may incorrectly get different gensyms here.
880
880
let ident = ident. gensym_if_underscore ( ) ;
881
881
let expansion = ExpnId :: root ( ) ; // FIXME(jseyfried) intercrate hygiene
882
+ // Record primary definitions.
882
883
match res {
883
884
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) => {
885
887
let module = self . r . new_module ( parent,
886
888
ModuleKind :: Def ( kind, def_id, ident. name ) ,
887
889
def_id,
888
890
expansion,
889
891
span) ;
890
892
self . r . define ( parent, ident, TypeNS , ( module, vis, DUMMY_SP , expansion) ) ;
891
893
}
892
- Res :: Def ( DefKind :: Variant , _)
894
+ Res :: Def ( DefKind :: Struct , _)
895
+ | Res :: Def ( DefKind :: Union , _)
896
+ | Res :: Def ( DefKind :: Variant , _)
893
897
| Res :: Def ( DefKind :: TyAlias , _)
894
898
| Res :: Def ( DefKind :: ForeignTy , _)
895
899
| Res :: Def ( DefKind :: OpaqueTy , _)
896
900
| Res :: Def ( DefKind :: TraitAlias , _)
901
+ | Res :: Def ( DefKind :: AssocTy , _)
902
+ | Res :: Def ( DefKind :: AssocOpaqueTy , _)
897
903
| 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) ) ,
901
906
Res :: Def ( DefKind :: Fn , _)
907
+ | Res :: Def ( DefKind :: Method , _)
902
908
| Res :: Def ( DefKind :: Static , _)
903
909
| 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 {
940
922
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.
944
923
let field_names = self . r . cstore . struct_field_names_untracked ( def_id) ;
945
924
self . insert_field_names ( def_id, field_names) ;
946
925
}
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
+ }
949
936
}
950
- _ => bug ! ( "unexpected resolution: {:?}" , res )
937
+ _ => { }
951
938
}
952
939
}
953
940
954
941
fn legacy_import_macro ( & mut self ,
955
- name : Name ,
942
+ name : ast :: Name ,
956
943
binding : & ' a NameBinding < ' a > ,
957
944
span : Span ,
958
945
allow_shadowing : bool ) {
@@ -1021,9 +1008,9 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
1021
1008
if let Some ( span) = import_all {
1022
1009
let directive = macro_use_directive ( self , span) ;
1023
1010
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) ;
1027
1014
} ) ;
1028
1015
} else {
1029
1016
for ident in single_imports. iter ( ) . cloned ( ) {
0 commit comments