weaveffi-model 0.24.0

IR, parsers, Rust extractor, validation, and the resolved binding model for WeaveFFI API definitions
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
//! The validation rules: per-module name/uniqueness checks, type-reference
//! existence, ABI-representability of element shapes, callback-interface
//! shape rules, interface positions, and error-domain consistency.
//!
//! Every rule pushes into a shared `Vec<ValidationError>` sink instead of
//! returning early, so one validation pass reports every violation in the
//! document.

use super::ValidationError;
use crate::ir::{
    CallbackInterfaceDef, ErrorDomain, Function, InterfaceDef, Module, Param, StructField, TypeRef,
};
use std::collections::{BTreeMap, BTreeSet};

const RESERVED: &[&str] = &[
    "if", "else", "for", "while", "loop", "match", "type", "return", "async", "await", "break",
    "continue", "fn", "struct", "enum", "mod", "use",
];

fn is_valid_identifier(s: &str) -> bool {
    let mut chars = s.chars();
    match chars.next() {
        None => false,
        Some(c) if !(c.is_ascii_alphabetic() || c == '_') => false,
        _ => chars.all(|c| c.is_ascii_alphanumeric() || c == '_'),
    }
}

fn check_identifier(name: &str, errors: &mut Vec<ValidationError>) -> bool {
    if !is_valid_identifier(name) {
        errors.push(ValidationError::InvalidIdentifier(
            name.to_string(),
            "must start with a letter or underscore and contain only alphanumeric characters or underscores",
        ));
        return false;
    }
    if RESERVED.contains(&name) {
        errors.push(ValidationError::ReservedKeyword(name.to_string()));
        return false;
    }
    true
}

/// The kinds of declarations a bare name can refer to, gathered from the
/// whole module forest before any rule runs. Validation operates on the
/// document as written (every user reference is a [`TypeRef::Named`]), so
/// positional rules consult this index to learn what a name is.
#[derive(Default)]
pub(super) struct TypeIndex {
    /// Bare name of every struct, enum, interface, and callback interface
    /// anywhere in the API, mapped to its declaring module's dot path.
    pub all: BTreeMap<String, String>,
    /// Bare names of every interface anywhere in the API.
    pub interfaces: BTreeSet<String>,
    /// Bare names of every callback interface anywhere in the API.
    pub callback_interfaces: BTreeSet<String>,
    /// Bare names of every C-style (payload-free) enum anywhere in the API.
    pub plain_enums: BTreeSet<String>,
}

impl TypeIndex {
    pub(super) fn build(modules: &[Module]) -> Self {
        let mut index = Self::default();
        index.walk(modules, "");
        index
    }

    fn walk(&mut self, modules: &[Module], parent: &str) {
        for m in modules {
            let path = if parent.is_empty() {
                m.name.clone()
            } else {
                format!("{parent}.{}", m.name)
            };
            let mut declare = |name: &str| {
                self.all
                    .entry(name.to_string())
                    .or_insert_with(|| path.clone());
            };
            for s in &m.structs {
                declare(&s.name);
            }
            for e in &m.enums {
                declare(&e.name);
                if !e.is_rich() {
                    self.plain_enums.insert(e.name.clone());
                }
            }
            for i in &m.interfaces {
                declare(&i.name);
                self.interfaces.insert(i.name.clone());
            }
            for cb in &m.callback_interfaces {
                declare(&cb.name);
                self.callback_interfaces.insert(cb.name.clone());
            }
            self.walk(&m.modules, &path);
        }
    }

    /// Is `name` (bare or dot-qualified) an interface?
    fn is_interface(&self, name: &str) -> bool {
        self.interfaces.contains(bare(name))
    }

    /// Is `name` (bare or dot-qualified) a callback interface?
    fn is_callback_interface(&self, name: &str) -> bool {
        self.callback_interfaces.contains(bare(name))
    }

    /// Does a declaration with this name exist? A bare name may refer to a
    /// declaration in any module; a dot-qualified name must spell the
    /// declaring module's path exactly.
    fn exists(&self, name: &str) -> bool {
        match (self.all.get(bare(name)), name.rsplit_once('.')) {
            (None, _) => false,
            (Some(_), None) => true,
            (Some(path), Some((qualifier, _))) => path == qualifier,
        }
    }

