Skip to content

Commit 91ddf3e

Browse files
committed
Auto merge of rust-lang#85266 - cjgillot:hir-dep-clean, r=michaelwoerister
Remove obsolete workaround. The regression test for rust-lang#62649 appears to pass even without the workaround.
2 parents 7baa7af + 4f8e34c commit 91ddf3e

File tree

5 files changed

+47
-73
lines changed

5 files changed

+47
-73
lines changed

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

+17-25
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::arena::Arena;
2-
use crate::hir::map::{HirOwnerData, Map};
3-
use crate::hir::{IndexedHir, Owner, OwnerNodes, ParentedNode};
2+
use crate::hir::map::Map;
3+
use crate::hir::{IndexedHir, OwnerNodes, ParentedNode};
44
use crate::ich::StableHashingContext;
55
use rustc_data_structures::fingerprint::Fingerprint;
66
use rustc_data_structures::fx::FxHashMap;
@@ -28,7 +28,7 @@ pub(super) struct NodeCollector<'a, 'hir> {
2828
/// Source map
2929
source_map: &'a SourceMap,
3030

31-
map: IndexVec<LocalDefId, HirOwnerData<'hir>>,
31+
map: IndexVec<LocalDefId, Option<&'hir mut OwnerNodes<'hir>>>,
3232
parenting: FxHashMap<LocalDefId, HirId>,
3333

3434
/// The parent of this node
@@ -107,9 +107,7 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
107107
current_dep_node_owner: LocalDefId { local_def_index: CRATE_DEF_INDEX },
108108
definitions,
109109
hcx,
110-
map: (0..definitions.def_index_count())
111-
.map(|_| HirOwnerData { signature: None, with_bodies: None })
112-
.collect(),
110+
map: IndexVec::from_fn_n(|_| None, definitions.def_index_count()),
113111
parenting: FxHashMap::default(),
114112
};
115113
collector.insert_entry(
@@ -124,7 +122,7 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
124122
pub(super) fn finalize_and_compute_crate_hash(mut self) -> IndexedHir<'hir> {
125123
// Insert bodies into the map
126124
for (id, body) in self.krate.bodies.iter() {
127-
let bodies = &mut self.map[id.hir_id.owner].with_bodies.as_mut().unwrap().bodies;
125+
let bodies = &mut self.map[id.hir_id.owner].as_mut().unwrap().bodies;
128126
assert!(bodies.insert(id.hir_id.local_id, body).is_none());
129127
}
130128
IndexedHir { map: self.map, parenting: self.parenting }
@@ -137,22 +135,13 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
137135

138136
let data = &mut self.map[id.owner];
139137

140-
if data.with_bodies.is_none() {
141-
data.with_bodies = Some(arena.alloc(OwnerNodes {
138+
if i == 0 {
139+
debug_assert!(data.is_none());
140+
*data = Some(arena.alloc(OwnerNodes {
142141
hash,
143142
nodes: IndexVec::new(),
144143
bodies: FxHashMap::default(),
145144
}));
146-
}
147-
148-
let nodes = data.with_bodies.as_mut().unwrap();
149-
150-
if i == 0 {
151-
// Overwrite the dummy hash with the real HIR owner hash.
152-
nodes.hash = hash;
153-
154-
debug_assert!(data.signature.is_none());
155-
data.signature = Some(self.arena.alloc(Owner { node: entry.node }));
156145

157146
let dk_parent = self.definitions.def_key(id.owner).parent;
158147
if let Some(dk_parent) = dk_parent {
@@ -168,13 +157,16 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
168157
debug_assert_eq!(self.parenting.get(&id.owner), Some(&entry.parent));
169158
}
170159
} else {
171-
assert_eq!(entry.parent.owner, id.owner);
172-
insert_vec_map(
173-
&mut nodes.nodes,
174-
id.local_id,
175-
ParentedNode { parent: entry.parent.local_id, node: entry.node },
176-
);
160+
debug_assert_eq!(entry.parent.owner, id.owner);
177161
}
162+
163+
let data = data.as_mut().unwrap();
164+
165+
insert_vec_map(
166+
&mut data.nodes,
167+
id.local_id,
168+
ParentedNode { parent: entry.parent.local_id, node: entry.node },
169+
);
178170
}
179171

180172
fn insert(&mut self, span: Span, hir_id: HirId, node: Node<'hir>) {

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use self::collector::NodeCollector;
22

3-
use crate::hir::{AttributeMap, HirOwnerData, IndexedHir};
3+
use crate::hir::{AttributeMap, IndexedHir};
44
use crate::middle::cstore::CrateStore;
55
use crate::ty::TyCtxt;
66
use rustc_ast as ast;
@@ -953,7 +953,7 @@ pub(super) fn crate_hash(tcx: TyCtxt<'_>, crate_num: CrateNum) -> Svh {
953953
.filter_map(|(def_id, hod)| {
954954
let def_path_hash = tcx.definitions.def_path_hash(def_id);
955955
let mut hasher = StableHasher::new();
956-
hod.with_bodies.as_ref()?.hash_stable(&mut hcx, &mut hasher);
956+
hod.as_ref()?.hash_stable(&mut hcx, &mut hasher);
957957
AttributeMap { map: &tcx.untracked_crate.attrs, prefix: def_id }
958958
.hash_stable(&mut hcx, &mut hasher);
959959
Some((def_path_hash, hasher.finish()))

compiler/rustc_middle/src/hir/mod.rs

+26-11
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,25 @@ use rustc_data_structures::fx::FxHashMap;
1515
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
1616
use rustc_hir::def_id::LocalDefId;
1717
use rustc_hir::*;
18-
use rustc_index::vec::IndexVec;
18+
use rustc_index::vec::{Idx, IndexVec};
1919
use rustc_span::DUMMY_SP;
2020
use std::collections::BTreeMap;
2121

22-
#[derive(Debug)]
23-
struct HirOwnerData<'hir> {
24-
signature: Option<&'hir Owner<'hir>>,
25-
with_bodies: Option<&'hir mut OwnerNodes<'hir>>,
26-
}
27-
22+
/// Result of HIR indexing.
2823
#[derive(Debug)]
2924
pub struct IndexedHir<'hir> {
30-
map: IndexVec<LocalDefId, HirOwnerData<'hir>>,
25+
/// Contents of the HIR owned by each definition. None for definitions that are not HIR owners.
26+
// The `mut` comes from construction time, and is harmless since we only ever hand out
27+
// immutable refs to IndexedHir.
28+
map: IndexVec<LocalDefId, Option<&'hir mut OwnerNodes<'hir>>>,
29+
/// Map from each owner to its parent's HirId inside another owner.
30+
// This map is separate from `map` to eventually allow for per-owner indexing.
3131
parenting: FxHashMap<LocalDefId, HirId>,
3232
}
3333

34-
#[derive(Debug)]
34+
/// Top-level HIR node for current owner. This only contains the node for which
35+
/// `HirId::local_id == 0`, and excludes bodies.
36+
#[derive(Copy, Clone, Debug)]
3537
pub struct Owner<'tcx> {
3638
node: Node<'tcx>,
3739
}
@@ -43,6 +45,9 @@ impl<'a, 'tcx> HashStable<StableHashingContext<'a>> for Owner<'tcx> {
4345
}
4446
}
4547

48+
/// HIR node coupled with its parent's id in the same HIR owner.
49+
///
50+
/// The parent is trash when the node is a HIR owner.
4651
#[derive(Clone, Debug)]
4752
pub struct ParentedNode<'tcx> {
4853
parent: ItemLocalId,
@@ -51,8 +56,12 @@ pub struct ParentedNode<'tcx> {
5156

5257
#[derive(Debug)]
5358
pub struct OwnerNodes<'tcx> {
59+
/// Pre-computed hash of the full HIR.
5460
hash: Fingerprint,
61+
/// Full HIR for the current owner.
62+
// The zeroth node's parent is trash, but is never accessed.
5563
nodes: IndexVec<ItemLocalId, Option<ParentedNode<'tcx>>>,
64+
/// Content of local bodies.
5665
bodies: FxHashMap<ItemLocalId, &'tcx Body<'tcx>>,
5766
}
5867

@@ -65,6 +74,8 @@ impl<'a, 'tcx> HashStable<StableHashingContext<'a>> for OwnerNodes<'tcx> {
6574
}
6675
}
6776

77+
/// Attributes owner by a HIR owner. It is build as a slice inside the attributes map, restricted
78+
/// to the nodes whose `HirId::owner` is `prefix`.
6879
#[derive(Copy, Clone)]
6980
pub struct AttributeMap<'tcx> {
7081
map: &'tcx BTreeMap<HirId, &'tcx [Attribute]>,
@@ -127,8 +138,12 @@ pub fn provide(providers: &mut Providers) {
127138
providers.index_hir = map::index_hir;
128139
providers.crate_hash = map::crate_hash;
129140
providers.hir_module_items = |tcx, id| &tcx.untracked_crate.modules[&id];
130-
providers.hir_owner = |tcx, id| tcx.index_hir(()).map[id].signature;
131-
providers.hir_owner_nodes = |tcx, id| tcx.index_hir(()).map[id].with_bodies.as_deref();
141+
providers.hir_owner = |tcx, id| {
142+
let owner = tcx.index_hir(()).map[id].as_ref()?;
143+
let node = owner.nodes[ItemLocalId::new(0)].as_ref()?.node;
144+
Some(Owner { node })
145+
};
146+
providers.hir_owner_nodes = |tcx, id| tcx.index_hir(()).map[id].as_deref();
132147
providers.hir_owner_parent = |tcx, id| {
133148
let index = tcx.index_hir(());
134149
index.parenting.get(&id).copied().unwrap_or(CRATE_HIR_ID)

compiler/rustc_middle/src/query/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ rustc_queries! {
4747
///
4848
/// This can be conveniently accessed by methods on `tcx.hir()`.
4949
/// Avoid calling this query directly.
50-
query hir_owner(key: LocalDefId) -> Option<&'tcx crate::hir::Owner<'tcx>> {
50+
query hir_owner(key: LocalDefId) -> Option<crate::hir::Owner<'tcx>> {
5151
eval_always
5252
desc { |tcx| "HIR owner of `{}`", tcx.def_path_str(key.to_def_id()) }
5353
}

compiler/rustc_query_impl/src/plumbing.rs

+1-34
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
//! manage the caches, and so forth.
44
55
use super::queries;
6-
use rustc_middle::dep_graph::{DepKind, DepNode, DepNodeExt, DepNodeIndex, SerializedDepNodeIndex};
6+
use rustc_middle::dep_graph::{DepKind, DepNode, DepNodeIndex, SerializedDepNodeIndex};
77
use rustc_middle::ty::query::on_disk_cache;
88
use rustc_middle::ty::tls::{self, ImplicitCtxt};
99
use rustc_middle::ty::{self, TyCtxt};
@@ -57,39 +57,6 @@ impl QueryContext for QueryCtxt<'tcx> {
5757
}
5858

5959
fn try_force_from_dep_node(&self, dep_node: &DepNode) -> bool {
60-
// FIXME: This match is just a workaround for incremental bugs and should
61-
// be removed. https://github.com/rust-lang/rust/issues/62649 is one such
62-
// bug that must be fixed before removing this.
63-
match dep_node.kind {
64-
DepKind::hir_owner | DepKind::hir_owner_nodes => {
65-
if let Some(def_id) = dep_node.extract_def_id(**self) {
66-
let def_id = def_id.expect_local();
67-
let hir_id = self.tcx.hir().local_def_id_to_hir_id(def_id);
68-
if def_id != hir_id.owner {
69-
// This `DefPath` does not have a
70-
// corresponding `DepNode` (e.g. a
71-
// struct field), and the ` DefPath`
72-
// collided with the `DefPath` of a
73-
// proper item that existed in the
74-
// previous compilation session.
75-
//
76-
// Since the given `DefPath` does not
77-
// denote the item that previously
78-
// existed, we just fail to mark green.
79-
return false;
80-
}
81-
} else {
82-
// If the node does not exist anymore, we
83-
// just fail to mark green.
84-
return false;
85-
}
86-
}
87-
_ => {
88-
// For other kinds of nodes it's OK to be
89-
// forced.
90-
}
91-
}
92-
9360
debug!("try_force_from_dep_node({:?}) --- trying to force", dep_node);
9461

9562
// We must avoid ever having to call `force_from_dep_node()` for a

0 commit comments

Comments
 (0)