Update `krate_attrs` and `get_module`
This commit is contained in:
parent
0c68b7a7fa
commit
38e613c4eb
|
@ -133,11 +133,7 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
|
|||
// 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: _,
|
||||
|
@ -155,7 +151,7 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
|
|||
dep_graph,
|
||||
&mut hcx,
|
||||
root_mod_def_path_hash,
|
||||
(module, attrs, span),
|
||||
item,
|
||||
&mut hir_body_nodes,
|
||||
)
|
||||
};
|
||||
|
@ -191,7 +187,7 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
|
|||
Entry {
|
||||
parent: hir::CRATE_HIR_ID,
|
||||
dep_node: root_mod_sig_dep_index,
|
||||
node: Node::Crate,
|
||||
node: Node::Crate(&krate.item),
|
||||
},
|
||||
);
|
||||
|
||||
|
|
|
@ -13,7 +13,7 @@ use rustc_ast::ast::{self, Name, NodeId};
|
|||
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;
|
||||
|
@ -41,7 +41,7 @@ pub struct Entry<'hir> {
|
|||
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),
|
||||
}
|
||||
}
|
||||
|
@ -389,7 +389,7 @@ impl<'hir> Map<'hir> {
|
|||
| 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,
|
||||
|
@ -403,6 +403,21 @@ impl<'hir> Map<'hir> {
|
|||
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,
|
||||
|
@ -528,18 +543,17 @@ impl<'hir> Map<'hir> {
|
|||
/// 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),
|
||||
}
|
||||
}
|
||||
|
@ -602,9 +616,9 @@ impl<'hir> Map<'hir> {
|
|||
|
||||
/// 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);
|
||||
}
|
||||
|
@ -675,7 +689,7 @@ impl<'hir> Map<'hir> {
|
|||
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,
|
||||
}
|
||||
}
|
||||
|
@ -752,7 +766,7 @@ impl<'hir> Map<'hir> {
|
|||
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(_)
|
||||
|
@ -973,7 +987,7 @@ impl<'hir> Map<'hir> {
|
|||
// 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(&[])
|
||||
|
@ -1013,7 +1027,7 @@ impl<'hir> Map<'hir> {
|
|||
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),
|
||||
}
|
||||
}
|
||||
|
@ -1255,7 +1269,7 @@ fn hir_id_to_string(map: &Map<'_>, id: HirId, include_id: bool) -> String {
|
|||
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),
|
||||
}
|
||||
}
|
||||
|
|
|
@ -535,9 +535,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
|
|||
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,
|
||||
|
|
|
@ -341,9 +341,9 @@ pub fn start_async_codegen<B: ExtraBackendMethods>(
|
|||
|
||||
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!(
|
||||
|
|
|
@ -606,6 +606,14 @@ pub struct ModuleItems {
|
|||
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.
|
||||
///
|
||||
|
@ -614,9 +622,7 @@ pub struct ModuleItems {
|
|||
/// [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],
|
||||
|
@ -2683,7 +2689,7 @@ pub enum Node<'hir> {
|
|||
GenericParam(&'hir GenericParam<'hir>),
|
||||
Visibility(&'hir Visibility<'hir>),
|
||||
|
||||
Crate,
|
||||
Crate(&'hir CrateItem<'hir>),
|
||||
}
|
||||
|
||||
impl Node<'_> {
|
||||
|
|
|
@ -438,8 +438,8 @@ pub trait Visitor<'v>: Sized {
|
|||
|
||||
/// 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);
|
||||
}
|
||||
|
||||
|
|
|
@ -102,7 +102,7 @@ impl<'a> State<'a> {
|
|||
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"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -151,7 +151,7 @@ pub fn print_crate<'a>(
|
|||
// 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()
|
||||
}
|
||||
|
|
|
@ -68,7 +68,7 @@ pub fn assert_dep_graph(tcx: TyCtxt<'_>) {
|
|||
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)
|
||||
};
|
||||
|
|
|
@ -44,7 +44,7 @@ pub fn assert_module_sources(tcx: TyCtxt<'_>) {
|
|||
|
||||
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);
|
||||
}
|
||||
})
|
||||
|
|
|
@ -399,7 +399,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MissingDoc {
|
|||
}
|
||||
|
||||
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));
|
||||
|
|
|
@ -419,7 +419,7 @@ fn late_lint_pass_crate<'tcx, T: for<'a> LateLintPass<'a, 'tcx>>(tcx: TyCtxt<'tc
|
|||
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);
|
||||
|
|
|
@ -29,7 +29,7 @@ fn lint_levels(tcx: TyCtxt<'_>, cnum: CrateNum) -> &LintLevelMap {
|
|||
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);
|
||||
|
|
|
@ -8,7 +8,7 @@ crate fn collect(tcx: TyCtxt<'_>) -> Vec<String> {
|
|||
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());
|
||||
|
|
|
@ -331,7 +331,7 @@ impl<'tcx> EncodeContext<'tcx> {
|
|||
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);
|
||||
|
|
|
@ -59,7 +59,7 @@ fn entry_fn(tcx: TyCtxt<'_>, cnum: CrateNum) -> Option<(DefId, EntryFnType)> {
|
|||
}
|
||||
|
||||
// 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;
|
||||
}
|
||||
|
||||
|
@ -157,7 +157,7 @@ fn configure_main(tcx: TyCtxt<'_>, visitor: &EntryContext<'_, '_>) -> Option<(De
|
|||
}
|
||||
|
||||
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.
|
||||
|
|
|
@ -459,8 +459,8 @@ fn new_index(tcx: TyCtxt<'tcx>) -> Index<'tcx> {
|
|||
|
||||
annotator.annotate(
|
||||
hir::CRATE_HIR_ID,
|
||||
&krate.attrs,
|
||||
krate.span,
|
||||
&krate.item.attrs,
|
||||
krate.item.span,
|
||||
AnnotationKind::Required,
|
||||
|v| intravisit::walk_crate(v, krate),
|
||||
);
|
||||
|
@ -585,7 +585,7 @@ pub fn check_unused_or_stable_features(tcx: TyCtxt<'_>) {
|
|||
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());
|
||||
}
|
||||
|
|
|
@ -282,7 +282,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
|
|||
}
|
||||
}
|
||||
|
||||
hir::Node::Crate => return,
|
||||
hir::Node::Crate(..) => return,
|
||||
|
||||
_ => {}
|
||||
}
|
||||
|
|
|
@ -141,6 +141,7 @@ impl Clean<ExternalCrate> for CrateNum {
|
|||
cx.tcx
|
||||
.hir()
|
||||
.krate()
|
||||
.item
|
||||
.module
|
||||
.item_ids
|
||||
.iter()
|
||||
|
@ -194,6 +195,7 @@ impl Clean<ExternalCrate> for CrateNum {
|
|||
cx.tcx
|
||||
.hir()
|
||||
.krate()
|
||||
.item
|
||||
.module
|
||||
.item_ids
|
||||
.iter()
|
||||
|
|
|
@ -112,7 +112,7 @@ pub fn run(options: Options) -> i32 {
|
|||
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);
|
||||
});
|
||||
});
|
||||
|
@ -146,6 +146,7 @@ fn scrape_test_config(krate: &::rustc_hir::Crate) -> TestOptions {
|
|||
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))
|
||||
|
|
|
@ -64,11 +64,11 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
|
|||
|
||||
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:
|
||||
|
|
Loading…
Reference in New Issue