Skip to content

Commit 2495573

Browse files
committed
Auto merge of #153065 - nnethercote:rm-desc-cache-modules, r=<try>
Remove `_description_fns` and `_cache_on_disk_if_fns` modules
2 parents 859951e + f52773b commit 2495573

8 files changed

Lines changed: 105 additions & 170 deletions

File tree

Cargo.lock

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4492,6 +4492,7 @@ version = "0.0.0"
44924492
dependencies = [
44934493
"measureme",
44944494
"rustc_abi",
4495+
"rustc_ast",
44954496
"rustc_data_structures",
44964497
"rustc_errors",
44974498
"rustc_hir",

compiler/rustc_macros/src/query.rs

Lines changed: 22 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -273,52 +273,6 @@ fn doc_comment_from_desc(list: &Punctuated<Expr, token::Comma>) -> Result<Attrib
273273
Ok(parse_quote! { #[doc = #doc_string] })
274274
}
275275

276-
/// Contains token streams that are used to accumulate per-query helper
277-
/// functions, to be used by the final output of `rustc_queries!`.
278-
///
279-
/// Helper items typically have the same name as the query they relate to,
280-
/// and expect to be interpolated into a dedicated module.
281-
#[derive(Default)]
282-
struct HelperTokenStreams {
283-
description_fns_stream: proc_macro2::TokenStream,
284-
cache_on_disk_if_fns_stream: proc_macro2::TokenStream,
285-
}
286-
287-
fn make_helpers_for_query(query: &Query, streams: &mut HelperTokenStreams) {
288-
let Query { name, key_pat, key_ty, modifiers, .. } = &query;
289-
290-
// Replace span for `name` to make rust-analyzer ignore it.
291-
let mut erased_name = name.clone();
292-
erased_name.set_span(Span::call_site());
293-
294-
// Generate a function to check whether we should cache the query to disk, for some key.
295-
if let Some(CacheOnDiskIf { block, .. }) = modifiers.cache_on_disk_if.as_ref() {
296-
// `pass_by_value`: some keys are marked with `rustc_pass_by_value`, but we take keys by
297-
// reference here.
298-
// FIXME: `pass_by_value` is badly named; `allow(rustc::pass_by_value)` actually means
299-
// "allow pass by reference of `rustc_pass_by_value` types".
300-
streams.cache_on_disk_if_fns_stream.extend(quote! {
301-
#[allow(unused_variables, rustc::pass_by_value)]
302-
#[inline]
303-
pub fn #erased_name<'tcx>(tcx: TyCtxt<'tcx>, #key_pat: &#key_ty) -> bool
304-
#block
305-
});
306-
}
307-
308-
let Desc { expr_list, .. } = &modifiers.desc;
309-
310-
let desc = quote! {
311-
#[allow(unused_variables)]
312-
pub fn #erased_name<'tcx>(tcx: TyCtxt<'tcx>, #key_pat: #key_ty) -> String {
313-
format!(#expr_list)
314-
}
315-
};
316-
317-
streams.description_fns_stream.extend(quote! {
318-
#desc
319-
});
320-
}
321-
322276
/// Add hints for rust-analyzer
323277
fn add_to_analyzer_stream(query: &Query, analyzer_stream: &mut proc_macro2::TokenStream) {
324278
// Add links to relevant modifiers
@@ -398,7 +352,6 @@ pub(super) fn rustc_queries(input: TokenStream) -> TokenStream {
398352
let queries = parse_macro_input!(input as List<Query>);
399353

400354
let mut query_stream = quote! {};
401-
let mut helpers = HelperTokenStreams::default();
402355
let mut analyzer_stream = quote! {};
403356
let mut errors = quote! {};
404357

@@ -413,7 +366,7 @@ pub(super) fn rustc_queries(input: TokenStream) -> TokenStream {
413366
}
414367

415368
for query in queries.0 {
416-
let Query { doc_comments, name, key_ty, return_ty, modifiers, .. } = &query;
369+
let Query { doc_comments, name, key_pat, key_ty, return_ty, modifiers, .. } = &query;
417370

418371
// Normalize an absent return type into `-> ()` to make macro-rules parsing easier.
419372
let return_ty = match return_ty {
@@ -431,6 +384,10 @@ pub(super) fn rustc_queries(input: TokenStream) -> TokenStream {
431384
}
432385
}
433386

387+
// Put a description closure inside the `desc` modifier: `(desc { <closure> })`.
388+
let expr_list = &modifiers.desc.expr_list;
389+
modifiers_out.push(quote! { (desc { |tcx, #key_pat| format!(#expr_list) }) });
390+
434391
passthrough!(
435392
arena_cache,
436393
cycle_fatal,
@@ -445,11 +402,23 @@ pub(super) fn rustc_queries(input: TokenStream) -> TokenStream {
445402
return_result_from_ensure_ok,
446403
);
447404

448-
// If there was a `cache_on_disk_if` modifier in the real input, pass
449-
// on a synthetic `(cache_on_disk)` modifier that can be inspected by
450-
// macro-rules macros.
451-
if modifiers.cache_on_disk_if.is_some() {
452-
modifiers_out.push(quote! { (cache_on_disk) });
405+
// If there was a `cache_on_disk_if` modifier, put a closure inside it:
406+
// `(cache_on_disk { <closure > }`.
407+
if let Some(CacheOnDiskIf { block, .. }) = &modifiers.cache_on_disk_if {
408+
modifiers_out.push(quote! {
409+
(cache_on_disk_if {
410+
// `pass_by_value`: some keys are marked with `rustc_pass_by_value`, but we
411+
// take keys by reference here.
412+
// FIXME: `pass_by_value` is badly named; `allow(rustc::pass_by_value)`
413+
// actually means "allow pass by reference of `rustc_pass_by_value` types".
414+
//
415+
// The type annotations are required to avoid compile errors, which is annoying
416+
// because it necessitates extra `use` items in the file using
417+
// `rustc_with_all_queries!`.
418+
#[allow(rustc::pass_by_value)]
419+
|tcx: TyCtxt<'_>, #key_pat: &#key_ty| #block
420+
})
421+
});
453422
}
454423

455424
// This uses the span of the query definition for the commas,
@@ -482,11 +451,8 @@ pub(super) fn rustc_queries(input: TokenStream) -> TokenStream {
482451
}
483452

484453
add_to_analyzer_stream(&query, &mut analyzer_stream);
485-
make_helpers_for_query(&query, &mut helpers);
486454
}
487455

488-
let HelperTokenStreams { description_fns_stream, cache_on_disk_if_fns_stream } = helpers;
489-
490456
TokenStream::from(quote! {
491457
/// Higher-order macro that invokes the specified macro with a prepared
492458
/// list of all query signatures (including modifiers).
@@ -516,25 +482,6 @@ pub(super) fn rustc_queries(input: TokenStream) -> TokenStream {
516482
#analyzer_stream
517483
}
518484

519-
/// Functions that format a human-readable description of each query
520-
/// and its key, as specified by the `desc` query modifier.
521-
///
522-
/// (The leading `_` avoids collisions with actual query names when
523-
/// expanded in `rustc_middle::queries`, and makes this macro-generated
524-
/// module easier to search for.)
525-
pub mod _description_fns {
526-
use super::*;
527-
#description_fns_stream
528-
}
529-
530-
// FIXME(Zalathar): Instead of declaring these functions directly, can
531-
// we put them in a macro and then expand that macro downstream in
532-
// `rustc_query_impl`, where the functions are actually used?
533-
pub mod _cache_on_disk_if_fns {
534-
use super::*;
535-
#cache_on_disk_if_fns_stream
536-
}
537-
538485
#errors
539486
})
540487
}

compiler/rustc_middle/src/queries.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,6 @@ use rustc_session::cstore::{
9696
CrateDepKind, CrateSource, ExternCrate, ForeignModule, LinkagePreference, NativeLib,
9797
};
9898
use rustc_session::lint::LintExpectationId;
99-
use rustc_span::def_id::LOCAL_CRATE;
10099
use rustc_span::source_map::Spanned;
101100
use rustc_span::{DUMMY_SP, LocalExpnId, Span, Symbol};
102101
use rustc_target::spec::PanicStrategy;
@@ -120,7 +119,6 @@ use crate::mir::interpret::{
120119
use crate::mir::mono::{
121120
CodegenUnit, CollectionMode, MonoItem, MonoItemPartitions, NormalizationErrorInMono,
122121
};
123-
use crate::query::describe_as_module;
124122
use crate::query::plumbing::CyclePlaceholder;
125123
use crate::traits::query::{
126124
CanonicalAliasGoal, CanonicalDropckOutlivesGoal, CanonicalImpliedOutlivesBoundsGoal,
@@ -135,7 +133,6 @@ use crate::traits::{
135133
};
136134
use crate::ty::fast_reject::SimplifiedType;
137135
use crate::ty::layout::ValidityRequirement;
138-
use crate::ty::print::PrintTraitRefExt;
139136
use crate::ty::util::AlwaysRequiresDrop;
140137
use crate::ty::{
141138
self, CrateInherentImpls, GenericArg, GenericArgsRef, LitToConstInput, PseudoCanonicalInput,

compiler/rustc_middle/src/query/plumbing.rs

Lines changed: 11 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -65,18 +65,6 @@ pub enum CycleErrorHandling {
6565
Stash,
6666
}
6767

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

8270
#[derive(Clone, Debug)]
@@ -133,7 +121,7 @@ pub struct QueryVTable<'tcx, C: QueryCache> {
133121
pub query_state: usize,
134122
// Offset of this query's cache field in the QueryCaches struct
135123
pub query_cache: usize,
136-
pub will_cache_on_disk_for_key_fn: Option<WillCacheOnDiskForKeyFn<'tcx, C::Key>>,
124+
pub will_cache_on_disk_for_key_fn: fn(tcx: TyCtxt<'tcx>, key: &C::Key) -> bool,
137125

138126
/// Function pointer that calls `tcx.$query(key)` for this query and
139127
/// discards the returned value.
@@ -149,8 +137,16 @@ pub struct QueryVTable<'tcx, C: QueryCache> {
149137
/// This should be the only code that calls the provider function.
150138
pub invoke_provider_fn: fn(tcx: TyCtxt<'tcx>, key: C::Key) -> C::Value,
151139

152-
pub try_load_from_disk_fn: Option<TryLoadFromDiskFn<'tcx, C::Key, C::Value>>,
153-
pub is_loadable_from_disk_fn: Option<IsLoadableFromDiskFn<'tcx, C::Key>>,
140+
pub try_load_from_disk_fn: fn(
141+
tcx: TyCtxt<'tcx>,
142+
key: &C::Key,
143+
prev_index: SerializedDepNodeIndex,
144+
index: DepNodeIndex,
145+
) -> Option<C::Value>,
146+
147+
pub is_loadable_from_disk_fn:
148+
fn(tcx: TyCtxt<'tcx>, key: &C::Key, index: SerializedDepNodeIndex) -> bool,
149+
154150
pub hash_result: HashResult<C::Value>,
155151
pub value_from_cycle_error:
156152
fn(tcx: TyCtxt<'tcx>, cycle_error: &CycleError, guar: ErrorGuaranteed) -> C::Value,
@@ -176,11 +172,6 @@ impl<'tcx, C: QueryCache> fmt::Debug for QueryVTable<'tcx, C> {
176172
}
177173

178174
impl<'tcx, C: QueryCache> QueryVTable<'tcx, C> {
179-
#[inline(always)]
180-
pub fn will_cache_on_disk_for_key(&self, tcx: TyCtxt<'tcx>, key: &C::Key) -> bool {
181-
self.will_cache_on_disk_for_key_fn.map_or(false, |f| f(tcx, key))
182-
}
183-
184175
// Don't use this method to access query results, instead use the methods on TyCtxt.
185176
#[inline(always)]
186177
pub fn query_state(&self, tcx: TyCtxt<'tcx>) -> &'tcx QueryState<'tcx, C::Key> {
@@ -205,38 +196,6 @@ impl<'tcx, C: QueryCache> QueryVTable<'tcx, C> {
205196
}
206197
}
207198

208-
#[inline(always)]
209-
pub fn try_load_from_disk(
210-
&self,
211-
tcx: TyCtxt<'tcx>,
212-
key: &C::Key,
213-
prev_index: SerializedDepNodeIndex,
214-
index: DepNodeIndex,
215-
) -> Option<C::Value> {
216-
// `?` will return None immediately for queries that never cache to disk.
217-
self.try_load_from_disk_fn?(tcx, key, prev_index, index)
218-
}
219-
220-
#[inline]
221-
pub fn is_loadable_from_disk(
222-
&self,
223-
tcx: TyCtxt<'tcx>,
224-
key: &C::Key,
225-
index: SerializedDepNodeIndex,
226-
) -> bool {
227-
self.is_loadable_from_disk_fn.map_or(false, |f| f(tcx, key, index))
228-
}
229-
230-
/// Synthesize an error value to let compilation continue after a cycle.
231-
pub fn value_from_cycle_error(
232-
&self,
233-
tcx: TyCtxt<'tcx>,
234-
cycle_error: &CycleError,
235-
guar: ErrorGuaranteed,
236-
) -> C::Value {
237-
(self.value_from_cycle_error)(tcx, cycle_error, guar)
238-
}
239-
240199
pub fn construct_dep_node(&self, tcx: TyCtxt<'tcx>, key: &C::Key) -> DepNode {
241200
DepNode::construct(tcx, self.dep_kind, key)
242201
}

compiler/rustc_query_impl/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ edition = "2024"
77
# tidy-alphabetical-start
88
measureme = "12.0.1"
99
rustc_abi = { path = "../rustc_abi" }
10+
rustc_ast = { path = "../rustc_ast" }
1011
rustc_data_structures = { path = "../rustc_data_structures" }
1112
rustc_errors = { path = "../rustc_errors" }
1213
rustc_hir = { path = "../rustc_hir" }

compiler/rustc_query_impl/src/execution.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ fn handle_cycle_error<'tcx, C: QueryCache>(
120120
match query.cycle_error_handling {
121121
CycleErrorHandling::Error => {
122122
let guar = error.emit();
123-
query.value_from_cycle_error(tcx, cycle_error, guar)
123+
(query.value_from_cycle_error)(tcx, cycle_error, guar)
124124
}
125125
CycleErrorHandling::Fatal => {
126126
error.emit();
@@ -129,7 +129,7 @@ fn handle_cycle_error<'tcx, C: QueryCache>(
129129
}
130130
CycleErrorHandling::DelayBug => {
131131
let guar = error.delay_as_bug();
132-
query.value_from_cycle_error(tcx, cycle_error, guar)
132+
(query.value_from_cycle_error)(tcx, cycle_error, guar)
133133
}
134134
CycleErrorHandling::Stash => {
135135
let guar = if let Some(root) = cycle_error.cycle.first()
@@ -139,7 +139,7 @@ fn handle_cycle_error<'tcx, C: QueryCache>(
139139
} else {
140140
error.emit()
141141
};
142-
query.value_from_cycle_error(tcx, cycle_error, guar)
142+
(query.value_from_cycle_error)(tcx, cycle_error, guar)
143143
}
144144
}
145145
}
@@ -508,7 +508,9 @@ fn try_load_from_disk_and_cache_in_memory<'tcx, C: QueryCache>(
508508

509509
// First we try to load the result from the on-disk cache.
510510
// Some things are never cached on disk.
511-
if let Some(result) = query.try_load_from_disk(tcx, key, prev_dep_node_index, dep_node_index) {
511+
if let Some(result) =
512+
(query.try_load_from_disk_fn)(tcx, key, prev_dep_node_index, dep_node_index)
513+
{
512514
if std::intrinsics::unlikely(tcx.sess.opts.unstable_opts.query_dep_graph) {
513515
dep_graph_data.mark_debug_loaded_from_disk(*dep_node)
514516
}
@@ -541,15 +543,15 @@ fn try_load_from_disk_and_cache_in_memory<'tcx, C: QueryCache>(
541543
// We always expect to find a cached result for things that
542544
// can be forced from `DepNode`.
543545
debug_assert!(
544-
!query.will_cache_on_disk_for_key(tcx, key)
546+
!(query.will_cache_on_disk_for_key_fn)(tcx, key)
545547
|| !tcx.key_fingerprint_style(dep_node.kind).reconstructible(),
546548
"missing on-disk cache entry for {dep_node:?}"
547549
);
548550

549551
// Sanity check for the logic in `ensure`: if the node is green and the result loadable,
550552
// we should actually be able to load it.
551553
debug_assert!(
552-
!query.is_loadable_from_disk(tcx, key, prev_dep_node_index),
554+
!(query.is_loadable_from_disk_fn)(tcx, key, prev_dep_node_index),
553555
"missing on-disk cache entry for loadable {dep_node:?}"
554556
);
555557

@@ -646,7 +648,7 @@ fn check_if_ensure_can_skip_execution<'tcx, C: QueryCache>(
646648
// In ensure-done mode, we can only skip execution for this key if
647649
// there's a disk-cached value available to load later if needed,
648650
// which guarantees the query provider will never run for this key.
649-
let is_loadable = query.is_loadable_from_disk(tcx, key, serialized_dep_node_index);
651+
let is_loadable = (query.is_loadable_from_disk_fn)(tcx, key, serialized_dep_node_index);
650652
EnsureCanSkip { skip_execution: is_loadable, dep_node: Some(dep_node) }
651653
}
652654
}

compiler/rustc_query_impl/src/lib.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,27 @@
55
#![feature(core_intrinsics)]
66
#![feature(min_specialization)]
77
#![feature(rustc_attrs)]
8+
#![feature(stmt_expr_attributes)]
89
#![feature(try_blocks)]
910
// tidy-alphabetical-end
1011

12+
use rustc_ast::tokenstream::TokenStream;
1113
use rustc_data_structures::sync::AtomicU64;
14+
use rustc_hir::def::DefKind;
15+
use rustc_hir::def_id::LOCAL_CRATE;
1216
use rustc_middle::dep_graph;
17+
use rustc_middle::mir::interpret::GlobalId;
18+
use rustc_middle::mir::mono::CollectionMode;
1319
use rustc_middle::queries::{
1420
self, ExternProviders, Providers, QueryCaches, QueryEngine, QueryStates,
1521
};
1622
use rustc_middle::query::on_disk_cache::{CacheEncoder, EncodedDepNodeIndex, OnDiskCache};
1723
use rustc_middle::query::plumbing::{QuerySystem, QuerySystemFns, QueryVTable};
18-
use rustc_middle::query::{AsLocalKey, QueryCache, QueryMode};
19-
use rustc_middle::ty::TyCtxt;
20-
use rustc_span::Span;
24+
use rustc_middle::query::{AsLocalKey, QueryCache, QueryMode, describe_as_module};
25+
use rustc_middle::ty::print::PrintTraitRefExt;
26+
use rustc_middle::ty::{self, PseudoCanonicalInput, TyCtxt};
27+
use rustc_span::def_id::{CrateNum, DefId, LocalDefId, LocalModDefId};
28+
use rustc_span::{LocalExpnId, Span};
2129

2230
pub use crate::dep_kind_vtables::make_dep_kind_vtables;
2331
pub use crate::job::{QueryJobMap, break_query_cycles, print_query_stack};

0 commit comments

Comments
 (0)