kailua_check 1.1.0

Type checker for Kailua
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
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
-- Basic tests for the Kailua type checker.

--8<-- funccall-nil
local p
p() --@< Error: Tried to call a non-function `nil`
--! error

--8<-- funccall-func
local function p() end
p()
--! ok

--8<-- funccall-func-too-many-args
local function p() end
p(3) --@< Error: The type `function() --> ()` cannot be called
     --@^ Cause: Cannot give more than 0 argument(s) to the function
     --@^^ Note: The other type originates here
--! error

--8<-- funccall-func-too-less-args-1 -- feature:!no_implicit_func_sig
local function p(x)
    x = x + 1
end
p() --@< Error: The type `function(x: <unknown type>) --> ()` cannot be called
    --@^ Cause: First function argument cannot be omitted because its type is `<unknown type>`
    --@^^ Note: The other type originates here
--! error

--8<-- funccall-func-too-less-args-2
local function p(x) --: integer
    x = x + 1
end
p() --@< Error: The type `function(x: integer) --> ()` cannot be called
    --@^ Cause: First function argument cannot be omitted because its type is `integer`
    --@^^ Note: The other type originates here
--! error

--8<-- funccall-func-too-less-args-3
local function p(x) --: integer?
    x = x + 1 --@< Error: Cannot apply + operator to `integer?` and `1`
              --@^ Cause: `integer?` is not a subtype of `number`
              --@^^ Error: Cannot assign `number` into `integer?`
              --@^^^ Note: The other type originates here
end
p()
--! error

--8<-- funccall-func-too-less-args-4
local function p(x) --: integer!
    x = x + 1
end
p() --@< Error: The type `function(x: integer!) --> ()` cannot be called
    --@^ Cause: First function argument cannot be omitted because its type is `integer!`
    --@^^ Note: The other type originates here
--! error

--8<-- funccall-var-outside-of-scope-1
local c
if c then
    local p
end
p() --@< Error: Global or local variable `p` is not defined
--! error

--8<-- funccall-var-outside-of-scope-2
local c = true
if c then
    local p
end
p() --@< Error: Global or local variable `p` is not defined
--! error

--8<-- funccall-var-outside-of-scope-3
local c = false
if c then
    local p
end
p() --@< Error: Global or local variable `p` is not defined
--! error

--8<-- funccall-var-outside-of-scope-4
local c --: boolean?
if c then
    local p
end
p() --@< Error: Global or local variable `p` is not defined
--! error

--8<-- funccall-integer-or-nil-1
local c, p
if c then p = 4 end
p() --@< Error: Tried to call a non-function `nil`
--! error

--8<-- funccall-integer-or-nil-2
local c --: boolean?
local p
if c then p = 4 end
p() --@< Error: Tried to call a non-function `4`
--! error

--8<-- funccall-undefined
p() --@< Error: Global or local variable `p` is not defined
--! error

--8<-- funccall-function
local f = function() end --: function
f() --@< Error: Cannot call `function` without further type information; specify more detailed type, or use `--# assume` as a last resort
--! error

--8<-- funccall-dynamic
--# assume p: WHATEVER
p()
--! ok

--8<-- methodcall-dynamic
--# assume s: WHATEVER
local p = s:find('hello')
--! ok

--8<-- add-number-integer
--# assume p: number
local x = p + 3
--! ok

--8<-- add-number-string
--# assume p: number
local x = p + 'foo' --@< Error: Cannot apply + operator to `number` and `"foo"`
                    --@^ Cause: `"foo"` is not a subtype of `number`
--! error

--8<-- add-number-func
local function p() end
local x = p + 'foo' --@< Error: Cannot apply + operator to `function() --> ()` and `"foo"`
                    --@^ Cause: `function() --> ()` is not a subtype of `number`
                    --@^^ Cause: `"foo"` is not a subtype of `number`
--! error

--8<-- arith-integer
--# assume p: integer
p = 2 + 3 * (4 + 5 - 6) % 7
--! ok

--8<-- arith-integer-implicit-nil
--# assume p: integer
local q = p + p --: integer!
--! ok

--8<-- arith-integer-explicit-nil
--# assume p: integer?
local p = p + p --: integer! --@< Error: Cannot apply + operator to `integer?` and `integer?`
                             --@^ Cause: `integer?` is not a subtype of `number`
                             --@^^ Cause: `integer?` is not a subtype of `number`
                             --@^^^ Error: Cannot assign `number` into `integer!`
                             --@^^^^ Note: The other type originates here
--! error

--8<-- arith-integer-no-nil
--# assume p: integer!
local p = p + p --: integer!
--! ok

--8<-- lt-number-integer-1
--# assume p: number
local x = p < 3
--! ok

--8<-- lt-number-integer-2
--# assume p: integer
local x = 2.17 < p
--! ok

--8<-- lt-integer-number-1
--# assume p: integer
local x = p < 3.14
--! ok

--8<-- lt-integer-number-2
--# assume p: number
local x = 2 < p
--! ok

--8<-- lt-string-string
--# assume p: string
local x = p < 'string'
--! ok

--8<-- lt-string-number
--# assume p: string
local x = p < 3.14 --@< Error: Operands `string` and `number` to < operator should be both numbers or both strings
--! error

--8<-- lt-string-or-number
--# assume p: string|number
local x = p < 3.14 --@< Error: Operand `(number|string)` to < operator should be either numbers or strings but not both
--! error

--8<-- lt-string-or-number-both
--# assume p: 'hello'|number
--# assume q: string|integer
local x = p < q --@< Error: Operand `(number|"hello")` to < operator should be either numbers or strings but not both
                --@^ Error: Operand `(integer|string)` to < operator should be either numbers or strings but not both
--! error

--8<-- lt-func-number
local function p() end
local x = p < 3.14 --@< Error: Cannot apply < operator to `function() --> ()` and `number`
--! error

--8<-- lt-error -- exact
local x = f() --@< Error: Global or local variable `f` is not defined
local y = 3 < x
--! error

--8<-- unknown-type
--# assume p: unknown_type --@< Error: Type `unknown_type` is not defined
--! error

--8<-- add-integer-integer
local p = 3 + 4
--! ok

--8<-- add-integer-string
local p = 3 + 'foo' --@< Error: Cannot apply + operator to `3` and `"foo"`
                    --@^ Cause: `"foo"` is not a subtype of `number`
--! error

--8<-- add-boolean-integer
local p = true + 7 --@< Error: Cannot apply + operator to `true` and `7`
                   --@^ Cause: `true` is not a subtype of `number`
--! error

--8<-- index-empty-with-integer
local p = ({})[3] --@< Error: Cannot index `{...}` with `3`
--! error

--8<-- index-intrec-with-integer
local p = ({[3] = 4})[3]
--! ok

--8<-- index-intrec-with-integer-no-key
local p = ({[2] = 4})[3] --@< Error: Cannot index `{2: 4, ...}` with `3`
--! error

--8<-- index-map-with-integer
local t = {[3] = 4, [8] = 5} --: map<integer, integer>
local p = t[3]
--! ok

--8<-- index-map-with-integer-type
local t = {[3] = 'four', [8] = 'five'} --: map<integer, string>
local p = t[3] + 3 --@< Error: Cannot apply + operator to `string` and `3`
                   --@^ Cause: `string` is not a subtype of `number`
--! error

--8<-- index-map-with-integer-no-key
local t = {[2] = 4, [8] = 5} --: map<integer, integer>
local p = t[3]
--! ok

--8<-- index-map-with-integer-no-subtype
local t = {[2] = 4, [8] = 5} --: map<integer, integer>
local p = t.string --@< Error: Cannot index `map<integer, integer>` with `"string"`
--! error

--8<-- index-empty-with-name
local p = ({}).a --@< Error: Missing key "a" in `{...}`
--! error

--8<-- index-rec-with-name
local p = ({a = 4}).a
--! ok

--8<-- index-rec-with-name-no-key
local p = ({a = 4}).b --@< Error: Missing key "b" in `{a: 4, ...}`
--! error

--8<-- index-rec-with-string
--# assume x: string
local p = ({a = 4})[x]
--@^ Error: Cannot index `{a: 4, ...}` with `string`
--! error

--8<-- index-rec-with-integer
local a = {}
local k = 1
local x = a[k] --@< Error: Cannot index `{...}` with `integer`
--! error

--8<-- index-rec-with-weird-string
--# assume x: 'not ice'
local p = ({['not ice'] = 4})[x]
--! ok

--8<-- index-rec-with-weird-string-error
--# assume x: 'ice'
local p = ({['not ice'] = 4})[x] --@< Error: Missing key "ice" in `{`not ice`: 4, ...}`
--! error

--8<-- index-rec-or-nil
local x = {a = 42} --: {a: integer}?
local p = x.a --@< Error: Tried to index a non-table type `{a: integer}?`
--! error

--8<-- index-table
local x = {a = 'foo'} --: table
local p = x.a --@< Error: Cannot index `table` without further type information; specify more detailed type, or use `--# assume` as a last resort
--! error

--8<-- index-unknown
--# open `internal kailua_test`
local x = kailua_test.gen_tvar()
local p = x.a --@< Error: The type `<unknown type>` is tabular but not known enough to index
--! error

--8<-- methodcall-empty
local p = ({}):hello() --@< Error: Missing key "hello" in `{...}`
--! error

--8<-- methodcall-rec-1 -- feature:no_implicit_func_sig
local x = {hello = function(a) end}
--@^ Error: The type for this argument in the anonymous function is missing but couldn't be inferred from the calls
local p = x:hello()
--! error

--8<-- methodcall-rec-1-type-1 -- feature:no_implicit_func_sig
-- TODO should this be inferrable?
local x = {hello = function(a) end} --: {hello: function(table)}
--@^ Error: The type for this argument in the anonymous function is missing but couldn't be inferred from the calls
local p = x:hello()
--! error

--8<-- methodcall-rec-1-type-2
local x = {}
--v function(a: table)
local function hello(a) end
x.hello = hello
local p = x:hello()
--! ok

--8<-- methodcall-rec-2
local x = {hello = --v function(a: table!, b: integer!)
                   function(a, b) end}
local p = x:hello() --@< Error: The type `function(a: table!, b: integer!) --> ()` cannot be called
                    --@^ Cause: Second method argument cannot be omitted because its type is `integer!`
                    --@^^ Note: The other type originates here
--! error

--8<-- methodcall-rec-3
local x = {hello = --v function(a: table!, b: integer!)
                   function(a, b) end}
local p = x:hello(4)
--! ok

--8<-- methodcall-rec-4
local x = {hello = --v function(a: table!, b: integer!)
                   function(a, b) end}
local p = x:hello('string') --@< Error: The type `function(a: table!, b: integer!) --> ()` cannot be called
                            --@^ Cause: First method argument `"string"` is not a subtype of `integer!`
                            --@^^ Note: The other type originates here
--! error

--8<-- methodcall-rec-5
local x = {hello = function() end}
local p = x:hello() --@< Error: The type `function() --> ()` cannot be called
                    --@^ Cause: Cannot give more than 0 argument(s) including `self` to the method
                    --@^^ Note: The other type originates here
--! error

--8<-- methodcall-integer
local s = (42):char() --@< Error: Tried to index a non-table type `42`
--! error

--8<-- index-func
local p = (function() end)[3] --@< Error: Tried to index a non-table type `function() --> ()`
--! error

--8<-- delayed-init-nil
local f
f = nil
--! ok

--8<-- delayed-init-string
local f
f = 'hello?'
--! ok

--8<-- reinit-nil-string-implicit
local f = nil
f = 'hello?' --@< Error: Cannot assign `"hello?"` into `nil`
             --@^ Note: The other type originates here
--! error

--8<-- reinit-nil-string-explicit
local f = nil --: string
f = 'hello?'
--! ok

--8<-- reinit-string-nil-implicit
local x = 'hello?' --: string
local f = x
f = nil
--! ok

--8<-- reinit-string-nil-explicit
local x = 'hello?' --: string
local f = x --: string
f = nil
--! ok

--8<-- reinit-func-func-1
local f = function() end
f = function() return 54 end --@< Error: Attempted to return a type `(54)` which is incompatible to given return type `()`
                             --@^ Cause: Cannot return more than 0 value(s)
                             --@^^ Note: The other type originates here
--! error

--8<-- reinit-func-func-2
local f = function() end
f = function() --> integer --@< Error: Cannot assign `function() --> integer` into `function() --> ()`
    return 54
end                        --@^^-< Note: The other type originates here
--! error

--8<-- reinit-func-table
local f = function() end
f = {54, 49} --@< Error: Cannot assign `{54, 49, ...}` into `function() --> ()`
             --@^ Note: The other type originates here
--! error

--8<-- assume-rec
local f = function() end
--# assume f: {index: integer}
local p = f.index
--! ok

--8<-- assume-table
local f = function() end
--# assume f: table
local p = f.index --@< Error: Cannot index `table` without further type information; specify more detailed type, or use `--# assume` as a last resort
--! error

--8<-- conjunctive-lhs-1
local a = ('string' and 53) + 42
--! ok

--8<-- conjunctive-lhs-2
local a = (53 and 'string') + 42 --@< Error: Cannot apply + operator to `"string"` and `42`
                                 --@^ Cause: `"string"` is not a subtype of `number`
--! error

--8<-- conjunctive-rhs-1
local a = (false and 'string') + 42 --@< Error: Cannot apply + operator to `false` and `42`
                                    --@^ Cause: `false` is not a subtype of `number`
--! error

--8<-- conjunctive-rhs-2
local a = (false and 53) + 42 --@< Error: Cannot apply + operator to `false` and `42`
                              --@^ Cause: `false` is not a subtype of `number`
