hopper-derive 0.2.0

Proc macro layer for Hopper. Generates account layouts, typed context accessors, instruction dispatchers, schema metadata, and CPI builders.
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
//! `#[hopper_state]`. contract-aware zero-copy layout codegen.
//!
//! The canonical proc-macro path must participate in the same runtime,
//! schema, and receipt pipeline as hand-written Hopper layouts. This macro
//! therefore emits more than a `SegmentMap`: it generates field metadata,
//! layout fingerprints, typed load helpers, and schema export hooks.

use proc_macro2::TokenStream;
use quote::{format_ident, quote, ToTokens};
use sha2::{Digest, Sha256};
use syn::{parse::Parser, parse2, Attribute, Field, Fields, ItemStruct, LitInt, LitStr, Result};

#[derive(Clone)]
struct StateOptions {
    disc: Option<u8>,
    version: u8,
    /// Audit innovation I5 (hybrid serialization). When set, the
    /// layout emits tail-access helpers that read/write a
    /// length-prefixed dynamic payload at offset
    /// `HEADER_LEN + BODY_SIZE`.
    dynamic_tail: Option<syn::Type>,
    /// Canonical compact-tail schema descriptor. `dynamic_account` supplies
    /// this automatically; hand-written `dynamic_tail = T` layouts at least
    /// fingerprint the tail type token.
    dynamic_tail_schema: Option<String>,
}

impl Default for StateOptions {
    fn default() -> Self {
        Self {
            disc: None,
            version: 1,
            dynamic_tail: None,
            dynamic_tail_schema: None,
        }
    }
}

/// Per-field metadata extracted from Hopper-specific attributes.
///
/// These attributes (`#[role = "..."]` and `#[invariant = "..."]`) are
/// consumed by `#[hopper::state]` itself. they are stripped from the
/// re-emitted struct so the compiler never sees them. This is how the
/// layout ties individual fields to schema intents and to the named
/// invariants declared on an associated `#[hopper::error]` enum.
///
/// ## Design notes
///
/// Hopper gives authored layouts a way to say "this field is a balance" or
/// "this field is guarded by
/// `balance_nonzero`" at declaration time. information that then
/// flows directly into the manifest, the receipt narrative, and the
/// Codama/Python client generators.
#[derive(Default, Clone)]
struct FieldMeta {
    /// Semantic role string (e.g. `"balance"`, `"authority"`). Empty
    /// string means no role was declared. the field falls back to
    /// `FieldIntent::Custom`.
    role: String,
    /// Invariant name this field is guarded by (e.g. `"balance_nonzero"`).
    /// Empty when the field has no declared invariant.
    invariant: String,
}

