Skip to content

Commit

Permalink
Rollup merge of rust-lang#72823 - matthewjasper:describe-queries, r=e…
Browse files Browse the repository at this point in the history
…ddyb

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
  • Loading branch information
Dylan-DPC authored Jun 1, 2020
2 parents 2e3417a + 8894bd2 commit cf46836
Show file tree
Hide file tree
Showing 19 changed files with 257 additions and 174 deletions.
57 changes: 28 additions & 29 deletions src/librustc_macros/src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ impl Parse for Group {

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

/// Use this type for the in-memory cache.
storage: Option<Type>,
Expand Down Expand Up @@ -295,6 +295,9 @@ fn process_modifiers(query: &mut Query) -> QueryModifiers {
}
}
}
let desc = desc.unwrap_or_else(|| {
panic!("no description provided for query `{}`", query.name);
});
QueryModifiers {
load_cached,
storage,
Expand All @@ -319,7 +322,7 @@ fn add_query_description_impl(
let key = &query.key.0;

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

#try_load_from_disk
}
});

if cache.is_none() && modifiers.load_cached.is_some() {
panic!("load_cached modifier on query `{}` without a cache modifier", name);
}
} else {
if modifiers.load_cached.is_some() {
panic!("load_cached modifier on query `{}` without a cache modifier", name);
}
quote! {}
};

let (tcx, desc) = modifiers.desc;
let tcx = tcx.as_ref().map(|t| quote! { #t }).unwrap_or(quote! { _ });

let desc = quote! {
#[allow(unused_variables)]
fn describe(
#tcx: TyCtxt<'tcx>,
#key: #arg,
) -> Cow<'static, str> {
format!(#desc).into()
}
};

let desc = modifiers.desc.as_ref().map(|(tcx, desc)| {
let tcx = tcx.as_ref().map(|t| quote! { #t }).unwrap_or(quote! { _ });
quote! {
#[allow(unused_variables)]
fn describe(
#tcx: TyCtxt<'tcx>,
#key: #arg,
) -> Cow<'static, str> {
format!(#desc).into()
}
impls.extend(quote! {
impl<'tcx> QueryDescription<TyCtxt<'tcx>> for queries::#name<'tcx> {
#desc
#cache
}
});

if desc.is_some() || cache.is_some() {
let cache = cache.unwrap_or(quote! {});
let desc = desc.unwrap_or(quote! {});

impls.extend(quote! {
impl<'tcx> QueryDescription<TyCtxt<'tcx>> for queries::#name<'tcx> {
#desc
#cache
}
});
}
}

pub fn rustc_queries(input: TokenStream) -> TokenStream {
Expand Down
Loading

0 comments on commit cf46836

Please sign in to comment.