--! error

--8<-- conjunctive-type-erasure
--# assume x: nil | boolean
local a = x and 53 --: integer | nil | false
--! ok

--8<-- disjunctive-lhs-1
local a = (53 or 'string') + 42
--! ok

--8<-- disjunctive-lhs-2
local a = (53 or nil) + 42
--! ok

--8<-- disjunctive-rhs-1
local a = (nil or 'string') + 42 --@< Error: Cannot apply + operator to `"string"` and `42`
                                 --@^ Cause: `"string"` is not a subtype of `number`
--! error

--8<-- disjunctive-rhs-2
local a = (nil or 53) + 42
--! ok

--8<-- disjunctive-type-erasure
--# assume x: integer?
--# assume y: integer | boolean
local a = (x or 53) + 42
local b = y or 53 --: integer | true
--! ok

--8<-- disjunctive-type-erasure-rec-1
--# assume x: {a: string, b: integer}?
--# assume y: {b: integer, c: boolean}
local z = x or y --: {a: string, b: integer, c: boolean}
--@^ Error: Cannot apply or operator to `{a: string, b: integer}?` and `{b: integer, c: boolean}`
--@^^ Cause: Cannot create a union type of `{a: string, b: integer}` and `{b: integer, c: boolean}`
--@^^^ Note: The other type originates here
--! error

--8<-- disjunctive-type-erasure-rec-2
-- records are still not extensible
--# assume x: {a: string?, b: integer}?
--# assume y: {b: integer, c: boolean?}
local z = x or y --: {a: string?, b: integer, c: boolean?}
--@^ Error: Cannot apply or operator to `{a: string?, b: integer}?` and `{b: integer, c: boolean?}`
--@^^ Cause: Cannot create a union type of `{a: string?, b: integer}` and `{b: integer, c: boolean?}`
--@^^^ Note: The other type originates here
--! error

--8<-- disjunctive-type-erasure-rec-3
--# assume x: {a: string?, b: integer, ...}?
--# assume y: {b: integer, c: boolean?, ...}
local z = x or y --: {a: string?, b: integer, c: boolean?}
--! ok

--8<-- disjunctive-type-erasure-rec-empty-1
--# assume x: {a: string, b: integer}?
local x = x or {} --: {a: string, b: integer}!
--@^ Error: Cannot apply or operator to `{a: string, b: integer}?` and `{...}`
--@^^ Cause: Cannot create a union type of `{a: string, b: integer}` and `{...}`
--@^^^ Note: The other type originates here
--! error

--8<-- disjunctive-type-erasure-rec-empty-2
--# assume x: {a: string?, b: integer?}?
local x = x or {} --: {a: string?, b: integer?}!
--! ok

--8<-- disjunctive-type-erasure-map-1
--# assume x: map<string, string>?
--# assume y: map<string, string>!
local z = x or y --: map<string, string>!
--! ok

--8<-- disjunctive-type-erasure-map-2
--# assume x: map<string, string>?
--# assume y: map<string, const string>!
local z = x or y --: map<string, string>!
--@^ Error: Cannot apply or operator to `map<string, string>?` and `map<string, const string>!`
--@^^ Cause: Cannot create a union type of `map<string, string>` and `map<string, const string>!`
--@^^^ Note: The other type originates here
--! error

--8<-- disjunctive-type-erasure-map-empty
--# assume x: map<string, boolean>?
local x = x or {} --: map<string, boolean>!
--! ok

--8<-- disjunctive-type-erasure-map-rec-sub
--# assume x: map<string, boolean>?
local x = x or {a = true, b = false} --: map<string, boolean>!
--! ok

--8<-- disjunctive-type-erasure-map-rec-no-sub
--# assume x: map<string, boolean>?
local x = x or {a = true, b = 42} --: map<string, boolean>!
--@^ Error: Cannot apply or operator to `map<string, boolean>?` and `{a: true, b: 42, ...}`
--@^^ Cause: Cannot create a union type of `map<string, boolean>` and `{a: true, b: 42, ...}`
--@^^^ Note: The other type originates here
--! error

--8<-- disjunctive-type-erasure-array-1
--# assume x: vector<string>?
--# assume y: vector<string>!
local z = x or y --: vector<string>!
--! ok

--8<-- disjunctive-type-erasure-array-2
--# assume x: vector<string>?
--# assume y: vector<const string>!
local z = x or y --: vector<string>!
--@^ Error: Cannot apply or operator to `vector<string>?` and `vector<const string>!`
--@^^ Cause: Cannot create a union type of `vector<string>` and `vector<const string>!`
--@^^^ Note: The other type originates here
--! error

--8<-- disjunctive-type-erasure-array-empty
--# assume x: vector<integer>?
local x = x or {} --: vector<integer>!
--! ok

--8<-- disjunctive-type-erasure-array-rec-sub
--# assume x: vector<integer>?
local x = x or {4, 5, 6} --: vector<integer>!
--! ok

--8<-- disjunctive-type-erasure-array-rec-no-sub
--# assume x: vector<integer>?
local x = x or {a = 4, b = 5, c = 6} --: vector<integer>!
--@^ Error: Cannot apply or operator to `vector<integer>?` and `{a: 4, b: 5, c: 6, ...}`
--@^^ Cause: Cannot create a union type of `vector<integer>` and `{a: 4, b: 5, c: 6, ...}`
--@^^^ Note: The other type originates here
--! error

--8<-- conjunctive-lhs-dynamic
--# assume p: WHATEVER
local a = (p and 'foo') + 54
--! ok

--8<-- conjunctive-rhs-dynamic
--# assume p: WHATEVER
local a = ('foo' and p) + 54
--! ok

--8<-- disjunctive-lhs-dynamic
--# assume p: WHATEVER
local a = (p or 'foo') + 54
--! ok

--8<-- disjunctive-rhs-dynamic
--# assume p: WHATEVER
local a = ('foo' or p) + 54
--! ok

--8<-- ad-hoc-conditional-1
--# assume x: boolean
local a = x and 54 or 42 --: integer
--! ok

--8<-- ad-hoc-conditional-2
--# assume x: integer|string|boolean|table
local a = x and 54 or 42 --: integer
--! ok

--8<-- cat-string-or-number
--# assume p: string | number
local q = p .. 3
--! ok

--8<-- add-string-or-number
--# assume p: string | number
local q = p + 3 --@< Error: Cannot apply + operator to `(number|string)` and `3`
                --@^ Cause: `(number|string)` is not a subtype of `number`
--! error

--8<-- cat-string-or-boolean
--# assume p: string | boolean
local q = p .. 3 --@< Error: Cannot apply .. operator to `(boolean|string)` and `3`
                 --@^ Cause: `(boolean|string)` is not a subtype of `(number|string)`
--! error

--8<-- cat-string-lit-1
--# assume p: 'a'
--# assume q: 'b'
--# assume r: 'ab'
r = p .. q
--! ok

--8<-- cat-string-lit-2
--# assume r: 'ab'
r = 'a' .. 'b'
--! ok

--8<-- cat-string-lit-3
--# assume r: 'ab'
local p = 'a' --: string
r = p .. 'b' --@< Error: Cannot assign `string` into `"ab"`
             --@^ Note: The other type originates here
--! error

--8<-- var-integer-literal
local x
--# assume x: 3
x = 3
--! ok

--8<-- var-integer-literals-1
local x
--# assume x: 3 | 4
x = 3
--! ok

--8<-- var-integer-literals-2
local x
--# assume x: 4 | 5
x = 3 --@< Error: Cannot assign `3` into `(4|5)`
      --@^ Note: The other type originates here
--! error

--8<-- add-sub-mul-mod-integer-integer
local x, y, z
--# assume x: integer
--# assume y: integer
--# assume z: integer
z = x + y
z = x - y
z = x * y
z = x % y
--! ok

--8<-- div-integer-integer
local x, y, z
--# assume x: integer
--# assume y: integer
--# assume z: integer
z = x / y --@< Error: Cannot assign `number` into `integer`
          --@^ Note: The other type originates here
--! error

--8<-- add-integer-integer-is-integer
local p
--# assume p: integer
p = 3 + 4
--! ok

--8<-- add-number-integer-is-not-integer
local p
--# assume p: integer
p = 3.1 + 4 --@< Error: Cannot assign `number` into `integer`
            --@^ Note: The other type originates here
--! error

--8<-- add-dynamic-integer
local p, q
--# assume p: WHATEVER
--# assume q: integer
q = p + 3
--! ok

--8<-- add-dynamic-number-is-not-integer
local p, q
--# assume p: WHATEVER
--# assume q: integer
q = p + 3.5 --@< Error: Cannot assign `number` into `integer`
            --@^ Note: The other type originates here
--! error

--8<-- add-dynamic-number-is-number
local p, q
--# assume p: WHATEVER
--# assume q: number
q = p + 3.5
--! ok

--8<-- add-dynamic-dynamic-1
local p, q
--# assume p: WHATEVER
--# assume q: number
q = p + p
--! ok

--8<-- add-dynamic-dynamic-2
local p, q
--# assume p: WHATEVER
--# assume q: integer
q = p + p --@< Error: Cannot assign `number` into `integer`
          --@^ Note: The other type originates here
--! error

--8<-- len-table
local a = 3 + #{1, 2, 3}
--! ok

--8<-- len-table-varargs-1
--v function() --> (integer...)
function f() end

local a = 3 + #{1, 2, 3, f()}
local b = 6 + #{f()}
--! ok

--8<-- len-table-varargs-2
--v function(...: integer)
function f(...)
    local a = #{...}
end
--! ok

--8<-- len-string
local a = 3 + #'heck'
--! ok

--8<-- len-integer
local a = 3 + #4 --@< Error: Cannot apply # operator to `4`
                 --@^ Cause: `4` is not a subtype of `(string|table)`
--! error

--8<-- for
--# assume a: integer
for i = 1, 9 do a = i end
--! ok

--8<-- for-step
--# assume a: integer
for i = 1, 9, 2 do a = i end
--! ok

--8<-- for-non-integer
--# assume a: integer
for i = 1.1, 9 do
    a = i --@< Error: Cannot assign `number` into `integer`
          --@^ Note: The other type originates here
end
--! error

--8<-- for-non-integer-step
--# assume a: integer
for i = 1, 9, 2.1 do
    a = i --@< Error: Cannot assign `number` into `integer`
          --@^ Note: The other type originates here
end
--! error

--8<-- for-diverges-1
--# assume f: function() --> !
for i = 1, f(), 2 do --@< Warning: A portion of this expression won't be evaluated because it contains a call to a function that never returns
end
--! ok

--8<-- for-diverges-2 -- feature:warn_on_dead_code exact
--# assume f: function() --> !
for i = f(), f(), f() do -- this will no longer warn
    local a = 42 --@<-v Warning: This code will never execute
    local b = 54
end
--! ok

--8<-- func-diverges
function f()
    while true do end
end
--! ok

--8<-- func-diverges-type-1
function f() --> !
    while true do end
end
--! ok

--8<-- func-diverges-type-2
function f() --> !
end --@< Error: Tried to return from a function that is marked that it never returns
--! error

--8<-- func-diverges-chain-1
--# assume f: function() --> !
function g() --> !
    f()
end
--! ok

--8<-- func-diverges-chain-2
--# assume f: function() --> !
function g() --> !
    if f() then
        -- technically this won't execute, but we report anyway
        return --@< Error: Tried to return from a function that is marked that it never returns
    end
end
--! error

--8<-- func-diverges-chain-3
--# assume f: function() --> !
function g() --> !
    do return end --@< Error: Tried to return from a function that is marked that it never returns
    f()
end
--! error

--8<-- func-diverges-chain-4
--# assume f: function() --> !
--v function(a: boolean, b: boolean) --> !
function g(a, b)
    if a then
        f()
    elseif b then
        f()
    end --@< Error: Tried to return from a function that is marked that it never returns
end
--! error

--8<-- func-diverges-chain-5
--# assume f: function() --> !
--v function(a: boolean, b: boolean) --> !
function g(a, b)
    if a then
        f()
    elseif b then
        f()
    else
        f()
    end
end
--! ok

--8<-- func-diverges-chain-6
--# assume f: function() --> !
--v function()
function g()
    local x = 42
    local y = 54
    f()
end
--! ok

--8<-- func-diverges-chain-7 -- feature:warn_on_dead_code
--# assume f: function() --> !
--v function()
function g()
    f()
    local x = 42 --@<-v Warning: This code will never execute
    local y = 54
end
--! ok

--8<-- func-diverges-chain-8
--# assume f: function() --> !
--v function(a: boolean, b: boolean) --> string
function g(a, b)
    if a then
        return 'a'
    elseif b then
        return 'b'
    else
        f()
    end
end
--! ok

--8<-- func-diverges-in-expr-1
--# assume f: function() --> !
function g() --> !
    local x = f() + f() * f()
    --@^ Warning: A portion of this expression won't be evaluated because it contains a call to a function that never returns
end
--! ok

--8<-- func-diverges-in-expr-2 -- feature:warn_on_dead_code
--# assume f: function() --> !
function g() --> !
    local x = f() + f() * f()
    --@^ Warning: A portion of this expression won't be evaluated because it contains a call to a function that never returns
    local y = x --@<-v Warning: This code will never execute
    local z = y
end
--! ok

--8<-- func-diverges-in-expr-3
--# assume f: function() --> !
--v function(x: integer, y: integer)
function g(x, y) end
function h() --> !
    g(f(), 0) --@< Warning: A portion of this expression won't be evaluated because it contains a call to a function that never returns
end
--! ok

