Extract combined backtrace-source case to separate match arm

This commit is contained in:
David Tolnay 2021-08-28 13:44:42 -07:00
parent d49c5af10f
commit 799bb53b10
No known key found for this signature in database
GPG key ID: F9BA143B95FF6D82

View file

@ -240,34 +240,36 @@ 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! {