rama-macros 0.4.0

procedural macross for rama
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
use proc_macro2::{Literal, Span, TokenStream};
use quote::quote;
use syn::{
    Data, DataEnum, DataStruct, DeriveInput, Fields, GenericArgument, Generics, Lifetime,
    LifetimeParam, PathArguments, Type,
};

use crate::utils::support_root_ts;

/// How a struct field is filled.
enum FieldShape<'a> {
    /// `Option<&'a T>`, `Option<Arc<T>>`, or `(_, usize)` variant
    Piece(FieldSpec<'a>),
    /// `Option<G>` where `G` is itself a `#[derive(FromExtensions)]` group
    Nested(&'a Type),
}

/// An extension piece and whether it's indexed
struct FieldSpec<'a> {
    kind: FieldKind,
    inner: &'a Type,
    indexed: bool,
}

enum FieldKind {
    /// `Option<&'a T>`, borrowed, gathered via `downcast_ref`.
    Ref,
    /// `Option<Arc<T>>`, owned Arc clone, gathered via `cloned_downcast`.
    Arc,
}

pub(crate) fn expand(input: DeriveInput) -> syn::Result<TokenStream> {
    let DeriveInput {
        ident,
        generics,
        data,
        ..
    } = input;

    if generics.type_params().next().is_some() {
        return Err(syn::Error::new_spanned(
            &generics,
            "`#[derive(FromExtensions)]` does not support type parameters",
        ));
    }
    if generics.const_params().next().is_some() {
        return Err(syn::Error::new_spanned(
            &generics,
            "`#[derive(FromExtensions)]` does not support const parameters",
        ));
    }
    let lifetimes: Vec<_> = generics.lifetimes().collect();
    if lifetimes.len() > 1 {
        return Err(syn::Error::new_spanned(
            &generics,
            "`#[derive(FromExtensions)]` supports at most one lifetime parameter",
        ));
    }
    let lifetime = lifetimes.first().map(|lt| lt.lifetime.clone());

    let root = support_root_ts("rama-core", None);

    match &data {
        Data::Struct(data) => expand_struct(&ident, &generics, lifetime.as_ref(), data, &root),
        Data::Enum(data) => expand_enum(&ident, &generics, lifetime.as_ref(), data, &root),
        Data::Union(_) => Err(syn::Error::new_spanned(
            &ident,
            "`#[derive(FromExtensions)]` can only be derived for structs and enums",
        )),
    }
}

