Skip to content

Commit 7273236

Browse files
committed
Accept path as crate rename as well
1 parent cef98e2 commit 7273236

File tree

4 files changed

+34
-34
lines changed

4 files changed

+34
-34
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

tokio-macros/src/entry.rs

+17-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, Ident, 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,17 @@ 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
357+
.crate_name
358+
.map(ToTokens::into_token_stream)
359+
.unwrap_or_else(|| Ident::new("tokio", last_stmt_start_span).into_token_stream());
360360

361361
let mut rt = match config.flavor {
362362
RuntimeFlavor::CurrentThread => quote_spanned! {last_stmt_start_span=>
363-
#crate_ident::runtime::Builder::new_current_thread()
363+
#crate_path::runtime::Builder::new_current_thread()
364364
},
365365
RuntimeFlavor::Threaded => quote_spanned! {last_stmt_start_span=>
366-
#crate_ident::runtime::Builder::new_multi_thread()
366+
#crate_path::runtime::Builder::new_multi_thread()
367367
},
368368
};
369369
if let Some(v) = config.worker_threads {
@@ -414,7 +414,7 @@ fn parse_knobs(mut input: syn::ItemFn, is_test: bool, config: FinalConfig) -> To
414414
};
415415
quote! {
416416
let body = async #body;
417-
#crate_ident::pin!(body);
417+
#crate_path::pin!(body);
418418
let body: ::std::pin::Pin<&mut dyn ::std::future::Future<Output = #output_type>> = body;
419419
}
420420
} 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)