Skip to content

Commit 462ae37

Browse files
committed
Remove the _cache_on_disk_if_fns module.
One of the modules generated by `rustc_queries` looks like this: ``` mod _cache_on_disk_if_fns { ... #[inline] pub fn const_param_default<'tcx>( _: TyCtxt<'tcx>, param: &crate::queries::const_param_default::Key<'tcx> ) -> bool { param.is_local() } ... ``` Members of this module are used to initialize multiple `QueryVTable` fields. This commit removes the `_cache_on_disk_if_fns` module entirely. For each cacheable query we generate a check closure which is passed as `(cache_on_disk_if { $check_closure }` in the modifiers list. This is similar to how `desc` modifiers are handled.
1 parent 33e27c7 commit 462ae37

7 files changed

Lines changed: 58 additions & 71 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: 17 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -273,38 +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-
cache_on_disk_if_fns_stream: proc_macro2::TokenStream,
284-
}
285-
286-
fn make_helpers_for_query(query: &Query, streams: &mut HelperTokenStreams) {
287-
let Query { name, key_pat, key_ty, modifiers, .. } = &query;
288-
289-
// Replace span for `name` to make rust-analyzer ignore it.
290-
let mut erased_name = name.clone();
291-
erased_name.set_span(Span::call_site());
292-
293-
// Generate a function to check whether we should cache the query to disk, for some key.
294-
if let Some(CacheOnDiskIf { block, .. }) = modifiers.cache_on_disk_if.as_ref() {
295-
// `pass_by_value`: some keys are marked with `rustc_pass_by_value`, but we take keys by
296-
// reference here.
297-
// FIXME: `pass_by_value` is badly named; `allow(rustc::pass_by_value)` actually means
298-
// "allow pass by reference of `rustc_pass_by_value` types".
299-
streams.cache_on_disk_if_fns_stream.extend(quote! {
300-
#[allow(unused_variables, rustc::pass_by_value)]
301-
#[inline]
302-
pub fn #erased_name<'tcx>(tcx: TyCtxt<'tcx>, #key_pat: &#key_ty) -> bool
303-
#block
304-
});
305-
}
306-
}
307-
308276
/// Add hints for rust-analyzer
309277
fn add_to_analyzer_stream(query: &Query, analyzer_stream: &mut proc_macro2::TokenStream) {
310278
// Add links to relevant modifiers
@@ -384,7 +352,6 @@ pub(super) fn rustc_queries(input: TokenStream) -> TokenStream {
384352
let queries = parse_macro_input!(input as List<Query>);
385353

386354
let mut query_stream = quote! {};
387-
let mut helpers = HelperTokenStreams::default();
388355
let mut analyzer_stream = quote! {};
389356
let mut errors = quote! {};
390357

@@ -435,11 +402,23 @@ pub(super) fn rustc_queries(input: TokenStream) -> TokenStream {
435402
return_result_from_ensure_ok,
436403
);
437404

438-
// If there was a `cache_on_disk_if` modifier in the real input, pass
439-
// on a synthetic `(cache_on_disk)` modifier that can be inspected by
440-
// macro-rules macros.
441-
if modifiers.cache_on_disk_if.is_some() {
442-
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+
});
443422
}
444423

445424
// This uses the span of the query definition for the commas,
@@ -472,11 +451,8 @@ pub(super) fn rustc_queries(input: TokenStream) -> TokenStream {
472451
}
473452

474453
add_to_analyzer_stream(&query, &mut analyzer_stream);
475-
make_helpers_for_query(&query, &mut helpers);
476454
}
477455

478-
let HelperTokenStreams { cache_on_disk_if_fns_stream } = helpers;
479-
480456
TokenStream::from(quote! {
481457
/// Higher-order macro that invokes the specified macro with a prepared
482458
/// list of all query signatures (including modifiers).
@@ -506,14 +482,6 @@ pub(super) fn rustc_queries(input: TokenStream) -> TokenStream {
506482
#analyzer_stream
507483
}
508484

509-
// FIXME(Zalathar): Instead of declaring these functions directly, can
510-
// we put them in a macro and then expand that macro downstream in
511-
// `rustc_query_impl`, where the functions are actually used?
512-
pub mod _cache_on_disk_if_fns {
513-
use super::*;
514-
#cache_on_disk_if_fns_stream
515-
}
516-
517485
#errors
518486
})
519487
}

