@@ -980,6 +980,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
980980 obligation. predicate, obligation. cause. span
981981 ) ;
982982 let source_map = self . tcx . sess . source_map ( ) ;
983+ let hir = self . tcx . hir ( ) ;
983984
984985 // Attempt to detect an async-await error by looking at the obligation causes, looking
985986 // for a generator to be present.
@@ -1063,7 +1064,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
10631064 let span = self . tcx . def_span ( generator_did) ;
10641065
10651066 // Do not ICE on closure typeck (#66868).
1066- if self . tcx . hir ( ) . as_local_hir_id ( generator_did) . is_none ( ) {
1067+ if hir. as_local_hir_id ( generator_did) . is_none ( ) {
10671068 return false ;
10681069 }
10691070
@@ -1089,12 +1090,10 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
10891090 }
10901091 } ;
10911092
1092- let generator_body = self
1093- . tcx
1094- . hir ( )
1093+ let generator_body = hir
10951094 . as_local_hir_id ( generator_did)
1096- . and_then ( |hir_id| self . tcx . hir ( ) . maybe_body_owned_by ( hir_id) )
1097- . map ( |body_id| self . tcx . hir ( ) . body ( body_id) ) ;
1095+ . and_then ( |hir_id| hir. maybe_body_owned_by ( hir_id) )
1096+ . map ( |body_id| hir. body ( body_id) ) ;
10981097 let mut visitor = AwaitsVisitor :: default ( ) ;
10991098 if let Some ( body) = generator_body {
11001099 visitor. visit_body ( body) ;
@@ -1104,50 +1103,46 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
11041103 // Look for a type inside the generator interior that matches the target type to get
11051104 // a span.
11061105 let target_ty_erased = self . tcx . erase_regions ( & target_ty) ;
1106+ let ty_matches = |ty| -> bool {
1107+ // Careful: the regions for types that appear in the
1108+ // generator interior are not generally known, so we
1109+ // want to erase them when comparing (and anyway,
1110+ // `Send` and other bounds are generally unaffected by
1111+ // the choice of region). When erasing regions, we
1112+ // also have to erase late-bound regions. This is
1113+ // because the types that appear in the generator
1114+ // interior generally contain "bound regions" to
1115+ // represent regions that are part of the suspended
1116+ // generator frame. Bound regions are preserved by
1117+ // `erase_regions` and so we must also call
1118+ // `erase_late_bound_regions`.
1119+ let ty_erased = self . tcx . erase_late_bound_regions ( & ty:: Binder :: bind ( ty) ) ;
1120+ let ty_erased = self . tcx . erase_regions ( & ty_erased) ;
1121+ let eq = ty:: TyS :: same_type ( ty_erased, target_ty_erased) ;
1122+ debug ! (
1123+ "maybe_note_obligation_cause_for_async_await: ty_erased={:?} \
1124+ target_ty_erased={:?} eq={:?}",
1125+ ty_erased, target_ty_erased, eq
1126+ ) ;
1127+ eq
1128+ } ;
11071129 let target_span = tables
11081130 . generator_interior_types
11091131 . iter ( )
1110- . find ( |ty:: GeneratorInteriorTypeCause { ty, .. } | {
1111- // Careful: the regions for types that appear in the
1112- // generator interior are not generally known, so we
1113- // want to erase them when comparing (and anyway,
1114- // `Send` and other bounds are generally unaffected by
1115- // the choice of region). When erasing regions, we
1116- // also have to erase late-bound regions. This is
1117- // because the types that appear in the generator
1118- // interior generally contain "bound regions" to
1119- // represent regions that are part of the suspended
1120- // generator frame. Bound regions are preserved by
1121- // `erase_regions` and so we must also call
1122- // `erase_late_bound_regions`.
1123- let ty_erased = self . tcx . erase_late_bound_regions ( & ty:: Binder :: bind ( * ty) ) ;
1124- let ty_erased = self . tcx . erase_regions ( & ty_erased) ;
1125- let eq = ty:: TyS :: same_type ( ty_erased, target_ty_erased) ;
1126- debug ! (
1127- "maybe_note_obligation_cause_for_async_await: ty_erased={:?} \
1128- target_ty_erased={:?} eq={:?}",
1129- ty_erased, target_ty_erased, eq
1130- ) ;
1131- eq
1132- } )
1132+ . find ( |ty:: GeneratorInteriorTypeCause { ty, .. } | ty_matches ( ty) )
11331133 . map ( |cause| {
11341134 // Check to see if any awaited expressions have the target type.
11351135 let from_awaited_ty = visitor
11361136 . awaits
11371137 . into_iter ( )
1138- . map ( |id| self . tcx . hir ( ) . expect_expr ( id) )
1139- . find ( |expr| {
1140- let ty = tables. expr_ty_adjusted ( & expr) ;
1141- // Compare types using the same logic as above.
1142- let ty_erased = self . tcx . erase_late_bound_regions ( & ty:: Binder :: bind ( ty) ) ;
1143- let ty_erased = self . tcx . erase_regions ( & ty_erased) ;
1144- let eq = ty:: TyS :: same_type ( ty_erased, target_ty_erased) ;
1138+ . map ( |id| hir. expect_expr ( id) )
1139+ . find ( |await_expr| {
1140+ let ty = tables. expr_ty_adjusted ( & await_expr) ;
11451141 debug ! (
1146- "maybe_note_obligation_cause_for_async_await: await_expr={:?} \
1147- await_ty_erased={:?} target_ty_erased={:?} eq={:?}",
1148- expr, ty_erased, target_ty_erased, eq
1142+ "maybe_note_obligation_cause_for_async_await: await_expr={:?}" ,
1143+ await_expr
11491144 ) ;
1150- eq
1145+ ty_matches ( ty )
11511146 } )
11521147 . map ( |expr| expr. span ) ;
11531148 let ty:: GeneratorInteriorTypeCause { span, scope_span, expr, .. } = cause;
@@ -1669,11 +1664,8 @@ impl<'v> Visitor<'v> for AwaitsVisitor {
16691664 }
16701665
16711666 fn visit_expr ( & mut self , ex : & ' v hir:: Expr < ' v > ) {
1672- match ex. kind {
1673- hir:: ExprKind :: Yield ( _, hir:: YieldSource :: Await { expr : Some ( id) } ) => {
1674- self . awaits . push ( id)
1675- }
1676- _ => ( ) ,
1667+ if let hir:: ExprKind :: Yield ( _, hir:: YieldSource :: Await { expr : Some ( id) } ) = ex. kind {
1668+ self . awaits . push ( id)
16771669 }
16781670 hir:: intravisit:: walk_expr ( self , ex)
16791671 }
0 commit comments