File: core.jl

package info (click to toggle)
julia 1.0.3%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 49,452 kB
  • sloc: lisp: 236,453; ansic: 55,579; cpp: 25,603; makefile: 1,685; pascal: 1,130; sh: 956; asm: 86; xml: 76
file content (6799 lines) | stat: -rw-r--r-- 162,796 bytes parent folder | download
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
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
# This file is a part of Julia. License is MIT: https://julialang.org/license

# test core language features

using Random, SparseArrays, InteractiveUtils

const Bottom = Union{}


# For curmod_*
include("testenv.jl")

f47(x::Vector{Vector{T}}) where {T} = 0
@test_throws MethodError f47(Vector{Vector}())
@test f47(Vector{Vector{Int}}()) == 0

# checking unionall and typevar components
@test_throws TypeError ([] where T)
@test_throws TypeError ([T] where T)
@test_throws TypeError (Array{T} where T<:[])
@test_throws TypeError (Array{T} where T>:[])
@test_throws TypeError (Array{T} where T<:Vararg)
@test_throws TypeError (Array{T} where T>:Vararg)
@test_throws TypeError (Array{T} where T<:Vararg{Int})
@test_throws TypeError (Array{T} where T<:Vararg{Int,2})

@test_throws TypeError TypeVar(:T) <: Any
@test_throws TypeError TypeVar(:T) >: Any

# issue #12939
module Issue12939
abstract type Abs; end
struct Foo <: Abs; end
struct Bar; val::Int64; end
struct Baz; val::Int64; end
f(::Type{T}, x::T) where {T} = T(3)
f(::Type{Bar}, x::T) where {T <: Abs} = Bar(2)
f(::Type{Bar}, x) = Bar(1)
f(::Type{Baz}, x) = Baz(1)
f(::Type{Baz}, x::T) where {T <: Abs} = Baz(2)
end

@test Issue12939.f(Issue12939.Baz,Issue12939.Foo()) === Issue12939.Baz(2)
@test Issue12939.f(Issue12939.Bar,Issue12939.Foo()) === Issue12939.Bar(2)

# issue #11840
TT11840{T} = Tuple{T,T}
f11840(::Type) = "Type"
f11840(::DataType) = "DataType"
f11840(::UnionAll) = "UnionAll"
f11840(::Type{T}) where {T<:Tuple} = "Tuple"
@test f11840(Type) == "UnionAll"
@test f11840(Type.body) == "DataType"
@test f11840(Union{Int,Int8}) == "Type"
@test f11840(Tuple) == "Tuple"
@test f11840(TT11840) == "Tuple"

g11840(::DataType) = 1
g11840(::Type) = 2
g11840(sig::Type{T}) where {T<:Tuple} = 3
@test g11840(Vector.body) == 1
@test g11840(Vector) == 2
@test g11840(Vector.body) == 1
@test g11840(Vector) == 2
@test g11840(Tuple) == 3
@test g11840(TT11840) == 3

g11840b(::DataType) = 1
g11840b(::Type) = 2
g11840b(sig::Type{T}) where {T<:Tuple} = 3
@test g11840b(Vector) == 2
@test g11840b(Vector.body) == 1
@test g11840b(Vector) == 2
@test g11840b(Vector.body) == 1
@test g11840b(Tuple) == 3
@test g11840b(TT11840) == 3

h11840(::DataType) = '1'
h11840(::Type) = '2'
h11840(::UnionAll) = '3'
h11840(::Type{T}) where {T<:Tuple} = '4'
@test h11840(Vector) == '3'
@test h11840(Vector.body) == '1'
@test h11840(Vector) == '3'
@test h11840(Union{Vector, Matrix}) == '2'
@test h11840(Union{Vector.body, Matrix.body}) == '2'
@test h11840(Tuple) == '4'
@test h11840(TT11840) == '4'

# show that we don't make the cache confused by using alternative representations
# when specificity is reversed
j11840(::DataType) = '1'
j11840(::Union{Type{T}, T}) where {T} = '2' # force cache to contain leaftypes
@test j11840(Union{Tuple{Int32}, Tuple{Int64}}) == '2'
@test j11840(Tuple{Union{Int32, Int64}}) == '1' # DataType more specific than Type

# but show we can correctly match types with alternate equivalent representations
k11840(::Type{Union{Tuple{Int32}, Tuple{Int64}}}) = '2'
@test k11840(Tuple{Union{Int32, Int64}}) == '2'
@test k11840(Tuple{Union{Int32, Int64}}) == '2'
@test k11840(Union{Tuple{Int32}, Tuple{Int64}}) == '2'


# issue #20511
f20511(x::DataType) = 0
f20511(x) = 1
Type{Integer}  # cache this
@test f20511(Union{Integer,T} where T <: Unsigned) == 1

# join
@test typejoin(Int8,Int16) === Signed
@test typejoin(Int,AbstractString) === Any
@test typejoin(Array{Float64},BitArray) <: AbstractArray
@test typejoin(Array{Bool},BitArray) <: AbstractArray{Bool}
@test typejoin(Tuple{Int,Int8},Tuple{Int8,Float64}) === Tuple{Signed,Real}
@test typejoin(Tuple{String,String}, Tuple{GenericString,String},
               Tuple{String,GenericString}, Tuple{Int,String,Int}) ==
    Tuple{Any,AbstractString,Vararg{Int}}
@test typejoin(Tuple{Int8,Vararg{Int}}, Tuple{Int8,Int8}) ==
    Tuple{Int8,Vararg{Signed}}
@test typejoin(Tuple{Int8,Vararg{Int}}, Tuple{Int8,Vararg{Int8}}) ==
    Tuple{Int8,Vararg{Signed}}
@test typejoin(Tuple{Int8,UInt8,Vararg{Int}}, Tuple{Int8,Vararg{Int8}}) ==
    Tuple{Int8,Vararg{Integer}}
@test typejoin(Union{Int,AbstractString}, Int) == Union{Int,AbstractString}
@test typejoin(Union{Int,AbstractString}, Int8) == Any
@test typejoin(Tuple{}, Tuple{Int}) == Tuple{Vararg{Int}}

# typejoin associativity
abstract type Foo____{K} end
mutable struct Wow____{K,V} <: Foo____{K} end
mutable struct Bar____{K,V} <: Foo____{K} end
let
    a = Wow____{Int64, Int64}
    b = Wow____{Int64, Float64}
    c = Bar____{Int64, Int64}
    @test typejoin(typejoin(b,c), a) == typejoin(typejoin(b,a), c) == Foo____{Int64}
end

# typejoin with Vararg{T,N}
@test typejoin(Tuple{Vararg{Int,2}}, Tuple{Int,Int,Int}) === Tuple{Int,Int,Vararg{Int}}
@test typejoin(Tuple{Vararg{Int,2}}, Tuple{Vararg{Int}}) === Tuple{Vararg{Int}}

@test typejoin(NTuple{3,Tuple}, NTuple{2,T} where T) == Tuple{Any,Any,Vararg{Tuple}}

# issue #26321
struct T26321{N,S<:NTuple{N}}
    t::S
end
let mi = T26321{3,NTuple{3,Int}}((1,2,3)), mf = T26321{3,NTuple{3,Float64}}((1.0,2.0,3.0))
    J = T26321{3,S} where S<:(Tuple{T,T,T} where T)
    @test typejoin(typeof(mi),typeof(mf)) == J
    a = [mi, mf]
    @test a[1] === mi
    @test a[2] === mf
    @test eltype(a) == J
    @test a isa Vector{<:T26321{3}}
end

# promote_typejoin returns a Union only with Nothing/Missing combined with concrete types
for T in (Nothing, Missing)
    @test Base.promote_typejoin(Int, Float64) === Real
    @test Base.promote_typejoin(Int, T) === Union{Int, T}
    @test Base.promote_typejoin(T, String) === Union{T, String}
    @test Base.promote_typejoin(Vector{Int}, T) === Union{Vector{Int}, T}
    @test Base.promote_typejoin(Vector, T) === Any
    @test Base.promote_typejoin(Real, T) === Any
    @test Base.promote_typejoin(Int, String) === Any
    @test Base.promote_typejoin(Int, Union{Float64, T}) === Any
    @test Base.promote_typejoin(Int, Union{String, T}) === Any
    @test Base.promote_typejoin(T, Union{}) === T
    @test Base.promote_typejoin(Union{}, T) === T
end

@test promote_type(Bool,Bottom) === Bool

# type declarations

abstract type Sup_{A,B} end
abstract type Qux_{T} <: Sup_{Qux_{Int},T} end

@test Qux_{Int}.super <: Sup_
@test ===(Qux_{Int}, Qux_{Int}.super.parameters[1])
@test ===(Qux_{Int}.super.parameters[2], Int)
@test Qux_{Char}.super <: Sup_
@test ===(Qux_{Int}, Qux_{Char}.super.parameters[1])
@test ===(Qux_{Char}.super.parameters[2], Char)

@test Qux_.body.super.parameters[1].super <: Sup_
@test ===(Qux_{Int}, Qux_.body.super.parameters[1].super.parameters[1])
@test ===(Int, Qux_.body.super.parameters[1].super.parameters[2])

mutable struct Foo_{T} x::Foo_{Int} end

@test ===(Foo_.body.types[1], Foo_{Int})
@test ===(Foo_.body.types[1].types[1], Foo_{Int})

mutable struct Circ_{T} x::Circ_{T} end
@test ===(Circ_{Int}, Circ_{Int}.types[1])

abstract type Sup2a_ end
abstract type Sup2b_{A <: Sup2a_, B} <: Sup2a_ end
@test_throws ErrorException @eval abstract type Qux2_{T} <: Sup2b_{Qux2_{Int}, T} end # wrapped in eval to avoid #16793

# issue #21923
struct A21923{T,N}; v::Vector{A21923{T}}; end
@test fieldtype(A21923,1) == Vector{A21923{T}} where T
struct B21923{T,N}; v::Vector{B21923{T,M} where M}; end
@test fieldtype(B21923, 1) == Vector{B21923{T,M} where M} where T
struct C21923{T,N}; v::C21923{T,M} where M; end
@test fieldtype(C21923, 1) == C21923
struct D21923{T,N}; v::D21923{T}; end
@test fieldtype(D21923, 1) == D21923

# issue #22624, more circular definitions
struct T22624{A,B,C}; v::Vector{T22624{Int64,A}}; end
let elT = T22624.body.body.body.types[1].parameters[1]
    @test elT == T22624{Int64, T22624.var, C} where C
    elT2 = elT.body.types[1].parameters[1]
    @test elT2 == T22624{Int64, Int64, C} where C
    @test elT2.body.types[1].parameters[1] === elT2
    @test Base.isconcretetype(elT2.body.types[1])
end

# issue #3890
mutable struct A3890{T1}
    x::Matrix{Complex{T1}}
end
@test A3890{Float64}.types[1] === Array{Complex{Float64},2}
# make sure the field type Matrix{Complex{T1}} isn't cached
mutable struct B3890{T2}
    x::Matrix{Complex{T2}}
end
@test B3890{Float64}.types[1] === Array{Complex{Float64},2}

# issue #786
mutable struct Node{T}
    v::Vector{Node}
end

@test ===(Node{Int}.types[1].parameters[1], Node)

mutable struct Node2{T}
    v::Vector{Node2{T}}
end

@test ===(Node2{Int}.types[1].parameters[1], Node2{Int})

mutable struct FooFoo{A,B} y::FooFoo{A} end

@test FooFoo{Int} <: FooFoo{Int,AbstractString}.types[1]


let x = (2,3)
    @test +(x...) == 5
end

# conversions
function fooo()
    local x::Int8
    x = 100
    x
end
@test fooo() === convert(Int8,100)
function fooo_2()
    local x::Int8
    x = 100
end
@test fooo_2() === 100
function fooo_3()
    local x::Int8
    y = x = 100
    @test isa(x,Int8)
    y
end
@test fooo_3() === 100
let
    function foo()
        local x::Int8
        function bar()
            x = 100
        end
        bar()
        x
    end
    @test foo() === convert(Int8,100)
end

function bar(x::T) where T
    local z::Complex{T}
    z = x
    z
end
@test bar(3.0) == Complex(3.0,0.0)

z = convert(Complex{Float64},2)
@test z == Complex(2.0,0.0)

function typeassert_instead_of_decl()
    local x
    x = 1
    x::Float64
    return 0
end
@test_throws TypeError typeassert_instead_of_decl()

# type declarations on globals not implemented yet
@test_throws ErrorException eval(Meta.parse("global x20327::Int"))

y20327 = 1
@test_throws TypeError y20327::Float64

# misc
fib(n) = n < 2 ? n : fib(n-1) + fib(n-2)
@test fib(20) == 6765

# static parameters
sptest1(x::T, y::T) where {T} = 42
sptest1(x::T, y::S) where {T,S} = 43
@test sptest1(1,2) == 42
@test sptest1(1,"b") == 43

sptest2(x::T) where {T} = T
@test ===(sptest2(:a),Symbol)

sptest3(x::T) where {T} = y->T
let m = sptest3(:a)
    @test ===(m(0),Symbol)
end

sptest4(x::T, y::T) where {T} = 42
sptest4(x::T, y) where {T} = 44
@test sptest4(1,2) == 42
@test sptest4(1, "cat") == 44

# closures
function clotest()
    c = 0
    function inc()
        c += 1
    end
    function dec()
        c -= 1
    end
    inc(); inc()
    @test c == 2
    dec()
    @test c == 1
    @test (()->c)() == 1

    fibb(n) = n < 2 ? n : fibb(n-1)+fibb(n-2)
    @test fibb(10) == 55

    return (n->(c+=n), ()->c)
end
let T = clotest()
    (inc, C) = T
    inc(11)
    @test C() == 12
end

Yc(f) = (h->f(x->h(h)(x)))(h->f(x->h(h)(x)))
yfib = Yc(fib->(n->(n < 2 ? n : fib(n-1) + fib(n-2))))
@test yfib(20) == 6765

function capt_before_def()
    f() = y
    y = 2
    f
end
@test capt_before_def()() == 2

function i18408()
    local i
    return (x -> i)
end
let f = i18408()
    @test_throws UndefVarError(:i) f(0)
end

# issue #23558
c23558(n,k) =
    let fact(n) = if (n == 0) 1 else n*fact(n-1) end
        fact(n)/fact(k)/fact(n-k)
    end
@test c23558(10, 5) == 252

# issue #23996
function foo23996(xs...)
    rets = []
    bar(::Int) = push!(rets, 1)
    foobar() = push!(rets, 3)
    bar(::AbstractFloat) = push!(rets, 2)
    bar(::Bool) = foobar()
    for x in xs
	bar(x)
    end
    rets
end
@test foo23996(1,2.0,false) == [1,2,3]

# variable scope, globals
glob_x = 23
function glotest()
    global glob_x
    glob_x = 24
    loc_x = 8
    function inner()
        global loc_x = 10
        glob_x = 88
    end
    function inner2()
        local glob_x  # override
        global loc_x
        glob_x = 2
        @test glob_x == 2
        @test loc_x == 10
    end
    inner()
    inner2()
    @test glob_x == 88
    @test loc_x == 8
end
glotest()
@test glob_x == 88
@test loc_x == 10

# issue #7234
f7234_cnt = 0
begin
    glob_x2 = 24
    function f7234_a()
        global f7234_cnt += 1
        glob_x2 += 1
        global f7234_cnt += -10000
    end
end
@test_throws UndefVarError(:glob_x2) f7234_a()
@test f7234_cnt == 1
begin
    global glob_x2 = 24
    function f7234_b()
        global f7234_cnt += 1
        glob_x2 += 1
        global f7234_cnt += -10000
    end
end
@test_throws UndefVarError(:glob_x2) f7234_b()
@test f7234_cnt == 2
# globals can accessed if declared
for i = 1:2
    global glob_x2 += 1
end
@test glob_x2 == 26
# globals declared as such in a non-global scope are inherited
let
    global glob_x3 = 11
    f7234_2() = (glob_x3 += 1)
    f7234_2()
end
@test glob_x3 == 12

# interaction between local variable renaming and nested globals (#19333)
x19333 = 1
function f19333(x19333)
    return let x19333 = x19333
        g19333() = (global x19333 += 2)
        g19333() + (x19333 += 1)
    end + (x19333 += 1)
end
@test f19333(0) == 5
@test f19333(0) == 7
@test x19333 == 5

function h19333()
    s = 0
    for (i, j) in ((1, 2),)
        s += i + j # use + as a global
    end
    for (k, +) in ((3, 4),)
        s -= (k - +) # use + as a local
    end
    return s
end
@test h19333() == 4

# let - new variables, including undefinedness
let_undef_cnt = 0
function let_undef()
    first = true
    for i = 1:2
        let x # new x
            if first # not defined on second pass
                x = 1
                first = false
            end
            global let_undef_cnt += 1
            x + 1
            global let_undef_cnt += 23
        end
    end
end
@test_throws UndefVarError let_undef()
@test let_undef_cnt == 25

# const implies local in a local scope block
function const_implies_local()
    let
        x = 1
        local y
        let
            # TODO: change back to `const` if that's ever allowed
            local x = 0
            y = x
        end
        x, y
    end
end
@test const_implies_local() === (1, 0)

a_global_closure_vector = Vector{Any}(undef, 3)
for i = 1:3
    let ii = i
        a_global_closure_vector[i] = x -> x + ii
    end
end
@test a_global_closure_vector[1](10) == 11
@test a_global_closure_vector[2](10) == 12
@test a_global_closure_vector[3](10) == 13

# issue #22032
let a = [], fs = []
    for f() in 1:3
        push!(a, f())
        push!(fs, f)
    end
    @test a == [1,2,3]
    @test [f() for f in fs] == [1,2,3]
end
let t = (22,33)
    (g(), x) = t
    @test g() == 22
    @test x == 33
end

# issue #23091
let (f(), x) = (1, 2)
    @test f() == 1
    @test x == 2
end

# issue #21900
f21900_cnt = 0
function f21900()
    for i = 1:1
        x = 0
    end
    global f21900_cnt += 1
    x # should be global
    global f21900_cnt += -1000
    nothing
end
@test_throws UndefVarError(:x) f21900()
@test f21900_cnt == 1

# use @eval so this runs as a toplevel scope block
@test_throws UndefVarError(:foo21900) @eval begin
    for i21900 = 1:10
        local bar21900
        for j21900 = 1:10
            foo21900 = 10
        end
        bar21900 = 0
        bar21900 = foo21900 + 1
    end
end
@test !@isdefined(foo21900)
@test !@isdefined(bar21900)
bar21900 = 0
@test_throws UndefVarError(:foo21900) @eval begin
    for i21900 = 1:10
        global bar21900
        for j21900 = 1:10
            foo21900 = 10
        end
        bar21900 = -1
        bar21900 = foo21900 + 1
    end
end
@test bar21900 == -1
@test !@isdefined foo21900
foo21900 = 0
@test nothing === begin
    for i21900 = 1:10
        global bar21900, foo21900
        for j21900 = 1:10
            foo21900 = 10
        end
        bar21900 = -1
        bar21900 = foo21900 + 1
    end
end
@test foo21900 == 10
@test bar21900 == 11

# ? syntax
@test (true ? 1 : false ? 2 : 3) == 1

# issue #7252
let
    local a
    1 > 0 ? a=2 : a=3
    @test a == 2
    1 < 0 ? a=2 : a=3
    @test a == 3
end

# tricky space sensitive syntax cases
@test [-1 ~1] == [(-1) (~1)]

# undefinedness
mutable struct UndefField
    field
    UndefField() = new()
end

let
    local a
    a = UndefField()
    @test !isdefined(a, :field)
    @test !isdefined(a, :foo)
    @test !isdefined(2, :a)

    @test_throws TypeError isdefined(Base, 2)
    @test_throws ArgumentError isdefined(2)
end

let
    local a
    a = Vector{Any}(undef, 2)
    @test !isassigned(a,1) && !isassigned(a,2)
    a[1] = 1
    @test isassigned(a,1) && !isassigned(a,2)
    a = Vector{Float64}(undef,1)
    @test isassigned(a,1)
    @test isassigned(a)
    @test !isassigned(a,2)
end

# isassigned, issue #11167
mutable struct Type11167{T,N} end
Type11167{Int,2}
let tname = Type11167.body.body.name
    @test !isassigned(tname.cache, 0)
    @test isassigned(tname.cache, 1)
    @test !isassigned(tname.cache, 2)
    Type11167{Float32,5}
    @test isassigned(tname.cache, 2)
    @test !isassigned(tname.cache, 3)
end

# dispatch
let
    local foo, foo2, fooN, bar, baz
    foo(x::Tuple{Vararg{Any}}) = 0
    foo(x::Tuple{Vararg{Integer}}) = 1
    @test foo((:a,)) == 0
    @test foo(( 2,)) == 1

    foo2(x::Vararg{Any,2}) = 2
    @test foo2(1,2) == 2
    @test_throws MethodError foo2(1)
    @test_throws MethodError foo2(1,2,3)

    fooN(A::Array{T,N}, x::Vararg{Any,N}) where {T,N} = -1
    @test fooN([1,2], 1) == -1
    @test_throws MethodError fooN([1,2], 1, 2) == -1
    @test fooN([1 2; 3 4], 1, 2) == -1
    @test_throws MethodError fooN([1 2; 3 4], 1)
    @test_throws MethodError fooN([1 2; 3 4], 1, 2, 3)

    bar(x::Tuple{T,T,T,T}) where {T} = 1
    bar(x::Tuple{Any,Any,Any,Any})=2
    @test bar((1,1,1,1)) == 1
    @test bar((1,1,1,"a")) == 2
    @test bar((:a,:a,:a,:a)) == 1

    baz(::Type{Rational}) = 1
    baz(::Type{Rational{T}}) where {T} = 2
    @test baz(Rational) == 1
    @test baz(Rational{Int}) == 2
end

let
    local mytype
    function mytype(vec)
        convert(Vector{Tuple{String, DataType}}, vec)
    end
    some_data = Any[("a", Int32), ("b", Int32)]
    @test isa(mytype(some_data),Vector{Tuple{String, DataType}})
end

mutable struct MyArray{N} <: AbstractArray{Int, N}
end
let
    local x
    x = MyArray{1}()
    foob(x::AbstractArray) = 0
    foob(x::AbstractVector{T}) where {T} = 1
    @test foob(x) == 1
end

let
    local f, g, a
    f(a::Vector{Vector{T}}) where {T} = a
    g(a::Vector{Vector{T}}) where {T} = a
    a = Vector{Int}[]
    @test ===(f(a), a)
    @test ===(g(a), a)
end

mutable struct _AA{T}; a::T; end
_AoA{T} = _AA{_AA{T}}
let
    local g, a
    g(a::_AA{_AA{T}}) where {T} = a
    a = _AA(_AA(1))
    @test ===(g(a),a)
end

# dispatch using Val{T}. See discussion in #9452, #22475 for instances vs types
let
    local firstlast
    firstlast(::Val{true}) = "First"
    firstlast(::Val{false}) = "Last"

    @test firstlast(Val(true)) == "First"
    @test firstlast(Val(false)) == "Last"
end

# x::Vararg{Any} declarations
let
    local f1, f2, f3
    f1(x...) = [x...]
    f2(x::Vararg{Any}) = [x...]
    f3(x::Vararg) = [x...]
    @test f1(1,2,3) == [1,2,3]
    @test f2(1,2,3) == [1,2,3]
    @test f3(1,2,3) == [1,2,3]
end

# try/finally
begin
    try_finally_glo_after = 0
    try_finally_loc_after = 0
    try_finally_glo_b = try
        1+2
    finally
        # try_finally_loc_after = 1 # enable with #19324
        global try_finally_glo_after = 1
    end
    @test try_finally_loc_after == 0
    @test try_finally_glo_b == 3
    @test try_finally_glo_after == 1

    try_finally_glo_after = 0
    gothere = 0
    try
        try
            error(" ")
        finally
            # try_finally_loc_after = 1 # enable with #19324
            global try_finally_glo_after = 1
        end
        global gothere = 1
    catch
    end
    @test try_finally_loc_after == 0
    @test try_finally_glo_after == 1
    @test gothere == 0

    try_finally_glo_after = 0
    try_finally_glo_b = try
        error(" ")
    catch
        42
    finally
        # try_finally_loc_after = 1 # enable with #19324
        global try_finally_glo_after = 1
    end
    @test try_finally_loc_after == 0
    @test try_finally_glo_b == 42
    @test try_finally_glo_after == 1

    global glo = 0
    function retfinally()
        try
            return 5
        finally
            global glo = 18
        end
    end
    @test retfinally() == 5
    @test glo == 18

    @test try error(); catch; end === nothing
end

# issue #12806
let i = 0, x = 0
    for outer i = 1:10
        try
            break
        finally
            x = 1
        end
    end
    @test i == 1
    @test x == 1
end

