Skip to content

Commit

Permalink
Remove reliance on Spanned for Member
Browse files Browse the repository at this point in the history
  • Loading branch information
dtolnay committed Oct 19, 2023
1 parent c9fe739 commit 7cec716
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 10 deletions.
18 changes: 9 additions & 9 deletions impl/src/expand.rs
Original file line number Diff line number Diff line change
@@ -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,
};
Expand Down Expand Up @@ -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
};
Expand Down Expand Up @@ -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);
}
};
Expand Down Expand Up @@ -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
};
Expand Down Expand Up @@ -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);
}
};
Expand Down Expand Up @@ -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);
}
};
Expand Down
1 change: 1 addition & 0 deletions impl/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ mod expand;
mod fmt;
mod generics;
mod prop;
mod span;
mod valid;

use proc_macro::TokenStream;
Expand Down
3 changes: 2 additions & 1 deletion impl/src/prop.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -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()
}
}
}
Expand Down
15 changes: 15 additions & 0 deletions impl/src/span.rs
Original file line number Diff line number Diff line change
@@ -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,
}
}
}

0 comments on commit 7cec716

Please sign in to comment.