diff --git a/impl/src/expand.rs b/impl/src/expand.rs index 1fb765e..5f761b8 100644 --- a/impl/src/expand.rs +++ b/impl/src/expand.rs @@ -240,36 +240,38 @@ fn impl_enum(input: Enum) -> TokenStream { } } } - (Some(backtrace_field), _) => { + (Some(backtrace_field), Some(source_field)) + if backtrace_field.member == source_field.member => + { let backtrace = &backtrace_field.member; - if variant.source_field().map_or(false, |f| f.member == *backtrace) { - let varsource = quote!(source); - let source_backtrace = if type_is_option(backtrace_field.ty) { - quote_spanned! {backtrace.span()=> - #varsource.as_ref().and_then(|source| source.as_dyn_error().backtrace()) - } - } else { - quote_spanned! {backtrace.span()=> - #varsource.as_dyn_error().backtrace() - } - }; - quote! { - #ty::#ident {#backtrace: #varsource, ..} => { - use thiserror::private::AsDynError; - #source_backtrace - } + let varsource = quote!(source); + let source_backtrace = if type_is_option(source_field.ty) { + quote_spanned! {backtrace.span()=> + #varsource.as_ref().and_then(|source| source.as_dyn_error().backtrace()) } } else { - let body = if type_is_option(backtrace_field.ty) { - quote!(backtrace.as_ref()) - } else { - quote!(std::option::Option::Some(backtrace)) - }; - quote! { - #ty::#ident {#backtrace: backtrace, ..} => #body, + quote_spanned! {backtrace.span()=> + #varsource.as_dyn_error().backtrace() + } + }; + quote! { + #ty::#ident {#backtrace: #varsource, ..} => { + use thiserror::private::AsDynError; + #source_backtrace } } } + (Some(backtrace_field), _) => { + let backtrace = &backtrace_field.member; + let body = if type_is_option(backtrace_field.ty) { + quote!(backtrace.as_ref()) + } else { + quote!(std::option::Option::Some(backtrace)) + }; + quote! { + #ty::#ident {#backtrace: backtrace, ..} => #body, + } + } (None, _) => quote! { #ty::#ident {..} => std::option::Option::None, },