wherror-impl 2.3.1

Implementation detail of the `wherror` crate (fork of thiserror with Location support)
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
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};
use quote::{format_ident, quote, quote_spanned, ToTokens};
use std::collections::BTreeSet as Set;
use syn::{DeriveInput, GenericArgument, PathArguments, Result, Token, Type};

pub fn derive(input: &DeriveInput) -> TokenStream {
    match try_expand(input) {
        Ok(expanded) => expanded,
        // If there are invalid attributes in the input, expand to an Error impl
        // anyway to minimize spurious secondary errors in other code that uses
        // this type as an Error.
        Err(error) => fallback::expand(input, error),
    }
}

fn try_expand(input: &DeriveInput) -> Result<TokenStream> {
    let input = Input::from_syn(input)?;
    input.validate()?;
    Ok(match input {
        Input::Struct(input) => impl_struct(input),
        Input::Enum(input) => impl_enum(input),
    })
}

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();
    let mut error_inferred_bounds = InferredBounds::new();

    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!(::wherror::__private::Error));
        }
        let member = &only_field.member;
        Some(quote_spanned! {transparent_attr.span=>
            ::wherror::__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!(::wherror::__private::Error + 'static));
        }
        let asref = if type_is_option(source_field.ty) {
            Some(quote_spanned!(source.span()=> .as_ref()?))
        } else {
            None
        };
        let dyn_error = quote_spanned! {source_field.source_span()=>
            self.#source #asref.as_dyn_error()
        };
        Some(quote! {
            ::core::option::Option::Some(#dyn_error)
        })
    } else {
        None
    };
    let source_method = source_body.map(|body| {
        quote! {
            fn source(&self) -> ::core::option::Option<&(dyn ::wherror::__private::Error + 'static)> {
                use ::wherror::__private::AsDynError as _;
                #body
            }
        }
    });

    let provide_method = input.backtrace_field().map(|backtrace_field| {
        let request = quote!(request);
        let backtrace = &backtrace_field.member;
        let body = if let Some(source_field) = input.source_field() {
            let source = &source_field.member;
            let source_provide = if type_is_option(source_field.ty) {
                quote_spanned! {source.span()=>
                    if let ::core::option::Option::Some(source) = &self.#source {
                        source.thiserror_provide(#request);
                    }
                }
            } else {
                quote_spanned! {source.span()=>
                    self.#source.thiserror_provide(#request);
                }
            };
            let self_provide = if source == backtrace {
                None
            } else if type_is_option(backtrace_field.ty) {
                Some(quote! {
                    if let ::core::option::Option::Some(backtrace) = &self.#backtrace {
                        #request.provide_ref::<::wherror::__private::Backtrace>(backtrace);
                    }
                })
            } else {
                Some(quote! {
                    #request.provide_ref::<::wherror::__private::Backtrace>(&self.#backtrace);
                })
            };
            quote! {
                use ::wherror::__private::ThiserrorProvide as _;
                #source_provide
                #self_provide
            }
        } else if type_is_option(backtrace_field.ty) {
            quote! {
                if let ::core::option::Option::Some(backtrace) = &self.#backtrace {
                    #request.provide_ref::<::wherror::__private::Backtrace>(backtrace);
                }
            }
        } else {
            quote! {
                #request.provide_ref::<::wherror::__private::Backtrace>(&self.#backtrace);
            }
        };
        quote! {
            fn provide<'_request>(&'_request self, #request: &mut ::core::error::Request<'_request>) {
                #body
            }
        }
    });

    let mut display_implied_bounds = Set::new();
    let display_body = if input.attrs.transparent.is_some() {
        let only_field = &input.fields[0].member;
        display_implied_bounds.insert((0, Trait::Display));
        Some(quote! {
            ::core::fmt::Display::fmt(&self.#only_field, __formatter)
        })
    } else if let Some(display) = &input.attrs.display {
        display_implied_bounds.clone_from(&display.implied_bounds);
        let use_as_display = use_as_display(display.has_bonus_display);
        let pat = fields_pat(&input.fields);
        Some(quote! {
            #use_as_display
            #[allow(unused_variables, deprecated)]
            let Self #pat = self;
            #display
        })
    } else if let Some(debug_attr) = &input.attrs.debug {
        // Fall back to Debug representation when #[error(debug)] is specified
        Some(quote_spanned! {debug_attr.span=>
            ::core::fmt::Debug::fmt(self, __formatter)
        })
    } else {
        None
    };
    let display_impl = display_body.map(|body| {
        let mut display_inferred_bounds = InferredBounds::new();
        for (field, bound) in display_implied_bounds {
            let field = &input.fields[field];
            if field.contains_generic {
                display_inferred_bounds.insert(field.ty, bound);
            }
        }
        let display_where_clause = display_inferred_bounds.augment_where_clause(input.generics);
        quote! {
            #[allow(unused_qualifications)]
            #[automatically_derived]
            impl #impl_generics ::core::fmt::Display for #ty #ty_generics #display_where_clause {
                #[allow(clippy::used_underscore_binding)]
                fn fmt(&self, __formatter: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
                    #body
                }
            }
        }
    });

    let from_impl = input.from_field().map(|from_field| {
        let span = from_field.attrs.from.unwrap().span;
        let backtrace_field = input.distinct_backtrace_field();
        let from = unoptional_type(from_field.ty);
        let track_caller = input.location_field().map(|_| quote!(#[track_caller]));
        let source_var = Ident::new("source", span);
        let body = from_initializer(
            from_field,
            backtrace_field,
            &source_var,
            input.location_field(),
        );

        // Check if the field type (after unwrapping Option) is Box<T>
        let field_type = type_parameter_of_option(from_field.ty).unwrap_or(from_field.ty);
        let box_implementations = type_parameter_of_box(field_type).map(|inner_type| {
            // Generate From<T> implementation that boxes the value
            let inner_source_var = Ident::new("source", span);
            let boxed_body = from_initializer_for_box(
                from_field,
                backtrace_field,
                &inner_source_var,
                input.location_field(),
            );
            let inner_from_function = quote! {
                #track_caller
                fn from(#inner_source_var: #inner_type) -> Self {
                    #ty #boxed_body
                }
            };
            let inner_from_impl = quote_spanned! {span=>
                #[automatically_derived]
                impl #impl_generics ::core::convert::From<#inner_type> for #ty #ty_generics #where_clause {
                    #inner_from_function
                }
            };
            quote! {
                #[allow(
                    deprecated,
                    unused_qualifications,
                    clippy::elidable_lifetime_names,
                    clippy::needless_lifetimes,
                )]
                #inner_from_impl
            }
        });

        let from_function = quote! {
            #track_caller
            fn from(#source_var: #from) -> Self {
                #ty #body
            }
        };
        let from_impl = quote_spanned! {span=>
            #[automatically_derived]
            impl #impl_generics ::core::convert::From<#from> for #ty #ty_generics #where_clause {
                #from_function
            }
        };
        Some(quote! {
            #[allow(
                deprecated,
                unused_qualifications,
                clippy::elidable_lifetime_names,
                clippy::needless_lifetimes,
            )]
            #from_impl
            #box_implementations
        })
    });

    let location_impl = input.location_field().map(|location_field| {
        let location = &location_field.member;
        let body = if type_is_option(location_field.ty) {
            quote! {
                self.#location
            }
        } else {
            quote! {
                Some(self.#location)
            }
        };
        quote! {
            #[allow(unused_qualifications)]
            #[automatically_derived]
            impl #impl_generics #ty #ty_generics #where_clause {
                pub fn location(&self) -> Option<&'static ::core::panic::Location<'static>> {
                    #body
                }
            }
        }
    });

    if input.generics.type_params().next().is_some() {
        let self_token = <Token![Self]>::default();
        error_inferred_bounds.insert(self_token, Trait::Debug);
        error_inferred_bounds.insert(self_token, Trait::Display);
    }
    let error_where_clause = error_inferred_bounds.augment_where_clause(input.generics);

    quote! {
        #[allow(unused_qualifications)]
        #[automatically_derived]
        impl #impl_generics ::wherror::__private::Error for #ty #ty_generics #error_where_clause {
            #source_method
            #provide_method
        }
        #display_impl
        #from_impl
        #location_impl
    }
}

