whichever-macros 0.2.1

Procedural macros of the whichever crate
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
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
//! Procedural macros of `whichever`.
//!
//! `#[dispatchable]` on a trait re-emits the trait and exports a hidden
//! `macro_rules!` that carries the trait's tokens; the macro is re-exported
//! under the trait's own name, so importing the trait imports the macro.
//! `#[dispatch(Trait)]` on an enum invokes that macro, which forwards the
//! trait tokens, the enum tokens and the options into the hidden
//! `implement!` macro that writes `impl Trait for Enum` with one `match` per
//! method. No state is shared between expansions, so the trait and the enum
//! may live in different crates.
use std::collections::hash_map::DefaultHasher;
use std::hash::{Hash, Hasher};

use proc_macro::TokenStream;
use proc_macro2::{Group, Punct, Spacing, Span, TokenStream as TokenStream2, TokenTree};
use proc_macro_crate::FoundCrate;
use quote::{format_ident, quote, ToTokens};
use syn::parse::{Parse, ParseStream};
use syn::punctuated::Punctuated;
use syn::visit::Visit;
use syn::visit_mut::{self, VisitMut};
use syn::{
    braced, parenthesized, parse_macro_input, Attribute, Block, Error, Expr, Fields, FnArg,
    GenericArgument, GenericParam, Ident, ImplItem, Item, ItemEnum, ItemImpl, ItemTrait, Lifetime,
    LitBool, Meta, Pat, PatIdent, Path, PathArguments, PathSegment, QSelf, Receiver, Result,
    ReturnType, Signature, Token, TraitItem, TraitItemFn, Type, TypeParamBound, TypePath, Variant,
    Visibility,
};

/// The name the facade crate is published under; a renamed dependency is
/// resolved through `proc-macro-crate`.
const FACADE: &str = "whichever";

/// Returns whether `attribute` is this crate's `dispatch` attribute, written
/// bare or as `whichever::dispatch`.
fn is_dispatch_attribute(attribute: &Attribute) -> bool {
    attribute
        .path()
        .segments
        .last()
        .is_some_and(|segment| segment.ident == "dispatch")
}

// ---------------------------------------------------------------------------
// Trait side
// ---------------------------------------------------------------------------

/// Makes a trait available to `#[dispatch]`, from this crate or any other.
///
/// The trait is emitted unchanged, except that `#[dispatch(..)]` attributes
/// on its methods are consumed. The attribute takes no arguments. The only
/// method option is `mismatch`, which gives the result of a method taking
/// `Self` arguments when the arguments hold a different variant than `self`:
///
/// ```rust,ignore
/// #[whichever::dispatchable]
/// pub trait Shape {
///     fn area(&self) -> u32;
///     #[dispatch(mismatch = false)]
///     fn congruent(&self, other: &Self) -> bool;
/// }
/// ```
///
/// The trait's signature types are later written into the dispatching
/// enum's module, so they must be nameable there; `crate::` paths are
/// rewritten to this crate, `super::` and `self::` paths are not and must
/// not be used.
#[proc_macro_attribute]
pub fn dispatchable(attr: TokenStream, item: TokenStream) -> TokenStream {
    if !attr.is_empty() {
        return Error::new(Span::call_site(), "`#[dispatchable]` takes no arguments")
            .to_compile_error()
            .into();
    }
    let item_trait = parse_macro_input!(item as ItemTrait);
    let site = proc_macro::Span::call_site();
    expand_dispatchable(item_trait, site.line(), site.column())
        .unwrap_or_else(|error| error.to_compile_error())
        .into()
}

