luau-analyzer-sys 0.1.1

A high-performance, embedded Luau type-checking and analysis engine written in Rust. This crate provides bindings to the Luau analyzer, allowing you to integrate static analysis and code intelligence directly into your applications.
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
// This file is part of the Luau programming language and is licensed under MIT License; see LICENSE.txt for details
#include "Luau/TypeInfer.h"
#include "Luau/BuiltinDefinitions.h"
#include "Luau/Common.h"

#include "Fixture.h"

#include "ScopedFlags.h"
#include "doctest.h"

using namespace Luau;

LUAU_FASTFLAG(DebugLuauForceOldSolver)
LUAU_FASTFLAG(LuauExplicitTypeInstantiationSupport)
LUAU_FASTFLAG(LuauTableFreezeCheckIsSubtype)
LUAU_FASTFLAG(LuauSilenceDynamicFormatStringErrors)
LUAU_FASTFLAG(LuauRelateHandlesCoincidentTables)
LUAU_FASTFLAG(LuauOverloadGetsInstantiated2)

TEST_SUITE_BEGIN("BuiltinTests");

TEST_CASE_FIXTURE(BuiltinsFixture, "math_things_are_defined")
{
    CheckResult result = check(R"(
        local a00 = math.frexp
        local a01 = math.ldexp
        local a02 = math.fmod
        local a03 = math.modf
        local a04 = math.pow
        local a05 = math.exp
        local a06 = math.floor
        local a07 = math.abs
        local a08 = math.sqrt
        local a09 = math.log
        local a10 = math.log10
        local a11 = math.rad
        local a12 = math.deg
        local a13 = math.sin
        local a14 = math.cos
        local a15 = math.tan
        local a16 = math.sinh
        local a17 = math.cosh
        local a18 = math.tanh
        local a19 = math.atan
        local a20 = math.acos
        local a21 = math.asin
        local a22 = math.atan2
        local a23 = math.ceil
        local a24 = math.min
        local a25 = math.max
        local a26 = math.pi
        local a27 = math.huge
        local a28 = math.nan
        local a29 = math.e
        local a30 = math.phi
        local a31 = math.sqrt2
        local a32 = math.tau
        local a33 = math.randomseed
        local a34 = math.random
    )");

    LUAU_REQUIRE_NO_ERRORS(result);
}

TEST_CASE_FIXTURE(BuiltinsFixture, "next_iterator_should_infer_types_and_type_check")
{
    CheckResult result = check(R"(
        local a: string, b: number = next({ 1 })

        local s = "foo"
        local t = { [s] = 1 }
        local c: string?, d: number = next(t)
    )");

    LUAU_REQUIRE_ERROR_COUNT(1, result);
}

TEST_CASE_FIXTURE(BuiltinsFixture, "pairs_iterator_should_infer_types_and_type_check")
{
    CheckResult result = check(R"(
        type Map<K, V> = { [K]: V }
        local map: Map<string, number> = { ["foo"] = 1, ["bar"] = 2, ["baz"] = 3 }

        local it: (Map<string, number>, string | nil) -> (string?, number), t: Map<string, number>, i: nil = pairs(map)
    )");

    LUAU_REQUIRE_NO_ERRORS(result);
}

TEST_CASE_FIXTURE(BuiltinsFixture, "ipairs_iterator_should_infer_types_and_type_check")
{
    CheckResult result = check(R"(
        type Map<K, V> = { [K]: V }
        local array: Map<number, string> = { "foo", "bar", "baz" }

        local it: (Map<number, string>, number) -> (number?, string), t: Map<number, string>, i: number = ipairs(array)
    )");

    LUAU_REQUIRE_NO_ERRORS(result);
}

TEST_CASE_FIXTURE(BuiltinsFixture, "table_dot_remove_optionally_returns_generic")
{
    CheckResult result = check(R"(
        local t = { 1 }
        local n = table.remove(t, 7)
    )");

    LUAU_REQUIRE_NO_ERRORS(result);
    CHECK_EQ(toString(requireType("n")), "number?");
}

TEST_CASE_FIXTURE(BuiltinsFixture, "table_concat_returns_string")
{
    CheckResult result = check(R"(
        local r = table.concat({1,2,3,4}, ",", 2);
    )");

    LUAU_REQUIRE_NO_ERRORS(result);
    CHECK("string" == toString(requireType("r")));
}

TEST_CASE_FIXTURE(BuiltinsFixture, "sort")
{
    CheckResult result = check(R"(
        local t = {1, 2, 3};
        table.sort(t)
    )");

    LUAU_REQUIRE_NO_ERRORS(result);
}

TEST_CASE_FIXTURE(BuiltinsFixture, "sort_with_predicate")
{
    CheckResult result = check(R"(
        --!strict
        local t = {1, 2, 3}
        local function p(a: number, b: number) return a < b end
        table.sort(t, p)
    )");

    LUAU_REQUIRE_NO_ERRORS(result);
}

TEST_CASE_FIXTURE(BuiltinsFixture, "sort_with_bad_predicate")
{
    DOES_NOT_PASS_NEW_SOLVER_GUARD();

    CheckResult result = check(R"(
        --!strict
        local t = {'one', 'two', 'three'}
        local function p(a: number, b: number) return a < b end
        table.sort(t, p)
    )");

    LUAU_REQUIRE_ERROR_COUNT(1, result);
    const std::string expected = "Expected this to be\n\t"
                                 "'((string, string) -> boolean)?'"
                                 "\nbut got\n\t"
                                 "'(number, number) -> boolean'"
                                 "\ncaused by:\n"
                                 "  None of the union options are compatible. For example:\n"
                                 "Expected this to be\n\t"
                                 "'(string, string) -> boolean'"
                                 "\nbut got\n\t"
                                 "'(number, number) -> boolean'"
                                 "\ncaused by:\n"
                                 "  Argument #1 type is not compatible.\n"
                                 "Expected this to be 'number', but got 'string'";
    CHECK_EQ(expected, toString(result.errors[0]));
}

TEST_CASE_FIXTURE(Fixture, "strings_have_methods")
{
    CheckResult result = check(R"LUA(
        local s = ("RoactHostChangeEvent(%s)"):format("hello")
    )LUA");

    LUAU_REQUIRE_NO_ERRORS(result);
    CHECK("string" == toString(requireType("s")));
}

TEST_CASE_FIXTURE(BuiltinsFixture, "math_max_variatic")
{
    CheckResult result = check(R"(
        local n = math.max(1,2,3,4,5,6,7,8,9,0)
    )");

    LUAU_REQUIRE_NO_ERRORS(result);
    CHECK("number" == toString(requireType("n")));
}

TEST_CASE_FIXTURE(BuiltinsFixture, "math_max_checks_for_numbers")
{
    CheckResult result = check(R"(
        local n = math.max(1,2,"3")
    )");

    LUAU_REQUIRE_ERRORS(result);
    CHECK_EQ("Expected this to be 'number', but got 'string'", toString(result.errors[0]));
}

TEST_CASE_FIXTURE(BuiltinsFixture, "builtin_tables_sealed")
{
    CheckResult result = check(R"LUA(
        local b = bit32
    )LUA");
    TypeId bit32 = requireType("b");
    REQUIRE(bit32 != nullptr);
    const TableType* bit32t = get<TableType>(bit32);
    REQUIRE(bit32t != nullptr);
    CHECK_EQ(bit32t->state, TableState::Sealed);
}

