1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
//! ink! chain extension diagnostics.

use ink_analyzer_ir::ast::{AstNode, HasName};
use ink_analyzer_ir::meta::MetaValue;
use ink_analyzer_ir::syntax::TextRange;
use ink_analyzer_ir::{
    ast, ChainExtension, Extension, FromInkAttribute, FromSyntax, InkArg, InkArgKind,
    InkAttributeKind, IsInkTrait,
};
use std::collections::HashSet;

use super::{extension, utils};
use crate::analysis::snippets::{ERROR_CODE_PLAIN, ERROR_CODE_SNIPPET};
use crate::analysis::text_edit::TextEdit;
use crate::analysis::utils as analysis_utils;
use crate::{Action, ActionKind, Diagnostic, Severity};

const CHAIN_EXTENSION_SCOPE_NAME: &str = "chain extension";

/// Runs all ink! chain extension diagnostics.
///
/// The entry point for finding ink! chain extension semantic rules is the `chain_extension` module of the `ink_ir` crate.
///
/// Ref: <https://github.com/paritytech/ink/blob/v4.1.0/crates/ink/ir/src/ir/chain_extension.rs#L201-L211>.
///
/// Ref: <https://github.com/paritytech/ink/blob/v4.1.0/crates/ink/ir/src/ir/chain_extension.rs#L188-L197>.
pub fn diagnostics(results: &mut Vec<Diagnostic>, chain_extension: &ChainExtension) {
    // Runs generic diagnostics, see `utils::run_generic_diagnostics` doc.
    utils::run_generic_diagnostics(results, chain_extension);

    // Ensures that ink! chain extension is a `trait` item, see `utils::ensure_trait` doc.
    // Ref: <https://github.com/paritytech/ink/blob/v4.1.0/crates/ink/ir/src/ir/chain_extension.rs#L222>.
    if let Some(diagnostic) = utils::ensure_trait(chain_extension, CHAIN_EXTENSION_SCOPE_NAME) {
        results.push(diagnostic);
    }

    if let Some(trait_item) = chain_extension.trait_item() {
        // Ensures that ink! chain extension `trait` item satisfies all common invariants of trait-based ink! entities,
        // see `utils::ensure_trait_invariants` doc.
        // Ref: <https://github.com/paritytech/ink/blob/v4.1.0/crates/ink/ir/src/ir/chain_extension.rs#L213-L254>.
        utils::ensure_trait_invariants(results, trait_item, CHAIN_EXTENSION_SCOPE_NAME);
    }

    // Ensures that ink! chain extension `trait` item's associated items satisfy all invariants,
    // see `ensure_trait_item_invariants` doc.
    ensure_trait_item_invariants(results, chain_extension);

    // Runs ink! extension diagnostics, see `extension::diagnostics` doc.
    for item in chain_extension.extensions() {
        extension::diagnostics(results, item);
    }

    // Ensures that exactly one `ErrorCode` associated type is defined, see `ensure_error_code_quantity` doc.
    ensure_error_code_type_quantity(results, chain_extension);

    // Ensures that no ink! extension ids are overlapping, see `ensure_no_overlapping_ids` doc.
    ensure_no_overlapping_ids(results, chain_extension);

    // Ensures that only valid quasi-direct ink! attribute descendants (i.e ink! descendants without any ink! ancestors),
    // see `ensure_valid_quasi_direct_ink_descendants` doc.
    ensure_valid_quasi_direct_ink_descendants(results, chain_extension);
}

