Stabilize use_nested_groups

上级 07ea2604
# `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}};`
......@@ -423,9 +423,6 @@ pub fn new() -> Features {
// In-band lifetime bindings (e.g. `fn foo(x: &'a u8) -> &'a u8`)
(active, in_band_lifetimes, "1.23.0", Some(44524)),
// Nested groups in `use` (RFC 2128)
(active, use_nested_groups, "1.23.0", Some(44494)),
// generic associated types (RFC 1598)
(active, generic_associated_types, "1.23.0", Some(44265)),
......@@ -544,6 +541,8 @@ pub fn new() -> Features {
(accepted, repr_align, "1.24.0", Some(33626)),
// allow '|' at beginning of match arms (RFC 1925)
(accepted, match_beginning_vert, "1.25.0", Some(44101)),
// Nested groups in `use` (RFC 2128)
(accepted, use_nested_groups, "1.25.0", Some(44494)),
);
// If you change this, please modify src/doc/unstable-book as well. You must
......@@ -1805,29 +1804,6 @@ fn visit_path(&mut self, path: &'a ast::Path, _id: NodeId) {
visit::walk_path(self, path);
}
fn visit_use_tree(&mut self, use_tree: &'a ast::UseTree, id: NodeId, nested: bool) {
if nested {
match use_tree.kind {
ast::UseTreeKind::Simple(_) => {
if use_tree.prefix.segments.len() != 1 {
gate_feature_post!(&self, use_nested_groups, use_tree.span,
"paths in `use` groups are experimental");
}
}
ast::UseTreeKind::Glob => {
gate_feature_post!(&self, use_nested_groups, use_tree.span,
"glob imports in `use` groups are experimental");
}
ast::UseTreeKind::Nested(_) => {
gate_feature_post!(&self, use_nested_groups, use_tree.span,
"nested groups in `use` are experimental");
}
}
}
visit::walk_use_tree(self, use_tree, id);
}
fn visit_vis(&mut self, vis: &'a ast::Visibility) {
if let ast::Visibility::Crate(span, ast::CrateSugar::JustCrate) = *vis {
gate_feature_post!(&self, crate_visibility_modifier, span,
......
......@@ -8,7 +8,6 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.
#![feature(use_nested_groups)]
#![allow(unused_imports)]
mod foo {}
......
......@@ -8,7 +8,6 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.
#![feature(use_nested_groups)]
#![allow(unused_import)]
use {{}, {}};
......
......@@ -8,8 +8,6 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.
#![feature(use_nested_groups)]
mod a {
pub enum B {}
......
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
#![allow(unused_imports, dead_code)]
mod a {
pub enum B {}
pub enum C {}
pub mod d {
pub enum E {}
pub enum F {}
pub mod g {
pub enum H {}
}
}
}
use a::{B, d::{*, g::H}}; //~ ERROR glob imports in `use` groups are experimental
//~^ ERROR nested groups in `use` are experimental
//~^^ ERROR paths in `use` groups are experimental
fn main() {}
error[E0658]: nested groups in `use` are experimental (see issue #44494)
--> $DIR/feature-gate-use_nested_groups.rs:27:12
|
27 | use a::{B, d::{*, g::H}}; //~ ERROR glob imports in `use` groups are experimental
| ^^^^^^^^^^^^
|
= help: add #![feature(use_nested_groups)] to the crate attributes to enable
error[E0658]: glob imports in `use` groups are experimental (see issue #44494)
--> $DIR/feature-gate-use_nested_groups.rs:27:16
|
27 | use a::{B, d::{*, g::H}}; //~ ERROR glob imports in `use` groups are experimental
| ^
|
= help: add #![feature(use_nested_groups)] to the crate attributes to enable
error[E0658]: paths in `use` groups are experimental (see issue #44494)
--> $DIR/feature-gate-use_nested_groups.rs:27:19
|
27 | use a::{B, d::{*, g::H}}; //~ ERROR glob imports in `use` groups are experimental
| ^^^^
|
= help: add #![feature(use_nested_groups)] to the crate attributes to enable
error: aborting due to 3 previous errors
......@@ -8,8 +8,6 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.
#![feature(use_nested_groups)]
mod a {
pub mod b1 {
pub enum C2 {}
......
error[E0432]: unresolved import `a::b1::C1`
--> $DIR/use-nested-groups-error.rs:21:14
--> $DIR/use-nested-groups-error.rs:19:14
|
21 | use a::{b1::{C1, C2}, B2};
19 | use a::{b1::{C1, C2}, B2};
| ^^ no `C1` in `a::b1`. Did you mean to use `C2`?
error: aborting due to previous error
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册