--8<-- func-diverges-in-expr-4 -- feature:warn_on_dead_code
--# assume f: function() --> !
--v function(x: integer, y: integer)
function g(x, y) end
function h() --> !
    g(f(), 0) --@< Warning: A portion of this expression won't be evaluated because it contains a call to a function that never returns
    local y = 42 --@<-v Warning: This code will never execute
    local z = 54
end
--! ok

--8<-- func-varargs -- feature:no_implicit_func_sig
--@v-vvv Error: The type for variadic arguments in the anonymous function is missing but couldn't be inferred from the calls
function a(...)
    return ...
end
--! error

--8<-- func-varargs-type
--v function(...: any) --> (any...)
function a(...)
    return ...
end
--! ok

--8<-- func-no-varargs
function a()
    return ... --@< Error: Variadic arguments do not exist in the innermost function
end
--! error

--8<-- func-nested-varargs
--v function(...: any) --> function() --> (any...)
function a(...)
    return function()
        return ... --@< Error: Variadic arguments do not exist in the innermost function
    end
end
--! error

--8<-- func-arg-const
function a(x) --: const integer
    x = 54 --@< Error: Cannot assign `54` into `const integer`
           --@^ Note: The other type originates here
end
--! error

--8<-- func-arg-const-only -- feature:no_implicit_func_sig
function a(x) --: const
    --@^ Error: Every argument in the named function should have a type specified
    --@^^ Error: The type for this argument in the anonymous function is missing but couldn't be inferred from the calls
end
--! error

--8<-- func-arg-const-only-inferred -- feature:!no_implicit_func_sig
function a(x) --: const
    local y = x --: integer
    x = 54 --@< Error: Cannot assign `54` into `const <unknown type>`
           --@^ Note: The other type originates here
end
a(42) -- this is okay
--! error

--8<-- funccall-func-hint
--v function(a: function(integer, integer) --> integer)
function p(a) end

p(function(x, y)
    return x + y
end)
--! ok

--8<-- funccall-func-hint-returns
--v function(a: function(integer, integer) --> integer)
function p(a) end

p(function(x, y)
    return x .. y --@< Error: Attempted to return a type `(string)` which is incompatible to given return type `(integer)`
                  --@^ Cause: First return type `string` is not a subtype of `integer`
                  --@^^ Note: The other type originates here
end)
--! error

--8<-- funccall-func-hint-not-func -- feature:no_implicit_func_sig
--v function(a: string)
function p(a) end

p(function(x) end)
--@^ Error: The type for this argument in the anonymous function is missing but couldn't be inferred from the calls
--@^^ Error: The type `function(a: string) --> ()` cannot be called
--@^^^ Cause: First function argument `function(x: <error>) --> ()` is not a subtype of `string`
--@^^^^ Note: The other type originates here
--! error

--8<-- funccall-func-hint-less-arity -- feature:no_implicit_func_sig
--v function(a: function(integer, integer) --> integer)
function p(a) end

-- the hint doesn't directly affect the diagnostics
--@vvv Error: The type `function(a: function(integer, integer) --> integer) --> ()` cannot be called
--@vv-vvvv Cause: First function argument `function(x: integer) --> integer` is not a subtype of `function(integer, integer) --> integer`
--@v Note: The other type originates here
p(function(x)
    return x * 2
end)
--! error

--8<-- funccall-func-hint-more-arity -- feature:no_implicit_func_sig
--v function(a: function(integer, integer) --> integer)
function p(a) end

p(function(x, y, z)
    local xy = x + y
    return xy + z --@< Error: Cannot apply + operator to `integer` and `nil`
                  --@^ Cause: `nil` is not a subtype of `number`
                  --@^^ Error: Attempted to return a type `(number)` which is incompatible to given return type `(integer)`
                  --@^^^ Cause: First return type `number` is not a subtype of `integer`
                  --@^^^^ Note: The other type originates here
end)
--! error

--8<-- funccall-func-hint-varargs
--v function(a: function(integer, integer, integer...) --> integer)
function p(a) end

p(function(x, y, ...)
    return x + y
end)
--! ok

--8<-- funccall-func-hint-varargs-less-arity -- feature:no_implicit_func_sig
--v function(a: function(integer, integer, integer...) --> integer)
function p(a) end

--@v-vvv Error: The type for variadic arguments in the anonymous function is missing but couldn't be inferred from the calls
p(function(x, ...)
    return x * 2
end)
--! error

--8<-- funccall-func-hint-varargs-more-arity
--v function(a: function(integer, integer, integer...) --> integer)
function p(a) end

p(function(x, y, z, ...)
    return x + y + z
end)
--! ok

--8<-- funccall-func-hint-diverges
--v function(a: function() --> !)
function p(a) end

p(function()
end) --@< Error: Tried to return from a function that is marked that it never returns
--! error

--8<-- funccall-func-hint-union
--v function(a: (function(integer, integer) --> integer) | string)
function p(a) end

p(function(x, y)
    return x + y
end)
--! ok

--8<-- methodcall-func-hint
local tab = {}

--v method(a: function(int, int) --> int)
function tab:p(a) end

tab.p(tab, function(x, y)
    return x + y
end)

tab:p(function(x, y)
    return x + y
end)
--! ok

--8<-- methodcall-func-hint-not-func -- feature:no_implicit_func_sig
local tab = {}

--v method(a: string)
function tab:p(a) end

tab.p(tab, function(x) end)
--@^ Error: The type for this argument in the anonymous function is missing but couldn't be inferred from the calls
--@^^ Error: The type `function(self: <unknown type>, a: string) --> ()` cannot be called
--@^^^ Cause: Second function argument `function(x: <error>) --> ()` is not a subtype of `string`
--@^^^^ Note: The other type originates here

tab:p(function(x) end)
--@^ Error: The type for this argument in the anonymous function is missing but couldn't be inferred from the calls
--@^^ Error: The type `function(self: <unknown type>, a: string) --> ()` cannot be called
--@^^^ Cause: First method argument `function(x: <error>) --> ()` is not a subtype of `string`
--@^^^^ Note: The other type originates here
--! error

--8<-- methodcall-func-hint-less-arity -- feature:no_implicit_func_sig
local tab = {}

--v method(a: function(integer, integer) --> integer)
function tab:p(a) end

--@vvv Error: The type `function(self: <unknown type>, a: function(integer, integer) --> integer) --> ()` cannot be called
--@vv-vvvv Cause: Second function argument `function(x: integer) --> integer` is not a subtype of `function(integer, integer) --> integer`
--@v Note: The other type originates here
tab.p(tab, function(x)
    return x * 2
end)

--@vvv Error: The type `function(self: <unknown type>, a: function(integer, integer) --> integer) --> ()` cannot be called
--@vv-vvvv Cause: First method argument `function(x: integer) --> integer` is not a subtype of `function(integer, integer) --> integer`
--@v Note: The other type originates here
tab:p(function(x)
    return x * 2
end)
--! error

--8<-- methodcall-func-hint-more-arity -- feature:no_implicit_func_sig
local tab = {}

--v method(a: function(integer, integer) --> integer)
function tab:p(a) end

tab.p(tab, function(x, y, z)
    local xy = x + y
    return xy + z --@< Error: Cannot apply + operator to `integer` and `nil`
                  --@^ Cause: `nil` is not a subtype of `number`
                  --@^^ Error: Attempted to return a type `(number)` which is incompatible to given return type `(integer)`
                  --@^^^ Cause: First return type `number` is not a subtype of `integer`
                  --@^^^^ Note: The other type originates here
end)

tab:p(function(x, y, z)
    local xy = x + y
    return xy + z --@< Error: Cannot apply + operator to `integer` and `nil`
                  --@^ Cause: `nil` is not a subtype of `number`
                  --@^^ Error: Attempted to return a type `(number)` which is incompatible to given return type `(integer)`
                  --@^^^ Cause: First return type `number` is not a subtype of `integer`
                  --@^^^^ Note: The other type originates here
end)
--! error

--8<-- methodcall-func-hint-varargs
local tab = {}

--v method(a: function(integer, integer, integer...) --> integer)
function tab:p(a) end

tab.p(tab, function(x, y, ...)
    return x + y
end)

tab:p(function(x, y, ...)
    return x + y
end)
--! ok

--8<-- methodcall-func-hint-varargs-less-arity -- feature:no_implicit_func_sig
local tab = {}

--v method(a: function(integer, integer, integer...) --> integer)
function tab:p(a) end

--@v-vvv Error: The type for variadic arguments in the anonymous function is missing but couldn't be inferred from the calls
tab.p(tab, function(x, ...)
    return x * 2
end)

--@v-vvv Error: The type for variadic arguments in the anonymous function is missing but couldn't be inferred from the calls
tab:p(function(x, ...)
    return x * 2
end)
--! error

--8<-- methodcall-func-hint-varargs-more-arity
local tab = {}

--v method(a: function(integer, integer, integer...) --> integer)
function tab:p(a) end

tab.p(tab, function(x, y, z, ...)
    return x + y + z
end)

tab:p(function(x, y, z, ...)
    return x + y + z
end)
--! ok

--8<-- index-rec-with-name-1
local a = { x = 3, y = 'foo' }
--# assume b: integer
b = a.x
--! ok

--8<-- index-rec-with-name-2
local a = { x = 3, y = 'foo' }
--# assume b: string
b = a.y
--! ok

--8<-- index-rec-with-wrong-name-1
local a = { x = 3, y = 'foo' }
local b = a.z + 1 -- z should be nil
--@^ Error: Missing key "z" in `{x: 3, y: "foo", ...}`
--! error

--8<-- index-rec-with-wrong-name-2
local a = { x = 3, y = 'foo' }
local b = a.z .. 'bar' -- ditto
--@^ Error: Missing key "z" in `{x: 3, y: "foo", ...}`
--! error

--8<-- table-update
local a = {}
a[1] = 1
a[2] = 2
a[3] = 3
--! ok

--8<-- func-number-arg-explicit
-- XXX can't propagate the number constraints upwards!
function p(a) --: number
    return a + 3
end
local x = p(4.5)
--! ok

--8<-- func-number-arg-implicit -- feature:!no_implicit_func_sig
function p(a) return a + 3 end
local x = p('what') --@< Error: The type `function(a: <unknown type>) --> integer` cannot be called
                    --@^ Cause: First function argument `"what"` is not a subtype of `<unknown type>`
                    --@^^ Note: The other type originates here
--! error

--8<-- capture-and-return-1
local a = 42
function p() return a end
local b = p() + 54
--! ok

--8<-- capture-and-return-2
local a = 42
function p() return a end
a = p() * 54
--! ok

--8<-- func-argtype-rettype-1
function p(x) --: string --> string
    return x
end
local a = p('foo') .. 'bar'
--! ok

--8<-- func-argtype-implicit-rettype
function p(x) --: string
    return x
end
local a = p('foo') .. 'bar'
--! ok

--8<-- func-argtype-rettype-2
function p(x) --: string --> string
    return x
end
local a = p('foo') + 3 --@< Error: Cannot apply + operator to `string` and `3`
                       --@^ Cause: `string` is not a subtype of `number`
--! error

--8<-- table-update-with-integer-1
local a = {}
a[1] = 42
a[2] = 54
--! ok

--8<-- table-update-with-integer-2
local a = {} --: {}
a[1] = 42 --@< Error: Cannot index `{}` with `1`
a[2] = 54 --@< Error: Cannot index `{}` with `2`
--! error

--8<-- table-update-with-integer-3
local a = {} --: {...}
a[1] = 42 -- now valid to do so
a[2] = 54
--! ok

--8<-- table-update-with-name
local a = {}
a.what = 42
--! ok

--8<-- table-update-with-index-and-name
local a = {}
a[1] = 42
a.what = 54
--! ok

--8<-- var-array
local a = {} --: vector<number>
--! ok

--8<-- var-array-update-with-integer-and-name
local a = {} --: vector<number>
a[1] = 42
a.what = 54
--@^ Error: Cannot index an array `vector<number>` with a non-integral key `"what"`
--! error

--8<-- var-map-update-and-index
local a = {} --: map<number, number>
a[1] = 42
a[3] = 54
a[1] = nil
local z = a[3] --: number?
--! ok

--8<-- var-map-update-and-index-subtype
local a = {} --: map<number, number>
a[1] = 42
a[3] = 54
a[1] = nil
local z = a[3] --: integer
--@^ Error: Cannot assign `number` into `integer`
--@^^ Note: The other type originates here
--! error

--8<-- var-map-update-and-index-wrong-key
local a = {} --: map<number, number>
a[1] = 42
a.string = 54 --@< Error: Cannot index `map<number, number>` with `"string"`
--! error

--8<-- var-map-update-and-index-without-nil
local a = {} --: map<number, number>
a[1] = 42
a[3] = 54
a[1] = nil
local z = a[3] --: integer
--@^ Error: Cannot assign `number` into `integer`
--@^^ Note: The other type originates here
--! error

--8<-- const-only-1
local a = 3 --: const
a = 3 --@< Error: Cannot assign `3` into `const integer`
      --@^ Note: The other type originates here
--! error

--8<-- const-only-2
a = 3 --: const
a = 3 --@< Error: Cannot assign `3` into `const integer`
      --@^ Note: The other type originates here
--! error

--8<-- const-only-3
local a = {x = 1, y = 2} --: const
local b = a.x + a.y
--! ok

--8<-- const-map-init
local a = {} --: const map<number, number>
--! ok

--8<-- const-map-update
local a = {} --: const map<number, number>
a[1] = 42 --@< Error: Cannot update the immutable type `const map<number, number>` by indexing
--! error

--8<-- var-any-update
local a --: any
a = {}
a = 'hello'
a = 42
--! ok

