@@ -41,7 +41,6 @@ use self::TypeParameters::*;
4141use self :: RibKind :: * ;
4242use self :: UseLexicalScopeFlag :: * ;
4343use self :: ModulePrefixResult :: * ;
44- use self :: ParentLink :: * ;
4544
4645use rustc:: hir:: map:: Definitions ;
4746use rustc:: hir:: { self , PrimTy , TyBool , TyChar , TyFloat , TyInt , TyUint , TyStr } ;
@@ -753,18 +752,15 @@ impl<'a> LexicalScopeBinding<'a> {
753752 }
754753}
755754
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 ) ,
762758}
763759
764760/// One node in the tree of modules.
765761pub struct ModuleS < ' a > {
766- parent_link : ParentLink < ' a > ,
767- def : Option < Def > ,
762+ parent : Option < Module < ' a > > ,
763+ kind : ModuleKind ,
768764
769765 // The node id of the closest normal module (`mod`) ancestor (including this module).
770766 normal_ancestor_id : Option < NodeId > ,
@@ -792,11 +788,11 @@ pub struct ModuleS<'a> {
792788pub type Module < ' a > = & ' a ModuleS < ' a > ;
793789
794790impl < ' 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 > )
796792 -> Self {
797793 ModuleS {
798- parent_link : parent_link ,
799- def : def ,
794+ parent : parent ,
795+ kind : kind ,
800796 normal_ancestor_id : normal_ancestor_id,
801797 extern_crate_id : None ,
802798 resolutions : RefCell :: new ( FnvHashMap ( ) ) ,
@@ -814,36 +810,36 @@ impl<'a> ModuleS<'a> {
814810 }
815811 }
816812
813+ fn def ( & self ) -> Option < Def > {
814+ match self . kind {
815+ ModuleKind :: Def ( def, _) => Some ( def) ,
816+ _ => None ,
817+ }
818+ }
819+
817820 fn def_id ( & self ) -> Option < DefId > {
818- self . def . as_ref ( ) . map ( Def :: def_id)
821+ self . def ( ) . as_ref ( ) . map ( Def :: def_id)
819822 }
820823
821824 // `self` resolves to the first module ancestor that `is_normal`.
822825 fn is_normal ( & self ) -> bool {
823- match self . def {
824- Some ( Def :: Mod ( _) ) => true ,
826+ match self . kind {
827+ ModuleKind :: Def ( Def :: Mod ( _) , _ ) => true ,
825828 _ => false ,
826829 }
827830 }
828831
829832 fn is_trait ( & self ) -> bool {
830- match self . def {
831- Some ( Def :: Trait ( _) ) => true ,
833+ match self . kind {
834+ ModuleKind :: Def ( Def :: Trait ( _) , _ ) => true ,
832835 _ => false ,
833836 }
834837 }
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- }
842838}
843839
844840impl < ' a > fmt:: Debug for ModuleS < ' a > {
845841 fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
846- write ! ( f, "{:?}" , self . def)
842+ write ! ( f, "{:?}" , self . def( ) )
847843 }
848844}
849845
@@ -903,7 +899,7 @@ impl<'a> NameBinding<'a> {
903899 fn def ( & self ) -> Def {
904900 match self . kind {
905901 NameBindingKind :: Def ( def) => def,
906- NameBindingKind :: Module ( module) => module. def . unwrap ( ) ,
902+ NameBindingKind :: Module ( module) => module. def ( ) . unwrap ( ) ,
907903 NameBindingKind :: Import { binding, .. } => binding. def ( ) ,
908904 NameBindingKind :: Ambiguity { .. } => Def :: Err ,
909905 }
@@ -1111,7 +1107,7 @@ impl<'a> ResolverArenas<'a> {
11111107impl < ' a > ty:: NodeIdTree for Resolver < ' a > {
11121108 fn is_descendant_of ( & self , mut node : NodeId , ancestor : NodeId ) -> bool {
11131109 while node != ancestor {
1114- node = match self . module_map [ & node] . parent ( ) {
1110+ node = match self . module_map [ & node] . parent {
11151111 Some ( parent) => parent. normal_ancestor_id . unwrap ( ) ,
11161112 None => return false ,
11171113 }
@@ -1178,10 +1174,10 @@ impl<'a> Resolver<'a> {
11781174 macro_loader : & ' a mut MacroLoader ,
11791175 arenas : & ' a ResolverArenas < ' a > )
11801176 -> 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 ( ) ) ;
11821179 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 ) ) ) ;
11851181 let mut module_map = NodeMap ( ) ;
11861182 module_map. insert ( CRATE_NODE_ID , graph_root) ;
11871183
@@ -1263,18 +1259,15 @@ impl<'a> Resolver<'a> {
12631259 self . report_errors ( ) ;
12641260 }
12651261
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 > )
12701263 -> 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) )
12721265 }
12731266
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 )
12751268 -> 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 ) ;
12781271 module. populated . set ( false ) ;
12791272 self . arenas . modules . alloc ( module)
12801273 }
@@ -1336,11 +1329,10 @@ impl<'a> Resolver<'a> {
13361329 -> Option < Module < ' a > > {
13371330 match this. resolve_name_in_module ( module, needle, TypeNS , false , None ) {
13381331 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
13441336 } ,
13451337 }
13461338 }
@@ -1516,15 +1508,13 @@ impl<'a> Resolver<'a> {
15161508 return Some ( LexicalScopeBinding :: Item ( binding) ) ;
15171509 }
15181510
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 ;
15281518 }
15291519 }
15301520
@@ -1561,7 +1551,7 @@ impl<'a> Resolver<'a> {
15611551 while i < module_path. len ( ) && "super" == module_path[ i] . as_str ( ) {
15621552 debug ! ( "(resolving module prefix) resolving `super` at {}" ,
15631553 module_to_string( & containing_module) ) ;
1564- if let Some ( parent) = containing_module. parent ( ) {
1554+ if let Some ( parent) = containing_module. parent {
15651555 containing_module = self . module_map [ & parent. normal_ancestor_id . unwrap ( ) ] ;
15661556 i += 1 ;
15671557 } else {
@@ -2954,7 +2944,7 @@ impl<'a> Resolver<'a> {
29542944 UseLexicalScope ,
29552945 Some ( expr. span ) ) {
29562946 Success ( e) => {
2957- if let Some ( def_type) = e. def {
2947+ if let Some ( def_type) = e. def ( ) {
29582948 def = def_type;
29592949 }
29602950 context = UnresolvedNameContext :: PathIsMod ( parent) ;
@@ -3163,16 +3153,13 @@ impl<'a> Resolver<'a> {
31633153 } ;
31643154 search_in_module ( self , search_module) ;
31653155
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) ) ;
31753161 }
3162+ break ;
31763163 }
31773164 }
31783165
@@ -3240,9 +3227,9 @@ impl<'a> Resolver<'a> {
32403227 // collect submodules to explore
32413228 if let Ok ( module) = name_binding. module ( ) {
32423229 // 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) => {
32463233 let mut paths = path_segments. clone ( ) ;
32473234 let ident = ast:: Ident :: with_empty_ctxt ( name) ;
32483235 let params = PathParameters :: none ( ) ;
@@ -3259,7 +3246,7 @@ impl<'a> Resolver<'a> {
32593246 if !in_module_is_extern || name_binding. vis == ty:: Visibility :: Public {
32603247 // add the module to the lookup
32613248 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 ( ) ) {
32633250 worklist. push ( ( module, path_segments, is_extern) ) ;
32643251 }
32653252 }
@@ -3294,7 +3281,7 @@ impl<'a> Resolver<'a> {
32943281 let mut path_resolution = err_path_resolution ( ) ;
32953282 let vis = match self . resolve_module_path ( & segments, DontUseLexicalScope , Some ( path. span ) ) {
32963283 Success ( module) => {
3297- path_resolution = PathResolution :: new ( module. def . unwrap ( ) ) ;
3284+ path_resolution = PathResolution :: new ( module. def ( ) . unwrap ( ) ) ;
32983285 ty:: Visibility :: Restricted ( module. normal_ancestor_id . unwrap ( ) )
32993286 }
33003287 Indeterminate => unreachable ! ( ) ,
@@ -3360,10 +3347,10 @@ impl<'a> Resolver<'a> {
33603347 return self . report_conflict ( parent, name, ns, old_binding, binding) ;
33613348 }
33623349
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" ,
33673354 _ => "enum" ,
33683355 } ;
33693356
@@ -3510,17 +3497,15 @@ fn module_to_string(module: Module) -> String {
35103497 let mut names = Vec :: new ( ) ;
35113498
35123499 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 {
35163502 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) ;
35233504 }
3505+ } else {
3506+ // danger, shouldn't be ident?
3507+ names. push ( token:: intern ( "<opaque>" ) ) ;
3508+ collect_mod ( names, module) ;
35243509 }
35253510 }
35263511 collect_mod ( & mut names, module) ;
0 commit comments