From 5ff21f138a1a807205180caf7921256e6dc16790 Mon Sep 17 00:00:00 2001 From: Jeffrey Seyfried Date: Thu, 17 Mar 2016 11:05:09 +0000 Subject: [PATCH] Refactor ModuleS fields `public_glob_count`, `private_glob_count`, and `resolved_globs` into a single field `globs: RefCell>`. --- src/librustc_resolve/build_reduced_graph.rs | 7 --- src/librustc_resolve/lib.rs | 22 +-------- src/librustc_resolve/resolve_imports.rs | 53 +++++++++------------ 3 files changed, 25 insertions(+), 57 deletions(-) diff --git a/src/librustc_resolve/build_reduced_graph.rs b/src/librustc_resolve/build_reduced_graph.rs index 211bff352fe..2480fd02bea 100644 --- a/src/librustc_resolve/build_reduced_graph.rs +++ b/src/librustc_resolve/build_reduced_graph.rs @@ -538,13 +538,6 @@ impl<'b, 'tcx:'b> Resolver<'b, 'tcx> { module_.increment_outstanding_references_for(target, ValueNS, is_public); module_.increment_outstanding_references_for(target, TypeNS, is_public); } - 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 => {} } diff --git a/src/librustc_resolve/lib.rs b/src/librustc_resolve/lib.rs index be01880e2a9..cc229f1f161 100644 --- a/src/librustc_resolve/lib.rs +++ b/src/librustc_resolve/lib.rs @@ -849,13 +849,7 @@ pub struct ModuleS<'a> { prelude: RefCell>>, glob_importers: RefCell, &'a ImportDirective<'a>)>>, - resolved_globs: RefCell<(Vec> /* public */, Vec> /* private */)>, - - // The number of public glob imports in this module. - public_glob_count: Cell, - - // The number of private glob imports in this module. - private_glob_count: Cell, + globs: RefCell>>, // Whether this module is populated. If not populated, any attempt to // access the children must be preceded with a @@ -883,19 +877,12 @@ impl<'a> ModuleS<'a> { module_children: RefCell::new(NodeMap()), prelude: RefCell::new(None), glob_importers: RefCell::new(Vec::new()), - resolved_globs: RefCell::new((Vec::new(), Vec::new())), - public_glob_count: Cell::new(0), - private_glob_count: Cell::new(0), + globs: RefCell::new((Vec::new())), populated: Cell::new(!external), arenas: arenas } } - fn add_import_directive(&self, import_directive: ImportDirective<'a>) { - let import_directive = self.arenas.alloc_import_directive(import_directive); - self.unresolved_imports.borrow_mut().push(import_directive); - } - fn for_each_child)>(&self, mut f: F) { for (&(name, ns), name_resolution) in self.resolutions.borrow().iter() { name_resolution.binding.map(|binding| f(name, ns, binding)); @@ -928,11 +915,6 @@ impl<'a> ModuleS<'a> { _ => false, } } - - fn inc_glob_count(&self, is_public: bool) { - let glob_count = if is_public { &self.public_glob_count } else { &self.private_glob_count }; - glob_count.set(glob_count.get() + 1); - } } impl<'a> fmt::Debug for ModuleS<'a> { diff --git a/src/librustc_resolve/resolve_imports.rs b/src/librustc_resolve/resolve_imports.rs index 6e527545ece..86cfa928a9d 100644 --- a/src/librustc_resolve/resolve_imports.rs +++ b/src/librustc_resolve/resolve_imports.rs @@ -212,29 +212,15 @@ impl<'a> ::ModuleS<'a> { }); } - let (ref mut public_globs, ref mut private_globs) = *self.resolved_globs.borrow_mut(); - - // Check if the public globs are determined - if public_globs.len() < self.public_glob_count.get() { - return Indeterminate; - } - for module in public_globs.iter() { - if let Indeterminate = module.resolve_name(name, ns, false) { - return Indeterminate; - } - } - - if !allow_private_imports { - return Failed(None); - } - - // Check if the private globs are determined - if private_globs.len() < self.private_glob_count.get() { - return Indeterminate; - } - for module in private_globs.iter() { - if let Indeterminate = module.resolve_name(name, ns, false) { - return Indeterminate; + // Check if the globs are determined + for directive in self.globs.borrow().iter() { + if !allow_private_imports && !directive.is_public { continue } + match directive.target_module.get() { + None => return Indeterminate, + Some(target_module) => match target_module.resolve_name(name, ns, false) { + Indeterminate => return Indeterminate, + _ => {} + } } } @@ -259,6 +245,18 @@ impl<'a> ::ModuleS<'a> { }) } + pub fn add_import_directive(&self, directive: ImportDirective<'a>) { + let directive = self.arenas.alloc_import_directive(directive); + self.unresolved_imports.borrow_mut().push(directive); + if let GlobImport = directive.subclass { + // We don't add prelude imports to the globs since they only affect lexical scopes, + // which are not relevant to import resolution. + if !directive.is_prelude { + self.globs.borrow_mut().push(directive); + } + } + } + pub fn increment_outstanding_references_for(&self, name: Name, ns: Namespace, is_public: bool) { self.resolutions.borrow_mut().entry((name, ns)).or_insert_with(Default::default) .increment_outstanding_references(is_public); @@ -603,12 +601,8 @@ impl<'a, 'b:'a, 'tcx:'b> ImportResolver<'a, 'b, 'tcx> { return Success(()); } - // Add to target_module's glob_importers and module_'s resolved_globs + // Add to target_module's glob_importers target_module.glob_importers.borrow_mut().push((module_, directive)); - match *module_.resolved_globs.borrow_mut() { - (ref mut public_globs, _) if directive.is_public => public_globs.push(target_module), - (_, ref mut private_globs) => private_globs.push(target_module), - } for (&(name, ns), resolution) in target_module.resolutions.borrow().iter() { if let Some(Success(binding)) = resolution.try_result(false) { @@ -635,8 +629,7 @@ impl<'a, 'b:'a, 'tcx:'b> ImportResolver<'a, 'b, 'tcx> { // reporting conflicts, reporting the PRIVATE_IN_PUBLIC lint, and reporting unresolved imports. fn finalize_resolutions(&mut self, module: Module<'b>, report_unresolved_imports: bool) { // Since import resolution is finished, globs will not define any more names. - module.public_glob_count.set(0); module.private_glob_count.set(0); - *module.resolved_globs.borrow_mut() = (Vec::new(), Vec::new()); + *module.globs.borrow_mut() = Vec::new(); let mut reexports = Vec::new(); for (&(name, ns), resolution) in module.resolutions.borrow().iter() {