compiler/rustc_middle/src/queries.rs

Lines changed: 0 additions & 1 deletion
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;

compiler/rustc_middle/src/query/plumbing.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,10 @@ pub struct QueryVTable<'tcx, C: QueryCache> {
139139
prev_index: SerializedDepNodeIndex,
140140
index: DepNodeIndex,
141141
) -> Option<C::Value>,
142+
142143
pub is_loadable_from_disk_fn:
143144
fn(tcx: TyCtxt<'tcx>, key: &C::Key, index: SerializedDepNodeIndex) -> bool,
145+
144146
pub hash_result: HashResult<C::Value>,
145147
pub value_from_cycle_error:
146148
fn(tcx: TyCtxt<'tcx>, cycle_error: &CycleError, guar: ErrorGuaranteed) -> C::Value,

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/lib.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +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;
1214
use rustc_hir::def::DefKind;
1315
use rustc_hir::def_id::LOCAL_CRATE;
1416
use rustc_middle::dep_graph;
17+
use rustc_middle::mir::interpret::GlobalId;
18+
use rustc_middle::mir::mono::CollectionMode;
1519
use rustc_middle::queries::{self, ExternProviders, Providers};
1620
use rustc_middle::query::on_disk_cache::{CacheEncoder, EncodedDepNodeIndex, OnDiskCache};
1721
use rustc_middle::query::plumbing::{QuerySystem, QuerySystemFns, QueryVTable};
1822
use rustc_middle::query::{
1923
AsLocalKey, QueryCache, QueryMode, describe_as_module,
2024
};
2125
use rustc_middle::ty::print::PrintTraitRefExt;
22-
use rustc_middle::ty::{self, TyCtxt};
23-
use rustc_span::Span;
26+
use rustc_middle::ty::{self, PseudoCanonicalInput, TyCtxt};
27+
use rustc_span::def_id::{CrateNum, DefId, LocalDefId, LocalModDefId};
28+
use rustc_span::{LocalExpnId, Span};
2429

2530
pub use crate::dep_kind_vtables::make_dep_kind_vtables;
2631
pub use crate::job::{QueryJobMap, break_query_cycles, print_query_stack};

compiler/rustc_query_impl/src/plumbing.rs

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -242,29 +242,42 @@ macro_rules! call_provider {
242242

243243
/// Expands to one of two token trees, depending on whether the current query
244244
/// has the `cache_on_disk_if` modifier.
245-
macro_rules! if_cache_on_disk {
245+
macro_rules! if_cache_on_disk_if {
246246
([] $yes:tt $no:tt) => {
247247
$no
248248
};
249-
// The `cache_on_disk_if` modifier generates a synthetic `(cache_on_disk)`,
250-
// modifier, for use by this macro and similar macros.
251-
([(cache_on_disk) $($rest:tt)*] $yes:tt $no:tt) => {
249+
// Ignore the closure.
250+
([(cache_on_disk_if { $_:expr }) $($rest:tt)*] $yes:tt $no:tt) => {
252251
$yes
253252
};
254253
([$other:tt $($modifiers:tt)*] $yes:tt $no:tt) => {
255-
if_cache_on_disk!([$($modifiers)*] $yes $no)
254+
if_cache_on_disk_if!([$($modifiers)*] $yes $no)
255+
};
256+
}
257+
258+
/// Extract the closure from the `cache_on_disk_if` modifier if present, otherwise generate a
259+
/// trivial failing closure.
260+
macro_rules! cache_on_disk_if_fn {
261+
([] $($item:tt)*) => {
262+
|_tcx, _key| false
263+
};
264+
([(cache_on_disk_if { $cache_closure:expr }) $($rest:tt)*]) => {
265+
$cache_closure
266+
};
267+
([$other:tt $($modifiers:tt)*]) => {
268+
cache_on_disk_if_fn! { [$($modifiers)*] }
256269
};
257270
}
258271

259272
/// Conditionally expands to some token trees, if the current query has the
260273
/// `cache_on_disk_if` modifier.
261-
macro_rules! item_if_cache_on_disk {
274+
macro_rules! item_if_cache_on_disk_if {
262275
([] $($item:tt)*) => {};
263-
([(cache_on_disk) $($rest:tt)*] $($item:tt)*) => {
276+
([(cache_on_disk_if { $_:expr }) $($rest:tt)*] $($item:tt)*) => {
264277
$($item)*
265278
};
266279
([$other:tt $($modifiers:tt)*] $($item:tt)*) => {
267-
item_if_cache_on_disk! { [$($modifiers)*] $($item)* }
280+
item_if_cache_on_disk_if! { [$($modifiers)*] $($item)* }
268281
};
269282
}
270283

@@ -568,21 +581,18 @@ macro_rules! define_queries {
568581
cycle_error_handling: cycle_error_handling!([$($modifiers)*]),
569582
state: Default::default(),
570583
cache: Default::default(),
571-
will_cache_on_disk_for_key_fn: if_cache_on_disk!([$($modifiers)*] {
572-
rustc_middle::queries::_cache_on_disk_if_fns::$name
573-
} {
574-
|_tcx, _key| false
575-
}),
584+
will_cache_on_disk_for_key_fn: cache_on_disk_if_fn!([$($modifiers)*]),
576585
call_query_method_fn: |tcx, key| {
577586
// Call the query method for its side-effect of loading a value
578587
// from disk-cache; the caller doesn't need the value.
579588
let _ = tcx.$name(key);
580589
},
581590
invoke_provider_fn: self::invoke_provider_fn::__rust_begin_short_backtrace,
582-
try_load_from_disk_fn: if_cache_on_disk!([$($modifiers)*] {
591+
try_load_from_disk_fn: if_cache_on_disk_if!([$($modifiers)*] {
583592
|tcx, key, prev_index, index| {
584593
// Check the `cache_on_disk_if` condition for this key.
585-
if !::rustc_middle::queries::_cache_on_disk_if_fns::$name(tcx, &key) {
594+
let cache_on_disk_if_fn = cache_on_disk_if_fn!([$($modifiers)*]);
595+
if !cache_on_disk_if_fn(tcx, key) {
586596
return None;
587597
}
588598

@@ -595,9 +605,10 @@ macro_rules! define_queries {
595605
} {
596606
|_tcx, _key, _prev_index, _index| None
597607
}),
598-
is_loadable_from_disk_fn: if_cache_on_disk!([$($modifiers)*] {
608+
is_loadable_from_disk_fn: if_cache_on_disk_if!([$($modifiers)*] {
599609
|tcx, key, index| -> bool {
600-
::rustc_middle::queries::_cache_on_disk_if_fns::$name(tcx, &key) &&
610+
let cache_on_disk_if_fn = cache_on_disk_if_fn!([$($modifiers)*]);
611+
cache_on_disk_if_fn(tcx, key) &&
601612
$crate::plumbing::loadable_from_disk(tcx, index)
602613
}
603614
} {
@@ -678,7 +689,7 @@ macro_rules! define_queries {
678689
)
679690
}
680691

681-
item_if_cache_on_disk! { [$($modifiers)*]
692+
item_if_cache_on_disk_if! { [$($modifiers)*]
682693
pub(crate) fn encode_query_results<'tcx>(
683694
tcx: TyCtxt<'tcx>,
684695
encoder: &mut CacheEncoder<'_, 'tcx>,
@@ -739,7 +750,7 @@ macro_rules! define_queries {
739750
>
740751
] = &[
741752
$(
742-
if_cache_on_disk!([$($modifiers)*] {
753+
if_cache_on_disk_if!([$($modifiers)*] {
743754
Some(query_impl::$name::encode_query_results)
744755
} {
745756
None

0 commit comments

Comments
 (0)