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
use super::kind::Kind;
use super::node::Node;
use ast::UseLookup;
use range::Range;
use Prelude;
use Type;

mod refine;

/// Runs type checking.
///
/// The type checking consists of 2 steps:
///
/// 1. Propagate types across graph nodes, check for direct conflicts
/// 2. After type propagation, check for missing or conflicting types
///
/// ### Step 1 - Propagate types
///
/// Step 1 runs as long any type information is propagated in the graph.
/// It stops when no further type information can be inferred.
///
/// This step is necessary to infer types of expressions, and can be quite complex.
///
/// When a node gets type information, it will no longer be checked.
/// Therefore, some nodes might delay setting a type to itself even it is known
/// because the node serves as a propagation point for other nodes.
///
/// For example, when declaring a local variable:
///
/// ```ignore
/// x := 2 + a
/// ```
///
/// It is known that a declaration always return `void`, but this knowledge is not always used.
/// Since the type of the left argument depends on the right one,
/// the assignment waits with setting type information until the type of the right expression
/// is known. Then it copies the type over to the left expression and then set itself to `void`.
///
/// ### Step 2 - After type propagation
///
/// This step is used to check conflicts between multiple ways of inferring types.
///
/// For example, the type of an `if` expression is inferred from the true block.
/// The type propagation step uses this assumption without checking the whole `if` expression.
/// After type propagation, all blocks in the `if` expression should have some type information,
/// but no further propagation is necessary, so it only need to check for consistency.
pub(crate) fn run(
    nodes: &mut Vec<Node>,
    prelude: &Prelude,
    use_lookup: &UseLookup,
) -> Result<(), Range<String>> {
    use std::collections::HashMap;

    // Keep an extra todo-list for nodes that are affected by type refinement.
    let mut todo: Vec<usize> = (0..nodes.len()).collect();
    // Keep an extra delay-errors map for nodes that should not report an error after all,
    // if the type refined turned out to match.
    let mut delay_errs: HashMap<usize, Range<String>> = HashMap::new();
    // Type propagation.
    let mut changed;
    loop {
        changed = false;
        // Keep track of the old length of the todo-list,
        // in order to separate tasks already done from new tasks.
        let todo_len = todo.len();
        'node: for i in 0..todo.len() {
            let i = todo[i];
            let kind = nodes[i].kind;
            let mut this_ty = None;
            match kind {
                // No further work required for these statements.
                Kind::Uses | Kind::Start | Kind::End => continue 'node,
                #[cfg(all(not(target_family = "wasm"), feature = "threading"))]
                Kind::Go => {
                    // Infer thread type from function.
                    if !nodes[i].children.is_empty() {
                        let ch = nodes[i].children[0];
                        if let Some(ref ty) = nodes[ch].ty {
                            this_ty = Some(Type::Thread(Box::new(ty.clone())))
                        }
                    }
                }
                Kind::Fn => {
                    if nodes[i].ty.is_some() {
                        // No further work is required for function.
                        continue 'node;
                    }
                    if let Some(ch) = nodes[i].find_child_by_kind(nodes, Kind::Expr) {
                        // If the block is unreachable at the end,
                        // this does not tell anything about the type of the function.
                        if nodes[ch].ty == Some(Type::Unreachable) {
                            todo.push(i);
                            continue 'node;
                        }
                        // Infer return type from body of function.
                        this_ty = nodes[ch].ty.clone();
                    }
                }
                Kind::CallArg => {
                    if nodes[i].children.is_empty() || nodes[i].item_ids() {
                        todo.push(i);
                        continue 'node;
                    }
                    let ch = nodes[i].children[0];
                    let expr_type = nodes[ch].ty.as_ref().map(|ty| nodes[i].inner_type(ty));
                    if let Some(parent) = nodes[i].parent {
                        // Remove previous delay errors.
                        if delay_errs.contains_key(&i) {
                            delay_errs.remove(&i);
                        }

                        // Take into account swizzling for the declared argument position.
                        let j = {
                            let mut sum = 0;
                            'inner: for &p_ch in &nodes[parent].children {
                                if p_ch == i {
                                    break 'inner;
                                }
                                if let Some(sw) =
                                    nodes[p_ch].find_child_by_kind(nodes, Kind::Swizzle)
                                {
                                    for &sw_ch in &nodes[sw].children {
                                        match nodes[sw_ch].kind {
                                            Kind::Sw0 | Kind::Sw1 | Kind::Sw2 | Kind::Sw3 => {
                                                sum += 1;
                                            }
                                            _ => {}
                                        }
                                    }
                                } else {
                                    sum += 1;
                                }
                            }
                            sum
                        };

                        // Check type against all arguments covered by swizzle.
                        let js = if nodes[ch].kind == Kind::Swizzle {
                            let mut sum = 0;
                            for &sw_ch in &nodes[ch].children {
                                match nodes[sw_ch].kind {
                                    Kind::Sw0 | Kind::Sw1 | Kind::Sw2 | Kind::Sw3 => {
                                        sum += 1;
                                    }
                                    _ => {}
                                }
                            }

                            let mut js = vec![];
                            for i in 0..sum {
                                js.push(j + i)
                            }
                            js
                        } else {
                            vec![j]
                        };

                        'inner2: for &j in &js {
                            if nodes[parent].kind == Kind::CallClosure {
                                // TODO: Check argument type against closure.
                                continue 'inner2;
                            }
                            if let Some(decl) = nodes[parent].declaration {
                                let arg = nodes[decl].children[j];
                                match (&expr_type, &nodes[arg].ty) {
                                    (&Some(ref ch_ty), &Some(ref arg_ty)) => {
                                        if !arg_ty.goes_with(ch_ty) {
                                            if !delay_errs.contains_key(&i) {
                                                delay_errs.insert(
                                                    i,
                                                    nodes[i].source.wrap(format!(
                                                        "Type mismatch (#100):\n\
                                                        Expected `{}`, found `{}`",
                                                        arg_ty.description(),
                                                        ch_ty.description()
                                                    )),
                                                );
                                            }
                                            todo.push(i);
                                            continue 'node;
                                        }
                                    }
                                    (&None, _) | (_, &None) => {}
                                }
                            } else if let Some(ref alias) = nodes[parent].alias {
                                use ast::FnAlias;

                                // External functions are treated as loaded in prelude.
                                if let Some(&FnAlias::Loaded(f)) = use_lookup
                                    .aliases
                                    .get(alias)
                                    .and_then(|map| map.get(nodes[parent].name().unwrap()))
                                {
                                    let f = &prelude.list[f];
                                    if let Some(ref ty) = expr_type {
                                        if !f.tys[j].goes_with(ty) {
                                            if !delay_errs.contains_key(&i) {
                                                delay_errs.insert(
                                                    i,
                                                    nodes[i].source.wrap(format!(
                                                        "Type mismatch (#150):\n\
                                                        Expected `{}`, found `{}`",
                                                        f.tys[j].description(),
                                                        ty.description()
                                                    )),
                                                );
                                            }
                                            todo.push(i);
                                            continue 'node;
                                        }
                                    }
                                }
                            } else if let Some(&f) =
                                prelude.functions.get(nodes[parent].name().unwrap())
                            {
                                let f = &prelude.list[f];
                                if let Some(ref ty) = expr_type {
                                    if !f.tys[j].goes_with(ty) {
                                        if !delay_errs.contains_key(&i) {
                                            delay_errs.insert(
                                                i,
                                                nodes[i].source.wrap(format!(
                                                    "Type mismatch (#200):\n\
                                                    Expected `{}`, found `{}`",
                                                    f.tys[j].description(),
                                                    ty.description()
                                                )),
                                            );
                                        }
                                        todo.push(i);
                                        continue 'node;
                                    }
                                }
                            }
                        }
                    }
                    this_ty = expr_type;
                }
                Kind::Call => {
                    if let Some(decl) = nodes[i].declaration {
                        refine::declaration(i, decl, nodes, &mut todo, &mut this_ty)?;

                        // If the type has not been refined, fall back to default type signature.
                        if this_ty.is_none() && nodes[i].ty.is_none() {
                            if let Some(ref ty) = nodes[decl].ty {
                                this_ty = Some(ty.clone());
                            }
                        }
                    } else if let Some(ref alias) = nodes[i].alias {
                        use ast::FnAlias;

                        // External functions are treated as loaded in prelude.
                        if let Some(&FnAlias::Loaded(f)) = use_lookup
                            .aliases
                            .get(alias)
                            .and_then(|map| map.get(nodes[i].name().unwrap()))
                        {
                            let f = &prelude.list[f];
                            if f.ext.is_empty() {
                                this_ty = Some(f.ret.clone());
                            } else {
                                refine::prelude(i, f, nodes, &mut todo, &mut this_ty)?;

                                // If the type has not been refined, fall back to default type signature.
                                if this_ty.is_none() && nodes[i].ty.is_none() {
                                    this_ty = Some(f.ret.clone());
                                }
                            }
                        }
                    } else if let Some(&f) = prelude.functions.get(nodes[i].name().unwrap()) {
                        let f = &prelude.list[f];
                        if f.ext.is_empty() {
                            this_ty = Some(f.ret.clone());
                        } else {
                            refine::prelude(i, f, nodes, &mut todo, &mut this_ty)?;

                            // If the type has not been refined, fall back to default type signature.
                            if this_ty.is_none() && nodes[i].ty.is_none() {
                                this_ty = Some(f.ret.clone());
                            }
                        }
                    }
                }
                Kind::CallClosure => {
                    if let Some(item) = nodes[i].find_child_by_kind(nodes, Kind::Item) {
                        if nodes[item].item_ids() {
                            todo.push(i);
                            continue 'node;
                        }
                        if let Some(decl) = nodes[item].declaration {
                            if let Some(ref ty) = nodes[decl].ty {
                                if let Some(ty) = ty.closure_ret_ty() {
                                    this_ty = Some(ty);
                                } else {
                                    return Err(nodes[item].source.wrap(format!(
                                        "Type mismatch (#250):\n\
                                                Expected `closure`, found `{}`",
                                        ty.description()
                                    )));
                                }
                            }
                        }
                    }
                }
                Kind::Assign => {
                    let left = match nodes[i].find_child_by_kind(nodes, Kind::Left) {
                        None => {
                            todo.push(i);
                            continue 'node;
                        }
                        Some(x) => x,
                    };
                    let right = match nodes[i].find_child_by_kind(nodes, Kind::Right) {
                        None => {
                            todo.push(i);
                            continue 'node;
                        }
                        Some(x) => x,
                    };
                    if nodes[right].item_ids() {
                        todo.push(i);
                        continue 'node;
                    }
                    nodes[left].ty = match (&nodes[left].ty, &nodes[right].ty) {
                        (&None, &Some(ref right_ty)) => {
                            // Make assign return void since there is no more need for checking.
                            this_ty = Some(Type::Void);
                            Some(right_ty.clone())
                        }
                        (&Some(ref left_ty), &Some(ref right_ty)) => {
                            if right_ty.goes_with(left_ty) {
                                if !nodes[left].children.is_empty() {
                                    // Tell the item that it needs refinement.
                                    let it = nodes[left].children[0];
                                    todo.push(it);
                                    // Tell all nodes that uses the item as declaration that
                                    // they need refinement.
                                    for j in it + 1..nodes.len() {
                                        if let Some(decl) = nodes[j].declaration {
                                            if decl == it {
                                                todo.push(j)
                                            }
                                        }
                                    }
                                }
                                this_ty = Some(Type::Void);
                                Some(right_ty.clone())
                            } else {
                                // TODO: Type conflict between left and refined right.
                                //       Might be caught by later rules.
                                todo.push(i);
                                continue 'node;
                            }
                        }
                        _ => {
                            todo.push(i);
                            continue 'node;
                        }
                    };
                    changed = true;
                }
                Kind::Item => {
                    if nodes[i].item_ids() {
                        todo.push(i);
                        continue 'node;
                    }
                    if let Some(decl) = nodes[i].declaration {
                        match nodes[decl].kind {
                            Kind::Sum
                            | Kind::Min
                            | Kind::Max
                            | Kind::Any
                            | Kind::All
                            | Kind::Sift
                            | Kind::Vec4UnLoop
                            | Kind::ForN
                            | Kind::LinkFor => {
                                if nodes[i].try {
                                    return Err(nodes[i].source.wrap(
                                        "Type mismatch (#300):\n\
                                        Can not use `?` with a number"
                                            .into(),
                                    ));
                                }
                                // All indices are numbers.
                                this_ty = Some(Type::F64);
                            }
                            Kind::Arg => {
                                this_ty = Some(
                                    nodes[i]
                                        .inner_type(nodes[decl].ty.as_ref().unwrap_or(&Type::Any)),
                                );
                            }
                            _ => {
                                if let Some(ref ty) = nodes[decl].ty {
                                    this_ty = Some(nodes[i].inner_type(ty));
                                }
                                if this_ty.is_some() {
                                    // Change the type of left expression,
                                    // to get a more accurate type.
                                    if let Some(parent) = nodes[i].parent {
                                        if nodes[parent].kind == Kind::Left {
                                            nodes[parent].ty = this_ty.clone();
                                        }
                                    }
                                }
                            }
                        }
                    } else if let Some(parent) = nodes[i].parent {
                        if nodes[parent].kind == Kind::Left {
                            if let Some(ref ty) = nodes[parent].ty {
                                // Get type from assignment left expression.
                                this_ty = Some(ty.clone());
                            }
                        }
                    }
                }
                Kind::Return
                | Kind::Val
                | Kind::Expr
                | Kind::Cond
                | Kind::Left
                | Kind::Right
                | Kind::ElseIfCond
                | Kind::Grab
                | Kind::Add
                | Kind::Mul
                | Kind::Pow => {
                    if nodes[i].children.is_empty() {
                        // No further work is required.
                        continue 'node;
                    }
                    let ch = nodes[i].children[0];
                    if nodes[ch].item_ids() {
                        todo.push(i);
                        continue 'node;
                    }
                    let ty = match nodes[ch].ty {
                        None => {
                            todo.push(i);
                            continue 'node;
                        }
                        Some(ref ty) => ty.clone(),
                    };
                    if nodes[i].kind == Kind::Grab && ty == Type::Void {
                        return Err(nodes[i].source.wrap(
                            "Type mismatch (#325):\n\
                                Expected something, found `void`"
                                .to_string(),
                        ));
                    }
                    if nodes[ch].kind == Kind::Return {
                        // Find function and check return type.
                        let mut p = i;
                        loop {
                            p = match nodes[p].parent {
                                None => break,
                                Some(p) => p,
                            };
                            if nodes[p].kind == Kind::Fn || nodes[p].kind == Kind::Closure {
                                if nodes[p].ty.is_none() {
                                    // Infer return type of function.
                                    nodes[p].ty = Some(ty.clone());
                                } else if let Some(ref fn_ty) = nodes[p].ty {
                                    if !fn_ty.goes_with(&ty) {
                                        return Err(nodes[ch].source.wrap(format!(
                                            "Type mismatch (#350):\n\
                                            Expected `{}`, found `{}`",
                                            fn_ty.description(),
                                            ty.description()
                                        )));
                                    }
                                }
                                break;
                            }
                        }
                        this_ty = Some(Type::Unreachable);
                    } else {
                        // Propagate type.
                        this_ty = Some(nodes[i].inner_type(&ty));
                    }
                }
                Kind::Compare => {
                    let left = match nodes[i].find_child_by_kind(nodes, Kind::Left) {
                        None => {
                            todo.push(i);
                            continue 'node;
                        }
                        Some(x) => x,
                    };
                    if nodes[left].item_ids() {
                        todo.push(i);
                        continue 'node;
                    }
                    match nodes[left].ty {
                        Some(Type::Any) => {
                            this_ty = Some(Type::Any);
                        }
                        Some(Type::Secret(_)) => {
                            this_ty = Some(Type::Secret(Box::new(Type::Bool)));
                        }
                        Some(_) => {
                            this_ty = Some(Type::Bool);
                        }
                        _ => {}
                    }
                }
                Kind::Block | Kind::TrueBlock | Kind::ElseIfBlock | Kind::ElseBlock => {
                    if nodes[i].children.is_empty() {
                        this_ty = Some(Type::Void);
                    }
                    if let Some(&ch) = nodes[i].children.last() {
                        if nodes[ch].item_ids() {
                            todo.push(i);
                            continue 'node;
                        }
                        if let Some(ref ty) = nodes[ch].ty {
                            this_ty = Some(nodes[i].inner_type(ty));
                        }
                    }
                }
                Kind::Sift => {
                    // Infer type from body.
                    let ch = if let Some(ch) = nodes[i].find_child_by_kind(nodes, Kind::Block) {
                        ch
                    } else {
                        todo.push(i);
                        continue 'node;
                    };
                    if let Some(ref ty) = nodes[ch].ty {
                        this_ty = Some(Type::Array(Box::new(ty.clone())));
                    }
                }
                Kind::X | Kind::Y | Kind::Z | Kind::W | Kind::N => {
                    if nodes[i].children.is_empty() {
                        todo.push(i);
                        continue 'node;
                    }
                    let ch = nodes[i].children[0];
                    if nodes[ch].item_ids() {
                        todo.push(i);
                        continue 'node;
                    }

                    let expr_type = nodes[ch].ty.as_ref().map(|ty| nodes[i].inner_type(ty));
                    if let Some(ref ty) = expr_type {
                        if !ty.goes_with(&Type::F64) {
                            return Err(nodes[i].source.wrap(format!(
                                "Type mismatch (#700):\nExpected `f64`, found `{}`",
                                expr_type.as_ref().unwrap().description()
                            )));
                        }
                    }
                    this_ty = expr_type;
                }
                Kind::If => {
                    let tb = match nodes[i].find_child_by_kind(nodes, Kind::TrueBlock) {
                        None => {
                            todo.push(i);
                            continue 'node;
                        }
                        Some(tb) => tb,
                    };
                    let true_type = match nodes[tb].ty.as_ref().map(|ty| nodes[i].inner_type(ty)) {
                        None => {
                            todo.push(i);
                            continue 'node;
                        }
                        Some(true_type) => true_type,
                    };

                    this_ty = Some(true_type);
                }
                Kind::Arg => {
                    if nodes[i].ty.is_none() {
                        this_ty = Some(Type::Any);
                    } else {
                        // No further work needed for this node.
                        continue 'node;
                    }
                }
                Kind::Closure => {
                    let mut lts = vec![];
                    let mut tys = vec![];
                    let mut ret: Option<Type> = None;
                    let mut all_args = true;
                    for &ch in &nodes[i].children {
                        if nodes[ch].kind == Kind::Arg {
                            if let Some(ref ty) = nodes[ch].ty {
                                use Lt;

                                lts.push(Lt::Default);
                                tys.push(ty.clone());
                            } else {
                                all_args = false;
                                break;
                            }
                        }
                        if nodes[ch].kind == Kind::Expr {
                            ret = nodes[ch].ty.clone();
                        }
                    }
                    if all_args && ret.is_some() {
                        use Dfn;

                        this_ty = Some(Type::Closure(Box::new(Dfn {
                            lts,
                            tys,
                            ret: ret.unwrap(),
                            ext: vec![],
                            lazy: crate::LAZY_NO,
                        })));
                    }
                }
                _ => {}
            }
            if this_ty.is_some() {
                if let (&Some(ref old_ty), &Some(ref new_ty)) = (&nodes[i].ty, &this_ty) {
                    if old_ty != new_ty {
                        // If type was refined, propagate changes to parent.
                        if let Some(parent) = nodes[i].parent {
                            // If the type of the parent is not set,
                            // then there is no need to add it to the todo-list,
                            // since it will be covered by the default loop.
                            if nodes[parent].ty.is_some() {
                                todo.push(parent);
                            }
                        }
                    }
                }
                nodes[i].ty = this_ty;
                changed = true;
            } else {
                todo.push(i);
            }
        }

        // Remove old tasks.
        for i in (0..todo_len).rev() {
            todo.swap_remove(i);
        }
        // There must be a change to continue checking,
        // even with type refinement, because the todo-list
        // waits for other changes to happen.
        if !changed {
            break;
        }

        // Prepare todo-list for binary search.
        todo.sort_unstable();
        todo.dedup();
    }

    // Report one delayed error, if any.
    if !delay_errs.is_empty() {
        return Err(delay_errs.values().next().unwrap().clone());
    }

    // After type propagation.
    for i in 0..nodes.len() {
        let kind = nodes[i].kind;
        match kind {
            Kind::Fn => {
                if let Some(ref ty) = nodes[i].ty {
                    // Check inferred type matches the one of the block.
                    // This is used by mathematical expressions where return type is inferred.
                    if let Some(ch) = nodes[i].find_child_by_kind(nodes, Kind::Expr) {
                        if let Some(ref ch_ty) = nodes[ch].ty {
                            if !ty.goes_with(ch_ty) {
                                return Err(nodes[ch].source.wrap(format!(
                                    "Type mismatch (#750):\nExpected `{}`, found `{}`",
                                    ty.description(),
                                    ch_ty.description()
                                )));
                            }
                        }
                    }

                    // Check all return statements.
                    let mut found_return = false;
                    check_fn(i, nodes, ty, &mut found_return)?;
                    // Report if there is no return statement.
                    if !found_return
                        && ty != &Type::Void
                        && nodes[i].find_child_by_kind(nodes, Kind::Expr).is_none()
                    {
                        return Err(nodes[i].source.wrap(format!(
                            "Type mismatch (#775):\nExpected `{}`, found `void`",
                            ty.description()
                        )));
                    }
                } else {
                    return Err(nodes[i].source.wrap(format!(
                        "Type mismatch (#800):\nCould not infer type of function `{}`",
                        nodes[i].name().unwrap()
                    )));
                }
            }
            #[cfg(all(not(target_family = "wasm"), feature = "threading"))]
            Kind::Go => {
                if !nodes[i].children.is_empty() {
                    if let Some(decl) = nodes[nodes[i].children[0]].declaration {
                        match nodes[decl].ty {
                            None | Some(Type::Void) => {
                                return Err(nodes[i].source.wrap(format!(
                                    "Type mismatch (#900):\nRequires `->` on `{}`",
                                    nodes[decl].name().unwrap()
                                )));
                            }
                            _ => {}
                        }
                    }
                }
            }
            Kind::If => check_if(i, nodes)?,
            Kind::Assign => {
                use ast::AssignOp;

                match nodes[i].op {
                    Some(AssignOp::Add) | Some(AssignOp::Sub) => {
                        let left = nodes[i].find_child_by_kind(nodes, Kind::Left).unwrap();
                        let right = nodes[i].find_child_by_kind(nodes, Kind::Right).unwrap();
                        if let Some(ref left_ty) = nodes[left].ty {
                            if let Some(ref right_ty) = nodes[right].ty {
                                if !left_ty.add_assign(right_ty) {
                                    return Err(nodes[i].source.wrap(format!(
                                        "Type mismatch (#1000):\n\
                                        Assignment operator can not be used with `{}` and `{}`",
                                        left_ty.description(),
                                        right_ty.description()
                                    )));
                                }
                            }
                        }
                    }
                    _ => {}
                }
            }
            Kind::Block => {
                // Make sure all results are used.
                // TODO: If the block is the body of a for loop,
                // then the last child node should be checked too.
                let n = nodes[i].children.len();
                if n == 0 {
                    continue;
                }
                let children = if let Some(parent) = nodes[i].parent {
                    match nodes[parent].kind {
                        Kind::Fn => match nodes[parent].ty {
                            Some(Type::Void) => &nodes[i].children,
                            None => continue,
                            _ => &nodes[i].children[0..n - 1],
                        },
                        _ => &nodes[i].children[0..n - 1],
                    }
                } else {
                    &nodes[i].children[0..n - 1]
                };
                for &ch in children.iter() {
                    if let Kind::Return = nodes[ch].kind {
                        continue;
                    };
                    if let Some(ref ty) = nodes[ch].ty {
                        if ty != &Type::Void && ty != &Type::Unreachable {
                            return Err(nodes[ch].source.wrap(format!(
                                "Type mismatch (#1100):\nUnused result `{}`",
                                ty.description()
                            )));
                        }
                    }
                }
            }
            Kind::Swizzle => {
                if let Some(ch) = nodes[i].find_child_by_kind(nodes, Kind::Expr) {
                    let expr_type = nodes[ch].ty.as_ref().map(|ty| nodes[ch].inner_type(ty));
                    if let Some(ref ty) = expr_type {
                        if !ty.goes_with(&Type::Vec4) {
                            return Err(nodes[ch].source.wrap(format!(
                                "Type mismatch (#1200):\nExpected `vec4`, found `{}`",
                                expr_type.as_ref().unwrap().description()
                            )));
                        }
                    }
                }
            }
            _ => {}
        }
    }
    Ok(())
}

