]> git.lizzy.rs Git - rust.git/commitdiff
Refactor how the prelude is handled
authorJeffrey Seyfried <jeffrey.seyfried@gmail.com>
Wed, 9 Mar 2016 01:46:46 +0000 (01:46 +0000)
committerJeffrey Seyfried <jeffrey.seyfried@gmail.com>
Fri, 25 Mar 2016 22:22:12 +0000 (22:22 +0000)
src/librustc_resolve/build_reduced_graph.rs
src/librustc_resolve/lib.rs
src/librustc_resolve/resolve_imports.rs

index c30e6b8e2cfd67ceb9c40a703517e0c12c31237a..479fc5ebf907e74133683f4f045166f51d33b0a6 100644 (file)
@@ -634,11 +634,14 @@ fn build_import_directive(&mut self,
                 module_.increment_outstanding_references_for(target, ValueNS, is_public);
                 module_.increment_outstanding_references_for(target, TypeNS, is_public);
             }
-            GlobImport => {
+            GlobImport if !is_prelude => {
                 // Set the glob flag. This tells us that we don't know the
                 // module's exports ahead of time.
                 module_.inc_glob_count(is_public)
             }
+            // Prelude imports are not included in the glob counts since they do not get added to
+            // `resolved_globs` -- they are handled separately in `resolve_imports`.
+            GlobImport => {}
         }
 
         let directive =
index af8c9d81687421457d089e80ed7cd1e90c040006..65d40edd9584c3d0d6f4b7715991f63d294c2e0a 100644 (file)
@@ -820,7 +820,7 @@ pub struct ModuleS<'a> {
     // entry block for `f`.
     module_children: RefCell<NodeMap<Module<'a>>>,
 
-    shadowed_traits: RefCell<Vec<&'a NameBinding<'a>>>,
+    prelude: RefCell<Option<Module<'a>>>,
 
     glob_importers: RefCell<Vec<(Module<'a>, &'a ImportDirective)>>,
     resolved_globs: RefCell<(Vec<Module<'a>> /* public */, Vec<Module<'a>> /* private */)>,
@@ -855,7 +855,7 @@ fn new(parent_link: ParentLink<'a>,
             resolutions: RefCell::new(HashMap::new()),
             unresolved_imports: RefCell::new(Vec::new()),
             module_children: RefCell::new(NodeMap()),
-            shadowed_traits: RefCell::new(Vec::new()),
+            prelude: RefCell::new(None),
             glob_importers: RefCell::new(Vec::new()),
             resolved_globs: RefCell::new((Vec::new(), Vec::new())),
             public_glob_count: Cell::new(0),
@@ -3336,33 +3336,25 @@ fn add_trait_info(found_traits: &mut Vec<DefId>, trait_def_id: DefId, name: Name
             }
 
             // Look for trait children.
-            build_reduced_graph::populate_module_if_necessary(self, &search_module);
-
-            search_module.for_each_child(|_, ns, name_binding| {
+            let mut search_in_module = |module: Module<'a>| module.for_each_child(|_, ns, binding| {
                 if ns != TypeNS { return }
-                let trait_def_id = match name_binding.def() {
+                let trait_def_id = match binding.def() {
                     Some(Def::Trait(trait_def_id)) => trait_def_id,
                     Some(..) | None => return,
                 };
                 if self.trait_item_map.contains_key(&(name, trait_def_id)) {
                     add_trait_info(&mut found_traits, trait_def_id, name);
                     let trait_name = self.get_trait_name(trait_def_id);
-                    self.record_use(trait_name, TypeNS, name_binding);
-                }
-            });
-
-            // Look for shadowed traits.
-            for binding in search_module.shadowed_traits.borrow().iter() {
-                let did = binding.def().unwrap().def_id();
-                if self.trait_item_map.contains_key(&(name, did)) {
-                    add_trait_info(&mut found_traits, did, name);
-                    let trait_name = self.get_trait_name(did);
                     self.record_use(trait_name, TypeNS, binding);
                 }
-            }
+            });
+            search_in_module(search_module);
 
             match search_module.parent_link {
-                NoParentLink | ModuleParentLink(..) => break,
+                NoParentLink | ModuleParentLink(..) => {
+                    search_module.prelude.borrow().map(search_in_module);
+                    break;
+                }
                 BlockParentLink(parent_module, _) => {
                     search_module = parent_module;
                 }
index eaa0753b8ce15844933761a72769da48f0302bd5..91bbb154bbeb716553622fb883de73c658d56fa2 100644 (file)
@@ -98,9 +98,6 @@ fn import<'a>(&self,
         if let GlobImport = self.subclass {
             modifiers = modifiers | DefModifiers::GLOB_IMPORTED;
         }
-        if self.is_prelude {
-            modifiers = modifiers | DefModifiers::PRELUDE;
-        }
 
         NameBinding {
             kind: NameBindingKind::Import {
@@ -252,7 +249,8 @@ pub fn resolve_name(&self, name: Name, ns: Namespace, allow_private_imports: boo
             }
         }
 
-        resolution.result(true)
+        self.prelude.borrow().map(|prelude| prelude.resolve_name(name, ns, false))
+                             .unwrap_or(Failed(None))
     }
 
     // Define the name or return the existing binding if there is a collision.
@@ -616,6 +614,11 @@ fn resolve_glob_import(&mut self,
         }
         build_reduced_graph::populate_module_if_necessary(self.resolver, target_module);
 
+        if directive.is_prelude {
+            *module_.prelude.borrow_mut() = Some(target_module);
+            return Success(());
+        }
+
         // Add to target_module's glob_importers and module_'s resolved_globs
         target_module.glob_importers.borrow_mut().push((module_, directive));
         match *module_.resolved_globs.borrow_mut() {
@@ -678,13 +681,6 @@ fn finalize_resolutions(&mut self, module: Module<'b>, report_unresolved_imports
                     self.resolver.session.add_lint(lint, id, binding.span.unwrap(), msg);
                 }
             }
-
-            // We can always use methods from the prelude traits
-            for glob_binding in resolution.duplicate_globs.iter() {
-                if glob_binding.defined_with(DefModifiers::PRELUDE) {
-                    module.shadowed_traits.borrow_mut().push(glob_binding);
-                }
-            }
         }
 
         if reexports.len() > 0 {