Skip to content

Commit 8aeb15a

Browse files
authored
Auto merge of #35894 - jseyfried:new_import_semantics, r=nrc
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
2 parents 497d67d + 90ce504 commit 8aeb15a

File tree

9 files changed

+508
-220
lines changed

9 files changed

+508
-220
lines changed

src/librustc_resolve/build_reduced_graph.rs

+13-10
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ use rustc::hir::def::*;
2626
use rustc::hir::def_id::{CRATE_DEF_INDEX, DefId};
2727
use rustc::ty::{self, VariantKind};
2828

29+
use std::cell::Cell;
30+
2931
use syntax::ast::Name;
3032
use syntax::attr;
3133
use syntax::parse::token;
@@ -81,7 +83,6 @@ impl<'b> Resolver<'b> {
8183
/// Constructs the reduced graph for one item.
8284
fn build_reduced_graph_for_item(&mut self, item: &Item) {
8385
let parent = self.current_module;
84-
let parent_vis = self.current_vis;
8586
let name = item.ident.name;
8687
let sp = item.span;
8788
let vis = self.resolve_visibility(&item.vis);
@@ -177,7 +178,10 @@ impl<'b> Resolver<'b> {
177178
}
178179
}
179180
ViewPathGlob(_) => {
180-
let subclass = GlobImport { is_prelude: is_prelude };
181+
let subclass = GlobImport {
182+
is_prelude: is_prelude,
183+
max_vis: Cell::new(ty::Visibility::PrivateExternal),
184+
};
181185
let span = view_path.span;
182186
self.add_import_directive(module_path, subclass, span, item.id, vis);
183187
}
@@ -204,7 +208,7 @@ impl<'b> Resolver<'b> {
204208
ItemKind::Mod(..) => {
205209
let parent_link = ModuleParentLink(parent, name);
206210
let def = Def::Mod(self.definitions.local_def_id(item.id));
207-
let module = self.new_module(parent_link, Some(def), false);
211+
let module = self.new_module(parent_link, Some(def), Some(item.id));
208212
module.no_implicit_prelude.set({
209213
parent.no_implicit_prelude.get() ||
210214
attr::contains_name(&item.attrs, "no_implicit_prelude")
@@ -214,7 +218,6 @@ impl<'b> Resolver<'b> {
214218

215219
// Descend into the module.
216220
self.current_module = module;
217-
self.current_vis = ty::Visibility::Restricted(item.id);
218221
}
219222

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

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

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

313317
visit::walk_item(&mut BuildReducedGraphVisitor { resolver: self }, item);
314318
self.current_module = parent;
315-
self.current_vis = parent_vis;
316319
}
317320

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

365368
let parent_link = BlockParentLink(parent, block_id);
366-
let new_module = self.new_module(parent_link, None, false);
369+
let new_module = self.new_module(parent_link, None, parent.normal_ancestor_id);
367370
self.module_map.insert(block_id, new_module);
368371
self.current_module = new_module; // Descend into the block.
369372
}
@@ -395,7 +398,7 @@ impl<'b> Resolver<'b> {
395398
debug!("(building reduced graph for external crate) building module {} {:?}",
396399
name, vis);
397400
let parent_link = ModuleParentLink(parent, name);
398-
let module = self.new_module(parent_link, Some(def), true);
401+
let module = self.new_module(parent_link, Some(def), None);
399402
let _ = self.try_define(parent, name, TypeNS, (module, DUMMY_SP, vis));
400403
}
401404
Def::Variant(_, variant_id) => {
@@ -437,7 +440,7 @@ impl<'b> Resolver<'b> {
437440
}
438441

439442
let parent_link = ModuleParentLink(parent, name);
440-
let module = self.new_module(parent_link, Some(def), true);
443+
let module = self.new_module(parent_link, Some(def), None);
441444
let _ = self.try_define(parent, name, TypeNS, (module, DUMMY_SP, vis));
442445
}
443446
Def::TyAlias(..) | Def::AssociatedTy(..) => {

0 commit comments

Comments
 (0)