stav 0.1.0

A statically typed, eager and total configuration language
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
//! Bodies: attribute, block, `let`, `include`, `if` and `for` statements,
//! priority markers, and the `self` dependencies of each body.

use std::collections::BTreeMap;

use super::decls::{GlobalKind, ValueItem};
use super::graph;
use super::load::FileId;
use super::model::{
    AttrFact, BlockFact, BlockRule, Fact, ForFact, IncludeFact, LabelFact, LetFact, Priority,
    SelfRead, Step, Target, UnitId,
};
use super::suggest::did_you_mean;
use super::types::{StructId, Type, TypeId};
use super::{Binding, BodyCx, Checker, Cx, Frame, SelfMode};
use crate::ast::{self, Expr, Item, Key, Stmt};
use crate::diagnostic::Diagnostic;
use crate::span::Span;

/// The late contributions of one body and the fields they read through
/// `self`. Nodes are fields of blocks, keyed by the path of the block from
/// the top of the body and the field name.
#[derive(Debug, Clone, Default)]
pub(crate) struct SelfGraph {
    nodes: BTreeMap<(Vec<String>, String), usize>,
    labels: Vec<String>,
    edges: Vec<(usize, usize, Span)>,
}

impl SelfGraph {
    fn node(&mut self, path: Vec<String>, field: String, label: String) -> usize {
        let next = self.labels.len();
        *self.nodes.entry((path, field)).or_insert_with(|| {
            self.labels.push(label);
            next
        })
    }
}

/// Where an attribute path stands: at a body target, or at a value of
/// some type.
#[derive(Clone, Copy)]
enum Place {
    Target(Target),
    Type(TypeId),
}

/// A field a body may contain: a struct field or a payload field.
struct BodyField {
    index: usize,
    name: String,
    ty: TypeId,
    block: Option<String>,
}

impl<'p> Checker<'p> {
    fn body_fields(&self, target: Target) -> Vec<BodyField> {
        match target {
            Target::Struct(s) => self
                .types
                .structure(s)
                .fields
                .iter()
                .enumerate()
                .map(|(index, f)| BodyField {
                    index,
                    name: f.name.clone(),
                    ty: f.ty,
                    block: f.block.clone(),
                })
                .collect(),
            Target::Variant { sum, variant } => self.types.sum(sum).variants[variant]
                .fields
                .iter()
                .flatten()
                .enumerate()
                .map(|(index, f)| BodyField {
                    index,
                    name: f.name.clone(),
                    ty: f.ty,
                    block: None,
                })
                .collect(),
            Target::Map { .. } => Vec::new(),
        }
    }

    fn target_name(&self, target: Target) -> String {
        match target {
            Target::Struct(s) => self.types.structure(s).name.clone(),
            Target::Variant { sum, variant } => {
                let sum = self.types.sum(sum);
                format!("{}.{}", sum.name, sum.variants[variant].name)
            }
            Target::Map { key, value } => match self.types.get(self.types.peel(key)) {
                Type::Error => "a map".to_owned(),
                _ => format!(
                    "Map<{}, {}>",
                    self.types.render_plain(key),
                    self.types.render_plain(value)
                ),
            },
        }
    }

    /// Checks the statements of a body of `target`. The bindings of `let`
    /// statements stay in scope for the caller to remove.
    pub(super) fn statements(
        &mut self,
        cx: &mut Cx,
        statements: impl Iterator<Item = Stmt<'p>>,
        target: Target,
    ) {
        self.nested_statements(cx, statements, target, &mut BTreeMap::new());
    }

    /// Checks statements that contribute to the same body as the
    /// statements around them, such as those of an `if` or `for` body.
    /// `included` holds the part files the body already includes.
    fn nested_statements(
        &mut self,
        cx: &mut Cx,
        statements: impl Iterator<Item = Stmt<'p>>,
        target: Target,
        included: &mut BTreeMap<FileId, Span>,
    ) {
        for statement in statements {
            self.statement(cx, statement, target, included);
        }
    }