let i = 1, a = []
    while true
        try
            push!(a, i)
            i += 1
            i < 5 && continue
            break
        catch
            push!(a, "catch")
        finally
            push!(a, "finally")
        end
    end
    @test a == [1, "finally", 2, "finally", 3, "finally", 4, "finally"]
end

function _two_finally(n)
    a = []
    for i = 1:5
        push!(a, i)
        try
            try
                n == 1 && break
                n == 2 && i > 1 && return [copy(a), a]
            finally
                push!(a, "finally 1")
            end
        finally
            push!(a, "finally 2")
        end
    end
    return a
end
@test _two_finally(1) == [1, "finally 1", "finally 2"]
@test _two_finally(2) == [[1, "finally 1", "finally 2", 2],
                          [1, "finally 1", "finally 2", 2, "finally 1", "finally 2"]]

let i = 0
    caught = nothing
    try
        try
            error("oops")
        catch
            throw(42)
        finally
            i = 1
        end
    catch e
        caught = e
    end
    @test caught == 42
    @test i == 1
end

let i = 0, a = []
    for i = 1:2
        try
            continue
        finally
            push!(a, "finally")
        end
        push!(a, "oops")
    end
    @test a == ["finally", "finally"]
end

# test from #13660
let x = 0, y = 0, z = 0
    for i = 1:2
        try
            i == 1 && continue
        finally
            x = 11
        end
        try
            i == 2 && throw(42)
        catch
            break
        finally
            y = 12
        end
    end
    for i = 1:2
        try i == 1 && break
        finally z = 13
        end
    end
    @test x == 11
    @test y == 12
    @test z == 13
end

function test12806()
    let catchb = false, catchc = false, catchr = false, a = []
        for i in 1:3
            try
                throw("try err")
            catch e
                i == 1 && break
                i == 2 && continue
                i == 3 && return (catchb, catchc, catchr, a)
            finally
                i == 1 && (catchb = true; continue)
                i == 2 && (catchc = true; )
                i == 3 && (catchr = true; push!(a, 1))
            end
        end
    end
end
@test test12806() == (true, true, false, [1])

# issue #24331
try
    c24331 = 1
finally
end
@test !isdefined(@__MODULE__, :c24331)
function f24331()
    try
        x = [2]
    finally
    end
end
@test f24331() == [2]

# issue #26743
function f26743()
    try
        return 5
    finally
    end
end
@test @inferred(f26743()) == 5

# finalizers
let A = [1]
    local x = 0
    finalizer(a->(x+=1), A)
    finalize(A)
    @test x == 1
    A = 0
    GC.gc(); GC.gc()
    @test x == 1
end

# Module() constructor
@test names(Module(:anonymous), all = true, imported = true) == [:anonymous]
@test names(Module(:anonymous, false), all = true, imported = true) == [:anonymous]

# exception from __init__()
let didthrow =
    try
        include_string(
            @__MODULE__,
            """
            module TestInitError
                __init__() = error()
            end
            """)
        false
    catch ex
        @test isa(ex, LoadError)
        @test isa(ex.error, InitError)
        true
    end
    @test didthrow
end

# issue #7307
function test7307(a, ret)
    try
        try
            ret && return a
        finally
            push!(a, "inner")
        end
    finally
        push!(a, "outer")
    end
    return a
end
@test test7307([], true) == ["inner","outer"]
@test test7307([], false) == ["inner","outer"]

# issue #8277
function test8277(a)
    i = 0
    for j=1:2
        try
            if i == 0
                push!(a,0)
            end
            i += 1
            error()
        catch
        end
    end
end
let a = []
    test8277(a)
    @test length(a) == 1
end

# chained and multiple assignment behavior (issue #2913)
let
    local x, a, b, c, d, e
    x = (a,b,b,b,e) = (1,2,3,4,5)
    @test x === (1,2,3,4,5)
    @test a == 1
    @test b == 4
    @test e == 5
    x = (a,b,b,e) = (1,2,3,4,5)
    @test x === (1,2,3,4,5)
    @test a == 1
    @test b == 3
    @test e == 4

    a = complex(1,2)
    b = 3
    b, a = a.re, b
    @test b == 1
    @test a == 3
    a = complex(1,2)
    b = 3
    a, b = b, a.re
    @test a == 3
    @test b == 1
end

# accessing fields by index
mutable struct TestMutable
    file::String
    line::Int
    error
end

let
    local z = complex(3, 4)
    v = Int[0, 0]
    for i = 1:2
        v[i] = getfield(z, i)
    end
    @test v == [3, 4]
    @test_throws BoundsError(z, -1) getfield(z, -1)
    @test_throws BoundsError(z, 0) getfield(z, 0)
    @test_throws BoundsError(z, 3) getfield(z, 3)

    strct = LoadError("yofile", 0, "bad")
    @test nfields(strct) == 3 # sanity test
    @test_throws BoundsError(strct, 10) getfield(strct, 10)
    @test_throws ErrorException("type LoadError is immutable") setfield!(strct, 0, "")
    @test_throws ErrorException("type LoadError is immutable") setfield!(strct, 4, "")
    @test_throws ErrorException("type is immutable") setfield!(strct, :line, 0)
    @test strct.file == "yofile"
    @test strct.line === 0
    @test strct.error == "bad"
    @test getfield(strct, 1) == "yofile"
    @test getfield(strct, 2) === 0
    @test getfield(strct, 3) == "bad"

    mstrct = TestMutable("melm", 1, nothing)
    @test Base.setproperty!(mstrct, :line, 8.0) === 8
    @test mstrct.line === 8
    @test_throws TypeError(:setfield!, "", Int, 8.0) setfield!(mstrct, :line, 8.0)
    @test_throws TypeError(:setfield!, "", Int, 8.0) setfield!(mstrct, 2, 8.0)
    @test setfield!(mstrct, 3, "hi") == "hi"
    @test mstrct.error == "hi"
    @test setfield!(mstrct, 1, "yo") == "yo"
    @test mstrct.file == "yo"
    @test_throws BoundsError(mstrct, 10) getfield(mstrct, 10)
    @test_throws BoundsError(mstrct, 0) setfield!(mstrct, 0, "")
    @test_throws BoundsError(mstrct, 4) setfield!(mstrct, 4, "")
end

# test getfield-overloading
function Base.getproperty(mstrct::TestMutable, p::Symbol)
    return (p, getfield(mstrct, :error))
end
function Base.setproperty!(mstrct::TestMutable, p::Symbol, v)
    return setfield!(mstrct, :error, (p, v))
end

let
    mstrct = TestMutable("melm", 1, nothing)
    @test mstrct.line === (:line, nothing)
    @test mstrct.bar === (:bar, nothing)
    @test getfield(mstrct, 1) == "melm"
    @test getfield(mstrct, :file) == "melm"
    @test_throws MethodError Base.getproperty(mstrct, 1)
    mstrct.error = 8.0
    @test mstrct.bar === (:bar, (:error, 8.0))
    mstrct.line = 8.0
    @test getfield(mstrct, :line) === 1
    @test getfield(mstrct, :error) === (:line, 8.0)
    @test mstrct.bar === (:bar, (:line, 8.0))
    @test mstrct.error === (:error, (:line, 8.0))
end

struct S29761
    x
end
function S29761_world(i)
    x = S29761(i)
    @eval function Base.getproperty(x::S29761, sym::Symbol)
        return sym => getfield(x, sym)
    end
    # ensure world updates are handled correctly for simple x.y expressions:
    return x.x, @eval($x.x), x.x
end
@test S29761_world(1) == (1, :x => 1, 1)


# allow typevar in Union to match as long as the arguments contain
# sufficient information
# issue #814
let
    local MatOrNot, my_func, M
    MatOrNot{T} = Union{AbstractMatrix{T}, Vector{Union{}}}
    my_func(A::MatOrNot{T}, B::MatOrNot{T}, C::MatOrNot{T}) where {T<:Real} = 0
    M = [ 2. 1. ; 1. 1. ]
    @test my_func(Union{}[], M, M) == 0
end

let
    local my_func, a, c
    my_func(P::Vector{T}, Q::Vector{T}) where {T} = 0
    my_func(x::T, P::Vector{T}) where {T} = 1
    my_func(P::Vector{T}, x::T) where {T} = 2
    a = Int[3]
    c = Vector[a]

    @test my_func(c,c)==0
    @test my_func(a,c)==1
end

let
    local baar, foor, boor
    # issue #1131
    baar(x::DataType) = 0
    baar(x::Union) = 1
    baar(x::UnionAll) = 2
    @test baar(StridedArray) == 2
    @test baar(Base.unwrap_unionall(StridedArray)) == 1
    @test baar(Vector) == 2
    @test baar(Vector.body) == 0

    boor(x) = 0
    boor(x::Union) = 1
    @test boor(StridedArray) == 0
    @test boor(Base.unwrap_unionall(StridedArray)) == 1

    # issue #1202
    foor(x::Union) = 1
    @test_throws MethodError foor(StridedArray)
    @test foor(Base.unwrap_unionall(StridedArray)) == 1
    @test_throws MethodError foor(StridedArray)
end

# issue #22842
f22842(x::UnionAll) = UnionAll
f22842(x::DataType) = length(x.parameters)
@test f22842(Tuple{Vararg{Int64,N} where N}) == 1
@test f22842(Tuple{Vararg{Int64,N}} where N) === UnionAll

# issue #1153
mutable struct SI{m, s, kg}
    value::AbstractFloat
end

import Base.*

*(x::SI{m1, s1, kg1}, y::SI{m2, s2, kg2}) where {m1, m2, s1, s2, kg1, kg2} = SI{m1 + m2, s1 + s2, kg1 + kg2}(x.value * y.value)

let
    local a,b
    a = SI{0,0,1}(1.0) * SI{1,2,0}(2.0)
    b = SI{0,0,1}(1.0) * SI{1,-2,0}(2.0)
    @test typeof(a) === SI{1,2,1}
    @test typeof(b) === SI{1,-2,1}
end

# pointer arithmetic
let
   local a,b,c
   a = C_NULL
   b = C_NULL + 1
   c = C_NULL - 1
   d = 1 + C_NULL
   @test eltype(a) == Nothing

   @test a != b != c
   @test b == d
   @test UInt(a) == 0
   @test UInt(b) == 1
   @test UInt(c) == typemax(UInt)

   @test b - a == -(a - b) == 1
   @test c - a == -(a - c) == typemax(UInt)
   @test c - b == -(b - c) == typemax(UInt) - 1
   @test a < b < c
end

# pull request 1270
let
    local a,p, a2,p2
    a = [11,12,13]
    p = pointer(a)
    @test unsafe_load(p, 1) == 11
    unsafe_store!(p, 99, 2)
    @test a == [11,99,13]
    a2 = Any[101,102,103]
    p2 = pointer(a2)
    @test unsafe_load(p2) == 101
    unsafe_store!(p2, 909, 3)
    @test a2 == [101,102,909]
end

@test unsafe_pointer_to_objref(ccall(:jl_call1, Ptr{Cvoid}, (Any,Any),
                                     x -> x+1, 314158)) == 314159
let x = [1,2,3]
    @test unsafe_pointer_to_objref(pointer_from_objref(x)) == x
    @test unsafe_pointer_to_objref(pointer_from_objref(x)) === x
end

let
    local a, aa
    a = [1,2,3]
    aa = unsafe_wrap(Array, pointer(a), length(a))
    @test aa == a
    aa = unsafe_wrap(Array, pointer(a), (length(a),))
    @test aa == a
    aa = unsafe_wrap(Array, pointer(a), UInt(length(a)))
    @test aa == a
    aa = unsafe_wrap(Array, pointer(a), UInt16(length(a)))
    @test aa == a
    aaa = unsafe_wrap(Array, pointer(a), (1, 1))
    @test size(aaa) == (1, 1)
    @test aaa[1] == a[1]
    @test_throws InexactError unsafe_wrap(Array, pointer(a), -3)
    # Misaligned pointer
    res = @test_throws ArgumentError unsafe_wrap(Array, pointer(a) + 1, length(a))
    @test occursin("is not properly aligned to $(sizeof(Int)) bytes", res.value.msg)
    res = @test_throws ArgumentError unsafe_wrap(Array, pointer(a) + 1, (1, 1))
    @test occursin("is not properly aligned to $(sizeof(Int)) bytes", res.value.msg)
end

struct FooBar2515
    foo::Int
    bar::Int
end
let
    local X, p
    X = FooBar2515[ FooBar2515(3,1), FooBar2515(4,4) ]
    p = pointer(X)
    @test unsafe_load(p) == FooBar2515(3,1)
    @test unsafe_load(p, 2) == FooBar2515(4,4)
    unsafe_store!(p, FooBar2515(8,4))
    @test X[1] == FooBar2515(8,4)
    unsafe_store!(p, FooBar2515(7,3), 1)
    @test X[1] == FooBar2515(7,3)
end

# issue #1287, combinations of try, catch, return
let
    local f, g

    function f()
        try
            return 1
        catch
        end
    end
    @test f() == 1

    function g()
        try
            error("badness")
        catch
            return 2
        end
    end
    @test g() == 2
end

# issue #1442
mutable struct S1442{T}
end

let
    local f1442
    f1442(::DataType) = 1
    f1442(::Type{S1442{T}}) where {T} = 2

    @test f1442(S1442{Int}) == 2
    @test f1442(DataType) == 1
end

# issue #1727
abstract type Component end

mutable struct Transform <: Component
    x
    y
    z

    Transform() = new(0, 0, 0)
end

mutable struct Body <: Component
    vel
    curr_force

    Body() = new(0, 0)
end

function NewEntity(components::Type{T}...) where T<:Component
    map((c)->c(), components)
end

@test_throws MethodError NewEntity(Transform, Transform, Body, Body)
@test isa(NewEntity(Transform, Transform), Tuple{Transform, Transform})
@test_throws MethodError NewEntity(Transform, Transform, Body, Body)

# issue #1826
let
    a = (1,2)
    a,b = a
    @test a==1 && b==2
end

@testset "issue #1876" begin
let
    tst = 1
    m1(i) = (tst+=1;i-1)
    x = [1:4;]
    x[1:end] *= 2
    @test x == [2:2:8;]
    x[m1(end)] += 3
    @test x == [2,4,9,8]
    @test tst == 2

    # issue #1886
    X = [1:4;]
    r = Vector{UnitRange{Int}}(undef, 1)
    r[1] = 2:3
    X[r...] *= 2
    @test X == [1,4,6,4]
end
end

# issue #1632
let
    f1632(::R, ::S) where {R,S} = 1
    f1632(::T, ::T) where {T} = 2
    @test f1632(1, 2) == 2
    @test f1632(:a, 2) == 1
    g1632(::T, ::T) where {T} = 2
    g1632(::R, ::S) where {R,S} = 1
    @test g1632(1, 2) == 2
    @test g1632(:a, 2) == 1
end

# issue #2098
let
    i2098() = begin
        c = Any[2.0]
        [1:1:c[1];]
    end
    @test isequal(i2098(), [1.0,2.0])
end

# issue #2161
let
    i2161_1() = promote(2,2,2.0,2)
    i2161_2() = i2161_1()[1]
    @test i2161_2() === 2.0
end

# issue #2169
let
    i2169(a::Array{T}) where {T} = typemin(T)
    @test invoke(i2169, Tuple{Array}, Int8[1]) === Int8(-128)
end

# issue #2365
mutable struct B2365{T}
     v::Union{T, Nothing}
end
@test B2365{Int}(nothing).v === nothing
@test B2365{Int}(0).v === 0

# issue #2352
let
    local Sum, n
    Sum=0.0; for n=1:2:10000
        Sum += -1/n + 1/(n+1)
    end
    @test Sum < -0.69
end

# issue #2509
struct Foo2509; foo::Int; end
@test Foo2509(1) != Foo2509(2)
@test Foo2509(42) == Foo2509(42)

# issue #2517
struct Foo2517; end
@test repr(Foo2517()) == "$(curmod_prefix)Foo2517()"
@test repr(Vector{Foo2517}(undef, 1)) == "$(curmod_prefix)Foo2517[Foo2517()]"
@test Foo2517() === Foo2517()

# issue #1474
mutable struct X1474{a,b} end
let
    local Y
    Y(::Type{X1474{A,B}}) where {A,B} = 1
    Y(::Type{X1474{A}}) where {A} = 2
    Y(::Type{X1474}) = 3
    @test Y(X1474) == 3
    @test Y(X1474{Int}) == 2
    @test Y(X1474{Int,AbstractString}) == 1
end

# issue #2562
mutable struct Node2562{T}
    value::T
    Node2562{T}(value::T) where T = new(value)
end
Node2562(value::T, args...) where {T} = Node2562{T}(value, args...)
makenode2562(value) = Node2562(value)
@test isa(Node2562(0), Node2562)
@test isa(makenode2562(0), Node2562)

# issue #2619
mutable struct I2619{T}
    v::T
    I2619{T}(v) where T = new(convert(T,v))
end
bad2619 = false
function i2619()
    global e2619 = try
        I2619{Float64}(0.0f)
        global bad2619 = true
    catch _e
        _e
    end
end
i2619()
@test !bad2619
@test isa(e2619,UndefVarError) && e2619.var === :f

# issue #2919
const Foo2919 = Int
mutable struct Baz2919; Foo2919::Foo2919; end
@test Baz2919(3).Foo2919 === 3

# issue #2982
module M2982
abstract type U end
macro bad(Y)
    quote
        mutable struct $(esc(Y)) <: U
        end
    end
end
export @bad
end

@M2982.bad(T2982)
@test T2982.super === M2982.U

# issue #3221
let x = fill(nothing, 1)
    @test_throws MethodError x[1] = 1
end

# issue #3220
function x3220()
    a = [1]
    a::Vector{Int} += [1]
end
@test x3220() == [2]

# issue #3471
function f3471(y)
    convert(Vector{typeof(y[1])}, y)
end
@test isa(f3471(Any[1.0,2.0]), Vector{Float64})

# issue #3729
A3729{B} = Vector{Vector{B}}
C3729{D} = Vector{Vector{D}}
@test Vector{Vector{Int}} === A3729{Int} === C3729{Int}

# issue #3789
x3789 = 0
while(all([false for idx in 1:10]))
    global x3789 = 1
end
@test x3789 == 0

# issue #3852
function f3852()
    local x
    for i = 1:10
        x = identity
    end
    x("hi")
end
@test f3852() == "hi"

# issue #3821
function f3821()
    p = []
    [x for x in p]
end
@test isa(f3821(), Array)

# issue #4075
struct Foo4075
    x::Int64
    y::Float64
end

function foo4075(f::Foo4075, s::Symbol)
    x = getfield(f,s)
    GC.gc()
    x
end

@test isa(foo4075(Foo4075(Int64(1),2.0),:y), Float64)
# very likely to segfault the second time if this is broken
@test isa(foo4075(Foo4075(Int64(1),2.0),:y), Float64)

# issue #3167
let
    function foo(x)
        ret=Vector{typeof(x[1])}(undef, length(x))
        for j = 1:length(x)
            ret[j] = x[j]
        end
        return ret
    end
    x = Vector{Union{Dict{Int64,AbstractString},Array{Int64,3},Number,AbstractString,Nothing}}(undef, 3)
    x[1] = 1.0
    x[2] = 2.0
    x[3] = 3.0
    @test foo(x) == [1.0, 2.0, 3.0]
end

# issue #4115
mutable struct Foo4115 end
const Foo4115s = NTuple{3, Union{Foo4115, Type{Foo4115}}}
baz4115(x::Foo4115s) = x
let t = (Foo4115, Foo4115, Foo4115())
    @test_throws MethodError baz4115(t)
end

# issue #4129
mutable struct Foo4129; end

abstract type Bar4129 end

mutable struct Bar41291 <: Bar4129
    f::Foo4129
end
mutable struct Bar41292 <: Bar4129
    f::Foo4129
end

mutable struct Baz4129
    b::Bar4129
end

foo4129(a::Baz4129,c::Foo4129,b::Bar4129,@nospecialize(x),y) = (a,b,c,x,y)
foo4129(a::Baz4129,b::Bar41291,args...) = foo4129(a,b.f,b,args...)
foo4129(a::Baz4129,b::Bar41292,args...) = foo4129(a,b.f,b,args...)
foo4129(a::Baz4129,args...)         = foo4129(a,a.b,args...)

@test isa(foo4129(Baz4129(Bar41291(Foo4129())),1,2), Tuple{Baz4129,Bar4129,Foo4129,Int,Int})

# issue #4141
mutable struct Vertex4141{N,T}; end
mutable struct Face4141{V}; end
mutable struct Hull4141{F<:Face4141}; end

g4141(N,T) = Hull4141{Face4141{Vertex4141{N,T}}}()
@test isa(g4141(4,Int), Hull4141{Face4141{Vertex4141{4,Int}}})

# issue #4154
mutable struct MyType4154{T}
    a1::T
    a2
end

foo4154(x) = MyType4154(x, [])
h4154() = typeof(foo4154(rand(2,2,2)))
g4154() = typeof(foo4154(rand(2,2,2,2,2,2,2,2,2)))

@test h4154() === MyType4154{Array{Float64,3}}
@test g4154() === MyType4154{Array{Float64,9}}

# issue #4208
mutable struct a4208
    a4208
end
@test isa(a4208(5),a4208)
mutable struct b4208
    b4208() = (local b4208=1;new())
end
@test isa(b4208(),b4208)

# make sure convert_default error isn't swallowed by typeof()
convert_default_should_fail_here() = similar([1],typeof(zero(typeof(rand(2,2)))))
@test_throws MethodError convert_default_should_fail_here()

# issue #4343
@test_throws ErrorException Array{Float64}{Int, 2}

mutable struct Foo4376{T}
    x
    Foo4376{T}(x::T) where T = new(x)
    Foo4376{T}(a::Foo4376{Int}) where T = new(a.x)
end

@test isa(Foo4376{Float32}(Foo4376{Int}(2)), Foo4376{Float32})

mutable struct _0_test_ctor_syntax_
    _0_test_ctor_syntax_(files::Vector{T},step) where {T<:AbstractString} = 0
end

# issue #4413
mutable struct A4413 end
mutable struct B4413 end
mutable struct C4413 end
f4413(::Union{A4413, B4413, C4413}) = "ABC"
f4413(::Union{A4413, B4413}) = "AB"
g4413(::Union{A4413, C4413}) = "AC"
g4413(::Union{A4413, B4413, C4413}) = "ABC"

@test f4413(A4413()) == "AB" && f4413(B4413()) == "AB"
@test g4413(A4413()) == "AC" && g4413(C4413()) == "AC"

# issue #4482
# what happens here: the method cache logic wants to widen the type of a
# tuple argument, but it shouldn't do that for an argument that a static
# parameter depends on.
f4482(x::T) where {T} = T
@test f4482((Ptr,Ptr)) === Tuple{UnionAll,UnionAll}
@test f4482((Ptr,))    === Tuple{UnionAll,}

# issue #4486
try
    # note: this test expression must run at the top level,
    # in the interpreter.
    (function() end)(1)
    # should throw an argument count error
    @test false
catch
end

# issue #4526
f4526(x) = isa(x.a, Nothing)
@test_throws ErrorException f4526(1)
@test_throws ErrorException f4526(im)
@test_throws ErrorException f4526(1+2im)

# issue #4528
function f4528(A, B)
    if A
        reinterpret(UInt64, B)
    end
end
@test f4528(false, Int32(12)) === nothing
@test_throws ErrorException f4528(true, Int32(12))

# issue #4518
f4518(x, y::Union{Int32,Int64}) = 0
f4518(x::String, y::Union{Int32,Int64}) = 1
@test f4518("",1) == 1

# issue #4645
i4645(x) = (println(zz); zz = x; zz)
@test_throws UndefVarError i4645(4)

# more undef var errors
function test_undef_var_9898(a)
    a1 = a1
    a
end
@test_throws UndefVarError test_undef_var_9898(1)

# issue #4505
let
    g4505(::X) where {X} = 0
    @test g4505(0) == 0
end
@test !@isdefined g4505
@test !isdefined(@__MODULE__, :g4505)

# issue #4681
# ccall should error if convert() returns something of the wrong type
mutable struct Z4681
    x::Ptr{Cvoid}
    Z4681() = new(C_NULL)
end
Base.unsafe_convert(::Type{Ptr{Z4681}},b::Z4681) = b.x
@test_throws TypeError ccall(:printf,Int,(Ptr{UInt8},Ptr{Z4681}),"",Z4681())

# issue #4479
f4479(::Real,c) = 1
f4479(::Int, ::Int, ::Bool) = 2
f4479(::Int, x, a...) = 0
@test f4479(1,1,true) == 2

# issue #4688
a4688(y) = "should be unreachable by calling b"
b4688(y) = "not an Int"
begin
    a4688(y::Int) = "an Int"
    let x = true
        global b4688(y::Int) = x == true ? a4688(y) : a4688(y)
    end
end
@test b4688(1) == "an Int"

