Skip to content

Commit

Permalink
Merge pull request #395 from dtolnay/fallback
Browse files Browse the repository at this point in the history
Move fallback expansion to separate module
  • Loading branch information
dtolnay authored Dec 7, 2024
2 parents 6712f8c + 88a4603 commit 366a7b2
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 30 deletions.
33 changes: 3 additions & 30 deletions impl/src/expand.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand All @@ -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),
}
}

Expand All @@ -26,34 +27,6 @@ fn try_expand(input: &DeriveInput) -> Result<TokenStream> {
})
}

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();
Expand Down Expand Up @@ -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
Expand Down
32 changes: 32 additions & 0 deletions impl/src/fallback.rs
Original file line number Diff line number Diff line change
@@ -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!()
}
}
}
}
1 change: 1 addition & 0 deletions impl/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ extern crate proc_macro;
mod ast;
mod attr;
mod expand;
mod fallback;
mod fmt;
mod generics;
mod prop;
Expand Down

0 comments on commit 366a7b2

Please sign in to comment.