Skip to content

Commit c02371c

Browse files
committed
Auto merge of #88880 - cjgillot:no-krate, r=oli-obk
Rework HIR API to make invocations of the hir_crate query harder. `hir_crate` forces the recomputation of queries that depend on it. This PR aims at avoiding useless invocations of `hir_crate` by making dependent code go through `tcx.hir()`.
2 parents b6057bf + 77c3002 commit c02371c

File tree

64 files changed

+193
-209
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

64 files changed

+193
-209
lines changed

compiler/rustc_driver/src/pretty.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -489,7 +489,7 @@ fn print_with_analysis(
489489
let mut out = String::new();
490490
abort_on_err(rustc_typeck::check_crate(tcx), tcx.sess);
491491
debug!("pretty printing THIR tree");
492-
for did in tcx.body_owners() {
492+
for did in tcx.hir().body_owners() {
493493
let _ = writeln!(
494494
out,
495495
"{:?}:\n{}\n",

compiler/rustc_hir/src/hir.rs

Lines changed: 1 addition & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::def::{CtorKind, DefKind, Res};
22
use crate::def_id::{DefId, CRATE_DEF_ID};
33
crate use crate::hir_id::{HirId, ItemLocalId};
4-
use crate::{itemlikevisit, LangItem};
4+
use crate::LangItem;
55

66
use rustc_ast::util::parser::ExprPrecedence;
77
use rustc_ast::{self as ast, CrateSugar, LlvmAsmDialect};
@@ -10,7 +10,6 @@ pub use rustc_ast::{BorrowKind, ImplPolarity, IsAuto};
1010
pub use rustc_ast::{CaptureBy, Movability, Mutability};
1111
use rustc_ast::{InlineAsmOptions, InlineAsmTemplatePiece};
1212
use rustc_data_structures::fx::FxHashMap;
13-
use rustc_data_structures::sync::{par_for_each_in, Send, Sync};
1413
use rustc_index::vec::IndexVec;
1514
use rustc_macros::HashStable_Generic;
1615
use rustc_span::source_map::Spanned;
@@ -708,52 +707,6 @@ impl Crate<'hir> {
708707
}
709708
}
710709

711-
impl Crate<'_> {
712-
/// Visits all items in the crate in some deterministic (but
713-
/// unspecified) order. If you just need to process every item,
714-
/// but don't care about nesting, this method is the best choice.
715-
///
716-
/// If you do care about nesting -- usually because your algorithm
717-
/// follows lexical scoping rules -- then you want a different
718-
/// approach. You should override `visit_nested_item` in your
719-
/// visitor and then call `intravisit::walk_crate` instead.
720-
pub fn visit_all_item_likes<'hir, V>(&'hir self, visitor: &mut V)
721-
where
722-
V: itemlikevisit::ItemLikeVisitor<'hir>,
723-
{
724-
for owner in self.owners.iter().filter_map(Option::as_ref) {
725-
match owner {
726-
OwnerNode::Item(item) => visitor.visit_item(item),
727-
OwnerNode::ForeignItem(item) => visitor.visit_foreign_item(item),
728-
OwnerNode::ImplItem(item) => visitor.visit_impl_item(item),
729-
OwnerNode::TraitItem(item) => visitor.visit_trait_item(item),
730-
OwnerNode::Crate(_) => {}
731-
}
732-
}
733-
}
734-
735-
/// A parallel version of `visit_all_item_likes`.
736-
pub fn par_visit_all_item_likes<'hir, V>(&'hir self, visitor: &V)
737-
where
738-
V: itemlikevisit::ParItemLikeVisitor<'hir> + Sync + Send,
739-
{
740-
par_for_each_in(&self.owners.raw, |owner| match owner {
741-
Some(OwnerNode::Item(item)) => visitor.visit_item(item),
742-
Some(OwnerNode::ForeignItem(item)) => visitor.visit_foreign_item(item),
743-
Some(OwnerNode::ImplItem(item)) => visitor.visit_impl_item(item),
744-
Some(OwnerNode::TraitItem(item)) => visitor.visit_trait_item(item),
745-
Some(OwnerNode::Crate(_)) | None => {}
746-
})
747-
}
748-
749-
pub fn items<'hir>(&'hir self) -> impl Iterator<Item = &'hir Item<'hir>> + 'hir {
750-
self.owners.iter().filter_map(|owner| match owner {
751-
Some(OwnerNode::Item(item)) => Some(*item),
752-
_ => None,
753-
})
754-
}
755-
}
756-
757710
/// A block of statements `{ .. }`, which may have a label (in this case the
758711
/// `targeted_by_break` field will be `true`) and may be `unsafe` by means of
759712
/// the `rules` being anything but `DefaultBlock`.

