@@ -125,7 +125,8 @@ impl ImportDirective {
125
125
/// Records information about the resolution of a name in a module.
126
126
pub struct NameResolution < ' a > {
127
127
/// The number of unresolved single imports that could define the name.
128
- outstanding_references : usize ,
128
+ outstanding_references : u32 ,
129
+ pub_outstanding_references : u32 ,
129
130
/// The least shadowable known binding for this name, or None if there are no known bindings.
130
131
pub binding : Option < & ' a NameBinding < ' a > > ,
131
132
duplicate_globs : Vec < & ' a NameBinding < ' a > > ,
@@ -151,7 +152,7 @@ impl<'a> NameResolution<'a> {
151
152
}
152
153
153
154
// Returns the resolution of the name assuming no more globs will define it.
154
- fn result ( & self ) -> ResolveResult < & ' a NameBinding < ' a > > {
155
+ fn result ( & self , allow_private_imports : bool ) -> ResolveResult < & ' a NameBinding < ' a > > {
155
156
match self . binding {
156
157
Some ( binding) if !binding. defined_with ( DefModifiers :: GLOB_IMPORTED ) => Success ( binding) ,
157
158
_ if self . outstanding_references > 0 => Indeterminate ,
@@ -162,8 +163,9 @@ impl<'a> NameResolution<'a> {
162
163
163
164
// Returns Some(the resolution of the name), or None if the resolution depends
164
165
// on whether more globs can define the name.
165
- fn try_result ( & self ) -> Option < ResolveResult < & ' a NameBinding < ' a > > > {
166
- match self . result ( ) {
166
+ fn try_result ( & self , allow_private_imports : bool )
167
+ -> Option < ResolveResult < & ' a NameBinding < ' a > > > {
168
+ match self . result ( allow_private_imports) {
167
169
Success ( binding) if binding. defined_with ( DefModifiers :: PRELUDE ) => None ,
168
170
Failed ( _) => None ,
169
171
result @ _ => Some ( result) ,
@@ -200,7 +202,7 @@ impl<'a> ::ModuleS<'a> {
200
202
} ;
201
203
202
204
let resolution = resolutions. get ( & ( name, ns) ) . cloned ( ) . unwrap_or_default ( ) ;
203
- if let Some ( result) = resolution. try_result ( ) {
205
+ if let Some ( result) = resolution. try_result ( allow_private_imports ) {
204
206
// If the resolution doesn't depend on glob definability, check privacy and return.
205
207
return result. and_then ( |binding| {
206
208
let allowed = allow_private_imports || !binding. is_import ( ) || binding. is_public ( ) ;
@@ -234,7 +236,7 @@ impl<'a> ::ModuleS<'a> {
234
236
}
235
237
}
236
238
237
- resolution. result ( )
239
+ resolution. result ( true )
238
240
}
239
241
240
242
// Define the name or return the existing binding if there is a collision.
@@ -246,15 +248,26 @@ impl<'a> ::ModuleS<'a> {
246
248
} )
247
249
}
248
250
249
- pub fn increment_outstanding_references_for ( & self , name : Name , ns : Namespace ) {
251
+ pub fn increment_outstanding_references_for ( & self , name : Name , ns : Namespace , is_public : bool ) {
250
252
let mut resolutions = self . resolutions . borrow_mut ( ) ;
251
- resolutions. entry ( ( name, ns) ) . or_insert_with ( Default :: default) . outstanding_references += 1 ;
253
+ let resolution = resolutions. entry ( ( name, ns) ) . or_insert_with ( Default :: default) ;
254
+ resolution. outstanding_references += 1 ;
255
+ if is_public {
256
+ resolution. pub_outstanding_references += 1 ;
257
+ }
252
258
}
253
259
254
- fn decrement_outstanding_references_for ( & self , name : Name , ns : Namespace ) {
255
- self . update_resolution ( name, ns, |resolution| match resolution. outstanding_references {
256
- 0 => panic ! ( "No more outstanding references!" ) ,
257
- ref mut outstanding_references => * outstanding_references -= 1 ,
260
+ fn decrement_outstanding_references_for ( & self , name : Name , ns : Namespace , is_public : bool ) {
261
+ let decrement_references = |count : & mut _ | {
262
+ assert ! ( * count > 0 ) ;
263
+ * count -= 1 ;
264
+ } ;
265
+
266
+ self . update_resolution ( name, ns, |resolution| {
267
+ decrement_references ( & mut resolution. outstanding_references ) ;
268
+ if is_public {
269
+ decrement_references ( & mut resolution. pub_outstanding_references ) ;
270
+ }
258
271
} )
259
272
}
260
273
@@ -265,11 +278,11 @@ impl<'a> ::ModuleS<'a> {
265
278
{
266
279
let mut resolutions = self . resolutions . borrow_mut ( ) ;
267
280
let resolution = resolutions. entry ( ( name, ns) ) . or_insert_with ( Default :: default) ;
268
- let was_success = resolution. try_result ( ) . and_then ( ResolveResult :: success) . is_some ( ) ;
281
+ let was_success = resolution. try_result ( false ) . and_then ( ResolveResult :: success) . is_some ( ) ;
269
282
270
283
let t = update ( resolution) ;
271
284
if !was_success {
272
- if let Some ( Success ( binding) ) = resolution. try_result ( ) {
285
+ if let Some ( Success ( binding) ) = resolution. try_result ( false ) {
273
286
self . define_in_glob_importers ( name, ns, binding) ;
274
287
}
275
288
}
@@ -460,10 +473,14 @@ impl<'a, 'b:'a, 'tcx:'b> ImportResolver<'a, 'b, 'tcx> {
460
473
let mut resolve_in_ns = |ns, determined : bool | {
461
474
// Temporarily count the directive as determined so that the resolution fails
462
475
// (as opposed to being indeterminate) when it can only be defined by the directive.
463
- if !determined { module_. decrement_outstanding_references_for ( target, ns) }
476
+ if !determined {
477
+ module_. decrement_outstanding_references_for ( target, ns, directive. is_public )
478
+ }
464
479
let result =
465
480
self . resolver . resolve_name_in_module ( target_module, source, ns, false , true ) ;
466
- if !determined { module_. increment_outstanding_references_for ( target, ns) }
481
+ if !determined {
482
+ module_. increment_outstanding_references_for ( target, ns, directive. is_public )
483
+ }
467
484
result
468
485
} ;
469
486
( resolve_in_ns ( ValueNS , value_determined. get ( ) ) ,
@@ -494,7 +511,7 @@ impl<'a, 'b:'a, 'tcx:'b> ImportResolver<'a, 'b, 'tcx> {
494
511
self . report_conflict ( target, ns, & directive. import ( binding, None ) , old_binding) ;
495
512
}
496
513
}
497
- module_. decrement_outstanding_references_for ( target, ns) ;
514
+ module_. decrement_outstanding_references_for ( target, ns, directive . is_public ) ;
498
515
}
499
516
500
517
match ( & value_result, & type_result) {
@@ -601,7 +618,7 @@ impl<'a, 'b:'a, 'tcx:'b> ImportResolver<'a, 'b, 'tcx> {
601
618
}
602
619
603
620
for ( & ( name, ns) , resolution) in target_module. resolutions . borrow ( ) . iter ( ) {
604
- if let Some ( Success ( binding) ) = resolution. try_result ( ) {
621
+ if let Some ( Success ( binding) ) = resolution. try_result ( false ) {
605
622
if binding. defined_with ( DefModifiers :: IMPORTABLE | DefModifiers :: PUBLIC ) {
606
623
let _ = module_. try_define_child ( name, ns, directive. import ( binding, None ) ) ;
607
624
}
0 commit comments