    /// May `ty` be a map key? Every target must be able to use the key in
    /// its native dictionary idiom with exact equality, so only integers,
    /// bools, strings, and C-style enums qualify; floats (NaN and signed zero
    /// break key equality in several languages), composites, optionals,
    /// bytes, and objects are rejected.
    fn is_map_key(&self, ty: &TypeRef) -> bool {
        match ty {
            TypeRef::I8
            | TypeRef::I16
            | TypeRef::I32
            | TypeRef::U8
            | TypeRef::U16
            | TypeRef::U32
            | TypeRef::I64
            | TypeRef::U64
            | TypeRef::Bool
            | TypeRef::StringUtf8 => true,
            TypeRef::Named(name) => self.plain_enums.contains(bare(name)),
            _ => false,
        }
    }

    /// May `ty` be returned from a callback-interface method? Only `void`
    /// and the direct family: scalars, bool, and C-style enums.
    fn is_direct(&self, ty: &TypeRef) -> bool {
        match ty {
            TypeRef::I8
            | TypeRef::I16
            | TypeRef::I32
            | TypeRef::U8
            | TypeRef::U16
            | TypeRef::U32
            | TypeRef::I64
            | TypeRef::U64
            | TypeRef::F32
            | TypeRef::F64
            | TypeRef::Bool => true,
            TypeRef::Named(name) => self.plain_enums.contains(bare(name)),
            _ => false,
        }
    }
}

/// The final segment of a possibly dot-qualified name.
fn bare(name: &str) -> &str {
    name.rsplit('.').next().unwrap_or(name)
}

/// Enforce global bare-name uniqueness for the type namespace: structs,
/// enums, interfaces, callback interfaces, and error domains across every
/// module (including nested submodules).
///
/// Generators emit flat per-language type names, and unqualified cross-module
/// references resolve by bare name, so two types sharing a name would collide
/// in generated code and make references ambiguous.
pub(super) fn check_global_type_names(modules: &[Module], errors: &mut Vec<ValidationError>) {
    fn walk<'a>(
        modules: &'a [Module],
        prefix: &str,
        seen: &mut BTreeMap<&'a str, String>,
        errors: &mut Vec<ValidationError>,
    ) {
        for m in modules {
            let path = if prefix.is_empty() {
                m.name.clone()
            } else {
                format!("{prefix}.{}", m.name)
            };
            let names = m
                .structs
                .iter()
                .map(|s| s.name.as_str())
                .chain(m.enums.iter().map(|e| e.name.as_str()))
                .chain(m.interfaces.iter().map(|i| i.name.as_str()))
                .chain(m.callback_interfaces.iter().map(|c| c.name.as_str()))
                .chain(m.errors.iter().map(|d| d.name.as_str()));
            for name in names {
                if let Some(first) = seen.get(name) {
                    errors.push(ValidationError::DuplicateTypeName {
                        name: name.to_string(),
                        first: first.clone(),
                        second: path.clone(),
                    });
                } else {
                    seen.insert(name, path.clone());
                }
            }
            walk(&m.modules, &path, seen, errors);
        }
    }
    let mut seen = BTreeMap::new();
    walk(modules, "", &mut seen, errors);
}

/// Enforce API-global uniqueness of error *code* names across domains.
///
/// Backends with flat namespaces derive one error class or constant per
/// code, so `NotFound` declared in two different domains would collide in
/// generated code even though each domain is internally consistent.
pub(super) fn check_global_error_code_names(modules: &[Module], errors: &mut Vec<ValidationError>) {
    fn walk<'a>(
        modules: &'a [Module],
        prefix: &str,
        seen: &mut BTreeMap<&'a str, String>,
        errors: &mut Vec<ValidationError>,
    ) {
        for m in modules {
            let path = if prefix.is_empty() {
                m.name.clone()
            } else {
                format!("{prefix}.{}", m.name)
            };
            if let Some(domain) = &m.errors {
                let owner = format!("{path}.{}", domain.name);
                for code in &domain.codes {
                    if let Some(first) = seen.get(code.name.as_str()) {
                        errors.push(ValidationError::DuplicateErrorCodeName {
                            name: code.name.clone(),
                            first: first.clone(),
                            second: owner.clone(),
                        });
                    } else {
                        seen.insert(&code.name, owner.clone());
                    }
                }
            }
            walk(&m.modules, &path, seen, errors);
        }
    }
    let mut seen = BTreeMap::new();
    walk(modules, "", &mut seen, errors);
}