# issue #4731
mutable struct SIQ{A,B} <: Number
    x::A
end
import Base: promote_rule
promote_rule(A::Type{SIQ{T,T2}},B::Type{SIQ{S,S2}}) where {T,T2,S,S2} = SIQ{promote_type(T,S)}
@test promote_type(SIQ{Int},SIQ{Float64}) == SIQ
f4731(x::T...) where {T} = ""
f4731(x...) = 0
g4731() = f4731()
@test f4731() == ""
@test g4731() == ""

# issue #4675
f4675(x::StridedArray...) = 1
f4675(x::StridedArray{T}...) where {T} = 2
@test f4675(zeros(50,50), zeros(50,50)) == 2
g4675(x::StridedArray{T}...) where {T} = 2
g4675(x::StridedArray...) = 1
@test g4675(zeros(50,50), zeros(50,50)) == 2

# issue #4771
module Lib4771
export @make_closure
macro make_closure()
    quote
        f = (x)->1
    end
end
end # module
@test (Lib4771.@make_closure)(0) == 1

# issue #4805
abstract type IT4805{N, T} end

let
    test0(::Type{IT4805{1, T}}, x) where {T <: Int64} = x
    test1() = test0(IT4805{1, Int64}, 1)
    test2() = test0(IT4805{1+0, Int64}, 1)
    test3(n) = test0(IT4805{n, Int64}, 1)

    @test test1() == 1
    @test test2() == 1
    @test test3(1) == 1
    @test_throws MethodError test3(2)
end

# issue #4873
macro myassert4873(ex)
    :($ex ? nothing : error("Assertion failed: ", $(string(ex))))
end
x4873 = 1
@myassert4873 (x -> x)(x4873) == 1

# issue from IRC
function invalid_tupleref()
    A = (1, "2", 3.0)
    try
        return A[0]
    catch
        return true
    end
end
@test invalid_tupleref()==true

# issue #5150
f5150(T) = Vector{Rational{T}}(undef, 1)
@test typeof(f5150(Int)) === Vector{Rational{Int}}


# issue #5165
primitive type T5165{S} 64 end
make_t(x::Int64) = Core.Intrinsics.bitcast(T5165{Nothing}, x)
xs5165 = T5165[make_t(Int64(1))]
b5165 = IOBuffer()
for x in xs5165
    println(b5165, x)   # segfaulted
end

# support tuples as type parameters

mutable struct TupleParam{P}
    x::Bool
end

function tupledispatch(a::TupleParam{(1,:a)})
    a.x
end

# tuples can be used as type params
let t1 = TupleParam{(1,:a)}(true),
    t2 = TupleParam{(1,:b)}(true)

    # tuple type params can't contain invalid type params
    @test_throws TypeError t3 = TupleParam{(1,"nope")}(true)

    # dispatch works properly
    @test tupledispatch(t1) == true
    @test_throws MethodError tupledispatch(t2)
end

# issue #5254
f5254(::Type{T}, b::T) where {T} = 0
f5254(a, b) = 1
@test f5254(Bottom, 1) == 1

# evaluate arguments left-to-right, including assignments. issue #4990
let i = 0, x = 65
    @test (i, i+=1, i+=1) === (0, 1, 2)
    @test i == 2
    @test [x, x|=0x20] == [65, 97]
end

# issue #5312
let
    local x = 0
    global incr5312, foo5312
    incr5312() = (x+=1; nothing)
    foo5312() = (incr5312(),)
    @test foo5312() === (nothing,)
    @test x == 1
end

# issue #5319
cnvt(T, x) = convert_default(T, x, cnvt)
cnvt(::Type{Array{S, N}}, x::Array{T, N}) where {S, T, N} = convert(Array{S}, x)

function tighttypes!(adf)
    T = Bottom
    tt = Any[Int]
    for t in tt
        T = typejoin(T, t)
    end
    cnvt(Vector{T}, adf[1])
end

@test isequal(tighttypes!(Any[Any[1.0,2.0],]), [1,2])

# issue #5142
primitive type Int5142 64 end
function h5142(a::Bool)
    x=a ? (Int64(0),reinterpret(Int5142,Int64(0))) : (Int64(1),reinterpret(Int5142,Int64(1)))
    x[2]::Int5142
end
function h5142(a::Int)
    x=(Int64(0),reinterpret(Int5142,Int64(0)))
    x[a]::Int5142
end
h5142(true)
@test_throws TypeError h5142(1)
h5142(2)
f5142() = h5142(1)
try
    # try running this code in a different context that triggers the codegen
    # assertion `assert(isboxed || v.typ == typ)`.
    f5142()
catch
end

primitive type Int5142b 8 end
function h5142b(a::Int)
    x=((Int8(1),Int8(2)),(reinterpret(Int5142b,Int8(3)),reinterpret(Int5142b,Int8(4))))
    x[a]::Tuple{Int8,Int8}
end
h5142b(1)
@test_throws TypeError h5142b(2)

# accessing bits tuples of structs
function test_bits_tuples()
    a = (complex(1,2),complex(1,3));s=0
    for i=1:10
        s += a[rand(1:2)]
    end
    s
end
@test real(test_bits_tuples()) == 10

# issue #5374
mutable struct FileObj5374
    io::IO
end
function read_file5374(fileobj)
    read(fileobj.io, Float32)
end
@test isa(read_file5374(FileObj5374(IOBuffer(UInt8[0,0,0,0]))), Float32)

# issue #5457
function f5457(obj_ptr::Ptr{Float64}, f)
    new_obj = convert(Float64, f(1.0))
    unsafe_store!(obj_ptr, new_obj)
    return Int32(1)
end
let
    a = [1.0]
    f5457(pointer(a,1), sin)
end

# issue #5584
# this is an intermittent memory bug, but this code is very likely to trigger it
mapshape_5584(s1::NTuple{N,Int}, s2::NTuple{N,Int}) where {N} =
    (s1 == s2 || error("Argument dimensions are not map-compatible."); s1)
function f5584()
    for i = 1:1000000
        a = rand(1:1000, 3)
        # the bug was a failure to root these tuples
        mapshape_5584(tuple(a...), tuple(a...))
    end
end
f5584()

# issue #5884

mutable struct Polygon5884{T<:Real}
    points::Vector{Complex{T}}
end

function test5884()
    star = Vector{Polygon5884}(undef, (3,))
    star[1] = Polygon5884([Complex(1.0,1.0)])
    p1 = star[1].points[1]
    @test p1 == Complex(1.0,1.0)
    @test p1.re == 1.0
    @test star[1].points[1].re == 1.0
end
test5884()

# issue #5924
let
    function test5924()
        func = function () end
        func
    end
    @test test5924()() === nothing
end

# issue #6031
macro m6031(x); x; end
@test @m6031([2,4,6])[3] == 6
@test (@m6031 [2,4,6])[2] == 4

# issue #6068
x6068 = 1
function test6068()
    local a
    while true
        a = x6068
        break
    end
    a + 1
end
@test test6068() == 2

# issue #6074
macro X6074()
    quote
        global x6074
        let x6074 = x6074
            x6074
        end
    end
end
x6074 = 6074
@test @X6074() == 6074

# issue #5536
test5536(a::Union{Real, AbstractArray}...) = "Splatting"
test5536(a::Union{Real, AbstractArray}) = "Non-splatting"
@test test5536(5) == "Non-splatting"

# multiline comments (#6139 and others raised in #6128) and embedded NUL chars (#10994)
@test 3 ==
    include_string(@__MODULE__, "1 + 2") ==
    include_string(@__MODULE__, "1 + #==# 2") ==
    include_string(@__MODULE__, "1 + #===# 2") ==
    include_string(@__MODULE__, "1 + #= #= blah =# =# 2") ==
    include_string(@__MODULE__, "1 + #= #= #= nested =# =# =# 2") ==
    include_string(@__MODULE__, "1 + #= \0 =# 2")
@test_throws LoadError include_string(@__MODULE__, "#=")
@test_throws LoadError include_string(@__MODULE__, "#= #= #= =# =# =")

# issue #6142
import Base: +
import LinearAlgebra: UniformScaling, I
mutable struct A6142 <: AbstractMatrix{Float64}; end
+(x::A6142, y::UniformScaling) = "UniformScaling method called"
+(x::A6142, y::AbstractArray) = "AbstractArray method called"
@test A6142() + I == "UniformScaling method called"
+(x::A6142, y::AbstractRange) = "AbstractRange method called" #16324 ambiguity

# issue #6175
function g6175(); print(""); (); end
g6175(i::Real, I...) = g6175(I...)
g6175(i, I...) = tuple(length(i), g6175(I...)...)
@test g6175(1:5) === (5,)

# issue #6242
f6242(x::NTuple{N,Int}) where {N} = (N==0 ? 1 : ntuple(n->x[n],N))
@test f6242(()) === 1

# issue #6292
let i = 0
    global g6292() = i+=1
end
@test g6292() == 1
@test g6292() == 2

# issue #6404
mutable struct type_2{T <: Integer, N} <: Number
    x::T
    type_2{T,N}(n::T) where {T<:Integer,N} = new(n)
end
mutable struct type_1{T <: Number} <: Number
    x::Vector{T}
    type_1{T}(x::Vector{T}) where T<:Number = new(x)
end
type_1(x::Vector{T}) where {T <: Number} = type_1{T}(x)
type_1(c::T) where {T <: Number} = type_1{T}([c])
Base.convert(::Type{type_1{T}}, x::S) where {T<:Number, S<:Number} = type_1(convert(T, x))
+(a::type_1{T}, b::type_1{T}) where {T <: Number} = a

function func1_6404(v1::Integer)
    e1 = type_1([type_2{Int,v1}(0)])
    e1+e1
end

@test isa(func1_6404(3), type_1)

# issue #5577
f5577(::Any) = false
f5577(::Type) = true
@test !f5577((Int,AbstractString,2))
@test !f5577(((Int,AbstractString),AbstractString))
@test f5577(Tuple{Tuple{Int,AbstractString},AbstractString})
@test f5577(Int)
@test !f5577(2)

# issue #6426
f6426(x,args...) = f6426(x,map(a->(isa(a,Type) ? Type{a} : typeof(a)), args))
f6426(x,t::Tuple{Vararg{Type}}) = string(t)
@test f6426(1, (1.,2.)) == "(Tuple{Float64,Float64},)"

# issue #6502
f6502() = convert(Tuple{Vararg{Int}}, (10,))
@test f6502() === (10,)
@test convert(Tuple{Bool,Vararg{Int}}, (true,10)) === (true,10)
@test convert(Tuple{Int,Vararg{Bool}}, (true,1,0)) === (1,true,false)

# issue #6611
function crc6611(spec)
    direcn = spec ? 1 : 2
    local remainder::blech
    ()->(remainder=1)
end
@test_throws UndefVarError crc6611(true)()

# issue #6634
function crc6634(spec)
    A = UInt
    remainder::A = 1
    function handler(append)
        remainder = append ? 1 : 2
    end
end
@test crc6634(0x1)(true) == 1
@test crc6634(0x1)(false) == 2

# issue #5876
module A5876
macro x()
    quote
        function $(esc(:f5876))(::Type{T}) where T
            T
        end
        42
    end
end
end

let
    local z = A5876.@x()
    @test z == 42
    @test f5876(Int) === Int
end

# issue #20524
macro m20524(ex)
    quote
        global f20524
        function f20524()
            $ex
        end
    end
end
@m20524 ((a,(b20524,c)) = (8,(1,5)); (a,b20524,c))
@test f20524() === (8,1,5)
@test !@isdefined b20524 # should not assign to a global

# issue #6387
primitive type Date6387{C} 64 end

mutable struct DateRange6387{C} <: AbstractRange{Date6387{C}}
end

mutable struct ObjMember
    member::DateRange6387
end

obj6387 = ObjMember(DateRange6387{Int64}())

function v6387(r::AbstractRange{T}) where T
    a = Vector{T}(undef, 1)
    a[1] = Core.Intrinsics.bitcast(Date6387{Int64}, Int64(1))
    return a
end

function day_in(obj::ObjMember)
    x = v6387(obj.member)
    @test isa(x, Vector{Date6387{Int64}})
    @test isa(x[1], Date6387{Int64})
end
day_in(obj6387)

# issue #6784
@test ndims(Array{Array{Float64}}(undef, 3,5)) == 2
@test ndims(Array{Array}(undef, 3,5)) == 2

# issue #6793
function segfault6793(;gamma=1)
    A = 1
    B = 1
    print()
    return
    -gamma
    nothing
end
@test segfault6793() === nothing

# issue #6896
g6896(x) = x::Int=x
@test g6896(5.0) === 5.0
f6896(x) = y::Int=x
@test f6896(5.0) === 5.0

# issue #6938
module M6938
macro mac()
    quote
        let
            y = 0
            y
        end
    end
end
end
@test @M6938.mac() == 0

# issue #7012
let x = zeros(2)
    x[1]::Float64 = 1
    @test x == [1.0, 0.0]
    @test_throws TypeError (x[1]::Int = 1)

    x[1]::Float64 += 1
    @test x == [2.0, 0.0]
    @test_throws TypeError (x[1]::Int += 1)
end

# issue #6980
abstract type A6980 end
mutable struct B6980 <: A6980 end
f6980(::Union{Int, Float64}, ::A6980) = false
f6980(::Union{Int, Float64}, ::B6980) = true
@test f6980(1, B6980())

# issue #7049
Maybe7049{T} = Union{T,Nothing}
function ttt7049(;init::Maybe7049{Union{AbstractString,Tuple{Int,Char}}} = nothing)
    string("init=", init)
end
@test ttt7049(init="a") == "init=a"

# issue #7074
let z(A::StridedMatrix{T}) where {T<:Union{Float64,Complex{Float64},Float32,Complex{Float32}}} = T,
    S = zeros(Complex,2,2)
    @test_throws MethodError z(S)
end

# issue #7062
f7062(::Type{Array{t}}  , ::Array{t,n}) where {t,n} = (t,n,1)
f7062(::Type{Array{t,n}}, ::Array{t,n}) where {t,n} = (t,n,2)
@test f7062(Array{Int,1}, [1,2,3]) === (Int,1,2)
@test f7062(Array{Int}  , [1,2,3]) === (Int,1,1)

# issue #7302
function test7302()
    t = [UInt64][1]
    convert(t, "5")
end
@test_throws MethodError test7302()

macro let_with_uninit()
    quote
        let x
            x = 1
            x+1
        end
    end
end

@test @let_with_uninit() == 2

# issue #5154
let
    v = []
    for i=1:3, j=1:3
        push!(v, (i, j))
        i == 1 && j == 2 && break
    end
    @test v == Any[(1,1), (1,2)]
end

# addition of ¬ (\neg) parsing
const (¬) = !
@test ¬false

# issue #7652
mutable struct A7652
    a :: Int
end
a7652 = A7652(0)
t_a7652 = A7652
f7652() = fieldtype(t_a7652, :a) <: Int
@test f7652() == (fieldtype(A7652, :a) <: Int) == true

g7652() = fieldtype(DataType, :types)
@test g7652() == fieldtype(DataType, :types) == Core.SimpleVector
@test fieldtype(t_a7652, 1) == Int

h7652() = setfield!(a7652, 1, 2)
@test h7652() === 2
@test a7652.a === 2

i7652() = Base.setproperty!(a7652, :a, 3.0)
@test i7652() === 3
@test a7652.a === 3

# issue #7679
@test map(f->f(), Any[ ()->i for i=1:3 ]) == Any[1,2,3]

# issue 7897
function issue7897!(data, arr)
    data = reinterpret(UInt32, data)
    a = arr[1]
end

let
    a = fill(0x01, 10)
    sa = view(a, 4:6)
    # This can throw an error, but shouldn't segfault
    try
        issue7897!(sa, zeros(10))
    catch
    end
end

# issue #7582
aₜ = "a variable using Unicode 6"

struct My8156{A, B}
    a::A
    b::B
end
let m = My8156(nothing, 1)
    @test sizeof(m) == sizeof(1)
    @test m.a === nothing
    @test m.b === 1
end

# issue #8184
struct Foo8184
    x::Nothing
    y::Nothing
    z::Float64
end
let f = Foo8184(nothing,nothing,1.0)
    g(x) = x.z
    @test g(f) === 1.0
end

# issue #8213
@test map((x...)->x,(1,2),(3,4),(5,6)) === ((1,3,5),(2,4,6))

# issue #8338
let ex = Expr(:(=), :(f8338(x;y=4)), :(x*y))
    eval(ex)
    @test f8338(2) == 8
end

# call overloading (#2403)
(x::Int)(y::Int) = x + 3y
issue2403func(f) = f(7)
let x = 10
    @test x(3) == 19
    @test x((3,)...) == 19
    @test issue2403func(x) == 31
end
mutable struct Issue2403
    x
end
(i::Issue2403)(y) = i.x + 2y
let x = Issue2403(20)
    @test x(3) == 26
    @test issue2403func(x) == 34
end

# issue #8798
let
    npy_typestrs = Dict("b1"=>Bool,
                        "i1"=>Int8,      "u1"=>UInt8,
                        "i2"=>Int16,     "u2"=>UInt16,
                        "i4"=>Int32,     "u4"=>UInt32,
                        "i8"=>Int64,     "u8"=>UInt64)
    sizeof_lookup() = sizeof(npy_typestrs["i8"])
    @test sizeof_lookup() == 8
end

# issue #8978
module I8978
y = 1
g() = f(y)
f(x) = 2
f(x::Int) = 3.0
module II8978
function callf(f)
    try
        f()
    finally
    end
end
end
h(f) = II8978.callf() do
    local x
    for i = 1:1
        x = g()+f
    end
    x
end
end

@test I8978.h(4) === 7.0

# issue #9134
function f9134()
    ii = zeros(Int32, 1)
    let i
        ii[1] = i
    end
end
@test_throws UndefVarError f9134()

# issue #9475
module I9475
    arr = Vector{Any}(undef, 1)
    @eval @eval $arr[1] = 1
end

# issue #9520
f9520a(::Any, ::Any, args...) = 15
f9520b(::Any, ::Any, ::Any, args...) = 23
f9520c(::Any, ::Any, ::Any, ::Any, ::Any, ::Any, args...) = 46
@test invoke(f9520a, Tuple{Any, Any}, 1, 2) == 15
@test invoke(f9520a, Tuple{Any, Any, Any}, 1, 2, 3) == 15
@test invoke(f9520b, Tuple{Any, Any, Any}, 1, 2, 3) == 23
@test invoke(f9520b, Tuple{Any, Any, Any, Any, Any, Any}, 1, 2, 3, 4, 5, 6) == 23
@test invoke(f9520c, Tuple{Any, Any, Any, Any, Any, Any}, 1, 2, 3, 4, 5, 6) == 46
@test invoke(f9520c, Tuple{Any, Any, Any, Any, Any, Any, Any}, 1, 2, 3, 4, 5, 6, 7) == 46

# issue #24460
f24460(x, y) = 1
f24460(x::T, y::T) where {T} = 2.0
f24460(x::Int, y::Int) = "3"
@test f24460(1, 2) === "3"
@test invoke(f24460, Tuple{T,T} where T, 1, 2) === 2.0
const T24460 = Tuple{T,T} where T
g24460() = invoke(f24460, T24460, 1, 2)
@test @inferred(g24460()) === 2.0

call_lambda1() = (()->x)(1)
call_lambda2() = ((x)->x)()
call_lambda3() = ((x)->x)(1,2)
call_lambda4() = ((x,y...)->x)()
@test_throws MethodError call_lambda1()
@test_throws MethodError call_lambda2()
@test_throws MethodError call_lambda3()
@test_throws MethodError call_lambda4()
call_lambda5() = ((x...)->x)()
call_lambda6() = ((x...)->x)(1)
call_lambda7() = ((x...)->x)(1,2)
@test call_lambda5() == ()
@test call_lambda6() == (1,)
@test call_lambda7() == (1,2)

# jl_new_bits testing
let x = [1,2,3]
    @test ccall(:jl_new_bits, Any, (Any,Ptr{Cvoid},), Int, x) === 1
    @test ccall(:jl_new_bits, Any, (Any,Ptr{Cvoid},), Complex{Int}, x) === 1+2im
    @test ccall(:jl_new_bits, Any, (Any,Ptr{Cvoid},), NTuple{3,Int}, x) === (1,2,3)
    @test ccall(:jl_new_bits, Any, (Any,Ptr{Cvoid},), Tuple{Int,Int,Int}, x) === (1,2,3)
    @test (ccall(:jl_new_bits, Any, (Any,Ptr{Cvoid},), Tuple{Int16,Tuple{Cvoid},Int8,Tuple{},Int,Cvoid,Int}, x)::Tuple)[[2,4,5,6,7]] === ((nothing,),(),2,nothing,3)
end

let
    # Exception frame automatically restores sigatomic counter.
    Base.sigatomic_begin()
    @test_throws ErrorException begin
        for i = 1:2
            Base.sigatomic_end()
        end
    end
    Base.sigatomic_end()
end

# pull request #9534
@test_throws BoundsError((1, 2), 3) begin; a, b, c = 1, 2; end
let a = []
    @test try; a[]; catch ex; (ex::BoundsError).a === a && ex.i == (); end
    @test_throws BoundsError(a, (1, 2)) a[1, 2]
    @test_throws BoundsError(a, (10,)) a[10]
end
f9534a() = (a = 1 + 2im; getfield(a, -100))
f9534a(x) = (a = 1 + 2im; getfield(a, x))
@test_throws BoundsError(1 + 2im, -100) f9534a()
@test_throws BoundsError(1 + 2im, 3) f9534a(3)
f9534b() = (a = (1, 2., ""); a[5])
f9534b(x) = (a = (1, 2., ""); a[x])
@test_throws BoundsError((1, 2., ""), 5) f9534b()
@test_throws BoundsError((1, 2., ""), 4) f9534b(4)
f9534c() = (a = (1, 2.); a[3])
f9534c(x) = (a = (1, 2.); a[x])
@test_throws BoundsError((1, 2.), 3) f9534c()
@test_throws BoundsError((1, 2.), 0) f9534c(0)
f9534d() = (a = (1, 2, 4, 6, 7); a[7])
f9534d(x) = (a = (1, 2, 4, 6, 7); a[x])
@test_throws BoundsError((1, 2, 4, 6, 7), 7) f9534d()
@test_throws BoundsError((1, 2, 4, 6, 7), -1) f9534d(-1)
let a = IOBuffer()
    f9534e(x) = setfield!(a, x, 3)
    @test_throws BoundsError(a, -2) f9534e(-2)
    f9534f() = getfield(a, -2)
    f9534f(x) = getfield(a, x)
    @test_throws BoundsError(a, -2) f9534f()
    @test_throws BoundsError(a, typemin(Int) + 2) f9534f(typemin(Int) + 2)
end
x9634 = 3
@test_throws BoundsError(1 + 2im, 3) getfield(1 + 2im, x9634)
@test try; throw(BoundsError()); catch ex; !isdefined((ex::BoundsError), :a) && !isdefined((ex::BoundsError), :i); end
@test try; throw(BoundsError(Int)); catch ex; (ex::BoundsError).a == Int && !isdefined((ex::BoundsError), :i); end
@test_throws BoundsError(Int, typemin(Int)) throw(BoundsError(Int, typemin(Int)))
@test_throws BoundsError(Int, (:a,)) throw(BoundsError(Int, (:a,)))
f9534g(a, b, c...) = c[0]
@test_throws BoundsError((3, 4, 5, 6), 0) f9534g(1, 2, 3, 4, 5, 6)
f9534h(a, b, c...) = c[a]
@test f9534h(4, 2, 3, 4, 5, 6) == 6
@test_throws BoundsError((3, 4, 5, 6), 5) f9534h(5, 2, 3, 4, 5, 6)

# issue #7978, comment 332352438
f7978a() = 1
@test_throws BoundsError(1, 2) begin; a, b = f7978a(); end
f7978b() = 1, 2
@test_throws BoundsError((1, 2), 3) begin; a, b, c = f7978b(); end

# issue #9535
counter9535 = 0
f9535() = (global counter9535; counter9535 += 1; counter9535)
g9535() = (f9535(),f9535())
@test g9535() == (1,2)
@test g9535() == (3,4)

# weak references
mutable struct Obj; x; end
@testset "weak references" begin
    @noinline function mk_wr(r, wr)
        x = Obj(1)
        push!(r, x)
        push!(wr, WeakRef(x))
    end
    test_wr(r,wr) = @test r[1] == wr[1].value
    function test_wr()
        ref = []
        wref = []
        mk_wr(ref, wref)
        test_wr(ref, wref)
        GC.gc()
        test_wr(ref, wref)
        pop!(ref)
        GC.gc()
        @test wref[1].value === nothing
    end
    test_wr()
end

# issue #9947
function f9947()
    if 1 == 0
        1
    else
        min(UInt128(2),1)
    end
end
@test f9947() == UInt128(1)

