Skip to content

Commit

Permalink
Accept path as crate rename as well
Browse files Browse the repository at this point in the history
  • Loading branch information
daxpedda committed Mar 18, 2023
1 parent cef98e2 commit aacbaaf
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 58 deletions.
7 changes: 2 additions & 5 deletions tests-build/tests/fail/macros_invalid_input.rs
Expand Up @@ -36,13 +36,10 @@ async fn test_worker_threads_not_int() {}
async fn test_worker_threads_and_current_thread() {}

#[tokio::test(crate = 456)]
async fn test_crate_not_ident_int() {}
async fn test_crate_not_path_int() {}

#[tokio::test(crate = "456")]
async fn test_crate_not_ident_invalid() {}

#[tokio::test(crate = "abc::edf")]
async fn test_crate_not_ident_path() {}
async fn test_crate_not_path_invalid() {}

#[tokio::test]
#[test]
Expand Down
18 changes: 6 additions & 12 deletions tests-build/tests/fail/macros_invalid_input.stderr
Expand Up @@ -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)]
| ^

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
|
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
|
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
--> $DIR/macros_invalid_input.rs:48:1
--> $DIR/macros_invalid_input.rs:45:1
|
48 | #[test]
45 | #[test]
| ^^^^^^^

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
Expand Down
52 changes: 28 additions & 24 deletions tests-build/tests/fail/macros_type_mismatch.stderr
@@ -1,47 +1,51 @@
error[E0308]: mismatched types
--> $DIR/macros_type_mismatch.rs:5:5
--> $DIR/macros_type_mismatch.rs:3:1
|
3 | #[tokio::main]
| ^^^^^^^^^^^^^^ expected `()`, found enum `Result`
4 | async fn missing_semicolon_or_return_type() {
| - help: a return type might be missing here: `-> _`
5 | Ok(())
| ^^^^^^ expected `()`, found enum `Result`
|
= note: expected unit type `()`
found enum `Result<(), _>`
= note: this error originates in the attribute macro `tokio::main` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0308]: mismatched types
--> $DIR/macros_type_mismatch.rs:10:5
|
9 | async fn missing_return_type() {
| - help: a return type might be missing here: `-> _`
10 | return Ok(());
| ^^^^^^^^^^^^^^ expected `()`, found enum `Result`
|
= note: expected unit type `()`
found enum `Result<(), _>`
--> $DIR/macros_type_mismatch.rs:8:1
|
8 | #[tokio::main]
| ^^^^^^^^^^^^^^ expected `()`, found enum `Result`
9 | async fn missing_return_type() {
| - help: a return type might be missing here: `-> _`
|
= note: expected unit type `()`
found enum `Result<(), _>`
= note: this error originates in the attribute macro `tokio::main` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0308]: mismatched types
--> $DIR/macros_type_mismatch.rs:23:5
--> $DIR/macros_type_mismatch.rs:13:1
|
13 | #[tokio::main]
| ^^^^^^^^^^^^^^ expected enum `Result`, found `()`
14 | async fn extra_semicolon() -> Result<(), ()> {
| -------------- expected `Result<(), ()>` because of return type
...
23 | Ok(());
| ^^^^^^^ expected enum `Result`, found `()`
|
= note: expected enum `Result<(), ()>`
found unit type `()`
help: try adding an expression at the end of the block
|
23 ~ Ok(());;
24 + Ok(())
= note: this error originates in the attribute macro `tokio::main` (in Nightly builds, run with -Z macro-backtrace for more info)
help: try wrapping the expression in a variant of `Result`
|
13 | Ok(#[tokio::main])
| +++ +
13 | Err(#[tokio::main])
| ++++ +

error[E0308]: mismatched types
--> $DIR/macros_type_mismatch.rs:32:5
--> $DIR/macros_type_mismatch.rs:29:1
|
29 | #[tokio::main]
| ^^^^^^^^^^^^^^ expected `()`, found integer
30 | async fn issue_4635() {
| - help: try adding a return type: `-> i32`
31 | return 1;
32 | ;
| ^ expected `()`, found integer
|
= note: this error originates in the attribute macro `tokio::main` (in Nightly builds, run with -Z macro-backtrace for more info)
34 changes: 17 additions & 17 deletions tokio-macros/src/entry.rs
@@ -1,7 +1,7 @@
use proc_macro::TokenStream;
use proc_macro2::{Ident, Span};
use proc_macro2::Span;
use quote::{quote, quote_spanned, ToTokens};
use syn::parse::Parser;
use syn::{parse::Parser, Ident, Path};

// syn::AttributeArgs does not implement syn::Parse
type AttributeArgs = syn::punctuated::Punctuated<syn::NestedMeta, syn::Token![,]>;
Expand Down Expand Up @@ -29,7 +29,7 @@ struct FinalConfig {
flavor: RuntimeFlavor,
worker_threads: Option<usize>,
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
Expand All @@ -47,7 +47,7 @@ struct Configuration {
worker_threads: Option<(usize, Span)>,
start_paused: Option<(bool, Span)>,
is_test: bool,
crate_name: Option<String>,
crate_name: Option<Path>,
}

impl Configuration {
Expand Down Expand Up @@ -112,8 +112,8 @@ impl Configuration {
if self.crate_name.is_some() {
return Err(syn::Error::new(span, "`crate` set multiple times."));
}
let name_ident = parse_ident(name, span, "crate")?;
self.crate_name = Some(name_ident.to_string());
let name_path = parse_path(name, span, "crate")?;
self.crate_name = Some(name_path);
Ok(())
}

Expand Down Expand Up @@ -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 {
syn::Lit::Str(s) => {
let err = syn::Error::new(
span,
format!(
"Failed to parse value of `{}` as ident: \"{}\"",
"Failed to parse value of `{}` as path: \"{}\"",
field,
s.value()
),
);
let path = s.parse::<syn::Path>().map_err(|_| err.clone())?;
path.get_ident().cloned().ok_or(err)
s.parse::<syn::Path>().map_err(|_| err.clone())
}
_ => Err(syn::Error::new(
span,
format!("Failed to parse value of `{}` as ident.", field),
format!("Failed to parse value of `{}` as path.", field),
)),
}
}
Expand Down Expand Up @@ -354,16 +353,17 @@ fn parse_knobs(mut input: syn::ItemFn, is_test: bool, config: FinalConfig) -> To
(start, end)
};

let crate_name = config.crate_name.as_deref().unwrap_or("tokio");

let crate_ident = Ident::new(crate_name, last_stmt_start_span);
let crate_path = config
.crate_name
.map(ToTokens::into_token_stream)
.unwrap_or_else(|| Ident::new("tokio", last_stmt_start_span).into_token_stream());

let mut rt = match config.flavor {
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=>
#crate_ident::runtime::Builder::new_multi_thread()
#crate_path::runtime::Builder::new_multi_thread()
},
};
if let Some(v) = config.worker_threads {
Expand Down Expand Up @@ -414,7 +414,7 @@ fn parse_knobs(mut input: syn::ItemFn, is_test: bool, config: FinalConfig) -> To
};
quote! {
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;
}
} else {
Expand Down
9 changes: 9 additions & 0 deletions tokio/tests/macros_rename_test.rs
Expand Up @@ -5,6 +5,10 @@ use std as tokio;

use ::tokio as tokio1;

mod test {
pub use ::tokio;
}

async fn compute() -> usize {
let join = tokio1::spawn(async { 1 });
join.await.unwrap()
Expand All @@ -24,3 +28,8 @@ fn crate_rename_main() {
async fn crate_rename_test() {
assert_eq!(1, compute().await);
}

#[test::tokio::test(crate = "test::tokio")]
async fn crate_path_test() {
assert_eq!(1, compute().await);
}

0 comments on commit aacbaaf

Please sign in to comment.