Skip to content

Commit 38e613c

Browse files
committed
Update krate_attrs and get_module
1 parent 0c68b7a commit 38e613c

File tree

20 files changed

+72
-55
lines changed

20 files changed

+72
-55
lines changed

src/librustc/hir/map/collector.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -133,11 +133,7 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
133133
// Allocate `DepNode`s for the root module.
134134
let (root_mod_sig_dep_index, root_mod_full_dep_index) = {
135135
let Crate {
136-
ref module,
137-
// Crate attributes are not copied over to the root `Mod`, so hash
138-
// them explicitly here.
139-
ref attrs,
140-
span,
136+
ref item,
141137
// These fields are handled separately:
142138
exported_macros: _,
143139
non_exported_macro_attrs: _,
@@ -155,7 +151,7 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
155151
dep_graph,
156152
&mut hcx,
157153
root_mod_def_path_hash,
158-
(module, attrs, span),
154+
item,
159155
&mut hir_body_nodes,
160156
)
161157
};
@@ -191,7 +187,7 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
191187
Entry {
192188
parent: hir::CRATE_HIR_ID,
193189
dep_node: root_mod_sig_dep_index,
194-
node: Node::Crate,
190+
node: Node::Crate(&krate.item),
195191
},
196192
);
197193

src/librustc/hir/map/mod.rs

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use rustc_ast::ast::{self, Name, NodeId};
1313
use rustc_data_structures::fx::FxHashMap;
1414
use rustc_data_structures::svh::Svh;
1515
use rustc_hir::def::{DefKind, Res};
16-
use rustc_hir::def_id::{DefId, DefIndex, LocalDefId, CRATE_DEF_INDEX};
16+
use rustc_hir::def_id::{DefId, DefIndex, LocalDefId};
1717
use rustc_hir::intravisit;
1818
use rustc_hir::itemlikevisit::ItemLikeVisitor;
1919
use rustc_hir::print::Nested;
@@ -41,7 +41,7 @@ pub struct Entry<'hir> {
4141
impl<'hir> Entry<'hir> {
4242
fn parent_node(self) -> Option<HirId> {
4343
match self.node {
44-
Node::Crate | Node::MacroDef(_) => None,
44+
Node::Crate(_) | Node::MacroDef(_) => None,
4545
_ => Some(self.parent),
4646
}
4747
}
@@ -389,7 +389,7 @@ impl<'hir> Map<'hir> {
389389
| Node::Lifetime(_)
390390
| Node::Visibility(_)
391391
| Node::Block(_)
392-
| Node::Crate => return None,
392+
| Node::Crate(_) => return None,
393393
Node::MacroDef(_) => DefKind::Macro(MacroKind::Bang),
394394
Node::GenericParam(param) => match param.kind {
395395
GenericParamKind::Lifetime { .. } => return None,
@@ -403,6 +403,21 @@ impl<'hir> Map<'hir> {
403403
self.lookup(id).cloned()
404404
}
405405

406+
fn get_entry(&self, id: HirId) -> Entry<'hir> {
407+
if id.local_id == ItemLocalId::from_u32_const(0) {
408+
let owner = self.tcx.hir_owner(id.owner_def_id());
409+
Entry { parent: owner.parent, node: owner.node, dep_node: DepNodeIndex::INVALID }
410+
} else {
411+
let owner = self.tcx.hir_owner_items(id.owner_def_id());
412+
let item = owner.items[id.local_id].as_ref().unwrap();
413+
Entry {
414+
parent: HirId { owner: id.owner, local_id: item.parent },
415+
node: item.node,
416+
dep_node: DepNodeIndex::INVALID,
417+
}
418+
}
419+
}
420+
406421
pub fn item(&self, id: HirId) -> &'hir Item<'hir> {
407422
match self.find(id).unwrap() {
408423
Node::Item(item) => item,
@@ -528,18 +543,17 @@ impl<'hir> Map<'hir> {
528543
/// invoking `krate.attrs` because it registers a tighter
529544
/// dep-graph access.
530545
pub fn krate_attrs(&self) -> &'hir [ast::Attribute] {
531-
let def_path_hash = self.definitions.def_path_hash(CRATE_DEF_INDEX);
532-
533-
self.dep_graph.read(def_path_hash.to_dep_node(DepKind::Hir));
534-
&self.krate.attrs
546+
match self.get_entry(CRATE_HIR_ID).node {
547+
Node::Crate(item) => item.attrs,
548+
_ => bug!(),
549+
}
535550
}
536551

537552
pub fn get_module(&self, module: DefId) -> (&'hir Mod<'hir>, Span, HirId) {
538553
let hir_id = self.as_local_hir_id(module).unwrap();
539-
self.read(hir_id);
540-
match self.find_entry(hir_id).unwrap().node {
554+
match self.get_entry(hir_id).node {
541555
Node::Item(&Item { span, kind: ItemKind::Mod(ref m), .. }) => (m, span, hir_id),
542-
Node::Crate => (&self.krate.module, self.krate.span, hir_id),
556+
Node::Crate(item) => (&item.module, item.span, hir_id),
543557
node => panic!("not a module: {:?}", node),
544558
}
545559
}
@@ -602,9 +616,9 @@ impl<'hir> Map<'hir> {
602616

603617
/// Retrieves the `Node` corresponding to `id`, returning `None` if cannot be found.
604618
pub fn find(&self, hir_id: HirId) -> Option<Node<'hir>> {
605-
let result = self
606-
.find_entry(hir_id)
607-
.and_then(|entry| if let Node::Crate = entry.node { None } else { Some(entry.node) });
619+
let result = self.find_entry(hir_id).and_then(|entry| {
620+
if let Node::Crate(..) = entry.node { None } else { Some(entry.node) }
621+
});
608622
if result.is_some() {
609623
self.read(hir_id);
610624
}
@@ -675,7 +689,7 @@ impl<'hir> Map<'hir> {
675689
pub fn is_hir_id_module(&self, hir_id: HirId) -> bool {
676690
match self.lookup(hir_id) {
677691
Some(Entry { node: Node::Item(Item { kind: ItemKind::Mod(_), .. }), .. })
678-
| Some(Entry { node: Node::Crate, .. }) => true,
692+
| Some(Entry { node: Node::Crate(..), .. }) => true,
679693
_ => false,
680694
}
681695
}
@@ -752,7 +766,7 @@ impl<'hir> Map<'hir> {
752766
pub fn get_parent_item(&self, hir_id: HirId) -> HirId {
753767
for (hir_id, node) in self.parent_iter(hir_id) {
754768
match node {
755-
Node::Crate
769+
Node::Crate(_)
756770
| Node::Item(_)
757771
| Node::ForeignItem(_)
758772
| Node::TraitItem(_)
@@ -973,7 +987,7 @@ impl<'hir> Map<'hir> {
973987
// Unit/tuple structs/variants take the attributes straight from
974988
// the struct/variant definition.
975989
Some(Node::Ctor(..)) => return self.attrs(self.get_parent_item(id)),
976-
Some(Node::Crate) => Some(&self.krate.attrs[..]),
990+
Some(Node::Crate(item)) => Some(&item.attrs[..]),
977991
_ => None,
978992
};
979993
attrs.unwrap_or(&[])
@@ -1013,7 +1027,7 @@ impl<'hir> Map<'hir> {
10131027
Some(Node::Visibility(v)) => bug!("unexpected Visibility {:?}", v),
10141028
Some(Node::Local(local)) => local.span,
10151029
Some(Node::MacroDef(macro_def)) => macro_def.span,
1016-
Some(Node::Crate) => self.krate.span,
1030+
Some(Node::Crate(item)) => item.span,
10171031
None => bug!("hir::map::Map::span: id not in map: {:?}", hir_id),
10181032
}
10191033
}
@@ -1255,7 +1269,7 @@ fn hir_id_to_string(map: &Map<'_>, id: HirId, include_id: bool) -> String {
12551269
Some(Node::GenericParam(ref param)) => format!("generic_param {:?}{}", param, id_str),
12561270
Some(Node::Visibility(ref vis)) => format!("visibility {:?}{}", vis, id_str),
12571271
Some(Node::MacroDef(_)) => format!("macro {}{}", path_str(), id_str),
1258-
Some(Node::Crate) => String::from("root_crate"),
1272+
Some(Node::Crate(..)) => String::from("root_crate"),
12591273
None => format!("unknown node{}", id_str),
12601274
}
12611275
}

src/librustc_ast_lowering/lib.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -535,9 +535,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
535535
self.resolver.definitions().init_node_id_to_hir_id_mapping(self.node_id_to_hir_id);
536536

537537
hir::Crate {
538-
module,
539-
attrs,
540-
span: c.span,
538+
item: hir::CrateItem { module, attrs, span: c.span },
541539
exported_macros: self.arena.alloc_from_iter(self.exported_macros),
542540
non_exported_macro_attrs: self.arena.alloc_from_iter(self.non_exported_macro_attrs),
543541
items: self.items,

src/librustc_codegen_ssa/back/write.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -341,9 +341,9 @@ pub fn start_async_codegen<B: ExtraBackendMethods>(
341341

342342
let crate_name = tcx.crate_name(LOCAL_CRATE);
343343
let crate_hash = tcx.crate_hash(LOCAL_CRATE);
344-
let no_builtins = attr::contains_name(&tcx.hir().krate().attrs, sym::no_builtins);
344+
let no_builtins = attr::contains_name(&tcx.hir().krate().item.attrs, sym::no_builtins);
345345
let subsystem =
346-
attr::first_attr_value_str_by_name(&tcx.hir().krate().attrs, sym::windows_subsystem);
346+
attr::first_attr_value_str_by_name(&tcx.hir().krate().item.attrs, sym::windows_subsystem);
347347
let windows_subsystem = subsystem.map(|subsystem| {
348348
if subsystem != sym::windows && subsystem != sym::console {
349349
tcx.sess.fatal(&format!(

src/librustc_hir/hir.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -606,6 +606,14 @@ pub struct ModuleItems {
606606
pub impl_items: BTreeSet<ImplItemId>,
607607
}
608608

609+
/// A type representing only the top-level module.
610+
#[derive(RustcEncodable, RustcDecodable, Debug, HashStable_Generic)]
611+
pub struct CrateItem<'hir> {
612+
pub module: Mod<'hir>,
613+
pub attrs: &'hir [Attribute],
614+
pub span: Span,
615+
}
616+
609617
/// The top-level data structure that stores the entire contents of
610618
/// the crate currently being compiled.
611619
///
@@ -614,9 +622,7 @@ pub struct ModuleItems {
614622
/// [rustc dev guide]: https://rustc-dev-guide.rust-lang.org/hir.html
615623
#[derive(RustcEncodable, RustcDecodable, Debug)]
616624
pub struct Crate<'hir> {
617-
pub module: Mod<'hir>,
618-
pub attrs: &'hir [Attribute],
619-
pub span: Span,
625+
pub item: CrateItem<'hir>,
620626
pub exported_macros: &'hir [MacroDef<'hir>],
621627
// Attributes from non-exported macros, kept only for collecting the library feature list.
622628
pub non_exported_macro_attrs: &'hir [Attribute],
@@ -2683,7 +2689,7 @@ pub enum Node<'hir> {
26832689
GenericParam(&'hir GenericParam<'hir>),
26842690
Visibility(&'hir Visibility<'hir>),
26852691

2686-
Crate,
2692+
Crate(&'hir CrateItem<'hir>),
26872693
}
26882694

26892695
impl Node<'_> {

src/librustc_hir/intravisit.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -438,8 +438,8 @@ pub trait Visitor<'v>: Sized {
438438

439439
/// Walks the contents of a crate. See also `Crate::visit_all_items`.
440440
pub fn walk_crate<'v, V: Visitor<'v>>(visitor: &mut V, krate: &'v Crate<'v>) {
441-
visitor.visit_mod(&krate.module, krate.span, CRATE_HIR_ID);
442-
walk_list!(visitor, visit_attribute, krate.attrs);
441+
visitor.visit_mod(&krate.item.module, krate.item.span, CRATE_HIR_ID);
442+
walk_list!(visitor, visit_attribute, krate.item.attrs);
443443
walk_list!(visitor, visit_macro_def, krate.exported_macros);
444444
}
445445

src/librustc_hir/print.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ impl<'a> State<'a> {
102102
Node::Ctor(..) => panic!("cannot print isolated Ctor"),
103103
Node::Local(a) => self.print_local_decl(&a),
104104
Node::MacroDef(_) => panic!("cannot print MacroDef"),
105-
Node::Crate => panic!("cannot print Crate"),
105+
Node::Crate(..) => panic!("cannot print Crate"),
106106
}
107107
}
108108
}
@@ -151,7 +151,7 @@ pub fn print_crate<'a>(
151151
// When printing the AST, we sometimes need to inject `#[no_std]` here.
152152
// Since you can't compile the HIR, it's not necessary.
153153

154-
s.print_mod(&krate.module, &krate.attrs);
154+
s.print_mod(&krate.item.module, &krate.item.attrs);
155155
s.print_remaining_comments();
156156
s.s.eof()
157157
}

src/librustc_incremental/assert_dep_graph.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ pub fn assert_dep_graph(tcx: TyCtxt<'_>) {
6868
let (if_this_changed, then_this_would_need) = {
6969
let mut visitor =
7070
IfThisChanged { tcx, if_this_changed: vec![], then_this_would_need: vec![] };
71-
visitor.process_attrs(hir::CRATE_HIR_ID, &tcx.hir().krate().attrs);
71+
visitor.process_attrs(hir::CRATE_HIR_ID, &tcx.hir().krate().item.attrs);
7272
tcx.hir().krate().visit_all_item_likes(&mut visitor.as_deep_visitor());
7373
(visitor.if_this_changed, visitor.then_this_would_need)
7474
};

src/librustc_incremental/assert_module_sources.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ pub fn assert_module_sources(tcx: TyCtxt<'_>) {
4444

4545
let ams = AssertModuleSource { tcx, available_cgus };
4646

47-
for attr in tcx.hir().krate().attrs {
47+
for attr in tcx.hir().krate().item.attrs {
4848
ams.check_attr(attr);
4949
}
5050
})

src/librustc_lint/builtin.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MissingDoc {
399399
}
400400

401401
fn check_crate(&mut self, cx: &LateContext<'_, '_>, krate: &hir::Crate<'_>) {
402-
self.check_missing_docs_attrs(cx, None, &krate.attrs, krate.span, "crate");
402+
self.check_missing_docs_attrs(cx, None, &krate.item.attrs, krate.item.span, "crate");
403403

404404
for macro_def in krate.exported_macros {
405405
let has_doc = macro_def.attrs.iter().any(|a| has_doc(a));

0 commit comments

Comments
 (0)