TEST_CASE_FIXTURE(BuiltinsFixture, "lua_51_exported_globals_all_exist")
{
    // Extracted from lua5.1
    CheckResult result = check(R"(
        local v__G = _G
        local v_string_sub = string.sub
        local v_string_upper = string.upper
        local v_string_len = string.len
        local v_string_rep = string.rep
        local v_string_find = string.find
        local v_string_match = string.match
        local v_string_char = string.char
        local v_string_gmatch = string.gmatch
        local v_string_reverse = string.reverse
        local v_string_byte = string.byte
        local v_string_format = string.format
        local v_string_gsub = string.gsub
        local v_string_lower = string.lower

        local v_xpcall = xpcall

        --local v_package_loadlib = package.loadlib
        --local v_package_loaders_1_ = package.loaders[1]
        --local v_package_loaders_2_ = package.loaders[2]
        --local v_package_loaders_3_ = package.loaders[3]
        --local v_package_loaders_4_ = package.loaders[4]

        local v_tostring = tostring
        local v_print = print

        --local v_os_exit = os.exit
        --local v_os_setlocale = os.setlocale
        local v_os_date = os.date
        --local v_os_getenv = os.getenv
        local v_os_difftime = os.difftime
        --local v_os_remove = os.remove
        local v_os_time = os.time
        --local v_os_clock = os.clock
        --local v_os_tmpname = os.tmpname
        --local v_os_rename = os.rename
        --local v_os_execute = os.execute

        local v_unpack = unpack
        local v_require = require
        local v_getfenv = getfenv
        local v_setmetatable = setmetatable
        local v_next = next
        local v_assert = assert
        local v_tonumber = tonumber

        --local v_io_lines = io.lines
        --local v_io_write = io.write
        --local v_io_close = io.close
        --local v_io_flush = io.flush
        --local v_io_open = io.open
        --local v_io_output = io.output
        --local v_io_type = io.type
        --local v_io_read = io.read
        --local v_io_stderr = io.stderr
        --local v_io_stdin = io.stdin
        --local v_io_input = io.input
        --local v_io_stdout = io.stdout
        --local v_io_popen = io.popen
        --local v_io_tmpfile = io.tmpfile

        local v_rawequal = rawequal
        --local v_collectgarbage = collectgarbage
        local v_getmetatable = getmetatable
        local v_rawset = rawset

        local v_math_log = math.log
        local v_math_max = math.max
        local v_math_acos = math.acos
        local v_math_huge = math.huge
        local v_math_ldexp = math.ldexp
        local v_math_pi = math.pi
        local v_math_cos = math.cos
        local v_math_tanh = math.tanh
        local v_math_pow = math.pow
        local v_math_deg = math.deg
        local v_math_tan = math.tan
        local v_math_cosh = math.cosh
        local v_math_sinh = math.sinh
        local v_math_random = math.random
        local v_math_randomseed = math.randomseed
        local v_math_frexp = math.frexp
        local v_math_ceil = math.ceil
        local v_math_floor = math.floor
        local v_math_rad = math.rad
        local v_math_abs = math.abs
        local v_math_sqrt = math.sqrt
        local v_math_modf = math.modf
        local v_math_asin = math.asin
        local v_math_min = math.min
        --local v_math_mod = math.mod
        local v_math_fmod = math.fmod
        local v_math_log10 = math.log10
        local v_math_atan2 = math.atan2
        local v_math_exp = math.exp
        local v_math_sin = math.sin
        local v_math_atan = math.atan

        --local v_debug_getupvalue = debug.getupvalue
        --local v_debug_debug = debug.debug
        --local v_debug_sethook = debug.sethook
        --local v_debug_getmetatable = debug.getmetatable
        --local v_debug_gethook = debug.gethook
        --local v_debug_setmetatable = debug.setmetatable
        --local v_debug_setlocal = debug.setlocal
        --local v_debug_traceback = debug.traceback
        --local v_debug_setfenv = debug.setfenv
        --local v_debug_getinfo = debug.getinfo
        --local v_debug_setupvalue = debug.setupvalue
        --local v_debug_getlocal = debug.getlocal
        --local v_debug_getregistry = debug.getregistry
        --local v_debug_getfenv = debug.getfenv

        local v_pcall = pcall

        --local v_table_setn = table.setn
        local v_table_insert = table.insert
        --local v_table_getn = table.getn
        --local v_table_foreachi = table.foreachi
        local v_table_maxn = table.maxn
        --local v_table_foreach = table.foreach
        local v_table_concat = table.concat
        local v_table_sort = table.sort
        local v_table_remove = table.remove

        local v_newproxy = newproxy
        local v_type = type

        local v_coroutine_resume = coroutine.resume
        local v_coroutine_yield = coroutine.yield
        local v_coroutine_status = coroutine.status
        local v_coroutine_wrap = coroutine.wrap
        local v_coroutine_create = coroutine.create
        local v_coroutine_running = coroutine.running

        local v_select = select
        local v_gcinfo = gcinfo
        local v_pairs = pairs
        local v_rawget = rawget
        local v_loadstring = loadstring
        local v_ipairs = ipairs
        local v__VERSION = _VERSION
        --local v_dofile = dofile
        local v_setfenv = setfenv
        --local v_load = load
        local v_error = error
        --local v_loadfile = loadfile
    )");

    dumpErrors(result);
    LUAU_REQUIRE_NO_ERRORS(result);
}

TEST_CASE_FIXTURE(BuiltinsFixture, "setmetatable_unpacks_arg_types_correctly")
{
    CheckResult result = check(R"(
        setmetatable({}, setmetatable({}, {}))
    )");
    LUAU_REQUIRE_NO_ERRORS(result);
}

TEST_CASE_FIXTURE(BuiltinsFixture, "setmetatable_on_union_of_tables")
{
    CheckResult result = check(R"(
        type A = {tag: "A", x: number}
        type B = {tag: "B", y: string}

        type T = A | B

        type X = typeof(
            setmetatable({} :: T, {})
        )
    )");

    LUAU_REQUIRE_NO_ERRORS(result);
    if (!FFlag::DebugLuauForceOldSolver)
        CHECK("{ @metatable {  }, A } | { @metatable {  }, B }" == toString(requireTypeAlias("X")));
    else
        CHECK("{ @metatable {|  |}, A } | { @metatable {|  |}, B }" == toString(requireTypeAlias("X")));
}

TEST_CASE_FIXTURE(BuiltinsFixture, "table_insert_correctly_infers_type_of_array_2_args_overload")
{
    CheckResult result = check(R"(
        local t = {}
        table.insert(t, "foo")
        local s = t[1]
    )");

    LUAU_REQUIRE_NO_ERRORS(result);
    CHECK_EQ(getBuiltins()->stringType, requireType("s"));
}

TEST_CASE_FIXTURE(BuiltinsFixture, "table_insert_correctly_infers_type_of_array_3_args_overload")
{
    CheckResult result = check(R"(
        local t = {}
        table.insert(t, 1, "foo")
        local s = t[1]
    )");

    LUAU_REQUIRE_NO_ERRORS(result);
    CHECK_EQ("string", toString(requireType("s")));
}

TEST_CASE_FIXTURE(BuiltinsFixture, "table_pack")
{
    CheckResult result = check(R"(
        local t = table.pack(1, "foo", true)
    )");

    LUAU_REQUIRE_NO_ERRORS(result);
    CHECK_EQ("{ [number]: boolean | number | string, n: number }", toString(requireType("t")));
}

TEST_CASE_FIXTURE(BuiltinsFixture, "table_pack_variadic")
{
    CheckResult result = check(R"(
--!strict
function f(): (string, ...number)
    return "str", 2, 3, 4
end

local t = table.pack(f())
    )");

    LUAU_REQUIRE_NO_ERRORS(result);
    CHECK_EQ("{ [number]: number | string, n: number }", toString(requireType("t")));
}

TEST_CASE_FIXTURE(BuiltinsFixture, "table_pack_reduce_1")
{
    CheckResult result = check(R"(
        local t = table.pack(1, 2, true)
    )");

    LUAU_REQUIRE_NO_ERRORS(result);
    CHECK_EQ("{ [number]: boolean | number, n: number }", toString(requireType("t")));
}

TEST_CASE_FIXTURE(BuiltinsFixture, "table_pack_reduce_2")
{
    CheckResult result = check(R"(
        local t = table.pack("a", "b", "c")
    )");

    LUAU_REQUIRE_NO_ERRORS(result);
    auto ty = requireType("t");

    if (FFlag::LuauOverloadGetsInstantiated2 && !FFlag::DebugLuauForceOldSolver)
    {
        // FIXME: This is a result of us solving for `table.pack` before we
        // generalize its arguments. After we've solved it, we end up
        // with a type like:
        //
        //  { n: number, [number]: ("a" <: 'a <: string) | ("b" <: 'b <: string) | ("c" <: 'c <: string) }
        //
        // ... which we cannot reasonably know to simplify at this time.
        CHECK_EQ("{ [number]: string | string | string, n: number }", toString(ty));
    }
    else
    {
        CHECK_EQ("{ [number]: string, n: number }", toString(ty));
    }
}

TEST_CASE_FIXTURE(BuiltinsFixture, "gcinfo")
{
    CheckResult result = check(R"(
        local n = gcinfo()
    )");

    LUAU_REQUIRE_NO_ERRORS(result);
    CHECK("number" == toString(requireType("n")));
}

TEST_CASE_FIXTURE(BuiltinsFixture, "getfenv")
{
    LUAU_REQUIRE_NO_ERRORS(check("getfenv(1)"));
}

TEST_CASE_FIXTURE(BuiltinsFixture, "os_time_takes_optional_date_table")
{
    CheckResult result = check(R"(
        local n1 = os.time()
        local n2 = os.time({ year = 2020, month = 4, day = 20 })
        local n3 = os.time({ year = 2020, month = 4, day = 20, hour = 0, min = 0, sec = 0, isdst = true })
    )");

    LUAU_REQUIRE_NO_ERRORS(result);
    CHECK("number" == toString(requireType("n1")));
    CHECK("number" == toString(requireType("n2")));
    CHECK("number" == toString(requireType("n3")));
}

TEST_CASE_FIXTURE(BuiltinsFixture, "thread_is_a_type")
{
    CheckResult result = check(R"(
        local co = coroutine.create(function() end)
    )");

    LUAU_REQUIRE_NO_ERRORS(result);
    CHECK("thread" == toString(requireType("co")));
}

