Skip to content

Commit 294f0ee

Browse files
committed
Auto merge of rust-lang#101173 - jyn514:simplify-macro-arguments, r=cjgillot
Further simplify the macros generated by `rustc_queries` This doesn't actually move anything outside the macros, but it makes them simpler to read. - Add a new `rustc_query_names` macro. This allows a much simpler syntax for the matchers in the macros passed to it as a callback. - Convert `define_dep_nodes` and `alloc_once` to use `rustc_query_names`. This is possible because they only use the names (despite the quite complicated matchers in `define_dep_nodes`, none of the other arguments are used). - Get rid of `rustc_dep_node_append`. r? `@cjgillot`
2 parents 00fcc82 + cb2949e commit 294f0ee

File tree

4 files changed

+40
-52
lines changed

4 files changed

+40
-52
lines changed

Diff for: compiler/rustc_macros/src/query.rs

+7-22
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,6 @@ pub fn rustc_queries(input: TokenStream) -> TokenStream {
289289

290290
let mut query_stream = quote! {};
291291
let mut query_description_stream = quote! {};
292-
let mut dep_node_def_stream = quote! {};
293292
let mut cached_queries = quote! {};
294293

295294
for query in queries.0 {
@@ -328,6 +327,10 @@ pub fn rustc_queries(input: TokenStream) -> TokenStream {
328327
remap_env_constness,
329328
);
330329

330+
if modifiers.cache.is_some() {
331+
attributes.push(quote! { (cache) });
332+
}
333+
// Pass on the cache modifier
331334
if modifiers.cache.is_some() {
332335
attributes.push(quote! { (cache) });
333336
}
@@ -340,45 +343,27 @@ pub fn rustc_queries(input: TokenStream) -> TokenStream {
340343
// be very useful.
341344
let span = name.span();
342345
let attribute_stream = quote_spanned! {span=> #(#attributes),*};
343-
let doc_comments = query.doc_comments.iter();
346+
let doc_comments = &query.doc_comments;
344347
// Add the query to the group
345348
query_stream.extend(quote! {
346349
#(#doc_comments)*
347350
[#attribute_stream] fn #name(#arg) #result,
348351
});
349352

350-
// Create a dep node for the query
351-
dep_node_def_stream.extend(quote! {
352-
[#attribute_stream] #name(#arg),
353-
});
354-
355353
add_query_description_impl(&query, &mut query_description_stream);
356354
}
357355

358356
TokenStream::from(quote! {
359357
#[macro_export]
360358
macro_rules! rustc_query_append {
361-
($macro:ident !) => {
359+
($macro:ident! $( [$($other:tt)*] )?) => {
362360
$macro! {
361+
$( $($other)* )?
363362
#query_stream
364363
}
365364
}
366365
}
367-
macro_rules! rustc_dep_node_append {
368-
($macro:ident! [$($other:tt)*]) => {
369-
$macro!(
370-
$($other)*
371366

372-
#dep_node_def_stream
373-
);
374-
}
375-
}
376-
#[macro_export]
377-
macro_rules! rustc_cached_queries {
378-
( $macro:ident! ) => {
379-
$macro!(#cached_queries);
380-
}
381-
}
382367
#[macro_export]
383368
macro_rules! rustc_query_description {
384369
#query_description_stream

Diff for: compiler/rustc_middle/src/dep_graph/dep_node.rs

+13-22
Original file line numberDiff line numberDiff line change
@@ -144,11 +144,9 @@ impl DepKind {
144144

145145
macro_rules! define_dep_nodes {
146146
(
147-
$(
148-
[$($attrs:tt)*]
149-
$variant:ident $(( $tuple_arg_ty:ty $(,)? ))*
150-
,)*
151-
) => (
147+
$($(#[$attr:meta])*
148+
[$($modifiers:tt)*] fn $variant:ident($($K:tt)*) -> $V:ty,)*) => {
149+
152150
#[macro_export]
153151
macro_rules! make_dep_kind_array {
154152
($mod:ident) => {[ $($mod::$variant()),* ]};
@@ -158,7 +156,7 @@ macro_rules! define_dep_nodes {
158156
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Encodable, Decodable)]
159157
#[allow(non_camel_case_types)]
160158
pub enum DepKind {
161-
$($variant),*
159+
$( $( #[$attr] )* $variant),*
162160
}
163161

164162
fn dep_kind_from_label_string(label: &str) -> Result<DepKind, ()> {
@@ -176,24 +174,17 @@ macro_rules! define_dep_nodes {
176174
pub const $variant: &str = stringify!($variant);
177175
)*
178176
}
179-
);
177+
};
180178
}
181179

182-
rustc_dep_node_append!(define_dep_nodes![
183-
// We use this for most things when incr. comp. is turned off.
184-
[] Null,
185-
186-
// We use this to create a forever-red node.
187-
[] Red,
188-
189-
[anon] TraitSelect,
190-
191-
// WARNING: if `Symbol` is changed, make sure you update `make_compile_codegen_unit` below.
192-
[] CompileCodegenUnit(Symbol),
193-
194-
// WARNING: if `MonoItem` is changed, make sure you update `make_compile_mono_item` below.
195-
// Only used by rustc_codegen_cranelift
196-
[] CompileMonoItem(MonoItem),
180+
rustc_query_append!(define_dep_nodes![
181+
/// We use this for most things when incr. comp. is turned off.
182+
[] fn Null() -> (),
183+
/// We use this to create a forever-red node.
184+
[] fn Red() -> (),
185+
[] fn TraitSelect() -> (),
186+
[] fn CompileCodegenUnit() -> (),
187+
[] fn CompileMonoItem() -> (),
197188
]);
198189

199190
// WARNING: `construct` is generic and does not know that `CompileCodegenUnit` takes `Symbol`s as keys.

Diff for: compiler/rustc_query_impl/src/plumbing.rs

+16-4
Original file line numberDiff line numberDiff line change
@@ -151,19 +151,31 @@ impl<'tcx> QueryCtxt<'tcx> {
151151
encoder: &mut on_disk_cache::CacheEncoder<'_, 'tcx>,
152152
query_result_index: &mut on_disk_cache::EncodedDepNodeIndex,
153153
) {
154+
macro_rules! expand_if_cached {
155+
([] $encode:expr) => {};
156+
([(cache) $($rest:tt)*] $encode:expr) => {
157+
$encode
158+
};
159+
([$other:tt $($modifiers:tt)*] $encode:expr) => {
160+
expand_if_cached!([$($modifiers)*] $encode)
161+
};
162+
}
163+
154164
macro_rules! encode_queries {
155-
($($query:ident,)*) => {
165+
(
166+
$($(#[$attr:meta])*
167+
[$($modifiers:tt)*] fn $query:ident($($K:tt)*) -> $V:ty,)*) => {
156168
$(
157-
on_disk_cache::encode_query_results::<_, super::queries::$query<'_>>(
169+
expand_if_cached!([$($modifiers)*] on_disk_cache::encode_query_results::<_, super::queries::$query<'_>>(
158170
self,
159171
encoder,
160172
query_result_index
161-
);
173+
));
162174
)*
163175
}
164176
}
165177

166-
rustc_cached_queries!(encode_queries!);
178+
rustc_query_append!(encode_queries!);
167179
}
168180

169181
pub fn try_print_query_stack(

Diff for: compiler/rustc_query_impl/src/profiling_support.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -307,16 +307,16 @@ pub fn alloc_self_profile_query_strings(tcx: TyCtxt<'_>) {
307307

308308
macro_rules! alloc_once {
309309
(
310-
$($(#[$attr:meta])* [$($modifiers:tt)*] fn $name:ident($K:ty) -> $V:ty,)*
311-
) => {
312-
$({
310+
$($(#[$attr:meta])*
311+
[$($modifiers:tt)*] fn $name:ident($($K:tt)*) -> $V:ty,)*) => {
312+
$(
313313
alloc_self_profile_query_strings_for_query_cache(
314314
tcx,
315315
stringify!($name),
316316
&tcx.query_caches.$name,
317317
&mut string_cache,
318318
);
319-
})*
319+
)+
320320
}
321321
}
322322

0 commit comments

Comments
 (0)