@@ -293,18 +293,6 @@ struct TypedAnnotation<'tcx> {
293
293
maybe_typeck_results : Cell < Option < & ' tcx ty:: TypeckResults < ' tcx > > > ,
294
294
}
295
295
296
- impl < ' tcx > TypedAnnotation < ' tcx > {
297
- /// Gets the type-checking results for the current body.
298
- /// As this will ICE if called outside bodies, only call when working with
299
- /// `Expr` or `Pat` nodes (they are guaranteed to be found only in bodies).
300
- #[ track_caller]
301
- fn typeck_results ( & self ) -> & ' tcx ty:: TypeckResults < ' tcx > {
302
- self . maybe_typeck_results
303
- . get ( )
304
- . expect ( "`TypedAnnotation::typeck_results` called outside of body" )
305
- }
306
- }
307
-
308
296
impl < ' tcx > HirPrinterSupport < ' tcx > for TypedAnnotation < ' tcx > {
309
297
fn sess ( & self ) -> & Session {
310
298
& self . tcx . sess
@@ -336,10 +324,24 @@ impl<'tcx> pprust_hir::PpAnn for TypedAnnotation<'tcx> {
336
324
}
337
325
fn post ( & self , s : & mut pprust_hir:: State < ' _ > , node : pprust_hir:: AnnNode < ' _ > ) {
338
326
if let pprust_hir:: AnnNode :: Expr ( expr) = node {
339
- s. s . space ( ) ;
340
- s. s . word ( "as" ) ;
341
- s. s . space ( ) ;
342
- s. s . word ( self . typeck_results ( ) . expr_ty ( expr) . to_string ( ) ) ;
327
+ let typeck_results =
328
+ self . maybe_typeck_results . get ( ) . or_else ( || {
329
+ if let Some ( body_id) = self . tcx . hir ( ) . maybe_body_owned_by (
330
+ self . tcx . hir ( ) . local_def_id_to_hir_id ( expr. hir_id . owner ) ,
331
+ ) {
332
+ Some ( self . tcx . typeck_body ( body_id) )
333
+ } else {
334
+ None
335
+ }
336
+ } ) ;
337
+
338
+ if let Some ( typeck_results) = typeck_results {
339
+ s. s . space ( ) ;
340
+ s. s . word ( "as" ) ;
341
+ s. s . space ( ) ;
342
+ s. s . word ( typeck_results. expr_ty ( expr) . to_string ( ) ) ;
343
+ }
344
+
343
345
s. pclose ( ) ;
344
346
}
345
347
}
0 commit comments