pub(super) fn validate_module(
    module: &Module,
    types: &TypeIndex,
    ancestor_has_domain: bool,
    errors: &mut Vec<ValidationError>,
) {
    if module.name.trim().is_empty() {
        errors.push(ValidationError::NoModuleName);
        return;
    }
    if !is_valid_identifier(&module.name) {
        errors.push(ValidationError::InvalidModuleName(
            module.name.clone(),
            "must start with a letter or underscore and contain only alphanumeric characters or underscores",
        ));
    } else if RESERVED.contains(&module.name.as_str()) {
        errors.push(ValidationError::InvalidModuleName(
            module.name.clone(),
            "reserved word",
        ));
    }

    let has_domain = ancestor_has_domain || module.errors.is_some();

    let mut function_names = BTreeSet::new();
    for f in &module.functions {
        if !function_names.insert(f.name.clone()) {
            errors.push(ValidationError::DuplicateFunctionName {
                module: module.name.clone(),
                function: f.name.clone(),
            });
        }
        validate_function(&module.name, &f.name, f, has_domain, errors);
    }

    let mut struct_names = BTreeSet::new();
    for s in &module.structs {
        check_identifier(&s.name, errors);
        if !struct_names.insert(s.name.clone()) {
            errors.push(ValidationError::DuplicateStructName {
                module: module.name.clone(),
                name: s.name.clone(),
            });
        }
        if s.fields.is_empty() {
            errors.push(ValidationError::EmptyStruct {
                module: module.name.clone(),
                name: s.name.clone(),
            });
        }
        let mut field_names = BTreeSet::new();
        for f in &s.fields {
            check_identifier(&f.name, errors);
            if !field_names.insert(f.name.clone()) {
                errors.push(ValidationError::DuplicateStructField {
                    struct_name: s.name.clone(),
                    field: f.name.clone(),
                });
            }
        }
    }

    let mut enum_names = BTreeSet::new();
    for e in &module.enums {
        check_identifier(&e.name, errors);
        if !enum_names.insert(e.name.clone()) {
            errors.push(ValidationError::DuplicateEnumName {
                module: module.name.clone(),
                name: e.name.clone(),
            });
        }
        if e.variants.is_empty() {
            errors.push(ValidationError::EmptyEnum {
                module: module.name.clone(),
                name: e.name.clone(),
            });
        }
        let mut variant_names = BTreeSet::new();
        let mut variant_values = BTreeMap::new();
        for v in &e.variants {
            check_identifier(&v.name, errors);
            if !variant_names.insert(v.name.clone()) {
                errors.push(ValidationError::DuplicateEnumVariant {
                    enum_name: e.name.clone(),
                    variant: v.name.clone(),
                });
            }
            if variant_values.insert(v.value, v.name.clone()).is_some() {
                errors.push(ValidationError::DuplicateEnumValue {
                    enum_name: e.name.clone(),
                    value: v.value,
                });
            }
            let mut variant_field_names = BTreeSet::new();
            for f in &v.fields {
                check_identifier(&f.name, errors);
                if !variant_field_names.insert(f.name.clone()) {
                    errors.push(ValidationError::DuplicateEnumVariantField {
                        enum_name: e.name.clone(),
                        variant: v.name.clone(),
                        field: f.name.clone(),
                    });
                }
            }
        }
    }

    let mut local_interface_names = BTreeSet::new();
    for i in &module.interfaces {
        check_identifier(&i.name, errors);
        if !local_interface_names.insert(i.name.clone()) {
            errors.push(ValidationError::DuplicateInterfaceName {
                module: module.name.clone(),
                name: i.name.clone(),
            });
        }
        validate_interface(module, i, has_domain, errors);
    }

    let mut callback_names = BTreeSet::new();
    for cb in &module.callback_interfaces {
        check_identifier(&cb.name, errors);
        if !callback_names.insert(cb.name.clone()) {
            errors.push(ValidationError::DuplicateCallbackInterfaceName {
                module: module.name.clone(),
                name: cb.name.clone(),
            });
        }
        validate_callback_interface(module, cb, types, errors);
    }

    // A field of a record, a rich-enum variant, or an error payload is
    // serialized inside a value buffer, so it obeys the buffered positional
    // rules: no iterators, no callback interfaces, no interface map keys, and
    // every reference must resolve.
    let mut check_buffered_field = |f: &StructField, location: &dyn Fn() -> String| {
        if contains_iterator(&f.ty) {
            errors.push(ValidationError::IteratorInInvalidPosition {
                location: location(),
            });
        }
        validate_type_ref(&f.ty, types, errors);
        check_interface_positions(&f.ty, types, location, errors);
        check_no_callback_interface(&f.ty, types, location, errors);
    };
    for s in &module.structs {
        for f in &s.fields {
            let location = || format!("field '{}' of struct '{}'", f.name, s.name);
            check_buffered_field(f, &location);
        }
    }
    for e in &module.enums {
        for v in &e.variants {
            for f in &v.fields {
                let location = || format!("field '{}' of variant '{}::{}'", f.name, e.name, v.name);
                check_buffered_field(f, &location);
            }
        }
    }
    if let Some(domain) = &module.errors {
        for c in &domain.codes {
            for f in &c.fields {
                let location = || {
                    format!(
                        "payload field '{}' of error code '{}::{}'",
                        f.name, domain.name, c.name
                    )
                };
                check_buffered_field(f, &location);
            }
        }
    }
    for f in &module.functions {
        validate_callable_types(&module.name, &f.name, f, types, errors);
    }
    for i in &module.interfaces {
        for f in i.constructors.iter().chain(&i.methods).chain(&i.statics) {
            let display = format!("{}.{}", i.name, f.name);
            validate_callable_types(&module.name, &display, f, types, errors);
        }
    }

    if let Some(domain) = &module.errors {
        validate_error_domain(module, domain, &function_names, errors);
    }

    let mut sub_module_names = BTreeSet::new();
    for sub in &module.modules {
        if !sub_module_names.insert(sub.name.clone()) {
            errors.push(ValidationError::DuplicateModuleName(sub.name.clone()));
        }
        validate_module(sub, types, has_domain, errors);
    }
}