--8<-- var-any-update-and-add
local a --: any
a = 42
local b = a + 5 --@< Error: Cannot apply + operator to `any` and `5`
                --@^ Cause: `any` is not a subtype of `number`
--! error

--8<-- var-typed-error-1
local a = 3 --: number
a = 'foo'
--@^ Error: Cannot assign `"foo"` into `number`
--@^^ Note: The other type originates here
a = 4
a = 'bar'
--@^ Error: Cannot assign `"bar"` into `number`
--@^^ Note: The other type originates here
--! error

--8<-- var-typed-error-2
local a = 'foo' --: number
--@^ Error: Cannot assign `"foo"` into `number`
--@^^ Note: The other type originates here
a = 3
a = 'bar'
--@^ Error: Cannot assign `"bar"` into `number`
--@^^ Note: The other type originates here
a = 4
--! error

--8<-- var-typed-error-lazy-1
local a --: number
a = 3
a = 'foo'
--@^ Error: Cannot assign `"foo"` into `number`
--@^^ Note: The other type originates here
a = 4
a = 'bar'
--@^ Error: Cannot assign `"bar"` into `number`
--@^^ Note: The other type originates here
--! error

--8<-- var-typed-error-lazy-2
local a --: number
a = 'foo'
--@^ Error: Cannot assign `"foo"` into `number`
--@^^ Note: The other type originates here
a = 3
a = 'bar'
--@^ Error: Cannot assign `"bar"` into `number`
--@^^ Note: The other type originates here
a = 4
--! error

--8<-- const-typed-error-1
local a = 3 --: const number
a = 'foo'
--@^ Error: Cannot assign `"foo"` into `const number`
--@^^ Note: The other type originates here
a = 4
--@^ Error: Cannot assign `4` into `const number`
--@^^ Note: The other type originates here
--! error

--8<-- const-typed-error-2
local a = 'foo' --: const number
--@^ Error: Cannot assign `"foo"` into `const number`
--@^^ Note: The other type originates here
a = 3
--@^ Error: Cannot assign `3` into `const number`
--@^^ Note: The other type originates here
a = 'bar'
--@^ Error: Cannot assign `"bar"` into `const number`
--@^^ Note: The other type originates here
--! error

--8<-- const-typed-error-lazy-1
local a --: const number
a = 3
a = 'foo'
--@^ Error: Cannot assign `"foo"` into `const number`
--@^^ Note: The other type originates here
a = 4
--@^ Error: Cannot assign `4` into `const number`
--@^^ Note: The other type originates here
--! error

--8<-- const-typed-error-lazy-2
local a --: const number
a = 'foo'
--@^ Error: Cannot assign `"foo"` into `const number`
--@^^ Note: The other type originates here
a = 3
--@^ Error: Cannot assign `3` into `const number`
--@^^ Note: The other type originates here
a = 'bar'
--@^ Error: Cannot assign `"bar"` into `const number`
--@^^ Note: The other type originates here
--! error

--8<-- local-init-does-not-copy-modf
local x = 42 --: const integer
local y = x
y = 54
--! ok

--8<-- func-returns-rec-1
--v function() --> {a:integer}
local function p() return {a=4} end
local x = p().a + 5
--! ok

--8<-- func-returns-rec-2
--v function() --> {a:integer}
local function p() return {a=4} end
local x = p().a.b --@< Error: Tried to index a non-table type `integer`
--! error

--8<-- func-returns-rec-2-span
--v function() --> {a:integer}
local function p() return {a=4} end
local x = p().a
local y = x.b --@< Error: Tried to index a non-table type `integer`
--! error

--8<-- func-implicit-returns-rec
local function p() return {a=4} end
local x = p().a + 5
--! ok

-->8-- func-implicit-returns-rec-union -- feature:!no_implicit_func_sig
function f(v) --: boolean
    if v then
        local x = {} --: vector<integer>
        return {a = x}
    else
        return {a = {}}
    end
end
--! ok

--8<-- func-returns-seq
--v function() --> (integer, integer)
local function p()
    return 3, 4
end
--! ok

--8<-- func-returns-seq-error
--v function() --> (integer, string)
local function p()
    return 3, 4
    --@^ Error: Attempted to return a type `(3, 4)` which is incompatible to given return type `(integer, string)`
    --@^^ Cause: Second return type `4` is not a subtype of `string`
    --@^^^ Note: The other type originates here
end
--! error

--8<-- func-returns-seq-with-nil
--v function() --> (integer, nil)
local function p()
    return 3
    --@^ Error: Attempted to return a type `(3)` which is incompatible to given return type `(integer, nil)`
    --@^^ Cause: Second return value cannot be omitted because its type is `nil`
    --@^^^ Note: The other type originates here
end
--! error

--8<-- func-returns-with-nil
--v function() --> integer
local function p()
    return 3, nil --@< Error: Attempted to return a type `(3, nil)` which is incompatible to given return type `(integer)`
                  --@^ Cause: Cannot return more than 1 value(s)
                  --@^^ Note: The other type originates here
end
--! error

--8<-- func-returns-seq-union-1
--v function(n: boolean) --> (string, string)
local function p(n)
    if n then
        return 'string' --@< Error: Attempted to return a type `("string")` which is incompatible to given return type `(string, string)`
                        --@^ Cause: Second return value cannot be omitted because its type is `string`
                        --@^^ Note: The other type originates here
    else
        return nil, 'error'
    end
end
--! error

--8<-- func-returns-seq-union-2
--v function(n: boolean) --> (string, string?)
local function p(n)
    if n then
        return 'string'
    else
        return nil, 'error'
    end
end
--! ok

--8<-- func-returns-wrong
--v function()
local function f()
    return 'foo'
    --@^ Error: Attempted to return a type `("foo")` which is incompatible to given return type `()`
    --@^^ Cause: Cannot return more than 0 value(s)
    --@^^^ Note: The other type originates here
end
--! error

--8<-- assign-func-no-hint-1
local x --: function(string)
x = function(a) end
--! ok

--8<-- assign-func-no-hint-2 -- feature:no_implicit_func_sig
local x
x = function(a) end --@< Error: The type for this argument in the anonymous function is missing but couldn't be inferred from the calls
--! error

--8<-- assign-from-seq-1
local function p()
    return 3, 4, 5
end
local a, b = p() --: integer, integer
--! ok

--8<-- assign-from-seq-2
local function p()
    return 3, 4, 'string'
end
local a, b = p() --: integer, integer
--! ok

--8<-- assign-from-seq-3
local function p()
    return 3, 'string', 5
end
local a, b = p() --: integer, integer
--@^ Error: Cannot assign `"string"` into `integer`
--@^^ Note: The other type originates here
--! error

--8<-- assign-from-seq-with-nil-1
local function p()
    return 3, 4
end
local a, b, c = p() --: integer, integer, nil
--! ok

--8<-- assign-from-seq-with-nil-2
local function p()
    return 3, 4, nil
end
local a, b = p() --: integer, integer
--! ok

--8<-- assign-from-seq-union-1
local function p(n) --: boolean
    if n then return 'string' else return nil, 'error' end
end
local a, b = p(false) --: string, string
local a, b = p(true) --: string, string
--! ok

--8<-- assign-from-seq-union-2
local function p(n) --: boolean
    if n then return 'string' else return nil, 'error' end
end
local a, b = p(false) --: number, number
--@^ Error: Cannot assign `"string"` into `number`
--@^^ Note: The other type originates here
--@^^^ Error: Cannot assign `"error"?` into `number`
--@^^^^ Note: The other type originates here
--! error

--8<-- table-from-seq
local function p()
    return 1, 2, 3
end
local a = {p(), p()} --: {integer, integer, integer, integer}
local b = {foo = p()} --: {foo: integer}
local c = {p(), bar = p()} --: map<integer|string, integer>
--! ok

--8<-- funccall-from-seq
local function p()
    return 1, 'string', false
end
--v function(a: number, b: integer, c: number, d: integer, e: string, f: boolean)
local function q(a,b,c,d,e,f) end
q(3.14, p(), -42, p())
--! ok

--8<-- func-varargs-type-1
--v function(...: integer)
local function p(...) end
p(1, 2, 3)
--! ok

--8<-- func-varargs-type-2
--v function(...: integer)
local function p(...) end
p(1, false, 3) --@< Error: The type `function(integer...) --> ()` cannot be called
               --@^ Cause: Second function argument `false` is not a subtype of `integer`
               --@^^ Note: The other type originates here
--! error

--8<-- func-varargs-type-with-nil
--v function(...: integer)
local function p(...) end
p(1, 2, 3, nil, nil, nil)
--! ok

--8<-- func-varargs-type-delegated -- feature:!no_implicit_func_sig
--v function(...: string)
function f(...)
end
function g(...)
    f(...)
end
--! ok

--8<-- type
--# type known_type = number
--# type local well_known_type = number
--# type global widespread_type = number
--# assume p: known_type
--# assume q: well_known_type
--# assume r: widespread_type
local s = p + q + r - 5
--! ok

--8<-- type-local
do
    --# type local known_type = number
end
--# assume p: known_type --@< Error: Type `known_type` is not defined
--! error

--8<-- type-local-no-shadowing
--# type local known_type = integer --@< Note: The type was originally defined here
do
    --# type local known_type = number --@< Error: A type `known_type` is already defined
end
--! error

--8<-- type-no-recursive
--# type known_type = known_type --@< Error: Type `known_type` is not defined
--# type local well_known_type = well_known_type --@< Error: Type `well_known_type` is not defined
--# type global widespread_type = widespread_type --@< Error: Type `widespread_type` is not defined
--! error

--8<-- type-reexport-1
--# type local known_type = integer
--# type known_type = known_type
--! ok

--8<-- type-reexport-2
--# type local well_known_type = integer
--# type local known_type = string --@< Note: The type was originally defined here
--# type known_type = well_known_type --@< Error: A non-exported type `known_type` can only be redefined and exported as itself
--! error

--8<-- type-globalize-1
--# type local known_type = integer
--# type global known_type = known_type
--! ok

--8<-- type-globalize-2
--# type known_type = integer
--# type global known_type = known_type
--! ok

--8<-- type-globalize-3
--# type local well_known_type = integer
--# type local known_type = string --@< Note: The type was originally defined here
--# type global known_type = well_known_type --@< Error: A locally defined type `known_type` can only be redefined as itself in the global scope
--! error

--8<-- type-transitive
--# type some_type = integer
--# type another_type = some_type
--# assume p: another_type
local q = p * 3
--! ok

--8<-- type-unknown-type
--# type some_type = another_type --@< Error: Type `another_type` is not defined
--! error

--8<-- type-transitive-out-of-order
--# type some_type = another_type --@< Error: Type `another_type` is not defined
--# type another_type = integer -- this is intentional
--! error

--8<-- invalid-open
--# open `internal kailua_dummy` --@< Error: Cannot find the built-in library name given to `--# open` directive
--! error

--8<-- duplicate-open
--# open `internal kailua_test`
--# open `internal kailua_test`
--! ok

--8<-- assign-identical
--# assume x: WHATEVER
x = x
--! ok

--8<-- call-identical
--# assume x: WHATEVER
x(x)
--! ok

--8<-- index-identical
--# assume x: WHATEVER
x[x]()
--! ok

--8<-- if-assign-identical
--# assume x: WHATEVER
if x then x = 42 end
--! ok

--8<-- while-assign-identical
--# assume x: WHATEVER
while x do x = 42 end
--! ok

--8<-- for-assign-identical
--# assume x: WHATEVER
for i = 1, x do x = 42 end
--! ok

--8<-- for-identical
--# assume x: WHATEVER
for i = x, x do end
--! ok

--8<-- table-identical
--# assume x: 'hello'
local p = {[x] = x}
--! ok

--8<-- assign-table-to-table
--# assume x: table
--# assume y: table
x = y
--! ok

--8<-- assign-function-to-function
--# assume x: function
--# assume y: function
x = y
--! ok

--8<-- assign-var-subtype-1
--# assume p: number
--# assume q: integer
p = q
--! ok

--8<-- assign-var-subtype-2
--# assume p: integer
--# assume q: 1|2
p = q
--! ok

--8<-- assign-var-suptype-1
--# assume p: integer
--# assume q: number
p = q
--@^ Error: Cannot assign `number` into `integer`
--@^^ Note: The other type originates here
--! error

--8<-- assign-var-suptype-2
--# assume p: 1|2
--# assume q: integer
p = q
--@^ Error: Cannot assign `integer` into `(1|2)`
--@^^ Note: The other type originates here
--! error

--8<-- assign-var-map-eqtype
--# assume p: map<string, number>
--# assume q: map<string, number?>
--# assume r: map<string, number!>
--# assume x: number
p.x = x
q.x = x
r.x = x
--! ok

--8<-- assign-var-map-subtype
--# assume p: map<string, number>
--# assume q: map<string, number?>
--# assume r: map<string, number!>
--# assume x: integer
p.x = x
q.x = x
r.x = x
--! ok

--8<-- assign-var-map-nil
--# assume p: map<string, number>
--# assume q: map<string, number?>
--# assume r: map<string, number!>
p.x = nil
q.x = nil
r.x = nil
--! ok

--8<-- assign-var-map-suptype-1
--# assume p: map<string, integer>
--# assume q: number
p.x = q
--@^ Error: Cannot assign `number` into `integer`
--@^^ Note: The other type originates here
--! error

--8<-- assign-var-map-suptype-2
--# assume p: map<string, integer?>
--# assume q: number
p.x = q
--@^ Error: Cannot assign `number` into `integer?`
--@^^ Note: The other type originates here
--! error

--8<-- assign-var-map-suptype-3
--# assume p: map<string, integer!>
--# assume q: number
p.x = q
--@^ Error: Cannot assign `number` into `integer`
--@^^ Note: The other type originates here
-- the error message should not mention `integer!`
--! error