    fn statement(
        &mut self,
        cx: &mut Cx,
        statement: Stmt<'p>,
        target: Target,
        included: &mut BTreeMap<FileId, Span>,
    ) {
        match statement {
            Stmt::Let(l) => self.let_statement(cx, l),
            Stmt::Attr(attr) => self.attr(cx, attr, target),
            Stmt::Block(block) => self.block(cx, block, target),
            Stmt::Include(include) => self.include(cx, include, target, included),
            Stmt::If(branch) => self.if_statement(cx, branch, target, included),
            Stmt::For(each) => {
                let span = each.syntax().span();
                let source = match each.collection() {
                    Some(e) => self.structural(cx, e, None, "a `for` collection"),
                    None => TypeId::ERROR,
                };
                let mark = cx.scope.len();
                let names: Vec<_> = each.names().collect();
                let binds = self.iterate(cx, source, &names, span);
                self.fact(cx, each.syntax(), Fact::For(ForFact { binds }));
                if let Some(body) = each.body() {
                    self.nested_statements(cx, body.statements(), target, included);
                }
                cx.scope.truncate(mark);
            }
        }
    }

    /// Checks an `if` statement. Each branch starts from the part files
    /// included before the statement, since only one of them runs, and the
    /// statements after it see the part files of every branch.
    fn if_statement(
        &mut self,
        cx: &mut Cx,
        branch: ast::IfStmt<'p>,
        target: Target,
        included: &mut BTreeMap<FileId, Span>,
    ) {
        if let Some(condition) = branch.condition() {
            self.structural(cx, condition, Some(TypeId::BOOL), "an `if` condition");
        }
        let before = included.clone();
        if let Some(body) = branch.then_body() {
            let mark = cx.scope.len();
            self.nested_statements(cx, body.statements(), target, included);
            cx.scope.truncate(mark);
        }
        let mut otherwise = before;
        match branch.else_branch() {
            Some(ast::ElseBranch::Body(body)) => {
                let mark = cx.scope.len();
                self.nested_statements(cx, body.statements(), target, &mut otherwise);
                cx.scope.truncate(mark);
            }
            Some(ast::ElseBranch::If(nested)) => {
                self.if_statement(cx, nested, target, &mut otherwise);
            }
            None => {}
        }
        for (file, span) in otherwise {
            included.entry(file).or_insert(span);
        }
    }

    /// Checks an expression that decides which contributions a body makes,
    /// where `self` is not allowed.
    pub(super) fn structural(
        &mut self,
        cx: &mut Cx,
        e: Expr<'p>,
        expected: Option<TypeId>,
        place: &'static str,
    ) -> TypeId {
        let saved = std::mem::replace(&mut cx.self_mode, SelfMode::Forbidden(place));
        let reads = std::mem::take(&mut cx.reads);
        let ty = self.expr(cx, e, expected);
        cx.self_mode = saved;
        cx.reads = reads;
        ty
    }

    fn let_statement(&mut self, cx: &mut Cx, l: ast::Let<'p>) {
        let annotation = l.ty().map(|t| self.resolve_local_type(cx, t));
        let saved = std::mem::take(&mut cx.reads);
        let ty = match (l.value(), annotation) {
            (Some(v), Some(a)) => {
                self.expr(cx, v, Some(a));
                a
            }
            (Some(v), None) => self.expr(cx, v, None),
            (None, a) => a.unwrap_or(TypeId::ERROR),
        };
        let reads = std::mem::replace(&mut cx.reads, saved);
        let Some(name) = l.name() else { return };
        let id = self.local(cx, name.text(), ty, reads);
        cx.scope.push((name.text().to_owned(), Binding::Local(id)));
        self.fact(cx, l.syntax(), Fact::Let(LetFact::Local(id)));
    }

