Skip to content

Commit

Permalink
Auto merge of #45846 - pietroalbini:use-nested-groups, r=petrochenkov
Browse files Browse the repository at this point in the history
Add nested groups in imports

This PR adds support for nested groups in imports (rust-lang/rfcs#2128, tracking issue #44494).

r? @petrochenkov
  • Loading branch information
bors committed Dec 1, 2017
2 parents d8a60c9 + f7f6951 commit 804b15b
Show file tree
Hide file tree
Showing 30 changed files with 949 additions and 578 deletions.
90 changes: 90 additions & 0 deletions src/doc/unstable-book/src/language-features/use-nested-groups.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
# `use_nested_groups`

The tracking issue for this feature is: [#44494]

[#44494]: https://github.com/rust-lang/rust/issues/44494

------------------------

The `use_nested_groups` feature allows you to import multiple items from a
complex module tree easily, by nesting different imports in the same
declaration. For example:

```rust
#![feature(use_nested_groups)]
# #![allow(unused_imports, dead_code)]
#
# mod foo {
# pub mod bar {
# pub type Foo = ();
# }
# pub mod baz {
# pub mod quux {
# pub type Bar = ();
# }
# }
# }

use foo::{
bar::{self, Foo},
baz::{*, quux::Bar},
};
#
# fn main() {}
```

## Snippet for the book's new features appendix

When stabilizing, add this to
`src/doc/book/second-edition/src/appendix-07-newest-features.md`:

### Nested groups in `use` declarations

If you have a complex module tree with many different submodules and you need
to import a few items from each one, it might be useful to group all the
imports in the same declaration to keep your code clean and avoid repeating the
base modules' name.

The `use` declaration supports nesting to help you in those cases, both with
simple imports and glob ones. For example this snippets imports `bar`, `Foo`,
all the items in `baz` and `Bar`:

```rust
# #![feature(use_nested_groups)]
# #![allow(unused_imports, dead_code)]
#
# mod foo {
# pub mod bar {
# pub type Foo = ();
# }
# pub mod baz {
# pub mod quux {
# pub type Bar = ();
# }
# }
# }
#
use foo::{
bar::{self, Foo},
baz::{*, quux::Bar},
};
#
# fn main() {}
```

## Updated reference

When stabilizing, replace the shortcut list in
`src/doc/reference/src/items/use-declarations.md` with this updated one:

* Simultaneously binding a list of paths with a common prefix, using the
glob-like brace syntax `use a::b::{c, d, e::f, g::h::i};`
* Simultaneously binding a list of paths with a common prefix and their common
parent module, using the `self` keyword, such as `use a::b::{self, c, d::e};`
* Rebinding the target name as a new local name, using the syntax `use p::q::r
as x;`. This can also be used with the last two features:
`use a::b::{self as ab, c as abc}`.
* Binding all paths matching a given prefix, using the asterisk wildcard syntax
`use a::b::*;`.
* Nesting groups of the previous features multiple times, such as
`use a::b::{self as ab, c d::{*, e::f}};`
208 changes: 130 additions & 78 deletions src/librustc/hir/lowering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1768,80 +1768,14 @@ impl<'a> LoweringContext<'a> {
-> hir::Item_ {
match *i {
ItemKind::ExternCrate(string) => hir::ItemExternCrate(string),
ItemKind::Use(ref view_path) => {
let path = match view_path.node {
ViewPathSimple(_, ref path) => path,
ViewPathGlob(ref path) => path,
ViewPathList(ref path, ref path_list_idents) => {
for &Spanned { node: ref import, span } in path_list_idents {
// `use a::{self as x, b as y};` lowers to
// `use a as x; use a::b as y;`
let mut ident = import.name;
let suffix = if ident.name == keywords::SelfValue.name() {
if let Some(last) = path.segments.last() {
ident = last.identifier;
}
None
} else {
Some(ident.name)
};

let mut path = self.lower_path_extra(import.id, path, suffix,
ParamMode::Explicit, true);
path.span = span;

self.allocate_hir_id_counter(import.id, import);
let LoweredNodeId {
node_id: import_node_id,
hir_id: import_hir_id,
} = self.lower_node_id(import.id);

self.with_hir_id_owner(import_node_id, |this| {
let vis = match *vis {
hir::Visibility::Public => hir::Visibility::Public,
hir::Visibility::Crate => hir::Visibility::Crate,
hir::Visibility::Inherited => hir::Visibility::Inherited,
hir::Visibility::Restricted { ref path, id: _ } => {
hir::Visibility::Restricted {
path: path.clone(),
// We are allocating a new NodeId here
id: this.next_id().node_id,
}
}
};

this.items.insert(import_node_id, hir::Item {
id: import_node_id,
hir_id: import_hir_id,
name: import.rename.unwrap_or(ident).name,
attrs: attrs.clone(),
node: hir::ItemUse(P(path), hir::UseKind::Single),
vis,
span,
});
});
}
path
}
ItemKind::Use(ref use_tree) => {
// Start with an empty prefix
let prefix = Path {
segments: vec![],
span: use_tree.span,
};
let path = P(self.lower_path(id, path, ParamMode::Explicit, true));
let kind = match view_path.node {
ViewPathSimple(ident, _) => {
*name = ident.name;
hir::UseKind::Single
}
ViewPathGlob(_) => {
hir::UseKind::Glob
}
ViewPathList(..) => {
// Privatize the degenerate import base, used only to check
// the stability of `use a::{};`, to avoid it showing up as
// a reexport by accident when `pub`, e.g. in documentation.
*vis = hir::Inherited;
hir::UseKind::ListStem
}
};
hir::ItemUse(path, kind)

self.lower_use_tree(use_tree, &prefix, id, vis, name, attrs)
}
ItemKind::Static(ref t, m, ref e) => {
let value = self.lower_body(None, |this| this.lower_expr(e));
Expand Down Expand Up @@ -1963,6 +1897,112 @@ impl<'a> LoweringContext<'a> {
// not cause an assertion failure inside the `lower_defaultness` function
}

fn lower_use_tree(&mut self,
tree: &UseTree,
prefix: &Path,
id: NodeId,
vis: &mut hir::Visibility,
name: &mut Name,
attrs: &hir::HirVec<Attribute>)
-> hir::Item_ {
let path = &tree.prefix;

match tree.kind {
UseTreeKind::Simple(ident) => {
*name = ident.name;

// First apply the prefix to the path
let mut path = Path {
segments: prefix.segments
.iter()
.chain(path.segments.iter())
.cloned()
.collect(),
span: path.span.to(prefix.span),
};

// Correctly resolve `self` imports
if path.segments.last().unwrap().identifier.name == keywords::SelfValue.name() {
let _ = path.segments.pop();
if ident.name == keywords::SelfValue.name() {
*name = path.segments.last().unwrap().identifier.name;
}
}

let path = P(self.lower_path(id, &path, ParamMode::Explicit, true));
hir::ItemUse(path, hir::UseKind::Single)
}
UseTreeKind::Glob => {
let path = P(self.lower_path(id, &Path {
segments: prefix.segments
.iter()
.chain(path.segments.iter())
.cloned()
.collect(),
span: path.span,
}, ParamMode::Explicit, true));
hir::ItemUse(path, hir::UseKind::Glob)
}
UseTreeKind::Nested(ref trees) => {
let prefix = Path {
segments: prefix.segments
.iter()
.chain(path.segments.iter())
.cloned()
.collect(),
span: prefix.span.to(path.span),
};

// Add all the nested PathListItems in the HIR
for &(ref use_tree, id) in trees {
self.allocate_hir_id_counter(id, &use_tree);
let LoweredNodeId {
node_id: new_id,
hir_id: new_hir_id,
} = self.lower_node_id(id);

let mut vis = vis.clone();
let mut name = name.clone();
let item = self.lower_use_tree(
use_tree, &prefix, new_id, &mut vis, &mut name, &attrs,
);

self.with_hir_id_owner(new_id, |this| {
let vis = match vis {
hir::Visibility::Public => hir::Visibility::Public,
hir::Visibility::Crate => hir::Visibility::Crate,
hir::Visibility::Inherited => hir::Visibility::Inherited,
hir::Visibility::Restricted { ref path, id: _ } => {
hir::Visibility::Restricted {
path: path.clone(),
// We are allocating a new NodeId here
id: this.next_id().node_id,
}
}
};

this.items.insert(new_id, hir::Item {
id: new_id,
hir_id: new_hir_id,
name: name,
attrs: attrs.clone(),
node: item,
vis,
span: use_tree.span,
});
});
}

// Privatize the degenerate import base, used only to check
// the stability of `use a::{};`, to avoid it showing up as
// a reexport by accident when `pub`, e.g. in documentation.
let path = P(self.lower_path(id, &prefix, ParamMode::Explicit, true));
*vis = hir::Inherited;
hir::ItemUse(path, hir::UseKind::ListStem)
}
}
}

fn lower_trait_item(&mut self, i: &TraitItem) -> hir::TraitItem {
self.with_parent_def(i.id, |this| {
let LoweredNodeId { node_id, hir_id } = this.lower_node_id(i.id);
Expand Down Expand Up @@ -2129,18 +2169,30 @@ impl<'a> LoweringContext<'a> {

fn lower_item_id(&mut self, i: &Item) -> SmallVector<hir::ItemId> {
match i.node {
ItemKind::Use(ref view_path) => {
if let ViewPathList(_, ref imports) = view_path.node {
return iter::once(i.id).chain(imports.iter().map(|import| import.node.id))
.map(|id| hir::ItemId { id: id }).collect();
}
ItemKind::Use(ref use_tree) => {
let mut vec = SmallVector::one(hir::ItemId { id: i.id });
self.lower_item_id_use_tree(use_tree, &mut vec);
return vec;
}
ItemKind::MacroDef(..) => return SmallVector::new(),
_ => {}
}
SmallVector::one(hir::ItemId { id: i.id })
}

fn lower_item_id_use_tree(&self, tree: &UseTree, vec: &mut SmallVector<hir::ItemId>) {
match tree.kind {
UseTreeKind::Nested(ref nested_vec) => {
for &(ref nested, id) in nested_vec {
vec.push(hir::ItemId { id, });
self.lower_item_id_use_tree(nested, vec);
}
}
UseTreeKind::Glob => {}
UseTreeKind::Simple(..) => {}
}
}

pub fn lower_item(&mut self, i: &Item) -> Option<hir::Item> {
let mut name = i.ident.name;
let mut vis = self.lower_visibility(&i.vis, None);
Expand Down
22 changes: 7 additions & 15 deletions src/librustc/hir/map/def_collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,21 +118,8 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> {
ItemKind::MacroDef(..) => DefPathData::MacroDef(i.ident.name.as_str()),
ItemKind::Mac(..) => return self.visit_macro_invoc(i.id, false),
ItemKind::GlobalAsm(..) => DefPathData::Misc,
ItemKind::Use(ref view_path) => {
match view_path.node {
ViewPathGlob(..) => {}

// FIXME(eddyb) Should use the real name. Which namespace?
ViewPathSimple(..) => {}
ViewPathList(_, ref imports) => {
for import in imports {
self.create_def(import.node.id,
DefPathData::Misc,
ITEM_LIKE_SPACE);
}
}
}
DefPathData::Misc
ItemKind::Use(..) => {
return visit::walk_item(self, i);
}
};
let def = self.create_def(i.id, def_data, ITEM_LIKE_SPACE);
Expand Down Expand Up @@ -180,6 +167,11 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> {
});
}

fn visit_use_tree(&mut self, use_tree: &'a UseTree, id: NodeId, _nested: bool) {
self.create_def(id, DefPathData::Misc, ITEM_LIKE_SPACE);
visit::walk_use_tree(self, use_tree, id);
}

fn visit_foreign_item(&mut self, foreign_item: &'a ForeignItem) {
let def = self.create_def(foreign_item.id,
DefPathData::ValueNs(foreign_item.ident.name.as_str()),
Expand Down
6 changes: 0 additions & 6 deletions src/librustc/lint/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -981,12 +981,6 @@ impl<'a> ast_visit::Visitor<'a> for EarlyContext<'a> {
ast_visit::walk_path(self, p);
}

fn visit_path_list_item(&mut self, prefix: &'a ast::Path, item: &'a ast::PathListItem) {
run_lints!(self, check_path_list_item, early_passes, item);
self.check_id(item.node.id);
ast_visit::walk_path_list_item(self, prefix, item);
}

fn visit_attribute(&mut self, attr: &'a ast::Attribute) {
run_lints!(self, check_attribute, early_passes, attr);
}
Expand Down
1 change: 0 additions & 1 deletion src/librustc/lint/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,6 @@ pub trait EarlyLintPass: LintPass {
fn check_lifetime(&mut self, _: &EarlyContext, _: &ast::Lifetime) { }
fn check_lifetime_def(&mut self, _: &EarlyContext, _: &ast::LifetimeDef) { }
fn check_path(&mut self, _: &EarlyContext, _: &ast::Path, _: ast::NodeId) { }
fn check_path_list_item(&mut self, _: &EarlyContext, _: &ast::PathListItem) { }
fn check_attribute(&mut self, _: &EarlyContext, _: &ast::Attribute) { }

/// Called when entering a syntax node that can have lint attributes such
Expand Down
Loading

0 comments on commit 804b15b

Please sign in to comment.