@@ -295,8 +295,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
295
295
ExprKind :: Index ( ref base, ref idx) => {
296
296
self . check_expr_index ( base, idx, needs, expr)
297
297
}
298
- ExprKind :: Yield ( ref value, _ ) => {
299
- self . check_expr_yield ( value, expr)
298
+ ExprKind :: Yield ( ref value, ref src ) => {
299
+ self . check_expr_yield ( value, expr, src )
300
300
}
301
301
hir:: ExprKind :: Err => {
302
302
tcx. types . err
@@ -1541,12 +1541,24 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
1541
1541
}
1542
1542
}
1543
1543
1544
- fn check_expr_yield ( & self , value : & ' tcx hir:: Expr , expr : & ' tcx hir:: Expr ) -> Ty < ' tcx > {
1544
+ fn check_expr_yield (
1545
+ & self ,
1546
+ value : & ' tcx hir:: Expr ,
1547
+ expr : & ' tcx hir:: Expr ,
1548
+ src : & ' tcx hir:: YieldSource
1549
+ ) -> Ty < ' tcx > {
1545
1550
match self . yield_ty {
1546
1551
Some ( ty) => {
1547
1552
self . check_expr_coercable_to_type ( & value, ty) ;
1548
1553
}
1549
- None => {
1554
+ // Given that this `yield` expression was generated as a result of lowering a `.await`,
1555
+ // we know that the yield type must be `()`; however, the context won't contain this
1556
+ // information. Hence, we check the source of the yield expression here and check its
1557
+ // value's type against `()` (this check should always hold).
1558
+ None if src == & hir:: YieldSource :: Await => {
1559
+ self . check_expr_coercable_to_type ( & value, self . tcx . mk_unit ( ) ) ;
1560
+ }
1561
+ _ => {
1550
1562
struct_span_err ! ( self . tcx. sess, expr. span, E0627 ,
1551
1563
"yield statement outside of generator literal" ) . emit ( ) ;
1552
1564
}
0 commit comments