From: Jeffrey Seyfried Date: Wed, 9 Mar 2016 01:46:46 +0000 (+0000) Subject: Refactor how the prelude is handled X-Git-Url: https://git.lizzy.rs/?a=commitdiff_plain;h=febef471e35653aec00fc198ce97b9de84ebae63;p=rust.git Refactor how the prelude is handled --- diff --git a/src/librustc_resolve/build_reduced_graph.rs b/src/librustc_resolve/build_reduced_graph.rs index c30e6b8e2cf..479fc5ebf90 100644 --- a/src/librustc_resolve/build_reduced_graph.rs +++ b/src/librustc_resolve/build_reduced_graph.rs @@ -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 = diff --git a/src/librustc_resolve/lib.rs b/src/librustc_resolve/lib.rs index af8c9d81687..65d40edd958 100644 --- a/src/librustc_resolve/lib.rs +++ b/src/librustc_resolve/lib.rs @@ -820,7 +820,7 @@ pub struct ModuleS<'a> { // entry block for `f`. module_children: RefCell>>, - shadowed_traits: RefCell>>, + prelude: RefCell>>, glob_importers: RefCell, &'a ImportDirective)>>, resolved_globs: RefCell<(Vec> /* public */, Vec> /* 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, 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; } diff --git a/src/librustc_resolve/resolve_imports.rs b/src/librustc_resolve/resolve_imports.rs index eaa0753b8ce..91bbb154bbe 100644 --- a/src/librustc_resolve/resolve_imports.rs +++ b/src/librustc_resolve/resolve_imports.rs @@ -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 {