Skip to content

Commit

Permalink
Merge pull request #370 from dtolnay/stderror
Browse files Browse the repository at this point in the history
Access Error trait exclusively through ::thiserror
  • Loading branch information
dtolnay authored Nov 6, 2024
2 parents db7825e + e0e9943 commit bf3d6f3
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
22 changes: 11 additions & 11 deletions impl/src/expand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ fn fallback(input: &DeriveInput, error: syn::Error) -> TokenStream {

#[allow(unused_qualifications)]
#[automatically_derived]
impl #impl_generics ::std::error::Error for #ty #ty_generics #where_clause
impl #impl_generics ::thiserror::__private::Error for #ty #ty_generics #where_clause
where
// Work around trivial bounds being unstable.
// https://github.com/rust-lang/rust/issues/48214
Expand All @@ -62,17 +62,17 @@ fn impl_struct(input: Struct) -> TokenStream {
let source_body = if let Some(transparent_attr) = &input.attrs.transparent {
let only_field = &input.fields[0];
if only_field.contains_generic {
error_inferred_bounds.insert(only_field.ty, quote!(::std::error::Error));
error_inferred_bounds.insert(only_field.ty, quote!(::thiserror::__private::Error));
}
let member = &only_field.member;
Some(quote_spanned! {transparent_attr.span=>
::std::error::Error::source(self.#member.as_dyn_error())
::thiserror::__private::Error::source(self.#member.as_dyn_error())
})
} else if let Some(source_field) = input.source_field() {
let source = &source_field.member;
if source_field.contains_generic {
let ty = unoptional_type(source_field.ty);
error_inferred_bounds.insert(ty, quote!(::std::error::Error + 'static));
error_inferred_bounds.insert(ty, quote!(::thiserror::__private::Error + 'static));
}
let asref = if type_is_option(source_field.ty) {
Some(quote_spanned!(source.span()=> .as_ref()?))
Expand All @@ -90,7 +90,7 @@ fn impl_struct(input: Struct) -> TokenStream {
};
let source_method = source_body.map(|body| {
quote! {
fn source(&self) -> ::core::option::Option<&(dyn ::std::error::Error + 'static)> {
fn source(&self) -> ::core::option::Option<&(dyn ::thiserror::__private::Error + 'static)> {
use ::thiserror::__private::AsDynError as _;
#body
}
Expand Down Expand Up @@ -218,7 +218,7 @@ fn impl_struct(input: Struct) -> TokenStream {
quote! {
#[allow(unused_qualifications)]
#[automatically_derived]
impl #impl_generics ::std::error::Error for #ty #ty_generics #error_where_clause {
impl #impl_generics ::thiserror::__private::Error for #ty #ty_generics #error_where_clause {
#source_method
#provide_method
}
Expand All @@ -238,11 +238,11 @@ fn impl_enum(input: Enum) -> TokenStream {
if let Some(transparent_attr) = &variant.attrs.transparent {
let only_field = &variant.fields[0];
if only_field.contains_generic {
error_inferred_bounds.insert(only_field.ty, quote!(::std::error::Error));
error_inferred_bounds.insert(only_field.ty, quote!(::thiserror::__private::Error));
}
let member = &only_field.member;
let source = quote_spanned! {transparent_attr.span=>
::std::error::Error::source(transparent.as_dyn_error())
::thiserror::__private::Error::source(transparent.as_dyn_error())
};
quote! {
#ty::#ident {#member: transparent} => #source,
Expand All @@ -251,7 +251,7 @@ fn impl_enum(input: Enum) -> TokenStream {
let source = &source_field.member;
if source_field.contains_generic {
let ty = unoptional_type(source_field.ty);
error_inferred_bounds.insert(ty, quote!(::std::error::Error + 'static));
error_inferred_bounds.insert(ty, quote!(::thiserror::__private::Error + 'static));
}
let asref = if type_is_option(source_field.ty) {
Some(quote_spanned!(source.span()=> .as_ref()?))
Expand All @@ -272,7 +272,7 @@ fn impl_enum(input: Enum) -> TokenStream {
}
});
Some(quote! {
fn source(&self) -> ::core::option::Option<&(dyn ::std::error::Error + 'static)> {
fn source(&self) -> ::core::option::Option<&(dyn ::thiserror::__private::Error + 'static)> {
use ::thiserror::__private::AsDynError as _;
#[allow(deprecated)]
match self {
Expand Down Expand Up @@ -483,7 +483,7 @@ fn impl_enum(input: Enum) -> TokenStream {
quote! {
#[allow(unused_qualifications)]
#[automatically_derived]
impl #impl_generics ::std::error::Error for #ty #ty_generics #error_where_clause {
impl #impl_generics ::thiserror::__private::Error for #ty #ty_generics #error_where_clause {
#source_method
#provide_method
}
Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,4 +287,6 @@ pub mod __private {
#[cfg(error_generic_member_access)]
#[doc(hidden)]
pub use crate::provide::ThiserrorProvide;
#[doc(hidden)]
pub use std::error::Error;
}

0 comments on commit bf3d6f3

Please sign in to comment.