Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consistently use quote! when emitting 'source' #121

Merged
merged 1 commit into from Feb 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 source_literal = quote! { source };
let dyn_error = quote_spanned!(source.span()=> #source_literal #asref.as_dyn_error());
quote! {
#ty::#ident {#source: source, ..} => std::option::Option::Some(#dyn_error),
#ty::#ident {#source: #source_literal, ..} => 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 source_literal = 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())
#source_literal.as_ref().and_then(|source| #source_literal.as_dyn_error().backtrace())
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks wrong -- I am going to turn this back into |source| source.as_dyn_error().backtrace() in the merge.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch! Sorry about that.

}
} else {
quote_spanned! {source.span()=>
source.as_dyn_error().backtrace()
#source_literal.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: #source_literal,
..
} => {
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));