Skip to content

Commit a5029ac

Browse files
committed
Auto merge of #83684 - cjgillot:csp, r=petrochenkov
Remove hir::CrateItem. The crate span is exactly the crate module's inner span. There is no need to store it twice.
2 parents 2a32abb + fbfef40 commit a5029ac

File tree

17 files changed

+28
-42
lines changed

17 files changed

+28
-42
lines changed

compiler/rustc_ast_lowering/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -569,7 +569,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
569569
}
570570

571571
hir::Crate {
572-
item: hir::CrateItem { module, span: c.span },
572+
item: module,
573573
exported_macros: self.arena.alloc_from_iter(self.exported_macros),
574574
non_exported_macro_attrs: self.arena.alloc_from_iter(self.non_exported_macro_attrs),
575575
items: self.items,

compiler/rustc_hir/src/hir.rs

+2-9
Original file line numberDiff line numberDiff line change
@@ -625,13 +625,6 @@ pub struct ModuleItems {
625625
pub foreign_items: BTreeSet<ForeignItemId>,
626626
}
627627

628-
/// A type representing only the top-level module.
629-
#[derive(Encodable, Debug, HashStable_Generic)]
630-
pub struct CrateItem<'hir> {
631-
pub module: Mod<'hir>,
632-
pub span: Span,
633-
}
634-
635628
/// The top-level data structure that stores the entire contents of
636629
/// the crate currently being compiled.
637630
///
@@ -640,7 +633,7 @@ pub struct CrateItem<'hir> {
640633
/// [rustc dev guide]: https://rustc-dev-guide.rust-lang.org/hir.html
641634
#[derive(Debug)]
642635
pub struct Crate<'hir> {
643-
pub item: CrateItem<'hir>,
636+
pub item: Mod<'hir>,
644637
pub exported_macros: &'hir [MacroDef<'hir>],
645638
// Attributes from non-exported macros, kept only for collecting the library feature list.
646639
pub non_exported_macro_attrs: &'hir [Attribute],
@@ -2983,7 +2976,7 @@ pub enum Node<'hir> {
29832976
GenericParam(&'hir GenericParam<'hir>),
29842977
Visibility(&'hir Visibility<'hir>),
29852978

2986-
Crate(&'hir CrateItem<'hir>),
2979+
Crate(&'hir Mod<'hir>),
29872980
}
29882981

