diff options
| author | Alex Crichton <alex@alexcrichton.com> | 2015-03-19 15:39:03 -0700 |
|---|---|---|
| committer | Alex Crichton <alex@alexcrichton.com> | 2015-03-24 14:55:15 -0700 |
| commit | eb2f1d925ffdb79d45c7b74cef549e54533c3951 (patch) | |
| tree | a517d12dbb1458f49e21ae40e026d699576dce92 /src/libsyntax | |
| parent | ed810385045ab0db90303574ba3ea47dfa2a36d5 (diff) | |
| download | rust-eb2f1d925ffdb79d45c7b74cef549e54533c3951.tar.gz rust-eb2f1d925ffdb79d45c7b74cef549e54533c3951.zip | |
rustc: Add support for `extern crate foo as bar`
The compiler will now issue a warning for crates that have syntax of the form `extern crate "foo" as bar`, but it will still continue to accept this syntax. Additionally, the string `foo-bar` will match the crate name `foo_bar` to assist in the transition period as well. This patch will land hopefully in tandem with a Cargo patch that will start translating all crate names to have underscores instead of hyphens. cc #23533
Diffstat (limited to 'src/libsyntax')
| -rw-r--r-- | src/libsyntax/ast.rs | 4 | ||||
| -rw-r--r-- | src/libsyntax/parse/mod.rs | 2 | ||||
| -rw-r--r-- | src/libsyntax/parse/obsolete.rs | 6 | ||||
| -rw-r--r-- | src/libsyntax/parse/parser.rs | 32 | ||||
| -rw-r--r-- | src/libsyntax/print/pprust.rs | 9 | ||||
| -rw-r--r-- | src/libsyntax/std_inject.rs | 6 |
6 files changed, 31 insertions, 28 deletions
diff --git a/src/libsyntax/ast.rs b/src/libsyntax/ast.rs index 704abc43f10..fec67c7aef4 100644 --- a/src/libsyntax/ast.rs +++ b/src/libsyntax/ast.rs @@ -1771,8 +1771,8 @@ pub struct Item { pub enum Item_ { /// An`extern crate` item, with optional original crate name, /// - /// e.g. `extern crate foo` or `extern crate "foo-bar" as foo` - ItemExternCrate(Option<(InternedString, StrStyle)>), + /// e.g. `extern crate foo` or `extern crate foo_bar as foo` + ItemExternCrate(Option<Name>), /// A `use` or `pub use` item ItemUse(P<ViewPath>), diff --git a/src/libsyntax/parse/mod.rs b/src/libsyntax/parse/mod.rs index 968d2fd7e2a..4e851761212 100644 --- a/src/libsyntax/parse/mod.rs +++ b/src/libsyntax/parse/mod.rs @@ -1060,7 +1060,7 @@ mod test { let vitem_s = item_to_string(&*vitem); assert_eq!(&vitem_s[..], ex_s); - let ex_s = "extern crate \"foo\" as bar;"; + let ex_s = "extern crate foo as bar;"; let vitem = string_to_item(ex_s.to_string()).unwrap(); let vitem_s = item_to_string(&*vitem); assert_eq!(&vitem_s[..], ex_s); diff --git a/src/libsyntax/parse/obsolete.rs b/src/libsyntax/parse/obsolete.rs index 7da0a6de547..276be73823a 100644 --- a/src/libsyntax/parse/obsolete.rs +++ b/src/libsyntax/parse/obsolete.rs @@ -24,6 +24,7 @@ use ptr::P; pub enum ObsoleteSyntax { ClosureKind, EmptyIndex, + ExternCrateString, } pub trait ParserObsoleteMethods { @@ -56,6 +57,11 @@ impl<'a> ParserObsoleteMethods for parser::Parser<'a> { "write `[..]` instead", false, // warning for now ), + ObsoleteSyntax::ExternCrateString => ( + "\"crate-name\"", + "use an identifier not in quotes instead", + false, // warning for now + ), }; self.report(sp, kind, kind_str, desc, error); diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index 4ae5e0faa31..fe259d0bf78 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -4986,37 +4986,28 @@ impl<'a> Parser<'a> { attrs: Vec<Attribute>) -> P<Item> { - let span = self.span; let (maybe_path, ident) = match self.token { token::Ident(..) => { - let the_ident = self.parse_ident(); - let path = if self.eat_keyword_noexpect(keywords::As) { - // skip the ident if there is one - if self.token.is_ident() { self.bump(); } - - self.span_err(span, "expected `;`, found `as`"); - self.fileline_help(span, - &format!("perhaps you meant to enclose the crate name `{}` in \ - a string?", - the_ident.as_str())); - None + let crate_name = self.parse_ident(); + if self.eat_keyword(keywords::As) { + (Some(crate_name.name), self.parse_ident()) } else { - None - }; - self.expect(&token::Semi); - (path, the_ident) + (None, crate_name) + } }, - token::Literal(token::Str_(..), suf) | token::Literal(token::StrRaw(..), suf) => { + token::Literal(token::Str_(..), suf) | + token::Literal(token::StrRaw(..), suf) => { let sp = self.span; self.expect_no_suffix(sp, "extern crate name", suf); // forgo the internal suffix check of `parse_str` to // avoid repeats (this unwrap will always succeed due // to the restriction of the `match`) - let (s, style, _) = self.parse_optional_str().unwrap(); + let (s, _, _) = self.parse_optional_str().unwrap(); self.expect_keyword(keywords::As); let the_ident = self.parse_ident(); - self.expect(&token::Semi); - (Some((s, style)), the_ident) + self.obsolete(sp, ObsoleteSyntax::ExternCrateString); + let s = token::intern(&s); + (Some(s), the_ident) }, _ => { let span = self.span; @@ -5027,6 +5018,7 @@ impl<'a> Parser<'a> { token_str)); } }; + self.expect(&token::Semi); let last_span = self.last_span; self.mk_item(lo, diff --git a/src/libsyntax/print/pprust.rs b/src/libsyntax/print/pprust.rs index 828d085fd43..f6213b7db40 100644 --- a/src/libsyntax/print/pprust.rs +++ b/src/libsyntax/print/pprust.rs @@ -821,8 +821,13 @@ impl<'a> State<'a> { ast::ItemExternCrate(ref optional_path) => { try!(self.head(&visibility_qualified(item.vis, "extern crate"))); - if let Some((ref p, style)) = *optional_path { - try!(self.print_string(p, style)); + if let Some(p) = *optional_path { + let val = token::get_name(p); + if val.contains("-") { + try!(self.print_string(&val, ast::CookedStr)); + } else { + try!(self.print_name(p)); + } try!(space(&mut self.s)); try!(word(&mut self.s, "as")); try!(space(&mut self.s)); diff --git a/src/libsyntax/std_inject.rs b/src/libsyntax/std_inject.rs index ac7cdb1b413..0fa7e4f902c 100644 --- a/src/libsyntax/std_inject.rs +++ b/src/libsyntax/std_inject.rs @@ -54,8 +54,8 @@ impl fold::Folder for StandardLibraryInjector { // The name to use in `extern crate "name" as std;` let actual_crate_name = match self.alt_std_name { - Some(ref s) => token::intern_and_get_ident(&s[..]), - None => token::intern_and_get_ident("std"), + Some(ref s) => token::intern(&s), + None => token::intern("std"), }; krate.module.items.insert(0, P(ast::Item { @@ -64,7 +64,7 @@ impl fold::Folder for StandardLibraryInjector { attrs: vec!( attr::mk_attr_outer(attr::mk_attr_id(), attr::mk_word_item( InternedString::new("macro_use")))), - node: ast::ItemExternCrate(Some((actual_crate_name, ast::CookedStr))), + node: ast::ItemExternCrate(Some(actual_crate_name)), vis: ast::Inherited, span: DUMMY_SP })); |