/// Checks all returns recursively in function.
fn check_fn(
    n: usize,
    nodes: &[Node],
    ty: &Type,
    found_return: &mut bool,
) -> Result<(), Range<String>> {
    for &ch in &nodes[n].children {
        match nodes[ch].kind {
            Kind::Return => {
                if let Some(ref ret_ty) = nodes[ch].ty {
                    if !ty.goes_with(ret_ty) {
                        return Err(nodes[ch].source.wrap(format!(
                            "Type mismatch (#1200):\nExpected `{}`, found `{}`",
                            ty.description(),
                            ret_ty.description()
                        )));
                    }
                }
                *found_return = true;
            }
            Kind::ReturnVoid => {
                if !ty.goes_with(&Type::Void) {
                    return Err(nodes[ch].source.wrap(format!(
                        "Type mismatch (#1300):\nExpected `{}`, found `{}`",
                        ty.description(),
                        Type::Void.description()
                    )));
                }
                *found_return = true;
            }
            Kind::Item => {
                if nodes[ch].name().as_ref().map(|n| &***n == "return") == Some(true) {
                    if let Some(parent) = nodes[ch].parent {
                        if nodes[parent].kind == Kind::Left {
                            if let Some(parent) = nodes[parent].parent {
                                if nodes[parent].kind == Kind::Assign {
                                    if let Some(ref ret_ty) = nodes[ch].ty {
                                        if !ty.goes_with(ret_ty) {
                                            return Err(nodes[ch].source.wrap(format!(
                                                "Type mismatch (#1250):\nExpected `{}`, found `{}`",
                                                ty.description(),
                                                ret_ty.description()
                                            )));
                                        }
                                    }
                                    *found_return = true;
                                }
                            }
                        }
                    }
                }
            }
            Kind::Closure => {
                continue;
            }
            _ => {}
        }
        check_fn(ch, nodes, ty, found_return)?;
    }
    Ok(())
}

