Skip to content

Commit

Permalink
Auto merge of #83684 - cjgillot:csp, r=petrochenkov
Browse files Browse the repository at this point in the history
Remove hir::CrateItem.

The crate span is exactly the crate module's inner span. There is no need to store it twice.
  • Loading branch information
bors committed Mar 31, 2021
2 parents 2a32abb + fbfef40 commit a5029ac
Show file tree
Hide file tree
Showing 17 changed files with 28 additions and 42 deletions.
2 changes: 1 addition & 1 deletion compiler/rustc_ast_lowering/src/lib.rs
Expand Up @@ -569,7 +569,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
}

hir::Crate {
item: hir::CrateItem { module, span: c.span },
item: module,
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,
Expand Down
11 changes: 2 additions & 9 deletions compiler/rustc_hir/src/hir.rs
Expand Up @@ -625,13 +625,6 @@ pub struct ModuleItems {
pub foreign_items: BTreeSet<ForeignItemId>,
}

/// A type representing only the top-level module.
#[derive(Encodable, Debug, HashStable_Generic)]
pub struct CrateItem<'hir> {
pub module: Mod<'hir>,
pub span: Span,
}

/// The top-level data structure that stores the entire contents of
/// the crate currently being compiled.
///
Expand All @@ -640,7 +633,7 @@ pub struct CrateItem<'hir> {
/// [rustc dev guide]: https://rustc-dev-guide.rust-lang.org/hir.html
#[derive(Debug)]
pub struct Crate<'hir> {
pub item: CrateItem<'hir>,
pub item: Mod<'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],
Expand Down Expand Up @@ -2983,7 +2976,7 @@ pub enum Node<'hir> {
GenericParam(&'hir GenericParam<'hir>),
Visibility(&'hir Visibility<'hir>),

Crate(&'hir CrateItem<'hir>),
Crate(&'hir Mod<'hir>),
}

