Skip to content

Commit

Permalink
librustc: Remove the ability for extern crate to specify crate IDs.
Browse files Browse the repository at this point in the history
Issue rust-lang#14468.

After a snapshot, it will be possible to remove the `extern crate foo =
STRING` syntax in favor of the new `extern crate foo = IDENT`.

This will break crates that have names that are not valid Rust
identifiers (in particular, those that contain hyphens). Change such
crates to have names that correspond to valid Rust identifiers. (Most
commonly, this will involve changing hyphens to underscores.)

[breaking-change]
  • Loading branch information
pcwalton committed Jun 14, 2014
1 parent 1cde9d8 commit 4204add
Show file tree
Hide file tree
Showing 197 changed files with 240 additions and 443 deletions.
8 changes: 3 additions & 5 deletions src/doc/rust.md
Original file line number Diff line number Diff line change
Expand Up @@ -817,7 +817,7 @@ There are several kinds of view item:
##### Extern crate declarations

~~~~ {.ebnf .gram}
extern_crate_decl : "extern" "crate" ident [ '(' link_attrs ')' ] ? [ '=' string_lit ] ? ;
extern_crate_decl : "extern" "crate" ident [ '(' link_attrs ')' ] ? [ '=' ident ] ? ;
link_attrs : link_attr [ ',' link_attrs ] + ;
link_attr : ident '=' literal ;
~~~~
Expand All @@ -839,11 +839,9 @@ Four examples of `extern crate` declarations:
~~~~ {.ignore}
extern crate pcre;
extern crate std; // equivalent to: extern crate std = "std";
extern crate std; // equivalent to: extern crate std = std;
extern crate ruststd = "std"; // linking to 'std' under another name
extern crate foo = "some/where/rust-foo#foo:1.0"; // a full crate ID for external tools
extern crate ruststd = std; // linking to 'std' under another name
~~~~

##### Use declarations
Expand Down
18 changes: 5 additions & 13 deletions src/librustc/front/std_inject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,23 +60,15 @@ struct StandardLibraryInjector<'a> {
sess: &'a Session,
}

