Skip to content

Commit e1d19df

Browse files
authored
Rollup merge of rust-lang#48922 - petrochenkov:asunder, r=nikomatsakis
Implement import renaming with `_` (RFC 2166) cc rust-lang#48216
2 parents 68a602e + 12ac032 commit e1d19df

File tree

6 files changed

+153
-1
lines changed

6 files changed

+153
-1
lines changed

src/libsyntax/feature_gate.rs

+18
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,9 @@ declare_features! (
455455

456456
// Parentheses in patterns
457457
(active, pattern_parentheses, "1.26.0", None, None),
458+
459+
// `use path as _;` and `extern crate c as _;`
460+
(active, underscore_imports, "1.26.0", Some(48216), None),
458461
);
459462

460463
declare_features! (
@@ -1436,9 +1439,24 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {
14361439
}
14371440
}
14381441

1442+
fn visit_use_tree(&mut self, use_tree: &'a ast::UseTree, id: NodeId, _nested: bool) {
1443+
if let ast::UseTreeKind::Simple(ident) = use_tree.kind {
1444+
if ident.name == "_" {
1445+
gate_feature_post!(&self, underscore_imports, use_tree.span,
1446+
"renaming imports with `_` is unstable");
1447+
}
1448+
}
1449+
1450+
visit::walk_use_tree(self, use_tree, id);
1451+
}
1452+
14391453
fn visit_item(&mut self, i: &'a ast::Item) {
14401454
match i.node {
14411455
ast::ItemKind::ExternCrate(_) => {
1456+
if i.ident.name == "_" {
1457+
gate_feature_post!(&self, underscore_imports, i.span,
1458+
"renaming extern crates with `_` is unstable");
1459+
}
14421460
if let Some(attr) = attr::find_by_name(&i.attrs[..], "macro_reexport") {
14431461
gate_feature_post!(&self, macro_reexport, attr.span,
14441462
"macros re-exports are experimental \

src/libsyntax/parse/parser.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -7040,7 +7040,11 @@ impl<'a> Parser<'a> {
70407040

70417041
fn parse_rename(&mut self) -> PResult<'a, Option<Ident>> {
70427042
if self.eat_keyword(keywords::As) {
7043-
self.parse_ident().map(Some)
7043+
if self.eat(&token::Underscore) {
7044+
Ok(Some(Ident::with_empty_ctxt(Symbol::gensym("_"))))
7045+
} else {
7046+
self.parse_ident().map(Some)
7047+
}
70447048
} else {
70457049
Ok(None)
70467050
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
extern crate std as _; //~ ERROR renaming extern crates with `_` is unstable
12+
use std::vec as _; //~ ERROR renaming imports with `_` is unstable
13+
14+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
error[E0658]: renaming extern crates with `_` is unstable (see issue #48216)
2+
--> $DIR/feature-gate-underscore-imports.rs:11:1
3+
|
4+
LL | extern crate std as _; //~ ERROR renaming extern crates with `_` is unstable
5+
| ^^^^^^^^^^^^^^^^^^^^^^
6+
|
7+
= help: add #![feature(underscore_imports)] to the crate attributes to enable
8+
9+
error[E0658]: renaming imports with `_` is unstable (see issue #48216)
10+
--> $DIR/feature-gate-underscore-imports.rs:12:5
11+
|
12+
LL | use std::vec as _; //~ ERROR renaming imports with `_` is unstable
13+
| ^^^^^^^^^^^^^
14+
|
15+
= help: add #![feature(underscore_imports)] to the crate attributes to enable
16+
17+
error: aborting due to 2 previous errors
18+
19+
For more information about this error, try `rustc --explain E0658`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// must-compile-successfully
12+
13+
#![feature(underscore_imports)]
14+
#![warn(unused_imports, unused_extern_crates)]
15+
16+
struct S;
17+
18+
mod m {
19+
pub trait Tr1 {
20+
fn tr1_is_in_scope(&self) {}
21+
}
22+
pub trait Tr2 {
23+
fn tr2_is_in_scope(&self) {}
24+
}
25+
26+
impl Tr1 for ::S {}
27+
impl Tr2 for ::S {}
28+
}
29+
30+
mod unused {
31+
use m::Tr1 as _; //~ WARN unused import
32+
use S as _; //~ WARN unused import
33+
extern crate core as _; //~ WARN unused extern crate
34+
}
35+
36+
mod outer {
37+
mod middle {
38+
pub use m::Tr1 as _;
39+
pub use m::Tr2 as _; // OK, no name conflict
40+
struct Tr1; // OK, no name conflict
41+
fn check() {
42+
// Both traits are in scope
43+
::S.tr1_is_in_scope();
44+
::S.tr2_is_in_scope();
45+
}
46+
47+
mod inner {
48+
// `_` imports are fetched by glob imports
49+
use super::*;
50+
fn check() {
51+
// Both traits are in scope
52+
::S.tr1_is_in_scope();
53+
::S.tr2_is_in_scope();
54+
}
55+
}
56+
}
57+
58+
// `_` imports are fetched by glob imports
59+
use self::middle::*;
60+
fn check() {
61+
// Both traits are in scope
62+
::S.tr1_is_in_scope();
63+
::S.tr2_is_in_scope();
64+
}
65+
}
66+
67+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
warning: unused import: `m::Tr1 as _`
2+
--> $DIR/basic.rs:31:9
3+
|
4+
LL | use m::Tr1 as _; //~ WARN unused import
5+
| ^^^^^^^^^^^
6+
|
7+
note: lint level defined here
8+
--> $DIR/basic.rs:14:9
9+
|
10+
LL | #![warn(unused_imports, unused_extern_crates)]
11+
| ^^^^^^^^^^^^^^
12+
13+
warning: unused import: `S as _`
14+
--> $DIR/basic.rs:32:9
15+
|
16+
LL | use S as _; //~ WARN unused import
17+
| ^^^^^^
18+
19+
warning: unused extern crate
20+
--> $DIR/basic.rs:33:5
21+
|
22+
LL | extern crate core as _; //~ WARN unused extern crate
23+
| ^^^^^^^^^^^^^^^^^^^^^^^
24+
|
25+
note: lint level defined here
26+
--> $DIR/basic.rs:14:25
27+
|
28+
LL | #![warn(unused_imports, unused_extern_crates)]
29+
| ^^^^^^^^^^^^^^^^^^^^
30+

0 commit comments

Comments
 (0)