@@ -917,12 +917,13 @@ impl<'hir> LoweringContext<'_, 'hir> {
917917 let poll_expr = {
918918 let awaitee = self . expr_ident ( span, awaitee_ident, awaitee_pat_hid) ;
919919 let ref_mut_awaitee = self . expr_mut_addr_of ( span, awaitee) ;
920- let task_context = if let Some ( task_context_hid) = self . task_context {
921- self . expr_ident_mut ( span, task_context_ident, task_context_hid)
922- } else {
923- // Use of `await` outside of an async context, we cannot use `task_context` here.
924- self . expr_err ( span, self . tcx . sess . span_delayed_bug ( span, "no task_context hir id" ) )
920+
921+ let Some ( task_context_hid) = self . task_context else {
922+ unreachable ! ( "use of `await` outside of an async context." ) ;
925923 } ;
924+
925+ let task_context = self . expr_ident_mut ( span, task_context_ident, task_context_hid) ;
926+
926927 let new_unchecked = self . expr_call_lang_item_fn_mut (
927928 span,
928929 hir:: LangItem :: PinNewUnchecked ,
@@ -991,16 +992,14 @@ impl<'hir> LoweringContext<'_, 'hir> {
991992 ) ;
992993 let yield_expr = self . arena . alloc ( yield_expr) ;
993994
994- if let Some ( task_context_hid) = self . task_context {
995- let lhs = self . expr_ident ( span, task_context_ident, task_context_hid) ;
996- let assign =
997- self . expr ( span, hir:: ExprKind :: Assign ( lhs, yield_expr, self . lower_span ( span) ) ) ;
998- self . stmt_expr ( span, assign)
999- } else {
1000- // Use of `await` outside of an async context. Return `yield_expr` so that we can
1001- // proceed with type checking.
1002- self . stmt ( span, hir:: StmtKind :: Semi ( yield_expr) )
1003- }
995+ let Some ( task_context_hid) = self . task_context else {
996+ unreachable ! ( "use of `await` outside of an async context." ) ;
997+ } ;
998+
999+ let lhs = self . expr_ident ( span, task_context_ident, task_context_hid) ;
1000+ let assign =
1001+ self . expr ( span, hir:: ExprKind :: Assign ( lhs, yield_expr, self . lower_span ( span) ) ) ;
1002+ self . stmt_expr ( span, assign)
10041003 } ;
10051004
10061005 let loop_block = self . block_all ( span, arena_vec ! [ self ; inner_match_stmt, yield_stmt] , None ) ;
@@ -1635,19 +1634,32 @@ impl<'hir> LoweringContext<'_, 'hir> {
16351634 }
16361635 } ;
16371636
1638- let mut yielded =
1637+ let yielded =
16391638 opt_expr. as_ref ( ) . map ( |x| self . lower_expr ( x) ) . unwrap_or_else ( || self . expr_unit ( span) ) ;
16401639
16411640 if is_async_gen {
1642- // yield async_gen_ready($expr);
1643- yielded = self . expr_call_lang_item_fn (
1641+ // `yield $expr` is transformed into `task_context = yield async_gen_ready($expr)`.
1642+ // This ensures that we store our resumed `ResumeContext` correctly, and also that
1643+ // the apparent value of the `yield` expression is `()`.
1644+ let wrapped_yielded = self . expr_call_lang_item_fn (
16441645 span,
16451646 hir:: LangItem :: AsyncGenReady ,
16461647 std:: slice:: from_ref ( yielded) ,
16471648 ) ;
1648- }
1649+ let yield_expr = self . arena . alloc (
1650+ self . expr ( span, hir:: ExprKind :: Yield ( wrapped_yielded, hir:: YieldSource :: Yield ) ) ,
1651+ ) ;
16491652
1650- hir:: ExprKind :: Yield ( yielded, hir:: YieldSource :: Yield )
1653+ let Some ( task_context_hid) = self . task_context else {
1654+ unreachable ! ( "use of `await` outside of an async context." ) ;
1655+ } ;
1656+ let task_context_ident = Ident :: with_dummy_span ( sym:: _task_context) ;
1657+ let lhs = self . expr_ident ( span, task_context_ident, task_context_hid) ;
1658+
1659+ hir:: ExprKind :: Assign ( lhs, yield_expr, self . lower_span ( span) )
1660+ } else {
1661+ hir:: ExprKind :: Yield ( yielded, hir:: YieldSource :: Yield )
1662+ }
16511663 }
16521664
16531665 /// Desugar `ExprForLoop` from: `[opt_ident]: for <pat> in <head> <body>` into:
0 commit comments