Skip to content

Commit cf46836

Browse files
authored
Rollup merge of rust-lang#72823 - matthewjasper:describe-queries, r=eddyb
Add descriptions for all queries This also removes the default description for queries with DefId keys and makes the macro validate that a description is provided. cc rust-lang#72730 r? @eddyb
2 parents 2e3417a + 8894bd2 commit cf46836

19 files changed

+257
-174
lines changed

src/librustc_macros/src/query.rs

+28-29
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ impl Parse for Group {
199199

200200
struct QueryModifiers {
201201
/// The description of the query.
202-
desc: Option<(Option<Ident>, Punctuated<Expr, Token![,]>)>,
202+
desc: (Option<Ident>, Punctuated<Expr, Token![,]>),
203203

204204
/// Use this type for the in-memory cache.
205205
storage: Option<Type>,
@@ -295,6 +295,9 @@ fn process_modifiers(query: &mut Query) -> QueryModifiers {
295295
}
296296
}
297297
}
298+
let desc = desc.unwrap_or_else(|| {
299+
panic!("no description provided for query `{}`", query.name);
300+
});
298301
QueryModifiers {
299302
load_cached,
300303
storage,
@@ -319,7 +322,7 @@ fn add_query_description_impl(
319322
let key = &query.key.0;
320323

321324
// Find out if we should cache the query on disk
322-
let cache = modifiers.cache.as_ref().map(|(args, expr)| {
325+
let cache = if let Some((args, expr)) = modifiers.cache.as_ref() {
323326
let try_load_from_disk = if let Some((tcx, id, block)) = modifiers.load_cached.as_ref() {
324327
// Use custom code to load the query from disk
325328
quote! {
@@ -373,36 +376,32 @@ fn add_query_description_impl(
373376

374377
#try_load_from_disk
375378
}
376-
});
377-
378-
if cache.is_none() && modifiers.load_cached.is_some() {
379-
panic!("load_cached modifier on query `{}` without a cache modifier", name);
380-
}
379+
} else {
380+
if modifiers.load_cached.is_some() {
381+
panic!("load_cached modifier on query `{}` without a cache modifier", name);
382+
}
383+
quote! {}
384+
};
385+
386+
let (tcx, desc) = modifiers.desc;
387+
let tcx = tcx.as_ref().map(|t| quote! { #t }).unwrap_or(quote! { _ });
388+
389+
let desc = quote! {
390+
#[allow(unused_variables)]
391+
fn describe(
392+
#tcx: TyCtxt<'tcx>,
393+
#key: #arg,
394+
) -> Cow<'static, str> {
395+
format!(#desc).into()
396+
}
397+
};
381398

382-
let desc = modifiers.desc.as_ref().map(|(tcx, desc)| {
383-
let tcx = tcx.as_ref().map(|t| quote! { #t }).unwrap_or(quote! { _ });
384-
quote! {
385-
#[allow(unused_variables)]
386-
fn describe(
387-
#tcx: TyCtxt<'tcx>,
388-
#key: #arg,
389-
) -> Cow<'static, str> {
390-
format!(#desc).into()
391-
}
399+
impls.extend(quote! {
400+
impl<'tcx> QueryDescription<TyCtxt<'tcx>> for queries::#name<'tcx> {
401+
#desc
402+
#cache
392403
}
393404
});
394-
395-
if desc.is_some() || cache.is_some() {
396-
let cache = cache.unwrap_or(quote! {});
397-
let desc = desc.unwrap_or(quote! {});
398-
399-
impls.extend(quote! {
400-
impl<'tcx> QueryDescription<TyCtxt<'tcx>> for queries::#name<'tcx> {
401-
#desc
402-
#cache
403-
}
404-
});
405-
}
406405
}
407406

408407
pub fn rustc_queries(input: TokenStream) -> TokenStream {

0 commit comments

Comments
 (0)