pub fn with_version(krate: &str) -> Option<(InternedString, ast::StrStyle)> {
// FIXME(pcwalton): Making `Ident`s out of this stuff is pretty bogus. What
// should we do instead?
pub fn with_version(krate: &str) -> Option<ast::Ident> {
match option_env!("CFG_DISABLE_INJECT_STD_VERSION") {
Some("1") => None,
#[cfg(stage0)]
_ => {
Some((token::intern_and_get_ident(format!("{}\\#{}",
krate,
VERSION).as_slice()),
ast::CookedStr))
}
_ => Some(token::str_to_ident(krate)),
#[cfg(not(stage0))]
_ => {
Some((token::intern_and_get_ident(format!("{}#{}",
krate,
VERSION).as_slice()),
ast::CookedStr))
}
_ => Some(token::str_to_ident(krate)),
}
}

Expand Down
5 changes: 3 additions & 2 deletions src/librustc/metadata/creader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,9 @@ fn extract_crate_info(e: &Env, i: &ast::ViewItem) -> Option<CrateInfo> {
debug!("resolving extern crate stmt. ident: {:?} path_opt: {:?}",
ident, path_opt);
let crate_id = match *path_opt {
Some((ref path_str, _)) => {
let crateid: Option<CrateId> = from_str(path_str.get());
Some(ref path_str) => {
let crateid: Option<CrateId> =
from_str(token::get_ident(*path_str).get());
match crateid {
None => {
e.sess.span_err(i.span, "malformed crate id");
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/middle/save/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1136,7 +1136,7 @@ impl<'l> Visitor<DxrVisitorEnv> for DxrVisitor<'l> {
ast::ViewItemExternCrate(ident, ref s, id) => {
let name = get_ident(ident).get().to_owned();
let s = match *s {
Some((ref s, _)) => s.get().to_owned(),
Some(ref s) => get_ident(*s).get().to_owned(),
None => name.to_owned(),
};
let sub_span = self.span.sub_span_after_keyword(i.span, keywords::Crate);
Expand Down
4 changes: 3 additions & 1 deletion src/librustdoc/clean/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1808,7 +1808,9 @@ impl Clean<ViewItemInner> for ast::ViewItem_ {
&ast::ViewItemExternCrate(ref i, ref p, ref id) => {
let string = match *p {
None => None,
Some((ref x, _)) => Some(x.get().to_string()),
Some(ref ident) => {
Some(token::get_ident(*ident).get().to_string())
}
};
ExternCrate(i.clean(), string, *id)
}
Expand Down
9 changes: 4 additions & 5 deletions src/libsyntax/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -974,11 +974,10 @@ pub struct ViewItem {

#[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash)]
pub enum ViewItem_ {
// ident: name used to refer to this crate in the code
// optional (InternedString,StrStyle): if present, this is a location
// (containing arbitrary characters) from which to fetch the crate sources
// For example, extern crate whatever = "github.com/mozilla/rust"
ViewItemExternCrate(Ident, Option<(InternedString,StrStyle)>, NodeId),
// required Ident: name used to refer to this crate in the code
// optional Ident: name from which to fetch the crate sources
// For example, extern crate whatever = foo;
ViewItemExternCrate(Ident, Option<Ident>, NodeId),
ViewItemUse(Gc<ViewPath>),
}

Expand Down
14 changes: 11 additions & 3 deletions src/libsyntax/parse/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4474,7 +4474,7 @@ impl<'a> Parser<'a> {
/// # Example
///
/// extern crate url;
/// extern crate foo = "bar";
/// extern crate foo = bar;
fn parse_item_extern_crate(&mut self,
lo: BytePos,
visibility: Visibility,
Expand All @@ -4487,8 +4487,16 @@ impl<'a> Parser<'a> {
self.expect_one_of(&[], &[token::EQ, token::SEMI]);
let path = if self.token == token::EQ {
self.bump();
Some(self.parse_str())
} else {None};
if is_ident(&self.token) {
Some(self.parse_ident())
} else {
// NOTE(pcwalton, stage0): Remove after snapshot.
let (string, _) = self.parse_str();
Some(token::str_to_ident(string.get()))
}
} else {
None
};

self.expect(&token::SEMI);
(path, the_ident)
Expand Down
4 changes: 2 additions & 2 deletions src/libsyntax/print/pprust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2107,11 +2107,11 @@ impl<'a> State<'a> {
ast::ViewItemExternCrate(id, ref optional_path, _) => {
try!(self.head("extern crate"));
try!(self.print_ident(id));
for &(ref p, style) in optional_path.iter() {
for ident in optional_path.iter() {
try!(space(&mut self.s));
try!(word(&mut self.s, "="));
try!(space(&mut self.s));
try!(self.print_string(p.get(), style));
try!(self.print_ident(*ident));
}
}

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// aux-build:crateresolve4a-1.rs
// aux-build:crateresolve4a-2.rs
// aux-build:crateresolve4a_1.rs
// aux-build:crateresolve4a_2.rs
#![crate_id="crateresolve4b#0.1"]
#![crate_type = "lib"]

extern crate crateresolve4a = "crateresolve4a#0.2";
extern crate crateresolve4a = crateresolve4a#0.2;

pub fn f() -> int { crateresolve4a::g() }
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// aux-build:crateresolve4a-1.rs
// aux-build:crateresolve4a-2.rs
// aux-build:crateresolve4a_1.rs
// aux-build:crateresolve4a_2.rs
#![crate_id="crateresolve4b#0.2"]
#![crate_type = "lib"]

extern crate crateresolve4a = "crateresolve4a#0.1";
extern crate crateresolve4a = crateresolve4a#0.1;

pub fn g() -> int { crateresolve4a::f() }
File renamed without changes.
File renamed without changes.
4 changes: 2 additions & 2 deletions src/test/auxiliary/crateresolve7x.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// aux-build:crateresolve_calories-1.rs
// aux-build:crateresolve_calories-2.rs
// aux-build:crateresolve_calories_1.rs
// aux-build:crateresolve_calories_2.rs

// These both have the same version but differ in other metadata
pub mod a {
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#![crate_id="externcallback#0.1"]
#![crate_id="externcallback"]
#![crate_type = "lib"]

extern crate libc;
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@

#![crate_type = "dylib"]

extern crate a = "issue-12133-rlib";
extern crate b = "issue-12133-dylib";
extern crate a = issue_12133_rlib;
extern crate b = issue_12133_dylib;

File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@
#![crate_type = "rlib"]
#![feature(phase)]

#[phase(plugin)] extern crate t1 = "issue-13560-1";
#[phase(plugin, link)] extern crate t2 = "issue-13560-2";
#[phase(plugin)] extern crate t1 = issue_13560_1;
#[phase(plugin, link)] extern crate t2 = issue_13560_2;

File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

extern crate crate1 = "issue-13620-1";
extern crate crate1 = issue_13620_1;

pub static FOO2: crate1::Foo = crate1::FOO;
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

extern crate foo = "issue-13872-1";
extern crate foo = issue_13872_1;

pub use foo::B;
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

extern crate bar = "issue-13872-2";
extern crate bar = issue_13872_2;

use bar::B;

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// aux-build:issue-9906.rs
// aux-build:issue_9906.rs

pub use other::FooBar;
pub use other::foo;
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#![crate_id="static-function-pointer-aux"]
#![crate_id="static_function_pointer_aux"]

pub fn f(x: int) -> int { -x }

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#![crate_type = "dylib"]
#![feature(plugin_registrar, quote, globs)]

extern crate other = "syntax-extension-with-dll-deps-1";
extern crate other = syntax_extension_with_dll_deps_1;
extern crate syntax;
extern crate rustc;

Expand Down
2 changes: 1 addition & 1 deletion src/test/auxiliary/trait_default_method_xc_aux_2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

// aux-build:trait_default_method_xc_aux.rs

extern crate aux = "trait_default_method_xc_aux";
extern crate aux = trait_default_method_xc_aux;
use aux::A;

pub struct a_struct { pub x: int }
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
3 changes: 1 addition & 2 deletions src/test/compile-fail/bad-crate-id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

extern crate foo = ""; //~ ERROR: malformed crate id
extern crate bar = "#a"; //~ ERROR: malformed crate id
extern crate foo = ; //~ ERROR expected string literal

fn main() {}
6 changes: 3 additions & 3 deletions src/test/compile-fail/changing-crates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
// except according to those terms.

// note that these aux-build directives must be in this order
// aux-build:changing-crates-a1.rs
// aux-build:changing-crates-b.rs
// aux-build:changing-crates-a2.rs
// aux-build:changing_crates_a1.rs
// aux-build:changing_crates_b.rs
// aux-build:changing_crates_a2.rs

extern crate a;
extern crate b; //~ ERROR: found possibly newer version of crate `a` which `b` depends on
Expand Down
6 changes: 3 additions & 3 deletions src/test/compile-fail/crateresolve1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// aux-build:crateresolve1-1.rs
// aux-build:crateresolve1-2.rs
// aux-build:crateresolve1-3.rs
// aux-build:crateresolve1_1.rs
// aux-build:crateresolve1_2.rs
// aux-build:crateresolve1_3.rs
// error-pattern:multiple matching crates for `crateresolve1`

extern crate crateresolve1;
Expand Down
24 changes: 0 additions & 24 deletions src/test/compile-fail/crateresolve2.rs

This file was deleted.

21 changes: 0 additions & 21 deletions src/test/compile-fail/crateresolve5.rs

This file was deleted.

4 changes: 2 additions & 2 deletions src/test/compile-fail/issue-11680.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// aux-build:issue-11680.rs
// aux-build:issue_11680.rs

extern crate other = "issue-11680";
extern crate other = issue_11680;

fn main() {
let _b = other::Bar(1);
Expand Down
2 changes: 1 addition & 1 deletion src/test/compile-fail/issue-11908-1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// aux-build:issue-11908-1.rs
// aux-build:issue_11908_1.rs
// ignore-android this test is incompatible with the android test runner
// error-pattern: multiple dylib candidates for `url` found

Expand Down
2 changes: 1 addition & 1 deletion src/test/compile-fail/issue-11908-2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// aux-build:issue-11908-2.rs
// aux-build:issue_11908_2.rs
// no-prefer-dynamic
// ignore-android this test is incompatible with the android test runner
// error-pattern: multiple rlib candidates for `url` found
Expand Down
4 changes: 2 additions & 2 deletions src/test/compile-fail/issue-12612.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// aux-build:issue-12612-1.rs
// aux-build:issue_12612_1.rs

extern crate foo = "issue-12612-1";
extern crate foo = issue_12612_1;

use foo::bar;

Expand Down
Loading

0 comments on commit 4204add

Please sign in to comment.