@@ -54,7 +54,7 @@ use rustc::util::nodemap::{NodeMap, NodeSet, FnvHashMap, FnvHashSet};
54
54
55
55
use syntax:: ext:: hygiene:: Mark ;
56
56
use syntax:: ast:: { self , FloatTy } ;
57
- use syntax:: ast:: { CRATE_NODE_ID , Name , NodeId , CrateNum , IntTy , UintTy } ;
57
+ use syntax:: ast:: { CRATE_NODE_ID , DUMMY_NODE_ID , Name , NodeId , CrateNum , IntTy , UintTy } ;
58
58
use syntax:: parse:: token:: { self , keywords} ;
59
59
use syntax:: util:: lev_distance:: find_best_match_for_name;
60
60
@@ -768,6 +768,9 @@ pub struct ModuleS<'a> {
768
768
parent_link : ParentLink < ' a > ,
769
769
def : Option < Def > ,
770
770
771
+ // The node id of the closest normal module (`mod`) ancestor (including this module).
772
+ normal_ancestor_id : NodeId ,
773
+
771
774
// If the module is an extern crate, `def` is root of the external crate and `extern_crate_id`
772
775
// is the NodeId of the local `extern crate` item (otherwise, `extern_crate_id` is None).
773
776
extern_crate_id : Option < NodeId > ,
@@ -791,17 +794,18 @@ pub struct ModuleS<'a> {
791
794
pub type Module < ' a > = & ' a ModuleS < ' a > ;
792
795
793
796
impl < ' a > ModuleS < ' a > {
794
- fn new ( parent_link : ParentLink < ' a > , def : Option < Def > , external : bool ) -> Self {
797
+ fn new ( parent_link : ParentLink < ' a > , def : Option < Def > , normal_ancestor_id : NodeId ) -> Self {
795
798
ModuleS {
796
799
parent_link : parent_link,
797
800
def : def,
801
+ normal_ancestor_id : normal_ancestor_id,
798
802
extern_crate_id : None ,
799
803
resolutions : RefCell :: new ( FnvHashMap ( ) ) ,
800
804
no_implicit_prelude : Cell :: new ( false ) ,
801
805
glob_importers : RefCell :: new ( Vec :: new ( ) ) ,
802
806
globs : RefCell :: new ( ( Vec :: new ( ) ) ) ,
803
807
traits : RefCell :: new ( None ) ,
804
- populated : Cell :: new ( !external ) ,
808
+ populated : Cell :: new ( normal_ancestor_id != DUMMY_NODE_ID ) ,
805
809
}
806
810
}
807
811
@@ -829,6 +833,13 @@ impl<'a> ModuleS<'a> {
829
833
_ => false ,
830
834
}
831
835
}
836
+
837
+ fn parent ( & self ) -> Option < & ' a Self > {
838
+ match self . parent_link {
839
+ ModuleParentLink ( parent, _) | BlockParentLink ( parent, _) => Some ( parent) ,
840
+ NoParentLink => None ,
841
+ }
842
+ }
832
843
}
833
844
834
845
impl < ' a > fmt:: Debug for ModuleS < ' a > {
@@ -983,10 +994,6 @@ pub struct Resolver<'a> {
983
994
// The module that represents the current item scope.
984
995
current_module : Module < ' a > ,
985
996
986
- // The visibility of `pub(self)` items in the current scope.
987
- // Equivalently, the visibility required for an item to be accessible from the current scope.
988
- current_vis : ty:: Visibility ,
989
-
990
997
// The current set of local scopes, for values.
991
998
// FIXME #4948: Reuse ribs to avoid allocation.
992
999
value_ribs : Vec < Rib < ' a > > ,
@@ -1079,15 +1086,12 @@ impl<'a> ResolverArenas<'a> {
1079
1086
}
1080
1087
1081
1088
impl < ' a > ty:: NodeIdTree for Resolver < ' a > {
1082
- fn is_descendant_of ( & self , node : NodeId , ancestor : NodeId ) -> bool {
1083
- let ancestor = self . definitions . local_def_id ( ancestor) ;
1084
- let mut module = * self . module_map . get ( & node) . unwrap ( ) ;
1085
- while module. def_id ( ) != Some ( ancestor) {
1086
- let module_parent = match self . get_nearest_normal_module_parent ( module) {
1087
- Some ( parent) => parent,
1089
+ fn is_descendant_of ( & self , mut node : NodeId , ancestor : NodeId ) -> bool {
1090
+ while node != ancestor {
1091
+ node = match self . module_map [ & node] . parent ( ) {
1092
+ Some ( parent) => parent. normal_ancestor_id ,
1088
1093
None => return false ,
1089
- } ;
1090
- module = module_parent;
1094
+ }
1091
1095
}
1092
1096
true
1093
1097
}
@@ -1149,8 +1153,7 @@ impl<'a> Resolver<'a> {
1149
1153
pub fn new ( session : & ' a Session , make_glob_map : MakeGlobMap , arenas : & ' a ResolverArenas < ' a > )
1150
1154
-> Resolver < ' a > {
1151
1155
let root_def_id = DefId :: local ( CRATE_DEF_INDEX ) ;
1152
- let graph_root =
1153
- ModuleS :: new ( NoParentLink , Some ( Def :: Mod ( root_def_id) ) , false ) ;
1156
+ let graph_root = ModuleS :: new ( NoParentLink , Some ( Def :: Mod ( root_def_id) ) , CRATE_NODE_ID ) ;
1154
1157
let graph_root = arenas. alloc_module ( graph_root) ;
1155
1158
let mut module_map = NodeMap ( ) ;
1156
1159
module_map. insert ( CRATE_NODE_ID , graph_root) ;
@@ -1173,7 +1176,6 @@ impl<'a> Resolver<'a> {
1173
1176
indeterminate_imports : Vec :: new ( ) ,
1174
1177
1175
1178
current_module : graph_root,
1176
- current_vis : ty:: Visibility :: Restricted ( ast:: CRATE_NODE_ID ) ,
1177
1179
value_ribs : vec ! [ Rib :: new( ModuleRibKind ( graph_root) ) ] ,
1178
1180
type_ribs : vec ! [ Rib :: new( ModuleRibKind ( graph_root) ) ] ,
1179
1181
label_ribs : Vec :: new ( ) ,
@@ -1217,21 +1219,20 @@ impl<'a> Resolver<'a> {
1217
1219
/// Entry point to crate resolution.
1218
1220
pub fn resolve_crate ( & mut self , krate : & Crate ) {
1219
1221
self . current_module = self . graph_root ;
1220
- self . current_vis = ty:: Visibility :: Restricted ( ast:: CRATE_NODE_ID ) ;
1221
1222
visit:: walk_crate ( self , krate) ;
1222
1223
1223
1224
check_unused:: check_crate ( self , krate) ;
1224
1225
self . report_privacy_errors ( ) ;
1225
1226
}
1226
1227
1227
- fn new_module ( & self , parent_link : ParentLink < ' a > , def : Option < Def > , external : bool )
1228
+ fn new_module ( & self , parent_link : ParentLink < ' a > , def : Option < Def > , normal_ancestor_id : NodeId )
1228
1229
-> Module < ' a > {
1229
- self . arenas . alloc_module ( ModuleS :: new ( parent_link, def, external ) )
1230
+ self . arenas . alloc_module ( ModuleS :: new ( parent_link, def, normal_ancestor_id ) )
1230
1231
}
1231
1232
1232
1233
fn new_extern_crate_module ( & self , parent_link : ParentLink < ' a > , def : Def , local_node_id : NodeId )
1233
1234
-> Module < ' a > {
1234
- let mut module = ModuleS :: new ( parent_link, Some ( def) , false ) ;
1235
+ let mut module = ModuleS :: new ( parent_link, Some ( def) , local_node_id ) ;
1235
1236
module. extern_crate_id = Some ( local_node_id) ;
1236
1237
self . arenas . modules . alloc ( module)
1237
1238
}
@@ -1473,35 +1474,6 @@ impl<'a> Resolver<'a> {
1473
1474
None
1474
1475
}
1475
1476
1476
- /// Returns the nearest normal module parent of the given module.
1477
- fn get_nearest_normal_module_parent ( & self , mut module : Module < ' a > ) -> Option < Module < ' a > > {
1478
- loop {
1479
- match module. parent_link {
1480
- NoParentLink => return None ,
1481
- ModuleParentLink ( new_module, _) |
1482
- BlockParentLink ( new_module, _) => {
1483
- let new_module = new_module;
1484
- if new_module. is_normal ( ) {
1485
- return Some ( new_module) ;
1486
- }
1487
- module = new_module;
1488
- }
1489
- }
1490
- }
1491
- }
1492
-
1493
- /// Returns the nearest normal module parent of the given module, or the
1494
- /// module itself if it is a normal module.
1495
- fn get_nearest_normal_module_parent_or_self ( & self , module : Module < ' a > ) -> Module < ' a > {
1496
- if module. is_normal ( ) {
1497
- return module;
1498
- }
1499
- match self . get_nearest_normal_module_parent ( module) {
1500
- None => module,
1501
- Some ( new_module) => new_module,
1502
- }
1503
- }
1504
-
1505
1477
/// Resolves a "module prefix". A module prefix is one or both of (a) `self::`;
1506
1478
/// (b) some chain of `super::`.
1507
1479
/// grammar: (SELF MOD_SEP ) ? (SUPER MOD_SEP) *
@@ -1514,22 +1486,19 @@ impl<'a> Resolver<'a> {
1514
1486
"super" => 0 ,
1515
1487
_ => return Success ( NoPrefixFound ) ,
1516
1488
} ;
1517
- let mut containing_module =
1518
- self . get_nearest_normal_module_parent_or_self ( self . current_module ) ;
1489
+
1490
+ let mut containing_module = self . module_map [ & self . current_module . normal_ancestor_id ] ;
1519
1491
1520
1492
// Now loop through all the `super`s we find.
1521
1493
while i < module_path. len ( ) && "super" == module_path[ i] . as_str ( ) {
1522
1494
debug ! ( "(resolving module prefix) resolving `super` at {}" ,
1523
1495
module_to_string( & containing_module) ) ;
1524
- match self . get_nearest_normal_module_parent ( containing_module) {
1525
- None => {
1526
- let msg = "There are too many initial `super`s." . into ( ) ;
1527
- return Failed ( span. map ( |span| ( span, msg) ) ) ;
1528
- }
1529
- Some ( new_module) => {
1530
- containing_module = new_module;
1531
- i += 1 ;
1532
- }
1496
+ if let Some ( parent) = containing_module. parent ( ) {
1497
+ containing_module = self . module_map [ & parent. normal_ancestor_id ] ;
1498
+ i += 1 ;
1499
+ } else {
1500
+ let msg = "There are too many initial `super`s." . into ( ) ;
1501
+ return Failed ( span. map ( |span| ( span, msg) ) ) ;
1533
1502
}
1534
1503
}
1535
1504
@@ -1564,14 +1533,12 @@ impl<'a> Resolver<'a> {
1564
1533
if let Some ( module) = module {
1565
1534
// Move down in the graph.
1566
1535
let orig_module = replace ( & mut self . current_module , module) ;
1567
- let orig_vis = replace ( & mut self . current_vis , ty:: Visibility :: Restricted ( id) ) ;
1568
1536
self . value_ribs . push ( Rib :: new ( ModuleRibKind ( module) ) ) ;
1569
1537
self . type_ribs . push ( Rib :: new ( ModuleRibKind ( module) ) ) ;
1570
1538
1571
1539
f ( self ) ;
1572
1540
1573
1541
self . current_module = orig_module;
1574
- self . current_vis = orig_vis;
1575
1542
self . value_ribs . pop ( ) ;
1576
1543
self . type_ribs . pop ( ) ;
1577
1544
} else {
@@ -3248,16 +3215,17 @@ impl<'a> Resolver<'a> {
3248
3215
ast:: Visibility :: Public => return ty:: Visibility :: Public ,
3249
3216
ast:: Visibility :: Crate ( _) => return ty:: Visibility :: Restricted ( ast:: CRATE_NODE_ID ) ,
3250
3217
ast:: Visibility :: Restricted { ref path, id } => ( path, id) ,
3251
- ast:: Visibility :: Inherited => return self . current_vis ,
3218
+ ast:: Visibility :: Inherited => {
3219
+ return ty:: Visibility :: Restricted ( self . current_module . normal_ancestor_id ) ;
3220
+ }
3252
3221
} ;
3253
3222
3254
3223
let segments: Vec < _ > = path. segments . iter ( ) . map ( |seg| seg. identifier . name ) . collect ( ) ;
3255
3224
let mut path_resolution = err_path_resolution ( ) ;
3256
3225
let vis = match self . resolve_module_path ( & segments, DontUseLexicalScope , Some ( path. span ) ) {
3257
3226
Success ( module) => {
3258
- let def = module. def . unwrap ( ) ;
3259
- path_resolution = PathResolution :: new ( def) ;
3260
- ty:: Visibility :: Restricted ( self . definitions . as_local_node_id ( def. def_id ( ) ) . unwrap ( ) )
3227
+ path_resolution = PathResolution :: new ( module. def . unwrap ( ) ) ;
3228
+ ty:: Visibility :: Restricted ( module. normal_ancestor_id )
3261
3229
}
3262
3230
Indeterminate => unreachable ! ( ) ,
3263
3231
Failed ( err) => {
@@ -3276,7 +3244,7 @@ impl<'a> Resolver<'a> {
3276
3244
}
3277
3245
3278
3246
fn is_accessible ( & self , vis : ty:: Visibility ) -> bool {
3279
- vis. is_at_least ( self . current_vis , self )
3247
+ vis. is_accessible_from ( self . current_module . normal_ancestor_id , self )
3280
3248
}
3281
3249
3282
3250
fn report_privacy_errors ( & self ) {
0 commit comments