--8<-- assign-record-1
local t = {}
t.a = 42
local x = t.b --@< Error: Missing key "b" in `{a: integer, ...}`
--! error

--8<-- assign-record-2
local t = {a = nil} --: {a: integer}
local x = t.a
t.a = 42
local x = t.a --: integer
--! ok

--8<-- assign-record-extension-1
local t = {}
local u = {} --: {}
t.a = 42
u.b = 54 --@< Error: Missing key "b" in `{}`
local x = t.a + u.b --: integer --@< Error: Missing key "b" in `{}`
--! error

--8<-- assign-record-extension-2
local t = {}
local u = {} --: {...}
t.a = 42
u.b = 54
local x = t.a + u.b --: integer
--! ok

--8<-- assign-record-row-variable
local x = {a = 1, b = 'foo'}

local y = x --: {a: integer, b: string, c: string?}
y.c = 'foo'
local c = x.c --: string

local z = x --: {a: integer, b: string, c: integer?}
--@^ Error: Cannot assign `{a: 1, b: "foo", c: string?, ...}` into `{a: integer, b: string, c: integer?}`
--@^^ Note: The other type originates here
--! error

--8<-- require-dummy
--# assume global `require`: [require] function(string) --> any
x = require(y) --@< Error: Global or local variable `y` is not defined
--! error

--8<-- require-unknown
--# assume global `require`: [require] function(string) --> any
x = require 'a' --@< Warning: Cannot resolve the module name given to `require`
--! ok

--8<-- require-unknown-returns-1
--# assume global `require`: [require] function(string) --> any
x = require 'a' --@< Warning: Cannot resolve the module name given to `require`
local y = x + 4 --@< Error: Cannot apply + operator to `any` and `4`
                --@^ Cause: `any` is not a subtype of `number`
--! error

--8<-- require-unknown-returns-2
--# assume global `require`: [require] function(string) --> any
x = require 'A' --: integer --@< Warning: Cannot resolve the module name given to `require`
                            --@^ Error: Cannot assign `any` into `integer`
                            --@^^ Note: The other type originates here

--& a
return 42

--! error

--8<-- require-returns-integer
--# assume global `require`: [require] function(string) --> any
x = require 'a' --: integer

--& a
return 42

--! ok

--8<-- require-returns-false
--# assume global `require`: [require] function(string) --> any
require 'a'
--@^ Error: Returning `false` from the module disables Lua's protection against recursive `require` calls and is heavily discouraged
-- XXX the span should be ideally at `return`

--& a
return false -- false triggers a Lua bug, so it is prohibited

--! error

--8<-- require-returns-dynamic
--# assume global `require`: [require] function(string) --> any
local a = require 'a'
a(a*a[a])

--& a
--# assume x: WHATEVER
return x

--! ok

--8<-- require-returns-string
--# assume global `require`: [require] function(string) --> any
x = require 'a' --: string

--& a
local function p() return 'hello' end
return p()

--! ok

--8<-- require-returns-func
--# assume global `require`: [require] function(string) --> any
local y = (require 'a')() + 5

--& a
local function p() return 42 end
return p

--! ok

--8<-- require-returns-not-fully-resolved
--# assume global `require`: [require] function(string) --> any
require 'a'
--@^ Error: The module has returned a type `<unknown type>` that is not yet fully resolved
-- XXX the span should be ideally at `return`

--& a
--# open `internal kailua_test`
return kailua_test.gen_tvar()

--! error

--8<-- require-no-returns
--# assume global `require`: [require] function(string) --> any
x = require 'a' --: true

--& a
local a = 54
--! ok

--8<-- require-nested
--# assume global `require`: [require] function(string) --> any
require 'a'

--& a
require 'b'

--& b
return true

--! ok

--8<-- require-nested-idempotent
--# assume global `require`: [require] function(string) --> any
require 'a'
require 'b'
require 'a'

--& a
require 'b'

--& b
return true

--! ok

--8<-- require-recursive
--# assume global `require`: [require] function(string) --> any
require 'a' --@< Note: The module was previously `require`d here

--& a
require 'b'

--& b
require 'a' --@< Error: Recursive `require` was requested

--! error

--8<-- require-disconnected
--# assume global `require`: [require] function(string) --> any

--& a
require 'b'

--& b
require 'a'

-- check is dynamic
--! ok

--8<-- require-name-expr
--# assume global `require`: [require] function(string) --> any
x = require('a' .. 'b') --: number

--& ab
return 42

--! ok

--8<-- require-type-local
--# assume global `require`: [require] function(string) --> any
local x = require('x')
local y = require('y')
local z = x + y --: integral --@< Error: Type `integral` is not defined

--& x
--# type local integral = integer
local p = 4 --: integral
return p

--& y
local q = 5 --: integral --@< Error: Type `integral` is not defined
return q

--! error

--8<-- require-type-export
--# assume global `require`: [require] function(string) --> any
local x = require('x')
local y = require('y')
local z = x + y --: integral

--& x
--# type integral = integer
local p = 4 --: integral
return p

--& y
local q = 5 --: integral --@< Error: Type `integral` is not defined
return q

--! error

--8<-- require-type-global
--# assume global `require`: [require] function(string) --> any
local x = require('x')
local y = require('y')
local z = x + y --: integral

--& x
--# type global integral = integer
local p = 4 --: integral
return p

--& y
local q = 5 --: integral
return q

--! ok

--8<-- require-type-no-reexport
--# assume global `require`: [require] function(string) --> any
local x = require('x')
local y = x + 3 --: integral --@< Error: Type `integral` is not defined

--& x
local p = require('y')
local q = p + 2 --: integral
return q

--& y
--# type integral = integer
local a = 1 --: integral
return a

--! error

--8<-- require-type-reexport
--# assume global `require`: [require] function(string) --> any
local x = require('x')
local y = x + 3 --: integral

--& x
local p = require('y')
--# type integral = integral
local q = p + 2 --: integral
return q

--& y
--# type integral = integer
local a = 1 --: integral
return a

--! ok

--8<-- require-type-import-no-shadowing
--# assume global `require`: [require] function(string) --> any
--# type local stringy = string --@< Note: The type was originally defined here
local x = require('x') --@< Error: A type `stringy` to be imported is already defined
local z = 'string' .. x.to_string() --: stringy

--& x
--# type stringy = { to_string: function() --> string }
local p = { to_string = function() return 'foo' end } --: stringy
return p

--! error

--8<-- require-type-import-multiple
--# assume global `require`: [require] function(string) --> any
local z = 0
do
    local x = require('x')
    local y = x + 4 --: integral
    z = z + y
end
do
    local x = require('x')
    local y = x + 5 --: integral
    z = z + y
end
local w = z + z --: integral --@< Error: Type `integral` is not defined

--& x
--# type integral = integer
local p = 42 --: integral
return p

--! error

--8<-- require-diverges-1
--# assume global `require`: [require] function(string) --> any
x = require 'a' --: nil -- it's really WHATEVER
--@^-< Warning: A portion of this expression won't be evaluated because it contains a call to a function that never returns

--& a
while true do end
--! ok

--8<-- require-diverges-2 -- feature:warn_on_dead_code
--# assume global `require`: [require] function(string) --> any
require 'a'
y = 42 --@< Warning: This code will never execute

--& a
while true do end
--! ok

--8<-- require-diverges-3 -- feature:warn_on_dead_code
--# assume global `require`: [require] function(string) --> any
local cond = true
if cond then
    require 'a'
else
    -- this will correctly diverge as cached above
    require 'a'
end
z = 42 --@< Warning: This code will never execute

--& a
while true do end
--! ok

--8<-- index-assign-typed
local p = {x = 5, y = 6} --: {x:number, y:number}
p.x = 'string' --@< Error: Cannot assign `"string"` into `number`
               --@^ Note: The other type originates here
--! error

--8<-- index-assign-dynamic
--# assume p: WHATEVER
p[1] = 42
--! ok

--8<-- for-in-simple-iter-1
--# assume func: const function(nil, nil) --> number?
for x in func do
    local a = x * 3
end
--! ok

--8<-- for-in-simple-iter-2
--# assume func: const function(nil, nil) --> number    -- technically infinite loop
for x in func do
    local a = x * 3
end
--! ok

--8<-- for-in-simple-iter-3
--# assume func: const function(nil, nil) --> number|string
for x in func do
    local a = x * 3 --@< Error: Cannot apply * operator to `(number|string)` and `3`
                    --@^ Cause: `(number|string)` is not a subtype of `number`
end
--! error

--8<-- for-in-simple-iter-bad-arity
--# assume func: const function() --> number
for x in func do --@< Error: The type `function() --> number` cannot be called
                 --@^ Cause: Cannot give more than 1 argument(s) to the function
    local a = x * 3
end
--! error

--8<-- for-in-stateful-iter-1
--# assume func: const function({const integer}, integer?) --> integer?
--# assume state: const {const integer}
--# assume first: const integer?
for x in func, state, first do
    local a = x * 3
end
--! ok

--8<-- for-in-stateful-iter-2
--# assume func: const function({const integer}, integer|string?) --> integer?
--# assume state: const {const integer}
--# assume first: const integer|string     -- the first value is silently discard
for x in func, state, first do
    local a = x * 3
end
--! ok

--8<-- for-in-multi
--# assume func: const function({const integer}, integer|string?) --> (integer?, string)
--# assume state: const {const integer}
--# assume first: const integer?
for x, y in func, state, first do
    local a = x --: integer
    local b = y --: string
end
--! ok

--8<-- for-in-non-func
--@v Error: The iterator given to `for`-`in` statement returned a non-function type `"hello"`
for x in 'hello' do
end
--! error

--8<-- for-in-non-func-recover
--@v Error: The iterator given to `for`-`in` statement returned a non-function type `"hello"`
for x in 'hello' do
    x()
    y() --@< Error: Global or local variable `y` is not defined
end
--! error

--8<-- for-in-diverges-1
--# assume func: const function(nil, nil) --> !
for x in func do
end
--! ok

--8<-- for-in-diverges-2 -- feature:warn_on_dead_code
--# assume func: const function(nil, nil) --> !
for x in func do
    local y = x * 42 .. x --@< Warning: This code will never execute
end
local x = 42 --@< Warning: This code will never execute
--! ok

--8<-- redefine-global
p = 42 --: integer
p = 54 --: integer --@< Error: Cannot redefine the type of a variable `p`
p = 63 --: integer --@< Error: Cannot redefine the type of a variable `p`
--! error

--8<-- redefine-global-in-single-stmt-1
p, p = 42, 54 --: integer, integer
--@^ Error: Cannot redefine the type of a variable `p`
--! error

--8<-- redefine-global-in-single-stmt-2
p, p, p = 42, 54, 63 --: integer, integer, integer
--@^ Error: Cannot redefine the type of a variable `p`
--@^^ Error: Cannot redefine the type of a variable `p`
--! error

--8<-- redefine-global-func
function p() end
function p() end --@< Error: Cannot redefine the type of a variable `p`
--! error

--8<-- redefine-local
local p = 42 --: integer
local p = 54 --: integer
local p = 'string' --: string
--! ok

--8<-- redefine-local-func
local function p() end
local function p() end
--! ok

--8<-- unknown-attr
--# --@v Warning: `sudo` is an unknown type attribute and ignored
--# assume sudo: [sudo] {
--#     --@v Warning: `make_sandwich` is an unknown type attribute and ignored
--#     make_sandwich: const [make_sandwich] function(boolean)
--# }
--! ok

--8<-- unknown-attr-func
--v [memoize] --@< Warning: `memoize` is an unknown type attribute and ignored
--v function(x: boolean)
function foo(x) end
--! ok

--8<-- duplicate-attr
-- `[string_meta] [genv] WHATEVER` is syntactically forbidden
--# assume foo: [string_meta] ([genv] WHATEVER)
--@^ Warning: Cannot add an attribute to a type `[genv] WHATEVER` with an existing attribute
--! ok

--8<-- duplicate-attr-func
--v [`internal no_subtype`]
--v [`internal no_subtype2`] --@< Warning: Cannot add an attribute to a function specification with an existing attribute
--v function(x: boolean)
function foo(x) end
--! ok

--8<-- duplicate-attr-func-no-check
--v [`internal no_subtype`]
--v [NO_CHECK] -- does not count
--v [`internal no_subtype2`] --@< Warning: Cannot add an attribute to a function specification with an existing attribute
--v function(x: boolean)
function foo(x) end
--! ok

--8<-- bad-attr-value
--# assume foo: [type] WHATEVER
--# assume bar: [type()] WHATEVER --@< Error: The type attribute `type` cannot have any values
--# assume baz: [type(hello)] WHATEVER --@< Error: The type attribute `type` cannot have any values
--# assume quux: [type(hello, goodbye)] WHATEVER --@< Error: The type attribute `type` cannot have any values
--! error

--8<-- builtin-with-subtyping-1
--# assume x: [`internal subtype`] number
--# assume y: number
x = y
--@^ Error: Cannot assign `number` into `[internal subtype] number`
--@^^ Note: The other type originates here
--! error

--8<-- builtin-with-subtyping-2
--# assume x: [`internal subtype`] number
--# assume y: number
y = x
--! ok

--8<-- builtin-without-subtyping-1
--# assume x: [`internal no_subtype`] number
--# assume y: number
x = y
--! ok

--8<-- builtin-without-subtyping
--# assume x: [`internal no_subtype`] number
--# assume y: number
y = x
--! ok

