Skip to content

Commit 9a227ca

Browse files
committed
Only return LocalDefId in hir_crate.
1 parent 598bd4f commit 9a227ca

File tree

6 files changed

+110
-108
lines changed

6 files changed

+110
-108
lines changed

compiler/rustc_ast_lowering/src/query.rs

+70-22
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,87 @@
11
use rustc_data_structures::fingerprint::Fingerprint;
22
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
33
use rustc_data_structures::svh::Svh;
4-
use rustc_hir::def_id::{CrateNum, LocalDefId, StableCrateId, LOCAL_CRATE};
4+
use rustc_hir::def_id::{CrateNum, LocalDefId, StableCrateId, CRATE_DEF_ID, LOCAL_CRATE};
55
use rustc_hir::intravisit::{self, Visitor};
6-
use rustc_hir::{ForeignItem, ImplItem, Item, ItemKind, Mod, TraitItem};
6+
use rustc_hir::{Crate, ForeignItem, ImplItem, Item, ItemKind, Mod, TraitItem};
77
use rustc_hir::{ForeignItemId, HirId, ImplItemId, ItemId, ModuleItems, TraitItemId};
88
use rustc_middle::hir::nested_filter;
99
use rustc_middle::ty::query::Providers;
1010
use rustc_middle::ty::TyCtxt;
11-
use rustc_span::Span;
11+
use rustc_span::{Span, DUMMY_SP};
1212

1313
pub fn provide(providers: &mut Providers) {
14-
*providers = Providers { crate_hash, hir_module_items, hir_crate_items, ..*providers };
14+
*providers =
15+
Providers { hir_crate, crate_hash, hir_module_items, hir_crate_items, ..*providers };
16+
}
17+
18+
fn hir_crate<'tcx>(tcx: TyCtxt<'tcx>, (): ()) -> Crate<'tcx> {
19+
let mut collector = CrateCollector { tcx, owners: vec![CRATE_DEF_ID] };
20+
tcx.hir().walk_toplevel_module(&mut collector);
21+
let owners = tcx.arena.alloc_from_iter(collector.owners);
22+
23+
// Discard hygiene data, which isn't required after lowering to HIR.
24+
if !tcx.sess.opts.debugging_opts.keep_hygiene_data {
25+
rustc_span::hygiene::clear_syntax_context_map();
26+
}
27+
28+
return Crate { owners };
29+
30+
struct CrateCollector<'tcx> {
31+
tcx: TyCtxt<'tcx>,
32+
owners: Vec<LocalDefId>,
33+
}
34+
35+
impl<'hir> Visitor<'hir> for CrateCollector<'hir> {
36+
type NestedFilter = nested_filter::All;
37+
38+
fn nested_visit_map(&mut self) -> Self::Map {
39+
self.tcx.hir()
40+
}
41+
42+
fn visit_item(&mut self, item: &'hir Item<'hir>) {
43+
self.owners.push(item.def_id);
44+
intravisit::walk_item(self, item)
45+
}
46+
47+
fn visit_trait_item(&mut self, item: &'hir TraitItem<'hir>) {
48+
self.owners.push(item.def_id);
49+
intravisit::walk_trait_item(self, item)
50+
}
51+
52+
fn visit_impl_item(&mut self, item: &'hir ImplItem<'hir>) {
53+
self.owners.push(item.def_id);
54+
intravisit::walk_impl_item(self, item)
55+
}
56+
57+
fn visit_foreign_item(&mut self, item: &'hir ForeignItem<'hir>) {
58+
self.owners.push(item.def_id);
59+
intravisit::walk_foreign_item(self, item)
60+
}
61+
}
1562
}
1663