29892982
impl<'hir> Node<'hir> {

compiler/rustc_hir/src/intravisit.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -478,7 +478,7 @@ 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.module, krate.item.span, CRATE_HIR_ID);
481+
visitor.visit_mod(&krate.item, krate.item.inner, CRATE_HIR_ID);
482482
walk_list!(visitor, visit_macro_def, krate.exported_macros);
483483
for (&id, attrs) in krate.attrs.iter() {
484484
for a in *attrs {

compiler/rustc_hir_pretty/src/lib.rs

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

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

compiler/rustc_lint/src/builtin.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -565,7 +565,7 @@ impl<'tcx> LateLintPass<'tcx> for MissingDoc {
565565
}
566566

567567
fn check_crate(&mut self, cx: &LateContext<'_>, krate: &hir::Crate<'_>) {
568-
self.check_missing_docs_attrs(cx, hir::CRATE_HIR_ID, krate.item.span, "the", "crate");
568+
self.check_missing_docs_attrs(cx, hir::CRATE_HIR_ID, krate.item.inner, "the", "crate");
569569

570570
for macro_def in krate.exported_macros {
571571
let attrs = cx.tcx.hir().attrs(macro_def.hir_id());

compiler/rustc_metadata/src/rmeta/encoder.rs

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

428428
fn encode_info_for_items(&mut self) {
429429
let krate = self.tcx.hir().krate();
430-
self.encode_info_for_mod(CRATE_DEF_ID, &krate.item.module);
430+
self.encode_info_for_mod(CRATE_DEF_ID, &krate.item);
431431

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

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,7 @@ impl<'hir> Map<'hir> {
459459
let hir_id = self.local_def_id_to_hir_id(module);
460460
match self.get_entry(hir_id).node {
461461
Node::Item(&Item { span, kind: ItemKind::Mod(ref m), .. }) => (m, span, hir_id),
462-
Node::Crate(item) => (&item.module, item.span, hir_id),
462+
Node::Crate(item) => (&item, item.inner, hir_id),
463463
node => panic!("not a module: {:?}", node),
464464
}
465465
}
@@ -868,7 +868,7 @@ impl<'hir> Map<'hir> {
868868
Node::Visibility(v) => bug!("unexpected Visibility {:?}", v),
869869
Node::Local(local) => local.span,
870870
Node::MacroDef(macro_def) => macro_def.span,
871-
Node::Crate(item) => item.span,
871+
Node::Crate(item) => item.inner,
872872
};
873873
Some(span)
874874
}

compiler/rustc_passes/src/entry.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ fn configure_main(
171171
}
172172

173173
fn no_main_err(tcx: TyCtxt<'_>, visitor: &EntryContext<'_, '_>) {
174-
let sp = tcx.hir().krate().item.span;
174+
let sp = tcx.hir().krate().item.inner;
175175
if *tcx.sess.parse_sess.reached_eof.borrow() {
176176
// There's an unclosed brace that made the parser reach `Eof`, we shouldn't complain about
177177
// 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
@@ -686,7 +686,7 @@ fn new_index(tcx: TyCtxt<'tcx>) -> Index<'tcx> {
686686

687687
annotator.annotate(
688688
hir::CRATE_HIR_ID,
689-
krate.item.span,
689+
krate.item.inner,
690690
AnnotationKind::Required,
691691
InheritDeprecation::Yes,
692692
InheritConstStability::No,
@@ -885,7 +885,7 @@ pub fn check_unused_or_stable_features(tcx: TyCtxt<'_>) {
885885
if tcx.stability().staged_api[&LOCAL_CRATE] {
886886
let krate = tcx.hir().krate();
887887
let mut missing = MissingStabilityAnnotations { tcx, access_levels };
888-
missing.check_missing_stability(hir::CRATE_HIR_ID, krate.item.span);
888+
missing.check_missing_stability(hir::CRATE_HIR_ID, krate.item.inner);
889889
intravisit::walk_crate(&mut missing, krate);
890890
krate.visit_all_item_likes(&mut missing.as_deep_visitor());
891891
}

compiler/rustc_save_analysis/src/dump_visitor.rs

+5-10
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ impl<'tcx> DumpVisitor<'tcx> {
151151
},
152152
crate_root: crate_root.unwrap_or_else(|| "<no source>".to_owned()),
153153
external_crates: self.save_ctxt.get_external_crates(),
154-
span: self.span_from_span(krate.item.span),
154+
span: self.span_from_span(krate.item.inner),
155155
};
156156

157157
self.dumper.crate_prelude(data);
@@ -1097,16 +1097,11 @@ impl<'tcx> DumpVisitor<'tcx> {
10971097
format!("::{}", self.tcx.def_path_str(self.tcx.hir().local_def_id(id).to_def_id()));
10981098

10991099
let sm = self.tcx.sess.source_map();
1100-
let filename = sm.span_to_filename(krate.item.span);
1100+
let filename = sm.span_to_filename(krate.item.inner);
11011101
let data_id = id_from_hir_id(id, &self.save_ctxt);
1102-
let children = krate
1103-
.item
1104-
.module
1105-
.item_ids
1106-
.iter()
1107-
.map(|i| id_from_def_id(i.def_id.to_def_id()))
1108-
.collect();
1109-
let span = self.span_from_span(krate.item.span);
1102+
let children =
1103+
krate.item.item_ids.iter().map(|i| id_from_def_id(i.def_id.to_def_id())).collect();
1104+
let span = self.span_from_span(krate.item.inner);
11101105
let attrs = self.tcx.hir().attrs(id);
11111106

11121107
self.dumper.dump_def(

src/librustdoc/clean/mod.rs

-2
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,6 @@ impl Clean<ExternalCrate> for CrateNum {
129129
tcx.hir()
130130
.krate()
131131
.item
132-
.module
133132
.item_ids
134133
.iter()
135134
.filter_map(|&id| {
@@ -174,7 +173,6 @@ impl Clean<ExternalCrate> for CrateNum {
174173
tcx.hir()
175174
.krate()
176175
.item
177-
.module
178176
.item_ids
179177
.iter()
180178
.filter_map(|&id| {

src/librustdoc/doctest.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ crate fn run(options: Options) -> Result<(), ErrorReported> {
141141
hir_collector.visit_testable(
142142
"".to_string(),
143143
CRATE_HIR_ID,
144-
krate.item.span,
144+
krate.item.inner,
145145
|this| {
146146
intravisit::walk_crate(this, krate);
147147
},

src/librustdoc/visit_ast.rs

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

7373
crate fn visit(mut self, krate: &'tcx hir::Crate<'_>) -> Module<'tcx> {
7474
let mut top_level_module = self.visit_mod_contents(
75-
krate.item.span,
75+
krate.item.inner,
7676
&Spanned { span: rustc_span::DUMMY_SP, node: hir::VisibilityKind::Public },
7777
hir::CRATE_HIR_ID,
78-
&krate.item.module,
78+
&krate.item,
7979
self.cx.tcx.crate_name,
8080
);
8181
top_level_module.is_crate = true;

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.span).emit()
36+
lint.build(&msg).set_span(krate.item.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.span)
34+
.set_span(krate.item.inner)
3535
.emit()
3636
});
3737
}

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

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

129129
fn check_crate(&mut self, cx: &LateContext<'tcx>, krate: &'tcx hir::Crate<'_>) {
130130
let attrs = cx.tcx.hir().attrs(hir::CRATE_HIR_ID);
131-
self.check_missing_docs_attrs(cx, attrs, krate.item.span, "the", "crate");
131+
self.check_missing_docs_attrs(cx, attrs, krate.item.inner, "the", "crate");
132132
}
133133

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

src/tools/clippy/clippy_utils/src/lib.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,10 @@ use rustc_hir::def::{CtorKind, CtorOf, DefKind, Res};
6161
use rustc_hir::def_id::{DefId, LOCAL_CRATE};
6262
use rustc_hir::intravisit::{self, NestedVisitorMap, Visitor};
6363
use rustc_hir::{
64-
def, Arm, BindingAnnotation, Block, Body, Constness, CrateItem, Expr, ExprKind, FieldDef, FnDecl, ForeignItem,
65-
GenericArgs, GenericParam, HirId, Impl, ImplItem, ImplItemKind, Item, ItemKind, LangItem, Lifetime, Local,
66-
MacroDef, MatchSource, Node, Param, Pat, PatKind, Path, PathSegment, QPath, Stmt, TraitItem, TraitItemKind,
67-
TraitRef, TyKind, Variant, Visibility,
64+
def, Arm, BindingAnnotation, Block, Body, Constness, Expr, ExprKind, FieldDef, FnDecl, ForeignItem, GenericArgs,
65+
GenericParam, HirId, Impl, ImplItem, ImplItemKind, Item, ItemKind, LangItem, Lifetime, Local, MacroDef,
66+
MatchSource, Mod, Node, Param, Pat, PatKind, Path, PathSegment, QPath, Stmt, TraitItem, TraitItemKind, TraitRef,
67+
TyKind, Variant, Visibility,
6868
};
6969
use rustc_lint::{LateContext, Level, Lint, LintContext};
7070
use rustc_middle::hir::exports::Export;
@@ -743,7 +743,7 @@ pub fn get_node_span(node: Node<'_>) -> Option<Span> {
743743
| Node::Lifetime(Lifetime { span, .. })
744744
| Node::GenericParam(GenericParam { span, .. })
745745
| Node::Visibility(Visibility { span, .. })
746-
| Node::Crate(CrateItem { span, .. }) => Some(*span),
746+
| Node::Crate(Mod { inner: span, .. }) => Some(*span),
747747
Node::Ctor(_) | Node::AnonConst(_) => None,
748748
}
749749
}

0 commit comments

Comments
 (0)