af-move-type-derive 0.6.1

Derive macros for traits defined in af-move-type.
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
use convert_case::{Case, Casing};
use proc_macro2::{Ident, TokenStream};
use quote::quote;
use syn::spanned::Spanned;
use syn::{DeriveInput, GenericParam, Generics, Path, TypeParamBound, parse_quote};

#[derive(deluxe::ExtractAttributes)]
#[deluxe(attributes(move_))]
struct MoveAttributes {
    #[deluxe(rename = crate)]
    thecrate: Option<Path>,
    address: Option<String>,
    module: Option<Ident>,
    #[deluxe(default = false)]
    nameless: bool,
}

#[expect(clippy::obfuscated_if_else)]
pub fn impl_move_struct(item: TokenStream) -> deluxe::Result<TokenStream> {
    // parse
    let mut ast: DeriveInput = syn::parse2(item)?;

    ensure_nonempty_struct(&ast)?;

    let MoveAttributes {
        thecrate,
        address,
        module,
        nameless,
    } = deluxe::extract_attributes(&mut ast)?;

    let thecrate: Path = thecrate
        .map(|c| parse_quote!(#c))
        .unwrap_or_else(|| parse_quote!(::af_move_type));
    let module = module.map(|i| i.to_string());

    let type_tag_params = type_tag_parameters(
        &ast,
        thecrate.clone(),
        address.clone(),
        module.clone(),
        nameless,
    );

    let type_tag_impl = impl_type_tag(type_tag_params.clone(), thecrate.clone());
    let move_struct_impl = impl_move_struct_(&ast, thecrate.clone(), type_tag_params);
    let static_address_impl = address
        .map(|a| impl_static_address(&ast, thecrate.clone(), a))
        .unwrap_or_else(|| quote!());
    let static_module_impl = module
        .map(|m| impl_static_module(&ast, thecrate.clone(), m))
        .unwrap_or_else(|| quote!());
    let static_name_impl = nameless
        .then(|| quote!())
        .unwrap_or_else(|| impl_static_name(&ast, thecrate.clone()));
    // There's always an implementation: either there are no type params or all type params can be
    // constrained to implement `StaticTypeTag`
    let static_type_params_impl = impl_static_type_params(&ast, thecrate);

    Ok(quote! {
        #type_tag_impl
        #move_struct_impl
        #static_address_impl
        #static_module_impl
        #static_name_impl
        #static_type_params_impl
    })
}

fn ensure_nonempty_struct(ast: &DeriveInput) -> deluxe::Result<()> {
    match &ast.data {
        syn::Data::Struct(data) => {
            if data.fields.is_empty() {
                return Err(syn::Error::new(
                    data.fields.span(),
                    "Structs can't be empty. If a Move struct is empty, then in the Rust equivalent it \
                must have a single field of type `bool`. This is because the BCS of an empty Move \
                struct encodes a single boolean dummy field.",
                ));
            }
        }
        _ => {
            return Err(syn::Error::new(
                ast.span(),
                "MoveStruct only defined for structs",
            ));
        }
    };
    Ok(())
}

/// Implementation of the `_TypeTag` for the struct.
fn impl_type_tag(type_tag_params: TypeTagParameters, thecrate: Path) -> TokenStream {
    let TypeTagParameters {
        ident,
        attr_idents,
        attr_types,
        generics,
        struct_tag_var_attrs,
        struct_tag_const_attrs,
        struct_tag_const_vals,
        struct_tag_consts_checks,
        mut type_param_idents,
        address_const,
        module_const,
        name_const,
    } = type_tag_params;

    let attr_declarations: Vec<_> = attr_idents
        .iter()
        .zip(&attr_types)
        .map(|(ident, type_)| quote!(pub #ident: #type_))
        .collect();
    let struct_tag_const_declarations: Vec<_> = struct_tag_const_attrs
        .iter()
        .zip(&struct_tag_const_vals)
        .map(|(ident, val)| quote!(#ident: #val))
        .collect();

    // Pascal-cased generic type param idents (e.g. `T`), in declaration order.
    // Needed BEFORE we reverse `type_param_idents` below for `unpack_type_params`.
    let type_param_pascals: Vec<Ident> = generics
        .params
        .iter()
        .filter_map(|p| match p {
            GenericParam::Type(t) => Some(t.ident.clone()),
            _ => None,
        })
        .collect();
    // Snake-cased type-param idents in declaration order — these are the field
    // names on the marker (e.g. `self.t`). Captured before the reverse below.
    let type_param_snakes: Vec<Ident> = type_param_idents.clone();

    type_param_idents.reverse();
    let unpack_type_params = quote!(
        // Unwrap here since we already checked the vector length
        #(
            let #type_param_idents = type_params
                .pop()
                .unwrap()
                .try_into()
                .map_err(#thecrate::TypeParamsError::from)?;)*
    );

    // define impl variables
    let (impl_generics, type_generics, where_clause) = generics.split_for_impl();
    let type_tag_type = quote!(#thecrate::external::TypeTag);
    let struct_tag_type = quote!(#thecrate::external::StructTag);
    let struct_tag_helper_type = quote!(#thecrate::external::StructTagHelper);
    let result_type = quote!(::std::result::Result);
    let derive_ord = if has_type_params(&generics) {
        quote! {
            #[#thecrate::external::derive_where::derive_where(
                crate = #thecrate::external::derive_where
            )]
            #[derive_where(PartialOrd, Ord)]
        }
    } else {
        quote!(#[derive(PartialOrd, Ord)])
    };
    let serde_with_crate = quote!(#thecrate::external::serde_with).to_string();

    // Emit a non-allocating override appropriate to the marker's identity shape:
    //   - All three of address/module/name are compile-time literals →
    //     override both `matches` (static, recurses via static
    //     `T::TypeTag::matches`) AND `matches_instance` (recurses via
    //     `self.<snake>.matches_instance`). The instance-based override exists
    //     so that fully-static outer markers wrapping runtime-identity inner
    //     type params (e.g. `Field<K, Leaf<RuntimeAddrStruct>>`) stay
    //     non-allocating when callers have a marker instance.
    //   - Module and name are literals but address is a runtime field →
    //     override `matches_instance` (uses `self.address`). Type-param
    //     recursion uses `self.<snake>.matches_instance(...)`.
    //   - Address and module are literals but name is a runtime field (i.e.
    //     nameless markers like `Otw`) → override `matches_instance` (uses
    //     `self.name`). Same instance-based type-param recursion.
    //   - Anything else → empty impl, both methods fall through to the default
    //     `try_from(tag.clone()).is_ok()`.
    let n_type_params = type_param_pascals.len();
    let type_param_checks_static: Vec<_> = type_param_pascals
        .iter()
        .enumerate()
        .map(|(i, t)| {
            quote!(
                <<#t as #thecrate::MoveType>::TypeTag as #thecrate::MoveTypeTag>::matches(
                    &type_params[#i]
                )
            )
        })
        .collect();
    let type_param_checks_instance: Vec<_> = type_param_snakes
        .iter()
        .enumerate()
        .map(|(i, snake)| {
            quote!(
                #thecrate::MoveTypeTag::matches_instance(&self.#snake, &type_params[#i])
            )
        })
        .collect();
    let move_type_tag_impl = match (&address_const, &module_const, &name_const) {
        (Some(addr), Some(module), Some(name)) => {
            quote! {
                impl #impl_generics #thecrate::MoveTypeTag for #ident #type_generics
                #where_clause
                {
                    fn matches(tag: &#type_tag_type) -> bool {
                        const EXPECTED_ADDRESS: #thecrate::external::Address =
                            <#thecrate::external::Address>::from_static(#addr);
                        let #type_tag_type::Struct(stag) = tag else {
                            return false;
                        };
                        let type_params = stag.type_params();
                        if type_params.len() != #n_type_params {
                            return false;
                        }
                        stag.address() == &EXPECTED_ADDRESS
                            && stag.module().as_str() == #module
                            && stag.name().as_str() == #name
                            #(&& #type_param_checks_static)*
                    }

                    fn matches_instance(&self, tag: &#type_tag_type) -> bool {
                        const EXPECTED_ADDRESS: #thecrate::external::Address =
                            <#thecrate::external::Address>::from_static(#addr);
                        let #type_tag_type::Struct(stag) = tag else {
                            return false;
                        };
                        let type_params = stag.type_params();
                        if type_params.len() != #n_type_params {
                            return false;
                        }
                        stag.address() == &EXPECTED_ADDRESS
                            && stag.module().as_str() == #module
                            && stag.name().as_str() == #name
                            #(&& #type_param_checks_instance)*
                    }
                }
            }
        }
        (None, Some(module), Some(name)) => {
            quote! {
                impl #impl_generics #thecrate::MoveTypeTag for #ident #type_generics
                #where_clause
                {
                    fn matches_instance(&self, tag: &#type_tag_type) -> bool {
                        let #type_tag_type::Struct(stag) = tag else {
                            return false;
                        };
                        let type_params = stag.type_params();
                        if type_params.len() != #n_type_params {
                            return false;
                        }
                        stag.address() == &self.address
                            && stag.module().as_str() == #module
                            && stag.name().as_str() == #name
                            #(&& #type_param_checks_instance)*
                    }
                }
            }
        }
        (Some(addr), Some(module), None) => {
            quote! {
                impl #impl_generics #thecrate::MoveTypeTag for #ident #type_generics
                #where_clause
                {
                    fn matches_instance(&self, tag: &#type_tag_type) -> bool {
                        const EXPECTED_ADDRESS: #thecrate::external::Address =
                            <#thecrate::external::Address>::from_static(#addr);
                        let #type_tag_type::Struct(stag) = tag else {
                            return false;
                        };
                        let type_params = stag.type_params();
                        if type_params.len() != #n_type_params {
                            return false;
                        }
                        stag.address() == &EXPECTED_ADDRESS
                            && stag.module().as_str() == #module
                            && stag.name() == &self.name
                            #(&& #type_param_checks_instance)*
                    }
                }
            }
        }
        _ => quote! {
            impl #impl_generics #thecrate::MoveTypeTag for #ident #type_generics
            #where_clause
            {}
        },
    };

    quote! {
        #[derive(
            Clone,
            Debug,
            PartialEq,
            Eq,
            Hash,
            #thecrate::external::DeserializeFromStr,
            #thecrate::external::SerializeDisplay,
        )]
        #[serde_with(crate = #serde_with_crate)]
        #derive_ord
        pub struct #ident #generics {
            #(#attr_declarations),*
        }

        impl #impl_generics ::std::convert::From<#ident #type_generics> for #type_tag_type
        #where_clause
        {
            fn from(value: #ident #type_generics) -> Self {
                Self::Struct(::std::boxed::Box::new(value.into()))
            }
        }

        impl #impl_generics ::std::convert::From<#ident #type_generics> for #struct_tag_type
        #where_clause
        {
            fn from(value: #ident #type_generics) -> Self {
                let #ident {
                    #(#attr_idents),*
                } = value;
                let helper = #struct_tag_helper_type {
                    #(#struct_tag_var_attrs,)*
                    #(#struct_tag_const_declarations),*
                };
                Self::from(helper)
            }
        }

        impl #impl_generics TryFrom<#type_tag_type> for #ident #type_generics
        #where_clause
        {
            type Error = #thecrate::TypeTagError;

            fn try_from(value: #type_tag_type) -> #result_type<Self, Self::Error> {
                match value {
                    #type_tag_type::Struct(stag) => #result_type::Ok((*stag).try_into()?),
                    other => #result_type::Err(#thecrate::TypeTagError::Variant {
                        expected: "Struct(_)".to_owned(),
                        got: other,
                    }),
                }
            }
        }

        impl #impl_generics TryFrom<#struct_tag_type> for #ident #type_generics
        #where_clause
        {
            type Error = #thecrate::StructTagError;

            fn try_from(value: #struct_tag_type) -> #result_type<Self, Self::Error> {
                use #thecrate::StructTagError::*;
                let helper = #struct_tag_helper_type::from(&value);
                let #struct_tag_helper_type {
                    address,
                    module,
                    name,
                    mut type_params,
                } = helper;
                #struct_tag_consts_checks
                #unpack_type_params
                #result_type::Ok(Self {
                    #(#attr_idents),*
                })
            }
        }

        impl #impl_generics ::std::fmt::Display for #ident #type_generics
        #where_clause
        {
            fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
                let stag: #struct_tag_type = self.clone().into();
                write!(f, "{}", stag)
            }
        }

        impl #impl_generics ::std::str::FromStr for #ident #type_generics
        #where_clause
        {
            type Err = #thecrate::ParseStructTagError;

            fn from_str(s: &str) -> #result_type<Self, Self::Err> {
                let stag: #struct_tag_type = s.parse()?;
                #result_type::Ok(stag.try_into()?)
            }
        }

        #move_type_tag_impl
    }
}