#issue #9835
module M9835
    using Test
    mutable struct A end; mutable struct B end
    f() = (isa(A(), A) ? A : B)()
    @test isa(f(), A)
end

#issue #10163
let a = :(()), b = :(())
    @test a.args !== b.args
end

# issue caused by commit 189b00aef0376d1a998d36115cd11b17464d26ce and worked around
# by commit 24c64b86bd4e793dbfe9d85c067dc0579b320d14
let
    g(x::T...) where {T} = T
    g(x...) = 0
    @test g((),Int) == 0
    @test g((),()) == Tuple{}
end

# TODO: hopefully this issue is obsolete after the tuple type change
## issue #8631
#f8631(::(Type, Type...), ::(Any, Any...)) = 1
#f8631{T}(::Type{(T...)}, x::Tuple) = 2
#@test length(methods(f8631, ((Type, Type...), (Any, Any...)))) == 2

# issue caused by 8d0037cb377257fc4232c8526b12337dd7bdf0a7
args8d003 = (:x, :y)
@test eval(:(:(f($(($args8d003)...))))) == :(f(x,y))
x8d003 = Any[:y8d003]
y8d003 = 777
@test eval(:(string(:(f($($(x8d003...))))))) == "f(777)"

# issue #9378
abstract type Foo9378{T,S} end
struct B9378{T} end
FooB9378{T} = Foo9378{T,B9378}
struct CFoo9378 <: FooB9378{Float64} end
@test isa(CFoo9378(),FooB9378)

# issue #10281
const N10281 = 1000
@test if false
    for i in 1:N10281
    end
end === nothing


# issue #10221
module GCbrokentype
using InteractiveUtils
OLD_STDOUT = stdout
fname = tempname()
file = open(fname, "w")
redirect_stdout(file)
versioninfo()
try
    mutable struct Foo{T}
        val::Bar{T}
    end
catch
end
GC.gc()
redirect_stdout(OLD_STDOUT)
close(file)
rm(fname)
end

# issue #10373
f10373(x) = x
g10373(x) = x
mutable struct newtype10373
end
let f
    for f in (f10373,g10373)
        (::typeof(f))(x::newtype10373) = println("$f")
    end
end
for m in methods(f10373)
    @test m.name == :f10373
end
for m in methods(g10373)
    @test m.name == :g10373
end

# issue #7221
f7221(::T) where {T<:Number} = 1
f7221(::BitArray) = 2
f7221(::AbstractVecOrMat) = 3
@test f7221(trues(1)) == 2

# issue #10570
struct Array_512_Uint8
    d1::UInt8
    d2::UInt8
    d3::UInt8
    d4::UInt8
    d5::UInt8
    d6::UInt8
    d7::UInt8
    d8::UInt8
    d9::UInt8
    d10::UInt8
    d11::UInt8
    d12::UInt8
    d13::UInt8
    d14::UInt8
    d15::UInt8
    d16::UInt8
    d17::UInt8
    d18::UInt8
    d19::UInt8
    d20::UInt8
    d21::UInt8
    d22::UInt8
    d23::UInt8
    d24::UInt8
    d25::UInt8
    d26::UInt8
    d27::UInt8
    d28::UInt8
    d29::UInt8
    d30::UInt8
    d31::UInt8
    d32::UInt8
    d33::UInt8
    d34::UInt8
    d35::UInt8
    d36::UInt8
    d37::UInt8
    d38::UInt8
    d39::UInt8
    d40::UInt8
    d41::UInt8
    d42::UInt8
    d43::UInt8
    d44::UInt8
    d45::UInt8
    d46::UInt8
    d47::UInt8
    d48::UInt8
    d49::UInt8
    d50::UInt8
    d51::UInt8
    d52::UInt8
    d53::UInt8
    d54::UInt8
    d55::UInt8
    d56::UInt8
    d57::UInt8
    d58::UInt8
    d59::UInt8
    d60::UInt8
    d61::UInt8
    d62::UInt8
    d63::UInt8
    d64::UInt8
    d65::UInt8
    d66::UInt8
    d67::UInt8
    d68::UInt8
    d69::UInt8
    d70::UInt8
    d71::UInt8
    d72::UInt8
    d73::UInt8
    d74::UInt8
    d75::UInt8
    d76::UInt8
    d77::UInt8
    d78::UInt8
    d79::UInt8
    d80::UInt8
    d81::UInt8
    d82::UInt8
    d83::UInt8
    d84::UInt8
    d85::UInt8
    d86::UInt8
    d87::UInt8
    d88::UInt8
    d89::UInt8
    d90::UInt8
    d91::UInt8
    d92::UInt8
    d93::UInt8
    d94::UInt8
    d95::UInt8
    d96::UInt8
    d97::UInt8
    d98::UInt8
    d99::UInt8
    d100::UInt8
    d101::UInt8
    d102::UInt8
    d103::UInt8
    d104::UInt8
    d105::UInt8
    d106::UInt8
    d107::UInt8
    d108::UInt8
    d109::UInt8
    d110::UInt8
    d111::UInt8
    d112::UInt8
    d113::UInt8
    d114::UInt8
    d115::UInt8
    d116::UInt8
    d117::UInt8
    d118::UInt8
    d119::UInt8
    d120::UInt8
    d121::UInt8
    d122::UInt8
    d123::UInt8
    d124::UInt8
    d125::UInt8
    d126::UInt8
    d127::UInt8
    d128::UInt8
    d129::UInt8
    d130::UInt8
    d131::UInt8
    d132::UInt8
    d133::UInt8
    d134::UInt8
    d135::UInt8
    d136::UInt8
    d137::UInt8
    d138::UInt8
    d139::UInt8
    d140::UInt8
    d141::UInt8
    d142::UInt8
    d143::UInt8
    d144::UInt8
    d145::UInt8
    d146::UInt8
    d147::UInt8
    d148::UInt8
    d149::UInt8
    d150::UInt8
    d151::UInt8
    d152::UInt8
    d153::UInt8
    d154::UInt8
    d155::UInt8
    d156::UInt8
    d157::UInt8
    d158::UInt8
    d159::UInt8
    d160::UInt8
    d161::UInt8
    d162::UInt8
    d163::UInt8
    d164::UInt8
    d165::UInt8
    d166::UInt8
    d167::UInt8
    d168::UInt8
    d169::UInt8
    d170::UInt8
    d171::UInt8
    d172::UInt8
    d173::UInt8
    d174::UInt8
    d175::UInt8
    d176::UInt8
    d177::UInt8
    d178::UInt8
    d179::UInt8
    d180::UInt8
    d181::UInt8
    d182::UInt8
    d183::UInt8
    d184::UInt8
    d185::UInt8
    d186::UInt8
    d187::UInt8
    d188::UInt8
    d189::UInt8
    d190::UInt8
    d191::UInt8
    d192::UInt8
    d193::UInt8
    d194::UInt8
    d195::UInt8
    d196::UInt8
    d197::UInt8
    d198::UInt8
    d199::UInt8
    d200::UInt8
    d201::UInt8
    d202::UInt8
    d203::UInt8
    d204::UInt8
    d205::UInt8
    d206::UInt8
    d207::UInt8
    d208::UInt8
    d209::UInt8
    d210::UInt8
    d211::UInt8
    d212::UInt8
    d213::UInt8
    d214::UInt8
    d215::UInt8
    d216::UInt8
    d217::UInt8
    d218::UInt8
    d219::UInt8
    d220::UInt8
    d221::UInt8
    d222::UInt8
    d223::UInt8
    d224::UInt8
    d225::UInt8
    d226::UInt8
    d227::UInt8
    d228::UInt8
    d229::UInt8
    d230::UInt8
    d231::UInt8
    d232::UInt8
    d233::UInt8
    d234::UInt8
    d235::UInt8
    d236::UInt8
    d237::UInt8
    d238::UInt8
    d239::UInt8
    d240::UInt8
    d241::UInt8
    d242::UInt8
    d243::UInt8
    d244::UInt8
    d245::UInt8
    d246::UInt8
    d247::UInt8
    d248::UInt8
    d249::UInt8
    d250::UInt8
    d251::UInt8
    d252::UInt8
    d253::UInt8
    d254::UInt8
    d255::UInt8
    d256::UInt8
    d257::UInt8
    d258::UInt8
    d259::UInt8
    d260::UInt8
    d261::UInt8
    d262::UInt8
    d263::UInt8
    d264::UInt8
    d265::UInt8
    d266::UInt8
    d267::UInt8
    d268::UInt8
    d269::UInt8
    d270::UInt8
    d271::UInt8
    d272::UInt8
    d273::UInt8
    d274::UInt8
    d275::UInt8
    d276::UInt8
    d277::UInt8
    d278::UInt8
    d279::UInt8
    d280::UInt8
    d281::UInt8
    d282::UInt8
    d283::UInt8
    d284::UInt8
    d285::UInt8
    d286::UInt8
    d287::UInt8
    d288::UInt8
    d289::UInt8
    d290::UInt8
    d291::UInt8
    d292::UInt8
    d293::UInt8
    d294::UInt8
    d295::UInt8
    d296::UInt8
    d297::UInt8
    d298::UInt8
    d299::UInt8
    d300::UInt8
    d301::UInt8
    d302::UInt8
    d303::UInt8
    d304::UInt8
    d305::UInt8
    d306::UInt8
    d307::UInt8
    d308::UInt8
    d309::UInt8
    d310::UInt8
    d311::UInt8
    d312::UInt8
    d313::UInt8
    d314::UInt8
    d315::UInt8
    d316::UInt8
    d317::UInt8
    d318::UInt8
    d319::UInt8
    d320::UInt8
    d321::UInt8
    d322::UInt8
    d323::UInt8
    d324::UInt8
    d325::UInt8
    d326::UInt8
    d327::UInt8
    d328::UInt8
    d329::UInt8
    d330::UInt8
    d331::UInt8
    d332::UInt8
    d333::UInt8
    d334::UInt8
    d335::UInt8
    d336::UInt8
    d337::UInt8
    d338::UInt8
    d339::UInt8
    d340::UInt8
    d341::UInt8
    d342::UInt8
    d343::UInt8
    d344::UInt8
    d345::UInt8
    d346::UInt8
    d347::UInt8
    d348::UInt8
    d349::UInt8
    d350::UInt8
    d351::UInt8
    d352::UInt8
    d353::UInt8
    d354::UInt8
    d355::UInt8
    d356::UInt8
    d357::UInt8
    d358::UInt8
    d359::UInt8
    d360::UInt8
    d361::UInt8
    d362::UInt8
    d363::UInt8
    d364::UInt8
    d365::UInt8
    d366::UInt8
    d367::UInt8
    d368::UInt8
    d369::UInt8
    d370::UInt8
    d371::UInt8
    d372::UInt8
    d373::UInt8
    d374::UInt8
    d375::UInt8
    d376::UInt8
    d377::UInt8
    d378::UInt8
    d379::UInt8
    d380::UInt8
    d381::UInt8
    d382::UInt8
    d383::UInt8
    d384::UInt8
    d385::UInt8
    d386::UInt8
    d387::UInt8
    d388::UInt8
    d389::UInt8
    d390::UInt8
    d391::UInt8
    d392::UInt8
    d393::UInt8
    d394::UInt8
    d395::UInt8
    d396::UInt8
    d397::UInt8
    d398::UInt8
    d399::UInt8
    d400::UInt8
    d401::UInt8
    d402::UInt8
    d403::UInt8
    d404::UInt8
    d405::UInt8
    d406::UInt8
    d407::UInt8
    d408::UInt8
    d409::UInt8
    d410::UInt8
    d411::UInt8
    d412::UInt8
    d413::UInt8
    d414::UInt8
    d415::UInt8
    d416::UInt8
    d417::UInt8
    d418::UInt8
    d419::UInt8
    d420::UInt8
    d421::UInt8
    d422::UInt8
    d423::UInt8
    d424::UInt8
    d425::UInt8
    d426::UInt8
    d427::UInt8
    d428::UInt8
    d429::UInt8
    d430::UInt8
    d431::UInt8
    d432::UInt8
    d433::UInt8
    d434::UInt8
    d435::UInt8
    d436::UInt8
    d437::UInt8
    d438::UInt8
    d439::UInt8
    d440::UInt8
    d441::UInt8
    d442::UInt8
    d443::UInt8
    d444::UInt8
    d445::UInt8
    d446::UInt8
    d447::UInt8
    d448::UInt8
    d449::UInt8
    d450::UInt8
    d451::UInt8
    d452::UInt8
    d453::UInt8
    d454::UInt8
    d455::UInt8
    d456::UInt8
    d457::UInt8
    d458::UInt8
    d459::UInt8
    d460::UInt8
    d461::UInt8
    d462::UInt8
    d463::UInt8
    d464::UInt8
    d465::UInt8
    d466::UInt8
    d467::UInt8
    d468::UInt8
    d469::UInt8
    d470::UInt8
    d471::UInt8
    d472::UInt8
    d473::UInt8
    d474::UInt8
    d475::UInt8
    d476::UInt8
    d477::UInt8
    d478::UInt8
    d479::UInt8
    d480::UInt8
    d481::UInt8
    d482::UInt8
    d483::UInt8
    d484::UInt8
    d485::UInt8
    d486::UInt8
    d487::UInt8
    d488::UInt8
    d489::UInt8
    d490::UInt8
    d491::UInt8
    d492::UInt8
    d493::UInt8
    d494::UInt8
    d495::UInt8
    d496::UInt8
    d497::UInt8
    d498::UInt8
    d499::UInt8
    d500::UInt8
    d501::UInt8
    d502::UInt8
    d503::UInt8
    d504::UInt8
    d505::UInt8
    d506::UInt8
    d507::UInt8
    d508::UInt8
    d509::UInt8
    d510::UInt8
    d511::UInt8
    d512::UInt8
end
GC.gc()

# issue #10867
@test collect(enumerate((Tuple,Int))) == [(1,Tuple), (2,Int)]
@test collect(enumerate((Tuple,3))) == [(1,Tuple), (2,3)]

# issue #10978
TupleType10978{T<:Tuple} = Type{T}
f10978(T::TupleType10978) = isa(T, TupleType10978)
@test f10978(Tuple{Int})

# issue #10995
#TupleType{T<:Tuple} = Type{T}
f10995(::Any) = (while false; end; nothing)
f10995(T::TupleType10978) = (while false; end; @assert isa(T, TupleType10978))
g10995(x) = f10995(typeof(x))
g10995((1, 2))
@test g10995(UInt8) === nothing

# issue #11149
@noinline f11149(a,b,args...) = (a,b,args...)
@test f11149(1,2,3) == invoke(f11149, Tuple{Int,Int,Int}, 1,2,3)

# issue #11357
function f11357()
    x = (1,2,3)
    i = (1,)
    x[i...]
end
@test f11357() === 1

# issue #11355
function f11355(sig::Type{T}) where T<:Tuple
    f11355(sig.parameters[1])
end
function f11355(arg::DataType)
    if arg <: Tuple
        return 200
    end
    return 100
end
let t = Tuple{Type{Vector{Int}}}
    @test f11355(t) == 100
    t = Tuple{Type{Dict{K} where K}}
    @test f11355(t) == 100
end

# issue #8283
function func8283 end
@test isa(func8283,Function)
@test_throws MethodError func8283()

# issue #11243
mutable struct Type11243{A, B}
    x::A
    y::B
end
let a = [Type11243(1,2), Type11243("a","b")]
    @test typeof(a) == Vector{Type11243}
    @test typeof(a) <: Vector{Type11243}
end

# issue #11065, #1571
function f11065()
    for i = 1:2
        if i == 1
            z = "z is defined"
        elseif i == 2
            print(z)
        end
    end
end
@test_throws UndefVarError f11065()

# issue #25724
a25724 = Any[]
for i = 1:3
    needX = false
    try
        X = X
        X[1] = X[1] + 1
    catch err
        needX = true
    end
    if needX
        X = [0]
    end
    push!(a25724, copy(X))
end
@test a25724 == [[0], [0], [0]]

# for loop iterator expression should be evaluated in outer scope
let
    for i in (local a = 1:2)
    end
    @test a == 1:2
end

# `for outer`
let
    function forouter()
        i = 1
        for outer i = 2:3
        end
        return i
    end
    @test forouter() == 3
end

@test_throws ErrorException("syntax: no outer local variable declaration exists for \"for outer\"") @eval function f()
    for outer i = 1:2
    end
end

# issue #11295
function f11295(x...)
    call = Expr(x...)
end
@test isa(f11295(:a,:b), Expr)

# issue #11675
struct T11675{T}
    x::T
    T11675{T}() where T = new()
end
let x = T11675{Union{}}()
    function f11675(x)
        x.x + 1
    end
    @test_throws UndefRefError f11675(x)
end

# issue #7864
module M7864
export x7864
x7864 = 1
end

@test_throws UndefVarError x7864
using .M7864
@test x7864 == 1

# issue #11715
f11715(x) = (x === Tuple{Any})
@test f11715(Tuple{Any})

# part of #11597
# make sure invalid, partly-constructed types don't end up in the cache
abstract type C11597{T<:Union{Nothing, Int}} end
mutable struct D11597{T} <: C11597{T} d::T end
@test_throws TypeError D11597(1.0)
@test_throws TypeError repr(D11597(1.0))

# issue #11772
@test_throws UndefRefError (Vector{Any}(undef, 5)...,)

# issue #11813
let a = UInt8[1, 107, 66, 88, 2, 99, 254, 13, 0, 0, 0, 0]
    u32 = UInt32[0x3]
    a[9:end] = reinterpret(UInt8, u32)
    p = pointer(a)
    @test (Int8(1),(Int8(2),Int32(3))) === unsafe_load(convert(Ptr{Tuple{Int8,Tuple{Int8,Int32}}},p))
    f11813(p) = (Int8(1),(Int8(2),Int32(3))) === unsafe_load(convert(Ptr{Tuple{Int8,Tuple{Int8,Int32}}},p))
    @test f11813(p) === true # redundant comparison test seems to make this test more reliable, don't remove
end
# issue #13037
let a = UInt8[0, 0, 0, 0, 0x66, 99, 254, 13, 0, 0, 0, 0]
    u32 = UInt32[0x3]
    a[1:4] = reinterpret(UInt8, u32)
    p = pointer(a)
    @test ((Int32(3),UInt8(0x66)),Int32(0)) === unsafe_load(convert(Ptr{Tuple{Tuple{Int32,UInt8},Int32}},p))
    f11813(p) = ((Int32(3),UInt8(0x66)),Int32(0)) === unsafe_load(convert(Ptr{Tuple{Tuple{Int32,UInt8},Int32}},p))
    @test f11813(p) === true # redundant comparison test seems to make this test more reliable, don't remove
end
let a = (1:1000...,),
    b = (1:1000...,)
    @test a == b
    @test a === b
    @test (a == b) === true
    @test (a === b) === true
end

# issue 11858
mutable struct Foo11858
    x::Float64
    Foo11858(x::Float64) = new(x)
end

mutable struct Bar11858
    x::Float64
    Bar11858(x::Float64) = new(x)
end

g11858(x::Float64) = x
f11858(a) = for Baz in a
    @eval (f::$Baz)(x) = f(float(x))
end
f11858(Any[Type{Foo11858}, Type{Bar11858}, typeof(g11858)])

@test g11858(1) == 1.0
@test Foo11858(1).x == 1.0
@test Bar11858(1).x == 1.0

# issue 11904
struct Nullable11904{T}
    value::T
    hasvalue::Bool
end
@noinline throw_error() = error()
foo11904(x::Int) = x
@inline function foo11904(x::Nullable11904{S}) where S
    if isbitstype(S)
        Nullable11904(foo11904(x.value), x.hasvalue)
    else
        throw_error()
    end
end

@test foo11904(Nullable11904(1, true)).hasvalue

# issue 11874
struct Foo11874
    x::Int
end

function bar11874(x)
    local y::Foo11874
    y = x
    nothing
end

Base.convert(::Type{Foo11874},x::Int) = float(x)

@test_throws TypeError bar11874(1)

# issue #9233
let
    try
        NTuple{Int, 1}
        @test false
    catch err
        @test isa(err, TypeError)
        @test err.func == :apply_type
        @test err.expected == Int
        @test err.got == Int
    end

    try
        NTuple{0x1, Int}
        @test false
    catch err
        @test isa(err, TypeError)
        @test err.func == :apply_type
        @test err.expected == Int
        @test err.got == 0x1
    end
end

# 11996
@test_throws ErrorException NTuple{-1, Int}
@test_throws TypeError Union{Int, 1}

mutable struct FooNTuple{N}
    z::Tuple{Integer, Vararg{Int, N}}
end
@test_throws ErrorException FooNTuple{-1}
@test_throws ErrorException FooNTuple{typemin(Int)}
@test_throws TypeError FooNTuple{0x01}
@test fieldtype(FooNTuple{0}, 1) == Tuple{Integer}

mutable struct FooTupleT{T}
    z::Tuple{Int, T, Int}
end
@test_throws TypeError FooTupleT{Vararg{Int, 2}}
@test fieldtype(FooTupleT{Int}, 1) == NTuple{3, Int}

@test Tuple{} === NTuple{0, Any}
@test Tuple{Int} === Tuple{Int, Vararg{Integer, 0}}

# issue #12003
using Dates
const DATE12003 = DateTime(1917,1,1)
failure12003(dt=DATE12003) = Dates.year(dt)
@test isa(failure12003(), Integer)

# issue #12023 Test error checking in primitive type
@test_throws ErrorException (@eval primitive type 0 SPJa12023 end)
@test_throws ErrorException (@eval primitive type 4294967312 SPJb12023 end)
@test_throws ErrorException (@eval primitive type -4294967280 SPJc12023 end)

# issue #12089
mutable struct A12089{K, N}
    sz::NTuple{N, Int}
    A12089{K,N}(sz::NTuple{N, Int}) where {K,N} = new(sz)
end
A12089{-1, 1}((1,))

# issue #12092
f12092(x::Int, y) = 0
f12092(x::Int,) = 1
f12092(x::Int, y::Int...) = 2
@test f12092(1) == 1

# issue #12063
# NOTE: should have > MAX_TUPLETYPE_LEN arguments
f12063(tt, g, p, c, b, v, cu::T, d::AbstractArray{T, 2}, ve) where {T} = 1
f12063(args...) = 2
g12063() = f12063(0, 0, 0, 0, 0, 0, 0.0, spzeros(0,0), Int[])
@test g12063() == 1

# issue #11587
mutable struct Sampler11587{N}
    clampedpos::Array{Int,2}
    buf::Array{Float64,N}
end
function Sampler11587()
    a = tuple(Any[32,32]...,)
    Sampler11587(zeros(Int,a), zeros(Float64,a))
end
@test isa(Sampler11587(), Sampler11587{2})

# issue #8010 - error when convert returns wrong type during new()
struct Vec8010{T}
    x::T
    y::T
end
Vec8010(a::AbstractVector) = Vec8010(ntuple(x->a[x],2)...)
Base.convert(::Type{Vec8010{T}},x::AbstractVector) where {T} = Vec8010(x)
Base.convert(::Type{Nothing},x::AbstractVector) = Vec8010(x)
struct MyType8010
     m::Vec8010{Float32}
end
struct MyType8010_ghost
     m::Nothing
end
@test_throws TypeError MyType8010([3.0;4.0])
@test_throws TypeError MyType8010_ghost([3.0;4.0])

module TestNewTypeError
using Test

struct A
end
struct B
    a::A
end
@eval function f1()
    # Emitting this direction is not recommended but it can come from `convert` that does not
    # return the correct type.
    $(Expr(:new, B, 1))
end
@eval function f2()
    a = $(Expr(:new, B, 1))
    a = a
    return nothing
end
@generated function f3()
    quote
        $(Expr(:new, B, 1))
        return nothing
    end
end
@test_throws TypeError f1()
@test_throws TypeError f2()
@test_throws TypeError f3()
@test_throws TypeError eval(Expr(:new, B, 1))

end

# don't allow redefining types if ninitialized changes
struct NInitializedTestType
    a
end

@test_throws ErrorException @eval struct NInitializedTestType
    a
    NInitializedTestType() = new()
end

# issue #12394
mutable struct Empty12394 end
let x = Vector{Empty12394}(undef, 1), y = [Empty12394()]
    @test_throws UndefRefError x==y
    @test_throws UndefRefError y==x
end

module TestRecursiveConstGlobalStructCtor
const x = (1,2)
const y = (x,(3,4))
f() = (x,y,(5,6))
end
@test TestRecursiveConstGlobalStructCtor.f() == ((1,2),((1,2),(3,4)),(5,6))

const const_array_int1 = Array{Int}
const const_array_int2 = Array{Int}
test_eq_array_int() = ===(const_array_int1, const_array_int2)
@test test_eq_array_int()

# objectid of haspadding field
struct HasPadding
    x::Bool
    y::Int
end
struct HasHasPadding
    x::HasPadding
