@@ -273,52 +273,6 @@ fn doc_comment_from_desc(list: &Punctuated<Expr, token::Comma>) -> Result<Attrib
273273 Ok ( parse_quote ! { #[ doc = #doc_string] } )
274274}
275275
276- /// Contains token streams that are used to accumulate per-query helper
277- /// functions, to be used by the final output of `rustc_queries!`.
278- ///
279- /// Helper items typically have the same name as the query they relate to,
280- /// and expect to be interpolated into a dedicated module.
281- #[ derive( Default ) ]
282- struct HelperTokenStreams {
283- description_fns_stream : proc_macro2:: TokenStream ,
284- cache_on_disk_if_fns_stream : proc_macro2:: TokenStream ,
285- }
286-
287- fn make_helpers_for_query ( query : & Query , streams : & mut HelperTokenStreams ) {
288- let Query { name, key_pat, key_ty, modifiers, .. } = & query;
289-
290- // Replace span for `name` to make rust-analyzer ignore it.
291- let mut erased_name = name. clone ( ) ;
292- erased_name. set_span ( Span :: call_site ( ) ) ;
293-
294- // Generate a function to check whether we should cache the query to disk, for some key.
295- if let Some ( CacheOnDiskIf { block, .. } ) = modifiers. cache_on_disk_if . as_ref ( ) {
296- // `pass_by_value`: some keys are marked with `rustc_pass_by_value`, but we take keys by
297- // reference here.
298- // FIXME: `pass_by_value` is badly named; `allow(rustc::pass_by_value)` actually means
299- // "allow pass by reference of `rustc_pass_by_value` types".
300- streams. cache_on_disk_if_fns_stream . extend ( quote ! {
301- #[ allow( unused_variables, rustc:: pass_by_value) ]
302- #[ inline]
303- pub fn #erased_name<' tcx>( tcx: TyCtxt <' tcx>, #key_pat: & #key_ty) -> bool
304- #block
305- } ) ;
306- }
307-
308- let Desc { expr_list, .. } = & modifiers. desc ;
309-
310- let desc = quote ! {
311- #[ allow( unused_variables) ]
312- pub fn #erased_name<' tcx>( tcx: TyCtxt <' tcx>, #key_pat: #key_ty) -> String {
313- format!( #expr_list)
314- }
315- } ;
316-
317- streams. description_fns_stream . extend ( quote ! {
318- #desc
319- } ) ;
320- }
321-
322276/// Add hints for rust-analyzer
323277fn add_to_analyzer_stream ( query : & Query , analyzer_stream : & mut proc_macro2:: TokenStream ) {
324278 // Add links to relevant modifiers
@@ -398,7 +352,6 @@ pub(super) fn rustc_queries(input: TokenStream) -> TokenStream {
398352 let queries = parse_macro_input ! ( input as List <Query >) ;
399353
400354 let mut query_stream = quote ! { } ;
401- let mut helpers = HelperTokenStreams :: default ( ) ;
402355 let mut analyzer_stream = quote ! { } ;
403356 let mut errors = quote ! { } ;
404357
@@ -413,7 +366,7 @@ pub(super) fn rustc_queries(input: TokenStream) -> TokenStream {
413366 }
414367
415368 for query in queries. 0 {
416- let Query { doc_comments, name, key_ty, return_ty, modifiers, .. } = & query;
369+ let Query { doc_comments, name, key_pat , key_ty, return_ty, modifiers, .. } = & query;
417370
418371 // Normalize an absent return type into `-> ()` to make macro-rules parsing easier.
419372 let return_ty = match return_ty {
@@ -431,6 +384,10 @@ pub(super) fn rustc_queries(input: TokenStream) -> TokenStream {
431384 }
432385 }
433386
387+ // Put a description closure inside the `desc` modifier: `(desc { <closure> })`.
388+ let expr_list = & modifiers. desc . expr_list ;
389+ modifiers_out. push ( quote ! { ( desc { |tcx, #key_pat| format!( #expr_list) } ) } ) ;
390+
434391 passthrough ! (
435392 arena_cache,
436393 cycle_fatal,
@@ -445,11 +402,23 @@ pub(super) fn rustc_queries(input: TokenStream) -> TokenStream {
445402 return_result_from_ensure_ok,
446403 ) ;
447404
448- // If there was a `cache_on_disk_if` modifier in the real input, pass
449- // on a synthetic `(cache_on_disk)` modifier that can be inspected by
450- // macro-rules macros.
451- if modifiers. cache_on_disk_if . is_some ( ) {
452- modifiers_out. push ( quote ! { ( cache_on_disk) } ) ;
405+ // If there was a `cache_on_disk_if` modifier, put a closure inside it:
406+ // `(cache_on_disk { <closure > }`.
407+ if let Some ( CacheOnDiskIf { block, .. } ) = & modifiers. cache_on_disk_if {
408+ modifiers_out. push ( quote ! {
409+ ( cache_on_disk_if {
410+ // `pass_by_value`: some keys are marked with `rustc_pass_by_value`, but we
411+ // take keys by reference here.
412+ // FIXME: `pass_by_value` is badly named; `allow(rustc::pass_by_value)`
413+ // actually means "allow pass by reference of `rustc_pass_by_value` types".
414+ //
415+ // The type annotations are required to avoid compile errors, which is annoying
416+ // because it necessitates extra `use` items in the file using
417+ // `rustc_with_all_queries!`.
418+ #[ allow( rustc:: pass_by_value) ]
419+ |tcx: TyCtxt <' _>, #key_pat: & #key_ty| #block
420+ } )
421+ } ) ;
453422 }
454423
455424 // This uses the span of the query definition for the commas,
@@ -482,11 +451,8 @@ pub(super) fn rustc_queries(input: TokenStream) -> TokenStream {
482451 }
483452
484453 add_to_analyzer_stream ( & query, & mut analyzer_stream) ;
485- make_helpers_for_query ( & query, & mut helpers) ;
486454 }
487455
488- let HelperTokenStreams { description_fns_stream, cache_on_disk_if_fns_stream } = helpers;
489-
490456 TokenStream :: from ( quote ! {
491457 /// Higher-order macro that invokes the specified macro with a prepared
492458 /// list of all query signatures (including modifiers).
@@ -516,25 +482,6 @@ pub(super) fn rustc_queries(input: TokenStream) -> TokenStream {
516482 #analyzer_stream
517483 }
518484
519- /// Functions that format a human-readable description of each query
520- /// and its key, as specified by the `desc` query modifier.
521- ///
522- /// (The leading `_` avoids collisions with actual query names when
523- /// expanded in `rustc_middle::queries`, and makes this macro-generated
524- /// module easier to search for.)
525- pub mod _description_fns {
526- use super :: * ;
527- #description_fns_stream
528- }
529-
530- // FIXME(Zalathar): Instead of declaring these functions directly, can
531- // we put them in a macro and then expand that macro downstream in
532- // `rustc_query_impl`, where the functions are actually used?
533- pub mod _cache_on_disk_if_fns {
534- use super :: * ;
535- #cache_on_disk_if_fns_stream
536- }
537-
538485 #errors
539486 } )
540487}
0 commit comments