From 7cec716420b0f0bfbec8a827955d810b0f9804ab Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Thu, 19 Oct 2023 10:42:06 -0700 Subject: [PATCH] Remove reliance on Spanned for Member --- impl/src/expand.rs | 18 +++++++++--------- impl/src/lib.rs | 1 + impl/src/prop.rs | 3 ++- impl/src/span.rs | 15 +++++++++++++++ 4 files changed, 27 insertions(+), 10 deletions(-) create mode 100644 impl/src/span.rs diff --git a/impl/src/expand.rs b/impl/src/expand.rs index 394aafb..9124ca3 100644 --- a/impl/src/expand.rs +++ b/impl/src/expand.rs @@ -1,10 +1,10 @@ use crate::ast::{Enum, Field, Input, Struct}; use crate::attr::Trait; use crate::generics::InferredBounds; +use crate::span::MemberSpan; use proc_macro2::TokenStream; use quote::{format_ident, quote, quote_spanned, ToTokens}; use std::collections::BTreeSet as Set; -use syn::spanned::Spanned; use syn::{ Data, DeriveInput, GenericArgument, Member, PathArguments, Result, Token, Type, Visibility, }; @@ -39,7 +39,7 @@ fn impl_struct(input: Struct) -> TokenStream { error_inferred_bounds.insert(ty, quote!(std::error::Error + 'static)); } let asref = if type_is_option(source_field.ty) { - Some(quote_spanned!(source.span()=> .as_ref()?)) + Some(quote_spanned!(source.member_span()=> .as_ref()?)) } else { None }; @@ -67,13 +67,13 @@ fn impl_struct(input: Struct) -> TokenStream { let body = if let Some(source_field) = input.source_field() { let source = &source_field.member; let source_provide = if type_is_option(source_field.ty) { - quote_spanned! {source.span()=> + quote_spanned! {source.member_span()=> if let ::core::option::Option::Some(source) = &self.#source { source.thiserror_provide(#request); } } } else { - quote_spanned! {source.span()=> + quote_spanned! {source.member_span()=> self.#source.thiserror_provide(#request); } }; @@ -214,7 +214,7 @@ fn impl_enum(input: Enum) -> TokenStream { error_inferred_bounds.insert(ty, quote!(std::error::Error + 'static)); } let asref = if type_is_option(source_field.ty) { - Some(quote_spanned!(source.span()=> .as_ref()?)) + Some(quote_spanned!(source.member_span()=> .as_ref()?)) } else { None }; @@ -256,13 +256,13 @@ fn impl_enum(input: Enum) -> TokenStream { let source = &source_field.member; let varsource = quote!(source); let source_provide = if type_is_option(source_field.ty) { - quote_spanned! {source.span()=> + quote_spanned! {source.member_span()=> if let ::core::option::Option::Some(source) = #varsource { source.thiserror_provide(#request); } } } else { - quote_spanned! {source.span()=> + quote_spanned! {source.member_span()=> #varsource.thiserror_provide(#request); } }; @@ -295,13 +295,13 @@ fn impl_enum(input: Enum) -> TokenStream { let backtrace = &backtrace_field.member; let varsource = quote!(source); let source_provide = if type_is_option(source_field.ty) { - quote_spanned! {backtrace.span()=> + quote_spanned! {backtrace.member_span()=> if let ::core::option::Option::Some(source) = #varsource { source.thiserror_provide(#request); } } } else { - quote_spanned! {backtrace.span()=> + quote_spanned! {backtrace.member_span()=> #varsource.thiserror_provide(#request); } }; diff --git a/impl/src/lib.rs b/impl/src/lib.rs index c5dab93..683c8fb 100644 --- a/impl/src/lib.rs +++ b/impl/src/lib.rs @@ -22,6 +22,7 @@ mod expand; mod fmt; mod generics; mod prop; +mod span; mod valid; use proc_macro::TokenStream; diff --git a/impl/src/prop.rs b/impl/src/prop.rs index e4d2f18..5a6a6bc 100644 --- a/impl/src/prop.rs +++ b/impl/src/prop.rs @@ -1,4 +1,5 @@ use crate::ast::{Enum, Field, Struct, Variant}; +use crate::span::MemberSpan; use proc_macro2::Span; use syn::spanned::Spanned; use syn::{Member, Type}; @@ -79,7 +80,7 @@ impl Field<'_> { } else if let Some(from_attr) = &self.attrs.from { from_attr.path().span() } else { - self.member.span() + self.member.member_span() } } } diff --git a/impl/src/span.rs b/impl/src/span.rs new file mode 100644 index 0000000..c1237dd --- /dev/null +++ b/impl/src/span.rs @@ -0,0 +1,15 @@ +use proc_macro2::Span; +use syn::Member; + +pub trait MemberSpan { + fn member_span(&self) -> Span; +} + +impl MemberSpan for Member { + fn member_span(&self) -> Span { + match self { + Member::Named(ident) => ident.span(), + Member::Unnamed(index) => index.span, + } + } +}