fn impl_enum(input: Enum) -> TokenStream {
    let ty = call_site_ident(&input.ident);
    let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl();
    let mut error_inferred_bounds = InferredBounds::new();

    let source_method = if input.has_source() {
        let arms = input.variants.iter().map(|variant| {
            let ident = &variant.ident;
            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!(::wherror::__private::Error));
                }
                let member = &only_field.member;
                let source = quote_spanned! {transparent_attr.span=>
                    ::wherror::__private::Error::source(transparent.as_dyn_error())
                };
                quote! {
                    #ty::#ident {#member: transparent} => #source,
                }
            } else if let Some(source_field) = variant.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!(::wherror::__private::Error + 'static));
                }
                let asref = if type_is_option(source_field.ty) {
                    Some(quote_spanned!(source.span()=> .as_ref()?))
                } else {
                    None
                };
                let varsource = quote!(source);
                let dyn_error = quote_spanned! {source_field.source_span()=>
                    #varsource #asref.as_dyn_error()
                };
                quote! {
                    #ty::#ident {#source: #varsource, ..} => ::core::option::Option::Some(#dyn_error),
                }
            } else {
                quote! {
                    #ty::#ident {..} => ::core::option::Option::None,
                }
            }
        });
        Some(quote! {
            fn source(&self) -> ::core::option::Option<&(dyn ::wherror::__private::Error + 'static)> {
                use ::wherror::__private::AsDynError as _;
                #[allow(deprecated)]
                match self {
                    #(#arms)*
                }
            }
        })
    } else {
        None
    };

    let provide_method = if input.has_backtrace() {
        let request = quote!(request);
        let arms = input.variants.iter().map(|variant| {
            let ident = &variant.ident;
            match (variant.backtrace_field(), variant.source_field()) {
                (Some(backtrace_field), Some(source_field))
                    if backtrace_field.attrs.backtrace.is_none() =>
                {
                    let backtrace = &backtrace_field.member;
                    let source = &source_field.member;
                    let varsource = quote!(source);
                    let source_provide = if type_is_option(source_field.ty) {
                        quote_spanned! {source.span()=>
                            if let ::core::option::Option::Some(source) = #varsource {
                                source.thiserror_provide(#request);
                            }
                        }
                    } else {
                        quote_spanned! {source.span()=>
                            #varsource.thiserror_provide(#request);
                        }
                    };
                    let self_provide = if type_is_option(backtrace_field.ty) {
                        quote! {
                            if let ::core::option::Option::Some(backtrace) = backtrace {
                                #request.provide_ref::<::wherror::__private::Backtrace>(backtrace);
                            }
                        }
                    } else {
                        quote! {
                            #request.provide_ref::<::wherror::__private::Backtrace>(backtrace);
                        }
                    };
                    quote! {
                        #ty::#ident {
                            #backtrace: backtrace,
                            #source: #varsource,
                            ..
                        } => {
                            use ::wherror::__private::ThiserrorProvide as _;
                            #source_provide
                            #self_provide
                        }
                    }
                }
                (Some(backtrace_field), Some(source_field))
                    if backtrace_field.member == source_field.member =>
                {
                    let backtrace = &backtrace_field.member;
                    let varsource = quote!(source);
                    let source_provide = if type_is_option(source_field.ty) {
                        quote_spanned! {backtrace.span()=>
                            if let ::core::option::Option::Some(source) = #varsource {
                                source.thiserror_provide(#request);
                            }
                        }
                    } else {
                        quote_spanned! {backtrace.span()=>
                            #varsource.thiserror_provide(#request);
                        }
                    };
                    quote! {
                        #ty::#ident {#backtrace: #varsource, ..} => {
                            use ::wherror::__private::ThiserrorProvide as _;
                            #source_provide
                        }
                    }
                }
                (Some(backtrace_field), _) => {
                    let backtrace = &backtrace_field.member;
                    let body = if type_is_option(backtrace_field.ty) {
                        quote! {
                            if let ::core::option::Option::Some(backtrace) = backtrace {
                                #request.provide_ref::<::wherror::__private::Backtrace>(backtrace);
                            }
                        }
                    } else {
                        quote! {
                            #request.provide_ref::<::wherror::__private::Backtrace>(backtrace);
                        }
                    };
                    quote! {
                        #ty::#ident {#backtrace: backtrace, ..} => {
                            #body
                        }
                    }
                }
                (None, _) => quote! {
                    #ty::#ident {..} => {}
                },
            }
        });
        Some(quote! {
            fn provide<'_request>(&'_request self, #request: &mut ::core::error::Request<'_request>) {
                #[allow(deprecated)]
                match self {
                    #(#arms)*
                }
            }
        })
    } else {
        None
    };

    let display_impl = if input.has_display() {
        let mut display_inferred_bounds = InferredBounds::new();
        let has_bonus_display = input.variants.iter().any(|v| {
            v.attrs
                .display
                .as_ref()
                .map_or(false, |display| display.has_bonus_display)
        });
        let use_as_display = use_as_display(has_bonus_display);
        let void_deref = if input.variants.is_empty() {
            Some(quote!(*))
        } else {
            None
        };
        let arms = input.variants.iter().map(|variant| {
            let mut display_implied_bounds = Set::new();
            let display = if let Some(display) = &variant.attrs.display {
                display_implied_bounds.clone_from(&display.implied_bounds);
                display.to_token_stream()
            } else if let Some(fmt) = &variant.attrs.fmt {
                let fmt_path = &fmt.path;
                let vars = variant.fields.iter().map(|field| match &field.member {
                    MemberUnraw::Named(ident) => ident.to_local(),
                    MemberUnraw::Unnamed(index) => format_ident!("_{}", index),
                });
                quote!(#fmt_path(#(#vars,)* __formatter))
            } else if let Some(_transparent_attr) = &variant.attrs.transparent {
                // Transparent: forward to the single field's Display implementation
                let only_field = match &variant.fields[0].member {
                    MemberUnraw::Named(ident) => ident.to_local(),
                    MemberUnraw::Unnamed(index) => format_ident!("_{}", index),
                };
                display_implied_bounds.insert((0, Trait::Display));
                quote!(::core::fmt::Display::fmt(#only_field, __formatter))
            } else if let Some(debug_attr) = &variant.attrs.debug {
                // Variant-level #[error(debug)]: debug the variant fields directly
                let ident = &variant.ident;
                let debug_span = debug_attr.span;
                if variant.fields.is_empty() {
                    // Unit variant: just display the variant name
                    let variant_name = ident.to_string();
                    quote_spanned! {debug_span=>
                        ::core::write!(__formatter, "{}", #variant_name)
                    }
                } else if variant.fields.len() == 1 && matches!(variant.fields[0].member, MemberUnraw::Unnamed(_)) {
                    // Tuple variant with single field: Format as Variant(field)
                    let mut field_var = format_ident!("_0");
                    field_var.set_span(debug_span);
                    quote_spanned! {debug_span=>
                        ::core::write!(__formatter, "{}({:?})", stringify!(#ident), #field_var)
                    }
                } else if variant.fields.iter().all(|f| matches!(f.member, MemberUnraw::Unnamed(_))) {
                    // Tuple variant with multiple fields: Format as Variant(field1, field2, ...)
                    let field_vars: Vec<_> = variant.fields.iter().enumerate().map(|(i, _)| {
                        let var = format_ident!("_{}", i);
                        quote_spanned! {debug_span=> #var}
                    }).collect();
                    quote_spanned! {debug_span=>
                        ::core::write!(__formatter, "{}({:?})", stringify!(#ident), (#(#field_vars,)*))
                    }
                } else {
                    // Struct variant: generate proper debug formatting showing all field values
                    let field_writes: Vec<_> = variant.fields.iter().enumerate().map(|(i, field)| {
                        let comma = if i < variant.fields.len() - 1 {
                            quote_spanned! {debug_span=> ::core::write!(__formatter, ", ")?; }
                        } else {
                            quote_spanned! {debug_span=> }
                        };
                        match &field.member {
                            MemberUnraw::Named(ident) => {
                                let field_name = ident.to_string();
                                let var = ident.to_local();
                                quote_spanned! {debug_span=>
                                    ::core::write!(__formatter, "{}: {:?}", #field_name, #var)?;
                                    #comma
                                }
                            }
                            MemberUnraw::Unnamed(index) => {
                                let var = format_ident!("_{}", index);
                                quote_spanned! {debug_span=>
                                    ::core::write!(__formatter, "{:?}", #var)?;
                                    #comma
                                }
                            }
                        }
                    }).collect();

                    quote_spanned! {debug_span=>
                        {
                            ::core::write!(__formatter, "{} {{ ", stringify!(#ident))?;
                            #(#field_writes)*
                            ::core::write!(__formatter, " }}")
                        }
                    }
                }
            } else if let Some(debug_attr) = &input.attrs.debug {
                // Top-level #[error(debug)]: fall back to Debug representation of the variant
                // This implements the feature request from issue #172
                let ident = &variant.ident;
                if variant.fields.is_empty() {
                    // Unit variant: just display the variant name
                    let variant_name = ident.to_string();
                    quote_spanned! {debug_attr.span=>
                        ::core::write!(__formatter, "{}", #variant_name)
                    }
                } else if variant.fields.len() == 1 && matches!(variant.fields[0].member, MemberUnraw::Unnamed(_)) {
                    // Tuple variant with single field: Format as Variant(field)
                    let mut field_var = format_ident!("_0");
                    field_var.set_span(debug_attr.span);
                    quote_spanned! {debug_attr.span=>
                        ::core::write!(__formatter, "{}({:?})", stringify!(#ident), #field_var)
                    }
                } else if variant.fields.iter().all(|f| matches!(f.member, MemberUnraw::Unnamed(_))) {
                    // Tuple variant with multiple fields: Format as Variant(field1, field2, ...)
                    let field_vars: Vec<_> = variant.fields.iter().enumerate().map(|(i, _)| {
                        let mut var = format_ident!("_{}", i);
                        var.set_span(debug_attr.span);
                        var
                    }).collect();
                    quote_spanned! {debug_attr.span=>
                        ::core::write!(__formatter, "{}({:?})", stringify!(#ident), (#(#field_vars,)*))
                    }
                } else {
                    // Struct variant: generate proper debug formatting showing all field values
                    let field_writes: Vec<_> = variant.fields.iter().enumerate().map(|(i, field)| {
                        let comma = if i < variant.fields.len() - 1 {
                            quote_spanned! {debug_attr.span=> ::core::write!(__formatter, ", ")?; }
                        } else {
                            quote_spanned! {debug_attr.span=> }
                        };
                        match &field.member {
                            MemberUnraw::Named(ident) => {
                                let field_name = ident.to_string();
                                let var = ident.to_local();
                                quote_spanned! {debug_attr.span=>
                                    ::core::write!(__formatter, "{}: {:?}", #field_name, #var)?;
                                    #comma
                                }
                            }
                            MemberUnraw::Unnamed(index) => {
                                let var = format_ident!("_{}", index);
                                quote_spanned! {debug_attr.span=>
                                    ::core::write!(__formatter, "{:?}", #var)?;
                                    #comma
                                }
                            }
                        }
                    }).collect();

                    quote_spanned! {debug_attr.span=>
                        {
                            ::core::write!(__formatter, "{} {{ ", stringify!(#ident))?;
                            #(#field_writes)*
                            ::core::write!(__formatter, " }}")
                        }
                    }
                }
            } else {
                // This should be caught by validation - no display attribute and no fallback
                quote!(unreachable!("missing display attribute should have been caught by validation"))
            };
            for (field, bound) in display_implied_bounds {
                let field = &variant.fields[field];
                if field.contains_generic {
                    display_inferred_bounds.insert(field.ty, bound);
                }
            }
            let ident = &variant.ident;
            let pat = fields_pat(&variant.fields);
            quote! {
                #ty::#ident #pat => #display
            }
        });
        let arms = arms.collect::<Vec<_>>();
        let display_where_clause = display_inferred_bounds.augment_where_clause(input.generics);
        Some(quote! {
            #[allow(unused_qualifications)]
            #[automatically_derived]
            impl #impl_generics ::core::fmt::Display for #ty #ty_generics #display_where_clause {
                fn fmt(&self, __formatter: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
                    #use_as_display
                    #[allow(unused_variables, deprecated, clippy::used_underscore_binding)]
                    match #void_deref self {
                        #(#arms,)*
                    }
                }
            }
        })
    } else {
        None
    };

    let from_impls = input.variants.iter().flat_map(|variant| {
        let from_field = variant.from_field()?;
        let span = from_field.attrs.from.unwrap().span;
        let backtrace_field = variant.distinct_backtrace_field();
        let location_field = variant.location_field();
        let variant_ident = &variant.ident;
        let from = unoptional_type(from_field.ty);
        let source_var = Ident::new("source", span);
        let body = from_initializer(from_field, backtrace_field, &source_var, location_field);
        let track_caller = location_field.map(|_| quote!(#[track_caller]));

        let mut implementations = Vec::new();

        // Main From implementation (always generated)
        let from_function = quote! {
            #track_caller
            fn from(#source_var: #from) -> Self {
                #ty::#variant_ident #body
            }
        };
        let from_impl = quote_spanned! {span=>
            #[automatically_derived]
            impl #impl_generics ::core::convert::From<#from> for #ty #ty_generics #where_clause {
                #from_function
            }
        };
        implementations.push(quote! {
            #[allow(
                deprecated,
                unused_qualifications,
                clippy::elidable_lifetime_names,
                clippy::needless_lifetimes,
            )]
            #from_impl
        });

        // Check if the field type (after unwrapping Option) is Box<T>
        let field_type = type_parameter_of_option(from_field.ty).unwrap_or(from_field.ty);
        if let Some(inner_type) = type_parameter_of_box(field_type) {
            // Generate additional From<T> implementation that boxes the value
            let inner_source_var = Ident::new("source", span);
            let boxed_body = from_initializer_for_box(
                from_field,
                backtrace_field,
                &inner_source_var,
                location_field,
            );
            let inner_from_function = quote! {
                #track_caller
                fn from(#inner_source_var: #inner_type) -> Self {
                    #ty::#variant_ident #boxed_body
                }
            };
            let inner_from_impl = quote_spanned! {span=>
                #[automatically_derived]
                impl #impl_generics ::core::convert::From<#inner_type> for #ty #ty_generics #where_clause {
                    #inner_from_function
                }
            };
            implementations.push(quote! {
                #[allow(
                    deprecated,
                    unused_qualifications,
                    clippy::elidable_lifetime_names,
                    clippy::needless_lifetimes,
                )]
                #inner_from_impl
            });
        }

        Some(implementations)
    }).flatten();

    let location_impl = if input.has_location() {
        let arms = input.variants.iter().map(|variant| {
            let ident = &variant.ident;
            if let Some(location_field) = variant.location_field() {
                let location = &location_field.member;
                let var_location = quote!(location);
                let body = if type_is_option(location_field.ty) {
                    quote! {
                        #var_location
                    }
                } else {
                    quote! {
                        Some(#var_location)
                    }
                };
                quote! {
                    #ty::#ident {#location: #var_location, ..} => #body,
                }
            } else {
                quote! {
                    #ty::#ident {..} => None,
                }
            }
        });
        Some(quote! {
            #[allow(unused_qualifications)]
            #[automatically_derived]
            impl #impl_generics #ty #ty_generics #where_clause {
                pub fn location(&self) -> Option<&'static ::core::panic::Location<'static>> {
                    #[allow(deprecated)]
                    match self {
                        #(#arms)*
                    }
                }
            }
        })
    } else {
        None
    };

    if input.generics.type_params().next().is_some() {
        let self_token = <Token![Self]>::default();
        error_inferred_bounds.insert(self_token, Trait::Debug);
        error_inferred_bounds.insert(self_token, Trait::Display);
    }
    let error_where_clause = error_inferred_bounds.augment_where_clause(input.generics);

    quote! {
        #[allow(unused_qualifications)]
        #[automatically_derived]
        impl #impl_generics ::wherror::__private::Error for #ty #ty_generics #error_where_clause {
            #source_method
            #provide_method
        }
        #display_impl
        #(#from_impls)*
        #location_impl
    }
}

// Create an ident with which we can expand `impl Trait for #ident {}` on a
// deprecated type without triggering deprecation warning on the generated impl.
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
}

fn fields_pat(fields: &[Field]) -> TokenStream {
    let mut members = fields.iter().map(|field| &field.member).peekable();
    match members.peek() {
        Some(MemberUnraw::Named(_)) => quote!({ #(#members),* }),
        Some(MemberUnraw::Unnamed(_)) => {
            let vars = members.map(|member| match member {
                MemberUnraw::Unnamed(index) => format_ident!("_{}", index),
                MemberUnraw::Named(_) => unreachable!(),
            });
            quote!((#(#vars),*))
        }
        None => quote!({}),
    }
}

fn use_as_display(needs_as_display: bool) -> Option<TokenStream> {
    if needs_as_display {
        Some(quote! {
            use ::wherror::__private::AsDisplay as _;
        })
    } else {
        None
    }
}

fn from_initializer(
    from_field: &Field,
    backtrace_field: Option<&Field>,
    source_var: &Ident,
    location_field: Option<&Field>,
) -> TokenStream {
    let from_member = &from_field.member;
    let some_source = if type_is_option(from_field.ty) {
        quote!(::core::option::Option::Some(#source_var))
    } else {
        quote!(#source_var)
    };
    let backtrace = backtrace_field.map(|backtrace_field| {
        let backtrace_member = &backtrace_field.member;
        if type_is_option(backtrace_field.ty) {
            quote! {
                #backtrace_member: ::core::option::Option::Some(::wherror::__private::Backtrace::capture()),
            }
        } else {
            quote! {
                #backtrace_member: ::core::convert::From::from(::wherror::__private::Backtrace::capture()),
            }
        }
    });
    let location = location_field.map(|location_field| {
        let location_member = &location_field.member;

        if type_is_option(location_field.ty) {
            quote! {
                #location_member: ::core::option::Option::Some(::core::panic::Location::caller()),
            }
        } else {
            quote! {
                #location_member: ::core::convert::From::from(::core::panic::Location::caller()),
            }
        }
    });
    quote!({
        #from_member: #some_source,
        #backtrace
        #location
    })
}

fn from_initializer_for_box(
    from_field: &Field,
    backtrace_field: Option<&Field>,
    source_var: &Ident,
    location_field: Option<&Field>,
) -> TokenStream {
    let from_member = &from_field.member;
    // For Box<T> fields, we need to Box::new the source when receiving T
    let some_source = if type_is_option(from_field.ty) {
        quote!(::core::option::Option::Some(::std::boxed::Box::new(#source_var)))
    } else {
        quote!(::std::boxed::Box::new(#source_var))
    };
    let backtrace = backtrace_field.map(|backtrace_field| {
        let backtrace_member = &backtrace_field.member;
        if type_is_option(backtrace_field.ty) {
            quote! {
                #backtrace_member: ::core::option::Option::Some(::wherror::__private::Backtrace::capture()),
            }
        } else {
            quote! {
                #backtrace_member: ::core::convert::From::from(::wherror::__private::Backtrace::capture()),
            }
        }
    });
    let location = location_field.map(|location_field| {
        let location_member = &location_field.member;

        if type_is_option(location_field.ty) {
            quote! {
                #location_member: ::core::option::Option::Some(::core::panic::Location::caller()),
            }
        } else {
            quote! {
                #location_member: ::core::convert::From::from(::core::panic::Location::caller()),
            }
        }
    });
    quote!({
        #from_member: #some_source,
        #backtrace
        #location
    })
}

fn type_is_option(ty: &Type) -> bool {
    type_parameter_of_option(ty).is_some()
}

fn unoptional_type(ty: &Type) -> TokenStream {
    let unoptional = type_parameter_of_option(ty).unwrap_or(ty);
    quote!(#unoptional)
}

fn type_parameter_of_option(ty: &Type) -> Option<&Type> {
    let path = match ty {
        Type::Path(ty) => &ty.path,
        _ => return None,
    };

    let last = path.segments.last().unwrap();
    if last.ident != "Option" {
        return None;
    }

    let bracketed = match &last.arguments {
        PathArguments::AngleBracketed(bracketed) => bracketed,
        _ => return None,
    };

    if bracketed.args.len() != 1 {
        return None;
    }

    match &bracketed.args[0] {
        GenericArgument::Type(arg) => Some(arg),
        _ => None,
    }
}

fn type_parameter_of_box(ty: &Type) -> Option<&Type> {
    let path = match ty {
        Type::Path(ty) => &ty.path,
        _ => return None,
    };

    let last = path.segments.last().unwrap();
    if last.ident != "Box" {
        return None;
    }

    let bracketed = match &last.arguments {
        PathArguments::AngleBracketed(bracketed) => bracketed,
        _ => return None,
    };

    if bracketed.args.len() != 1 {
        return None;
    }

    match &bracketed.args[0] {
        GenericArgument::Type(arg) => Some(arg),
        _ => None,
    }
}