From 88a46035e1bbb17ada53acf28ed7b7b00a0b049a Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Sat, 7 Dec 2024 09:02:44 -0800 Subject: [PATCH] Move fallback expansion to separate module --- impl/src/expand.rs | 33 +++------------------------------ impl/src/fallback.rs | 32 ++++++++++++++++++++++++++++++++ impl/src/lib.rs | 1 + 3 files changed, 36 insertions(+), 30 deletions(-) create mode 100644 impl/src/fallback.rs diff --git a/impl/src/expand.rs b/impl/src/expand.rs index 52782e3..d0b302b 100644 --- a/impl/src/expand.rs +++ b/impl/src/expand.rs @@ -1,5 +1,6 @@ use crate::ast::{Enum, Field, Input, Struct}; use crate::attr::Trait; +use crate::fallback; use crate::generics::InferredBounds; use crate::unraw::MemberUnraw; use proc_macro2::{Ident, Span, TokenStream}; @@ -13,7 +14,7 @@ pub fn derive(input: &DeriveInput) -> TokenStream { // If there are invalid attributes in the input, expand to an Error impl // anyway to minimize spurious knock-on errors in other code that uses // this type as an Error. - Err(error) => fallback(input, error), + Err(error) => fallback::expand(input, error), } } @@ -26,34 +27,6 @@ fn try_expand(input: &DeriveInput) -> Result { }) } -fn fallback(input: &DeriveInput, error: syn::Error) -> TokenStream { - let ty = call_site_ident(&input.ident); - let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl(); - - let error = error.to_compile_error(); - - quote! { - #error - - #[allow(unused_qualifications)] - #[automatically_derived] - 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 - for<'workaround> #ty #ty_generics: ::core::fmt::Debug, - {} - - #[allow(unused_qualifications)] - #[automatically_derived] - impl #impl_generics ::core::fmt::Display for #ty #ty_generics #where_clause { - fn fmt(&self, __formatter: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - ::core::unreachable!() - } - } - } -} - fn impl_struct(input: Struct) -> TokenStream { let ty = call_site_ident(&input.ident); let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl(); @@ -494,7 +467,7 @@ fn impl_enum(input: Enum) -> TokenStream { // Create an ident with which we can expand `impl Trait for #ident {}` on a // deprecated type without triggering deprecation warning on the generated impl. -fn call_site_ident(ident: &Ident) -> Ident { +pub(crate) fn call_site_ident(ident: &Ident) -> Ident { let mut ident = ident.clone(); ident.set_span(ident.span().resolved_at(Span::call_site())); ident diff --git a/impl/src/fallback.rs b/impl/src/fallback.rs new file mode 100644 index 0000000..e9c429b --- /dev/null +++ b/impl/src/fallback.rs @@ -0,0 +1,32 @@ +use crate::expand::call_site_ident; +use proc_macro2::TokenStream; +use quote::quote; +use syn::DeriveInput; + +pub(crate) fn expand(input: &DeriveInput, error: syn::Error) -> TokenStream { + let ty = call_site_ident(&input.ident); + let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl(); + + let error = error.to_compile_error(); + + quote! { + #error + + #[allow(unused_qualifications)] + #[automatically_derived] + 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 + for<'workaround> #ty #ty_generics: ::core::fmt::Debug, + {} + + #[allow(unused_qualifications)] + #[automatically_derived] + impl #impl_generics ::core::fmt::Display for #ty #ty_generics #where_clause { + fn fmt(&self, __formatter: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + ::core::unreachable!() + } + } + } +} diff --git a/impl/src/lib.rs b/impl/src/lib.rs index 5148323..7f3767e 100644 --- a/impl/src/lib.rs +++ b/impl/src/lib.rs @@ -21,6 +21,7 @@ extern crate proc_macro; mod ast; mod attr; mod expand; +mod fallback; mod fmt; mod generics; mod prop;