2
2
//! generate the actual methods on tcx which find and execute the provider,
3
3
//! manage the caches, and so forth.
4
4
5
+ use crate :: keys:: Key ;
5
6
use crate :: { on_disk_cache, Queries } ;
6
- use rustc_middle:: dep_graph:: { DepNodeIndex , SerializedDepNodeIndex } ;
7
+ use rustc_middle:: dep_graph:: { self , DepKind , DepNodeIndex , SerializedDepNodeIndex } ;
7
8
use rustc_middle:: ty:: tls:: { self , ImplicitCtxt } ;
8
- use rustc_middle:: ty:: TyCtxt ;
9
+ use rustc_middle:: ty:: { self , TyCtxt } ;
9
10
use rustc_query_system:: dep_graph:: HasDepContext ;
10
- use rustc_query_system:: query:: { QueryContext , QueryJobId , QueryMap , QuerySideEffects } ;
11
+ use rustc_query_system:: ich:: StableHashingContext ;
12
+ use rustc_query_system:: query:: {
13
+ QueryContext , QueryJobId , QueryMap , QuerySideEffects , QueryStackFrame ,
14
+ } ;
11
15
16
+ use rustc_data_structures:: stable_hasher:: { HashStable , StableHasher } ;
12
17
use rustc_data_structures:: sync:: Lock ;
13
18
use rustc_data_structures:: thin_vec:: ThinVec ;
14
19
use rustc_errors:: { Diagnostic , Handler } ;
@@ -233,12 +238,58 @@ macro_rules! get_provider {
233
238
} ;
234
239
}
235
240
241
+ pub ( crate ) fn create_query_frame <
242
+ ' tcx ,
243
+ K : Copy + Key + for < ' a > HashStable < StableHashingContext < ' a > > ,
244
+ > (
245
+ tcx : QueryCtxt < ' tcx > ,
246
+ do_describe : fn ( QueryCtxt < ' tcx > , K ) -> String ,
247
+ key : K ,
248
+ kind : DepKind ,
249
+ name : & ' static str ,
250
+ ) -> QueryStackFrame {
251
+ // Disable visible paths printing for performance reasons.
252
+ // Showing visible path instead of any path is not that important in production.
253
+ let description = ty:: print:: with_no_visible_paths!(
254
+ // Force filename-line mode to avoid invoking `type_of` query.
255
+ ty:: print:: with_forced_impl_filename_line!( do_describe( tcx, key) )
256
+ ) ;
257
+ let description =
258
+ if tcx. sess . verbose ( ) { format ! ( "{} [{}]" , description, name) } else { description } ;
259
+ let span = if kind == dep_graph:: DepKind :: def_span {
260
+ // The `def_span` query is used to calculate `default_span`,
261
+ // so exit to avoid infinite recursion.
262
+ None
263
+ } else {
264
+ Some ( key. default_span ( * tcx) )
265
+ } ;
266
+ let def_kind = if kind == dep_graph:: DepKind :: opt_def_kind {
267
+ // Try to avoid infinite recursion.
268
+ None
269
+ } else {
270
+ key. key_as_def_id ( )
271
+ . and_then ( |def_id| def_id. as_local ( ) )
272
+ . and_then ( |def_id| tcx. opt_def_kind ( def_id) )
273
+ } ;
274
+ let hash = || {
275
+ tcx. with_stable_hashing_context ( |mut hcx| {
276
+ let mut hasher = StableHasher :: new ( ) ;
277
+ std:: mem:: discriminant ( & kind) . hash_stable ( & mut hcx, & mut hasher) ;
278
+ key. hash_stable ( & mut hcx, & mut hasher) ;
279
+ hasher. finish :: < u64 > ( )
280
+ } )
281
+ } ;
282
+
283
+ QueryStackFrame :: new ( name, description, span, def_kind, hash)
284
+ }
285
+
286
+ // NOTE: `$V` isn't used here, but we still need to match on it so it can be passed to other macros
287
+ // invoked by `rustc_query_append`.
236
288
macro_rules! define_queries {
237
- ( <$tcx : tt>
289
+ (
238
290
$( $( #[ $attr: meta] ) *
239
291
[ $( $modifiers: tt) * ] fn $name: ident( $( $K: tt) * ) -> $V: ty, ) * ) => {
240
292
define_queries_struct! {
241
- tcx: $tcx,
242
293
input: ( $( ( [ $( $modifiers) * ] [ $( $attr) * ] [ $name] ) ) * )
243
294
}
244
295
@@ -247,88 +298,51 @@ macro_rules! define_queries {
247
298
248
299
// Create an eponymous constructor for each query.
249
300
$( #[ allow( nonstandard_style) ] $( #[ $attr] ) *
250
- pub fn $name<$ tcx>( tcx: QueryCtxt <$ tcx>, key: query_keys :: $name<$ tcx>) -> QueryStackFrame {
301
+ pub fn $name<' tcx>( tcx: QueryCtxt <' tcx>, key: <queries :: $name<' tcx> as QueryConfig > :: Key ) -> QueryStackFrame {
251
302
let kind = dep_graph:: DepKind :: $name;
252
303
let name = stringify!( $name) ;
253
- // Disable visible paths printing for performance reasons.
254
- // Showing visible path instead of any path is not that important in production.
255
- let description = ty:: print:: with_no_visible_paths!(
256
- // Force filename-line mode to avoid invoking `type_of` query.
257
- ty:: print:: with_forced_impl_filename_line!(
258
- queries:: $name:: describe( tcx, key)
259
- )
260
- ) ;
261
- let description = if tcx. sess. verbose( ) {
262
- format!( "{} [{}]" , description, name)
263
- } else {
264
- description
265
- } ;
266
- let span = if kind == dep_graph:: DepKind :: def_span {
267
- // The `def_span` query is used to calculate `default_span`,
268
- // so exit to avoid infinite recursion.
269
- None
270
- } else {
271
- Some ( key. default_span( * tcx) )
272
- } ;
273
- let def_kind = if kind == dep_graph:: DepKind :: opt_def_kind {
274
- // Try to avoid infinite recursion.
275
- None
276
- } else {
277
- key. key_as_def_id( )
278
- . and_then( |def_id| def_id. as_local( ) )
279
- . and_then( |def_id| tcx. opt_def_kind( def_id) )
280
- } ;
281
- let hash = || {
282
- tcx. with_stable_hashing_context( |mut hcx|{
283
- let mut hasher = StableHasher :: new( ) ;
284
- std:: mem:: discriminant( & kind) . hash_stable( & mut hcx, & mut hasher) ;
285
- key. hash_stable( & mut hcx, & mut hasher) ;
286
- hasher. finish:: <u64 >( )
287
- } )
288
- } ;
289
-
290
- QueryStackFrame :: new( name, description, span, def_kind, hash)
304
+ $crate:: plumbing:: create_query_frame( tcx, queries:: $name:: describe, key, kind, name)
291
305
} ) *
292
306
}
293
307
294
308
#[ allow( nonstandard_style) ]
295
309
mod queries {
296
310
use std:: marker:: PhantomData ;
297
311
298
- $( pub struct $name<$ tcx> {
299
- data: PhantomData <& $ tcx ( ) >
312
+ $( pub struct $name<' tcx> {
313
+ data: PhantomData <& ' tcx ( ) >
300
314
} ) *
301
315
}
302
316
303
- $( impl <$ tcx> QueryConfig for queries:: $name<$ tcx> {
304
- type Key = query_keys:: $name<$ tcx>;
305
- type Value = query_values:: $name<$ tcx>;
306
- type Stored = query_stored:: $name<$ tcx>;
317
+ $( impl <' tcx> QueryConfig for queries:: $name<' tcx> {
318
+ type Key = query_keys:: $name<' tcx>;
319
+ type Value = query_values:: $name<' tcx>;
320
+ type Stored = query_stored:: $name<' tcx>;
307
321
const NAME : & ' static str = stringify!( $name) ;
308
322
}
309
323
310
- impl <$ tcx> QueryDescription <QueryCtxt <$ tcx>> for queries:: $name<$ tcx> {
311
- rustc_query_description! { $name<$ tcx> }
324
+ impl <' tcx> QueryDescription <QueryCtxt <' tcx>> for queries:: $name<' tcx> {
325
+ rustc_query_description! { $name<' tcx> }
312
326
313
- type Cache = query_storage:: $name<$ tcx>;
327
+ type Cache = query_storage:: $name<' tcx>;
314
328
315
329
#[ inline( always) ]
316
- fn query_state<' a>( tcx: QueryCtxt <$ tcx>) -> & ' a QueryState <Self :: Key >
317
- where QueryCtxt <$ tcx>: ' a
330
+ fn query_state<' a>( tcx: QueryCtxt <' tcx>) -> & ' a QueryState <Self :: Key >
331
+ where QueryCtxt <' tcx>: ' a
318
332
{
319
333
& tcx. queries. $name
320
334
}
321
335
322
336
#[ inline( always) ]
323
- fn query_cache<' a>( tcx: QueryCtxt <$ tcx>) -> & ' a Self :: Cache
337
+ fn query_cache<' a>( tcx: QueryCtxt <' tcx>) -> & ' a Self :: Cache
324
338
where ' tcx: ' a
325
339
{
326
340
& tcx. query_caches. $name
327
341
}
328
342
329
343
#[ inline]
330
344
fn make_vtable( tcx: QueryCtxt <' tcx>, key: & Self :: Key ) ->
331
- QueryVTable <QueryCtxt <$ tcx>, Self :: Key , Self :: Value >
345
+ QueryVTable <QueryCtxt <' tcx>, Self :: Key , Self :: Value >
332
346
{
333
347
let compute = get_provider!( [ $( $modifiers) * ] [ tcx, $name, key] ) ;
334
348
let cache_on_disk = Self :: cache_on_disk( tcx. tcx, key) ;
@@ -349,7 +363,6 @@ macro_rules! define_queries {
349
363
mod query_callbacks {
350
364
use super :: * ;
351
365
use rustc_middle:: dep_graph:: DepNode ;
352
- use rustc_middle:: ty:: query:: query_keys;
353
366
use rustc_query_system:: dep_graph:: DepNodeParams ;
354
367
use rustc_query_system:: query:: { force_query, QueryDescription } ;
355
368
use rustc_query_system:: dep_graph:: FingerprintStyle ;
@@ -411,7 +424,7 @@ macro_rules! define_queries {
411
424
let is_eval_always = is_eval_always!( [ $( $modifiers) * ] ) ;
412
425
413
426
let fingerprint_style =
414
- <query_keys :: $name<' _> as DepNodeParams <TyCtxt <' _>>>:: fingerprint_style( ) ;
427
+ <<queries :: $name<' _> as QueryConfig > :: Key as DepNodeParams <TyCtxt <' _>>>:: fingerprint_style( ) ;
415
428
416
429
if is_anon || !fingerprint_style. reconstructible( ) {
417
430
return DepKindStruct {
@@ -424,8 +437,8 @@ macro_rules! define_queries {
424
437
}
425
438
426
439
#[ inline( always) ]
427
- fn recover<' tcx>( tcx: TyCtxt <' tcx>, dep_node: DepNode ) -> Option <query_keys :: $name<' tcx>> {
428
- <query_keys :: $name<' _> as DepNodeParams <TyCtxt <' _>>>:: recover( tcx, & dep_node)
440
+ fn recover<' tcx>( tcx: TyCtxt <' tcx>, dep_node: DepNode ) -> Option <<queries :: $name<' tcx> as QueryConfig > :: Key > {
441
+ <<queries :: $name<' _> as QueryConfig > :: Key as DepNodeParams <TyCtxt <' _>>>:: recover( tcx, & dep_node)
429
442
}
430
443
431
444
fn force_from_dep_node( tcx: TyCtxt <' _>, dep_node: DepNode ) -> bool {
@@ -465,28 +478,25 @@ macro_rules! define_queries {
465
478
}
466
479
}
467
480
468
- // FIXME(eddyb) this macro (and others?) use `$tcx` and `'tcx` interchangeably.
469
- // We should either not take `$tcx` at all and use `'tcx` everywhere, or use
470
- // `$tcx` everywhere (even if that isn't necessary due to lack of hygiene).
471
481
macro_rules! define_queries_struct {
472
- ( tcx : $tcx : tt ,
482
+ (
473
483
input: ( $( ( [ $( $modifiers: tt) * ] [ $( $attr: tt) * ] [ $name: ident] ) ) * ) ) => {
474
- pub struct Queries <$ tcx> {
484
+ pub struct Queries <' tcx> {
475
485
local_providers: Box <Providers >,
476
486
extern_providers: Box <ExternProviders >,
477
487
478
- pub on_disk_cache: Option <OnDiskCache <$ tcx>>,
488
+ pub on_disk_cache: Option <OnDiskCache <' tcx>>,
479
489
480
490
jobs: AtomicU64 ,
481
491
482
- $( $( #[ $attr] ) * $name: QueryState <query_keys :: $name<$ tcx>>, ) *
492
+ $( $( #[ $attr] ) * $name: QueryState <<queries :: $name<' tcx> as QueryConfig > :: Key >, ) *
483
493
}
484
494
485
- impl <$ tcx> Queries <$ tcx> {
495
+ impl <' tcx> Queries <' tcx> {
486
496
pub fn new(
487
497
local_providers: Providers ,
488
498
extern_providers: ExternProviders ,
489
- on_disk_cache: Option <OnDiskCache <$ tcx>>,
499
+ on_disk_cache: Option <OnDiskCache <' tcx>>,
490
500
) -> Self {
491
501
Queries {
492
502
local_providers: Box :: new( local_providers) ,
@@ -498,8 +508,8 @@ macro_rules! define_queries_struct {
498
508
}
499
509
500
510
pub ( crate ) fn try_collect_active_jobs(
501
- & $ tcx self ,
502
- tcx: TyCtxt <$ tcx>,
511
+ & ' tcx self ,
512
+ tcx: TyCtxt <' tcx>,
503
513
) -> Option <QueryMap > {
504
514
let tcx = QueryCtxt { tcx, queries: self } ;
505
515
let mut jobs = QueryMap :: default ( ) ;
@@ -532,13 +542,13 @@ macro_rules! define_queries_struct {
532
542
#[ tracing:: instrument( level = "trace" , skip( self , tcx) ) ]
533
543
fn $name(
534
544
& ' tcx self ,
535
- tcx: TyCtxt <$ tcx>,
545
+ tcx: TyCtxt <' tcx>,
536
546
span: Span ,
537
- key: query_keys :: $name<$ tcx>,
547
+ key: <queries :: $name<' tcx> as QueryConfig > :: Key ,
538
548
mode: QueryMode ,
539
- ) -> Option <query_stored:: $name<$ tcx>> {
549
+ ) -> Option <query_stored:: $name<' tcx>> {
540
550
let qcx = QueryCtxt { tcx, queries: self } ;
541
- get_query:: <queries:: $name<$ tcx>, _>( qcx, span, key, mode)
551
+ get_query:: <queries:: $name<' tcx>, _>( qcx, span, key, mode)
542
552
} ) *
543
553
}
544
554
} ;
0 commit comments