Skip to content

Commit 8856927

Browse files
committed
Auto merge of #27451 - seanmonstar:use-groups-as, r=alexcrichton
An implementation of [RFC 1219](rust-lang/rfcs#1219). The RFC is not merged yet, but once merged, this could be.
2 parents 3d69bec + cfcd449 commit 8856927

File tree

16 files changed

+169
-33
lines changed

16 files changed

+169
-33
lines changed

src/grammar/parser-lalr.y

+1
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,7 @@ visibility
472472

473473
idents_or_self
474474
: ident_or_self { $$ = mk_node("IdentsOrSelf", 1, $1); }
475+
| ident_or_self AS ident { $$ = mk_node("IdentsOrSelf", 2, $1, $3); }
475476
| idents_or_self ',' ident_or_self { $$ = ext_node($1, 1, $3); }
476477
;
477478

src/librustc_privacy/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -859,11 +859,11 @@ impl<'a, 'tcx, 'v> Visitor<'v> for PrivacyVisitor<'a, 'tcx> {
859859
if let ast::ViewPathList(ref prefix, ref list) = vpath.node {
860860
for pid in list {
861861
match pid.node {
862-
ast::PathListIdent { id, name } => {
862+
ast::PathListIdent { id, name, .. } => {
863863
debug!("privacy - ident item {}", id);
864864
self.check_path(pid.span, id, name.name);
865865
}
866-
ast::PathListMod { id } => {
866+
ast::PathListMod { id, .. } => {
867867
debug!("privacy - mod item {}", id);
868868
let name = prefix.segments.last().unwrap().identifier.name;
869869
self.check_path(pid.span, id, name);

src/librustc_resolve/build_reduced_graph.rs

+7-6
Original file line numberDiff line numberDiff line change
@@ -341,10 +341,10 @@ impl<'a, 'b:'a, 'tcx:'b> GraphBuilder<'a, 'b, 'tcx> {
341341
}
342342

343343
for source_item in source_items {
344-
let (module_path, name) = match source_item.node {
345-
PathListIdent { name, .. } =>
346-
(module_path.clone(), name.name),
347-
PathListMod { .. } => {
344+
let (module_path, name, rename) = match source_item.node {
345+
PathListIdent { name, rename, .. } =>
346+
(module_path.clone(), name.name, rename.unwrap_or(name).name),
347+
PathListMod { rename, .. } => {
348348
let name = match module_path.last() {
349349
Some(name) => *name,
350350
None => {
@@ -358,13 +358,14 @@ impl<'a, 'b:'a, 'tcx:'b> GraphBuilder<'a, 'b, 'tcx> {
358358
}
359359
};
360360
let module_path = module_path.split_last().unwrap().1;
361-
(module_path.to_vec(), name)
361+
let rename = rename.map(|n| n.name).unwrap_or(name);
362+
(module_path.to_vec(), name, rename)
362363
}
363364
};
364365
self.build_import_directive(
365366
&**parent,
366367
module_path,
367-
SingleImport(name, name),
368+
SingleImport(rename, name),
368369
source_item.span,
369370
source_item.node.id(),
370371
is_public,

src/librustdoc/clean/mod.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -2366,7 +2366,7 @@ impl Clean<Vec<Item>> for doctree::Import {
23662366
let remaining = if !denied {
23672367
let mut remaining = vec![];
23682368
for path in list {
2369-
match inline::try_inline(cx, path.node.id(), None) {
2369+
match inline::try_inline(cx, path.node.id(), path.node.rename()) {
23702370
Some(items) => {
23712371
ret.extend(items);
23722372
}
@@ -2428,18 +2428,21 @@ pub struct ImportSource {
24282428
#[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
24292429
pub struct ViewListIdent {
24302430
pub name: String,
2431+
pub rename: Option<String>,
24312432
pub source: Option<ast::DefId>,
24322433
}
24332434

24342435
impl Clean<ViewListIdent> for ast::PathListItem {
24352436
fn clean(&self, cx: &DocContext) -> ViewListIdent {
24362437
match self.node {
2437-
ast::PathListIdent { id, name } => ViewListIdent {
2438+
ast::PathListIdent { id, name, rename } => ViewListIdent {
24382439
name: name.clean(cx),
2440+
rename: rename.map(|r| r.clean(cx)),
24392441
source: resolve_def(cx, id)
24402442
},
2441-
ast::PathListMod { id } => ViewListIdent {
2443+
ast::PathListMod { id, rename } => ViewListIdent {
24422444
name: "self".to_string(),
2445+
rename: rename.map(|r| r.clean(cx)),
24432446
source: resolve_def(cx, id)
24442447
}
24452448
}

src/librustdoc/html/format.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -687,10 +687,15 @@ impl fmt::Display for clean::ViewListIdent {
687687
match self.source {
688688
Some(did) => {
689689
let path = clean::Path::singleton(self.name.clone());
690-
resolved_path(f, did, &path, false)
690+
try!(resolved_path(f, did, &path, false));
691691
}
692-
_ => write!(f, "{}", self.name),
692+
_ => try!(write!(f, "{}", self.name)),
693+
}
694+
695+
if let Some(ref name) = self.rename {
696+
try!(write!(f, " as {}", name));
693697
}
698+
Ok(())
694699
}
695700
}
696701

src/libsyntax/ast.rs

+18-3
Original file line numberDiff line numberDiff line change
@@ -1658,14 +1658,29 @@ pub type Variant = Spanned<Variant_>;
16581658

16591659
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)]
16601660
pub enum PathListItem_ {
1661-
PathListIdent { name: Ident, id: NodeId },
1662-
PathListMod { id: NodeId }
1661+
PathListIdent {
1662+
name: Ident,
1663+
/// renamed in list, eg `use foo::{bar as baz};`
1664+
rename: Option<Ident>,
1665+
id: NodeId
1666+
},
1667+
PathListMod {
1668+
/// renamed in list, eg `use foo::{self as baz};`
1669+
rename: Option<Ident>,
1670+
id: NodeId
1671+
}
16631672
}
16641673

16651674
impl PathListItem_ {
16661675
pub fn id(&self) -> NodeId {
16671676
match *self {
1668-
PathListIdent { id, .. } | PathListMod { id } => id
1677+
PathListIdent { id, .. } | PathListMod { id, .. } => id
1678+
}
1679+
}
1680+
1681+
pub fn rename(&self) -> Option<Ident> {
1682+
match *self {
1683+
PathListIdent { rename, .. } | PathListMod { rename, .. } => rename
16691684
}
16701685
}
16711686
}

src/libsyntax/ext/build.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1141,7 +1141,7 @@ impl<'a> AstBuilder for ExtCtxt<'a> {
11411141
fn item_use_list(&self, sp: Span, vis: ast::Visibility,
11421142
path: Vec<ast::Ident>, imports: &[ast::Ident]) -> P<ast::Item> {
11431143
let imports = imports.iter().map(|id| {
1144-
respan(sp, ast::PathListIdent { name: *id, id: ast::DUMMY_NODE_ID })
1144+
respan(sp, ast::PathListIdent { name: *id, rename: None, id: ast::DUMMY_NODE_ID })
11451145
}).collect();
11461146

11471147
self.item_use(sp, vis,

src/libsyntax/fold.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -335,13 +335,17 @@ pub fn noop_fold_view_path<T: Folder>(view_path: P<ViewPath>, fld: &mut T) -> P<
335335
path_list_idents.move_map(|path_list_ident| {
336336
Spanned {
337337
node: match path_list_ident.node {
338-
PathListIdent { id, name } =>
338+
PathListIdent { id, name, rename } =>
339339
PathListIdent {
340340
id: fld.new_id(id),
341+
rename: rename,
341342
name: name
342343
},
343-
PathListMod { id } =>
344-
PathListMod { id: fld.new_id(id) }
344+
PathListMod { id, rename } =>
345+
PathListMod {
346+
id: fld.new_id(id),
347+
rename: rename
348+
}
345349
},
346350
span: fld.new_span(path_list_ident.span)
347351
}

src/libsyntax/parse/parser.rs

+14-6
Original file line numberDiff line numberDiff line change
@@ -574,10 +574,12 @@ impl<'a> Parser<'a> {
574574
pub fn parse_path_list_item(&mut self) -> PResult<ast::PathListItem> {
575575
let lo = self.span.lo;
576576
let node = if try!(self.eat_keyword(keywords::SelfValue)) {
577-
ast::PathListMod { id: ast::DUMMY_NODE_ID }
577+
let rename = try!(self.parse_rename());
578+
ast::PathListMod { id: ast::DUMMY_NODE_ID, rename: rename }
578579
} else {
579580
let ident = try!(self.parse_ident());
580-
ast::PathListIdent { name: ident, id: ast::DUMMY_NODE_ID }
581+
let rename = try!(self.parse_rename());
582+
ast::PathListIdent { name: ident, rename: rename, id: ast::DUMMY_NODE_ID }
581583
};
582584
let hi = self.last_span.hi;
583585
Ok(spanned(lo, hi, node))
@@ -5117,8 +5119,8 @@ impl<'a> Parser<'a> {
51175119
-> PResult<P<Item>> {
51185120

51195121
let crate_name = try!(self.parse_ident());
5120-
let (maybe_path, ident) = if try!(self.eat_keyword(keywords::As)) {
5121-
(Some(crate_name.name), try!(self.parse_ident()))
5122+
let (maybe_path, ident) = if let Some(ident) = try!(self.parse_rename()) {
5123+
(Some(crate_name.name), ident)
51225124
} else {
51235125
(None, crate_name)
51245126
};
@@ -5779,10 +5781,16 @@ impl<'a> Parser<'a> {
57795781
}
57805782
}).collect()
57815783
};
5784+
rename_to = try!(self.parse_rename()).unwrap_or(rename_to);
5785+
Ok(P(spanned(lo, self.last_span.hi, ViewPathSimple(rename_to, path))))
5786+
}
5787+
5788+
fn parse_rename(&mut self) -> PResult<Option<Ident>> {
57825789
if try!(self.eat_keyword(keywords::As)) {
5783-
rename_to = try!(self.parse_ident())
5790+
self.parse_ident().map(Some)
5791+
} else {
5792+
Ok(None)
57845793
}
5785-
Ok(P(spanned(lo, self.last_span.hi, ViewPathSimple(rename_to, path))))
57865794
}
57875795

57885796
/// Parses a source module as a crate. This is the main

src/libsyntax/print/pprust.rs

+16-4
Original file line numberDiff line numberDiff line change
@@ -2646,11 +2646,23 @@ impl<'a> State<'a> {
26462646
}
26472647
try!(self.commasep(Inconsistent, &idents[..], |s, w| {
26482648
match w.node {
2649-
ast::PathListIdent { name, .. } => {
2650-
s.print_ident(name)
2649+
ast::PathListIdent { name, rename, .. } => {
2650+
try!(s.print_ident(name));
2651+
if let Some(ident) = rename {
2652+
try!(space(&mut s.s));
2653+
try!(s.word_space("as"));
2654+
try!(s.print_ident(ident));
2655+
}
2656+
Ok(())
26512657
},
2652-
ast::PathListMod { .. } => {
2653-
word(&mut s.s, "self")
2658+
ast::PathListMod { rename, .. } => {
2659+
try!(word(&mut s.s, "self"));
2660+
if let Some(ident) = rename {
2661+
try!(space(&mut s.s));
2662+
try!(s.word_space("as"));
2663+
try!(s.print_ident(ident));
2664+
}
2665+
Ok(())
26542666
}
26552667
}
26562668
}));

src/libsyntax/visit.rs

+9-2
Original file line numberDiff line numberDiff line change
@@ -233,10 +233,17 @@ pub fn walk_item<'v, V: Visitor<'v>>(visitor: &mut V, item: &'v Item) {
233233
ViewPathList(ref prefix, ref list) => {
234234
for id in list {
235235
match id.node {
236-
PathListIdent { name, .. } => {
236+
PathListIdent { name, rename, .. } => {
237237
visitor.visit_ident(id.span, name);
238+
if let Some(ident) = rename {
239+
visitor.visit_ident(id.span, ident);
240+
}
241+
}
242+
PathListMod { rename, .. } => {
243+
if let Some(ident) = rename {
244+
visitor.visit_ident(id.span, ident);
245+
}
238246
}
239-
PathListMod { .. } => ()
240247
}
241248
}
242249

src/test/compile-fail/unresolved-import.rs

+15-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2013-2014 The Rust Project Developers. See the COPYRIGHT
1+
// Copyright 2013-2015 The Rust Project Developers. See the COPYRIGHT
22
// file at the top-level directory of this distribution and at
33
// http://rust-lang.org/COPYRIGHT.
44
//
@@ -12,6 +12,20 @@ use foo::bar; //~ ERROR unresolved import `foo::bar`. Maybe a missing `extern cr
1212

1313
use bar::baz as x; //~ ERROR unresolved import `bar::baz`. There is no `baz` in `bar`
1414

15+
use food::baz; //~ ERROR unresolved import `food::baz`. There is no `baz` in `food`
16+
17+
use food::{quux as beans}; //~ ERROR unresolved import `food::quux`. There is no `quux` in `food`
18+
1519
mod bar {
1620
struct bar;
1721
}
22+
23+
mod food {
24+
pub use self::zug::baz::{self as bag, quux as beans};
25+
26+
mod zug {
27+
pub mod baz {
28+
pub struct quux;
29+
}
30+
}
31+
}

src/test/pretty/import-renames.rs

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Copyright 2015 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+
// pp-exact
12+
13+
use std::io::{self, Error as IoError};
14+
use std::net::{self as stdnet, TcpStream};

src/test/run-pass/import-rename.rs

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright 2015 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+
use foo::{x, y as fooy};
12+
use Maybe::{Yes as MaybeYes};
13+
14+
pub enum Maybe { Yes, No }
15+
mod foo {
16+
use super::Maybe::{self as MaybeFoo};
17+
pub fn x(a: MaybeFoo) {}
18+
pub fn y(a: i32) { println!("{}", a); }
19+
}
20+
21+
pub fn main() { x(MaybeYes); fooy(10); }

src/test/run-pass/use.rs

+3
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ extern crate std as zed;
1919

2020
use std::str;
2121
use zed::str as x;
22+
23+
use std::io::{self, Error as IoError, Result as IoResult};
24+
use std::error::{self as foo};
2225
mod baz {
2326
pub use std::str as x;
2427
}

src/test/rustdoc/viewpath-rename.rs

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright 2015 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+
#![crate_name = "foo"]
12+
13+
pub mod io {
14+
pub trait Reader { fn dummy(&self) { } }
15+
}
16+
17+
pub enum Maybe<A> {
18+
Just(A),
19+
Nothing
20+
}
21+
22+
// @has foo/prelude/index.html
23+
pub mod prelude {
24+
// @has foo/prelude/index.html '//code' 'pub use io::{self as FooIo, Reader as FooReader}'
25+
#[doc(no_inline)] pub use io::{self as FooIo, Reader as FooReader};
26+
// @has foo/prelude/index.html '//code' 'pub use Maybe::{self, Just as MaybeJust, Nothing}'
27+
#[doc(no_inline)] pub use Maybe::{self, Just as MaybeJust, Nothing};
28+
}

0 commit comments

Comments
 (0)