Skip to content

Commit 366a7b2

Browse files
authored
Merge pull request #395 from dtolnay/fallback
Move fallback expansion to separate module
2 parents 6712f8c + 88a4603 commit 366a7b2

File tree

3 files changed

+36
-30
lines changed

3 files changed

+36
-30
lines changed

impl/src/expand.rs

+3-30
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use crate::ast::{Enum, Field, Input, Struct};
22
use crate::attr::Trait;
3+
use crate::fallback;
34
use crate::generics::InferredBounds;
45
use crate::unraw::MemberUnraw;
56
use proc_macro2::{Ident, Span, TokenStream};
@@ -13,7 +14,7 @@ pub fn derive(input: &DeriveInput) -> TokenStream {
1314
// If there are invalid attributes in the input, expand to an Error impl
1415
// anyway to minimize spurious knock-on errors in other code that uses
1516
// this type as an Error.
16-
Err(error) => fallback(input, error),
17+
Err(error) => fallback::expand(input, error),
1718
}
1819
}
1920

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

29-
fn fallback(input: &DeriveInput, error: syn::Error) -> TokenStream {
30-
let ty = call_site_ident(&input.ident);
31-
let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl();
32-
33-
let error = error.to_compile_error();
34-
35-
quote! {
36-
#error
37-
38-
#[allow(unused_qualifications)]
39-
#[automatically_derived]
40-
impl #impl_generics ::thiserror::__private::Error for #ty #ty_generics #where_clause
41-
where
42-
// Work around trivial bounds being unstable.
43-
// https://github.com/rust-lang/rust/issues/48214
44-
for<'workaround> #ty #ty_generics: ::core::fmt::Debug,
45-
{}
46-
47-
#[allow(unused_qualifications)]
48-
#[automatically_derived]
49-
impl #impl_generics ::core::fmt::Display for #ty #ty_generics #where_clause {
50-
fn fmt(&self, __formatter: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
51-
::core::unreachable!()
52-
}
53-
}
54-
}
55-
}
56-
5730
fn impl_struct(input: Struct) -> TokenStream {
5831
let ty = call_site_ident(&input.ident);
5932
let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl();
@@ -494,7 +467,7 @@ fn impl_enum(input: Enum) -> TokenStream {
494467

495468
// Create an ident with which we can expand `impl Trait for #ident {}` on a
496469
// deprecated type without triggering deprecation warning on the generated impl.
497-
fn call_site_ident(ident: &Ident) -> Ident {
470+
pub(crate) fn call_site_ident(ident: &Ident) -> Ident {
498471
let mut ident = ident.clone();
499472
ident.set_span(ident.span().resolved_at(Span::call_site()));
500473
ident

impl/src/fallback.rs

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
use crate::expand::call_site_ident;
2+
use proc_macro2::TokenStream;
3+
use quote::quote;
4+
use syn::DeriveInput;
5+
6+
pub(crate) fn expand(input: &DeriveInput, error: syn::Error) -> TokenStream {
7+
let ty = call_site_ident(&input.ident);
8+
let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl();
9+
10+
let error = error.to_compile_error();
11+
12+
quote! {
13+
#error
14+
15+
#[allow(unused_qualifications)]
16+
#[automatically_derived]
17+
impl #impl_generics ::thiserror::__private::Error for #ty #ty_generics #where_clause
18+
where
19+
// Work around trivial bounds being unstable.
20+
// https://github.com/rust-lang/rust/issues/48214
21+
for<'workaround> #ty #ty_generics: ::core::fmt::Debug,
22+
{}
23+
24+
#[allow(unused_qualifications)]
25+
#[automatically_derived]
26+
impl #impl_generics ::core::fmt::Display for #ty #ty_generics #where_clause {
27+
fn fmt(&self, __formatter: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
28+
::core::unreachable!()
29+
}
30+
}
31+
}
32+
}

impl/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ extern crate proc_macro;
2121
mod ast;
2222
mod attr;
2323
mod expand;
24+
mod fallback;
2425
mod fmt;
2526
mod generics;
2627
mod prop;

0 commit comments

Comments
 (0)