    fn attr(&mut self, cx: &mut Cx, attr: ast::Attr<'p>, target: Target) {
        let Some(path) = attr.path() else { return };
        let keys: Vec<Key<'p>> = path.keys().collect();
        let mut place = Place::Target(target);
        let mut steps = Vec::new();
        let mut names = Vec::new();
        let mut owner = self.target_name(target);
        let mut i = 0;
        let ty = loop {
            let Some(key) = keys.get(i) else {
                break match place {
                    Place::Type(ty) => Some(ty),
                    Place::Target(_) => None,
                };
            };
            let Some(text) = key_text(*key) else {
                break None;
            };
            match place {
                Place::Target(Target::Map { key: k, value }) => {
                    if let Some(s) = self.types.as_sum(k)
                        && self.types.sum(s).variant(&text).is_none()
                    {
                        let sum = self.types.sum(s);
                        let help =
                            did_you_mean(&text, sum.variants.iter().map(|v| v.name.as_str()));
                        let name = sum.name.clone();
                        self.error(
                            cx.file,
                            Diagnostic::error(
                                "E0302",
                                key.span(),
                                format!("`{text}` is not a variant of `{name}`"),
                            )
                            .with_optional_help(help),
                        );
                        break None;
                    }
                    steps.push(Step::Key(text.clone()));
                    names.push(text);
                    place = Place::Type(value);
                    i += 1;
                }
                Place::Target(t) => {
                    let fields = self.body_fields(t);
                    let Some(field) = fields.iter().find(|f| f.name == text) else {
                        let help = did_you_mean(&text, fields.iter().map(|f| f.name.as_str()));
                        let name = self.target_name(t);
                        self.error(
                            cx.file,
                            Diagnostic::error(
                                "E0302",
                                key.span(),
                                format!("`{name}` has no field `{text}`"),
                            )
                            .with_optional_help(help),
                        );
                        break None;
                    };
                    owner = self.target_name(t);
                    steps.push(Step::Field(field.index));
                    names.push(text);
                    place = Place::Type(field.ty);
                    i += 1;
                }
                Place::Type(ty) => {
                    let inner = self
                        .types
                        .unwrap_option(ty)
                        .unwrap_or_else(|| self.types.peel(ty));
                    match self.types.get(inner).clone() {
                        Type::Struct(s) => place = Place::Target(Target::Struct(s)),
                        Type::Map(k, v) => place = Place::Target(Target::Map { key: k, value: v }),
                        Type::Error => break None,
                        _ => {
                            let prefix: Vec<String> = names.clone();
                            let rendered = self.types.render_plain(ty);
                            self.error(
                                cx.file,
                                Diagnostic::error(
                                    "E0436",
                                    key.span(),
                                    format!("`{}` is a value of type `{rendered}`, which has no attribute `{text}`", prefix.join(".")),
                                )
                                .with_help("attribute paths go through struct fields and map keys"),
                            );
                            break None;
                        }
                    }
                }
            }
        };
        let Some(value) = attr.value() else { return };
        let Some((priority, value)) = self.marker(cx, value) else {
            return;
        };
        let saved = std::mem::take(&mut cx.reads);
        cx.partial = true;
        self.expr(cx, value, Some(ty.unwrap_or(TypeId::ERROR)));
        cx.partial = false;
        let reads = std::mem::replace(&mut cx.reads, saved);
        let Some(ty) = ty else { return };
        if !reads.is_empty() {
            self.late_contribution(cx, &names, owner, &reads, value.syntax().span());
        }
        self.fact(
            cx,
            attr.syntax(),
            Fact::Attr(AttrFact {
                steps,
                ty,
                priority,
                value: value.syntax().index(),
                reads,
            }),
        );
    }

    /// Returns the priority of a right-hand side and the contributed value:
    /// the argument of `default(...)` or `force(...)`, or the whole side.
    /// Returns `None` after reporting a marker with the wrong arguments.
    fn marker(&mut self, cx: &mut Cx, value: Expr<'p>) -> Option<(Priority, Expr<'p>)> {
        let plain = Some((Priority::Plain, value));
        let Expr::Call(call) = value else {
            return plain;
        };
        let Some(Expr::Name(name)) = call.callee() else {
            return plain;
        };
        let Some(token) = name.token() else {
            return plain;
        };
        let priority = match token.text() {
            "default" => Priority::Default,
            "force" => Priority::Force,
            _ => return plain,
        };
        if cx.lookup(token.text()).is_some() || self.lookup_value(cx.file, token.text()).is_some() {
            return plain;
        }
        let args: Vec<ast::Arg<'p>> = call.args().map(|a| a.args().collect()).unwrap_or_default();
        match args.as_slice() {
            [arg] if arg.name().is_none() => match arg.value() {
                Some(inner) => Some((priority, inner)),
                None => plain,
            },
            _ => {
                self.error(
                    cx.file,
                    Diagnostic::error(
                        "E0444",
                        call.syntax().span(),
                        format!("`{}` takes one positional argument", token.text()),
                    ),
                );
                self.synth_args(cx, &args);
                None
            }
        }
    }

