Skip to content

Commit

Permalink
Merge pull request #264 from dtolnay/errortraitpath
Browse files Browse the repository at this point in the history
Delete elaborate spans on path of error trait
  • Loading branch information
dtolnay committed Dec 15, 2023
2 parents ae3d41d + d1efad1 commit 1c6c4bb
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 29 deletions.
4 changes: 0 additions & 4 deletions impl/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,13 @@ pub enum Input<'a> {
}

pub struct Struct<'a> {
pub original: &'a DeriveInput,
pub attrs: Attrs<'a>,
pub ident: Ident,
pub generics: &'a Generics,
pub fields: Vec<Field<'a>>,
}

pub struct Enum<'a> {
pub original: &'a DeriveInput,
pub attrs: Attrs<'a>,
pub ident: Ident,
pub generics: &'a Generics,
Expand Down Expand Up @@ -65,7 +63,6 @@ impl<'a> Struct<'a> {
display.expand_shorthand(&fields);
}
Ok(Struct {
original: node,
attrs,
ident: node.ident.clone(),
generics: &node.generics,
Expand Down Expand Up @@ -96,7 +93,6 @@ impl<'a> Enum<'a> {
})
.collect::<Result<_>>()?;
Ok(Enum {
original: node,
attrs,
ident: node.ident.clone(),
generics: &node.generics,
Expand Down
28 changes: 3 additions & 25 deletions impl/src/expand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ use crate::span::MemberSpan;
use proc_macro2::TokenStream;
use quote::{format_ident, quote, quote_spanned, ToTokens};
use std::collections::BTreeSet as Set;
use syn::{
Data, DeriveInput, GenericArgument, Member, PathArguments, Result, Token, Type, Visibility,
};
use syn::{DeriveInput, GenericArgument, Member, PathArguments, Result, Token, Type};

pub fn derive(node: &DeriveInput) -> Result<TokenStream> {
let input = Input::from_syn(node)?;
Expand Down Expand Up @@ -168,7 +166,6 @@ fn impl_struct(input: Struct) -> TokenStream {
}
});

let error_trait = spanned_error_trait(input.original);
if input.generics.type_params().next().is_some() {
let self_token = <Token![Self]>::default();
error_inferred_bounds.insert(self_token, Trait::Debug);
Expand All @@ -178,7 +175,7 @@ fn impl_struct(input: Struct) -> TokenStream {

quote! {
#[allow(unused_qualifications)]
impl #impl_generics #error_trait for #ty #ty_generics #error_where_clause {
impl #impl_generics std::error::Error for #ty #ty_generics #error_where_clause {
#source_method
#provide_method
}
Expand Down Expand Up @@ -425,7 +422,6 @@ fn impl_enum(input: Enum) -> TokenStream {
})
});

let error_trait = spanned_error_trait(input.original);
if input.generics.type_params().next().is_some() {
let self_token = <Token![Self]>::default();
error_inferred_bounds.insert(self_token, Trait::Debug);
Expand All @@ -435,7 +431,7 @@ fn impl_enum(input: Enum) -> TokenStream {

quote! {
#[allow(unused_qualifications)]
impl #impl_generics #error_trait for #ty #ty_generics #error_where_clause {
impl #impl_generics std::error::Error for #ty #ty_generics #error_where_clause {
#source_method
#provide_method
}
Expand Down Expand Up @@ -528,21 +524,3 @@ fn type_parameter_of_option(ty: &Type) -> Option<&Type> {
_ => None,
}
}

fn spanned_error_trait(input: &DeriveInput) -> TokenStream {
let vis_span = match &input.vis {
Visibility::Public(vis) => Some(vis.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)
}

0 comments on commit 1c6c4bb

Please sign in to comment.