Skip to content

Commit fee4216

Browse files
committed
Introduce OwnerNode::Crate.
1 parent 36a2806 commit fee4216

File tree

17 files changed

+69
-65
lines changed

17 files changed

+69
-65
lines changed

compiler/rustc_ast_lowering/src/lib.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -512,8 +512,11 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
512512
visit::walk_crate(&mut MiscCollector { lctx: &mut self }, c);
513513
visit::walk_crate(&mut item::ItemLowerer { lctx: &mut self }, c);
514514

515-
let module = self.lower_mod(&c.items, c.span);
515+
let module = self.arena.alloc(self.lower_mod(&c.items, c.span));
516516
self.lower_attrs(hir::CRATE_HIR_ID, &c.attrs);
517+
self.owners.ensure_contains_elem(CRATE_DEF_ID, || None);
518+
self.owners[CRATE_DEF_ID] = Some(hir::OwnerNode::Crate(module));
519+
517520
let body_ids = body_ids(&self.bodies);
518521
let proc_macros =
519522
c.proc_macros.iter().map(|id| self.node_id_to_hir_id[*id].unwrap()).collect();
@@ -548,7 +551,6 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
548551
}
549552

550553
let krate = hir::Crate {
551-
item: module,
552554
non_exported_macro_attrs: self.arena.alloc_from_iter(self.non_exported_macro_attrs),
553555
owners: self.owners,
554556
bodies: self.bodies,

compiler/rustc_hir/src/arena.rs

+1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ macro_rules! arena_types {
3636
[few] llvm_inline_asm: rustc_hir::LlvmInlineAsm<$tcx>,
3737
[] local: rustc_hir::Local<$tcx>,
3838
[few] macro_def: rustc_hir::MacroDef<$tcx>,
39+
[few] mod_: rustc_hir::Mod<$tcx>,
3940
[] param: rustc_hir::Param<$tcx>,
4041
[] pat: rustc_hir::Pat<$tcx>,
4142
[] path: rustc_hir::Path<$tcx>,

compiler/rustc_hir/src/hir.rs

+41-26
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// ignore-tidy-filelength
22
use crate::def::{CtorKind, DefKind, Res};
3-
use crate::def_id::DefId;
3+
use crate::def_id::{DefId, CRATE_DEF_ID};
44
crate use crate::hir_id::{HirId, ItemLocalId};
55
use crate::{itemlikevisit, LangItem};
66

@@ -628,7 +628,6 @@ pub struct ModuleItems {
628628
/// [rustc dev guide]: https://rustc-dev-guide.rust-lang.org/hir.html
629629
#[derive(Debug)]
630630
pub struct Crate<'hir> {
631-
pub item: Mod<'hir>,
632631
// Attributes from non-exported macros, kept only for collecting the library feature list.
633632
pub non_exported_macro_attrs: &'hir [Attribute],
634633

@@ -658,6 +657,10 @@ pub struct Crate<'hir> {
658657
}
659658

660659
impl Crate<'hir> {
660+
pub fn module(&self) -> &'hir Mod<'hir> {
661+
if let Some(OwnerNode::Crate(m)) = self.owners[CRATE_DEF_ID] { m } else { panic!() }
662+
}
663+
661664
pub fn item(&self, id: ItemId) -> &'hir Item<'hir> {
662665
self.owners[id.def_id].as_ref().unwrap().expect_item()
663666
}
@@ -698,7 +701,7 @@ impl Crate<'_> {
698701
OwnerNode::ForeignItem(item) => visitor.visit_foreign_item(item),
699702
OwnerNode::ImplItem(item) => visitor.visit_impl_item(item),
700703
OwnerNode::TraitItem(item) => visitor.visit_trait_item(item),
701-
OwnerNode::MacroDef(_) => {}
704+
OwnerNode::MacroDef(_) | OwnerNode::Crate(_) => {}
702705
}
703706
}
704707
}
@@ -713,7 +716,7 @@ impl Crate<'_> {
713716
Some(OwnerNode::ForeignItem(item)) => visitor.visit_foreign_item(item),
714717
Some(OwnerNode::ImplItem(item)) => visitor.visit_impl_item(item),
715718
Some(OwnerNode::TraitItem(item)) => visitor.visit_trait_item(item),
716-
Some(OwnerNode::MacroDef(_)) | None => {}
719+
Some(OwnerNode::MacroDef(_)) | Some(OwnerNode::Crate(_)) | None => {}
717720
})
718721
}
719722