--8<-- package-path-non-literal-local-1
-- while this is very contrived, this and subsequent tests check the ability to
-- declare *and* assign special variables altogether.
--# assume x: string
local p = x --: [package_path] string
--@^ Warning: Cannot infer the values assigned to the `package_path` built-in variable; subsequent `require` may be unable to find the module path
--! ok

--8<-- package-path-non-literal-local-2
--# assume x: string
local p = '' --: [package_path] string
local q = 0 --: integer
p, q = x, 42
--@^ Warning: Cannot infer the values assigned to the `package_path` built-in variable; subsequent `require` may be unable to find the module path
--! ok

--8<-- package-path-non-literal-global-1
--# assume x: string
p = x --: [package_path] string
--@^ Warning: Cannot infer the values assigned to the `package_path` built-in variable; subsequent `require` may be unable to find the module path
--! ok

--8<-- package-path-non-literal-global-2
--# assume x: string
p, q = x, x --: [package_path] string, [package_cpath] string
--@^ Warning: Cannot infer the values assigned to the `package_path` built-in variable; subsequent `require` may be unable to find the module path
--@^^ Warning: Cannot infer the values assigned to the `package_cpath` built-in variable; subsequent `require` may be unable to find the module path
--! ok

--8<-- if-false-warning-1 -- feature:warn_on_useless_conds
--@v-vvvvv Warning: These `if` case(s) are never executed
if false then --@< Note: This condition always evaluates to a falsy value
    local a
    local b
    local c
end
--! ok

--8<-- if-false-warning-2 -- feature:warn_on_useless_conds
--@v-vv Warning: These `if` case(s) are never executed
if false then --@< Note: This condition always evaluates to a falsy value
    local a
else
    local b
    local c
end
--! ok

--8<-- if-false-warning-3 -- feature:warn_on_useless_conds
--# assume x: boolean
if x then
    local a
--@v-vv Warning: These `if` case(s) are never executed
elseif false then --@< Note: This condition always evaluates to a falsy value
    local b
--@v-vv Warning: These `if` case(s) are never executed
elseif false then --@< Note: This condition always evaluates to a falsy value
    local c
else
    local d
end
--! ok

--8<-- if-true-warning-1 -- feature:warn_on_useless_conds
if true then --@< Warning: This condition always evaluates to a truthy value
    local a
    local b
    local c
end
--! ok

--8<-- if-true-warning-2 -- feature:warn_on_useless_conds
--# assume x: boolean
if x then
    local a
elseif true then --@< Warning: This condition always evaluates to a truthy value
    local b
end
--! ok

--8<-- if-true-warning-3 -- feature:warn_on_useless_conds
--# assume x: boolean
if true then --@< Note: This condition always evaluates to a truthy value
    local a
elseif x then --@<-vvvv Warning: These `if` case(s) are never executed
    local b
else
    local c
end
--! ok

--8<-- if-true-warning-4 -- feature:warn_on_useless_conds
--# assume x: boolean
if x then
    local a
elseif true then --@< Note: This condition always evaluates to a truthy value
    local b
elseif true then --@<-vvvvvv Warning: These `if` case(s) are never executed
    local c
elseif false then
    local d
else
    local e
end
--! ok

--8<-- if-warning-varargs-1 -- feature:warn_on_useless_conds exact
--v function() --> (string...)
function f() end
if f() then
end
--! ok

--8<-- if-warning-varargs-2 -- feature:warn_on_useless_conds
--v function() --> (string, string...)
function f() return 'foo' end
if f() then --@< Warning: This condition always evaluates to a truthy value
end
--! ok

--8<-- while-false-warning -- feature:warn_on_dead_code
while false do
    local a --@<-vv Warning: This code will never execute
    local b
    local c
end
--! ok

--8<-- unassigned-local-var-1
local p --: string!
--! ok

--8<-- unassigned-local-var-2
local p --: string!
--v function(x: string)
local function f(x) end
f(p)
--@^ Error: The variable is not yet initialized
--@1 Note: The variable was not implicitly initialized to `nil` as its type is `string!`
f(p)
--@^ Error: The variable is not yet initialized
--@1 Note: The variable was not implicitly initialized to `nil` as its type is `string!`
p = 'string'
f(p) -- no longer an error
--! error

--8<-- unassigned-local-var-nil-1
local p --: string
--! ok

--8<-- unassigned-local-var-nil-2
local p --: string
--v function(x: string)
local function f(x) end
f(p)
p = 'string'
f(p)
p = nil
f(p)
--! ok

--8<-- unassigned-global-var
q, p = 42 --: integer, string!
--@^ Error: Cannot assign `nil` into `string!`
--@^^ Note: The other type originates here
--! error

--8<-- table-update-nested-1
local a = {}
a.b = {}
a.b.c = {}
a.b.c.d = {}
--! ok

--8<-- table-update-nested-2
local a = { b = {} }
a.b.c = {}
a.b.c.d = {}
--! ok

--8<-- table-update-nested-3
local a = { b = { c = {} } }
a.b.c.d = {}
--! ok

--8<-- method-decl
local p = {}
function p.a() end
p.a()
--! ok

--8<-- method-decl-nested
local p = {a = {}}
function p.a.b() end
p.a.b()
--! ok

--8<-- method-decl-self
local p = {}
function p:a() end
p:a()
--! ok

--8<-- method-decl-self-nested
local p = {a = {}}
function p.a:b() end
p.a:b()
--! ok

--8<-- method-decl-nontable
local p = 42
function p.a() end --@< Error: Tried to index a non-table type `integer`
p.a()              --@< Error: Tried to index a non-table type `integer`
--! error

--8<-- method-decl-nontable-nested
local p = {a = 42}
function p.a.b() end --@< Error: Tried to index a non-table type `42`
p.a.b()              --@< Error: Tried to index a non-table type `42`
--! error

--8<-- method-decl-const
local p = {} --: const {}
function p.a() end --@< Error: Cannot update the immutable type `const {}` by indexing
p.a() --@< Error: Missing key "a" in `const {}`
--! error

--8<-- method-decl-const-nested
local p = { a = {} } --: const {a: const {}}
function p.a.b() end --@< Error: Cannot update the immutable type `const {}` by indexing
p.a.b() --@< Error: Missing key "b" in `const {}`
--! error

--8<-- methodcall-string-meta-table
--# assume blah: [string_meta] { byte: function(string) --> integer }
local x = ('f'):byte() --: integer
--! ok

--8<-- methodcall-string-meta-dynamic
--# assume blah: [string_meta] WHATEVER
local x = ('f'):foobar(1, 'what', false) --: string
--! ok

--8<-- methodcall-string-meta-undefined
local x = ('f'):byte()
--@^ Error: Cannot use string methods as a metatable for `string` type is not yet defined
--! error

--8<-- methodcall-string-meta-non-table
--# assume blah: [string_meta] integer
--@^ Note: A metatable for `string` type has been previously defined here
local x = ('f'):byte() --: integer
--@^ Error: A metatable for `string` type has been defined but is not a table
-- XXX it is not very clear that we should catch it from the definition
--! error

--8<-- string-meta-redefine
--# assume blah: [string_meta] { byte: function(string) --> integer }
--@^ Note: A metatable for `string` type has been previously defined here
--# assume blah2: [string_meta] { lower: function(string) --> string }
--@^ Error: A metatable for `string` type cannot be defined more than once and in general should only be set via `--# open` directive
--! error

--8<-- methodcall-recover
local q = {}
local x = q:f() --@< Error: Missing key "f" in `{...}`
-- `x` should be a dummy type now, so the following shouldn't fail
x()
local p = 3 + x
--! error

--8<-- no-check
--v [NO_CHECK]
--v function(x: integer) --> integer
function foo(x)
    return x + "string"
end
--! ok

--8<-- no-check-func-type
--v [NO_CHECK]
--v function(x: integer) --> integer
function foo(x)
    return x + "string"
end

local a = foo(3) --: integer

local b = foo(4) --: string
--@^ Error: Cannot assign `integer` into `string`
--@^^ Note: The other type originates here

local c = foo('hi') --: integer
--@^ Error: The type `function(x: integer) --> integer` cannot be called
--@^^ Cause: First function argument `"hi"` is not a subtype of `integer`
--@^^^ Note: The other type originates here

--! error

--8<-- no-check-untyped-args -- feature:!no_implicit_func_sig
--v [NO_CHECK]
function foo(x) --> boolean --@< Error: [NO_CHECK] attribute requires the arguments to be typed
    return x + "string"
end
--! error

--8<-- no-check-untyped-varargs
--@v-vvvvv Error: [NO_CHECK] attribute requires the variadic arguments to be typed
--v [NO_CHECK]
function foo(x, --: integer
             ...) --> boolean
    return x + "string"
end
--! error

--8<-- no-check-untyped-returns
--@v-vvvv Error: [NO_CHECK] attribute requires the return type to be present
--v [NO_CHECK]
function foo(x) --: integer
    return x + "string"
end
--! error

--8<-- no-check-untyped-self
foo = {}

--@vvv Error: [NO_CHECK] attribute requires that the type for `self` argument is clear; directly specify the type for `self` with the function declaration instead
--v [NO_CHECK]
--v method(x: integer) --> boolean
function foo:bar(x)
    return x + "string"
end

local a = foo:bar(4) --: integer --@< Error: Cannot assign `boolean` into `integer`
                                 --@^ Note: The other type originates here
--! error

--8<-- no-check-func-hint
--v function(f: function(a: integer) --> boolean)
function foo(f) end

foo(--v [NO_CHECK]
    function(x) return x + "string" end)
--! ok

--8<-- no-check-func-hint-ignored
--v function(f: function(a: integer) --> boolean)
function foo(f) end

foo(--v [NO_CHECK]
    --v function(x: integer) --> boolean
    function(x) return x + "string" end)
--! ok

--8<-- no-check-func-hint-partial
--v function(f: function(a: integer) --> boolean)
function foo(f) end

--@vvv Error: The type `function(f: function(a: integer) --> boolean) --> ()` cannot be called
--@vv-vvvvv Cause: First function argument `function(x: integer) --> string` is not a subtype of `function(a: integer) --> boolean`
--@v Note: The other type originates here
foo(--v [NO_CHECK]
    function(x) --> string
        return x + "string"
    end)

--@vvv Error: The type `function(f: function(a: integer) --> boolean) --> ()` cannot be called
--@vv-vvvvv Cause: First function argument `function(x: string) --> boolean` is not a subtype of `function(a: integer) --> boolean`
--@v Note: The other type originates here
foo(--v [NO_CHECK]
    function(x) --: string
        return x + "string"
    end)
--! error

--8<-- no-check-diverges
--v [NO_CHECK]
--v function(x: integer) --> !
function foo(x)
    return 42
end
--! ok

--8<-- local-func-without-sibling-scope-1
local r
--v function(p: any)
function r(p)
end
--! ok

--8<-- local-func-without-sibling-scope-2
local r
do
    local s
    --v function(p: any)
    function r(p)
    end
end
--! ok

--8<-- void-arbitrary
x = 42
y = "foo"
-- the first is a parsing error, and the second is a type error from the recovered AST
x + y --@< Error: Only function calls are allowed as statement-level expressions
      --@^ Error: Cannot apply + operator to `42` and `"foo"`
      --@^^ Cause: `"foo"` is not a subtype of `number`
--! error

--8<-- assign-empty-rhs
a = 42 --: integer
b = "string" --: string
do
    a, b -- this won't generate any type error! the following is a parsing error.
end --@< Error: Expected `=`, got a keyword `end`
--! ok

--8<-- funccall-desugared
local function f(x) --: any
end
f'oo'
f[[unction]]
f{reak=true}
--! ok

--8<-- seq-type-without-paren
local function f()
    return 1, 2, 3
end
--v function(a: integer, b: integer)
local function g(a, b)
end
g(0, f()) --@< Error: The type `function(a: integer, b: integer) --> ()` cannot be called
          --@^ Cause: Cannot give more than 2 argument(s) to the function
          --@^^ Note: The other type originates here
--! error

--8<-- seq-type-with-paren
local function f()
    return 1, 2, 3
end
--v function(a: integer, b: integer)
local function g(a, b)
end
g(0, (f()))
--! ok

--8<-- table-lit-duplicate-key-1
local a = {
    what = 4,
    what = 5, --@< Error: The key `what` is duplicated in the table constructor
              --@^^ Note: The key was previously assigned here
}
--! error

--8<-- table-lit-duplicate-key-2
local a = {
    1,
    2,
    3,
    4,
    [2] = 5, --@< Error: The key `2` is duplicated in the table constructor
             --@^^^^ Note: The key was previously assigned here
}
--! error

--8<-- table-lit-duplicate-key-3
local a = {
    [2] = 0,
    1,
    2, --@< Error: The key `2` is duplicated in the table constructor
       --@^^^ Note: The key was previously assigned here
    3,
    4,
}
--! error

--8<-- table-lit-rec-arbitrary-key
--# assume k: string
local a = {[k] = 42} --@< Error: The type `string` cannot be used as a key in the table constructor for records
                     --@^ Note: The type of this table was unknown so is assumed to be a record; please specify its type
--! error

--8<-- table-lit-rec-non-stringy-key
--# assume k: table
local a = {[k] = 42} --@< Error: The type `table` cannot be used as a key in the table constructor for records
                     --@^ Note: The type of this table was unknown so is assumed to be a record; please specify its type
--! error

--8<-- table-lit-rec-bounded-seq
function f() return 3, 4 end
local a = {1, 2, f()}
local aa = a --: {integer, integer, integer, integer}
--! ok

--8<-- table-lit-rec-unbounded-seq
function f() --> (integer...)
end
-- note the missing hint
local a = {1, 2, f()} --@< Error: This expression has an unknown number of return values, so cannot be used as the last value in the table constructor for records
                      --@^ Note: The type of this table was unknown so is assumed to be a record; please specify its type
