commit | 25fd6ca62f4e09627a60d41e2f51abf98dfb6977 | [log] [tgz] |
---|---|---|
author | CreepySkeleton <creepy-skeleton@yandex.ru> | Mon Sep 02 02:56:03 2019 |
committer | CreepySkeleton <creepy-skeleton@yandex.ru> | Mon Sep 02 02:57:36 2019 |
tree | d82e97758d87fd232e804d01f8cb1ed117e163a0 | |
parent | 464d553fee5945d87444f0357cac068f4cbc6f83 [diff] |
Add `dummy` module and deprecate `multi` module
This crate aims to provide an error reporting mechanism that is usable inside proc-macros
, can highlight a specific span, and can be migrated from panic!
-based errors with minimal efforts.
//! Also, there's ability to append a dummy token stream to your errors.
In your Cargo.toml
:
proc-macro-error = "0.2"
In lib.rs
:
extern crate proc_macro_error; use proc_macro_error::{ filter_macro_errors, span_error, call_site_error, ResultExt, OptionExt }; // This is your main entry point #[proc_macro] pub fn make_answer(input: TokenStream) -> TokenStream { // This macro **must** be placed at the top level. // No need to touch the code inside though. filter_macro_errors! { // `parse_macro_input!` and 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 to the end of `filter_macro_errors!` invocation span_error!(span, "You made an error, go fix it: {}", msg); } // `Result` gets some handy shortcuts if your error type implements // `Into<``MacroError``>`. `Option` has one unconditionally 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`! }
Error handling in proc-macros sucks. It's not much of a choice today: you either “bubble up” the error up to the top-level of your macro and convert it to a compile_error!
invocation or just use a good old panic. Both these ways suck:
.expect
is too tempting.rustc
will highlight the whole invocation itself but not some specific token inside it. Furthermore, panics aren’t for error-reporting at all; panics are for bug-detecting (like unwrapping on None
or out-of range indexing) or for early development stages when you need a prototype ASAP and error handling can wait. Mixing these usages only messes things up.proc_macro::Diagnostics
but it's experimental. (This crate will be deprecated once Diagnostics
is stable.)That said, we need a solution, but this solution must meet these conditions:
panic!
. The main point: it must offer a way to carry span information over to the user.panic!
. Ideally, a new macro with the same semantics plus ability to carry out span info.This crate aims to provide such a mechanism. All you have to do is enclose all the code inside your top-level #[proc_macro]
function in filter_macro_errors!
invocation and change panics to span_error!
/call_site_error!
where appropriate, see Usage
Effectively, it emulates try-catch mechanism on top of panics.
Essentially, the filter_macro_errors!
macro is (C++ like pseudo-code)
try { /* your code */ } catch (MacroError) { /* conversion to compile_error! */ } catch (MultiMacroErrors) { /* conversion to multiple compile_error! invocations */ }
span_error!
and co are
throw MacroError::new(span, format!(msg...));
By calling span_error!
you trigger panic that will be caught by filter_macro_errors!
and converted to compile_error!
invocation. All the panics that weren't triggered by span_error!
and co will be resumed as is.
Panic catching is indeed slow but the macro is about to abort anyway so speed is not a concern here. Please note that this crate is not intended to be used in any other way than a proc-macro error reporting, use Result
and ?
instead.