/// Ensures that ink! chain extension is a `trait` item whose associated items satisfy all invariants.
///
/// See reference below for details about checked invariants.
///
/// Ref: <https://github.com/paritytech/ink/blob/v4.1.0/crates/ink/ir/src/ir/chain_extension.rs#L213-L254>.
///
/// See `utils::ensure_trait_item_invariants` doc for common invariants for all trait-based ink! entities that are handled by that utility.
/// This utility also runs `extension::diagnostics` on trait methods with a ink! extension attribute.
fn ensure_trait_item_invariants(results: &mut Vec<Diagnostic>, chain_extension: &ChainExtension) {
    // Tracks already used and suggested ids for quickfixes.
    let mut unavailable_ids = init_unavailable_ids(chain_extension);
    if let Some(trait_item) = chain_extension.trait_item() {
        utils::ensure_trait_item_invariants(
            results,
            trait_item,
            "chain extension",
            |results, fn_item| {
                // All trait methods should be ink! extensions.
                // Ref: <https://github.com/paritytech/ink/blob/v4.1.0/crates/ink/ir/src/ir/chain_extension.rs#L447-L464>.
                // Ref: <https://github.com/paritytech/ink/blob/v4.1.0/crates/ink/ir/src/ir/chain_extension.rs#L467-L501>.
                match ink_analyzer_ir::ink_attrs(fn_item.syntax()).find_map(Extension::cast) {
                    // Runs ink! extension diagnostics, see `extension::diagnostics` doc.
                    Some(extension_item) => extension::diagnostics(results, &extension_item),
                    // Add diagnostic if method isn't an ink! extension.
                    None => {
                        // Determines quickfix insertion offset and affixes.
                        let insert_offset =
                            analysis_utils::first_ink_attribute_insert_offset(fn_item.syntax());
                        // Computes a unique id for the chain extension method.
                        let suggested_id =
                            analysis_utils::suggest_unique_id(Some(1), &mut unavailable_ids);
                        // Gets the declaration range for the item.
                        let range = analysis_utils::ast_item_declaration_range(&ast::Item::Fn(
                            fn_item.clone(),
                        ))
                        .unwrap_or(fn_item.syntax().text_range());
                        results.push(Diagnostic {
                            message: "All ink! chain extension methods must be ink! extensions."
                                .to_string(),
                            range,
                            severity: Severity::Error,
                            quickfixes: Some(vec![Action {
                                label: "Add ink! extension attribute.".to_string(),
                                kind: ActionKind::QuickFix,
                                range,
                                edits: [TextEdit::insert_with_snippet(
                                    format!("#[ink(extension = {suggested_id})]"),
                                    insert_offset,
                                    Some(format!("#[ink(extension = ${{1:{suggested_id}}})]")),
                                )]
                                .into_iter()
                                .chain(ink_analyzer_ir::ink_attrs(fn_item.syntax()).filter_map(
                                    |attr| {
                                        (!matches!(
                                            attr.kind(),
                                            InkAttributeKind::Arg(InkArgKind::Extension)
                                                | InkAttributeKind::Arg(InkArgKind::HandleStatus)
                                        ))
                                        .then_some(TextEdit::delete(attr.syntax().text_range()))
                                    },
                                ))
                                .collect(),
                            }]),
                        })
                    }
                }
            },
            |results, type_alias| {
                // Associated type invariants.
                // Ref: <https://github.com/paritytech/ink/blob/v4.1.0/crates/ink/ir/src/ir/chain_extension.rs#L256-L307>.
                let (is_named_error_code, name_marker) = match type_alias.name() {
                    Some(name) => (name.to_string() == "ErrorCode", Some(name)),
                    None => (false, None),
                };

                if !is_named_error_code {
                    results.push(Diagnostic {
                        message: "The associated type of a ink! chain extension must be named `ErrorCode`."
                            .to_string(),
                        range: name_marker
                            .as_ref()
                            .map(|it| it.syntax().text_range())
                            // Defaults to the declaration range for the chain extension.
                            .unwrap_or(
                                chain_extension_declaration_range(chain_extension)
                            ),
                        severity: Severity::Error,
                        quickfixes: name_marker.as_ref().map(|name| {
                            vec![Action {
                                label: "Rename associated type to `ErrorCode`.".to_string(),
                                kind: ActionKind::QuickFix,
                                range: name.syntax().text_range(),
                                edits: vec![TextEdit::replace(
                                    "ErrorCode".to_string(),
                                    name.syntax().text_range(),
                                )],
                            }]
                        }),
                    });
                }

                if let Some(diagnostic) =
                    utils::ensure_no_generics(type_alias, "chain extension `ErrorCode` type")
                {
                    results.push(diagnostic);
                }

                if let Some(diagnostic) = utils::ensure_no_trait_bounds(
                    type_alias,
                    "Trait bounds on ink! chain extension `ErrorCode` types are not supported.",
                ) {
                    results.push(diagnostic);
                }

                if type_alias.ty().is_none() {
                    // Get the insert offset and affixes for the quickfix.
                    let insert_offset = type_alias
                        .eq_token()
                        .map(|it| it.text_range().end())
                        .or(type_alias
                            .semicolon_token()
                            .map(|it| it.text_range().start()))
                        .unwrap_or(type_alias.syntax().text_range().start());
                    let insert_prefix = if type_alias.eq_token().is_none() {
                        " = "
                    } else {
                        ""
                    };
                    let insert_suffix = if type_alias.semicolon_token().is_none() {
                        ";"
                    } else {
                        ""
                    };
                    results.push(Diagnostic {
                        message: "ink! chain extension `ErrorCode` types must have a default type."
                            .to_string(),
                        range: type_alias.syntax().text_range(),
                        severity: Severity::Error,
                        quickfixes: Some(vec![Action {
                            label: "Add `ErrorCode` default type.".to_string(),
                            kind: ActionKind::QuickFix,
                            range: type_alias.syntax().text_range(),
                            edits: vec![TextEdit::insert_with_snippet(
                                format!("{insert_prefix}(){insert_suffix}"),
                                insert_offset,
                                Some(format!("{insert_prefix}${{1:()}}{insert_suffix}")),
                            )],
                        }]),
                    });
                }
            },
        );
    }
}

