Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 6 additions & 27 deletions compiler/rustc_macros/src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,6 @@ fn doc_comment_from_desc(list: &Punctuated<Expr, token::Comma>) -> Result<Attrib
/// and expect to be interpolated into a dedicated module.
#[derive(Default)]
struct HelperTokenStreams {
description_fns_stream: proc_macro2::TokenStream,
cache_on_disk_if_fns_stream: proc_macro2::TokenStream,
}

Expand All @@ -304,19 +303,6 @@ fn make_helpers_for_query(query: &Query, streams: &mut HelperTokenStreams) {
#block
});
}

let Desc { expr_list, .. } = &modifiers.desc;

let desc = quote! {
#[allow(unused_variables)]
pub fn #erased_name<'tcx>(tcx: TyCtxt<'tcx>, #key_pat: #key_ty) -> String {
format!(#expr_list)
}
};

streams.description_fns_stream.extend(quote! {
#desc
});
}

/// Add hints for rust-analyzer
Expand Down Expand Up @@ -413,7 +399,7 @@ pub(super) fn rustc_queries(input: TokenStream) -> TokenStream {
}

for query in queries.0 {
let Query { doc_comments, name, key_ty, return_ty, modifiers, .. } = &query;
let Query { doc_comments, name, key_pat, key_ty, return_ty, modifiers, .. } = &query;

// Normalize an absent return type into `-> ()` to make macro-rules parsing easier.
let return_ty = match return_ty {
Expand All @@ -431,6 +417,10 @@ pub(super) fn rustc_queries(input: TokenStream) -> TokenStream {
}
}

// Put a description closure inside the `desc` modifier: `(desc { <closure> })`.
let expr_list = &modifiers.desc.expr_list;
modifiers_out.push(quote! { (desc { |tcx, #key_pat| format!(#expr_list) }) });

passthrough!(
arena_cache,
cycle_fatal,
Expand Down Expand Up @@ -485,7 +475,7 @@ pub(super) fn rustc_queries(input: TokenStream) -> TokenStream {
make_helpers_for_query(&query, &mut helpers);
}

let HelperTokenStreams { description_fns_stream, cache_on_disk_if_fns_stream } = helpers;
let HelperTokenStreams { cache_on_disk_if_fns_stream } = helpers;

TokenStream::from(quote! {
/// Higher-order macro that invokes the specified macro with a prepared
Expand Down Expand Up @@ -516,17 +506,6 @@ pub(super) fn rustc_queries(input: TokenStream) -> TokenStream {
#analyzer_stream
}

/// Functions that format a human-readable description of each query
/// and its key, as specified by the `desc` query modifier.
///
/// (The leading `_` avoids collisions with actual query names when
/// expanded in `rustc_middle::queries`, and makes this macro-generated
/// module easier to search for.)
pub mod _description_fns {
use super::*;
#description_fns_stream
}

// FIXME(Zalathar): Instead of declaring these functions directly, can
// we put them in a macro and then expand that macro downstream in
// `rustc_query_impl`, where the functions are actually used?
Expand Down
2 changes: 0 additions & 2 deletions compiler/rustc_middle/src/queries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,6 @@ use crate::mir::interpret::{
use crate::mir::mono::{
CodegenUnit, CollectionMode, MonoItem, MonoItemPartitions, NormalizationErrorInMono,
};
use crate::query::describe_as_module;
use crate::query::plumbing::CyclePlaceholder;
use crate::traits::query::{
CanonicalAliasGoal, CanonicalDropckOutlivesGoal, CanonicalImpliedOutlivesBoundsGoal,
Expand All @@ -135,7 +134,6 @@ use crate::traits::{
};
use crate::ty::fast_reject::SimplifiedType;
use crate::ty::layout::ValidityRequirement;
use crate::ty::print::PrintTraitRefExt;
use crate::ty::util::AlwaysRequiresDrop;
use crate::ty::{
self, CrateInherentImpls, GenericArg, GenericArgsRef, LitToConstInput, PseudoCanonicalInput,
Expand Down
9 changes: 7 additions & 2 deletions compiler/rustc_query_impl/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,17 @@
// tidy-alphabetical-end

use rustc_data_structures::sync::AtomicU64;
use rustc_hir::def::DefKind;
use rustc_hir::def_id::LOCAL_CRATE;
use rustc_middle::dep_graph;
use rustc_middle::queries::{self, ExternProviders, Providers};
use rustc_middle::query::on_disk_cache::{CacheEncoder, EncodedDepNodeIndex, OnDiskCache};
use rustc_middle::query::plumbing::{QuerySystem, QuerySystemFns, QueryVTable};
use rustc_middle::query::{AsLocalKey, QueryCache, QueryMode};
use rustc_middle::ty::TyCtxt;
use rustc_middle::query::{
AsLocalKey, QueryCache, QueryMode, describe_as_module,
};
use rustc_middle::ty::print::PrintTraitRefExt;
use rustc_middle::ty::{self, TyCtxt};
use rustc_span::Span;

pub use crate::dep_kind_vtables::make_dep_kind_vtables;
Expand Down
15 changes: 12 additions & 3 deletions compiler/rustc_query_impl/src/plumbing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,13 @@ macro_rules! item_if_cache_on_disk {
};
}

/// Extract the description closure from the `desc` modifier, which must be present.
macro_rules! desc_fn {
([] $($item:tt)*) => { compile_error!("query missing `desc`") };
([(desc { $desc_fn_closure:expr }) $($rest:tt)*]) => { $desc_fn_closure };
([$other:tt $($modifiers:tt)*]) => { desc_fn! { [$($modifiers)*] } };
}

/// The deferred part of a deferred query stack frame.
fn mk_query_stack_frame_extra<'tcx, Cache>(
(tcx, vtable, key): (TyCtxt<'tcx>, &'tcx QueryVTable<'tcx, Cache>, Cache::Key),
Expand Down Expand Up @@ -597,12 +604,14 @@ macro_rules! define_queries {
None
}),
value_from_cycle_error: |tcx, cycle, guar| {
let result: queries::$name::Value<'tcx> = Value::from_cycle_error(tcx, cycle, guar);
let result: queries::$name::Value<'tcx> =
Value::from_cycle_error(tcx, cycle, guar);
erase::erase_val(result)
},
hash_result: hash_result!([$($modifiers)*][queries::$name::Value<'tcx>]),
format_value: |value| format!("{:?}", erase::restore_val::<queries::$name::Value<'tcx>>(*value)),
description_fn: $crate::queries::_description_fns::$name,
format_value: |value|
format!("{:?}", erase::restore_val::<queries::$name::Value<'tcx>>(*value)),
description_fn: desc_fn!([$($modifiers)*]),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To me, this seems like a clear downgrade in understandability.

Instead of seeing a reference to a _description_fns module that I can search for, all I see is an opaque desc_fn! call.

And if I look at the definition of that helper macro, it gives very little indication of what's happening. At least the other helper macros can eventually be understood as a necessary idiom for checking a boolean condition.

In order to understand what's going on here, I have to know every part of what the proc macro is doing, and what the smuggling mechanism is doing. And it's harder to chase down that chain, because there are fewer clues or landmarks along the way.

(I personally do understand, but I'm trying to put myself in the shoes of someone seeing this for the first time. To me, this is the kind of complexity and indirection that I found very frustrating when trying to unravel the query plumbing macros.)

execute_query_fn: if incremental {
query_impl::$name::get_query_incr::__rust_end_short_backtrace
} else {
Expand Down