mirror of
https://github.com/rust-lang/rust.git
synced 2025-05-14 02:49:40 +00:00
AST/HIR: Clarify what the optional name in extern crate items mean
This commit is contained in:
parent
61b6bf54fd
commit
c6c6cf9515
@ -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, _) => {
|
||||
|
@ -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 {
|
||||
|
@ -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;`
|
||||
|
@ -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()?;
|
||||
|
@ -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),
|
||||
|
@ -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
|
||||
|
@ -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)
|
||||
});
|
||||
|
||||
|
@ -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,
|
||||
};
|
||||
@ -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();
|
||||
|
@ -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
|
||||
@ -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),
|
||||
|
@ -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,
|
||||
|
@ -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>),
|
||||
|
@ -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)))
|
||||
}
|
||||
|
@ -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
|
||||
|
@ -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()?;
|
||||
|
@ -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") {
|
||||
@ -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,
|
||||
|
@ -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) => {
|
||||
|
Loading…
Reference in New Issue
Block a user