From 28ad5f98833e3bc6ea3de5a74dea548d075d3da5 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Sat, 6 Mar 2021 21:01:44 -0800 Subject: [PATCH 1/3] Add regression test for issue 149 --- tests/test.rs | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/tests/test.rs b/tests/test.rs index a97af36..06c0918 100644 --- a/tests/test.rs +++ b/tests/test.rs @@ -1258,3 +1258,23 @@ pub mod issue147 { } } } + +// https://github.com/dtolnay/async-trait/issues/149 +pub mod issue149 { + use async_trait::async_trait; + + pub struct Thing; + pub trait Ret {} + impl Ret for Thing {} + + pub async fn ok() -> &'static dyn Ret { + return &Thing; + } + + #[async_trait] + pub trait Trait { + async fn fail() -> &'static dyn Ret { + return &Thing; + } + } +} From c8b4dc52e72a46cd9fd12a276ec432748afd5cbb Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Sat, 6 Mar 2021 21:05:39 -0800 Subject: [PATCH 2/3] Prepend early return to force unsize coercion --- src/expand.rs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/expand.rs b/src/expand.rs index b0ebb4c..2b000ab 100644 --- a/src/expand.rs +++ b/src/expand.rs @@ -345,6 +345,10 @@ fn transform_block(sig: &mut Signature, block: &mut Block) { } let stmts = &block.stmts; + let ret = match &sig.output { + ReturnType::Default => quote!(()), + ReturnType::Type(_, ret) => quote!(#ret), + }; let let_ret = match &sig.output { ReturnType::Default => quote_spanned! {block.brace_token.span=> let _: () = { #(#decls)* #(#stmts)* }; @@ -356,7 +360,12 @@ fn transform_block(sig: &mut Signature, block: &mut Block) { }, }; let box_pin = quote_spanned!(block.brace_token.span=> - Box::pin(async move { #let_ret }) + Box::pin(async move { + if let ::core::option::Option::Some(__ret) = ::core::option::Option::None::<#ret> { + return __ret; + } + #let_ret + }) ); block.stmts = parse_quote!(#box_pin); } From 3596fe77343155543e3baab4e2ff6ef7e3b2d19c Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Sat, 6 Mar 2021 21:09:32 -0800 Subject: [PATCH 3/3] Remove unneeded early return from unit case --- src/expand.rs | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/src/expand.rs b/src/expand.rs index 2b000ab..06c4ba3 100644 --- a/src/expand.rs +++ b/src/expand.rs @@ -345,27 +345,21 @@ fn transform_block(sig: &mut Signature, block: &mut Block) { } let stmts = &block.stmts; - let ret = match &sig.output { - ReturnType::Default => quote!(()), - ReturnType::Type(_, ret) => quote!(#ret), - }; let let_ret = match &sig.output { ReturnType::Default => quote_spanned! {block.brace_token.span=> let _: () = { #(#decls)* #(#stmts)* }; }, ReturnType::Type(_, ret) => quote_spanned! {block.brace_token.span=> + if let ::core::option::Option::Some(__ret) = ::core::option::Option::None::<#ret> { + return __ret; + } let __ret: #ret = { #(#decls)* #(#stmts)* }; #[allow(unreachable_code)] __ret }, }; let box_pin = quote_spanned!(block.brace_token.span=> - Box::pin(async move { - if let ::core::option::Option::Some(__ret) = ::core::option::Option::None::<#ret> { - return __ret; - } - #let_ret - }) + Box::pin(async move { #let_ret }) ); block.stmts = parse_quote!(#box_pin); }