compiler/rustc_hir_pretty/src/lib.rs

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -51,19 +51,6 @@ pub struct NoAnn;
5151
impl PpAnn for NoAnn {}
5252
pub const NO_ANN: &dyn PpAnn = &NoAnn;
5353

54-
impl PpAnn for hir::Crate<'_> {
55-
fn nested(&self, state: &mut State<'_>, nested: Nested) {
56-
match nested {
57-
Nested::Item(id) => state.print_item(self.item(id)),
58-
Nested::TraitItem(id) => state.print_trait_item(self.trait_item(id)),
59-
Nested::ImplItem(id) => state.print_impl_item(self.impl_item(id)),
60-
Nested::ForeignItem(id) => state.print_foreign_item(self.foreign_item(id)),
61-
Nested::Body(id) => state.print_expr(&self.body(id).value),
62-
Nested::BodyParamPat(id, i) => state.print_pat(&self.body(id).params[i].pat),
63-
}
64-
}
65-
}
66-
6754
/// Identical to the `PpAnn` implementation for `hir::Crate`,
6855
/// except it avoids creating a dependency on the whole crate.
6956
impl PpAnn for &dyn rustc_hir::intravisit::Map<'_> {

compiler/rustc_incremental/src/assert_dep_graph.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ pub fn assert_dep_graph(tcx: TyCtxt<'_>) {
7474
let mut visitor =
7575
IfThisChanged { tcx, if_this_changed: vec![], then_this_would_need: vec![] };
7676
visitor.process_attrs(hir::CRATE_HIR_ID);
77-
tcx.hir().krate().visit_all_item_likes(&mut visitor.as_deep_visitor());
77+
tcx.hir().visit_all_item_likes(&mut visitor.as_deep_visitor());
7878
(visitor.if_this_changed, visitor.then_this_would_need)
7979
};
8080

compiler/rustc_incremental/src/persist/dirty_clean.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,9 +137,8 @@ pub fn check_dirty_clean_annotations(tcx: TyCtxt<'_>) {
137137
}
138138