end
let hashaspadding = Ref(HasHasPadding(HasPadding(true,1))),
    hashaspadding2 = Ref(HasHasPadding(HasPadding(true,1)))
    unsafe_store!(convert(Ptr{UInt8},pointer_from_objref(hashaspadding)), 0x12, 2)
    unsafe_store!(convert(Ptr{UInt8},pointer_from_objref(hashaspadding2)), 0x21, 2)
    @test objectid(hashaspadding[]) == objectid(hashaspadding2[])
end

# issue #12517
let x = (1,2)
    @eval f12517() = Val{$x}
    @test f12517() === Val{(1,2)}
end

# don't allow Vararg{} in Union{} type constructor
@test_throws TypeError Union{Int,Vararg{Int}}
@test_throws TypeError Union{Vararg{Int}}

# only allow Vararg{} in last position of Tuple{ }
@test_throws TypeError Tuple{Vararg{Int32},Int64,Float64}
@test_throws TypeError Tuple{Int64,Vararg{Int32},Float64}
@test_throws TypeError Array{Vararg}

# don't allow non-types in Union
@test_throws TypeError Union{1}
@test_throws TypeError Union{Int,0}
PossiblyInvalidUnion{T} = Union{T,Int}
@test_throws TypeError PossiblyInvalidUnion{1}

# issue #12569
@test Symbol("x") === Symbol("x")
@test split(string(gensym("abc")),'#')[3] == "abc"

# issue #13007
call13007(::Type{Array{T,N}}) where {T,N} = 0
call13007(::Type{Array}) = 1
@test length(Base._methods(call13007, Tuple{Type{x} where x<:Array}, 4, typemax(UInt))) == 2

# detecting cycles during type intersection, e.g. #1631
cycle_in_solve_tvar_constraints(::Type{Some{S}}, x::S) where {S} = 0
cycle_in_solve_tvar_constraints(::Type{T}, x::Val{T}) where {T} = 1
@test length(methods(cycle_in_solve_tvar_constraints)) == 2

# issue #12967
foo12967(x, @nospecialize y) = 1
TupleType12967{T<:Tuple} = Type{T}
foo12967(x, ::TupleType12967) = 2
@test foo12967(1, Int) == 1
@test foo12967(1, Tuple{}) == 2

# issue #13083
@test Nothing() === nothing

# issue discovered in #11973
for j = 1:1
    x = try
        error()
        2
    catch
        continue
    end
end

# PR 11888
struct A11888{T}
    a::NTuple{16,T}
end

B11888{T} = A11888{A11888{A11888{T}}}

@test sizeof(B11888{B11888{Int64}}) == (1 << 24) * 8

# issue #13175
struct EmptyImmutable13175 end
struct EmptyIIOtherField13175
    x::EmptyImmutable13175
    y::Float64
end
@test EmptyIIOtherField13175(EmptyImmutable13175(), 1.0) == EmptyIIOtherField13175(EmptyImmutable13175(), 1.0)
@test EmptyIIOtherField13175(EmptyImmutable13175(), 1.0) != EmptyIIOtherField13175(EmptyImmutable13175(), 2.0)

# issue 8932 (llvm return type legalizer error)
struct Vec3_8932
    x::Float32
    y::Float32
    z::Float32
end
f8932(a::Vec3_8932, b::Vec3_8932) = Vec3_8932(a.x % b.x, a.y % b.y, a.z % b.z)
a8932 = Vec3_8932(1,1,1)
b8932 = Vec3_8932(2,2,2)
@test f8932(a8932, b8932) == Vec3_8932(1.0, 1.0, 1.0)

# issue #13261
f13261() = (x = (error("oops"),); +(x...))
g13261() = f13261()
@test_throws ErrorException g13261()

# issue 13432
@noinline function f13432(x)
    offset = x ? Base.Bottom : 1
    return ===(offset, Base.Bottom)
end
@test f13432(true) == true
@test f13432(false) == false
@noinline function f13432b(x)
    a = x ? 1 : 1.0
    b = x ? 1 : 1.0f0
    return ===(a, b)
end
@test f13432b(true) == true
@test f13432b(false) == false

#13433, read!(::IO, a::Vector{UInt8}) should return a
mutable struct IO13433 <: IO end
Base.read(::IO13433, ::Type{UInt8}) = 0x01
@test read!(IO13433(), Array{UInt8}(undef, 4)) == [0x01, 0x01, 0x01, 0x01]

# issue #13647, comparing boxed isbits immutables
struct X13647
    a::Int
    b::Bool
end
function f13647(x, y)
    z = false
    z = y
    x === z
end
@test f13647(X13647(1, false), X13647(1, false))
@test !f13647(X13647(1, false), X13647(1, true))
@test !f13647(X13647(2, false), X13647(1, false))

# issue #13636
module I13636
foo(x) = 1
end
let cache = Dict()
    function I13636.foo(y::Int;k::Int=1)
        cache[1] = y+k
    end
end
@test I13636.foo(1,k=2) == 3

# issue #11327 and #13547
@test_throws MethodError convert(Type{Int}, Float32)
@test_throws MethodError Array{Type{Int64}}([Float32])
abstract type A11327 end
abstract type B11327 <: A11327 end
f11327(::Type{T},x::T) where {T} = x
@test_throws MethodError f11327(Type{A11327},B11327)

# issue #8487
@test [x for x in 1:3] == [x for x ∈ 1:3] == [x for x = 1:3]
let A = Matrix{Int}(undef, 4,3)
    for i ∈ 1:size(A,1), j ∈ 1:size(A,2)
        A[i,j] = 17*i + 51*j
    end
    @test A == [17*i + 51*j for i ∈ 1:size(A,1), j ∈ 1:size(A,2)]
end

# check if finalizers for the old gen can be triggered manually
# issue #13986
let
    obj = Ref(1)
    finalized = 0
    finalizer((obj) -> (finalized = 1), obj)
    # obj should be marked for promotion after the second gc and be promoted
    # after the third GC
    # GC_CLEAN; age = 0
    GC.gc(false)
    # GC_CLEAN; age = 1
    GC.gc(false)
    # GC_QUEUED; age = 1
    GC.gc(false)
    # GC_MARKED; age = 1
    finalize(obj)
    @test finalized == 1
end

# check if finalizers for the old gen can be triggered manually
# PR #14181
let
    # The following three `GC.gc(false)` clears the `finalizer_list`. It is
    # not strictly necessary to make the test pass but should make the failure
    # more repeatable if something breaks.
    GC.gc(false)
    # At least: GC_CLEAN; age = 1
    GC.gc(false)
    # At least: GC_QUEUED; age = 1
    GC.gc(false)
    # all objects in `finalizer_list` are now moved to `finalizer_list_marked`

    obj1 = Ref(1)
    obj2 = Ref(1)
    finalized = 0
    finalizer((obj) -> (finalized += 1), obj1)
    finalizer((obj) -> (finalized += 1), obj1)
    finalizer((obj) -> (finalized += 1; finalize(obj1)), obj2)
    finalizer((obj) -> (finalized += 1; finalize(obj1)), obj2)
    finalize(obj2)
    @test finalized == 4
end

# issue #14323
@test eval(Expr(:block, :(1))) === 1

# issue #14339
f14339(x::T, y::T) where {T<:Union{}} = 0
@test_throws MethodError f14339(1, 2)

# Make sure jlcall objects are rooted
# PR #14301
module JLCall14301

# Define f
function f end

let i = Any[[1.23], [2.34]]
    # f() with capture variables
    # Intentionally type unstable so that the dynamic dispatch will
    # read the corrupted tag if the object is incorrectly GC'd.
    global @noinline f() = i[1][1] * i[2][1]
end

# Another function that use f()
g() = f() * 100
# Compile it
g()

let i = 9.0
    # Override f()
    global @noinline f() = i + 1
end

# Make sure the old f() method is GC'd if it was not rooted properly
GC.gc()
GC.gc()
GC.gc()

# Run again.
g()

end

# make sure codegen doesn't remove argument to `isa`
@noinline __g_isa_test_1(a) = push!(a,1)
function __f_isa_arg_1()
    a = []
    isa(__g_isa_test_1(a), Any)
    length(a)
end
@test __f_isa_arg_1() == 1

# issue #14477
struct Z14477
    fld::Z14477
    Z14477() = new(new())
end
let z1 = Z14477()
    @test isa(z1, Z14477)
    @test isa(z1.fld, Z14477)
end

# issue #8846, generic macros
macro m8846(a, b=0)
    a, b
end
@test @m8846(a) === (:a, 0)
@test @m8846(a, 1) === (:a, 1)
let nometh = try; @eval @m8846(a, b, c); false; catch ex; ex; end
    __source__ = LineNumberNode(@__LINE__() -  1, Symbol(@__FILE__))
    nometh::LoadError
    @test nometh.file === string(__source__.file)
    @test nometh.line === __source__.line
    e = nometh.error::MethodError
    @test e.f === getfield(@__MODULE__, Symbol("@m8846"))
    @test e.args === (__source__, @__MODULE__, :a, :b, :c)
 end

# a simple case of parametric dispatch with unions
let foo(x::Union{T, Nothing}, y::Union{T, Nothing}) where {T} = 1
    @test foo(1, nothing) === 1
    @test foo(nothing, nothing) === 1
end
let foo(x::Union{T, Nothing}, y::Union{T, Nothing}) where {T} = T
    @test foo(1, nothing) === Int
    @test_throws UndefVarError(:T) foo(nothing, nothing)
end

module TestMacroGlobalFunction
macro makefn(f,g)
    quote
        global $(f)
        function $(f)(x)
            x+1
        end
        global $(g)
        $(g)(x) = x+2
    end
end
@makefn ff gg
end
@test TestMacroGlobalFunction.ff(1) == 2
@test TestMacroGlobalFunction.gg(1) == 3

# issue #18672
macro x18672()
    quote
        function f
        end
    end
end
let
    @test isa(@x18672, Function)
end

# issue #14564
@test isa(objectid(Tuple.name.cache), Integer)

# issue #14691
mutable struct T14691; a::UInt; end
@test (T14691(0).a = 0) === 0

# issue #14245
f14245() = (v = []; push!(v, length(v)); v)
@test f14245()[1] == 0

# issue #9677
@generated function foo9677(x::AbstractArray{T,N}) where {T,N}
    quote
        x=$N
        y=x+1
        return y
    end
end
foo9677(x::Array) = invoke(foo9677, Tuple{AbstractArray}, x)
@test foo9677(1:5) == foo9677(randn(3))

# issue #6846
f6846() = (please6846; 2)
@test_throws UndefVarError(:please6846) f6846()

module M6846
    macro f()
        return esc(:(please6846; 2))
    end
end
@test_throws UndefVarError(:please6846) @M6846.f()

# issue #14758
@test isa(@eval(f14758(; $([]...)) = ()), Function)

# issue #14767
@inline f14767(x) = x ? A14767 : ()
const A14767 = f14767(false)
@test A14767 === ()

# issue #10985
f10985(::Any...) = 1
@test f10985(1, 2, 3) == 1

# a tricky case for closure conversion
mutable struct _CaptureInCtor
    yy
    function _CaptureInCtor(list_file::AbstractString="")
        y = 0
        f = x->add_node(y)
        new(f(2))
    end
    add_node(y) = y+1
end
@test _CaptureInCtor().yy == 1

# issue #14610
let sometypes = (Int,Int8)
    f(::Union{ntuple(i->Type{sometypes[i]}, length(sometypes))...}) = 1
    @test hasmethod(f, (Union{Type{Int},Type{Int8}},))
end

let
    b=()->c
    c=1
    @test b() == 1
end

# issue #14825
abstract type abstest_14825 end

mutable struct t1_14825{A <: abstest_14825, B}
  x::A
  y::B
end

mutable struct t2_14825{C, B} <: abstest_14825
  x::C
  y::t1_14825{t2_14825{C, B}, B}
end

@test t2_14825{Int,Int}.types[2] <: t1_14825

# issue #14917
@test isa(let generic
          function generic end
          end,
          Function)

# f.(x) vectorization syntax (#15032)
@test (x -> 2x).([1,2,3]) == [2,4,6]
@test ((x,y) -> 2x+y^2).([1,2,3],[3,4,5]) == [1,2,3]*2 + [3,4,5].^2

# let syntax with multiple lhs
let z = (3,9,42)
    let (a,b,c) = z
        @test a == 3 && b == 9 && c == 42
    end
    let (a,b::Float64,c::Int8) = z
        @test a == 3 && b === 9.0 && c === Int8(42)
    end
    z = (1, z, 10)
    let (a, (b,c,d), e) = z
        @test (a,b,c,d,e) == (1,3,9,42,10)
    end
end

# issue #15072
let grphtest = ((1, [2]),)
    for (s, g) in grphtest
        g_ = map(s -> s+1, g)
        @test g_ == [3]
    end
    for s = 1:1
    end
end

# issue #15186
let ex = quote
             $(if true; :(test); end)
         end
    @test ex.args[2] == :test
end

# issue #25652
x25652 = 1
x25652_2 = let (x25652, _) = (x25652, nothing)
    x25652 = x25652 + 1
    x25652
end
@test x25652_2 == 2
@test x25652 == 1

# issue #15180
function f15180(x::T) where T
    X = Vector{T}(undef, 1)
    X[1] = x
    @noinline ef(::J) where {J} = (J,X[1]) # Use T
    ef(::J, ::Int) where {J} = (T,J)
    return ef
end
@test map(f15180(1), [1,2]) == [(Int,1),(Int,1)]

let ary = Vector{Any}(undef, 10)
    check_undef_and_fill(ary, rng) = for i in rng
        @test !isassigned(ary, i)
        ary[i] = (Float64(i), i) # some non-cached content
        @test isassigned(ary, i)
    end
    # Check if the memory is initially zerod and fill it with value
    # to check if these values are not reused later.
    check_undef_and_fill(ary, 1:10)
    # Check if the memory grown at the end are zerod
    ccall(:jl_array_grow_end, Cvoid, (Any, Csize_t), ary, 10)
    check_undef_and_fill(ary, 11:20)
    # Make sure the content of the memory deleted at the end are not reused
    ccall(:jl_array_del_end, Cvoid, (Any, Csize_t), ary, 5)
    ccall(:jl_array_grow_end, Cvoid, (Any, Csize_t), ary, 5)
    check_undef_and_fill(ary, 16:20)

    # Now check grow/del_end
    ary = Vector{Any}(undef, 1010)
    check_undef_and_fill(ary, 1:1010)
    # This del_beg should move the buffer
    ccall(:jl_array_del_beg, Cvoid, (Any, Csize_t), ary, 1000)
    ccall(:jl_array_grow_beg, Cvoid, (Any, Csize_t), ary, 1000)
    check_undef_and_fill(ary, 1:1000)
    ary = Vector{Any}(undef, 1010)
    check_undef_and_fill(ary, 1:1010)
    # This del_beg should not move the buffer
    ccall(:jl_array_del_beg, Cvoid, (Any, Csize_t), ary, 10)
    ccall(:jl_array_grow_beg, Cvoid, (Any, Csize_t), ary, 10)
    check_undef_and_fill(ary, 1:10)

    ary = Vector{Any}(undef, 1010)
    check_undef_and_fill(ary, 1:1010)
    ccall(:jl_array_grow_end, Cvoid, (Any, Csize_t), ary, 10)
    check_undef_and_fill(ary, 1011:1020)
    ccall(:jl_array_del_end, Cvoid, (Any, Csize_t), ary, 10)
    ccall(:jl_array_grow_beg, Cvoid, (Any, Csize_t), ary, 10)
    check_undef_and_fill(ary, 1:10)

    # Make sure newly malloc'd buffers are filled with 0
    # test this for a few different sizes since we need to make sure
    # we are malloc'ing the buffer after the grow_end and malloc is not using
    # mmap directly (which may return a zero'd new page).
    for n in [50, 51, 100, 101, 200, 201, 300, 301]
        ary = Vector{Any}(undef, n)
        # Try to free the previous buffer that was filled with random content
        # and to increase the chance of getting a non-zero'd buffer next time
        GC.gc()
        GC.gc()
        GC.gc()
        ccall(:jl_array_grow_beg, Cvoid, (Any, Csize_t), ary, 4)
        ccall(:jl_array_del_beg, Cvoid, (Any, Csize_t), ary, 4)
        ccall(:jl_array_grow_end, Cvoid, (Any, Csize_t), ary, n)
        ccall(:jl_array_grow_beg, Cvoid, (Any, Csize_t), ary, 4)
        check_undef_and_fill(ary, 1:(2n + 4))
    end

    ary = Vector{Any}(undef, 100)
    ccall(:jl_array_grow_end, Cvoid, (Any, Csize_t), ary, 10000)
    ary[:] = 1:length(ary)
    ccall(:jl_array_del_beg, Cvoid, (Any, Csize_t), ary, 10000)
    # grow on the back until a buffer reallocation happens
    cur_ptr = pointer(ary)
    while cur_ptr == pointer(ary)
        len = length(ary)
        ccall(:jl_array_grow_end, Cvoid, (Any, Csize_t), ary, 10)
        for i in (len + 1):(len + 10)
            @test !isassigned(ary, i)
        end
    end

    ary = Vector{Any}(undef, 100)
    ary[:] = 1:length(ary)
    ccall(:jl_array_grow_at, Cvoid, (Any, Csize_t, Csize_t), ary, 50, 10)
    for i in 51:60
        @test !isassigned(ary, i)
    end
end

# check if we can run multiple finalizers at the same time
# Use a `@noinline` function to make sure the inefficient gc root generation
# doesn't keep the object alive.
@noinline function create_dead_object13995(finalized)
    obj = Ref(1)
    finalizer((x)->(finalized[1] = true), obj)
    finalizer((x)->(finalized[2] = true), obj)
    finalizer((x)->(finalized[3] = true), obj)
    finalizer((x)->(finalized[4] = true), obj)
    nothing
end
# disable GC to make sure no collection/promotion happens
# when we are constructing the objects
let gc_enabled13995 = GC.enable(false)
    finalized13995 = [false, false, false, false]
    create_dead_object13995(finalized13995)
    GC.enable(true)
    # obj is unreachable and young, a single young gc should collect it
    # and trigger all the finalizers.
    GC.gc(false)
    GC.enable(false)
    @test finalized13995 == [true, true, true, true]
    GC.enable(gc_enabled13995)
end

# issue #15283
j15283 = 0
let
    global j15283
    k15283 = (j15283 += 1)
end
@test j15283 == 1
@test !@isdefined k15283

# issue #15264
module Test15264
    mod1(x::T) where {T} = x < 1 ? x : mod1(x-1)
end
@test Test15264.mod1 !== Base.mod1

module M15455
function rpm_provides(r::T) where T
    push!([], partialsort(r,T))
end
partialsort(a,b) = 0
end
@test M15455.partialsort(1,2)==0

# check that medium-sized array is 64-byte aligned (#15139)
@test Int(pointer(Vector{Float64}(undef, 1024))) % 64 == 0

# PR #15413
# Make sure arrayset can handle `Array{T}` (where `T` is a type and not a
# `TypeVar`) without crashing
let
    function arrayset_unknown_dim(::Type{T}, n) where T
        Base.arrayset(true, reshape(Vector{T}(undef, 1), fill(1, n)...), 2, 1)
    end
    arrayset_unknown_dim(Any, 1)
    arrayset_unknown_dim(Any, 2)
    arrayset_unknown_dim(Any, 3)
    arrayset_unknown_dim(Int, 1)
    arrayset_unknown_dim(Int, 2)
    arrayset_unknown_dim(Int, 3)
end

module TestSharedArrayResize
using Test
# Attempting to change the shape of a shared array should unshare it and
# not modify the original data
function test_shared_array_resize(::Type{T}) where T
    len = 100
    a = Vector{T}(undef, len)
    function test_unshare(f)
        a′ = reshape(reshape(a, (len ÷ 2, 2)), len)
        a[:] = 1:length(a)
        # The operation should fail on the owner shared array
        # and has no side effect.
        @test_throws ErrorException f(a)
        @test a == [1:len;]
        @test a′ == [1:len;]
        @test pointer(a) == pointer(a′)
        # The operation should pass on the non-owner shared array
        # and should unshare the arrays with no effect on the original one.
        f(a′)
        @test a == [1:len;]
        @test pointer(a) != pointer(a′)
    end

    test_unshare(a->ccall(:jl_array_del_end, Cvoid, (Any, Csize_t), a, 0))
    test_unshare(a->ccall(:jl_array_del_end, Cvoid, (Any, Csize_t), a, 1))
    test_unshare(a->ccall(:jl_array_del_beg, Cvoid, (Any, Csize_t), a, 0))
    test_unshare(a->ccall(:jl_array_del_beg, Cvoid, (Any, Csize_t), a, 1))
    test_unshare(a->deleteat!(a, 10))
    test_unshare(a->deleteat!(a, 90))
    test_unshare(a->ccall(:jl_array_grow_end, Cvoid, (Any, Csize_t), a, 0))
    test_unshare(a->ccall(:jl_array_grow_end, Cvoid, (Any, Csize_t), a, 1))
    test_unshare(a->ccall(:jl_array_grow_beg, Cvoid, (Any, Csize_t), a, 0))
    test_unshare(a->ccall(:jl_array_grow_beg, Cvoid, (Any, Csize_t), a, 1))
    test_unshare(a->insert!(a, 10, 10))
    test_unshare(a->insert!(a, 90, 90))
end
test_shared_array_resize(Int)
test_shared_array_resize(Any)
end

module TestArrayNUL
using Test
function check_nul(a::Vector{UInt8})
    b = ccall(:jl_array_cconvert_cstring,
              Ref{Vector{UInt8}}, (Vector{UInt8},), a)
    @test unsafe_load(pointer(b), length(b) + 1) == 0x0
    return b === a
end

a = UInt8[]
b = "aaa"
c = [0x2, 0x1, 0x3]

@test check_nul(a)
@test check_nul(unsafe_wrap(Vector{UInt8},b))
@test check_nul(c)
d = [0x2, 0x1, 0x3]
@test check_nul(d)
push!(d, 0x3)
@test check_nul(d)
push!(d, 0x3)
@test check_nul(d)
ccall(:jl_array_del_end, Cvoid, (Any, UInt), d, 2)
@test check_nul(d)
ccall(:jl_array_grow_end, Cvoid, (Any, UInt), d, 1)
@test check_nul(d)
ccall(:jl_array_grow_end, Cvoid, (Any, UInt), d, 1)
@test check_nul(d)
ccall(:jl_array_grow_end, Cvoid, (Any, UInt), d, 10)
@test check_nul(d)
ccall(:jl_array_del_beg, Cvoid, (Any, UInt), d, 8)
@test check_nul(d)
ccall(:jl_array_grow_beg, Cvoid, (Any, UInt), d, 8)
@test check_nul(d)
ccall(:jl_array_grow_beg, Cvoid, (Any, UInt), d, 8)
@test check_nul(d)
f = unsafe_wrap(Array, pointer(d), length(d))
@test !check_nul(f)
f = unsafe_wrap(Array, ccall(:malloc, Ptr{UInt8}, (Csize_t,), 10), 10, own = true)
@test !check_nul(f)
end

# Copy of `#undef`
copyto!(Vector{Any}(undef, 10), Vector{Any}(undef, 10))
function test_copy_alias(::Type{T}) where T
    ary = T[1:100;]
    unsafe_copyto!(ary, 1, ary, 11, 90)
    @test ary == [11:100; 91:100]
    ary = T[1:100;]
    unsafe_copyto!(ary, 11, ary, 1, 90)
    @test ary == [1:10; 1:90]
end
test_copy_alias(Int)
test_copy_alias(Any)

# issue #15370
@test isdefined(Core, :Box)
@test !isdefined(Base, :Box)
@test !isdefined(Main, :Box)

# issue #1784
let a = [false]
function foo1784()
    (a,b) = try
        return true
        (0,1)
    finally
         a[1] = true
    end
end
@test foo1784()
@test a[1] == true
end

# issue #14113
module A14113
    using Test, Random
    # show that making several thousand methods (and lots of AST constants)
    # doesn't cause any serious issues (for example, for the serializer)
    # although to keep runtime on the order of several seconds for this test,
    # only several hundred of them are compiled / called
    for i = 1:2^14 + 256
        r = rand(2^4)
        code = Expr(:tuple, r...)
        f = @eval () -> $code
        i > (2^14 - 256) && @test [f()...] == r
    end
end

# issue #15425
@noinline function f15425(x)
end
@test f15425(1) === nothing

# issue #15809
# but note, direct global method defs inside functions have since been disallowed
function f15809()
    @eval g15809(x::T) where {T} = T
end
f15809()
@test g15809(2) === Int

module Macro_Yielding_Global_Assignment
macro m()
    quote
        global x
        x = 2
    end
end
@m
end
@test Macro_Yielding_Global_Assignment.x == 2

# issue #15718
@test :(f($NaN)) == :(f($NaN))
@test isequal(:(f($NaN)), :(f($NaN)))

