@@ -101,39 +101,30 @@ impl ImportDirective {
101
101
}
102
102
103
103
#[ derive( Debug ) ]
104
- /// An ImportResolution records what we know about an imported name in a given namespace.
104
+ /// An NameResolution records what we know about an imported name in a given namespace.
105
105
/// More specifically, it records the number of unresolved `use` directives that import the name,
106
106
/// the `use` directive importing the name in the namespace, and the `NameBinding` to which the
107
107
/// name in the namespace resolves (if applicable).
108
108
/// Different `use` directives may import the same name in different namespaces.
109
- pub struct ImportResolution < ' a > {
109
+ pub struct NameResolution < ' a > {
110
110
// When outstanding_references reaches zero, outside modules can count on the targets being
111
111
// correct. Before then, all bets are off; future `use` directives could override the name.
112
112
// Since shadowing is forbidden, the only way outstanding_references > 1 in a legal program
113
113
// is if the name is imported by exactly two `use` directives, one of which resolves to a
114
114
// value and the other of which resolves to a type.
115
115
pub outstanding_references : usize ,
116
116
117
- /// Whether this resolution came from a `use` or a `pub use`.
118
- pub is_public : bool ,
119
-
120
117
/// Resolution of the name in the namespace
121
118
pub binding : Option < & ' a NameBinding < ' a > > ,
122
-
123
- /// The source node of the `use` directive
124
- pub id : NodeId ,
125
119
}
126
120
127
- impl < ' a > ImportResolution < ' a > {
128
- pub fn new ( id : NodeId , is_public : bool ) -> Self {
129
- ImportResolution {
130
- outstanding_references : 0 ,
131
- id : id,
132
- binding : None ,
133
- is_public : is_public,
134
- }
121
+ impl < ' a > Default for NameResolution < ' a > {
122
+ fn default ( ) -> Self {
123
+ NameResolution { outstanding_references : 0 , binding : None }
135
124
}
125
+ }
136
126
127
+ impl < ' a > NameResolution < ' a > {
137
128
pub fn shadowable ( & self ) -> Shadowable {
138
129
match self . binding {
139
130
Some ( binding) if binding. defined_with ( DefModifiers :: PRELUDE ) =>
@@ -216,8 +207,7 @@ impl<'a, 'b:'a, 'tcx:'b> ImportResolver<'a, 'b, 'tcx> {
216
207
debug ! ( "(resolving import error) adding import resolution for `{}`" ,
217
208
target) ;
218
209
219
- ImportResolution :: new ( e. import_directive . id ,
220
- e. import_directive . is_public )
210
+ NameResolution :: default ( )
221
211
} ) ;
222
212
223
213
if resolution. binding . is_none ( ) {
@@ -402,13 +392,13 @@ impl<'a, 'b:'a, 'tcx:'b> ImportResolver<'a, 'b, 'tcx> {
402
392
403
393
// The name is an import which has been fully resolved, so we just follow it.
404
394
Some ( resolution) if resolution. outstanding_references == 0 => {
405
- // Import resolutions must be declared with "pub" in order to be exported.
406
- if !resolution. is_public {
407
- return Failed ( None ) ;
408
- }
409
-
410
395
if let Some ( binding) = resolution. binding {
411
- self . resolver . record_import_use ( name, ns, & resolution) ;
396
+ // Import resolutions must be declared with "pub" in order to be exported.
397
+ if !binding. is_public ( ) {
398
+ return Failed ( None ) ;
399
+ }
400
+
401
+ self . resolver . record_import_use ( name, ns, binding) ;
412
402
Success ( binding)
413
403
} else {
414
404
Failed ( None )
@@ -549,10 +539,8 @@ impl<'a, 'b:'a, 'tcx:'b> ImportResolver<'a, 'b, 'tcx> {
549
539
550
540
import_resolution. binding =
551
541
Some ( self . resolver . new_name_binding ( directive. import ( name_binding) ) ) ;
552
- import_resolution. id = directive. id ;
553
- import_resolution. is_public = directive. is_public ;
554
542
555
- self . add_export ( module_, target, & import_resolution) ;
543
+ self . add_export ( module_, target, import_resolution. binding . unwrap ( ) ) ;
556
544
}
557
545
Failed ( _) => {
558
546
// Continue.
@@ -644,7 +632,6 @@ impl<'a, 'b:'a, 'tcx:'b> ImportResolver<'a, 'b, 'tcx> {
644
632
lp : LastPrivate )
645
633
-> ResolveResult < ( ) > {
646
634
let id = import_directive. id ;
647
- let is_public = import_directive. is_public ;
648
635
649
636
// This function works in a highly imperative manner; it eagerly adds
650
637
// everything it can to the list of import resolutions of the module
@@ -679,19 +666,17 @@ impl<'a, 'b:'a, 'tcx:'b> ImportResolver<'a, 'b, 'tcx> {
679
666
let mut import_resolutions = module_. import_resolutions . borrow_mut ( ) ;
680
667
let mut dest_import_resolution =
681
668
import_resolutions. entry ( ( name, ns) )
682
- . or_insert_with ( || ImportResolution :: new ( id , is_public ) ) ;
669
+ . or_insert_with ( || NameResolution :: default ( ) ) ;
683
670
684
671
match target_import_resolution. binding {
685
- Some ( binding) if target_import_resolution . is_public => {
672
+ Some ( binding) if binding . is_public ( ) => {
686
673
self . check_for_conflicting_import ( & dest_import_resolution,
687
674
import_directive. span ,
688
675
name,
689
676
ns) ;
690
- dest_import_resolution. id = id;
691
- dest_import_resolution. is_public = is_public;
692
677
dest_import_resolution. binding =
693
678
Some ( self . resolver . new_name_binding ( import_directive. import ( binding) ) ) ;
694
- self . add_export ( module_, name, & dest_import_resolution) ;
679
+ self . add_export ( module_, name, dest_import_resolution. binding . unwrap ( ) ) ;
695
680
}
696
681
_ => { }
697
682
}
@@ -728,12 +713,11 @@ impl<'a, 'b:'a, 'tcx:'b> ImportResolver<'a, 'b, 'tcx> {
728
713
import_directive : & ImportDirective ,
729
714
( name, ns) : ( Name , Namespace ) ,
730
715
name_binding : & ' b NameBinding < ' b > ) {
731
- let id = import_directive. id ;
732
716
let is_public = import_directive. is_public ;
733
717
734
718
let mut import_resolutions = module_. import_resolutions . borrow_mut ( ) ;
735
719
let dest_import_resolution = import_resolutions. entry ( ( name, ns) ) . or_insert_with ( || {
736
- ImportResolution :: new ( id , is_public )
720
+ NameResolution :: default ( )
737
721
} ) ;
738
722
739
723
debug ! ( "(resolving glob import) writing resolution `{}` in `{}` to `{}`" ,
@@ -767,9 +751,7 @@ impl<'a, 'b:'a, 'tcx:'b> ImportResolver<'a, 'b, 'tcx> {
767
751
} else {
768
752
dest_import_resolution. binding =
769
753
Some ( self . resolver . new_name_binding ( import_directive. import ( name_binding) ) ) ;
770
- dest_import_resolution. id = id;
771
- dest_import_resolution. is_public = is_public;
772
- self . add_export ( module_, name, & dest_import_resolution) ;
754
+ self . add_export ( module_, name, dest_import_resolution. binding . unwrap ( ) ) ;
773
755
}
774
756
}
775
757
@@ -779,13 +761,13 @@ impl<'a, 'b:'a, 'tcx:'b> ImportResolver<'a, 'b, 'tcx> {
779
761
( name, ns) ) ;
780
762
}
781
763
782
- fn add_export ( & mut self , module : Module < ' b > , name : Name , resolution : & ImportResolution < ' b > ) {
783
- if !resolution . is_public { return }
764
+ fn add_export ( & mut self , module : Module < ' b > , name : Name , binding : & NameBinding < ' b > ) {
765
+ if !binding . is_public ( ) { return }
784
766
let node_id = match module. def_id ( ) {
785
767
Some ( def_id) => self . resolver . ast_map . as_local_node_id ( def_id) . unwrap ( ) ,
786
768
None => return ,
787
769
} ;
788
- let export = match resolution . binding . as_ref ( ) . unwrap ( ) . def ( ) {
770
+ let export = match binding. def ( ) {
789
771
Some ( def) => Export { name : name, def_id : def. def_id ( ) } ,
790
772
None => return ,
791
773
} ;
@@ -794,7 +776,7 @@ impl<'a, 'b:'a, 'tcx:'b> ImportResolver<'a, 'b, 'tcx> {
794
776
795
777
/// Checks that imported names and items don't have the same name.
796
778
fn check_for_conflicting_import ( & mut self ,
797
- import_resolution : & ImportResolution ,
779
+ import_resolution : & NameResolution ,
798
780
import_span : Span ,
799
781
name : Name ,
800
782
namespace : Namespace ) {
@@ -815,8 +797,6 @@ impl<'a, 'b:'a, 'tcx:'b> ImportResolver<'a, 'b, 'tcx> {
815
797
}
816
798
ValueNS => "value" ,
817
799
} ;
818
- let use_id = import_resolution. id ;
819
- let item = self . resolver . ast_map . expect_item ( use_id) ;
820
800
let mut err = struct_span_err ! ( self . resolver. session,
821
801
import_span,
822
802
E0252 ,
@@ -825,7 +805,7 @@ impl<'a, 'b:'a, 'tcx:'b> ImportResolver<'a, 'b, 'tcx> {
825
805
ns_word,
826
806
name) ;
827
807
span_note ! ( & mut err,
828
- item . span,
808
+ binding . span. unwrap ( ) ,
829
809
"previous import of `{}` here" ,
830
810
name) ;
831
811
err. emit ( ) ;
@@ -848,7 +828,7 @@ impl<'a, 'b:'a, 'tcx:'b> ImportResolver<'a, 'b, 'tcx> {
848
828
/// Checks that imported names and items don't have the same name.
849
829
fn check_for_conflicts_between_imports_and_items ( & mut self ,
850
830
module : Module < ' b > ,
851
- import : & ImportResolution < ' b > ,
831
+ import : & NameResolution < ' b > ,
852
832
import_span : Span ,
853
833
( name, ns) : ( Name , Namespace ) ) {
854
834
// Check for item conflicts.
0 commit comments