Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit eb2f1d9

Browse files
committedMar 24, 2015
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
1 parent ed81038 commit eb2f1d9

File tree

67 files changed

+144
-145
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

67 files changed

+144
-145
lines changed
 

‎src/librustc/metadata/creader.rs

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -73,22 +73,24 @@ struct CrateInfo {
7373
}
7474

7575
pub fn validate_crate_name(sess: Option<&Session>, s: &str, sp: Option<Span>) {
76-
let err = |s: &str| {
76+
let say = |s: &str, warn: bool| {
7777
match (sp, sess) {
7878
(_, None) => panic!("{}", s),
79+
(Some(sp), Some(sess)) if warn => sess.span_warn(sp, s),
7980
(Some(sp), Some(sess)) => sess.span_err(sp, s),
81+
(None, Some(sess)) if warn => sess.warn(s),
8082
(None, Some(sess)) => sess.err(s),
8183
}
8284
};
8385
if s.len() == 0 {
84-
err("crate name must not be empty");
85-
} else if s.char_at(0) == '-' {
86-
err(&format!("crate name cannot start with a hyphen: {}", s));
86+
say("crate name must not be empty", false);
87+
} else if s.contains("-") {
88+
say(&format!("crate names soon cannot contain hyphens: {}", s), true);
8789
}
8890
for c in s.chars() {
8991
if c.is_alphanumeric() { continue }
9092
if c == '_' || c == '-' { continue }
91-
err(&format!("invalid character `{}` in crate name: `{}`", c, s));
93+
say(&format!("invalid character `{}` in crate name: `{}`", c, s), false);
9294
}
9395
match sess {
9496
Some(sess) => sess.abort_if_errors(),
@@ -153,8 +155,9 @@ impl<'a> CrateReader<'a> {
153155
}
154156
}
155157

156-
// Traverses an AST, reading all the information about use'd crates and extern
157-
// libraries necessary for later resolving, typechecking, linking, etc.
158+
// Traverses an AST, reading all the information about use'd crates and
159+
// extern libraries necessary for later resolving, typechecking, linking,
160+
// etc.
158161
pub fn read_crates(&mut self, krate: &ast::Crate) {
159162
self.process_crate(krate);
160163
visit::walk_crate(self, krate);
@@ -184,11 +187,10 @@ impl<'a> CrateReader<'a> {
184187
debug!("resolving extern crate stmt. ident: {} path_opt: {:?}",
185188
ident, path_opt);
186189
let name = match *path_opt {
187-
Some((ref path_str, _)) => {
188-
let name = path_str.to_string();
189-
validate_crate_name(Some(self.sess), &name[..],
190+
Some(name) => {
191+
validate_crate_name(Some(self.sess), name.as_str(),
190192
Some(i.span));
191-
name
193+
name.as_str().to_string()
192194
}
193195
None => ident.to_string(),
194196
};
@@ -304,7 +306,13 @@ impl<'a> CrateReader<'a> {
304306
-> Option<ast::CrateNum> {
305307
let mut ret = None;
306308
self.sess.cstore.iter_crate_data(|cnum, data| {
307-
if data.name != name { return }
309+
// For now we do a "fuzzy match" on crate names by considering
310+
// hyphens equal to underscores. This is purely meant to be a
311+
// transitionary feature while we deprecate the quote syntax of
312+
// `extern crate` statements.
313+
if data.name != name.replace("-", "_") {
314+
return
315+
}
308316

309317
match hash {
310318
Some(hash) if *hash == data.hash() => { ret = Some(cnum); return }

‎src/librustc_trans/save/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1165,7 +1165,7 @@ impl<'l, 'tcx, 'v> Visitor<'v> for DxrVisitor<'l, 'tcx> {
11651165
let name = get_ident(item.ident);
11661166
let name = &name;
11671167
let location = match *s {
1168-
Some((ref s, _)) => s.to_string(),
1168+
Some(s) => s.to_string(),
11691169
None => name.to_string(),
11701170
};
11711171
let alias_span = self.span.span_for_last_ident(item.span);

0 commit comments

Comments
 (0)
Please sign in to comment.