mirror of
https://github.com/tokio-rs/tokio.git
synced 2026-08-28 00:00:11 +02:00
macros: improve error messages (#1420)
This commit is contained in:
+27
-28
@@ -7,8 +7,7 @@
|
|||||||
extern crate proc_macro;
|
extern crate proc_macro;
|
||||||
|
|
||||||
use proc_macro::TokenStream;
|
use proc_macro::TokenStream;
|
||||||
use quote::{quote, quote_spanned};
|
use quote::quote;
|
||||||
use syn::spanned::Spanned;
|
|
||||||
|
|
||||||
/// Marks async function to be executed by selected runtime.
|
/// Marks async function to be executed by selected runtime.
|
||||||
///
|
///
|
||||||
@@ -56,17 +55,15 @@ pub fn main(args: TokenStream, item: TokenStream) -> TokenStream {
|
|||||||
let attrs = &input.attrs;
|
let attrs = &input.attrs;
|
||||||
|
|
||||||
if input.asyncness.is_none() {
|
if input.asyncness.is_none() {
|
||||||
let tokens = quote_spanned! { input.span() =>
|
let msg = "the async keyword is missing from the function declaration";
|
||||||
compile_error!("the async keyword is missing from the function declaration");
|
return syn::Error::new_spanned(input.decl.fn_token, msg)
|
||||||
};
|
.to_compile_error()
|
||||||
|
.into();
|
||||||
return TokenStream::from(tokens);
|
|
||||||
} else if !input.decl.inputs.is_empty() {
|
} else if !input.decl.inputs.is_empty() {
|
||||||
let tokens = quote_spanned! { input.span() =>
|
let msg = "the main function cannot accept arguments";
|
||||||
compile_error!("the main function cannot accept arguments");
|
return syn::Error::new_spanned(&input.decl.inputs, msg)
|
||||||
};
|
.to_compile_error()
|
||||||
|
.into();
|
||||||
return TokenStream::from(tokens);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut runtime = RuntimeType::Multi;
|
let mut runtime = RuntimeType::Multi;
|
||||||
@@ -76,7 +73,12 @@ pub fn main(args: TokenStream, item: TokenStream) -> TokenStream {
|
|||||||
match ident.to_string().to_lowercase().as_str() {
|
match ident.to_string().to_lowercase().as_str() {
|
||||||
"multi_thread" => runtime = RuntimeType::Multi,
|
"multi_thread" => runtime = RuntimeType::Multi,
|
||||||
"single_thread" => runtime = RuntimeType::Single,
|
"single_thread" => runtime = RuntimeType::Single,
|
||||||
name => panic!("Unknown attribute {} is specified", name),
|
name => {
|
||||||
|
let msg = format!("Unknown attribute {} is specified", name);
|
||||||
|
return syn::Error::new_spanned(ident, msg)
|
||||||
|
.to_compile_error()
|
||||||
|
.into();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -126,26 +128,23 @@ pub fn test(_attr: TokenStream, item: TokenStream) -> TokenStream {
|
|||||||
|
|
||||||
for attr in attrs {
|
for attr in attrs {
|
||||||
if attr.path.is_ident("test") {
|
if attr.path.is_ident("test") {
|
||||||
let tokens = quote_spanned! { input.span() =>
|
let msg = "second test attribute is supplied";
|
||||||
compile_error!("second test attribute is supplied");
|
return syn::Error::new_spanned(&attr, msg)
|
||||||
};
|
.to_compile_error()
|
||||||
|
.into();
|
||||||
return TokenStream::from(tokens);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if input.asyncness.is_none() {
|
if input.asyncness.is_none() {
|
||||||
let tokens = quote_spanned! { input.span() =>
|
let msg = "the async keyword is missing from the function declaration";
|
||||||
compile_error!("the async keyword is missing from the function declaration");
|
return syn::Error::new_spanned(&input, msg)
|
||||||
};
|
.to_compile_error()
|
||||||
|
.into();
|
||||||
return TokenStream::from(tokens);
|
|
||||||
} else if !input.decl.inputs.is_empty() {
|
} else if !input.decl.inputs.is_empty() {
|
||||||
let tokens = quote_spanned! { input.span() =>
|
let msg = "the test function cannot accept arguments";
|
||||||
compile_error!("the test function cannot accept arguments");
|
return syn::Error::new_spanned(&input.decl.inputs, msg)
|
||||||
};
|
.to_compile_error()
|
||||||
|
.into();
|
||||||
return TokenStream::from(tokens);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let result = quote! {
|
let result = quote! {
|
||||||
|
|||||||
Reference in New Issue
Block a user