Skip to content

Commit a61003d

Browse files
committed
Auto merge of #152636 - nnethercote:big-cleanups, r=Zalathar
Big query system cleanups Recent PRs have moved a lot of code from `rustc_query_system` to `rustc_middle` and `rustc_query_impl`, where this code now has access to `TyCtxt`, e.g. #152419, #152516. As a result, a lot of abstraction and indirection that existed to work around this limitation is no longer necessary. This PR removes a lot of it. r? @Zalathar
2 parents 1396514 + fcea886 commit a61003d

19 files changed

Lines changed: 312 additions & 487 deletions

File tree

Cargo.lock

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4500,7 +4500,6 @@ dependencies = [
45004500
"rustc_index",
45014501
"rustc_macros",
45024502
"rustc_middle",
4503-
"rustc_query_system",
45044503
"rustc_serialize",
45054504
"rustc_session",
45064505
"rustc_span",

compiler/rustc_incremental/src/assert_dep_graph.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ use rustc_hir::def_id::{CRATE_DEF_ID, DefId, LocalDefId};
4545
use rustc_hir::intravisit::{self, Visitor};
4646
use rustc_middle::bug;
4747
use rustc_middle::dep_graph::{
48-
DepGraphQuery, DepKind, DepNode, DepNodeExt, DepNodeFilter, EdgeFilter, dep_kinds,
48+
DepGraphQuery, DepKind, DepNode, DepNodeFilter, EdgeFilter, dep_kinds,
4949
};
5050
use rustc_middle::hir::nested_filter;
5151
use rustc_middle::ty::TyCtxt;

compiler/rustc_incremental/src/persist/clean.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ use rustc_hir::{
2727
Attribute, ImplItemKind, ItemKind as HirItem, Node as HirNode, TraitItemKind, find_attr,
2828
intravisit,
2929
};
30-
use rustc_middle::dep_graph::{DepNode, DepNodeExt, dep_kind_from_label, label_strs};
30+
use rustc_middle::dep_graph::{DepNode, dep_kind_from_label, label_strs};
3131
use rustc_middle::hir::nested_filter;
3232
use rustc_middle::ty::TyCtxt;
3333
use rustc_span::{Span, Symbol};

compiler/rustc_incremental/src/persist/load.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use std::sync::Arc;
66
use rustc_data_structures::memmap::Mmap;
77
use rustc_data_structures::unord::UnordMap;
88
use rustc_hashes::Hash64;
9-
use rustc_middle::dep_graph::{DepGraph, DepsType, SerializedDepGraph, WorkProductMap};
9+
use rustc_middle::dep_graph::{DepGraph, SerializedDepGraph, WorkProductMap};
1010
use rustc_middle::query::on_disk_cache::OnDiskCache;
1111
use rustc_serialize::Decodable;
1212
use rustc_serialize::opaque::MemDecoder;
@@ -171,7 +171,7 @@ fn load_dep_graph(sess: &Session) -> LoadResult<(Arc<SerializedDepGraph>, WorkPr
171171
return LoadResult::DataOutOfDate;
172172
}
173173

174-
let dep_graph = SerializedDepGraph::decode::<DepsType>(&mut decoder);
174+
let dep_graph = SerializedDepGraph::decode(&mut decoder);
175175

176176
LoadResult::Ok { data: (dep_graph, prev_work_products) }
177177
}

compiler/rustc_interface/src/callbacks.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,8 @@ use std::fmt;
1313

1414
use rustc_errors::{DiagInner, TRACK_DIAGNOSTIC};
1515
use rustc_middle::dep_graph::dep_node::default_dep_kind_debug;
16-
use rustc_middle::dep_graph::{DepContext, DepKind, DepNode, DepNodeExt, TaskDepsRef};
16+
use rustc_middle::dep_graph::{DepKind, DepNode, TaskDepsRef};
1717
use rustc_middle::ty::tls;
18-
use rustc_query_impl::QueryCtxt;
1918

