Skip to content

Commit

Permalink
Auto merge of #35894 - jseyfried:new_import_semantics, r=nrc
Browse files Browse the repository at this point in the history
Implement RFC 1560 behind `#![feature(item_like_imports)]`

This implements rust-lang/rfcs#1560 (cc #35120) behind the `item_like_imports` feature gate.

The [RFC text](https://github.com/rust-lang/rfcs/blob/master/text/1560-name-resolution.md#changes-to-name-resolution-rules) describes the changes to name resolution enabled by `#![feature(item_like_imports)` in detail. To summarize,
 - Items and named imports shadow glob imports.
 - Multiple globs can import the same name if the name is unused or the imports are shadowed.
 - Multiple globs can import the same name if the imports are of the same item (following re-exports).
  - The visibility of such a name is the maximum visibility of the imports.
  - Equivalently, adding a glob import will never reduce the visibility of a name, nor will removing one increase it.
 - Non-prelude private imports can be used wherever we currently allow private items to be used.
  - Prelude-imported names are unaffected, i.e. they continue to be usable only in lexical scopes.
 - Globs import all visible names, not just public names.
  - Equivalently, glob importing from an ancestor module imports all of the ancestor's names, and glob importing from other modules is unchanged.

r? @nrc
  • Loading branch information
bors authored Sep 2, 2016
2 parents 497d67d + 90ce504 commit 8aeb15a
Show file tree
Hide file tree
Showing 9 changed files with 508 additions and 220 deletions.
23 changes: 13 additions & 10 deletions src/librustc_resolve/build_reduced_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ use rustc::hir::def::*;
use rustc::hir::def_id::{CRATE_DEF_INDEX, DefId};
use rustc::ty::{self, VariantKind};

use std::cell::Cell;

use syntax::ast::Name;
use syntax::attr;
use syntax::parse::token;
Expand Down Expand Up @@ -81,7 +83,6 @@ impl<'b> Resolver<'b> {
/// Constructs the reduced graph for one item.
fn build_reduced_graph_for_item(&mut self, item: &Item) {
let parent = self.current_module;
let parent_vis = self.current_vis;
let name = item.ident.name;
let sp = item.span;
let vis = self.resolve_visibility(&item.vis);
Expand Down Expand Up @@ -177,7 +178,10 @@ impl<'b> Resolver<'b> {
}
}
ViewPathGlob(_) => {
let subclass = GlobImport { is_prelude: is_prelude };
let subclass = GlobImport {
is_prelude: is_prelude,
max_vis: Cell::new(ty::Visibility::PrivateExternal),
};
let span = view_path.span;
self.add_import_directive(module_path, subclass, span, item.id, vis);
}
Expand All @@ -204,7 +208,7 @@ impl<'b> Resolver<'b> {
ItemKind::Mod(..) => {
let parent_link = ModuleParentLink(parent, name);
let def = Def::Mod(self.definitions.local_def_id(item.id));
let module = self.new_module(parent_link, Some(def), false);
let module = self.new_module(parent_link, Some(def), Some(item.id));
module.no_implicit_prelude.set({
parent.no_implicit_prelude.get() ||
attr::contains_name(&item.attrs, "no_implicit_prelude")
Expand All @@ -214,7 +218,6 @@ impl<'b> Resolver<'b> {

// Descend into the module.
self.current_module = module;
self.current_vis = ty::Visibility::Restricted(item.id);
}

ItemKind::ForeignMod(..) => {}
Expand Down Expand Up @@ -243,7 +246,7 @@ impl<'b> Resolver<'b> {
ItemKind::Enum(ref enum_definition, _) => {
let parent_link = ModuleParentLink(parent, name);
let def = Def::Enum(self.definitions.local_def_id(item.id));
let module = self.new_module(parent_link, Some(def), false);
let module = self.new_module(parent_link, Some(def), parent.normal_ancestor_id);
self.define(parent, name, TypeNS, (module, sp, vis));

for variant in &(*enum_definition).variants {
Expand Down Expand Up @@ -285,7 +288,8 @@ impl<'b> Resolver<'b> {
// Add all the items within to a new module.
let parent_link = ModuleParentLink(parent, name);
let def = Def::Trait(def_id);
let module_parent = self.new_module(parent_link, Some(def), false);
let module_parent =
self.new_module(parent_link, Some(def), parent.normal_ancestor_id);
self.define(parent, name, TypeNS, (module_parent, sp, vis));

// Add the names of all the items to the trait info.
Expand All @@ -312,7 +316,6 @@ impl<'b> Resolver<'b> {

visit::walk_item(&mut BuildReducedGraphVisitor { resolver: self }, item);
self.current_module = parent;
self.current_vis = parent_vis;
}

// Constructs the reduced graph for one variant. Variants exist in the
Expand Down Expand Up @@ -363,7 +366,7 @@ impl<'b> Resolver<'b> {
block_id);

let parent_link = BlockParentLink(parent, block_id);
let new_module = self.new_module(parent_link, None, false);
let new_module = self.new_module(parent_link, None, parent.normal_ancestor_id);
self.module_map.insert(block_id, new_module);
self.current_module = new_module; // Descend into the block.
}
Expand Down Expand Up @@ -395,7 +398,7 @@ impl<'b> Resolver<'b> {
debug!("(building reduced graph for external crate) building module {} {:?}",
name, vis);
let parent_link = ModuleParentLink(parent, name);
let module = self.new_module(parent_link, Some(def), true);
let module = self.new_module(parent_link, Some(def), None);
let _ = self.try_define(parent, name, TypeNS, (module, DUMMY_SP, vis));
}
Def::Variant(_, variant_id) => {
Expand Down Expand Up @@ -437,7 +440,7 @@ impl<'b> Resolver<'b> {
}

let parent_link = ModuleParentLink(parent, name);
let module = self.new_module(parent_link, Some(def), true);
let module = self.new_module(parent_link, Some(def), None);
let _ = self.try_define(parent, name, TypeNS, (module, DUMMY_SP, vis));
}
Def::TyAlias(..) | Def::AssociatedTy(..) => {
Expand Down
Loading

0 comments on commit 8aeb15a

Please sign in to comment.