1764
fn crate_hash(tcx: TyCtxt<'_>, crate_num: CrateNum) -> Svh {
1865
debug_assert_eq!(crate_num, LOCAL_CRATE);
1966
let krate = tcx.hir_crate(());
20-
let hir_body_hash = krate.hir_hash;
67+
let definitions = tcx.definitions_untracked();
68+
69+
let mut hir_body_nodes: Vec<_> = krate
70+
.owners
71+
.iter()
72+
.map(|&def_id| {
73+
let def_path_hash = tcx.hir().def_path_hash(def_id);
74+
let info = tcx.lower_to_hir(def_id).unwrap();
75+
let span = if tcx.sess.opts.debugging_opts.incremental_relative_spans {
76+
definitions.def_span(def_id)
77+
} else {
78+
DUMMY_SP
79+
};
80+
debug_assert_eq!(span.parent(), None);
81+
(def_path_hash, info, span)
82+
})
83+
.collect();
84+
hir_body_nodes.sort_unstable_by_key(|bn| bn.0);
2185

2286
let upstream_crates = upstream_crates(tcx);
2387

@@ -39,25 +103,9 @@ fn crate_hash(tcx: TyCtxt<'_>, crate_num: CrateNum) -> Svh {
39103

40104
let crate_hash: Fingerprint = tcx.with_stable_hashing_context(|mut hcx| {
41105
let mut stable_hasher = StableHasher::new();
42-
hir_body_hash.hash_stable(&mut hcx, &mut stable_hasher);
106+
hir_body_nodes.hash_stable(&mut hcx, &mut stable_hasher);
43107
upstream_crates.hash_stable(&mut hcx, &mut stable_hasher);
44108
source_file_names.hash_stable(&mut hcx, &mut stable_hasher);
45-
if tcx.sess.opts.debugging_opts.incremental_relative_spans {
46-
let definitions = tcx.definitions_untracked();
47-
let mut owner_spans: Vec<_> = krate
48-
.owners
49-
.iter_enumerated()
50-
.filter_map(|(def_id, info)| {
51-
let _ = info.as_owner()?;
52-
let def_path_hash = definitions.def_path_hash(def_id);
53-
let span = definitions.def_span(def_id);
54-
debug_assert_eq!(span.parent(), None);
55-
Some((def_path_hash, span))
56-
})
57-
.collect();
58-
owner_spans.sort_unstable_by_key(|bn| bn.0);
59-
owner_spans.hash_stable(&mut hcx, &mut stable_hasher);
60-
}
61109
tcx.sess.opts.dep_tracking_hash(true).hash_stable(&mut hcx, &mut stable_hasher);
62110
tcx.sess.local_stable_crate_id().hash_stable(&mut hcx, &mut stable_hasher);
63111
// Hash visibility information since it does not appear in HIR.

compiler/rustc_driver/src/pretty.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,12 @@ pub fn print_after_hir_lowering<'tcx>(
461461
HirTree => {
462462
call_with_pp_support_hir(&PpHirMode::Normal, tcx, move |_annotation, hir_map| {
463463
debug!("pretty printing HIR tree");
464-
format!("{:#?}", hir_map.krate())
464+
hir_map
465+
.krate()
466+
.owners
467+
.iter()
468+
.map(|&owner| format!("{:#?} => {:#?}\n", owner, tcx.lower_to_hir(owner)))
469+
.collect()
465470
})
466471
}
467472

compiler/rustc_hir/src/hir.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -831,10 +831,10 @@ impl ModuleItems {
831831
/// For more details, see the [rustc dev guide].
832832
///
833833
/// [rustc dev guide]: https://rustc-dev-guide.rust-lang.org/hir.html
834-
#[derive(Debug)]
834+
#[derive(Debug, HashStable_Generic)]
835835
pub struct Crate<'hir> {
836-
pub owners: IndexVec<LocalDefId, MaybeOwner<&'hir OwnerInfo<'hir>>>,
837-
pub hir_hash: Fingerprint,
836+
/// List of all the definitions in visitor order.
837+
pub owners: &'hir [LocalDefId],
838838
}
839839

840840
/// A block of statements `{ .. }`, which may have a label (in this case the

compiler/rustc_hir/src/stable_hash_impls.rs

+1-9
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
use rustc_data_structures::stable_hasher::{HashStable, StableHasher, ToStableHashKey};
22

33
use crate::hir::{
4-
AttributeMap, BodyId, Crate, Expr, ForeignItemId, ImplItemId, ItemId, OwnerNodes, TraitItemId,
5-
Ty,
4+
AttributeMap, BodyId, Expr, ForeignItemId, ImplItemId, ItemId, OwnerNodes, TraitItemId, Ty,
65
};
76
use crate::hir_id::{HirId, ItemLocalId};
87
use rustc_span::def_id::DefPathHash;
@@ -134,10 +133,3 @@ impl<'tcx, HirCtx: crate::HashStableContext> HashStable<HirCtx> for AttributeMap
134133
hash.hash_stable(hcx, hasher);
135134
}
136135
}
137-
138-
impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for Crate<'_> {
139-
fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
140-
let Crate { owners: _, hir_hash } = self;
141-
hir_hash.hash_stable(hcx, hasher)
142-
}
143-
}

compiler/rustc_interface/src/passes.rs

