Skip to content

Commit b69bf11

Browse files
committed
Block import resolution only on 'pub' imports.
When resolving 'use' statements, only consider pub imports of the target module as blocking. Closes rust-lang#4865
1 parent d034561 commit b69bf11

File tree

3 files changed

+36
-6
lines changed

3 files changed

+36
-6
lines changed

src/librustc_resolve/build_reduced_graph.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -926,6 +926,11 @@ impl<'a, 'b:'a, 'tcx:'b> GraphBuilder<'a, 'b, 'tcx> {
926926
is_public,
927927
shadowable));
928928
self.unresolved_imports += 1;
929+
930+
if is_public {
931+
module_.pub_count.set(module_.pub_count.get() + 1);
932+
}
933+
929934
// Bump the reference count on the name. Or, if this is a glob, set
930935
// the appropriate flag.
931936

@@ -959,6 +964,9 @@ impl<'a, 'b:'a, 'tcx:'b> GraphBuilder<'a, 'b, 'tcx> {
959964
// module's exports ahead of time.
960965

961966
module_.glob_count.set(module_.glob_count.get() + 1);
967+
if is_public {
968+
module_.pub_glob_count.set(module_.pub_glob_count.get() + 1);
969+
}
962970
}
963971
}
964972
}

src/librustc_resolve/lib.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -699,6 +699,12 @@ pub struct Module {
699699
// The number of unresolved globs that this module exports.
700700
glob_count: Cell<usize>,
701701

702+
// The number of unresolved pub imports (both regular and globs) in this module
703+
pub_count: Cell<usize>,
704+
705+
// The number of unresolved pub glob imports in this module
706+
pub_glob_count: Cell<usize>,
707+
702708
// The index of the import we're resolving.
703709
resolved_import_count: Cell<usize>,
704710

@@ -726,6 +732,8 @@ impl Module {
726732
anonymous_children: RefCell::new(NodeMap()),
727733
import_resolutions: RefCell::new(HashMap::new()),
728734
glob_count: Cell::new(0),
735+
pub_count: Cell::new(0),
736+
pub_glob_count: Cell::new(0),
729737
resolved_import_count: Cell::new(0),
730738
populated: Cell::new(!external),
731739
}

src/librustc_resolve/resolve_imports.rs

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -409,11 +409,19 @@ impl<'a, 'b:'a, 'tcx:'b> ImportResolver<'a, 'b, 'tcx> {
409409
GlobImport => {
410410
assert!(module_.glob_count.get() >= 1);
411411
module_.glob_count.set(module_.glob_count.get() - 1);
412+
if import_directive.is_public {
413+
assert!(module_.pub_glob_count.get() >= 1);
414+
module_.pub_glob_count.set(module_.pub_glob_count.get() - 1);
415+
}
412416
}
413417
SingleImport(..) => {
414418
// Ignore.
415419
}
416420
}
421+
if import_directive.is_public {
422+
assert!(module_.pub_count.get() >= 1);
423+
module_.pub_count.set(module_.pub_count.get() - 1);
424+
}
417425
}
418426

419427
return resolution_result;
@@ -503,8 +511,8 @@ impl<'a, 'b:'a, 'tcx:'b> ImportResolver<'a, 'b, 'tcx> {
503511
// containing module, bail out. We don't know enough to be
504512
// able to resolve this import.
505513

506-
if target_module.glob_count.get() > 0 {
507-
debug!("(resolving single import) unresolved glob; \
514+
if target_module.pub_glob_count.get() > 0 {
515+
debug!("(resolving single import) unresolved pub glob; \
508516
bailing out");
509517
return ResolveResult::Indeterminate;
510518
}
@@ -767,16 +775,22 @@ impl<'a, 'b:'a, 'tcx:'b> ImportResolver<'a, 'b, 'tcx> {
767775

768776
// We must bail out if the node has unresolved imports of any kind
769777
// (including globs).
770-
if !(*target_module).all_imports_resolved() {
778+
if (*target_module).pub_count.get() > 0 {
771779
debug!("(resolving glob import) target module has unresolved \
772-
imports; bailing out");
780+
pub imports; bailing out");
773781
return ResolveResult::Indeterminate;
774782
}
775783

776-
assert_eq!(target_module.glob_count.get(), 0);
777-
778784
// Add all resolved imports from the containing module.
779785
let import_resolutions = target_module.import_resolutions.borrow();
786+
787+
if module_.import_resolutions.borrow_state() != ::std::cell::BorrowState::Unused {
788+
// In this case, target_module == module_
789+
// This means we are trying to glob import a module into itself,
790+
// and it is a no-go
791+
return ResolveResult::Indeterminate;
792+
}
793+
780794
for (ident, target_import_resolution) in import_resolutions.iter() {
781795
debug!("(resolving glob import) writing module resolution \
782796
{} into `{}`",

0 commit comments

Comments
 (0)