Skip to content

Commit ca2655c

Browse files
authored
Rollup merge of rust-lang#153169 - nnethercote:rm-is_anon-etc, r=petrochenkov
Various small query cleanups I found these while doing a close read of the query code. r? @oli-obk
2 parents b6faef2 + fa5138b commit ca2655c

8 files changed

Lines changed: 72 additions & 137 deletions

File tree

compiler/rustc_middle/src/dep_graph/dep_node.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -174,11 +174,6 @@ impl fmt::Debug for DepNode {
174174
/// of the `DepKind`. Overall, this allows to implement `DepContext` using this manual
175175
/// jump table instead of large matches.
176176
pub struct DepKindVTable<'tcx> {
177-
/// Anonymous queries cannot be replayed from one compiler invocation to the next.
178-
/// When their result is needed, it is recomputed. They are useful for fine-grained
179-
/// dependency tracking, and caching within one compiler invocation.
180-
pub is_anon: bool,
181-
182177
/// Eval-always queries do not track their dependencies, and are always recomputed, even if
183178
/// their inputs have not changed since the last compiler invocation. The result is still
184179
/// cached within one compiler invocation.

compiler/rustc_middle/src/queries.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -150,10 +150,10 @@ use crate::{dep_graph, mir, thir};
150150
// `Providers` that the driver creates (using several `rustc_*` crates).
151151
//
152152
// The result type of each query must implement `Clone`, and additionally
153-
// `ty::query::values::Value`, which produces an appropriate placeholder
154-
// (error) value if the query resulted in a query cycle.
153+
// `ty::query::from_cycle_error::FromCycleError`, which produces an appropriate
154+
// placeholder (error) value if the query resulted in a query cycle.
155155
// Queries marked with `cycle_fatal` do not need the latter implementation,
156-
// as they will raise an fatal error on query cycles instead.
156+
// as they will raise a fatal error on query cycles instead.
157157
rustc_queries! {
158158
/// Caches the expansion of a derive proc macro, e.g. `#[derive(Serialize)]`.
159159
/// The key is:

compiler/rustc_middle/src/query/plumbing.rs

Lines changed: 13 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -62,18 +62,6 @@ pub enum CycleErrorHandling {
6262
Stash,
6363
}
6464

65-
pub type WillCacheOnDiskForKeyFn<'tcx, Key> = fn(tcx: TyCtxt<'tcx>, key: &Key) -> bool;
66-
67-
pub type TryLoadFromDiskFn<'tcx, Key, Value> = fn(
68-
tcx: TyCtxt<'tcx>,
69-
key: &Key,
70-
prev_index: SerializedDepNodeIndex,
71-
index: DepNodeIndex,
72-
) -> Option<Value>;
73-
74-
pub type IsLoadableFromDiskFn<'tcx, Key> =
75-
fn(tcx: TyCtxt<'tcx>, key: &Key, index: SerializedDepNodeIndex) -> bool;
76-
7765
pub type HashResult<V> = Option<fn(&mut StableHashingContext<'_>, &V) -> Fingerprint>;
7866

7967
#[derive(Clone, Debug)]
@@ -128,7 +116,7 @@ pub struct QueryVTable<'tcx, C: QueryCache> {
128116
pub cycle_error_handling: CycleErrorHandling,
129117
pub state: QueryState<'tcx, C::Key>,
130118
pub cache: C,
131-
pub will_cache_on_disk_for_key_fn: Option<WillCacheOnDiskForKeyFn<'tcx, C::Key>>,
119+
pub will_cache_on_disk_for_key_fn: Option<fn(tcx: TyCtxt<'tcx>, key: &C::Key) -> bool>,
132120

133121
/// Function pointer that calls `tcx.$query(key)` for this query and
134122
/// discards the returned value.
@@ -144,11 +132,19 @@ pub struct QueryVTable<'tcx, C: QueryCache> {
144132
/// This should be the only code that calls the provider function.
145133
pub invoke_provider_fn: fn(tcx: TyCtxt<'tcx>, key: C::Key) -> C::Value,
146134

147-
pub try_load_from_disk_fn: Option<TryLoadFromDiskFn<'tcx, C::Key, C::Value>>,
148-
pub is_loadable_from_disk_fn: Option<IsLoadableFromDiskFn<'tcx, C::Key>>,
135+
pub try_load_from_disk_fn: Option<
136+
fn(
137+
tcx: TyCtxt<'tcx>,
138+
key: &C::Key,
139+
prev_index: SerializedDepNodeIndex,
140+
index: DepNodeIndex,
141+
) -> Option<C::Value>,
142+
>,
143+
pub is_loadable_from_disk_fn:
144+
Option<fn(tcx: TyCtxt<'tcx>, key: &C::Key, index: SerializedDepNodeIndex) -> bool>,
149145
pub hash_result: HashResult<C::Value>,
150146
pub value_from_cycle_error:
151-
fn(tcx: TyCtxt<'tcx>, cycle_error: &CycleError, guar: ErrorGuaranteed) -> C::Value,
147+
fn(tcx: TyCtxt<'tcx>, cycle_error: CycleError, guar: ErrorGuaranteed) -> C::Value,
152148
pub format_value: fn(&C::Value) -> String,
153149

154150
/// Formats a human-readable description of this query and its key, as
@@ -213,7 +209,7 @@ impl<'tcx, C: QueryCache> QueryVTable<'tcx, C> {
213209
pub fn value_from_cycle_error(
214210
&self,
215211
tcx: TyCtxt<'tcx>,
216-
cycle_error: &CycleError,
212+
cycle_error: CycleError,
217213
guar: ErrorGuaranteed,
218214
) -> C::Value {
219215
(self.value_from_cycle_error)(tcx, cycle_error, guar)
@@ -647,18 +643,6 @@ macro_rules! define_callbacks {
647643
};
648644
}
649645

650-
// Each of these queries corresponds to a function pointer field in the
651-
// `Providers` struct for requesting a value of that type, and a method
652-
// on `tcx: TyCtxt` (and `tcx.at(span)`) for doing that request in a way
653-
// which memoizes and does dep-graph tracking, wrapping around the actual
654-
// `Providers` that the driver creates (using several `rustc_*` crates).
655-
//
656-
// The result type of each query must implement `Clone`, and additionally
657-
// `ty::query::values::Value`, which produces an appropriate placeholder
658-
// (error) value if the query resulted in a query cycle.
659-
// Queries marked with `cycle_fatal` do not need the latter implementation,
660-
// as they will raise an fatal error on query cycles instead.
661-
662646
mod sealed {
663647
use rustc_hir::def_id::{LocalModDefId, ModDefId};
664648

compiler/rustc_query_impl/src/dep_kind_vtables.rs

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ mod non_query {
1313
// We use this for most things when incr. comp. is turned off.
1414
pub(crate) fn Null<'tcx>() -> DepKindVTable<'tcx> {
1515
DepKindVTable {
16-
is_anon: false,
1716
is_eval_always: false,
1817
key_fingerprint_style: KeyFingerprintStyle::Unit,
1918
force_from_dep_node: Some(|_, dep_node, _| {
@@ -26,7 +25,6 @@ mod non_query {
2625
// We use this for the forever-red node.
2726
pub(crate) fn Red<'tcx>() -> DepKindVTable<'tcx> {
2827
DepKindVTable {
29-
is_anon: false,
3028
is_eval_always: false,
3129
key_fingerprint_style: KeyFingerprintStyle::Unit,
3230
force_from_dep_node: Some(|_, dep_node, _| {
@@ -38,7 +36,6 @@ mod non_query {
3836

3937
pub(crate) fn SideEffect<'tcx>() -> DepKindVTable<'tcx> {
4038
DepKindVTable {
41-
is_anon: false,
4239
is_eval_always: false,
4340
key_fingerprint_style: KeyFingerprintStyle::Unit,
4441
force_from_dep_node: Some(|tcx, _, prev_index| {
@@ -51,7 +48,6 @@ mod non_query {
5148

5249
pub(crate) fn AnonZeroDeps<'tcx>() -> DepKindVTable<'tcx> {
5350
DepKindVTable {
54-
is_anon: true,
5551
is_eval_always: false,
5652
key_fingerprint_style: KeyFingerprintStyle::Opaque,
5753
force_from_dep_node: Some(|_, _, _| bug!("cannot force an anon node")),
@@ -61,7 +57,6 @@ mod non_query {
6157

6258
pub(crate) fn TraitSelect<'tcx>() -> DepKindVTable<'tcx> {
6359
DepKindVTable {
64-
is_anon: true,
6560
is_eval_always: false,
6661
key_fingerprint_style: KeyFingerprintStyle::Unit,
6762
force_from_dep_node: None,
@@ -71,7 +66,6 @@ mod non_query {
7166

7267
pub(crate) fn CompileCodegenUnit<'tcx>() -> DepKindVTable<'tcx> {
7368
DepKindVTable {
74-
is_anon: false,
7569
is_eval_always: false,
7670
key_fingerprint_style: KeyFingerprintStyle::Opaque,
7771
force_from_dep_node: None,
@@ -81,7 +75,6 @@ mod non_query {
8175

8276
pub(crate) fn CompileMonoItem<'tcx>() -> DepKindVTable<'tcx> {
8377
DepKindVTable {
84-
is_anon: false,
8578
is_eval_always: false,
8679
key_fingerprint_style: KeyFingerprintStyle::Opaque,
8780
force_from_dep_node: None,
@@ -91,7 +84,6 @@ mod non_query {
9184

9285
pub(crate) fn Metadata<'tcx>() -> DepKindVTable<'tcx> {
9386
DepKindVTable {
94-
is_anon: false,
9587
is_eval_always: false,
9688
key_fingerprint_style: KeyFingerprintStyle::Unit,
9789
force_from_dep_node: None,
@@ -117,7 +109,6 @@ where
117109

118110
if is_anon || !key_fingerprint_style.reconstructible() {
119111
return DepKindVTable {
120-
is_anon,
121112
is_eval_always,
122113
key_fingerprint_style,
123114
force_from_dep_node: None,
@@ -126,7 +117,6 @@ where
126117
}
127118

128119
DepKindVTable {
129-
is_anon,
130120
is_eval_always,
131121
key_fingerprint_style,
132122
force_from_dep_node: Some(|tcx, dep_node, _| {

compiler/rustc_query_impl/src/execution.rs

Lines changed: 19 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::mem;
44
use rustc_data_structures::hash_table::{Entry, HashTable};
55
use rustc_data_structures::stack::ensure_sufficient_stack;
66
use rustc_data_structures::{outline, sharded, sync};
7-
use rustc_errors::{Diag, FatalError, StashKey};
7+
use rustc_errors::{FatalError, StashKey};
88
use rustc_middle::dep_graph::{DepGraphData, DepNodeKey, SerializedDepNodeIndex};
99
use rustc_middle::query::plumbing::QueryVTable;
1010
use rustc_middle::query::{
@@ -108,24 +108,14 @@ fn mk_cycle<'tcx, C: QueryCache>(
108108
cycle_error: CycleError,
109109
) -> C::Value {
110110
let error = report_cycle(tcx.sess, &cycle_error);
111-
handle_cycle_error(query, tcx, &cycle_error, error)
112-
}
113-
114-
fn handle_cycle_error<'tcx, C: QueryCache>(
115-
query: &'tcx QueryVTable<'tcx, C>,
116-
tcx: TyCtxt<'tcx>,
117-
cycle_error: &CycleError,
118-
error: Diag<'_>,
119-
) -> C::Value {
120111
match query.cycle_error_handling {
121112
CycleErrorHandling::Error => {
122113
let guar = error.emit();
123114
query.value_from_cycle_error(tcx, cycle_error, guar)
124115
}
125116
CycleErrorHandling::Fatal => {
126-
error.emit();
127-
tcx.dcx().abort_if_errors();
128-
unreachable!()
117+
let guar = error.emit();
118+
guar.raise_fatal();
129119
}
130120
CycleErrorHandling::DelayBug => {
131121
let guar = error.delay_as_bug();
@@ -322,15 +312,15 @@ fn try_execute_query<'tcx, C: QueryCache, const INCR: bool>(
322312

323313
// Only call `wait_for_query` if we're using a Rayon thread pool
324314
// as it will attempt to mark the worker thread as blocked.
325-
return wait_for_query(query, tcx, span, key, latch, current_job_id);
326-
}
327-
328-
let id = job.id;
329-
drop(state_lock);
315+
wait_for_query(query, tcx, span, key, latch, current_job_id)
316+
} else {
317+
let id = job.id;
318+
drop(state_lock);
330319

331-
// If we are single-threaded we know that we have cycle error,
332-
// so we just return the error.
333-
cycle_error(query, tcx, id, span)
320+
// If we are single-threaded we know that we have cycle error,
321+
// so we just return the error.
322+
cycle_error(query, tcx, id, span)
323+
}
334324
}
335325
ActiveKeyStatus::Poisoned => FatalError.raise(),
336326
}
@@ -412,27 +402,21 @@ fn execute_job_non_incr<'tcx, C: QueryCache>(
412402
) -> (C::Value, DepNodeIndex) {
413403
debug_assert!(!tcx.dep_graph.is_fully_enabled());
414404

415-
// Fingerprint the key, just to assert that it doesn't
416-
// have anything we don't consider hashable
417-
if cfg!(debug_assertions) {
418-
let _ = key.to_fingerprint(tcx);
419-
}
420-
421405
let prof_timer = tcx.prof.query_provider();
422406
// Call the query provider.
423407
let result =
424408
start_query(tcx, job_id, query.depth_limit, || (query.invoke_provider_fn)(tcx, key));
425409
let dep_node_index = tcx.dep_graph.next_virtual_depnode_index();
426410
prof_timer.finish_with_query_invocation_id(dep_node_index.into());
427411

428-
// Similarly, fingerprint the result to assert that
429-
// it doesn't have anything not considered hashable.
430-
if cfg!(debug_assertions)
431-
&& let Some(hash_result) = query.hash_result
432-
{
433-
tcx.with_stable_hashing_context(|mut hcx| {
434-
hash_result(&mut hcx, &result);
435-
});
412+
// Sanity: Fingerprint the key and the result to assert they don't contain anything unhashable.
413+
if cfg!(debug_assertions) {
414+
let _ = key.to_fingerprint(tcx);
415+
if let Some(hash_result) = query.hash_result {
416+
tcx.with_stable_hashing_context(|mut hcx| {
417+
hash_result(&mut hcx, &result);
418+
});
419+
}
436420
}
437421

438422
(result, dep_node_index)

0 commit comments

Comments
 (0)