TEST_CASE_FIXTURE(BuiltinsFixture, "buffer_is_a_type")
{
    CheckResult result = check(R"(
        local b = buffer.create(10)
    )");

    LUAU_REQUIRE_NO_ERRORS(result);
    CHECK("buffer" == toString(requireType("b")));
}

TEST_CASE_FIXTURE(BuiltinsFixture, "coroutine_resume_anything_goes")
{
    DOES_NOT_PASS_NEW_SOLVER_GUARD();

    CheckResult result = check(R"(
        local function nifty(x, y)
            print(x, y)
            local z = coroutine.yield(1, 2)
            print(z)
            return 42
        end

        local co = coroutine.create(nifty)
        local x, y = coroutine.resume(co, 1, 2)
        local answer = coroutine.resume(co, 3)
    )");

    LUAU_REQUIRE_NO_ERRORS(result);
}

TEST_CASE_FIXTURE(BuiltinsFixture, "coroutine_wrap_anything_goes")
{
    CheckResult result = check(R"(
        --!nonstrict
        local function nifty(x, y)
            print(x, y)
            local z = coroutine.yield(1, 2)
            print(z)
            return 42
        end

        local f = coroutine.wrap(nifty)
        local x, y = f(1, 2)
        local answer = f(3)
    )");

    LUAU_REQUIRE_NO_ERRORS(result);
}

TEST_CASE_FIXTURE(BuiltinsFixture, "setmetatable_should_not_mutate_persisted_types")
{
    if (!FFlag::DebugLuauForceOldSolver)
        return;

    CheckResult result = check(R"(
        local string = string

        setmetatable(string, {})
    )");

    LUAU_REQUIRE_ERROR_COUNT(1, result);

    auto stringType = requireType("string");
    auto ttv = get<TableType>(stringType);
    REQUIRE(ttv);
}

TEST_CASE_FIXTURE(BuiltinsFixture, "string_format_arg_types_inference")
{
    CheckResult result = check(R"(
        --!strict
        function f(a, b, c)
            return string.format("%f %d %s", a, b, c)
        end
    )");

    LUAU_REQUIRE_NO_ERRORS(result);
    CHECK_EQ("(number, number, string) -> string", toString(requireType("f")));
}

TEST_CASE_FIXTURE(BuiltinsFixture, "string_format_arg_count_mismatch")
{
    CheckResult result = check(R"(
        --!strict
        string.format("%f %d %s")
        string.format("%s", "hi", 42)
        string.format("%s", "hi", 42, ...)
        string.format("%s", "hi", ...)
    )");

    LUAU_REQUIRE_ERROR_COUNT(3, result);
    CHECK_EQ(result.errors[0].location.begin.line, 2);
    CHECK_EQ(result.errors[1].location.begin.line, 3);
    CHECK_EQ(result.errors[2].location.begin.line, 4);
}

TEST_CASE_FIXTURE(BuiltinsFixture, "string_format_correctly_ordered_types")
{
    // CLI-115690
    if (!FFlag::DebugLuauForceOldSolver)
        return;

    CheckResult result = check(R"(
        --!strict
        string.format("%s", 123)
    )");

    LUAU_REQUIRE_ERROR_COUNT(1, result);
    TypeMismatch* tm = get<TypeMismatch>(result.errors[0]);
    REQUIRE(tm);
    CHECK_EQ(tm->wantedType, getBuiltins()->stringType);
    CHECK_EQ(tm->givenType, getBuiltins()->numberType);
}

TEST_CASE_FIXTURE(BuiltinsFixture, "string_format_tostring_specifier")
{
    CheckResult result = check(R"(
        --!strict
        string.format("%* %* %* %*", "string", 1, true, function() end)
    )");

    LUAU_REQUIRE_NO_ERRORS(result);
}

TEST_CASE_FIXTURE(BuiltinsFixture, "string_format_tostring_specifier_type_constraint")
{
    CheckResult result = check(R"(
        local function f(x): string
            local _ = string.format("%*", x)
            return x
        end
    )");

    LUAU_REQUIRE_NO_ERRORS(result);
    CHECK_EQ("(string) -> string", toString(requireType("f")));
}

TEST_CASE_FIXTURE(BuiltinsFixture, "xpcall")
{
    CheckResult result = check(R"(
        --!strict
        local a, b, c = xpcall(
            function() return 5, true end,
            function(e) return 0, false end
        )
    )");

    LUAU_REQUIRE_NO_ERRORS(result);
    CHECK_EQ("boolean", toString(requireType("a")));
    CHECK_EQ("number", toString(requireType("b")));
    CHECK_EQ("boolean", toString(requireType("c")));
}

TEST_CASE_FIXTURE(BuiltinsFixture, "pcall_returns_at_least_two_value_but_function_returns_nothing")
{
    // We have no plans to fix this in the old solver.
    if (FFlag::DebugLuauForceOldSolver)
        return;

    CheckResult result = check(R"(
        local function f(): () end
        local ok, res = pcall(f)
    )");

    LUAU_REQUIRE_NO_ERRORS(result);
    CHECK_EQ("boolean", toString(requireType("ok")));
    CHECK_EQ("unknown", toString(requireType("res")));
}

TEST_CASE_FIXTURE(BuiltinsFixture, "trivial_select")
{
    CheckResult result = check(R"(
        local a:number = select(1, 42)
    )");

    LUAU_REQUIRE_NO_ERRORS(result);
}

TEST_CASE_FIXTURE(BuiltinsFixture, "see_thru_select")
{
    CheckResult result = check(R"(
        local a:number, b:boolean = select(2,"hi", 10, true)
    )");

    LUAU_REQUIRE_NO_ERRORS(result);
}

TEST_CASE_FIXTURE(BuiltinsFixture, "see_thru_select_count")
{
    CheckResult result = check(R"(
        local a = select("#","hi", 10, true)
    )");

    dumpErrors(result);
    LUAU_REQUIRE_NO_ERRORS(result);
}

TEST_CASE_FIXTURE(BuiltinsFixture, "select_with_decimal_argument_is_rounded_down")
{
    CheckResult result = check(R"(
        local a: number, b: boolean = select(2.9, "foo", 1, true)
    )");

    LUAU_REQUIRE_NO_ERRORS(result);
}

// Could be flaky if the fix has regressed.
TEST_CASE_FIXTURE(BuiltinsFixture, "bad_select_should_not_crash")
{
    CheckResult result = check(R"(
        do end
        local _ = function(l0,...)
        end
        local _ = function()
            _(_);
            _ += select(_())
        end
    )");

    if (!FFlag::DebugLuauForceOldSolver)
    {
        // Note, the function "_" places no constraints on its arguments.  They
        // can therefore be nil.  They are therefore optional.  Only the
        // select() call is invalid here.
        LUAU_REQUIRE_ERROR_COUNT(1, result);

        CHECK(Location{{6, 17}, {6, 23}} == result.errors.at(0).location);
        const CountMismatch* err = get<CountMismatch>(result.errors.at(0));
        REQUIRE(err);
        CHECK(1 == err->expected);

        // "_" returns 0 values.
        CHECK(0 == err->actual);
    }
    else if (!FFlag::DebugLuauForceOldSolver)
    {
        LUAU_REQUIRE_ERROR_COUNT(2, result);
        CHECK_EQ("Argument count mismatch. Function expects at least 1 argument, but none are specified", toString(result.errors[0]));
        CHECK_EQ("Argument count mismatch. Function expects at least 1 argument, but none are specified", toString(result.errors[1]));
    }
    else
    {
        LUAU_REQUIRE_ERROR_COUNT(2, result);
        CHECK_EQ("Argument count mismatch. Function '_' expects at least 1 argument, but none are specified", toString(result.errors[0]));
        CHECK_EQ("Argument count mismatch. Function 'select' expects 1 argument, but none are specified", toString(result.errors[1]));
    }
}

TEST_CASE_FIXTURE(BuiltinsFixture, "select_way_out_of_range")
{
    // CLI-115720
    if (!FFlag::DebugLuauForceOldSolver)
        return;

    CheckResult result = check(R"(
        select(5432598430953240958)
    )");

    LUAU_REQUIRE_ERROR_COUNT(1, result);
    REQUIRE(get<GenericError>(result.errors[0]));
    CHECK_EQ("bad argument #1 to select (index out of range)", toString(result.errors[0]));
}

TEST_CASE_FIXTURE(BuiltinsFixture, "select_slightly_out_of_range")
{
    // CLI-115720
    if (!FFlag::DebugLuauForceOldSolver)
        return;

    CheckResult result = check(R"(
        select(3, "a", 1)
    )");

    LUAU_REQUIRE_ERROR_COUNT(1, result);
    REQUIRE(get<GenericError>(result.errors[0]));
    CHECK_EQ("bad argument #1 to select (index out of range)", toString(result.errors[0]));
}

