From ed16526d4dda99938a4e067c1d61e01b67eb1d6c Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Fri, 15 Dec 2023 10:51:40 -0800 Subject: [PATCH 1/2] Delete elaborate spans on path of error trait --- impl/src/expand.rs | 28 +++------------------------- 1 file changed, 3 insertions(+), 25 deletions(-) diff --git a/impl/src/expand.rs b/impl/src/expand.rs index 9124ca3..2cf0967 100644 --- a/impl/src/expand.rs +++ b/impl/src/expand.rs @@ -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 { let input = Input::from_syn(node)?; @@ -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 = ::default(); error_inferred_bounds.insert(self_token, Trait::Debug); @@ -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 } @@ -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 = ::default(); error_inferred_bounds.insert(self_token, Trait::Debug); @@ -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 } @@ -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) -} From d1efad11a57833578a81215662829cd656e6b2ed Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Fri, 15 Dec 2023 10:54:25 -0800 Subject: [PATCH 2/2] Delete unused original DeriveInput from ast --- impl/src/ast.rs | 4 ---- 1 file changed, 4 deletions(-) diff --git a/impl/src/ast.rs b/impl/src/ast.rs index 2aa7246..9e06928 100644 --- a/impl/src/ast.rs +++ b/impl/src/ast.rs @@ -12,7 +12,6 @@ pub enum Input<'a> { } pub struct Struct<'a> { - pub original: &'a DeriveInput, pub attrs: Attrs<'a>, pub ident: Ident, pub generics: &'a Generics, @@ -20,7 +19,6 @@ pub struct Struct<'a> { } pub struct Enum<'a> { - pub original: &'a DeriveInput, pub attrs: Attrs<'a>, pub ident: Ident, pub generics: &'a Generics, @@ -65,7 +63,6 @@ impl<'a> Struct<'a> { display.expand_shorthand(&fields); } Ok(Struct { - original: node, attrs, ident: node.ident.clone(), generics: &node.generics, @@ -96,7 +93,6 @@ impl<'a> Enum<'a> { }) .collect::>()?; Ok(Enum { - original: node, attrs, ident: node.ident.clone(), generics: &node.generics,