    /// Adds the edges of a late contribution to the `self` graph of the
    /// body.
    fn late_contribution(
        &mut self,
        cx: &mut Cx,
        names: &[String],
        owner: String,
        reads: &[SelfRead],
        span: Span,
    ) {
        let SelfMode::Body(body) = &mut cx.self_mode else {
            return;
        };
        let Some((field, prefix)) = names.split_last() else {
            return;
        };
        let mut path = body
            .frames
            .last()
            .map(|f| f.path.clone())
            .unwrap_or_default();
        path.extend(prefix.iter().cloned());
        let label = format!("{owner}.{field}");
        let from = body.graph.node(path, field.clone(), label);
        for read in reads {
            let Some(frame) = body.frames.get(read.depth) else {
                continue;
            };
            let def = &self.types.structs[read.structure.index()];
            let Some(f) = def.fields.get(read.field) else {
                continue;
            };
            let label = format!("{}.{}", def.name, f.name);
            let to = body.graph.node(frame.path.clone(), f.name.clone(), label);
            body.graph.edges.push((from, to, span));
        }
    }

    /// Reports the cycles of the `self` graph of a body.
    pub(super) fn self_cycles(&mut self, file: FileId, graph: &SelfGraph) {
        let mut adj = vec![Vec::new(); graph.labels.len()];
        for &(from, to, _) in &graph.edges {
            adj[from].push(to);
        }
        for list in &mut adj {
            list.sort_unstable();
            list.dedup();
        }
        for cycle in graph::cycles(&adj) {
            let mut diagnostic: Option<Diagnostic> = None;
            for (i, &from) in cycle.iter().enumerate() {
                let to = cycle[(i + 1) % cycle.len()];
                let Some(&(_, _, span)) =
                    graph.edges.iter().find(|(f, t, _)| *f == from && *t == to)
                else {
                    continue;
                };
                let message = format!("`{}` reads `{}`", graph.labels[from], graph.labels[to]);
                diagnostic = Some(match diagnostic {
                    None => Diagnostic::error("E0607", span, "`self` dependencies form a cycle")
                        .with_label(None, span, message),
                    Some(d) => d.with_label(None, span, message),
                });
            }
            let Some(diagnostic) = diagnostic else {
                continue;
            };
            let mut chain: Vec<&str> = cycle.iter().map(|&n| graph.labels[n].as_str()).collect();
            chain.push(&graph.labels[cycle[0]]);
            self.error(
                file,
                diagnostic.with_note(format!("cycle: {}", chain.join(" -> "))),
            );
        }
    }