/// `#[derive(FromExtensions)]` on a struct: gather one extension piece per named
/// field, plus any nested groups in a single run:
/// producing `fn from_extensions(&Extensions) -> Self`.
fn expand_struct(
    ident: &syn::Ident,
    generics: &Generics,
    lifetime: Option<&Lifetime>,
    data: &DataStruct,
    root: &TokenStream,
) -> syn::Result<TokenStream> {
    let Fields::Named(named) = &data.fields else {
        return Err(syn::Error::new_spanned(
            ident,
            "`#[derive(FromExtensions)]` only supports structs with named fields",
        ));
    };
    let fields = &named.named;
    if fields.is_empty() {
        return Err(syn::Error::new_spanned(
            ident,
            "`#[derive(FromExtensions)]` requires at least one field",
        ));
    }

    let (impl_generics, ty_generics, where_clause) = generics.split_for_impl();
    let (fn_generics, lt) = fn_lifetime(lifetime);

    // The shared buffers are filled and read by a running `__off` cursor, so each
    // field, whether a single piece (one slot) or a nested group (`TARGETS`
    // slots), advances the cursor by its own width. `__N` is the total width,
    // summed at compile time, nested widths are read from the group type via the
    // anonymous lifetime since `'a`/the method lifetime cannot appear in a const.
    let mut contribs = Vec::with_capacity(fields.len());
    let mut target_fills = Vec::with_capacity(fields.len());
    let mut field_lets = Vec::with_capacity(fields.len());
    let mut field_names = Vec::with_capacity(fields.len());

    for field in fields {
        let name = field
            .ident
            .as_ref()
            .expect("named field always has an ident");
        field_names.push(name);
        match classify_field(&field.ty)? {
            FieldShape::Piece(spec) => {
                let inner = spec.inner;
                let downcast = downcast_expr(&spec, root);
                let read = if spec.indexed {
                    quote!(__out[__off].and_then(|(__e, __rank)| #downcast.map(|__v| (__v, __rank))))
                } else {
                    quote!(__out[__off].and_then(|(__e, _)| #downcast))
                };
                contribs.push(quote!(1usize));
                target_fills.push(quote! {
                    __targets[__off] = ::core::any::TypeId::of::<#inner>();
                    __off += 1;
                });
                field_lets.push(quote! {
                    let #name = #read;
                    __off += 1;
                });
            }
            FieldShape::Nested(group) => {
                let anon = anon_lifetimes(group);
                let as_group = quote!(<#group as #root::extensions::FromExtensionsGroup<#lt>>);
                contribs
                    .push(quote!(<#anon as #root::extensions::FromExtensionsGroup<'_>>::TARGETS));
                target_fills.push(quote! {
                    #as_group::from_ext_targets(&mut __targets, __off);
                    __off += #as_group::TARGETS;
                });
                field_lets.push(quote! {
                    let #name = #as_group::from_ext_slots(&__out, __off);
                    __off += #as_group::TARGETS;
                });
            }
        }
    }

    Ok(quote! {
        impl #impl_generics #ident #ty_generics #where_clause {
            /// Gather these extension pieces from `ext` in a single pass.
            ///
            /// Generated by `#[derive(FromExtensions)]`. Each field uses the
            /// same lookup as `Extensions::get_ref` (newest-wins, walks wrappers
            /// and the parent chain), but the store is traversed only once, even
            /// for nested group fields, whose candidates are folded into
            /// the same pass. A field shaped `Option<(&'a T, usize)>` (or
            /// `Option<(Arc<T>, usize)>`) also captures the entry's traversal rank,
            /// `0` is the newest value seen, growing for older ones, so ranks
            /// order fields by recency.
            pub fn from_extensions #fn_generics (
                ext: & #lt #root::extensions::Extensions,
            ) -> Self {
                const __N: usize = #( #contribs )+*;
                let mut __targets = [::core::any::TypeId::of::<()>(); __N];
                let mut __off = 0usize;
                #( #target_fills )*
                let _ = __off;
                let mut __out: [
                    ::core::option::Option<(& #lt #root::extensions::TypeErasedExtension, usize)>; __N
                ] = [::core::option::Option::None; __N];
                ext.get_many_erased(&__targets, &mut __out);
                let mut __off = 0usize;
                #( #field_lets )*
                let _ = __off;
                Self { #( #field_names ),* }
            }
        }
    })
}

/// `#[derive(FromExtensions)]` on an enum: each variant names one candidate
/// extension type. Generates a [`FromExtensionsGroup`] impl (so the enum folds
/// into a parent struct's single pass) plus an inherent `fn
/// from_extensions(&Extensions) -> Option<Self>` returning the variant whose
/// value was inserted most recently (lowest traversal rank), newest-wins.
fn expand_enum(
    ident: &syn::Ident,
    generics: &Generics,
    lifetime: Option<&Lifetime>,
    data: &DataEnum,
    root: &TokenStream,
) -> syn::Result<TokenStream> {
    if data.variants.is_empty() {
        return Err(syn::Error::new_spanned(
            ident,
            "`#[derive(FromExtensions)]` requires at least one variant",
        ));
    }

    let mut target_writes = Vec::with_capacity(data.variants.len());
    let mut builders = Vec::with_capacity(data.variants.len());
    for (i, variant) in data.variants.iter().enumerate() {
        let Fields::Unnamed(unnamed) = &variant.fields else {
            return Err(syn::Error::new_spanned(
                variant,
                "every `#[derive(FromExtensions)]` enum variant must be a tuple variant \
                 holding exactly one `&'a T` or `Arc<T>`",
            ));
        };
        if unnamed.unnamed.len() != 1 {
            return Err(syn::Error::new_spanned(
                variant,
                "every `#[derive(FromExtensions)]` enum variant must hold exactly one value",
            ));
        }
        let vname = &variant.ident;
        let spec = classify_value(&unnamed.unnamed[0].ty)?;
        let inner = spec.inner;
        let idx = Literal::usize_unsuffixed(i);
        let downcast = downcast_expr(&spec, root);
        target_writes.push(quote! {
            __targets[__offset + #idx] = ::core::any::TypeId::of::<#inner>();
        });
        builders.push(if spec.indexed {
            quote!(__out[__offset + #idx].and_then(|(__e, __rank)| #downcast.map(|__v| (Self::#vname((__v, __rank)), __rank))))
        } else {
            quote!(__out[__offset + #idx].and_then(|(__e, __rank)| #downcast.map(|__v| (Self::#vname(__v), __rank))))
        });
    }

    let n = Literal::usize_unsuffixed(data.variants.len());
    let (impl_generics, ty_generics, where_clause) = generics.split_for_impl();
    let (fn_generics, lt) = fn_lifetime(lifetime);

    // Trait impl header: the group lifetime is the enum's own lifetime, or a
    // new one introduced on the impl if only `Arc`s are used
    let (group_impl_generics, group_lt, self_ty) = if let Some(lt) = lifetime {
        (quote!(<#lt>), quote!(#lt), quote!(#ident<#lt>))
    } else {
        let fresh = LifetimeParam::new(Lifetime::new("'__from_ext", Span::call_site()));
        (quote!(<#fresh>), quote!(#fresh), quote!(#ident))
    };

    Ok(quote! {
        impl #group_impl_generics #root::extensions::FromExtensionsGroup<#group_lt>
            for #self_ty #where_clause
        {
            const TARGETS: usize = #n;

            fn from_ext_targets(__targets: &mut [::core::any::TypeId], __offset: usize) {
                #( #target_writes )*
            }

            fn from_ext_slots(
                __out: &[::core::option::Option<(
                    & #group_lt #root::extensions::TypeErasedExtension, usize
                )>],
                __offset: usize,
            ) -> ::core::option::Option<Self> {
                let __candidates: [::core::option::Option<(Self, usize)>; #n] = [ #( #builders ),* ];
                // `min_by_key` keeps the first of equal keys, so on a rank tie
                // (only possible when two variants name the same type and thus
                // resolve to the same entry) the earlier-declared variant wins.
                ::core::iter::IntoIterator::into_iter(__candidates)
                    .flatten()
                    .min_by_key(|(_, __rank)| *__rank)
                    .map(|(__v, _)| __v)
            }
        }

        impl #impl_generics #ident #ty_generics #where_clause {
            /// Gather the candidate extension pieces from `ext` in a single pass
            /// and return the variant whose value was inserted most recently.
            ///
            /// Generated by `#[derive(FromExtensions)]`. Each variant names one
            /// candidate type, lookup uses the same rule as `Extensions::get_ref`
            /// (newest-wins, walks wrappers and the parent chain). When several
            /// candidates are present the one with the lowest traversal rank
            /// (newest) wins, returns `None` if none are present. If two variants
            /// name the same type they resolve to the same entry (equal rank), the
            /// tie is broken deterministically in favour of the earlier-declared
            /// variant.
            pub fn from_extensions #fn_generics (
                ext: & #lt #root::extensions::Extensions,
            ) -> ::core::option::Option<Self> {
                let mut __targets = [::core::any::TypeId::of::<()>(); #n];
                <Self as #root::extensions::FromExtensionsGroup<#lt>>::from_ext_targets(
                    &mut __targets, 0,
                );
                let mut __out: [
                    ::core::option::Option<(& #lt #root::extensions::TypeErasedExtension, usize)>; #n
                ] = [::core::option::Option::None; #n];
                ext.get_many_erased(&__targets, &mut __out);
                <Self as #root::extensions::FromExtensionsGroup<#lt>>::from_ext_slots(&__out, 0)
            }
        }
    })
}

/// The lifetime tying the borrowed slots to `ext`: reuse the type's lifetime if
/// it has one, otherwise introduce a fresh one on the method (a type with only
/// `Arc<T>` pieces needs no lifetime of its own).
fn fn_lifetime(lifetime: Option<&Lifetime>) -> (TokenStream, TokenStream) {
    if let Some(lt) = lifetime {
        (quote!(), quote!(#lt))
    } else {
        let fresh = LifetimeParam::new(Lifetime::new("'__from_ext", Span::call_site()));
        (quote!(<#fresh>), quote!(#fresh))
    }
}

/// Clone `ty` with every lifetime argument replaced by the anonymous `'_`, so it
/// can be named in a const context (where `'a`/method lifetimes are forbidden) to
/// read a nested group's `TARGETS`.
fn anon_lifetimes(ty: &Type) -> Type {
    let mut ty = ty.clone();
    anon_lifetimes_in(&mut ty);
    ty
}

fn anon_lifetimes_in(ty: &mut Type) {
    if let Type::Path(type_path) = ty {
        for segment in &mut type_path.path.segments {
            if let PathArguments::AngleBracketed(args) = &mut segment.arguments {
                for arg in &mut args.args {
                    match arg {
                        GenericArgument::Lifetime(lt) => {
                            *lt = Lifetime::new("'_", lt.apostrophe);
                        }
                        GenericArgument::Type(inner) => anon_lifetimes_in(inner),
                        _ => {}
                    }
                }
            }
        }
    }
}

/// Build the downcast expression for a classified piece, binding the slot's
/// erased entry as `__e`.
fn downcast_expr(spec: &FieldSpec<'_>, root: &TokenStream) -> TokenStream {
    let inner = spec.inner;
    let method = match spec.kind {
        FieldKind::Ref => quote!(downcast_ref),
        FieldKind::Arc => quote!(cloned_downcast),
    };
    quote!(#root::extensions::TypeErasedExtension::#method::<#inner>(__e))
}

/// Classify a struct field: strip the mandatory `Option<…>`, then decide whether
/// it is a gathered piece (`&'a T`, `Arc<T>`, or their `(_, usize)` indexed
/// forms) or a nested `FromExtensions` group (any other path, e.g. an any-of
/// enum) to delegate to.
fn classify_field(ty: &Type) -> syn::Result<FieldShape<'_>> {
    let inner = option_inner(ty).ok_or_else(|| unsupported_field(ty))?;
    if index_tuple(inner).is_some() || matches!(inner, Type::Reference(_)) || is_arc(inner) {
        return Ok(FieldShape::Piece(classify_value(inner)?));
    }
    if matches!(inner, Type::Path(_)) {
        return Ok(FieldShape::Nested(inner));
    }
    Err(unsupported_field(ty))
}

/// Whether `ty` is `Arc<…>` (matched by the last path segment, like the rest of
/// the classifier).
fn is_arc(ty: &Type) -> bool {
    matches!(ty, Type::Path(p) if p.path.segments.last().is_some_and(|s| s.ident == "Arc"))
}

/// Classify a value piece: `&'a T`, `Arc<T>`, or their `(value, usize)` indexed
/// tuple form. Returns the [`FieldKind`], the inner `T`, and whether the rank is
/// captured.
fn classify_value(ty: &Type) -> syn::Result<FieldSpec<'_>> {
    if let Some((value_ty, index_ty)) = index_tuple(ty) {
        if !is_usize(index_ty) {
            return Err(syn::Error::new_spanned(
                index_ty,
                "the second element of a `#[derive(FromExtensions)]` indexed piece must be `usize`",
            ));
        }
        let (kind, inner) = classify_inner(value_ty)?;
        return Ok(FieldSpec {
            kind,
            inner,
            indexed: true,
        });
    }
    let (kind, inner) = classify_inner(ty)?;
    Ok(FieldSpec {
        kind,
        inner,
        indexed: false,
    })
}

/// Classify `&'a T` (borrowed) or `Arc<T>` (owned Arc clone) and extract `T`.
fn classify_inner(ty: &Type) -> syn::Result<(FieldKind, &Type)> {
    match ty {
        Type::Reference(reference) if reference.mutability.is_some() => {
            Err(syn::Error::new_spanned(
                reference,
                "`#[derive(FromExtensions)]` can only hand out shared references; \
             use `&'a T`, not `&mut`",
            ))
        }
        Type::Reference(reference) => Ok((FieldKind::Ref, &reference.elem)),
        Type::Path(type_path) => {
            let segment = type_path
                .path
                .segments
                .last()
                .ok_or_else(|| unsupported_field(ty))?;
            if segment.ident != "Arc" {
                return Err(unsupported_field(ty));
            }
            let PathArguments::AngleBracketed(args) = &segment.arguments else {
                return Err(unsupported_field(ty));
            };
            if args.args.len() != 1 {
                return Err(syn::Error::new_spanned(
                    segment,
                    "`#[derive(FromExtensions)]` supports only `Arc<T>` (no custom allocator)",
                ));
            }
            let GenericArgument::Type(inner) = &args.args[0] else {
                return Err(unsupported_field(ty));
            };
            Ok((FieldKind::Arc, inner))
        }
        _ => Err(unsupported_field(ty)),
    }
}

fn unsupported_field(ty: &Type) -> syn::Error {
    syn::Error::new_spanned(
        ty,
        "every `#[derive(FromExtensions)]` piece must be `&'a T` / `Arc<T>` (struct fields \
         wrapped in `Option<…>`), optionally paired with the entry rank as `(_, usize)`",
    )
}

/// Extract `T` from `Option<T>`.
fn option_inner(ty: &Type) -> Option<&Type> {
    let Type::Path(type_path) = ty else {
        return None;
    };
    let segment = type_path.path.segments.last()?;
    if segment.ident != "Option" {
        return None;
    }
    let PathArguments::AngleBracketed(args) = &segment.arguments else {
        return None;
    };
    let GenericArgument::Type(inner) = args.args.first()? else {
        return None;
    };
    Some(inner)
}

/// Match a 2-element tuple
fn index_tuple(ty: &Type) -> Option<(&Type, &Type)> {
    let Type::Tuple(tuple) = ty else {
        return None;
    };
    if tuple.elems.len() != 2 {
        return None;
    }
    Some((&tuple.elems[0], &tuple.elems[1]))
}

fn is_usize(ty: &Type) -> bool {
    matches!(ty, Type::Path(p) if p.path.is_ident("usize"))
}

#[cfg(test)]
mod tests {
    use super::*;
    use syn::{DeriveInput, parse_quote};

    fn expand_err(input: DeriveInput) -> String {
        expand(input)
            .expect_err("expected expansion to fail")
            .to_string()
    }

    fn expand_ok(input: DeriveInput) -> String {
        expand(input)
            .expect("expected expansion to succeed")
            .to_string()
    }

    #[test]
    fn rejects_type_parameters() {
        let e = expand_err(parse_quote! {
            struct Bad<T> { x: Option<T> }
        });
        assert!(e.contains("type parameters"), "{e}");
    }

    #[test]
    fn rejects_const_parameters() {
        let e = expand_err(parse_quote! {
            struct Bad<const N: usize> { x: Option<&'static u8> }
        });
        assert!(e.contains("const parameters"), "{e}");
    }

    #[test]
    fn rejects_multiple_lifetimes() {
        let e = expand_err(parse_quote! {
            struct Bad<'a, 'b> { x: Option<&'a u8>, y: Option<&'b u8> }
        });
        assert!(e.contains("at most one lifetime"), "{e}");
    }

    #[test]
    fn rejects_union() {
        let e = expand_err(parse_quote! {
            union Bad { x: u8 }
        });
        assert!(e.contains("structs and enums"), "{e}");
    }

    #[test]
    fn rejects_tuple_struct() {
        let e = expand_err(parse_quote! {
            struct Bad(u8);
        });
        assert!(e.contains("named fields"), "{e}");
    }

    #[test]
    fn rejects_empty_struct() {
        let e = expand_err(parse_quote! {
            struct Bad {}
        });
        assert!(e.contains("at least one field"), "{e}");
    }

    #[test]
    fn rejects_field_without_option() {
        let e = expand_err(parse_quote! {
            struct Bad<'a> { x: &'a u8 }
        });
        assert!(e.contains("piece must be"), "{e}");
    }

    #[test]
    fn rejects_mut_reference() {
        let e = expand_err(parse_quote! {
            struct Bad<'a> { x: Option<&'a mut u8> }
        });
        assert!(e.contains("shared references"), "{e}");
    }

    #[test]
    fn rejects_arc_with_allocator() {
        let e = expand_err(parse_quote! {
            struct Bad { x: Option<Arc<u8, MyAlloc>> }
        });
        assert!(e.contains("no custom allocator"), "{e}");
    }

    #[test]
    fn rejects_index_tuple_with_non_usize() {
        let e = expand_err(parse_quote! {
            struct Bad<'a> { x: Option<(&'a u8, u32)> }
        });
        assert!(e.contains("must be `usize`"), "{e}");
    }

    #[test]
    fn rejects_empty_enum() {
        let e = expand_err(parse_quote! {
            enum Bad {}
        });
        assert!(e.contains("at least one variant"), "{e}");
    }

    #[test]
    fn rejects_struct_variant() {
        let e = expand_err(parse_quote! {
            enum Bad<'a> { V { x: &'a u8 } }
        });
        assert!(e.contains("tuple variant"), "{e}");
    }

    #[test]
    fn rejects_unit_variant() {
        let e = expand_err(parse_quote! {
            enum Bad { V }
        });
        assert!(e.contains("tuple variant"), "{e}");
    }

    #[test]
    fn rejects_variant_with_two_fields() {
        let e = expand_err(parse_quote! {
            enum Bad<'a> { V(&'a u8, &'a u16) }
        });
        assert!(e.contains("exactly one value"), "{e}");
    }

    #[test]
    fn rejects_unsupported_struct_field_type() {
        let e = expand_err(parse_quote! {
            struct Bad { x: Option<[u8; 4]> }
        });
        assert!(e.contains("piece must be"), "{e}");
    }

    #[test]
    fn rejects_enum_variant_bare_value_type() {
        let e = expand_err(parse_quote! {
            enum Bad { V(u8) }
        });
        assert!(e.contains("piece must be"), "{e}");
    }

    #[test]
    fn rejects_enum_variant_array_value() {
        let e = expand_err(parse_quote! {
            enum Bad { V([u8; 2]) }
        });
        assert!(e.contains("piece must be"), "{e}");
    }

    #[test]
    fn struct_ref_arc_and_indexed_expand() {
        let out = expand_ok(parse_quote! {
            struct View<'a> {
                a: Option<&'a A>,
                b: Option<Arc<B>>,
                c: Option<(&'a C, usize)>,
            }
        });
        assert!(out.contains("fn from_extensions"), "{out}");
        assert!(out.contains("get_many_erased"), "{out}");
        assert!(out.contains("downcast_ref"), "{out}");
        assert!(out.contains("cloned_downcast"), "{out}");
    }

    #[test]
    fn all_arc_struct_needs_no_lifetime() {
        // No declared lifetime so we create one
        let out = expand_ok(parse_quote! {
            struct View { a: Option<Arc<A>> }
        });
        assert!(out.contains("'__from_ext"), "{out}");
    }

    #[test]
    fn enum_expands_group_impl_and_inherent() {
        let out = expand_ok(parse_quote! {
            enum Auth<'a> { A(&'a Server), B(&'a Verifier) }
        });
        assert!(out.contains("FromExtensionsGroup"), "{out}");
        assert!(out.contains("from_ext_targets"), "{out}");
        assert!(out.contains("from_ext_slots"), "{out}");
        assert!(out.contains("min_by_key"), "{out}");
        assert!(out.contains("fn from_extensions"), "{out}");
    }

    #[test]
    fn struct_nested_group_folds_into_single_pass() {
        let out = expand_ok(parse_quote! {
            struct Cfg<'a> {
                toggle: Option<&'a T>,
                either: Option<Either<'a>>,
            }
        });
        // the nested group is gathered through the trait (one shared pass),
        // not by calling its own `from_extensions`.
        assert!(out.contains("from_ext_targets"), "{out}");
        assert!(out.contains("from_ext_slots"), "{out}");
        assert!(out.contains("get_many_erased"), "{out}");
        // its width is read via the anonymous lifetime for the const buffer size.
        assert!(out.contains("'_"), "{out}");
    }
}