/// Validate an interface's shape: unique member names across constructors,
/// methods, and statics; constructor restrictions; per-member signature
/// rules. C symbol collisions are checked API-wide once the model is built.
fn validate_interface(
    module: &Module,
    iface: &InterfaceDef,
    has_domain: bool,
    errors: &mut Vec<ValidationError>,
) {
    if iface.constructors.is_empty() && iface.methods.is_empty() && iface.statics.is_empty() {
        errors.push(ValidationError::EmptyInterface {
            module: module.name.clone(),
            name: iface.name.clone(),
        });
    }
    let mut member_names = BTreeSet::new();
    let mut check_member = |f: &Function, errors: &mut Vec<ValidationError>| {
        if !member_names.insert(f.name.clone()) {
            errors.push(ValidationError::DuplicateInterfaceMember {
                interface: iface.name.clone(),
                name: f.name.clone(),
            });
        }
        let display = format!("{}.{}", iface.name, f.name);
        validate_function(&module.name, &display, f, has_domain, errors);
    };
    for c in &iface.constructors {
        check_member(c, errors);
        if c.returns.is_some() {
            errors.push(ValidationError::ConstructorHasReturn {
                interface: iface.name.clone(),
                constructor: c.name.clone(),
            });
        }
        if c.r#async {
            errors.push(ValidationError::AsyncConstructor {
                interface: iface.name.clone(),
                constructor: c.name.clone(),
            });
        }
    }
    for m in &iface.methods {
        check_member(m, errors);
    }
    for s in &iface.statics {
        check_member(s, errors);
    }
}

/// Validate a callback interface: at least one method, unique method names,
/// and per-method restrictions. A callback method is implemented by the
/// consumer, so it is synchronous, never throws, never takes a cancel token,
/// returns nothing or a direct value, and takes no callback interface or
/// iterator as a parameter.
fn validate_callback_interface(
    module: &Module,
    cb: &CallbackInterfaceDef,
    types: &TypeIndex,
    errors: &mut Vec<ValidationError>,
) {
    if cb.methods.is_empty() {
        errors.push(ValidationError::EmptyCallbackInterface {
            module: module.name.clone(),
            name: cb.name.clone(),
        });
    }
    let mut method_names = BTreeSet::new();
    for m in &cb.methods {
        check_identifier(&m.name, errors);
        if !method_names.insert(m.name.clone()) {
            errors.push(ValidationError::DuplicateCallbackMethod {
                interface: cb.name.clone(),
                name: m.name.clone(),
            });
        }
        let reject = |reason: &'static str, errors: &mut Vec<ValidationError>| {
            errors.push(ValidationError::InvalidCallbackMethod {
                interface: cb.name.clone(),
                method: m.name.clone(),
                reason,
            });
        };
        if m.r#async {
            reject("cannot be async", errors);
        }
        if m.throws {
            reject("cannot declare throws", errors);
        }
        if m.cancellable {
            reject("cannot be cancellable", errors);
        }
        if let Some(ret) = &m.returns {
            validate_type_ref(ret, types, errors);
            if !types.is_direct(ret) {
                reject(
                    "must return nothing or a direct value (scalar, bool, or C-style enum)",
                    errors,
                );
            }
        }
        let mut param_names = BTreeSet::new();
        for p in &m.params {
            validate_param(p, errors);
            if !param_names.insert(p.name.clone()) {
                errors.push(ValidationError::DuplicateParamName {
                    module: module.name.clone(),
                    function: format!("{}.{}", cb.name, m.name),
                    param: p.name.clone(),
                });
            }
            let location = || {
                format!(
                    "param '{}' of callback interface method '{}.{}'",
                    p.name, cb.name, m.name
                )
            };
            if contains_iterator(&p.ty) {
                errors.push(ValidationError::IteratorInInvalidPosition {
                    location: location(),
                });
            }
            validate_type_ref(&p.ty, types, errors);
            check_interface_positions(&p.ty, types, location, errors);
            check_no_callback_interface(&p.ty, types, location, errors);
        }
    }
}

/// Name-level checks for one callable: a valid identifier, unique parameter
/// names, and an error domain in scope when the callable declares `throws`.
fn validate_function(
    module_name: &str,
    display_name: &str,
    f: &Function,
    has_domain: bool,
    errors: &mut Vec<ValidationError>,
) {
    check_identifier(&f.name, errors);

    if f.cancellable && !f.r#async {
        errors.push(ValidationError::CancellableNotAsync {
            module: module_name.to_string(),
            function: display_name.to_string(),
        });
    }

    if f.throws && !has_domain {
        errors.push(ValidationError::ThrowsWithoutErrorDomain {
            module: module_name.to_string(),
            function: display_name.to_string(),
        });
    }

    let mut param_names = BTreeSet::new();
    for p in &f.params {
        validate_param(p, errors);
        if !param_names.insert(p.name.clone()) {
            errors.push(ValidationError::DuplicateParamName {
                module: module_name.to_string(),
                function: display_name.to_string(),
                param: p.name.clone(),
            });
        }
    }
}

/// Type-level checks for one callable's parameters and return: iterator
/// positions, async-iterator exclusion, reference existence, element shapes,
/// interface map keys, and callback-interface positions.
fn validate_callable_types(
    module_name: &str,
    display_name: &str,
    f: &Function,
    types: &TypeIndex,
    errors: &mut Vec<ValidationError>,
) {
    for p in &f.params {
        let location = || {
            format!(
                "param '{}' of function '{module_name}::{display_name}'",
                p.name
            )
        };
        if contains_iterator(&p.ty) {
            errors.push(ValidationError::IteratorInInvalidPosition {
                location: location(),
            });
        }
        validate_type_ref(&p.ty, types, errors);
        check_interface_positions(&p.ty, types, location, errors);
        // A bare callback interface is the one legal callback position;
        // anything nested (`Listener?`, `[Listener]`) is not.
        match &p.ty {
            TypeRef::Named(name) if types.is_callback_interface(name) => {}
            other => check_no_callback_interface(other, types, location, errors),
        }
    }
    if let Some(ret) = &f.returns {
        let location = || format!("return type of {module_name}::{display_name}");
        // An async function completes through a one-shot callback; an
        // iterator needs a pull-based handle. The two shapes cannot
        // compose on the C ABI, so reject the combination up front
        // instead of letting backends lower it inconsistently.
        if f.r#async && contains_iterator(ret) {
            errors.push(ValidationError::AsyncIteratorReturn {
                module: module_name.to_string(),
                function: display_name.to_string(),
            });
        }
        // An iterator is a pull handle, valid only as the outermost return
        // shape: `[iter<T>]`, `iter<T>?`, `iter<iter<T>>`, and iterators
        // inside a map have no lowering.
        let nested_iterator = match ret {
            TypeRef::Iterator(elem) => contains_iterator(elem),
            other => contains_iterator(other),
        };
        if nested_iterator {
            errors.push(ValidationError::IteratorInInvalidPosition {
                location: format!("nested inside the {}", location()),
            });
        }
        validate_type_ref(ret, types, errors);
        check_interface_positions(ret, types, location, errors);
        check_no_callback_interface(ret, types, location, errors);
    }
}