# PR #16011 Make sure dead code elimination doesn't delete push and pop
# of metadata
module TestDeadElim16011
using Test

function count_expr_push(ex::Expr, head::Symbol, counter)
    if ex.head === head
        if ex.args[1] === :pop
            counter[] -= 1
        else
            counter[] += 1
        end
        return
    end
    for arg in ex.args
        isa(arg, Expr) && count_expr_push(arg, head, counter)
    end
    return false
end

function metadata_matches(ast::Core.CodeInfo)
    inbounds_cnt = Ref(0)
    for ex in ast.code::Array{Any,1}
        if isa(ex, Expr)
            ex = ex::Expr
            count_expr_push(ex, :inbounds, inbounds_cnt)
        end
    end
    @test inbounds_cnt[] == 0
end

function test_metadata_matches(@nospecialize(f), @nospecialize(tt))
    metadata_matches(code_typed(f, tt)[1][1])
end

function f1()
    @inbounds return 1
end
function f2()
    @boundscheck begin
        error()
    end
end
# No, don't write code this way...
@noinline function g(a)
end
@eval function f3()
    g($(Expr(:inbounds, true)))
    @goto out
    g($(Expr(:inbounds, :pop)))
    @label out
end

test_metadata_matches(f1, Tuple{})
test_metadata_matches(f2, Tuple{})
test_metadata_matches(f3, Tuple{})

end

# SSA value where the assignment is after the user in syntactic order
let f = function(a, b)
    @goto a
    @label b
    return j[1] + j[2] * 2
    @label a
    j = (a, b)
    @goto b
end
    @test f(1, 2) == 5
end

# issue #8712
mutable struct Issue8712; end
@test isa(invoke(Issue8712, Tuple{}), Issue8712)

# issue #16089
f16089(args...) = typeof(args)
g16089() = f16089(UInt8)
@test g16089() === Tuple{DataType}

# issue #16023
function f16023()
    x
    x = 1
end
@test_throws UndefVarError f16023()

# issue #16158
function f16158(x)
    bar(x) = length(x)==1 ? x : string(x, bar(x[1:end-1]))
    bar(x)
end
@test f16158("abc") == "abcaba"

# LLVM verifier error for noreturn function
# the `code_llvm(devnull, ...)` tests are only meaningful on debug build
# with verifier on (but should still pass on release build).
module TestSSA16244

using Test, InteractiveUtils
@noinline k(a) = a

# unreachable branch due to `ccall(:jl_throw)`
function f1(a)
    if a
        b = (k(a) + 1, 3)
    else
        throw(DivideError())
    end
    b[1]
end
code_llvm(devnull, f1, Tuple{Bool})
@test f1(true) == 2
@test_throws DivideError f1(false)

# unreachable branch due to function that does not return
@noinline g() = error()
function f2(a)
    if a
        b = (k(a) + 1, 3)
    else
        # Make sure type inference can infer the type of `g`
        g()
    end
    b[1]
end
code_llvm(devnull, f2, Tuple{Bool})
@test f2(true) == 2
@test_throws ErrorException f2(false)

# SA but not SSA
function f3(a)
    if a
        b = (k(a) + 1, 3)
    end
    b[1]
end
code_llvm(devnull, f3, Tuple{Bool})
@test f3(true) == 2
ex = try
    f3(false)
catch _ex
    _ex
end
@test isa(ex, UndefVarError)
@test ex.var === :b

# unreachable branch due to ccall that does not return
function f4(a, p)
    if a
        b = (k(a) + 1, 3)
    else
        ccall(p, Union{}, ())
    end
    b[1]
end
code_llvm(devnull, f4, Tuple{Bool,Ptr{Cvoid}})
@test f4(true, C_NULL) == 2
@test_throws UndefRefError f4(false, C_NULL)

# SSA due to const prop of condition
function f5(a)
    c = true
    if c
        b = (k(a) + 1, 3)
    end
    b[1]
end
code_llvm(devnull, f5, Tuple{Bool})
@test f5(true) == 2
@test f5(false) == 1

# SSA due to const prop of condition
function f6(a)
    if 1 === 1
        b = (k(a) + 1, 3)
    end
    b[1]
end
code_llvm(devnull, f6, Tuple{Bool})
@test f6(true) == 2
@test f6(false) == 1

# unreachable branch due to typeassert
function f7(a)
    if a
        b = (k(a) + 1, 3)
    else
        a = a::Int
    end
    b[1]
end
code_llvm(devnull, f7, Tuple{Bool})
@test f7(true) == 2
@test_throws TypeError f7(false)

# unreachable branch due to non-Bool used in Bool context
function f8(a, c)
    if a
        b = (k(a) + 1, 3)
    else
        c && a
    end
    b[1]
end
code_llvm(devnull, f8, Tuple{Bool,Int})
@test f8(true, 1) == 2
@test_throws TypeError f8(false, 1)

# unreachable branch due to undef local variable
function f9(a)
    if a
        b = (k(a) + 1, 3)
    else
        d
        d = 1
    end
    b[1]
end
code_llvm(devnull, f9, Tuple{Bool})
@test f9(true) == 2
ex = try
    f9(false)
catch _ex
    _ex
end
@test isa(ex, UndefVarError)
@test ex.var === :d

end

# issue #16153
f16153(x) = 1
f16153(@nospecialize(x), y...) = 2
@test f16153("") == 1
ff16153(@nospecialize(x), y...) = 2
ff16153(x) = 1
@test ff16153("") == 1
g16153(@nospecialize(x), y...) = 1
g16153(@nospecialize(x), @nospecialize(y)) = 2
@test g16153(1, 1) == 2
gg16153(@nospecialize(x), @nospecialize(y)) = 2
gg16153(@nospecialize(x), y...) = 1
@test gg16153(1, 1) == 2

# don't remove global variable accesses even if we "know" their type
# see #16090
f16090() = typeof(undefined_x16090::Tuple{Type{Int}})
@test_throws UndefVarError f16090()
undefined_x16090 = (Int,)
@test_throws TypeError f16090()

# issue #12238
struct A12238{T} end
mutable struct B12238{T,S}
    a::A12238{B12238{Int,S}}
end
@test B12238.body.body.types[1] === A12238{B12238{Int}.body}
@test isa(A12238{B12238{Int}}.instance, A12238{B12238{Int}})
@test !isdefined(B12238.body.body.types[1], :instance)  # has free type vars

# issue #16315
let a = Any[]
    @noinline f() = a[end]
    @test (push!(a,10); f()) - (push!(a,2); f()) == 8
    @test a == [10, 2]
end

# issue #12096
let a = Val{Val{TypeVar(:_, Int)}},
    b = Val{Val{x} where x<:Int}

    @test !isdefined(a, :instance)
    @test  isdefined(b, :instance)
    @test Base.isconcretetype(b)
end

# A return type widened to Type{Union{T,Nothing}} should not confuse
# codegen
@noinline MaybeFunc(T) = Union{T, Nothing}
fMaybeFunc() = MaybeFunc(Int64)
@test fMaybeFunc() == Union{Int64, Nothing}

# issue #16431
function f16431(x)
    z::Int = x * 2
    g(y) = begin z = z + y; y + x end
    z * g(x)
end
@test @inferred(f16431(1)) == 4

# issue #14878
mutable struct A14878
    ext
end
A14878() = A14878(Dict())
mutable struct B14878
end
B14878(ng) = B14878()
function trigger14878()
    w = A14878()
    w.ext[:14878] = B14878(junk)  # global junk not defined!
    return w
end
@test_throws UndefVarError(:junk) trigger14878()

# issue #1090
function f1090(x)::Int
    if x == 1
        return 1
    end
    2.0
end
@test f1090(1) === 1
@test f1090(2) === 2
(g1090(x::T)::T) where {T} = x+1.0
@test g1090(1) === 2
@test g1090(Float32(3)) === Float32(4)

# error during conversion to return type
function f1090_err()::Int
    try
        return ""
    catch
        8
    end
end
@test_throws MethodError f1090_err()

function f17613_2(x)::Float64
    try
        return x
    catch
        return x+1
    end
end
@test isa(f17613_2(1), Float64)

# return type decl with `where`
function where1090(x::Array{T})::T where T<:Real
    return x[1] + 2.0
end
@test where1090([4]) === 6
@test_throws MethodError where1090(String[])

mutable struct A1090 end
Base.convert(::Type{Int}, ::A1090) = "hey"
f1090()::Int = A1090()
@test_throws TypeError f1090()

# issue #19106
function f19106()::Nothing end
@test f19106() === nothing

# issue #16783
function f16783()
    T = UInt32
    x::T = 0
    bar() = x+1
end
@test f16783()() == 1

# issue #16767
mutable struct A16767{T}
    a::Base.RefValue{T}
end
mutable struct B16767{T}
    b::A16767{B16767{T}}
end
mutable struct C16767{T}
    b::A16767{C16767{:a}}
end
@test B16767.body.types[1].types[1].parameters[1].types[1] === A16767{B16767.body}
@test C16767.body.types[1].types[1].parameters[1].types[1] === A16767{C16767{:a}}

# issue #16340
function f16340(x::T) where T
    function g(y::T) where T
        return (T,T)
    end
    return g
end
let g = f16340(1)
    @test isa(typeof(g).name.mt.defs.sig, UnionAll)
end

# issue #16793
try
    abstract type T16793 end
catch
end
@test isa(T16793, Type)
@test isa(abstract type T16793_2 end, Nothing)

# issue #17147
f17147(::Tuple) = 1
f17147(::Vararg{Tuple,N}) where {N} = 2
@test f17147((), ()) == 2

# issue #17449, argument evaluation order
@noinline f17449(x, y) = nothing
@noinline function g17449(r)
    r[] = :g
    return 1
end
@noinline function k17449(r, v)
    r[] = :k
    return v ? 1 : 1.0
end
function h17449(v)
    r = Ref(:h)
    f17449(g17449(r), k17449(r, v))
    return r[]
end
@test h17449(true) === :k

# make sure lowering agrees on sp order
function captsp(x::T, y::S) where {T, S}
    subf(x2::Int) = T
    subf(x2::UInt) = S
    return subf(Int(1)), subf(UInt(1))
end
@test captsp(1, 2.0) == (Int, Float64)

# issue #15068
function sp_innersig(x::T) where {T}
   subf(x2::T) = (x, x2, :a)
   subf(x2) = (x, x2, :b)
   return (subf(one(T)), subf(unsigned(one(T))))
end
@test sp_innersig(2) == ((2, 1, :a), (2, UInt(1), :b))

# TODO: also allow local variables?
#function local_innersig{T}(x::T)
#   V = typeof(x)
#   U = unsigned(T)
#   subf(x2::T, x3::Complex{V}) = (x, x2, x3)
#   subf(x2::U) = (x, x2)
#   return (subf(one(T), x * im), subf(unsigned(one(T))))
#end
#@test local_innersig(Int32(2)) == ((Int32(2), Int32(1), Int32(2)im), (Int32(2), UInt32(1)))
#@test local_innersig(Int64(3)) == ((Int64(3), Int64(1), Int64(3)im), (Int64(3), UInt64(1)))

# Issue 4914
let
    j(j) = j
    @test j(1) == 1
    k(x) = (k = x; k)
    @test k(1) == 1
end

# issue #18085
f18085(a, x...) = (0, )
for (f, g) in ((:asin, :sin), (:acos, :cos))
    gx = eval(g)
    global f18085(::Type{Val{f}}, x...) = map(x -> 2gx(x), f18085(Val{g}, x...))
end
@test f18085(Val{:asin}, 3) === (0.0,)

# issue #18236 constant VecElement in ast triggers codegen assertion/undef
# VecElement of scalar
v18236 = VecElement(1.0)
ptr18236 = @cfunction(identity, VecElement{Float64}, (VecElement{Float64},))
@eval @noinline f18236(ptr) = ccall(ptr, VecElement{Float64},
                                    (VecElement{Float64},), $v18236)
@test f18236(ptr18236) === v18236
@test !occursin("double undef", sprint(code_llvm, f18236, Tuple{Ptr{Cvoid}}))
# VecElement of struct, not necessarily useful but does have special
# ABI so should be handled correctly
# This struct should be small enough to be passed by value in C ABI
# in order to trigger the problematic code path.
# We should be at least testing this on some platforms.
# Not sure if there's a better way to trigger unboxing in codegen.
v18236_2 = VecElement((Int8(1), Int8(2)))
ptr18236_2 = @cfunction(identity, VecElement{NTuple{2,Int8}},
                        (VecElement{NTuple{2,Int8}},))
@eval @noinline f18236_2(ptr) = ccall(ptr, VecElement{NTuple{2,Int8}},
                                      (VecElement{NTuple{2,Int8}},),
                                      $v18236_2)
@test f18236_2(ptr18236_2) === v18236_2

# issue #18385
function f18385(g)
    if g
        a = (1, 2)
    end
    return a[1]
end
@test f18385(true) === 1
# variable name in the error is tested above in `TestSSA16244`
@test_throws UndefVarError f18385(false)

# Another similar issue, make sure newvar nodes are created for the fields
# variables too.
function f18386(a, b, second_pass)
    s = 0
    firstpass = true
    for i in 1:2
        if firstpass
            x = (a, b)
            firstpass = !second_pass
        end
        s += x[1]
    end
    s
end
@test f18386(1, 2, false) === 2
# variable name in the error is tested above in `TestSSA16244`
@test_throws UndefVarError f18386(1, 2, true)

Base.@propagate_inbounds function f18412(a)
    @inbounds b = a[1]
    return b
end
@test f18412([1]) == 1

# issue #18173
function f18173()
    identity(()->successflag)
    successflag = false
end
@test f18173() == false

fVararg(x) = Vararg{x}
gVararg(a::fVararg(Int)) = length(a)
@test gVararg(1,2,3,4,5) == 5

# issue #18577
@generated f18577() = quote ()->1 end
@test try
    f18577()
    false
catch e
    (e::ErrorException).msg
end == "generated function body is not pure. this likely means it contains a closure or comprehension."

let x = 1
    global g18444
    @noinline g18444(a) = (x += 1; a[])
    f18444_1(a) = invoke(sin, Tuple{Int}, g18444(a))
    f18444_2(a) = invoke(sin, Tuple{Integer}, g18444(a))
    @test_throws ErrorException("invoke: argument type error") f18444_1(Ref{Any}(1.0))
    @test x == 2
    @test_throws ErrorException("invoke: argument type error") f18444_2(Ref{Any}(1.0))
    @test x == 3
    @test f18444_1(Ref{Any}(1)) === sin(1)
    @test x == 4
    @test f18444_2(Ref{Any}(1)) === sin(1)
    @test x == 5
end

f18095(::Int, ::Number) = 0x21
f18095(::Number, ::Int) = 0x12
@test_throws MethodError f18095(1, 2)
@test_throws MethodError invoke(f18095, Tuple{Int, Int}, 1, 2)
@test_throws MethodError invoke(f18095, Tuple{Int, Any}, 1, 2)
@test invoke(f18095, Tuple{Int, Real}, 1, 2) === 0x21

# `invoke` with non-constant function
struct CassetteLikeWrapper{F}
    x
    f::F
end
(foo::CassetteLikeWrapper)(args...) = foo.f(args...)
(foo::CassetteLikeWrapper)(x) = invoke(foo, Tuple{Vararg{Any}}, x)
@test CassetteLikeWrapper(1,-)(2) == -2

f26301(x) = 1
f26301(x::Int) = 2
function g26301()
    f = Any[f26301][1]
    invoke(f, Tuple{Any}, 0)
end
@test g26301() == 1

# issue #10981, long argument lists
let a = fill(["sdf"], 2*10^6), temp_vcat(x...) = vcat(x...)
    # we introduce a new function `temp_vcat` to make sure there is no existing
    # method cache match, leading to a path that allocates a large tuple type.
    b = temp_vcat(a...)
    @test isa(b, Vector{String})
    @test length(b) == 2*10^6
    @test b[1] == b[end] == "sdf"
end

# issue #17255, take `deferred_alloc` into account
# when calculating total allocation size.
@noinline function f17255(n)
    GC.enable(false)
    b0 = Base.gc_bytes()
    local a
    for i in 1:n
        a, t, allocd = @timed [Ref(1) for i in 1:1000]
        @test allocd > 0
        b1 = Base.gc_bytes()
        if b1 < b0
            return false, a
        end
    end
    return true, a
end
@test f17255(10000)[1]
GC.enable(true)

# issue #18710
bad_tvars() where {T} = 1
@test isa(which(bad_tvars, ()), Method)
@test bad_tvars() === 1
bad_tvars2() where {T} = T
@test_throws UndefVarError(:T) bad_tvars2()
missing_tvar(::T...) where {T} = T
@test_throws UndefVarError(:T) missing_tvar()
@test missing_tvar(1) === Int
@test missing_tvar(1, 2, 3) === Int
@test_throws MethodError missing_tvar(1, 2, "3")

# issue #19059 - test for lowering of `let` with assignment not adding Box in simple cases
contains_Box(e::GlobalRef) = (e.name === :Box)
contains_Box(@nospecialize(e)) = false
contains_Box(e::Expr) = any(contains_Box, e.args)

function let_noBox()
    local x
    for i = 1:2
        if i == 1
            x = 21
        end
        let x = x
            return () -> x
        end
    end
end
function let_Box1()
    local x
    for i = 1:2
        if i == 1
            x = 22
        end
        let y = x
            return () -> x
        end
    end
end
function let_Box2()
    local x
    for i = 1:2
        if i == 1
            x = 23
        end
        let x = x
            # In the future, this may change to no-Box if lowering improves
            return () -> x
            x = 43
        end
    end
end
function let_Box3()
    local x
    for i = 1:2
        if i == 1
            x = 24
        end
        let y
            # In the future, this may change to no-Box if lowering improves
            y = x
            return () -> x
        end
    end
end
function let_Box4()
    local x, g
    for i = 1:2
        if i == 1
            x = 25
        end
        let x = x
            g = () -> x
            x = 44
        end
        @test x == 25
        return g
    end
end
function let_Box5()
    local x, g, h
    for i = 1:2
        if i == 1
            x = 25
        end
        let x = x
            g = () -> (x = 46)
            h = () -> x
        end
        @test x == 25
        @test h() == 25
        @test g() == 46
        @test h() == 46
        @test x == 25
        return g
    end
end
@test any(contains_Box, code_lowered(let_Box1,())[1].code)
@test any(contains_Box, code_lowered(let_Box2,())[1].code)
@test any(contains_Box, code_lowered(let_Box3,())[1].code)
@test any(contains_Box, code_lowered(let_Box4,())[1].code)
@test any(contains_Box, code_lowered(let_Box5,())[1].code)
@test !any(contains_Box, code_lowered(let_noBox,())[1].code)
@test let_Box1()() == 22
@test let_Box2()() == 23
@test let_Box3()() == 24
@test let_Box4()() == 44
@test let_Box5()() == 46
@test let_noBox()() == 21

module TestModuleAssignment
using Test
@eval $(GlobalRef(TestModuleAssignment, :x)) = 1
@test x == 1
@eval $(GlobalRef(TestModuleAssignment, :x)) = 2
@test x == 2
end

# issue #14893
module M14893
x = 14893
macro m14893()
    :x
end
function f14893()
    x = 1
    @m14893
end
end
function f14893()
    x = 2
    M14893.@m14893
end

@test f14893() == 14893
@test M14893.f14893() == 14893

# issue #19599
f19599(x::((S)->Vector{S})(T)...) where {T} = 1
@test f19599([1],[1]) == 1
@test_throws MethodError f19599([1],[1.0])

# avoiding StackOverflowErrors (issues #12007, #10326, #15736)
module SOE
mutable struct Sgnd <: Signed
    v::Int
end
using Test
using Dates
@test_throws ErrorException abs(Sgnd(1))       #12007
io = IOBuffer()
@test_throws ErrorException show(io, Sgnd(1))  #12007

struct MyTime <: Dates.TimeType
    value::Int
end
@test_throws ErrorException isless(MyTime(1), now())

end # module SOE

# issue #15240
@test_nowarn begin
    local p15240
    p15240 = ccall(:jl_realloc, Ptr{Cvoid}, (Ptr{Cvoid}, Csize_t), C_NULL, 10)
    ccall(:jl_free, Cvoid, (Ptr{Cvoid},), p15240)
end

# issue #19963
@test_nowarn ccall(:jl_free, Cvoid, (Ptr{Cvoid},), C_NULL)

# Wrong string size on 64bits for large string.
if Sys.WORD_SIZE == 64
    @noinline function test_large_string20360(slot)
        try
            # Do no touch the string to avoid triggering OOM
            slot[] = Base._string_n(2^32)
            GC.gc(false)
        catch ex
            # This can happen if there's a virtual address size limit
            @test isa(ex, OutOfMemoryError)
            @test_broken false
        end
        return
    end
    @noinline function tester20360()
        GC.gc()
        # Makes sure the string is rooted during the `GC.gc(false)`
        # but is not before the last gc in this function.
        slot = Ref{Any}()
        test_large_string20360(slot)
        slot[] = nothing
        GC.gc()
        return
    end
    @test_nowarn tester20360()
end

@test_throws ArgumentError eltype(Bottom)

# issue #16424, re-evaluating type definitions
struct A16424
    x
    y
end

struct A16424  # allowed
    x
    y
end

@test_throws ErrorException @eval struct A16424
    x
    z
end

@test_throws ErrorException @eval struct A16424
    x
    y::Real
end

struct B16424{T}
    a
end

struct B16424{T}
    a
end

@test_throws ErrorException @eval struct B16424{S}
    a
end

struct C16424{T,S}
    x::T
    y::S
end

struct C16424{T,S}
    x::T
    y::S
end

@test_throws ErrorException @eval struct C16424{T,S}
    x::S
    y::T
end

struct D16424{T<:Real,S<:T}
    x::Vector{S}
    y::Vector{T}
end

struct D16424{T<:Real,S<:T}
    x::Vector{S}
    y::Vector{T}
end

@test_throws ErrorException struct D16424{T<:Real,S<:Real}
    x::Vector{S}
    y::Vector{T}
end

# issue #20999, allow more type redefinitions
struct T20999
    x::Array{T} where T<:Real
end

struct T20999
    x::Array{T} where T<:Real
end

@test_throws ErrorException struct T20999
    x::Array{T} where T<:Integer
end

let a = Vector{Core.TypeofBottom}(undef, 2)
    @test a[1] == Union{}
    @test a == [Union{}, Union{}]
end

@test_throws TypeError(:T17951, "type definition", Type, Vararg) @eval begin
    struct T17951
        x::Vararg
    end
end

# issue #21178
struct F21178{A,B} end
b21178(::F1,::F2) where {B1,B2,F1<:F21178{B1,<:Any},F2<:F21178{B2}} = F1,F2,B1,B2
@test b21178(F21178{1,2}(),F21178{1,2}()) == (F21178{1,2}, F21178{1,2}, 1, 1)

# issue #21172
a21172 = f21172(x) = 2x
@test f21172(8) == 16
@test a21172 === f21172

# issue #21271
f21271() = convert(Tuple{Type{Int}, Type{Float64}}, (Int, Float64))::Tuple{Type{Int}, Type{Float64}}
f21271(x) = x::Tuple{Type{Int}, Type{Float64}}
@test_throws TypeError f21271()
@test_throws TypeError f21271((Int, Float64))

# issue #21397
bar21397(x::T) where {T} = T
foo21397(x) = bar21397(x)
@test foo21397(Tuple) == DataType

# issue 21216
primitive type FP128test <: AbstractFloat 128 end
struct FP128align <: AbstractFloat
    i::Int # cause forced misalignment
    fp::FP128test
end
let ni128 = sizeof(FP128test) ÷ sizeof(Int),
    ns128 = sizeof(FP128align) ÷ sizeof(Int),
    nbit = sizeof(Int) * 8,
    arr = Vector{FP128align}(undef, 2),
    offset = Base.datatype_alignment(FP128test) ÷ sizeof(Int),
    little,
    expected,
    arrint = reinterpret(Int, arr)

    @test length(arrint) == 2 * ns128
    arrint .= 1:(2 * ns128)
    @test sizeof(FP128test) == 16
    @test arr[1].i == 1
    @test arr[2].i == 1 + ns128
    expected = UInt128(0)
    for little in ni128:-1:1
        little += offset
        expected = (expected << nbit) + little
    end
    @test arr[1].fp == reinterpret(FP128test, expected)
    expected = UInt128(0)
    for little in ni128:-1:1
        little += offset + ns128
        expected = (expected << nbit) + little
    end
    @test reinterpret(UInt128, arr[2].fp) == expected
end

# issue #21516
struct T21516
    x::Vector{Float64}
    y::Vector{Float64}
    # check that this definition works
    T21516(x::Vector{T}, y::Vector{T}) where {T<:Real} = new(float.(x), float.(y))
end
@test isa(T21516([1],[2]).x, Vector{Float64})

# let with type declaration
let letvar::Int = 2
    letvar = 3.0
    @test letvar === 3
