Skip to content

Commit

Permalink
AST/HIR: Clarify what the optional name in extern crate items mean
Browse files Browse the repository at this point in the history
  • Loading branch information
petrochenkov committed Mar 17, 2018
1 parent 61b6bf5 commit c6c6cf9
Show file tree
Hide file tree
Showing 16 changed files with 42 additions and 58 deletions.
6 changes: 3 additions & 3 deletions src/librustc/hir/intravisit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -444,10 +444,10 @@ pub fn walk_item<'v, V: Visitor<'v>>(visitor: &mut V, item: &'v Item) {
visitor.visit_vis(&item.vis);
visitor.visit_name(item.span, item.name);
match item.node {
ItemExternCrate(opt_name) => {
ItemExternCrate(orig_name) => {
visitor.visit_id(item.id);
if let Some(name) = opt_name {
visitor.visit_name(item.span, name);
if let Some(orig_name) = orig_name {
visitor.visit_name(item.span, orig_name);
}
}
ItemUse(ref path, _) => {
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/hir/lowering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1904,7 +1904,7 @@ impl<'a> LoweringContext<'a> {
i: &ItemKind)
-> hir::Item_ {
match *i {
ItemKind::ExternCrate(string) => hir::ItemExternCrate(string),
ItemKind::ExternCrate(orig_name) => hir::ItemExternCrate(orig_name),
ItemKind::Use(ref use_tree) => {
// Start with an empty prefix
let prefix = Path {
Expand Down
4 changes: 2 additions & 2 deletions src/librustc/hir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2011,9 +2011,9 @@ pub struct Item {

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

/// `use foo::bar::*;` or `use foo::bar::baz as quux;`
Expand Down
11 changes: 3 additions & 8 deletions src/librustc/hir/print.rs
Original file line number Diff line number Diff line change
Expand Up @@ -524,15 +524,10 @@ impl<'a> State<'a> {
self.print_outer_attributes(&item.attrs)?;
self.ann.pre(self, NodeItem(item))?;
match item.node {
hir::ItemExternCrate(ref optional_path) => {
hir::ItemExternCrate(orig_name) => {
self.head(&visibility_qualified(&item.vis, "extern crate"))?;
if let Some(p) = *optional_path {
let val = p.as_str();
if val.contains("-") {
self.print_string(&val, ast::StrStyle::Cooked)?;
} else {
self.print_name(p)?;
}
if let Some(orig_name) = orig_name {
self.print_name(orig_name)?;
self.s.space()?;
self.s.word("as")?;
self.s.space()?;
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/ich/impls_hir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -851,7 +851,7 @@ impl<'a> HashStable<StableHashingContext<'a>> for hir::Item {
}

impl_stable_hash_for!(enum hir::Item_ {
ItemExternCrate(name),
ItemExternCrate(orig_name),
ItemUse(path, use_kind),
ItemStatic(ty, mutability, body_id),
ItemConst(ty, body_id),
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/session/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,7 @@ top_level_options!(
externs: Externs [UNTRACKED],
crate_name: Option<String> [TRACKED],
// An optional name to use as the crate for std during std injection,
// written `extern crate std = "name"`. Default to "std". Used by
// written `extern crate name as std`. Defaults to `std`. Used by
// out-of-tree drivers.
alt_std_name: Option<String> [TRACKED],
// Indicates how the compiler should treat unstable features
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_driver/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -683,7 +683,7 @@ pub fn phase_2_configure_and_expand_inner<'a, F>(sess: &'a Session,
});

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

Expand Down
16 changes: 9 additions & 7 deletions src/librustc_metadata/creader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1052,12 +1052,14 @@ impl<'a> middle::cstore::CrateLoader for CrateLoader<'a> {

fn process_item(&mut self, item: &ast::Item, definitions: &Definitions) {
match item.node {
ast::ItemKind::ExternCrate(rename) => {
debug!("resolving extern crate stmt. ident: {} rename: {:?}", item.ident, rename);
let rename = match rename {
Some(rename) => {
validate_crate_name(Some(self.sess), &rename.as_str(), Some(item.span));
rename
ast::ItemKind::ExternCrate(orig_name) => {
debug!("resolving extern crate stmt. ident: {} orig_name: {:?}",
item.ident, orig_name);
let orig_name = match orig_name {
Some(orig_name) => {
validate_crate_name(Some(self.sess), &orig_name.as_str(),
Some(item.span));
orig_name
}
None => item.ident.name,
};
Expand All @@ -1068,7 +1070,7 @@ impl<'a> middle::cstore::CrateLoader for CrateLoader<'a> {
};

let (cnum, ..) = self.resolve_crate(
&None, item.ident.name, rename, None, item.span, PathKind::Crate, dep_kind,
&None, item.ident.name, orig_name, None, item.span, PathKind::Crate, dep_kind,
);

let def_id = definitions.opt_local_def_id(item.id).unwrap();
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_resolve/build_reduced_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ impl<'a> Resolver<'a> {
);
}

ItemKind::ExternCrate(as_name) => {
ItemKind::ExternCrate(orig_name) => {
self.crate_loader.process_item(item, &self.definitions);

// n.b. we don't need to look at the path option here, because cstore already did
Expand All @@ -274,7 +274,7 @@ impl<'a> Resolver<'a> {
id: item.id,
parent,
imported_module: Cell::new(Some(module)),
subclass: ImportDirectiveSubclass::ExternCrate(as_name),
subclass: ImportDirectiveSubclass::ExternCrate(orig_name),
span: item.span,
module_path: Vec::new(),
vis: Cell::new(vis),
Expand Down
4 changes: 2 additions & 2 deletions src/librustdoc/visit_ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -406,13 +406,13 @@ impl<'a, 'tcx, 'rcx> RustdocVisitor<'a, 'tcx, 'rcx> {
// If we're inlining, skip private items.
_ if self.inlining && item.vis != hir::Public => {}
hir::ItemGlobalAsm(..) => {}
hir::ItemExternCrate(ref p) => {
hir::ItemExternCrate(orig_name) => {
let def_id = self.cx.tcx.hir.local_def_id(item.id);
om.extern_crates.push(ExternCrate {
cnum: self.cx.tcx.extern_mod_stmt_cnum(def_id)
.unwrap_or(LOCAL_CRATE),
name,
path: p.map(|x|x.to_string()),
path: orig_name.map(|x|x.to_string()),
vis: item.vis.clone(),
attrs: item.attrs.clone(),
whence: item.span,
Expand Down
2 changes: 1 addition & 1 deletion src/libsyntax/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2055,7 +2055,7 @@ pub struct Item {

#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
pub enum ItemKind {
/// An `extern crate` item, with optional original crate name.
/// An `extern crate` item, with optional *original* crate name if the crate was renamed.
///
/// E.g. `extern crate foo` or `extern crate foo_bar as foo`
ExternCrate(Option<Name>),
Expand Down
2 changes: 1 addition & 1 deletion src/libsyntax/fold.rs
Original file line number Diff line number Diff line change
Expand Up @@ -886,7 +886,7 @@ pub fn noop_fold_block<T: Folder>(b: P<Block>, folder: &mut T) -> P<Block> {

pub fn noop_fold_item_kind<T: Folder>(i: ItemKind, folder: &mut T) -> ItemKind {
match i {
ItemKind::ExternCrate(string) => ItemKind::ExternCrate(string),
ItemKind::ExternCrate(orig_name) => ItemKind::ExternCrate(orig_name),
ItemKind::Use(use_tree) => {
ItemKind::Use(use_tree.map(|tree| folder.fold_use_tree(tree)))
}
Expand Down
20 changes: 7 additions & 13 deletions src/libsyntax/parse/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6291,23 +6291,17 @@ impl<'a> Parser<'a> {
lo: Span,
visibility: Visibility,
attrs: Vec<Attribute>)
-> PResult<'a, P<Item>> {

let crate_name = self.parse_ident()?;
let (maybe_path, ident) = if let Some(ident) = self.parse_rename()? {
(Some(crate_name.name), ident)
-> PResult<'a, P<Item>> {
let orig_name = self.parse_ident()?;
let (item_name, orig_name) = if let Some(rename) = self.parse_rename()? {
(rename, Some(orig_name.name))
} else {
(None, crate_name)
(orig_name, None)
};
self.expect(&token::Semi)?;

let prev_span = self.prev_span;

Ok(self.mk_item(lo.to(prev_span),
ident,
ItemKind::ExternCrate(maybe_path),
visibility,
attrs))
let span = lo.to(self.prev_span);
Ok(self.mk_item(span, item_name, ItemKind::ExternCrate(orig_name), visibility, attrs))
}

/// Parse `extern` for foreign ABIs
Expand Down
11 changes: 3 additions & 8 deletions src/libsyntax/print/pprust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1174,15 +1174,10 @@ impl<'a> State<'a> {
self.print_outer_attributes(&item.attrs)?;
self.ann.pre(self, NodeItem(item))?;
match item.node {
ast::ItemKind::ExternCrate(ref optional_path) => {
ast::ItemKind::ExternCrate(orig_name) => {
self.head(&visibility_qualified(&item.vis, "extern crate"))?;
if let Some(p) = *optional_path {
let val = p.as_str();
if val.contains('-') {
self.print_string(&val, ast::StrStyle::Cooked)?;
} else {
self.print_name(p)?;
}
if let Some(orig_name) = orig_name {
self.print_name(orig_name)?;
self.s.space()?;
self.s.word("as")?;
self.s.space()?;
Expand Down
6 changes: 2 additions & 4 deletions src/libsyntax/std_inject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ thread_local! {
static INJECTED_CRATE_NAME: Cell<Option<&'static str>> = Cell::new(None);
}

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

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

let crate_name = Symbol::intern(&alt_std_name.unwrap_or_else(|| name.to_string()));

krate.module.items.insert(0, P(ast::Item {
attrs: vec![attr::mk_attr_outer(DUMMY_SP,
attr::mk_attr_id(),
attr::mk_word_item(Symbol::intern("macro_use")))],
vis: dummy_spanned(ast::VisibilityKind::Inherited),
node: ast::ItemKind::ExternCrate(Some(crate_name)),
node: ast::ItemKind::ExternCrate(alt_std_name.map(Symbol::intern)),
ident: ast::Ident::from_str(name),
id: ast::DUMMY_NODE_ID,
span: DUMMY_SP,
Expand Down
6 changes: 3 additions & 3 deletions src/libsyntax/visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,9 +213,9 @@ pub fn walk_item<'a, V: Visitor<'a>>(visitor: &mut V, item: &'a Item) {
visitor.visit_vis(&item.vis);
visitor.visit_ident(item.span, item.ident);
match item.node {
ItemKind::ExternCrate(opt_name) => {
if let Some(name) = opt_name {
visitor.visit_name(item.span, name);
ItemKind::ExternCrate(orig_name) => {
if let Some(orig_name) = orig_name {
visitor.visit_name(item.span, orig_name);
}
}
ItemKind::Use(ref use_tree) => {
Expand Down

0 comments on commit c6c6cf9

Please sign in to comment.