fn check_if(n: usize, nodes: &[Node]) -> Result<(), Range<String>> {
    if let Some(ch) = nodes[n].find_child_by_kind(nodes, Kind::Cond) {
        if let Some(ref cond_ty) = nodes[ch].ty {
            if !Type::Bool.goes_with(cond_ty) {
                return Err(nodes[ch].source.wrap(format!(
                    "Type mismatch (#1400):\nExpected `{}`, found `{}`",
                    Type::Bool.description(),
                    cond_ty.description()
                )));
            }
        }
    }

    // The type of ifs are inferred from the true block.
    let true_type = match nodes[n].ty {
        None => return Ok(()),
        Some(ref ty) => ty,
    };

    for &ch in &nodes[n].children {
        if let Kind::ElseIfCond = nodes[ch].kind {
            if let Some(ref cond_ty) = nodes[ch].ty {
                if !Type::Bool.goes_with(cond_ty) {
                    return Err(nodes[ch].source.wrap(format!(
                        "Type mismatch (#1500):\nExpected `{}`, found `{}`",
                        Type::Bool.description(),
                        cond_ty.description()
                    )));
                }
            }
        } else if let Kind::ElseIfBlock = nodes[ch].kind {
            if let Some(ref else_if_type) = nodes[ch].ty {
                if !else_if_type.goes_with(true_type) {
                    return Err(nodes[ch].source.wrap(format!(
                        "Type mismatch (#1600):\nExpected `{}`, found `{}`",
                        true_type.description(),
                        else_if_type.description()
                    )));
                }
            }
        }
    }

    if let Some(eb) = nodes[n].find_child_by_kind(nodes, Kind::ElseBlock) {
        if let Some(ref else_type) = nodes[eb].ty {
            if !else_type.goes_with(true_type) {
                return Err(nodes[eb].source.wrap(format!(
                    "Type mismatch (#1700):\nExpected `{}`, found `{}`",
                    true_type.description(),
                    else_type.description()
                )));
            }
        }
    }

    Ok(())
}