end

# issue #21568
f21568() = 0
function foo21568()
    y = 1
    @eval f21568(x::AbstractArray{T,1}) where {T<:Real} = $y
end
foo21568()
@test f21568([0]) == 1

# issue #21719
mutable struct T21719{V}
    f
    tol::Float64
    goal::V
end
g21719(f, goal; tol = 1e-6) = T21719(f, tol, goal)
@test isa(g21719(identity, 1.0; tol=0.1), T21719)

# Alignment of perm boxes
for i in 1:10
    # Int64 box should be 16bytes aligned even on 32bits
    ptr1 = ccall(:jl_box_int64, UInt, (Int64,), i)
    ptr2 = ccall(:jl_box_int64, UInt, (Int64,), i)
    @test ptr1 === ptr2
    @test ptr1 % 16 == 0
end

# issue #21581
global function f21581()::Int
    return 2.0
end
@test f21581() === 2
global g21581()::Int = 2.0
@test g21581() === 2
module M21581
macro bar()
    :(foo21581(x)::Int = x)
end
M21581.@bar
end
@test M21581.foo21581(1) === 1

module N21581
macro foo(var)
    quote
        function f(x::T = 1) where T
            ($(esc(var)), x)
        end
        f()
    end
end
end
let x = 8
    @test @N21581.foo(x) === (8, 1)
end

# issue #22122
let
    global @inline function f22122(x::T) where {T}
        T
    end
end
@test f22122(1) === Int

# issue #22256
mutable struct Bar22256{AParameter}
    inner::Int
end
mutable struct Foo22256
    bar::Bar22256
end
setbar22256_inner(a) = (a.bar.inner = 3; nothing)
let a_foo = Foo22256(Bar22256{true}(2))
    @test a_foo.bar.inner == 2
    setbar22256_inner(a_foo)
    @test a_foo.bar.inner == 3
end

# macro hygiene scope (#22307, #23239)
macro a22307()
    return esc(:a22307)
end
macro b22307()
    return :(@a22307)
end
function c22307()
    a22307 = 1
    return @b22307
end
a22307 = 2
@test c22307() == 2

macro identity23239b(x)
    return esc(x)
end
macro identity23239c(x)
    return quote
        $(esc(x))
    end
end
macro assign23239d(x, v)
    return esc(:($x = $v))
end
macro assign23239e(x, v)
    return quote
        $(esc(:($x = $v)))
    end
end
macro aa23239()
    return quote
        a = 1
        @identity23239b b = 2
        @identity23239c c = 3
        @assign23239d d 4
        @assign23239e e 5
        (a, b, c, d, e)
    end
end
f23239() = @aa23239()
@test @inferred(f23239()) === (1, 2, 3, 4, 5)


# issue #22026
module M22026

macro foo(TYP)
    quote
        global foofunction
        foofunction(x::Type{T}) where {T<:Number} = x
    end
end
struct Foo end
@foo Foo

macro foo2()
    quote
        global foofunction2
        (foofunction2(x::T)::Float32) where {T<:Number} = 2x
    end
end

@foo2

end
@test M22026.foofunction(Int16) === Int16
@test M22026.foofunction2(3) === 6.0f0

# tests for isdefined behavior and code generation
global undefined_variable
@test @isdefined Test
@test !@isdefined undefined_variable
@test !@isdefined undefined_variable2
@test let local_undef, local_def = 1
    !@isdefined local_undef
    @isdefined local_def
end
f_isdefined_latedef() = @isdefined f_isdefined_def
@test !f_isdefined_latedef()
f_isdefined(x) = @isdefined x
f_isdefined_undef() = @isdefined x_isundef
f_isdefined_def() = @isdefined f_isdefined_def
@test f_isdefined(1)
@test f_isdefined("")
@test !f_isdefined_undef()
@test f_isdefined_def()
@test f_isdefined_latedef()
f_isdefined_defvarI() = (x = rand(Int); @isdefined x)
f_isdefined_defvarS() = (x = randstring(1); @isdefined x)
@test f_isdefined_defvarI()
@test f_isdefined_defvarS()
f_isdefined_undefvar() = (local x; @isdefined x)
@test !f_isdefined_undefvar()
f_isdefined_unionvar(y, t) = (t > 0 && (x = (t == 1 ? 1 : y)); @isdefined x)
@test f_isdefined_unionvar(nothing, 1)
@test f_isdefined_unionvar("", 1)
@test f_isdefined_unionvar(1.0, 1)
@test f_isdefined_unionvar(1, 1)
@test !f_isdefined_unionvar(nothing, 0)
@test !f_isdefined_unionvar("", 0)
@test !f_isdefined_unionvar(1.0, 0)
@test !f_isdefined_unionvar(1, 0)
f_isdefined_splat(x...) = @isdefined x
@test f_isdefined_splat(1, 2, 3)
let err = try; @macroexpand @isdefined :x; false; catch ex; ex; end,
    __source__ = LineNumberNode(@__LINE__() - 1, Symbol(@__FILE__))
    @test err.file === string(__source__.file)
    @test err.line === __source__.line
    e = err.error::MethodError
    @test e.f === getfield(@__MODULE__, Symbol("@isdefined"))
    @test e.args === (__source__, @__MODULE__, :(:x))
end
f_isdefined_cl_1(y) = (local x; for i = 1:y; x = 2; end; () -> x; @isdefined x)
f_isdefined_cl_2(y) = (local x; for i = 1:y; x = 2; end; () -> @isdefined x)
f_isdefined_cl_3() = (x = 2; () -> x; @isdefined x)
f_isdefined_cl_4() = (local x; () -> x; @isdefined x)
f_isdefined_cl_5() = (x = 2; () -> @isdefined x)
f_isdefined_cl_6() = (local x; () -> @isdefined x)
@test f_isdefined_cl_1(1)
@test !f_isdefined_cl_1(0)
@test f_isdefined_cl_2(1)()
@test !f_isdefined_cl_2(0)()
@test f_isdefined_cl_3()
@test !f_isdefined_cl_4()
@test f_isdefined_cl_5()()
@test !f_isdefined_cl_6()()
f_isdefined_tv(::T) where {T} = @isdefined T
@test f_isdefined_tv(1)
f_isdefined_va(::T...) where {T} = @isdefined T
@test !f_isdefined_va()
@test f_isdefined_va(1, 2, 3)

# note: the constant `5` here should be > DataType.ninitialized.
# This tests that there's no crash due to accessing Type.body.layout.
let f(n) = isdefined(typeof(n), 5)
    @test f(0) === false
    @test isdefined(Int, 5) === false
end

# @isdefined in a loop
let a = []
    for i = 1:2
        push!(a, @isdefined(j))
        local j = 1
    end
    @test a == [false, false]
end

# while loop scope
let a = [], i = 0
    while i < (local b = 2)
        push!(a, @isdefined(j))
        local j = 1
        i += 1
    end
    @test a == [false, false]
    @test b == 2
end

mutable struct MyStruct22929
    x::MyStruct22929
    MyStruct22929() = new()
end
isdefined_22929_1(x) = isdefined(x, 1)
isdefined_22929_x(x) = isdefined(x, :x)
m22929_1 = MyStruct22929()
m22929_2 = MyStruct22929()
m22929_2.x = m22929_1
@test !isdefined_22929_1(m22929_1)
@test !isdefined_22929_x(m22929_1)
@test isdefined_22929_1(m22929_2)
@test isdefined_22929_x(m22929_2)

# Union type sorting
for T in (
        (Nothing, Int8),
        (Nothing, Int64),
        (Nothing, Tuple{Int64, String}),
        (Nothing, Array),
        (Float64, Int64),
        (Float64, String),
        (Float64, Array),
        (String, Array),
        (Int64, Tuple{Int64, Float64}),
        (Tuple{Int64, Float64}, Array)
    )
    @test Base.uniontypes(Union{T...}) == collect(T)
    @test Base.uniontypes(Union{reverse(T)...}) == collect(T)
end
@test Base.uniontypes(Union{Nothing, Union{Int64, Float64}}) == Any[Nothing, Float64, Int64]
module AlternativeIntModule
    struct Int64
        val::UInt64
    end
end
@test Base.uniontypes(Union{Int64, AlternativeIntModule.Int64}) == Any[AlternativeIntModule.Int64, Int64]
@test Base.uniontypes(Union{AlternativeIntModule.Int64, Int64}) == Any[AlternativeIntModule.Int64, Int64]
# because DAlternativeIntModule is alphabetically after Core.Int64
module DAlternativeIntModule
    struct Int64
        val::UInt64
    end
end
@test Base.uniontypes(Union{Int64, DAlternativeIntModule.Int64}) == Any[Int64, DAlternativeIntModule.Int64]
@test Base.uniontypes(Union{DAlternativeIntModule.Int64, Int64}) == Any[Int64, DAlternativeIntModule.Int64]
@test Base.uniontypes(Union{Vector{Int8}, Vector{Int16}}) == Base.uniontypes(Union{Vector{Int16}, Vector{Int8}})
mutable struct ANonIsBitsType
    v::Int64
end
@test Base.uniontypes(Union{Int64, ANonIsBitsType}) == Base.uniontypes(Union{ANonIsBitsType, Int64})

# issue 18933
module GlobalDef18933
    using Test
    import Base.sqrt
    # test that global declaration vs assignment operates correctly in local scope
    f() = (global sin; nothing)
    g() = (global cos; cos = 2; nothing)
    h() = (global sqrt; nothing)
    @test !@isdefined sin
    @test !@isdefined cos
    @test @isdefined sqrt
    f()
    g()
    h()
    @test !@isdefined sin
    @test @isdefined cos
    @test sqrt === Base.sqrt
    @test cos === 2
    # test that function definitions declared global
    # introduce a new, local global
    let
        global tan
        @test !@isdefined tan
        tan() = nothing
        @test @isdefined tan
        @test tan() === nothing
    end
    # test that global declaration side-effects don't ignore conditionals
    if false
        global sincos
        nothing
    end
    @test which(Main, :sincos) === Base.Math
    @test @isdefined sincos
    @test sincos === Base.sincos
end

# issue #23218
let idx = (7,5,9)
    (v,) = (idx...,)
    @test v == 7
end

module UnionOptimizations

using Test
using Dates
using Random

const boxedunions = [Union{}, Union{String, Nothing}]
const unboxedunions = [Union{Int8, Nothing},
                       Union{Int8, Float16, Nothing},
                       Union{Int8, UInt8, Int16, UInt16, Int32, UInt32, Int64, UInt64, Int128, UInt128},
                       Union{Char, Date, Int}]

@test !Base.isbitsunion(boxedunions[1])
@test !Base.isbitsunion(boxedunions[2])
@test Base.isbitsunion(unboxedunions[1])
@test Base.isbitsunion(unboxedunions[2])
@test Base.isbitsunion(unboxedunions[3])

@test Base.bitsunionsize(unboxedunions[1]) == 1
@test Base.bitsunionsize(unboxedunions[2]) == 2
@test Base.bitsunionsize(unboxedunions[3]) == 16
@test Base.bitsunionsize(unboxedunions[4]) == 8

@test sizeof(unboxedunions[1]) == 1
@test sizeof(unboxedunions[2]) == 2
@test sizeof(unboxedunions[3]) == 16
@test sizeof(unboxedunions[4]) == 8

initvalue(::Type{Nothing}) = nothing
initvalue(::Type{Char}) = '\0'
initvalue(::Type{Date}) = Date(0, 12, 31)
initvalue(::Type{T}) where {T <: Number} = T(0)

initvalue2(::Type{Nothing}) = nothing
initvalue2(::Type{Char}) = Char(0x01)
initvalue2(::Type{Date}) = Date(1)
initvalue2(::Type{T}) where {T <: Number} = T(1)

U = unboxedunions[1]

@noinline compare(a, b) = (a === b) # make sure we are testing code-generation of `is`
egal(x, y) = (ccall(:jl_egal, Cint, (Any, Any), x, y) != 0) # make sure we are NOT testing code-generate of `is`

mutable struct UnionField
    u::U
end

let x = UnionField(initvalue(Base.uniontypes(U)[1]))
    @test x.u === initvalue(Base.uniontypes(U)[1])
    x.u = initvalue2(Base.uniontypes(U)[1])
    @test x.u === initvalue2(Base.uniontypes(U)[1])
    x.u = initvalue(Base.uniontypes(U)[2])
    @test x.u === initvalue(Base.uniontypes(U)[2])
end

mutable struct UnionField2
    x::Union{Nothing, Int}
    @noinline UnionField2() = new()
end
@test UnionField2().x === nothing

struct UnionField3
    x::Union{Nothing, Int}
    @noinline UnionField3() = new()
end
@test UnionField3().x === nothing

mutable struct UnionField4
    x::Union{Nothing, Float64}
    y::Union{Nothing, Int8}
    z::NTuple{8, UInt8}
    @noinline UnionField4() = new()
    @noinline UnionField4(x, y) = new(x, y, (0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88))
end
@test UnionField4().x === nothing
@test UnionField4().y === nothing
let x4 = UnionField4(nothing, Int8(3))
    x4copy = deepcopy(x4)
    @test x4.x === nothing
    @test x4.y === Int8(3)
    @test x4.z[1] === 0x11
    @test compare(x4, x4)
    @test x4 == x4
    @test egal(x4, x4)
    @test !(x4 === x4copy)
    @test !(x4 == x4copy)
    @test !egal(x4, x4copy)
end

struct UnionField5
    x::Union{Nothing, Float64}
    y::Union{Nothing, Int8}
    z::NTuple{8, UInt8}
    @noinline UnionField5() = new()
    @noinline UnionField5(x, y) = new(x, y, (0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88))
end
@test UnionField5().x === nothing
@test UnionField5().y === nothing
let x5 = UnionField5(nothing, Int8(3))
    x5copy = deepcopy(x5)
    @test x5.x === nothing
    @test x5.y === Int8(3)
    @test x5.z[1] === 0x11
    @test compare(x5, x5)
    @test x5 == x5
    @test compare(x5, x5copy)
    @test x5 == x5copy
    @test egal(x5, x5copy)
    @test objectid(x5) === objectid(x5copy)
    @test hash(x5) === hash(x5copy)
end

struct UnionField6
    alignment::Int32
    padding::NTuple{3, UInt8}
    #= implicit-padding::UInt8 =#
    maybe_val::Union{UInt16, Nothing} # offset = 8, align = 8, size = 2
end
@test UnionField6(1,(1,1,1),2018).maybe_val == 2018

# PR #23367
struct A23367
    x::Union{Int8, Int16, NTuple{7, Int8}, Nothing}
end
struct B23367
    x::Int8
    y::A23367
    z::Int8
end
@noinline get_x(a::A23367) = a.x
function constant23367 end
let
    b = B23367(91, A23367(ntuple(i -> Int8(i), Val(7))), 23)
    @eval @noinline constant23367(a, b) = (a ? b : $b)
    b2 = Ref(b)[] # copy b via field assignment
    b3 = B23367[b][1] # copy b via array assignment
    addr(@nospecialize x) = ccall(:jl_value_ptr, Ptr{Cvoid}, (Any,), x)
    @test addr(b)  == addr(b)
    @test addr(b)  == addr(b2)
    @test addr(b)  == addr(b3)
    @test addr(b2) == addr(b3)

    @test b === b2 === b3 === b
    @test egal(b, b2) && egal(b2, b3) && egal(b3, b)
    @test compare(b, b2) && compare(b, b3) && compare(b2, b3)
    @test objectid(b) === objectid(b2) == objectid(b3)
    @test b.x === Int8(91)
    @test b.z === Int8(23)
    @test b.y === A23367((Int8(1), Int8(2), Int8(3), Int8(4), Int8(5), Int8(6), Int8(7)))
    @test sizeof(b) == sizeof(Int) * 3
    @test A23367(Int8(1)).x === Int8(1)
    @test A23367(Int8(0)).x === Int8(0)
    @test A23367(Int16(1)).x === Int16(1)
    @test A23367(nothing).x === nothing
    @test sizeof(b.y) == 8
    @test get_x(A23367(Int8(1))) === Int8(1)

    # test code-generation of constants
    other = B23367(91, A23367(nothing), 23)
    @test constant23367(true, other) === other
    @test constant23367(false, other) === b
end

for U in boxedunions
    local U
    for N in (1, 2, 3, 4)
        A = Array{U}(undef, ntuple(x->0, N)...)
        @test isempty(A)
        @test sizeof(A) == 0

        A = Array{U}(undef, ntuple(x->10, N)...)
        @test length(A) == 10^N
        @test sizeof(A) == sizeof(Int) * (10^N)
        @test !isassigned(A, 1)
    end
end

# unsafe_wrap
let
    A4 = [1, 2, 3]
    @test_throws ArgumentError unsafe_wrap(Array, convert(Ptr{Union{Int, Nothing}}, pointer(A4)), 3)
    A5 = [1 2 3; 4 5 6]
    @test_throws ArgumentError unsafe_wrap(Array, convert(Ptr{Union{Int, Nothing}}, pointer(A5)), 6)
end

# copyto!
A23567 = Vector{Union{Float64, Nothing}}(undef, 5)
B23567 = collect(Union{Float64, Nothing}, 1.0:3.0)
copyto!(A23567, 2, B23567)
@test A23567[1] === nothing
@test A23567[2] === 1.0
@test A23567[3] === 2.0
@test A23567[4] === 3.0

# vcat
t2 = deepcopy(A23567)
t3 = deepcopy(A23567)
t4 = vcat(A23567, t2, t3)
@test t4[1:5] == A23567
@test t4[6:10] == A23567
@test t4[11:15] == A23567

using Serialization

for U in unboxedunions
    local U
    for N in (1, 2, 3, 4)
        A = Array{U}(undef, ntuple(x->0, N)...)
        @test isempty(A)
        @test sizeof(A) == 0

        len = ntuple(x->10, N)
        mxsz = maximum(sizeof, Base.uniontypes(U))
        A = Array{U}(undef, len)
        @test length(A) == prod(len)
        @test sizeof(A) == prod(len) * mxsz
        @test isassigned(A, 1)
        @test isassigned(A, length(A))

        # arrayref / arrayset
        F = Base.uniontypes(U)[1]
        @test A[1] === initvalue(F)
        A[1] = initvalue2(F)
        @test A[1] === initvalue2(F)

        F2 = Base.uniontypes(U)[2]
        A[2] = initvalue(F2)
        @test A[2] === initvalue(F2)

        for (i, U2) in enumerate(Base.uniontypes(U))
            A[i] = initvalue2(U2)
            @test A[i] === initvalue2(U2)
        end

        # serialize / deserialize
        io = IOBuffer()
        serialize(io, A)
        seekstart(io)
        A2 = deserialize(io)
        @test A == A2

        # reshape
        A3 = reshape(A, (div(prod(len), 2), 2))
        @test sizeof(A) == prod(len) * mxsz
        @test isassigned(A, 1)
        @test A[1] === initvalue2(F)

        # copy
        A4 = copy(A)
        @test A == A4

        if N == 1
            ## Dequeue functions
            # pop!
            F2 = Base.uniontypes(U)[2]
            len = len[1]
            A = U[initvalue2(F2) for i = 1:len]
            for i = 1:len
                @test A[end] === initvalue2(F2)
                v = pop!(A)
                @test v === initvalue2(F2)
            end
            @test isempty(A)

            # popfirst!
            A = U[initvalue2(F2) for i = 1:len]
            for i = 1:len
                @test A[1] === initvalue2(F2)
                popfirst!(A)
            end
            @test isempty(A)

            # empty!
            A = U[initvalue2(F2) for i = 1:len]
            empty!(A)
            @test isempty(A)

            # resize!
            A = U[initvalue2(F2) for i = 1:len]
            resize!(A, 1)
            @test length(A) === 1
            @test A[1] === initvalue2(F2)
            resize!(A, len)
            @test length(A) === len
            @test A[1] === initvalue2(F2)
            @test typeof(A[end]) === F

            # deleteat!
            F = Base.uniontypes(U)[2]
            A = U[rand(F(1):F(len)) for i = 1:len]
            # The 2-arg `unique` method works around #22688
            deleteat!(A, map(Int, sort!(unique(identity, A[1:4]))))
            A = U[initvalue2(F2) for i = 1:len]
            deleteat!(A, 1:2)
            @test length(A) == len - 2
            @test all(A .== initvalue2(F2))
            deleteat!(A, 1:2)
            @test length(A) == len - 4
            @test all(A .== initvalue2(F2))
            A = U[initvalue2(F2) for i = 1:len]
            deleteat!(A, length(A)-1:length(A))
            @test length(A) == len - 2
            @test all(A .== initvalue2(F2))
            deleteat!(A, length(A)-1:length(A))
            @test length(A) == len - 4
            @test all(A .== initvalue2(F2))
            A = U[initvalue2(F2) for i = 1:len]
            deleteat!(A, 2:3)
            @test length(A) == len - 2
            @test all(A .== initvalue2(F2))
            A = U[initvalue2(F2) for i = 1:len]
            deleteat!(A, length(A)-2:length(A)-1)
            @test length(A) == len - 2
            @test all(A .== initvalue2(F2))

            # pushfirst!
            A = U[initvalue2(F2) for i = 1:len]
            for i = 1:5
                pushfirst!(A, initvalue2(F))
                pushfirst!(A, initvalue(F2))
                @test A[1] === initvalue(F2)
                @test A[2] === initvalue2(F)
            end

            # push! / append! / prepend!
            A = U[initvalue2(F2) for i = 1:len]
            push!(A, initvalue2(F))
            @test A[end] === initvalue2(F)
            push!(A, initvalue2(F2))
            @test A[end] === initvalue2(F2)
            append!(A, [initvalue(F), initvalue2(F)])
            @test A[end] === initvalue2(F)
            @test A[end-1] === initvalue(F)
            prepend!(A, [initvalue(F), initvalue2(F)])
            @test A[2] === initvalue2(F)
            @test A[1] === initvalue(F)

            # insert!
            A = U[initvalue2(F2) for i = 1:len]
            insert!(A, 2, initvalue2(F))
            @test A[2] === initvalue2(F)
            @test A[1] === initvalue2(F2)
            @test A[3] === initvalue2(F2)
            @test A[end] === initvalue2(F2)
            A = U[initvalue2(F2) for i = 1:len]
            insert!(A, 8, initvalue2(F))
            @test A[8] === initvalue2(F)
            @test A[7] === initvalue2(F2)
            @test A[9] === initvalue2(F2)
            @test A[end] === initvalue2(F2)

            # splice!
            A = U[initvalue2(F2) for i = 1:len]
            V = splice!(A, 1:2)
            @test length(A) == len - 2
            @test length(V) == 2
            @test V[1] == initvalue2(F2)
            @test V[2] == initvalue2(F2)
            @test A[1] == initvalue2(F2)
            @test A[end] == initvalue2(F2)

            A = U[initvalue2(F2) for i = 1:len]
            V = splice!(A, 4:5)
            @test length(A) == len - 2
            @test length(V) == 2
            @test V[1] == initvalue2(F2)
            @test V[2] == initvalue2(F2)
            @test A[1] == initvalue2(F2)
            @test A[end] == initvalue2(F2)
        end
    end
end

@testset "jl_array_grow_at_end" begin

# start w/ array, set & check elements, grow it, check that elements stayed correct, set & check elements
A = Vector{Union{Missing, UInt8}}(undef, 2)
Base.arrayset(true, A, 0x01, 1)
Base.arrayset(true, A, missing, 2)
@test Base.arrayref(true, A, 1) === 0x01
@test Base.arrayref(true, A, 2) === missing

# grow_at_end 2
resize!(A, 5)
@test Base.arrayref(true, A, 1) === 0x01
@test Base.arrayref(true, A, 2) === missing
@test Base.arrayref(true, A, 3) === missing
@test Base.arrayref(true, A, 4) === missing
@test Base.arrayref(true, A, 5) === missing
Base.arrayset(true, A, 0x03, 3)
Base.arrayset(true, A, missing, 4)
Base.arrayset(true, A, 0x05, 5)
@test Base.arrayref(true, A, 1) === 0x01
@test Base.arrayref(true, A, 2) === missing
@test Base.arrayref(true, A, 3) === 0x03
@test Base.arrayref(true, A, 4) === missing
@test Base.arrayref(true, A, 5) === 0x05

# grow_at_end 1
Base._growat!(A, 4, 1)
@test Base.arrayref(true, A, 1) === 0x01
@test Base.arrayref(true, A, 2) === missing
@test Base.arrayref(true, A, 3) === 0x03
@test Base.arrayref(true, A, 4) === missing
@test Base.arrayref(true, A, 5) === missing
@test Base.arrayref(true, A, 6) === 0x05