fn expand_dispatchable(item_trait: ItemTrait, line: usize, column: usize) -> Result<TokenStream2> {
    let captured = item_trait.clone();
    let mut emitted = item_trait;
    for item in &mut emitted.items {
        if let TraitItem::Fn(function) = item {
            function
                .attrs
                .retain(|attribute| !is_dispatch_attribute(attribute));
        }
    }
    let trait_ident = emitted.ident.clone();
    let vis = emitted.vis.clone();
    // The exported macro lives at the crate root, so its name carries the
    // trait's tokens and position to keep same-named traits apart.
    let mut hasher = DefaultHasher::new();
    captured.to_token_stream().to_string().hash(&mut hasher);
    let macro_name = format_ident!(
        "__whichever_{}_{:016x}_{}_{}",
        trait_ident,
        hasher.finish(),
        line,
        column
    );
    // The captured tokens expand inside the enum's crate, where `crate::`
    // would name the wrong crate; `$crate` resolves to this crate instead.
    let captured = rewrite_crate_paths(captured.to_token_stream());
    Ok(quote! {
        #emitted

        #[doc(hidden)]
        #[macro_export]
        macro_rules! #macro_name {
            (@v1 $facade:ident $($rest:tt)*) => {
                ::$facade::__private::implement! { @v1 trait { #captured } $($rest)* }
            };
        }

        #[doc(hidden)]
        #[allow(unused_imports)]
        #vis use #macro_name as #trait_ident;
    })
}

/// Replaces every `crate::` path prefix in `tokens` with `$crate::`.
fn rewrite_crate_paths(tokens: TokenStream2) -> TokenStream2 {
    let mut output = TokenStream2::new();
    let mut iter = tokens.into_iter().peekable();
    while let Some(token) = iter.next() {
        match token {
            TokenTree::Group(group) => {
                let mut rewritten =
                    Group::new(group.delimiter(), rewrite_crate_paths(group.stream()));
                rewritten.set_span(group.span());
                output.extend([TokenTree::Group(rewritten)]);
            }
            TokenTree::Ident(ident)
                if ident == "crate"
                    && matches!(iter.peek(), Some(TokenTree::Punct(punct)) if punct.as_char() == ':') =>
            {
                output.extend([
                    TokenTree::Punct(Punct::new('$', Spacing::Alone)),
                    TokenTree::Ident(ident),
                ]);
            }
            other => output.extend([other]),
        }
    }
    output
}

// ---------------------------------------------------------------------------
// Enum side
// ---------------------------------------------------------------------------

/// One `#[dispatch(..)]` attribute on an enum: a trait path with its
/// arguments, options, or both. The path is absent when the attribute only
/// carries enum-level options.
struct TraitSpec {
    path: Option<Path>,
    assoc_overrides: Vec<(Ident, Type)>,
    from: Option<bool>,
    try_from: Option<bool>,
    mismatch: Vec<(Ident, Expr)>,
}

impl TraitSpec {
    fn assoc_override(&self, name: &Ident) -> Option<&Type> {
        self.assoc_overrides
            .iter()
            .find(|(key, _)| key == name)
            .map(|(_, ty)| ty)
    }

    fn mismatch_for(&self, method: &Ident) -> Option<&Expr> {
        self.mismatch
            .iter()
            .find(|(key, _)| key == method)
            .map(|(_, expr)| expr)
    }
}

struct KeyedExpr {
    key: Ident,
    value: Expr,
}

impl Parse for KeyedExpr {
    fn parse(input: ParseStream) -> Result<Self> {
        let key = input.parse()?;
        input.parse::<Token![=]>()?;
        let value = input.parse()?;
        Ok(Self { key, value })
    }
}

impl Parse for TraitSpec {
    fn parse(input: ParseStream) -> Result<Self> {
        let starts_with_option =
            input.peek(Ident) && (input.peek2(Token![=]) || input.peek2(syn::token::Paren));
        let path = if input.is_empty() || starts_with_option {
            None
        } else {
            Some(input.parse::<Path>()?)
        };
        let mut spec = TraitSpec {
            path,
            assoc_overrides: vec![],
            from: None,
            try_from: None,
            mismatch: vec![],
        };
        let mut expect_comma = spec.path.is_some();
        while !input.is_empty() {
            if expect_comma {
                input.parse::<Token![,]>()?;
                if input.is_empty() {
                    break;
                }
            }
            expect_comma = true;
            let key: Ident = input.parse()?;
            if key == "mismatch" {
                let content;
                parenthesized!(content in input);
                let pairs = Punctuated::<KeyedExpr, Token![,]>::parse_terminated(&content)?;
                spec.mismatch
                    .extend(pairs.into_iter().map(|pair| (pair.key, pair.value)));
                continue;
            }
            if !input.peek(Token![=]) {
                return Err(Error::new(
                    key.span(),
                    format!("unknown option `{key}`; the options are `Name = Type` for an associated type, `mismatch(method = expr)`, `from = false` and `try_from = true`"),
                ));
            }
            input.parse::<Token![=]>()?;
            match key.to_string().as_str() {
                "from" => spec.from = Some(input.parse::<LitBool>()?.value),
                "try_from" => spec.try_from = Some(input.parse::<LitBool>()?.value),
                _ => spec.assoc_overrides.push((key, input.parse()?)),
            }
        }
        Ok(spec)
    }
}

/// Implements a `#[dispatchable]` trait for an enum by forwarding every
/// method to whichever variant the value holds, or forwards the methods of
/// an inherent `impl` block of such an enum.
///
/// On an enum, each attribute names one trait, with its generic arguments
/// when it has any, followed by options; several attributes dispatch several
/// traits, and a bare `#[dispatch]` only registers the enum. Every variant
/// must hold exactly one unnamed field whose type implements the trait. The
/// enum also gets a `From<Payload>` impl for every payload type that appears
/// in exactly one variant.
///
/// ```rust,ignore
/// #[whichever::dispatch(Shape)]
/// #[whichever::dispatch(Encoder<Json>, Error = EncodeError)]
/// pub enum AnyShape {
///     Circle(Circle),
///     #[dispatch(deref)]
///     Square(Box<Square>),
/// }
/// ```
///
/// Options, after the trait path:
/// - `Name = Type`: the associated type `Name` of the enum's impl, instead
///   of the first variant's. Methods returning `Self::Name` are converted
///   with `Into`, methods returning `Result<_, Self::Name>` with
///   `map_err(Into::into)`.
/// - `mismatch(method = expr, ..)`: the result of `method` when its `Self`
///   arguments hold a different variant than `self`.
/// - `from = false`: no `From<Payload>` impls.
/// - `try_from = true`: a `TryFrom<Enum> for Payload` impl per payload type,
///   returning the enum unchanged when it holds another variant.
///
/// The one variant option, `#[dispatch(deref)]`, calls the trait on what the
/// payload dereferences to, for `Box<T>`, `Rc<T>`, `Arc<T>`, `&T` and
/// `&mut T` payloads; such a variant cannot take part in a method that
/// returns `Self`.
///
/// On an inherent `impl` block of a dispatch enum, in the same crate, every
/// method carrying `#[dispatch(..)]` is forwarded to the named variants'
/// own method of the same name, and the body is what the other variants
/// return. `*` names every variant; the method then has no fallback and is
/// written without a body:
///
/// ```rust,ignore
/// #[whichever::dispatch]
/// impl AnyShape {
///     /// Only circles have a radius.
///     #[dispatch(Circle, with = Some)]
///     pub fn radius(&self) -> Option<f64> {
///         None
///     }
///
///     #[dispatch(*)]
///     pub fn perimeter(&self) -> f64;
/// }
/// ```
///
/// Method options: `with = path` wraps each forwarded result in a call to
/// `path`, and `call = name` forwards to a payload method of another name.
/// Methods without the attribute are left as they are.
#[proc_macro_attribute]
pub fn dispatch(attr: TokenStream, item: TokenStream) -> TokenStream {
    let site = proc_macro::Span::call_site();
    let (line, column) = (site.line(), site.column());
    let item = parse_macro_input!(item as Item);
    let expanded = match item {
        Item::Enum(item_enum) => expand_dispatch(attr.into(), item_enum, line, column),
        Item::Impl(item_impl) => expand_inherent_invocation(attr.into(), item_impl),
        other => Err(Error::new_spanned(
            other,
            "`#[dispatch]` goes on an enum, or on an inherent impl block of a dispatch enum",
        )),
    };
    expanded
        .unwrap_or_else(|error| error.to_compile_error())
        .into()
}

fn expand_dispatch(
    first: TokenStream2,
    mut item_enum: ItemEnum,
    line: usize,
    column: usize,
) -> Result<TokenStream2> {
    let mut specs = Vec::new();
    if !first.is_empty() {
        specs.push(first);
    }
    let mut kept = vec![];
    for attribute in std::mem::take(&mut item_enum.attrs) {
        if !is_dispatch_attribute(&attribute) {
            kept.push(attribute);
            continue;
        }
        match &attribute.meta {
            Meta::List(list) => specs.push(list.tokens.clone()),
            // A bare `#[dispatch]` registers the enum without a trait.
            Meta::Path(_) => {}
            Meta::NameValue(_) => {
                return Err(Error::new_spanned(
                    attribute,
                    "expected `#[dispatch(Trait, ...)]`",
                ));
            }
        }
    }
    item_enum.attrs = kept;
    let variants = item_enum
        .variants
        .iter()
        .map(variant_info)
        .collect::<Result<Vec<_>>>()?;
    let captured = item_enum.clone();
    let mut emitted = item_enum;
    for variant in &mut emitted.variants {
        variant
            .attrs
            .retain(|attribute| !is_dispatch_attribute(attribute));
    }
    let facade = facade_ident()?;
    let parsed_specs = specs
        .iter()
        .map(|tokens| syn::parse2::<TraitSpec>(tokens.clone()))
        .collect::<Result<Vec<_>>>()?;
    let generate_from = !parsed_specs.iter().any(|spec| spec.from == Some(false));
    let generate_try_from = parsed_specs.iter().any(|spec| spec.try_from == Some(true));
    let conversions = conversion_impls(&emitted, &variants, generate_from, generate_try_from);
    let mut invocations = TokenStream2::new();
    for (spec, tokens) in parsed_specs.iter().zip(&specs) {
        let Some(path) = &spec.path else { continue };
        let mut macro_path = path.clone();
        if let Some(last) = macro_path.segments.last_mut() {
            last.arguments = PathArguments::None;
        }
        invocations.extend(quote! {
            #macro_path! { @v1 #facade enum { #captured } spec { #tokens } }
        });
    }
    // The enum's own macro carries its definition to `#[dispatch]` on an
    // inherent impl block. Inherent impls live in the enum's crate, so the
    // macro stays crate-local and is reached through the alias.
    let enum_ident = &emitted.ident;
    let mut hasher = DefaultHasher::new();
    emitted.to_token_stream().to_string().hash(&mut hasher);
    let enum_macro = format_ident!(
        "__whichever_enum_{}_{:016x}_{}_{}",
        enum_ident,
        hasher.finish(),
        line,
        column
    );
    Ok(quote! {
        #emitted
        #conversions

        #[allow(unused_macros)]
        macro_rules! #enum_macro {
            (@v1 $facade:ident $($rest:tt)*) => {
                ::$facade::__private::implement_inherent! { @v1 enum { #emitted } $($rest)* }
            };
        }

        #[doc(hidden)]
        #[allow(unused_imports)]
        pub(crate) use #enum_macro as #enum_ident;

        #invocations
    })
}

/// Turns `#[dispatch]` on an inherent impl block into a call of the enum's
/// macro, which supplies the enum's definition.
fn expand_inherent_invocation(attr: TokenStream2, item_impl: ItemImpl) -> Result<TokenStream2> {
    if !attr.is_empty() {
        return Err(Error::new_spanned(
            attr,
            "`#[dispatch]` on an impl block takes no arguments; the variants are named on each method",
        ));
    }
    if let Some((_, trait_path, _)) = &item_impl.trait_ {
        return Err(Error::new_spanned(
            trait_path,
            "`#[dispatch]` forwards inherent methods; a trait is dispatched with `#[dispatch(Trait)]` on the enum",
        ));
    }
    let Type::Path(self_type) = &*item_impl.self_ty else {
        return Err(Error::new_spanned(
            &item_impl.self_ty,
            "the impl must name a dispatch enum by path",
        ));
    };
    let mut macro_path = self_type.path.clone();
    if let Some(last) = macro_path.segments.last_mut() {
        last.arguments = PathArguments::None;
    }
    let facade = facade_ident()?;
    Ok(quote! {
        #macro_path! { @v1 #facade impl { #item_impl } }
    })
}

fn facade_ident() -> Result<Ident> {
    match proc_macro_crate::crate_name(FACADE) {
        Ok(FoundCrate::Itself) => Ok(format_ident!("{FACADE}")),
        Ok(FoundCrate::Name(name)) => Ok(Ident::new(&name, Span::call_site())),
        Err(error) => Err(Error::new(
            Span::call_site(),
            format!("`{FACADE}` must be a dependency of this crate: {error}"),
        )),
    }
}

struct VariantInfo {
    ident: Ident,
    payload: Type,
    cfgs: Vec<Attribute>,
    /// Whether the trait is called on what the payload dereferences to.
    deref: bool,
}

fn variant_info(variant: &Variant) -> Result<VariantInfo> {
    let Fields::Unnamed(fields) = &variant.fields else {
        return Err(Error::new_spanned(
            variant,
            "a dispatch variant must be `Name(Payload)`",
        ));
    };
    if fields.unnamed.len() != 1 {
        return Err(Error::new_spanned(
            variant,
            "a dispatch variant must hold exactly one payload",
        ));
    }
    let mut deref = false;
    for attribute in variant
        .attrs
        .iter()
        .filter(|attribute| is_dispatch_attribute(attribute))
    {
        let option: Ident = attribute.parse_args()?;
        if option != "deref" {
            return Err(Error::new_spanned(
                &option,
                format!("unknown variant option `{option}`; the only one is `deref`"),
            ));
        }
        deref = true;
    }
    Ok(VariantInfo {
        ident: variant.ident.clone(),
        payload: fields.unnamed[0].ty.clone(),
        cfgs: cfg_attributes(&variant.attrs),
        deref,
    })
}

/// Returns the type the trait is called on for `variant`: its payload, or
/// what the payload dereferences to.
fn target_type(variant: &VariantInfo) -> TokenStream2 {
    let payload = &variant.payload;
    if variant.deref {
        quote!(<#payload as ::core::ops::Deref>::Target)
    } else {
        quote!(#payload)
    }
}

fn cfg_attributes(attributes: &[Attribute]) -> Vec<Attribute> {
    attributes
        .iter()
        .filter(|attribute| attribute.path().is_ident("cfg"))
        .cloned()
        .collect()
}

/// Writes the `From<Payload>` and `TryFrom<Enum>` impls that were asked for.
fn conversion_impls(
    item_enum: &ItemEnum,
    variants: &[VariantInfo],
    from: bool,
    try_from: bool,
) -> TokenStream2 {
    let (impl_generics, ty_generics, where_clause) = item_enum.generics.split_for_impl();
    let enum_ident = &item_enum.ident;
    // A payload type held by two variants gets no conversion: the impls
    // would conflict, and neither variant is the obvious target.
    let mut payload_counts = std::collections::HashMap::<String, usize>::new();
    for variant in variants {
        *payload_counts
            .entry(variant.payload.to_token_stream().to_string())
            .or_default() += 1;
    }
    let value = Ident::new("__value", Span::mixed_site());
    let other = Ident::new("__other", Span::mixed_site());
    let mut output = TokenStream2::new();
    for variant in variants {
        let VariantInfo {
            ident,
            payload,
            cfgs,
            ..
        } = variant;
        if payload_counts[&payload.to_token_stream().to_string()] > 1 {
            continue;
        }
        if from {
            output.extend(quote! {
                #(#cfgs)*
                #[automatically_derived]
                impl #impl_generics ::core::convert::From<#payload> for #enum_ident #ty_generics #where_clause {
                    #[inline]
                    fn from(#value: #payload) -> Self {
                        Self::#ident(#value)
                    }
                }
            });
        }
        if try_from {
            output.extend(quote! {
                #(#cfgs)*
                #[automatically_derived]
                impl #impl_generics ::core::convert::TryFrom<#enum_ident #ty_generics> for #payload #where_clause {
                    type Error = #enum_ident #ty_generics;
                    #[inline]
                    #[allow(unreachable_patterns)]
                    fn try_from(#value: #enum_ident #ty_generics) -> ::core::result::Result<Self, Self::Error> {
                        match #value {
                            #enum_ident::#ident(#value) => ::core::result::Result::Ok(#value),
                            #other => ::core::result::Result::Err(#other),
                        }
                    }
                }
            });
        }
    }
    output
}

// ---------------------------------------------------------------------------
// The hidden implementation macro
// ---------------------------------------------------------------------------

struct ImplementInput {
    item_trait: ItemTrait,
    item_enum: ItemEnum,
    spec: TraitSpec,
}

impl Parse for ImplementInput {
    fn parse(input: ParseStream) -> Result<Self> {
        input.parse::<Token![@]>()?;
        let version: Ident = input.parse()?;
        if version != "v1" {
            return Err(Error::new_spanned(
                version,
                "unsupported `whichever` protocol version",
            ));
        }
        input.parse::<Token![trait]>()?;
        let trait_tokens;
        braced!(trait_tokens in input);
        let item_trait: ItemTrait = trait_tokens.parse()?;
        input.parse::<Token![enum]>()?;
        let enum_tokens;
        braced!(enum_tokens in input);
        let item_enum: ItemEnum = enum_tokens.parse()?;
        let tag: Ident = input.parse()?;
        if tag != "spec" {
            return Err(Error::new_spanned(tag, "expected `spec`"));
        }
        let spec_tokens;
        braced!(spec_tokens in input);
        let spec: TraitSpec = spec_tokens.parse()?;
        Ok(Self {
            item_trait,
            item_enum,
            spec,
        })
    }
}

/// Writes the trait impl; called by the macro that `#[dispatchable]` exports.
#[doc(hidden)]
#[proc_macro]
pub fn implement(input: TokenStream) -> TokenStream {
    let input = parse_macro_input!(input as ImplementInput);
    expand_implement(input)
        .unwrap_or_else(|error| error.to_compile_error())
        .into()
}

/// Replaces the trait's generic parameters with the arguments the enum names.
struct Substitution {
    types: Vec<(Ident, Type, Option<Path>)>,
    lifetimes: Vec<(Lifetime, Lifetime)>,
    consts: Vec<(Ident, Expr)>,
}

impl Substitution {
    fn new(item_trait: &ItemTrait, trait_path: &Path) -> Result<Self> {
        let params: Vec<&GenericParam> = item_trait.generics.params.iter().collect();
        let last = trait_path
            .segments
            .last()
            .expect("a path has at least one segment");
        let args: Vec<&GenericArgument> = match &last.arguments {
            PathArguments::AngleBracketed(arguments) => arguments
                .args
                .iter()
                .filter(|argument| {
                    !matches!(
                        argument,
                        GenericArgument::AssocType(_)
                            | GenericArgument::AssocConst(_)
                            | GenericArgument::Constraint(_)
                    )
                })
                .collect(),
            _ => vec![],
        };
        if args.len() != params.len() {
            return Err(Error::new_spanned(
                trait_path,
                format!(
                    "`{}` takes {} generic argument(s); name them, e.g. `Trait<Arg>`",
                    item_trait.ident,
                    params.len()
                ),
            ));
        }
        let mut substitution = Self {
            types: vec![],
            lifetimes: vec![],
            consts: vec![],
        };
        for (param, argument) in params.into_iter().zip(args) {
            match (param, argument) {
                (GenericParam::Type(param), GenericArgument::Type(ty)) => {
                    let single_bound = match param.bounds.iter().collect::<Vec<_>>().as_slice() {
                        [TypeParamBound::Trait(bound)] => Some(bound.path.clone()),
                        _ => None,
                    };
                    substitution
                        .types
                        .push((param.ident.clone(), ty.clone(), single_bound));
                }
                (GenericParam::Lifetime(param), GenericArgument::Lifetime(lifetime)) => {
                    substitution
                        .lifetimes
                        .push((param.lifetime.clone(), lifetime.clone()));
                }
                (GenericParam::Const(param), GenericArgument::Const(expr)) => {
                    substitution
                        .consts
                        .push((param.ident.clone(), expr.clone()));
                }
                // A bare identifier given for a const parameter parses as a type.
                (GenericParam::Const(param), GenericArgument::Type(Type::Path(path)))
                    if path.qself.is_none() =>
                {
                    let expr = Expr::Path(syn::ExprPath {
                        attrs: vec![],
                        qself: None,
                        path: path.path.clone(),
                    });
                    substitution.consts.push((param.ident.clone(), expr));
                }
                _ => {
                    return Err(Error::new_spanned(
                        argument,
                        "generic argument does not match the trait's parameter",
                    ))
                }
            }
        }
        Ok(substitution)
    }

    fn const_for(&self, ident: &Ident) -> Option<&Expr> {
        self.consts
            .iter()
            .find(|(name, _)| name == ident)
            .map(|(_, expr)| expr)
    }
}

/// Returns the identifier of a bare single-segment path type.
fn bare_ident(ty: &Type) -> Option<&Ident> {
    match ty {
        Type::Path(TypePath { qself: None, path }) if path.segments.len() == 1 => {
            Some(&path.segments[0].ident)
        }
        _ => None,
    }
}

impl VisitMut for Substitution {
    fn visit_type_mut(&mut self, ty: &mut Type) {
        if let Type::Path(type_path) = ty {
            if type_path.qself.is_none() {
                let first = &type_path.path.segments[0];
                let replacement = self
                    .types
                    .iter()
                    .find(|(ident, _, _)| *ident == first.ident);
                if let Some((_, replacement, bound)) = replacement {
                    if type_path.path.segments.len() == 1 {
                        *ty = replacement.clone();
                        return;
                    }
                    // `W::Assoc` becomes `<Arg as Bound>::Assoc` when the
                    // parameter has exactly one bound to qualify it with.
                    if let Some(bound) = bound {
                        let rest: Vec<PathSegment> =
                            type_path.path.segments.iter().skip(1).cloned().collect();
                        let mut path = bound.clone();
                        let position = path.segments.len();
                        path.segments.extend(rest);
                        *ty = Type::Path(TypePath {
                            qself: Some(QSelf {
                                lt_token: Default::default(),
                                ty: Box::new(replacement.clone()),
                                position,
                                as_token: Some(Default::default()),
                                gt_token: Default::default(),
                            }),
                            path,
                        });
                        return;
                    }
                }
            }
        }
        visit_mut::visit_type_mut(self, ty);
    }

    fn visit_lifetime_mut(&mut self, lifetime: &mut Lifetime) {
        if let Some((_, replacement)) = self.lifetimes.iter().find(|(from, _)| from == lifetime) {
            *lifetime = replacement.clone();
        }
    }

    fn visit_expr_mut(&mut self, expr: &mut Expr) {
        if let Expr::Path(path) = expr {
            if path.qself.is_none() && path.path.segments.len() == 1 {
                if let Some(replacement) = self.const_for(&path.path.segments[0].ident) {
                    *expr = replacement.clone();
                    return;
                }
            }
        }
        visit_mut::visit_expr_mut(self, expr);
    }

    /// A const parameter in argument position parses as a type when it is a
    /// bare identifier; it becomes the const argument the enum named.
    fn visit_generic_argument_mut(&mut self, argument: &mut GenericArgument) {
        if let GenericArgument::Type(ty) = argument {
            if let Some(replacement) = bare_ident(ty).and_then(|ident| self.const_for(ident)) {
                *argument = GenericArgument::Const(replacement.clone());
                return;
            }
        }
        visit_mut::visit_generic_argument_mut(self, argument);
    }
}

fn expand_implement(input: ImplementInput) -> Result<TokenStream2> {
    let ImplementInput {
        item_trait,
        item_enum,
        spec,
    } = input;
    let enum_ident = &item_enum.ident;
    let (impl_generics, ty_generics, where_clause) = item_enum.generics.split_for_impl();
    let Some(trait_path) = &spec.path else {
        return Err(Error::new(
            Span::call_site(),
            "the trait to implement is missing",
        ));
    };
    let variants = item_enum
        .variants
        .iter()
        .map(variant_info)
        .collect::<Result<Vec<_>>>()?;
    let Some(first) = variants.first() else {
        return Err(Error::new_spanned(
            &item_enum,
            "a dispatch enum needs at least one variant",
        ));
    };
    let first_target = target_type(first);
    let first_payload = &first_target;
    let mut substitution = Substitution::new(&item_trait, trait_path)?;
    let mut items = TokenStream2::new();
    let mut agreed_types: Vec<&Ident> = vec![];
    for item in &item_trait.items {
        match item {
            TraitItem::Type(assoc) => {
                if !assoc.generics.params.is_empty() {
                    return Err(Error::new_spanned(
                        assoc,
                        "generic associated types are not supported",
                    ));
                }
                let name = &assoc.ident;
                let ty = match spec.assoc_override(name) {
                    Some(ty) => quote!(#ty),
                    None => {
                        agreed_types.push(name);
                        quote!(<#first_payload as #trait_path>::#name)
                    }
                };
                items.extend(quote! { type #name = #ty; });
            }
            TraitItem::Const(constant) => {
                let name = &constant.ident;
                let mut ty = constant.ty.clone();
                substitution.visit_type_mut(&mut ty);
                items.extend(quote! {
                    const #name: #ty = <#first_payload as #trait_path>::#name;
                });
            }
            TraitItem::Fn(function) => {
                items.extend(expand_method(
                    function,
                    &variants,
                    enum_ident,
                    trait_path,
                    &spec,
                    &mut substitution,
                )?);
            }
            TraitItem::Macro(item) => {
                return Err(Error::new_spanned(
                    item,
                    "macro invocations inside the trait are not supported",
                ));
            }
            _ => {}
        }
    }
    let agreement_check = associated_type_check(
        &agreed_types,
        &variants,
        trait_path,
        first_payload,
        &item_enum.generics,
    );
    Ok(quote! {
        #[automatically_derived]
        impl #impl_generics #trait_path for #enum_ident #ty_generics #where_clause {
            #items
        }
        #agreement_check
    })
}

/// Checks at compile time that every variant's associated types equal the
/// first variant's, so a disagreement is reported on the variant instead of
/// inside a generated method body.
///
/// The helper carries the enum's own generics, since a nested function
/// could not name them; the outer function passes them on to each call.
fn associated_type_check(
    names: &[&Ident],
    variants: &[VariantInfo],
    trait_path: &Path,
    first_payload: &TokenStream2,
    generics: &syn::Generics,
) -> TokenStream2 {
    if names.is_empty() || variants.len() < 2 {
        return TokenStream2::new();
    }
    let bindings: Vec<GenericArgument> = names
        .iter()
        .map(|name| syn::parse_quote!(#name = <#first_payload as #trait_path>::#name))
        .collect();
    let mut bound = trait_path.clone();
    let last = bound
        .segments
        .last_mut()
        .expect("a path has at least one segment");
    match &mut last.arguments {
        PathArguments::AngleBracketed(arguments) => arguments.args.extend(bindings),
        arguments => {
            *arguments = PathArguments::AngleBracketed(syn::parse_quote!(<#(#bindings),*>))
        }
    }
    let mut helper_generics = generics.clone();
    helper_generics
        .params
        .push(syn::parse_quote!(__Variant: ?Sized + #bound));
    let (helper_impl_generics, _, helper_where_clause) = helper_generics.split_for_impl();
    let (impl_generics, _, where_clause) = generics.split_for_impl();
    let enum_arguments: Vec<TokenStream2> = generics
        .params
        .iter()
        .map(|param| match param {
            GenericParam::Lifetime(param) => param.lifetime.to_token_stream(),
            GenericParam::Type(param) => param.ident.to_token_stream(),
            GenericParam::Const(param) => param.ident.to_token_stream(),
        })
        .collect();
    let checks = variants.iter().skip(1).map(|variant| {
        let payload = target_type(variant);
        let cfgs = &variant.cfgs;
        quote! { #(#cfgs)* __same_as_first_variant::<#(#enum_arguments,)* #payload>(); }
    });
    quote! {
        const _: () = {
            #[allow(dead_code, non_snake_case)]
            fn __same_as_first_variant #helper_impl_generics () #helper_where_clause {}
            #[allow(dead_code, non_snake_case)]
            fn __whichever_associated_types_agree #impl_generics () #where_clause {
                #(#checks)*
            }
        };
    }
}

enum ReturnShape {
    Plain,
    RewrapSelf,
    MapSelf,
    IntoOverridden,
    MapErrOverridden,
}

fn single_segment(ty: &Type) -> Option<&PathSegment> {
    match ty {
        Type::Path(TypePath { qself: None, path }) if path.segments.len() == 1 => {
            Some(&path.segments[0])
        }
        _ => None,
    }
}

fn is_self(ty: &Type) -> bool {
    single_segment(ty).is_some_and(|segment| segment.ident == "Self" && segment.arguments.is_none())
}

/// Returns the name of the associated type in a `Self::Name` type.
fn self_assoc(ty: &Type) -> Option<&Ident> {
    match ty {
        Type::Path(TypePath { qself: None, path })
            if path.segments.len() == 2 && path.segments[0].ident == "Self" =>
        {
            Some(&path.segments[1].ident)
        }
        _ => None,
    }
}

fn generic_type_args(segment: &PathSegment) -> Vec<&Type> {
    match &segment.arguments {
        PathArguments::AngleBracketed(arguments) => arguments
            .args
            .iter()
            .filter_map(|argument| match argument {
                GenericArgument::Type(ty) => Some(ty),
                _ => None,
            })
            .collect(),
        _ => vec![],
    }
}

fn classify_return(output: &ReturnType, spec: &TraitSpec) -> Result<ReturnShape> {
    let ReturnType::Type(_, ty) = output else {
        return Ok(ReturnShape::Plain);
    };
    if let Type::ImplTrait(_) = **ty {
        return Err(Error::new_spanned(
            ty,
            "a return-position `impl Trait` cannot be dispatched by a `match` because every variant returns a different opaque type; return a concrete type or a boxed trait object",
        ));
    }
    if is_self(ty) {
        return Ok(ReturnShape::RewrapSelf);
    }
    if let Some(name) = self_assoc(ty) {
        return Ok(if spec.assoc_override(name).is_some() {
            ReturnShape::IntoOverridden
        } else {
            ReturnShape::Plain
        });
    }
    if let Some(segment) = single_segment(ty) {
        let args = generic_type_args(segment);
        let wraps_self = args.first().is_some_and(|first| is_self(first));
        if (segment.ident == "Option" || segment.ident == "Result") && wraps_self {
            return Ok(ReturnShape::MapSelf);
        }
        if segment.ident == "Result" && args.len() == 2 {
            if let Some(name) = self_assoc(args[1]) {
                if spec.assoc_override(name).is_some() {
                    return Ok(ReturnShape::MapErrOverridden);
                }
            }
        }
    }
    Ok(ReturnShape::Plain)
}

struct SelfFinder(bool);

impl<'ast> Visit<'ast> for SelfFinder {
    fn visit_type_path(&mut self, type_path: &'ast TypePath) {
        if type_path.qself.is_none()
            && type_path.path.segments.len() == 1
            && type_path.path.segments[0].ident == "Self"
        {
            self.0 = true;
        }
        syn::visit::visit_type_path(self, type_path);
    }
}

/// How a method reaches its receiver or a `Self` argument.
#[derive(Clone, Copy)]
enum Access {
    Shared,
    Exclusive,
    Owned,
}

enum ArgumentKind {
    Plain,
    SameVariant(Access),
}

/// Returns how a receiver reaches the value, from its type: `self` and
/// `self: Self` own it, `&self` and `self: &Self` share it, `&mut self` and
/// `self: &mut Self` have it exclusively. Any other receiver type is an
/// error.
fn receiver_access(receiver: &Receiver) -> Result<Access> {
    match &*receiver.ty {
        ty if is_self(ty) => Ok(Access::Owned),
        Type::Reference(reference) if is_self(&reference.elem) => {
            Ok(if reference.mutability.is_some() { Access::Exclusive } else { Access::Shared })
        }
        other => Err(Error::new_spanned(
            other,
            "this receiver type is not supported; a receiver is `self`, `&self` or `&mut self`, also written `self: Self`, `self: &Self` or `self: &mut Self`",
        )),
    }
}

fn argument_kind(ty: &Type) -> Result<ArgumentKind> {
    let (inner, access) = match ty {
        Type::Reference(reference) if reference.mutability.is_some() => {
            (&*reference.elem, Access::Exclusive)
        }
        Type::Reference(reference) => (&*reference.elem, Access::Shared),
        other => (other, Access::Owned),
    };
    if is_self(inner) {
        return Ok(ArgumentKind::SameVariant(access));
    }
    let mut finder = SelfFinder(false);
    finder.visit_type(ty);
    if finder.0 {
        return Err(Error::new_spanned(
            ty,
            "only `Self`, `&Self` and `&mut Self` arguments can be dispatched; this type mentions `Self` elsewhere",
        ));
    }
    Ok(ArgumentKind::Plain)
}

/// Returns the expression that hands `binding`, bound to a variant's
/// payload, to the trait method: the payload itself, or what it
/// dereferences to for a `#[dispatch(deref)]` variant.
fn payload_expression(deref: bool, access: Access, binding: &Ident) -> TokenStream2 {
    if !deref {
        return quote!(#binding);
    }
    match access {
        Access::Shared => quote!(&**#binding),
        Access::Exclusive => quote!(&mut **#binding),
        Access::Owned => quote!(*#binding),
    }
}

/// Returns the `mismatch` expression given on the trait method itself.
fn method_mismatch(function: &TraitItemFn) -> Result<Option<Expr>> {
    for attribute in &function.attrs {
        if !is_dispatch_attribute(attribute) {
            continue;
        }
        let pair: KeyedExpr = attribute.parse_args()?;
        if pair.key != "mismatch" {
            return Err(Error::new_spanned(
                pair.key,
                "expected `#[dispatch(mismatch = <expr>)]`",
            ));
        }
        return Ok(Some(pair.value));
    }
    Ok(None)
}

fn expand_method(
    function: &TraitItemFn,
    variants: &[VariantInfo],
    enum_ident: &Ident,
    trait_path: &Path,
    spec: &TraitSpec,
    substitution: &mut Substitution,
) -> Result<TokenStream2> {
    let mut signature: Signature = function.sig.clone();
    substitution.visit_signature_mut(&mut signature);
    let method = signature.ident.clone();
    let Some(FnArg::Receiver(receiver)) = signature.inputs.first() else {
        if function.default.is_some() {
            return Ok(TokenStream2::new());
        }
        return Err(Error::new_spanned(
            &function.sig,
            format!("`{method}` has no receiver, so there is no variant to dispatch on; give it a default body or implement it by hand"),
        ));
    };
    let receiver_access = receiver_access(receiver)?;
    // `self` is resolved like a local variable, so the generated `match`
    // must use the receiver's own token to share its hygiene.
    let self_token = receiver.self_token;
    // Generated bindings get `macro_rules`-style hygiene, so they can never
    // collide with identifiers written by the user.
    let hygienic = |name: &str| Ident::new(name, Span::mixed_site());
    let inner = hygienic("__inner");
    let mut argument_idents = vec![];
    let mut same_variant: Vec<(usize, Access)> = vec![];
    for (index, input) in signature.inputs.iter_mut().skip(1).enumerate() {
        let FnArg::Typed(pat_type) = input else {
            unreachable!("receiver handled above")
        };
        let ident = hygienic(&format!("__argument{index}"));
        *pat_type.pat = Pat::Ident(PatIdent {
            attrs: vec![],
            by_ref: None,
            mutability: None,
            ident: ident.clone(),
            subpat: None,
        });
        if let ArgumentKind::SameVariant(access) = argument_kind(&pat_type.ty)? {
            same_variant.push((index, access));
        }
        argument_idents.push(ident);
    }
    let return_shape = classify_return(&signature.output, spec)?;
    let is_async = signature.asyncness.is_some();
    let is_unsafe = signature.unsafety.is_some();
    let mismatch = match spec.mismatch_for(&method) {
        Some(expr) => Some(expr.clone()),
        None => method_mismatch(function)?,
    };
    let mut arms = TokenStream2::new();
    for variant in variants {
        let VariantInfo {
            ident: variant_ident,
            payload,
            cfgs,
            deref,
        } = variant;
        if *deref && matches!(return_shape, ReturnShape::RewrapSelf | ReturnShape::MapSelf) {
            return Err(Error::new_spanned(
                payload,
                format!("`{method}` returns `Self`, which cannot be wrapped back into the dereferenced payload of variant `{variant_ident}`; drop `deref` on it or implement the method by hand"),
            ));
        }
        let target = target_type(variant);
        let receiver_expression = payload_expression(*deref, receiver_access, &inner);
        let call_arguments = argument_idents.iter().enumerate().map(|(index, ident)| {
            match same_variant
                .iter()
                .find(|(same_index, _)| *same_index == index)
            {
                Some((_, access)) => {
                    payload_expression(*deref, *access, &hygienic(&format!("__same{index}")))
                }
                None => quote!(#ident),
            }
        });
        let mut call = quote! {
            <#target as #trait_path>::#method(#receiver_expression #(, #call_arguments)*)
        };
        if is_unsafe {
            call = quote! { unsafe { #call } };
        }
        if is_async {
            call = quote! { #call.await };
        }
        call = match return_shape {
            ReturnShape::Plain => call,
            ReturnShape::RewrapSelf => quote! { #enum_ident::#variant_ident(#call) },
            ReturnShape::MapSelf => quote! { #call.map(#enum_ident::#variant_ident) },
            ReturnShape::IntoOverridden => quote! { ::core::convert::Into::into(#call) },
            ReturnShape::MapErrOverridden => {
                quote! { #call.map_err(::core::convert::Into::into) }
            }
        };
        let pattern = if same_variant.is_empty() {
            quote! { #enum_ident::#variant_ident(#inner) }
        } else {
            let same_patterns = same_variant.iter().map(|(index, _)| {
                let same = hygienic(&format!("__same{index}"));
                quote! { #enum_ident::#variant_ident(#same) }
            });
            quote! { (#enum_ident::#variant_ident(#inner), #(#same_patterns),*) }
        };
        arms.extend(quote! { #(#cfgs)* #pattern => #call, });
    }
    let scrutinee = if same_variant.is_empty() {
        quote!(#self_token)
    } else {
        let arguments = same_variant
            .iter()
            .map(|(index, _)| &argument_idents[*index]);
        quote! { (#self_token, #(#arguments),*) }
    };
    let (fallback, allow_unreachable) = if same_variant.is_empty() {
        (TokenStream2::new(), TokenStream2::new())
    } else {
        let Some(expr) = mismatch else {
            return Err(Error::new_spanned(
                &function.sig,
                format!("`{method}` takes `Self` arguments; give the result for two different variants with `#[dispatch(mismatch = <expr>)]` on the trait method or `mismatch({method} = <expr>)` on the enum"),
            ));
        };
        (
            quote! { _ => #expr, },
            quote! { #[allow(unreachable_patterns)] },
        )
    };
    let inline = if function
        .attrs
        .iter()
        .any(|attribute| attribute.path().is_ident("inline"))
    {
        TokenStream2::new()
    } else {
        quote! { #[inline] }
    };
    let cfgs = cfg_attributes(&function.attrs);
    Ok(quote! {
        #(#cfgs)*
        #inline
        #allow_unreachable
        #signature {
            match #scrutinee {
                #arms
                #fallback
            }
        }
    })
}

// ---------------------------------------------------------------------------
// Inherent methods
// ---------------------------------------------------------------------------

struct InherentInput {
    item_enum: ItemEnum,
    item_impl: ItemImpl,
}

impl Parse for InherentInput {
    fn parse(input: ParseStream) -> Result<Self> {
        input.parse::<Token![@]>()?;
        let version: Ident = input.parse()?;
        if version != "v1" {
            return Err(Error::new_spanned(
                version,
                "unsupported `whichever` protocol version",
            ));
        }
        input.parse::<Token![enum]>()?;
        let enum_tokens;
        braced!(enum_tokens in input);
        let item_enum: ItemEnum = enum_tokens.parse()?;
        input.parse::<Token![impl]>()?;
        let impl_tokens;
        braced!(impl_tokens in input);
        let item_impl: ItemImpl = impl_tokens.parse()?;
        Ok(Self {
            item_enum,
            item_impl,
        })
    }
}

/// Rewrites an inherent impl block; called by the macro that `#[dispatch]`
/// defines for the enum.
#[doc(hidden)]
#[proc_macro]
pub fn implement_inherent(input: TokenStream) -> TokenStream {
    let input = parse_macro_input!(input as InherentInput);
    expand_inherent(input)
        .unwrap_or_else(|error| error.to_compile_error())
        .into()
}

/// Which variants an inherent method forwards to, and how.
struct Forwarding {
    all: bool,
    variants: Vec<Ident>,
    call: Option<Ident>,
    with: Option<Path>,
}

impl Parse for Forwarding {
    fn parse(input: ParseStream) -> Result<Self> {
        let mut forwarding = Forwarding {
            all: false,
            variants: vec![],
            call: None,
            with: None,
        };
        while !input.is_empty() {
            if input.peek(Token![*]) {
                input.parse::<Token![*]>()?;
                forwarding.all = true;
            } else {
                let ident: Ident = input.parse()?;
                if input.peek(Token![=]) {
                    input.parse::<Token![=]>()?;
                    match ident.to_string().as_str() {
                        "call" => forwarding.call = Some(input.parse()?),
                        "with" => forwarding.with = Some(input.parse()?),
                        other => {
                            return Err(Error::new(
                                ident.span(),
                                format!("unknown option `{other}`; the options are `call = method` and `with = path`"),
                            ))
                        }
                    }
                } else {
                    forwarding.variants.push(ident);
                }
            }
            if input.is_empty() {
                break;
            }
            input.parse::<Token![,]>()?;
        }
        if forwarding.all && !forwarding.variants.is_empty() {
            return Err(Error::new(
                forwarding.variants[0].span(),
                "`*` already names every variant",
            ));
        }
        if !forwarding.all && forwarding.variants.is_empty() {
            return Err(input.error("name the variants to forward to, or `*` for all of them"));
        }
        Ok(forwarding)
    }
}

/// A method declared without a body, which `syn` leaves as verbatim tokens.
struct BodylessMethod {
    attrs: Vec<Attribute>,
    vis: Visibility,
    sig: Signature,
}

impl Parse for BodylessMethod {
    fn parse(input: ParseStream) -> Result<Self> {
        let attrs = input.call(Attribute::parse_outer)?;
        let vis = input.parse()?;
        let sig = input.parse()?;
        input.parse::<Token![;]>()?;
        Ok(Self { attrs, vis, sig })
    }
}

fn expand_inherent(input: InherentInput) -> Result<TokenStream2> {
    let InherentInput {
        item_enum,
        mut item_impl,
    } = input;
    let variants = item_enum
        .variants
        .iter()
        .map(variant_info)
        .collect::<Result<Vec<_>>>()?;
    let mut items = Vec::with_capacity(item_impl.items.len());
    for item in std::mem::take(&mut item_impl.items) {
        match item {
            ImplItem::Fn(mut function) => {
                let Some(position) = function.attrs.iter().position(is_dispatch_attribute) else {
                    items.push(ImplItem::Fn(function));
                    continue;
                };
                let forwarding: Forwarding = function.attrs.remove(position).parse_args()?;
                let generated = forward_method(
                    function.attrs,
                    function.vis,
                    function.sig,
                    Some(function.block),
                    forwarding,
                    &variants,
                    &item_enum.ident,
                )?;
                items.push(ImplItem::Verbatim(generated));
            }
            ImplItem::Verbatim(tokens) => {
                let method: BodylessMethod = syn::parse2(tokens.clone()).map_err(|_| {
                    Error::new_spanned(
                        &tokens,
                        "only methods, with or without a body, are forwarded in a `#[dispatch]` impl block",
                    )
                })?;
                let BodylessMethod {
                    mut attrs,
                    vis,
                    sig,
                } = method;
                let Some(position) = attrs.iter().position(is_dispatch_attribute) else {
                    return Err(Error::new_spanned(
                        &sig,
                        "a method without a body needs `#[dispatch(*)]` to be forwarded to every variant",
                    ));
                };
                let forwarding: Forwarding = attrs.remove(position).parse_args()?;
                let generated = forward_method(
                    attrs,
                    vis,
                    sig,
                    None,
                    forwarding,
                    &variants,
                    &item_enum.ident,
                )?;
                items.push(ImplItem::Verbatim(generated));
            }
            other => items.push(other),
        }
    }
    item_impl.items = items;
    Ok(item_impl.to_token_stream())
}

/// Writes one forwarded inherent method: a `match` with one arm per selected
/// variant calling the payload's own method, and the body as the arm for
/// every other variant.
fn forward_method(
    attrs: Vec<Attribute>,
    vis: Visibility,
    sig: Signature,
    body: Option<Block>,
    forwarding: Forwarding,
    variants: &[VariantInfo],
    enum_ident: &Ident,
) -> Result<TokenStream2> {
    let method = &sig.ident;
    let Some(FnArg::Receiver(receiver)) = sig.inputs.first() else {
        return Err(Error::new_spanned(
            &sig,
            format!("`{method}` has no receiver, so there is no variant to forward to"),
        ));
    };
    receiver_access(receiver)?;
    let self_token = receiver.self_token;
    let mut arguments = Vec::new();
    for input in sig.inputs.iter().skip(1) {
        let FnArg::Typed(pat_type) = input else {
            unreachable!("receiver handled above")
        };
        let Pat::Ident(pattern) = &*pat_type.pat else {
            return Err(Error::new_spanned(
                &pat_type.pat,
                "a forwarded parameter must be a plain identifier",
            ));
        };
        arguments.push(pattern.ident.clone());
    }
    let selected: Vec<&VariantInfo> = if forwarding.all {
        variants.iter().collect()
    } else {
        forwarding
            .variants
            .iter()
            .map(|name| {
                variants
                    .iter()
                    .find(|variant| &variant.ident == name)
                    .ok_or_else(|| {
                        Error::new(
                            name.span(),
                            format!("`{enum_ident}` has no variant `{name}`"),
                        )
                    })
            })
            .collect::<Result<_>>()?
    };
    let covers_all = forwarding.all
        || variants
            .iter()
            .all(|variant| forwarding.variants.contains(&variant.ident));
    match (covers_all, &body) {
        (true, Some(body)) => {
            return Err(Error::new_spanned(
                body,
                format!("`{method}` is forwarded to every variant, so its body would never run; end the signature with `;` instead"),
            ))
        }
        (false, None) => {
            return Err(Error::new_spanned(
                &sig,
                format!("`{method}` is forwarded to some variants only; give it a body, which is what the other variants return"),
            ))
        }
        _ => {}
    }
    let inner = Ident::new("__inner", Span::mixed_site());
    let call_name = forwarding.call.as_ref().unwrap_or(method);
    let awaited = sig.asyncness.is_some();
    let arms = selected.iter().map(|variant| {
        let VariantInfo { ident, cfgs, .. } = variant;
        let mut call = quote! { #inner.#call_name(#(#arguments),*) };
        if awaited {
            call = quote! { #call.await };
        }
        if let Some(with) = &forwarding.with {
            call = quote! { #with(#call) };
        }
        quote! { #(#cfgs)* Self::#ident(#inner) => #call, }
    });
    let fallback = body.map(|body| quote! { _ => #body, });
    Ok(quote! {
        #(#attrs)*
        #vis #sig {
            match #self_token {
                #(#arms)*
                #fallback
            }
        }
    })
}