/// Main `impl` block for the struct and `MoveStruct` impl for it
fn impl_move_struct_(
    ast: &DeriveInput,
    thecrate: Path,
    type_tag_params: TypeTagParameters,
) -> TokenStream {
    let TypeTagParameters {
        ident: type_tag_ident,
        generics: type_tag_generics,
        attr_idents,
        attr_types,
        ..
    } = type_tag_params;

    // Remove the bounds from the type tag generics and construct the type tag type
    let type_tag_type = {
        let (_, type_generics, _) = type_tag_generics.split_for_impl();
        quote!(#type_tag_ident #type_generics)
    };

    // for use in function signatures
    let type_tag_fn_args: Vec<_> = attr_idents
        .iter()
        .zip(&attr_types)
        .map(|(name, ty)| quote!(#name: #ty))
        .collect();

    // let generics = add_type_bound(ast.generics.clone(), parse_quote!(#move_type_trait));
    let ident = &ast.ident;
    let (impl_generics, type_generics, where_clause) = ast.generics.split_for_impl();

    quote! {
        impl #impl_generics #thecrate::MoveType for #ident #type_generics #where_clause {
            type TypeTag = #type_tag_type;
        }

        impl #impl_generics #thecrate::MoveStruct for #ident #type_generics #where_clause {
            type StructTag = #type_tag_type;
        }

        impl #impl_generics #ident #type_generics #where_clause {
            pub fn move_instance(self, #(#type_tag_fn_args),*) -> #thecrate::MoveInstance<Self> {
                #thecrate::MoveInstance {
                    type_: Self::type_(#(#attr_idents),*),
                    value: self,
                }
            }

            pub fn type_(#(#type_tag_fn_args),*) -> #type_tag_type {
                #type_tag_ident {
                    #(#attr_idents),*
                }
            }
        }
    }
}