139139
tcx.dep_graph.with_ignore(|| {
140-
let krate = tcx.hir().krate();
141140
let mut dirty_clean_visitor = DirtyCleanVisitor { tcx, checked_attrs: Default::default() };
142-
krate.visit_all_item_likes(&mut dirty_clean_visitor);
141+
tcx.hir().visit_all_item_likes(&mut dirty_clean_visitor);
143142

144143
let mut all_attrs = FindAllAttrs { tcx, found_attrs: vec![] };
145144
tcx.hir().walk_attributes(&mut all_attrs);

compiler/rustc_interface/src/passes.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -886,9 +886,7 @@ fn analysis(tcx: TyCtxt<'_>, (): ()) -> Result<()> {
886886
parallel!(
887887
{
888888
sess.time("match_checking", || {
889-
tcx.par_body_owners(|def_id| {
890-
tcx.ensure().check_match(def_id.to_def_id());
891-
});
889+
tcx.hir().par_body_owners(|def_id| tcx.ensure().check_match(def_id.to_def_id()))
892890
});
893891
},
894892
{
@@ -907,11 +905,11 @@ fn analysis(tcx: TyCtxt<'_>, (): ()) -> Result<()> {
907905
});
908906

909907
sess.time("MIR_borrow_checking", || {
910-
tcx.par_body_owners(|def_id| tcx.ensure().mir_borrowck(def_id));
908+
tcx.hir().par_body_owners(|def_id| tcx.ensure().mir_borrowck(def_id));
911909
});
912910

913911
sess.time("MIR_effect_checking", || {
914-
for def_id in tcx.body_owners() {
912+
for def_id in tcx.hir().body_owners() {
915913
tcx.ensure().thir_check_unsafety(def_id);
916914
if !tcx.sess.opts.debugging_opts.thir_unsafeck {
917915
rustc_mir_transform::check_unsafety::check_unsafety(tcx, def_id);

compiler/rustc_interface/src/proc_macro_decls.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use rustc_span::symbol::sym;
77

88
fn proc_macro_decls_static(tcx: TyCtxt<'_>, (): ()) -> Option<LocalDefId> {
99
let mut finder = Finder { tcx, decls: None };
10-
tcx.hir().krate().visit_all_item_likes(&mut finder);
10+
tcx.hir().visit_all_item_likes(&mut finder);
1111

1212
finder.decls.map(|id| tcx.hir().local_def_id(id))
1313
}

compiler/rustc_lint/src/builtin.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -584,8 +584,14 @@ impl<'tcx> LateLintPass<'tcx> for MissingDoc {
584584
self.doc_hidden_stack.pop().expect("empty doc_hidden_stack");
585585
}
586586

587-
fn check_crate(&mut self, cx: &LateContext<'_>, krate: &hir::Crate<'_>) {
588-
self.check_missing_docs_attrs(cx, CRATE_DEF_ID, krate.module().inner, "the", "crate");
587+
fn check_crate(&mut self, cx: &LateContext<'_>) {
588+
self.check_missing_docs_attrs(
589+
cx,
590+
CRATE_DEF_ID,
591+
cx.tcx.def_span(CRATE_DEF_ID),
592+
"the",
593+
"crate",
594+
);
589595
}
590596

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

compiler/rustc_lint/src/late.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -430,8 +430,6 @@ pub fn late_lint_mod<'tcx, T: LateLintPass<'tcx>>(
430430
fn late_lint_pass_crate<'tcx, T: LateLintPass<'tcx>>(tcx: TyCtxt<'tcx>, pass: T) {
431431
let access_levels = &tcx.privacy_access_levels(());
432432

433-
let krate = tcx.hir().krate();
434-
435433
let context = LateContext {
436434
tcx,
437435
enclosing_body: None,
@@ -450,10 +448,10 @@ fn late_lint_pass_crate<'tcx, T: LateLintPass<'tcx>>(tcx: TyCtxt<'tcx>, pass: T)
450448
cx.with_lint_attrs(hir::CRATE_HIR_ID, |cx| {
451449
// since the root module isn't visited as an item (because it isn't an
452450
// item), warn for it here.
453-
lint_callback!(cx, check_crate, krate);
451+
lint_callback!(cx, check_crate,);
454452
tcx.hir().walk_toplevel_module(cx);
455453
tcx.hir().walk_attributes(cx);
456-
lint_callback!(cx, check_crate_post, krate);
454+
lint_callback!(cx, check_crate_post,);
457455
})
458456
}
459457

compiler/rustc_lint/src/passes.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ macro_rules! late_lint_methods {
1616
fn check_body(a: &$hir hir::Body<$hir>);
1717
fn check_body_post(a: &$hir hir::Body<$hir>);
1818
fn check_name(a: Span, b: Symbol);
19-
fn check_crate(a: &$hir hir::Crate<$hir>);
20-
fn check_crate_post(a: &$hir hir::Crate<$hir>);
19+
fn check_crate();
20+
fn check_crate_post();
2121
fn check_mod(a: &$hir hir::Mod<$hir>, b: Span, c: hir::HirId);
2222
fn check_mod_post(a: &$hir hir::Mod<$hir>, b: Span, c: hir::HirId);
2323
fn check_foreign_item(a: &$hir hir::ForeignItem<$hir>);

0 commit comments

Comments
 (0)