Skip to content

Commit 15491d7

Browse files
committed
Auto merge of #89343 - Mark-Simulacrum:no-args-queries, r=cjgillot
Refactor fingerprint reconstruction This PR replaces can_reconstruct_query_key with fingerprint_style, which returns the style of the fingerprint for that query. This allows us to avoid trying to extract a DefId (or equivalent) from keys which *are* reconstructible because they're () but not as DefIds. This is done with the goal of fixing -Zdump-dep-graph, which seems to have broken a while ago (I didn't try to bisect). Currently even on a `fn main() {}` file it'll ICE (you need to also pass -Zquery-dep-graph for it to work at all), and this patch indirectly fixes the cause of that ICE. This also adds a test for it continuing to work.
2 parents bb918d0 + 415a9a2 commit 15491d7

File tree

8 files changed

+87
-48
lines changed

8 files changed

+87
-48
lines changed

compiler/rustc_middle/src/dep_graph/dep_node.rs

+38-34
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ use rustc_data_structures::fingerprint::Fingerprint;
6363
use rustc_hir::def_id::{CrateNum, DefId, LocalDefId, CRATE_DEF_INDEX};
6464
use rustc_hir::definitions::DefPathHash;
6565
use rustc_hir::HirId;
66+
use rustc_query_system::dep_graph::FingerprintStyle;
6667
use rustc_span::symbol::Symbol;
6768
use std::hash::Hash;
6869

@@ -89,9 +90,9 @@ pub struct DepKindStruct {
8990

9091
/// Whether the query key can be recovered from the hashed fingerprint.
9192
/// See [DepNodeParams] trait for the behaviour of each key type.
92-
// FIXME: Make this a simple boolean once DepNodeParams::can_reconstruct_query_key
93+
// FIXME: Make this a simple boolean once DepNodeParams::fingerprint_style
9394
// can be made a specialized associated const.
94-
can_reconstruct_query_key: fn() -> bool,
95+
fingerprint_style: fn() -> FingerprintStyle,
9596
}
9697

