Skip to content

Commit 3ed32d1

Browse files
committed
Update krate_attrs and get_module
1 parent 450b784 commit 3ed32d1

File tree

20 files changed

+74
-54
lines changed

20 files changed

+74
-54
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: _,
@@ -154,7 +150,7 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
154150
dep_graph,
155151
&mut hcx,
156152
root_mod_def_path_hash,
157-
(module, attrs, span),
153+
item,
158154
&mut hir_body_nodes,
159155
)
160156
};
@@ -190,7 +186,7 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
190186
Entry {
191187
parent: hir::CRATE_HIR_ID,
192188
dep_node: root_mod_sig_dep_index,
193-
node: Node::Crate,
189+
node: Node::Crate(&krate.item),
194190
},
195191
);
196192

src/librustc/hir/map/mod.rs

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use crate::ty::TyCtxt;
1212
use rustc_data_structures::fx::FxHashMap;
1313
use rustc_data_structures::svh::Svh;
1414
use rustc_hir::def::{DefKind, Res};
15-
use rustc_hir::def_id::{DefId, DefIndex, LocalDefId, CRATE_DEF_INDEX};
15+
use rustc_hir::def_id::{DefId, DefIndex, LocalDefId};
1616
use rustc_hir::intravisit;
1717
use rustc_hir::itemlikevisit::ItemLikeVisitor;
1818
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
}
@@ -394,7 +394,7 @@ impl<'hir> Map<'hir> {
394394
| Node::Lifetime(_)
395395
| Node::Visibility(_)
396396
| Node::Block(_)
397-
| Node::Crate => return None,
397+
| Node::Crate(_) => return None,
398398
Node::MacroDef(_) => DefKind::Macro(MacroKind::Bang),
399399
Node::GenericParam(param) => match param.kind {
400400
GenericParamKind::Lifetime { .. } => return None,
@@ -408,6 +408,21 @@ impl<'hir> Map<'hir> {
408408
self.lookup(id).cloned()
409409
}
410410

411+
fn get_entry(&self, id: HirId) -> Entry<'hir> {
412+
if id.local_id == ItemLocalId::from_u32_const(0) {
413+
let owner = self.tcx.hir_owner(id.owner_def_id());
414+
Entry { parent: owner.parent, node: owner.node, dep_node: DepNodeIndex::INVALID }
415+
} else {
416+
let owner = self.tcx.hir_owner_items(id.owner_def_id());
417+
let item = owner.items[id.local_id].as_ref().unwrap();
418+
Entry {
419+
parent: HirId { owner: id.owner, local_id: item.parent },
420+
node: item.node,
421+
dep_node: DepNodeIndex::INVALID,
422+
}
423+
}
424+
}
425+
411426
pub fn item(&self, id: HirId) -> &'hir Item<'hir> {
412427
match self.find(id).unwrap() {
413428
Node::Item(item) => item,
@@ -533,18 +548,17 @@ impl<'hir> Map<'hir> {
533548
/// invoking `krate.attrs` because it registers a tighter
534549
/// dep-graph access.
535550
pub fn krate_attrs(&self) -> &'hir [ast::Attribute] {
536-
let def_path_hash = self.definitions.def_path_hash(CRATE_DEF_INDEX);
537-
538-
self.dep_graph.read(def_path_hash.to_dep_node(DepKind::Hir));
539-
&self.krate.attrs
551+
match self.get_entry(CRATE_HIR_ID).node {
552+
Node::Crate(item) => item.attrs,
553+
_ => bug!(),
554+
}
540555
}
541556

542557
pub fn get_module(&self, module: DefId) -> (&'hir Mod<'hir>, Span, HirId) {
543558
let hir_id = self.as_local_hir_id(module).unwrap();
544-
self.read(hir_id);
545-
match self.find_entry(hir_id).unwrap().node {
559+
match self.get_entry(hir_id).node {
546560
Node::Item(&Item { span, kind: ItemKind::Mod(ref m), .. }) => (m, span, hir_id),
547-
Node::Crate => (&self.krate.module, self.krate.span, hir_id),
561+
Node::Crate(item) => (&item.module, item.span, hir_id),
548562
node => panic!("not a module: {:?}", node),
549563
}
550564
}
@@ -607,9 +621,9 @@ impl<'hir> Map<'hir> {
607621

608622
/// Retrieves the `Node` corresponding to `id`, returning `None` if cannot be found.
609623
pub fn find(&self, hir_id: HirId) -> Option<Node<'hir>> {
610-
let result = self
611-
.find_entry(hir_id)
612-
.and_then(|entry| if let Node::Crate = entry.node { None } else { Some(entry.node) });
624+
let result = self.find_entry(hir_id).and_then(|entry| {
625+
if let Node::Crate(..) = entry.node { None } else { Some(entry.node) }
626+
});
613627
if result.is_some() {
614628
self.read(hir_id);
615629
}
@@ -674,7 +688,7 @@ impl<'hir> Map<'hir> {
674688
pub fn is_hir_id_module(&self, hir_id: HirId) -> bool {
675689
match self.lookup(hir_id) {
676690
Some(Entry { node: Node::Item(Item { kind: ItemKind::Mod(_), .. }), .. })
677-
| Some(Entry { node: Node::Crate, .. }) => true,
691+
| Some(Entry { node: Node::Crate(..), .. }) => true,
678692
_ => false,
679693
}
680694
}
@@ -751,7 +765,7 @@ impl<'hir> Map<'hir> {
751765
pub fn get_parent_item(&self, hir_id: HirId) -> HirId {
752766
for (hir_id, node) in ParentHirIterator::new(hir_id, &self) {
753767
match node {
754-
Node::Crate
768+
Node::Crate(_)
755769
| Node::Item(_)
756770
| Node::ForeignItem(_)
757771
| Node::TraitItem(_)
@@ -978,7 +992,7 @@ impl<'hir> Map<'hir> {
978992
// Unit/tuple structs/variants take the attributes straight from
979993
// the struct/variant definition.
980994
Some(Node::Ctor(..)) => return self.attrs(self.get_parent_item(id)),
981-
Some(Node::Crate) => Some(&self.krate.attrs[..]),
995+
Some(Node::Crate(item)) => Some(&item.attrs[..]),
982996
_ => None,
983997
};
984998
attrs.unwrap_or(&[])
@@ -1018,7 +1032,7 @@ impl<'hir> Map<'hir> {
10181032
Some(Node::Visibility(v)) => bug!("unexpected Visibility {:?}", v),
10191033
Some(Node::Local(local)) => local.span,
10201034
Some(Node::MacroDef(macro_def)) => macro_def.span,
1021-
Some(Node::Crate) => self.krate.span,
1035+
Some(Node::Crate(item)) => item.span,
10221036
None => bug!("hir::map::Map::span: id not in map: {:?}", hir_id),
10231037
}
10241038
}
@@ -1260,7 +1274,7 @@ fn hir_id_to_string(map: &Map<'_>, id: HirId, include_id: bool) -> String {
12601274
Some(Node::GenericParam(ref param)) => format!("generic_param {:?}{}", param, id_str),
12611275
Some(Node::Visibility(ref vis)) => format!("visibility {:?}{}", vis, id_str),
12621276
Some(Node::MacroDef(_)) => format!("macro {}{}", path_str(), id_str),
1263-
Some(Node::Crate) => String::from("root_crate"),
1277+
Some(Node::Crate(..)) => String::from("root_crate"),
12641278
None => format!("unknown node{}", id_str),
12651279
}
12661280
}

src/librustc/traits/error_reporting/suggestions.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
158158
}
159159
}
160160

161-
hir::Node::Crate => return,
161+
hir::Node::Crate(..) => return,
162162

163163
_ => {}
164164
}

src/librustc_ast_lowering/lib.rs

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

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

src/librustc_codegen_ssa/back/write.rs

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

338338
let crate_name = tcx.crate_name(LOCAL_CRATE);
339339
let crate_hash = tcx.crate_hash(LOCAL_CRATE);
340-
let no_builtins = attr::contains_name(&tcx.hir().krate().attrs, sym::no_builtins);
340+
let no_builtins = attr::contains_name(&tcx.hir().krate().item.attrs, sym::no_builtins);
341341
let subsystem =
342-
attr::first_attr_value_str_by_name(&tcx.hir().krate().attrs, sym::windows_subsystem);
342+
attr::first_attr_value_str_by_name(&tcx.hir().krate().item.attrs, sym::windows_subsystem);
343343
let windows_subsystem = subsystem.map(|subsystem| {
344344
if subsystem != sym::windows && subsystem != sym::console {
345345
tcx.sess.fatal(&format!(

src/librustc_hir/hir.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -588,6 +588,14 @@ pub struct ModuleItems {
588588
pub impl_items: BTreeSet<ImplItemId>,
589589
}
590590

591+
/// A type representing only the top-level module.
592+
#[derive(RustcEncodable, RustcDecodable, Debug, HashStable_Generic)]
593+
pub struct CrateItem<'hir> {
594+
pub module: Mod<'hir>,
595+
pub attrs: &'hir [Attribute],
596+
pub span: Span,
597+
}
598+
591599
/// The top-level data structure that stores the entire contents of
592600
/// the crate currently being compiled.
593601
///
@@ -596,9 +604,10 @@ pub struct ModuleItems {
596604
/// [rustc guide]: https://rust-lang.github.io/rustc-guide/hir.html
597605
#[derive(RustcEncodable, RustcDecodable, Debug)]
598606
pub struct Crate<'hir> {
599-
pub module: Mod<'hir>,
607+
/*pub module: Mod<'hir>,
600608
pub attrs: &'hir [Attribute],
601-
pub span: Span,
609+
pub span: Span,*/
610+
pub item: CrateItem<'hir>,
602611
pub exported_macros: &'hir [MacroDef<'hir>],
603612
// Attributes from non-exported macros, kept only for collecting the library feature list.
604613
pub non_exported_macro_attrs: &'hir [Attribute],
@@ -2618,7 +2627,7 @@ pub enum Node<'hir> {
26182627
GenericParam(&'hir GenericParam<'hir>),
26192628
Visibility(&'hir Visibility<'hir>),
26202629

2621-
Crate,
2630+
Crate(&'hir CrateItem<'hir>),
26222631
}
26232632

26242633
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
@@ -386,7 +386,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MissingDoc {
386386
}
387387

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

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

src/librustc_lint/late.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,7 @@ fn late_lint_pass_crate<'tcx, T: for<'a> LateLintPass<'a, 'tcx>>(tcx: TyCtxt<'tc
419419
let mut cx = LateContextAndPass { context, pass };
420420

421421
// Visit the whole crate.
422-
cx.with_lint_attrs(hir::CRATE_HIR_ID, &krate.attrs, |cx| {
422+
cx.with_lint_attrs(hir::CRATE_HIR_ID, &krate.item.attrs, |cx| {
423423
// since the root module isn't visited as an item (because it isn't an
424424
// item), warn for it here.
425425
lint_callback!(cx, check_crate, krate);

src/librustc_lint/levels.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ fn lint_levels(tcx: TyCtxt<'_>, cnum: CrateNum) -> &LintLevelMap {
2828
let mut builder = LintLevelMapBuilder { levels, tcx, store };
2929
let krate = tcx.hir().krate();
3030

31-
let push = builder.levels.push(&krate.attrs, &store);
31+
let push = builder.levels.push(&krate.item.attrs, &store);
3232
builder.levels.register_id(hir::CRATE_HIR_ID);
3333
for macro_def in krate.exported_macros {
3434
builder.levels.register_id(macro_def.hir_id);

src/librustc_metadata/link_args.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ crate fn collect(tcx: TyCtxt<'_>) -> Vec<String> {
88
let mut collector = Collector { args: Vec::new() };
99
tcx.hir().krate().visit_all_item_likes(&mut collector);
1010

11-
for attr in tcx.hir().krate().attrs.iter() {
11+
for attr in tcx.hir().krate().item.attrs.iter() {
1212
if attr.has_name(sym::link_args) {
1313
if let Some(linkarg) = attr.value_str() {
1414
collector.add_link_args(&linkarg.as_str());

src/librustc_metadata/rmeta/encoder.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ impl<'tcx> EncodeContext<'tcx> {
332332
fn encode_info_for_items(&mut self) {
333333
let krate = self.tcx.hir().krate();
334334
let vis = Spanned { span: rustc_span::DUMMY_SP, node: hir::VisibilityKind::Public };
335-
self.encode_info_for_mod(hir::CRATE_HIR_ID, &krate.module, &krate.attrs, &vis);
335+
self.encode_info_for_mod(hir::CRATE_HIR_ID, &krate.item.module, &krate.item.attrs, &vis);
336336
krate.visit_all_item_likes(&mut self.as_deep_visitor());
337337
for macro_def in krate.exported_macros {
338338
self.visit_macro_def(macro_def);

src/librustc_passes/entry.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ fn entry_fn(tcx: TyCtxt<'_>, cnum: CrateNum) -> Option<(DefId, EntryFnType)> {
5959
}
6060

6161
// If the user wants no main function at all, then stop here.
62-
if attr::contains_name(&tcx.hir().krate().attrs, sym::no_main) {
62+
if attr::contains_name(&tcx.hir().krate().item.attrs, sym::no_main) {
6363
return None;
6464
}
6565

@@ -157,7 +157,7 @@ fn configure_main(tcx: TyCtxt<'_>, visitor: &EntryContext<'_, '_>) -> Option<(De
157157
}
158158

159159
fn no_main_err(tcx: TyCtxt<'_>, visitor: &EntryContext<'_, '_>) {
160-
let sp = tcx.hir().krate().span;
160+
let sp = tcx.hir().krate().item.span;
161161
if *tcx.sess.parse_sess.reached_eof.borrow() {
162162
// There's an unclosed brace that made the parser reach `Eof`, we shouldn't complain about
163163
// the missing `fn main()` then as it might have been hidden inside an unclosed block.

src/librustc_passes/stability.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -404,8 +404,8 @@ fn new_index(tcx: TyCtxt<'tcx>) -> Index<'tcx> {
404404

405405
annotator.annotate(
406406
hir::CRATE_HIR_ID,
407-
&krate.attrs,
408-
krate.span,
407+
&krate.item.attrs,
408+
krate.item.span,
409409
AnnotationKind::Required,
410410
|v| intravisit::walk_crate(v, krate),
411411
);
@@ -529,7 +529,7 @@ pub fn check_unused_or_stable_features(tcx: TyCtxt<'_>) {
529529
if tcx.stability().staged_api[&LOCAL_CRATE] {
530530
let krate = tcx.hir().krate();
531531
let mut missing = MissingStabilityAnnotations { tcx, access_levels };
532-
missing.check_missing_stability(hir::CRATE_HIR_ID, krate.span, "crate");
532+
missing.check_missing_stability(hir::CRATE_HIR_ID, krate.item.span, "crate");
533533
intravisit::walk_crate(&mut missing, krate);
534534
krate.visit_all_item_likes(&mut missing.as_deep_visitor());
535535
}

src/librustdoc/clean/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ impl Clean<ExternalCrate> for CrateNum {
141141
cx.tcx
142142
.hir()
143143
.krate()
144+
.item
144145
.module
145146
.item_ids
146147
.iter()
@@ -194,6 +195,7 @@ impl Clean<ExternalCrate> for CrateNum {
194195
cx.tcx
195196
.hir()
196197
.krate()
198+
.item
197199
.module
198200
.item_ids
199201
.iter()

src/librustdoc/test.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ pub fn run(options: Options) -> i32 {
112112
compiler.session().opts.unstable_features.is_nightly_build(),
113113
),
114114
};
115-
hir_collector.visit_testable("".to_string(), &krate.attrs, |this| {
115+
hir_collector.visit_testable("".to_string(), &krate.item.attrs, |this| {
116116
intravisit::walk_crate(this, krate);
117117
});
118118
});
@@ -146,6 +146,7 @@ fn scrape_test_config(krate: &::rustc_hir::Crate) -> TestOptions {
146146
TestOptions { no_crate_inject: false, display_warnings: false, attrs: Vec::new() };
147147

148148
let test_attrs: Vec<_> = krate
149+
.item
149150
.attrs
150151
.iter()
151152
.filter(|a| a.check_name(sym::doc))

0 commit comments

Comments
 (0)