@@ -2943,16 +2946,29 @@ pub enum OwnerNode<'hir> {
29432946
TraitItem(&'hir TraitItem<'hir>),
29442947
ImplItem(&'hir ImplItem<'hir>),
29452948
MacroDef(&'hir MacroDef<'hir>),
2949+
Crate(&'hir Mod<'hir>),
29462950
}
29472951

29482952
impl<'hir> OwnerNode<'hir> {
2949-
pub fn ident(&self) -> Ident {
2953+
pub fn ident(&self) -> Option<Ident> {
29502954
match self {
29512955
OwnerNode::Item(Item { ident, .. })
29522956
| OwnerNode::ForeignItem(ForeignItem { ident, .. })
29532957
| OwnerNode::ImplItem(ImplItem { ident, .. })
29542958
| OwnerNode::TraitItem(TraitItem { ident, .. })
2955-
| OwnerNode::MacroDef(MacroDef { ident, .. }) => *ident,
2959+
| OwnerNode::MacroDef(MacroDef { ident, .. }) => Some(*ident),
2960+
OwnerNode::Crate(..) => None,
2961+
}
2962+
}
2963+
2964+
pub fn span(&self) -> Span {
2965+
match self {
2966+
OwnerNode::Item(Item { span, .. })
2967+
| OwnerNode::ForeignItem(ForeignItem { span, .. })
2968+
| OwnerNode::ImplItem(ImplItem { span, .. })
2969+
| OwnerNode::TraitItem(TraitItem { span, .. })
2970+
| OwnerNode::MacroDef(MacroDef { span, .. })
2971+
| OwnerNode::Crate(Mod { inner: span, .. }) => *span,
29562972
}
29572973
}
29582974

@@ -2997,56 +3013,42 @@ impl<'hir> OwnerNode<'hir> {
29973013
| OwnerNode::ImplItem(ImplItem { def_id, .. })
29983014
| OwnerNode::ForeignItem(ForeignItem { def_id, .. })
29993015
| OwnerNode::MacroDef(MacroDef { def_id, .. }) => *def_id,
3016+
OwnerNode::Crate(..) => crate::CRATE_HIR_ID.owner,
30003017
}
30013018
}
30023019

30033020
pub fn expect_item(self) -> &'hir Item<'hir> {
30043021
match self {
30053022
OwnerNode::Item(n) => n,
3006-
OwnerNode::ForeignItem(_)
3007-
| OwnerNode::ImplItem(_)
3008-
| OwnerNode::TraitItem(_)
3009-
| OwnerNode::MacroDef(_) => panic!(),
3023+
_ => panic!(),
30103024
}
30113025
}
30123026

30133027
pub fn expect_foreign_item(self) -> &'hir ForeignItem<'hir> {
30143028
match self {
30153029
OwnerNode::ForeignItem(n) => n,
3016-
OwnerNode::Item(_)
3017-
| OwnerNode::ImplItem(_)
3018-
| OwnerNode::TraitItem(_)
3019-
| OwnerNode::MacroDef(_) => panic!(),
3030+
_ => panic!(),
30203031
}
30213032
}
30223033

30233034
pub fn expect_impl_item(self) -> &'hir ImplItem<'hir> {
30243035
match self {
30253036
OwnerNode::ImplItem(n) => n,
3026-
OwnerNode::ForeignItem(_)
3027-
| OwnerNode::Item(_)
3028-
| OwnerNode::TraitItem(_)
3029-
| OwnerNode::MacroDef(_) => panic!(),
3037+
_ => panic!(),
30303038
}
30313039
}
30323040

30333041
pub fn expect_trait_item(self) -> &'hir TraitItem<'hir> {
30343042
match self {
30353043
OwnerNode::TraitItem(n) => n,
3036-
OwnerNode::ForeignItem(_)
3037-
| OwnerNode::ImplItem(_)
3038-
| OwnerNode::Item(_)
3039-
| OwnerNode::MacroDef(_) => panic!(),
3044+
_ => panic!(),
30403045
}
30413046
}
30423047

30433048
pub fn expect_macro_def(self) -> &'hir MacroDef<'hir> {
30443049
match self {
30453050
OwnerNode::MacroDef(n) => n,
3046-
OwnerNode::ForeignItem(_)
3047-
| OwnerNode::ImplItem(_)
3048-
| OwnerNode::TraitItem(_)
3049-
| OwnerNode::Item(_) => panic!(),
3051+
_ => panic!(),
30503052
}
30513053
}
30523054
}
@@ -3089,6 +3091,7 @@ impl<'hir> Into<Node<'hir>> for OwnerNode<'hir> {
30893091
OwnerNode::ImplItem(n) => Node::ImplItem(n),
30903092
OwnerNode::TraitItem(n) => Node::TraitItem(n),
30913093
OwnerNode::MacroDef(n) => Node::MacroDef(n),
3094+
OwnerNode::Crate(n) => Node::Crate(n),
30923095
}
30933096
}
30943097
}
@@ -3221,6 +3224,18 @@ impl<'hir> Node<'hir> {
32213224
_ => Constness::NotConst,
32223225
}
32233226
}
3227+
3228+
pub fn as_owner(self) -> Option<OwnerNode<'hir>> {
3229+
match self {
3230+
Node::Item(i) => Some(OwnerNode::Item(i)),
3231+
Node::ForeignItem(i) => Some(OwnerNode::ForeignItem(i)),
3232+
Node::TraitItem(i) => Some(OwnerNode::TraitItem(i)),
3233+
Node::ImplItem(i) => Some(OwnerNode::ImplItem(i)),
3234+
Node::MacroDef(i) => Some(OwnerNode::MacroDef(i)),
3235+
Node::Crate(i) => Some(OwnerNode::Crate(i)),
3236+
_ => None,
3237+
}
3238+
}
32243239
}
32253240

