18
18
#![ cfg_attr( not( stage0) , deny( warnings) ) ]
19
19
20
20
#![ feature( associated_consts) ]
21
+ #![ feature( borrow_state) ]
21
22
#![ feature( rustc_diagnostic_macros) ]
22
23
#![ feature( rustc_private) ]
23
24
#![ feature( staged_api) ]
@@ -812,7 +813,7 @@ pub struct ModuleS<'a> {
812
813
extern_crate_id : Option < NodeId > ,
813
814
814
815
resolutions : RefCell < HashMap < ( Name , Namespace ) , NameResolution < ' a > > > ,
815
- unresolved_imports : RefCell < Vec < ImportDirective > > ,
816
+ unresolved_imports : RefCell < Vec < & ' a ImportDirective > > ,
816
817
817
818
// The module children of this node, including normal modules and anonymous modules.
818
819
// Anonymous children are pseudo-modules that are implicitly created around items
@@ -832,26 +833,31 @@ pub struct ModuleS<'a> {
832
833
833
834
shadowed_traits : RefCell < Vec < & ' a NameBinding < ' a > > > ,
834
835
835
- // The number of unresolved globs that this module exports.
836
- glob_count : Cell < usize > ,
836
+ glob_importers : RefCell < Vec < ( Module < ' a > , & ' a ImportDirective ) > > ,
837
+ resolved_globs : RefCell < ( Vec < Module < ' a > > /* public */ , Vec < Module < ' a > > /* private */ ) > ,
837
838
838
- // The number of unresolved pub imports (both regular and globs) in this module
839
- pub_count : Cell < usize > ,
839
+ // The number of public glob imports in this module.
840
+ public_glob_count : Cell < usize > ,
840
841
841
- // The number of unresolved pub glob imports in this module
842
- pub_glob_count : Cell < usize > ,
842
+ // The number of private glob imports in this module.
843
+ private_glob_count : Cell < usize > ,
843
844
844
845
// Whether this module is populated. If not populated, any attempt to
845
846
// access the children must be preceded with a
846
847
// `populate_module_if_necessary` call.
847
848
populated : Cell < bool > ,
849
+
850
+ arenas : & ' a ResolverArenas < ' a > ,
848
851
}
849
852
850
853
pub type Module < ' a > = & ' a ModuleS < ' a > ;
851
854
852
855
impl < ' a > ModuleS < ' a > {
853
-
854
- fn new ( parent_link : ParentLink < ' a > , def : Option < Def > , external : bool , is_public : bool ) -> Self {
856
+ fn new ( parent_link : ParentLink < ' a > ,
857
+ def : Option < Def > ,
858
+ external : bool ,
859
+ is_public : bool ,
860
+ arenas : & ' a ResolverArenas < ' a > ) -> Self {
855
861
ModuleS {
856
862
parent_link : parent_link,
857
863
def : def,
@@ -861,53 +867,18 @@ impl<'a> ModuleS<'a> {
861
867
unresolved_imports : RefCell :: new ( Vec :: new ( ) ) ,
862
868
module_children : RefCell :: new ( NodeMap ( ) ) ,
863
869
shadowed_traits : RefCell :: new ( Vec :: new ( ) ) ,
864
- glob_count : Cell :: new ( 0 ) ,
865
- pub_count : Cell :: new ( 0 ) ,
866
- pub_glob_count : Cell :: new ( 0 ) ,
870
+ glob_importers : RefCell :: new ( Vec :: new ( ) ) ,
871
+ resolved_globs : RefCell :: new ( ( Vec :: new ( ) , Vec :: new ( ) ) ) ,
872
+ public_glob_count : Cell :: new ( 0 ) ,
873
+ private_glob_count : Cell :: new ( 0 ) ,
867
874
populated : Cell :: new ( !external) ,
875
+ arenas : arenas
868
876
}
869
877
}
870
878
871
- fn resolve_name ( & self , name : Name , ns : Namespace , allow_private_imports : bool )
872
- -> ResolveResult < & ' a NameBinding < ' a > > {
873
- let glob_count =
874
- if allow_private_imports { self . glob_count . get ( ) } else { self . pub_glob_count . get ( ) } ;
875
-
876
- self . resolutions . borrow ( ) . get ( & ( name, ns) ) . cloned ( ) . unwrap_or_default ( ) . result ( glob_count)
877
- . and_then ( |binding| {
878
- let allowed = allow_private_imports || !binding. is_import ( ) || binding. is_public ( ) ;
879
- if allowed { Success ( binding) } else { Failed ( None ) }
880
- } )
881
- }
882
-
883
- // Define the name or return the existing binding if there is a collision.
884
- fn try_define_child ( & self , name : Name , ns : Namespace , binding : & ' a NameBinding < ' a > )
885
- -> Result < ( ) , & ' a NameBinding < ' a > > {
886
- let mut children = self . resolutions . borrow_mut ( ) ;
887
- let resolution = children. entry ( ( name, ns) ) . or_insert_with ( Default :: default) ;
888
-
889
- // FIXME #31379: We can use methods from imported traits shadowed by non-import items
890
- if let Some ( old_binding) = resolution. binding {
891
- if !old_binding. is_import ( ) && binding. is_import ( ) {
892
- if let Some ( Def :: Trait ( _) ) = binding. def ( ) {
893
- self . shadowed_traits . borrow_mut ( ) . push ( binding) ;
894
- }
895
- }
896
- }
897
-
898
- resolution. try_define ( binding)
899
- }
900
-
901
- fn increment_outstanding_references_for ( & self , name : Name , ns : Namespace ) {
902
- let mut children = self . resolutions . borrow_mut ( ) ;
903
- children. entry ( ( name, ns) ) . or_insert_with ( Default :: default) . outstanding_references += 1 ;
904
- }
905
-
906
- fn decrement_outstanding_references_for ( & self , name : Name , ns : Namespace ) {
907
- match self . resolutions . borrow_mut ( ) . get_mut ( & ( name, ns) ) . unwrap ( ) . outstanding_references {
908
- 0 => panic ! ( "No more outstanding references!" ) ,
909
- ref mut outstanding_references => { * outstanding_references -= 1 ; }
910
- }
879
+ fn add_import_directive ( & self , import_directive : ImportDirective ) {
880
+ let import_directive = self . arenas . alloc_import_directive ( import_directive) ;
881
+ self . unresolved_imports . borrow_mut ( ) . push ( import_directive) ;
911
882
}
912
883
913
884
fn for_each_child < F : FnMut ( Name , Namespace , & ' a NameBinding < ' a > ) > ( & self , mut f : F ) {
@@ -943,26 +914,9 @@ impl<'a> ModuleS<'a> {
943
914
}
944
915
}
945
916
946
- pub fn inc_glob_count ( & self ) {
947
- self . glob_count . set ( self . glob_count . get ( ) + 1 ) ;
948
- }
949
- pub fn dec_glob_count ( & self ) {
950
- assert ! ( self . glob_count. get( ) > 0 ) ;
951
- self . glob_count . set ( self . glob_count . get ( ) - 1 ) ;
952
- }
953
- pub fn inc_pub_count ( & self ) {
954
- self . pub_count . set ( self . pub_count . get ( ) + 1 ) ;
955
- }
956
- pub fn dec_pub_count ( & self ) {
957
- assert ! ( self . pub_count. get( ) > 0 ) ;
958
- self . pub_count . set ( self . pub_count . get ( ) - 1 ) ;
959
- }
960
- pub fn inc_pub_glob_count ( & self ) {
961
- self . pub_glob_count . set ( self . pub_glob_count . get ( ) + 1 ) ;
962
- }
963
- pub fn dec_pub_glob_count ( & self ) {
964
- assert ! ( self . pub_glob_count. get( ) > 0 ) ;
965
- self . pub_glob_count . set ( self . pub_glob_count . get ( ) - 1 ) ;
917
+ fn inc_glob_count ( & self , is_public : bool ) {
918
+ let glob_count = if is_public { & self . public_glob_count } else { & self . private_glob_count } ;
919
+ glob_count. set ( glob_count. get ( ) + 1 ) ;
966
920
}
967
921
}
968
922
@@ -995,14 +949,14 @@ bitflags! {
995
949
}
996
950
997
951
// Records a possibly-private value, type, or module definition.
998
- #[ derive( Debug ) ]
952
+ #[ derive( Clone , Debug ) ]
999
953
pub struct NameBinding < ' a > {
1000
954
modifiers : DefModifiers ,
1001
955
kind : NameBindingKind < ' a > ,
1002
956
span : Option < Span > ,
1003
957
}
1004
958
1005
- #[ derive( Debug ) ]
959
+ #[ derive( Clone , Debug ) ]
1006
960
enum NameBindingKind < ' a > {
1007
961
Def ( Def ) ,
1008
962
Module ( Module < ' a > ) ,
@@ -1167,6 +1121,19 @@ pub struct Resolver<'a, 'tcx: 'a> {
1167
1121
pub struct ResolverArenas < ' a > {
1168
1122
modules : arena:: TypedArena < ModuleS < ' a > > ,
1169
1123
name_bindings : arena:: TypedArena < NameBinding < ' a > > ,
1124
+ import_directives : arena:: TypedArena < ImportDirective > ,
1125
+ }
1126
+
1127
+ impl < ' a > ResolverArenas < ' a > {
1128
+ fn alloc_module ( & ' a self , module : ModuleS < ' a > ) -> Module < ' a > {
1129
+ self . modules . alloc ( module)
1130
+ }
1131
+ fn alloc_name_binding ( & ' a self , name_binding : NameBinding < ' a > ) -> & ' a NameBinding < ' a > {
1132
+ self . name_bindings . alloc ( name_binding)
1133
+ }
1134
+ fn alloc_import_directive ( & ' a self , import_directive : ImportDirective ) -> & ' a ImportDirective {
1135
+ self . import_directives . alloc ( import_directive)
1136
+ }
1170
1137
}
1171
1138
1172
1139
#[ derive( PartialEq ) ]
@@ -1182,8 +1149,9 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
1182
1149
arenas : & ' a ResolverArenas < ' a > )
1183
1150
-> Resolver < ' a , ' tcx > {
1184
1151
let root_def_id = ast_map. local_def_id ( CRATE_NODE_ID ) ;
1185
- let graph_root = ModuleS :: new ( NoParentLink , Some ( Def :: Mod ( root_def_id) ) , false , true ) ;
1186
- let graph_root = arenas. modules . alloc ( graph_root) ;
1152
+ let graph_root =
1153
+ ModuleS :: new ( NoParentLink , Some ( Def :: Mod ( root_def_id) ) , false , true , arenas) ;
1154
+ let graph_root = arenas. alloc_module ( graph_root) ;
1187
1155
1188
1156
Resolver {
1189
1157
session : session,
@@ -1234,6 +1202,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
1234
1202
ResolverArenas {
1235
1203
modules : arena:: TypedArena :: new ( ) ,
1236
1204
name_bindings : arena:: TypedArena :: new ( ) ,
1205
+ import_directives : arena:: TypedArena :: new ( ) ,
1237
1206
}
1238
1207
}
1239
1208
@@ -1242,11 +1211,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
1242
1211
def : Option < Def > ,
1243
1212
external : bool ,
1244
1213
is_public : bool ) -> Module < ' a > {
1245
- self . arenas . modules . alloc ( ModuleS :: new ( parent_link, def, external, is_public) )
1246
- }
1247
-
1248
- fn new_name_binding ( & self , name_binding : NameBinding < ' a > ) -> & ' a NameBinding < ' a > {
1249
- self . arenas . name_bindings . alloc ( name_binding)
1214
+ self . arenas . alloc_module ( ModuleS :: new ( parent_link, def, external, is_public, self . arenas ) )
1250
1215
}
1251
1216
1252
1217
fn new_extern_crate_module ( & self ,
@@ -1255,7 +1220,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
1255
1220
is_public : bool ,
1256
1221
local_node_id : NodeId )
1257
1222
-> Module < ' a > {
1258
- let mut module = ModuleS :: new ( parent_link, Some ( def) , false , is_public) ;
1223
+ let mut module = ModuleS :: new ( parent_link, Some ( def) , false , is_public, self . arenas ) ;
1259
1224
module. extern_crate_id = Some ( local_node_id) ;
1260
1225
self . arenas . modules . alloc ( module)
1261
1226
}
@@ -1626,18 +1591,6 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
1626
1591
} )
1627
1592
}
1628
1593
1629
- fn report_unresolved_imports ( & mut self , module_ : Module < ' a > ) {
1630
- for import in module_. unresolved_imports . borrow ( ) . iter ( ) {
1631
- resolve_error ( self , import. span , ResolutionError :: UnresolvedImport ( None ) ) ;
1632
- break ;
1633
- }
1634
-
1635
- // Descend into children and anonymous children.
1636
- for ( _, module_) in module_. module_children . borrow ( ) . iter ( ) {
1637
- self . report_unresolved_imports ( module_) ;
1638
- }
1639
- }
1640
-
1641
1594
// AST resolution
1642
1595
//
1643
1596
// We maintain a list of value ribs and type ribs.
0 commit comments