Skip to content

Commit 8875b6d

Browse files
committed
Accept path as crate rename as well
1 parent e7bd754 commit 8875b6d

File tree

5 files changed

+59
-58
lines changed

5 files changed

+59
-58
lines changed

tests-build/tests/fail/macros_invalid_input.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,10 @@ async fn test_worker_threads_not_int() {}
3636
async fn test_worker_threads_and_current_thread() {}
3737

3838
#[tokio::test(crate = 456)]
39-
async fn test_crate_not_ident_int() {}
39+
async fn test_crate_not_path_int() {}
4040

4141
#[tokio::test(crate = "456")]
42-
async fn test_crate_not_ident_invalid() {}
43-
44-
#[tokio::test(crate = "abc::edf")]
45-
async fn test_crate_not_ident_path() {}
42+
async fn test_crate_not_path_invalid() {}
4643

4744
#[tokio::test]
4845
#[test]

tests-build/tests/fail/macros_invalid_input.stderr

+6-12
Original file line numberDiff line numberDiff line change
@@ -64,34 +64,28 @@ error: The `worker_threads` option requires the `multi_thread` runtime flavor. U
6464
35 | #[tokio::test(flavor = "current_thread", worker_threads = 4)]
6565
| ^
6666

67-
error: Failed to parse value of `crate` as ident.
67+
error: Failed to parse value of `crate` as path.
6868
--> $DIR/macros_invalid_input.rs:38:23
6969
|
7070
38 | #[tokio::test(crate = 456)]
7171
| ^^^
7272

73-
error: Failed to parse value of `crate` as ident: "456"
73+
error: Failed to parse value of `crate` as path: "456"
7474
--> $DIR/macros_invalid_input.rs:41:23
7575
|
7676
41 | #[tokio::test(crate = "456")]
7777
| ^^^^^
7878

79-
error: Failed to parse value of `crate` as ident: "abc::edf"
80-
--> $DIR/macros_invalid_input.rs:44:23
81-
|
82-
44 | #[tokio::test(crate = "abc::edf")]
83-
| ^^^^^^^^^^
84-
8579
error: second test attribute is supplied
86-
--> $DIR/macros_invalid_input.rs:48:1
80+
--> $DIR/macros_invalid_input.rs:45:1
8781
|
88-
48 | #[test]
82+
45 | #[test]
8983
| ^^^^^^^
9084

