diff --git a/tokio-macros/src/entry.rs b/tokio-macros/src/entry.rs index 6631a76ad0c..65fd352b611 100644 --- a/tokio-macros/src/entry.rs +++ b/tokio-macros/src/entry.rs @@ -1,4 +1,4 @@ -use proc_macro2::{Span, TokenStream, TokenTree}; +use proc_macro2::{token_stream, Span, TokenStream, TokenTree}; use quote::{quote, quote_spanned, ToTokens}; use syn::parse::{Parse, ParseStream, Parser}; use syn::token::Brace; @@ -455,8 +455,16 @@ fn fn_without_args(mut input: ItemFn, is_test: bool, config: FinalConfig) -> Tok input.into_tokens(generated_attrs, last_block) } +fn has_self_keyword(mut iter: token_stream::IntoIter) -> bool { + iter.any(|tt| match tt { + TokenTree::Ident(ident) => ident == "Self", + TokenTree::Group(group) => has_self_keyword(group.stream().into_iter()), + _ => false, + }) +} + fn parse_knobs(mut input: ItemFn, is_test: bool, config: FinalConfig) -> TokenStream { - if input.sig.inputs.is_empty() { + if !has_self_keyword(input.body.clone().into_iter()) && input.sig.inputs.is_empty() { return fn_without_args(input, is_test, config); } diff --git a/tokio/tests/macros_test.rs b/tokio/tests/macros_test.rs index bed443cf293..2270c421e09 100644 --- a/tokio/tests/macros_test.rs +++ b/tokio/tests/macros_test.rs @@ -25,11 +25,19 @@ async fn unused_braces_test() { assert_eq!(1 + 1, 2) } #[std::prelude::v1::test] fn trait_method() { trait A { + fn _new() -> Self; fn f(self); fn g(self); } + impl A for () { + #[tokio::main] + async fn _new() { + let v: Self = (); + v + } + #[tokio::main] async fn f(self) { self.g()