TEST_CASE_FIXTURE(BuiltinsFixture, "select_with_variadic_typepack_tail")
{
    CheckResult result = check(R"(
        --!nonstrict
        local function f(...)
            return ...
        end

        local foo, bar, baz, quux = select(1, f("foo", true))
    )");

    LUAU_REQUIRE_NO_ERRORS(result);

    CHECK_EQ("any", toString(requireType("foo")));
    CHECK_EQ("any", toString(requireType("bar")));
    CHECK_EQ("any", toString(requireType("baz")));
    CHECK_EQ("any", toString(requireType("quux")));
}

TEST_CASE_FIXTURE(BuiltinsFixture, "select_with_variadic_typepack_tail_and_string_head")
{
    // CLI-115720
    if (!FFlag::DebugLuauForceOldSolver)
        return;

    CheckResult result = check(R"(
        --!nonstrict
        local function f(...)
            return ...
        end

        local foo, bar, baz, quux = select(1, "foo", f("bar", true))
    )");

    LUAU_REQUIRE_NO_ERRORS(result);

    CHECK_EQ("any", toString(requireType("foo")));
    CHECK_EQ("any", toString(requireType("bar")));
    CHECK_EQ("any", toString(requireType("baz")));
    CHECK_EQ("any", toString(requireType("quux")));
}

TEST_CASE_FIXTURE(Fixture, "string_format_as_method")
{
    CheckResult result = check("local _ = ('%s'):format(5)");

    LUAU_REQUIRE_ERROR_COUNT(1, result);

    TypeMismatch* tm = get<TypeMismatch>(result.errors[0]);
    REQUIRE(tm);
    CHECK_EQ(tm->wantedType, getBuiltins()->stringType);
    CHECK_EQ(tm->givenType, getBuiltins()->numberType);
}

TEST_CASE_FIXTURE(BuiltinsFixture, "string_format_trivial_arity")
{
    CheckResult result = check(R"(
        string.format()
    )");

    LUAU_REQUIRE_ERROR_COUNT(1, result);

    CHECK_EQ("Argument count mismatch. Function 'string.format' expects at least 1 argument, but none are specified", toString(result.errors[0]));
}

TEST_CASE_FIXTURE(Fixture, "string_format_use_correct_argument")
{
    CheckResult result = check(R"(
        local _ = ("%s"):format("%d", "hello")
    )");

    LUAU_REQUIRE_ERROR_COUNT(1, result);

    CHECK_EQ("Argument count mismatch. Function expects 2 arguments, but 3 are specified", toString(result.errors[0]));
}

TEST_CASE_FIXTURE(Fixture, "string_format_use_correct_argument2")
{
    CheckResult result = check(R"(
        local _ = ("%s %d").format("%d %s", "A type error", 2)
    )");

    LUAU_REQUIRE_ERROR_COUNT(2, result);

    CHECK_EQ("Expected this to be 'number', but got 'string'", toString(result.errors[0]));
    CHECK_EQ("Expected this to be 'string', but got 'number'", toString(result.errors[1]));
}

TEST_CASE_FIXTURE(BuiltinsFixture, "string_format_use_correct_argument3")
{
    CheckResult result = check(R"(
        local s1 = string.format("%d")
        local s2 = string.format("%d", 1)
        local s3 = string.format("%d", 1, 2)
    )");

    LUAU_REQUIRE_ERROR_COUNT(2, result);

    CHECK_EQ("Argument count mismatch. Function expects 2 arguments, but only 1 is specified", toString(result.errors[0]));
    CHECK_EQ("Argument count mismatch. Function expects 2 arguments, but 3 are specified", toString(result.errors[1]));
}

TEST_CASE_FIXTURE(BuiltinsFixture, "debug_traceback_is_crazy")
{
    CheckResult result = check(R"(
        function f(co: thread)
            -- debug.traceback takes thread?, message?, level? - yes, all optional!
            debug.traceback()
            debug.traceback(nil, 1)
            debug.traceback("msg")
            debug.traceback("msg", 1)
            debug.traceback(co)
            debug.traceback(co, "msg")
            debug.traceback(co, "msg", 1)
        end
    )");

    LUAU_REQUIRE_NO_ERRORS(result);
}

TEST_CASE_FIXTURE(BuiltinsFixture, "debug_info_is_crazy")
{
    CheckResult result = check(R"(
        function f(co: thread, f: () -> ())
            -- debug.info takes thread?, level, options or function, options
            debug.info(1, "n")
            debug.info(co, 1, "n")
            debug.info(f, "n")
        end
    )");

    LUAU_REQUIRE_NO_ERRORS(result);
}

TEST_CASE_FIXTURE(BuiltinsFixture, "aliased_string_format")
{
    CheckResult result = check(R"(
        local fmt = string.format
        local s = fmt("%d", "oops")
    )");

    LUAU_REQUIRE_ERROR_COUNT(1, result);
    CHECK_EQ("Expected this to be 'number', but got 'string'", toString(result.errors[0]));
}

TEST_CASE_FIXTURE(BuiltinsFixture, "string_lib_self_noself")
{
    CheckResult result = check(R"(
        --!nonstrict
        local a1 = string.byte("abcdef", 2)
        local a2 = string.find("abcdef", "def")
        local a3 = string.gmatch("ab ab", "%a+")
        local a4 = string.gsub("abab", "ab", "cd")
        local a5 = string.len("abc")
        local a6 = string.match("12 ab", "%d+ %a+")
        local a7 = string.rep("a", 10)
        local a8 = string.sub("abcd", 1, 2)
        local a9 = string.split("a,b,c", ",")
        local a0 = string.packsize("ff")
    )");

    LUAU_REQUIRE_NO_ERRORS(result);
}

TEST_CASE_FIXTURE(BuiltinsFixture, "gmatch_definition")
{
    CheckResult result = check(R"_(
        local a, b, c = ("hey"):gmatch("(.)(.)(.)")()

        for c in ("hey"):gmatch("(.)") do
            print(c:upper())
        end
    )_");

    LUAU_REQUIRE_NO_ERRORS(result);
}

TEST_CASE_FIXTURE(BuiltinsFixture, "select_on_variadic")
{
    CheckResult result = check(R"(
        local function f(): (number, ...(boolean | number))
            return 100, true, 1
        end

        local a, b, c = select(f())
    )");

    LUAU_REQUIRE_NO_ERRORS(result);
    CHECK_EQ("any", toString(requireType("a")));
    CHECK_EQ("any", toString(requireType("b")));
    CHECK_EQ("any", toString(requireType("c")));
}

TEST_CASE_FIXTURE(BuiltinsFixture, "string_format_report_all_type_errors_at_correct_positions")
{
    CheckResult result = check(R"(
        ("%s%d%s"):format(1, "hello", true)
        string.format("%s%d%s", 1, "hello", true)
    )");

    TypeId stringType = getBuiltins()->stringType;
    TypeId numberType = getBuiltins()->numberType;
    TypeId booleanType = getBuiltins()->booleanType;

    LUAU_REQUIRE_ERROR_COUNT(6, result);

    CHECK_EQ(Location(Position{1, 26}, Position{1, 27}), result.errors[0].location);
    CHECK_EQ(TypeErrorData(TypeMismatch{stringType, numberType}), result.errors[0].data);

    CHECK_EQ(Location(Position{1, 29}, Position{1, 36}), result.errors[1].location);
    CHECK_EQ(TypeErrorData(TypeMismatch{numberType, stringType}), result.errors[1].data);

    CHECK_EQ(Location(Position{1, 38}, Position{1, 42}), result.errors[2].location);
    CHECK_EQ(TypeErrorData(TypeMismatch{stringType, booleanType}), result.errors[2].data);

    CHECK_EQ(Location(Position{2, 32}, Position{2, 33}), result.errors[3].location);
    CHECK_EQ(TypeErrorData(TypeMismatch{stringType, numberType}), result.errors[3].data);

    CHECK_EQ(Location(Position{2, 35}, Position{2, 42}), result.errors[4].location);
    CHECK_EQ(TypeErrorData(TypeMismatch{numberType, stringType}), result.errors[4].data);

    CHECK_EQ(Location(Position{2, 44}, Position{2, 48}), result.errors[5].location);
    CHECK_EQ(TypeErrorData(TypeMismatch{stringType, booleanType}), result.errors[5].data);
}

TEST_CASE_FIXTURE(BuiltinsFixture, "tonumber_returns_optional_number_type")
{
    CheckResult result = check(R"(
        --!strict
        local b: number = tonumber('asdf')
    )");

    LUAU_REQUIRE_ERROR_COUNT(1, result);

    if (!FFlag::DebugLuauForceOldSolver)
    {
        CHECK_EQ(
            "Expected this to be 'number', but got 'number?'; \n"
            "the 2nd component of the union is `nil`, which is not a subtype of `number`",
            toString(result.errors[0])
        );
    }
    else
    {
        CHECK_EQ("Expected this to be 'number', but got 'number?'", toString(result.errors[0]));
    }
}

TEST_CASE_FIXTURE(BuiltinsFixture, "tonumber_returns_optional_number_type2")
{
    CheckResult result = check(R"(
        --!strict
        local b: number = tonumber('asdf') or 1
    )");

    LUAU_REQUIRE_NO_ERRORS(result);
}

