@@ -41,7 +41,6 @@ use self::TypeParameters::*;
41
41
use self :: RibKind :: * ;
42
42
use self :: UseLexicalScopeFlag :: * ;
43
43
use self :: ModulePrefixResult :: * ;
44
- use self :: ParentLink :: * ;
45
44
46
45
use rustc:: hir:: map:: Definitions ;
47
46
use rustc:: hir:: { self , PrimTy , TyBool , TyChar , TyFloat , TyInt , TyUint , TyStr } ;
@@ -753,18 +752,15 @@ impl<'a> LexicalScopeBinding<'a> {
753
752
}
754
753
}
755
754
756
- /// The link from a module up to its nearest parent node.
757
- #[ derive( Clone , Debug ) ]
758
- enum ParentLink < ' a > {
759
- NoParentLink ,
760
- ModuleParentLink ( Module < ' a > , Name ) ,
761
- BlockParentLink ( Module < ' a > , NodeId ) ,
755
+ enum ModuleKind {
756
+ Block ( NodeId ) ,
757
+ Def ( Def , Name ) ,
762
758
}
763
759
764
760
/// One node in the tree of modules.
765
761
pub struct ModuleS < ' a > {
766
- parent_link : ParentLink < ' a > ,
767
- def : Option < Def > ,
762
+ parent : Option < Module < ' a > > ,
763
+ kind : ModuleKind ,
768
764
769
765
// The node id of the closest normal module (`mod`) ancestor (including this module).
770
766
normal_ancestor_id : Option < NodeId > ,
@@ -792,11 +788,11 @@ pub struct ModuleS<'a> {
792
788
pub type Module < ' a > = & ' a ModuleS < ' a > ;
793
789
794
790
impl < ' a > ModuleS < ' a > {
795
- fn new ( parent_link : ParentLink < ' a > , def : Option < Def > , normal_ancestor_id : Option < NodeId > )
791
+ fn new ( parent : Option < Module < ' a > > , kind : ModuleKind , normal_ancestor_id : Option < NodeId > )
796
792
-> Self {
797
793
ModuleS {
798
- parent_link : parent_link ,
799
- def : def ,
794
+ parent : parent ,
795
+ kind : kind ,
800
796
normal_ancestor_id : normal_ancestor_id,
801
797
extern_crate_id : None ,
802
798
resolutions : RefCell :: new ( FnvHashMap ( ) ) ,
@@ -814,36 +810,36 @@ impl<'a> ModuleS<'a> {
814
810
}
815
811
}
816
812
813
+ fn def ( & self ) -> Option < Def > {
814
+ match self . kind {
815
+ ModuleKind :: Def ( def, _) => Some ( def) ,
816
+ _ => None ,
817
+ }
818
+ }
819
+
817
820
fn def_id ( & self ) -> Option < DefId > {
818
- self . def . as_ref ( ) . map ( Def :: def_id)
821
+ self . def ( ) . as_ref ( ) . map ( Def :: def_id)
819
822
}
820
823
821
824
// `self` resolves to the first module ancestor that `is_normal`.
822
825
fn is_normal ( & self ) -> bool {
823
- match self . def {
824
- Some ( Def :: Mod ( _) ) => true ,
826
+ match self . kind {
827
+ ModuleKind :: Def ( Def :: Mod ( _) , _ ) => true ,
825
828
_ => false ,
826
829
}
827
830
}
828
831
829
832
fn is_trait ( & self ) -> bool {
830
- match self . def {
831
- Some ( Def :: Trait ( _) ) => true ,
833
+ match self . kind {
834
+ ModuleKind :: Def ( Def :: Trait ( _) , _ ) => true ,
832
835
_ => false ,
833
836
}
834
837
}
835
-
836
- fn parent ( & self ) -> Option < & ' a Self > {
837
- match self . parent_link {
838
- ModuleParentLink ( parent, _) | BlockParentLink ( parent, _) => Some ( parent) ,
839
- NoParentLink => None ,
840
- }
841
- }
842
838
}
843
839
844
840
impl < ' a > fmt:: Debug for ModuleS < ' a > {
845
841
fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
846
- write ! ( f, "{:?}" , self . def)
842
+ write ! ( f, "{:?}" , self . def( ) )
847
843
}
848
844
}
849
845
@@ -903,7 +899,7 @@ impl<'a> NameBinding<'a> {
903
899
fn def ( & self ) -> Def {
904
900
match self . kind {
905
901
NameBindingKind :: Def ( def) => def,
906
- NameBindingKind :: Module ( module) => module. def . unwrap ( ) ,
902
+ NameBindingKind :: Module ( module) => module. def ( ) . unwrap ( ) ,
907
903
NameBindingKind :: Import { binding, .. } => binding. def ( ) ,
908
904
NameBindingKind :: Ambiguity { .. } => Def :: Err ,
909
905
}
@@ -1111,7 +1107,7 @@ impl<'a> ResolverArenas<'a> {
1111
1107
impl < ' a > ty:: NodeIdTree for Resolver < ' a > {
1112
1108
fn is_descendant_of ( & self , mut node : NodeId , ancestor : NodeId ) -> bool {
1113
1109
while node != ancestor {
1114
- node = match self . module_map [ & node] . parent ( ) {
1110
+ node = match self . module_map [ & node] . parent {
1115
1111
Some ( parent) => parent. normal_ancestor_id . unwrap ( ) ,
1116
1112
None => return false ,
1117
1113
}
@@ -1178,10 +1174,10 @@ impl<'a> Resolver<'a> {
1178
1174
macro_loader : & ' a mut MacroLoader ,
1179
1175
arenas : & ' a ResolverArenas < ' a > )
1180
1176
-> Resolver < ' a > {
1181
- let root_def_id = DefId :: local ( CRATE_DEF_INDEX ) ;
1177
+ let graph_root_kind =
1178
+ ModuleKind :: Def ( Def :: Mod ( DefId :: local ( CRATE_DEF_INDEX ) ) , keywords:: Invalid . name ( ) ) ;
1182
1179
let graph_root =
1183
- ModuleS :: new ( NoParentLink , Some ( Def :: Mod ( root_def_id) ) , Some ( CRATE_NODE_ID ) ) ;
1184
- let graph_root = arenas. alloc_module ( graph_root) ;
1180
+ arenas. alloc_module ( ModuleS :: new ( None , graph_root_kind, Some ( CRATE_NODE_ID ) ) ) ;
1185
1181
let mut module_map = NodeMap ( ) ;
1186
1182
module_map. insert ( CRATE_NODE_ID , graph_root) ;
1187
1183
@@ -1263,18 +1259,15 @@ impl<'a> Resolver<'a> {
1263
1259
self . report_errors ( ) ;
1264
1260
}
1265
1261
1266
- fn new_module ( & self ,
1267
- parent_link : ParentLink < ' a > ,
1268
- def : Option < Def > ,
1269
- normal_ancestor_id : Option < NodeId > )
1262
+ fn new_module ( & self , parent : Module < ' a > , kind : ModuleKind , normal_ancestor_id : Option < NodeId > )
1270
1263
-> Module < ' a > {
1271
- self . arenas . alloc_module ( ModuleS :: new ( parent_link , def , normal_ancestor_id) )
1264
+ self . arenas . alloc_module ( ModuleS :: new ( Some ( parent ) , kind , normal_ancestor_id) )
1272
1265
}
1273
1266
1274
- fn new_extern_crate_module ( & self , parent_link : ParentLink < ' a > , def : Def , local_node_id : NodeId )
1267
+ fn new_extern_crate_module ( & self , parent : Module < ' a > , name : Name , def : Def , node_id : NodeId )
1275
1268
-> Module < ' a > {
1276
- let mut module = ModuleS :: new ( parent_link , Some ( def) , Some ( local_node_id ) ) ;
1277
- module. extern_crate_id = Some ( local_node_id ) ;
1269
+ let mut module = ModuleS :: new ( Some ( parent ) , ModuleKind :: Def ( def, name ) , Some ( node_id ) ) ;
1270
+ module. extern_crate_id = Some ( node_id ) ;
1278
1271
module. populated . set ( false ) ;
1279
1272
self . arenas . modules . alloc ( module)
1280
1273
}
@@ -1336,11 +1329,10 @@ impl<'a> Resolver<'a> {
1336
1329
-> Option < Module < ' a > > {
1337
1330
match this. resolve_name_in_module ( module, needle, TypeNS , false , None ) {
1338
1331
Success ( binding) if binding. is_extern_crate ( ) => Some ( module) ,
1339
- _ => match module. parent_link {
1340
- ModuleParentLink ( ref parent, _) => {
1341
- search_parent_externals ( this, needle, parent)
1342
- }
1343
- _ => None ,
1332
+ _ => if let ( & ModuleKind :: Def ( ..) , Some ( parent) ) = ( & module. kind , module. parent ) {
1333
+ search_parent_externals ( this, needle, parent)
1334
+ } else {
1335
+ None
1344
1336
} ,
1345
1337
}
1346
1338
}
@@ -1516,15 +1508,13 @@ impl<'a> Resolver<'a> {
1516
1508
return Some ( LexicalScopeBinding :: Item ( binding) ) ;
1517
1509
}
1518
1510
1519
- // We can only see through anonymous modules
1520
- if module. def . is_some ( ) {
1521
- return match self . prelude {
1522
- Some ( prelude) if !module. no_implicit_prelude . get ( ) => {
1523
- self . resolve_name_in_module ( prelude, name, ns, false , None ) . success ( )
1524
- . map ( LexicalScopeBinding :: Item )
1525
- }
1526
- _ => None ,
1527
- } ;
1511
+ if let ModuleKind :: Block ( ..) = module. kind { // We can see through blocks
1512
+ } else if !module. no_implicit_prelude . get ( ) {
1513
+ return self . prelude . and_then ( |prelude| {
1514
+ self . resolve_name_in_module ( prelude, name, ns, false , None ) . success ( )
1515
+ } ) . map ( LexicalScopeBinding :: Item )
1516
+ } else {
1517
+ return None ;
1528
1518
}
1529
1519
}
1530
1520
@@ -1561,7 +1551,7 @@ impl<'a> Resolver<'a> {
1561
1551
while i < module_path. len ( ) && "super" == module_path[ i] . as_str ( ) {
1562
1552
debug ! ( "(resolving module prefix) resolving `super` at {}" ,
1563
1553
module_to_string( & containing_module) ) ;
1564
- if let Some ( parent) = containing_module. parent ( ) {
1554
+ if let Some ( parent) = containing_module. parent {
1565
1555
containing_module = self . module_map [ & parent. normal_ancestor_id . unwrap ( ) ] ;
1566
1556
i += 1 ;
1567
1557
} else {
@@ -2954,7 +2944,7 @@ impl<'a> Resolver<'a> {
2954
2944
UseLexicalScope ,
2955
2945
Some ( expr. span ) ) {
2956
2946
Success ( e) => {
2957
- if let Some ( def_type) = e. def {
2947
+ if let Some ( def_type) = e. def ( ) {
2958
2948
def = def_type;
2959
2949
}
2960
2950
context = UnresolvedNameContext :: PathIsMod ( parent) ;
@@ -3163,16 +3153,13 @@ impl<'a> Resolver<'a> {
3163
3153
} ;
3164
3154
search_in_module ( self , search_module) ;
3165
3155
3166
- match search_module. parent_link {
3167
- NoParentLink | ModuleParentLink ( ..) => {
3168
- if !search_module. no_implicit_prelude . get ( ) {
3169
- self . prelude . map ( |prelude| search_in_module ( self , prelude) ) ;
3170
- }
3171
- break ;
3172
- }
3173
- BlockParentLink ( parent_module, _) => {
3174
- search_module = parent_module;
3156
+ if let ModuleKind :: Block ( ..) = search_module. kind {
3157
+ search_module = search_module. parent . unwrap ( ) ;
3158
+ } else {
3159
+ if !search_module. no_implicit_prelude . get ( ) {
3160
+ self . prelude . map ( |prelude| search_in_module ( self , prelude) ) ;
3175
3161
}
3162
+ break ;
3176
3163
}
3177
3164
}
3178
3165
@@ -3240,9 +3227,9 @@ impl<'a> Resolver<'a> {
3240
3227
// collect submodules to explore
3241
3228
if let Ok ( module) = name_binding. module ( ) {
3242
3229
// form the path
3243
- let path_segments = match module. parent_link {
3244
- NoParentLink => path_segments. clone ( ) ,
3245
- ModuleParentLink ( _, name) => {
3230
+ let path_segments = match module. kind {
3231
+ _ if module . parent . is_none ( ) => path_segments. clone ( ) ,
3232
+ ModuleKind :: Def ( _, name) => {
3246
3233
let mut paths = path_segments. clone ( ) ;
3247
3234
let ident = ast:: Ident :: with_empty_ctxt ( name) ;
3248
3235
let params = PathParameters :: none ( ) ;
@@ -3259,7 +3246,7 @@ impl<'a> Resolver<'a> {
3259
3246
if !in_module_is_extern || name_binding. vis == ty:: Visibility :: Public {
3260
3247
// add the module to the lookup
3261
3248
let is_extern = in_module_is_extern || name_binding. is_extern_crate ( ) ;
3262
- if !worklist. iter ( ) . any ( |& ( m, ..) | m. def == module. def ) {
3249
+ if !worklist. iter ( ) . any ( |& ( m, ..) | m. def ( ) == module. def ( ) ) {
3263
3250
worklist. push ( ( module, path_segments, is_extern) ) ;
3264
3251
}
3265
3252
}
@@ -3294,7 +3281,7 @@ impl<'a> Resolver<'a> {
3294
3281
let mut path_resolution = err_path_resolution ( ) ;
3295
3282
let vis = match self . resolve_module_path ( & segments, DontUseLexicalScope , Some ( path. span ) ) {
3296
3283
Success ( module) => {
3297
- path_resolution = PathResolution :: new ( module. def . unwrap ( ) ) ;
3284
+ path_resolution = PathResolution :: new ( module. def ( ) . unwrap ( ) ) ;
3298
3285
ty:: Visibility :: Restricted ( module. normal_ancestor_id . unwrap ( ) )
3299
3286
}
3300
3287
Indeterminate => unreachable ! ( ) ,
@@ -3360,10 +3347,10 @@ impl<'a> Resolver<'a> {
3360
3347
return self . report_conflict ( parent, name, ns, old_binding, binding) ;
3361
3348
}
3362
3349
3363
- let container = match parent. def {
3364
- Some ( Def :: Mod ( _) ) => "module" ,
3365
- Some ( Def :: Trait ( _) ) => "trait" ,
3366
- None => "block" ,
3350
+ let container = match parent. kind {
3351
+ ModuleKind :: Def ( Def :: Mod ( _) , _ ) => "module" ,
3352
+ ModuleKind :: Def ( Def :: Trait ( _) , _ ) => "trait" ,
3353
+ ModuleKind :: Block ( .. ) => "block" ,
3367
3354
_ => "enum" ,
3368
3355
} ;
3369
3356
@@ -3510,17 +3497,15 @@ fn module_to_string(module: Module) -> String {
3510
3497
let mut names = Vec :: new ( ) ;
3511
3498
3512
3499
fn collect_mod ( names : & mut Vec < ast:: Name > , module : Module ) {
3513
- match module. parent_link {
3514
- NoParentLink => { }
3515
- ModuleParentLink ( ref module, name) => {
3500
+ if let ModuleKind :: Def ( _, name) = module. kind {
3501
+ if let Some ( parent) = module. parent {
3516
3502
names. push ( name) ;
3517
- collect_mod ( names, module) ;
3518
- }
3519
- BlockParentLink ( ref module, _) => {
3520
- // danger, shouldn't be ident?
3521
- names. push ( token:: intern ( "<opaque>" ) ) ;
3522
- collect_mod ( names, module) ;
3503
+ collect_mod ( names, parent) ;
3523
3504
}
3505
+ } else {
3506
+ // danger, shouldn't be ident?
3507
+ names. push ( token:: intern ( "<opaque>" ) ) ;
3508
+ collect_mod ( names, module) ;
3524
3509
}
3525
3510
}
3526
3511
collect_mod ( & mut names, module) ;
0 commit comments