pub fn expand(attr: TokenStream, item: TokenStream) -> Result<TokenStream> {
    let options = parse_state_options(attr)?;
    let dynamic_tail = options.dynamic_tail.clone();
    let mut input: ItemStruct = parse2(item)?;
    let name = input.ident.clone();
    let vis = input.vis.clone();

    if !has_repr_c(&input.attrs) {
        return Err(syn::Error::new_spanned(
            &input,
            "hopper_state requires #[repr(C)] so segment offsets and typed loads stay stable",
        ));
    }

    // State overlays are plain wire values. The authored struct must be
    // `Clone + Copy` so the generated Pod impls satisfy Hopper's canonical
    // Pod contract. Do not inject derives here: rustc may apply an outer
    // `#[derive(Clone, Copy)]` separately from this attribute macro, and
    // emitting a second derive produces duplicate trait impls. Instead, keep
    // the user's derives intact and emit an explicit proof below so missing
    // derives fail at the layout declaration site.

    // Verify we have named fields before we start mutating. We can't
    // iterate `&input.fields` here because we'll need `&mut` access
    // below to strip the hopper-internal attributes from the struct
    // we re-emit. Do a bare shape-check first; actual iteration
    // happens against `input.fields` directly.
    if !matches!(input.fields, Fields::Named(_)) {
        return Err(syn::Error::new_spanned(
            &input,
            "hopper_state requires a struct with named fields",
        ));
    }

    let mut segment_entries = Vec::new();
    let mut module_items = Vec::new();
    let mut inherent_items = Vec::new();
    let mut field_name_literals = Vec::new();
    let mut field_type_literals = Vec::new();
    let mut field_types = Vec::new();
    let mut field_intent_tokens: Vec<TokenStream> = Vec::new();
    let mut field_role_literals: Vec<LitStr> = Vec::new();
    let mut field_invariant_literals: Vec<LitStr> = Vec::new();
    let mut running_offset = quote! { 0u32 };

    let struct_name_upper = to_screaming_snake(&name.to_string());

    // First pass: extract hopper-internal attributes from each field
    // and strip them from the re-emitted struct. The consumed attrs
    // (`#[role = "..."]`, `#[invariant = "..."]`) are bare identifiers
    // without a `::` path prefix, so leaving them in place would cause
    // rustc to reject the re-emitted struct with "unknown attribute".
    let field_metas: Vec<FieldMeta> = match &mut input.fields {
        Fields::Named(named) => {
            let mut out = Vec::with_capacity(named.named.len());
            for field in named.named.iter_mut() {
                let meta = parse_field_meta(field)?;
                strip_hopper_field_attrs(field);
                out.push(meta);
            }
            out
        }
        _ => unreachable!("checked above"),
    };

    // Borrow the (now-cleaned) named fields for the main codegen walk.
    let fields = match &input.fields {
        Fields::Named(f) => &f.named,
        _ => unreachable!("checked above"),
    };

    for (field, meta) in fields.iter().zip(field_metas.iter()) {
        let field_name = field.ident.as_ref().unwrap();
        let field_name_str = field_name.to_string();
        let field_ty = &field.ty;
        let field_name_upper = to_screaming_snake(&field_name_str);
        let current_offset = running_offset.clone();

        field_name_literals.push(syn::LitStr::new(&field_name_str, field_name.span()));
        field_type_literals.push(syn::LitStr::new(
            &field_ty.to_token_stream().to_string().replace(' ', ""),
            field_name.span(),
        ));
        field_types.push(field_ty.clone());
        field_intent_tokens.push(role_to_intent_tokens(&meta.role, field_name.span())?);
        field_role_literals.push(LitStr::new(&meta.role, field_name.span()));
        field_invariant_literals.push(LitStr::new(&meta.invariant, field_name.span()));

        segment_entries.push(quote! {
            ::hopper::hopper_core::segment_map::StaticSegment::new(
                #field_name_str,
                #current_offset,
                core::mem::size_of::<#field_ty>() as u32,
            )
        });

        let const_name = format_ident!("{}_{}_OFFSET", struct_name_upper, field_name_upper);
        let const_abs_name = format_ident!("{}_{}_ABS_OFFSET", struct_name_upper, field_name_upper);
        let const_size_name = format_ident!("{}_{}_SIZE", struct_name_upper, field_name_upper);
        let const_type_name = format_ident!("{}_{}_TYPE", struct_name_upper, field_name_upper);
        let assoc_offset_name = format_ident!("{}_OFFSET", field_name_upper);
        let assoc_abs_offset_name = format_ident!("{}_ABS_OFFSET", field_name_upper);
        let assoc_size_name = format_ident!("{}_SIZE", field_name_upper);

        // Body-relative offset + account-absolute offset for each field.
        //
        // `*_OFFSET` is the field's offset within the layout body (0-indexed
        // from the start of the layout's `#[repr(C)]` struct). That's the
        // value `ctx.segment_mut::<T>(..)` historically expected with the
        // 16-byte header pre-added.
        //
        // `*_ABS_OFFSET` folds in `HEADER_LEN` so the caller can pass it
        // directly to any segment accessor that expects a buffer-absolute
        // offset. avoiding `HEADER_LEN + Vault::AUTHORITY_OFFSET`
        // boilerplate at every call site.
        module_items.push(quote! {
            #vis const #const_name: u32 = #current_offset;
            #vis const #const_abs_name: u32 =
                ::hopper::hopper_core::account::HEADER_LEN as u32 + #current_offset;
            #vis const #const_size_name: u32 = core::mem::size_of::<#field_ty>() as u32;
            #vis type #const_type_name = #field_ty;
        });

        inherent_items.push(quote! {
            #vis const #assoc_offset_name: u32 = #current_offset;
            #vis const #assoc_abs_offset_name: u32 =
                ::hopper::hopper_core::account::HEADER_LEN as u32 + #current_offset;
            #vis const #assoc_size_name: u32 = core::mem::size_of::<#field_ty>() as u32;
        });

        running_offset = quote! {
            #current_offset + core::mem::size_of::<#field_ty>() as u32
        };
    }

    let body_size = running_offset.clone();
    let version = options.version;
    let dynamic_tail_fingerprint = options
        .dynamic_tail_schema
        .as_deref()
        .map(str::to_owned)
        .or_else(|| {
            dynamic_tail
                .as_ref()
                .map(|ty| format!("type:{}", ty.to_token_stream().to_string().replace(' ', "")))
        });
    let layout_id = layout_id_bytes(&name, version, fields, dynamic_tail_fingerprint.as_deref());
    // Default discriminator: first byte of the layout_id fingerprint.
    // If that byte is zero (1-in-256 chance given SHA-256 uniformity)
    // we fall through to the first non-zero byte so the compile-time
    // "disc != 0" fence never fires spuriously. This mirrors Quasar's
    // `validate_discriminator_not_zero()` but in a forgiving form:
    // the user never needs to set `disc = ...` explicitly unless they
    // want a specific wire value.
    let disc = options.disc.unwrap_or_else(|| {
        for byte in layout_id.iter() {
            if *byte != 0 {
                return *byte;
            }
        }
        // All-zero SHA-256 first 8 bytes is astronomically improbable;
        // if we ever hit it the 1-byte fallback is still non-zero.
        1u8
    });
    let layout_id_tokens = byte_array_literal(&layout_id);
    let field_count = field_name_literals.len();

    // Unique per-layout static that pins LAYOUT_ID bytes into
    // `.rodata`. `hopper verify` searches the compiled binary for
    // this exact 8-byte sequence to prove manifest/binary agreement.
    let layout_id_hex = layout_id
        .iter()
        .map(|byte| format!("{:02X}", byte))
        .collect::<String>();
    let layout_id_anchor_ident = format_ident!(
        "__HOPPER_LAYOUT_ID_ANCHOR_{}_{}",
        struct_name_upper,
        layout_id_hex
    );

    // ── Audit I5: hybrid-serialization tail helpers ──────────────────
    //
    // When the user writes `#[hopper::state(dynamic_tail = MyTail)]`,
    // emit `tail_len`, `tail_read`, `tail_write`, and a
    // `TAIL_PREFIX_OFFSET` constant on the layout's inherent impl.
    // The offset points to the `u32 LE` length prefix that precedes
    // the dynamic payload. exactly the position defined in
    // `crates/hopper-runtime/src/tail.rs`.
    //
    // Layouts without `dynamic_tail` emit an empty token stream and
    // pay zero cost: the fast path stays strictly zero-copy.
    let dynamic_tail_methods = if let Some(tail_ty) = &dynamic_tail {
        quote! {
            /// This layout opts in to the Hopper hybrid-serialization
            /// tail (audit innovation I5). Fixed body remains zero-copy;
            /// tail access is explicit via the `tail_*` helpers below.
            pub const HAS_DYNAMIC_TAIL: bool = true;

            /// Byte offset of the tail's `u32 LE` length prefix. The
            /// payload starts at `TAIL_PREFIX_OFFSET + 4`. Layouts
            /// without a dynamic tail do not emit this constant.
            pub const TAIL_PREFIX_OFFSET: usize = Self::LEN;

            /// Read the tail's length prefix.
            #[inline]
            pub fn tail_len(data: &[u8]) -> ::core::result::Result<
                u32,
                ::hopper::__runtime::ProgramError,
            > {
                ::hopper::__runtime::read_tail_len(data, Self::TAIL_PREFIX_OFFSET)
            }

            /// Decode and return the dynamic tail as `#tail_ty`.
            ///
            /// The full encoded length (from the u32 prefix) must be
            /// consumed exactly by `#tail_ty::decode`. trailing bytes
            /// indicate a malformed encoding and are rejected.
            #[inline]
            pub fn tail_read(data: &[u8]) -> ::core::result::Result<
                #tail_ty,
                ::hopper::__runtime::ProgramError,
            > {
                ::hopper::__runtime::read_tail::<#tail_ty>(data, Self::TAIL_PREFIX_OFFSET)
            }

            /// Encode `tail` in place and update the u32 length prefix.
            /// Returns the number of bytes written (excluding the prefix).
            /// Caller is responsible for ensuring the account has enough
            /// room (call `resize` first when growing).
            #[inline]
            pub fn tail_write(
                data: &mut [u8],
                tail: &#tail_ty,
            ) -> ::core::result::Result<
                usize,
                ::hopper::__runtime::ProgramError,
            > {
                ::hopper::__runtime::write_tail::<#tail_ty>(
                    data,
                    Self::TAIL_PREFIX_OFFSET,
                    tail,
                )
            }
        }
    } else {
        quote! {
            /// This layout has no dynamic tail. The constant is emitted
            /// unconditionally so callers can branch on it at compile time.
            pub const HAS_DYNAMIC_TAIL: bool = false;
        }
    };

    let state_param_inits = fields
        .iter()
        .map(state_field_param_init)
        .collect::<Result<Vec<_>>>()?;
    let constructor_params: Vec<TokenStream> = state_param_inits
        .iter()
        .map(|item| item.0.clone())
        .collect();
    let constructor_inits: Vec<TokenStream> = state_param_inits
        .iter()
        .map(|item| item.1.clone())
        .collect();
    let set_inner_assigns: Vec<TokenStream> = state_param_inits
        .iter()
        .map(|item| item.2.clone())
        .collect();
    let constructor_method = if options.dynamic_tail_schema.is_none() {
        quote! {
            #[inline(always)]
            #vis const fn new(#(#constructor_params),*) -> Self {
                Self {
                    #(#constructor_inits),*
                }
            }
        }
    } else {
        TokenStream::new()
    };

    let set_inner_method = quote! {
        #[inline(always)]
        #vis fn set_inner(
            &mut self,
            #(#constructor_params),*
        ) -> ::core::result::Result<(), ::hopper::__runtime::ProgramError> {
            #(#set_inner_assigns)*
            Ok(())
        }
    };

    let expanded = quote! {
        #input

        // ── Compile-time safety fence ──────────────────────────────────
        // Mirrors Quasar's alignment/padding/zero-discriminator asserts
        // plus Hopper's own size invariant. All four checks fire at
        // type-check time, so malformed layouts never reach link time.
        const _: () = {
            assert!(
                core::mem::align_of::<#name>() == 1,
                "hopper_state layouts must use alignment-1 field types such as WireU64 or TypedAddress",
            );
            assert!(
                core::mem::size_of::<#name>() == ((#body_size) as usize),
                "hopper_state layouts must be #[repr(C)] with no implicit padding",
            );
            assert!(
                core::mem::size_of::<#name>() > 0,
                "hopper_state layouts must have at least one field; zero-sized overlays project to dangling pointers",
            );
            assert!(
                #disc != 0,
                "hopper_state discriminator must be non-zero: a zero discriminator cannot be distinguished from an uninitialized account",
            );
        };

        #(#module_items)*

        impl #name {
            #constructor_method
            #set_inner_method

            #(#inherent_items)*

            pub const BODY_SIZE: usize = core::mem::size_of::<Self>();
            pub const LEN: usize = ::hopper::hopper_core::account::HEADER_LEN + Self::BODY_SIZE;
            pub const DISC: u8 = #disc;
            pub const VERSION: u8 = #version;
            pub const LAYOUT_ID: [u8; 8] = #layout_id_tokens;

            /// Bytes to allocate for a fresh account of this layout.
            ///
            /// Equal to [`Self::LEN`] (header + body) and spelled
            /// `INIT_SPACE` so Anchor-style `#[account(init, space = T::INIT_SPACE)]`
            /// ports over unchanged. Zero-copy layouts are always
            /// fixed-size, so this is a const the compiler can fold
            /// into the System Program allocate CPI at the call site.
            pub const INIT_SPACE: usize = Self::LEN;

            #[inline(always)]
            pub fn write_init_header(
                data: &mut [u8],
            ) -> ::core::result::Result<(), ::hopper::__runtime::ProgramError> {
                ::hopper::hopper_core::account::write_header(
                    data,
                    Self::DISC,
                    Self::VERSION,
                    &Self::LAYOUT_ID,
                )
            }

            #[inline(always)]
            pub fn overlay(
                data: &[u8],
            ) -> ::core::result::Result<&Self, ::hopper::__runtime::ProgramError> {
                ::hopper::hopper_core::account::pod_from_bytes::<Self>(data)
            }

            #[inline(always)]
            pub fn overlay_mut(
                data: &mut [u8],
            ) -> ::core::result::Result<&mut Self, ::hopper::__runtime::ProgramError> {
                ::hopper::hopper_core::account::pod_from_bytes_mut::<Self>(data)
            }

            #[inline(always)]
            pub fn load<'a>(
                account: &'a ::hopper::prelude::AccountView,
                program_id: &::hopper::prelude::Address,
            ) -> ::core::result::Result<
                ::hopper::__runtime::Ref<'a, Self>,
                ::hopper::__runtime::ProgramError,
            > {
                account.check_owned_by(program_id)?;
                account.load::<Self>()
            }

            #[inline(always)]
            pub fn load_mut<'a>(
                account: &'a ::hopper::prelude::AccountView,
                program_id: &::hopper::prelude::Address,
            ) -> ::core::result::Result<
                ::hopper::__runtime::RefMut<'a, Self>,
                ::hopper::__runtime::ProgramError,
            > {
                account.check_owned_by(program_id)?.check_writable()?;
                account.load_mut::<Self>()
            }

            #[inline(always)]
            #[deprecated(since = "0.2.0", note = "renamed to load_cross_program()")]
            pub fn load_foreign<'a>(
                account: &'a ::hopper::prelude::AccountView,
                expected_owner: &::hopper::prelude::Address,
            ) -> ::core::result::Result<
                ::hopper::__runtime::Ref<'a, Self>,
                ::hopper::__runtime::ProgramError,
            > {
                Self::load_cross_program(account, expected_owner)
            }

            #[inline(always)]
            pub fn load_cross_program<'a>(
                account: &'a ::hopper::prelude::AccountView,
                expected_owner: &::hopper::prelude::Address,
            ) -> ::core::result::Result<
                ::hopper::__runtime::Ref<'a, Self>,
                ::hopper::__runtime::ProgramError,
            > {
                account.check_owned_by(expected_owner)?;
                account.load::<Self>()
            }

            #dynamic_tail_methods

            // ── Field-level metadata registries ──────────────────────────
            //
            // These two tables are the structural twin of the `CODE_TABLE`
            // / `INVARIANT_TABLE` pair produced by `#[hopper::error]`.
            // Together they let off-chain tooling (SDK narrator, Codama
            // client generator, IDL exporter) answer three questions at
            // declaration time, without ever running the program:
            //
            //   1. "What does field `X` mean?". `FIELD_ROLES`
            //   2. "Which invariant guards field `X`?". `FIELD_INVARIANTS`
            //   3. "Which FieldIntent enum value does the runtime use?". //      the manifest's FieldDescriptor.intent column.
            //
            // No other Solana framework ships this. Anchor has no field
            // intent at all. Quasar tracks offsets but not semantics.
            // Pinocchio deliberately stays schema-free. Hopper's edge is
            // that the *same source declaration* flows into the manifest,
            // the Python client, the receipt narrative, and any future
            // lint pass that wants to say "this field was declared as a
            // balance but a non-financial invariant is guarding it."

            /// Declared semantic role per field, in struct declaration
            /// order. Empty string means no `#[role = "..."]` was given
            /// and the field falls back to `FieldIntent::Custom`.
            pub const FIELD_ROLES: &'static [(&'static str, &'static str)] = &[
                #( (#field_name_literals, #field_role_literals) ),*
            ];

            /// Declared invariant name per field, in struct declaration
            /// order. Empty string means no `#[invariant = "..."]` was
            /// given. Pair this with
            /// `<ErrorEnum>::INVARIANT_TABLE` to resolve the invariant
            /// name back to the error variant it raises. completing
            /// the field → invariant → error → receipt chain.
            pub const FIELD_INVARIANTS: &'static [(&'static str, &'static str)] = &[
                #( (#field_name_literals, #field_invariant_literals) ),*
            ];
        }

        // `#[hopper::state]` emits Pod impls, and Hopper's canonical Pod
        // contract is `Copy + Sized`. Keep the visible Rust spelling
        // conventional (`#[derive(Clone, Copy)]`) instead of silently
        // injecting a derive from inside the attribute macro; this avoids
        // duplicate trait impls when users follow the README pattern.
        #[doc(hidden)]
        const _: () = {
            struct __StateCopyProof<T: ::core::clone::Clone + ::core::marker::Copy>(
                ::core::marker::PhantomData<T>,
            );
            const _: __StateCopyProof<#name> =
                __StateCopyProof(::core::marker::PhantomData);
        };

        // Bytemuck-backed field-level Pod proof (Hopper Safety Audit
        // Must-Fix #4 / #5).
        //
        // The three `unsafe impl`s alone would be rubber stamps. the
        // compiler does not inspect fields when a bare `unsafe impl
        // bytemuck::Pod for T` is emitted. We therefore pair them with
        // a per-field `__FieldPodProof<T: bytemuck::Pod + Zeroable>`
        // instantiation. Each field type is forced through that bound;
        // a `bool`, `char`, reference, or non-`bytemuck::Pod` nested
        // struct fails the trait bound *on the field*, not at some
        // distant `segment_ref::<T>()` call site.
        #[doc(hidden)]
        const _: () = {
            struct __FieldPodProof<
                T: ::hopper::__runtime::__hopper_native::bytemuck::Pod
                    + ::hopper::__runtime::__hopper_native::bytemuck::Zeroable,
            >(::core::marker::PhantomData<T>);
            #(
                #[allow(dead_code)]
                const _: __FieldPodProof<#field_types> =
                    __FieldPodProof(::core::marker::PhantomData);
            )*
        };

        unsafe impl ::hopper::__runtime::__hopper_native::bytemuck::Zeroable for #name {}
        unsafe impl ::hopper::__runtime::__hopper_native::bytemuck::Pod for #name {}
        unsafe impl ::hopper::hopper_core::account::Pod for #name {}
        // Audit final-API Step 5 seal. `#[hopper::state]` stamps
        // the Hopper-authored marker so the `ZeroCopy` blanket
        // picks up the type. Bare `unsafe impl Pod` outside this
        // macro path is not automatically `ZeroCopy`.
        unsafe impl ::hopper::__runtime::__sealed::HopperZeroCopySealed for #name {}

        // Anchor the layout fingerprint in `.rodata` so it survives
        // dead-code elimination on SBF / LTO builds. `hopper verify`
        // scans the compiled `.so` for this exact byte sequence to
        // prove the binary matches the emitted manifest. Without
        // `#[used]` the SBF linker aggressively strips const bytes
        // that only appear inside inlined comparisons.
        #[used]
        #[doc(hidden)]
        #[no_mangle]
        pub static #layout_id_anchor_ident: [u8; 8] = #name::LAYOUT_ID;

        impl ::hopper::hopper_core::account::FixedLayout for #name {
            const SIZE: usize = core::mem::size_of::<Self>();
        }

        impl ::hopper::hopper_core::segment_map::SegmentMap for #name {
            const SEGMENTS: &'static [::hopper::hopper_core::segment_map::StaticSegment] = &[
                #(#segment_entries),*
            ];
        }

        impl ::hopper::hopper_core::field_map::FieldMap for #name {
            const FIELDS: &'static [::hopper::hopper_core::field_map::FieldInfo] = {
                const FIELD_COUNT: usize = #field_count;
                const NAMES: [&str; FIELD_COUNT] = [#(#field_name_literals),*];
                const SIZES: [usize; FIELD_COUNT] = [#(core::mem::size_of::<#field_types>()),*];
                const FIELDS: [::hopper::hopper_core::field_map::FieldInfo; FIELD_COUNT] = {
                    let mut result = [::hopper::hopper_core::field_map::FieldInfo::new("", 0, 0); FIELD_COUNT];
                    let mut offset = ::hopper::hopper_core::account::HEADER_LEN;
                    let mut index = 0;
                    while index < FIELD_COUNT {
                        result[index] = ::hopper::hopper_core::field_map::FieldInfo::new(
                            NAMES[index],
                            offset,
                            SIZES[index],
                        );
                        offset += SIZES[index];
                        index += 1;
                    }
                    result
                };
                &FIELDS
            };
        }

        impl ::hopper::hopper_runtime::LayoutContract for #name {
            const DISC: u8 = #name::DISC;
            const VERSION: u8 = #name::VERSION;
            const LAYOUT_ID: [u8; 8] = #name::LAYOUT_ID;
            const SIZE: usize = #name::LEN;
        }

        impl ::hopper::hopper_core::check::modifier::HopperLayout for #name {
            const DISC: u8 = #name::DISC;
            const VERSION: u8 = #name::VERSION;
            const LAYOUT_ID: [u8; 8] = #name::LAYOUT_ID;
            const LEN_WITH_HEADER: usize = #name::LEN;
        }

        impl ::hopper::hopper_schema::SchemaExport for #name {
            fn layout_manifest() -> ::hopper::hopper_schema::LayoutManifest {
                const FIELD_COUNT: usize = #field_count;
                const NAMES: [&str; FIELD_COUNT] = [#(#field_name_literals),*];
                const TYPES: [&str; FIELD_COUNT] = [#(#field_type_literals),*];
                const SIZES: [u16; FIELD_COUNT] = [#(core::mem::size_of::<#field_types>() as u16),*];
                // Per-field declared intents, parsed from `#[role = "..."]`
                // field attributes at proc-macro expansion time. Fields
                // without a role attribute default to `Custom`. their
                // position in this table still keeps the slice
                // `FIELD_COUNT`-sized so downstream code never needs a
                // branch on presence.
                const INTENTS: [::hopper::hopper_schema::FieldIntent; FIELD_COUNT] = [
                    #(#field_intent_tokens),*
                ];
                const FIELDS: [::hopper::hopper_schema::FieldDescriptor; FIELD_COUNT] = {
                    let mut result = [::hopper::hopper_schema::FieldDescriptor {
                        name: "",
                        canonical_type: "",
                        size: 0,
                        offset: 0,
                        intent: ::hopper::hopper_schema::FieldIntent::Custom,
                    }; FIELD_COUNT];
                    let mut offset = ::hopper::hopper_core::account::HEADER_LEN as u16;
                    let mut index = 0;
                    while index < FIELD_COUNT {
                        result[index] = ::hopper::hopper_schema::FieldDescriptor {
                            name: NAMES[index],
                            canonical_type: TYPES[index],
                            size: SIZES[index],
                            offset,
                            intent: INTENTS[index],
                        };
                        offset += SIZES[index];
                        index += 1;
                    }
                    result
                };

                ::hopper::hopper_schema::LayoutManifest {
                    name: stringify!(#name),
                    version: #name::VERSION,
                    disc: #name::DISC,
                    layout_id: #name::LAYOUT_ID,
                    total_size: #name::LEN,
                    field_count: FIELD_COUNT,
                    fields: &FIELDS,
                }
            }
        }
    };

    Ok(expanded)
}

/// Consume hopper-internal attributes (`#[role = "..."]`,
/// `#[invariant = "..."]`) from a single field and return the
/// extracted metadata. Attributes that fail to parse raise a
/// compile error pointing at the offending span.
///
/// Accepted forms:
///
/// ```ignore
/// #[role = "balance"]
/// #[role(balance)]          // path form, also accepted
/// #[invariant = "balance_nonzero"]
/// ```
fn parse_field_meta(field: &Field) -> Result<FieldMeta> {
    let mut meta = FieldMeta::default();
    for attr in &field.attrs {
        if attr.path().is_ident("role") {
            // Accept either `#[role = "balance"]` or `#[role(balance)]`.
            // The former uses `NameValue` meta; the latter uses `List`.
            // Both are equivalent at the attribute level. we just want
            // the string value.
            if let Ok(nv) = attr.meta.require_name_value() {
                if let syn::Expr::Lit(syn::ExprLit {
                    lit: syn::Lit::Str(s),
                    ..
                }) = &nv.value
                {
                    meta.role = s.value();
                    continue;
                }
                return Err(syn::Error::new_spanned(
                    &nv.value,
                    "#[role = \"...\"] expects a string literal (e.g. \"balance\", \"authority\")",
                ));
            }
            if let Ok(list) = attr.meta.require_list() {
                // Parse `#[role(balance)]` by pulling the single path ident.
                let tokens: TokenStream = list.tokens.clone();
                let parsed: syn::Ident = parse2(tokens).map_err(|_| {
                    syn::Error::new_spanned(
                        &list.tokens,
                        "#[role(...)] expects a single identifier (e.g. balance, authority)",
                    )
                })?;
                meta.role = parsed.to_string();
                continue;
            }
            return Err(syn::Error::new_spanned(
                attr,
                "unsupported #[role] form; use #[role = \"balance\"] or #[role(balance)]",
            ));
        }
        if attr.path().is_ident("invariant") {
            let nv = attr.meta.require_name_value().map_err(|_| {
                syn::Error::new_spanned(
                    attr,
                    "#[invariant] on a field expects name-value form: #[invariant = \"balance_nonzero\"]",
                )
            })?;
            if let syn::Expr::Lit(syn::ExprLit {
                lit: syn::Lit::Str(s),
                ..
            }) = &nv.value
            {
                meta.invariant = s.value();
                continue;
            }
            return Err(syn::Error::new_spanned(
                &nv.value,
                "#[invariant = \"...\"] expects a string literal (the invariant name)",
            ));
        }
    }
    Ok(meta)
}

/// Remove hopper-internal attributes from a field before the struct is
/// re-emitted. Leaving bare `#[role = "..."]` / `#[invariant = "..."]`
/// in place would cause rustc to reject the struct because those
/// attribute names are not registered with the compiler.
fn strip_hopper_field_attrs(field: &mut Field) {
    field
        .attrs
        .retain(|a| !a.path().is_ident("role") && !a.path().is_ident("invariant"));
}

/// Map a role string to a `FieldIntent` variant path token stream.
///
/// The role string is matched case-insensitively. An empty string
/// resolves to `FieldIntent::Custom` so fields without a declared
/// role still emit a well-formed descriptor. An unknown role raises
/// a compile error listing the full accepted vocabulary. this is
/// the cheapest place to catch typos (`"authorty"`, `"ballance"`)
/// that would otherwise silently degrade to `Custom`.
fn role_to_intent_tokens(role: &str, span: proc_macro2::Span) -> Result<TokenStream> {
    let normalized = role.to_ascii_lowercase();
    let variant = match normalized.as_str() {
        "" => "Custom",
        "balance" => "Balance",
        "authority" => "Authority",
        "timestamp" => "Timestamp",
        "counter" => "Counter",
        "index" => "Index",
        "basis_points" | "basispoints" | "bps" => "BasisPoints",
        "flag" | "bool" => "Flag",
        "address" | "pubkey" => "Address",
        "hash" | "fingerprint" => "Hash",
        "pda_seed" | "pdaseed" | "seed" => "PDASeed",
        "version" => "Version",
        "bump" => "Bump",
        "nonce" => "Nonce",
        "supply" | "total_supply" => "Supply",
        "limit" | "cap" | "ceiling" => "Limit",
        "threshold" => "Threshold",
        "owner" => "Owner",
        "delegate" => "Delegate",
        "status" | "state" | "lifecycle" => "Status",
        "custom" => "Custom",
        other => {
            return Err(syn::Error::new(
                span,
                format!(
                    "unknown #[role = \"{}\"]. accepted: balance, authority, timestamp, \
                     counter, index, basis_points, flag, address, hash, pda_seed, version, \
                     bump, nonce, supply, limit, threshold, owner, delegate, status, custom",
                    other
                ),
            ));
        }
    };
    let ident = syn::Ident::new(variant, span);
    Ok(quote! { ::hopper::hopper_schema::FieldIntent::#ident })
}

fn state_field_param_init(field: &Field) -> Result<(TokenStream, TokenStream, TokenStream)> {
    let field_name = field
        .ident
        .as_ref()
        .ok_or_else(|| syn::Error::new_spanned(field, "hopper_state requires named fields"))?;
    let field_ty = &field.ty;
    let (param_ty, init_expr) = if let Some(native_ty) = native_wire_param_type(field_ty) {
        (native_ty, quote! { <#field_ty>::new(#field_name) })
    } else {
        (quote! { #field_ty }, quote! { #field_name })
    };

    Ok((
        quote! { #field_name: #param_ty },
        quote! { #field_name: #init_expr },
        quote! { self.#field_name = #init_expr; },
    ))
}

fn native_wire_param_type(ty: &syn::Type) -> Option<TokenStream> {
    let syn::Type::Path(type_path) = ty else {
        return None;
    };
    let ident = type_path.path.segments.last()?.ident.to_string();
    let native = match ident.as_str() {
        "WireBool" => quote! { bool },
        "WireU16" => quote! { u16 },
        "WireU32" => quote! { u32 },
        "WireU64" => quote! { u64 },
        "WireU128" => quote! { u128 },
        "WireI16" => quote! { i16 },
        "WireI32" => quote! { i32 },
        "WireI64" => quote! { i64 },
        "WireI128" => quote! { i128 },
        _ => return None,
    };
    Some(native)
}

fn parse_state_options(attr: TokenStream) -> Result<StateOptions> {
    if attr.is_empty() {
        return Ok(StateOptions::default());
    }

    let mut options = StateOptions::default();
    let parser = syn::meta::parser(|meta| {
        if meta.path.is_ident("disc") || meta.path.is_ident("discriminator") {
            let value: LitInt = meta.value()?.parse()?;
            options.disc = Some(value.base10_parse()?);
            return Ok(());
        }
        if meta.path.is_ident("version") {
            let value: LitInt = meta.value()?.parse()?;
            options.version = value.base10_parse()?;
            return Ok(());
        }
        if meta.path.is_ident("dynamic_tail") {
            let ty: syn::Type = meta.value()?.parse()?;
            options.dynamic_tail = Some(ty);
            return Ok(());
        }
        if meta.path.is_ident("dynamic_tail_schema") || meta.path.is_ident("tail_schema") {
            let value: LitStr = meta.value()?.parse()?;
            options.dynamic_tail_schema = Some(value.value());
            return Ok(());
        }
        Err(meta.error("unsupported hopper_state option; expected `disc = N`, `discriminator = N`, `version = N`, `dynamic_tail = T`, or `dynamic_tail_schema = \"...\"`"))
    });

    parser.parse2(attr)?;
    Ok(options)
}

fn has_repr_c(attrs: &[Attribute]) -> bool {
    attrs.iter().any(|attr| {
        if !attr.path().is_ident("repr") {
            return false;
        }

        let mut has_c = false;
        let _ = attr.parse_nested_meta(|meta| {
            if meta.path.is_ident("C") {
                has_c = true;
            }
            Ok(())
        });
        has_c
    })
}

/// Build the 8-byte wire fingerprint for a layout.
///
/// The Hopper Safety Audit flagged the pre-fix algorithm. hashing
/// raw Rust token strings. as source-spelling-dependent and therefore
/// unsuitable as a long-term ABI identity primitive. A rename of
/// `foo::bar::WireU64` to `crate::WireU64`, or a swap of the generic
/// parameter on `TypedAddress<Authority>` → `TypedAddress<Token>`,
/// changed the fingerprint even though the wire layout was identical.
///
/// The audit-compliant algorithm this function implements normalizes
/// each field's type to a **canonical wire stem**:
///
/// - `Type::Path`. the last `::`-separated path segment only
///   (`foo::bar::WireU64` → `WireU64`), with generic parameters
///   stripped (`TypedAddress<Authority>` → `TypedAddress`). This
///   makes path re-exports and phantom-only generic changes ABI-
///   invisible, which they should be.
/// - `Type::Array`. `arr_<elem_stem>_<len>` so `[u8; 32]` becomes
///   `arr_u8_32`. stable against spelling variants.
/// - Anything else. fall back to the normalized token string so a
///   non-path type still produces a deterministic value.
///
/// The canonical descriptor format then feeds into SHA-256:
///
/// ```text
/// hopper:wire:v2|S:<StructName>|V:<Version>|
///   f0:<field_name>:<wire_stem>|f1:...|...|tail:<schema-or-type>
/// ```
///
/// Dynamic-tail layouts include either the precise compact-tail schema supplied
/// by `#[hopper::dynamic_account]` or the explicit `dynamic_tail` type token.
/// Fixed-only layouts omit the tail component.
///
/// `hopper:wire:v2` is the fingerprint-algorithm version marker. If
/// the descriptor format itself ever changes, bump this tag and the
/// old fingerprints stay distinguishable.
fn layout_id_bytes(
    name: &syn::Ident,
    version: u8,
    fields: &syn::punctuated::Punctuated<syn::Field, syn::token::Comma>,
    dynamic_tail_fingerprint: Option<&str>,
) -> [u8; 8] {
    let mut input = format!("hopper:wire:v2|S:{}|V:{}", name, version);
    for (idx, field) in fields.iter().enumerate() {
        let field_name = field.ident.as_ref().expect("named fields only");
        let stem = canonical_wire_stem(&field.ty);
        input.push_str(&format!("|f{}:{}:{}", idx, field_name, stem));
    }
    if let Some(tail) = dynamic_tail_fingerprint {
        input.push_str("|tail:");
        input.push_str(tail);
    }

    let digest = Sha256::digest(input.as_bytes());
    let mut layout_id = [0u8; 8];
    layout_id.copy_from_slice(&digest[..8]);
    layout_id
}

/// Normalize a Rust type token to a canonical wire stem.
///
/// See [`layout_id_bytes`] for the full contract. The normalization
/// is deliberately lossy on purely-cosmetic differences (paths,
/// phantom generics) and lossless on anything that actually shifts
/// the wire shape.
fn canonical_wire_stem(ty: &syn::Type) -> String {
    match ty {
        syn::Type::Path(type_path) => {
            if let Some(last) = type_path.path.segments.last() {
                // `TypedAddress<Authority>` → `TypedAddress`.
                // Phantom-only generic parameters shouldn't be part
                // of the wire ABI fingerprint because they don't
                // affect the byte layout.
                last.ident.to_string()
            } else {
                "unknown_path".to_string()
            }
        }
        syn::Type::Array(arr) => {
            let elem = canonical_wire_stem(&arr.elem);
            // `[u8; 32]` → `arr_u8_32`. Normalize so `[u8; 32]`,
            // `[u8; 32usize]`, and `[u8 ; 32]` all hash the same:
            // strip whitespace, then strip any integer-type suffix
            // (`u8`, `u16`, …, `usize`, `i8`, …) that a literal may
            // carry. We only touch the trailing non-digit tail so an
            // expression-shaped length like `N + 1` stays intact.
            let raw = arr
                .len
                .to_token_stream()
                .to_string()
                .replace(char::is_whitespace, "");
            let canonical_len = strip_int_literal_suffix(&raw);
            format!("arr_{}_{}", elem, canonical_len)
        }
        syn::Type::Tuple(tup) if tup.elems.is_empty() => "unit".to_string(),
        // Fallback: deterministic whitespace-stripped token string.
        // Not ideal, but covers exotic types (references, slices,
        // bare function pointers) without producing a collision.
        other => other
            .to_token_stream()
            .to_string()
            .replace(char::is_whitespace, ""),
    }
}

fn byte_array_literal(bytes: &[u8; 8]) -> TokenStream {
    let items = bytes.iter();
    quote! { [#(#items),*] }
}

/// Strip a Rust integer literal's type suffix if the literal is a
/// pure digit run, e.g. `"32usize"` → `"32"`, `"255u8"` → `"255"`,
/// `"0x10u32"` → `"0x10"`. Expression-shaped strings like `"N + 1"`
/// or `"SIZE"` are returned unchanged.
fn strip_int_literal_suffix(raw: &str) -> String {
    // Only strip if the prefix is a plausible integer literal: starts
    // with a digit, and the trailing non-digit tail is one of the
    // known integer-type suffixes.
    if !raw.chars().next().map_or(false, |c| c.is_ascii_digit()) {
        return raw.to_string();
    }
    const SUFFIXES: &[&str] = &[
        "usize", "isize", "u128", "i128", "u64", "i64", "u32", "i32", "u16", "i16", "u8", "i8",
    ];
    for suffix in SUFFIXES {
        if let Some(stripped) = raw.strip_suffix(suffix) {
            // Guard: the character immediately before the suffix must
            // be a digit (or `_` which Rust allows inside literals),
            // otherwise the suffix is actually the type identifier for
            // something like `N_u32` (an identifier).
            let before_ok = stripped
                .chars()
                .last()
                .map_or(false, |c| c.is_ascii_digit() || c == '_');
            if before_ok {
                return stripped.to_string();
            }
        }
    }
    raw.to_string()
}

// ══════════════════════════════════════════════════════════════════════
//  Canonical wire fingerprint. regression tests
// ══════════════════════════════════════════════════════════════════════
//
// These tests lock in the audit's "no source-spelling drift" invariant:
// path imports, full-qualification, and phantom-only generic parameters
// MUST produce the same wire fingerprint, because none of them change
// the byte layout of the serialized account.

#[cfg(test)]
mod fingerprint_tests {
    use super::*;
    use syn::parse_quote;

    fn fp(ty: syn::Type) -> String {
        canonical_wire_stem(&ty)
    }

    #[test]
    fn path_spelling_drift_does_not_change_stem() {
        assert_eq!(fp(parse_quote!(WireU64)), fp(parse_quote!(crate::WireU64)));
        assert_eq!(
            fp(parse_quote!(WireU64)),
            fp(parse_quote!(hopper_native::wire::WireU64)),
        );
        assert_eq!(fp(parse_quote!(WireU64)), fp(parse_quote!(self::WireU64)));
    }

    #[test]
    fn phantom_generic_parameters_are_stripped() {
        // `TypedAddress<Authority>` and `TypedAddress<Token>` have
        // identical byte layout; they must hash the same under the
        // post-audit canonical algorithm.
        assert_eq!(
            fp(parse_quote!(TypedAddress<Authority>)),
            fp(parse_quote!(TypedAddress<Token>)),
        );
        assert_eq!(
            fp(parse_quote!(TypedAddress<Authority>)),
            fp(parse_quote!(TypedAddress)),
        );
    }

    #[test]
    fn arrays_normalize_whitespace_and_usize_suffix() {
        assert_eq!(fp(parse_quote!([u8; 32])), fp(parse_quote!([u8; 32])));
        assert_eq!(fp(parse_quote!([u8; 32])), fp(parse_quote!([u8; 32usize])));
    }

    #[test]
    fn different_wire_types_still_distinguishable() {
        // Two genuinely different wire types must produce different
        // stems, otherwise we've over-normalized and lost ABI safety.
        assert_ne!(fp(parse_quote!(WireU64)), fp(parse_quote!(WireU32)));
        assert_ne!(fp(parse_quote!([u8; 32])), fp(parse_quote!([u8; 64])));
        assert_ne!(fp(parse_quote!(u8)), fp(parse_quote!(u16)));
    }

    #[test]
    fn unit_and_tuple_roll_up_cleanly() {
        assert_eq!(fp(parse_quote!(())), "unit");
    }
}

// ══════════════════════════════════════════════════════════════════════
//  Field-attribute parsing. regression tests (Task 18)
// ══════════════════════════════════════════════════════════════════════
//
// These tests lock in the per-field declarative surface introduced to
// close the "Anchor has no field intent" gap. Three properties matter:
//
//   1. Both `#[role = "..."]` and `#[role(...)]` work and agree.
//   2. Role strings map to the correct `FieldIntent` variant path, and
//      unknown roles produce a readable compile error rather than
//      silently degrading to `Custom`.
//   3. Stripping the hopper-internal attrs leaves the struct re-emit
//      valid. no `#[role]` leaks through to rustc.

#[cfg(test)]
mod field_attr_tests {
    use super::*;
    use syn::parse_quote;

    fn parse_first_field(f: syn::Field) -> FieldMeta {
        parse_field_meta(&f).expect("valid field meta")
    }

    #[test]
    fn name_value_role_parses() {
        let f: syn::Field = parse_quote!(
            #[role = "balance"]
            pub amount: WireU64
        );
        let m = parse_first_field(f);
        assert_eq!(m.role, "balance");
        assert_eq!(m.invariant, "");
    }

    #[test]
    fn path_form_role_parses() {
        let f: syn::Field = parse_quote!(
            #[role(authority)]
            pub owner: TypedAddress<Authority>
        );
        let m = parse_first_field(f);
        assert_eq!(m.role, "authority");
    }

    #[test]
    fn invariant_attr_parses() {
        let f: syn::Field = parse_quote!(
            #[invariant = "balance_nonzero"]
            pub amount: WireU64
        );
        let m = parse_first_field(f);
        assert_eq!(m.invariant, "balance_nonzero");
        assert_eq!(m.role, "");
    }

    #[test]
    fn both_attrs_parse_together() {
        let f: syn::Field = parse_quote!(
            #[role = "balance"]
            #[invariant = "balance_nonzero"]
            pub amount: WireU64
        );
        let m = parse_first_field(f);
        assert_eq!(m.role, "balance");
        assert_eq!(m.invariant, "balance_nonzero");
    }

    #[test]
    fn unknown_role_is_rejected() {
        // Typo in role name must produce a compile error, not a silent
        // downgrade to `Custom`. That's the whole point of making this
        // a closed vocabulary instead of a free-form string.
        let span = proc_macro2::Span::call_site();
        let err = role_to_intent_tokens("authorty", span).unwrap_err();
        let msg = err.to_string();
        assert!(
            msg.contains("unknown #[role = \"authorty\"]"),
            "expected helpful error, got: {msg}",
        );
        assert!(
            msg.contains("authority"),
            "error message should suggest valid vocabulary"
        );
    }

    #[test]
    fn empty_role_maps_to_custom() {
        // Fields without `#[role = ...]` must still emit a well-formed
        // FieldIntent (Custom) rather than a compile error. The parallel
        // arrays inside `SchemaExport::layout_manifest` rely on every
        // field slot being populated.
        let span = proc_macro2::Span::call_site();
        let tokens = role_to_intent_tokens("", span).expect("empty role is valid");
        let rendered = tokens.to_string().replace(' ', "");
        assert!(
            rendered.ends_with("FieldIntent::Custom"),
            "empty role should render FieldIntent::Custom, got: {rendered}",
        );
    }

    #[test]
    fn role_mapping_covers_full_vocabulary() {
        // Every FieldIntent variant (except the `Custom` fallback) must
        // be reachable through at least one accepted role spelling.
        // If this list drifts from `FieldIntent`, the test fails loudly
        // rather than letting partial coverage rot silently.
        let expected = [
            ("balance", "Balance"),
            ("authority", "Authority"),
            ("timestamp", "Timestamp"),
            ("counter", "Counter"),
            ("index", "Index"),
            ("basis_points", "BasisPoints"),
            ("flag", "Flag"),
            ("address", "Address"),
            ("hash", "Hash"),
            ("pda_seed", "PDASeed"),
            ("version", "Version"),
            ("bump", "Bump"),
            ("nonce", "Nonce"),
            ("supply", "Supply"),
            ("limit", "Limit"),
            ("threshold", "Threshold"),
            ("owner", "Owner"),
            ("delegate", "Delegate"),
            ("status", "Status"),
        ];
        let span = proc_macro2::Span::call_site();
        for (role, variant) in expected {
            let tokens = role_to_intent_tokens(role, span).unwrap_or_else(|e| {
                panic!("role `{role}` should map to FieldIntent::{variant}: {e}")
            });
            let rendered = tokens.to_string().replace(' ', "");
            assert!(
                rendered.ends_with(&format!("FieldIntent::{variant}")),
                "role `{role}` should map to FieldIntent::{variant}, got: {rendered}",
            );
        }
    }

    #[test]
    fn case_variants_and_aliases_resolve() {
        let span = proc_macro2::Span::call_site();
        // Case insensitivity: users should not be punished for
        // `#[role = "Balance"]` vs `#[role = "balance"]`.
        let a = role_to_intent_tokens("Balance", span).unwrap().to_string();
        let b = role_to_intent_tokens("balance", span).unwrap().to_string();
        assert_eq!(a, b);
        // Aliases: `bps` ≡ `basis_points`, `seed` ≡ `pda_seed`, etc.
        let c = role_to_intent_tokens("bps", span).unwrap().to_string();
        let d = role_to_intent_tokens("basis_points", span)
            .unwrap()
            .to_string();
        assert_eq!(c, d);
        let e = role_to_intent_tokens("seed", span).unwrap().to_string();
        let f = role_to_intent_tokens("pda_seed", span).unwrap().to_string();
        assert_eq!(e, f);
    }

    #[test]
    fn strip_removes_hopper_attrs_but_preserves_others() {
        let mut f: syn::Field = parse_quote!(
            #[role = "balance"]
            #[invariant = "balance_nonzero"]
            #[serde(skip)]
            pub amount: WireU64
        );
        strip_hopper_field_attrs(&mut f);
        // `#[serde(skip)]` must survive. anything else the user
        // stacked alongside our attrs is not ours to consume.
        assert_eq!(f.attrs.len(), 1);
        assert!(f.attrs[0].path().is_ident("serde"));
    }
}

fn to_screaming_snake(s: &str) -> String {
    let mut result = String::with_capacity(s.len() + 4);
    for (i, c) in s.chars().enumerate() {
        if c.is_uppercase() && i > 0 {
            result.push('_');
        }
        result.push(c.to_ascii_uppercase());
    }
    result
}