@@ -21,7 +21,7 @@ use {names_to_string, module_to_string};
21
21
use { resolve_error, ResolutionError } ;
22
22
23
23
use rustc:: ty;
24
- use rustc:: lint;
24
+ use rustc:: lint:: builtin :: PRIVATE_IN_PUBLIC ;
25
25
use rustc:: hir:: def:: * ;
26
26
27
27
use syntax:: ast:: { NodeId , Name } ;
@@ -343,6 +343,25 @@ struct ImportResolver<'a, 'b: 'a> {
343
343
resolver : & ' a mut Resolver < ' b > ,
344
344
}
345
345
346
+ impl < ' a , ' b : ' a > :: std:: ops:: Deref for ImportResolver < ' a , ' b > {
347
+ type Target = Resolver < ' b > ;
348
+ fn deref ( & self ) -> & Resolver < ' b > {
349
+ self . resolver
350
+ }
351
+ }
352
+
353
+ impl < ' a , ' b : ' a > :: std:: ops:: DerefMut for ImportResolver < ' a , ' b > {
354
+ fn deref_mut ( & mut self ) -> & mut Resolver < ' b > {
355
+ self . resolver
356
+ }
357
+ }
358
+
359
+ impl < ' a , ' b : ' a > ty:: NodeIdTree for ImportResolver < ' a , ' b > {
360
+ fn is_descendant_of ( & self , node : NodeId , ancestor : NodeId ) -> bool {
361
+ self . resolver . is_descendant_of ( node, ancestor)
362
+ }
363
+ }
364
+
346
365
impl < ' a , ' b : ' a > ImportResolver < ' a , ' b > {
347
366
// Import resolution
348
367
//
@@ -360,31 +379,29 @@ impl<'a, 'b:'a> ImportResolver<'a, 'b> {
360
379
let mut errors = Vec :: new ( ) ;
361
380
362
381
loop {
363
- debug ! ( "(resolving imports) iteration {}, {} imports left" ,
364
- i,
365
- self . resolver. unresolved_imports) ;
382
+ debug ! ( "(resolving imports) iteration {}, {} imports left" , i, self . unresolved_imports) ;
366
383
367
384
// Attempt to resolve imports in all local modules.
368
- for module in self . resolver . arenas . local_modules ( ) . iter ( ) {
369
- self . resolver . current_module = module;
385
+ for module in self . arenas . local_modules ( ) . iter ( ) {
386
+ self . current_module = module;
370
387
self . resolve_imports_in_current_module ( & mut errors) ;
371
388
}
372
389
373
- if self . resolver . unresolved_imports == 0 {
390
+ if self . unresolved_imports == 0 {
374
391
debug ! ( "(resolving imports) success" ) ;
375
- for module in self . resolver . arenas . local_modules ( ) . iter ( ) {
392
+ for module in self . arenas . local_modules ( ) . iter ( ) {
376
393
self . finalize_resolutions_in ( module, false ) ;
377
394
}
378
395
break ;
379
396
}
380
397
381
- if self . resolver . unresolved_imports == prev_unresolved_imports {
398
+ if self . unresolved_imports == prev_unresolved_imports {
382
399
// resolving failed
383
400
// Report unresolved imports only if no hard error was already reported
384
401
// to avoid generating multiple errors on the same import.
385
402
// Imports that are still indeterminate at this point are actually blocked
386
403
// by errored imports, so there is no point reporting them.
387
- for module in self . resolver . arenas . local_modules ( ) . iter ( ) {
404
+ for module in self . arenas . local_modules ( ) . iter ( ) {
388
405
self . finalize_resolutions_in ( module, errors. len ( ) == 0 ) ;
389
406
}
390
407
for e in errors {
@@ -394,15 +411,15 @@ impl<'a, 'b:'a> ImportResolver<'a, 'b> {
394
411
}
395
412
396
413
i += 1 ;
397
- prev_unresolved_imports = self . resolver . unresolved_imports ;
414
+ prev_unresolved_imports = self . unresolved_imports ;
398
415
}
399
416
}
400
417
401
418
// Define a "dummy" resolution containing a Def::Err as a placeholder for a
402
419
// failed resolution
403
420
fn import_dummy_binding ( & self , source_module : Module < ' b > , directive : & ' b ImportDirective < ' b > ) {
404
421
if let SingleImport { target, .. } = directive. subclass {
405
- let dummy_binding = self . resolver . arenas . alloc_name_binding ( NameBinding {
422
+ let dummy_binding = self . arenas . alloc_name_binding ( NameBinding {
406
423
kind : NameBindingKind :: Def ( Def :: Err ) ,
407
424
span : DUMMY_SP ,
408
425
vis : ty:: Visibility :: Public ,
@@ -430,7 +447,7 @@ impl<'a, 'b:'a> ImportResolver<'a, 'b> {
430
447
/// Attempts to resolve imports for the given module only.
431
448
fn resolve_imports_in_current_module ( & mut self , errors : & mut Vec < ImportResolvingError < ' b > > ) {
432
449
let mut imports = Vec :: new ( ) ;
433
- let mut unresolved_imports = self . resolver . current_module . unresolved_imports . borrow_mut ( ) ;
450
+ let mut unresolved_imports = self . current_module . unresolved_imports . borrow_mut ( ) ;
434
451
:: std:: mem:: swap ( & mut imports, & mut unresolved_imports) ;
435
452
436
453
for import_directive in imports {
@@ -441,7 +458,7 @@ impl<'a, 'b:'a> ImportResolver<'a, 'b> {
441
458
None => ( import_directive. span , String :: new ( ) ) ,
442
459
} ;
443
460
errors. push ( ImportResolvingError {
444
- source_module : self . resolver . current_module ,
461
+ source_module : self . current_module ,
445
462
import_directive : import_directive,
446
463
span : span,
447
464
help : help,
@@ -450,8 +467,8 @@ impl<'a, 'b:'a> ImportResolver<'a, 'b> {
450
467
Indeterminate => unresolved_imports. push ( import_directive) ,
451
468
Success ( ( ) ) => {
452
469
// Decrement the count of unresolved imports.
453
- assert ! ( self . resolver . unresolved_imports >= 1 ) ;
454
- self . resolver . unresolved_imports -= 1 ;
470
+ assert ! ( self . unresolved_imports >= 1 ) ;
471
+ self . unresolved_imports -= 1 ;
455
472
}
456
473
}
457
474
}
@@ -465,13 +482,13 @@ impl<'a, 'b:'a> ImportResolver<'a, 'b> {
465
482
fn resolve_import ( & mut self , directive : & ' b ImportDirective < ' b > ) -> ResolveResult < ( ) > {
466
483
debug ! ( "(resolving import for module) resolving import `{}::...` in `{}`" ,
467
484
names_to_string( & directive. module_path) ,
468
- module_to_string( self . resolver . current_module) ) ;
485
+ module_to_string( self . current_module) ) ;
469
486
470
487
let target_module = match directive. target_module . get ( ) {
471
488
Some ( module) => module,
472
- _ => match self . resolver . resolve_module_path ( & directive. module_path ,
473
- DontUseLexicalScope ,
474
- directive. span ) {
489
+ _ => match self . resolve_module_path ( & directive. module_path ,
490
+ DontUseLexicalScope ,
491
+ directive. span ) {
475
492
Success ( module) => module,
476
493
Indeterminate => return Indeterminate ,
477
494
Failed ( err) => return Failed ( err) ,
@@ -486,12 +503,10 @@ impl<'a, 'b:'a> ImportResolver<'a, 'b> {
486
503
} ;
487
504
488
505
// We need to resolve both namespaces for this to succeed.
489
- let value_result =
490
- self . resolver . resolve_name_in_module ( target_module, source, ValueNS , false , true ) ;
491
- let type_result =
492
- self . resolver . resolve_name_in_module ( target_module, source, TypeNS , false , true ) ;
506
+ let value_result = self . resolve_name_in_module ( target_module, source, ValueNS , false , true ) ;
507
+ let type_result = self . resolve_name_in_module ( target_module, source, TypeNS , false , true ) ;
493
508
494
- let module_ = self . resolver . current_module ;
509
+ let module_ = self . current_module ;
495
510
let mut privacy_error = true ;
496
511
for & ( ns, result, determined) in & [ ( ValueNS , & value_result, value_determined) ,
497
512
( TypeNS , & type_result, type_determined) ] {
@@ -504,20 +519,20 @@ impl<'a, 'b:'a> ImportResolver<'a, 'b> {
504
519
}
505
520
Success ( binding) if !binding. is_importable ( ) => {
506
521
let msg = format ! ( "`{}` is not directly importable" , target) ;
507
- span_err ! ( self . resolver . session, directive. span, E0253 , "{}" , & msg) ;
522
+ span_err ! ( self . session, directive. span, E0253 , "{}" , & msg) ;
508
523
// Do not import this illegal binding. Import a dummy binding and pretend
509
524
// everything is fine
510
525
self . import_dummy_binding ( module_, directive) ;
511
526
return Success ( ( ) ) ;
512
527
}
513
- Success ( binding) if !self . resolver . is_accessible ( binding. vis ) => { }
528
+ Success ( binding) if !self . is_accessible ( binding. vis ) => { }
514
529
Success ( binding) if !determined. get ( ) => {
515
530
determined. set ( true ) ;
516
531
let imported_binding = directive. import ( binding) ;
517
532
let conflict = module_. try_define_child ( target, ns, imported_binding) ;
518
533
if let Err ( old_binding) = conflict {
519
534
let binding = & directive. import ( binding) ;
520
- self . resolver . report_conflict ( module_, target, ns, binding, old_binding) ;
535
+ self . report_conflict ( module_, target, ns, binding, old_binding) ;
521
536
}
522
537
privacy_error = false ;
523
538
}
@@ -556,39 +571,34 @@ impl<'a, 'b:'a> ImportResolver<'a, 'b> {
556
571
if privacy_error {
557
572
for & ( ns, result) in & [ ( ValueNS , & value_result) , ( TypeNS , & type_result) ] {
558
573
let binding = match * result { Success ( binding) => binding, _ => continue } ;
559
- self . resolver . privacy_errors . push ( PrivacyError ( directive. span , source, binding) ) ;
574
+ self . privacy_errors . push ( PrivacyError ( directive. span , source, binding) ) ;
560
575
let _ = module_. try_define_child ( target, ns, directive. import ( binding) ) ;
561
576
}
562
577
}
563
578
564
579
match ( & value_result, & type_result) {
565
- ( & Success ( binding) , _) if !binding. pseudo_vis ( )
566
- . is_at_least ( directive. vis , self . resolver ) &&
567
- self . resolver . is_accessible ( binding. vis ) => {
580
+ ( & Success ( binding) , _) if !binding. pseudo_vis ( ) . is_at_least ( directive. vis , self ) &&
581
+ self . is_accessible ( binding. vis ) => {
568
582
let msg = format ! ( "`{}` is private, and cannot be reexported" , source) ;
569
583
let note_msg = format ! ( "consider marking `{}` as `pub` in the imported module" ,
570
584
source) ;
571
- struct_span_err ! ( self . resolver . session, directive. span, E0364 , "{}" , & msg)
585
+ struct_span_err ! ( self . session, directive. span, E0364 , "{}" , & msg)
572
586
. span_note ( directive. span , & note_msg)
573
587
. emit ( ) ;
574
588
}
575
589
576
- ( _, & Success ( binding) ) if !binding. pseudo_vis ( )
577
- . is_at_least ( directive. vis , self . resolver ) &&
578
- self . resolver . is_accessible ( binding. vis ) => {
590
+ ( _, & Success ( binding) ) if !binding. pseudo_vis ( ) . is_at_least ( directive. vis , self ) &&
591
+ self . is_accessible ( binding. vis ) => {
579
592
if binding. is_extern_crate ( ) {
580
593
let msg = format ! ( "extern crate `{}` is private, and cannot be reexported \
581
594
(error E0364), consider declaring with `pub`",
582
595
source) ;
583
- self . resolver . session . add_lint ( lint:: builtin:: PRIVATE_IN_PUBLIC ,
584
- directive. id ,
585
- directive. span ,
586
- msg) ;
596
+ self . session . add_lint ( PRIVATE_IN_PUBLIC , directive. id , directive. span , msg) ;
587
597
} else {
588
598
let msg = format ! ( "`{}` is private, and cannot be reexported" , source) ;
589
599
let note_msg =
590
600
format ! ( "consider declaring type or module `{}` with `pub`" , source) ;
591
- struct_span_err ! ( self . resolver . session, directive. span, E0365 , "{}" , & msg)
601
+ struct_span_err ! ( self . session, directive. span, E0365 , "{}" , & msg)
592
602
. span_note ( directive. span , & note_msg)
593
603
. emit ( ) ;
594
604
}
@@ -605,7 +615,7 @@ impl<'a, 'b:'a> ImportResolver<'a, 'b> {
605
615
None => value_result. success ( ) . and_then ( NameBinding :: def) . unwrap ( ) ,
606
616
} ;
607
617
let path_resolution = PathResolution :: new ( def) ;
608
- self . resolver . def_map . insert ( directive. id , path_resolution) ;
618
+ self . def_map . insert ( directive. id , path_resolution) ;
609
619
610
620
debug ! ( "(resolving single import) successfully resolved import" ) ;
611
621
return Success ( ( ) ) ;
@@ -618,19 +628,19 @@ impl<'a, 'b:'a> ImportResolver<'a, 'b> {
618
628
fn resolve_glob_import ( & mut self , target_module : Module < ' b > , directive : & ' b ImportDirective < ' b > )
619
629
-> ResolveResult < ( ) > {
620
630
if let Some ( Def :: Trait ( _) ) = target_module. def {
621
- self . resolver . session . span_err ( directive. span , "items in traits are not importable." ) ;
631
+ self . session . span_err ( directive. span , "items in traits are not importable." ) ;
622
632
}
623
633
624
- let module_ = self . resolver . current_module ;
634
+ let module_ = self . current_module ;
625
635
if module_. def_id ( ) == target_module. def_id ( ) {
626
636
// This means we are trying to glob import a module into itself, and it is a no-go
627
637
let msg = "Cannot glob-import a module into itself." . into ( ) ;
628
638
return Failed ( Some ( ( directive. span , msg) ) ) ;
629
639
}
630
- self . resolver . populate_module_if_necessary ( target_module) ;
640
+ self . populate_module_if_necessary ( target_module) ;
631
641
632
642
if let GlobImport { is_prelude : true } = directive. subclass {
633
- self . resolver . prelude = Some ( target_module) ;
643
+ self . prelude = Some ( target_module) ;
634
644
return Success ( ( ) ) ;
635
645
}
636
646
@@ -651,7 +661,7 @@ impl<'a, 'b:'a> ImportResolver<'a, 'b> {
651
661
// Record the destination of this import
652
662
if let Some ( did) = target_module. def_id ( ) {
653
663
let resolution = PathResolution :: new ( Def :: Mod ( did) ) ;
654
- self . resolver . def_map . insert ( directive. id , resolution) ;
664
+ self . def_map . insert ( directive. id , resolution) ;
655
665
}
656
666
657
667
debug ! ( "(resolving glob import) successfully resolved import" ) ;
@@ -668,7 +678,7 @@ impl<'a, 'b:'a> ImportResolver<'a, 'b> {
668
678
for ( & ( name, ns) , resolution) in module. resolutions . borrow ( ) . iter ( ) {
669
679
let resolution = resolution. borrow ( ) ;
670
680
resolution. report_conflicts ( |b1, b2| {
671
- self . resolver . report_conflict ( module, name, ns, b1, b2)
681
+ self . report_conflict ( module, name, ns, b1, b2)
672
682
} ) ;
673
683
674
684
let binding = match resolution. binding {
@@ -685,20 +695,19 @@ impl<'a, 'b:'a> ImportResolver<'a, 'b> {
685
695
686
696
if let NameBindingKind :: Import { binding : orig_binding, directive, .. } = binding. kind {
687
697
if ns == TypeNS && orig_binding. is_variant ( ) &&
688
- !orig_binding. vis . is_at_least ( binding. vis , self . resolver ) {
698
+ !orig_binding. vis . is_at_least ( binding. vis , self ) {
689
699
let msg = format ! ( "variant `{}` is private, and cannot be reexported \
690
700
(error E0364), consider declaring its enum as `pub`",
691
701
name) ;
692
- let lint = lint:: builtin:: PRIVATE_IN_PUBLIC ;
693
- self . resolver . session . add_lint ( lint, directive. id , binding. span , msg) ;
702
+ self . session . add_lint ( PRIVATE_IN_PUBLIC , directive. id , binding. span , msg) ;
694
703
}
695
704
}
696
705
}
697
706
698
707
if reexports. len ( ) > 0 {
699
708
if let Some ( def_id) = module. def_id ( ) {
700
- let node_id = self . resolver . definitions . as_local_node_id ( def_id) . unwrap ( ) ;
701
- self . resolver . export_map . insert ( node_id, reexports) ;
709
+ let node_id = self . definitions . as_local_node_id ( def_id) . unwrap ( ) ;
710
+ self . export_map . insert ( node_id, reexports) ;
702
711
}
703
712
}
704
713
0 commit comments