TEST_CASE_FIXTURE(BuiltinsFixture, "dont_add_definitions_to_persistent_types")
{
    // This test makes no sense with type states and I think it generally makes no sense under the new solver.
    // TODO: clip.
    if (!FFlag::DebugLuauForceOldSolver)
        return;

    CheckResult result = check(R"(
        local f = math.sin
        local function g(x) return math.sin(x) end
        f = g
    )");
    LUAU_REQUIRE_NO_ERRORS(result);

    TypeId fType = requireType("f");
    const FunctionType* ftv = get<FunctionType>(fType);
    REQUIRE(fType);
    REQUIRE(fType->persistent);
    REQUIRE(!ftv->definition);

    TypeId gType = requireType("g");
    const FunctionType* gtv = get<FunctionType>(gType);
    REQUIRE(gType);
    REQUIRE(!gType->persistent);
    REQUIRE(gtv->definition);
}

TEST_CASE_FIXTURE(BuiltinsFixture, "assert_removes_falsy_types")
{
    CheckResult result = check(R"(
        local function f(x: (number | boolean)?)
            return assert(x)
        end
    )");

    LUAU_REQUIRE_NO_ERRORS(result);

    if (!FFlag::DebugLuauForceOldSolver)
        CHECK_EQ("((boolean | number)?) -> number | true", toString(requireType("f")));
    else
        CHECK_EQ("((boolean | number)?) -> boolean | number", toString(requireType("f")));
}

TEST_CASE_FIXTURE(BuiltinsFixture, "assert_removes_falsy_types2")
{
    CheckResult result = check(R"(
        local function f(x: (number | boolean)?): number | true
            return assert(x)
        end
    )");

    LUAU_REQUIRE_NO_ERRORS(result);
    CHECK_EQ("((boolean | number)?) -> number | true", toString(requireType("f")));
}

TEST_CASE_FIXTURE(BuiltinsFixture, "assert_removes_falsy_types3")
{
    CheckResult result = check(R"(
        local function f(x: (number | boolean)?)
            assert(x)
            return x
        end
    )");

    LUAU_REQUIRE_NO_ERRORS(result);
    if (!FFlag::DebugLuauForceOldSolver)
        CHECK_EQ("((boolean | number)?) -> number | true", toString(requireType("f")));
    else // without the annotation, the old solver doesn't infer the best return type here
        CHECK_EQ("((boolean | number)?) -> boolean | number", toString(requireType("f")));
}

TEST_CASE_FIXTURE(BuiltinsFixture, "assert_removes_falsy_types_even_from_type_pack_tail_but_only_for_the_first_type")
{
    if (!FFlag::DebugLuauForceOldSolver)
        return;

    CheckResult result = check(R"(
        local function f(...: number?)
            return assert(...)
        end
    )");

    LUAU_REQUIRE_NO_ERRORS(result);
    CHECK_EQ("(...number?) -> (number, ...number?)", toString(requireType("f")));
}

TEST_CASE_FIXTURE(BuiltinsFixture, "assert_returns_false_and_string_iff_it_knows_the_first_argument_cannot_be_truthy")
{
    if (!FFlag::DebugLuauForceOldSolver)
    {
        // CLI-114134 - egraph simplification
        return;
    }

    CheckResult result = check(R"(
        local function f(x: nil)
            return assert(x, "hmm")
        end
    )");

    LUAU_REQUIRE_NO_ERRORS(result);
    CHECK_EQ("(nil) -> (never, ...never)", toString(requireType("f")));
}

TEST_CASE_FIXTURE(BuiltinsFixture, "table_freeze_is_generic")
{
    CheckResult result = check(R"(
        local t1: {a: number} = {a = 42}
        local t2: {b: string} = {b = "hello"}
        local t3: {boolean} = {false, true}

        local tf1 = table.freeze(t1)
        local tf2 = table.freeze(t2)
        local tf3 = table.freeze(t3)

        local a = tf1.a
        local b = tf2.b
        local c = tf3[2]

        local d = tf1.b

        local a2 = t1.a
        local b2 = t2.b
        local c2 = t3[2]
    )");

    LUAU_REQUIRE_ERROR_COUNT(1, result);
    if (!FFlag::DebugLuauForceOldSolver)
        CHECK("Key 'b' not found in table '{ read a: number }'" == toString(result.errors[0]));
    else
        CHECK_EQ("Key 'b' not found in table '{ a: number }'", toString(result.errors[0]));
    CHECK(Location({13, 18}, {13, 23}) == result.errors[0].location);

    if (!FFlag::DebugLuauForceOldSolver)
    {
        CHECK_EQ("{ read a: number }", toString(requireTypeAtPosition({15, 19})));
        CHECK_EQ("{ read b: string }", toString(requireTypeAtPosition({16, 19})));
        CHECK_EQ("{boolean}", toString(requireTypeAtPosition({17, 19})));
    }

    CHECK_EQ("number", toString(requireType("a")));
    CHECK_EQ("string", toString(requireType("b")));
    CHECK_EQ("boolean", toString(requireType("c")));

    if (!FFlag::DebugLuauForceOldSolver)
        CHECK_EQ("any", toString(requireType("d")));
    else
        CHECK_EQ("*error-type*", toString(requireType("d")));

    CHECK_EQ("number", toString(requireType("a2")));
    CHECK_EQ("string", toString(requireType("b2")));
    CHECK_EQ("boolean", toString(requireType("c2")));
}

TEST_CASE_FIXTURE(BuiltinsFixture, "table_freeze_does_not_retroactively_block_mutation")
{
    ScopedFastFlag _{FFlag::LuauRelateHandlesCoincidentTables, true};

    CheckResult result = check(R"(
        local t1 = {a = 42}

        t1.q = ":3"

        local tf1 = table.freeze(t1)

        local a = tf1.a
        local b = t1.a
    )");

    LUAU_REQUIRE_NO_ERRORS(result);

    if (!FFlag::DebugLuauForceOldSolver)
    {
        CHECK_EQ("{ read a: number, read q: string }", toString(requireType("t1"), {/*exhaustive */ true}));
        // before the assignment, it's `t1`
        CHECK_EQ("{ a: number, q: string }", toString(requireTypeAtPosition({3, 8}), {/*exhaustive */ true}));
        // after the assignment, it's read-only.
        CHECK_EQ("{ read a: number, read q: string }", toString(requireTypeAtPosition({8, 18}), {/*exhaustive */ true}));
    }

    CHECK_EQ("number", toString(requireType("a")));
    CHECK_EQ("number", toString(requireType("b")));
}

TEST_CASE_FIXTURE(BuiltinsFixture, "table_freeze_no_generic_table")
{
    ScopedFastFlag sff{FFlag::DebugLuauForceOldSolver, false};

    CheckResult result = check(R"(
        --!strict
        type k = {
            read k: string,
        }

        function _(): k
            return table.freeze({
                k = "",
            })
        end
    )");

    LUAU_REQUIRE_NO_ERRORS(result);
}

TEST_CASE_FIXTURE(BuiltinsFixture, "table_freeze_on_metatable")
{
    CheckResult result = check(R"(
        --!strict
        local meta = {
            __index = function()
                return "foo"
            end
        }

        local myTable = setmetatable({}, meta)
        table.freeze(myTable)
    )");

    LUAU_REQUIRE_NO_ERRORS(result);
}

TEST_CASE_FIXTURE(BuiltinsFixture, "table_freeze_errors_on_no_args")
{
    CheckResult result = check(R"(
        --!strict
        table.freeze()
    )");

    LUAU_REQUIRE_ERROR_COUNT(1, result);

    CHECK(get<CountMismatch>(result.errors[0]));
}

TEST_CASE_FIXTURE(BuiltinsFixture, "table_freeze_errors_on_non_tables")
{
    CheckResult result = check(R"(
        --!strict
        table.freeze(42)
    )");

    LUAU_REQUIRE_ERROR_COUNT(1, result);

    TypeMismatch* tm = get<TypeMismatch>(result.errors[0]);
    REQUIRE(tm);

    if (!FFlag::DebugLuauForceOldSolver)
        CHECK_EQ(toString(tm->wantedType), "table");
    else
        CHECK_EQ(toString(tm->wantedType), "{-  -}");
    CHECK_EQ(toString(tm->givenType), "number");
}

TEST_CASE_FIXTURE(BuiltinsFixture, "table_freeze_persistent_skip")
{
    CheckResult result = check(R"(
        table.freeze(table)
    )");

    LUAU_REQUIRE_NO_ERRORS(result);
}

TEST_CASE_FIXTURE(BuiltinsFixture, "table_clone_persistent_skip")
{
    CheckResult result = check(R"(
        table.clone(table)
    )");

    LUAU_REQUIRE_NO_ERRORS(result);
}