9185
error: duplicated attribute
92-
--> $DIR/macros_invalid_input.rs:48:1
86+
--> $DIR/macros_invalid_input.rs:45:1
9387
|
94-
48 | #[test]
88+
45 | #[test]
9589
| ^^^^^^^
9690
|
9791
note: the lint level is defined here
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,51 @@
11
error[E0308]: mismatched types
2-
--> $DIR/macros_type_mismatch.rs:5:5
2+
--> $DIR/macros_type_mismatch.rs:3:1
33
|
4+
3 | #[tokio::main]
5+
| ^^^^^^^^^^^^^^ expected `()`, found enum `Result`
46
4 | async fn missing_semicolon_or_return_type() {
57
| - help: a return type might be missing here: `-> _`
6-
5 | Ok(())
7-
| ^^^^^^ expected `()`, found enum `Result`
88
|
99
= note: expected unit type `()`
1010
found enum `Result<(), _>`
11+
= note: this error originates in the attribute macro `tokio::main` (in Nightly builds, run with -Z macro-backtrace for more info)
1112

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

2325
error[E0308]: mismatched types
24-
--> $DIR/macros_type_mismatch.rs:23:5
26+
--> $DIR/macros_type_mismatch.rs:13:1
2527
|
28+
13 | #[tokio::main]
29+
| ^^^^^^^^^^^^^^ expected enum `Result`, found `()`
2630
14 | async fn extra_semicolon() -> Result<(), ()> {
2731
| -------------- expected `Result<(), ()>` because of return type
28-
...
29-
23 | Ok(());
30-
| ^^^^^^^ expected enum `Result`, found `()`
3132
|
3233
= note: expected enum `Result<(), ()>`
3334
found unit type `()`
34-
help: try adding an expression at the end of the block
35-
|
36-
23 ~ Ok(());;
37-
24 + Ok(())
35+
= note: this error originates in the attribute macro `tokio::main` (in Nightly builds, run with -Z macro-backtrace for more info)
36+
help: try wrapping the expression in a variant of `Result`
3837
|
38+
13 | Ok(#[tokio::main])
39+
| +++ +
40+
13 | Err(#[tokio::main])
41+
| ++++ +
3942

4043
error[E0308]: mismatched types
41-
--> $DIR/macros_type_mismatch.rs:32:5
44+
--> $DIR/macros_type_mismatch.rs:29:1
4245
|
46+
29 | #[tokio::main]
47+
| ^^^^^^^^^^^^^^ expected `()`, found integer
4348
30 | async fn issue_4635() {
4449
| - help: try adding a return type: `-> i32`
45-
31 | return 1;
46-
32 | ;
47-
| ^ expected `()`, found integer
50+
|
51+
= note: this error originates in the attribute macro `tokio::main` (in Nightly builds, run with -Z macro-backtrace for more info)

tokio-macros/src/entry.rs

+14-17
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use proc_macro::TokenStream;
2-
use proc_macro2::{Ident, Span};
2+
use proc_macro2::Span;
33
use quote::{quote, quote_spanned, ToTokens};
4-
use syn::parse::Parser;
4+
use syn::{parse::Parser, parse_quote, Path};
55

66
// syn::AttributeArgs does not implement syn::Parse
77
type AttributeArgs = syn::punctuated::Punctuated<syn::NestedMeta, syn::Token![,]>;
@@ -29,7 +29,7 @@ struct FinalConfig {
2929
flavor: RuntimeFlavor,
3030
worker_threads: Option<usize>,
3131
start_paused: Option<bool>,
32-
crate_name: Option<String>,
32+
crate_name: Option<Path>,
3333
}
3434

3535
/// Config used in case of the attribute not being able to build a valid config
@@ -47,7 +47,7 @@ struct Configuration {
4747
worker_threads: Option<(usize, Span)>,
4848
start_paused: Option<(bool, Span)>,
4949
is_test: bool,
50-
crate_name: Option<String>,
50+
crate_name: Option<Path>,
5151
}
5252

5353
impl Configuration {
@@ -112,8 +112,8 @@ impl Configuration {
112112
if self.crate_name.is_some() {
113113
return Err(syn::Error::new(span, "`crate` set multiple times."));
114114
}
115-
let name_ident = parse_ident(name, span, "crate")?;
116-
self.crate_name = Some(name_ident.to_string());
115+
let name_path = parse_path(name, span, "crate")?;
116+
self.crate_name = Some(name_path);
117117
Ok(())
118118
}
119119

@@ -199,23 +199,22 @@ fn parse_string(int: syn::Lit, span: Span, field: &str) -> Result<String, syn::E
199199
}
200200
}
201201

202-
fn parse_ident(lit: syn::Lit, span: Span, field: &str) -> Result<Ident, syn::Error> {
202+
fn parse_path(lit: syn::Lit, span: Span, field: &str) -> Result<Path, syn::Error> {
203203
match lit {
204204
syn::Lit::Str(s) => {
205205
let err = syn::Error::new(
206206
span,
207207
format!(
208-
"Failed to parse value of `{}` as ident: \"{}\"",
208+
"Failed to parse value of `{}` as path: \"{}\"",
209209
field,
210210
s.value()
211211
),
212212
);
213-
let path = s.parse::<syn::Path>().map_err(|_| err.clone())?;
214-
path.get_ident().cloned().ok_or(err)
213+
s.parse::<syn::Path>().map_err(|_| err.clone())
215214
}
216215
_ => Err(syn::Error::new(
217216
span,
218-
format!("Failed to parse value of `{}` as ident.", field),
217+
format!("Failed to parse value of `{}` as path.", field),
219218
)),
220219
}
221220
}
@@ -354,16 +353,14 @@ fn parse_knobs(mut input: syn::ItemFn, is_test: bool, config: FinalConfig) -> To
354353
(start, end)
355354
};
356355

357-
let crate_name = config.crate_name.as_deref().unwrap_or("tokio");
358-
359-
let crate_ident = Ident::new(crate_name, last_stmt_start_span);
356+
let crate_path = config.crate_name.unwrap_or_else(|| parse_quote! { tokio });
360357

361358
let mut rt = match config.flavor {
362359
RuntimeFlavor::CurrentThread => quote_spanned! {last_stmt_start_span=>
363-
#crate_ident::runtime::Builder::new_current_thread()
360+
#crate_path::runtime::Builder::new_current_thread()
364361
},
365362
RuntimeFlavor::Threaded => quote_spanned! {last_stmt_start_span=>
366-
#crate_ident::runtime::Builder::new_multi_thread()
363+
#crate_path::runtime::Builder::new_multi_thread()
367364
},
368365
};
369366
if let Some(v) = config.worker_threads {
@@ -414,7 +411,7 @@ fn parse_knobs(mut input: syn::ItemFn, is_test: bool, config: FinalConfig) -> To
414411
};
415412
quote! {
416413
let body = async #body;
417-
#crate_ident::pin!(body);
414+
#crate_path::pin!(body);
418415
let body: ::std::pin::Pin<&mut dyn ::std::future::Future<Output = #output_type>> = body;
419416
}
420417
} else {

tokio/tests/macros_rename_test.rs

+9
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ use std as tokio;
55

66
use ::tokio as tokio1;
77

8+
mod test {
9+
pub use ::tokio;
10+
}
11+
812
async fn compute() -> usize {
913
let join = tokio1::spawn(async { 1 });
1014
join.await.unwrap()
@@ -24,3 +28,8 @@ fn crate_rename_main() {
2428
async fn crate_rename_test() {
2529
assert_eq!(1, compute().await);
2630
}
31+
32+
#[test::tokio::test(crate = "test::tokio")]
33+
async fn crate_path_test() {
34+
assert_eq!(1, compute().await);
35+
}

0 commit comments

Comments
 (0)