Base.arrayset(true, A, missing, 1)
Base.arrayset(true, A, 0x02, 2)
Base.arrayset(true, A, missing, 3)
Base.arrayset(true, A, 0x04, 4)
Base.arrayset(true, A, missing, 5)
Base.arrayset(true, A, 0x06, 6)
@test Base.arrayref(true, A, 1) === missing
@test Base.arrayref(true, A, 2) === 0x02
@test Base.arrayref(true, A, 3) === missing
@test Base.arrayref(true, A, 4) === 0x04
@test Base.arrayref(true, A, 5) === missing
@test Base.arrayref(true, A, 6) === 0x06

# grow_at_end 5
Base._growat!(A, 4, 1)
@test Base.arrayref(true, A, 1) === missing
@test Base.arrayref(true, A, 2) === 0x02
@test Base.arrayref(true, A, 3) === missing
@test Base.arrayref(true, A, 4) === missing
@test Base.arrayref(true, A, 5) === 0x04
@test Base.arrayref(true, A, 6) === missing
@test Base.arrayref(true, A, 7) === 0x06

# grow_at_end 6
resize!(A, 8)
@test Base.arrayref(true, A, 1) === missing
@test Base.arrayref(true, A, 2) === 0x02
@test Base.arrayref(true, A, 3) === missing
@test Base.arrayref(true, A, 4) === missing
@test Base.arrayref(true, A, 5) === 0x04
@test Base.arrayref(true, A, 6) === missing
@test Base.arrayref(true, A, 7) === 0x06
@test Base.arrayref(true, A, 8) === missing

# grow_at_end 4
resize!(A, 1048576)
resize!(A, 1048577)
@test Base.arrayref(true, A, 1) === missing
@test Base.arrayref(true, A, 2) === 0x02
@test Base.arrayref(true, A, 3) === missing
@test Base.arrayref(true, A, 4) === missing
@test Base.arrayref(true, A, 5) === 0x04
@test Base.arrayref(true, A, 6) === missing
@test Base.arrayref(true, A, 7) === 0x06
@test Base.arrayref(true, A, 8) === missing
foreach(9:1048577) do i
    @test Base.arrayref(true, A, i) === missing
end
foreach(9:1048577) do i
    Base.arrayset(true, A, i % UInt8, i)
    @test Base.arrayref(true, A, i) === i % UInt8
end

# grow_at_end 3
A = Vector{Union{Missing, UInt8}}(undef, 1048577)
foreach(1:1048577) do i
    @test Base.arrayref(true, A, i) === missing
    Base.arrayset(true, A, i % UInt8, i)
    @test Base.arrayref(true, A, i) === i % UInt8
end
Base._growat!(A, 1048576, 1)
@test length(A) == 1048578
foreach(1:1048575) do i
    @test Base.arrayref(true, A, i) === i % UInt8
    @test A[i] === i % UInt8
end
@test Base.arrayref(true, A, 1048576) === missing
@test Base.arrayref(true, A, 1048577) === 1048576 % UInt8
@test Base.arrayref(true, A, 1048578) === 1048577 % UInt8

end # @testset

@testset "jl_array_grow_at_beg" begin

# grow_at_beg 4
A = Vector{Union{Missing, UInt8}}(undef, 5)
Base.arrayset(true, A, 0x01, 1)
Base.arrayset(true, A, missing, 2)
Base.arrayset(true, A, 0x03, 3)
Base.arrayset(true, A, missing, 4)
Base.arrayset(true, A, 0x05, 5)
Base._growat!(A, 1, 1)

@test Base.arrayref(true, A, 1) === missing
@test Base.arrayref(true, A, 2) === 0x01
@test Base.arrayref(true, A, 3) === missing
@test Base.arrayref(true, A, 4) === 0x03
@test Base.arrayref(true, A, 5) === missing
@test Base.arrayref(true, A, 6) === 0x05

# grow_at_beg 2
Base._growat!(A, 1, 1)
@test Base.arrayref(true, A, 1) === missing
@test Base.arrayref(true, A, 2) === missing
@test Base.arrayref(true, A, 3) === 0x01
@test Base.arrayref(true, A, 4) === missing
@test Base.arrayref(true, A, 5) === 0x03
@test Base.arrayref(true, A, 6) === missing
@test Base.arrayref(true, A, 7) === 0x05

# grow_at_beg 1
Base._growat!(A, 2, 1)
@test Base.arrayref(true, A, 1) === missing
@test Base.arrayref(true, A, 2) === missing
@test Base.arrayref(true, A, 3) === missing
@test Base.arrayref(true, A, 4) === 0x01
@test Base.arrayref(true, A, 5) === missing
@test Base.arrayref(true, A, 6) === 0x03
@test Base.arrayref(true, A, 7) === missing
@test Base.arrayref(true, A, 8) === 0x05

# grow_at_beg 9
Base._growat!(A, 1, 1)
@test Base.arrayref(true, A, 1) === missing
@test Base.arrayref(true, A, 2) === missing
@test Base.arrayref(true, A, 3) === missing
@test Base.arrayref(true, A, 4) === missing
@test Base.arrayref(true, A, 5) === 0x01
@test Base.arrayref(true, A, 6) === missing
@test Base.arrayref(true, A, 7) === 0x03
@test Base.arrayref(true, A, 8) === missing
@test Base.arrayref(true, A, 9) === 0x05

# grow_at_beg 8
A = Vector{Union{Missing, UInt8}}(undef, 5)
Base.arrayset(true, A, 0x01, 1)
Base.arrayset(true, A, missing, 2)
Base.arrayset(true, A, 0x03, 3)
Base.arrayset(true, A, missing, 4)
Base.arrayset(true, A, 0x05, 5)
Base._growat!(A, 2, 1)
Base._growat!(A, 2, 1)

@test Base.arrayref(true, A, 1) === 0x01
@test Base.arrayref(true, A, 2) === missing
@test Base.arrayref(true, A, 3) === missing
@test Base.arrayref(true, A, 4) === missing
@test Base.arrayref(true, A, 5) === 0x03
@test Base.arrayref(true, A, 6) === missing
@test Base.arrayref(true, A, 7) === 0x05

# grow_at_beg 5
A = Vector{Union{Missing, UInt8}}(undef, 5)
Base.arrayset(true, A, 0x01, 1)
Base.arrayset(true, A, missing, 2)
Base.arrayset(true, A, 0x03, 3)
Base.arrayset(true, A, missing, 4)
Base.arrayset(true, A, 0x05, 5)
Base._growat!(A, 4, 1)
Base._growat!(A, 4, 1)

@test Base.arrayref(true, A, 1) === 0x01
@test Base.arrayref(true, A, 2) === missing
@test Base.arrayref(true, A, 3) === 0x03
@test Base.arrayref(true, A, 4) === missing
@test Base.arrayref(true, A, 5) === missing
@test Base.arrayref(true, A, 6) === missing
@test Base.arrayref(true, A, 7) === 0x05

# grow_at_beg 6
Base._growat!(A, 2, 3)
@test Base.arrayref(true, A, 1) === 0x01
@test Base.arrayref(true, A, 2) === missing
@test Base.arrayref(true, A, 3) === missing
@test Base.arrayref(true, A, 4) === missing
@test Base.arrayref(true, A, 5) === missing
@test Base.arrayref(true, A, 6) === 0x03
@test Base.arrayref(true, A, 7) === missing
@test Base.arrayref(true, A, 8) === missing
@test Base.arrayref(true, A, 9) === missing
@test Base.arrayref(true, A, 10) === 0x05

# grow_at_beg 3
A = Vector{Union{Missing, UInt8}}(undef, 1048577)
Base.arrayset(true, A, 0x01, 1)
Base.arrayset(true, A, missing, 2)
Base.arrayset(true, A, 0x03, 3)
Base.arrayset(true, A, missing, 4)
Base.arrayset(true, A, 0x05, 5)
Base._growat!(A, 2, 1)

@test Base.arrayref(true, A, 1) === 0x01
@test Base.arrayref(true, A, 2) === missing
@test Base.arrayref(true, A, 3) === missing
@test Base.arrayref(true, A, 4) === 0x03
@test Base.arrayref(true, A, 5) === missing
@test Base.arrayref(true, A, 6) === 0x05

foreach(7:length(A)) do i
    @test Base.arrayref(true, A, i) === missing
    Base.arrayset(true, A, i % UInt8, i)
    @test Base.arrayref(true, A, i) === i % UInt8
end

end # @testset

@testset "jl_array_del_at_beg" begin

A = Vector{Union{Missing, UInt8}}(undef, 5)
Base.arrayset(true, A, 0x01, 1)
Base.arrayset(true, A, missing, 2)
Base.arrayset(true, A, 0x03, 3)
Base.arrayset(true, A, missing, 4)
Base.arrayset(true, A, 0x05, 5)
Base._deleteat!(A, 2, 1)

@test Base.arrayref(true, A, 1) === 0x01
@test Base.arrayref(true, A, 2) === 0x03
@test Base.arrayref(true, A, 3) === missing
@test Base.arrayref(true, A, 4) === 0x05

Base._deleteat!(A, 1, 1)
@test Base.arrayref(true, A, 1) === 0x03
@test Base.arrayref(true, A, 2) === missing
@test Base.arrayref(true, A, 3) === 0x05

A = Vector{Union{Missing, UInt8}}(undef, 5)
Base.arrayset(true, A, 0x01, 1)
Base.arrayset(true, A, missing, 2)
Base.arrayset(true, A, 0x03, 3)
Base.arrayset(true, A, missing, 4)
Base.arrayset(true, A, 0x05, 5)
Base._growat!(A, 1, 1)
@test Base.arrayref(true, A, 1) === missing
@test Base.arrayref(true, A, 2) === 0x01
@test Base.arrayref(true, A, 3) === missing
@test Base.arrayref(true, A, 4) === 0x03
@test Base.arrayref(true, A, 5) === missing
@test Base.arrayref(true, A, 6) === 0x05
Base._deleteat!(A, 2, 1)
@test Base.arrayref(true, A, 1) === missing
@test Base.arrayref(true, A, 2) === missing
@test Base.arrayref(true, A, 3) === 0x03
@test Base.arrayref(true, A, 4) === missing
@test Base.arrayref(true, A, 5) === 0x05
Base._deleteat!(A, 1, 2)
@test Base.arrayref(true, A, 1) === 0x03
@test Base.arrayref(true, A, 2) === missing
@test Base.arrayref(true, A, 3) === 0x05
Base._deleteat!(A, 1, 1)
@test Base.arrayref(true, A, 1) === missing
@test Base.arrayref(true, A, 2) === 0x05

end # @testset

@testset "jl_array_del_at_end" begin

A = Vector{Union{Missing, UInt8}}(undef, 5)
Base.arrayset(true, A, 0x01, 1)
Base.arrayset(true, A, missing, 2)
Base.arrayset(true, A, 0x03, 3)
Base.arrayset(true, A, missing, 4)
Base.arrayset(true, A, 0x05, 5)
Base._deleteat!(A, 5, 1)

@test Base.arrayref(true, A, 1) === 0x01
@test Base.arrayref(true, A, 2) === missing
@test Base.arrayref(true, A, 3) === 0x03
@test Base.arrayref(true, A, 4) === missing

Base._deleteat!(A, 3, 1)
@test Base.arrayref(true, A, 1) === 0x01
@test Base.arrayref(true, A, 2) === missing
@test Base.arrayref(true, A, 3) === missing

end # @testset

# issue #27767
let A=Vector{Union{Int, Missing}}(undef, 1)
    resize!(A, 2)
    @test length(A) == 2
    @test A[2] === missing
end

# issue #27809
let A=Vector{Union{Int, Missing}}(undef, 0)
    while length(A) < 2^17
        push!(A, 0.0)
    end
    push!(A, 0.0)
    @test !any(ismissing, A)
end

# jl_array_shrink
let A=Vector{Union{UInt8, Missing}}(undef, 1048577)
    Base.arrayset(true, A, 0x01, 1)
    Base.arrayset(true, A, missing, 2)
    Base.arrayset(true, A, 0x03, 3)
    Base.arrayset(true, A, missing, 4)
    Base.arrayset(true, A, 0x05, 5)
    deleteat!(A, 6:1048577)
    @test Base.arrayref(true, A, 1) === 0x01
    @test Base.arrayref(true, A, 2) === missing
    @test Base.arrayref(true, A, 3) === 0x03
    @test Base.arrayref(true, A, 4) === missing
    @test Base.arrayref(true, A, 5) === 0x05
    sizehint!(A, 5)
    @test Base.arrayref(true, A, 1) === 0x01
    @test Base.arrayref(true, A, 2) === missing
    @test Base.arrayref(true, A, 3) === 0x03
    @test Base.arrayref(true, A, 4) === missing
    @test Base.arrayref(true, A, 5) === 0x05
end

# copyto!/vcat w/ internal padding
let A=[0, missing], B=[missing, 0], C=Vector{Union{Int, Missing}}(undef, 6)
    push!(A, missing)
    push!(B, missing)
    @test isequal(vcat(A, B), [0, missing, missing, missing, 0, missing])
    copyto!(C, 1, A)
    copyto!(C, 4, B)
    @test isequal(C, [0, missing, missing, missing, 0, missing])
end

# issue #29718
function f29718()
    nt = NamedTuple{(:a, :b, :c, :d, :e, :f,),
                    Tuple{Union{Missing, Float64},
                          Tuple{UInt8},
                          Union{Missing, Int8},
                          Int8,
                          Tuple{UInt8,UInt8},
                          Union{Missing, Int16}}
                    }((missing,
                       (1,),
                       1,
                       41,
                       (1,2),
                       1915,
                       ))
    return Ref{Any}(nt)[].f
end
@test f29718() == 1915

end # module UnionOptimizations

# issue #6614, argument destructuring
f6614((x, y)) = [x, y]
@test f6614((4, 3)) == [4, 3]
g6614((x, y), (z,), (a, b)) = (x,y,z,a,b)
@test g6614((1, 2), (3,), (4, 5)) === (1,2,3,4,5)
@test_throws MethodError g6614(1, 2)
@test_throws MethodError g6614((1, 2), (3,))
@test_throws BoundsError g6614((1, 2), (3,), (1,))
h6614((x, y) = (5, 6)) = (y, x)
@test h6614() == (6, 5)
@test h6614((4, 5)) == (5, 4)
ff6614((x, y)::Tuple{Int, String}) = (x, y)
@test ff6614((1, "")) == (1, "")
@test_throws MethodError ff6614((1, 1))
gg6614((x, y)::Tuple{Int, String} = (2, " ")) = (x, y)
@test gg6614() == (2, " ")
function hh6614()
    x, y = 1, 2
    function g((x,y))
        # make sure x and y are local
    end
    g((4,5))
    x, y
end
@test hh6614() == (1, 2)
# issue #26518
function f26518((a,b)) end
@test f26518((1,2)) === nothing

# issue 22098
macro m22098 end
handle_on_m22098 = getfield(@__MODULE__, Symbol("@m22098"))
@test isempty(methods(handle_on_m22098))

# issue 24363
mutable struct A24363
    x::Union{Int,Nothing}
end

int24363 = A24363(65535)
void24363 = A24363(nothing)
f24363(a) = a.x
@test f24363(int24363) === 65535
@test f24363(void24363) === nothing

# issue 17149
mutable struct Foo17149
end
@test Foo17149() !== Foo17149()
let a = Foo17149()
    @test a === a
end

# issue #21004
const PTuple_21004{N,T} = NTuple{N,VecElement{T}}
@test_throws ArgumentError PTuple_21004(1)
@test_throws UndefVarError PTuple_21004_2{N,T} = NTuple{N, VecElement{T}}(1)

#issue #22792
foo_22792(::Type{<:Union{Int8,Int,UInt}}) = 1;
@test foo_22792(Union{Int,UInt}) == 1
foo_22792(::Union) = 2;
@test foo_22792(Union{Int,UInt}) == 1
@test foo_22792(Union{Int8,UInt}) == 1
@test foo_22792(Union{Int,UInt}) == 1

# issue #25907
g25907a(x) = x[1]::Integer
@test g25907a(Union{Int, UInt, Nothing}[1]) === 1
g25907b(x) = x[1]::Complex
@test g25907b(Union{Complex{Int}, Complex{UInt}, Nothing}[1im]) === 1im

#issue #26363
@test eltype(Ref(Float64(1))) === Float64
@test ndims(Ref(1)) === 0
@test collect(Ref(1)) == [v for v in Ref(1)] == fill(1)
@test axes(Ref(1)) === size(Ref(1)) === ()

# issue #23206
g1_23206(::Tuple{Type{Int}, T}) where T = 0
g2_23206(::Tuple{Type{Int}}) = 1
@test_throws MethodError g1_23206(tuple(Int, 2))
@test_throws MethodError g2_23206(tuple(Int, 2))

# issue #26739
let x26739 = Int[1]
    @test eval(:(identity.($x26739))) == x26739
end

# issue #27018
@test Base.isvatuple(Tuple{Float64,Vararg{Int}})
@test Base.isvatuple(Tuple{T,Vararg{Int}} where T)
@test Base.isvatuple(Tuple{Int,Int,Vararg{Int,N}} where N)
@test Base.isvatuple(Tuple{T,S,Vararg{T}} where T<:S where S)
@test Base.isvatuple(Tuple{T,S,Vararg{T,3}} where T<:S where S)
@test !Base.isvatuple(Tuple{Float64,Vararg{Int,1}})
@test !Base.isvatuple(Tuple{T,Vararg{Int,2}} where T)
@test !Base.isvatuple(Tuple{Int,Int,Vararg{Int,2}})

# Issue 27103
function f27103()
    a = @isdefined x
    x = 3
    b = @isdefined x
    (a, b)
end
@test f27103() == (false, true)

g27103() = @isdefined z27103
@test g27103() == false
z27103 = 1
@test g27103() == true

# Issue 27181
struct A27181
    typ::Type
end

struct C27181
    val
end

function f27181()
    invoke(A27181(C27181).typ, Tuple{Any}, nothing)
end
@test f27181() == C27181(nothing)

# Issue #27204
struct Foo27204{T}
end
(::Foo27204{Int})() = 1
(::Foo27204{Float64})() = 2
@noinline f27204(x) = x ? Foo27204{Int}() : Foo27204{Float64}()
foo27204(x) = f27204(x)()
@test foo27204(true) == 1
@test foo27204(false) == 2

# Issue 27209
@noinline function f27209(x::Union{Float64, Nothing})
    if x === nothing
        y = x; return @isdefined(y)
    else
        return @isdefined(y)
    end
end
g27209(x) = f27209(x ? nothing : 1.0)
@test g27209(true) == true

# Issue 27240
@inline function foo27240()
    if rand(Bool)
        return foo_nonexistant_27240
    else
        return bar_nonexistant_27240
    end
end
bar27240() = foo27240()
@test_throws UndefVarError bar27240()

# issue #27269
struct T27269{X, Y <: Vector{X}}
    v::Vector{Y}
end
@test T27269([[1]]) isa T27269{Int, Vector{Int}}

# issue #27368
struct Combinator27368
    op
    args::Vector{Any}
    Combinator27368(op, args...) =
        new(op, collect(Any, args))
end
field27368(name) =
    Combinator27368(field27368, name)
translate27368(name::Symbol) =
    translate27368(Val{name})
translate27368(::Type{Val{name}}) where {name} =
    field27368(name)
@test isa(translate27368(:name), Combinator27368)

# issue #27456
@inline foo27456() = try baz_nonexistent27456(); catch; nothing; end
bar27456() = foo27456()
@test bar27456() == nothing

# issue #27365
mutable struct foo27365
    x::Float64
    foo27365() = new()
end

function baz27365()
    data = foo27365()
    return data.x
end

@test isa(baz27365(), Float64)

# Issue #27566
function test27566(a,b)
    c = (b,(0,1)...)
    test27566(a, c...)
end
test27566(a, b, c, d) = a.*(b, c, d)
@test test27566(1,1) == (1,0,1)

# Issue #27594
struct Iter27594 end
Base.iterate(::Iter27594) = (1, nothing)
Base.iterate(::Iter27594, ::Any) = nothing

function foo27594()
    ind = 0
    for x in (1,)
        for y in Iter27594()
            ind += 1
        end
    end
    ind
end

@test foo27594() == 1

# Issue 27597
function f27597(y)
    x = Int[]

    if isempty(y)
        y = 1:length(x)
    elseif false
        ;
    end

    length(y)
    return y
end
@test f27597([1]) == [1]
@test f27597([]) == 1:0

# issue #22291
wrap22291(ind) = (ind...,)
@test @inferred(wrap22291(1)) == (1,)
@test @inferred(wrap22291((1, 2))) == (1, 2)

# Issue 27770
mutable struct Handle27770
    ptr::Ptr{Cvoid}
end
Handle27770() = Handle27770(Ptr{Cvoid}(UInt(0xfeedface)))

struct Nullable27770
    hasvalue::Bool
    value::Handle27770
    Nullable27770() = new(false)
    Nullable27770(v::Handle27770) = new(true, Handle27770)
end
get27770(n::Nullable27770, v::Handle27770) = n.hasvalue ? n.value : v

foo27770() = get27770(Nullable27770(), Handle27770())
@test foo27770().ptr == Ptr{Cvoid}(UInt(0xfeedface))

bar27770() = Nullable27770().value
@test_throws UndefRefError bar27770()

# Issue 27910
f27910() = ((),)[2]
@test_throws BoundsError f27910()

# Issue 9765
f9765(::Bool) = 1
g9765() = f9765(isa(1, 1))
@test_throws TypeError g9765()

# Issue 28102
struct HasPlain28102
    plain::Int
    HasPlain28102() = new()
end
@noinline function bam28102()
    x = HasPlain28102()
    if isdefined(x,:plain)
        x.plain
    end
end
@test isa(bam28102(), Int)

# Check that the tfunc for fieldtype is correct
struct FooFieldType; x::Int; end
f_fieldtype(b) = fieldtype(b ? Int : FooFieldType, 1)

@test @inferred(f_fieldtype(false)) == Int
@test_throws BoundsError f_fieldtype(true)

# Issue #28224
@noinline make_error28224(n) = n == 5 ? error() : true
function foo28224()
    z = 0
    try
        while make_error28224(z)
            z+=1
        end
    catch end
    return z
end
@test foo28224() == 5

# Issue #28208
@noinline function foo28208(a::Bool, b::Bool)
    x = (1, 2)
    if a
        if b
            y = nothing
        else
            y = missing
        end
        x = y
    end
    x
end
@test isa(foo28208(false, true), Tuple)
@test foo28208(true, false) === missing
@test foo28208(true, true) === nothing

# Issue #28326
function foo28326(a)
    try
        @inbounds a[1]
        return false
    catch
        return true
    end
end
@test foo28326(Vector(undef, 1))

# Issue #28392
struct Foo28392; end
@test_throws MethodError iterate(Foo28392())

# issue #28399
function g28399(n)
    for a = 1:n
        c28399 = 1
    end
    ()->c28399
end
function f28399()
    for a = __undef_28399__
        c28399 = 1
    end
    ()->c28399
end
c28399 = 42
@test g28399(0)() == 42
@test g28399(1)() == 42
@test_throws UndefVarError(:__undef_28399__) f28399()

# issue #28445
mutable struct foo28445
    x::Int
end

@noinline make_foo28445() = (foo28445(1), foo28445(rand(1:10)), foo28445(rand(1:10)))
@noinline function use_tuple28445(c)
    @test isa(c[2], foo28445)
    @test isa(c[3], foo28445)
end

function repackage28445()
    (_, a, b) = make_foo28445()
    GC.gc()
    c = (foo28445(1), foo28445(2), a, b)
    use_tuple28445(c)
    true
end
@test repackage28445()

# issue #28597
@test_throws ErrorException Array{Int, 2}(undef, 0, -10)
@test_throws ErrorException Array{Int, 2}(undef, -10, 0)
@test_throws ErrorException Array{Int, 2}(undef, -1, -1)

# issue #29145
struct T29145{A,B}
    function T29145()
        new{S,Ref{S}}() where S
    end
end
@test_throws TypeError T29145()

# interpreted but inferred/optimized top-level expressions with vars
let code = """
           while true
               try
                   this_is_undefined_29213
                   ed = 0
                   break
               finally
                   break
               end
           end
           print(42)
           """
    @test read(`$(Base.julia_cmd()) --startup-file=no --compile=min -e $code`, String) == "42"
end

# issue #29175
function f29175(tuple::T) where {T<:Tuple}
    prefix::Tuple{T.parameters[1:end-1]...} = tuple[1:length(T.parameters)-1]
    x = prefix
    prefix = x  # force another conversion to declared type
    return prefix
end
@test f29175((1,2,3)) === (1,2)

# issue #29306
let a = [1,2,3,4,missing,6,7]
    @test_throws TypeError [ (x>6 ? missing : x)  for x in a]
    foo(x) = x > 0 ? x : missing
    @test_throws TypeError foo(missing)
end

# issue #29152
function f29152()
    try
        g29152()
    finally
    end
end
g29152() = (_true29152 ? error() : _true29152 ? 0 : false)
_true29152 = true;
@test_throws ErrorException f29152()