@@ -57,13 +57,6 @@ impl ImportDirectiveSubclass {
57
57
}
58
58
}
59
59
60
- /// Whether an import can be shadowed by another import.
61
- #[ derive( Debug , PartialEq , Clone , Copy ) ]
62
- pub enum Shadowable {
63
- Always ,
64
- Never ,
65
- }
66
-
67
60
/// One import directive.
68
61
#[ derive( Debug , Clone ) ]
69
62
pub struct ImportDirective {
@@ -72,7 +65,7 @@ pub struct ImportDirective {
72
65
pub span : Span ,
73
66
pub id : NodeId ,
74
67
pub is_public : bool , // see note in ImportResolutionPerNamespace about how to use this
75
- pub shadowable : Shadowable ,
68
+ pub is_prelude : bool ,
76
69
}
77
70
78
71
impl ImportDirective {
@@ -81,15 +74,15 @@ impl ImportDirective {
81
74
span : Span ,
82
75
id : NodeId ,
83
76
is_public : bool ,
84
- shadowable : Shadowable )
77
+ is_prelude : bool )
85
78
-> ImportDirective {
86
79
ImportDirective {
87
80
module_path : module_path,
88
81
subclass : subclass,
89
82
span : span,
90
83
id : id,
91
84
is_public : is_public,
92
- shadowable : shadowable ,
85
+ is_prelude : is_prelude ,
93
86
}
94
87
}
95
88
@@ -105,9 +98,6 @@ impl ImportDirective {
105
98
if let GlobImport = self . subclass {
106
99
modifiers = modifiers | DefModifiers :: GLOB_IMPORTED ;
107
100
}
108
- if self . shadowable == Shadowable :: Always {
109
- modifiers = modifiers | DefModifiers :: PRELUDE ;
110
- }
111
101
112
102
NameBinding {
113
103
kind : NameBindingKind :: Import {
@@ -135,44 +125,36 @@ pub struct NameResolution<'a> {
135
125
136
126
impl < ' a > NameResolution < ' a > {
137
127
fn try_define ( & mut self , binding : & ' a NameBinding < ' a > ) -> Result < ( ) , & ' a NameBinding < ' a > > {
138
- match self . binding {
139
- Some ( old_binding) if !old_binding. defined_with ( DefModifiers :: PRELUDE ) => {
140
- if binding. defined_with ( DefModifiers :: GLOB_IMPORTED ) {
141
- self . duplicate_globs . push ( binding) ;
142
- } else if old_binding. defined_with ( DefModifiers :: GLOB_IMPORTED ) {
143
- self . duplicate_globs . push ( old_binding) ;
144
- self . binding = Some ( binding) ;
145
- } else {
146
- return Err ( old_binding) ;
147
- }
128
+ if let Some ( old_binding) = self . binding {
129
+ if binding. defined_with ( DefModifiers :: GLOB_IMPORTED ) {
130
+ self . duplicate_globs . push ( binding) ;
131
+ } else if old_binding. defined_with ( DefModifiers :: GLOB_IMPORTED ) {
132
+ self . duplicate_globs . push ( old_binding) ;
133
+ self . binding = Some ( binding) ;
134
+ } else {
135
+ return Err ( old_binding) ;
148
136
}
149
- _ => self . binding = Some ( binding) ,
137
+ } else {
138
+ self . binding = Some ( binding) ;
150
139
}
151
140
152
141
Ok ( ( ) )
153
142
}
154
143
155
- // Returns the resolution of the name assuming no more globs will define it.
156
- fn result ( & self , allow_private_imports : bool ) -> ResolveResult < & ' a NameBinding < ' a > > {
157
- match self . binding {
158
- Some ( binding) if !binding. defined_with ( DefModifiers :: GLOB_IMPORTED ) => Success ( binding) ,
159
- // If we don't allow private imports and no public imports can define the name, fail.
160
- _ if !allow_private_imports && self . pub_outstanding_references == 0 &&
161
- !self . binding . map ( NameBinding :: is_public) . unwrap_or ( false ) => Failed ( None ) ,
162
- _ if self . outstanding_references > 0 => Indeterminate ,
163
- Some ( binding) => Success ( binding) ,
164
- None => Failed ( None ) ,
165
- }
166
- }
167
-
168
144
// Returns Some(the resolution of the name), or None if the resolution depends
169
145
// on whether more globs can define the name.
170
146
fn try_result ( & self , allow_private_imports : bool )
171
147
-> Option < ResolveResult < & ' a NameBinding < ' a > > > {
172
- match self . result ( allow_private_imports) {
173
- Success ( binding) if binding. defined_with ( DefModifiers :: PRELUDE ) => None ,
174
- Failed ( _) => None ,
175
- result @ _ => Some ( result) ,
148
+ match self . binding {
149
+ Some ( binding) if !binding. defined_with ( DefModifiers :: GLOB_IMPORTED ) =>
150
+ Some ( Success ( binding) ) ,
151
+ // If (1) we don't allow private imports, (2) no public single import can define the
152
+ // name, and (3) no public glob has defined the name, the resolution depends on globs.
153
+ _ if !allow_private_imports && self . pub_outstanding_references == 0 &&
154
+ !self . binding . map ( NameBinding :: is_public) . unwrap_or ( false ) => None ,
155
+ _ if self . outstanding_references > 0 => Some ( Indeterminate ) ,
156
+ Some ( binding) => Some ( Success ( binding) ) ,
157
+ None => None ,
176
158
}
177
159
}
178
160
@@ -202,8 +184,6 @@ impl<'a> NameResolution<'a> {
202
184
} ;
203
185
204
186
for duplicate_glob in self . duplicate_globs . iter ( ) {
205
- if duplicate_glob. defined_with ( DefModifiers :: PRELUDE ) { continue }
206
-
207
187
// FIXME #31337: We currently allow items to shadow glob-imported re-exports.
208
188
if !binding. is_import ( ) {
209
189
if let NameBindingKind :: Import { binding, .. } = duplicate_glob. kind {
@@ -259,7 +239,16 @@ impl<'a> ::ModuleS<'a> {
259
239
}
260
240
}
261
241
262
- resolution. result ( true )
242
+ Failed ( None )
243
+ }
244
+
245
+ // Invariant: this may not be called until import resolution is complete.
246
+ pub fn resolve_name_in_lexical_scope ( & self , name : Name , ns : Namespace )
247
+ -> Option < & ' a NameBinding < ' a > > {
248
+ self . resolutions . borrow ( ) . get ( & ( name, ns) ) . and_then ( |resolution| resolution. binding )
249
+ . or_else ( || self . prelude . borrow ( ) . and_then ( |prelude| {
250
+ prelude. resolve_name ( name, ns, false ) . success ( )
251
+ } ) )
263
252
}
264
253
265
254
// Define the name or return the existing binding if there is a collision.
@@ -369,7 +358,7 @@ impl<'a, 'b:'a, 'tcx:'b> ImportResolver<'a, 'b, 'tcx> {
369
358
// resolution for it so that later resolve stages won't complain.
370
359
if let SingleImport { target, .. } = e. import_directive . subclass {
371
360
let dummy_binding = self . resolver . arenas . alloc_name_binding ( NameBinding {
372
- modifiers : DefModifiers :: PRELUDE ,
361
+ modifiers : DefModifiers :: GLOB_IMPORTED ,
373
362
kind : NameBindingKind :: Def ( Def :: Err ) ,
374
363
span : None ,
375
364
} ) ;
@@ -623,6 +612,11 @@ impl<'a, 'b:'a, 'tcx:'b> ImportResolver<'a, 'b, 'tcx> {
623
612
}
624
613
build_reduced_graph:: populate_module_if_necessary ( self . resolver , target_module) ;
625
614
615
+ if directive. is_prelude {
616
+ * module_. prelude . borrow_mut ( ) = Some ( target_module) ;
617
+ return Success ( ( ) ) ;
618
+ }
619
+
626
620
// Add to target_module's glob_importers and module_'s resolved_globs
627
621
target_module. glob_importers . borrow_mut ( ) . push ( ( module_, directive) ) ;
628
622
match * module_. resolved_globs . borrow_mut ( ) {
@@ -685,13 +679,6 @@ impl<'a, 'b:'a, 'tcx:'b> ImportResolver<'a, 'b, 'tcx> {
685
679
self . resolver . session . add_lint ( lint, id, binding. span . unwrap ( ) , msg) ;
686
680
}
687
681
}
688
-
689
- // We can always use methods from the prelude traits
690
- for glob_binding in resolution. duplicate_globs . iter ( ) {
691
- if glob_binding. defined_with ( DefModifiers :: PRELUDE ) {
692
- module. shadowed_traits . borrow_mut ( ) . push ( glob_binding) ;
693
- }
694
- }
695
682
}
696
683
697
684
if reexports. len ( ) > 0 {
0 commit comments