impl<'hir> Node<'hir> {
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_hir/src/intravisit.rs
Expand Up @@ -478,7 +478,7 @@ 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.item.module, krate.item.span, CRATE_HIR_ID);
visitor.visit_mod(&krate.item, krate.item.inner, CRATE_HIR_ID);
walk_list!(visitor, visit_macro_def, krate.exported_macros);
for (&id, attrs) in krate.attrs.iter() {
for a in *attrs {
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_hir_pretty/src/lib.rs
Expand Up @@ -170,7 +170,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.item.module, s.attrs(hir::CRATE_HIR_ID));
s.print_mod(&krate.item, s.attrs(hir::CRATE_HIR_ID));
s.print_remaining_comments();
s.s.eof()
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_lint/src/builtin.rs
Expand Up @@ -565,7 +565,7 @@ impl<'tcx> LateLintPass<'tcx> for MissingDoc {
}

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

for macro_def in krate.exported_macros {
let attrs = cx.tcx.hir().attrs(macro_def.hir_id());
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_metadata/src/rmeta/encoder.rs
Expand Up @@ -427,7 +427,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {

fn encode_info_for_items(&mut self) {
let krate = self.tcx.hir().krate();
self.encode_info_for_mod(CRATE_DEF_ID, &krate.item.module);
self.encode_info_for_mod(CRATE_DEF_ID, &krate.item);

// Proc-macro crates only export proc-macro items, which are looked
// up using `proc_macro_data`
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_middle/src/hir/map/mod.rs
Expand Up @@ -459,7 +459,7 @@ impl<'hir> Map<'hir> {
let hir_id = self.local_def_id_to_hir_id(module);
match self.get_entry(hir_id).node {
Node::Item(&Item { span, kind: ItemKind::Mod(ref m), .. }) => (m, span, hir_id),
Node::Crate(item) => (&item.module, item.span, hir_id),
Node::Crate(item) => (&item, item.inner, hir_id),
node => panic!("not a module: {:?}", node),
}
}
Expand Down Expand Up @@ -868,7 +868,7 @@ impl<'hir> Map<'hir> {
Node::Visibility(v) => bug!("unexpected Visibility {:?}", v),
Node::Local(local) => local.span,
Node::MacroDef(macro_def) => macro_def.span,
Node::Crate(item) => item.span,
Node::Crate(item) => item.inner,
};
Some(span)
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_passes/src/entry.rs
Expand Up @@ -171,7 +171,7 @@ fn configure_main(
}

fn no_main_err(tcx: TyCtxt<'_>, visitor: &EntryContext<'_, '_>) {
let sp = tcx.hir().krate().item.span;
let sp = tcx.hir().krate().item.inner;
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.
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_passes/src/stability.rs
Expand Up @@ -686,7 +686,7 @@ fn new_index(tcx: TyCtxt<'tcx>) -> Index<'tcx> {

annotator.annotate(
hir::CRATE_HIR_ID,
krate.item.span,
krate.item.inner,
AnnotationKind::Required,
InheritDeprecation::Yes,
InheritConstStability::No,
Expand Down Expand Up @@ -885,7 +885,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.item.span);
missing.check_missing_stability(hir::CRATE_HIR_ID, krate.item.inner);
intravisit::walk_crate(&mut missing, krate);
krate.visit_all_item_likes(&mut missing.as_deep_visitor());
}
Expand Down
15 changes: 5 additions & 10 deletions compiler/rustc_save_analysis/src/dump_visitor.rs
Expand Up @@ -151,7 +151,7 @@ impl<'tcx> DumpVisitor<'tcx> {
},
crate_root: crate_root.unwrap_or_else(|| "<no source>".to_owned()),
external_crates: self.save_ctxt.get_external_crates(),
span: self.span_from_span(krate.item.span),
span: self.span_from_span(krate.item.inner),
};

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

let sm = self.tcx.sess.source_map();
let filename = sm.span_to_filename(krate.item.span);
let filename = sm.span_to_filename(krate.item.inner);
let data_id = id_from_hir_id(id, &self.save_ctxt);
let children = krate
.item
.module
.item_ids
.iter()
.map(|i| id_from_def_id(i.def_id.to_def_id()))
.collect();
let span = self.span_from_span(krate.item.span);
let children =
krate.item.item_ids.iter().map(|i| id_from_def_id(i.def_id.to_def_id())).collect();
let span = self.span_from_span(krate.item.inner);
let attrs = self.tcx.hir().attrs(id);

self.dumper.dump_def(
Expand Down
2 changes: 0 additions & 2 deletions src/librustdoc/clean/mod.rs
Expand Up @@ -129,7 +129,6 @@ impl Clean<ExternalCrate> for CrateNum {
tcx.hir()
.krate()
.item
.module
.item_ids
.iter()
.filter_map(|&id| {
Expand Down Expand Up @@ -174,7 +173,6 @@ impl Clean<ExternalCrate> for CrateNum {
tcx.hir()
.krate()
.item
.module
.item_ids
.iter()
.filter_map(|&id| {
Expand Down
2 changes: 1 addition & 1 deletion src/librustdoc/doctest.rs
Expand Up @@ -141,7 +141,7 @@ crate fn run(options: Options) -> Result<(), ErrorReported> {
hir_collector.visit_testable(
"".to_string(),
CRATE_HIR_ID,
krate.item.span,
krate.item.inner,
|this| {
intravisit::walk_crate(this, krate);
},
Expand Down
4 changes: 2 additions & 2 deletions src/librustdoc/visit_ast.rs
Expand Up @@ -72,10 +72,10 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {

crate fn visit(mut self, krate: &'tcx hir::Crate<'_>) -> Module<'tcx> {
let mut top_level_module = self.visit_mod_contents(
krate.item.span,
krate.item.inner,
&Spanned { span: rustc_span::DUMMY_SP, node: hir::VisibilityKind::Public },
hir::CRATE_HIR_ID,
&krate.item.module,
&krate.item,
self.cx.tcx.crate_name,
);
top_level_module.is_crate = true;
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui-fulldeps/auxiliary/lint-for-crate-rpass.rs
Expand Up @@ -33,7 +33,7 @@ macro_rules! fake_lint_pass {
if !cx.sess().contains_name(attrs, $attr) {
cx.lint(CRATE_NOT_OKAY, |lint| {
let msg = format!("crate is not marked with #![{}]", $attr);
lint.build(&msg).set_span(krate.item.span).emit()
lint.build(&msg).set_span(krate.item.inner).emit()
});
}
)*
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui-fulldeps/auxiliary/lint-for-crate.rs
Expand Up @@ -31,7 +31,7 @@ impl<'tcx> LateLintPass<'tcx> for Pass {
if !cx.sess().contains_name(attrs, Symbol::intern("crate_okay")) {
cx.lint(CRATE_NOT_OKAY, |lint| {
lint.build("crate is not marked with #![crate_okay]")
.set_span(krate.item.span)
.set_span(krate.item.inner)
.emit()
});
}
Expand Down
2 changes: 1 addition & 1 deletion src/tools/clippy/clippy_lints/src/missing_doc.rs
Expand Up @@ -128,7 +128,7 @@ impl<'tcx> LateLintPass<'tcx> for MissingDoc {

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

fn check_item(&mut self, cx: &LateContext<'tcx>, it: &'tcx hir::Item<'_>) {
Expand Down
10 changes: 5 additions & 5 deletions src/tools/clippy/clippy_utils/src/lib.rs
Expand Up @@ -61,10 +61,10 @@ use rustc_hir::def::{CtorKind, CtorOf, DefKind, Res};
use rustc_hir::def_id::{DefId, LOCAL_CRATE};
use rustc_hir::intravisit::{self, NestedVisitorMap, Visitor};
use rustc_hir::{
def, Arm, BindingAnnotation, Block, Body, Constness, CrateItem, Expr, ExprKind, FieldDef, FnDecl, ForeignItem,
GenericArgs, GenericParam, HirId, Impl, ImplItem, ImplItemKind, Item, ItemKind, LangItem, Lifetime, Local,
MacroDef, MatchSource, Node, Param, Pat, PatKind, Path, PathSegment, QPath, Stmt, TraitItem, TraitItemKind,
TraitRef, TyKind, Variant, Visibility,
def, Arm, BindingAnnotation, Block, Body, Constness, Expr, ExprKind, FieldDef, FnDecl, ForeignItem, GenericArgs,
GenericParam, HirId, Impl, ImplItem, ImplItemKind, Item, ItemKind, LangItem, Lifetime, Local, MacroDef,
MatchSource, Mod, Node, Param, Pat, PatKind, Path, PathSegment, QPath, Stmt, TraitItem, TraitItemKind, TraitRef,
TyKind, Variant, Visibility,
};
use rustc_lint::{LateContext, Level, Lint, LintContext};
use rustc_middle::hir::exports::Export;
Expand Down Expand Up @@ -743,7 +743,7 @@ pub fn get_node_span(node: Node<'_>) -> Option<Span> {
| Node::Lifetime(Lifetime { span, .. })
| Node::GenericParam(GenericParam { span, .. })
| Node::Visibility(Visibility { span, .. })
| Node::Crate(CrateItem { span, .. }) => Some(*span),
| Node::Crate(Mod { inner: span, .. }) => Some(*span),
Node::Ctor(_) | Node::AnonConst(_) => None,
}
}
Expand Down

0 comments on commit a5029ac

Please sign in to comment.