@@ -148,15 +148,16 @@ impl<'hir> LoweringContext<'_, 'hir> {
148
148
self . arena . alloc_from_iter ( arms. iter ( ) . map ( |x| self . lower_arm ( x) ) ) ,
149
149
hir:: MatchSource :: Normal ,
150
150
) ,
151
- ExprKind :: Async ( capture_clause, closure_node_id, ref block) => self
152
- . make_async_expr (
151
+ ExprKind :: Async ( capture_clause, closure_node_id, ref block) => {
152
+ return self . make_async_expr (
153
153
capture_clause,
154
154
closure_node_id,
155
155
None ,
156
- block . span ,
156
+ e . span ,
157
157
hir:: AsyncGeneratorKind :: Block ,
158
158
|this| this. with_new_scopes ( |this| this. lower_block_expr ( block) ) ,
159
- ) ,
159
+ ) ;
160
+ }
160
161
ExprKind :: Await ( ref expr) => {
161
162
let dot_await_span = if expr. span . hi ( ) < e. span . hi ( ) {
162
163
let span_with_whitespace = self
@@ -575,14 +576,14 @@ impl<'hir> LoweringContext<'_, 'hir> {
575
576
}
576
577
}
577
578
578
- /// Lower an `async` construct to a generator that is then wrapped so it implements `Future`.
579
+ /// Lower an `async` construct to a generator that implements `Future`.
579
580
///
580
581
/// This results in:
581
582
///
582
583
/// ```text
583
- /// std::future::from_generator( static move? |_task_context| -> <ret_ty> {
584
+ /// static move? |_task_context| -> <ret_ty> {
584
585
/// <body>
585
- /// })
586
+ /// }
586
587
/// ```
587
588
pub ( super ) fn make_async_expr (
588
589
& mut self ,
@@ -592,20 +593,22 @@ impl<'hir> LoweringContext<'_, 'hir> {
592
593
span : Span ,
593
594
async_gen_kind : hir:: AsyncGeneratorKind ,
594
595
body : impl FnOnce ( & mut Self ) -> hir:: Expr < ' hir > ,
595
- ) -> hir:: ExprKind < ' hir > {
596
+ ) -> hir:: Expr < ' hir > {
596
597
let output = match ret_ty {
597
598
Some ( ty) => hir:: FnRetTy :: Return (
598
599
self . lower_ty ( & ty, & ImplTraitContext :: Disallowed ( ImplTraitPosition :: AsyncBlock ) ) ,
599
600
) ,
600
601
None => hir:: FnRetTy :: DefaultReturn ( self . lower_span ( span) ) ,
601
602
} ;
602
603
603
- // Resume argument type. We let the compiler infer this to simplify the lowering. It is
604
- // fully constrained by `future::from_generator`.
604
+ // Resume argument type: `ResumeTy`
605
+ let unstable_span =
606
+ self . mark_span_with_reason ( DesugaringKind :: Async , span, self . allow_gen_future . clone ( ) ) ;
607
+ let resume_ty = hir:: QPath :: LangItem ( hir:: LangItem :: ResumeTy , unstable_span, None ) ;
605
608
let input_ty = hir:: Ty {
606
609
hir_id : self . next_id ( ) ,
607
- kind : hir:: TyKind :: Infer ,
608
- span : self . lower_span ( span ) ,
610
+ kind : hir:: TyKind :: Path ( resume_ty ) ,
611
+ span : unstable_span ,
609
612
} ;
610
613
611
614
// The closure/generator `FnDecl` takes a single (resume) argument of type `input_ty`.
@@ -688,16 +691,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
688
691
689
692
let generator = hir:: Expr { hir_id, kind : generator_kind, span : self . lower_span ( span) } ;
690
693
691
- // `future::from_generator`:
692
- let gen_future = self . expr_lang_item_path (
693
- unstable_span,
694
- hir:: LangItem :: FromGenerator ,
695
- AttrVec :: new ( ) ,
696
- None ,
697
- ) ;
698
-
699
- // `future::from_generator(generator)`:
700
- hir:: ExprKind :: Call ( self . arena . alloc ( gen_future) , arena_vec ! [ self ; generator] )
694
+ generator
701
695
}
702
696
703
697
/// Desugar `<expr>.await` into:
@@ -1001,7 +995,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
1001
995
}
1002
996
1003
997
// Transform `async |x: u8| -> X { ... }` into
1004
- // `|x: u8| future_from_generator( || -> X { ... }) `.
998
+ // `|x: u8| || -> X { ... }`.
1005
999
let body_id = this. lower_fn_body ( & outer_decl, |this| {
1006
1000
let async_ret_ty =
1007
1001
if let FnRetTy :: Ty ( ty) = & decl. output { Some ( ty. clone ( ) ) } else { None } ;
@@ -1013,7 +1007,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
1013
1007
hir:: AsyncGeneratorKind :: Closure ,
1014
1008
|this| this. with_new_scopes ( |this| this. lower_expr_mut ( body) ) ,
1015
1009
) ;
1016
- this . expr ( fn_decl_span , async_body, AttrVec :: new ( ) )
1010
+ async_body
1017
1011
} ) ;
1018
1012
body_id
1019
1013
} ) ;
0 commit comments