macros: accept path as crate rename (#5557)

This commit is contained in:
daxpedda
2023-03-19 12:28:43 +01:00
committed by GitHub
parent 0a93ed7e7a
commit 17cc283f58
4 changed files with 34 additions and 34 deletions
@@ -36,13 +36,10 @@ async fn test_worker_threads_not_int() {}
async fn test_worker_threads_and_current_thread() {} async fn test_worker_threads_and_current_thread() {}
#[tokio::test(crate = 456)] #[tokio::test(crate = 456)]
async fn test_crate_not_ident_int() {} async fn test_crate_not_path_int() {}
#[tokio::test(crate = "456")] #[tokio::test(crate = "456")]
async fn test_crate_not_ident_invalid() {} async fn test_crate_not_path_invalid() {}
#[tokio::test(crate = "abc::edf")]
async fn test_crate_not_ident_path() {}
#[tokio::test] #[tokio::test]
#[test] #[test]
@@ -64,34 +64,28 @@ error: The `worker_threads` option requires the `multi_thread` runtime flavor. U
35 | #[tokio::test(flavor = "current_thread", worker_threads = 4)] 35 | #[tokio::test(flavor = "current_thread", worker_threads = 4)]
| ^ | ^
error: Failed to parse value of `crate` as ident. error: Failed to parse value of `crate` as path.
--> $DIR/macros_invalid_input.rs:38:23 --> $DIR/macros_invalid_input.rs:38:23
| |
38 | #[tokio::test(crate = 456)] 38 | #[tokio::test(crate = 456)]
| ^^^ | ^^^
error: Failed to parse value of `crate` as ident: "456" error: Failed to parse value of `crate` as path: "456"
--> $DIR/macros_invalid_input.rs:41:23 --> $DIR/macros_invalid_input.rs:41:23
| |
41 | #[tokio::test(crate = "456")] 41 | #[tokio::test(crate = "456")]
| ^^^^^ | ^^^^^
error: Failed to parse value of `crate` as ident: "abc::edf"
--> $DIR/macros_invalid_input.rs:44:23
|
44 | #[tokio::test(crate = "abc::edf")]
| ^^^^^^^^^^
error: second test attribute is supplied error: second test attribute is supplied
--> $DIR/macros_invalid_input.rs:48:1 --> $DIR/macros_invalid_input.rs:45:1
| |
48 | #[test] 45 | #[test]
| ^^^^^^^ | ^^^^^^^
error: duplicated attribute error: duplicated attribute
--> $DIR/macros_invalid_input.rs:48:1 --> $DIR/macros_invalid_input.rs:45:1
| |
48 | #[test] 45 | #[test]
| ^^^^^^^ | ^^^^^^^
| |
note: the lint level is defined here note: the lint level is defined here
+17 -17
View File
@@ -1,7 +1,7 @@
use proc_macro::TokenStream; use proc_macro::TokenStream;
use proc_macro2::{Ident, Span}; use proc_macro2::Span;
use quote::{quote, quote_spanned, ToTokens}; use quote::{quote, quote_spanned, ToTokens};
use syn::parse::Parser; use syn::{parse::Parser, Ident, Path};
// syn::AttributeArgs does not implement syn::Parse // syn::AttributeArgs does not implement syn::Parse
type AttributeArgs = syn::punctuated::Punctuated<syn::NestedMeta, syn::Token![,]>; type AttributeArgs = syn::punctuated::Punctuated<syn::NestedMeta, syn::Token![,]>;
@@ -29,7 +29,7 @@ struct FinalConfig {
flavor: RuntimeFlavor, flavor: RuntimeFlavor,
worker_threads: Option<usize>, worker_threads: Option<usize>,
start_paused: Option<bool>, start_paused: Option<bool>,
crate_name: Option<String>, crate_name: Option<Path>,
} }
/// Config used in case of the attribute not being able to build a valid config /// Config used in case of the attribute not being able to build a valid config
@@ -47,7 +47,7 @@ struct Configuration {
worker_threads: Option<(usize, Span)>, worker_threads: Option<(usize, Span)>,
start_paused: Option<(bool, Span)>, start_paused: Option<(bool, Span)>,
is_test: bool, is_test: bool,
crate_name: Option<String>, crate_name: Option<Path>,
} }
impl Configuration { impl Configuration {
@@ -112,8 +112,8 @@ impl Configuration {
if self.crate_name.is_some() { if self.crate_name.is_some() {
return Err(syn::Error::new(span, "`crate` set multiple times.")); return Err(syn::Error::new(span, "`crate` set multiple times."));
} }
let name_ident = parse_ident(name, span, "crate")?; let name_path = parse_path(name, span, "crate")?;
self.crate_name = Some(name_ident.to_string()); self.crate_name = Some(name_path);
Ok(()) Ok(())
} }
@@ -199,23 +199,22 @@ fn parse_string(int: syn::Lit, span: Span, field: &str) -> Result<String, syn::E
} }
} }
fn parse_ident(lit: syn::Lit, span: Span, field: &str) -> Result<Ident, syn::Error> { fn parse_path(lit: syn::Lit, span: Span, field: &str) -> Result<Path, syn::Error> {
match lit { match lit {
syn::Lit::Str(s) => { syn::Lit::Str(s) => {
let err = syn::Error::new( let err = syn::Error::new(
span, span,
format!( format!(
"Failed to parse value of `{}` as ident: \"{}\"", "Failed to parse value of `{}` as path: \"{}\"",
field, field,
s.value() s.value()
), ),
); );
let path = s.parse::<syn::Path>().map_err(|_| err.clone())?; s.parse::<syn::Path>().map_err(|_| err.clone())
path.get_ident().cloned().ok_or(err)
} }
_ => Err(syn::Error::new( _ => Err(syn::Error::new(
span, span,
format!("Failed to parse value of `{}` as ident.", field), format!("Failed to parse value of `{}` as path.", field),
)), )),
} }
} }
@@ -354,16 +353,17 @@ fn parse_knobs(mut input: syn::ItemFn, is_test: bool, config: FinalConfig) -> To
(start, end) (start, end)
}; };
let crate_name = config.crate_name.as_deref().unwrap_or("tokio"); let crate_path = config
.crate_name
let crate_ident = Ident::new(crate_name, last_stmt_start_span); .map(ToTokens::into_token_stream)
.unwrap_or_else(|| Ident::new("tokio", last_stmt_start_span).into_token_stream());
let mut rt = match config.flavor { let mut rt = match config.flavor {
RuntimeFlavor::CurrentThread => quote_spanned! {last_stmt_start_span=> RuntimeFlavor::CurrentThread => quote_spanned! {last_stmt_start_span=>
#crate_ident::runtime::Builder::new_current_thread() #crate_path::runtime::Builder::new_current_thread()
}, },
RuntimeFlavor::Threaded => quote_spanned! {last_stmt_start_span=> RuntimeFlavor::Threaded => quote_spanned! {last_stmt_start_span=>
#crate_ident::runtime::Builder::new_multi_thread() #crate_path::runtime::Builder::new_multi_thread()
}, },
}; };
if let Some(v) = config.worker_threads { if let Some(v) = config.worker_threads {
@@ -414,7 +414,7 @@ fn parse_knobs(mut input: syn::ItemFn, is_test: bool, config: FinalConfig) -> To
}; };
quote! { quote! {
let body = async #body; let body = async #body;
#crate_ident::pin!(body); #crate_path::pin!(body);
let body: ::std::pin::Pin<&mut dyn ::std::future::Future<Output = #output_type>> = body; let body: ::std::pin::Pin<&mut dyn ::std::future::Future<Output = #output_type>> = body;
} }
} else { } else {
+9
View File
@@ -5,6 +5,10 @@ use std as tokio;
use ::tokio as tokio1; use ::tokio as tokio1;
mod test {
pub use ::tokio;
}
async fn compute() -> usize { async fn compute() -> usize {
let join = tokio1::spawn(async { 1 }); let join = tokio1::spawn(async { 1 });
join.await.unwrap() join.await.unwrap()
@@ -24,3 +28,8 @@ fn crate_rename_main() {
async fn crate_rename_test() { async fn crate_rename_test() {
assert_eq!(1, compute().await); assert_eq!(1, compute().await);
} }
#[test::tokio::test(crate = "test::tokio")]
async fn crate_path_test() {
assert_eq!(1, compute().await);
}