Skip to content

Commit c6c6cf9

Browse files
committed
AST/HIR: Clarify what the optional name in extern crate items mean
1 parent 61b6bf5 commit c6c6cf9

File tree

16 files changed

+42
-58
lines changed

16 files changed

+42
-58
lines changed

src/librustc/hir/intravisit.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -444,10 +444,10 @@ pub fn walk_item<'v, V: Visitor<'v>>(visitor: &mut V, item: &'v Item) {
444444
visitor.visit_vis(&item.vis);
445445
visitor.visit_name(item.span, item.name);
446446
match item.node {
447-
ItemExternCrate(opt_name) => {
447+
ItemExternCrate(orig_name) => {
448448
visitor.visit_id(item.id);
449-
if let Some(name) = opt_name {
450-
visitor.visit_name(item.span, name);
449+
if let Some(orig_name) = orig_name {
450+
visitor.visit_name(item.span, orig_name);
451451
}
452452
}
453453
ItemUse(ref path, _) => {

src/librustc/hir/lowering.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1904,7 +1904,7 @@ impl<'a> LoweringContext<'a> {
19041904
i: &ItemKind)
19051905
-> hir::Item_ {
19061906
match *i {
1907-
ItemKind::ExternCrate(string) => hir::ItemExternCrate(string),
1907+
ItemKind::ExternCrate(orig_name) => hir::ItemExternCrate(orig_name),
19081908
ItemKind::Use(ref use_tree) => {
19091909
// Start with an empty prefix
19101910
let prefix = Path {

src/librustc/hir/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2011,9 +2011,9 @@ pub struct Item {
20112011

20122012
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
20132013
pub enum Item_ {
2014-
/// An `extern crate` item, with optional original crate name,
2014+
/// An `extern crate` item, with optional *original* crate name if the crate was renamed.
20152015
///
2016-
/// e.g. `extern crate foo` or `extern crate foo_bar as foo`
2016+
/// E.g. `extern crate foo` or `extern crate foo_bar as foo`
20172017
ItemExternCrate(Option<Name>),
20182018

20192019
/// `use foo::bar::*;` or `use foo::bar::baz as quux;`

src/librustc/hir/print.rs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -524,15 +524,10 @@ impl<'a> State<'a> {
524524
self.print_outer_attributes(&item.attrs)?;
525525
self.ann.pre(self, NodeItem(item))?;
526526
match item.node {
527-
hir::ItemExternCrate(ref optional_path) => {
527+
hir::ItemExternCrate(orig_name) => {
528528
self.head(&visibility_qualified(&item.vis, "extern crate"))?;
529-
if let Some(p) = *optional_path {
530-
let val = p.as_str();
531-
if val.contains("-") {
532-
self.print_string(&val, ast::StrStyle::Cooked)?;
533-
} else {
534-
self.print_name(p)?;
535-
}
529+
if let Some(orig_name) = orig_name {
530+
self.print_name(orig_name)?;
536531
self.s.space()?;
537532
self.s.word("as")?;
538533
self.s.space()?;

src/librustc/ich/impls_hir.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -851,7 +851,7 @@ impl<'a> HashStable<StableHashingContext<'a>> for hir::Item {
851851
}
852852

853853
impl_stable_hash_for!(enum hir::Item_ {
854-
ItemExternCrate(name),
854+
ItemExternCrate(orig_name),
855855
ItemUse(path, use_kind),
856856
ItemStatic(ty, mutability, body_id),
857857
ItemConst(ty, body_id),

src/librustc/session/config.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,7 @@ top_level_options!(
385385
externs: Externs [UNTRACKED],
386386
crate_name: Option<String> [TRACKED],
387387
// An optional name to use as the crate for std during std injection,
388-
// written `extern crate std = "name"`. Default to "std". Used by
388+
// written `extern crate name as std`. Defaults to `std`. Used by
389389
// out-of-tree drivers.
390390
alt_std_name: Option<String> [TRACKED],
391391
// Indicates how the compiler should treat unstable features

src/librustc_driver/driver.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -683,7 +683,7 @@ pub fn phase_2_configure_and_expand_inner<'a, F>(sess: &'a Session,
683683
});
684684

685685
krate = time(sess, "crate injection", || {
686-
let alt_std_name = sess.opts.alt_std_name.clone();
686+
let alt_std_name = sess.opts.alt_std_name.as_ref().map(|s| &**s);
687687
syntax::std_inject::maybe_inject_crates_ref(krate, alt_std_name)
688688
});
689689

src/librustc_metadata/creader.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1052,12 +1052,14 @@ impl<'a> middle::cstore::CrateLoader for CrateLoader<'a> {
10521052

10531053
fn process_item(&mut self, item: &ast::Item, definitions: &Definitions) {
10541054
match item.node {
1055-
ast::ItemKind::ExternCrate(rename) => {
1056-
debug!("resolving extern crate stmt. ident: {} rename: {:?}", item.ident, rename);
1057-
let rename = match rename {
1058-
Some(rename) => {
1059-
validate_crate_name(Some(self.sess), &rename.as_str(), Some(item.span));
1060-
rename
1055+
ast::ItemKind::ExternCrate(orig_name) => {
1056+
debug!("resolving extern crate stmt. ident: {} orig_name: {:?}",
1057+
item.ident, orig_name);
1058+
let orig_name = match orig_name {
1059+
Some(orig_name) => {
1060+
validate_crate_name(Some(self.sess), &orig_name.as_str(),
1061+
Some(item.span));
1062+
orig_name
10611063
}
10621064
None => item.ident.name,
10631065
};
@@ -1068,7 +1070,7 @@ impl<'a> middle::cstore::CrateLoader for CrateLoader<'a> {
10681070
};
10691071

10701072
let (cnum, ..) = self.resolve_crate(
1071-
&None, item.ident.name, rename, None, item.span, PathKind::Crate, dep_kind,
1073+
&None, item.ident.name, orig_name, None, item.span, PathKind::Crate, dep_kind,
10721074
);
10731075

10741076
let def_id = definitions.opt_local_def_id(item.id).unwrap();

src/librustc_resolve/build_reduced_graph.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ impl<'a> Resolver<'a> {
255255
);
256256
}
257257

258-
ItemKind::ExternCrate(as_name) => {
258+
ItemKind::ExternCrate(orig_name) => {
259259
self.crate_loader.process_item(item, &self.definitions);
260260

261261
// n.b. we don't need to look at the path option here, because cstore already did
@@ -274,7 +274,7 @@ impl<'a> Resolver<'a> {
274274
id: item.id,
275275
parent,
276276
imported_module: Cell::new(Some(module)),
277-
subclass: ImportDirectiveSubclass::ExternCrate(as_name),
277+
subclass: ImportDirectiveSubclass::ExternCrate(orig_name),
278278
span: item.span,
279279
module_path: Vec::new(),
280280
vis: Cell::new(vis),

src/librustdoc/visit_ast.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -406,13 +406,13 @@ impl<'a, 'tcx, 'rcx> RustdocVisitor<'a, 'tcx, 'rcx> {
406406
// If we're inlining, skip private items.
407407
_ if self.inlining && item.vis != hir::Public => {}
408408
hir::ItemGlobalAsm(..) => {}
409-
hir::ItemExternCrate(ref p) => {
409+
hir::ItemExternCrate(orig_name) => {
410410
let def_id = self.cx.tcx.hir.local_def_id(item.id);
411411
om.extern_crates.push(ExternCrate {
412412
cnum: self.cx.tcx.extern_mod_stmt_cnum(def_id)
413413
.unwrap_or(LOCAL_CRATE),
414414
name,
415-
path: p.map(|x|x.to_string()),
415+
path: orig_name.map(|x|x.to_string()),
416416
vis: item.vis.clone(),
417417
attrs: item.attrs.clone(),
418418
whence: item.span,

src/libsyntax/ast.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2055,7 +2055,7 @@ pub struct Item {
20552055

20562056
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
20572057
pub enum ItemKind {
2058-
/// An `extern crate` item, with optional original crate name.
2058+
/// An `extern crate` item, with optional *original* crate name if the crate was renamed.
20592059
///
20602060
/// E.g. `extern crate foo` or `extern crate foo_bar as foo`
20612061
ExternCrate(Option<Name>),

src/libsyntax/fold.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -886,7 +886,7 @@ pub fn noop_fold_block<T: Folder>(b: P<Block>, folder: &mut T) -> P<Block> {
886886

887887
pub fn noop_fold_item_kind<T: Folder>(i: ItemKind, folder: &mut T) -> ItemKind {
888888
match i {
889-
ItemKind::ExternCrate(string) => ItemKind::ExternCrate(string),
889+
ItemKind::ExternCrate(orig_name) => ItemKind::ExternCrate(orig_name),
890890
ItemKind::Use(use_tree) => {
891891
ItemKind::Use(use_tree.map(|tree| folder.fold_use_tree(tree)))
892892
}

src/libsyntax/parse/parser.rs

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6291,23 +6291,17 @@ impl<'a> Parser<'a> {
62916291
lo: Span,
62926292
visibility: Visibility,
62936293
attrs: Vec<Attribute>)
6294-
-> PResult<'a, P<Item>> {
6295-
6296-
let crate_name = self.parse_ident()?;
6297-
let (maybe_path, ident) = if let Some(ident) = self.parse_rename()? {
6298-
(Some(crate_name.name), ident)
6294+
-> PResult<'a, P<Item>> {
6295+
let orig_name = self.parse_ident()?;
6296+
let (item_name, orig_name) = if let Some(rename) = self.parse_rename()? {
6297+
(rename, Some(orig_name.name))
62996298
} else {
6300-
(None, crate_name)
6299+
(orig_name, None)
63016300
};
63026301
self.expect(&token::Semi)?;
63036302

6304-
let prev_span = self.prev_span;
6305-
6306-
Ok(self.mk_item(lo.to(prev_span),
6307-
ident,
6308-
ItemKind::ExternCrate(maybe_path),
6309-
visibility,
6310-
attrs))
6303+
let span = lo.to(self.prev_span);
6304+
Ok(self.mk_item(span, item_name, ItemKind::ExternCrate(orig_name), visibility, attrs))
63116305
}
63126306

63136307
/// Parse `extern` for foreign ABIs

src/libsyntax/print/pprust.rs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1174,15 +1174,10 @@ impl<'a> State<'a> {
11741174
self.print_outer_attributes(&item.attrs)?;
11751175
self.ann.pre(self, NodeItem(item))?;
11761176
match item.node {
1177-
ast::ItemKind::ExternCrate(ref optional_path) => {
1177+
ast::ItemKind::ExternCrate(orig_name) => {
11781178
self.head(&visibility_qualified(&item.vis, "extern crate"))?;
1179-
if let Some(p) = *optional_path {
1180-
let val = p.as_str();
1181-
if val.contains('-') {
1182-
self.print_string(&val, ast::StrStyle::Cooked)?;
1183-
} else {
1184-
self.print_name(p)?;
1185-
}
1179+
if let Some(orig_name) = orig_name {
1180+
self.print_name(orig_name)?;
11861181
self.s.space()?;
11871182
self.s.word("as")?;
11881183
self.s.space()?;

src/libsyntax/std_inject.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ thread_local! {
4343
static INJECTED_CRATE_NAME: Cell<Option<&'static str>> = Cell::new(None);
4444
}
4545

46-
pub fn maybe_inject_crates_ref(mut krate: ast::Crate, alt_std_name: Option<String>) -> ast::Crate {
46+
pub fn maybe_inject_crates_ref(mut krate: ast::Crate, alt_std_name: Option<&str>) -> ast::Crate {
4747
let name = if attr::contains_name(&krate.attrs, "no_core") {
4848
return krate;
4949
} else if attr::contains_name(&krate.attrs, "no_std") {
@@ -54,14 +54,12 @@ pub fn maybe_inject_crates_ref(mut krate: ast::Crate, alt_std_name: Option<Strin
5454

5555
INJECTED_CRATE_NAME.with(|opt_name| opt_name.set(Some(name)));
5656

57-
let crate_name = Symbol::intern(&alt_std_name.unwrap_or_else(|| name.to_string()));
58-
5957
krate.module.items.insert(0, P(ast::Item {
6058
attrs: vec![attr::mk_attr_outer(DUMMY_SP,
6159
attr::mk_attr_id(),
6260
attr::mk_word_item(Symbol::intern("macro_use")))],
6361
vis: dummy_spanned(ast::VisibilityKind::Inherited),
64-
node: ast::ItemKind::ExternCrate(Some(crate_name)),
62+
node: ast::ItemKind::ExternCrate(alt_std_name.map(Symbol::intern)),
6563
ident: ast::Ident::from_str(name),
6664
id: ast::DUMMY_NODE_ID,
6765
span: DUMMY_SP,

src/libsyntax/visit.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -213,9 +213,9 @@ pub fn walk_item<'a, V: Visitor<'a>>(visitor: &mut V, item: &'a Item) {
213213
visitor.visit_vis(&item.vis);
214214
visitor.visit_ident(item.span, item.ident);
215215
match item.node {
216-
ItemKind::ExternCrate(opt_name) => {
217-
if let Some(name) = opt_name {
218-
visitor.visit_name(item.span, name);
216+
ItemKind::ExternCrate(orig_name) => {
217+
if let Some(orig_name) = orig_name {
218+
visitor.visit_name(item.span, orig_name);
219219
}
220220
}
221221
ItemKind::Use(ref use_tree) => {

0 commit comments

Comments
 (0)