@@ -917,12 +917,13 @@ impl<'hir> LoweringContext<'_, 'hir> {
917
917
let poll_expr = {
918
918
let awaitee = self . expr_ident ( span, awaitee_ident, awaitee_pat_hid) ;
919
919
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." ) ;
925
923
} ;
924
+
925
+ let task_context = self . expr_ident_mut ( span, task_context_ident, task_context_hid) ;
926
+
926
927
let new_unchecked = self . expr_call_lang_item_fn_mut (
927
928
span,
928
929
hir:: LangItem :: PinNewUnchecked ,
@@ -991,16 +992,14 @@ impl<'hir> LoweringContext<'_, 'hir> {
991
992
) ;
992
993
let yield_expr = self . arena . alloc ( yield_expr) ;
993
994
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)
1004
1003
} ;
1005
1004
1006
1005
let loop_block = self . block_all ( span, arena_vec ! [ self ; inner_match_stmt, yield_stmt] , None ) ;
@@ -1635,19 +1634,32 @@ impl<'hir> LoweringContext<'_, 'hir> {
1635
1634
}
1636
1635
} ;
1637
1636
1638
- let mut yielded =
1637
+ let yielded =
1639
1638
opt_expr. as_ref ( ) . map ( |x| self . lower_expr ( x) ) . unwrap_or_else ( || self . expr_unit ( span) ) ;
1640
1639
1641
1640
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 (
1644
1645
span,
1645
1646
hir:: LangItem :: AsyncGenReady ,
1646
1647
std:: slice:: from_ref ( yielded) ,
1647
1648
) ;
1648
- }
1649
+ let yield_expr = self . arena . alloc (
1650
+ self . expr ( span, hir:: ExprKind :: Yield ( wrapped_yielded, hir:: YieldSource :: Yield ) ) ,
1651
+ ) ;
1649
1652
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
+ }
1651
1663
}
1652
1664
1653
1665
/// Desugar `ExprForLoop` from: `[opt_ident]: for <pat> in <head> <body>` into:
0 commit comments