1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
extern crate proc_macro;

use proc_macro::TokenStream;
use quote::quote;
use syn::{parse_macro_input, Attribute, ItemFn};
use version_check::Version;

#[proc_macro_attribute]
pub fn proc_macro_error(_attr: TokenStream, input: TokenStream) -> TokenStream {
    let input = parse_macro_input!(input as ItemFn);

    if !is_proc_macro(&input.attrs) {
        return quote!(
            #input
            compile_error!("#[proc_macro_error] attribute can be used only with a proc-macro");
        )
        .into();
    }

    let ItemFn {
        attrs,
        vis,
        sig,
        block,
    } = input;

    let version = Version::read().unwrap();
    let body = if version.at_least("1.37.0") {
        quote! {
            ::proc_macro_error::entry_point(|| #block )
        }
    } else {
        quote! {
            // FIXME:
            // proc_macro::TokenStream does not implement UnwindSafe until 1.37.0.
            // Considering this is the closure's return type the safety check would fail
            // for virtually every closure possible, the check is meaningless.
            ::proc_macro_error::entry_point(::std::panic::AssertUnwindSafe(|| #block ))
        }
    };

    quote!( #(#attrs)* #vis #sig { #body } ).into()
}

fn is_proc_macro(attrs: &[Attribute]) -> bool {
    attrs.iter().any(|attr| {
        attr.path.is_ident("proc_macro")
            || attr.path.is_ident("proc_macro_derive")
            || attr.path.is_ident("proc_macro_attribute")
    })
}