    fn block(&mut self, cx: &mut Cx, block: ast::Block<'p>, target: Target) {
        let Some(kind_token) = block.kind() else {
            return;
        };
        let kind = kind_token.text();
        let kind_span = kind_token.span();
        let label = block.label();
        let label_span = label.map_or(kind_span, Key::span);
        let label_fact = label.map(|l| match key_text(l) {
            Some(text) => LabelFact::Static(text),
            None => LabelFact::Dynamic,
        });
        if let Some(Key::Str(string)) = label {
            let saved = std::mem::replace(&mut cx.self_mode, SelfMode::Forbidden("a block label"));
            let reads = std::mem::take(&mut cx.reads);
            self.interpolations(cx, string);
            cx.self_mode = saved;
            cx.reads = reads;
        }
        let fields = self.body_fields(target);
        let clause = match target {
            Target::Struct(_) => fields.iter().find(|f| f.block.as_deref() == Some(kind)),
            _ => None,
        };
        let named = fields.iter().find(|f| f.name == kind);
        let segment = match &label_fact {
            Some(LabelFact::Static(text)) => text.clone(),
            _ => format!("#{}", block.syntax().index()),
        };
        let mapped = match (clause, named) {
            (Some(field), _) => {
                let ty = self.types.peel(field.ty);
                match self.types.get(ty).clone() {
                    Type::Map(k, v) => {
                        let target = match self.types.as_struct(v) {
                            Some(s) => Target::Struct(s),
                            None => Target::Map {
                                key: TypeId::ERROR,
                                value: TypeId::ERROR,
                            },
                        };
                        if label.is_none() {
                            self.needs_label(cx, kind, kind_span);
                        } else if let Some(s) = self.types.as_sum(k) {
                            self.label_variant(cx, s, label_fact.as_ref(), label_span);
                        }
                        Some((
                            BlockRule::Clause,
                            field.index,
                            vec![field.name.clone(), segment],
                            target,
                        ))
                    }
                    _ => self.tagged(cx, field, kind, label_fact.as_ref(), label_span),
                }
            }
            (None, Some(field)) => {
                let ty = self
                    .types
                    .unwrap_option(field.ty)
                    .unwrap_or_else(|| self.types.peel(field.ty));
                match self.types.get(ty).clone() {
                    Type::Struct(s) => {
                        self.no_label(cx, kind, label, label_span);
                        Some((
                            BlockRule::Field,
                            field.index,
                            vec![field.name.clone()],
                            Target::Struct(s),
                        ))
                    }
                    Type::Map(k, v) => {
                        self.no_label(cx, kind, label, label_span);
                        Some((
                            BlockRule::MapField,
                            field.index,
                            vec![field.name.clone()],
                            Target::Map { key: k, value: v },
                        ))
                    }
                    Type::Sum(_) => self.tagged(cx, field, kind, label_fact.as_ref(), label_span),
                    Type::Error => None,
                    _ => {
                        self.unknown_block(cx, target, &fields, kind, kind_span);
                        None
                    }
                }
            }
            (None, None) => {
                self.unknown_block(cx, target, &fields, kind, kind_span);
                None
            }
        };
        let Some((rule, field, segments, body_target)) = mapped else {
            if let Some(body) = block.body() {
                self.ignored_body(cx, body);
            }
            return;
        };
        self.fact(
            cx,
            block.syntax(),
            Fact::Block(BlockFact {
                rule,
                field,
                label: label_fact,
                target: body_target,
            }),
        );
        let Some(body) = block.body() else { return };
        if let SelfMode::Body(b) = &mut cx.self_mode {
            let mut path = b.frames.last().map(|f| f.path.clone()).unwrap_or_default();
            path.extend(segments);
            b.frames.push(Frame {
                target: body_target,
                path,
            });
        }
        let mark = cx.scope.len();
        self.statements(cx, body.statements(), body_target);
        cx.scope.truncate(mark);
        if let SelfMode::Body(b) = &mut cx.self_mode {
            b.frames.pop();
        }
    }

    /// Checks the statements of a block that maps to nothing, without a
    /// target, so that their own errors are still found.
    fn ignored_body(&mut self, cx: &mut Cx, body: ast::Body<'p>) {
        let error = Target::Map {
            key: TypeId::ERROR,
            value: TypeId::ERROR,
        };
        let mark = cx.scope.len();
        if let SelfMode::Body(b) = &mut cx.self_mode {
            b.frames.push(Frame {
                target: error,
                path: vec![format!("#{}", body.syntax().index())],
            });
        }
        self.statements(cx, body.statements(), error);
        if let SelfMode::Body(b) = &mut cx.self_mode {
            b.frames.pop();
        }
        cx.scope.truncate(mark);
    }

    fn tagged(
        &mut self,
        cx: &Cx,
        field: &BodyField,
        kind: &str,
        label: Option<&LabelFact>,
        span: Span,
    ) -> Option<(BlockRule, usize, Vec<String>, Target)> {
        let ty = self
            .types
            .unwrap_option(field.ty)
            .unwrap_or_else(|| self.types.peel(field.ty));
        let s = self.types.as_sum(ty)?;
        if label.is_none() {
            self.needs_label(cx, kind, span);
            return None;
        }
        let variant = self.label_variant(cx, s, label, span)?;
        let name = self.types.sum(s).variants[variant].name.clone();
        Some((
            BlockRule::Tagged,
            field.index,
            vec![field.name.clone(), format!(":{name}")],
            Target::Variant { sum: s, variant },
        ))
    }

