Skip to content

Commit b3572ae

Browse files
committed
Finish encapsulating the details of import resolution in resolve_imports
1 parent 51ca449 commit b3572ae

File tree

3 files changed

+31
-79
lines changed

3 files changed

+31
-79
lines changed

src/librustc_resolve/build_reduced_graph.rs

+1-9
Original file line numberDiff line numberDiff line change
@@ -676,10 +676,6 @@ impl<'a, 'b:'a, 'tcx:'b> GraphBuilder<'a, 'b, 'tcx> {
676676
id: NodeId,
677677
is_public: bool,
678678
shadowable: Shadowable) {
679-
if is_public {
680-
module_.inc_pub_count();
681-
}
682-
683679
// Bump the reference count on the name. Or, if this is a glob, set
684680
// the appropriate flag.
685681

@@ -691,11 +687,7 @@ impl<'a, 'b:'a, 'tcx:'b> GraphBuilder<'a, 'b, 'tcx> {
691687
GlobImport => {
692688
// Set the glob flag. This tells us that we don't know the
693689
// module's exports ahead of time.
694-
695-
module_.inc_glob_count();
696-
if is_public {
697-
module_.inc_pub_glob_count();
698-
}
690+
module_.inc_glob_count(is_public)
699691
}
700692
}
701693

src/librustc_resolve/lib.rs

+9-42
Original file line numberDiff line numberDiff line change
@@ -836,14 +836,11 @@ pub struct ModuleS<'a> {
836836
glob_importers: RefCell<Vec<(Module<'a>, &'a ImportDirective)>>,
837837
resolved_globs: RefCell<(Vec<Module<'a>> /* public */, Vec<Module<'a>> /* private */)>,
838838

839-
// The number of unresolved globs that this module exports.
840-
glob_count: Cell<usize>,
839+
// The number of public glob imports in this module.
840+
public_glob_count: Cell<usize>,
841841

842-
// The number of unresolved pub imports (both regular and globs) in this module
843-
pub_count: Cell<usize>,
844-
845-
// The number of unresolved pub glob imports in this module
846-
pub_glob_count: Cell<usize>,
842+
// The number of private glob imports in this module.
843+
private_glob_count: Cell<usize>,
847844

848845
// Whether this module is populated. If not populated, any attempt to
849846
// access the children must be preceded with a
@@ -872,9 +869,8 @@ impl<'a> ModuleS<'a> {
872869
shadowed_traits: RefCell::new(Vec::new()),
873870
glob_importers: RefCell::new(Vec::new()),
874871
resolved_globs: RefCell::new((Vec::new(), Vec::new())),
875-
glob_count: Cell::new(0),
876-
pub_count: Cell::new(0),
877-
pub_glob_count: Cell::new(0),
872+
public_glob_count: Cell::new(0),
873+
private_glob_count: Cell::new(0),
878874
populated: Cell::new(!external),
879875
arenas: arenas
880876
}
@@ -918,26 +914,9 @@ impl<'a> ModuleS<'a> {
918914
}
919915
}
920916

921-
pub fn inc_glob_count(&self) {
922-
self.glob_count.set(self.glob_count.get() + 1);
923-
}
924-
pub fn dec_glob_count(&self) {
925-
assert!(self.glob_count.get() > 0);
926-
self.glob_count.set(self.glob_count.get() - 1);
927-
}
928-
pub fn inc_pub_count(&self) {
929-
self.pub_count.set(self.pub_count.get() + 1);
930-
}
931-
pub fn dec_pub_count(&self) {
932-
assert!(self.pub_count.get() > 0);
933-
self.pub_count.set(self.pub_count.get() - 1);
934-
}
935-
pub fn inc_pub_glob_count(&self) {
936-
self.pub_glob_count.set(self.pub_glob_count.get() + 1);
937-
}
938-
pub fn dec_pub_glob_count(&self) {
939-
assert!(self.pub_glob_count.get() > 0);
940-
self.pub_glob_count.set(self.pub_glob_count.get() - 1);
917+
fn inc_glob_count(&self, is_public: bool) {
918+
let glob_count = if is_public { &self.public_glob_count } else { &self.private_glob_count };
919+
glob_count.set(glob_count.get() + 1);
941920
}
942921
}
943922

@@ -1612,18 +1591,6 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
16121591
})
16131592
}
16141593

1615-
fn report_unresolved_imports(&mut self, module_: Module<'a>) {
1616-
for import in module_.unresolved_imports.borrow().iter() {
1617-
resolve_error(self, import.span, ResolutionError::UnresolvedImport(None));
1618-
break;
1619-
}
1620-
1621-
// Descend into children and anonymous children.
1622-
for (_, module_) in module_.module_children.borrow().iter() {
1623-
self.report_unresolved_imports(module_);
1624-
}
1625-
}
1626-
16271594
// AST resolution
16281595
//
16291596
// We maintain a list of value ribs and type ribs.