32263241
// Some nodes are used a lot. Make sure they don't unintentionally get bigger.

compiler/rustc_hir/src/intravisit.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -478,7 +478,8 @@ pub trait Visitor<'v>: Sized {
478478

479479
/// Walks the contents of a crate. See also `Crate::visit_all_items`.
480480
pub fn walk_crate<'v, V: Visitor<'v>>(visitor: &mut V, krate: &'v Crate<'v>) {
481-
visitor.visit_mod(&krate.item, krate.item.inner, CRATE_HIR_ID);
481+
let top_mod = krate.module();
482+
visitor.visit_mod(top_mod, top_mod.inner, CRATE_HIR_ID);
482483
walk_list!(visitor, visit_macro_def, krate.exported_macros());
483484
for (&id, attrs) in krate.attrs.iter() {
484485
for a in *attrs {

compiler/rustc_hir_pretty/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ pub fn print_crate<'a>(
169169
// When printing the AST, we sometimes need to inject `#[no_std]` here.
170170
// Since you can't compile the HIR, it's not necessary.
171171

172-
s.print_mod(&krate.item, s.attrs(hir::CRATE_HIR_ID));
172+
s.print_mod(&krate.module(), s.attrs(hir::CRATE_HIR_ID));
173173
s.print_remaining_comments();
174174
s.s.eof()
175175
}

compiler/rustc_lint/src/builtin.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -568,7 +568,7 @@ impl<'tcx> LateLintPass<'tcx> for MissingDoc {
568568
}
569569

570570
fn check_crate(&mut self, cx: &LateContext<'_>, krate: &hir::Crate<'_>) {
571-
self.check_missing_docs_attrs(cx, hir::CRATE_HIR_ID, krate.item.inner, "the", "crate");
571+
self.check_missing_docs_attrs(cx, hir::CRATE_HIR_ID, krate.module().inner, "the", "crate");
572572

573573
for macro_def in krate.exported_macros() {
574574
// Non exported macros should be skipped, since `missing_docs` only

compiler/rustc_metadata/src/rmeta/encoder.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
439439

440440
fn encode_info_for_items(&mut self) {
441441
let krate = self.tcx.hir().krate();
442-
self.encode_info_for_mod(CRATE_DEF_ID, &krate.item);
442+
self.encode_info_for_mod(CRATE_DEF_ID, krate.module());
443443

444444
// Proc-macro crates only export proc-macro items, which are looked
445445
// up using `proc_macro_data`

compiler/rustc_middle/src/hir/map/collector.rs

+2-18
Original file line numberDiff line numberDiff line change
@@ -77,23 +77,7 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
7777
definitions: &'a definitions::Definitions,
7878
mut hcx: StableHashingContext<'a>,
7979
) -> NodeCollector<'a, 'hir> {
80-
let hash = {
81-
let Crate {
82-
ref item,
83-
// These fields are handled separately:
84-
non_exported_macro_attrs: _,
85-
owners: _,
86-
trait_impls: _,
87-
bodies: _,
88-
body_ids: _,
89-
modules: _,
90-
proc_macros: _,
91-
trait_map: _,
92-
attrs: _,
93-
} = *krate;
94-
95-
hash_body(&mut hcx, item)
96-
};
80+
let hash = hash_body(&mut hcx, krate.module());
9781

9882
let mut collector = NodeCollector {
9983
arena,
@@ -108,7 +92,7 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
10892
};
10993
collector.insert_entry(
11094
hir::CRATE_HIR_ID,
111-
Entry { parent: hir::CRATE_HIR_ID, node: Node::Crate(&krate.item) },
95+
Entry { parent: hir::CRATE_HIR_ID, node: Node::Crate(&krate.module()) },
11296
hash,
11397
);
11498

compiler/rustc_passes/src/entry.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ fn configure_main(tcx: TyCtxt<'_>, visitor: &EntryContext<'_, '_>) -> Option<(De
183183
}
184184

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

compiler/rustc_passes/src/stability.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -732,7 +732,7 @@ fn stability_index(tcx: TyCtxt<'tcx>, (): ()) -> Index<'tcx> {
732732

733733
annotator.annotate(
734734
hir::CRATE_HIR_ID,
735-
krate.item.inner,
735+
krate.module().inner,
736736
None,
737737
AnnotationKind::Required,
738738
InheritDeprecation::Yes,
@@ -929,7 +929,7 @@ pub fn check_unused_or_stable_features(tcx: TyCtxt<'_>) {
929929
if tcx.stability().staged_api[&LOCAL_CRATE] {
930930
let krate = tcx.hir().krate();
931931
let mut missing = MissingStabilityAnnotations { tcx, access_levels };
932-
missing.check_missing_stability(hir::CRATE_HIR_ID, krate.item.inner);
932+
missing.check_missing_stability(hir::CRATE_HIR_ID, krate.module().inner);
933933
intravisit::walk_crate(&mut missing, krate);
934934
krate.visit_all_item_likes(&mut missing.as_deep_visitor());
935935
}

compiler/rustc_save_analysis/src/dump_visitor.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ impl<'tcx> DumpVisitor<'tcx> {
146146
},
147147
crate_root: crate_root.unwrap_or_else(|| "<no source>".to_owned()),
148148
external_crates: self.save_ctxt.get_external_crates(),
149-
span: self.span_from_span(krate.item.inner),
149+
span: self.span_from_span(krate.module().inner),
150150
};
151151

152152
self.dumper.crate_prelude(data);
@@ -1092,11 +1092,12 @@ impl<'tcx> DumpVisitor<'tcx> {
10921092
format!("::{}", self.tcx.def_path_str(self.tcx.hir().local_def_id(id).to_def_id()));
10931093

10941094
let sm = self.tcx.sess.source_map();
1095-
let filename = sm.span_to_filename(krate.item.inner);
1095+
let krate_mod = krate.module();
1096+
let filename = sm.span_to_filename(krate_mod.inner);
10961097
let data_id = id_from_hir_id(id, &self.save_ctxt);
10971098
let children =
1098-
krate.item.item_ids.iter().map(|i| id_from_def_id(i.def_id.to_def_id())).collect();
1099-
let span = self.span_from_span(krate.item.inner);
1099+
krate_mod.item_ids.iter().map(|i| id_from_def_id(i.def_id.to_def_id())).collect();
1100+
let span = self.span_from_span(krate_mod.inner);
11001101
let attrs = self.tcx.hir().attrs(id);
11011102

11021103
self.dumper.dump_def(

src/librustdoc/clean/types.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ impl ExternalCrate {
227227
if root.is_local() {
228228
tcx.hir()
229229
.krate()
230-
.item
230+
.module()
231231
.item_ids
232232
.iter()
233233
.filter_map(|&id| {
@@ -293,7 +293,7 @@ impl ExternalCrate {
293293
if root.is_local() {
294294
tcx.hir()
295295
.krate()
296-
.item
296+
.module()
297297
.item_ids
298298
.iter()
299299
.filter_map(|&id| {

src/librustdoc/doctest.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ crate fn run(options: Options) -> Result<(), ErrorReported> {
144144
hir_collector.visit_testable(
145145
"".to_string(),
146146
CRATE_HIR_ID,
147-
krate.item.inner,
147+
krate.module().inner,
148148
|this| {
149149
intravisit::walk_crate(this, krate);
150150
},

src/librustdoc/visit_ast.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,11 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
7272
}
7373

7474
crate fn visit(mut self, krate: &'tcx hir::Crate<'_>) -> Module<'tcx> {
75-
let span = krate.item.inner;
75+
let span = krate.module().inner;
7676
let mut top_level_module = self.visit_mod_contents(
7777
&Spanned { span, node: hir::VisibilityKind::Public },
7878
hir::CRATE_HIR_ID,
79-
&krate.item,
79+
&krate.module(),
8080
self.cx.tcx.crate_name(LOCAL_CRATE),
8181
);
8282
// Attach the crate's exported macros to the top-level module.

src/test/ui-fulldeps/auxiliary/lint-for-crate-rpass.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ macro_rules! fake_lint_pass {
3333
if !cx.sess().contains_name(attrs, $attr) {
3434
cx.lint(CRATE_NOT_OKAY, |lint| {
3535
let msg = format!("crate is not marked with #![{}]", $attr);
36-
lint.build(&msg).set_span(krate.item.inner).emit()
36+
lint.build(&msg).set_span(krate.module().inner).emit()
3737
});
3838
}
3939
)*

src/test/ui-fulldeps/auxiliary/lint-for-crate.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ impl<'tcx> LateLintPass<'tcx> for Pass {
3131
if !cx.sess().contains_name(attrs, Symbol::intern("crate_okay")) {
3232
cx.lint(CRATE_NOT_OKAY, |lint| {
3333
lint.build("crate is not marked with #![crate_okay]")
34-
.set_span(krate.item.inner)
34+
.set_span(krate.module().inner)
3535
.emit()
3636
});
3737
}

src/tools/clippy/clippy_lints/src/missing_doc.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ impl<'tcx> LateLintPass<'tcx> for MissingDoc {
106106

107107
fn check_crate(&mut self, cx: &LateContext<'tcx>, krate: &'tcx hir::Crate<'_>) {
108108
let attrs = cx.tcx.hir().attrs(hir::CRATE_HIR_ID);
109-
self.check_missing_docs_attrs(cx, attrs, krate.item.inner, "the", "crate");
109+
self.check_missing_docs_attrs(cx, attrs, krate.module().inner, "the", "crate");
110110
}
111111

112112
fn check_item(&mut self, cx: &LateContext<'tcx>, it: &'tcx hir::Item<'_>) {

0 commit comments

Comments
 (0)