9798
impl std::ops::Deref for DepKind {
@@ -103,14 +104,14 @@ impl std::ops::Deref for DepKind {
103104

104105
impl DepKind {
105106
#[inline(always)]
106-
pub fn can_reconstruct_query_key(&self) -> bool {
107+
pub fn fingerprint_style(&self) -> FingerprintStyle {
107108
// Only fetch the DepKindStruct once.
108109
let data: &DepKindStruct = &**self;
109110
if data.is_anon {
110-
return false;
111+
return FingerprintStyle::Opaque;
111112
}
112113

113-
(data.can_reconstruct_query_key)()
114+
(data.fingerprint_style)()
114115
}
115116
}
116117

@@ -151,38 +152,39 @@ macro_rules! contains_eval_always_attr {
151152
pub mod dep_kind {
152153
use super::*;
153154
use crate::ty::query::query_keys;
155+
use rustc_query_system::dep_graph::FingerprintStyle;
154156

155157
// We use this for most things when incr. comp. is turned off.
156158
pub const Null: DepKindStruct = DepKindStruct {
157159
has_params: false,
158160
is_anon: false,
159161
is_eval_always: false,
160162

161-
can_reconstruct_query_key: || true,
163+
fingerprint_style: || FingerprintStyle::Unit,
162164
};
163165

164166
pub const TraitSelect: DepKindStruct = DepKindStruct {
165167
has_params: false,
166168
is_anon: true,
167169
is_eval_always: false,
168170

169-
can_reconstruct_query_key: || true,
171+
fingerprint_style: || FingerprintStyle::Unit,
170172
};
171173

172174
pub const CompileCodegenUnit: DepKindStruct = DepKindStruct {
173175
has_params: true,
174176
is_anon: false,
175177
is_eval_always: false,
176178

177-
can_reconstruct_query_key: || false,
179+
fingerprint_style: || FingerprintStyle::Opaque,
178180
};
179181

180182
pub const CompileMonoItem: DepKindStruct = DepKindStruct {
181183
has_params: true,
182184
is_anon: false,
183185
is_eval_always: false,
184186

185-
can_reconstruct_query_key: || false,
187+
fingerprint_style: || FingerprintStyle::Opaque,
186188
};
187189

188190
macro_rules! define_query_dep_kinds {
@@ -196,16 +198,16 @@ pub mod dep_kind {
196198
const is_eval_always: bool = contains_eval_always_attr!($($attrs)*);
197199

198200
#[inline(always)]
199-
fn can_reconstruct_query_key() -> bool {
201+
fn fingerprint_style() -> rustc_query_system::dep_graph::FingerprintStyle {
200202
<query_keys::$variant<'_> as DepNodeParams<TyCtxt<'_>>>
201-
::can_reconstruct_query_key()
203+
::fingerprint_style()
202204
}
203205

204206
DepKindStruct {
205207
has_params,
206208
is_anon,
207209
is_eval_always,
208-
can_reconstruct_query_key,
210+
fingerprint_style,
209211
}
210212
};)*
211213
);
@@ -320,7 +322,7 @@ impl DepNodeExt for DepNode {
320322
/// method will assert that the given DepKind actually requires a
321323
/// single DefId/DefPathHash parameter.
322324
fn from_def_path_hash(def_path_hash: DefPathHash, kind: DepKind) -> DepNode {
323-
debug_assert!(kind.can_reconstruct_query_key() && kind.has_params);
325+
debug_assert!(kind.fingerprint_style() == FingerprintStyle::DefPathHash);
324326
DepNode { kind, hash: def_path_hash.0.into() }
325327
}
326328

@@ -335,7 +337,7 @@ impl DepNodeExt for DepNode {
335337
/// refers to something from the previous compilation session that
336338
/// has been removed.
337339
fn extract_def_id(&self, tcx: TyCtxt<'tcx>) -> Option<DefId> {
338-
if self.kind.can_reconstruct_query_key() {
340+
if self.kind.fingerprint_style() == FingerprintStyle::DefPathHash {
339341
Some(
340342
tcx.on_disk_cache
341343
.as_ref()?
@@ -350,14 +352,16 @@ impl DepNodeExt for DepNode {
350352
fn from_label_string(label: &str, def_path_hash: DefPathHash) -> Result<DepNode, ()> {
351353
let kind = dep_kind_from_label_string(label)?;
352354

353-
if !kind.can_reconstruct_query_key() {
354-
return Err(());
355-
}
356-
357-
if kind.has_params {
358-
Ok(DepNode::from_def_path_hash(def_path_hash, kind))
359-
} else {
360-
Ok(DepNode::new_no_params(kind))
355+
match kind.fingerprint_style() {
356+
FingerprintStyle::Opaque => Err(()),
357+
FingerprintStyle::Unit => {
358+
if !kind.has_params {
359+
Ok(DepNode::new_no_params(kind))
360+
} else {
361+
Err(())
362+
}
363+
}
364+
FingerprintStyle::DefPathHash => Ok(DepNode::from_def_path_hash(def_path_hash, kind)),
361365
}
362366
}
363367

@@ -369,8 +373,8 @@ impl DepNodeExt for DepNode {
369373

370374
impl<'tcx> DepNodeParams<TyCtxt<'tcx>> for () {
371375
#[inline(always)]
372-
fn can_reconstruct_query_key() -> bool {
373-
true
376+
fn fingerprint_style() -> FingerprintStyle {
377+
FingerprintStyle::Unit
374378
}
375379

376380
fn to_fingerprint(&self, _: TyCtxt<'tcx>) -> Fingerprint {
@@ -384,8 +388,8 @@ impl<'tcx> DepNodeParams<TyCtxt<'tcx>> for () {
384388

385389
impl<'tcx> DepNodeParams<TyCtxt<'tcx>> for DefId {
386390
#[inline(always)]
387-
fn can_reconstruct_query_key() -> bool {
388-
true
391+
fn fingerprint_style() -> FingerprintStyle {
392+
FingerprintStyle::DefPathHash
389393
}
390394

391395
fn to_fingerprint(&self, tcx: TyCtxt<'tcx>) -> Fingerprint {
@@ -403,8 +407,8 @@ impl<'tcx> DepNodeParams<TyCtxt<'tcx>> for DefId {
403407

404408
impl<'tcx> DepNodeParams<TyCtxt<'tcx>> for LocalDefId {
405409
#[inline(always)]
406-
fn can_reconstruct_query_key() -> bool {
407-
true
410+
fn fingerprint_style() -> FingerprintStyle {
411+
FingerprintStyle::DefPathHash
408412
}
409413

410414
fn to_fingerprint(&self, tcx: TyCtxt<'tcx>) -> Fingerprint {
@@ -422,8 +426,8 @@ impl<'tcx> DepNodeParams<TyCtxt<'tcx>> for LocalDefId {
422426

423427
impl<'tcx> DepNodeParams<TyCtxt<'tcx>> for CrateNum {
424428
#[inline(always)]
425-
fn can_reconstruct_query_key() -> bool {
426-
true
429+
fn fingerprint_style() -> FingerprintStyle {
430+
FingerprintStyle::DefPathHash
427431
}
428432

429433
fn to_fingerprint(&self, tcx: TyCtxt<'tcx>) -> Fingerprint {
@@ -442,8 +446,8 @@ impl<'tcx> DepNodeParams<TyCtxt<'tcx>> for CrateNum {
442446

443447
impl<'tcx> DepNodeParams<TyCtxt<'tcx>> for (DefId, DefId) {
444448
#[inline(always)]
445-
fn can_reconstruct_query_key() -> bool {
446-
false
449+
fn fingerprint_style() -> FingerprintStyle {
450+
FingerprintStyle::Opaque
447451
}
448452

449453
// We actually would not need to specialize the implementation of this
@@ -467,8 +471,8 @@ impl<'tcx> DepNodeParams<TyCtxt<'tcx>> for (DefId, DefId) {
467471

468472
impl<'tcx> DepNodeParams<TyCtxt<'tcx>> for HirId {
469473
#[inline(always)]
470-
fn can_reconstruct_query_key() -> bool {
471-
false
474+
fn fingerprint_style() -> FingerprintStyle {
475+
FingerprintStyle::Opaque
472476
}
473477

474478
// We actually would not need to specialize the implementation of this

compiler/rustc_middle/src/dep_graph/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ impl rustc_query_system::dep_graph::DepKind for DepKind {
2525
const NULL: Self = DepKind::Null;
2626

2727
#[inline(always)]
28-
fn can_reconstruct_query_key(&self) -> bool {
29-
DepKind::can_reconstruct_query_key(self)
28+
fn fingerprint_style(&self) -> rustc_query_system::dep_graph::FingerprintStyle {
29+
DepKind::fingerprint_style(self)
3030
}
3131

3232
#[inline(always)]

compiler/rustc_query_impl/src/plumbing.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,7 @@ macro_rules! define_queries {
428428
use rustc_middle::ty::query::query_keys;
429429
use rustc_query_system::dep_graph::DepNodeParams;
430430
use rustc_query_system::query::{force_query, QueryDescription};
431+
use rustc_query_system::dep_graph::FingerprintStyle;
431432

432433
// We use this for most things when incr. comp. is turned off.
433434
pub const Null: QueryStruct = QueryStruct {
@@ -454,9 +455,9 @@ macro_rules! define_queries {
454455
const is_anon: bool = is_anon!([$($modifiers)*]);
455456

456457
#[inline(always)]
457-
fn can_reconstruct_query_key() -> bool {
458+
fn fingerprint_style() -> FingerprintStyle {
458459
<query_keys::$name<'_> as DepNodeParams<TyCtxt<'_>>>
459-
::can_reconstruct_query_key()
460+
::fingerprint_style()
460461
}
461462

462463
fn recover<'tcx>(tcx: TyCtxt<'tcx>, dep_node: &DepNode) -> Option<query_keys::$name<'tcx>> {
@@ -472,7 +473,7 @@ macro_rules! define_queries {
472473
return
473474
}
474475

475-
if !can_reconstruct_query_key() {
476+
if !fingerprint_style().reconstructible() {
476477
return
477478
}
478479

compiler/rustc_query_system/src/dep_graph/dep_node.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
//! `DefId` it was computed from. In other cases, too much information gets
4343
//! lost during fingerprint computation.
4444
45-
use super::{DepContext, DepKind};
45+
use super::{DepContext, DepKind, FingerprintStyle};
4646
use crate::ich::StableHashingContext;
4747

4848
use rustc_data_structures::fingerprint::{Fingerprint, PackedFingerprint};
@@ -75,7 +75,7 @@ impl<K: DepKind> DepNode<K> {
7575

7676
#[cfg(debug_assertions)]
7777
{
78-
if !kind.can_reconstruct_query_key()
78+
if !kind.fingerprint_style().reconstructible()
7979
&& (tcx.sess().opts.debugging_opts.incremental_info
8080
|| tcx.sess().opts.debugging_opts.query_dep_graph)
8181
{
@@ -94,7 +94,7 @@ impl<K: DepKind> fmt::Debug for DepNode<K> {
9494
}
9595

9696
pub trait DepNodeParams<Ctxt: DepContext>: fmt::Debug + Sized {
97-
fn can_reconstruct_query_key() -> bool;
97+
fn fingerprint_style() -> FingerprintStyle;
9898

9999
/// This method turns the parameters of a DepNodeConstructor into an opaque
100100
/// Fingerprint to be used in DepNode.
@@ -111,7 +111,7 @@ pub trait DepNodeParams<Ctxt: DepContext>: fmt::Debug + Sized {
111111
/// This method tries to recover the query key from the given `DepNode`,
112112
/// something which is needed when forcing `DepNode`s during red-green
113113
/// evaluation. The query system will only call this method if
114-
/// `can_reconstruct_query_key()` is `true`.
114+
/// `fingerprint_style()` is not `FingerprintStyle::Opaque`.
115115
/// It is always valid to return `None` here, in which case incremental
116116
/// compilation will treat the query as having changed instead of forcing it.
117117
fn recover(tcx: Ctxt, dep_node: &DepNode<Ctxt::DepKind>) -> Option<Self>;
@@ -122,8 +122,8 @@ where
122122
T: for<'a> HashStable<StableHashingContext<'a>> + fmt::Debug,
123123
{
124124
#[inline]
125-
default fn can_reconstruct_query_key() -> bool {
126-
false
125+
default fn fingerprint_style() -> FingerprintStyle {
126+
FingerprintStyle::Opaque
127127
}
128128

129129
default fn to_fingerprint(&self, tcx: Ctxt) -> Fingerprint {

compiler/rustc_query_system/src/dep_graph/mod.rs

+22-1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,27 @@ impl<T: DepContext> HasDepContext for T {
5050
}
5151
}
5252

53+
/// Describes the contents of the fingerprint generated by a given query.
54+
#[derive(PartialEq, Eq, Copy, Clone)]
55+
pub enum FingerprintStyle {
56+
/// The fingerprint is actually a DefPathHash.
57+
DefPathHash,
58+
/// Query key was `()` or equivalent, so fingerprint is just zero.
59+
Unit,
60+
/// Some opaque hash.
61+
Opaque,
62+
}
63+
64+
impl FingerprintStyle {
65+
#[inline]
66+
pub fn reconstructible(self) -> bool {
67+
match self {
68+
FingerprintStyle::DefPathHash | FingerprintStyle::Unit => true,
69+
FingerprintStyle::Opaque => false,
70+
}
71+
}
72+
}
73+
5374
/// Describe the different families of dependency nodes.
5475
pub trait DepKind: Copy + fmt::Debug + Eq + Hash + Send + Encodable<FileEncoder> + 'static {
5576
const NULL: Self;
@@ -73,5 +94,5 @@ pub trait DepKind: Copy + fmt::Debug + Eq + Hash + Send + Encodable<FileEncoder>
7394
where
7495
OP: for<'a> FnOnce(Option<&'a Lock<TaskDeps<Self>>>);
7596

76-
fn can_reconstruct_query_key(&self) -> bool;
97+
fn fingerprint_style(&self) -> FingerprintStyle;
7798
}

compiler/rustc_query_system/src/query/plumbing.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -540,7 +540,7 @@ where
540540
// We always expect to find a cached result for things that
541541
// can be forced from `DepNode`.
542542
debug_assert!(
543-
!dep_node.kind.can_reconstruct_query_key() || result.is_some(),
543+
!dep_node.kind.fingerprint_style().reconstructible() || result.is_some(),
544544
"missing on-disk cache entry for {:?}",
545545
dep_node
546546
);
@@ -778,7 +778,7 @@ where
778778
return false;
779779
}
780780

781-
if !<Q::Key as DepNodeParams<CTX::DepContext>>::can_reconstruct_query_key() {
781+
if !<Q::Key as DepNodeParams<CTX::DepContext>>::fingerprint_style().reconstructible() {
782782
return false;
783783
}
784784

src/test/run-make/dep-graph/Makefile

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
-include ../../run-make-fulldeps/tools.mk
2+
3+
# ignore-cross-compile
4+
5+
# Just verify that we successfully run and produce dep graphs when requested.
6+
7+
all:
8+
RUST_DEP_GRAPH=$(TMPDIR)/dep-graph $(RUSTC) \
9+
-Cincremental=$(TMPDIR)/incr \
10+
-Zquery-dep-graph -Zdump-dep-graph foo.rs
11+
test -f $(TMPDIR)/dep-graph.txt
12+
test -f $(TMPDIR)/dep-graph.dot

src/test/run-make/dep-graph/foo.rs

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
fn main() {}

0 commit comments

Comments
 (0)