mirror of
https://github.com/rust-lang/rust.git
synced 2024-11-22 06:44:35 +00:00
Get rid of rustc_query_description!
Queries can provide an arbitrary expression for their description and their caching behavior. Before, these expressions where stored in a `rustc_query_description` macro emitted by the `rustc_queries` macro, and then used in `rustc_query_impl` to fill out the methods for the `QueryDescription` trait. Instead, we now emit two new modules from `rustc_queries` containing the functions with the expressions. `rustc_query_impl` calls these functions now instead of invoking the macro. Since we are now defining some of the functions in `rustc_middle::query`, we now need all the imports for the key types there as well.
This commit is contained in:
parent
1566273f48
commit
167b3bd3b2
@ -237,27 +237,32 @@ fn doc_comment_from_desc(list: &Punctuated<Expr, token::Comma>) -> Result<Attrib
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Add the impl of QueryDescription for the query to `impls` if one is requested
|
/// Add the impl of QueryDescription for the query to `impls` if one is requested
|
||||||
fn add_query_description_impl(query: &Query, impls: &mut proc_macro2::TokenStream) {
|
fn add_query_desc_cached_impl(
|
||||||
let name = &query.name;
|
query: &Query,
|
||||||
let key = &query.key;
|
descs: &mut proc_macro2::TokenStream,
|
||||||
let modifiers = &query.modifiers;
|
cached: &mut proc_macro2::TokenStream,
|
||||||
|
) {
|
||||||
|
let Query { name, key, modifiers, .. } = &query;
|
||||||
|
|
||||||
// Find out if we should cache the query on disk
|
// Find out if we should cache the query on disk
|
||||||
let cache = if let Some((args, expr)) = modifiers.cache.as_ref() {
|
let cache = if let Some((args, expr)) = modifiers.cache.as_ref() {
|
||||||
let tcx = args.as_ref().map(|t| quote! { #t }).unwrap_or_else(|| quote! { _ });
|
let tcx = args.as_ref().map(|t| quote! { #t }).unwrap_or_else(|| quote! { _ });
|
||||||
// expr is a `Block`, meaning that `{ #expr }` gets expanded
|
// expr is a `Block`, meaning that `{ #expr }` gets expanded
|
||||||
// to `{ { stmts... } }`, which triggers the `unused_braces` lint.
|
// to `{ { stmts... } }`, which triggers the `unused_braces` lint.
|
||||||
|
// we're taking `key` by reference, but some rustc types usually prefer being passed by value
|
||||||
quote! {
|
quote! {
|
||||||
#[allow(unused_variables, unused_braces)]
|
#[allow(unused_variables, unused_braces, rustc::pass_by_value)]
|
||||||
#[inline]
|
#[inline]
|
||||||
fn cache_on_disk(#tcx: TyCtxt<'tcx>, #key: &Self::Key) -> bool {
|
pub fn #name<'tcx>(#tcx: TyCtxt<'tcx>, #key: &crate::ty::query::query_keys::#name<'tcx>) -> bool {
|
||||||
#expr
|
#expr
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
quote! {
|
quote! {
|
||||||
|
// we're taking `key` by reference, but some rustc types usually prefer being passed by value
|
||||||
|
#[allow(rustc::pass_by_value)]
|
||||||
#[inline]
|
#[inline]
|
||||||
fn cache_on_disk(_: TyCtxt<'tcx>, _: &Self::Key) -> bool {
|
pub fn #name<'tcx>(_: TyCtxt<'tcx>, _: &crate::ty::query::query_keys::#name<'tcx>) -> bool {
|
||||||
false
|
false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -268,19 +273,20 @@ fn add_query_description_impl(query: &Query, impls: &mut proc_macro2::TokenStrea
|
|||||||
|
|
||||||
let desc = quote! {
|
let desc = quote! {
|
||||||
#[allow(unused_variables)]
|
#[allow(unused_variables)]
|
||||||
fn describe(tcx: QueryCtxt<'tcx>, key: Self::Key) -> String {
|
pub fn #name<'tcx>(tcx: TyCtxt<'tcx>, key: crate::ty::query::query_keys::#name<'tcx>) -> String {
|
||||||
let (#tcx, #key) = (*tcx, key);
|
let (#tcx, #key) = (tcx, key);
|
||||||
::rustc_middle::ty::print::with_no_trimmed_paths!(
|
::rustc_middle::ty::print::with_no_trimmed_paths!(
|
||||||
format!(#desc)
|
format!(#desc)
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
impls.extend(quote! {
|
descs.extend(quote! {
|
||||||
(#name) => {
|
#desc
|
||||||
#desc
|
});
|
||||||
#cache
|
|
||||||
};
|
cached.extend(quote! {
|
||||||
|
#cache
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -289,6 +295,7 @@ pub fn rustc_queries(input: TokenStream) -> TokenStream {
|
|||||||
|
|
||||||
let mut query_stream = quote! {};
|
let mut query_stream = quote! {};
|
||||||
let mut query_description_stream = quote! {};
|
let mut query_description_stream = quote! {};
|
||||||
|
let mut query_cached_stream = quote! {};
|
||||||
|
|
||||||
for query in queries.0 {
|
for query in queries.0 {
|
||||||
let Query { name, arg, modifiers, .. } = &query;
|
let Query { name, arg, modifiers, .. } = &query;
|
||||||
@ -343,7 +350,7 @@ pub fn rustc_queries(input: TokenStream) -> TokenStream {
|
|||||||
[#attribute_stream] fn #name(#arg) #result,
|
[#attribute_stream] fn #name(#arg) #result,
|
||||||
});
|
});
|
||||||
|
|
||||||
add_query_description_impl(&query, &mut query_description_stream);
|
add_query_desc_cached_impl(&query, &mut query_description_stream, &mut query_cached_stream);
|
||||||
}
|
}
|
||||||
|
|
||||||
TokenStream::from(quote! {
|
TokenStream::from(quote! {
|
||||||
@ -357,9 +364,13 @@ pub fn rustc_queries(input: TokenStream) -> TokenStream {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[macro_export]
|
pub mod descs {
|
||||||
macro_rules! rustc_query_description {
|
use super::*;
|
||||||
#query_description_stream
|
#query_description_stream
|
||||||
}
|
}
|
||||||
|
pub mod cached {
|
||||||
|
use super::*;
|
||||||
|
#query_cached_stream
|
||||||
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -4,6 +4,9 @@
|
|||||||
//! ["Queries: demand-driven compilation"](https://rustc-dev-guide.rust-lang.org/query.html).
|
//! ["Queries: demand-driven compilation"](https://rustc-dev-guide.rust-lang.org/query.html).
|
||||||
//! This chapter includes instructions for adding new queries.
|
//! This chapter includes instructions for adding new queries.
|
||||||
|
|
||||||
|
use crate::ty::{self, print::describe_as_module, TyCtxt};
|
||||||
|
use rustc_span::def_id::LOCAL_CRATE;
|
||||||
|
|
||||||
// Each of these queries corresponds to a function pointer field in the
|
// Each of these queries corresponds to a function pointer field in the
|
||||||
// `Providers` struct for requesting a value of that type, and a method
|
// `Providers` struct for requesting a value of that type, and a method
|
||||||
// on `tcx: TyCtxt` (and `tcx.at(span)`) for doing that request in a way
|
// on `tcx: TyCtxt` (and `tcx.at(span)`) for doing that request in a way
|
||||||
@ -1214,7 +1217,7 @@ rustc_queries! {
|
|||||||
desc { |tcx| "finding all vtable entries for trait {}", tcx.def_path_str(key.def_id()) }
|
desc { |tcx| "finding all vtable entries for trait {}", tcx.def_path_str(key.def_id()) }
|
||||||
}
|
}
|
||||||
|
|
||||||
query vtable_trait_upcasting_coercion_new_vptr_slot(key: (ty::Ty<'tcx>, ty::Ty<'tcx>)) -> Option<usize> {
|
query vtable_trait_upcasting_coercion_new_vptr_slot(key: (Ty<'tcx>, Ty<'tcx>)) -> Option<usize> {
|
||||||
desc { |tcx| "finding the slot within vtable for trait object {} vtable ptr during trait upcasting coercion from {} vtable",
|
desc { |tcx| "finding the slot within vtable for trait object {} vtable ptr during trait upcasting coercion from {} vtable",
|
||||||
key.1, key.0 }
|
key.1, key.0 }
|
||||||
}
|
}
|
||||||
|
@ -3,7 +3,7 @@ use crate::ty::{self, DefIdTree, Ty, TyCtxt};
|
|||||||
|
|
||||||
use rustc_data_structures::fx::FxHashSet;
|
use rustc_data_structures::fx::FxHashSet;
|
||||||
use rustc_data_structures::sso::SsoHashSet;
|
use rustc_data_structures::sso::SsoHashSet;
|
||||||
use rustc_hir::def_id::{CrateNum, DefId};
|
use rustc_hir::def_id::{CrateNum, DefId, LocalDefId};
|
||||||
use rustc_hir::definitions::{DefPathData, DisambiguatedDefPathData};
|
use rustc_hir::definitions::{DefPathData, DisambiguatedDefPathData};
|
||||||
|
|
||||||
// `pretty` is a separate module only for organization.
|
// `pretty` is a separate module only for organization.
|
||||||
@ -325,3 +325,12 @@ impl<'tcx, P: Printer<'tcx>> Print<'tcx, P> for ty::Const<'tcx> {
|
|||||||
cx.print_const(*self)
|
cx.print_const(*self)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// This is only used by query descriptions
|
||||||
|
pub fn describe_as_module(def_id: LocalDefId, tcx: TyCtxt<'_>) -> String {
|
||||||
|
if def_id.is_top_level_module() {
|
||||||
|
"top-level module".to_string()
|
||||||
|
} else {
|
||||||
|
format!("module `{}`", tcx.def_path_str(def_id.to_def_id()))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -22,8 +22,7 @@ use rustc_middle::arena::Arena;
|
|||||||
use rustc_middle::dep_graph::{self, DepKindStruct};
|
use rustc_middle::dep_graph::{self, DepKindStruct};
|
||||||
use rustc_middle::ty::query::{query_keys, query_storage, query_stored, query_values};
|
use rustc_middle::ty::query::{query_keys, query_storage, query_stored, query_values};
|
||||||
use rustc_middle::ty::query::{ExternProviders, Providers, QueryEngine};
|
use rustc_middle::ty::query::{ExternProviders, Providers, QueryEngine};
|
||||||
use rustc_middle::ty::{self, TyCtxt};
|
use rustc_middle::ty::TyCtxt;
|
||||||
use rustc_span::def_id::{LocalDefId, LOCAL_CRATE};
|
|
||||||
use rustc_span::Span;
|
use rustc_span::Span;
|
||||||
|
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
@ -45,14 +44,6 @@ pub use on_disk_cache::OnDiskCache;
|
|||||||
mod profiling_support;
|
mod profiling_support;
|
||||||
pub use self::profiling_support::alloc_self_profile_query_strings;
|
pub use self::profiling_support::alloc_self_profile_query_strings;
|
||||||
|
|
||||||
fn describe_as_module(def_id: LocalDefId, tcx: TyCtxt<'_>) -> String {
|
|
||||||
if def_id.is_top_level_module() {
|
|
||||||
"top-level module".to_string()
|
|
||||||
} else {
|
|
||||||
format!("module `{}`", tcx.def_path_str(def_id.to_def_id()))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
rustc_query_append! { define_queries! }
|
rustc_query_append! { define_queries! }
|
||||||
|
|
||||||
impl<'tcx> Queries<'tcx> {
|
impl<'tcx> Queries<'tcx> {
|
||||||
|
@ -466,7 +466,14 @@ macro_rules! define_queries {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<'tcx> QueryDescription<QueryCtxt<'tcx>> for queries::$name<'tcx> {
|
impl<'tcx> QueryDescription<QueryCtxt<'tcx>> for queries::$name<'tcx> {
|
||||||
rustc_query_description! { $name }
|
fn describe(tcx: QueryCtxt<'tcx>, key: Self::Key) -> String {
|
||||||
|
::rustc_middle::query::descs::$name(tcx.tcx, key)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn cache_on_disk(tcx: TyCtxt<'tcx>, key: &Self::Key) -> bool {
|
||||||
|
::rustc_middle::query::cached::$name(tcx, key)
|
||||||
|
}
|
||||||
|
|
||||||
type Cache = query_storage::$name<'tcx>;
|
type Cache = query_storage::$name<'tcx>;
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user