@@ -758,11 +758,10 @@ pub struct ModuleS<'a> {
758
758
extern_crate_id : Option < NodeId > ,
759
759
760
760
resolutions : RefCell < HashMap < ( Name , Namespace ) , & ' a RefCell < NameResolution < ' a > > > > ,
761
- unresolved_imports : RefCell < Vec < & ' a ImportDirective < ' a > > > ,
762
761
763
762
no_implicit_prelude : Cell < bool > ,
764
763
765
- glob_importers : RefCell < Vec < ( Module < ' a > , & ' a ImportDirective < ' a > ) > > ,
764
+ glob_importers : RefCell < Vec < & ' a ImportDirective < ' a > > > ,
766
765
globs : RefCell < Vec < & ' a ImportDirective < ' a > > > ,
767
766
768
767
// Used to memoize the traits in this module for faster searches through all traits in scope.
@@ -772,29 +771,22 @@ pub struct ModuleS<'a> {
772
771
// access the children must be preceded with a
773
772
// `populate_module_if_necessary` call.
774
773
populated : Cell < bool > ,
775
-
776
- arenas : & ' a ResolverArenas < ' a > ,
777
774
}
778
775
779
776
pub type Module < ' a > = & ' a ModuleS < ' a > ;
780
777
781
778
impl < ' a > ModuleS < ' a > {
782
- fn new ( parent_link : ParentLink < ' a > ,
783
- def : Option < Def > ,
784
- external : bool ,
785
- arenas : & ' a ResolverArenas < ' a > ) -> Self {
779
+ fn new ( parent_link : ParentLink < ' a > , def : Option < Def > , external : bool ) -> Self {
786
780
ModuleS {
787
781
parent_link : parent_link,
788
782
def : def,
789
783
extern_crate_id : None ,
790
784
resolutions : RefCell :: new ( HashMap :: new ( ) ) ,
791
- unresolved_imports : RefCell :: new ( Vec :: new ( ) ) ,
792
785
no_implicit_prelude : Cell :: new ( false ) ,
793
786
glob_importers : RefCell :: new ( Vec :: new ( ) ) ,
794
787
globs : RefCell :: new ( ( Vec :: new ( ) ) ) ,
795
788
traits : RefCell :: new ( None ) ,
796
789
populated : Cell :: new ( !external) ,
797
- arenas : arenas
798
790
}
799
791
}
800
792
@@ -971,12 +963,19 @@ pub struct Resolver<'a> {
971
963
972
964
structs : FnvHashMap < DefId , Vec < Name > > ,
973
965
974
- // The number of imports that are currently unresolved.
975
- unresolved_imports : usize ,
966
+ // All imports known to succeed or fail.
967
+ determined_imports : Vec < & ' a ImportDirective < ' a > > ,
968
+
969
+ // All non-determined imports.
970
+ indeterminate_imports : Vec < & ' a ImportDirective < ' a > > ,
976
971
977
972
// The module that represents the current item scope.
978
973
current_module : Module < ' a > ,
979
974
975
+ // The visibility of `pub(self)` items in the current scope.
976
+ // Equivalently, the visibility required for an item to be accessible from the current scope.
977
+ current_vis : ty:: Visibility ,
978
+
980
979
// The current set of local scopes, for values.
981
980
// FIXME #4948: Reuse ribs to avoid allocation.
982
981
value_ribs : Vec < Rib < ' a > > ,
@@ -1140,7 +1139,7 @@ impl<'a> Resolver<'a> {
1140
1139
-> Resolver < ' a > {
1141
1140
let root_def_id = DefId :: local ( CRATE_DEF_INDEX ) ;
1142
1141
let graph_root =
1143
- ModuleS :: new ( NoParentLink , Some ( Def :: Mod ( root_def_id) ) , false , arenas ) ;
1142
+ ModuleS :: new ( NoParentLink , Some ( Def :: Mod ( root_def_id) ) , false ) ;
1144
1143
let graph_root = arenas. alloc_module ( graph_root) ;
1145
1144
let mut module_map = NodeMap ( ) ;
1146
1145
module_map. insert ( CRATE_NODE_ID , graph_root) ;
@@ -1159,9 +1158,11 @@ impl<'a> Resolver<'a> {
1159
1158
trait_item_map : FnvHashMap ( ) ,
1160
1159
structs : FnvHashMap ( ) ,
1161
1160
1162
- unresolved_imports : 0 ,
1161
+ determined_imports : Vec :: new ( ) ,
1162
+ indeterminate_imports : Vec :: new ( ) ,
1163
1163
1164
1164
current_module : graph_root,
1165
+ current_vis : ty:: Visibility :: Restricted ( ast:: CRATE_NODE_ID ) ,
1165
1166
value_ribs : vec ! [ Rib :: new( ModuleRibKind ( graph_root) ) ] ,
1166
1167
type_ribs : vec ! [ Rib :: new( ModuleRibKind ( graph_root) ) ] ,
1167
1168
label_ribs : Vec :: new ( ) ,
@@ -1205,6 +1206,7 @@ impl<'a> Resolver<'a> {
1205
1206
/// Entry point to crate resolution.
1206
1207
pub fn resolve_crate ( & mut self , krate : & Crate ) {
1207
1208
self . current_module = self . graph_root ;
1209
+ self . current_vis = ty:: Visibility :: Restricted ( ast:: CRATE_NODE_ID ) ;
1208
1210
visit:: walk_crate ( self , krate) ;
1209
1211
1210
1212
check_unused:: check_crate ( self , krate) ;
@@ -1213,12 +1215,12 @@ impl<'a> Resolver<'a> {
1213
1215
1214
1216
fn new_module ( & self , parent_link : ParentLink < ' a > , def : Option < Def > , external : bool )
1215
1217
-> Module < ' a > {
1216
- self . arenas . alloc_module ( ModuleS :: new ( parent_link, def, external, self . arenas ) )
1218
+ self . arenas . alloc_module ( ModuleS :: new ( parent_link, def, external) )
1217
1219
}
1218
1220
1219
1221
fn new_extern_crate_module ( & self , parent_link : ParentLink < ' a > , def : Def , local_node_id : NodeId )
1220
1222
-> Module < ' a > {
1221
- let mut module = ModuleS :: new ( parent_link, Some ( def) , false , self . arenas ) ;
1223
+ let mut module = ModuleS :: new ( parent_link, Some ( def) , false ) ;
1222
1224
module. extern_crate_id = Some ( local_node_id) ;
1223
1225
self . arenas . modules . alloc ( module)
1224
1226
}
@@ -1250,14 +1252,15 @@ impl<'a> Resolver<'a> {
1250
1252
mut search_module : Module < ' a > ,
1251
1253
module_path : & [ Name ] ,
1252
1254
index : usize ,
1253
- span : Span )
1255
+ span : Option < Span > )
1254
1256
-> ResolveResult < Module < ' a > > {
1255
- fn search_parent_externals ( needle : Name , module : Module ) -> Option < Module > {
1256
- match module. resolve_name ( needle, TypeNS , false ) {
1257
+ fn search_parent_externals < ' a > ( this : & mut Resolver < ' a > , needle : Name , module : Module < ' a > )
1258
+ -> Option < Module < ' a > > {
1259
+ match this. resolve_name_in_module ( module, needle, TypeNS , false , None ) {
1257
1260
Success ( binding) if binding. is_extern_crate ( ) => Some ( module) ,
1258
1261
_ => match module. parent_link {
1259
1262
ModuleParentLink ( ref parent, _) => {
1260
- search_parent_externals ( needle, parent)
1263
+ search_parent_externals ( this , needle, parent)
1261
1264
}
1262
1265
_ => None ,
1263
1266
} ,
@@ -1272,16 +1275,17 @@ impl<'a> Resolver<'a> {
1272
1275
// modules as we go.
1273
1276
while index < module_path_len {
1274
1277
let name = module_path[ index] ;
1275
- match self . resolve_name_in_module ( search_module, name, TypeNS , false , true ) {
1278
+ match self . resolve_name_in_module ( search_module, name, TypeNS , false , span ) {
1276
1279
Failed ( None ) => {
1277
1280
let segment_name = name. as_str ( ) ;
1278
1281
let module_name = module_to_string ( search_module) ;
1279
1282
let msg = if "???" == & module_name {
1280
- match search_parent_externals ( name, & self . current_module ) {
1283
+ let current_module = self . current_module ;
1284
+ match search_parent_externals ( self , name, current_module) {
1281
1285
Some ( module) => {
1282
1286
let path_str = names_to_string ( module_path) ;
1283
1287
let target_mod_str = module_to_string ( & module) ;
1284
- let current_mod_str = module_to_string ( & self . current_module ) ;
1288
+ let current_mod_str = module_to_string ( current_module) ;
1285
1289
1286
1290
let prefix = if target_mod_str == current_mod_str {
1287
1291
"self::" . to_string ( )
@@ -1297,7 +1301,7 @@ impl<'a> Resolver<'a> {
1297
1301
format ! ( "Could not find `{}` in `{}`" , segment_name, module_name)
1298
1302
} ;
1299
1303
1300
- return Failed ( Some ( ( span, msg) ) ) ;
1304
+ return Failed ( span . map ( |span| ( span, msg) ) ) ;
1301
1305
}
1302
1306
Failed ( err) => return Failed ( err) ,
1303
1307
Indeterminate => {
@@ -1310,11 +1314,10 @@ impl<'a> Resolver<'a> {
1310
1314
// Check to see whether there are type bindings, and, if
1311
1315
// so, whether there is a module within.
1312
1316
if let Some ( module_def) = binding. module ( ) {
1313
- self . check_privacy ( name, binding, span) ;
1314
1317
search_module = module_def;
1315
1318
} else {
1316
1319
let msg = format ! ( "Not a module `{}`" , name) ;
1317
- return Failed ( Some ( ( span, msg) ) ) ;
1320
+ return Failed ( span . map ( |span| ( span, msg) ) ) ;
1318
1321
}
1319
1322
}
1320
1323
}
@@ -1330,7 +1333,7 @@ impl<'a> Resolver<'a> {
1330
1333
fn resolve_module_path ( & mut self ,
1331
1334
module_path : & [ Name ] ,
1332
1335
use_lexical_scope : UseLexicalScopeFlag ,
1333
- span : Span )
1336
+ span : Option < Span > )
1334
1337
-> ResolveResult < Module < ' a > > {
1335
1338
if module_path. len ( ) == 0 {
1336
1339
return Success ( self . graph_root ) // Use the crate root
@@ -1367,7 +1370,7 @@ impl<'a> Resolver<'a> {
1367
1370
// first component of the path in the current lexical
1368
1371
// scope and then proceed to resolve below that.
1369
1372
let ident = ast:: Ident :: with_empty_ctxt ( module_path[ 0 ] ) ;
1370
- match self . resolve_ident_in_lexical_scope ( ident, TypeNS , true )
1373
+ match self . resolve_ident_in_lexical_scope ( ident, TypeNS , span )
1371
1374
. and_then ( LexicalScopeBinding :: module) {
1372
1375
None => return Failed ( None ) ,
1373
1376
Some ( containing_module) => {
@@ -1384,10 +1387,7 @@ impl<'a> Resolver<'a> {
1384
1387
}
1385
1388
}
1386
1389
1387
- self . resolve_module_path_from_root ( search_module,
1388
- module_path,
1389
- start_index,
1390
- span)
1390
+ self . resolve_module_path_from_root ( search_module, module_path, start_index, span)
1391
1391
}
1392
1392
1393
1393
/// This resolves the identifier `ident` in the namespace `ns` in the current lexical scope.
@@ -1410,7 +1410,7 @@ impl<'a> Resolver<'a> {
1410
1410
fn resolve_ident_in_lexical_scope ( & mut self ,
1411
1411
mut ident : ast:: Ident ,
1412
1412
ns : Namespace ,
1413
- record_used : bool )
1413
+ record_used : Option < Span > )
1414
1414
-> Option < LexicalScopeBinding < ' a > > {
1415
1415
if ns == TypeNS {
1416
1416
ident = ast:: Ident :: with_empty_ctxt ( ident. name ) ;
@@ -1438,8 +1438,8 @@ impl<'a> Resolver<'a> {
1438
1438
if module. def . is_some ( ) {
1439
1439
return match self . prelude {
1440
1440
Some ( prelude) if !module. no_implicit_prelude . get ( ) => {
1441
- prelude . resolve_name ( name, ns, false ) . success ( )
1442
- . map ( LexicalScopeBinding :: Item )
1441
+ self . resolve_name_in_module ( prelude , name, ns, false , None ) . success ( )
1442
+ . map ( LexicalScopeBinding :: Item )
1443
1443
}
1444
1444
_ => None ,
1445
1445
} ;
@@ -1491,7 +1491,7 @@ impl<'a> Resolver<'a> {
1491
1491
/// Resolves a "module prefix". A module prefix is one or both of (a) `self::`;
1492
1492
/// (b) some chain of `super::`.
1493
1493
/// grammar: (SELF MOD_SEP ) ? (SUPER MOD_SEP) *
1494
- fn resolve_module_prefix ( & mut self , module_path : & [ Name ] , span : Span )
1494
+ fn resolve_module_prefix ( & mut self , module_path : & [ Name ] , span : Option < Span > )
1495
1495
-> ResolveResult < ModulePrefixResult < ' a > > {
1496
1496
// Start at the current module if we see `self` or `super`, or at the
1497
1497
// top of the crate otherwise.
@@ -1510,7 +1510,7 @@ impl<'a> Resolver<'a> {
1510
1510
match self . get_nearest_normal_module_parent ( containing_module) {
1511
1511
None => {
1512
1512
let msg = "There are too many initial `super`s." . into ( ) ;
1513
- return Failed ( Some ( ( span, msg) ) ) ;
1513
+ return Failed ( span . map ( |span| ( span, msg) ) ) ;
1514
1514
}
1515
1515
Some ( new_module) => {
1516
1516
containing_module = new_module;
@@ -1525,27 +1525,6 @@ impl<'a> Resolver<'a> {
1525
1525
return Success ( PrefixFound ( containing_module, i) ) ;
1526
1526
}
1527
1527
1528
- /// Attempts to resolve the supplied name in the given module for the
1529
- /// given namespace. If successful, returns the binding corresponding to
1530
- /// the name.
1531
- fn resolve_name_in_module ( & mut self ,
1532
- module : Module < ' a > ,
1533
- name : Name ,
1534
- namespace : Namespace ,
1535
- use_lexical_scope : bool ,
1536
- record_used : bool )
1537
- -> ResolveResult < & ' a NameBinding < ' a > > {
1538
- debug ! ( "(resolving name in module) resolving `{}` in `{}`" , name, module_to_string( module) ) ;
1539
-
1540
- self . populate_module_if_necessary ( module) ;
1541
- module. resolve_name ( name, namespace, use_lexical_scope) . and_then ( |binding| {
1542
- if record_used {
1543
- self . record_use ( name, namespace, binding) ;
1544
- }
1545
- Success ( binding)
1546
- } )
1547
- }
1548
-
1549
1528
// AST resolution
1550
1529
//
1551
1530
// We maintain a list of value ribs and type ribs.
@@ -1570,13 +1549,15 @@ impl<'a> Resolver<'a> {
1570
1549
let module = self . module_map . get ( & id) . cloned ( ) ; // clones a reference
1571
1550
if let Some ( module) = module {
1572
1551
// Move down in the graph.
1573
- let orig_module = :: std:: mem:: replace ( & mut self . current_module , module) ;
1552
+ let orig_module = replace ( & mut self . current_module , module) ;
1553
+ let orig_vis = replace ( & mut self . current_vis , ty:: Visibility :: Restricted ( id) ) ;
1574
1554
self . value_ribs . push ( Rib :: new ( ModuleRibKind ( module) ) ) ;
1575
1555
self . type_ribs . push ( Rib :: new ( ModuleRibKind ( module) ) ) ;
1576
1556
1577
1557
f ( self ) ;
1578
1558
1579
1559
self . current_module = orig_module;
1560
+ self . current_vis = orig_vis;
1580
1561
self . value_ribs . pop ( ) ;
1581
1562
self . type_ribs . pop ( ) ;
1582
1563
} else {
@@ -2314,7 +2295,7 @@ impl<'a> Resolver<'a> {
2314
2295
PatKind :: Ident ( bmode, ref ident, ref opt_pat) => {
2315
2296
// First try to resolve the identifier as some existing
2316
2297
// entity, then fall back to a fresh binding.
2317
- let binding = self . resolve_ident_in_lexical_scope ( ident. node , ValueNS , false )
2298
+ let binding = self . resolve_ident_in_lexical_scope ( ident. node , ValueNS , None )
2318
2299
. and_then ( LexicalScopeBinding :: item) ;
2319
2300
let resolution = binding. and_then ( NameBinding :: def) . and_then ( |def| {
2320
2301
let always_binding = !pat_src. is_refutable ( ) || opt_pat. is_some ( ) ||
@@ -2481,11 +2462,11 @@ impl<'a> Resolver<'a> {
2481
2462
//
2482
2463
// Such behavior is required for backward compatibility.
2483
2464
// The same fallback is used when `a` resolves to nothing.
2484
- let def = resolve_identifier_with_fallback ( self , true ) . ok_or ( false ) ;
2465
+ let def = resolve_identifier_with_fallback ( self , Some ( span ) ) . ok_or ( false ) ;
2485
2466
return def. and_then ( |def| self . adjust_local_def ( def, span) . ok_or ( true ) ) . map ( mk_res) ;
2486
2467
}
2487
2468
2488
- let unqualified_def = resolve_identifier_with_fallback ( self , false ) ;
2469
+ let unqualified_def = resolve_identifier_with_fallback ( self , None ) ;
2489
2470
let qualified_binding = self . resolve_module_relative_path ( span, segments, namespace) ;
2490
2471
match ( qualified_binding, unqualified_def) {
2491
2472
( Ok ( binding) , Some ( ref ud) ) if binding. def ( ) . unwrap ( ) == ud. def => {
@@ -2505,7 +2486,7 @@ impl<'a> Resolver<'a> {
2505
2486
fn resolve_identifier ( & mut self ,
2506
2487
identifier : ast:: Ident ,
2507
2488
namespace : Namespace ,
2508
- record_used : bool )
2489
+ record_used : Option < Span > )
2509
2490
-> Option < LocalDef > {
2510
2491
if identifier. name == keywords:: Invalid . name ( ) {
2511
2492
return None ;
@@ -2619,7 +2600,7 @@ impl<'a> Resolver<'a> {
2619
2600
. collect :: < Vec < _ > > ( ) ;
2620
2601
2621
2602
let containing_module;
2622
- match self . resolve_module_path ( & module_path, UseLexicalScope , span) {
2603
+ match self . resolve_module_path ( & module_path, UseLexicalScope , Some ( span) ) {
2623
2604
Failed ( err) => {
2624
2605
let ( span, msg) = match err {
2625
2606
Some ( ( span, msg) ) => ( span, msg) ,
@@ -2640,11 +2621,9 @@ impl<'a> Resolver<'a> {
2640
2621
}
2641
2622
2642
2623
let name = segments. last ( ) . unwrap ( ) . identifier . name ;
2643
- let result = self . resolve_name_in_module ( containing_module, name, namespace, false , true ) ;
2644
- result. success ( ) . map ( |binding| {
2645
- self . check_privacy ( name, binding, span) ;
2646
- binding
2647
- } ) . ok_or ( false )
2624
+ let result =
2625
+ self . resolve_name_in_module ( containing_module, name, namespace, false , Some ( span) ) ;
2626
+ result. success ( ) . ok_or ( false )
2648
2627
}
2649
2628
2650
2629
/// Invariant: This must be called only during main resolution, not during
@@ -2658,10 +2637,7 @@ impl<'a> Resolver<'a> {
2658
2637
let root_module = self . graph_root ;
2659
2638
2660
2639
let containing_module;
2661
- match self . resolve_module_path_from_root ( root_module,
2662
- & module_path,
2663
- 0 ,
2664
- span) {
2640
+ match self . resolve_module_path_from_root ( root_module, & module_path, 0 , Some ( span) ) {
2665
2641
Failed ( err) => {
2666
2642
let ( span, msg) = match err {
2667
2643
Some ( ( span, msg) ) => ( span, msg) ,
@@ -2684,11 +2660,9 @@ impl<'a> Resolver<'a> {
2684
2660
}
2685
2661
2686
2662
let name = segments. last ( ) . unwrap ( ) . name ( ) ;
2687
- let result = self . resolve_name_in_module ( containing_module, name, namespace, false , true ) ;
2688
- result. success ( ) . map ( |binding| {
2689
- self . check_privacy ( name, binding, span) ;
2690
- binding
2691
- } ) . ok_or ( false )
2663
+ let result =
2664
+ self . resolve_name_in_module ( containing_module, name, namespace, false , Some ( span) ) ;
2665
+ result. success ( ) . ok_or ( false )
2692
2666
}
2693
2667
2694
2668
fn with_no_errors < T , F > ( & mut self , f : F ) -> T
@@ -2716,7 +2690,6 @@ impl<'a> Resolver<'a> {
2716
2690
fn with_empty_ribs < T , F > ( & mut self , f : F ) -> T
2717
2691
where F : FnOnce ( & mut Resolver < ' a > ) -> T ,
2718
2692
{
2719
- use :: std:: mem:: replace;
2720
2693
let value_ribs = replace ( & mut self . value_ribs , Vec :: new ( ) ) ;
2721
2694
let type_ribs = replace ( & mut self . type_ribs , Vec :: new ( ) ) ;
2722
2695
let label_ribs = replace ( & mut self . label_ribs , Vec :: new ( ) ) ;
@@ -2941,7 +2914,7 @@ impl<'a> Resolver<'a> {
2941
2914
2942
2915
match self . resolve_module_path ( & name_path[ ..] ,
2943
2916
UseLexicalScope ,
2944
- expr. span ) {
2917
+ Some ( expr. span ) ) {
2945
2918
Success ( e) => {
2946
2919
if let Some ( def_type) = e. def {
2947
2920
def = def_type;
@@ -3274,18 +3247,12 @@ impl<'a> Resolver<'a> {
3274
3247
ast:: Visibility :: Public => return ty:: Visibility :: Public ,
3275
3248
ast:: Visibility :: Crate ( _) => return ty:: Visibility :: Restricted ( ast:: CRATE_NODE_ID ) ,
3276
3249
ast:: Visibility :: Restricted { ref path, id } => ( path, id) ,
3277
- ast:: Visibility :: Inherited => {
3278
- let current_module =
3279
- self . get_nearest_normal_module_parent_or_self ( self . current_module ) ;
3280
- let id =
3281
- self . definitions . as_local_node_id ( current_module. def_id ( ) . unwrap ( ) ) . unwrap ( ) ;
3282
- return ty:: Visibility :: Restricted ( id) ;
3283
- }
3250
+ ast:: Visibility :: Inherited => return self . current_vis ,
3284
3251
} ;
3285
3252
3286
3253
let segments: Vec < _ > = path. segments . iter ( ) . map ( |seg| seg. identifier . name ) . collect ( ) ;
3287
3254
let mut path_resolution = err_path_resolution ( ) ;
3288
- let vis = match self . resolve_module_path ( & segments, DontUseLexicalScope , path. span ) {
3255
+ let vis = match self . resolve_module_path ( & segments, DontUseLexicalScope , Some ( path. span ) ) {
3289
3256
Success ( module) => {
3290
3257
let def = module. def . unwrap ( ) ;
3291
3258
path_resolution = PathResolution :: new ( def) ;
@@ -3309,15 +3276,7 @@ impl<'a> Resolver<'a> {
3309
3276
}
3310
3277
3311
3278
fn is_accessible ( & self , vis : ty:: Visibility ) -> bool {
3312
- let current_module = self . get_nearest_normal_module_parent_or_self ( self . current_module ) ;
3313
- let node_id = self . definitions . as_local_node_id ( current_module. def_id ( ) . unwrap ( ) ) . unwrap ( ) ;
3314
- vis. is_accessible_from ( node_id, self )
3315
- }
3316
-
3317
- fn check_privacy ( & mut self , name : Name , binding : & ' a NameBinding < ' a > , span : Span ) {
3318
- if !self . is_accessible ( binding. vis ) {
3319
- self . privacy_errors . push ( PrivacyError ( span, name, binding) ) ;
3320
- }
3279
+ vis. is_at_least ( self . current_vis , self )
3321
3280
}
3322
3281
3323
3282
fn report_privacy_errors ( & self ) {
0 commit comments