From 245a0c5530f8d4d251bcef2d7b8de2fa19f442bf Mon Sep 17 00:00:00 2001 From: Jeffrey Seyfried Date: Sat, 20 Aug 2016 00:33:06 +0000 Subject: [PATCH] item_like_imports: Make all visible items glob importable. --- src/librustc_resolve/lib.rs | 4 ++++ src/librustc_resolve/resolve_imports.rs | 10 +++++++--- src/test/compile-fail/imports/reexports.rs | 7 +++++++ 3 files changed, 18 insertions(+), 3 deletions(-) diff --git a/src/librustc_resolve/lib.rs b/src/librustc_resolve/lib.rs index 8428507e686d..12b708fa1a16 100644 --- a/src/librustc_resolve/lib.rs +++ b/src/librustc_resolve/lib.rs @@ -3255,6 +3255,10 @@ impl<'a> Resolver<'a> { vis.is_accessible_from(self.current_module.normal_ancestor_id, self) } + fn is_accessible_from(&self, vis: ty::Visibility, module: Module<'a>) -> bool { + vis.is_accessible_from(module.normal_ancestor_id, self) + } + fn report_privacy_errors(&self) { if self.privacy_errors.len() == 0 { return } let mut reported_spans = FnvHashSet(); diff --git a/src/librustc_resolve/resolve_imports.rs b/src/librustc_resolve/resolve_imports.rs index 189253348b0a..4ab4ec4789d0 100644 --- a/src/librustc_resolve/resolve_imports.rs +++ b/src/librustc_resolve/resolve_imports.rs @@ -356,8 +356,11 @@ impl<'a> Resolver<'a> { }; // Define `binding` in `module`s glob importers. - if binding.vis == ty::Visibility::Public { - for directive in module.glob_importers.borrow_mut().iter() { + for directive in module.glob_importers.borrow_mut().iter() { + if match self.new_import_semantics { + true => self.is_accessible_from(binding.vis, directive.parent), + false => binding.vis == ty::Visibility::Public, + } { let imported_binding = self.import(binding, directive); let _ = self.try_define(directive.parent, name, ns, imported_binding); } @@ -708,7 +711,8 @@ impl<'a, 'b:'a> ImportResolver<'a, 'b> { resolution.borrow().binding().map(|binding| (*name, binding)) }).collect::>(); for ((name, ns), binding) in bindings { - if binding.pseudo_vis() == ty::Visibility::Public { + if binding.pseudo_vis() == ty::Visibility::Public || + self.new_import_semantics && self.is_accessible(binding.vis) { let imported_binding = self.import(binding, directive); let _ = self.try_define(directive.parent, name, ns, imported_binding); } diff --git a/src/test/compile-fail/imports/reexports.rs b/src/test/compile-fail/imports/reexports.rs index f8dbb4d44488..fc46b23351ad 100644 --- a/src/test/compile-fail/imports/reexports.rs +++ b/src/test/compile-fail/imports/reexports.rs @@ -16,6 +16,7 @@ mod a { mod a { pub use super::foo; //~ ERROR cannot be reexported + pub use super::*; //~ ERROR must import something with the glob's visibility } } @@ -27,11 +28,17 @@ mod b { pub use super::foo; // This is OK since the value `foo` is visible enough. fn f(_: foo::S) {} // `foo` is imported in the type namespace (but not `pub` reexported). } + + pub mod b { + pub use super::*; // This is also OK since the value `foo` is visible enough. + fn f(_: foo::S) {} // Again, the module `foo` is imported (but not `pub` reexported). + } } mod c { // Test that `foo` is not reexported. use b::a::foo::S; //~ ERROR `foo` + use b::b::foo::S as T; //~ ERROR `foo` } fn main() {}