AST/HIR: Clarify what the optional name in extern crate items mean
This commit is contained in:
parent
61b6bf54fd
commit
c6c6cf9515
16 changed files with 42 additions and 58 deletions
|
@ -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_vis(&item.vis);
|
||||||
visitor.visit_name(item.span, item.name);
|
visitor.visit_name(item.span, item.name);
|
||||||
match item.node {
|
match item.node {
|
||||||
ItemExternCrate(opt_name) => {
|
ItemExternCrate(orig_name) => {
|
||||||
visitor.visit_id(item.id);
|
visitor.visit_id(item.id);
|
||||||
if let Some(name) = opt_name {
|
if let Some(orig_name) = orig_name {
|
||||||
visitor.visit_name(item.span, name);
|
visitor.visit_name(item.span, orig_name);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
ItemUse(ref path, _) => {
|
ItemUse(ref path, _) => {
|
||||||
|
|
|
@ -1904,7 +1904,7 @@ impl<'a> LoweringContext<'a> {
|
||||||
i: &ItemKind)
|
i: &ItemKind)
|
||||||
-> hir::Item_ {
|
-> hir::Item_ {
|
||||||
match *i {
|
match *i {
|
||||||
ItemKind::ExternCrate(string) => hir::ItemExternCrate(string),
|
ItemKind::ExternCrate(orig_name) => hir::ItemExternCrate(orig_name),
|
||||||
ItemKind::Use(ref use_tree) => {
|
ItemKind::Use(ref use_tree) => {
|
||||||
// Start with an empty prefix
|
// Start with an empty prefix
|
||||||
let prefix = Path {
|
let prefix = Path {
|
||||||
|
|
|
@ -2011,9 +2011,9 @@ pub struct Item {
|
||||||
|
|
||||||
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
|
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
|
||||||
pub enum Item_ {
|
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>),
|
ItemExternCrate(Option<Name>),
|
||||||
|
|
||||||
/// `use foo::bar::*;` or `use foo::bar::baz as quux;`
|
/// `use foo::bar::*;` or `use foo::bar::baz as quux;`
|
||||||
|
|
|
@ -524,15 +524,10 @@ impl<'a> State<'a> {
|
||||||
self.print_outer_attributes(&item.attrs)?;
|
self.print_outer_attributes(&item.attrs)?;
|
||||||
self.ann.pre(self, NodeItem(item))?;
|
self.ann.pre(self, NodeItem(item))?;
|
||||||
match item.node {
|
match item.node {
|
||||||
hir::ItemExternCrate(ref optional_path) => {
|
hir::ItemExternCrate(orig_name) => {
|
||||||
self.head(&visibility_qualified(&item.vis, "extern crate"))?;
|
self.head(&visibility_qualified(&item.vis, "extern crate"))?;
|
||||||
if let Some(p) = *optional_path {
|
if let Some(orig_name) = orig_name {
|
||||||
let val = p.as_str();
|
self.print_name(orig_name)?;
|
||||||
if val.contains("-") {
|
|
||||||
self.print_string(&val, ast::StrStyle::Cooked)?;
|
|
||||||
} else {
|
|
||||||
self.print_name(p)?;
|
|
||||||
}
|
|
||||||
self.s.space()?;
|
self.s.space()?;
|
||||||
self.s.word("as")?;
|
self.s.word("as")?;
|
||||||
self.s.space()?;
|
self.s.space()?;
|
||||||
|
|
|
@ -851,7 +851,7 @@ impl<'a> HashStable<StableHashingContext<'a>> for hir::Item {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl_stable_hash_for!(enum hir::Item_ {
|
impl_stable_hash_for!(enum hir::Item_ {
|
||||||
ItemExternCrate(name),
|
ItemExternCrate(orig_name),
|
||||||
ItemUse(path, use_kind),
|
ItemUse(path, use_kind),
|
||||||
ItemStatic(ty, mutability, body_id),
|
ItemStatic(ty, mutability, body_id),
|
||||||
ItemConst(ty, body_id),
|
ItemConst(ty, body_id),
|
||||||
|
|
|
@ -385,7 +385,7 @@ top_level_options!(
|
||||||
externs: Externs [UNTRACKED],
|
externs: Externs [UNTRACKED],
|
||||||
crate_name: Option<String> [TRACKED],
|
crate_name: Option<String> [TRACKED],
|
||||||
// An optional name to use as the crate for std during std injection,
|
// 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.
|
// out-of-tree drivers.
|
||||||
alt_std_name: Option<String> [TRACKED],
|
alt_std_name: Option<String> [TRACKED],
|
||||||
// Indicates how the compiler should treat unstable features
|
// Indicates how the compiler should treat unstable features
|
||||||
|
|
|
@ -683,7 +683,7 @@ pub fn phase_2_configure_and_expand_inner<'a, F>(sess: &'a Session,
|
||||||
});
|
});
|
||||||
|
|
||||||
krate = time(sess, "crate injection", || {
|
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)
|
syntax::std_inject::maybe_inject_crates_ref(krate, alt_std_name)
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
@ -1052,12 +1052,14 @@ impl<'a> middle::cstore::CrateLoader for CrateLoader<'a> {
|
||||||
|
|
||||||
fn process_item(&mut self, item: &ast::Item, definitions: &Definitions) {
|
fn process_item(&mut self, item: &ast::Item, definitions: &Definitions) {
|
||||||
match item.node {
|
match item.node {
|
||||||
ast::ItemKind::ExternCrate(rename) => {
|
ast::ItemKind::ExternCrate(orig_name) => {
|
||||||
debug!("resolving extern crate stmt. ident: {} rename: {:?}", item.ident, rename);
|
debug!("resolving extern crate stmt. ident: {} orig_name: {:?}",
|
||||||
let rename = match rename {
|
item.ident, orig_name);
|
||||||
Some(rename) => {
|
let orig_name = match orig_name {
|
||||||
validate_crate_name(Some(self.sess), &rename.as_str(), Some(item.span));
|
Some(orig_name) => {
|
||||||
rename
|
validate_crate_name(Some(self.sess), &orig_name.as_str(),
|
||||||
|
Some(item.span));
|
||||||
|
orig_name
|
||||||
}
|
}
|
||||||
None => item.ident.name,
|
None => item.ident.name,
|
||||||
};
|
};
|
||||||
|
@ -1068,7 +1070,7 @@ impl<'a> middle::cstore::CrateLoader for CrateLoader<'a> {
|
||||||
};
|
};
|
||||||
|
|
||||||
let (cnum, ..) = self.resolve_crate(
|
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();
|
let def_id = definitions.opt_local_def_id(item.id).unwrap();
|
||||||
|
|
|
@ -255,7 +255,7 @@ impl<'a> Resolver<'a> {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
ItemKind::ExternCrate(as_name) => {
|
ItemKind::ExternCrate(orig_name) => {
|
||||||
self.crate_loader.process_item(item, &self.definitions);
|
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
|
// 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> {
|
||||||
id: item.id,
|
id: item.id,
|
||||||
parent,
|
parent,
|
||||||
imported_module: Cell::new(Some(module)),
|
imported_module: Cell::new(Some(module)),
|
||||||
subclass: ImportDirectiveSubclass::ExternCrate(as_name),
|
subclass: ImportDirectiveSubclass::ExternCrate(orig_name),
|
||||||
span: item.span,
|
span: item.span,
|
||||||
module_path: Vec::new(),
|
module_path: Vec::new(),
|
||||||
vis: Cell::new(vis),
|
vis: Cell::new(vis),
|
||||||
|
|
|
@ -406,13 +406,13 @@ impl<'a, 'tcx, 'rcx> RustdocVisitor<'a, 'tcx, 'rcx> {
|
||||||
// If we're inlining, skip private items.
|
// If we're inlining, skip private items.
|
||||||
_ if self.inlining && item.vis != hir::Public => {}
|
_ if self.inlining && item.vis != hir::Public => {}
|
||||||
hir::ItemGlobalAsm(..) => {}
|
hir::ItemGlobalAsm(..) => {}
|
||||||
hir::ItemExternCrate(ref p) => {
|
hir::ItemExternCrate(orig_name) => {
|
||||||
let def_id = self.cx.tcx.hir.local_def_id(item.id);
|
let def_id = self.cx.tcx.hir.local_def_id(item.id);
|
||||||
om.extern_crates.push(ExternCrate {
|
om.extern_crates.push(ExternCrate {
|
||||||
cnum: self.cx.tcx.extern_mod_stmt_cnum(def_id)
|
cnum: self.cx.tcx.extern_mod_stmt_cnum(def_id)
|
||||||
.unwrap_or(LOCAL_CRATE),
|
.unwrap_or(LOCAL_CRATE),
|
||||||
name,
|
name,
|
||||||
path: p.map(|x|x.to_string()),
|
path: orig_name.map(|x|x.to_string()),
|
||||||
vis: item.vis.clone(),
|
vis: item.vis.clone(),
|
||||||
attrs: item.attrs.clone(),
|
attrs: item.attrs.clone(),
|
||||||
whence: item.span,
|
whence: item.span,
|
||||||
|
|
|
@ -2055,7 +2055,7 @@ pub struct Item {
|
||||||
|
|
||||||
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
|
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
|
||||||
pub enum ItemKind {
|
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`
|
/// E.g. `extern crate foo` or `extern crate foo_bar as foo`
|
||||||
ExternCrate(Option<Name>),
|
ExternCrate(Option<Name>),
|
||||||
|
|
|
@ -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 {
|
pub fn noop_fold_item_kind<T: Folder>(i: ItemKind, folder: &mut T) -> ItemKind {
|
||||||
match i {
|
match i {
|
||||||
ItemKind::ExternCrate(string) => ItemKind::ExternCrate(string),
|
ItemKind::ExternCrate(orig_name) => ItemKind::ExternCrate(orig_name),
|
||||||
ItemKind::Use(use_tree) => {
|
ItemKind::Use(use_tree) => {
|
||||||
ItemKind::Use(use_tree.map(|tree| folder.fold_use_tree(tree)))
|
ItemKind::Use(use_tree.map(|tree| folder.fold_use_tree(tree)))
|
||||||
}
|
}
|
||||||
|
|
|
@ -6291,23 +6291,17 @@ impl<'a> Parser<'a> {
|
||||||
lo: Span,
|
lo: Span,
|
||||||
visibility: Visibility,
|
visibility: Visibility,
|
||||||
attrs: Vec<Attribute>)
|
attrs: Vec<Attribute>)
|
||||||
-> PResult<'a, P<Item>> {
|
-> PResult<'a, P<Item>> {
|
||||||
|
let orig_name = self.parse_ident()?;
|
||||||
let crate_name = self.parse_ident()?;
|
let (item_name, orig_name) = if let Some(rename) = self.parse_rename()? {
|
||||||
let (maybe_path, ident) = if let Some(ident) = self.parse_rename()? {
|
(rename, Some(orig_name.name))
|
||||||
(Some(crate_name.name), ident)
|
|
||||||
} else {
|
} else {
|
||||||
(None, crate_name)
|
(orig_name, None)
|
||||||
};
|
};
|
||||||
self.expect(&token::Semi)?;
|
self.expect(&token::Semi)?;
|
||||||
|
|
||||||
let prev_span = self.prev_span;
|
let span = lo.to(self.prev_span);
|
||||||
|
Ok(self.mk_item(span, item_name, ItemKind::ExternCrate(orig_name), visibility, attrs))
|
||||||
Ok(self.mk_item(lo.to(prev_span),
|
|
||||||
ident,
|
|
||||||
ItemKind::ExternCrate(maybe_path),
|
|
||||||
visibility,
|
|
||||||
attrs))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Parse `extern` for foreign ABIs
|
/// Parse `extern` for foreign ABIs
|
||||||
|
|
|
@ -1174,15 +1174,10 @@ impl<'a> State<'a> {
|
||||||
self.print_outer_attributes(&item.attrs)?;
|
self.print_outer_attributes(&item.attrs)?;
|
||||||
self.ann.pre(self, NodeItem(item))?;
|
self.ann.pre(self, NodeItem(item))?;
|
||||||
match item.node {
|
match item.node {
|
||||||
ast::ItemKind::ExternCrate(ref optional_path) => {
|
ast::ItemKind::ExternCrate(orig_name) => {
|
||||||
self.head(&visibility_qualified(&item.vis, "extern crate"))?;
|
self.head(&visibility_qualified(&item.vis, "extern crate"))?;
|
||||||
if let Some(p) = *optional_path {
|
if let Some(orig_name) = orig_name {
|
||||||
let val = p.as_str();
|
self.print_name(orig_name)?;
|
||||||
if val.contains('-') {
|
|
||||||
self.print_string(&val, ast::StrStyle::Cooked)?;
|
|
||||||
} else {
|
|
||||||
self.print_name(p)?;
|
|
||||||
}
|
|
||||||
self.s.space()?;
|
self.s.space()?;
|
||||||
self.s.word("as")?;
|
self.s.word("as")?;
|
||||||
self.s.space()?;
|
self.s.space()?;
|
||||||
|
|
|
@ -43,7 +43,7 @@ thread_local! {
|
||||||
static INJECTED_CRATE_NAME: Cell<Option<&'static str>> = Cell::new(None);
|
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") {
|
let name = if attr::contains_name(&krate.attrs, "no_core") {
|
||||||
return krate;
|
return krate;
|
||||||
} else if attr::contains_name(&krate.attrs, "no_std") {
|
} 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
|
||||||
|
|
||||||
INJECTED_CRATE_NAME.with(|opt_name| opt_name.set(Some(name)));
|
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 {
|
krate.module.items.insert(0, P(ast::Item {
|
||||||
attrs: vec![attr::mk_attr_outer(DUMMY_SP,
|
attrs: vec![attr::mk_attr_outer(DUMMY_SP,
|
||||||
attr::mk_attr_id(),
|
attr::mk_attr_id(),
|
||||||
attr::mk_word_item(Symbol::intern("macro_use")))],
|
attr::mk_word_item(Symbol::intern("macro_use")))],
|
||||||
vis: dummy_spanned(ast::VisibilityKind::Inherited),
|
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),
|
ident: ast::Ident::from_str(name),
|
||||||
id: ast::DUMMY_NODE_ID,
|
id: ast::DUMMY_NODE_ID,
|
||||||
span: DUMMY_SP,
|
span: DUMMY_SP,
|
||||||
|
|
|
@ -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_vis(&item.vis);
|
||||||
visitor.visit_ident(item.span, item.ident);
|
visitor.visit_ident(item.span, item.ident);
|
||||||
match item.node {
|
match item.node {
|
||||||
ItemKind::ExternCrate(opt_name) => {
|
ItemKind::ExternCrate(orig_name) => {
|
||||||
if let Some(name) = opt_name {
|
if let Some(orig_name) = orig_name {
|
||||||
visitor.visit_name(item.span, name);
|
visitor.visit_name(item.span, orig_name);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
ItemKind::Use(ref use_tree) => {
|
ItemKind::Use(ref use_tree) => {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue