decl-lang 0.4.7

Decl — a declarative language for describing, generating, and validating structured data: the `decl` CLI (check / evaluate / validate / fmt) and the `decl-lsp` language server, implemented natively in Rust
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
//! AST of the runtime — the shape the tree-sitter CST lowers into
//! (parse.rs), mirroring the reference implementation's ast.ts.
use crate::semantics::Value;
use std::cell::RefCell;
use std::collections::HashMap;
use std::rc::Rc;

/// a source range: zero-based rows and columns (UTF-16 units, as the
/// reference reads them from web-tree-sitter), end exclusive
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Loc {
    /// the start line
    pub sl: usize,
    /// the start column
    pub sc: usize,
    /// the end line
    pub el: usize,
    /// the end column, exclusive
    pub ec: usize,
}

// Every expression node carries its source range in a side table keyed
// by the node's address (expressions are shared through `Rc`, so the
// address is the node's identity, as object identity is in the
// reference); types, members, and declarations carry `loc` inline.
thread_local! {
    static EXPR_LOCS: RefCell<HashMap<usize, Loc>> = RefCell::new(HashMap::new());
}
fn expr_key(e: &Rc<Expr>) -> usize {
    Rc::as_ptr(e) as *const u8 as usize
}
/// Record an expression's source range.
pub fn set_expr_loc(e: &Rc<Expr>, loc: Loc) {
    EXPR_LOCS.with(|t| t.borrow_mut().insert(expr_key(e), loc));
}
/// An expression's source range, when recorded.
pub fn expr_loc(e: &Rc<Expr>) -> Option<Loc> {
    EXPR_LOCS.with(|t| t.borrow().get(&expr_key(e)).copied())
}

#[derive(Debug, Clone)]
/// a type expression (§3; chapter 11)
pub enum TypeAst {
    /// a primitive type by name
    Prim {
        /// the name
        name: String,
        /// the source range
        loc: Option<Loc>,
    },
    /// a literal type
    Lit {
        /// the literal
        v: Value,
        /// the source range
        loc: Option<Loc>,
    },
    /// a numeric range
    Range {
        /// the lower bound
        lo: Value,
        /// the upper bound
        hi: Value,
        /// whether the upper bound is excluded
        excl: bool,
        /// the source range
        loc: Option<Loc>,
    },
    /// a string pattern
    Pattern {
        /// the pattern's text
        re: String,
        /// the source range
        loc: Option<Loc>,
    },
    /// a record type
    Record {
        /// its members
        members: Vec<MemberAst>,
        /// whether it is open (`...`)
        open: bool,
        /// the source range
        loc: Option<Loc>,
    },
    /// a map type
    Map {
        /// the key type
        key: Box<TypeAst>,
        /// the value type
        val: Box<TypeAst>,
        /// the source range
        loc: Option<Loc>,
    },
    /// an array type
    Array {
        /// the element type
        elem: Box<TypeAst>,
        /// the lower size bound
        lo: Option<Value>,
        /// the upper size bound
        hi: Option<Value>,
        /// whether the upper bound is excluded
        excl: bool,
        /// the source range
        loc: Option<Loc>,
    },
    /// a union
    Union {
        /// its arms
        arms: Vec<TypeAst>,
        /// the source range
        loc: Option<Loc>,
    },
    /// an intersection
    Isect {
        /// its arms
        arms: Vec<TypeAst>,
        /// the source range
        loc: Option<Loc>,
    },
    /// a function type
    Func {
        /// the parameter types
        params: Vec<TypeAst>,
        /// the return type
        ret: Box<TypeAst>,
        /// the source range
        loc: Option<Loc>,
    },
    /// a named type, with its arguments, predicates, and extension
    Named {
        /// the name
        name: String,
        /// its type arguments
        args: Vec<TypeAst>,
        /// its predicates, when refined
        preds: Option<Vec<Rc<Expr>>>,
        /// its extension (`{ … }`)
        ext: Option<Box<TypeAst>>,
        /// the source range
        loc: Option<Loc>,
    },
}
impl TypeAst {
    /// The source range.
    pub fn loc(&self) -> Option<Loc> {
        match self {
            TypeAst::Prim { loc, .. }
            | TypeAst::Lit { loc, .. }
            | TypeAst::Range { loc, .. }
            | TypeAst::Pattern { loc, .. }
            | TypeAst::Record { loc, .. }
            | TypeAst::Map { loc, .. }
            | TypeAst::Array { loc, .. }
            | TypeAst::Union { loc, .. }
            | TypeAst::Isect { loc, .. }
            | TypeAst::Func { loc, .. }
            | TypeAst::Named { loc, .. } => *loc,
        }
    }
    /// Record the source range.
    pub fn set_loc(&mut self, l: Loc) {
        match self {
            TypeAst::Prim { loc, .. }
            | TypeAst::Lit { loc, .. }
            | TypeAst::Range { loc, .. }
            | TypeAst::Pattern { loc, .. }
            | TypeAst::Record { loc, .. }
            | TypeAst::Map { loc, .. }
            | TypeAst::Array { loc, .. }
            | TypeAst::Union { loc, .. }
            | TypeAst::Isect { loc, .. }
            | TypeAst::Func { loc, .. }
            | TypeAst::Named { loc, .. } => *loc = Some(l),
        }
    }
}

#[derive(Debug, Clone)]
/// a member of a record type (§5)
pub enum MemberAst {
    /// a value member: required, optional, or defaulted
    Value {
        /// the name
        name: String,
        /// optional (`?`)
        opt: bool,
        /// the type
        ty: TypeAst,
        /// the default, for a defaulted member
        dflt: Option<Rc<Expr>>,
        /// the annotations (§5.10)
        annotations: Vec<Annotation>,
        /// the source range
        loc: Option<Loc>,
    },
    /// `hidden`: `x$ = e` — computed for the schema's own use, never part of the value (D34)
    Derived {
        /// the name
        name: String,
        /// the annotation, when given
        ty: Option<TypeAst>,
        /// the expression
        expr: Rc<Expr>,
        /// hidden (`x$ = e`)
        hidden: bool,
        /// the annotations (§5.10)
        annotations: Vec<Annotation>,
        /// the source range
        loc: Option<Loc>,
    },
    /// a context declaration (`$parent: ref<P>`, §7.3)
    Context {
        /// the variable
        variable: String,
        /// the declared type
        ty: TypeAst,
        /// the annotations (§5.10)
        annotations: Vec<Annotation>,
        /// the source range
        loc: Option<Loc>,
    },
    /// an assertion
    Assert {
        /// the name
        name: String,
        /// the condition
        cond: Rc<Expr>,
        /// the `else` tail
        tail: Option<Tail>,
        /// the annotations (§5.10)
        annotations: Vec<Annotation>,
        /// the source range
        loc: Option<Loc>,
    },
    /// a guarded group of members (`when`)
    When {
        /// the condition
        cond: Rc<Expr>,
        /// the members
        body: Vec<MemberAst>,
        /// the annotations (§5.10)
        annotations: Vec<Annotation>,
        /// the source range
        loc: Option<Loc>,
    },
}
impl MemberAst {
    /// The source range.
    pub fn loc(&self) -> Option<Loc> {
        match self {
            MemberAst::Value { loc, .. }
            | MemberAst::Derived { loc, .. }
            | MemberAst::Context { loc, .. }
            | MemberAst::Assert { loc, .. }
            | MemberAst::When { loc, .. } => *loc,
        }
    }
    /// Record the source range.
    pub fn set_loc(&mut self, l: Loc) {
        match self {
            MemberAst::Value { loc, .. }
            | MemberAst::Derived { loc, .. }
            | MemberAst::Context { loc, .. }
            | MemberAst::Assert { loc, .. }
            | MemberAst::When { loc, .. } => *loc = Some(l),
        }
    }
    /// Attach the annotations (§5.10).
    pub fn set_annotations(&mut self, a: Vec<Annotation>) {
        match self {
            MemberAst::Value { annotations, .. }
            | MemberAst::Derived { annotations, .. }
            | MemberAst::Context { annotations, .. }
            | MemberAst::Assert { annotations, .. }
            | MemberAst::When { annotations, .. } => *annotations = a,
        }
    }
    /// the annotations (§5.10)
    pub fn annotations(&self) -> &[Annotation] {
        match self {
            MemberAst::Value { annotations, .. }
            | MemberAst::Derived { annotations, .. }
            | MemberAst::Context { annotations, .. }
            | MemberAst::Assert { annotations, .. }
            | MemberAst::When { annotations, .. } => annotations,
        }
    }
    /// the member's name (`name` in the reference: value, derived, and assert members)
    pub fn name(&self) -> Option<&str> {
        match self {
            MemberAst::Value { name, .. }
            | MemberAst::Derived { name, .. }
            | MemberAst::Assert { name, .. } => Some(name),
            _ => None,
        }
    }
}

#[derive(Debug, Clone)]
/// a part of a template: text, or an interpolated expression
pub enum TPart {
    /// text
    Text(String),
    /// an interpolated expression
    Expr(Rc<Expr>),
}

#[derive(Debug, Clone)]
/// an `else` tail: an inline message with a severity, or a diagnostic reference
pub enum Tail {
    /// an inline message
    Inline {
        /// the severity
        severity: String,
        /// the message template
        template: Vec<TPart>,
    },
    /// a diagnostic reference
    Ref {
        /// the diagnostic's name
        name: String,
        /// its arguments
        args: Vec<Rc<Expr>>,
    },
}

#[derive(Debug, Clone)]
/// a comprehension clause
pub struct ForClause {
    /// the variable
    pub v: String,
    /// what it ranges over
    pub iter: Rc<Expr>,
    /// the `if` filters
    pub filters: Vec<Rc<Expr>>,
}

#[derive(Debug, Clone)]
/// an arm of a `match`
pub struct MatchArm {
    /// the variable
    pub v: String,
    /// the type it matches; none for the catch-all
    pub ty: Option<TypeAst>,
    /// the body
    pub body: Rc<Expr>,
}

#[derive(Debug, Clone)]
/// an expression (§4)
pub enum Expr {
    /// a literal
    Lit(Value),
    /// a unit literal
    UnitLit {
        /// the number
        num: f64,
        /// the unit
        unit: String,
    },
    /// a template string
    Template(Vec<TPart>),
    /// a name
    Name(String),
    /// a context variable (`$this`, `$parent`, `$root`, `$key`)
    Ctx(String),
    /// `$referrers(T, "m")`
    Referrers {
        /// the referring type
        ty: String,
        /// its member
        member: String,
    },
    /// a record literal
    Obj(Vec<(String, Rc<Expr>)>),
    /// a spread entry's operand (§4.2): the value of an object entry keyed `...`
    Spread(Rc<Expr>),
    /// an array literal: (spread, item)
    Arr(Vec<(bool, Rc<Expr>)>),
    /// an array comprehension
    Comp {
        /// the head
        head: Rc<Expr>,
        /// the clauses
        clauses: Vec<ForClause>,
    },
    /// a map comprehension
    MapComp {
        /// the key
        key: Rc<Expr>,
        /// the value
        val: Rc<Expr>,
        /// the clauses
        clauses: Vec<ForClause>,
    },
    /// a binary operation
    Bin {
        /// the operator
        op: String,
        /// the left operand
        l: Rc<Expr>,
        /// the right operand
        r: Rc<Expr>,
    },
    /// a unary operation
    Un {
        /// the operator
        op: String,
        /// the operand
        x: Rc<Expr>,
    },
    /// a parenthesized expression
    Paren(Rc<Expr>),
    /// `if … then … else`
    If {
        /// the condition
        c: Rc<Expr>,
        /// the `then` branch
        t: Rc<Expr>,
        /// the `else` branch
        f: Rc<Expr>,
    },
    /// a function literal
    Lambda {
        /// the parameters
        params: Vec<String>,
        /// the body
        body: Rc<Expr>,
    },
    /// a call
    Call {
        /// the function
        fun: Rc<Expr>,
        /// the arguments
        args: Vec<Rc<Expr>>,
    },
    /// a member access
    Member {
        /// the record
        x: Rc<Expr>,
        /// the member
        name: String,
        /// `?.`
        safe: bool,
    },
    /// an index or key access
    Index {
        /// the array or map
        x: Rc<Expr>,
        /// the index or key
        i: Rc<Expr>,
    },
    /// a record update (`with`)
    With {
        /// the base
        base: Rc<Expr>,
        /// the patch
        patch: Rc<Expr>,
    },
    /// a pattern literal
    Pattern(String),
    /// a `match`
    Match {
        /// the subject
        subject: Rc<Expr>,
        /// the arms
        arms: Vec<MatchArm>,
    },
}

#[derive(Debug, Clone)]
/// an annotation (§5.10): `@name` or `@name(args)` — metadata only (D4)
pub struct Annotation {
    /// the name
    pub name: String,
    /// the arguments
    pub args: Vec<Rc<Expr>>,
    /// the source range
    pub loc: Option<Loc>,
}

#[derive(Debug, Clone)]
/// a parameter of a function, a type, or a diagnostic
pub struct Param {
    /// the name
    pub name: String,
    /// the type, when annotated
    pub ty: Option<TypeAst>,
}

#[derive(Debug, Clone)]
/// an imported name, possibly renamed
pub struct ImportItem {
    /// the name
    pub name: String,
    /// the alias
    pub alias: Option<String>,
}

// `ret` is lowered for fidelity with the reference AST; the runtime does
// not check return annotations (that is the static checker's job)
#[allow(dead_code)]
#[derive(Debug, Clone)]
/// a declaration (§5, §8)
pub enum DeclBody {
    /// a type
    Type {
        /// the name
        name: String,
        /// the type parameters
        params: Vec<Param>,
        /// the type
        ty: TypeAst,
        /// its `else` tail
        tail: Option<Tail>,
    },
    /// a constant
    Const {
        /// the name
        name: String,
        /// the annotation, when given
        ty: Option<TypeAst>,
        /// the expression
        expr: Rc<Expr>,
    },
    /// a function
    Func {
        /// the name
        name: String,
        /// the parameters
        params: Vec<Param>,
        /// the return type, when annotated
        ret: Option<TypeAst>,
        /// the body
        body: Rc<Expr>,
    },
    /// an output root
    Output {
        /// the name
        name: String,
        /// the type
        ty: TypeAst,
        /// the expression
        expr: Rc<Expr>,
    },
    /// an input root
    Input {
        /// the name
        name: String,
        /// the type
        ty: TypeAst,
        /// the fallback, when given
        fallback: Option<Rc<Expr>>,
    },
    /// a diagnostic template
    Diagnostic {
        /// the name
        name: String,
        /// the parameters
        params: Vec<Param>,
        /// the severity
        severity: String,
        /// the message template
        template: Vec<TPart>,
    },
    /// a dimension
    Dimension {
        /// the name
        name: String,
        /// its definition as (dimension, exponent) terms; none for a base dimension
        terms: Option<Vec<(String, i32)>>,
    },
    /// a unit
    Unit {
        /// the name
        name: String,
        /// its dimension
        dim: Option<String>,
        /// its factor against the base unit
        factor: Option<Rc<Expr>>,
        /// the base unit it is defined against
        base: Option<String>,
    },
    /// an import
    Import {
        /// the module
        from: String,
        /// the names imported; none for a namespace import
        names: Option<Vec<ImportItem>>,
        /// the namespace, for `import * as ns`
        ns: Option<String>,
    },
    /// a re-export
    ReExport {
        /// the module
        from: String,
        /// the names
        names: Vec<ImportItem>,
    },
}

#[derive(Debug, Clone)]
/// a declaration, with whether it is exported and its source range
pub struct Decl {
    /// the declaration
    pub body: DeclBody,
    /// `export`
    pub exported: bool,
    /// the annotations (§5.10)
    pub annotations: Vec<Annotation>,
    /// the declaration's source range (Phase 6 foundations); `export` included
    pub loc: Option<Loc>,
}

impl Decl {
    /// The declared name, when the declaration has one.
    pub fn name(&self) -> Option<&str> {
        match &self.body {
            DeclBody::Type { name, .. }
            | DeclBody::Const { name, .. }
            | DeclBody::Func { name, .. }
            | DeclBody::Output { name, .. }
            | DeclBody::Input { name, .. }
            | DeclBody::Diagnostic { name, .. }
            | DeclBody::Dimension { name, .. }
            | DeclBody::Unit { name, .. } => Some(name),
            _ => None,
        }
    }
}

/// does an expression mention `$referrers` anywhere? (deferred slots)
pub fn mentions_referrers(e: &Expr) -> bool {
    match e {
        Expr::Referrers { .. } => true,
        Expr::Lit(_) | Expr::UnitLit { .. } | Expr::Name(_) | Expr::Ctx(_) | Expr::Pattern(_) => {
            false
        }
        Expr::Template(parts) => parts
            .iter()
            .any(|p| matches!(p, TPart::Expr(x) if mentions_referrers(x))),
        Expr::Obj(es) => es.iter().any(|(_, v)| mentions_referrers(v)),
        Expr::Spread(x) => mentions_referrers(x),
        Expr::Arr(items) => items.iter().any(|(_, v)| mentions_referrers(v)),
        Expr::Comp { head, clauses } => {
            mentions_referrers(head) || clauses.iter().any(clause_mentions)
        }
        Expr::MapComp { key, val, clauses } => {
            mentions_referrers(key)
                || mentions_referrers(val)
                || clauses.iter().any(clause_mentions)
        }
        Expr::Bin { l, r, .. } => mentions_referrers(l) || mentions_referrers(r),
        Expr::Un { x, .. } | Expr::Paren(x) => mentions_referrers(x),
        Expr::If { c, t, f } => {
            mentions_referrers(c) || mentions_referrers(t) || mentions_referrers(f)
        }
        Expr::Lambda { body, .. } => mentions_referrers(body),
        Expr::Call { fun, args } => {
            mentions_referrers(fun) || args.iter().any(|a| mentions_referrers(a))
        }
        Expr::Member { x, .. } => mentions_referrers(x),
        Expr::Index { x, i } => mentions_referrers(x) || mentions_referrers(i),
        Expr::With { base, patch } => mentions_referrers(base) || mentions_referrers(patch),
        Expr::Match { subject, arms } => {
            mentions_referrers(subject) || arms.iter().any(|a| mentions_referrers(&a.body))
        }
    }
}

fn clause_mentions(c: &ForClause) -> bool {
    mentions_referrers(&c.iter) || c.filters.iter().any(|f| mentions_referrers(f))
}

/// A short name for an expression, for messages.
pub fn expr_name(e: &Expr) -> String {
    match e {
        Expr::Name(n) => n.clone(),
        Expr::Call { fun, .. } => expr_name(fun),
        _ => "<predicate>".into(),
    }
}

// ---------------- generic AST traversal (the reference walks object values) ----------------
/// every expression reachable from `e`, in the reference's object-value
/// order; `into_types` also descends into type ASTs embedded in
/// expressions (match arm types), as the untyped walks do
pub(crate) fn walk_expr_tree(e: &Rc<Expr>, into_types: bool, f: &mut dyn FnMut(&Rc<Expr>)) {
    f(e);
    let mut go = |x: &Rc<Expr>| walk_expr_tree(x, into_types, f);
    match &**e {
        Expr::Lit(_)
        | Expr::UnitLit { .. }
        | Expr::Name(_)
        | Expr::Ctx(_)
        | Expr::Referrers { .. }
        | Expr::Pattern(_) => {}
        Expr::Template(parts) => {
            for p in parts {
                if let TPart::Expr(x) = p {
                    go(x);
                }
            }
        }
        Expr::Obj(entries) => entries.iter().for_each(|(_, v)| go(v)),
        Expr::Arr(items) => items.iter().for_each(|(_, v)| go(v)),
        Expr::Comp { head, clauses } => {
            go(head);
            for c in clauses {
                go(&c.iter);
                c.filters.iter().for_each(&mut go);
            }
        }
        Expr::MapComp { key, val, clauses } => {
            go(key);
            go(val);
            for c in clauses {
                go(&c.iter);
                c.filters.iter().for_each(&mut go);
            }
        }
        Expr::Bin { l, r, .. } => {
            go(l);
            go(r);
        }
        Expr::Un { x, .. } | Expr::Paren(x) | Expr::Spread(x) => go(x),
        Expr::If { c, t, f: ff } => {
            go(c);
            go(t);
            go(ff);
        }
        Expr::Lambda { body, .. } => go(body),
        Expr::Call { fun, args } => {
            go(fun);
            args.iter().for_each(&mut go);
        }
        Expr::Member { x, .. } => go(x),
        Expr::Index { x, i } => {
            go(x);
            go(i);
        }
        Expr::With { base, patch } => {
            go(base);
            go(patch);
        }
        Expr::Match { subject, arms } => {
            go(subject);
            for a in arms {
                if into_types {
                    if let Some(t) = &a.ty {
                        walk_type_exprs(t, into_types, f);
                    }
                }
                walk_expr_tree(&a.body, into_types, f);
            }
        }
    }
}
/// every expression embedded in a type AST (predicates, member defaults, asserts)
pub(crate) fn walk_type_exprs(t: &TypeAst, into_types: bool, f: &mut dyn FnMut(&Rc<Expr>)) {
    match t {
        TypeAst::Prim { .. }
        | TypeAst::Lit { .. }
        | TypeAst::Range { .. }
        | TypeAst::Pattern { .. } => {}
        TypeAst::Record { members, .. } => members
            .iter()
            .for_each(|m| walk_member_exprs(m, into_types, f)),
        TypeAst::Map { key, val, .. } => {
            walk_type_exprs(key, into_types, f);
            walk_type_exprs(val, into_types, f);
        }
        TypeAst::Array { elem, .. } => walk_type_exprs(elem, into_types, f),
        TypeAst::Union { arms, .. } | TypeAst::Isect { arms, .. } => {
            arms.iter().for_each(|a| walk_type_exprs(a, into_types, f))
        }
        TypeAst::Func { params, ret, .. } => {
            params
                .iter()
                .for_each(|p| walk_type_exprs(p, into_types, f));
            walk_type_exprs(ret, into_types, f);
        }
        TypeAst::Named {
            args, preds, ext, ..
        } => {
            args.iter().for_each(|a| walk_type_exprs(a, into_types, f));
            if let Some(ps) = preds {
                ps.iter().for_each(|p| walk_expr_tree(p, into_types, f));
            }
            if let Some(x) = ext {
                walk_type_exprs(x, into_types, f);
            }
        }
    }
}
fn walk_member_exprs(m: &MemberAst, into_types: bool, f: &mut dyn FnMut(&Rc<Expr>)) {
    match m {
        MemberAst::Value { ty, dflt, .. } => {
            walk_type_exprs(ty, into_types, f);
            if let Some(d) = dflt {
                walk_expr_tree(d, into_types, f);
            }
        }
        MemberAst::Derived { ty, expr, .. } => {
            if let Some(t) = ty {
                walk_type_exprs(t, into_types, f);
            }
            walk_expr_tree(expr, into_types, f);
        }
        MemberAst::Context { ty, .. } => walk_type_exprs(ty, into_types, f),
        MemberAst::Assert { cond, tail, .. } => {
            walk_expr_tree(cond, into_types, f);
            match tail {
                Some(Tail::Inline { template, .. }) => {
                    for p in template {
                        if let TPart::Expr(x) = p {
                            walk_expr_tree(x, into_types, f);
                        }
                    }
                }
                Some(Tail::Ref { args, .. }) => {
                    args.iter().for_each(|a| walk_expr_tree(a, into_types, f))
                }
                None => {}
            }
        }
        MemberAst::When { cond, body, .. } => {
            walk_expr_tree(cond, into_types, f);
            body.iter()
                .for_each(|b| walk_member_exprs(b, into_types, f));
        }
    }
}