    fn label_variant(
        &mut self,
        cx: &Cx,
        s: super::types::SumId,
        label: Option<&LabelFact>,
        span: Span,
    ) -> Option<usize> {
        let sum = self.types.sum(s);
        let text = match label {
            Some(LabelFact::Static(text)) => text.clone(),
            _ => {
                let name = sum.name.clone();
                self.error(
                    cx.file,
                    Diagnostic::error(
                        "E0439",
                        span,
                        format!("the label must name a variant of `{name}`"),
                    )
                    .with_help("write the variant name without interpolation"),
                );
                return None;
            }
        };
        match sum.variant(&text) {
            Some((index, _)) => Some(index),
            None => {
                let help = did_you_mean(&text, sum.variants.iter().map(|v| v.name.as_str()));
                let name = sum.name.clone();
                self.error(
                    cx.file,
                    Diagnostic::error(
                        "E0439",
                        span,
                        format!("`{text}` is not a variant of `{name}`"),
                    )
                    .with_optional_help(help),
                );
                None
            }
        }
    }

    fn needs_label(&mut self, cx: &Cx, kind: &str, span: Span) {
        self.error(
            cx.file,
            Diagnostic::error("E0437", span, format!("the `{kind}` block needs a label"))
                .with_help(format!("write `{kind} <label> {{ ... }}`")),
        );
    }

    fn no_label(&mut self, cx: &Cx, kind: &str, label: Option<Key<'p>>, span: Span) {
        if label.is_some() {
            self.error(
                cx.file,
                Diagnostic::error("E0438", span, format!("the `{kind}` block takes no label"))
                    .with_help(format!("write `{kind} {{ ... }}`")),
            );
        }
    }

    fn unknown_block(
        &mut self,
        cx: &Cx,
        target: Target,
        fields: &[BodyField],
        kind: &str,
        span: Span,
    ) {
        let candidates: Vec<&str> = fields
            .iter()
            .flat_map(|f| [Some(f.name.as_str()), f.block.as_deref()])
            .flatten()
            .collect();
        let help = did_you_mean(kind, candidates);
        let name = self.target_name(target);
        self.error(
            cx.file,
            Diagnostic::error("E0440", span, format!("`{name}` has no block `{kind}`"))
                .with_optional_help(help),
        );
    }

    fn include(
        &mut self,
        cx: &mut Cx,
        include: ast::Include<'p>,
        target: Target,
        included: &mut BTreeMap<FileId, Span>,
    ) {
        let span = include.syntax().span();
        let part_file = self
            .program
            .file(cx.file)
            .includes
            .get(&include.syntax().index())
            .copied();
        let Target::Struct(s) = target else {
            let name = self.target_name(target);
            self.error(
                cx.file,
                Diagnostic::error(
                    "E0442",
                    span,
                    format!("a part cannot be included into a body of `{name}`"),
                ),
            );
            return;
        };
        if let Some(file) = part_file {
            if let Some(&first) = included.get(&file) {
                let name = self.program.file(file).name.clone();
                self.report(
                    Some(cx.file),
                    Diagnostic::warning(
                        "W0703",
                        span,
                        format!("`{name}` is already included into this body"),
                    )
                    .with_label(None, first, "first included here")
                    .with_help("remove one of the includes"),
                );
            }
            included.insert(file, span);
            let unit = self.part_unit(file, s);
            self.fact(cx, include.syntax(), Fact::Include(IncludeFact::File(unit)));
            return;
        }
        let Some(value) = include.value() else { return };
        if let Expr::Str(string) = value
            && string.value().is_some()
        {
            return;
        }
        let expected = self.types.intern(Type::Part(s));
        let found = if let Expr::Part(_) = value {
            self.structural(cx, value, Some(expected), "an `include` expression")
        } else {
            self.structural(cx, value, None, "an `include` expression")
        };
        match self.types.get(self.types.peel(found)).clone() {
            Type::Part(a) if a == s => {}
            Type::Part(a) => {
                let (a, b) = (
                    self.types.structure(a).name.clone(),
                    self.types.structure(s).name.clone(),
                );
                self.error(
                    cx.file,
                    Diagnostic::error(
                        "E0442",
                        value.syntax().span(),
                        format!("cannot include a `Part<{a}>` into a body of `{b}`"),
                    )
                    .with_help(format!(
                        "include it into a `{a}` block, or make it a `Part<{b}>`"
                    )),
                );
            }
            _ => self.mismatch(cx.file, value.syntax().span(), expected, found),
        }
        self.fact(cx, include.syntax(), Fact::Include(IncludeFact::Value));
    }

