mirror of
https://github.com/dtolnay/thiserror.git
synced 2025-04-06 06:17:39 +03:00
Improve span of missing Display impl error
This commit is contained in:
parent
b649704973
commit
4885372ced
3 changed files with 33 additions and 7 deletions
|
@ -2,7 +2,7 @@ use crate::ast::{Enum, Field, Input, Struct};
|
|||
use proc_macro2::TokenStream;
|
||||
use quote::{format_ident, quote, quote_spanned, ToTokens};
|
||||
use syn::spanned::Spanned;
|
||||
use syn::{DeriveInput, Member, PathArguments, Result, Type};
|
||||
use syn::{Data, DeriveInput, Member, PathArguments, Result, Type, Visibility};
|
||||
|
||||
pub fn derive(node: &DeriveInput) -> Result<TokenStream> {
|
||||
let input = Input::from_syn(node)?;
|
||||
|
@ -135,8 +135,10 @@ fn impl_struct(input: Struct) -> TokenStream {
|
|||
}
|
||||
});
|
||||
|
||||
let error_trait = spanned_error_trait(input.original);
|
||||
|
||||
quote! {
|
||||
impl #impl_generics std::error::Error for #ty #ty_generics #where_clause {
|
||||
impl #impl_generics #error_trait for #ty #ty_generics #where_clause {
|
||||
#source_method
|
||||
#backtrace_method
|
||||
}
|
||||
|
@ -321,8 +323,10 @@ fn impl_enum(input: Enum) -> TokenStream {
|
|||
})
|
||||
});
|
||||
|
||||
let error_trait = spanned_error_trait(input.original);
|
||||
|
||||
quote! {
|
||||
impl #impl_generics std::error::Error for #ty #ty_generics #where_clause {
|
||||
impl #impl_generics #error_trait for #ty #ty_generics #where_clause {
|
||||
#source_method
|
||||
#backtrace_method
|
||||
}
|
||||
|
@ -382,3 +386,22 @@ fn type_is_option(ty: &Type) -> bool {
|
|||
_ => false,
|
||||
}
|
||||
}
|
||||
|
||||
fn spanned_error_trait(input: &DeriveInput) -> TokenStream {
|
||||
let vis_span = match &input.vis {
|
||||
Visibility::Public(vis) => Some(vis.pub_token.span()),
|
||||
Visibility::Crate(vis) => Some(vis.crate_token.span()),
|
||||
Visibility::Restricted(vis) => Some(vis.pub_token.span()),
|
||||
Visibility::Inherited => None,
|
||||
};
|
||||
let data_span = match &input.data {
|
||||
Data::Struct(data) => data.struct_token.span(),
|
||||
Data::Enum(data) => data.enum_token.span(),
|
||||
Data::Union(data) => data.union_token.span(),
|
||||
};
|
||||
let first_span = vis_span.unwrap_or(data_span);
|
||||
let last_span = input.ident.span();
|
||||
let path = quote_spanned!(first_span=> std::error::);
|
||||
let error = quote_spanned!(last_span=> Error);
|
||||
quote!(#path #error)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue