// Allocate `DepNode`s for the root module.
let (root_mod_sig_dep_index, root_mod_full_dep_index) = {
let Crate {
- ref module,
- // Crate attributes are not copied over to the root `Mod`, so hash
- // them explicitly here.
- ref attrs,
- span,
+ ref item,
// These fields are handled separately:
exported_macros: _,
non_exported_macro_attrs: _,
dep_graph,
&mut hcx,
root_mod_def_path_hash,
- (module, attrs, span),
+ item,
&mut hir_body_nodes,
)
};
Entry {
parent: hir::CRATE_HIR_ID,
dep_node: root_mod_sig_dep_index,
- node: Node::Crate,
+ node: Node::Crate(&krate.item),
},
);
use rustc_data_structures::fx::FxHashMap;
use rustc_data_structures::svh::Svh;
use rustc_hir::def::{DefKind, Res};
-use rustc_hir::def_id::{DefId, DefIndex, LocalDefId, CRATE_DEF_INDEX};
+use rustc_hir::def_id::{DefId, DefIndex, LocalDefId};
use rustc_hir::intravisit;
use rustc_hir::itemlikevisit::ItemLikeVisitor;
use rustc_hir::print::Nested;
impl<'hir> Entry<'hir> {
fn parent_node(self) -> Option<HirId> {
match self.node {
- Node::Crate | Node::MacroDef(_) => None,
+ Node::Crate(_) | Node::MacroDef(_) => None,
_ => Some(self.parent),
}
}
| Node::Lifetime(_)
| Node::Visibility(_)
| Node::Block(_)
- | Node::Crate => return None,
+ | Node::Crate(_) => return None,
Node::MacroDef(_) => DefKind::Macro(MacroKind::Bang),
Node::GenericParam(param) => match param.kind {
GenericParamKind::Lifetime { .. } => return None,
self.lookup(id).cloned()
}
+ fn get_entry(&self, id: HirId) -> Entry<'hir> {
+ if id.local_id == ItemLocalId::from_u32_const(0) {
+ let owner = self.tcx.hir_owner(id.owner_def_id());
+ Entry { parent: owner.parent, node: owner.node, dep_node: DepNodeIndex::INVALID }
+ } else {
+ let owner = self.tcx.hir_owner_items(id.owner_def_id());
+ let item = owner.items[id.local_id].as_ref().unwrap();
+ Entry {
+ parent: HirId { owner: id.owner, local_id: item.parent },
+ node: item.node,
+ dep_node: DepNodeIndex::INVALID,
+ }
+ }
+ }
+
pub fn item(&self, id: HirId) -> &'hir Item<'hir> {
match self.find(id).unwrap() {
Node::Item(item) => item,
/// invoking `krate.attrs` because it registers a tighter
/// dep-graph access.
pub fn krate_attrs(&self) -> &'hir [ast::Attribute] {
- let def_path_hash = self.definitions.def_path_hash(CRATE_DEF_INDEX);
-
- self.dep_graph.read(def_path_hash.to_dep_node(DepKind::Hir));
- &self.krate.attrs
+ match self.get_entry(CRATE_HIR_ID).node {
+ Node::Crate(item) => item.attrs,
+ _ => bug!(),
+ }
}
pub fn get_module(&self, module: DefId) -> (&'hir Mod<'hir>, Span, HirId) {
let hir_id = self.as_local_hir_id(module).unwrap();
- self.read(hir_id);
- match self.find_entry(hir_id).unwrap().node {
+ match self.get_entry(hir_id).node {
Node::Item(&Item { span, kind: ItemKind::Mod(ref m), .. }) => (m, span, hir_id),
- Node::Crate => (&self.krate.module, self.krate.span, hir_id),
+ Node::Crate(item) => (&item.module, item.span, hir_id),
node => panic!("not a module: {:?}", node),
}
}
/// Retrieves the `Node` corresponding to `id`, returning `None` if cannot be found.
pub fn find(&self, hir_id: HirId) -> Option<Node<'hir>> {
- let result = self
- .find_entry(hir_id)
- .and_then(|entry| if let Node::Crate = entry.node { None } else { Some(entry.node) });
+ let result = self.find_entry(hir_id).and_then(|entry| {
+ if let Node::Crate(..) = entry.node { None } else { Some(entry.node) }
+ });
if result.is_some() {
self.read(hir_id);
}
pub fn is_hir_id_module(&self, hir_id: HirId) -> bool {
match self.lookup(hir_id) {
Some(Entry { node: Node::Item(Item { kind: ItemKind::Mod(_), .. }), .. })
- | Some(Entry { node: Node::Crate, .. }) => true,
+ | Some(Entry { node: Node::Crate(..), .. }) => true,
_ => false,
}
}
pub fn get_parent_item(&self, hir_id: HirId) -> HirId {
for (hir_id, node) in self.parent_iter(hir_id) {
match node {
- Node::Crate
+ Node::Crate(_)
| Node::Item(_)
| Node::ForeignItem(_)
| Node::TraitItem(_)
// Unit/tuple structs/variants take the attributes straight from
// the struct/variant definition.
Some(Node::Ctor(..)) => return self.attrs(self.get_parent_item(id)),
- Some(Node::Crate) => Some(&self.krate.attrs[..]),
+ Some(Node::Crate(item)) => Some(&item.attrs[..]),
_ => None,
};
attrs.unwrap_or(&[])
Some(Node::Visibility(v)) => bug!("unexpected Visibility {:?}", v),
Some(Node::Local(local)) => local.span,
Some(Node::MacroDef(macro_def)) => macro_def.span,
- Some(Node::Crate) => self.krate.span,
+ Some(Node::Crate(item)) => item.span,
None => bug!("hir::map::Map::span: id not in map: {:?}", hir_id),
}
}
Some(Node::GenericParam(ref param)) => format!("generic_param {:?}{}", param, id_str),
Some(Node::Visibility(ref vis)) => format!("visibility {:?}{}", vis, id_str),
Some(Node::MacroDef(_)) => format!("macro {}{}", path_str(), id_str),
- Some(Node::Crate) => String::from("root_crate"),
+ Some(Node::Crate(..)) => String::from("root_crate"),
None => format!("unknown node{}", id_str),
}
}
self.resolver.definitions().init_node_id_to_hir_id_mapping(self.node_id_to_hir_id);
hir::Crate {
- module,
- attrs,
- span: c.span,
+ item: hir::CrateItem { module, attrs, span: c.span },
exported_macros: self.arena.alloc_from_iter(self.exported_macros),
non_exported_macro_attrs: self.arena.alloc_from_iter(self.non_exported_macro_attrs),
items: self.items,
let crate_name = tcx.crate_name(LOCAL_CRATE);
let crate_hash = tcx.crate_hash(LOCAL_CRATE);
- let no_builtins = attr::contains_name(&tcx.hir().krate().attrs, sym::no_builtins);
+ let no_builtins = attr::contains_name(&tcx.hir().krate().item.attrs, sym::no_builtins);
let subsystem =
- attr::first_attr_value_str_by_name(&tcx.hir().krate().attrs, sym::windows_subsystem);
+ attr::first_attr_value_str_by_name(&tcx.hir().krate().item.attrs, sym::windows_subsystem);
let windows_subsystem = subsystem.map(|subsystem| {
if subsystem != sym::windows && subsystem != sym::console {
tcx.sess.fatal(&format!(
pub impl_items: BTreeSet<ImplItemId>,
}
+/// A type representing only the top-level module.
+#[derive(RustcEncodable, RustcDecodable, Debug, HashStable_Generic)]
+pub struct CrateItem<'hir> {
+ pub module: Mod<'hir>,
+ pub attrs: &'hir [Attribute],
+ pub span: Span,
+}
+
/// The top-level data structure that stores the entire contents of
/// the crate currently being compiled.
///
/// [rustc dev guide]: https://rustc-dev-guide.rust-lang.org/hir.html
#[derive(RustcEncodable, RustcDecodable, Debug)]
pub struct Crate<'hir> {
- pub module: Mod<'hir>,
- pub attrs: &'hir [Attribute],
- pub span: Span,
+ pub item: CrateItem<'hir>,
pub exported_macros: &'hir [MacroDef<'hir>],
// Attributes from non-exported macros, kept only for collecting the library feature list.
pub non_exported_macro_attrs: &'hir [Attribute],
GenericParam(&'hir GenericParam<'hir>),
Visibility(&'hir Visibility<'hir>),
- Crate,
+ Crate(&'hir CrateItem<'hir>),
}
impl Node<'_> {
/// Walks the contents of a crate. See also `Crate::visit_all_items`.
pub fn walk_crate<'v, V: Visitor<'v>>(visitor: &mut V, krate: &'v Crate<'v>) {
- visitor.visit_mod(&krate.module, krate.span, CRATE_HIR_ID);
- walk_list!(visitor, visit_attribute, krate.attrs);
+ visitor.visit_mod(&krate.item.module, krate.item.span, CRATE_HIR_ID);
+ walk_list!(visitor, visit_attribute, krate.item.attrs);
walk_list!(visitor, visit_macro_def, krate.exported_macros);
}
Node::Ctor(..) => panic!("cannot print isolated Ctor"),
Node::Local(a) => self.print_local_decl(&a),
Node::MacroDef(_) => panic!("cannot print MacroDef"),
- Node::Crate => panic!("cannot print Crate"),
+ Node::Crate(..) => panic!("cannot print Crate"),
}
}
}
// When printing the AST, we sometimes need to inject `#[no_std]` here.
// Since you can't compile the HIR, it's not necessary.
- s.print_mod(&krate.module, &krate.attrs);
+ s.print_mod(&krate.item.module, &krate.item.attrs);
s.print_remaining_comments();
s.s.eof()
}
let (if_this_changed, then_this_would_need) = {
let mut visitor =
IfThisChanged { tcx, if_this_changed: vec![], then_this_would_need: vec![] };
- visitor.process_attrs(hir::CRATE_HIR_ID, &tcx.hir().krate().attrs);
+ visitor.process_attrs(hir::CRATE_HIR_ID, &tcx.hir().krate().item.attrs);
tcx.hir().krate().visit_all_item_likes(&mut visitor.as_deep_visitor());
(visitor.if_this_changed, visitor.then_this_would_need)
};
let ams = AssertModuleSource { tcx, available_cgus };
- for attr in tcx.hir().krate().attrs {
+ for attr in tcx.hir().krate().item.attrs {
ams.check_attr(attr);
}
})
}
fn check_crate(&mut self, cx: &LateContext<'_, '_>, krate: &hir::Crate<'_>) {
- self.check_missing_docs_attrs(cx, None, &krate.attrs, krate.span, "crate");
+ self.check_missing_docs_attrs(cx, None, &krate.item.attrs, krate.item.span, "crate");
for macro_def in krate.exported_macros {
let has_doc = macro_def.attrs.iter().any(|a| has_doc(a));
let mut cx = LateContextAndPass { context, pass };
// Visit the whole crate.
- cx.with_lint_attrs(hir::CRATE_HIR_ID, &krate.attrs, |cx| {
+ cx.with_lint_attrs(hir::CRATE_HIR_ID, &krate.item.attrs, |cx| {
// since the root module isn't visited as an item (because it isn't an
// item), warn for it here.
lint_callback!(cx, check_crate, krate);
let mut builder = LintLevelMapBuilder { levels, tcx, store };
let krate = tcx.hir().krate();
- let push = builder.levels.push(&krate.attrs, &store);
+ let push = builder.levels.push(&krate.item.attrs, &store);
builder.levels.register_id(hir::CRATE_HIR_ID);
for macro_def in krate.exported_macros {
builder.levels.register_id(macro_def.hir_id);
let mut collector = Collector { args: Vec::new() };
tcx.hir().krate().visit_all_item_likes(&mut collector);
- for attr in tcx.hir().krate().attrs.iter() {
+ for attr in tcx.hir().krate().item.attrs.iter() {
if attr.has_name(sym::link_args) {
if let Some(linkarg) = attr.value_str() {
collector.add_link_args(&linkarg.as_str());
fn encode_info_for_items(&mut self) {
let krate = self.tcx.hir().krate();
let vis = Spanned { span: rustc_span::DUMMY_SP, node: hir::VisibilityKind::Public };
- self.encode_info_for_mod(hir::CRATE_HIR_ID, &krate.module, &krate.attrs, &vis);
+ self.encode_info_for_mod(hir::CRATE_HIR_ID, &krate.item.module, &krate.item.attrs, &vis);
krate.visit_all_item_likes(&mut self.as_deep_visitor());
for macro_def in krate.exported_macros {
self.visit_macro_def(macro_def);
}
// If the user wants no main function at all, then stop here.
- if attr::contains_name(&tcx.hir().krate().attrs, sym::no_main) {
+ if attr::contains_name(&tcx.hir().krate().item.attrs, sym::no_main) {
return None;
}
}
fn no_main_err(tcx: TyCtxt<'_>, visitor: &EntryContext<'_, '_>) {
- let sp = tcx.hir().krate().span;
+ let sp = tcx.hir().krate().item.span;
if *tcx.sess.parse_sess.reached_eof.borrow() {
// There's an unclosed brace that made the parser reach `Eof`, we shouldn't complain about
// the missing `fn main()` then as it might have been hidden inside an unclosed block.
annotator.annotate(
hir::CRATE_HIR_ID,
- &krate.attrs,
- krate.span,
+ &krate.item.attrs,
+ krate.item.span,
AnnotationKind::Required,
|v| intravisit::walk_crate(v, krate),
);
if tcx.stability().staged_api[&LOCAL_CRATE] {
let krate = tcx.hir().krate();
let mut missing = MissingStabilityAnnotations { tcx, access_levels };
- missing.check_missing_stability(hir::CRATE_HIR_ID, krate.span, "crate");
+ missing.check_missing_stability(hir::CRATE_HIR_ID, krate.item.span, "crate");
intravisit::walk_crate(&mut missing, krate);
krate.visit_all_item_likes(&mut missing.as_deep_visitor());
}
}
}
- hir::Node::Crate => return,
+ hir::Node::Crate(..) => return,
_ => {}
}
cx.tcx
.hir()
.krate()
+ .item
.module
.item_ids
.iter()
cx.tcx
.hir()
.krate()
+ .item
.module
.item_ids
.iter()
compiler.session().opts.unstable_features.is_nightly_build(),
),
};
- hir_collector.visit_testable("".to_string(), &krate.attrs, |this| {
+ hir_collector.visit_testable("".to_string(), &krate.item.attrs, |this| {
intravisit::walk_crate(this, krate);
});
});
TestOptions { no_crate_inject: false, display_warnings: false, attrs: Vec::new() };
let test_attrs: Vec<_> = krate
+ .item
.attrs
.iter()
.filter(|a| a.check_name(sym::doc))
pub fn visit(mut self, krate: &'tcx hir::Crate) -> Module<'tcx> {
let mut module = self.visit_mod_contents(
- krate.span,
- krate.attrs,
+ krate.item.span,
+ krate.item.attrs,
&Spanned { span: rustc_span::DUMMY_SP, node: hir::VisibilityKind::Public },
hir::CRATE_HIR_ID,
- &krate.module,
+ &krate.item.module,
None,
);
// Attach the crate's exported macros to the top-level module: