@@ -764,10 +764,28 @@ impl<'hir> LoweringContext<'_, 'hir> {
764
764
Some ( hir:: CoroutineKind :: Coroutine ( _) )
765
765
| Some ( hir:: CoroutineKind :: Desugared ( hir:: CoroutineDesugaring :: Gen , _) )
766
766
| None => {
767
- return hir:: ExprKind :: Err ( self . dcx ( ) . emit_err ( AwaitOnlyInAsyncFnAndBlocks {
768
- await_kw_span,
769
- item_span : self . current_item ,
770
- } ) ) ;
767
+ // Lower to a block `{ EXPR; <error> }` so that the awaited expr
768
+ // is not accidentally orphaned.
769
+ let stmt_id = self . next_id ( ) ;
770
+ let expr_err = self . expr (
771
+ expr. span ,
772
+ hir:: ExprKind :: Err ( self . dcx ( ) . emit_err ( AwaitOnlyInAsyncFnAndBlocks {
773
+ await_kw_span,
774
+ item_span : self . current_item ,
775
+ } ) ) ,
776
+ ) ;
777
+ return hir:: ExprKind :: Block (
778
+ self . block_all (
779
+ expr. span ,
780
+ arena_vec ! [ self ; hir:: Stmt {
781
+ hir_id: stmt_id,
782
+ kind: hir:: StmtKind :: Semi ( expr) ,
783
+ span: expr. span,
784
+ } ] ,
785
+ Some ( self . arena . alloc ( expr_err) ) ,
786
+ ) ,
787
+ None ,
788
+ ) ;
771
789
}
772
790
} ;
773
791
@@ -1500,12 +1518,31 @@ impl<'hir> LoweringContext<'_, 'hir> {
1500
1518
}
1501
1519
1502
1520
fn lower_expr_yield ( & mut self , span : Span , opt_expr : Option < & Expr > ) -> hir:: ExprKind < ' hir > {
1521
+ let yielded =
1522
+ opt_expr. as_ref ( ) . map ( |x| self . lower_expr ( x) ) . unwrap_or_else ( || self . expr_unit ( span) ) ;
1523
+
1503
1524
let is_async_gen = match self . coroutine_kind {
1504
1525
Some ( hir:: CoroutineKind :: Desugared ( hir:: CoroutineDesugaring :: Gen , _) ) => false ,
1505
1526
Some ( hir:: CoroutineKind :: Desugared ( hir:: CoroutineDesugaring :: AsyncGen , _) ) => true ,
1506
1527
Some ( hir:: CoroutineKind :: Desugared ( hir:: CoroutineDesugaring :: Async , _) ) => {
1507
- return hir:: ExprKind :: Err (
1508
- self . dcx ( ) . emit_err ( AsyncCoroutinesNotSupported { span } ) ,
1528
+ // Lower to a block `{ EXPR; <error> }` so that the awaited expr
1529
+ // is not accidentally orphaned.
1530
+ let stmt_id = self . next_id ( ) ;
1531
+ let expr_err = self . expr (
1532
+ yielded. span ,
1533
+ hir:: ExprKind :: Err ( self . dcx ( ) . emit_err ( AsyncCoroutinesNotSupported { span } ) ) ,
1534
+ ) ;
1535
+ return hir:: ExprKind :: Block (
1536
+ self . block_all (
1537
+ yielded. span ,
1538
+ arena_vec ! [ self ; hir:: Stmt {
1539
+ hir_id: stmt_id,
1540
+ kind: hir:: StmtKind :: Semi ( yielded) ,
1541
+ span: yielded. span,
1542
+ } ] ,
1543
+ Some ( self . arena . alloc ( expr_err) ) ,
1544
+ ) ,
1545
+ None ,
1509
1546
) ;
1510
1547
}
1511
1548
Some ( hir:: CoroutineKind :: Coroutine ( _) ) => {
@@ -1535,9 +1572,6 @@ impl<'hir> LoweringContext<'_, 'hir> {
1535
1572
}
1536
1573
} ;
1537
1574
1538
- let yielded =
1539
- opt_expr. as_ref ( ) . map ( |x| self . lower_expr ( x) ) . unwrap_or_else ( || self . expr_unit ( span) ) ;
1540
-
1541
1575
if is_async_gen {
1542
1576
// `yield $expr` is transformed into `task_context = yield async_gen_ready($expr)`.
1543
1577
// This ensures that we store our resumed `ResumeContext` correctly, and also that
0 commit comments