--! error

--8<-- table-lit-subtyping-1
-- avoid hinting
local a = {1, 2, 3}
local aa = a --: vector<integer>
local b = {1, 2, 3, [4] = 4}
local bb = b --: vector<integer>
local c = {1, 2, 3, [8] = 4}
local cc = c --: map<integer, integer>
--! ok

--8<-- table-lit-subtyping-2
local d = {a = 3, b = 4}
local dd = d --: map<string, integer>
local e = {1, 2, 3, string = 4}
local ee = e --: map<integer|string, integer>
--! ok

--8<-- table-lit-subtyping-3
local x = {1, 2, 3, [5] = 4}
local xx = x --: vector<integer>
--@^ Error: Cannot assign `{1, 2, 3, 5: 4}` into `vector<integer>`
--@^^ Note: The other type originates here
--! error

--8<-- table-lit-subtyping-4
local y = {1, 2, 3, string = 4}
local yy = y --: vector<integer>
--@^ Error: Cannot assign `{1, 2, 3, string: 4, ...}` into `vector<integer>`
--@^^ Note: The other type originates here
--! error

--8<-- table-lit-subtyping-5
local z = {1, 2, 3, string = 4}
local zz = z --: map<integer, integer>
--@^ Error: Cannot assign `{1, 2, 3, string: 4, ...}` into `map<integer, integer>`
--@^^ Note: The other type originates here
--! error

--8<-- table-lit-no-splicing-with-key
function f() --> (integer, string)
    return 42, 'foo'
end

local a = { [1] = f() } --: {integer}
local b = { [1] = f() } --: vector<integer>
--! ok

--8<-- table-lit-hint-rec -- exact
function f() --> (integer...)
end

-- there should be *no* hint about the explicit type, as we already have one
local a = {1, 2, f()} --: {integer, integer}
--@^ Error: This expression has an unknown number of return values, so cannot be used as the last value in the table constructor for records
--! error

--8<-- table-lit-hint-rec-subtype
local s = 'string'
local t = 42
local a = {s, t} --: {any, any}
local b = {'string', 42} --: {any, any}
--! ok

--8<-- table-lit-hint-vector-1
local function f() --> (integer...)
end

local a = {} --: vector<integer>
local b = {1, 2, 3} --: vector<integer>
local c = {f()} --: vector<integer>
local d = {1, 2, 3, f()} --: vector<integer>
--! ok

--8<-- table-lit-hint-vector-2
local function f() --> ((4|5)...)
end

local a = {1, 2, 3} --: vector<1|2|3|4|5>
local b = {1, 2, 3, f()} --: vector<1|2|3|4|5>

local c = {1, 2, 3, f()} --: vector<1|2|3|4>
--@^ Error: The type `(4|5)` cannot be used as a value in the table constructor for the type `vector<(1|2|3|4)>`
--@^^ Cause: `(4|5)` is not a subtype of `(1|2|3|4)`
--@^^^ Note: The other type originates here
--! error

--8<-- table-lit-hint-vector-3
local function f() --> (integer...)
end

local a = {1, 2} --: vector<const string>
--@^ Error: The type `1` cannot be used as a value in the table constructor for the type `vector<const string>`
--@^^ Cause: `1` is not a subtype of `string`
--@^^^ Note: The other type originates here
--@^^^^ Error: The type `2` cannot be used as a value in the table constructor for the type `vector<const string>`
--@^^^^^ Cause: `2` is not a subtype of `string`
--@^^^^^^ Note: The other type originates here

local b = {f()} --: vector<string>
--@^ Error: The type `integer` cannot be used as a value in the table constructor for the type `vector<string>`
--@^^ Cause: `integer` is not a subtype of `string`
--@^^^ Note: The other type originates here
--! error

--8<-- table-lit-hint-vector-4
local x = 42 --: integer
local a = {[x] = 43} --: vector<integer>
--@^ Error: The type `integer` cannot be used as a key in the table constructor for arrays

local y = 42 --: 42?
local b = {[y] = 43} --: vector<integer>
--@^ Error: The type `42?` cannot be used as a key in the table constructor for arrays

local z = 1 --: 1
local c = {[z] = 43} --: vector<integer>
--! error

--8<-- table-lit-hint-vector-5
local a = {[2] = 2, [3] = 3} --: vector<integer>
--@^ Error: The minimum key in the table constructor for arrays is not 1
local b = {[1] = 1, [3] = 3} --: vector<integer>
--@^ Error: Keys in the table constructor for arrays have a missing key
--! error

--8<-- table-lit-hint-vector-subtype
local s = 'string'
local a = {s} --: vector<any>
local b = {'string'} --: vector<any>
--! ok

--8<-- table-lit-hint-map-1
local function f() --> (integer...)
end

local a = {} --: map<integer, integer>
local b = {1, 2, 3} --: map<integer, const integer>
local c = {f()} --: map<integer, const integer>
local d = {1, 2, 3, f()} --: map<integer, integer>
local e = {[7]=1, [9]=2, [4]=3, f()} --: map<integer, integer>
--! ok

--8<-- table-lit-hint-map-2
local function f() --> (integer...)
end

local a = {1, 2} --: map<integer, const string>
--@^ Error: The type `1` cannot be used as a value in the table constructor for the type `map<integer, const string>`
--@^^ Cause: `1` is not a subtype of `string`
--@^^^ Note: The other type originates here
--@^^^^ Error: The type `2` cannot be used as a value in the table constructor for the type `map<integer, const string>`
--@^^^^^ Cause: `2` is not a subtype of `string`
--@^^^^^^ Note: The other type originates here

local b = {f()} --: map<integer, string>
--@^ Error: The type `integer` cannot be used as a value in the table constructor for the type `map<integer, string>`
--@^^ Cause: `integer` is not a subtype of `string`
--@^^^ Note: The other type originates here
--! error

--8<-- table-lit-hint-map-3
local function f() --> (integer...)
end

local a = {1, [7] = 2} --: map<string, const integer>
--@^ Error: The type `1` cannot be used as a key in the table constructor for the type `map<string, const integer>`
--@^^ Cause: `1` is not a subtype of `string`
--@^^^ Note: The other type originates here
--@^^^^ Error: The type `7` cannot be used as a key in the table constructor for the type `map<string, const integer>`
--@^^^^^ Cause: `7` is not a subtype of `string`
--@^^^^^^ Note: The other type originates here

local b = {f()} --: map<string, integer>
--@^ Error: The type `integer` cannot be used as a key in the table constructor for the type `map<string, integer>`
--@^^ Cause: `integer` is not a subtype of `string`
--@^^^ Note: The other type originates here
--! error

--8<-- table-lit-hint-map-subtype
local s = 'string'
local a = {a = 'string'} --: map<string, any>
local b = {b = s} --: map<string, any>
--! ok

--8<-- table-lit-hint-table
local function f() --> (function()...)
end

local x = nil --: thread
local a = {[x] = 2, [f()] = x, f()} --: table
--! ok

--8<-- table-lit-hint-duplicate-key
-- duplicate keys are checked as much as possible
-- even when the hint doesn't require integral or stringy keys
local x = nil --: thread
local a = {
    foo = 1,
    [x] = 2,
    [1] = 3,
    [x] = 4, -- this cannot be caught
    bar = 5,
    foo = 6, --@< Error: The key `foo` is duplicated in the table constructor
             --@^^^^^^ Note: The key was previously assigned here
    7, --@< Error: The key `1` is duplicated in the table constructor
       --@^^^^^^ Note: The key was previously assigned here
} --: table
--! error

--8<-- no-table-union
local a --: {string} | {integer}
--@^ Error: This union type is not supported in the specification
--@^^ Cause: Cannot create a union type of `{string}` and `{integer}`
--@^^^ Note: The other type originates here
--! error

--8<-- no-function-union
local a --: (function(string)) | (function(integer))
--@^ Error: This union type is not supported in the specification
--@^^ Cause: Cannot create a union type of `function(string) --> ()` and `function(integer) --> ()`
--@^^^ Note: The other type originates here
--! error

--8<-- no-true-false-union
local a --: true | false -- should be written as `boolean`
--@^ Error: This union type is not supported in the specification
--@^^ Cause: Cannot create a union type of `true` and `false`
--@^^^ Note: The other type originates here
--! error

--8<-- maximally-disjoint-union
local a --: true | 42 | 'foobar' | thread | userdata | (function()) | {string}
--! ok

--8<-- union-assert-or-1
--# open `internal kailua_test`
local a = kailua_test.gen_tvar()
-- a is unresolved
local s = '' --: boolean|string
-- `s or a` forces an unresolved a to have the same type to the truthy part of s
local u = s or a --: true|string
-- so that a's type is now known
local b = a --: true|string
a = b
--! ok

--8<-- union-assert-or-2
--# open `internal kailua_test`
local a = kailua_test.gen_tvar()
-- a is unresolved
local t = {a}
local s = {''} --: {boolean|string}
-- `s or t` forces an unresolved t to have the same type to the truthy part of s
local u = s or t --: {boolean|string}
-- so that a's type is now known
local b = a --: {true|string}
a = b
--! ok

--8<-- union-assert-return -- feature:!no_implicit_func_sig
function x(q, a)
    if q then
        return 42
    else
        -- a is forced to have the same type to the previous return type `true`
        return a
    end
end
local y = x(true, false)
--@^ Error: The type `function(q: <unknown type>, a: integer) --> integer` cannot be called
--@^^ Cause: Second function argument `false` is not a subtype of `integer`
--@^^^ Note: The other type originates here
--! error

--8<-- table-assign-just
({a=1, b=2}).c = 3
--! ok

--8<-- table-assign-const
local p = {} --: const {}
p.a = 42 --@< Error: Cannot update the immutable type `const {}` by indexing
--! error

--8<-- table-assign-const-nested-1
local p = {a = {}, b = {}} --: {a: {}, b: const {}}
p.a.x = 42 --@< Error: Missing key "x" in `{}`
p.b.y = 54 --@< Error: Cannot update the immutable type `const {}` by indexing
--! error

--8<-- table-assign-const-nested-2
local p = {a = {}, b = {}} --: {a: {...}, b: const {...}}
p.a.x = 42
p.b.y = 54 --@< Error: Cannot update the immutable type `const {...}` by indexing
--! error

--8<-- table-assign-type-1
local p = {}
p.a = 42 --: 42|43
p.a = 43
--! ok

--8<-- table-assign-type-2
local p = {}
p.a = 42 --: 42|43
p.a = 44 --@< Error: Cannot assign `44` into `(42|43)`
         --@^ Note: The other type originates here
--! error

--8<-- table-assign-type-dup
local p = {}
p.a = 42 --: 42|43
p.a = 42 --: integer --@< Error: Cannot specify the type of indexing expression
--! error

--8<-- table-assign-type-to-vector
local p = {} --: vector<integer>
p[1] = 42 --: integer --@< Error: Cannot specify the type of indexing expression
--! error

--8<-- table-assign-type-to-map
local p = {} --: map<string, integer>
p.a = 42 --: integer --@< Error: Cannot specify the type of indexing expression
--! error

--8<-- table-assign-type-const-1
local p = {}
p.a = 42 --: const integer
p.a = 54 --@< Error: Cannot assign `54` into `const integer`
         --@^ Note: The other type originates here
--! error

--8<-- table-assign-type-const-2
local p = {}
p.a = 42 --: const
p.a = 54 --@< Error: Cannot assign `54` into `const integer`
         --@^ Note: The other type originates here
--! error

--8<-- implicit-literal-type
local x = 42
x = 54
local y = 'string'
y = 'another string'
local z = true
z = false
--! ok

--8<-- implicit-literal-type-in-record
local a = {}
a.x = 42
a.x = 54
a.y = 'string'
a.y = 'another string'
a.z = true
a.z = false
--! ok

--8<-- implicit-literal-type-in-func-args -- feature:!no_implicit_func_sig
-- XXX this essentially depends on the accuracy of constraint solver,
-- which we chose not to concern when we are not explicit about types
local function f(x, y, z)
    x = 42
    x = 54 --@< Error: Cannot assign `54` into `<unknown type>`
           --@^ Note: The other type originates here
    y = 'string'
    y = 'another string' --@< Error: Cannot assign `"another string"` into `<unknown type>`
                         --@^ Note: The other type originates here
    z = true
    z = false  --@< Error: Cannot assign `false` into `<unknown type>`
               --@^ Note: The other type originates here
end
f(0, '', false) --@< Error: The type `function(x: <unknown type>, y: <unknown type>, z: <unknown type>) --> ()` cannot be called
                --@^ Cause: First function argument `0` is not a subtype of `<unknown type>`
                --@^^ Note: The other type originates here
--! error

--8<-- explicit-literal-type
local x = 42 --: 42
x = 54 --@< Error: Cannot assign `54` into `42`
       --@^ Note: The other type originates here
local y = 'string' --: 'string'
y = 'another string' --@< Error: Cannot assign `"another string"` into `"string"`
                     --@^ Note: The other type originates here
local z = true --: true
z = false --@< Error: Cannot assign `false` into `true`
          --@^ Note: The other type originates here
--! error

--8<-- explicit-literal-type-in-record
local a = {}
a.x = 42 --: 42
a.x = 54 --@< Error: Cannot assign `54` into `42`
         --@^ Note: The other type originates here
a.y = 'string' --: 'string'
a.y = 'another string' --@< Error: Cannot assign `"another string"` into `"string"`
                       --@^ Note: The other type originates here
a.z = true --: true
a.z = false --@< Error: Cannot assign `false` into `true`
            --@^ Note: The other type originates here
--! error

