80
80
PpmTyped => {
81
81
abort_on_err ( tcx. analysis ( LOCAL_CRATE ) , tcx. sess ) ;
82
82
83
- let empty_tables = ty:: TypeckTables :: empty ( None ) ;
84
- let annotation = TypedAnnotation { tcx, tables : Cell :: new ( & empty_tables) } ;
83
+ let annotation = TypedAnnotation { tcx, maybe_typeck_tables : Cell :: new ( None ) } ;
85
84
tcx. dep_graph . with_ignore ( || f ( & annotation, tcx. hir ( ) . krate ( ) ) )
86
85
}
87
86
_ => panic ! ( "Should use call_with_pp_support" ) ,
@@ -304,12 +303,22 @@ impl<'a> pprust::PpAnn for HygieneAnnotation<'a> {
304
303
}
305
304
}
306
305
307
- struct TypedAnnotation < ' a , ' tcx > {
306
+ struct TypedAnnotation < ' tcx > {
308
307
tcx : TyCtxt < ' tcx > ,
309
- tables : Cell < & ' a ty:: TypeckTables < ' tcx > > ,
308
+ maybe_typeck_tables : Cell < Option < & ' tcx ty:: TypeckTables < ' tcx > > > ,
310
309
}
311
310
312
- impl < ' b , ' tcx > HirPrinterSupport < ' tcx > for TypedAnnotation < ' b , ' tcx > {
311
+ impl < ' tcx > TypedAnnotation < ' tcx > {
312
+ /// Gets the type-checking side-tables for the current body.
313
+ /// As this will ICE if called outside bodies, only call when working with
314
+ /// `Expr` or `Pat` nodes (they are guaranteed to be found only in bodies).
315
+ #[ track_caller]
316
+ fn tables ( & self ) -> & ' tcx ty:: TypeckTables < ' tcx > {
317
+ self . maybe_typeck_tables . get ( ) . expect ( "`TypedAnnotation::tables` called outside of body" )
318
+ }
319
+ }
320
+
321
+ impl < ' tcx > HirPrinterSupport < ' tcx > for TypedAnnotation < ' tcx > {
313
322
fn sess ( & self ) -> & Session {
314
323
& self . tcx . sess
315
324
}
@@ -327,15 +336,15 @@ impl<'b, 'tcx> HirPrinterSupport<'tcx> for TypedAnnotation<'b, 'tcx> {
327
336
}
328
337
}
329
338
330
- impl < ' a , ' tcx > pprust_hir:: PpAnn for TypedAnnotation < ' a , ' tcx > {
339
+ impl < ' tcx > pprust_hir:: PpAnn for TypedAnnotation < ' tcx > {
331
340
fn nested ( & self , state : & mut pprust_hir:: State < ' _ > , nested : pprust_hir:: Nested ) {
332
- let old_tables = self . tables . get ( ) ;
341
+ let old_maybe_typeck_tables = self . maybe_typeck_tables . get ( ) ;
333
342
if let pprust_hir:: Nested :: Body ( id) = nested {
334
- self . tables . set ( self . tcx . body_tables ( id) ) ;
343
+ self . maybe_typeck_tables . set ( Some ( self . tcx . body_tables ( id) ) ) ;
335
344
}
336
345
let pp_ann = & ( & self . tcx . hir ( ) as & dyn hir:: intravisit:: Map < ' _ > ) ;
337
346
pprust_hir:: PpAnn :: nested ( pp_ann, state, nested) ;
338
- self . tables . set ( old_tables ) ;
347
+ self . maybe_typeck_tables . set ( old_maybe_typeck_tables ) ;
339
348
}
340
349
fn pre ( & self , s : & mut pprust_hir:: State < ' _ > , node : pprust_hir:: AnnNode < ' _ > ) {
341
350
if let pprust_hir:: AnnNode :: Expr ( _) = node {
@@ -347,7 +356,7 @@ impl<'a, 'tcx> pprust_hir::PpAnn for TypedAnnotation<'a, 'tcx> {
347
356
s. s . space ( ) ;
348
357
s. s . word ( "as" ) ;
349
358
s. s . space ( ) ;
350
- s. s . word ( self . tables . get ( ) . expr_ty ( expr) . to_string ( ) ) ;
359
+ s. s . word ( self . tables ( ) . expr_ty ( expr) . to_string ( ) ) ;
351
360
s. pclose ( ) ;
352
361
}
353
362
}
0 commit comments