Access all std types through absolute path

This commit is contained in:
David Tolnay 2024-11-05 19:10:14 -05:00
parent 12b8c27f75
commit 2f92680b5f
No known key found for this signature in database
GPG key ID: F9BA143B95FF6D82

View file

@ -37,7 +37,7 @@ fn fallback(input: &DeriveInput, error: syn::Error) -> TokenStream {
#[allow(unused_qualifications)] #[allow(unused_qualifications)]
#[automatically_derived] #[automatically_derived]
impl #impl_generics std::error::Error for #ty #ty_generics #where_clause impl #impl_generics ::std::error::Error for #ty #ty_generics #where_clause
where where
// Work around trivial bounds being unstable. // Work around trivial bounds being unstable.
// https://github.com/rust-lang/rust/issues/48214 // https://github.com/rust-lang/rust/issues/48214
@ -62,17 +62,17 @@ fn impl_struct(input: Struct) -> TokenStream {
let source_body = if let Some(transparent_attr) = &input.attrs.transparent { let source_body = if let Some(transparent_attr) = &input.attrs.transparent {
let only_field = &input.fields[0]; let only_field = &input.fields[0];
if only_field.contains_generic { if only_field.contains_generic {
error_inferred_bounds.insert(only_field.ty, quote!(std::error::Error)); error_inferred_bounds.insert(only_field.ty, quote!(::std::error::Error));
} }
let member = &only_field.member; let member = &only_field.member;
Some(quote_spanned! {transparent_attr.span=> Some(quote_spanned! {transparent_attr.span=>
std::error::Error::source(self.#member.as_dyn_error()) ::std::error::Error::source(self.#member.as_dyn_error())
}) })
} else if let Some(source_field) = input.source_field() { } else if let Some(source_field) = input.source_field() {
let source = &source_field.member; let source = &source_field.member;
if source_field.contains_generic { if source_field.contains_generic {
let ty = unoptional_type(source_field.ty); let ty = unoptional_type(source_field.ty);
error_inferred_bounds.insert(ty, quote!(std::error::Error + 'static)); error_inferred_bounds.insert(ty, quote!(::std::error::Error + 'static));
} }
let asref = if type_is_option(source_field.ty) { let asref = if type_is_option(source_field.ty) {
Some(quote_spanned!(source.span()=> .as_ref()?)) Some(quote_spanned!(source.span()=> .as_ref()?))
@ -90,7 +90,7 @@ fn impl_struct(input: Struct) -> TokenStream {
}; };
let source_method = source_body.map(|body| { let source_method = source_body.map(|body| {
quote! { quote! {
fn source(&self) -> ::core::option::Option<&(dyn std::error::Error + 'static)> { fn source(&self) -> ::core::option::Option<&(dyn ::std::error::Error + 'static)> {
use ::thiserror::__private::AsDynError as _; use ::thiserror::__private::AsDynError as _;
#body #body
} }
@ -118,12 +118,12 @@ fn impl_struct(input: Struct) -> TokenStream {
} else if type_is_option(backtrace_field.ty) { } else if type_is_option(backtrace_field.ty) {
Some(quote! { Some(quote! {
if let ::core::option::Option::Some(backtrace) = &self.#backtrace { if let ::core::option::Option::Some(backtrace) = &self.#backtrace {
#request.provide_ref::<std::backtrace::Backtrace>(backtrace); #request.provide_ref::<::std::backtrace::Backtrace>(backtrace);
} }
}) })
} else { } else {
Some(quote! { Some(quote! {
#request.provide_ref::<std::backtrace::Backtrace>(&self.#backtrace); #request.provide_ref::<::std::backtrace::Backtrace>(&self.#backtrace);
}) })
}; };
quote! { quote! {
@ -134,16 +134,16 @@ fn impl_struct(input: Struct) -> TokenStream {
} else if type_is_option(backtrace_field.ty) { } else if type_is_option(backtrace_field.ty) {
quote! { quote! {
if let ::core::option::Option::Some(backtrace) = &self.#backtrace { if let ::core::option::Option::Some(backtrace) = &self.#backtrace {
#request.provide_ref::<std::backtrace::Backtrace>(backtrace); #request.provide_ref::<::std::backtrace::Backtrace>(backtrace);
} }
} }
} else { } else {
quote! { quote! {
#request.provide_ref::<std::backtrace::Backtrace>(&self.#backtrace); #request.provide_ref::<::std::backtrace::Backtrace>(&self.#backtrace);
} }
}; };
quote! { quote! {
fn provide<'_request>(&'_request self, #request: &mut std::error::Request<'_request>) { fn provide<'_request>(&'_request self, #request: &mut ::std::error::Request<'_request>) {
#body #body
} }
} }
@ -218,7 +218,7 @@ fn impl_struct(input: Struct) -> TokenStream {
quote! { quote! {
#[allow(unused_qualifications)] #[allow(unused_qualifications)]
#[automatically_derived] #[automatically_derived]
impl #impl_generics std::error::Error for #ty #ty_generics #error_where_clause { impl #impl_generics ::std::error::Error for #ty #ty_generics #error_where_clause {
#source_method #source_method
#provide_method #provide_method
} }
@ -238,11 +238,11 @@ fn impl_enum(input: Enum) -> TokenStream {
if let Some(transparent_attr) = &variant.attrs.transparent { if let Some(transparent_attr) = &variant.attrs.transparent {
let only_field = &variant.fields[0]; let only_field = &variant.fields[0];
if only_field.contains_generic { if only_field.contains_generic {
error_inferred_bounds.insert(only_field.ty, quote!(std::error::Error)); error_inferred_bounds.insert(only_field.ty, quote!(::std::error::Error));
} }
let member = &only_field.member; let member = &only_field.member;
let source = quote_spanned! {transparent_attr.span=> let source = quote_spanned! {transparent_attr.span=>
std::error::Error::source(transparent.as_dyn_error()) ::std::error::Error::source(transparent.as_dyn_error())
}; };
quote! { quote! {
#ty::#ident {#member: transparent} => #source, #ty::#ident {#member: transparent} => #source,
@ -251,7 +251,7 @@ fn impl_enum(input: Enum) -> TokenStream {
let source = &source_field.member; let source = &source_field.member;
if source_field.contains_generic { if source_field.contains_generic {
let ty = unoptional_type(source_field.ty); let ty = unoptional_type(source_field.ty);
error_inferred_bounds.insert(ty, quote!(std::error::Error + 'static)); error_inferred_bounds.insert(ty, quote!(::std::error::Error + 'static));
} }
let asref = if type_is_option(source_field.ty) { let asref = if type_is_option(source_field.ty) {
Some(quote_spanned!(source.span()=> .as_ref()?)) Some(quote_spanned!(source.span()=> .as_ref()?))
@ -272,7 +272,7 @@ fn impl_enum(input: Enum) -> TokenStream {
} }
}); });
Some(quote! { Some(quote! {
fn source(&self) -> ::core::option::Option<&(dyn std::error::Error + 'static)> { fn source(&self) -> ::core::option::Option<&(dyn ::std::error::Error + 'static)> {
use ::thiserror::__private::AsDynError as _; use ::thiserror::__private::AsDynError as _;
#[allow(deprecated)] #[allow(deprecated)]
match self { match self {
@ -309,12 +309,12 @@ fn impl_enum(input: Enum) -> TokenStream {
let self_provide = if type_is_option(backtrace_field.ty) { let self_provide = if type_is_option(backtrace_field.ty) {
quote! { quote! {
if let ::core::option::Option::Some(backtrace) = backtrace { if let ::core::option::Option::Some(backtrace) = backtrace {
#request.provide_ref::<std::backtrace::Backtrace>(backtrace); #request.provide_ref::<::std::backtrace::Backtrace>(backtrace);
} }
} }
} else { } else {
quote! { quote! {
#request.provide_ref::<std::backtrace::Backtrace>(backtrace); #request.provide_ref::<::std::backtrace::Backtrace>(backtrace);
} }
}; };
quote! { quote! {
@ -357,12 +357,12 @@ fn impl_enum(input: Enum) -> TokenStream {
let body = if type_is_option(backtrace_field.ty) { let body = if type_is_option(backtrace_field.ty) {
quote! { quote! {
if let ::core::option::Option::Some(backtrace) = backtrace { if let ::core::option::Option::Some(backtrace) = backtrace {
#request.provide_ref::<std::backtrace::Backtrace>(backtrace); #request.provide_ref::<::std::backtrace::Backtrace>(backtrace);
} }
} }
} else { } else {
quote! { quote! {
#request.provide_ref::<std::backtrace::Backtrace>(backtrace); #request.provide_ref::<::std::backtrace::Backtrace>(backtrace);
} }
}; };
quote! { quote! {
@ -377,7 +377,7 @@ fn impl_enum(input: Enum) -> TokenStream {
} }
}); });
Some(quote! { Some(quote! {
fn provide<'_request>(&'_request self, #request: &mut std::error::Request<'_request>) { fn provide<'_request>(&'_request self, #request: &mut ::std::error::Request<'_request>) {
#[allow(deprecated)] #[allow(deprecated)]
match self { match self {
#(#arms)* #(#arms)*
@ -483,7 +483,7 @@ fn impl_enum(input: Enum) -> TokenStream {
quote! { quote! {
#[allow(unused_qualifications)] #[allow(unused_qualifications)]
#[automatically_derived] #[automatically_derived]
impl #impl_generics std::error::Error for #ty #ty_generics #error_where_clause { impl #impl_generics ::std::error::Error for #ty #ty_generics #error_where_clause {
#source_method #source_method
#provide_method #provide_method
} }
@ -532,11 +532,11 @@ fn from_initializer(
let backtrace_member = &backtrace_field.member; let backtrace_member = &backtrace_field.member;
if type_is_option(backtrace_field.ty) { if type_is_option(backtrace_field.ty) {
quote! { quote! {
#backtrace_member: ::core::option::Option::Some(std::backtrace::Backtrace::capture()), #backtrace_member: ::core::option::Option::Some(::std::backtrace::Backtrace::capture()),
} }
} else { } else {
quote! { quote! {
#backtrace_member: ::core::convert::From::from(std::backtrace::Backtrace::capture()), #backtrace_member: ::core::convert::From::from(::std::backtrace::Backtrace::capture()),
} }
} }
}); });