@@ -45,7 +45,7 @@ impl HasDepContext for QueryCtxt<'tcx> {
45
45
}
46
46
47
47
impl QueryContext for QueryCtxt < ' tcx > {
48
- type Query = Query < ' tcx > ;
48
+ type Query = Query ;
49
49
50
50
fn def_path_str ( & self , def_id : DefId ) -> String {
51
51
self . tcx . def_path_str ( def_id)
@@ -59,7 +59,7 @@ impl QueryContext for QueryCtxt<'tcx> {
59
59
& self ,
60
60
) -> Option < FxHashMap < QueryJobId < Self :: DepKind > , QueryJobInfo < Self :: DepKind , Self :: Query > > >
61
61
{
62
- self . queries . try_collect_active_jobs ( )
62
+ self . queries . try_collect_active_jobs ( * * self )
63
63
}
64
64
65
65
fn try_load_from_on_disk_cache ( & self , dep_node : & DepNode ) {
@@ -185,12 +185,12 @@ impl<'tcx> QueryCtxt<'tcx> {
185
185
#[ cold]
186
186
pub ( super ) fn report_cycle (
187
187
self ,
188
- CycleError { usage, cycle : stack } : CycleError < Query < ' tcx > > ,
188
+ CycleError { usage, cycle : stack } : CycleError < Query > ,
189
189
) -> DiagnosticBuilder < ' tcx > {
190
190
assert ! ( !stack. is_empty( ) ) ;
191
191
192
- let fix_span = |span : Span , query : & Query < ' tcx > | {
193
- self . sess . source_map ( ) . guess_head_span ( query. default_span ( * self , span) )
192
+ let fix_span = |span : Span , query : & Query | {
193
+ self . sess . source_map ( ) . guess_head_span ( query. default_span ( span) )
194
194
} ;
195
195
196
196
// Disable naming impls with types in this path, since that
@@ -204,24 +204,24 @@ impl<'tcx> QueryCtxt<'tcx> {
204
204
span,
205
205
E0391 ,
206
206
"cycle detected when {}" ,
207
- stack[ 0 ] . query. describe ( self )
207
+ stack[ 0 ] . query. description
208
208
) ;
209
209
210
210
for i in 1 ..stack. len ( ) {
211
211
let query = & stack[ i] . query ;
212
212
let span = fix_span ( stack[ ( i + 1 ) % stack. len ( ) ] . span , query) ;
213
- err. span_note ( span, & format ! ( "...which requires {}..." , query. describe ( self ) ) ) ;
213
+ err. span_note ( span, & format ! ( "...which requires {}..." , query. description ) ) ;
214
214
}
215
215
216
216
err. note ( & format ! (
217
217
"...which again requires {}, completing the cycle" ,
218
- stack[ 0 ] . query. describe ( self )
218
+ stack[ 0 ] . query. description
219
219
) ) ;
220
220
221
221
if let Some ( ( span, query) ) = usage {
222
222
err. span_note (
223
223
fix_span ( span, & query) ,
224
- & format ! ( "cycle used when {}" , query. describe ( self ) ) ,
224
+ & format ! ( "cycle used when {}" , query. description ) ,
225
225
) ;
226
226
}
227
227
@@ -371,54 +371,58 @@ macro_rules! define_queries {
371
371
input: ( $( ( [ $( $modifiers) * ] [ $( $attr) * ] [ $name] ) ) * )
372
372
}
373
373
374
- #[ allow( nonstandard_style) ]
375
374
#[ derive( Clone , Debug ) ]
376
- pub enum Query <$tcx> {
377
- $( $( #[ $attr] ) * $name( query_keys:: $name<$tcx>) ) ,*
375
+ pub struct Query {
376
+ pub name: & ' static str ,
377
+ hash: Fingerprint ,
378
+ description: String ,
379
+ span: Option <Span >,
378
380
}
379
381
380
- impl <$tcx> Query <$tcx> {
381
- pub fn name( & self ) -> & ' static str {
382
- match * self {
383
- $( Query :: $name( _) => stringify!( $name) , ) *
384
- }
385
- }
386
-
387
- pub ( crate ) fn describe( & self , tcx: QueryCtxt <$tcx>) -> String {
388
- let ( r, name) = match * self {
389
- $( Query :: $name( key) => {
390
- ( queries:: $name:: describe( tcx, key) , stringify!( $name) )
391
- } ) *
382
+ impl Query {
383
+ $( #[ allow( nonstandard_style) ] $( #[ $attr] ) *
384
+ pub fn $name<$tcx>( tcx: QueryCtxt <$tcx>, key: query_keys:: $name<$tcx>) -> Self {
385
+ let kind = dep_graph:: DepKind :: $name;
386
+ let name = stringify!( $name) ;
387
+ let description = ty:: print:: with_forced_impl_filename_line(
388
+ // Force filename-line mode to avoid invoking `type_of` query.
389
+ || queries:: $name:: describe( tcx, key)
390
+ ) ;
391
+ let description = if tcx. sess. verbose( ) {
392
+ format!( "{} [{}]" , description, name)
393
+ } else {
394
+ description
392
395
} ;
393
- if tcx. sess. verbose( ) {
394
- format!( "{} [{}]" , r, name)
396
+ let span = if kind == dep_graph:: DepKind :: def_span {
397
+ // The `def_span` query is used to calculate `default_span`,
398
+ // so exit to avoid infinite recursion.
399
+ None
395
400
} else {
396
- r
397
- }
398
- }
401
+ Some ( key. default_span( * tcx) )
402
+ } ;
403
+ let hash = {
404
+ let mut hcx = tcx. create_stable_hashing_context( ) ;
405
+ let mut hasher = StableHasher :: new( ) ;
406
+ std:: mem:: discriminant( & kind) . hash_stable( & mut hcx, & mut hasher) ;
407
+ key. hash_stable( & mut hcx, & mut hasher) ;
408
+ hasher. finish( )
409
+ } ;
410
+
411
+ Self { name, description, span, hash }
412
+ } ) *
399
413
400
414
// FIXME(eddyb) Get more valid `Span`s on queries.
401
- pub fn default_span( & self , tcx : TyCtxt <$tcx> , span: Span ) -> Span {
415
+ pub fn default_span( & self , span: Span ) -> Span {
402
416
if !span. is_dummy( ) {
403
417
return span;
404
418
}
405
- // The `def_span` query is used to calculate `default_span`,
406
- // so exit to avoid infinite recursion.
407
- if let Query :: def_span( ..) = * self {
408
- return span
409
- }
410
- match * self {
411
- $( Query :: $name( key) => key. default_span( tcx) , ) *
412
- }
419
+ self . span. unwrap_or( span)
413
420
}
414
421
}
415
422
416
- impl <' a, $tcx > HashStable <StableHashingContext <' a>> for Query <$tcx> {
423
+ impl <' a> HashStable <StableHashingContext <' a>> for Query {
417
424
fn hash_stable( & self , hcx: & mut StableHashingContext <' a>, hasher: & mut StableHasher ) {
418
- mem:: discriminant( self ) . hash_stable( hcx, hasher) ;
419
- match * self {
420
- $( Query :: $name( key) => key. hash_stable( hcx, hasher) , ) *
421
- }
425
+ self . hash. hash_stable( hcx, hasher)
422
426
}
423
427
}
424
428
@@ -446,7 +450,9 @@ macro_rules! define_queries {
446
450
type Cache = query_storage:: $name<$tcx>;
447
451
448
452
#[ inline( always) ]
449
- fn query_state<' a>( tcx: QueryCtxt <$tcx>) -> & ' a QueryState <crate :: dep_graph:: DepKind , Query <$tcx>, Self :: Key > {
453
+ fn query_state<' a>( tcx: QueryCtxt <$tcx>) -> & ' a QueryState <crate :: dep_graph:: DepKind , Query , Self :: Key >
454
+ where QueryCtxt <$tcx>: ' a
455
+ {
450
456
& tcx. queries. $name
451
457
}
452
458
@@ -478,7 +484,7 @@ macro_rules! define_queries {
478
484
479
485
fn handle_cycle_error(
480
486
tcx: QueryCtxt <' tcx>,
481
- error: CycleError <Query < ' tcx> >
487
+ error: CycleError <Query >
482
488
) -> Self :: Value {
483
489
handle_cycle_error!( [ $( $modifiers) * ] [ tcx, error] )
484
490
}
@@ -581,7 +587,7 @@ macro_rules! define_queries_struct {
581
587
582
588
$( $( #[ $attr] ) * $name: QueryState <
583
589
crate :: dep_graph:: DepKind ,
584
- Query <$tcx> ,
590
+ Query ,
585
591
query_keys:: $name<$tcx>,
586
592
>, ) *
587
593
}
@@ -599,13 +605,16 @@ macro_rules! define_queries_struct {
599
605
}
600
606
601
607
pub ( crate ) fn try_collect_active_jobs(
602
- & self
603
- ) -> Option <FxHashMap <QueryJobId <crate :: dep_graph:: DepKind >, QueryJobInfo <crate :: dep_graph:: DepKind , Query <$tcx>>>> {
608
+ & $tcx self ,
609
+ tcx: TyCtxt <$tcx>,
610
+ ) -> Option <FxHashMap <QueryJobId <crate :: dep_graph:: DepKind >, QueryJobInfo <crate :: dep_graph:: DepKind , Query >>> {
611
+ let tcx = QueryCtxt { tcx, queries: self } ;
604
612
let mut jobs = FxHashMap :: default ( ) ;
605
613
606
614
$(
607
615
self . $name. try_collect_active_jobs(
608
- <queries:: $name<' tcx> as QueryAccessors <QueryCtxt <' tcx>>>:: DEP_KIND ,
616
+ tcx,
617
+ dep_graph:: DepKind :: $name,
609
618
Query :: $name,
610
619
& mut jobs,
611
620
) ?;
@@ -651,7 +660,7 @@ macro_rules! define_queries_struct {
651
660
handler: & Handler ,
652
661
num_frames: Option <usize >,
653
662
) -> usize {
654
- let query_map = self . try_collect_active_jobs( ) ;
663
+ let query_map = self . try_collect_active_jobs( tcx ) ;
655
664
656
665
let mut current_query = query;
657
666
let mut i = 0 ;
@@ -671,8 +680,8 @@ macro_rules! define_queries_struct {
671
680
& format!(
672
681
"#{} [{}] {}" ,
673
682
i,
674
- query_info. info. query. name( ) ,
675
- query_info. info. query. describe ( QueryCtxt { tcx , queries : self } )
683
+ query_info. info. query. name,
684
+ query_info. info. query. description ,
676
685
) ,
677
686
) ;
678
687
diag. span = tcx. sess. source_map( ) . guess_head_span( query_info. info. span) . into( ) ;
0 commit comments