src/librustc_resolve/resolve_imports.rs

+21-28
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ impl<'a> ::ModuleS<'a> {
211211
let (ref mut public_globs, ref mut private_globs) = *self.resolved_globs.borrow_mut();
212212

213213
// Check if the public globs are determined
214-
if self.pub_glob_count.get() > 0 {
214+
if public_globs.len() < self.public_glob_count.get() {
215215
return Indeterminate;
216216
}
217217
for module in public_globs.iter() {
@@ -225,7 +225,7 @@ impl<'a> ::ModuleS<'a> {
225225
}
226226

227227
// Check if the private globs are determined
228-
if self.glob_count.get() > 0 {
228+
if private_globs.len() < self.private_glob_count.get() {
229229
return Indeterminate;
230230
}
231231
for module in private_globs.iter() {
@@ -322,23 +322,19 @@ impl<'a, 'b:'a, 'tcx:'b> ImportResolver<'a, 'b, 'tcx> {
322322

323323
if self.resolver.unresolved_imports == 0 {
324324
debug!("(resolving imports) success");
325-
self.finalize_resolutions(self.resolver.graph_root);
325+
self.finalize_resolutions(self.resolver.graph_root, false);
326326
break;
327327
}
328328

329329
if self.resolver.unresolved_imports == prev_unresolved_imports {
330330
// resolving failed
331-
self.finalize_resolutions(self.resolver.graph_root);
332-
if errors.len() > 0 {
333-
for e in errors {
334-
self.import_resolving_error(e)
335-
}
336-
} else {
337-
// Report unresolved imports only if no hard error was already reported
338-
// to avoid generating multiple errors on the same import.
339-
// Imports that are still indeterminate at this point are actually blocked
340-
// by errored imports, so there is no point reporting them.
341-
self.resolver.report_unresolved_imports(self.resolver.graph_root);
331+
// Report unresolved imports only if no hard error was already reported
332+
// to avoid generating multiple errors on the same import.
333+
// Imports that are still indeterminate at this point are actually blocked
334+
// by errored imports, so there is no point reporting them.
335+
self.finalize_resolutions(self.resolver.graph_root, errors.len() == 0);
336+
for e in errors {
337+
self.import_resolving_error(e)
342338
}
343339
break;
344340
}
@@ -444,16 +440,6 @@ impl<'a, 'b:'a, 'tcx:'b> ImportResolver<'a, 'b, 'tcx> {
444440
// Decrement the count of unresolved imports.
445441
assert!(self.resolver.unresolved_imports >= 1);
446442
self.resolver.unresolved_imports -= 1;
447-
448-
if let GlobImport = import_directive.subclass {
449-
module_.dec_glob_count();
450-
if import_directive.is_public {
451-
module_.dec_pub_glob_count();
452-
}
453-
}
454-
if import_directive.is_public {
455-
module_.dec_pub_count();
456-
}
457443
Success(())
458444
})
459445
}
@@ -697,10 +683,10 @@ impl<'a, 'b:'a, 'tcx:'b> ImportResolver<'a, 'b, 'tcx> {
697683
}
698684

699685
// Miscellaneous post-processing, including recording reexports, recording shadowed traits,
700-
// reporting conflicts, and reporting the PRIVATE_IN_PUBLIC lint.
701-
fn finalize_resolutions(&mut self, module: Module<'b>) {
686+
// reporting conflicts, reporting the PRIVATE_IN_PUBLIC lint, and reporting unresolved imports.
687+
fn finalize_resolutions(&mut self, module: Module<'b>, report_unresolved_imports: bool) {
702688
// Since import resolution is finished, globs will not define any more names.
703-
module.pub_glob_count.set(0); module.glob_count.set(0);
689+
module.public_glob_count.set(0); module.private_glob_count.set(0);
704690
*module.resolved_globs.borrow_mut() = (Vec::new(), Vec::new());
705691

706692
let mut reexports = Vec::new();
@@ -743,8 +729,15 @@ impl<'a, 'b:'a, 'tcx:'b> ImportResolver<'a, 'b, 'tcx> {
743729
}
744730
}
745731

732+
if report_unresolved_imports {
733+
for import in module.unresolved_imports.borrow().iter() {
734+
resolve_error(self.resolver, import.span, ResolutionError::UnresolvedImport(None));
735+
break;
736+
}
737+
}
738+
746739
for (_, child) in module.module_children.borrow().iter() {
747-
self.finalize_resolutions(child);
740+
self.finalize_resolutions(child, report_unresolved_imports);
748741
}
749742
}
750743
}

0 commit comments

Comments
 (0)