TEST_CASE_FIXTURE(BuiltinsFixture, "table_clone_should_support_variadic_any_in_old_solver")
{
    fileResolver.source["game/A"] = R"(
        --!nonstrict
        return function()
            return {}
        end
    )";

    fileResolver.source["game/B"] = R"(
        local A = require(game.A)
        local _ = table.clone(A())
    )";

    CheckResult result = getFrontend().check("game/B");
    LUAU_REQUIRE_ERROR_COUNT(0, result);
}

TEST_CASE_FIXTURE(BuiltinsFixture, "set_metatable_needs_arguments")
{
    // In the new solver, nil can certainly be used where a generic is required, so all generic parameters are optional.
    DOES_NOT_PASS_NEW_SOLVER_GUARD();

    CheckResult result = check(R"(
        local a = {b=setmetatable}
        a.b()
        a:b()
        a:b({})
    )");
    LUAU_REQUIRE_ERROR_COUNT(2, result);
    CHECK_EQ(toString(result.errors[0]), "Argument count mismatch. Function 'a.b' expects 2 arguments, but none are specified");
    CHECK_EQ(toString(result.errors[1]), "Argument count mismatch. Function 'a.b' expects 2 arguments, but only 1 is specified");
}

TEST_CASE_FIXTURE(BuiltinsFixture, "table_clone_intersection_of_tables")
{
    CheckResult result = check(R"(
        type FIRST = {
            some: string,
        }

        type SECOND = FIRST & {
            thing: string,
        }

        local b: SECOND
        -- c's type used to be FIRST, but should be the full type of SECOND
        local c = table.clone(b)
    )");

    LUAU_REQUIRE_NO_ERRORS(result);

    CHECK_EQ("{ some: string } & { thing: string }", toString(requireType("c"), {true}));
    CHECK_EQ("FIRST & { thing: string }", toString(requireType("c")));
}

TEST_CASE_FIXTURE(Fixture, "typeof_unresolved_function")
{
    CheckResult result = check(R"(
        local function f(a: typeof(f)) end
        )");
    LUAU_REQUIRE_ERROR_COUNT(1, result);
    CHECK_EQ("Unknown global 'f'; consider assigning to it first", toString(result.errors[0]));
}

TEST_CASE_FIXTURE(BuiltinsFixture, "no_persistent_typelevel_change")
{
    TypeId mathTy = requireType(getFrontend().globals.globalScope, "math");
    REQUIRE(mathTy);
    TableType* ttv = getMutable<TableType>(mathTy);
    REQUIRE(ttv);

    REQUIRE(ttv->props["frexp"].readTy);
    const FunctionType* ftv = get<FunctionType>(*ttv->props["frexp"].readTy);

    REQUIRE(ftv);
    auto original = ftv->level;

    CheckResult result = check("local a = math.frexp");

    LUAU_REQUIRE_NO_ERRORS(result);
    CHECK(ftv->level.level == original.level);
    CHECK(ftv->level.subLevel == original.subLevel);
}