+1-38
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,12 @@ use rustc_borrowck as mir_borrowck;
88
use rustc_codegen_ssa::back::link::emit_metadata;
99
use rustc_codegen_ssa::traits::CodegenBackend;
1010
use rustc_data_structures::parallel;
11-
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
1211
use rustc_data_structures::sync::{Lrc, OnceCell, WorkerLocal};
1312
use rustc_data_structures::temp_dir::MaybeTempDir;
1413
use rustc_errors::{Applicability, ErrorGuaranteed, MultiSpan, PResult};
1514
use rustc_expand::base::{ExtCtxt, LintStoreExpand, ResolverExpand};
16-
use rustc_hir::def_id::{LocalDefId, StableCrateId, LOCAL_CRATE};
15+
use rustc_hir::def_id::{StableCrateId, LOCAL_CRATE};
1716
use rustc_hir::definitions::Definitions;
18-
use rustc_hir::Crate;
19-
use rustc_index::vec::{Idx, IndexVec};
2017
use rustc_lint::{EarlyCheckNode, LintStore};
2118
use rustc_metadata::creader::CStore;
2219
use rustc_metadata::{encode_metadata, EncodedMetadata};
@@ -492,39 +489,6 @@ pub fn configure_and_expand(
492489
Ok(krate)
493490
}
494491

