1
Fork 0

auto merge of #16468 : pcwalton/rust/as-renaming-import, r=alexcrichton

The old syntax will be removed after a snapshot.

RFC #47.

Issue #16461.

r? @brson
This commit is contained in:
bors 2014-08-14 21:01:19 +00:00
commit f8e0ede921
29 changed files with 51 additions and 43 deletions

View file

@ -919,7 +919,7 @@ extern crate foo = "some/where/rust-foo#foo:1.0"; // a full crate ID for externa
##### Use declarations ##### Use declarations
~~~~ {.ebnf .gram} ~~~~ {.ebnf .gram}
use_decl : "pub" ? "use" [ ident '=' path use_decl : "pub" ? "use" [ path "as" ident
| path_glob ] ; | path_glob ] ;
path_glob : ident [ "::" [ path_glob path_glob : ident [ "::" [ path_glob
@ -939,7 +939,7 @@ module item. These declarations may appear at the top of [modules](#modules) and
Use declarations support a number of convenient shortcuts: Use declarations support a number of convenient shortcuts:
* Rebinding the target name as a new local name, using the syntax `use x = p::q::r;`. * Rebinding the target name as a new local name, using the syntax `use p::q::r as x;`.
* Simultaneously binding a list of paths differing only in their final element, * Simultaneously binding a list of paths differing only in their final element,
using the glob-like brace syntax `use a::b::{c,d,e,f};` using the glob-like brace syntax `use a::b::{c,d,e,f};`
* Binding all paths matching a given prefix, using the asterisk wildcard syntax `use a::b::*;` * Binding all paths matching a given prefix, using the asterisk wildcard syntax `use a::b::*;`

View file

@ -1119,11 +1119,11 @@ pub type ViewPath = Spanned<ViewPath_>;
#[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash, Show)] #[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash, Show)]
pub enum ViewPath_ { pub enum ViewPath_ {
/// `quux = foo::bar::baz` /// `foo::bar::baz as quux`
/// ///
/// or just /// or just
/// ///
/// `foo::bar::baz ` (with 'baz =' implicitly on the left) /// `foo::bar::baz` (with `as baz` implicitly on the right)
ViewPathSimple(Ident, Path, NodeId), ViewPathSimple(Ident, Path, NodeId),
/// `foo::bar::*` /// `foo::bar::*`

View file

@ -5317,6 +5317,7 @@ impl<'a> Parser<'a> {
match self.token { match self.token {
token::EQ => { token::EQ => {
// x = foo::bar // x = foo::bar
// NOTE(stage0, #16461, pcwalton): Deprecate after snapshot.
self.bump(); self.bump();
let path_lo = self.span.lo; let path_lo = self.span.lo;
path = vec!(self.parse_ident()); path = vec!(self.parse_ident());
@ -5399,7 +5400,7 @@ impl<'a> Parser<'a> {
} }
_ => () _ => ()
} }
let last = *path.get(path.len() - 1u); let mut rename_to = *path.get(path.len() - 1u);
let path = ast::Path { let path = ast::Path {
span: mk_sp(lo, self.span.hi), span: mk_sp(lo, self.span.hi),
global: false, global: false,
@ -5411,9 +5412,12 @@ impl<'a> Parser<'a> {
} }
}).collect() }).collect()
}; };
if self.eat_keyword(keywords::As) {
rename_to = self.parse_ident()
}
return box(GC) spanned(lo, return box(GC) spanned(lo,
self.last_span.hi, self.last_span.hi,
ViewPathSimple(last, path, ast::DUMMY_NODE_ID)); ViewPathSimple(rename_to, path, ast::DUMMY_NODE_ID));
} }
/// Parses a sequence of items. Stops when it finds program /// Parses a sequence of items. Stops when it finds program

View file

@ -2275,13 +2275,17 @@ impl<'a> State<'a> {
pub fn print_view_path(&mut self, vp: &ast::ViewPath) -> IoResult<()> { pub fn print_view_path(&mut self, vp: &ast::ViewPath) -> IoResult<()> {
match vp.node { match vp.node {
ast::ViewPathSimple(ident, ref path, _) => { ast::ViewPathSimple(ident, ref path, _) => {
try!(self.print_path(path, false));
// FIXME(#6993) can't compare identifiers directly here // FIXME(#6993) can't compare identifiers directly here
if path.segments.last().unwrap().identifier.name != ident.name { if path.segments.last().unwrap().identifier.name !=
try!(self.print_ident(ident)); ident.name {
try!(space(&mut self.s)); try!(space(&mut self.s));
try!(self.word_space("=")); try!(self.word_space("as"));
try!(self.print_ident(ident));
} }
self.print_path(path, false)
Ok(())
} }
ast::ViewPathGlob(ref path, _) => { ast::ViewPathGlob(ref path, _) => {

View file

@ -8,7 +8,7 @@
// option. This file may not be copied, modified, or distributed // option. This file may not be copied, modified, or distributed
// except according to those terms. // except according to those terms.
pub use bar = foo; pub use foo as bar;
mod foo { mod foo {
pub fn frob() {} pub fn frob() {}

View file

@ -9,7 +9,7 @@
// except according to those terms. // except according to those terms.
pub use sub_foo::Foo; pub use sub_foo::Foo;
pub use Baz = self::Bar; pub use self::Bar as Baz;
pub use sub_foo::Boz; pub use sub_foo::Boz;
pub use sub_foo::Bort; pub use sub_foo::Bort;

View file

@ -39,10 +39,10 @@ mod foo {
} }
pub mod bar { pub mod bar {
pub use e = foo::reexported_a; pub use foo::reexported_a as e;
pub use f = foo::reexported_b; pub use foo::reexported_b as f;
pub use g = foo::reexported_c; pub use foo::reexported_c as g;
pub use h = foo::reexported_d; pub use foo::reexported_d as h;
} }
pub static a: int = 0; pub static a: int = 0;

View file

@ -12,7 +12,7 @@
// move, when the struct implements Drop. // move, when the struct implements Drop.
// NoCopy // NoCopy
use NP = std::kinds::marker::NoCopy; use std::kinds::marker::NoCopy as NP;
struct S { a: int, np: NP } struct S { a: int, np: NP }

View file

@ -15,7 +15,7 @@
use bar::*; use bar::*;
mod bar { mod bar {
use import = self::fpriv; use self::fpriv as import;
fn fpriv() {} fn fpriv() {}
extern { extern {
fn epriv(); fn epriv();

View file

@ -10,7 +10,7 @@
// error-pattern:expected // error-pattern:expected
use baz = foo::{bar}; use foo::{bar} as baz;
mod foo { mod foo {
pub fn bar() {} pub fn bar() {}

View file

@ -10,7 +10,7 @@
// error-pattern:expected // error-pattern:expected
use baz = foo::*; use foo::* as baz;
mod foo { mod foo {
pub fn bar() {} pub fn bar() {}

View file

@ -12,8 +12,8 @@
// the `--test` harness creates modules with these textual names, but // the `--test` harness creates modules with these textual names, but
// they should be inaccessible from normal code. // they should be inaccessible from normal code.
use x = __test; //~ ERROR unresolved import `__test` use __test as x; //~ ERROR unresolved import `__test`
use y = __test_reexports; //~ ERROR unresolved import `__test_reexports` use __test_reexports as y; //~ ERROR unresolved import `__test_reexports`
#[test] #[test]
fn baz() {} fn baz() {}

View file

@ -8,7 +8,7 @@
// option. This file may not be copied, modified, or distributed // option. This file may not be copied, modified, or distributed
// except according to those terms. // except according to those terms.
use x = m::f; //~ ERROR unresolved import `m::f`. There is no `f` in `m` use m::f as x; //~ ERROR unresolved import `m::f`. There is no `f` in `m`
mod m {} mod m {}

View file

@ -16,7 +16,7 @@
extern crate libc; extern crate libc;
pub use x = extern_foo; pub use extern_foo as x;
extern { extern {
fn extern_foo(); fn extern_foo();
} }

View file

@ -148,8 +148,8 @@ mod internal_impl {
} }
/// dox /// dox
pub mod public_interface { pub mod public_interface {
pub use foo = internal_impl::documented; pub use internal_impl::documented as foo;
pub use bar = internal_impl::undocumented1; pub use internal_impl::undocumented1 as bar;
pub use internal_impl::{documented, undocumented2}; pub use internal_impl::{documented, undocumented2};
pub use internal_impl::globbed::*; pub use internal_impl::globbed::*;
} }

View file

@ -12,7 +12,7 @@
#![deny(unused_imports)] #![deny(unused_imports)]
#![allow(dead_code)] #![allow(dead_code)]
use cal = bar::c::cc; use bar::c::cc as cal;
use std::mem::*; // shouldn't get errors for not using use std::mem::*; // shouldn't get errors for not using
// everything imported // everything imported

View file

@ -43,7 +43,7 @@ mod n {
} }
fn h() { fn h() {
use not_okay = self::n::OKAY; use self::n::OKAY as not_okay;
let r = match (0,0) { let r = match (0,0) {
(0, not_okay) => 0, (0, not_okay) => 0,
//~^ ERROR static constant in pattern `not_okay` should have an uppercase name such as `NOT_OKAY` //~^ ERROR static constant in pattern `not_okay` should have an uppercase name such as `NOT_OKAY`

View file

@ -176,7 +176,7 @@ pub mod mytest {
//~^ NOTE: module `i` is private //~^ NOTE: module `i` is private
pub mod foo { pub mod foo {
pub use foo = self::i::A; pub use self::i::A as foo;
mod i { mod i {
pub struct A; pub struct A;

View file

@ -10,7 +10,7 @@
use foo::bar; //~ ERROR unresolved import `foo::bar`. Maybe a missing `extern crate foo`? use foo::bar; //~ ERROR unresolved import `foo::bar`. Maybe a missing `extern crate foo`?
use x = bar::baz; //~ ERROR unresolved import `bar::baz`. There is no `baz` in `bar` use bar::baz as x; //~ ERROR unresolved import `bar::baz`. There is no `baz` in `bar`
mod bar { mod bar {
struct bar; struct bar;

View file

@ -19,8 +19,8 @@ extern crate time;
use std::hashmap::{HashMap, HashSet}; use std::hashmap::{HashMap, HashSet};
use EBReader = rbml::reader; use rbml::reader as EBReader;
use EBWriter = rbml::writer; use rbml::writer as EBWriter;
use std::cmp::Eq; use std::cmp::Eq;
use std::cmp; use std::cmp;
use std::io; use std::io;

View file

@ -10,8 +10,8 @@
#![feature(macro_rules)] #![feature(macro_rules)]
use s = std::num::strconv; use std::num::strconv as s;
use to_string = std::num::strconv::float_to_str_common; use std::num::strconv::float_to_str_common as to_string;
macro_rules! t(($a:expr, $b:expr) => { { let (r, _) = $a; assert_eq!(r, $b.to_string()) } }) macro_rules! t(($a:expr, $b:expr) => { { let (r, _) = $a; assert_eq!(r, $b.to_string()) } })

View file

@ -11,5 +11,5 @@
pub fn main() { pub fn main() {
// Make sure that this view item is filtered out because otherwise it would // Make sure that this view item is filtered out because otherwise it would
// trigger a compilation error // trigger a compilation error
#[cfg(not_present)] use foo = bar; #[cfg(not_present)] use bar as foo;
} }

View file

@ -11,7 +11,7 @@
// Issue 4691: Ensure that functional-struct-updates operates // Issue 4691: Ensure that functional-struct-updates operates
// correctly and moves rather than copy when appropriate. // correctly and moves rather than copy when appropriate.
use NP = std::kinds::marker::NoCopy; use std::kinds::marker::NoCopy as NP;
struct ncint { np: NP, v: int } struct ncint { np: NP, v: int }
fn ncint(v: int) -> ncint { ncint { np: NP, v: v } } fn ncint(v: int) -> ncint { ncint { np: NP, v: v } }

View file

@ -16,7 +16,7 @@ mod foo {
mod bar { mod bar {
use foo::x; use foo::x;
use z = foo::x; use foo::x as z;
pub fn thing() { x(10); z(10); } pub fn thing() { x(10); z(10); }
} }

View file

@ -11,7 +11,7 @@
use foo::x; use foo::x;
use z = foo::x; use foo::x as z;
mod foo { mod foo {
pub fn x(y: int) { println!("{}", y); } pub fn x(y: int) { println!("{}", y); }

View file

@ -9,7 +9,7 @@
// except according to those terms. // except according to those terms.
pub use local_alias = local; pub use local as local_alias;
mod local { } mod local { }

View file

@ -38,7 +38,7 @@ mod m {
} }
fn g() { fn g() {
use AHA = self::m::aha; use self::m::aha as AHA;
let r = match (0,0) { let r = match (0,0) {
(0, AHA) => 0, (0, AHA) => 0,
(x, y) => 1 + x + y, (x, y) => 1 + x + y,

View file

@ -18,9 +18,9 @@ extern crate zed = "std";
use std::str; use std::str;
use x = zed::str; use zed::str as x;
mod baz { mod baz {
pub use x = std::str; pub use std::str as x;
} }
#[start] #[start]

View file

@ -12,7 +12,7 @@
extern crate xcrate_static_addresses; extern crate xcrate_static_addresses;
use other = xcrate_static_addresses; use xcrate_static_addresses as other;
pub fn main() { pub fn main() {
other::verify_same(&other::global); other::verify_same(&other::global);