TEST_CASE_FIXTURE(BuiltinsFixture, "global_singleton_types_are_sealed")
{
    CheckResult result = check(R"(
local function f(x: string)
    local p = x:split('a')
    p = table.pack(table.unpack(p, 1, #p - 1))
    return p
end
    )");

    LUAU_REQUIRE_NO_ERRORS(result);
}

TEST_CASE_FIXTURE(Fixture, "string_match")
{
    CheckResult result = check(R"(
        local s: string = "hello"
        local p = s:match("foo")
    )");

    LUAU_REQUIRE_NO_ERRORS(result);
    CHECK_EQ(toString(requireType("p")), "string?");
}

TEST_CASE_FIXTURE(BuiltinsFixture, "gmatch_capture_types")
{
    CheckResult result = check(R"END(
        local a, b, c = string.gmatch("This is a string", "(.()(%a+))")()
    )END");

    LUAU_REQUIRE_NO_ERRORS(result);

    CHECK_EQ(toString(requireType("a")), "string?");
    CHECK_EQ(toString(requireType("b")), "number?");
    CHECK_EQ(toString(requireType("c")), "string?");
}

TEST_CASE_FIXTURE(Fixture, "gmatch_capture_types2")
{
    CheckResult result = check(R"END(
        local a, b, c = ("This is a string"):gmatch("(.()(%a+))")()
    )END");

    LUAU_REQUIRE_NO_ERRORS(result);

    CHECK_EQ(toString(requireType("a")), "string?");
    CHECK_EQ(toString(requireType("b")), "number?");
    CHECK_EQ(toString(requireType("c")), "string?");
}

TEST_CASE_FIXTURE(BuiltinsFixture, "gmatch_capture_types_default_capture")
{
    CheckResult result = check(R"END(
        local a, b, c, d = string.gmatch("T(his)() is a string", ".")()
    )END");

    LUAU_REQUIRE_ERROR_COUNT(1, result);

    CountMismatch* acm = get<CountMismatch>(result.errors[0]);
    REQUIRE(acm);
    CHECK_EQ(acm->context, CountMismatch::FunctionResult);
    CHECK_EQ(acm->expected, 1);
    CHECK_EQ(acm->actual, 4);

    CHECK_EQ(toString(requireType("a")), "string?");
}

TEST_CASE_FIXTURE(BuiltinsFixture, "gmatch_capture_types_balanced_escaped_parens")
{
    CheckResult result = check(R"END(
        local a, b, c, d = string.gmatch("T(his) is a string", "((.)%b()())")()
    )END");

    LUAU_REQUIRE_ERROR_COUNT(1, result);

    CountMismatch* acm = get<CountMismatch>(result.errors[0]);
    REQUIRE(acm);
    CHECK_EQ(acm->context, CountMismatch::FunctionResult);
    CHECK_EQ(acm->expected, 3);
    CHECK_EQ(acm->actual, 4);

    CHECK_EQ(toString(requireType("a")), "string?");
    CHECK_EQ(toString(requireType("b")), "string?");
    CHECK_EQ(toString(requireType("c")), "number?");
}

TEST_CASE_FIXTURE(BuiltinsFixture, "gmatch_capture_types_parens_in_sets_are_ignored")
{
    CheckResult result = check(R"END(
        local a, b, c = string.gmatch("T(his)() is a string", "(T[()])()")()
    )END");

    LUAU_REQUIRE_ERROR_COUNT(1, result);

    CountMismatch* acm = get<CountMismatch>(result.errors[0]);
    REQUIRE(acm);
    CHECK_EQ(acm->context, CountMismatch::FunctionResult);
    CHECK_EQ(acm->expected, 2);
    CHECK_EQ(acm->actual, 3);

    CHECK_EQ(toString(requireType("a")), "string?");
    CHECK_EQ(toString(requireType("b")), "number?");
}

TEST_CASE_FIXTURE(BuiltinsFixture, "gmatch_capture_types_set_containing_lbracket")
{
    CheckResult result = check(R"END(
        local a, b = string.gmatch("[[[", "()([[])")()
    )END");

    LUAU_REQUIRE_NO_ERRORS(result);

    CHECK_EQ(toString(requireType("a")), "number?");
    CHECK_EQ(toString(requireType("b")), "string?");
}

TEST_CASE_FIXTURE(BuiltinsFixture, "gmatch_capture_types_leading_end_bracket_is_part_of_set")
{
    CheckResult result = check(R"END(
        -- An immediate right-bracket following a left-bracket is included within the set;
        -- thus, '[]]'' is the set containing ']', and '[]' is an invalid set missing an enclosing
        -- right-bracket. We detect an invalid set in this case and fall back to to default gmatch
        -- typing.
        local foo = string.gmatch("T[hi%]s]]]() is a string", "([]s)")
    )END");

    LUAU_REQUIRE_NO_ERRORS(result);

    CHECK_EQ(toString(requireType("foo")), "() -> (...string)");
}

TEST_CASE_FIXTURE(BuiltinsFixture, "gmatch_capture_types_invalid_pattern_fallback_to_builtin")
{
    CheckResult result = check(R"END(
        local foo = string.gmatch("T(his)() is a string", ")")
    )END");

    LUAU_REQUIRE_NO_ERRORS(result);

    CHECK_EQ(toString(requireType("foo")), "() -> (...string)");
}

TEST_CASE_FIXTURE(BuiltinsFixture, "gmatch_capture_types_invalid_pattern_fallback_to_builtin2")
{
    CheckResult result = check(R"END(
        local foo = string.gmatch("T(his)() is a string", "[")
    )END");

    LUAU_REQUIRE_NO_ERRORS(result);

    CHECK_EQ(toString(requireType("foo")), "() -> (...string)");
}

TEST_CASE_FIXTURE(BuiltinsFixture, "match_capture_types")
{
    CheckResult result = check(R"END(
        local a, b, c = string.match("This is a string", "(.()(%a+))")
    )END");

    LUAU_REQUIRE_NO_ERRORS(result);

    CHECK_EQ(toString(requireType("a")), "string?");
    CHECK_EQ(toString(requireType("b")), "number?");
    CHECK_EQ(toString(requireType("c")), "string?");
}

TEST_CASE_FIXTURE(BuiltinsFixture, "match_capture_types2")
{
    CheckResult result = check(R"END(
        local a, b, c = string.match("This is a string", "(.()(%a+))", "this should be a number")
    )END");

    LUAU_REQUIRE_ERROR_COUNT(1, result);

    TypeMismatch* tm = get<TypeMismatch>(result.errors[0]);
    REQUIRE(tm);
    CHECK_EQ(toString(tm->wantedType), "number?");
    CHECK_EQ(toString(tm->givenType), "string");

    CHECK_EQ(toString(requireType("a")), "string?");
    CHECK_EQ(toString(requireType("b")), "number?");
    CHECK_EQ(toString(requireType("c")), "string?");
}

TEST_CASE_FIXTURE(BuiltinsFixture, "find_capture_types")
{
    CheckResult result = check(R"END(
        local d, e, a, b, c = string.find("This is a string", "(.()(%a+))")
    )END");

    LUAU_REQUIRE_NO_ERRORS(result);

    CHECK_EQ(toString(requireType("a")), "string?");
    CHECK_EQ(toString(requireType("b")), "number?");
    CHECK_EQ(toString(requireType("c")), "string?");
    CHECK_EQ(toString(requireType("d")), "number?");
    CHECK_EQ(toString(requireType("e")), "number?");
}

TEST_CASE_FIXTURE(BuiltinsFixture, "find_capture_types2")
{
    CheckResult result = check(R"END(
        local d, e, a, b, c = string.find("This is a string", "(.()(%a+))", "this should be a number")
    )END");

    LUAU_REQUIRE_ERROR_COUNT(1, result);

    TypeMismatch* tm = get<TypeMismatch>(result.errors[0]);
    REQUIRE(tm);
    CHECK_EQ(toString(tm->wantedType), "number?");
    CHECK_EQ(toString(tm->givenType), "string");

    CHECK_EQ(toString(requireType("a")), "string?");
    CHECK_EQ(toString(requireType("b")), "number?");
    CHECK_EQ(toString(requireType("c")), "string?");
    CHECK_EQ(toString(requireType("d")), "number?");
    CHECK_EQ(toString(requireType("e")), "number?");
}

TEST_CASE_FIXTURE(BuiltinsFixture, "find_capture_types3")
{
    CheckResult result = check(R"END(
        local d, e, a, b, c = string.find("This is a string", "(.()(%a+))", 1, "this should be a bool")
    )END");

    LUAU_REQUIRE_ERROR_COUNT(1, result);

    TypeMismatch* tm = get<TypeMismatch>(result.errors[0]);
    REQUIRE(tm);
    CHECK_EQ(toString(tm->wantedType), "boolean?");
    CHECK_EQ(toString(tm->givenType), "string");

    CHECK_EQ(toString(requireType("a")), "string?");
    CHECK_EQ(toString(requireType("b")), "number?");
    CHECK_EQ(toString(requireType("c")), "string?");
    CHECK_EQ(toString(requireType("d")), "number?");
    CHECK_EQ(toString(requireType("e")), "number?");
}

TEST_CASE_FIXTURE(BuiltinsFixture, "find_capture_types3")
{
    CheckResult result = check(R"END(
        local d, e, a, b = string.find("This is a string", "(.()(%a+))", 1, true)
    )END");

    LUAU_REQUIRE_ERROR_COUNT(1, result);

    CountMismatch* acm = get<CountMismatch>(result.errors[0]);
    REQUIRE(acm);
    CHECK_EQ(acm->context, CountMismatch::FunctionResult);
    CHECK_EQ(acm->expected, 2);
    CHECK_EQ(acm->actual, 4);

    CHECK_EQ(toString(requireType("d")), "number?");
    CHECK_EQ(toString(requireType("e")), "number?");
}

TEST_CASE_FIXTURE(BuiltinsFixture, "string_find_should_not_crash")
{
    ScopedFastFlag _{FFlag::DebugLuauForceOldSolver, false};

    LUAU_REQUIRE_NO_ERRORS(check(R"(
        local function StringSplit(input, separator)
            string.find(input, separator)
            if not separator then
                separator = "%s+"
            end
        end
    )"));
}

TEST_CASE_FIXTURE(BuiltinsFixture, "table_dot_clone_type_states")
{
    CheckResult result = check(R"(
        local t1 = {}
        t1.x = 5
        local t2 = table.clone(t1)
        t2.y = 6
        t1.z = 3
    )");

    LUAU_REQUIRE_NO_ERRORS(result);

    if (!FFlag::DebugLuauForceOldSolver)
    {
        CHECK_EQ(toString(requireType("t1"), {true}), "{ x: number, z: number }");
        CHECK_EQ(toString(requireType("t2"), {true}), "{ x: number, y: number }");
    }
    else
    {
        CHECK_EQ(toString(requireType("t1"), {true}), "{| x: number, z: number |}");
        CHECK_EQ(toString(requireType("t2"), {true}), "{| x: number, y: number |}");
    }
}

TEST_CASE_FIXTURE(BuiltinsFixture, "table_clone_should_not_break")
{
    CheckResult result = check(R"(
        local Immutable = {}

        function Immutable.Set(dictionary, key, value)
            local new = table.clone(dictionary)

            new[key] = value

            return new
        end

        return Immutable
    )");

    LUAU_REQUIRE_NO_ERRORS(result);
}

TEST_CASE_FIXTURE(BuiltinsFixture, "table_clone_should_not_break_2")
{
    CheckResult result = check(R"(
        function set(dictionary, key, value)
            local new = table.clone(dictionary)

            new[key] = value

            return new
        end
    )");

    LUAU_REQUIRE_NO_ERRORS(result);
}

TEST_CASE_FIXTURE(BuiltinsFixture, "string_format_should_support_any")
{
    ScopedFastFlag _{FFlag::DebugLuauForceOldSolver, false};

    CheckResult result = check(R"(
        local x: any = "world"
        print(string.format("Hello, %s!", x))
    )");

    LUAU_REQUIRE_NO_ERRORS(result);
}

TEST_CASE_FIXTURE(BuiltinsFixture, "string_format_should_support_any_2")
{
    ScopedFastFlag _{FFlag::DebugLuauForceOldSolver, false};

    CheckResult result = check(R"(
        local fmt = "Hello, %s!" :: any
        local x = "world" :: any
        print(string.format(fmt, x))
        print(string.format(fmt, "hello"))
        print(string.format(fmt, 5)) -- unchecked because the format string is `any`!
    )");

    LUAU_REQUIRE_NO_ERRORS(result);
}

TEST_CASE_FIXTURE(BuiltinsFixture, "string_format_should_support_singleton_types")
{
    ScopedFastFlag _{FFlag::DebugLuauForceOldSolver, false};

    CheckResult result = check(R"(
        local fmt: "Hello, %s!" = "Hello, %s!"
        print(string.format(fmt, "hello"))
        print(string.format(fmt, 5)) -- should still produce an error since the expected type is `string`!
    )");

    LUAU_REQUIRE_ERROR_COUNT(1, result);
    TypeMismatch* tm = get<TypeMismatch>(result.errors[0]);
    REQUIRE(tm);
    CHECK_EQ(tm->wantedType, getBuiltins()->stringType);
    CHECK_EQ(tm->givenType, getBuiltins()->numberType);
}

// Remove this test with FFlagLuauSilenceDynamicFormatStringErrors.
TEST_CASE_FIXTURE(BuiltinsFixture, "better_string_format_error_when_format_string_is_dynamic")
{
    ScopedFastFlag solver2{FFlag::DebugLuauForceOldSolver, false};
    ScopedFastFlag keepDynamicFormatString{FFlag::LuauSilenceDynamicFormatStringErrors, false};

    CheckResult result = check(R"(
        local fmt: string = "Hello, %s!"
        print(string.format(fmt, "hello"))
        print(string.format(fmt :: any, "hello")) -- no error
    )");

    LUAU_REQUIRE_ERROR_COUNT(1, result);
    CHECK_EQ(
        "We cannot statically check the type of `string.format` when called with a format string that is not statically known.\n"
        "If you'd like to use an unchecked `string.format` call, you can cast the format string to `any` using `:: any`.",
        toString(result.errors[0])
    );
}

TEST_CASE_FIXTURE(Fixture, "write_only_table_assertion")
{
    DOES_NOT_PASS_OLD_SOLVER_GUARD();

    LUAU_REQUIRE_NO_ERRORS(check(R"(
        local function accept(t: { write foo: number })
        end

        accept({ foo = "lol", foo = true })
    )"));
}

TEST_CASE_FIXTURE(BuiltinsFixture, "table_insert_into_any")
{
    LUAU_REQUIRE_NO_ERRORS(check(R"(
table.insert(1::any, 2::any)
    )"));
}

TEST_CASE_FIXTURE(BuiltinsFixture, "table_insert_requires_all_fields")
{
    CheckResult result = check(R"(
        local function huh(): { { x: number, y: string } }
            local ret = {}
            while true do
                table.insert(ret, { x = 42 })
            end
            return ret
        end
    )");

    LUAU_REQUIRE_ERRORS(result);
}

TEST_CASE_FIXTURE(BuiltinsFixture, "read_refinements_on_persistent_tables_known_property_identity")
{
    // This will not result in a real refinement, as we refine `bnot`, a function, to be truthy
    LUAU_REQUIRE_NO_ERRORS(check(R"(
        if bit32.bnot then
        end
    )"));
}

TEST_CASE_FIXTURE(BuiltinsFixture, "read_refinements_on_persistent_tables_unknown_property")
{
    CheckResult results = check(R"(
        if bit32.scrambleEggs then
        end
    )");

    LUAU_REQUIRE_ERROR_COUNT(1, results);
    auto err = get<UnknownProperty>(results.errors[0]);
    CHECK_EQ(err->key, "scrambleEggs");
    CHECK_EQ(toString(err->table), "typeof(bit32)");
}

TEST_CASE_FIXTURE(BuiltinsFixture, "read_refinements_on_persistent_tables_known_property_narrow")
{
    ScopedFastFlag _{FFlag::DebugLuauForceOldSolver, false};

    LUAU_REQUIRE_NO_ERRORS(check(R"(
        local myutf8 = utf8
        if myutf8.charpattern == "lol" then
            local foobar = myutf8.charpattern
            local _ = foobar
        end
    )"));
    CHECK_EQ("\"lol\"", toString(requireTypeAtPosition(Position{4, 23})));
}

TEST_CASE_FIXTURE(BuiltinsFixture, "next_with_refined_any")
{
    ScopedFastFlag lsv2{FFlag::DebugLuauForceOldSolver, false};

    CheckResult result = check(R"(
        --!strict
        local t: any = {"hello", "world"}
        if type(t) == "table" and next(t) then
            local foo, bar = next(t)
            local _ = foo
            local _ = bar
        end
    )");

    LUAU_REQUIRE_NO_ERRORS(result);

    CHECK_EQ(toString(requireTypeAtPosition(Position{5, 23})), "unknown?");
    CHECK_EQ(toString(requireTypeAtPosition(Position{6, 23})), "unknown");
}

TEST_CASE_FIXTURE(BuiltinsFixture, "pairs_with_refined_any")
{
    ScopedFastFlag lsv2{FFlag::DebugLuauForceOldSolver, false};

    CheckResult result = check(R"(
        --!strict
        local t: any = {"hello", "world"}
        if type(t) == "table" and pairs(t) then
	        local foo, bar, lorem = pairs(t)
            local _ = foo
            local _ = bar
            local _ = lorem
        end
    )");

    LUAU_REQUIRE_NO_ERRORS(result);

    CHECK_EQ(toString(requireTypeAtPosition(Position{5, 23})), "({+ [unknown]: unknown +}, unknown?) -> (unknown?, unknown)");
    CHECK_EQ(toString(requireTypeAtPosition(Position{6, 23})), "{+ [unknown]: unknown +}");
    CHECK_EQ(toString(requireTypeAtPosition(Position{7, 23})), "nil");
}

TEST_CASE_FIXTURE(BuiltinsFixture, "vector_lerp_should_not_crash")
{
    LUAU_REQUIRE_NO_ERRORS(check(R"(
        local function half(x: number, y: number, z: number): vector
            return vector.lerp(vector.zero, vector.create(x, y, z), 0.5)
        end
    )"));
}

TEST_CASE_FIXTURE(BuiltinsFixture, "instantiation_works_on_builtins")
{
    ScopedFastFlag sffs[] = {
        {FFlag::LuauExplicitTypeInstantiationSupport, true},
    };

    CheckResult result = check(R"(
        local foo = table.create<<string>>(4)
        local bar = table.unpack<<string>>({})
        local baz = table.find<<string>>({}, 1) -- should error
        assert<<string>>("asd", "lol")
    )");

    LUAU_REQUIRE_ERROR_COUNT(1, result);
    CHECK_EQ("Expected this to be 'string', but got 'number'", toString(result.errors[0]));
}

TEST_CASE_FIXTURE(BuiltinsFixture, "table_freeze_on_any_should_not_error")
{
    ScopedFastFlag sffs[] = {{FFlag::DebugLuauForceOldSolver, false}, {FFlag::LuauTableFreezeCheckIsSubtype, true}};

    CheckResult result = check(R"(
        local function foo(): any
            return true
        end

        table.freeze(foo())
    )");

    LUAU_REQUIRE_NO_ERRORS(result);
}

TEST_CASE_FIXTURE(BuiltinsFixture, "table_freeze_with_type_check_should_not_error")
{
    ScopedFastFlag sffs[] = {{FFlag::DebugLuauForceOldSolver, false}, {FFlag::LuauTableFreezeCheckIsSubtype, true}};

    CheckResult result = check(R"(
        local function maybeFreeze(t: any)
            if type(t) == "table" then
                table.freeze(t)
            end
        end
    )");

    LUAU_REQUIRE_NO_ERRORS(result);
}

TEST_CASE_FIXTURE(BuiltinsFixture, "table_freeze_no_args")
{
    ScopedFastFlag sffs[] = {{FFlag::DebugLuauForceOldSolver, false}, {FFlag::LuauTableFreezeCheckIsSubtype, true}};

    CheckResult result = check(R"(
        table.freeze()
    )");

    LUAU_REQUIRE_ERROR_COUNT(1, result);
    CHECK_EQ("Argument count mismatch. Function 'table.freeze' expects 1 argument, but none are specified", toString(result.errors[0]));
}

TEST_CASE_FIXTURE(BuiltinsFixture, "table_freeze_with_type_pack_should_error")
{
    ScopedFastFlag sffs[] = {{FFlag::DebugLuauForceOldSolver, false}, {FFlag::LuauTableFreezeCheckIsSubtype, true}};

    CheckResult result = check(R"(
        table.freeze({x = 5}, {y = "hello"})
    )");

    LUAU_REQUIRE_ERROR_COUNT(1, result);
    CHECK_EQ("Argument count mismatch. Function 'table.freeze' expects 1 argument, but 2 are specified", toString(result.errors[0]));
}

TEST_CASE_FIXTURE(BuiltinsFixture, "table_freeze_with_variadic_any_should_not_error")
{
    ScopedFastFlag sffs[] = {{FFlag::DebugLuauForceOldSolver, false}, {FFlag::LuauTableFreezeCheckIsSubtype, true}};

    CheckResult result = check(R"(
        local function bar(): ...any
            return true
        end

        table.freeze(bar())
    )");

    LUAU_REQUIRE_NO_ERRORS(result);
}

TEST_CASE_FIXTURE(BuiltinsFixture, "table_freeze_with_variadic_non_error_suppressing_should_error")
{
    ScopedFastFlag sffs[] = {{FFlag::DebugLuauForceOldSolver, false}, {FFlag::LuauTableFreezeCheckIsSubtype, true}};

    CheckResult result = check(R"(
        local function bar(): ...string
            return
        end

        table.freeze(bar())
    )");

    LUAU_REQUIRE_ERROR_COUNT(1, result);
    // TODO (CLI-185019): We probably want a count mismatch error here instead.
    CHECK_EQ("Expected this to be 'table', but got 'string'", toString(result.errors[0]));
}

TEST_CASE_FIXTURE(BuiltinsFixture, "variadic_return_to_single_parameter_function")
{
    CheckResult result = check(R"(
        local function bar(): ...string
            return
        end

        local function foo(x: string)
            print(x) -- nil
        end

        foo(bar())
    )");

    // TODO (CLI-185019): We want to error here in some way, maybe a count mismatch?
    LUAU_REQUIRE_NO_ERRORS(result);
}

TEST_CASE_FIXTURE(BuiltinsFixture, "table_freeze_generic_pack")
{
    ScopedFastFlag sffs[] = {{FFlag::DebugLuauForceOldSolver, false}, {FFlag::LuauTableFreezeCheckIsSubtype, true}};

    CheckResult result = check(R"(
        local function foo<T...>(...: T...)
            table.freeze(...)
        end
    )");

    LUAU_REQUIRE_ERROR_COUNT(1, result);

    TypePackMismatch* tpm = get<TypePackMismatch>(result.errors[0]);
    REQUIRE(tpm);
    CHECK_EQ(toString(tpm->wantedTp), "table");
    CHECK_EQ(toString(tpm->givenTp), "T...");
}

TEST_CASE_FIXTURE(BuiltinsFixture, "table_freeze_function")
{
    ScopedFastFlag sffs[] = {{FFlag::DebugLuauForceOldSolver, false}, {FFlag::LuauTableFreezeCheckIsSubtype, true}};

    CheckResult result = check(R"(
        local function foo(f: () -> ())
            table.freeze(f())
        end
    )");

    LUAU_REQUIRE_ERROR_COUNT(1, result);
    CHECK_EQ("Argument count mismatch. Function 'table.freeze' expects 1 argument, but none are specified", toString(result.errors[0]));
}

TEST_SUITE_END();