From 23d62813c9145d73190e582690c0616768993ef8 Mon Sep 17 00:00:00 2001 From: Taiki Endo Date: Sat, 10 Aug 2019 00:58:55 +0900 Subject: [PATCH 1/2] macro: improve error massages --- tokio-macros/src/lib.rs | 55 ++++++++++++++++++++--------------------- 1 file changed, 27 insertions(+), 28 deletions(-) diff --git a/tokio-macros/src/lib.rs b/tokio-macros/src/lib.rs index 991558f7e98..a08238a0c27 100644 --- a/tokio-macros/src/lib.rs +++ b/tokio-macros/src/lib.rs @@ -7,8 +7,7 @@ extern crate proc_macro; use proc_macro::TokenStream; -use quote::{quote, quote_spanned}; -use syn::spanned::Spanned; +use quote::quote; /// 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; if input.asyncness.is_none() { - let tokens = quote_spanned! { input.span() => - compile_error!("the async keyword is missing from the function declaration"); - }; - - return TokenStream::from(tokens); + let msg = "the async keyword is missing from the function declaration"; + return syn::Error::new_spanned(input.decl.fn_token, msg) + .to_compile_error() + .into(); } else if !input.decl.inputs.is_empty() { - let tokens = quote_spanned! { input.span() => - compile_error!("the main function cannot accept arguments"); - }; - - return TokenStream::from(tokens); + let msg = "the main function cannot accept arguments"; + return syn::Error::new_spanned(&input.decl.inputs, msg) + .to_compile_error() + .into(); } 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() { "multi_thread" => runtime = RuntimeType::Multi, "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 { if attr.path.is_ident("test") { - let tokens = quote_spanned! { input.span() => - compile_error!("second test attribute is supplied"); - }; - - return TokenStream::from(tokens); + let msg = "second test attribute is supplied"; + return syn::Error::new_spanned(&attr.path, msg) + .to_compile_error() + .into(); } } if input.asyncness.is_none() { - let tokens = quote_spanned! { input.span() => - compile_error!("the async keyword is missing from the function declaration"); - }; - - return TokenStream::from(tokens); + let msg = "the async keyword is missing from the function declaration"; + return syn::Error::new_spanned(&input, msg) + .to_compile_error() + .into(); } else if !input.decl.inputs.is_empty() { - let tokens = quote_spanned! { input.span() => - compile_error!("the test function cannot accept arguments"); - }; - - return TokenStream::from(tokens); + let msg = "the test function cannot accept arguments"; + return syn::Error::new_spanned(&input.decl.inputs, msg) + .to_compile_error() + .into(); } let result = quote! { From 93f44b1e98d38354d088c31f56b3a0f0f3632b02 Mon Sep 17 00:00:00 2001 From: Taiki Endo Date: Sat, 10 Aug 2019 01:15:44 +0900 Subject: [PATCH 2/2] fix --- tokio-macros/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tokio-macros/src/lib.rs b/tokio-macros/src/lib.rs index a08238a0c27..1d14f5ea672 100644 --- a/tokio-macros/src/lib.rs +++ b/tokio-macros/src/lib.rs @@ -129,7 +129,7 @@ pub fn test(_attr: TokenStream, item: TokenStream) -> TokenStream { for attr in attrs { if attr.path.is_ident("test") { let msg = "second test attribute is supplied"; - return syn::Error::new_spanned(&attr.path, msg) + return syn::Error::new_spanned(&attr, msg) .to_compile_error() .into(); }