@@ -30,7 +30,7 @@ use syntax::codemap::Span;
30
30
use syntax:: util:: lev_distance:: find_best_match_for_name;
31
31
32
32
use std:: mem:: replace;
33
- use std:: cell:: Cell ;
33
+ use std:: cell:: { Cell , RefCell } ;
34
34
35
35
/// Contains data for specific types of import directives.
36
36
#[ derive( Clone , Debug ) ]
@@ -194,8 +194,8 @@ impl<'a> NameResolution<'a> {
194
194
None => return Some ( Indeterminate ) ,
195
195
} ;
196
196
let name = match directive. subclass {
197
- SingleImport { source, target , .. } if source == target => target ,
198
- _ => return Some ( Indeterminate ) ,
197
+ SingleImport { source, .. } => source ,
198
+ GlobImport => unreachable ! ( ) ,
199
199
} ;
200
200
match target_module. resolve_name ( name, ns, false ) {
201
201
Failed ( _) => { }
@@ -227,14 +227,19 @@ impl<'a> NameResolution<'a> {
227
227
}
228
228
229
229
impl < ' a > :: ModuleS < ' a > {
230
+ fn resolution ( & self , name : Name , ns : Namespace ) -> & ' a RefCell < NameResolution < ' a > > {
231
+ * self . resolutions . borrow_mut ( ) . entry ( ( name, ns) )
232
+ . or_insert_with ( || self . arenas . alloc_name_resolution ( ) )
233
+ }
234
+
230
235
pub fn resolve_name ( & self , name : Name , ns : Namespace , allow_private_imports : bool )
231
236
-> ResolveResult < & ' a NameBinding < ' a > > {
232
- let resolutions = match self . resolutions . borrow_state ( ) {
233
- :: std:: cell:: BorrowState :: Unused => self . resolutions . borrow ( ) ,
234
- _ => return Failed ( None ) , // This happens when there is a cycle of glob imports
237
+ let resolution = self . resolution ( name, ns) ;
238
+ let resolution = match resolution. borrow_state ( ) {
239
+ :: std:: cell:: BorrowState :: Unused => resolution. borrow_mut ( ) ,
240
+ _ => return Failed ( None ) , // This happens when there is a cycle of imports
235
241
} ;
236
242
237
- let resolution = resolutions. get ( & ( name, ns) ) . cloned ( ) . unwrap_or_default ( ) ;
238
243
if let Some ( result) = resolution. try_result ( ns, allow_private_imports) {
239
244
// If the resolution doesn't depend on glob definability, check privacy and return.
240
245
return result. and_then ( |binding| {
@@ -261,7 +266,7 @@ impl<'a> ::ModuleS<'a> {
261
266
// Invariant: this may not be called until import resolution is complete.
262
267
pub fn resolve_name_in_lexical_scope ( & self , name : Name , ns : Namespace )
263
268
-> Option < & ' a NameBinding < ' a > > {
264
- self . resolutions . borrow ( ) . get ( & ( name, ns) ) . and_then ( |resolution| resolution . binding )
269
+ self . resolution ( name, ns) . borrow ( ) . binding
265
270
. or_else ( || self . prelude . borrow ( ) . and_then ( |prelude| {
266
271
prelude. resolve_name ( name, ns, false ) . success ( )
267
272
} ) )
@@ -296,10 +301,9 @@ impl<'a> ::ModuleS<'a> {
296
301
self . unresolved_imports . borrow_mut ( ) . push ( directive) ;
297
302
match directive. subclass {
298
303
SingleImport { target, .. } => {
299
- let mut resolutions = self . resolutions . borrow_mut ( ) ;
300
304
for & ns in & [ ValueNS , TypeNS ] {
301
- resolutions . entry ( ( target, ns) ) . or_insert_with ( Default :: default )
302
- . single_imports . add_directive ( directive) ;
305
+ self . resolution ( target, ns) . borrow_mut ( ) . single_imports
306
+ . add_directive ( directive) ;
303
307
}
304
308
}
305
309
// We don't add prelude imports to the globs since they only affect lexical scopes,
@@ -314,8 +318,7 @@ impl<'a> ::ModuleS<'a> {
314
318
fn update_resolution < T , F > ( & self , name : Name , ns : Namespace , update : F ) -> T
315
319
where F : FnOnce ( & mut NameResolution < ' a > ) -> T
316
320
{
317
- let mut resolutions = self . resolutions . borrow_mut ( ) ;
318
- let resolution = resolutions. entry ( ( name, ns) ) . or_insert_with ( Default :: default) ;
321
+ let mut resolution = & mut * self . resolution ( name, ns) . borrow_mut ( ) ;
319
322
let was_known = resolution. binding ( ) . is_some ( ) ;
320
323
321
324
let t = update ( resolution) ;
@@ -638,7 +641,7 @@ impl<'a, 'b:'a, 'tcx:'b> ImportResolver<'a, 'b, 'tcx> {
638
641
target_module. glob_importers . borrow_mut ( ) . push ( ( module_, directive) ) ;
639
642
640
643
for ( & ( name, ns) , resolution) in target_module. resolutions . borrow ( ) . iter ( ) {
641
- if let Some ( binding) = resolution. binding ( ) {
644
+ if let Some ( binding) = resolution. borrow ( ) . binding ( ) {
642
645
if binding. defined_with ( DefModifiers :: IMPORTABLE | DefModifiers :: PUBLIC ) {
643
646
let _ = module_. try_define_child ( name, ns, directive. import ( binding, None ) ) ;
644
647
}
@@ -666,6 +669,7 @@ impl<'a, 'b:'a, 'tcx:'b> ImportResolver<'a, 'b, 'tcx> {
666
669
667
670
let mut reexports = Vec :: new ( ) ;
668
671
for ( & ( name, ns) , resolution) in module. resolutions . borrow ( ) . iter ( ) {
672
+ let resolution = resolution. borrow ( ) ;
669
673
resolution. report_conflicts ( |b1, b2| {
670
674
self . resolver . report_conflict ( module, name, ns, b1, b2)
671
675
} ) ;
0 commit comments