@@ -465,32 +465,6 @@ impl<'a> LoweringContext<'a> {
465465 visit:: walk_pat ( self , p)
466466 }
467467
468- fn visit_fn ( & mut self , fk : visit:: FnKind < ' lcx > , fd : & ' lcx FnDecl , s : Span , _: NodeId ) {
469- if fk. header ( ) . map ( |h| h. asyncness . node . is_async ( ) ) . unwrap_or ( false ) {
470- // Don't visit the original pattern for async functions as it will be
471- // replaced.
472- for arg in & fd. inputs {
473- if let ArgSource :: AsyncFn ( pat) = & arg. source { self . visit_pat ( pat) ; }
474- self . visit_ty ( & arg. ty )
475- }
476- self . visit_fn_ret_ty ( & fd. output ) ;
477-
478- match fk {
479- visit:: FnKind :: ItemFn ( _, decl, _, body) => {
480- self . visit_fn_header ( decl) ;
481- self . visit_block ( body)
482- } ,
483- visit:: FnKind :: Method ( _, sig, _, body) => {
484- self . visit_fn_header ( & sig. header ) ;
485- self . visit_block ( body)
486- } ,
487- visit:: FnKind :: Closure ( body) => self . visit_expr ( body) ,
488- }
489- } else {
490- visit:: walk_fn ( self , fk, fd, s)
491- }
492- }
493-
494468 fn visit_item ( & mut self , item : & ' lcx Item ) {
495469 let hir_id = self . lctx . allocate_hir_id_counter ( item. id ) ;
496470
@@ -2266,17 +2240,10 @@ impl<'a> LoweringContext<'a> {
22662240 init : l. init . as_ref ( ) . map ( |e| P ( self . lower_expr ( e) ) ) ,
22672241 span : l. span ,
22682242 attrs : l. attrs . clone ( ) ,
2269- source : self . lower_local_source ( l . source ) ,
2243+ source : hir :: LocalSource :: Normal ,
22702244 } , ids)
22712245 }
22722246
2273- fn lower_local_source ( & mut self , ls : LocalSource ) -> hir:: LocalSource {
2274- match ls {
2275- LocalSource :: Normal => hir:: LocalSource :: Normal ,
2276- LocalSource :: AsyncFn => hir:: LocalSource :: AsyncFn ,
2277- }
2278- }
2279-
22802247 fn lower_mutability ( & mut self , m : Mutability ) -> hir:: Mutability {
22812248 match m {
22822249 Mutability :: Mutable => hir:: MutMutable ,
@@ -2292,14 +2259,7 @@ impl<'a> LoweringContext<'a> {
22922259 hir:: Arg {
22932260 hir_id : self . lower_node_id ( arg. id ) ,
22942261 pat : self . lower_pat ( & arg. pat ) ,
2295- source : self . lower_arg_source ( & arg. source ) ,
2296- }
2297- }
2298-
2299- fn lower_arg_source ( & mut self , source : & ArgSource ) -> hir:: ArgSource {
2300- match source {
2301- ArgSource :: Normal => hir:: ArgSource :: Normal ,
2302- ArgSource :: AsyncFn ( pat) => hir:: ArgSource :: AsyncFn ( self . lower_pat ( pat) ) ,
2262+ source : hir:: ArgSource :: Normal ,
23032263 }
23042264 }
23052265
@@ -3028,44 +2988,13 @@ impl<'a> LoweringContext<'a> {
30282988 fn lower_async_body (
30292989 & mut self ,
30302990 decl : & FnDecl ,
3031- asyncness : & IsAsync ,
2991+ asyncness : IsAsync ,
30322992 body : & Block ,
30332993 ) -> hir:: BodyId {
30342994 self . lower_body ( Some ( & decl) , |this| {
3035- if let IsAsync :: Async { closure_id, ref arguments, .. } = asyncness {
3036- let mut body = body. clone ( ) ;
3037-
3038- // Async function arguments are lowered into the closure body so that they are
3039- // captured and so that the drop order matches the equivalent non-async functions.
3040- //
3041- // async fn foo(<pattern>: <ty>, <pattern>: <ty>, <pattern>: <ty>) {
3042- // async move {
3043- // }
3044- // }
3045- //
3046- // // ...becomes...
3047- // fn foo(__arg0: <ty>, __arg1: <ty>, __arg2: <ty>) {
3048- // async move {
3049- // let __arg2 = __arg2;
3050- // let <pattern> = __arg2;
3051- // let __arg1 = __arg1;
3052- // let <pattern> = __arg1;
3053- // let __arg0 = __arg0;
3054- // let <pattern> = __arg0;
3055- // }
3056- // }
3057- //
3058- // If `<pattern>` is a simple ident, then it is lowered to a single
3059- // `let <pattern> = <pattern>;` statement as an optimization.
3060- for a in arguments. iter ( ) . rev ( ) {
3061- if let Some ( pat_stmt) = a. pat_stmt . clone ( ) {
3062- body. stmts . insert ( 0 , pat_stmt) ;
3063- }
3064- body. stmts . insert ( 0 , a. move_stmt . clone ( ) ) ;
3065- }
3066-
2995+ if let IsAsync :: Async { closure_id, .. } = asyncness {
30672996 let async_expr = this. make_async_expr (
3068- CaptureBy :: Value , * closure_id, None , body. span ,
2997+ CaptureBy :: Value , closure_id, None , body. span ,
30692998 |this| {
30702999 let body = this. lower_block ( & body, false ) ;
30713000 this. expr_block ( body, ThinVec :: new ( ) )
@@ -3126,47 +3055,26 @@ impl<'a> LoweringContext<'a> {
31263055 value
31273056 )
31283057 }
3129- ItemKind :: Fn ( ref decl, ref header, ref generics, ref body) => {
3058+ ItemKind :: Fn ( ref decl, header, ref generics, ref body) => {
31303059 let fn_def_id = self . resolver . definitions ( ) . local_def_id ( id) ;
31313060 self . with_new_scopes ( |this| {
3061+ // Note: we don't need to change the return type from `T` to
3062+ // `impl Future<Output = T>` here because lower_body
3063+ // only cares about the input argument patterns in the function
3064+ // declaration (decl), not the return types.
3065+ let body_id = this. lower_async_body ( decl, header. asyncness . node , body) ;
3066+ let ( generics, fn_decl) = this. add_in_band_defs (
3067+ generics,
3068+ fn_def_id,
3069+ AnonymousLifetimeMode :: PassThrough ,
3070+ |this, idty| this. lower_fn_decl (
3071+ decl,
3072+ Some ( ( fn_def_id, idty) ) ,
3073+ true ,
3074+ header. asyncness . node . opt_return_id ( )
3075+ ) ,
3076+ ) ;
31323077 this. current_item = Some ( ident. span ) ;
3133- let mut lower_fn = |decl : & FnDecl | {
3134- // Note: we don't need to change the return type from `T` to
3135- // `impl Future<Output = T>` here because lower_body
3136- // only cares about the input argument patterns in the function
3137- // declaration (decl), not the return types.
3138- let body_id = this. lower_async_body ( & decl, & header. asyncness . node , body) ;
3139-
3140- let ( generics, fn_decl) = this. add_in_band_defs (
3141- generics,
3142- fn_def_id,
3143- AnonymousLifetimeMode :: PassThrough ,
3144- |this, idty| this. lower_fn_decl (
3145- & decl,
3146- Some ( ( fn_def_id, idty) ) ,
3147- true ,
3148- header. asyncness . node . opt_return_id ( )
3149- ) ,
3150- ) ;
3151-
3152- ( body_id, generics, fn_decl)
3153- } ;
3154-
3155- let ( body_id, generics, fn_decl) = if let IsAsync :: Async {
3156- arguments, ..
3157- } = & header. asyncness . node {
3158- let mut decl = decl. clone ( ) ;
3159- // Replace the arguments of this async function with the generated
3160- // arguments that will be moved into the closure.
3161- for ( i, a) in arguments. clone ( ) . drain ( ..) . enumerate ( ) {
3162- if let Some ( arg) = a. arg {
3163- decl. inputs [ i] = arg;
3164- }
3165- }
3166- lower_fn ( & decl)
3167- } else {
3168- lower_fn ( decl)
3169- } ;
31703078
31713079 hir:: ItemKind :: Fn (
31723080 fn_decl,
@@ -3638,36 +3546,15 @@ impl<'a> LoweringContext<'a> {
36383546 )
36393547 }
36403548 ImplItemKind :: Method ( ref sig, ref body) => {
3641- let mut lower_method = |sig : & MethodSig | {
3642- let body_id = self . lower_async_body (
3643- & sig. decl , & sig. header . asyncness . node , body
3644- ) ;
3645- let impl_trait_return_allow = !self . is_in_trait_impl ;
3646- let ( generics, sig) = self . lower_method_sig (
3647- & i. generics ,
3648- sig,
3649- impl_item_def_id,
3650- impl_trait_return_allow,
3651- sig. header . asyncness . node . opt_return_id ( ) ,
3652- ) ;
3653- ( body_id, generics, sig)
3654- } ;
3655-
3656- let ( body_id, generics, sig) = if let IsAsync :: Async {
3657- ref arguments, ..
3658- } = sig. header . asyncness . node {
3659- let mut sig = sig. clone ( ) ;
3660- // Replace the arguments of this async function with the generated
3661- // arguments that will be moved into the closure.
3662- for ( i, a) in arguments. clone ( ) . drain ( ..) . enumerate ( ) {
3663- if let Some ( arg) = a. arg {
3664- sig. decl . inputs [ i] = arg;
3665- }
3666- }
3667- lower_method ( & sig)
3668- } else {
3669- lower_method ( sig)
3670- } ;
3549+ let body_id = self . lower_async_body ( & sig. decl , sig. header . asyncness . node , body) ;
3550+ let impl_trait_return_allow = !self . is_in_trait_impl ;
3551+ let ( generics, sig) = self . lower_method_sig (
3552+ & i. generics ,
3553+ sig,
3554+ impl_item_def_id,
3555+ impl_trait_return_allow,
3556+ sig. header . asyncness . node . opt_return_id ( ) ,
3557+ ) ;
36713558 self . current_item = Some ( i. span ) ;
36723559
36733560 ( generics, hir:: ImplItemKind :: Method ( sig, body_id) )
@@ -3860,7 +3747,7 @@ impl<'a> LoweringContext<'a> {
38603747 impl_trait_return_allow : bool ,
38613748 is_async : Option < NodeId > ,
38623749 ) -> ( hir:: Generics , hir:: MethodSig ) {
3863- let header = self . lower_fn_header ( & sig. header ) ;
3750+ let header = self . lower_fn_header ( sig. header ) ;
38643751 let ( generics, decl) = self . add_in_band_defs (
38653752 generics,
38663753 fn_def_id,
@@ -3882,10 +3769,10 @@ impl<'a> LoweringContext<'a> {
38823769 }
38833770 }
38843771
3885- fn lower_fn_header ( & mut self , h : & FnHeader ) -> hir:: FnHeader {
3772+ fn lower_fn_header ( & mut self , h : FnHeader ) -> hir:: FnHeader {
38863773 hir:: FnHeader {
38873774 unsafety : self . lower_unsafety ( h. unsafety ) ,
3888- asyncness : self . lower_asyncness ( & h. asyncness . node ) ,
3775+ asyncness : self . lower_asyncness ( h. asyncness . node ) ,
38893776 constness : self . lower_constness ( h. constness ) ,
38903777 abi : h. abi ,
38913778 }
@@ -3905,7 +3792,7 @@ impl<'a> LoweringContext<'a> {
39053792 }
39063793 }
39073794
3908- fn lower_asyncness ( & mut self , a : & IsAsync ) -> hir:: IsAsync {
3795+ fn lower_asyncness ( & mut self , a : IsAsync ) -> hir:: IsAsync {
39093796 match a {
39103797 IsAsync :: Async { .. } => hir:: IsAsync :: Async ,
39113798 IsAsync :: NotAsync => hir:: IsAsync :: NotAsync ,
@@ -4222,7 +4109,7 @@ impl<'a> LoweringContext<'a> {
42224109 }
42234110 ExprKind :: Await ( _origin, ref expr) => self . lower_await ( e. span , expr) ,
42244111 ExprKind :: Closure (
4225- capture_clause, ref asyncness, movability, ref decl, ref body, fn_decl_span
4112+ capture_clause, asyncness, movability, ref decl, ref body, fn_decl_span
42264113 ) => {
42274114 if let IsAsync :: Async { closure_id, .. } = asyncness {
42284115 let outer_decl = FnDecl {
@@ -4260,7 +4147,7 @@ impl<'a> LoweringContext<'a> {
42604147 Some ( & * * ty)
42614148 } else { None } ;
42624149 let async_body = this. make_async_expr (
4263- capture_clause, * closure_id, async_ret_ty, body. span ,
4150+ capture_clause, closure_id, async_ret_ty, body. span ,
42644151 |this| {
42654152 this. with_new_scopes ( |this| this. lower_expr ( body) )
42664153 } ) ;
0 commit comments