    /// Returns the unit of the part file `file` included into a body of
    /// `s`, queueing it to be checked on first use.
    pub(super) fn part_unit(&mut self, file: FileId, s: StructId) -> UnitId {
        let (unit, new) = self.unit(file, Some(s));
        if new {
            self.parts.push((unit, file, s));
        }
        unit
    }

    /// Checks the queued part file units, including the ones they queue in
    /// turn.
    pub(super) fn check_parts(&mut self) {
        let mut next = 0;
        while let Some(&(unit, file, s)) = self.parts.get(next) {
            next += 1;
            let mut cx = Cx::new(unit, file, SelfMode::Forbidden("a part file"), None);
            let statements = self
                .program
                .file(file)
                .parsed
                .file()
                .items()
                .filter_map(|item| match item {
                    Item::Stmt(stmt) => Some(stmt),
                    _ => None,
                });
            self.top_body(&mut cx, statements, s);
        }
        self.parts.clear();
    }

    /// Checks the statements of an entry file as a body of the root struct.
    pub(super) fn entry_body(&mut self, file: FileId, root: StructId) -> UnitId {
        let unit = self.file_unit(file);
        let top = self.entry_top(file);
        let body = BodyCx {
            top,
            frames: vec![Frame {
                target: Target::Struct(root),
                path: Vec::new(),
            }],
            graph: SelfGraph::default(),
        };
        let mut cx = Cx::new(unit, file, SelfMode::Body(body), None);
        let mut included = BTreeMap::new();
        for item in self.program.file(file).parsed.file().items() {
            let Item::Stmt(statement) = item else {
                continue;
            };
            if let Stmt::Let(_) = statement {
                continue;
            }
            cx.limit = Some(statement.syntax().span().start);
            self.statement(&mut cx, statement, Target::Struct(root), &mut included);
        }
        if let SelfMode::Body(body) = std::mem::replace(&mut cx.self_mode, SelfMode::Forbidden(""))
        {
            self.self_cycles(file, &body.graph);
        }
        unit
    }

    /// Checks the defaults, function bodies and top-level `let` values of
    /// every file that may declare them.
    ///
    /// Prelude files come first, and every other file after the modules it
    /// imports. Within a file, `let` values come first and in source order.
    /// A `let` value can only use earlier declarations of its file, the
    /// prelude and imported modules, so each is checked before it is used
    /// and checking never recurses through a chain of `let` values.
    pub(super) fn check_declaration_bodies(&mut self) {
        let program = self.program;
        let adj: Vec<Vec<usize>> = program
            .files
            .iter()
            .map(|file| {
                let mut targets: Vec<usize> = file
                    .imports
                    .iter()
                    .filter_map(|link| link.target.map(FileId::index))
                    .collect();
                targets.sort_unstable();
                targets.dedup();
                targets
            })
            .collect();
        let others = graph::components(&adj)
            .into_iter()
            .flatten()
            .map(FileId::new)
            .filter(|&id| !matches!(self.role(id), super::Role::Part | super::Role::Prelude));
        let order: Vec<FileId> = program.prelude.iter().copied().chain(others).collect();
        for id in order {
            self.file_declarations(id);
        }
    }

