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,

0 commit comments

Comments
 (0)