Skip to content

Commit d13ab28

Browse files
WIP
1 parent 7e7f42a commit d13ab28

File tree

2 files changed

+29
-14
lines changed

2 files changed

+29
-14
lines changed

tokio-macros/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ proc-macro = true
2424
[dependencies]
2525
proc-macro2 = "1.0.60"
2626
quote = "1"
27-
syn = { version = "2.0", features = ["full"] }
27+
syn = { version = "2.0", features = ["full", "extra-traits"] }
2828

2929
[dev-dependencies]
3030
tokio = { version = "1.0.0", path = "../tokio", features = ["full"] }

tokio-macros/src/entry.rs

+28-13
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use proc_macro2::{Span, TokenStream, TokenTree};
22
use quote::{quote, quote_spanned, ToTokens};
33
use syn::parse::{Parse, ParseStream, Parser};
4-
use syn::{braced, Attribute, Ident, Path, Signature, Visibility};
4+
use syn::{braced, parse_quote, Attribute, Ident, Path, Signature, Visibility};
55

66
// syn::AttributeArgs does not implement syn::Parse
77
type AttributeArgs = syn::punctuated::Punctuated<syn::Meta, syn::Token![,]>;
@@ -380,24 +380,39 @@ fn parse_knobs(mut input: ItemFn, is_test: bool, config: FinalConfig) -> TokenSt
380380
}
381381
};
382382

383+
let unit_type = Box::new(parse_quote! { () });
384+
let never_type = Box::new(parse_quote! { ! });
385+
383386
let output_type = match &input.sig.output {
384387
// For functions with no return value syn doesn't print anything,
385388
// but that doesn't work as `Output` for our boxed `Future`, so
386389
// default to `()` (the same type as the function output).
387-
syn::ReturnType::Default => quote! { () },
388-
syn::ReturnType::Type(_, ret_type) => quote! { #ret_type },
390+
syn::ReturnType::Default => &unit_type,
391+
// match on never type
392+
syn::ReturnType::Type(_, ret_type) if ret_type == &never_type => &unit_type,
393+
syn::ReturnType::Type(_, ret_type) => ret_type,
389394
};
390395

391-
input.stmts.last_mut().map(|stmt| {
392-
*stmt = quote! {
393-
let _lst_stmt: #output_type = {
394-
#stmt
395-
};
396-
_lst_stmt
397-
};
398-
});
396+
// input.stmts.last_mut().map(|stmt| {
397+
// if output_type != &unit_type {
398+
// *stmt = quote! {
399+
// let _lst_stmt: #output_type = {
400+
// #stmt
401+
// };
402+
// _lst_stmt
403+
// };
404+
// }
405+
// });
399406

400407
let body = input.body();
408+
let body = if output_type == &unit_type {
409+
quote! { #body }
410+
} else {
411+
quote! {
412+
let main_body: #output_type = #body;
413+
main_body
414+
}
415+
};
401416

402417
// For test functions pin the body to the stack and use `Pin<&mut dyn
403418
// Future>` to reduce the amount of `Runtime::block_on` (and related
@@ -410,13 +425,13 @@ fn parse_knobs(mut input: ItemFn, is_test: bool, config: FinalConfig) -> TokenSt
410425
// there will be no benefit.
411426
let body = if is_test {
412427
quote! {
413-
let body = async #body;
428+
let body = async { #body };
414429
#crate_path::pin!(body);
415430
let body: ::core::pin::Pin<&mut dyn ::core::future::Future<Output = #output_type>> = body;
416431
}
417432
} else {
418433
quote! {
419-
let body = async #body;
434+
let body = async { #body };
420435
}
421436
};
422437

0 commit comments

Comments
 (0)