/// Ensures that exactly one `ErrorCode` associated type is defined.
///
/// Ref: <https://github.com/paritytech/ink/blob/v4.1.0/crates/ink/ir/src/ir/chain_extension.rs#L292-L305>.
///
/// Ref: <https://github.com/paritytech/ink/blob/v4.1.0/crates/ink/ir/src/ir/chain_extension.rs#L383-L391>.
fn ensure_error_code_type_quantity(
    results: &mut Vec<Diagnostic>,
    chain_extension: &ChainExtension,
) {
    if let Some(trait_item) = chain_extension.trait_item() {
        if let Some(assoc_item_list) = trait_item.assoc_item_list() {
            let error_codes: Vec<ast::TypeAlias> = assoc_item_list
                .assoc_items()
                .filter_map(|assoc_item| match assoc_item {
                    ast::AssocItem::TypeAlias(type_alias) => {
                        let name = type_alias.name()?;
                        (name.to_string() == "ErrorCode").then_some(type_alias)
                    }
                    _ => None,
                })
                .collect();

            if error_codes.is_empty() {
                // Set quickfix insertion offset at the beginning of the associated items list.
                let insert_offset =
                    analysis_utils::assoc_item_insert_offset_start(&assoc_item_list);
                // Set quickfix insertion indent.
                let indent = analysis_utils::item_children_indenting(trait_item.syntax());
                // Gets the declaration range for the item.
                let range = chain_extension_declaration_range(chain_extension);
                // Creates diagnostic and quickfix for missing `ErrorCode` type.
                results.push(Diagnostic {
                    message: "Missing `ErrorCode` associated type for ink! chain extension."
                        .to_string(),
                    range,
                    severity: Severity::Error,
                    quickfixes: Some(vec![Action {
                        label: "Add `ErrorCode` type for ink! chain extension.".to_string(),
                        kind: ActionKind::QuickFix,
                        range,
                        edits: vec![TextEdit::insert_with_snippet(
                            analysis_utils::apply_indenting(ERROR_CODE_PLAIN, &indent),
                            insert_offset,
                            Some(analysis_utils::apply_indenting(ERROR_CODE_SNIPPET, &indent)),
                        )],
                    }]),
                });
            } else if error_codes.len() > 1 {
                for item in &error_codes[1..] {
                    results.push(Diagnostic {
                        message: "Duplicate `ErrorCode` associated type for ink! chain extension."
                            .to_string(),
                        range: item.syntax().text_range(),
                        severity: Severity::Error,
                        quickfixes: Some(vec![Action {
                            label: "Remove duplicate `ErrorCode` type for ink! chain extension."
                                .to_string(),
                            kind: ActionKind::QuickFix,
                            range: item.syntax().text_range(),
                            edits: vec![TextEdit::delete(item.syntax().text_range())],
                        }]),
                    });
                }
            };
        }
    }
}

/// Returns text range of the contract `mod` "declaration" (i.e tokens between meta - attributes/rustdoc - and the start of the item list).
fn chain_extension_declaration_range(chain_extension: &ChainExtension) -> TextRange {
    chain_extension
        .trait_item()
        .and_then(|it| analysis_utils::ast_item_declaration_range(&ast::Item::Trait(it.clone())))
        .unwrap_or(chain_extension.syntax().text_range())
}

/// Ensures that no ink! extension ids are overlapping.
///
/// Ref: <https://github.com/paritytech/ink/blob/v4.1.0/crates/ink/ir/src/ir/chain_extension.rs#L292-L306>.
fn ensure_no_overlapping_ids(results: &mut Vec<Diagnostic>, chain_extension: &ChainExtension) {
    let mut seen_ids: HashSet<u32> = HashSet::new();
    let mut unavailable_ids = init_unavailable_ids(chain_extension);
    for (idx, extension) in chain_extension.extensions().iter().enumerate() {
        if let Some(id) = extension.id() {
            if seen_ids.get(&id).is_some() {
                // Determines text range for the argument value.
                let value_range_option = extension
                    .ink_attr()
                    .args()
                    .iter()
                    .find(|it| *it.kind() == InkArgKind::Extension)
                    .and_then(InkArg::value)
                    .map(MetaValue::text_range);
                results.push(Diagnostic {
                    message: "Extension ids must be unique across all ink! extensions in an ink! chain extension."
                        .to_string(),
                    range: value_range_option.unwrap_or(extension.ink_attr().syntax().text_range()),
                    severity: Severity::Error,
                    quickfixes: value_range_option.map(|range| {
                        let suggested_id = analysis_utils::suggest_unique_id(
                            Some(idx as u32 + 1),
                            &mut unavailable_ids,
                        );
                        vec![Action {
                            label: "Replace with a unique extension id.".to_string(),
                            kind: ActionKind::QuickFix,
                            range,
                            edits: vec![TextEdit::replace_with_snippet(
                                format!("{suggested_id}"),
                                range,
                                Some(format!("${{1:{suggested_id}}}")),
                            )],
                        }]
                    }),
                })
            }

            seen_ids.insert(id);
        }
    }
}

/// Ensures that only valid quasi-direct ink! attribute descendants (i.e ink! descendants without any ink! ancestors).
///
/// Ref: <https://github.com/paritytech/ink/blob/v4.1.0/crates/ink/ir/src/ir/chain_extension.rs#L476-L487>.
fn ensure_valid_quasi_direct_ink_descendants(
    results: &mut Vec<Diagnostic>,
    chain_extension: &ChainExtension,
) {
    utils::ensure_valid_quasi_direct_ink_descendants(results, chain_extension, |attr| {
        matches!(
            attr.kind(),
            InkAttributeKind::Arg(InkArgKind::Extension | InkArgKind::HandleStatus)
        )
    });
}

