@@ -295,8 +295,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
295295 ExprKind :: Index ( ref base, ref idx) => {
296296 self . check_expr_index ( base, idx, needs, expr)
297297 }
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 )
300300 }
301301 hir:: ExprKind :: Err => {
302302 tcx. types . err
@@ -1541,12 +1541,24 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
15411541 }
15421542 }
15431543
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 > {
15451550 match self . yield_ty {
15461551 Some ( ty) => {
15471552 self . check_expr_coercable_to_type ( & value, ty) ;
15481553 }
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+ _ => {
15501562 struct_span_err ! ( self . tcx. sess, expr. span, E0627 ,
15511563 "yield statement outside of generator literal" ) . emit ( ) ;
15521564 }
0 commit comments