    fn file_declarations(&mut self, id: FileId) {
        let file = self.program.file(id);
        let unit = self.file_unit(id);
        let items: Vec<Item<'p>> = file.parsed.file().items().collect();
        for item in &items {
            if let Item::Stmt(Stmt::Let(decl)) = item
                && let Some(g) = self.declared_global(id, decl.name(), decl.syntax().index())
            {
                self.let_type(g);
            }
        }
        for s in 0..self.types.structs.len() {
            if self.types.structs[s].file != id {
                continue;
            }
            let sid = StructId::new(s);
            let fields = self.types.structs[s].fields.clone();
            let mut cx = Cx::new(
                unit,
                id,
                SelfMode::Default(sid),
                Some(super::totality::Node::StructDefaults(sid)),
            );
            for field in fields {
                let Some(e) = field
                    .default
                    .and_then(|n| file.parsed.tree.node(n))
                    .and_then(Expr::cast)
                else {
                    continue;
                };
                cx.partial = true;
                self.expr(&mut cx, e, Some(field.ty));
                cx.reads.clear();
            }
        }
        for s in 0..self.types.sums.len() {
            if self.types.sums[s].file != id {
                continue;
            }
            let sid = super::types::SumId::new(s);
            let variants = self.types.sums[s].variants.clone();
            let mut cx = Cx::new(
                unit,
                id,
                SelfMode::Forbidden("a payload default"),
                Some(super::totality::Node::SumDefaults(sid)),
            );
            for param in variants.iter().flat_map(|v| v.fields.iter().flatten()) {
                if let Some(e) = param
                    .default
                    .and_then(|n| file.parsed.tree.node(n))
                    .and_then(Expr::cast)
                {
                    self.expr(&mut cx, e, Some(param.ty));
                }
            }
        }
        for item in &items {
            if let Item::Fn(decl) = item
                && let Some(g) = self.declared_global(id, decl.name(), decl.syntax().index())
            {
                self.fn_body(g);
            }
        }
    }

    /// Returns the top-level function or `let` that the declaration at
    /// node `node` of `file` declares, unless the name is declared twice
    /// and another declaration owns it.
    fn declared_global(
        &self,
        file: FileId,
        name: Option<crate::syntax::SyntaxToken<'_>>,
        node: usize,
    ) -> Option<super::types::GlobalId> {
        match self.scopes[file.index()].values.get(name?.text()) {
            Some(&ValueItem::Global(g)) if self.globals[g.index()].node == node => Some(g),
            _ => None,
        }
    }

    fn fn_body(&mut self, g: super::types::GlobalId) {
        let global = &self.globals[g.index()];
        let GlobalKind::Fn(sig) = &global.kind else {
            return;
        };
        let (file, node, params, ret) = (global.file, global.node, sig.params.clone(), sig.ret);
        let unit = self.file_unit(file);
        let owner = Some(super::totality::Node::Global(g));
        let tree = &self.program.file(file).parsed.tree;
        let mut defaults = Cx::new(
            unit,
            file,
            SelfMode::Forbidden("a parameter default"),
            owner,
        );
        for param in &params {
            if let Some(e) = param
                .default
                .and_then(|n| tree.node(n))
                .and_then(Expr::cast)
            {
                self.expr(&mut defaults, e, Some(param.ty));
            }
        }
        let mut cx = Cx::new(unit, file, SelfMode::Forbidden("a function body"), owner);
        let mut binds = Vec::new();
        for param in &params {
            let id = self.local(&cx, &param.name, param.ty, Vec::new());
            cx.scope.push((param.name.clone(), Binding::Local(id)));
            binds.push(id);
        }
        if let GlobalKind::Fn(sig) = &mut self.globals[g.index()].kind {
            sig.binds = binds;
        }
        let body = tree
            .node(node)
            .and_then(ast::FnDecl::cast)
            .and_then(ast::FnDecl::body);
        if let Some(body) = body {
            self.expr(&mut cx, body, Some(ret));
        }
    }
}

/// Returns the text of a key: the identifier, or the value of a string
/// without interpolation.
fn key_text(key: Key<'_>) -> Option<String> {
    match key {
        Key::Name(token) => Some(token.text().to_owned()),
        Key::Str(string) => string.value(),
    }
}