Deprecated: The each() function is deprecated. This message will be suppressed on further calls in /home/zhenxiangba/zhenxiangba.com/public_html/phproxy-improved-master/index.php on line 456
proc_macro_error::filter_macro_errors - Rust
[go: Go Back, main page]

[][src]Macro proc_macro_error::filter_macro_errors

macro_rules! filter_macro_errors {
    ($($code:tt)*) => { ... };
}

This macro is supposed to be used at the top level of your proc-macro, the function marked with a #[proc_macro*] attribute. It catches all the errors triggered by span_error!, call_site_error!, and trigger_error. Once caught, it converts it to a proc_macro::TokenStream containing a compile_error! invocation.

This example is not tested
#[proc_macro]
pub fn make_answer(input: TokenStream) -> TokenStream {
    // this macro at the top level
    filter_macro_errors! {
        // `parse_macro_input!` and its friends work just fine inside this macro
        let input = parse_macro_input!(input as MyParser);

        if let Err(err) = some_logic(&input) {
            /// we've got a span to blame, let's use it
            let span = err.span_should_be_highlighted();
            let msg = err.message();
            // This call jumps directly at the end of `filter_macro_errors!` invocation
            span_error!(span, "You made an error, go fix it: {}", msg);
        }
         
        // You can use some handy shortcuts if your error type
        // implements Into<MacroError>         
        use proc_macro_error::ResultExt;
        more_logic(&input).expect_or_exit("What a careless user, behave!");

        if !more_logic_for_logic_god!(&input) {
            // We don't have an exact location this time,
            // so just highlight the proc-macro invocation itself
            call_site_error!(
                "Bad, bad user! Now go stand in the corner and think about what you did!");
        }

        // Now all the processing is done, return `proc_macro::TokenStream`
        quote!(/* stuff */).into()
    }
     
    // At this point we have a new shining `proc_macro::TokenStream`!
}