Skip to content

Commit

Permalink
Merge pull request 121 from Aaron1011/fix/source-span
Browse files Browse the repository at this point in the history
  • Loading branch information
dtolnay committed Feb 19, 2021
2 parents d0f521c + 0fa679b commit d81b746
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
12 changes: 7 additions & 5 deletions impl/src/expand.rs
Expand Up @@ -171,9 +171,10 @@ fn impl_enum(input: Enum) -> TokenStream {
} else {
None
};
let dyn_error = quote_spanned!(source.span()=> source #asref.as_dyn_error());
let varsource = quote!(source);
let dyn_error = quote_spanned!(source.span()=> #varsource #asref.as_dyn_error());
quote! {
#ty::#ident {#source: source, ..} => std::option::Option::Some(#dyn_error),
#ty::#ident {#source: #varsource, ..} => std::option::Option::Some(#dyn_error),
}
} else {
quote! {
Expand Down Expand Up @@ -203,13 +204,14 @@ fn impl_enum(input: Enum) -> TokenStream {
{
let backtrace = &backtrace_field.member;
let source = &source_field.member;
let varsource = quote!(source);
let source_backtrace = if type_is_option(source_field.ty) {
quote_spanned! {source.span()=>
source.as_ref().and_then(|source| source.as_dyn_error().backtrace())
#varsource.as_ref().and_then(|source| source.as_dyn_error().backtrace())
}
} else {
quote_spanned! {source.span()=>
source.as_dyn_error().backtrace()
#varsource.as_dyn_error().backtrace()
}
};
let combinator = if type_is_option(backtrace_field.ty) {
Expand All @@ -224,7 +226,7 @@ fn impl_enum(input: Enum) -> TokenStream {
quote! {
#ty::#ident {
#backtrace: backtrace,
#source: source,
#source: #varsource,
..
} => {
use thiserror::private::AsDynError;
Expand Down
13 changes: 13 additions & 0 deletions tests/test_source.rs
Expand Up @@ -48,3 +48,16 @@ fn test_boxed_source() {
let error = BoxedSource { source };
error.source().unwrap().downcast_ref::<io::Error>().unwrap();
}

macro_rules! error_from_macro {
($($variants:tt)*) => {
#[derive(Error)]
#[derive(Debug)]
pub enum MacroSource {
$($variants)*
}
}
}

// Test that we generate impls with the proper hygiene
error_from_macro!(#[error("Something")] Variant(#[from] io::Error));

0 comments on commit d81b746

Please sign in to comment.