#[derive(Clone)]
struct TypeTagParameters {
    ident: Ident,
    attr_idents: Vec<TokenStream>,
    attr_types: Vec<TokenStream>,
    generics: Generics,
    struct_tag_var_attrs: Vec<TokenStream>,
    struct_tag_const_attrs: Vec<TokenStream>,
    struct_tag_const_vals: Vec<TokenStream>,
    struct_tag_consts_checks: TokenStream,
    type_param_idents: Vec<Ident>,
    address_const: Option<String>,
    module_const: Option<String>,
    name_const: Option<String>,
}

fn type_tag_parameters(
    ast: &DeriveInput,
    thecrate: Path,
    address: Option<String>,
    module: Option<String>,
    nameless: bool,
) -> TypeTagParameters {
    let result_type = quote!(::std::result::Result);
    let mut params = TypeTagParameters {
        ident: type_tag_ident(ast),
        attr_idents: vec![],
        attr_types: vec![],
        generics: ast.generics.clone(),
        struct_tag_var_attrs: vec![],
        struct_tag_const_attrs: vec![],
        struct_tag_const_vals: vec![],
        struct_tag_consts_checks: quote!(),
        type_param_idents: vec![],
        address_const: None,
        module_const: None,
        name_const: None,
    };

    let address_check = if let Some(address) = address {
        params.struct_tag_const_attrs.push(quote!(address));
        let value = quote!(#address.parse().unwrap());
        let check = quote!(
            let expected = #value;
            if address != expected {
                return #result_type::Err(Address { expected, got: address });
            }
        );
        params.struct_tag_const_vals.push(value);
        params.address_const = Some(address);
        check
    } else {
        params.attr_idents.push(quote!(address));
        params.attr_types.push(quote!(#thecrate::external::Address));
        params.struct_tag_var_attrs.push(quote!(address));
        quote!()
    };

    let module_check = if let Some(module) = module {
        params.struct_tag_const_attrs.push(quote!(module));
        let value = quote!(#module.parse().unwrap());
        let check = quote!(
            let expected = #value;
            if module != expected {
                return #result_type::Err(Module { expected, got: module });
            }
        );
        params.struct_tag_const_vals.push(value);
        params.module_const = Some(module);
        check
    } else {
        params.attr_idents.push(quote!(module));
        params
            .attr_types
            .push(quote!(#thecrate::external::Identifier));
        params.struct_tag_var_attrs.push(quote!(module));
        quote!()
    };

    let name_check = if nameless {
        params.attr_idents.push(quote!(name));
        params
            .attr_types
            .push(quote!(#thecrate::external::Identifier));
        params.struct_tag_var_attrs.push(quote!(name));
        quote!()
    } else {
        let name = ast.ident.to_string();
        params.struct_tag_const_attrs.push(quote!(name));
        let value = quote!(#name.parse().unwrap());
        let check = quote!(
            let expected = #value;
            if name != expected {
                return #result_type::Err(Name { expected, got: name });
            }
        );
        params.struct_tag_const_vals.push(value);
        params.name_const = Some(name);
        check
    };

    let n_types_expected = if has_type_params(&ast.generics) {
        let move_type_trait = quote!(#thecrate::MoveType);
        let TypeNames {
            snake: type_names_snake,
            pascal,
        } = extract_type_names(&ast.generics);
        params
            .attr_idents
            .extend(type_names_snake.iter().map(|n| quote!(#n)));
        params.attr_types.extend(
            pascal
                .iter()
                .map(|n| quote!(<#n as #move_type_trait>::TypeTag)),
        );
        params
            .struct_tag_var_attrs
            .push(quote!(type_params: vec![#(#type_names_snake.into()),*]));
        params.type_param_idents = type_names_snake;
        let n_types = pascal.len();
        quote!(#n_types)
    } else {
        params.struct_tag_const_attrs.push(quote!(type_params));
        params.struct_tag_const_vals.push(quote!(vec![]));
        quote!(0_usize)
    };

    params.struct_tag_consts_checks = quote!(
        #address_check
        #module_check
        #name_check
        let expected = #n_types_expected;
        let n_types = type_params.len();
        if n_types != expected {
            return #result_type::Err(TypeParams(#thecrate::TypeParamsError::Number {
                expected, got: n_types
            }));
        }
    );

    params
}

fn type_tag_ident(ast: &DeriveInput) -> Ident {
    let ident = &ast.ident;
    Ident::new(&format!("{ident}TypeTag"), ident.span())
}

// https://github.com/dtolnay/syn/blob/master/examples/heapsize/heapsize_derive/src/lib.rs#L36-L44
fn add_type_bound(mut generics: Generics, bound: TypeParamBound) -> Generics {
    for param in &mut generics.params {
        if let GenericParam::Type(ref mut type_param) = *param {
            type_param.bounds.push(bound.clone());
        }
    }
    generics
}

#[derive(Default)]
struct TypeNames {
    pascal: Vec<Ident>,
    snake: Vec<Ident>,
}

fn extract_type_names(generics: &Generics) -> TypeNames {
    let mut type_names: TypeNames = Default::default();
    for param in &generics.params {
        if let GenericParam::Type(ref type_param) = *param {
            let ident = type_param.ident.clone();
            type_names.snake.push(Ident::new(
                &type_param.ident.to_string().as_str().to_case(Case::Snake),
                ident.span(),
            ));
            type_names.pascal.push(ident); // Assume type names are already Pascal
        }
    }
    type_names
}

fn has_type_params(generics: &Generics) -> bool {
    generics
        .params
        .iter()
        .any(|g| matches!(g, GenericParam::Type(_)))
}

fn impl_static_address(ast: &DeriveInput, thecrate: Path, address: String) -> TokenStream {
    let name = &ast.ident;
    let (impl_generics, type_generics, where_clause) = ast.generics.split_for_impl();
    quote! {
        impl #impl_generics #thecrate::StaticAddress for #name #type_generics #where_clause {
            fn address() -> #thecrate::external::Address {
                #address.parse().unwrap()
            }
        }
    }
}

fn impl_static_module(ast: &DeriveInput, thecrate: Path, module: String) -> TokenStream {
    let name = &ast.ident;
    let (impl_generics, type_generics, where_clause) = ast.generics.split_for_impl();
    quote! {
        impl #impl_generics #thecrate::StaticModule for #name #type_generics #where_clause {
            fn module() -> #thecrate::external::Identifier {
                #module.parse().unwrap()
            }
        }
    }
}

fn impl_static_name(ast: &DeriveInput, thecrate: Path) -> TokenStream {
    let name = &ast.ident;
    let name_str = name.to_string();
    let (impl_generics, type_generics, where_clause) = ast.generics.split_for_impl();
    quote! {
        impl #impl_generics #thecrate::StaticName for #name #type_generics #where_clause {
            fn name() -> #thecrate::external::Identifier {
                #name_str.parse().unwrap()
            }
        }
    }
}

fn impl_static_type_params(ast: &DeriveInput, thecrate: Path) -> TokenStream {
    let name = &ast.ident;
    let trait_type = quote! { #thecrate::StaticTypeParams };
    if has_type_params(&ast.generics) {
        let static_type_tag = quote!(#thecrate::StaticTypeTag);
        let generics = add_type_bound(ast.generics.clone(), parse_quote!(#static_type_tag));
        let (impl_generics, type_generics, where_clause) = generics.split_for_impl();
        let TypeNames {
            pascal: type_names, ..
        } = extract_type_names(&generics);
        quote! {
            impl #impl_generics #trait_type for #name #type_generics #where_clause {
                fn type_params() -> Vec<#thecrate::external::TypeTag> {
                    vec![#(<#type_names as #static_type_tag>::type_tag()),*]
                }
            }
        }
    } else {
        let (impl_generics, type_generics, where_clause) = ast.generics.split_for_impl();
        quote! {
            impl #impl_generics #trait_type for #name #type_generics #where_clause {
                fn type_params() -> Vec<#thecrate::external::TypeTag> {
                    vec![]
                }
            }
        }
    }
}