2019
fn track_span_parent(def_id: rustc_span::def_id::LocalDefId) {
2120
tls::with_context_opt(|icx| {
@@ -41,7 +40,7 @@ fn track_span_parent(def_id: rustc_span::def_id::LocalDefId) {
4140
fn track_diagnostic<R>(diagnostic: DiagInner, f: &mut dyn FnMut(DiagInner) -> R) -> R {
4241
tls::with_context_opt(|icx| {
4342
if let Some(icx) = icx {
44-
icx.tcx.dep_graph.record_diagnostic(QueryCtxt::new(icx.tcx), &diagnostic);
43+
icx.tcx.dep_graph.record_diagnostic(icx.tcx, &diagnostic);
4544

4645
// Diagnostics are tracked, we can ignore the dependency.
4746
let icx = tls::ImplicitCtxt { task_deps: TaskDepsRef::Ignore, ..icx.clone() };

compiler/rustc_middle/src/dep_graph/dep_node.rs

Lines changed: 49 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ use rustc_macros::{Decodable, Encodable};
6767
use rustc_query_system::ich::StableHashingContext;
6868
use rustc_span::Symbol;
6969

70-
use super::{DepContext, FingerprintStyle, SerializedDepNodeIndex};
70+
use super::{FingerprintStyle, SerializedDepNodeIndex};
7171
use crate::mir::mono::MonoItem;
7272
use crate::ty::TyCtxt;
7373

@@ -92,6 +92,26 @@ impl DepKind {
9292
pub const fn as_usize(&self) -> usize {
9393
self.variant as usize
9494
}
95+
96+
pub(crate) fn name(self) -> &'static str {
97+
DEP_KIND_NAMES[self.as_usize()]
98+
}
99+
100+
/// We use this for most things when incr. comp. is turned off.
101+
pub(crate) const NULL: DepKind = dep_kinds::Null;
102+
103+
/// We use this to create a forever-red node.
104+
pub(crate) const RED: DepKind = dep_kinds::Red;
105+
106+
/// We use this to create a side effect node.
107+
pub(crate) const SIDE_EFFECT: DepKind = dep_kinds::SideEffect;
108+
109+
/// We use this to create the anon node with zero dependencies.
110+
pub(crate) const ANON_ZERO_DEPS: DepKind = dep_kinds::AnonZeroDeps;
111+
112+
/// This is the highest value a `DepKind` can have. It's used during encoding to
113+
/// pack information into the unused bits.
114+
pub(crate) const MAX: u16 = DEP_KIND_VARIANTS - 1;
95115
}
96116

97117
pub fn default_dep_kind_debug(kind: DepKind, f: &mut fmt::Formatter<'_>) -> fmt::Result {
@@ -117,29 +137,25 @@ impl DepNode {
117137
/// Creates a new, parameterless DepNode. This method will assert
118138
/// that the DepNode corresponding to the given DepKind actually
119139
/// does not require any parameters.
120-
pub fn new_no_params<Tcx>(tcx: Tcx, kind: DepKind) -> DepNode
121-
where
122-
Tcx: super::DepContext,
123-
{
140+
pub fn new_no_params<'tcx>(tcx: TyCtxt<'tcx>, kind: DepKind) -> DepNode {
124141
debug_assert_eq!(tcx.fingerprint_style(kind), FingerprintStyle::Unit);
125142
DepNode { kind, hash: Fingerprint::ZERO.into() }
126143
}
127144

128-
pub fn construct<Tcx, Key>(tcx: Tcx, kind: DepKind, arg: &Key) -> DepNode
145+
pub fn construct<'tcx, Key>(tcx: TyCtxt<'tcx>, kind: DepKind, arg: &Key) -> DepNode
129146
where
130-
Tcx: super::DepContext,
131-
Key: DepNodeKey<Tcx>,
147+
Key: DepNodeKey<'tcx>,
132148
{
133149
let hash = arg.to_fingerprint(tcx);
134150
let dep_node = DepNode { kind, hash: hash.into() };
135151

136152
#[cfg(debug_assertions)]
137153
{
138154
if !tcx.fingerprint_style(kind).reconstructible()
139-
&& (tcx.sess().opts.unstable_opts.incremental_info
140-
|| tcx.sess().opts.unstable_opts.query_dep_graph)
155+
&& (tcx.sess.opts.unstable_opts.incremental_info
156+
|| tcx.sess.opts.unstable_opts.query_dep_graph)
141157
{
142-
tcx.dep_graph().register_dep_node_debug_str(dep_node, || arg.to_debug_str(tcx));
158+
tcx.dep_graph.register_dep_node_debug_str(dep_node, || arg.to_debug_str(tcx));
143159
}
144160
}
145161

@@ -149,10 +165,11 @@ impl DepNode {
149165
/// Construct a DepNode from the given DepKind and DefPathHash. This
150166
/// method will assert that the given DepKind actually requires a
151167
/// single DefId/DefPathHash parameter.
152-
pub fn from_def_path_hash<Tcx>(tcx: Tcx, def_path_hash: DefPathHash, kind: DepKind) -> Self
153-
where
154-
Tcx: super::DepContext,
155-
{
168+
pub fn from_def_path_hash<'tcx>(
169+
tcx: TyCtxt<'tcx>,
170+
def_path_hash: DefPathHash,
171+
kind: DepKind,
172+
) -> Self {
156173
debug_assert!(tcx.fingerprint_style(kind) == FingerprintStyle::DefPathHash);
157174
DepNode { kind, hash: def_path_hash.0.into() }
158175
}
@@ -172,26 +189,26 @@ impl fmt::Debug for DepNode {
172189
}
173190

174191
/// Trait for query keys as seen by dependency-node tracking.
175-
pub trait DepNodeKey<Tcx: DepContext>: fmt::Debug + Sized {
192+
pub trait DepNodeKey<'tcx>: fmt::Debug + Sized {
176193
fn fingerprint_style() -> FingerprintStyle;
177194

178195
/// This method turns a query key into an opaque `Fingerprint` to be used
179196
/// in `DepNode`.
180-
fn to_fingerprint(&self, _: Tcx) -> Fingerprint;
197+
fn to_fingerprint(&self, tcx: TyCtxt<'tcx>) -> Fingerprint;
181198

182-
fn to_debug_str(&self, tcx: Tcx) -> String;
199+
fn to_debug_str(&self, tcx: TyCtxt<'tcx>) -> String;
183200

184201
/// This method tries to recover the query key from the given `DepNode`,
185202
/// something which is needed when forcing `DepNode`s during red-green
186203
/// evaluation. The query system will only call this method if
187204
/// `fingerprint_style()` is not `FingerprintStyle::Opaque`.
188205
/// It is always valid to return `None` here, in which case incremental
189206
/// compilation will treat the query as having changed instead of forcing it.
190-
fn recover(tcx: Tcx, dep_node: &DepNode) -> Option<Self>;
207+
fn recover(tcx: TyCtxt<'tcx>, dep_node: &DepNode) -> Option<Self>;
191208
}
192209

193210
// Blanket impl of `DepNodeKey`, which is specialized by other impls elsewhere.
194-
impl<Tcx: DepContext, T> DepNodeKey<Tcx> for T
211+
impl<'tcx, T> DepNodeKey<'tcx> for T
195212
where
196213
T: for<'a> HashStable<StableHashingContext<'a>> + fmt::Debug,
197214
{
@@ -201,7 +218,7 @@ where
201218
}
202219

203220
#[inline(always)]
204-
default fn to_fingerprint(&self, tcx: Tcx) -> Fingerprint {
221+
default fn to_fingerprint(&self, tcx: TyCtxt<'tcx>) -> Fingerprint {
205222
tcx.with_stable_hashing_context(|mut hcx| {
206223
let mut hasher = StableHasher::new();
207224
self.hash_stable(&mut hcx, &mut hasher);
@@ -210,15 +227,15 @@ where
210227
}
211228

212229
#[inline(always)]
213-
default fn to_debug_str(&self, tcx: Tcx) -> String {
230+
default fn to_debug_str(&self, tcx: TyCtxt<'tcx>) -> String {
214231
// Make sure to print dep node params with reduced queries since printing
215232
// may themselves call queries, which may lead to (possibly untracked!)
216233
// query cycles.
217234
tcx.with_reduced_queries(|| format!("{self:?}"))
218235
}
219236

220237
#[inline(always)]
221-
default fn recover(_: Tcx, _: &DepNode) -> Option<Self> {
238+
default fn recover(_: TyCtxt<'tcx>, _: &DepNode) -> Option<Self> {
222239
None
223240
}
224241
}
@@ -228,7 +245,7 @@ where
228245
/// Information is retrieved by indexing the `DEP_KINDS` array using the integer value
229246
/// of the `DepKind`. Overall, this allows to implement `DepContext` using this manual
230247
/// jump table instead of large matches.
231-
pub struct DepKindVTable<Tcx: DepContext> {
248+
pub struct DepKindVTable<'tcx> {
232249
/// Anonymous queries cannot be replayed from one compiler invocation to the next.
233250
/// When their result is needed, it is recomputed. They are useful for fine-grained
234251
/// dependency tracking, and caching within one compiler invocation.
@@ -279,11 +296,12 @@ pub struct DepKindVTable<Tcx: DepContext> {
279296
/// with kind `MirValidated`, we know that the GUID/fingerprint of the `DepNode`
280297
/// is actually a `DefPathHash`, and can therefore just look up the corresponding
281298
/// `DefId` in `tcx.def_path_hash_to_def_id`.
282-
pub force_from_dep_node:
283-
Option<fn(tcx: Tcx, dep_node: DepNode, prev_index: SerializedDepNodeIndex) -> bool>,
299+
pub force_from_dep_node: Option<
300+
fn(tcx: TyCtxt<'tcx>, dep_node: DepNode, prev_index: SerializedDepNodeIndex) -> bool,
301+
>,
284302

285303
/// Invoke a query to put the on-disk cached value in memory.
286-
pub try_load_from_on_disk_cache: Option<fn(Tcx, DepNode)>,
304+
pub try_load_from_on_disk_cache: Option<fn(TyCtxt<'tcx>, DepNode)>,
287305

288306
/// The name of this dep kind.
289307
pub name: &'static &'static str,
@@ -434,19 +452,7 @@ pub(crate) fn make_metadata(tcx: TyCtxt<'_>) -> DepNode {
434452
DepNode::construct(tcx, dep_kinds::Metadata, &())
435453
}
436454

437-
pub trait DepNodeExt: Sized {
438-
fn extract_def_id(&self, tcx: TyCtxt<'_>) -> Option<DefId>;
439-
440-
fn from_label_string(
441-
tcx: TyCtxt<'_>,
442-
label: &str,
443-
def_path_hash: DefPathHash,
444-
) -> Result<Self, ()>;
445-
446-
fn has_label_string(label: &str) -> bool;
447-
}
448-
449-
impl DepNodeExt for DepNode {
455+
impl DepNode {
450456
/// Extracts the DefId corresponding to this DepNode. This will work
451457
/// if two conditions are met:
452458
///
@@ -457,16 +463,15 @@ impl DepNodeExt for DepNode {
457463
/// DepNode. Condition (2) might not be fulfilled if a DepNode
458464
/// refers to something from the previous compilation session that
459465
/// has been removed.
460-
fn extract_def_id(&self, tcx: TyCtxt<'_>) -> Option<DefId> {
466+
pub fn extract_def_id(&self, tcx: TyCtxt<'_>) -> Option<DefId> {
461467
if tcx.fingerprint_style(self.kind) == FingerprintStyle::DefPathHash {
462468
tcx.def_path_hash_to_def_id(DefPathHash(self.hash.into()))
463469
} else {
464470
None
465471
}
466472
}
467473

468-
/// Used in testing
469-
fn from_label_string(
474+
pub fn from_label_string(
470475
tcx: TyCtxt<'_>,
471476
label: &str,
472477
def_path_hash: DefPathHash,
@@ -482,8 +487,7 @@ impl DepNodeExt for DepNode {
482487
}
483488
}
484489

485-
/// Used in testing
486-
fn has_label_string(label: &str) -> bool {
490+
pub fn has_label_string(label: &str) -> bool {
487491
dep_kind_from_label_string(label).is_ok()
488492
}
489493
}

compiler/rustc_middle/src/dep_graph/dep_node_key.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ use rustc_hir::def_id::{CrateNum, DefId, LOCAL_CRATE, LocalDefId, LocalModDefId,
33
use rustc_hir::definitions::DefPathHash;
44
use rustc_hir::{HirId, ItemLocalId, OwnerId};
55

6-
use crate::dep_graph::{DepContext, DepNode, DepNodeExt, DepNodeKey, FingerprintStyle};
6+
use crate::dep_graph::{DepNode, DepNodeKey, FingerprintStyle};
77
use crate::ty::TyCtxt;
88

9-
impl<'tcx> DepNodeKey<TyCtxt<'tcx>> for () {
9+
impl<'tcx> DepNodeKey<'tcx> for () {
1010
#[inline(always)]
1111
fn fingerprint_style() -> FingerprintStyle {
1212
FingerprintStyle::Unit
@@ -23,7 +23,7 @@ impl<'tcx> DepNodeKey<TyCtxt<'tcx>> for () {
2323
}
2424
}
2525

26-
impl<'tcx> DepNodeKey<TyCtxt<'tcx>> for DefId {
26+
impl<'tcx> DepNodeKey<'tcx> for DefId {
2727
#[inline(always)]
2828
fn fingerprint_style() -> FingerprintStyle {
2929
FingerprintStyle::DefPathHash
@@ -45,7 +45,7 @@ impl<'tcx> DepNodeKey<TyCtxt<'tcx>> for DefId {
4545
}
4646
}
4747

48-
impl<'tcx> DepNodeKey<TyCtxt<'tcx>> for LocalDefId {
48+
impl<'tcx> DepNodeKey<'tcx> for LocalDefId {
4949
#[inline(always)]
5050
fn fingerprint_style() -> FingerprintStyle {
5151
FingerprintStyle::DefPathHash
@@ -67,7 +67,7 @@ impl<'tcx> DepNodeKey<TyCtxt<'tcx>> for LocalDefId {
6767
}
6868
}
6969

70-
impl<'tcx> DepNodeKey<TyCtxt<'tcx>> for OwnerId {
70+
impl<'tcx> DepNodeKey<'tcx> for OwnerId {
7171
#[inline(always)]
7272
fn fingerprint_style() -> FingerprintStyle {
7373
FingerprintStyle::DefPathHash
@@ -89,7 +89,7 @@ impl<'tcx> DepNodeKey<TyCtxt<'tcx>> for OwnerId {
8989
}
9090
}
9191

92-
impl<'tcx> DepNodeKey<TyCtxt<'tcx>> for CrateNum {
92+
impl<'tcx> DepNodeKey<'tcx> for CrateNum {
9393
#[inline(always)]
9494
fn fingerprint_style() -> FingerprintStyle {
9595
FingerprintStyle::DefPathHash
@@ -112,7 +112,7 @@ impl<'tcx> DepNodeKey<TyCtxt<'tcx>> for CrateNum {
112112
}
113113
}
114114

115-
impl<'tcx> DepNodeKey<TyCtxt<'tcx>> for (DefId, DefId) {
115+
impl<'tcx> DepNodeKey<'tcx> for (DefId, DefId) {
116116
#[inline(always)]
117117
fn fingerprint_style() -> FingerprintStyle {
118118
FingerprintStyle::Opaque
@@ -139,7 +139,7 @@ impl<'tcx> DepNodeKey<TyCtxt<'tcx>> for (DefId, DefId) {
139139
}
140140
}
141141

142-
impl<'tcx> DepNodeKey<TyCtxt<'tcx>> for HirId {
142+
impl<'tcx> DepNodeKey<'tcx> for HirId {
143143
#[inline(always)]
144144
fn fingerprint_style() -> FingerprintStyle {
145145
FingerprintStyle::HirId
@@ -182,7 +182,7 @@ impl<'tcx> DepNodeKey<TyCtxt<'tcx>> for HirId {
182182
}
183183
}
184184

185-
impl<'tcx> DepNodeKey<TyCtxt<'tcx>> for ModDefId {
185+
impl<'tcx> DepNodeKey<'tcx> for ModDefId {
186186
#[inline(always)]
187187
fn fingerprint_style() -> FingerprintStyle {
188188
FingerprintStyle::DefPathHash
@@ -204,7 +204,7 @@ impl<'tcx> DepNodeKey<TyCtxt<'tcx>> for ModDefId {
204204
}
205205
}
206206

207-
impl<'tcx> DepNodeKey<TyCtxt<'tcx>> for LocalModDefId {
207+
impl<'tcx> DepNodeKey<'tcx> for LocalModDefId {
208208
#[inline(always)]
209209
fn fingerprint_style() -> FingerprintStyle {
210210
FingerprintStyle::DefPathHash

0 commit comments

Comments
 (0)