Skip to content

Commit

Permalink
fixup! macros: append generated test attribute so others are aware of it
Browse files Browse the repository at this point in the history
  • Loading branch information
kezhuw committed Apr 19, 2024
1 parent d6c9bc2 commit 81e10af
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 13 deletions.
8 changes: 4 additions & 4 deletions tests-build/tests/fail/macros_invalid_input.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ error: Failed to parse value of `crate` as path: "456"
41 | #[tokio::test(crate = "456")]
| ^^^^^

error: second test attribute is supplied, try to order it after `tokio::test`
error: second test attribute is supplied, consider removing it or ordering it after `tokio::test`
--> $DIR/macros_invalid_input.rs:45:1
|
45 | #[test]
Expand All @@ -94,16 +94,16 @@ note: the lint level is defined here
1 | #![deny(duplicate_macro_attributes)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^

error: second test attribute is supplied, try to order it after `tokio::test`
error: second test attribute is supplied, consider removing it or ordering it after `tokio::test`
--> $DIR/macros_invalid_input.rs:49:1
|
49 | #[::core::prelude::v1::test::test]
49 | #[::core::prelude::v1::test]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: duplicated attribute
--> $DIR/macros_invalid_input.rs:49:1
|
49 | #[::core::prelude::v1::test::test]
49 | #[::core::prelude::v1::test]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
note: the lint level is defined here
Expand Down
2 changes: 1 addition & 1 deletion tokio-macros/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ proc-macro = true
[dependencies]
proc-macro2 = "1.0.60"
quote = "1"
syn = { version = "2.0", features = ["full", "extra-traits"] }
syn = { version = "2.0", features = ["full"] }

[dev-dependencies]
tokio = { version = "1.0.0", path = "../tokio", features = ["full"] }
Expand Down
30 changes: 22 additions & 8 deletions tokio-macros/src/entry.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use proc_macro2::{Span, TokenStream, TokenTree};
use quote::{quote, quote_spanned, ToTokens};
use syn::parse::{Parse, ParseStream, Parser};
use syn::{braced, parse_quote, Attribute, Ident, Path, Signature, Visibility};
use syn::{braced, Attribute, Ident, Path, Signature, Visibility};

// syn::AttributeArgs does not implement syn::Parse
type AttributeArgs = syn::punctuated::Punctuated<syn::Meta, syn::Token![,]>;
Expand Down Expand Up @@ -443,6 +443,25 @@ pub(crate) fn main(args: TokenStream, item: TokenStream, rt_multi_thread: bool)
}
}

// Check whether given attribute is `#[test]` or `#[::core::prelude::v1::test]`.
fn is_test_attribute(attr: &Attribute) -> bool {
let syn::Meta::Path(ref path) = attr.meta else {
return false;
};
let segments = ["core", "prelude", "v1", "test"];
if path.leading_colon.is_none() {
return path.segments.len() == 1
&& path.segments[0].arguments.is_none()
&& path.segments[0].ident == "test";
} else if path.segments.len() != segments.len() {
return false;
}
path.segments
.iter()
.zip(segments.into_iter())
.all(|(segment, path)| segment.arguments.is_none() && segment.ident == path)
}

pub(crate) fn test(args: TokenStream, item: TokenStream, rt_multi_thread: bool) -> TokenStream {
// If any of the steps for this macro fail, we still want to expand to an item that is as close
// to the expected output as possible. This helps out IDEs such that completions and other
Expand All @@ -451,13 +470,8 @@ pub(crate) fn test(args: TokenStream, item: TokenStream, rt_multi_thread: bool)
Ok(it) => it,
Err(e) => return token_stream_with_error(item, e),
};
let test_attr: Attribute = parse_quote! { #[test] };
let qualified_test_attr = parse_quote! { #[::core::prelude::v1::test] };
let config = if let Some(attr) = input
.attrs()
.find(|attr| **attr == test_attr || **attr == qualified_test_attr)
{
let msg = "second test attribute is supplied, try to order it after `tokio::test`";
let config = if let Some(attr) = input.attrs().find(|attr| is_test_attribute(*attr)) {
let msg = "second test attribute is supplied, consider removing it or ordering it after `tokio::test`";
Err(syn::Error::new_spanned(attr, msg))
} else {
AttributeArgs::parse_terminated
Expand Down

0 comments on commit 81e10af

Please sign in to comment.