495-
fn hir_crate<'tcx>(tcx: TyCtxt<'tcx>, (): ()) -> Crate<'tcx> {
496-
let mut owners: IndexVec<LocalDefId, _> = IndexVec::new();
497-
while owners.next_index().index() < tcx.definitions_untracked().def_index_count() {
498-
let next = owners.next_index();
499-
owners.push(tcx.lower_to_hir(next));
500-
}
501-
502-
// Discard hygiene data, which isn't required after lowering to HIR.
503-
if !tcx.sess.opts.debugging_opts.keep_hygiene_data {
504-
rustc_span::hygiene::clear_syntax_context_map();
505-
}
506-
507-
let mut hir_body_nodes: Vec<_> = owners
508-
.iter_enumerated()
509-
.filter_map(|(def_id, info)| {
510-
let info = info.as_owner()?;
511-
let def_path_hash = tcx.hir().def_path_hash(def_id);
512-
Some((def_path_hash, info))
513-
})
514-
.collect();
515-
hir_body_nodes.sort_unstable_by_key(|bn| bn.0);
516-
517-
let hir_hash = tcx.with_stable_hashing_context(|mut hcx| {
518-
let mut stable_hasher = StableHasher::new();
519-
hir_body_nodes.hash_stable(&mut hcx, &mut stable_hasher);
520-
stable_hasher.finish()
521-
});
522-
523-
let hir_crate = Crate { owners, hir_hash };
524-
525-
hir_crate
526-
}
527-
528492
// Returns all the paths that correspond to generated files.
529493
fn generated_output_paths(
530494
sess: &Session,
@@ -789,7 +753,6 @@ pub fn prepare_outputs(
789753
pub static DEFAULT_QUERY_PROVIDERS: SyncLazy<Providers> = SyncLazy::new(|| {
790754
let providers = &mut Providers::default();
791755
providers.analysis = analysis;
792-
providers.hir_crate = hir_crate;
793756
providers.lower_to_hir =
794757
|tcx, def_id| rustc_ast_lowering::lower_to_hir(tcx, def_id, rustc_parse::nt_to_tokenstream);
795758
rustc_ast_lowering::provide(providers);

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

+29-35
Original file line numberDiff line numberDiff line change
@@ -499,34 +499,28 @@ impl<'hir> Map<'hir> {
499499
/// crate. If you would prefer to iterate over the bodies
500500
/// themselves, you can do `self.hir().krate().body_ids.iter()`.
501501
pub fn body_owners(self) -> impl Iterator<Item = LocalDefId> + 'hir {
502-
self.krate()
503-
.owners
504-
.iter_enumerated()
505-
.flat_map(move |(owner, owner_info)| {
506-
let bodies = &owner_info.as_owner()?.nodes.bodies;
507-
Some(bodies.iter().map(move |&(local_id, _)| {
508-
let hir_id = HirId { owner, local_id };
509-
let body_id = BodyId { hir_id };
510-
self.body_owner_def_id(body_id)
511-
}))
502+
self.krate().owners.iter().flat_map(move |&owner| {
503+
let bodies = &self.tcx.hir_owner_nodes(owner).unwrap().bodies;
504+
bodies.iter().filter_map(move |(local_id, _)| {
505+
let hir_id = HirId { owner, local_id: *local_id };
506+
let body_id = BodyId { hir_id };
507+
Some(self.body_owner_def_id(body_id))
512508
})
513-
.flatten()
509+
})
514510
}
515511

516512
pub fn par_body_owners<F: Fn(LocalDefId) + Sync + Send>(self, f: F) {
517513
use rustc_data_structures::sync::{par_iter, ParallelIterator};
518514
#[cfg(parallel_compiler)]
519515
use rustc_rayon::iter::IndexedParallelIterator;
520516

521-
par_iter(&self.krate().owners.raw).enumerate().for_each(|(owner, owner_info)| {
522-
let owner = LocalDefId::new(owner);
523-
if let MaybeOwner::Owner(owner_info) = owner_info {
524-
par_iter(owner_info.nodes.bodies.range(..)).for_each(|(local_id, _)| {
525-
let hir_id = HirId { owner, local_id: *local_id };
526-
let body_id = BodyId { hir_id };
527-
f(self.body_owner_def_id(body_id))
528-
})
529-
}
517+
par_iter(&*self.krate().owners).for_each(|&owner| {
518+
let owner_info = self.tcx.lower_to_hir(owner).unwrap();
519+
par_iter(owner_info.nodes.bodies.range(..)).for_each(|(local_id, _)| {
520+
let hir_id = HirId { owner, local_id: *local_id };
521+
let body_id = BodyId { hir_id };
522+
f(self.body_owner_def_id(body_id))
523+
})
530524
});
531525
}
532526

@@ -583,13 +577,12 @@ impl<'hir> Map<'hir> {
583577
/// Walks the attributes in a crate.
584578
pub fn walk_attributes(self, visitor: &mut impl Visitor<'hir>) {
585579
let krate = self.krate();
586-
for (owner, info) in krate.owners.iter_enumerated() {
587-
if let MaybeOwner::Owner(info) = info {
588-
for (local_id, attrs) in info.attrs.map.iter() {
589-
let id = HirId { owner, local_id: *local_id };
590-
for a in *attrs {
591-
visitor.visit_attribute(id, a)
592-
}
580+
for &owner in krate.owners.iter() {
581+
let info = self.tcx.lower_to_hir(owner).unwrap();
582+
for (local_id, attrs) in info.attrs.map.iter() {
583+
let id = HirId { owner, local_id: *local_id };
584+
for a in *attrs {
585+
visitor.visit_attribute(id, a)
593586
}
594587
}
595588
}
@@ -608,8 +601,9 @@ impl<'hir> Map<'hir> {
608601
V: itemlikevisit::ItemLikeVisitor<'hir>,
609602
{
610603
let krate = self.krate();
611-
for owner in krate.owners.iter().filter_map(|i| i.as_owner()) {
612-
match owner.node() {
604+
for &owner in krate.owners.iter() {
605+
let info = self.tcx.hir_owner(owner).unwrap();
606+
match info.node {
613607
OwnerNode::Item(item) => visitor.visit_item(item),
614608
OwnerNode::ForeignItem(item) => visitor.visit_foreign_item(item),
615609
OwnerNode::ImplItem(item) => visitor.visit_impl_item(item),
@@ -625,12 +619,12 @@ impl<'hir> Map<'hir> {
625619
V: itemlikevisit::ParItemLikeVisitor<'hir> + Sync + Send,
626620
{
627621
let krate = self.krate();
628-
par_for_each_in(&krate.owners.raw, |owner| match owner.map(OwnerInfo::node) {
629-
MaybeOwner::Owner(OwnerNode::Item(item)) => visitor.visit_item(item),
630-
MaybeOwner::Owner(OwnerNode::ForeignItem(item)) => visitor.visit_foreign_item(item),
631-
MaybeOwner::Owner(OwnerNode::ImplItem(item)) => visitor.visit_impl_item(item),
632-
MaybeOwner::Owner(OwnerNode::TraitItem(item)) => visitor.visit_trait_item(item),
633-
MaybeOwner::Owner(OwnerNode::Crate(_)) | MaybeOwner::NonOwner(_) => {}
622+
par_for_each_in(&*krate.owners, |&owner| match self.tcx.hir_owner(owner).unwrap().node {
623+
OwnerNode::Item(item) => visitor.visit_item(item),
624+
OwnerNode::ForeignItem(item) => visitor.visit_foreign_item(item),
625+
OwnerNode::ImplItem(item) => visitor.visit_impl_item(item),
626+
OwnerNode::TraitItem(item) => visitor.visit_trait_item(item),
627+
OwnerNode::Crate(_) => {}
634628
})
635629
}
636630

0 commit comments

Comments
 (0)