8080 PpmTyped => {
8181 abort_on_err ( tcx. analysis ( LOCAL_CRATE ) , tcx. sess ) ;
8282
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 ) } ;
8584 tcx. dep_graph . with_ignore ( || f ( & annotation, tcx. hir ( ) . krate ( ) ) )
8685 }
8786 _ => panic ! ( "Should use call_with_pp_support" ) ,
@@ -304,12 +303,22 @@ impl<'a> pprust::PpAnn for HygieneAnnotation<'a> {
304303 }
305304}
306305
307- struct TypedAnnotation < ' a , ' tcx > {
306+ struct TypedAnnotation < ' tcx > {
308307 tcx : TyCtxt < ' tcx > ,
309- tables : Cell < & ' a ty:: TypeckTables < ' tcx > > ,
308+ maybe_typeck_tables : Cell < Option < & ' tcx ty:: TypeckTables < ' tcx > > > ,
310309}
311310
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 > {
313322 fn sess ( & self ) -> & Session {
314323 & self . tcx . sess
315324 }
@@ -327,15 +336,15 @@ impl<'b, 'tcx> HirPrinterSupport<'tcx> for TypedAnnotation<'b, 'tcx> {
327336 }
328337}
329338
330- impl < ' a , ' tcx > pprust_hir:: PpAnn for TypedAnnotation < ' a , ' tcx > {
339+ impl < ' tcx > pprust_hir:: PpAnn for TypedAnnotation < ' tcx > {
331340 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 ( ) ;
333342 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) ) ) ;
335344 }
336345 let pp_ann = & ( & self . tcx . hir ( ) as & dyn hir:: intravisit:: Map < ' _ > ) ;
337346 pprust_hir:: PpAnn :: nested ( pp_ann, state, nested) ;
338- self . tables . set ( old_tables ) ;
347+ self . maybe_typeck_tables . set ( old_maybe_typeck_tables ) ;
339348 }
340349 fn pre ( & self , s : & mut pprust_hir:: State < ' _ > , node : pprust_hir:: AnnNode < ' _ > ) {
341350 if let pprust_hir:: AnnNode :: Expr ( _) = node {
@@ -347,7 +356,7 @@ impl<'a, 'tcx> pprust_hir::PpAnn for TypedAnnotation<'a, 'tcx> {
347356 s. s . space ( ) ;
348357 s. s . word ( "as" ) ;
349358 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 ( ) ) ;
351360 s. pclose ( ) ;
352361 }
353362 }
0 commit comments