Refactor ModuleS fields public_glob_count
, private_glob_count
, and
`resolved_globs` into a single field `globs: RefCell<Vec<ImportDirective>>`.
This commit is contained in:
parent
0d1bc02da8
commit
5ff21f138a
@ -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 => {}
|
||||
}
|
||||
|
||||
|
@ -849,13 +849,7 @@ pub struct ModuleS<'a> {
|
||||
prelude: RefCell<Option<Module<'a>>>,
|
||||
|
||||
glob_importers: RefCell<Vec<(Module<'a>, &'a ImportDirective<'a>)>>,
|
||||
resolved_globs: RefCell<(Vec<Module<'a>> /* public */, Vec<Module<'a>> /* private */)>,
|
||||
|
||||
// The number of public glob imports in this module.
|
||||
public_glob_count: Cell<usize>,
|
||||
|
||||
// The number of private glob imports in this module.
|
||||
private_glob_count: Cell<usize>,
|
||||
globs: RefCell<Vec<&'a ImportDirective<'a>>>,
|
||||
|
||||
// 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<F: FnMut(Name, Namespace, &'a NameBinding<'a>)>(&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> {
|
||||
|
@ -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() {
|
||||
|
Loading…
Reference in New Issue
Block a user