@@ -32,7 +32,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
32
32
pub ( super ) fn lower_expr_mut ( & mut self , e : & Expr ) -> hir:: Expr < ' hir > {
33
33
ensure_sufficient_stack ( || {
34
34
match & e. kind {
35
- // Paranthesis expression does not have a HirId and is handled specially.
35
+ // Parenthesis expression does not have a HirId and is handled specially.
36
36
ExprKind :: Paren ( ex) => {
37
37
let mut ex = self . lower_expr_mut ( ex) ;
38
38
// Include parens in span, but only if it is a super-span.
@@ -63,6 +63,20 @@ impl<'hir> LoweringContext<'_, 'hir> {
63
63
ExprKind :: ForLoop ( pat, head, body, opt_label) => {
64
64
return self . lower_expr_for ( e, pat, head, body, * opt_label) ;
65
65
}
66
+ // Similarly, async blocks do not use `e.id` but rather `closure_node_id`.
67
+ ExprKind :: Async ( capture_clause, closure_node_id, block) => {
68
+ let hir_id = self . lower_node_id ( * closure_node_id) ;
69
+ self . lower_attrs ( hir_id, & e. attrs ) ;
70
+ return self . make_async_expr (
71
+ * capture_clause,
72
+ hir_id,
73
+ * closure_node_id,
74
+ None ,
75
+ e. span ,
76
+ hir:: AsyncGeneratorKind :: Block ,
77
+ |this| this. with_new_scopes ( |this| this. lower_block_expr ( block) ) ,
78
+ ) ;
79
+ }
66
80
_ => ( ) ,
67
81
}
68
82
@@ -173,15 +187,6 @@ impl<'hir> LoweringContext<'_, 'hir> {
173
187
self . arena . alloc_from_iter ( arms. iter ( ) . map ( |x| self . lower_arm ( x) ) ) ,
174
188
hir:: MatchSource :: Normal ,
175
189
) ,
176
- ExprKind :: Async ( capture_clause, closure_node_id, block) => self . make_async_expr (
177
- * capture_clause,
178
- hir_id,
179
- * closure_node_id,
180
- None ,
181
- e. span ,
182
- hir:: AsyncGeneratorKind :: Block ,
183
- |this| this. with_new_scopes ( |this| this. lower_block_expr ( block) ) ,
184
- ) ,
185
190
ExprKind :: Await ( expr) => {
186
191
let dot_await_span = if expr. span . hi ( ) < e. span . hi ( ) {
187
192
let span_with_whitespace = self
@@ -315,7 +320,9 @@ impl<'hir> LoweringContext<'_, 'hir> {
315
320
) ,
316
321
ExprKind :: Try ( sub_expr) => self . lower_expr_try ( e. span , sub_expr) ,
317
322
318
- ExprKind :: Paren ( _) | ExprKind :: ForLoop ( ..) => unreachable ! ( "already handled" ) ,
323
+ ExprKind :: Paren ( _) | ExprKind :: ForLoop ( ..) | ExprKind :: Async ( ..) => {
324
+ unreachable ! ( "already handled" )
325
+ }
319
326
320
327
ExprKind :: MacCall ( _) => panic ! ( "{:?} shouldn't exist here" , e. span) ,
321
328
} ;
@@ -577,9 +584,9 @@ impl<'hir> LoweringContext<'_, 'hir> {
577
584
/// This results in:
578
585
///
579
586
/// ```text
580
- /// std::future::identity_future( static move? |_task_context| -> <ret_ty> {
587
+ /// static move? |_task_context| -> <ret_ty> {
581
588
/// <body>
582
- /// })
589
+ /// }
583
590
/// ```
584
591
pub ( super ) fn make_async_expr (
585
592
& mut self ,
@@ -590,7 +597,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
590
597
span : Span ,
591
598
async_gen_kind : hir:: AsyncGeneratorKind ,
592
599
body : impl FnOnce ( & mut Self ) -> hir:: Expr < ' hir > ,
593
- ) -> hir:: ExprKind < ' hir > {
600
+ ) -> hir:: Expr < ' hir > {
594
601
let output = ret_ty. unwrap_or_else ( || hir:: FnRetTy :: DefaultReturn ( self . lower_span ( span) ) ) ;
595
602
596
603
// Resume argument type: `ResumeTy`
@@ -655,13 +662,12 @@ impl<'hir> LoweringContext<'_, 'hir> {
655
662
} ;
656
663
657
664
let hir_id = self . lower_node_id ( closure_node_id) ;
658
- let unstable_span =
659
- self . mark_span_with_reason ( DesugaringKind :: Async , span, self . allow_gen_future . clone ( ) ) ;
660
-
661
665
if self . tcx . features ( ) . closure_track_caller
662
666
&& let Some ( attrs) = self . attrs . get ( & outer_hir_id. local_id )
663
667
&& attrs. into_iter ( ) . any ( |attr| attr. has_name ( sym:: track_caller) )
664
668
{
669
+ let unstable_span =
670
+ self . mark_span_with_reason ( DesugaringKind :: Async , span, self . allow_gen_future . clone ( ) ) ;
665
671
self . lower_attrs (
666
672
hir_id,
667
673
& [ Attribute {
@@ -680,22 +686,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
680
686
) ;
681
687
}
682
688
683
- let generator = hir:: Expr { hir_id, kind : generator_kind, span : self . lower_span ( span) } ;
684
-
685
- // FIXME(swatinem):
686
- // For some reason, the async block needs to flow through *any*
687
- // call (like the identity function), as otherwise type and lifetime
688
- // inference have a hard time figuring things out.
689
- // Without this, we would get:
690
- // E0720 in tests/ui/impl-trait/in-trait/default-body-with-rpit.rs
691
- // E0700 in tests/ui/self/self_lifetime-async.rs
692
-
693
- // `future::identity_future`:
694
- let identity_future =
695
- self . expr_lang_item_path ( unstable_span, hir:: LangItem :: IdentityFuture , None ) ;
696
-
697
- // `future::identity_future(generator)`:
698
- hir:: ExprKind :: Call ( self . arena . alloc ( identity_future) , arena_vec ! [ self ; generator] )
689
+ hir:: Expr { hir_id, kind : generator_kind, span : self . lower_span ( span) }
699
690
}
700
691
701
692
/// Desugar `<expr>.await` into:
@@ -1001,7 +992,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
1001
992
}
1002
993
1003
994
// Transform `async |x: u8| -> X { ... }` into
1004
- // `|x: u8| identity_future( || -> X { ... }) `.
995
+ // `|x: u8| || -> X { ... }`.
1005
996
let body_id = this. lower_fn_body ( & outer_decl, |this| {
1006
997
let async_ret_ty = if let FnRetTy :: Ty ( ty) = & decl. output {
1007
998
let itctx = ImplTraitContext :: Disallowed ( ImplTraitPosition :: AsyncBlock ) ;
@@ -1010,16 +1001,15 @@ impl<'hir> LoweringContext<'_, 'hir> {
1010
1001
None
1011
1002
} ;
1012
1003
1013
- let async_body = this. make_async_expr (
1004
+ this. make_async_expr (
1014
1005
capture_clause,
1015
1006
closure_hir_id,
1016
1007
inner_closure_id,
1017
1008
async_ret_ty,
1018
1009
body. span ,
1019
1010
hir:: AsyncGeneratorKind :: Closure ,
1020
1011
|this| this. with_new_scopes ( |this| this. lower_expr_mut ( body) ) ,
1021
- ) ;
1022
- this. expr ( fn_decl_span, async_body)
1012
+ )
1023
1013
} ) ;
1024
1014
body_id
1025
1015
} ) ;
0 commit comments