add explanation for recovery code

This commit is contained in:
Rob Ede 2021-10-14 03:16:59 +01:00
parent 59697a2648
commit 362e48d0e3
No known key found for this signature in database
GPG Key ID: 97C636207D3EF933
2 changed files with 17 additions and 12 deletions

View File

@ -30,12 +30,8 @@ use quote::quote;
pub fn main(args: TokenStream, item: TokenStream) -> TokenStream {
let mut input = match syn::parse::<syn::ItemFn>(item.clone()) {
Ok(input) => input,
Err(err) => {
let mut item = item;
let compile_err = TokenStream::from(err.to_compile_error());
item.extend(compile_err);
return item;
}
// on parse err, make IDEs happy; see fn docs
Err(err) => return input_and_compile_error(item, err),
};
let args = syn::parse_macro_input!(args as syn::AttributeArgs);
@ -113,12 +109,8 @@ pub fn main(args: TokenStream, item: TokenStream) -> TokenStream {
pub fn test(args: TokenStream, item: TokenStream) -> TokenStream {
let mut input = match syn::parse::<syn::ItemFn>(item.clone()) {
Ok(input) => input,
Err(err) => {
let mut item = item;
let compile_err = TokenStream::from(err.to_compile_error());
item.extend(compile_err);
return item;
}
// on parse err, make IDEs happy; see fn docs
Err(err) => return input_and_compile_error(item, err),
};
let args = syn::parse_macro_input!(args as syn::AttributeArgs);
@ -196,3 +188,15 @@ pub fn test(args: TokenStream, item: TokenStream) -> TokenStream {
})
.into()
}
/// Converts the error to a token stream and appends it to the original input.
///
/// Returning the original input in addition to the error is good for IDEs which can gracefully
/// recover and show more precise errors within the macro body.
///
/// See <https://github.com/rust-analyzer/rust-analyzer/issues/10468> for more info.
fn input_and_compile_error(mut item: TokenStream, err: syn::Error) -> TokenStream {
let compile_err = TokenStream::from(err.to_compile_error());
item.extend(compile_err);
return item;
}

View File

@ -24,4 +24,5 @@ serde = { version = "1.0", optional = true }
[dev-dependencies]
serde_json = "1.0"
# TODO: remove when ahash MSRV is restored
ahash = { version = "=0.7.4", default-features = false }