/// Initializes unavailable extension ids.
fn init_unavailable_ids(chain_extension: &ChainExtension) -> HashSet<u32> {
    chain_extension
        .extensions()
        .iter()
        .filter_map(Extension::id)
        .collect()
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::test_utils::verify_actions;
    use ink_analyzer_ir::syntax::TextSize;
    use ink_analyzer_ir::{InkFile, InkMacroKind, IsInkEntity, IsInkTrait};
    use quote::quote;
    use test_utils::{
        parse_offset_at, quote_as_pretty_string, quote_as_str, TestResultAction,
        TestResultTextRange,
    };

    fn parse_first_chain_extension(code: &str) -> ChainExtension {
        ChainExtension::cast(
            InkFile::parse(code)
                .tree()
                .ink_attrs_in_scope()
                .find(|attr| *attr.kind() == InkAttributeKind::Macro(InkMacroKind::ChainExtension))
                .unwrap(),
        )
        .unwrap()
    }

    // List of valid minimal ink! chain extensions used for positive(`works`) tests for ink! chain extension verifying utilities.
    // Ref: <https://github.com/paritytech/ink/blob/v4.1.0/crates/ink/ir/src/ir/chain_extension.rs#L875-L888>.
    // Ref: <https://github.com/paritytech/ink/blob/v4.1.0/crates/ink/ir/src/ir/chain_extension.rs#L923-L940>.
    macro_rules! valid_chain_extensions {
        () => {
            [
                // No methods.
                quote! {
                    // Chain extension with no methods is valid.
                },
                // Simple.
                quote! {
                    #[ink(extension=1)]
                    fn my_extension();

                    #[ink(extension=2)]
                    fn my_extension2();
                },
                // Input + output variations.
                quote! {
                    #[ink(extension=1)]
                    fn my_extension();

                    #[ink(extension=2)]
                    fn my_extension2(a: i32);

                    #[ink(extension=3)]
                    fn my_extension3() -> bool;

                    #[ink(extension=4)]
                    fn my_extension4(a: i32) -> bool;

                    #[ink(extension=5)]
                    fn my_extension5(a: i32) -> (i32, u64, bool);

                    #[ink(extension=6)]
                    fn my_extension6(a: i32, b: u64, c: [u8; 32]) -> bool;

                    #[ink(extension=7)]
                    fn my_extension7(a: i32, b: u64, c: [u8; 32]) -> (i32, u64, bool);
                },
                // Handle status.
                quote! {
                    #[ink(extension=1, handle_status=true)]
                    fn my_extension();

                    #[ink(extension=2, handle_status=false)]
                    fn my_extension2(a: i32);

                    #[ink(extension=3, handle_status=true)]
                    fn my_extension3() -> bool;

                    #[ink(extension=4, handle_status=false)]
                    fn my_extension4(a: i32) -> bool;

                    #[ink(extension=5, handle_status=true)]
                    fn my_extension5(a: i32) -> (i32, u64, bool);

                    #[ink(extension=6, handle_status=false)]
                    fn my_extension6(a: i32, b: u64, c: [u8; 32]) -> bool;

                    #[ink(extension=7, handle_status=true)]
                    fn my_extension7(a: i32, b: u64, c: [u8; 32]) -> (i32, u64, bool);
                },
            ]
            .iter()
            .flat_map(|extensions| {
                [
                    // Simple.
                    quote! {
                        #[ink::chain_extension]
                        pub trait MyChainExtension {
                            type ErrorCode = ();

                            #extensions
                        }
                    },
                ]
            })
        };
    }

    #[test]
    fn valid_trait_properties_works() {
        for code in valid_chain_extensions!() {
            let chain_extension = parse_first_chain_extension(quote_as_str! {
                #code
            });

            let mut results = Vec::new();
            utils::ensure_trait_invariants(
                &mut results,
                chain_extension.trait_item().unwrap(),
                "chain extension",
            );
            assert!(results.is_empty(), "chain extension: {code}");
        }
    }

    #[test]
    fn invalid_trait_properties_fails() {
        for (code, expected_quickfixes) in [
            // Visibility.
            // Ref: <https://github.com/paritytech/ink/blob/v4.1.0/crates/ink/ir/src/ir/chain_extension.rs#L539-L549>.
            (
                quote! {
                    trait MyChainExtension {}
                },
                vec![TestResultAction {
                    label: "`pub`",
                    edits: vec![TestResultTextRange {
                        text: "pub",
                        start_pat: Some("<-trait"),
                        end_pat: Some("<-trait"),
                    }],
                }],
            ),
            (
                quote! {
                    pub(crate) trait MyChainExtension {}
                },
                vec![TestResultAction {
                    label: "`pub`",
                    edits: vec![TestResultTextRange {
                        text: "pub",
                        start_pat: Some("<-pub(crate)"),
                        end_pat: Some("pub(crate)"),
                    }],
                }],
            ),
            (
                quote! {
                    pub(self) trait MyChainExtension {}
                },
                vec![TestResultAction {
                    label: "`pub`",
                    edits: vec![TestResultTextRange {
                        text: "pub",
                        start_pat: Some("<-pub(self)"),
                        end_pat: Some("pub(self)"),
                    }],
                }],
            ),
            (
                quote! {
                    pub(super) trait MyChainExtension {}
                },
                vec![TestResultAction {
                    label: "`pub`",
                    edits: vec![TestResultTextRange {
                        text: "pub",
                        start_pat: Some("<-pub(super)"),
                        end_pat: Some("pub(super)"),
                    }],
                }],
            ),
            (
                quote! {
                    pub(in my::path) trait MyChainExtension {}
                },
                vec![TestResultAction {
                    label: "`pub`",
                    edits: vec![TestResultTextRange {
                        text: "pub",
                        start_pat: Some("<-pub(in my::path)"),
                        end_pat: Some("pub(in my::path)"),
                    }],
                }],
            ),
            // Unsafe.
            // Ref: <https://github.com/paritytech/ink/blob/v4.1.0/crates/ink/ir/src/ir/chain_extension.rs#L523-L529>.
            (
                quote! {
                    pub unsafe trait MyChainExtension {}
                },
                vec![TestResultAction {
                    label: "Remove `unsafe`",
                    edits: vec![TestResultTextRange {
                        text: "",
                        start_pat: Some("<-unsafe"),
                        end_pat: Some("unsafe "),
                    }],
                }],
            ),
            // Auto.
            // Ref: <https://github.com/paritytech/ink/blob/v4.1.0/crates/ink/ir/src/ir/chain_extension.rs#L531-L537>.
            (
                quote! {
                    pub auto trait MyChainExtension {}
                },
                vec![TestResultAction {
                    label: "Remove `auto`",
                    edits: vec![TestResultTextRange {
                        text: "",
                        start_pat: Some("<-auto"),
                        end_pat: Some("auto "),
                    }],
                }],
            ),
            // Generic.
            // Ref: <https://github.com/paritytech/ink/blob/v4.1.0/crates/ink/ir/src/ir/chain_extension.rs#L551-L557>.
            (
                quote! {
                    pub trait MyChainExtension<T> {}
                },
                vec![TestResultAction {
                    label: "Remove generic",
                    edits: vec![TestResultTextRange {
                        text: "",
                        start_pat: Some("<-<T>"),
                        end_pat: Some("<T>"),
                    }],
                }],
            ),
            // Supertrait.
            // Ref: <https://github.com/paritytech/ink/blob/v4.1.0/crates/ink/ir/src/ir/chain_extension.rs#L559-L565>.
            (
                quote! {
                    pub trait MyChainExtension: SuperChainExtension {}
                },
                vec![TestResultAction {
                    label: "Remove type",
                    edits: vec![TestResultTextRange {
                        text: "",
                        start_pat: Some("<-: SuperChainExtension"),
                        end_pat: Some(": SuperChainExtension"),
                    }],
                }],
            ),
        ] {
            let code = quote_as_pretty_string! {
                #[ink::chain_extension]
                #code
            };
            let chain_extension = parse_first_chain_extension(&code);

            let mut results = Vec::new();
            utils::ensure_trait_invariants(
                &mut results,
                chain_extension.trait_item().unwrap(),
                "chain extension",
            );

            // Verifies diagnostics.
            assert_eq!(results.len(), 1, "chain extension: {code}");
            assert_eq!(
                results[0].severity,
                Severity::Error,
                "chain extension: {code}"
            );
            // Verifies quickfixes.
            verify_actions(
                &code,
                results[0].quickfixes.as_ref().unwrap(),
                &expected_quickfixes,
            );
        }
    }

    #[test]
    fn valid_trait_items_works() {
        for code in valid_chain_extensions!() {
            let chain_extension = parse_first_chain_extension(quote_as_str! {
                #code
            });

            let mut results = Vec::new();
            ensure_trait_item_invariants(&mut results, &chain_extension);
            assert!(results.is_empty(), "chain extension: {code}");
        }
    }

    #[test]
    fn invalid_trait_items_fails() {
        for (items, expected_quickfixes) in [
            // Const.
            // Ref: <https://github.com/paritytech/ink/blob/v4.1.0/crates/ink/ir/src/ir/chain_extension.rs#L567-L575>.
            (
                quote! {
                    const T: i32;
                },
                vec![TestResultAction {
                    label: "Remove",
                    edits: vec![TestResultTextRange {
                        text: "",
                        start_pat: Some("<-const"),
                        end_pat: Some("i32;"),
                    }],
                }],
            ),
            // Associated type name.
            // NOTE: default type set to `()` to test only the name.
            // Ref: <https://github.com/paritytech/ink/blob/v4.1.0/crates/ink/ir/src/ir/chain_extension.rs#L577-L585>.
            // Ref: <https://github.com/paritytech/ink/blob/v4.1.0/crates/ink/ir/src/ir/chain_extension.rs#L587-L620>.
            (
                quote! {
                    type Type = ();
                },
                vec![TestResultAction {
                    label: "`ErrorCode`",
                    edits: vec![TestResultTextRange {
                        text: "ErrorCode",
                        start_pat: Some("<-Type"),
                        end_pat: Some("Type"),
                    }],
                }],
            ),
            (
                quote! {
                    type IncorrectName  = ();
                },
                vec![TestResultAction {
                    label: "`ErrorCode`",
                    edits: vec![TestResultTextRange {
                        text: "ErrorCode",
                        start_pat: Some("<-IncorrectName"),
                        end_pat: Some("IncorrectName"),
                    }],
                }],
            ),
            // Associated type invariants.
            // Ref: <https://github.com/paritytech/ink/blob/v4.1.0/crates/ink/ir/src/ir/chain_extension.rs#L587-L620>.
            (
                quote! {
                    type ErrorCode<T> = (); // generics.
                },
                vec![TestResultAction {
                    label: "Remove generic",
                    edits: vec![TestResultTextRange {
                        text: "",
                        start_pat: Some("<-<T>"),
                        end_pat: Some("<T>"),
                    }],
                }],
            ),
            (
                quote! {
                    type ErrorCode: Copy = (); // trait bounds.
                },
                vec![TestResultAction {
                    label: "Remove type",
                    edits: vec![TestResultTextRange {
                        text: "",
                        start_pat: Some("<-: Copy"),
                        end_pat: Some(": Copy"),
                    }],
                }],
            ),
            (
                quote! {
                    type ErrorCode; // no default type.
                },
                vec![TestResultAction {
                    label: "Add",
                    edits: vec![TestResultTextRange {
                        text: "=",
                        start_pat: Some("ErrorCode"),
                        end_pat: Some("ErrorCode"),
                    }],
                }],
            ),
            // Macro.
            // Ref: <https://github.com/paritytech/ink/blob/v4.1.0/crates/ink/ir/src/ir/chain_extension.rs#L622-L630>.
            (
                quote! {
                    my_macro_call!();
                },
                vec![TestResultAction {
                    label: "Remove macro",
                    edits: vec![TestResultTextRange {
                        text: "",
                        start_pat: Some("<-my_macro_call"),
                        end_pat: Some("my_macro_call!();"),
                    }],
                }],
            ),
            // Non-flagged method.
            // Ref: <https://github.com/paritytech/ink/blob/v4.1.0/crates/ink/ir/src/ir/chain_extension.rs#L632-L652>.
            (
                quote! {
                    fn non_flagged();
                },
                vec![TestResultAction {
                    label: "Add ink! extension",
                    edits: vec![TestResultTextRange {
                        text: "extension",
                        start_pat: Some("<-fn"),
                        end_pat: Some("<-fn"),
                    }],
                }],
            ),
            // Default implementation.
            // Ref: <https://github.com/paritytech/ink/blob/v4.1.0/crates/ink/ir/src/ir/chain_extension.rs#L654-L663>.
            (
                quote! {
                    #[ink(extension=1)]
                    fn default_implemented() {}
                },
                vec![TestResultAction {
                    label: "Remove",
                    edits: vec![TestResultTextRange {
                        text: "",
                        start_pat: Some("<-{}"),
                        end_pat: Some("{}"),
                    }],
                }],
            ),
            // Const method.
            // Ref: <https://github.com/paritytech/ink/blob/v4.1.0/crates/ink/ir/src/ir/chain_extension.rs#L665-L674>.
            (
                quote! {
                    #[ink(extension=1)]
                    const fn const_extension();
                },
                vec![TestResultAction {
                    label: "Remove `const`",
                    edits: vec![TestResultTextRange {
                        text: "",
                        start_pat: Some("<-const"),
                        end_pat: Some("const "),
                    }],
                }],
            ),
            // Async method.
            // Ref: <https://github.com/paritytech/ink/blob/v4.1.0/crates/ink/ir/src/ir/chain_extension.rs#L676-L685>.
            (
                quote! {
                    #[ink(extension=1)]
                    async fn async_extension();
                },
                vec![TestResultAction {
                    label: "Remove `async`",
                    edits: vec![TestResultTextRange {
                        text: "",
                        start_pat: Some("<-async"),
                        end_pat: Some("async "),
                    }],
                }],
            ),
            // Unsafe method.
            // Ref: <https://github.com/paritytech/ink/blob/v4.1.0/crates/ink/ir/src/ir/chain_extension.rs#L687-L696>.
            (
                quote! {
                    #[ink(extension=1)]
                    unsafe fn unsafe_extension();
                },
                vec![TestResultAction {
                    label: "Remove `unsafe`",
                    edits: vec![TestResultTextRange {
                        text: "",
                        start_pat: Some("<-unsafe"),
                        end_pat: Some("unsafe "),
                    }],
                }],
            ),
            // Explicit ABI.
            // Ref: <https://github.com/paritytech/ink/blob/v4.1.0/crates/ink/ir/src/ir/chain_extension.rs#L698-L707>.
            (
                quote! {
                    #[ink(extension=1)]
                    extern fn extern_extension();
                },
                vec![TestResultAction {
                    label: "Remove explicit ABI",
                    edits: vec![TestResultTextRange {
                        text: "",
                        start_pat: Some("<-extern"),
                        end_pat: Some("extern "),
                    }],
                }],
            ),
            // Variadic method.
            // Ref: <https://github.com/paritytech/ink/blob/v4.1.0/crates/ink/ir/src/ir/chain_extension.rs#L709-L718>.
            (
                quote! {
                    #[ink(extension=1)]
                    fn variadic_extension(...);
                },
                vec![TestResultAction {
                    label: "un-variadic",
                    edits: vec![TestResultTextRange {
                        text: "",
                        start_pat: Some("<-..."),
                        end_pat: Some("..."),
                    }],
                }],
            ),
            // Generic method.
            // Ref: <https://github.com/paritytech/ink/blob/v4.1.0/crates/ink/ir/src/ir/chain_extension.rs#L720-L729>.
            (
                quote! {
                    #[ink(extension=1)]
                    fn generic_message<T>();
                },
                vec![TestResultAction {
                    label: "Remove generic",
                    edits: vec![TestResultTextRange {
                        text: "",
                        start_pat: Some("<-<T>"),
                        end_pat: Some("<T>"),
                    }],
                }],
            ),
            // Unsupported ink! attribute.
            // Ref: <https://github.com/paritytech/ink/blob/v4.1.0/crates/ink/ir/src/ir/chain_extension.rs#L731-L749>.
            (
                quote! {
                    #[ink(constructor)]
                    fn my_constructor() -> Self;
                },
                vec![TestResultAction {
                    label: "Add ink! extension",
                    edits: vec![
                        // Add ink! extension attribute.
                        TestResultTextRange {
                            text: "extension",
                            start_pat: Some("<-#[ink(constructor)]"),
                            end_pat: Some("<-#[ink(constructor)]"),
                        },
                        // Remove ink! constructor attribute.
                        TestResultTextRange {
                            text: "",
                            start_pat: Some("<-#[ink(constructor)]"),
                            end_pat: Some("#[ink(constructor)]"),
                        },
                    ],
                }],
            ),
            (
                quote! {
                    #[ink(message)]
                    fn my_message();
                },
                vec![TestResultAction {
                    label: "Add ink! extension",
                    edits: vec![
                        // Add ink! extension attribute.
                        TestResultTextRange {
                            text: "extension",
                            start_pat: Some("<-#[ink(message)]"),
                            end_pat: Some("<-#[ink(message)]"),
                        },
                        // Remove ink! message attribute.
                        TestResultTextRange {
                            text: "",
                            start_pat: Some("<-#[ink(message)]"),
                            end_pat: Some("#[ink(message)]"),
                        },
                    ],
                }],
            ),
            (
                quote! {
                    #[ink(unknown)]
                    fn unknown_method();
                },
                vec![TestResultAction {
                    label: "Add ink! extension",
                    edits: vec![
                        // Add ink! extension attribute.
                        TestResultTextRange {
                            text: "extension",
                            start_pat: Some("<-#[ink(unknown)]"),
                            end_pat: Some("<-#[ink(unknown)]"),
                        },
                        // Remove unknown ink! attribute.
                        TestResultTextRange {
                            text: "",
                            start_pat: Some("<-#[ink(unknown)]"),
                            end_pat: Some("#[ink(unknown)]"),
                        },
                    ],
                }],
            ),
            // self receiver.
            // Ref: <https://github.com/paritytech/ink/blob/v4.1.0/crates/ink/ir/src/ir/chain_extension.rs#L810-L857>.
            (
                quote! {
                    #[ink(extension=1)]
                    fn has_self_receiver(self);
                },
                vec![TestResultAction {
                    label: "Remove self",
                    edits: vec![TestResultTextRange {
                        text: "",
                        start_pat: Some("<-self)"),
                        end_pat: Some("(self"),
                    }],
                }],
            ),
            (
                quote! {
                    #[ink(extension=1)]
                    fn has_self_receiver(mut self);
                },
                vec![TestResultAction {
                    label: "Remove self",
                    edits: vec![TestResultTextRange {
                        text: "",
                        start_pat: Some("<-mut self"),
                        end_pat: Some("mut self"),
                    }],
                }],
            ),
            (
                quote! {
                    #[ink(extension=1)]
                    fn has_self_receiver(&self);
                },
                vec![TestResultAction {
                    label: "Remove self",
                    edits: vec![TestResultTextRange {
                        text: "",
                        start_pat: Some("<-&self"),
                        end_pat: Some("&self"),
                    }],
                }],
            ),
            (
                quote! {
                    #[ink(extension=1)]
                    fn has_self_receiver(&mut self);
                },
                vec![TestResultAction {
                    label: "Remove self",
                    edits: vec![TestResultTextRange {
                        text: "",
                        start_pat: Some("<-&mut self"),
                        end_pat: Some("&mut self"),
                    }],
                }],
            ),
        ] {
            let code = quote_as_pretty_string! {
                #[ink::chain_extension]
                pub trait MyChainExtension {
                    #items
                }
            };
            let chain_extension = parse_first_chain_extension(&code);

            let mut results = Vec::new();
            ensure_trait_item_invariants(&mut results, &chain_extension);

            // Verifies diagnostics.
            assert_eq!(results.len(), 1, "chain extension: {items}");
            assert_eq!(
                results[0].severity,
                Severity::Error,
                "chain extension: {items}"
            );
            // Verifies quickfixes.
            verify_actions(
                &code,
                results[0].quickfixes.as_ref().unwrap(),
                &expected_quickfixes,
            );
        }
    }

    #[test]
    fn one_error_code_type_works() {
        for code in valid_chain_extensions!() {
            let chain_extension = parse_first_chain_extension(quote_as_str! {
                #code
            });

            let mut results = Vec::new();
            ensure_error_code_type_quantity(&mut results, &chain_extension);
            assert!(results.is_empty(), "chain extension: {code}");
        }
    }

    #[test]
    fn multiple_error_code_types_fails() {
        // Tests snippets with btn 2 and 5 error code types.
        for idx in 2..=5 {
            // Creates multiple error code types.
            let error_code_types = (1..=idx).map(|_| {
                quote! {
                    type ErrorCode = ();
                }
            });

            // Creates contract with multiple error code types.
            let chain_extension = parse_first_chain_extension(quote_as_str! {
                #[ink::chain_extension]
                pub trait MyChainExtension {
                    #( #error_code_types )*
                }
            });

            let mut results = Vec::new();
            ensure_error_code_type_quantity(&mut results, &chain_extension);
            // There should be `idx-1` extraneous error code types.
            assert_eq!(results.len(), idx - 1);
            // All diagnostics should be errors.
            assert_eq!(
                results
                    .iter()
                    .filter(|item| item.severity == Severity::Error)
                    .count(),
                idx - 1
            );
            // All quickfixes should be for removal.
            for item in results {
                let fix = &item.quickfixes.as_ref().unwrap()[0];
                assert!(fix.label.contains("Remove duplicate `ErrorCode`"));
                assert_eq!(&fix.edits[0].text, "");
            }
        }
    }

    #[test]
    fn missing_error_code_type_fails() {
        let code = quote_as_pretty_string! {
            #[ink::chain_extension]
            pub trait MyChainExtension {
            }
        };
        let chain_extension = parse_first_chain_extension(&code);

        let mut results = Vec::new();
        ensure_error_code_type_quantity(&mut results, &chain_extension);

        // Verifies diagnostics.
        assert_eq!(results.len(), 1);
        assert_eq!(results[0].severity, Severity::Error);
        // Verifies quickfixes.
        assert!(results[0].quickfixes.as_ref().unwrap()[0]
            .label
            .contains("Add `ErrorCode`"));
        let offset = TextSize::from(parse_offset_at(&code, Some("{")).unwrap() as u32);
        assert_eq!(
            results[0].quickfixes.as_ref().unwrap()[0].edits[0].range,
            TextRange::new(offset, offset)
        );
    }

    #[test]
    fn non_overlapping_ids_works() {
        for code in valid_chain_extensions!() {
            let chain_extension = parse_first_chain_extension(quote_as_str! {
                #code
            });

            let mut results = Vec::new();
            ensure_no_overlapping_ids(&mut results, &chain_extension);
            assert!(results.is_empty(), "chain extension: {code}");
        }
    }

    #[test]
    // Ref: <https://github.com/paritytech/ink/blob/v4.1.0/crates/ink/ir/src/ir/chain_extension.rs#L859-L870>.
    fn overlapping_ids_fails() {
        for code in [
            // Overlapping decimal.
            quote! {
                #[ink(extension=1)]
                fn my_extension();

                #[ink(extension=1)]
                fn my_extension2();
            },
            // Overlapping hexadecimal.
            quote! {
                #[ink(extension=0x1)]
                fn my_extension();

                #[ink(extension=0x1)]
                fn my_extension2();
            },
            // Overlapping detected across decimal and hex representations.
            quote! {
                #[ink(extension=1)]
                fn my_extension();

                #[ink(extension=0x1)]
                fn my_extension2();
            },
        ] {
            let chain_extension = parse_first_chain_extension(quote_as_str! {
                #[ink::chain_extension]
                pub trait MyChainExtension {
                    #code
                }
            });

            let mut results = Vec::new();
            ensure_no_overlapping_ids(&mut results, &chain_extension);
            // 1 error the overlapping extension id.
            assert_eq!(results.len(), 1, "chain extension: {code}");
            // All diagnostics should be errors.
            assert_eq!(
                results[0].severity,
                Severity::Error,
                "chain extension: {code}"
            );
            // Verifies quickfixes.
            let quick_fix_label = &results[0].quickfixes.as_ref().unwrap()[0].label;
            assert!(
                quick_fix_label.contains("Replace")
                    && quick_fix_label.contains("unique extension id")
            );
        }
    }

    #[test]
    fn valid_quasi_direct_descendant_works() {
        for code in valid_chain_extensions!() {
            let chain_extension = parse_first_chain_extension(quote_as_str! {
                #code
            });

            let mut results = Vec::new();
            ensure_valid_quasi_direct_ink_descendants(&mut results, &chain_extension);
            assert!(results.is_empty(), "chain extension: {code}");
        }
    }

    #[test]
    // Ref: <https://github.com/paritytech/ink/blob/v4.1.0/crates/ink/ir/src/ir/chain_extension.rs#L731-L749>.
    fn invalid_quasi_direct_descendant_fails() {
        let code = quote_as_pretty_string! {
            #[ink::chain_extension]
            pub trait MyChainExtension {
                #[ink(constructor)]
                fn my_constructor() -> Self;

                #[ink(message)]
                fn my_message(&self);

                #[ink(unknown)]
                fn unknown_method(&self);
            }
        };
        let chain_extension = parse_first_chain_extension(&code);

        let mut results = Vec::new();
        ensure_valid_quasi_direct_ink_descendants(&mut results, &chain_extension);

        // 1 diagnostic each for `constructor`, `message` and `unknown`.
        assert_eq!(results.len(), 3);
        // All diagnostics should be errors.
        assert_eq!(
            results
                .iter()
                .filter(|item| item.severity == Severity::Error)
                .count(),
            3
        );
        // Verifies quickfixes.
        let expected_quickfixes = vec![
            vec![
                TestResultAction {
                    label: "Remove `#[ink(constructor)]`",
                    edits: vec![TestResultTextRange {
                        text: "",
                        start_pat: Some("<-#[ink(constructor)]"),
                        end_pat: Some("#[ink(constructor)]"),
                    }],
                },
                TestResultAction {
                    label: "Remove item",
                    edits: vec![TestResultTextRange {
                        text: "",
                        start_pat: Some("<-#[ink(constructor)]"),
                        end_pat: Some("fn my_constructor() -> Self;"),
                    }],
                },
            ],
            vec![
                TestResultAction {
                    label: "Remove `#[ink(message)]`",
                    edits: vec![TestResultTextRange {
                        text: "",
                        start_pat: Some("<-#[ink(message)]"),
                        end_pat: Some("#[ink(message)]"),
                    }],
                },
                TestResultAction {
                    label: "Remove item",
                    edits: vec![TestResultTextRange {
                        text: "",
                        start_pat: Some("<-#[ink(message)]"),
                        end_pat: Some("fn my_message(&self);"),
                    }],
                },
            ],
            vec![
                TestResultAction {
                    label: "Remove `#[ink(unknown)]`",
                    edits: vec![TestResultTextRange {
                        text: "",
                        start_pat: Some("<-#[ink(unknown)]"),
                        end_pat: Some("#[ink(unknown)]"),
                    }],
                },
                TestResultAction {
                    label: "Remove item",
                    edits: vec![TestResultTextRange {
                        text: "",
                        start_pat: Some("<-#[ink(unknown)]"),
                        end_pat: Some("fn unknown_method(&self);"),
                    }],
                },
            ],
        ];
        for (idx, item) in results.iter().enumerate() {
            let quickfixes = item.quickfixes.as_ref().unwrap();
            verify_actions(&code, quickfixes, &expected_quickfixes[idx]);
        }
    }

    #[test]
    fn compound_diagnostic_works() {
        for code in valid_chain_extensions!() {
            let chain_extension = parse_first_chain_extension(quote_as_str! {
                #code
            });

            let mut results = Vec::new();
            diagnostics(&mut results, &chain_extension);
            assert!(results.is_empty(), "chain extension: {code}");
        }
    }
}