fn validate_param(p: &Param, errors: &mut Vec<ValidationError>) {
    check_identifier(&p.name, errors);
}

/// Enforce the one place an interface reference may not appear: as a map
/// key. Objects are reference-counted tokens, so they compose with every
/// other buffered shape (fields, elements, map values, optionals), but no
/// target can hash an object by identity in a way that survives the ABI.
fn check_interface_positions(
    ty: &TypeRef,
    types: &TypeIndex,
    location: impl Fn() -> String + Copy,
    errors: &mut Vec<ValidationError>,
) {
    ty.walk(&mut |t| {
        if let TypeRef::Map(k, _) = t {
            if let TypeRef::Named(name) = &**k {
                if types.is_interface(name) {
                    errors.push(ValidationError::InterfaceInInvalidPosition {
                        name: name.clone(),
                        location: format!("map key of {}", location()),
                    });
                }
            }
        }
    });
}

/// Reject any callback-interface reference reachable from `ty`. Callers that
/// permit a bare top-level callback interface unwrap it before calling.
fn check_no_callback_interface(
    ty: &TypeRef,
    types: &TypeIndex,
    location: impl Fn() -> String + Copy,
    errors: &mut Vec<ValidationError>,
) {
    ty.walk(&mut |t| {
        if let TypeRef::Named(name) = t {
            if types.is_callback_interface(name) {
                errors.push(ValidationError::CallbackInterfaceInInvalidPosition {
                    name: name.clone(),
                    location: location(),
                });
            }
        }
    });
}

fn contains_iterator(ty: &TypeRef) -> bool {
    let mut found = false;
    ty.walk(&mut |t| {
        if matches!(t, TypeRef::Iterator(_)) {
            found = true;
        }
    });
    found
}

fn validate_type_ref(ty: &TypeRef, types: &TypeIndex, errors: &mut Vec<ValidationError>) {
    match ty {
        TypeRef::Named(name) => {
            if !types.exists(name) {
                errors.push(ValidationError::UnknownTypeRef { name: name.clone() });
            }
        }
        TypeRef::Optional(inner) | TypeRef::List(inner) | TypeRef::Iterator(inner) => {
            validate_type_ref(inner, types, errors);
        }
        TypeRef::Map(k, v) => {
            if !types.is_map_key(k) {
                errors.push(ValidationError::InvalidMapKey {
                    key_type: k.to_string(),
                });
            }
            validate_type_ref(k, types, errors);
            validate_type_ref(v, types, errors);
        }
        _ => {}
    }
}

fn validate_error_domain(
    module: &Module,
    domain: &ErrorDomain,
    function_names: &BTreeSet<String>,
    errors: &mut Vec<ValidationError>,
) {
    if domain.name.trim().is_empty() {
        errors.push(ValidationError::ErrorDomainMissingName(module.name.clone()));
        return;
    }
    check_identifier(&domain.name, errors);
    if function_names.contains(&domain.name) {
        errors.push(ValidationError::NameCollisionWithErrorDomain {
            module: module.name.clone(),
            name: domain.name.clone(),
        });
    }

    let mut by_name: BTreeSet<String> = BTreeSet::new();
    let mut by_code: BTreeMap<i32, String> = BTreeMap::new();
    for c in &domain.codes {
        check_identifier(&c.name, errors);
        // 0 means success and the whole negative range is reserved for the
        // runtime (-1 generic error, -2 panic, -3 marshalling failure, -4
        // foreign callback error, and room to grow), so domain codes must be
        // positive.
        if c.code <= 0 {
            errors.push(ValidationError::InvalidErrorCode {
                module: module.name.clone(),
                name: c.name.clone(),
            });
        }
        if !by_name.insert(c.name.clone()) {
            errors.push(ValidationError::DuplicateErrorName {
                module: module.name.clone(),
                name: c.name.clone(),
            });
        }
        if by_code.insert(c.code, c.name.clone()).is_some() {
            errors.push(ValidationError::DuplicateErrorCode {
                module: module.name.clone(),
                code: c.code,
            });
        }
    }
}