--8<-- explicit-literal-type-in-func-args-1
--v function(x: 42, y: 'string', z: true)
local function f(x, y, z)
    x = 42
    x = 54 --@< Error: Cannot assign `54` into `42`
           --@^ Note: The other type originates here
    y = 'string'
    y = 'another string' --@< Error: Cannot assign `"another string"` into `"string"`
                         --@^ Note: The other type originates here
    z = true
    z = false --@< Error: Cannot assign `false` into `true`
              --@^ Note: The other type originates here
end
f(0, '', false) --@< Error: The type `function(x: 42, y: "string", z: true) --> ()` cannot be called
                --@^ Cause: First function argument `0` is not a subtype of `42`
                --@^^ Note: The other type originates here
--! error

--8<-- explicit-literal-type-in-func-args-2
--v function(x: 42|54, y: 'string'|'another string', z: boolean)
local function f(x, y, z)
    x = 42
    x = 54
    y = 'string'
    y = 'another string'
    z = true
    z = false
end
f(0, '', false) --@< Error: The type `function(x: (42|54), y: ("another string"|"string"), z: boolean) --> ()` cannot be called
                --@^ Cause: First function argument `0` is not a subtype of `(42|54)`
                --@^^ Note: The other type originates here
--! error

--8<-- explicit-literal-type-in-func-args-3
--v function(x: integer, y: string, z: boolean)
local function f(x, y, z)
    x = 42
    x = 54
    y = 'string'
    y = 'another string'
    z = true
    z = false
end
f(0, '', false)
--! ok

--8<-- kailua-test-gen-tvar
--# open `internal kailua_test`

local a = kailua_test.gen_tvar()
local b = a --: integer
local c = a --: integer
local d = a --: string --@< Error: Cannot assign `<unknown type>` into `string`
                       --@^ Note: The other type originates here

local a = kailua_test.gen_tvar()
local b = a --: string
local c = a --: integer --@< Error: Cannot assign `<unknown type>` into `integer`
                        --@^ Note: The other type originates here

--! error

--8<-- kailua-test-assert-tvar
--# open `internal kailua_test`

-- concrete type
kailua_test.assert_tvar('string')
--@^ Error: Internal Error: A type `"string"` is not a type variable
local p = 'string'
kailua_test.assert_tvar(p)
--@^ Error: Internal Error: A type `string` is not a type variable

-- type variable (that is later inferred to a concrete type, which doesn't matter)
local a = kailua_test.gen_tvar()
kailua_test.assert_tvar(a)
a = 'string'
kailua_test.assert_tvar(a)
--! error

--8<-- assume-scope
local x --: string
do
    --# assume x: integer
    x = 42
end
local y = x + 42 --@< Error: Cannot apply + operator to `string` and `42`
                 --@^ Cause: `string` is not a subtype of `number`
--! error

--8<-- assume-field
local x = {}
--# assume x.y: integer
local z = x.y + 42 --: integer
--! ok

--8<-- assume-field-no-ref
--# assume x.y: integer --@< Error: Global or local variable `x` is not defined
local z = x.y + 42 --: integer --@< Error: Global or local variable `x` is not defined
--! error

--8<-- assume-field-scope
local x = {}
do
    --# assume x.y: integer
    x.y = 42
end
local y = x.y + 42
--! ok

--8<-- assume-field-overwrite
local x = {y = 'string'}
--# assume x.y: integer
local z = x.y + 42 --: integer
--! ok

--8<-- assume-field-nested
local x = {}
--# assume x.y: {}
--# assume x.y.z: {}
--# assume x.y.z.u: integer
local v = x.y.z.u + 42 --: integer
--! ok

--8<-- assume-field-inferred
--# open `internal kailua_test`
local x = {}
x.y = {}
kailua_test.assert_tvar(x.y)
--# assume x.y.z: integer
local q = x.y.z + 3 --: integer
--! ok

--8<-- assume-field-whatever
--# assume x: WHATEVER
--# assume x.y: integer -- unlike most cases, WHATEVER is an error here
--@^ Error: `--# assume` directive tried to access a field from a non-record type `WHATEVER`
local z = x.y + 42 --: integer
--! error

--8<-- assume-field-missing-1
local x = 54
--# assume x.y: integer
--@^ Error: `--# assume` directive tried to access a field from a non-record type `integer`
local z = x.y + 42 --: integer --@< Error: Tried to index a non-table type `integer`
--! error

--8<-- assume-field-missing-2
local x = {}
--# assume x.y.z: integer
--@^ Error: `--# assume` directive tried to access a missing field
local z = x.y.z + 42 --: integer --@< Error: Missing key "y" in `{...}`
--! error

--8<-- assume-field-unknown
--# open `internal kailua_test`
local x = kailua_test.gen_tvar()

--# assume x.y.z: integer
--@^ Error: `--# assume` directive tried to access a field from a type not yet known enough
local z = x.y.z + 42 --: integer --@< Error: The type `<unknown type>` is tabular but not known enough to index
--! error

--8<-- assume-field-table-or-nil
local x = {} --: {}?
--# assume x.y: integer --@< Error: `--# assume` directive tried to access a field from a non-record type `{}?`
local z = x.y + 42 --: integer --@< Error: Tried to index a non-table type `{}?`
--! error

--8<-- assume-field-static
local x = {}
--# assume static x.y: integer --@< Error: `--# assume` cannot be used to add a static field to a non-class type `{...}`
local z = x.y + 42 --: integer
--! error

--8<-- assume-field-non-class-method
local a = {}
--# assume a.f: method() --> string --@< Error: `--# assume` cannot be used to add a static field to a non-class type `{...}`
--! error

--8<-- dead-code -- feature:warn_on_dead_code
function f()
    local a = 42
    do
        do
            local b = 42
            for i = 1, 10 do
                if i < 5 then
                    break
                end
                do
                    break
                end
                local c --@< Warning: This code will never execute
            end
            return
        end
        local d --@< Warning: This code will never execute
    end
    --@v-vvvv Warning: This code will never execute
    local e = a --: string --@< Error: Cannot assign `integer` into `string`
                           --@^ Note: The other type originates here
    local f = a
    local g = a
end
do
    f()
    f()
    return
end
local z --@< Warning: This code will never execute
--! error

--8<-- dead-code-nested-break-1 -- feature:warn_on_dead_code exact
--# assume x: boolean
--# assume y: boolean
while x do
    while y do
        break
    end
    local z = 0
end
--! ok

--8<-- dead-code-nested-break-2 -- feature:warn_on_dead_code exact
--# assume x: boolean
while x do
    while true do
        break
    end
    local z = 0
end
--! ok

--8<-- funccall-no-rvar-extension-args
--v function(a: {x: string?, y: string?, ...})
function f(a) end

-- these two calls do not interfere with each other!
f({x = 'string', z = 'f'})
f({x = 'string', z = 42})

-- this is still an error
f({x = 54, z = 42}) --@< Error: The type `function(a: {x: string?, y: string?, ...}) --> ()` cannot be called
                    --@^ Cause: First function argument `{x: 54, z: 42, ...}` is not a subtype of `{x: string?, y: string?, ...}`
                    --@^^ Note: The other type originates here
--! error

--8<-- funccall-no-rvar-extension-returns
--v function() --> {x: string, ...}
function f() return {x = 'string'} end

-- these two uses do not interfere with each other!
local a = f()
a.z = 'f'

local b = f()
b.z = 42

-- this is still an error
local c = f() --: {x: integer} --@< Error: Cannot assign `{x: string, ...}` into `{x: integer}`
                               --@^ Note: The other type originates here
local d = f() --: {x: integer, ...} --@< Error: Cannot assign `{x: string, ...}` into `{x: integer, ...}`
                                    --@^ Note: The other type originates here
--! error

--8<-- funccall-table-args-1
--v function(opts: {mandatory: string, optional: string?})
function f(opts) end

f{}
--@^ Error: The type `function(opts: {mandatory: string, optional: string?}) --> ()` cannot be called
--@^^ Cause: First function argument `{...}` is not a subtype of `{mandatory: string, optional: string?}`
--@^^^ Note: The other type originates here
f{mandatory = nil}
f{mandatory = 42}
--@^ Error: The type `function(opts: {mandatory: string, optional: string?}) --> ()` cannot be called
--@^^ Cause: First function argument `{mandatory: 42, ...}` is not a subtype of `{mandatory: string, optional: string?}`
--@^^^ Note: The other type originates here
f{mandatory = 'foo'}
f{mandatory = 'foo', optional = nil}
f{mandatory = 'foo', optional = 'bar'}
f{mandatory = 'foo', optional = 54}
--@^ Error: The type `function(opts: {mandatory: string, optional: string?}) --> ()` cannot be called
--@^^ Cause: First function argument `{mandatory: "foo", optional: 54, ...}` is not a subtype of `{mandatory: string, optional: string?}`
--@^^^ Note: The other type originates here
-- XXX for those cases, additional `optional: string?` fields would be confusing
f{mandatory = 'foo', additional = nil}
--@^ Error: The type `function(opts: {mandatory: string, optional: string?}) --> ()` cannot be called
--@^^ Cause: First function argument `{additional: nil, mandatory: "foo", optional: string?, ...}` is not a subtype of `{mandatory: string, optional: string?}`
--@^^^ Note: The other type originates here
f{mandatory = 'foo', additional = true}
--@^ Error: The type `function(opts: {mandatory: string, optional: string?}) --> ()` cannot be called
--@^^ Cause: First function argument `{additional: true, mandatory: "foo", optional: string?, ...}` is not a subtype of `{mandatory: string, optional: string?}`
--@^^^ Note: The other type originates here
f{mandatory = 'foo', optional = 'bar', additional = true}
--@^ Error: The type `function(opts: {mandatory: string, optional: string?}) --> ()` cannot be called
--@^^ Cause: First function argument `{additional: true, mandatory: "foo", optional: "bar", ...}` is not a subtype of `{mandatory: string, optional: string?}`
--@^^^ Note: The other type originates here

--! error

--8<-- funccall-table-args-2
--v function(opts: {mandatory: string, optional: string?, ...})
function f(opts) end

f{}
--@^ Error: The type `function(opts: {mandatory: string, optional: string?, ...}) --> ()` cannot be called
--@^^ Cause: First function argument `{...}` is not a subtype of `{mandatory: string, optional: string?, ...}`
--@^^^ Note: The other type originates here
f{mandatory = nil}
f{mandatory = 42}
--@^ Error: The type `function(opts: {mandatory: string, optional: string?, ...}) --> ()` cannot be called
--@^^ Cause: First function argument `{mandatory: 42, ...}` is not a subtype of `{mandatory: string, optional: string?, ...}`
--@^^^ Note: The other type originates here
f{mandatory = 'foo'}
f{mandatory = 'foo', optional = nil}
f{mandatory = 'foo', optional = 'bar'}
f{mandatory = 'foo', optional = 54}
--@^ Error: The type `function(opts: {mandatory: string, optional: string?, ...}) --> ()` cannot be called
--@^^ Cause: First function argument `{mandatory: "foo", optional: 54, ...}` is not a subtype of `{mandatory: string, optional: string?, ...}`
--@^^^ Note: The other type originates here
f{mandatory = 'foo', additional = nil}
f{mandatory = 'foo', additional = true}
f{mandatory = 'foo', optional = 'bar', additional = true}

--! error

--8<-- rec-recursive
-- XXX we currently allow them, but it's probably going to be problematic later
local x = {}
x.x = x
x.y = x
x.z = x
--! ok

--8<-- duplicate-name-1
local x, x = 3, 'string' --: integer, string
--@^ Error: This variable will overwrite another same-named variable in the same scope
--@^^ Note: This variable is being overwritten
--@^^^ Error: Cannot redefine the type of a variable `x`
-- XXX in fact x will be 'string' at this point
x = 4
x = 'hello' --@< Error: Cannot assign `"hello"` into `integer`
            --@^ Note: The other type originates here
--! error

--8<-- duplicate-name-2
--v function(x: integer, x: string)
--@vv Error: This variable will overwrite another same-named variable in the same scope
--@v Note: This variable is being overwritten
function f(x, x)
    x = 4 --@< Error: Cannot assign `4` into `string`
          --@^ Note: The other type originates here
    x = 'hello'
end
f(3, 'string')
--! error

--8<-- legacy-arg
function f(...) --: string
    local v = arg.n --: integer
    local w = arg[42] --: string
    arg.n = 42
    arg[42] = 'string'
end
--! ok

--8<-- legacy-arg-subtype-1
function f(...) --: string
    local x = arg --: table

    local y = {} --: vector<string>
    arg = y

    local z = {n = 42} --: {n: integer}
    arg = z

    local w = {n = 42} --: {n: integer, ...}
    arg = w
end
--! ok

--8<-- legacy-arg-subtype-2
function f(...) --: string
    local w = {n = 42} --: {n: integer, ...}
    arg = w -- w is no longer extensible
    w.a = 54 --@< Error: Missing key "a" in `{n: integer}`
end
--! error

--8<-- legacy-arg-subtype-3
-- the type for `arg` is quite restricted, and while we have borrowed the notation
-- for intersection types it is really not.
function f(...) --: string
    local a = arg --: vector<string>
    --@^ Error: Cannot assign `vector<string> & {n: integer}` into `vector<string>`
    --@^^ Note: The other type originates here

    local b = arg --: map<integer, string>
    --@^ Error: Cannot assign `vector<string> & {n: integer}` into `map<integer, string>`
    --@^^ Note: The other type originates here

    local c = arg --: map<integer|"n", integer|string>
    --@^ Error: Cannot assign `vector<string> & {n: integer}` into `map<(integer|"n"), (integer|string)>`
    --@^^ Note: The other type originates here
end
--! error