Skip to content

Commit a5b58ad

Browse files
committed
Auto merge of #101307 - jyn514:simplify-storage, r=cjgillot
Simplify caching and storage for queries I highly recommend reviewing commit-by-commit; each individual commit is quite small but it can be hard to see looking at the overall diff that the behavior is the same. Each commit depends on the previous. r? `@cjgillot`
2 parents 88a1922 + 0a9d7db commit a5b58ad

File tree

7 files changed

+107
-153
lines changed

7 files changed

+107
-153
lines changed

compiler/rustc_macros/src/query.rs

+26-79
Original file line numberDiff line numberDiff line change
@@ -86,14 +86,11 @@ struct QueryModifiers {
8686
desc: (Option<Ident>, Punctuated<Expr, Token![,]>),
8787

8888
/// Use this type for the in-memory cache.
89-
storage: Option<Type>,
89+
arena_cache: Option<Ident>,
9090

9191
/// Cache the query to disk if the `Block` returns true.
9292
cache: Option<(Option<Pat>, Block)>,
9393

94-
/// Custom code to load the query from disk.
95-
load_cached: Option<(Ident, Ident, Block)>,
96-
9794
/// A cycle error for this query aborting the compilation with a fatal error.
9895
fatal_cycle: Option<Ident>,
9996

@@ -120,8 +117,7 @@ struct QueryModifiers {
120117
}
121118

122119
fn parse_query_modifiers(input: ParseStream<'_>) -> Result<QueryModifiers> {
123-
let mut load_cached = None;
124-
let mut storage = None;
120+
let mut arena_cache = None;
125121
let mut cache = None;
126122
let mut desc = None;
127123
let mut fatal_cycle = None;
@@ -173,21 +169,8 @@ fn parse_query_modifiers(input: ParseStream<'_>) -> Result<QueryModifiers> {
173169
};
174170
let block = input.parse()?;
175171
try_insert!(cache = (args, block));
176-
} else if modifier == "load_cached" {
177-
// Parse a load_cached modifier like:
178-
// `load_cached(tcx, id) { tcx.on_disk_cache.try_load_query_result(tcx, id) }`
179-
let args;
180-
parenthesized!(args in input);
181-
let tcx = args.parse()?;
182-
args.parse::<Token![,]>()?;
183-
let id = args.parse()?;
184-
let block = input.parse()?;
185-
try_insert!(load_cached = (tcx, id, block));
186-
} else if modifier == "storage" {
187-
let args;
188-
parenthesized!(args in input);
189-
let ty = args.parse()?;
190-
try_insert!(storage = ty);
172+
} else if modifier == "arena_cache" {
173+
try_insert!(arena_cache = modifier);
191174
} else if modifier == "fatal_cycle" {
192175
try_insert!(fatal_cycle = modifier);
193176
} else if modifier == "cycle_delay_bug" {
@@ -212,8 +195,7 @@ fn parse_query_modifiers(input: ParseStream<'_>) -> Result<QueryModifiers> {
212195
return Err(input.error("no description provided"));
213196
};
214197
Ok(QueryModifiers {
215-
load_cached,
216-
storage,
198+
arena_cache,
217199
cache,
218200
desc,
219201
fatal_cycle,
@@ -262,20 +244,6 @@ fn add_query_description_impl(query: &Query, impls: &mut proc_macro2::TokenStrea
262244

263245
// Find out if we should cache the query on disk
264246
let cache = if let Some((args, expr)) = modifiers.cache.as_ref() {
265-
let try_load_from_disk = if let Some((tcx, id, block)) = modifiers.load_cached.as_ref() {
266-
// Use custom code to load the query from disk
267-
quote! {
268-
const TRY_LOAD_FROM_DISK: Option<fn(QueryCtxt<'tcx>, SerializedDepNodeIndex) -> Option<Self::Value>>
269-
= Some(|#tcx, #id| { #block });
270-
}
271-
} else {
272-
// Use the default code to load the query from disk
273-
quote! {
274-
const TRY_LOAD_FROM_DISK: Option<fn(QueryCtxt<'tcx>, SerializedDepNodeIndex) -> Option<Self::Value>>
275-
= Some(|tcx, id| tcx.on_disk_cache().as_ref()?.try_load_query_result(*tcx, id));
276-
}
277-
};
278-
279247
let tcx = args.as_ref().map(|t| quote! { #t }).unwrap_or_else(|| quote! { _ });
280248
// expr is a `Block`, meaning that `{ #expr }` gets expanded
281249
// to `{ { stmts... } }`, which triggers the `unused_braces` lint.
@@ -285,20 +253,13 @@ fn add_query_description_impl(query: &Query, impls: &mut proc_macro2::TokenStrea
285253
fn cache_on_disk(#tcx: TyCtxt<'tcx>, #key: &Self::Key) -> bool {
286254
#expr
287255
}
288-
289-
#try_load_from_disk
290256
}
291257
} else {
292-
if modifiers.load_cached.is_some() {
293-
panic!("load_cached modifier on query `{}` without a cache modifier", name);
294-
}
295258
quote! {
296259
#[inline]
297260
fn cache_on_disk(_: TyCtxt<'tcx>, _: &Self::Key) -> bool {
298261
false
299262
}
300-
301-
const TRY_LOAD_FROM_DISK: Option<fn(QueryCtxt<'tcx>, SerializedDepNodeIndex) -> Option<Self::Value>> = None;
302263
}
303264
};
304265

@@ -347,42 +308,28 @@ pub fn rustc_queries(input: TokenStream) -> TokenStream {
347308

348309
let mut attributes = Vec::new();
349310

350-
// Pass on the fatal_cycle modifier
351-
if let Some(fatal_cycle) = &modifiers.fatal_cycle {
352-
attributes.push(quote! { (#fatal_cycle) });
353-
};
354-
// Pass on the storage modifier
355-
if let Some(ref ty) = modifiers.storage {
356-
let span = ty.span();
357-
attributes.push(quote_spanned! {span=> (storage #ty) });
358-
};
359-
// Pass on the cycle_delay_bug modifier
360-
if let Some(cycle_delay_bug) = &modifiers.cycle_delay_bug {
361-
attributes.push(quote! { (#cycle_delay_bug) });
362-
};
363-
// Pass on the no_hash modifier
364-
if let Some(no_hash) = &modifiers.no_hash {
365-
attributes.push(quote! { (#no_hash) });
366-
};
367-
// Pass on the anon modifier
368-
if let Some(anon) = &modifiers.anon {
369-
attributes.push(quote! { (#anon) });
370-
};
371-
// Pass on the eval_always modifier
372-
if let Some(eval_always) = &modifiers.eval_always {
373-
attributes.push(quote! { (#eval_always) });
374-
};
375-
// Pass on the depth_limit modifier
376-
if let Some(depth_limit) = &modifiers.depth_limit {
377-
attributes.push(quote! { (#depth_limit) });
378-
};
379-
// Pass on the separate_provide_extern modifier
380-
if let Some(separate_provide_extern) = &modifiers.separate_provide_extern {
381-
attributes.push(quote! { (#separate_provide_extern) });
311+
macro_rules! passthrough {
312+
( $( $modifier:ident ),+ $(,)? ) => {
313+
$( if let Some($modifier) = &modifiers.$modifier {
314+
attributes.push(quote! { (#$modifier) });
315+
}; )+
316+
}
382317
}
383-
// Pass on the remap_env_constness modifier
384-
if let Some(remap_env_constness) = &modifiers.remap_env_constness {
385-
attributes.push(quote! { (#remap_env_constness) });
318+
319+
passthrough!(
320+
fatal_cycle,
321+
arena_cache,
322+
cycle_delay_bug,
323+
no_hash,
324+
anon,
325+
eval_always,
326+
depth_limit,
327+
separate_provide_extern,
328+
remap_env_constness,
329+
);
330+
331+
if modifiers.cache.is_some() {
332+
attributes.push(quote! { (cache) });
386333
}
387334

388335
// This uses the span of the query definition for the commas,

0 commit comments

Comments
 (0)