@@ -57,13 +57,6 @@ impl ImportDirectiveSubclass {
5757 }
5858}
5959
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-
6760/// One import directive.
6861#[ derive( Debug , Clone ) ]
6962pub struct ImportDirective {
@@ -72,7 +65,7 @@ pub struct ImportDirective {
7265 pub span : Span ,
7366 pub id : NodeId ,
7467 pub is_public : bool , // see note in ImportResolutionPerNamespace about how to use this
75- pub shadowable : Shadowable ,
68+ pub is_prelude : bool ,
7669}
7770
7871impl ImportDirective {
@@ -81,15 +74,15 @@ impl ImportDirective {
8174 span : Span ,
8275 id : NodeId ,
8376 is_public : bool ,
84- shadowable : Shadowable )
77+ is_prelude : bool )
8578 -> ImportDirective {
8679 ImportDirective {
8780 module_path : module_path,
8881 subclass : subclass,
8982 span : span,
9083 id : id,
9184 is_public : is_public,
92- shadowable : shadowable ,
85+ is_prelude : is_prelude ,
9386 }
9487 }
9588
@@ -105,9 +98,6 @@ impl ImportDirective {
10598 if let GlobImport = self . subclass {
10699 modifiers = modifiers | DefModifiers :: GLOB_IMPORTED ;
107100 }
108- if self . shadowable == Shadowable :: Always {
109- modifiers = modifiers | DefModifiers :: PRELUDE ;
110- }
111101
112102 NameBinding {
113103 kind : NameBindingKind :: Import {
@@ -135,44 +125,36 @@ pub struct NameResolution<'a> {
135125
136126impl < ' a > NameResolution < ' a > {
137127 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) ;
148136 }
149- _ => self . binding = Some ( binding) ,
137+ } else {
138+ self . binding = Some ( binding) ;
150139 }
151140
152141 Ok ( ( ) )
153142 }
154143
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-
168144 // Returns Some(the resolution of the name), or None if the resolution depends
169145 // on whether more globs can define the name.
170146 fn try_result ( & self , allow_private_imports : bool )
171147 -> 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 ,
176158 }
177159 }
178160
@@ -202,8 +184,6 @@ impl<'a> NameResolution<'a> {
202184 } ;
203185
204186 for duplicate_glob in self . duplicate_globs . iter ( ) {
205- if duplicate_glob. defined_with ( DefModifiers :: PRELUDE ) { continue }
206-
207187 // FIXME #31337: We currently allow items to shadow glob-imported re-exports.
208188 if !binding. is_import ( ) {
209189 if let NameBindingKind :: Import { binding, .. } = duplicate_glob. kind {
@@ -259,7 +239,16 @@ impl<'a> ::ModuleS<'a> {
259239 }
260240 }
261241
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+ } ) )
263252 }
264253
265254 // 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> {
369358 // resolution for it so that later resolve stages won't complain.
370359 if let SingleImport { target, .. } = e. import_directive . subclass {
371360 let dummy_binding = self . resolver . arenas . alloc_name_binding ( NameBinding {
372- modifiers : DefModifiers :: PRELUDE ,
361+ modifiers : DefModifiers :: GLOB_IMPORTED ,
373362 kind : NameBindingKind :: Def ( Def :: Err ) ,
374363 span : None ,
375364 } ) ;
@@ -623,6 +612,11 @@ impl<'a, 'b:'a, 'tcx:'b> ImportResolver<'a, 'b, 'tcx> {
623612 }
624613 build_reduced_graph:: populate_module_if_necessary ( self . resolver , target_module) ;
625614
615+ if directive. is_prelude {
616+ * module_. prelude . borrow_mut ( ) = Some ( target_module) ;
617+ return Success ( ( ) ) ;
618+ }
619+
626620 // Add to target_module's glob_importers and module_'s resolved_globs
627621 target_module. glob_importers . borrow_mut ( ) . push ( ( module_, directive) ) ;
628622 match * module_. resolved_globs . borrow_mut ( ) {
@@ -685,13 +679,6 @@ impl<'a, 'b:'a, 'tcx:'b> ImportResolver<'a, 'b, 'tcx> {
685679 self . resolver . session . add_lint ( lint, id, binding. span . unwrap ( ) , msg) ;
686680 }
687681 }
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- }
695682 }
696683
697684 if reexports. len ( ) > 0 {
0 commit comments