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
|
//===--- TypeCheckStmt.cpp - Type Checking for Statements -----------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2018 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
//
// This file implements semantic analysis for statements.
//
//===----------------------------------------------------------------------===//
#include "MiscDiagnostics.h"
#include "TypeCheckAvailability.h"
#include "TypeCheckConcurrency.h"
#include "TypeCheckDistributed.h"
#include "TypeCheckType.h"
#include "TypeChecker.h"
#include "swift/AST/ASTPrinter.h"
#include "swift/AST/ASTScope.h"
#include "swift/AST/ASTVisitor.h"
#include "swift/AST/ASTWalker.h"
#include "swift/AST/DiagnosticSuppression.h"
#include "swift/AST/DiagnosticsSema.h"
#include "swift/AST/Identifier.h"
#include "swift/AST/Initializer.h"
#include "swift/AST/NameLookup.h"
#include "swift/AST/ParameterList.h"
#include "swift/AST/PrettyStackTrace.h"
#include "swift/AST/SourceFile.h"
#include "swift/AST/TypeCheckRequests.h"
#include "swift/Basic/Range.h"
#include "swift/Basic/STLExtras.h"
#include "swift/Basic/SourceManager.h"
#include "swift/Basic/Statistic.h"
#include "swift/Basic/TopCollection.h"
#include "swift/Parse/Lexer.h"
#include "swift/Parse/Parser.h"
#include "swift/Sema/ConstraintSystem.h"
#include "swift/Sema/IDETypeChecking.h"
#include "swift/Subsystems.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/PointerUnion.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/ADT/TinyPtrVector.h"
#include "llvm/ADT/Twine.h"
#include "llvm/Support/Compiler.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/Format.h"
#include "llvm/Support/Timer.h"
#include <cmath>
#include <iterator>
using namespace swift;
#define DEBUG_TYPE "TypeCheckStmt"
namespace {
class ContextualizeClosuresAndMacros : public ASTWalker {
DeclContext *ParentDC;
public:
ContextualizeClosuresAndMacros(DeclContext *parent) : ParentDC(parent) {}
MacroWalking getMacroWalkingBehavior() const override {
return MacroWalking::ArgumentsAndExpansion;
}
PreWalkResult<Expr *> walkToExprPre(Expr *E) override {
// Autoclosures need to be numbered and potentially reparented.
// Reparenting is required with:
// - nested autoclosures, because the inner autoclosure will be
// parented to the outer context, not the outer autoclosure
// - non-local initializers
if (auto CE = dyn_cast<AutoClosureExpr>(E)) {
CE->setParent(ParentDC);
// Recurse into the autoclosure body using the same sequence,
// but parenting to the autoclosure instead of the outer closure.
auto oldParentDC = ParentDC;
ParentDC = CE;
CE->getBody()->walk(*this);
ParentDC = oldParentDC;
TypeChecker::computeCaptures(CE);
return Action::SkipNode(E);
}
if (auto CapE = dyn_cast<CaptureListExpr>(E)) {
// Capture lists need to be reparented to enclosing autoclosures
// and/or initializers of property wrapper backing properties
// (because they subsume initializers associated with wrapped
// properties).
if (isa<AutoClosureExpr>(ParentDC) ||
isPropertyWrapperBackingPropertyInitContext(ParentDC)) {
for (auto &Cap : CapE->getCaptureList()) {
Cap.PBD->setDeclContext(ParentDC);
Cap.getVar()->setDeclContext(ParentDC);
}
}
}
// Explicit closures start their own sequence.
if (auto CE = dyn_cast<ClosureExpr>(E)) {
CE->setParent(ParentDC);
// If the closure was type checked within its enclosing context,
// we need to walk into it with a new sequence.
// Otherwise, it'll have been separately type-checked.
if (!CE->isSeparatelyTypeChecked())
CE->getBody()->walk(ContextualizeClosuresAndMacros(CE));
TypeChecker::computeCaptures(CE);
return Action::SkipNode(E);
}
// Caller-side default arguments need their @autoclosures checked.
if (auto *DAE = dyn_cast<DefaultArgumentExpr>(E))
if (DAE->isCallerSide() &&
(DAE->getParamDecl()->isAutoClosure() ||
(DAE->getParamDecl()->getDefaultArgumentKind() ==
DefaultArgumentKind::ExpressionMacro)))
DAE->getCallerSideDefaultExpr()->walk(*this);
// Macro expansion expressions require a DeclContext as well.
if (auto macroExpansion = dyn_cast<MacroExpansionExpr>(E)) {
macroExpansion->setDeclContext(ParentDC);
}
return Action::Continue(E);
}
/// We don't want to recurse into most local declarations.
PreWalkAction walkToDeclPre(Decl *D) override {
// But we do want to walk into the initializers of local
// variables.
return Action::VisitNodeIf(isa<PatternBindingDecl>(D));
}
private:
static bool isPropertyWrapperBackingPropertyInitContext(DeclContext *DC) {
auto *init = dyn_cast<PatternBindingInitializer>(DC);
if (!init)
return false;
if (auto *PB = init->getBinding()) {
auto *var = PB->getSingleVar();
return var && var->getOriginalWrappedProperty(
PropertyWrapperSynthesizedPropertyKind::Backing);
}
return false;
}
};
/// Used for debugging which parts of the code are taking a long time to
/// compile.
class FunctionBodyTimer {
AnyFunctionRef Function;
llvm::TimeRecord StartTime = llvm::TimeRecord::getCurrentTime();
public:
FunctionBodyTimer(AnyFunctionRef Fn) : Function(Fn) {}
~FunctionBodyTimer() {
llvm::TimeRecord endTime = llvm::TimeRecord::getCurrentTime(false);
auto elapsed = endTime.getProcessTime() - StartTime.getProcessTime();
unsigned elapsedMS = static_cast<unsigned>(elapsed * 1000);
ASTContext &ctx = Function.getAsDeclContext()->getASTContext();
auto *AFD = Function.getAbstractFunctionDecl();
if (ctx.TypeCheckerOpts.DebugTimeFunctionBodies) {
// Round up to the nearest 100th of a millisecond.
llvm::errs() << llvm::format("%0.2f", std::ceil(elapsed * 100000) / 100) << "ms\t";
Function.getLoc().print(llvm::errs(), ctx.SourceMgr);
if (AFD) {
llvm::errs()
<< "\t" << Decl::getDescriptiveKindName(AFD->getDescriptiveKind())
<< " ";
AFD->dumpRef(llvm::errs());
} else {
llvm::errs() << "\t(closure)";
}
llvm::errs() << "\n";
}
const auto WarnLimit = ctx.TypeCheckerOpts.WarnLongFunctionBodies;
if (WarnLimit != 0 && elapsedMS >= WarnLimit) {
if (AFD) {
ctx.Diags.diagnose(AFD, diag::debug_long_function_body,
AFD, elapsedMS, WarnLimit);
} else {
ctx.Diags.diagnose(Function.getLoc(), diag::debug_long_closure_body,
elapsedMS, WarnLimit);
}
}
}
};
} // end anonymous namespace
void TypeChecker::contextualizeInitializer(Initializer *DC, Expr *E) {
ContextualizeClosuresAndMacros CC(DC);
E->walk(CC);
}
void TypeChecker::contextualizeCallSideDefaultArgument(DeclContext *DC,
Expr *E) {
ContextualizeClosuresAndMacros CC(DC);
E->walk(CC);
}
void TypeChecker::contextualizeTopLevelCode(TopLevelCodeDecl *TLCD) {
ContextualizeClosuresAndMacros CC(TLCD);
if (auto *body = TLCD->getBody())
body->walk(CC);
}
namespace {
/// Visitor that assigns local discriminators through whatever it walks.
class SetLocalDiscriminators : public ASTWalker {
/// The initial discriminator that everything starts with.
unsigned InitialDiscriminator;
// Next (explicit) closure discriminator.
unsigned NextClosureDiscriminator;
// Next autoclosure discriminator.
unsigned NextAutoclosureDiscriminator;
/// Local declaration discriminators.
llvm::SmallDenseMap<Identifier, unsigned> DeclDiscriminators;
public:
SetLocalDiscriminators(
unsigned initialDiscriminator = 0
) : InitialDiscriminator(initialDiscriminator),
NextClosureDiscriminator(initialDiscriminator),
NextAutoclosureDiscriminator(initialDiscriminator) { }
/// Determine the maximum discriminator assigned to any local.
unsigned maxAssignedDiscriminator() const {
unsigned result = std::max(
NextClosureDiscriminator, NextAutoclosureDiscriminator);
for (const auto &decl : DeclDiscriminators) {
result = std::max(result, decl.second);
}
return result;
}
MacroWalking getMacroWalkingBehavior() const override {
return MacroWalking::Arguments;
}
PreWalkResult<Expr *> walkToExprPre(Expr *E) override {
// Autoclosures need to be numbered and potentially reparented.
// Reparenting is required with:
// - nested autoclosures, because the inner autoclosure will be
// parented to the outer context, not the outer autoclosure
// - non-local initializers
if (auto CE = dyn_cast<AutoClosureExpr>(E)) {
if (CE->getRawDiscriminator() != AutoClosureExpr::InvalidDiscriminator)
return Action::SkipNode(E);
assert(
CE->getRawDiscriminator() == AutoClosureExpr::InvalidDiscriminator);
CE->setDiscriminator(NextAutoclosureDiscriminator++);
// Recurse into the autoclosure body using the same sequence,
// but parenting to the autoclosure instead of the outer closure.
CE->getBody()->walk(*this);
return Action::SkipNode(E);
}
// Explicit closures start their own sequence.
if (auto CE = dyn_cast<ClosureExpr>(E)) {
if(CE->getRawDiscriminator() == ClosureExpr::InvalidDiscriminator)
CE->setDiscriminator(NextClosureDiscriminator++);
// If the closure was type checked within its enclosing context,
// we need to walk into it with a new sequence.
// Otherwise, it'll have been separately type-checked.
if (!CE->isSeparatelyTypeChecked()) {
SetLocalDiscriminators innerVisitor;
if (auto params = CE->getParameters()) {
for (auto *param : *params) {
innerVisitor.setLocalDiscriminator(param);
}
}
CE->getBody()->walk(innerVisitor);
}
return Action::SkipNode(E);
}
// Caller-side default arguments need their @autoclosures checked.
if (auto *DAE = dyn_cast<DefaultArgumentExpr>(E))
if (DAE->isCallerSide() && DAE->getParamDecl()->isAutoClosure())
DAE->getCallerSideDefaultExpr()->walk(*this);
// Tap expression bodies have an $interpolation variable that doesn't
// normally get visited. Visit it specifically.
if (auto tap = dyn_cast<TapExpr>(E)) {
if (auto body = tap->getBody()) {
if (!body->empty()) {
if (auto decl = body->getFirstElement().dyn_cast<Decl *>()) {
if (auto var = dyn_cast<VarDecl>(decl))
if (var->getName() ==
var->getASTContext().Id_dollarInterpolation)
setLocalDiscriminator(var);
}
}
}
}
return Action::Continue(E);
}
/// We don't want to recurse into most local declarations.
PreWalkAction walkToDeclPre(Decl *D) override {
// If we have a local declaration, assign a local discriminator to it.
if (auto valueDecl = dyn_cast<ValueDecl>(D)) {
setLocalDiscriminator(valueDecl);
}
// But we do want to walk into the initializers of local
// variables.
return Action::VisitNodeIf(isa<PatternBindingDecl>(D));
}
PreWalkResult<Stmt *> walkToStmtPre(Stmt *S) override {
if (auto caseStmt = dyn_cast<CaseStmt>(S)) {
for (auto var : caseStmt->getCaseBodyVariablesOrEmptyArray())
setLocalDiscriminator(var);
}
return Action::Continue(S);
}
/// Set the local discriminator for a named declaration.
void setLocalDiscriminator(ValueDecl *valueDecl) {
if (valueDecl->hasLocalDiscriminator()) {
if (valueDecl->getRawLocalDiscriminator() ==
ValueDecl::InvalidDiscriminator) {
// Assign the next discriminator.
Identifier name = valueDecl->getBaseIdentifier();
auto &discriminator = DeclDiscriminators[name];
if (discriminator < InitialDiscriminator)
discriminator = InitialDiscriminator;
valueDecl->setLocalDiscriminator(discriminator++);
} else {
// Assign the next discriminator.
Identifier name = valueDecl->getBaseIdentifier();
auto &discriminator = DeclDiscriminators[name];
discriminator = std::max(discriminator, std::max(InitialDiscriminator, valueDecl->getLocalDiscriminator() + 1));
}
}
// If this is a property wrapper, check for projected storage.
if (auto var = dyn_cast<VarDecl>(valueDecl)) {
if (auto auxVars = var->getPropertyWrapperAuxiliaryVariables()) {
if (auxVars.backingVar && auxVars.backingVar != var)
setLocalDiscriminator(auxVars.backingVar);
// If there is a projection variable, give it a local discriminator.
if (auxVars.projectionVar && auxVars.projectionVar != var) {
if (var->hasLocalDiscriminator() &&
var->getName() == auxVars.projectionVar->getName()) {
auxVars.projectionVar->setLocalDiscriminator(
var->getRawLocalDiscriminator());
} else {
setLocalDiscriminator(auxVars.projectionVar);
}
}
// For the wrapped local variable, adopt the same discriminator as
// the parameter. For all intents and purposes, these are the same.
if (auxVars.localWrappedValueVar &&
auxVars.localWrappedValueVar != var) {
if (var->hasLocalDiscriminator() &&
var->getName() == auxVars.localWrappedValueVar->getName()) {
auxVars.localWrappedValueVar->setLocalDiscriminator(
var->getRawLocalDiscriminator());
} else {
setLocalDiscriminator(
auxVars.localWrappedValueVar);
}
}
}
}
}
};
}
unsigned LocalDiscriminatorsRequest::evaluate(
Evaluator &evaluator, DeclContext *dc
) const {
ASTContext &ctx = dc->getASTContext();
// Autoclosures aren't their own contexts; look to the parent instead.
if (auto autoclosure = dyn_cast<AutoClosureExpr>(dc)) {
return evaluateOrDefault(evaluator,
LocalDiscriminatorsRequest{dc->getParent()}, 0);
}
ASTNode node;
ParameterList *params = nullptr;
ParamDecl *selfParam = nullptr;
if (auto func = dyn_cast<AbstractFunctionDecl>(dc)) {
if (!func->isBodySkipped())
node = func->getBody();
selfParam = func->getImplicitSelfDecl();
params = func->getParameters();
// Accessors for lazy properties should be walked as part of the property's
// pattern.
if (auto accessor = dyn_cast<AccessorDecl>(func)) {
if (accessor->isImplicit() &&
accessor->getStorage()->getAttrs().hasAttribute<LazyAttr>()) {
if (auto var = dyn_cast<VarDecl>(accessor->getStorage())) {
if (auto binding = var->getParentPatternBinding()) {
node = binding;
}
}
}
}
} else if (auto closure = dyn_cast<ClosureExpr>(dc)) {
node = closure->getBody();
params = closure->getParameters();
} else if (auto topLevel = dyn_cast<TopLevelCodeDecl>(dc)) {
node = topLevel->getBody();
dc = topLevel->getParentModule();
} else if (auto patternBindingInit = dyn_cast<PatternBindingInitializer>(dc)){
auto patternBinding = patternBindingInit->getBinding();
node = patternBinding->getInit(patternBindingInit->getBindingIndex());
} else if (auto defaultArgInit = dyn_cast<DefaultArgumentInitializer>(dc)) {
auto param = getParameterAt(
cast<ValueDecl>(dc->getParent()->getAsDecl()),
defaultArgInit->getIndex());
if (!param)
return 0;
node = param->getTypeCheckedDefaultExpr();
} else if (auto propertyWrapperInit =
dyn_cast<PropertyWrapperInitializer>(dc)) {
auto var = propertyWrapperInit->getWrappedVar();
auto initInfo = var->getPropertyWrapperInitializerInfo();
switch (propertyWrapperInit->getKind()) {
case PropertyWrapperInitializer::Kind::WrappedValue:
node = initInfo.getInitFromWrappedValue();
break;
case PropertyWrapperInitializer::Kind::ProjectedValue:
node = initInfo.getInitFromProjectedValue();
break;
}
} else {
params = getParameterList(dc);
}
auto startDiscriminator = ctx.getNextDiscriminator(dc);
if (!node && !params && !selfParam)
return startDiscriminator;
SetLocalDiscriminators visitor(startDiscriminator);
// Set local discriminator for the 'self' parameter.
if (selfParam)
visitor.setLocalDiscriminator(selfParam);
// Set local discriminators for the parameters, which might have property
// wrappers that need it.
if (params) {
for (auto *param : *params)
visitor.setLocalDiscriminator(param);
}
if (node)
node.walk(visitor);
unsigned nextDiscriminator = visitor.maxAssignedDiscriminator();
ctx.setMaxAssignedDiscriminator(dc, nextDiscriminator);
// Return the next discriminator.
return nextDiscriminator;
}
/// Emits an error with a fixit for the case of unnecessary cast over a
/// OptionSet value. The primary motivation is to help with SDK changes.
/// Example:
/// \code
/// func supported() -> MyMask {
/// return Int(MyMask.Bingo.rawValue)
/// }
/// \endcode
static void tryDiagnoseUnnecessaryCastOverOptionSet(ASTContext &Ctx,
Expr *E,
Type ResultType,
ModuleDecl *module) {
auto *NTD = ResultType->getAnyNominal();
if (!NTD)
return;
auto optionSetType = dyn_cast_or_null<ProtocolDecl>(Ctx.getOptionSetDecl());
if (!optionSetType)
return;
if (!module->lookupConformance(ResultType, optionSetType))
return;
auto *CE = dyn_cast<CallExpr>(E);
if (!CE)
return;
if (!isa<ConstructorRefCallExpr>(CE->getFn()))
return;
auto *unaryArg = CE->getArgs()->getUnlabeledUnaryExpr();
if (!unaryArg)
return;
auto *ME = dyn_cast<MemberRefExpr>(unaryArg);
if (!ME)
return;
ValueDecl *VD = ME->getMember().getDecl();
if (!VD || VD->getBaseName() != Ctx.Id_rawValue)
return;
auto *BME = dyn_cast<MemberRefExpr>(ME->getBase());
if (!BME)
return;
if (!BME->getType()->isEqual(ResultType))
return;
Ctx.Diags.diagnose(E->getLoc(), diag::unnecessary_cast_over_optionset,
ResultType)
.highlight(E->getSourceRange())
.fixItRemoveChars(E->getLoc(), ME->getStartLoc())
.fixItRemove(SourceRange(ME->getDotLoc(), E->getEndLoc()));
}
/// Whether the given enclosing context is a "defer" body.
static bool isDefer(DeclContext *dc) {
if (auto *func = dyn_cast<FuncDecl>(dc))
return func->isDeferBody();
return false;
}
/// Climb the context to find the method or accessor we're within. We do not
/// look past local functions or closures, since those cannot contain a
/// discard statement.
/// \param dc the inner decl context containing the discard statement
/// \return either the type member we reside in, or the offending context that
/// stopped the search for the type member (e.g. closure).
static DeclContext *climbContextForDiscardStmt(DeclContext *dc) {
do {
if (auto decl = dc->getAsDecl()) {
auto func = dyn_cast<AbstractFunctionDecl>(decl);
// If we found a non-func decl, we're done.
if (func == nullptr)
break;
// If this function's parent is the type context, our search is done.
if (func->getDeclContext()->isTypeContext())
break;
// Only continue if we're in a defer. We want to stop at the first local
// function or closure.
if (!isDefer(dc))
break;
}
} while ((dc = dc->getParent()));
return dc;
}
/// Check that a labeled statement doesn't shadow another statement with the
/// same label.
static void checkLabeledStmtShadowing(
ASTContext &ctx, SourceFile *sourceFile, LabeledStmt *ls) {
auto name = ls->getLabelInfo().Name;
if (name.empty() || !sourceFile || ls->getStartLoc().isInvalid())
return;
auto activeLabeledStmtsVec = ASTScope::lookupLabeledStmts(
sourceFile, ls->getStartLoc());
auto activeLabeledStmts = llvm::ArrayRef(activeLabeledStmtsVec);
for (auto prevLS : activeLabeledStmts.slice(1)) {
if (prevLS->getLabelInfo().Name == name) {
ctx.Diags.diagnose(
ls->getLabelInfo().Loc, diag::label_shadowed, name);
ctx.Diags.diagnose(
prevLS->getLabelInfo().Loc, diag::invalid_redecl_prev_name, name);
}
}
}
static void
emitUnresolvedLabelDiagnostics(DiagnosticEngine &DE,
SourceLoc targetLoc, Identifier targetName,
TopCollection<unsigned, LabeledStmt *> corrections) {
// If an unresolved label was used, but we have a single correction,
// produce the specific diagnostic and fixit.
if (corrections.size() == 1) {
DE.diagnose(targetLoc, diag::unresolved_label_corrected,
targetName, corrections.begin()->Value->getLabelInfo().Name)
.highlight(SourceRange(targetLoc))
.fixItReplace(SourceRange(targetLoc),
corrections.begin()->Value->getLabelInfo().Name.str());
DE.diagnose(corrections.begin()->Value->getLabelInfo().Loc,
diag::name_declared_here,
corrections.begin()->Value->getLabelInfo().Name);
} else {
// If we have multiple corrections or none, produce a generic diagnostic
// and all corrections available.
DE.diagnose(targetLoc, diag::unresolved_label, targetName)
.highlight(SourceRange(targetLoc));
for (auto &entry : corrections)
DE.diagnose(entry.Value->getLabelInfo().Loc, diag::note_typo_candidate,
entry.Value->getLabelInfo().Name.str())
.fixItReplace(SourceRange(targetLoc),
entry.Value->getLabelInfo().Name.str());
}
}
/// Find the target of a break or continue statement without a label.
///
/// \returns the target, if one was found, or \c nullptr if no such target
/// exists.
static LabeledStmt *findUnlabeledBreakOrContinueStmtTarget(
ASTContext &ctx, SourceFile *sourceFile, SourceLoc loc,
bool isContinue, DeclContext *dc,
ArrayRef<LabeledStmt *> activeLabeledStmts) {
for (auto labeledStmt : activeLabeledStmts) {
// 'break' with no label looks through non-loop structures
// except 'switch'.
// 'continue' ignores non-loop structures.
if (!labeledStmt->requiresLabelOnJump() &&
(!isContinue || labeledStmt->isPossibleContinueTarget())) {
return labeledStmt;
}
}
// If we're in a defer, produce a tailored diagnostic.
if (isDefer(dc)) {
ctx.Diags.diagnose(
loc, diag::jump_out_of_defer, isContinue ? "continue": "break");
return nullptr;
}
// If we're dealing with an unlabeled break inside of an 'if' or 'do'
// statement, produce a more specific error.
if (!isContinue &&
llvm::any_of(activeLabeledStmts,
[&](Stmt *S) -> bool {
return isa<IfStmt>(S) || isa<DoStmt>(S);
})) {
ctx.Diags.diagnose(
loc, diag::unlabeled_break_outside_loop);
return nullptr;
}
// Otherwise produce a generic error.
ctx.Diags.diagnose(
loc,
isContinue ? diag::continue_outside_loop : diag::break_outside_loop);
return nullptr;
}
/// Find the target of a break or continue statement.
///
/// \returns the target, if one was found, or \c nullptr if no such target
/// exists.
LabeledStmt *swift::findBreakOrContinueStmtTarget(
ASTContext &ctx, SourceFile *sourceFile,
SourceLoc loc, Identifier targetName, SourceLoc targetLoc,
bool isContinue, DeclContext *dc) {
// Retrieve the active set of labeled statements.
SmallVector<LabeledStmt *, 4> activeLabeledStmts;
activeLabeledStmts = ASTScope::lookupLabeledStmts(sourceFile, loc);
// Handle an unlabeled break separately; that's the easy case.
if (targetName.empty()) {
return findUnlabeledBreakOrContinueStmtTarget(
ctx, sourceFile, loc, isContinue, dc, activeLabeledStmts);
}
// Scan inside out until we find something with the right label.
TopCollection<unsigned, LabeledStmt *> labelCorrections(3);
for (auto labeledStmt : activeLabeledStmts) {
if (targetName == labeledStmt->getLabelInfo().Name) {
// Continue cannot be used to repeat switches, use fallthrough instead.
if (isContinue && !labeledStmt->isPossibleContinueTarget()) {
ctx.Diags.diagnose(
loc, diag::continue_not_in_this_stmt,
isa<SwitchStmt>(labeledStmt) ? "switch" : "if");
return nullptr;
}
return labeledStmt;
}
unsigned distance =
TypeChecker::getCallEditDistance(
DeclNameRef(targetName),
labeledStmt->getLabelInfo().Name,
TypeChecker::UnreasonableCallEditDistance);
if (distance < TypeChecker::UnreasonableCallEditDistance)
labelCorrections.insert(distance, std::move(labeledStmt));
}
labelCorrections.filterMaxScoreRange(
TypeChecker::MaxCallEditDistanceFromBestCandidate);
// If we're in a defer, produce a tailored diagnostic.
if (isDefer(dc)) {
ctx.Diags.diagnose(
loc, diag::jump_out_of_defer, isContinue ? "continue": "break");
return nullptr;
}
// Provide potential corrections for an incorrect label.
emitUnresolvedLabelDiagnostics(
ctx.Diags, targetLoc, targetName, labelCorrections);
return nullptr;
}
LabeledStmt *
BreakTargetRequest::evaluate(Evaluator &evaluator, const BreakStmt *BS) const {
auto *DC = BS->getDeclContext();
return findBreakOrContinueStmtTarget(
DC->getASTContext(), DC->getParentSourceFile(), BS->getLoc(),
BS->getTargetName(), BS->getTargetLoc(), /*isContinue*/ false, DC);
}
LabeledStmt *
ContinueTargetRequest::evaluate(Evaluator &evaluator,
const ContinueStmt *CS) const {
auto *DC = CS->getDeclContext();
return findBreakOrContinueStmtTarget(
DC->getASTContext(), DC->getParentSourceFile(), CS->getLoc(),
CS->getTargetName(), CS->getTargetLoc(), /*isContinue*/ true, DC);
}
static Expr *getDeclRefProvidingExpressionForHasSymbol(Expr *E) {
// Strip coercions, which are necessary in source to disambiguate overloaded
// functions or generic functions, e.g.
//
// if #_hasSymbol(foo as () -> ()) { ... }
//
if (auto CE = dyn_cast<CoerceExpr>(E))
return getDeclRefProvidingExpressionForHasSymbol(CE->getSubExpr());
// Unwrap curry thunks which are injected into the AST to wrap some forms of
// unapplied method references, e.g.
//
// if #_hasSymbol(SomeStruct.foo(_:)) { ... }
//
if (auto ACE = dyn_cast<AutoClosureExpr>(E))
return getDeclRefProvidingExpressionForHasSymbol(
ACE->getUnwrappedCurryThunkExpr());
// Drill into the function expression for a DotSyntaxCallExpr. These sometimes
// wrap or are wrapped by an AutoClosureExpr.
//
// if #_hasSymbol(someStruct.foo(_:)) { ... }
//
if (auto DSCE = dyn_cast<DotSyntaxCallExpr>(E))
return getDeclRefProvidingExpressionForHasSymbol(DSCE->getFn());
return E;
}
ConcreteDeclRef TypeChecker::getReferencedDeclForHasSymbolCondition(Expr *E) {
// Match DotSelfExprs (e.g. `SomeStruct.self`) when the type is static.
if (auto DSE = dyn_cast<DotSelfExpr>(E)) {
if (DSE->isStaticallyDerivedMetatype())
return DSE->getType()->getMetatypeInstanceType()->getAnyNominal();
}
if (auto declRefExpr = getDeclRefProvidingExpressionForHasSymbol(E)) {
if (auto CDR = declRefExpr->getReferencedDecl())
return CDR;
}
return ConcreteDeclRef();
}
bool TypeChecker::typeCheckStmtConditionElement(StmtConditionElement &elt,
bool &isFalsable,
DeclContext *dc) {
auto &Context = dc->getASTContext();
// Typecheck a #available or #unavailable condition.
if (elt.getKind() == StmtConditionElement::CK_Availability) {
isFalsable = true;
return false;
}
// Typecheck a #_hasSymbol condition.
if (elt.getKind() == StmtConditionElement::CK_HasSymbol) {
isFalsable = true;
auto Info = elt.getHasSymbolInfo();
auto E = Info->getSymbolExpr();
if (!E)
return false;
auto exprTy = TypeChecker::typeCheckExpression(E, dc);
Info->setSymbolExpr(E);
if (!exprTy) {
Info->setInvalid();
return true;
}
Info->setReferencedDecl(getReferencedDeclForHasSymbolCondition(E));
return false;
}
if (auto E = elt.getBooleanOrNull()) {
assert(!E->getType() && "the bool condition is already type checked");
bool hadError = TypeChecker::typeCheckCondition(E, dc);
elt.setBoolean(E);
isFalsable = true;
return hadError;
}
assert(elt.getKind() != StmtConditionElement::CK_Boolean);
// This is cleanup goop run on the various paths where type checking of the
// pattern binding fails.
auto typeCheckPatternFailed = [&] {
elt.getPattern()->setType(ErrorType::get(Context));
elt.getInitializer()->setType(ErrorType::get(Context));
elt.getPattern()->forEachVariable([&](VarDecl *var) {
// Don't change the type of a variable that we've been able to
// compute a type for.
if (var->hasInterfaceType() && !var->isInvalid())
return;
var->setInvalid();
});
};
// Resolve the pattern.
assert(!elt.getPattern()->hasType() &&
"the pattern binding condition is already type checked");
auto *pattern = TypeChecker::resolvePattern(elt.getPattern(), dc,
/*isStmtCondition*/ true);
if (!pattern) {
typeCheckPatternFailed();
return true;
}
elt.setPattern(pattern);
// Check the pattern, it allows unspecified types because the pattern can
// provide type information.
auto contextualPattern = ContextualPattern::forRawPattern(pattern, dc);
Type patternType = TypeChecker::typeCheckPattern(contextualPattern);
if (patternType->hasError()) {
typeCheckPatternFailed();
return true;
}
// If the pattern didn't get a type, it's because we ran into some
// unknown types along the way. We'll need to check the initializer.
auto init = elt.getInitializer();
bool hadError = TypeChecker::typeCheckBinding(pattern, init, dc, patternType);
elt.setPattern(pattern);
elt.setInitializer(init);
isFalsable |= pattern->isRefutablePattern();
return hadError;
}
/// Type check the given 'if', 'while', or 'guard' statement condition.
///
/// \param stmt The conditional statement to type-check, which will be modified
/// in place.
///
/// \returns true if an error occurred, false otherwise.
static bool typeCheckConditionForStatement(LabeledConditionalStmt *stmt,
DeclContext *dc) {
bool hadError = false;
bool hadAnyFalsable = false;
auto cond = stmt->getCond();
for (auto &elt : cond) {
hadError |=
TypeChecker::typeCheckStmtConditionElement(elt, hadAnyFalsable, dc);
}
// If the binding is not refutable, and there *is* an else, reject it as
// unreachable.
if (!hadAnyFalsable && !hadError) {
auto &diags = dc->getASTContext().Diags;
Diag<> msg = diag::invalid_diagnostic;
switch (stmt->getKind()) {
case StmtKind::If:
msg = diag::if_always_true;
break;
case StmtKind::While:
msg = diag::while_always_true;
break;
case StmtKind::Guard:
msg = diag::guard_always_succeeds;
break;
default:
llvm_unreachable("unknown LabeledConditionalStmt kind");
}
diags.diagnose(cond[0].getStartLoc(), msg);
}
stmt->setCond(cond);
return false;
}
/// Verify that the pattern bindings for the cases that we're falling through
/// from and to are equivalent.
static void checkFallthroughPatternBindingsAndTypes(
ASTContext &ctx,
CaseStmt *caseBlock, CaseStmt *previousBlock,
FallthroughStmt *fallthrough) {
auto firstPattern = caseBlock->getCaseLabelItems()[0].getPattern();
SmallVector<VarDecl *, 4> vars;
firstPattern->collectVariables(vars);
// We know that the typechecker has already guaranteed that all of
// the case label items in the fallthrough have the same var
// decls. So if we match against the case body var decls,
// transitively we will match all of the other case label items in
// the fallthrough destination as well.
auto previousVars = previousBlock->getCaseBodyVariablesOrEmptyArray();
for (auto *expected : vars) {
bool matched = false;
if (!expected->hasName())
continue;
for (auto *previous : previousVars) {
if (!previous->hasName() ||
expected->getName() != previous->getName()) {
continue;
}
if (!previous->getTypeInContext()->isEqual(expected->getTypeInContext())) {
ctx.Diags.diagnose(
previous->getLoc(), diag::type_mismatch_fallthrough_pattern_list,
previous->getTypeInContext(), expected->getTypeInContext());
previous->setInvalid();
expected->setInvalid();
}
// Ok, we found our match. Make the previous fallthrough statement var
// decl our parent var decl.
expected->setParentVarDecl(previous);
matched = true;
break;
}
if (!matched) {
ctx.Diags.diagnose(
fallthrough->getLoc(), diag::fallthrough_into_case_with_var_binding,
expected->getName());
}
}
}
/// Check the correctness of a 'fallthrough' statement.
///
/// \returns true if an error occurred.
bool swift::checkFallthroughStmt(DeclContext *dc, FallthroughStmt *stmt) {
CaseStmt *fallthroughSource;
CaseStmt *fallthroughDest;
ASTContext &ctx = dc->getASTContext();
auto sourceFile = dc->getParentSourceFile();
std::tie(fallthroughSource, fallthroughDest) =
ASTScope::lookupFallthroughSourceAndDest(sourceFile, stmt->getLoc());
if (!fallthroughSource) {
ctx.Diags.diagnose(stmt->getLoc(), diag::fallthrough_outside_switch);
return true;
}
if (!fallthroughDest) {
ctx.Diags.diagnose(stmt->getLoc(), diag::fallthrough_from_last_case);
return true;
}
stmt->setFallthroughSource(fallthroughSource);
stmt->setFallthroughDest(fallthroughDest);
checkFallthroughPatternBindingsAndTypes(
ctx, fallthroughDest, fallthroughSource, stmt);
return false;
}
namespace {
class StmtChecker : public StmtVisitor<StmtChecker, Stmt*> {
public:
ASTContext &Ctx;
/// DC - This is the current DeclContext.
DeclContext *DC;
/// Skip type checking any elements inside 'BraceStmt', also this is
/// propagated to ConstraintSystem.
bool LeaveBraceStmtBodyUnchecked = false;
ASTContext &getASTContext() const { return Ctx; };
StmtChecker(DeclContext *DC) : Ctx(DC->getASTContext()), DC(DC) { }
llvm::SmallVector<GenericEnvironment *, 4> genericSigStack;
//===--------------------------------------------------------------------===//
// Helper Functions.
//===--------------------------------------------------------------------===//
bool isInDefer() const {
return isDefer(DC);
}
template<typename StmtTy>
bool typeCheckStmt(StmtTy *&S) {
FrontendStatsTracer StatsTracer(getASTContext().Stats,
"typecheck-stmt", S);
PrettyStackTraceStmt trace(getASTContext(), "type-checking", S);
StmtTy *S2 = cast_or_null<StmtTy>(visit(S));
if (S2 == nullptr)
return true;
S = S2;
performStmtDiagnostics(S, DC);
return false;
}
/// Type-check an entire function body.
bool typeCheckBody(BraceStmt *&S) {
bool HadError = typeCheckStmt(S);
S->walk(ContextualizeClosuresAndMacros(DC));
return HadError;
}
void typeCheckASTNode(ASTNode &node);
//===--------------------------------------------------------------------===//
// Visit Methods.
//===--------------------------------------------------------------------===//
Stmt *visitBraceStmt(BraceStmt *BS);
Stmt *visitReturnStmt(ReturnStmt *RS) {
// First, let's do a pre-check, and bail if the return is completely
// invalid.
auto &eval = getASTContext().evaluator;
auto *S =
evaluateOrDefault(eval, PreCheckReturnStmtRequest{RS, DC}, nullptr);
// We do a cast here as it may have been turned into a FailStmt. We should
// return that without doing anything else.
RS = dyn_cast_or_null<ReturnStmt>(S);
if (!RS)
return S;
auto TheFunc = AnyFunctionRef::fromDeclContext(DC);
assert(TheFunc && "Should have bailed from pre-check if this is None");
Type ResultTy = TheFunc->getBodyResultType();
if (!ResultTy || ResultTy->hasError())
return nullptr;
if (!RS->hasResult()) {
if (!ResultTy->isVoid())
getASTContext().Diags.diagnose(RS->getReturnLoc(),
diag::return_expr_missing);
return RS;
}
using namespace constraints;
auto target = SyntacticElementTarget::forReturn(RS, ResultTy, DC);
auto resultTarget = TypeChecker::typeCheckTarget(target);
if (resultTarget) {
RS->setResult(resultTarget->getAsExpr());
} else {
tryDiagnoseUnnecessaryCastOverOptionSet(getASTContext(), RS->getResult(),
ResultTy, DC->getParentModule());
}
return RS;
}
Stmt *visitYieldStmt(YieldStmt *YS) {
// If the yield is in a defer, then it isn't valid.
if (isInDefer()) {
getASTContext().Diags.diagnose(YS->getYieldLoc(),
diag::jump_out_of_defer, "yield");
return YS;
}
SmallVector<AnyFunctionType::Yield, 4> buffer;
auto TheFunc = AnyFunctionRef::fromDeclContext(DC);
auto yieldResults = TheFunc->getBodyYieldResults(buffer);
auto yieldExprs = YS->getMutableYields();
if (yieldExprs.size() != yieldResults.size()) {
getASTContext().Diags.diagnose(YS->getYieldLoc(), diag::bad_yield_count,
yieldResults.size());
return YS;
}
for (auto i : indices(yieldExprs)) {
Type yieldType = yieldResults[i].getType();
auto exprToCheck = yieldExprs[i];
InOutExpr *inout = nullptr;
// Classify whether we're yielding by reference or by value.
ContextualTypePurpose contextTypePurpose;
Type contextType = yieldType;
if (yieldResults[i].isInOut()) {
contextTypePurpose = CTP_YieldByReference;
contextType = LValueType::get(contextType);
// Check that the yielded expression is a &.
if ((inout = dyn_cast<InOutExpr>(exprToCheck))) {
// Strip the & off so that the constraint system doesn't complain
// about the unparented &.
exprToCheck = inout->getSubExpr();
} else {
getASTContext().Diags.diagnose(exprToCheck->getLoc(),
diag::missing_address_of_yield, yieldType)
.highlight(exprToCheck->getSourceRange());
inout = new (getASTContext()) InOutExpr(exprToCheck->getStartLoc(),
exprToCheck,
Type(), /*implicit*/ true);
}
} else {
contextTypePurpose = CTP_YieldByValue;
}
TypeChecker::typeCheckExpression(exprToCheck, DC,
{contextType, contextTypePurpose});
// Propagate the change into the inout expression we stripped before.
if (inout) {
inout->setSubExpr(exprToCheck);
inout->setType(InOutType::get(yieldType));
exprToCheck = inout;
}
// Note that this modifies the statement's expression list in-place.
yieldExprs[i] = exprToCheck;
}
return YS;
}
Stmt *visitThenStmt(ThenStmt *TS) {
// These are invalid outside of if/switch expressions, but let's type-check
// to get extra diagnostics.
getASTContext().Diags.diagnose(TS->getThenLoc(),
diag::out_of_place_then_stmt);
auto *E = TS->getResult();
TypeChecker::typeCheckExpression(E, DC);
TS->setResult(E);
return TS;
}
Stmt *visitThrowStmt(ThrowStmt *TS) {
// Coerce the operand to the exception type.
auto E = TS->getSubExpr();
// Look up the catch node for this "throw" to determine the error type.
CatchNode catchNode = ASTScope::lookupCatchNode(
DC->getParentModule(), TS->getThrowLoc());
Type errorType;
if (catchNode) {
errorType = catchNode.getThrownErrorTypeInContext(Ctx).value_or(Type());
}
// If there was no error type, use 'any Error'. We'll check it later.
if (!errorType) {
errorType = getASTContext().getErrorExistentialType();
}
if (!errorType)
return TS;
TypeChecker::typeCheckExpression(E, DC, {errorType, CTP_ThrowStmt});
TS->setSubExpr(E);
return TS;
}
Stmt *visitDiscardStmt(DiscardStmt *DS) {
// There are a lot of rules about whether a discard statement is even valid.
//
// The order of the checks below roughly reflects a sort of funneling from
// least correct to most correct usage, while aiming to not emit more than
// one diagnostic for misuse, since there are so many ways you can write it
// in the wrong place.
constraints::ContextualTypeInfo contextualInfo;
auto &ctx = getASTContext();
bool diagnosed = false;
auto *outerDC = climbContextForDiscardStmt(DC);
AbstractFunctionDecl *fn = nullptr; // the type member we reside in.
if (outerDC->getParent()->isTypeContext()) {
fn = dyn_cast<AbstractFunctionDecl>(outerDC);
}
// The discard statement must be in some type's member.
if (!fn) {
// Then we're not in some type's member function; emit diagnostics.
if (auto decl = outerDC->getAsDecl()) {
ctx.Diags.diagnose(DS->getDiscardLoc(), diag::discard_wrong_context_decl,
decl->getDescriptiveKind());
} else if (auto clos = dyn_cast<AbstractClosureExpr>(outerDC)) {
ctx.Diags.diagnose(DS->getDiscardLoc(),
diag::discard_wrong_context_closure);
} else {
ctx.Diags.diagnose(DS->getDiscardLoc(), diag::discard_wrong_context_misc);
}
diagnosed = true;
}
// Member function-like-thing must have a 'self' and not be a destructor.
if (!diagnosed) {
// Save this for SILGen, since Stmt's don't know their decl context.
DS->setInnermostMethodContext(fn);
if (fn->isStatic() || isa<DestructorDecl>(fn)
|| isa<ConstructorDecl>(fn)) {
ctx.Diags.diagnose(DS->getDiscardLoc(), diag::discard_wrong_context_decl,
fn->getDescriptiveKind());
diagnosed = true;
}
}
// check the kind of type this discard statement appears within.
if (!diagnosed) {
auto *nominalDecl = fn->getDeclContext()->getSelfNominalTypeDecl();
Type nominalType =
fn->mapTypeIntoContext(nominalDecl->getDeclaredInterfaceType());
// must be noncopyable
if (!nominalType->isNoncopyable()) {
ctx.Diags.diagnose(DS->getDiscardLoc(),
diag::discard_wrong_context_copyable,
fn->getDescriptiveKind());
diagnosed = true;
// has to have a deinit or else it's pointless.
} else if (!nominalDecl->getValueTypeDestructor()) {
ctx.Diags.diagnose(DS->getDiscardLoc(),
diag::discard_no_deinit,
nominalType)
.fixItRemove(DS->getSourceRange());
diagnosed = true;
} else {
// Set the contextual type for the sub-expression before we typecheck.
contextualInfo = {nominalType, CTP_DiscardStmt};
// Now verify that we're not discarding a type from another module.
//
// NOTE: We could do a proper resilience check instead of just asking
// if the modules differ, so that you can discard a @frozen type from a
// resilient module. But for now the proposal simply says that it has to
// be the same module, which is probably better for everyone.
auto *fnModule = fn->getModuleContext();
auto *typeModule = nominalDecl->getModuleContext();
if (fnModule != typeModule) {
ctx.Diags.diagnose(DS->getDiscardLoc(), diag::discard_wrong_module,
nominalType);
diagnosed = true;
} else {
assert(
!nominalDecl->isResilient(fnModule, ResilienceExpansion::Maximal)
&& "trying to discard a type resilient to us!");
}
}
}
{
// Typecheck the sub expression unconditionally.
auto E = DS->getSubExpr();
TypeChecker::typeCheckExpression(E, DC, contextualInfo);
DS->setSubExpr(E);
}
// Can only 'discard self'. This check must happen after typechecking.
if (!diagnosed) {
bool isSelf = false;
auto *checkE = DS->getSubExpr();
assert(fn->getImplicitSelfDecl() && "no self?");
// Look through a load. Only expected if we're in an init.
if (auto *load = dyn_cast<LoadExpr>(checkE))
checkE = load->getSubExpr();
if (auto DRE = dyn_cast<DeclRefExpr>(checkE))
isSelf = DRE->getDecl() == fn->getImplicitSelfDecl();
if (!isSelf) {
ctx.Diags
.diagnose(DS->getStartLoc(), diag::discard_wrong_not_self)
.fixItReplace(DS->getSubExpr()->getSourceRange(), "self");
diagnosed = true;
}
}
// The 'self' parameter must be owned (aka "consuming").
if (!diagnosed) {
if (auto *funcDecl = dyn_cast<FuncDecl>(fn)) {
switch (funcDecl->getSelfAccessKind()) {
case SelfAccessKind::LegacyConsuming:
case SelfAccessKind::Consuming:
break;
case SelfAccessKind::Borrowing:
case SelfAccessKind::NonMutating:
case SelfAccessKind::Mutating:
ctx.Diags.diagnose(DS->getDiscardLoc(),
diag::discard_wrong_context_nonconsuming,
fn->getDescriptiveKind());
diagnosed = true;
break;
}
}
}
return DS;
}
Stmt *visitPoundAssertStmt(PoundAssertStmt *PA) {
Expr *C = PA->getCondition();
TypeChecker::typeCheckCondition(C, DC);
PA->setCondition(C);
return PA;
}
Stmt *visitDeferStmt(DeferStmt *DS) {
TypeChecker::typeCheckDecl(DS->getTempDecl());
Expr *theCall = DS->getCallExpr();
TypeChecker::typeCheckExpression(theCall, DC);
DS->setCallExpr(theCall);
return DS;
}
Stmt *visitIfStmt(IfStmt *IS) {
typeCheckConditionForStatement(IS, DC);
auto sourceFile = DC->getParentSourceFile();
checkLabeledStmtShadowing(getASTContext(), sourceFile, IS);
auto *TS = IS->getThenStmt();
typeCheckStmt(TS);
IS->setThenStmt(TS);
if (auto *ES = IS->getElseStmt()) {
typeCheckStmt(ES);
IS->setElseStmt(ES);
}
return IS;
}
Stmt *visitGuardStmt(GuardStmt *GS) {
typeCheckConditionForStatement(GS, DC);
BraceStmt *S = GS->getBody();
typeCheckStmt(S);
GS->setBody(S);
return GS;
}
Stmt *visitDoStmt(DoStmt *DS) {
auto sourceFile = DC->getParentSourceFile();
checkLabeledStmtShadowing(getASTContext(), sourceFile, DS);
BraceStmt *S = DS->getBody();
typeCheckStmt(S);
DS->setBody(S);
return DS;
}
Stmt *visitWhileStmt(WhileStmt *WS) {
typeCheckConditionForStatement(WS, DC);
auto sourceFile = DC->getParentSourceFile();
checkLabeledStmtShadowing(getASTContext(), sourceFile, WS);
Stmt *S = WS->getBody();
typeCheckStmt(S);
WS->setBody(S);
return WS;
}
Stmt *visitRepeatWhileStmt(RepeatWhileStmt *RWS) {
auto sourceFile = DC->getParentSourceFile();
checkLabeledStmtShadowing(getASTContext(), sourceFile, RWS);
Stmt *S = RWS->getBody();
typeCheckStmt(S);
RWS->setBody(S);
Expr *E = RWS->getCond();
TypeChecker::typeCheckCondition(E, DC);
RWS->setCond(E);
return RWS;
}
Stmt *visitForEachStmt(ForEachStmt *S) {
GenericEnvironment *genericSignature =
genericSigStack.empty() ? nullptr : genericSigStack.back();
if (TypeChecker::typeCheckForEachPreamble(DC, S, genericSignature))
return nullptr;
// Type-check the body of the loop.
auto sourceFile = DC->getParentSourceFile();
checkLabeledStmtShadowing(getASTContext(), sourceFile, S);
BraceStmt *Body = S->getBody();
if (auto packExpansion =
dyn_cast<PackExpansionExpr>(S->getParsedSequence()))
genericSigStack.push_back(packExpansion->getGenericEnvironment());
typeCheckStmt(Body);
S->setBody(Body);
if (isa<PackExpansionExpr>(S->getParsedSequence()))
genericSigStack.pop_back();
return S;
}
Stmt *visitBreakStmt(BreakStmt *S) {
// Force the target to be computed in case it produces diagnostics.
(void)S->getTarget();
return S;
}
Stmt *visitContinueStmt(ContinueStmt *S) {
// Force the target to be computed in case it produces diagnostics.
(void)S->getTarget();
return S;
}
Stmt *visitFallthroughStmt(FallthroughStmt *S) {
if (checkFallthroughStmt(DC, S))
return nullptr;
return S;
}
void checkCaseLabelItemPattern(CaseStmt *caseBlock, CaseLabelItem &labelItem,
CaseParentKind parentKind,
bool &limitExhaustivityChecks,
Type subjectType) {
Pattern *pattern = labelItem.getPattern();
if (!labelItem.isPatternResolved()) {
pattern = TypeChecker::resolvePattern(
pattern, DC, /*isStmtCondition*/ false);
if (!pattern) {
return;
}
}
// Coerce the pattern to the subject's type.
bool coercionError = false;
if (subjectType) {
auto contextualPattern = ContextualPattern::forRawPattern(pattern, DC);
TypeResolutionOptions patternOptions(TypeResolverContext::InExpression);
if (parentKind == CaseParentKind::DoCatch)
patternOptions |= TypeResolutionFlags::SilenceNeverWarnings;
auto coercedPattern = TypeChecker::coercePatternToType(
contextualPattern, subjectType, patternOptions);
if (coercedPattern)
pattern = coercedPattern;
else
coercionError = true;
}
if (!subjectType || coercionError) {
limitExhaustivityChecks = true;
// If that failed, mark any variables binding pieces of the pattern
// as invalid to silence follow-on errors.
pattern->forEachVariable([&](VarDecl *VD) {
VD->setInvalid();
});
}
labelItem.setPattern(pattern, /*resolved=*/true);
// Otherwise for each variable in the pattern, make sure its type is
// identical to the initial case decl and stash the previous case decl as
// the parent of the decl.
pattern->forEachVariable([&](VarDecl *vd) {
if (!vd->hasName() || vd->isInvalid())
return;
// We know that prev var decls matches the initial var decl. So if we can
// match prevVarDecls, we can also match initial var decl... So for each
// decl in prevVarDecls...
auto expected = vd->getParentVarDecl();
if (!expected)
return;
// Then we check for errors.
//
// NOTE: We emit the diagnostics against the initial case label item var
// decl associated with expected to ensure that we always emit
// diagnostics against a single reference var decl. If we used expected
// instead, we would emit confusing diagnostics since a correct var decl
// after an incorrect var decl will be marked as incorrect. For instance
// given the following case statement.
//
// case .a(let x), .b(var x), .c(let x):
//
// if we use expected, we will emit errors saying that .b(var x) needs
// to be a let and .c(let x) needs to be a var. Thus if one
// automatically applied both fix-its, one would still get an error
// producing program:
//
// case .a(let x), .b(let x), .c(var x):
//
// More complex case label item lists could cause even longer fixup
// sequences. Thus, we emit errors against the VarDecl associated with
// expected in the initial case label item list.
//
// Luckily, it is easy for us to compute this since we only change the
// parent field of the initial case label item's VarDecls /after/ we
// finish updating the parent pointers of the VarDecls associated with
// all other CaseLabelItems. So that initial group of VarDecls are
// guaranteed to still have a parent pointer pointing at our
// CaseStmt. Since we have setup the parent pointer VarDecl linked list
// for all other CaseLabelItem var decls that we have already processed,
// we can use our VarDecl linked list to find that initial case label
// item VarDecl.
auto *initialCaseVarDecl = expected;
while (auto *prev = initialCaseVarDecl->getParentVarDecl()) {
initialCaseVarDecl = prev;
}
assert(isa<CaseStmt>(initialCaseVarDecl->getParentPatternStmt()));
if (!initialCaseVarDecl->isInvalid() &&
!vd->getInterfaceType()->isEqual(initialCaseVarDecl->getInterfaceType())) {
getASTContext().Diags.diagnose(
vd->getLoc(), diag::type_mismatch_multiple_pattern_list,
vd->getInterfaceType(), initialCaseVarDecl->getInterfaceType());
vd->setInvalid();
initialCaseVarDecl->setInvalid();
}
});
}
template <typename Iterator>
void checkSiblingCaseStmts(Iterator casesBegin, Iterator casesEnd,
CaseParentKind parentKind,
bool &limitExhaustivityChecks, Type subjectType) {
static_assert(
std::is_same<typename std::iterator_traits<Iterator>::value_type,
CaseStmt *>::value,
"Expected an iterator over CaseStmt *");
// First pass: check all of the bindings.
for (auto *caseBlock : make_range(casesBegin, casesEnd)) {
// Bind all of the pattern variables together so we can follow the
// "parent" pointers later on.
bindSwitchCasePatternVars(DC, caseBlock);
auto caseLabelItemArray = caseBlock->getMutableCaseLabelItems();
for (auto &labelItem : caseLabelItemArray) {
// Resolve the pattern in our case label if it has not been resolved and
// check that our var decls follow invariants.
checkCaseLabelItemPattern(caseBlock, labelItem, parentKind,
limitExhaustivityChecks, subjectType);
// Check the guard expression, if present.
if (auto *guard = labelItem.getGuardExpr()) {
limitExhaustivityChecks |= TypeChecker::typeCheckCondition(guard, DC);
labelItem.setGuardExpr(guard);
}
}
// Setup the types of our case body var decls.
for (auto *expected : caseBlock->getCaseBodyVariablesOrEmptyArray()) {
assert(expected->hasName());
auto prev = expected->getParentVarDecl();
if (prev->hasInterfaceType())
expected->setInterfaceType(prev->getInterfaceType());
}
}
// Second pass: type-check the body statements.
for (auto i = casesBegin; i != casesEnd; ++i) {
auto *caseBlock = *i;
// Check restrictions on '@unknown'.
if (caseBlock->hasUnknownAttr()) {
assert(parentKind == CaseParentKind::Switch &&
"'@unknown' can only appear on switch cases");
checkUnknownAttrRestrictions(
getASTContext(), caseBlock, limitExhaustivityChecks);
}
BraceStmt *body = caseBlock->getBody();
limitExhaustivityChecks |= typeCheckStmt(body);
caseBlock->setBody(body);
}
}
Stmt *visitSwitchStmt(SwitchStmt *switchStmt) {
// Type-check the subject expression.
Expr *subjectExpr = switchStmt->getSubjectExpr();
auto resultTy = TypeChecker::typeCheckExpression(subjectExpr, DC);
auto limitExhaustivityChecks = !resultTy;
if (Expr *newSubjectExpr =
TypeChecker::coerceToRValue(getASTContext(), subjectExpr))
subjectExpr = newSubjectExpr;
switchStmt->setSubjectExpr(subjectExpr);
Type subjectType = switchStmt->getSubjectExpr()->getType();
// Type-check the case blocks.
auto sourceFile = DC->getParentSourceFile();
checkLabeledStmtShadowing(getASTContext(), sourceFile, switchStmt);
// Preemptively visit all Decls (#if/#warning/#error) that still exist in
// the list of raw cases.
for (auto &node : switchStmt->getRawCases()) {
if (!node.is<Decl *>())
continue;
TypeChecker::typeCheckDecl(node.get<Decl *>());
}
auto cases = switchStmt->getCases();
checkSiblingCaseStmts(cases.begin(), cases.end(), CaseParentKind::Switch,
limitExhaustivityChecks, subjectType);
if (!switchStmt->isImplicit()) {
TypeChecker::checkSwitchExhaustiveness(switchStmt, DC,
limitExhaustivityChecks);
}
return switchStmt;
}
Stmt *visitCaseStmt(CaseStmt *S) {
// Cases are handled in visitSwitchStmt.
llvm_unreachable("case stmt outside of switch?!");
}
Stmt *visitDoCatchStmt(DoCatchStmt *S) {
// The labels are in scope for both the 'do' and all of the catch
// clauses. This allows the user to break out of (or restart) the
// entire construct.
auto sourceFile = DC->getParentSourceFile();
checkLabeledStmtShadowing(getASTContext(), sourceFile, S);
// Type-check the 'do' body. Type failures in here will generally
// not cause type failures in the 'catch' clauses.
Stmt *newBody = S->getBody();
typeCheckStmt(newBody);
S->setBody(newBody);
// Do-catch statements always limit exhaustivity checks.
bool limitExhaustivityChecks = true;
Type caughtErrorType = TypeChecker::catchErrorType(DC, S);
// If there was no throwing expression in the body, let's pretend it can
// throw 'any Error' just for type checking the pattern. That avoids
// superfluous diagnostics. Note that we still diagnose unreachable 'catch'
// separately in TypeCheckEffects.
if (caughtErrorType->isNever())
caughtErrorType = Ctx.getErrorExistentialType();
auto catches = S->getCatches();
checkSiblingCaseStmts(catches.begin(), catches.end(),
CaseParentKind::DoCatch, limitExhaustivityChecks,
caughtErrorType);
return S;
}
Stmt *visitFailStmt(FailStmt *S) {
// These are created as part of type-checking "return" in an initializer.
// There is nothing more to do.
return S;
}
};
} // end anonymous namespace
Stmt *PreCheckReturnStmtRequest::evaluate(Evaluator &evaluator, ReturnStmt *RS,
DeclContext *DC) const {
auto &ctx = DC->getASTContext();
auto fn = AnyFunctionRef::fromDeclContext(DC);
// Not valid outside of a function.
if (!fn) {
ctx.Diags.diagnose(RS->getReturnLoc(), diag::return_invalid_outside_func);
return nullptr;
}
// If the return is in a defer, then it isn't valid either.
if (isDefer(DC)) {
ctx.Diags.diagnose(RS->getReturnLoc(), diag::jump_out_of_defer, "return");
return nullptr;
}
// The rest of the checks only concern return statements with results.
if (!RS->hasResult())
return RS;
auto *E = RS->getResult();
// In an initializer, the only expressions allowed are "nil", which indicates
// failure from a failable initializer or "self" in the case of ~Escapable
// initializers with explicit lifetime dependence.
if (auto *ctor =
dyn_cast_or_null<ConstructorDecl>(fn->getAbstractFunctionDecl())) {
// The only valid return expression in an initializer is the literal
// 'nil'.
auto *nilExpr = dyn_cast<NilLiteralExpr>(E->getSemanticsProvidingExpr());
if (!nilExpr) {
if (ctor->hasLifetimeDependentReturn()) {
bool isSelf = false;
if (auto *UDRE = dyn_cast<UnresolvedDeclRefExpr>(E)) {
isSelf = UDRE->getName().isSimpleName(ctx.Id_self);
// Result the result expression so that rest of the compilation
// pipeline handles initializers with lifetime dependence specifiers
// in the same way as other initializers.
RS->setResult(nullptr);
}
if (!isSelf) {
ctx.Diags.diagnose(
RS->getStartLoc(),
diag::lifetime_dependence_ctor_non_self_or_nil_return);
RS->setResult(nullptr);
}
return RS;
}
ctx.Diags.diagnose(RS->getReturnLoc(), diag::return_init_non_nil)
.highlight(E->getSourceRange());
RS->setResult(nullptr);
return RS;
}
// "return nil" is only permitted in a failable initializer.
if (!ctor->isFailable()) {
ctx.Diags.diagnose(RS->getReturnLoc(), diag::return_non_failable_init)
.highlight(E->getSourceRange());
ctx.Diags
.diagnose(ctor->getLoc(), diag::make_init_failable, ctor)
.fixItInsertAfter(ctor->getLoc(), "?");
RS->setResult(nullptr);
return RS;
}
// Replace the "return nil" with a new 'fail' statement.
return new (ctx)
FailStmt(RS->getReturnLoc(), nilExpr->getLoc(), RS->isImplicit());
}
return RS;
}
static bool isDiscardableType(Type type) {
// If type is `(_: repeat ...)`, it can be discardable.
if (auto *tuple = type->getAs<TupleType>()) {
if (constraints::isSingleUnlabeledPackExpansionTuple(tuple)) {
type = tuple->getElementType(0);
}
}
if (auto *expansion = type->getAs<PackExpansionType>())
return isDiscardableType(expansion->getPatternType());
return (type->hasError() ||
type->isUninhabited() ||
type->lookThroughAllOptionalTypes()->isVoid());
}
static void diagnoseIgnoredLiteral(ASTContext &Ctx, LiteralExpr *LE) {
Ctx.Diags.diagnose(LE->getLoc(), diag::expression_unused_literal,
LE->getLiteralKindDescription())
.highlight(LE->getSourceRange());
}
void TypeChecker::checkIgnoredExpr(Expr *E) {
// Skip checking if there is no type, which presumably means there was a
// type error.
if (!E->getType()) {
return;
}
// Complain about l-values that are neither loaded nor stored.
auto &Context = E->getType()->getASTContext();
auto &DE = Context.Diags;
if (E->getType()->hasLValueType()) {
// This must stay in sync with diag::expression_unused_lvalue.
enum {
SK_Variable = 0,
SK_Property,
SK_Subscript
} storageKind = SK_Variable;
if (auto declRef = E->getReferencedDecl()) {
auto decl = declRef.getDecl();
if (isa<SubscriptDecl>(decl))
storageKind = SK_Subscript;
else if (decl->getDeclContext()->isTypeContext())
storageKind = SK_Property;
}
DE.diagnose(E->getLoc(), diag::expression_unused_lvalue, storageKind)
.highlight(E->getSourceRange());
return;
}
// Stash the type of the original expression for display: the precise
// expression we're looking for might have an intermediary, non-user-facing
// type, such as an opened archetype.
const Type TypeForDiag = E->getType();
// Drill through expressions we don't care about.
auto valueE = E;
while (1) {
valueE = valueE->getValueProvidingExpr();
if (auto *OEE = dyn_cast<OpenExistentialExpr>(valueE))
valueE = OEE->getSubExpr();
else if (auto *CRCE = dyn_cast<CovariantReturnConversionExpr>(valueE))
valueE = CRCE->getSubExpr();
else if (auto *EE = dyn_cast<ErasureExpr>(valueE))
valueE = EE->getSubExpr();
else if (auto *BOE = dyn_cast<BindOptionalExpr>(valueE))
valueE = BOE->getSubExpr();
else {
// If we have an OptionalEvaluationExpr at the top level, then someone is
// "optional chaining" and ignoring the result. Keep drilling if it
// doesn't make sense to ignore it.
if (auto *OEE = dyn_cast<OptionalEvaluationExpr>(valueE)) {
if (auto *IIO = dyn_cast<InjectIntoOptionalExpr>(OEE->getSubExpr())) {
valueE = IIO->getSubExpr();
} else if (auto *C = dyn_cast<CallExpr>(OEE->getSubExpr())) {
valueE = C;
} else if (auto *OE =
dyn_cast<OpenExistentialExpr>(OEE->getSubExpr())) {
valueE = OE;
} else {
break;
}
} else {
break;
}
}
}
// Check for macro expressions whose macros are marked as
// @discardableResult.
if (auto expansion = dyn_cast<MacroExpansionExpr>(valueE)) {
if (auto macro = expansion->getMacroRef().getDecl()) {
if (macro->getAttrs().hasAttribute<DiscardableResultAttr>()) {
return;
}
}
}
// Complain about functions that aren't called.
// TODO: What about tuples which contain functions by-value that are
// dead?
if (valueE->getType()->is<AnyFunctionType>()) {
bool isDiscardable = false;
// The called function could be wrapped inside a `dot_syntax_call_expr`
// node, for example:
//
// class Bar {
// @discardableResult
// func foo() -> Int { return 0 }
//
// func baz() {
// self.foo
// foo
// }
// }
//
// So look through the DSCE and get the function being called.
auto expr = isa<DotSyntaxCallExpr>(valueE)
? cast<DotSyntaxCallExpr>(valueE)->getFn()
: valueE;
if (auto *Fn = dyn_cast<ApplyExpr>(expr)) {
if (auto *calledValue = Fn->getCalledValue()) {
if (auto *FD = dyn_cast<AbstractFunctionDecl>(calledValue)) {
if (FD->getAttrs().hasAttribute<DiscardableResultAttr>()) {
isDiscardable = true;
}
}
}
}
if (!isDiscardable) {
DE.diagnose(E->getLoc(), diag::expression_unused_function)
.highlight(E->getSourceRange());
return;
}
}
// If the result of this expression is of type "Never" or "()"
// (the latter potentially wrapped in optionals) then it is
// safe to ignore.
if (isDiscardableType(valueE->getType()))
return;
// Complain about '#selector'.
if (auto *ObjCSE = dyn_cast<ObjCSelectorExpr>(valueE)) {
DE.diagnose(ObjCSE->getLoc(), diag::expression_unused_selector_result)
.highlight(E->getSourceRange());
return;
}
// Complain about '#keyPath'.
if (isa<KeyPathExpr>(valueE)) {
DE.diagnose(valueE->getLoc(), diag::expression_unused_keypath_result)
.highlight(E->getSourceRange());
return;
}
// Always complain about 'try?'.
if (auto *OTE = dyn_cast<OptionalTryExpr>(valueE)) {
DE.diagnose(OTE->getTryLoc(), diag::expression_unused_optional_try)
.highlight(E->getSourceRange());
return;
}
if (auto *LE = dyn_cast<LiteralExpr>(valueE)) {
diagnoseIgnoredLiteral(Context, LE);
return;
}
// Check if we have a call to a function not marked with
// '@discardableResult'.
if (auto call = dyn_cast<ApplyExpr>(valueE)) {
// Dig through all levels of calls.
Expr *fn = call->getFn();
while (true) {
fn = fn->getSemanticsProvidingExpr();
if (auto applyFn = dyn_cast<ApplyExpr>(fn)) {
fn = applyFn->getFn();
} else if (auto FVE = dyn_cast<ForceValueExpr>(fn)) {
fn = FVE->getSubExpr();
} else if (auto dotSyntaxRef = dyn_cast<DotSyntaxBaseIgnoredExpr>(fn)) {
fn = dotSyntaxRef->getRHS();
} else if (auto fnConvExpr = dyn_cast<FunctionConversionExpr>(fn)) {
fn = fnConvExpr->getSubExpr();
} else {
break;
}
}
// Find the callee.
AbstractFunctionDecl *callee = nullptr;
if (auto declRef = dyn_cast<DeclRefExpr>(fn))
callee = dyn_cast<AbstractFunctionDecl>(declRef->getDecl());
else if (auto ctorRef = dyn_cast<OtherConstructorDeclRefExpr>(fn))
callee = ctorRef->getDecl();
else if (auto memberRef = dyn_cast<MemberRefExpr>(fn))
callee = dyn_cast<AbstractFunctionDecl>(memberRef->getMember().getDecl());
else if (auto dynMemberRef = dyn_cast<DynamicMemberRefExpr>(fn))
callee = dyn_cast<AbstractFunctionDecl>(
dynMemberRef->getMember().getDecl());
// If the callee explicitly allows its result to be ignored, then don't
// complain.
if (callee && callee->getAttrs().getAttribute<DiscardableResultAttr>())
return;
// Otherwise, complain. Start with more specific diagnostics.
// Diagnose unused constructor calls.
if (isa_and_nonnull<ConstructorDecl>(callee) && !call->isImplicit()) {
DE.diagnose(fn->getLoc(), diag::expression_unused_init_result,
callee->getDeclContext()->getDeclaredInterfaceType())
.highlight(call->getArgs()->getSourceRange());
return;
}
SourceRange SR1 = call->getArgs()->getSourceRange(), SR2;
if (auto *BO = dyn_cast<BinaryExpr>(call)) {
SR1 = BO->getLHS()->getSourceRange();
SR2 = BO->getRHS()->getSourceRange();
}
// Otherwise, produce a generic diagnostic.
if (callee) {
auto &ctx = callee->getASTContext();
if (callee->isImplicit()) {
// Translate calls to implicit functions to their user-facing names
if (callee->getBaseName() == ctx.Id_derived_enum_equals ||
callee->getBaseName() == ctx.Id_derived_struct_equals) {
DE.diagnose(fn->getLoc(),
diag::expression_unused_result_operator_name,
ctx.Id_EqualsOperator)
.highlight(SR1).highlight(SR2);
return;
}
}
auto diagID = diag::expression_unused_result_call;
if (callee->getName().isOperator())
diagID = diag::expression_unused_result_operator;
DE.diagnose(fn->getLoc(), diagID, callee)
.highlight(SR1).highlight(SR2);
} else
DE.diagnose(fn->getLoc(), diag::expression_unused_result_unknown,
isa<ClosureExpr>(fn), TypeForDiag)
.highlight(SR1)
.highlight(SR2);
return;
}
// Produce a generic diagnostic.
DE.diagnose(valueE->getLoc(), diag::expression_unused_result, TypeForDiag)
.highlight(valueE->getSourceRange());
}
void StmtChecker::typeCheckASTNode(ASTNode &node) {
// Type check the expression
if (auto *E = node.dyn_cast<Expr *>()) {
auto checkMacroExpansion = [&] {
// If we have a macro expansion expression that's been replaced with a
// declaration, type-check that declaration.
if (auto macroExpr = dyn_cast<MacroExpansionExpr>(E)) {
if (auto decl = macroExpr->getSubstituteDecl()) {
ASTNode declNode(decl);
typeCheckASTNode(declNode);
return true;
}
}
return false;
};
if (checkMacroExpansion())
return;
auto &ctx = DC->getASTContext();
TypeCheckExprOptions options = TypeCheckExprFlags::IsExprStmt;
bool isDiscarded =
(!ctx.LangOpts.Playground && !ctx.LangOpts.DebuggerSupport);
if (isDiscarded)
options |= TypeCheckExprFlags::IsDiscarded;
auto resultTy =
TypeChecker::typeCheckExpression(E, DC, /*contextualInfo=*/{}, options);
// Check for a freestanding macro expansion that produced declarations or
// code items.
if (checkMacroExpansion())
return;
// If a closure expression is unused, the user might have intended to write
// "do { ... }".
auto *CE = dyn_cast<ClosureExpr>(E);
if (CE || isa<CaptureListExpr>(E)) {
ctx.Diags.diagnose(E->getLoc(), diag::expression_unused_closure);
if (CE && CE->hasAnonymousClosureVars() &&
CE->getParameters()->size() == 0) {
ctx.Diags.diagnose(CE->getStartLoc(), diag::brace_stmt_suggest_do)
.fixItInsert(CE->getStartLoc(), "do ");
}
} else if (isDiscarded && resultTy) {
TypeChecker::checkIgnoredExpr(E);
}
node = E;
return;
}
// Type check the statement.
if (auto *S = node.dyn_cast<Stmt *>()) {
typeCheckStmt(S);
node = S;
return;
}
// Type check the declaration.
if (auto *D = node.dyn_cast<Decl *>()) {
TypeChecker::typeCheckDecl(D);
return;
}
if (auto *Cond = node.dyn_cast<StmtConditionElement *>()) {
bool IsFalsable; // ignored
TypeChecker::typeCheckStmtConditionElement(*Cond, IsFalsable, DC);
return;
}
llvm_unreachable("Type checking null ASTNode");
}
Stmt *StmtChecker::visitBraceStmt(BraceStmt *BS) {
if (LeaveBraceStmtBodyUnchecked)
return BS;
// Diagnose defer statement being last one in block (only if
// BraceStmt does not start a TopLevelDecl).
if (!BS->empty()) {
if (auto stmt =
BS->getLastElement().dyn_cast<Stmt *>()) {
if (auto deferStmt = dyn_cast<DeferStmt>(stmt)) {
if (!isa<TopLevelCodeDecl>(DC) ||
cast<TopLevelCodeDecl>(DC)->getBody() != BS) {
getASTContext().Diags.diagnose(deferStmt->getStartLoc(),
diag::defer_stmt_at_block_end)
.fixItReplace(deferStmt->getStartLoc(), "do");
}
}
}
}
for (auto &elem : BS->getElements())
typeCheckASTNode(elem);
return BS;
}
void TypeChecker::typeCheckASTNode(ASTNode &node, DeclContext *DC,
bool LeaveBodyUnchecked) {
StmtChecker stmtChecker(DC);
// FIXME: 'ActiveLabeledStmts', etc. in StmtChecker are not
// populated. Since they don't affect "type checking", it's doesn't cause
// any issue for now. But it should be populated nonetheless.
stmtChecker.LeaveBraceStmtBodyUnchecked = LeaveBodyUnchecked;
stmtChecker.typeCheckASTNode(node);
}
static Type getResultBuilderType(FuncDecl *FD) {
Type builderType = FD->getResultBuilderType();
// For getters, fall back on looking on the attribute on the storage.
if (!builderType) {
auto accessor = dyn_cast<AccessorDecl>(FD);
if (accessor && accessor->getAccessorKind() == AccessorKind::Get) {
builderType = accessor->getStorage()->getResultBuilderType();
}
}
return builderType;
}
/// Attempts to build an implicit call within the provided constructor
/// to the provided class's zero-argument super initializer.
/// @returns nullptr if there was an error and a diagnostic was emitted.
static Expr* constructCallToSuperInit(ConstructorDecl *ctor,
ClassDecl *ClDecl) {
ASTContext &Context = ctor->getASTContext();
Expr *superRef = new (Context) SuperRefExpr(ctor->getImplicitSelfDecl(),
SourceLoc(), /*Implicit=*/true);
Expr *r = UnresolvedDotExpr::createImplicit(
Context, superRef, DeclBaseName::createConstructor());
r = CallExpr::createImplicitEmpty(Context, r);
if (ctor->hasThrows())
r = new (Context) TryExpr(SourceLoc(), r, Type(), /*implicit=*/true);
DiagnosticSuppression suppression(ctor->getASTContext().Diags);
auto resultTy = TypeChecker::typeCheckExpression(
r, ctor, /*contextualInfo=*/{}, TypeCheckExprFlags::IsDiscarded);
if (!resultTy)
return nullptr;
return r;
}
/// Check a super.init call.
///
/// \returns true if an error occurred.
static bool checkSuperInit(ConstructorDecl *fromCtor,
ApplyExpr *apply, bool implicitlyGenerated) {
// Make sure we are referring to a designated initializer.
auto otherCtorRef = dyn_cast<OtherConstructorDeclRefExpr>(
apply->getSemanticFn());
if (!otherCtorRef)
return false;
auto ctor = otherCtorRef->getDecl();
if (!ctor->isDesignatedInit()) {
if (!implicitlyGenerated) {
auto selfTy = fromCtor->getDeclContext()->getSelfInterfaceType();
if (auto classTy = selfTy->getClassOrBoundGenericClass()) {
assert(classTy->getSuperclass());
auto &Diags = fromCtor->getASTContext().Diags;
Diags.diagnose(apply->getArgs()->getLoc(), diag::chain_convenience_init,
classTy->getSuperclass());
ctor->diagnose(diag::convenience_init_here);
}
}
return true;
}
// For an implicitly generated super.init() call, make sure there's
// only one designated initializer.
if (implicitlyGenerated) {
auto *dc = ctor->getDeclContext();
auto *superclassDecl = dc->getSelfClassDecl();
superclassDecl->synthesizeSemanticMembersIfNeeded(
DeclBaseName::createConstructor());
NLOptions subOptions = NL_QualifiedDefault;
SmallVector<ValueDecl *, 4> lookupResults;
fromCtor->lookupQualified(superclassDecl,
DeclNameRef::createConstructor(),
apply->getLoc(),
subOptions, lookupResults);
for (auto decl : lookupResults) {
auto superclassCtor = dyn_cast<ConstructorDecl>(decl);
if (!superclassCtor || !superclassCtor->isDesignatedInit() ||
superclassCtor == ctor)
continue;
// Found another designated initializer in the superclass. Don't add the
// super.init() call.
return true;
}
// Make sure we can reference the designated initializer correctly.
auto loc = fromCtor->getLoc();
const bool didDiagnose = diagnoseDeclAvailability(
ctor, loc, nullptr,
ExportContext::forFunctionBody(fromCtor, loc));
if (didDiagnose) {
fromCtor->diagnose(diag::availability_unavailable_implicit_init,
ctor, superclassDecl->getName());
}
// Only allowed to synthesize a throwing super.init() call if the init being
// checked is also throwing.
if (ctor->hasThrows()) {
// Diagnose on nonthrowing or rethrowing initializer.
if (!fromCtor->hasThrows() || fromCtor->hasPolymorphicEffect(EffectKind::Throws)) {
fromCtor->diagnose(diag::implicit_throws_super_init);
return true; // considered an error
}
}
// Not allowed to implicitly generate a super.init() call if the init
// is async; that would hide the 'await' from the programmer.
if (ctor->hasAsync()) {
fromCtor->diagnose(diag::implicit_async_super_init);
return true; // considered an error
}
}
return false;
}
static bool isKnownEndOfConstructor(ASTNode N) {
auto *S = N.dyn_cast<Stmt*>();
if (!S) return false;
return isa<ReturnStmt>(S) || isa<FailStmt>(S);
}
/// Check for problems specific to the body of a constructor within a
/// class, involving (e.g.) implicit calls to the superclass initializer and
/// issues related to designated/convenience initializers.
static void checkClassConstructorBody(ClassDecl *classDecl,
ConstructorDecl *ctor,
BraceStmt *body) {
ASTContext &ctx = classDecl->getASTContext();
bool wantSuperInitCall = false;
bool isDelegating = false;
auto initKindAndExpr = ctor->getDelegatingOrChainedInitKind();
switch (initKindAndExpr.initKind) {
case BodyInitKind::Delegating:
isDelegating = true;
wantSuperInitCall = false;
break;
case BodyInitKind::Chained:
checkSuperInit(ctor, initKindAndExpr.initExpr, false);
/// A convenience initializer cannot chain to a superclass constructor.
if (ctor->isConvenienceInit()) {
ctx.Diags.diagnose(initKindAndExpr.initExpr->getLoc(),
diag::delegating_convenience_super_init,
ctor->getDeclContext()->getDeclaredInterfaceType());
}
LLVM_FALLTHROUGH;
case BodyInitKind::None:
wantSuperInitCall = false;
break;
case BodyInitKind::ImplicitChained:
wantSuperInitCall = true;
break;
}
// A class designated initializer must never be delegating.
if (ctor->isDesignatedInit() && isDelegating) {
if (classDecl->getForeignClassKind() == ClassDecl::ForeignKind::CFType) {
ctor->diagnose(diag::delegating_designated_init_in_extension,
ctor->getDeclContext()->getDeclaredInterfaceType());
} else {
ctor->diagnose(diag::delegating_designated_init,
ctor->getDeclContext()->getDeclaredInterfaceType())
.fixItInsert(ctor->getLoc(), "convenience ");
}
ctx.Diags.diagnose(initKindAndExpr.initExpr->getLoc(), diag::delegation_here);
}
// An inlinable constructor in a class must always be delegating,
// unless the class is '@_fixed_layout'.
// Note: This is specifically not using isFormallyResilient. We relax this
// rule for classes in non-resilient modules so that they can have inlinable
// constructors, as long as those constructors don't reference private
// declarations.
if (!isDelegating && classDecl->isResilient()) {
auto kind = ctor->getFragileFunctionKind();
if (kind.kind != FragileFunctionKind::None) {
ctor->diagnose(diag::class_designated_init_inlinable_resilient,
classDecl->getDeclaredInterfaceType(), kind.getSelector());
}
}
// If we don't want a super.init call, we're done.
if (!wantSuperInitCall)
return;
// Find a default initializer in the superclass.
Expr *SuperInitCall = constructCallToSuperInit(ctor, classDecl);
if (!SuperInitCall)
return;
// If the initializer we found is a designated initializer, we're okay.
class FindOtherConstructorRef : public ASTWalker {
public:
ApplyExpr *Found = nullptr;
MacroWalking getMacroWalkingBehavior() const override {
return MacroWalking::Expansion;
}
PreWalkResult<Expr *> walkToExprPre(Expr *E) override {
if (auto apply = dyn_cast<ApplyExpr>(E)) {
if (isa<OtherConstructorDeclRefExpr>(apply->getSemanticFn())) {
Found = apply;
return Action::Stop();
}
}
return Action::Continue(E);
}
};
FindOtherConstructorRef finder;
SuperInitCall->walk(finder);
if (!checkSuperInit(ctor, finder.Found, true)) {
// Store the super.init expression within the constructor declaration
// to be emitted during SILGen.
ctor->setSuperInitCall(SuperInitCall);
}
}
void swift::simple_display(llvm::raw_ostream &out,
const TypeCheckASTNodeAtLocContext &ctx) {
if (ctx.isForUnattachedNode()) {
llvm::errs() << "(unattached_node: ";
simple_display(out, ctx.getUnattachedNode());
llvm::errs() << " decl_context: ";
simple_display(out, ctx.getDeclContext());
llvm::errs() << ")";
} else {
llvm::errs() << "(decl_context: ";
simple_display(out, ctx.getDeclContext());
llvm::errs() << ")";
}
}
bool TypeCheckASTNodeAtLocRequest::evaluate(
Evaluator &evaluator, TypeCheckASTNodeAtLocContext typeCheckCtx,
SourceLoc Loc) const {
auto &ctx = typeCheckCtx.getDeclContext()->getASTContext();
assert(DiagnosticSuppression::isEnabled(ctx.Diags) &&
"Diagnosing and Single ASTNode type checking don't mix");
if (!typeCheckCtx.isForUnattachedNode()) {
auto DC = typeCheckCtx.getDeclContext();
// Initializers aren't walked by ASTWalker and thus we don't find the
// context to type check using ASTNodeFinder. Also, initializers aren't
// representable by ASTNodes that can be type checked using
// typeCheckASTNode. Handle them specifically here.
if (auto *patternInit = dyn_cast<PatternBindingInitializer>(DC)) {
if (auto *PBD = patternInit->getBinding()) {
auto i = patternInit->getBindingIndex();
PBD->getPattern(i)->forEachVariable(
[](VarDecl *VD) { (void)VD->getInterfaceType(); });
if (auto Init = PBD->getInit(i)) {
if (!PBD->isInitializerChecked(i)) {
typeCheckPatternBinding(PBD, i);
// Retrieve the accessor's body to trigger RecontextualizeClosures
// This is important to get the correct USR of variables defined
// in closures initializing lazy variables.
PBD->getPattern(i)->forEachVariable([](VarDecl *VD) {
VD->visitEmittedAccessors(
[&](AccessorDecl *accessor) { (void)accessor->getBody(); });
});
return false;
}
}
}
} else if (auto *defaultArg = dyn_cast<DefaultArgumentInitializer>(DC)) {
if (const ParamDecl *Param =
getParameterAt(defaultArg->getParent(), defaultArg->getIndex())) {
(void)Param->getTypeCheckedDefaultExpr();
return false;
}
}
if (auto *AFD = dyn_cast<AbstractFunctionDecl>(DC)) {
if (AFD->hasBody() && !AFD->isBodyTypeChecked() &&
!AFD->isBodySkipped()) {
// Pre-check the function body if needed.
(void)evaluateOrDefault(evaluator, PreCheckFunctionBodyRequest{AFD},
nullptr);
}
}
}
// Find innermost ASTNode at Loc from DC. Results the reference to the found
// ASTNode and the decl context of it.
class ASTNodeFinder : public ASTWalker {
SourceManager &SM;
SourceLoc Loc;
/// When the \c ASTNode that we want to check was found inside a brace
/// statement, we need to store a *reference* to the element in the
/// \c BraceStmt. When the brace statement gets type checked for result
/// builders its elements will be updated in-place, which makes
/// \c FoundNodeRef now point to the type-checked replacement node. We need
/// this behavior.
///
/// But for all other cases, we just want to store a plain \c ASTNode. To
/// make sure we free the \c ASTNode again, we store it in
/// \c FoundNodeStorage and set \c FoundNodeRef to point to
/// \c FoundNodeStorage.
ASTNode FoundNodeStorage;
ASTNode *FoundNode = nullptr;
/// The innermost DeclContext that contains \c FoundNode.
DeclContext *DC = nullptr;
public:
ASTNodeFinder(SourceManager &SM, SourceLoc Loc) : SM(SM), Loc(Loc) {}
/// Set an \c ASTNode and \c DeclContext to type check if we don't find a
/// more nested node.
void setInitialFind(ASTNode &FoundNode, DeclContext *DC) {
this->FoundNode = &FoundNode;
this->DC = DC;
}
bool isNull() const { return !FoundNode; }
ASTNode &getRef() const {
assert(FoundNode);
return *FoundNode;
}
DeclContext *getDeclContext() const { return DC; }
MacroWalking getMacroWalkingBehavior() const override {
return MacroWalking::ArgumentsAndExpansion;
}
PreWalkResult<Stmt *> walkToStmtPre(Stmt *S) override {
if (auto *brace = dyn_cast<BraceStmt>(S)) {
auto braceCharRange = Lexer::getCharSourceRangeFromSourceRange(
SM, brace->getSourceRange());
// Unless this brace contains the loc, there's nothing to do.
if (!braceCharRange.contains(Loc))
return Action::SkipNode(S);
// Reset the node found in a parent context if it's not part of this
// brace statement.
// We must not reset FoundNode if it's inside thei BraceStmt's source
// range because the found node could be inside a capture list, which is
// syntactically part of the brace stmt's range but won't be walked as
// a child of the brace stmt.
if (!brace->isImplicit() && FoundNode) {
auto foundNodeCharRange = Lexer::getCharSourceRangeFromSourceRange(
SM, FoundNode->getSourceRange());
if (!braceCharRange.contains(foundNodeCharRange)) {
FoundNode = nullptr;
}
}
for (ASTNode &node : brace->getElements()) {
if (SM.isBeforeInBuffer(Loc, node.getStartLoc()))
break;
// NOTE: We need to check the character loc here because the target
// loc can be inside the last token of the node. i.e. interpolated
// string.
SourceLoc endLoc = Lexer::getLocForEndOfToken(SM, node.getEndLoc());
if (SM.isBeforeInBuffer(endLoc, Loc) || endLoc == Loc)
continue;
// 'node' may be the target node, except 'CaseStmt' which cannot be
// type checked alone.
if (!node.isStmt(StmtKind::Case))
FoundNode = &node;
// Walk into the node to narrow down.
node.walk(*this);
break;
}
// Already walked into.
return Action::Stop();
} else if (auto Conditional = dyn_cast<LabeledConditionalStmt>(S)) {
for (StmtConditionElement &Cond : Conditional->getCond()) {
if (SM.isBeforeInBuffer(Loc, Cond.getStartLoc())) {
break;
}
SourceLoc endLoc = Lexer::getLocForEndOfToken(SM, Cond.getEndLoc());
if (SM.isBeforeInBuffer(endLoc, Loc) || endLoc == Loc) {
continue;
}
FoundNodeStorage = ASTNode(&Cond);
FoundNode = &FoundNodeStorage;
return Action::Stop();
}
}
return Action::Continue(S);
}
PreWalkResult<Expr *> walkToExprPre(Expr *E) override {
if (SM.isBeforeInBuffer(Loc, E->getStartLoc()))
return Action::SkipNode(E);
SourceLoc endLoc = Lexer::getLocForEndOfToken(SM, E->getEndLoc());
if (SM.isBeforeInBuffer(endLoc, Loc))
return Action::SkipNode(E);
// Don't walk into 'TapExpr'. They should be type checked with parent
// 'InterpolatedStringLiteralExpr'.
if (isa<TapExpr>(E))
return Action::SkipNode(E);
// If the location is within a result of a SingleValueStmtExpr, walk it
// directly rather than as part of the brace. This ensures we type-check
// it a part of the whole expression, unless it has an inner closure or
// SVE, in which case we can still pick up a better node to type-check.
if (auto *SVE = dyn_cast<SingleValueStmtExpr>(E)) {
SmallVector<Expr *> scratch;
for (auto *result : SVE->getResultExprs(scratch)) {
auto resultCharRange = Lexer::getCharSourceRangeFromSourceRange(
SM, result->getSourceRange());
if (resultCharRange.contains(Loc)) {
if (!result->walk(*this))
return Action::Stop();
return Action::SkipNode(E);
}
}
return Action::Continue(E);
}
if (auto closure = dyn_cast<ClosureExpr>(E)) {
// NOTE: When a client wants to type check a closure signature, it
// requests with closure's 'getLoc()' location.
if (Loc == closure->getLoc())
return Action::SkipNode(E);
DC = closure;
}
return Action::Continue(E);
}
PreWalkAction walkToDeclPre(Decl *D) override {
if (auto *newDC = dyn_cast<DeclContext>(D))
DC = newDC;
if (!SM.isBeforeInBuffer(Loc, D->getStartLoc())) {
// NOTE: We need to check the character loc here because the target
// loc can be inside the last token of the node. i.e. interpolated
// string.
SourceLoc endLoc = Lexer::getLocForEndOfToken(SM, D->getEndLoc());
if (!(SM.isBeforeInBuffer(endLoc, Loc) || endLoc == Loc)) {
if (!isa<TopLevelCodeDecl>(D)) {
FoundNodeStorage = ASTNode(D);
FoundNode = &FoundNodeStorage;
}
}
}
return Action::Continue();
}
} finder(ctx.SourceMgr, Loc);
if (typeCheckCtx.isForUnattachedNode()) {
finder.setInitialFind(typeCheckCtx.getUnattachedNode(),
typeCheckCtx.getDeclContext());
typeCheckCtx.getUnattachedNode().walk(finder);
} else {
typeCheckCtx.getDeclContext()->walkContext(finder);
}
// Nothing found at the location, or the decl context does not own the 'Loc'.
if (finder.isNull() || !finder.getDeclContext())
return true;
DeclContext *DC = finder.getDeclContext();
if (auto *AFD = dyn_cast<AbstractFunctionDecl>(DC)) {
if (AFD->isBodyTypeChecked())
return false;
ASTScope::expandFunctionBody(AFD);
}
// Function builder function doesn't support partial type checking.
if (auto *func = dyn_cast<FuncDecl>(DC)) {
if (Type builderType = getResultBuilderType(func)) {
if (func->getBody()) {
auto optBody =
TypeChecker::applyResultBuilderBodyTransform(func, builderType);
if ((ctx.CompletionCallback && ctx.CompletionCallback->gotCallback()) ||
(ctx.SolutionCallback && ctx.SolutionCallback->gotCallback())) {
// We already informed the completion callback of solutions found by
// type checking the entire result builder from
// applyResultBuilderBodyTransform. No need to typecheck the requested
// AST node individually anymore.
return false;
}
if (!ctx.CompletionCallback && !ctx.SolutionCallback && optBody &&
*optBody) {
// Wire up the function body now.
func->setBody(*optBody, AbstractFunctionDecl::BodyKind::TypeChecked);
return false;
}
// We did not find a solution while applying the result builder,
// possibly because the result builder contained an invalid element and
// thus the transform couldn't be applied. Perform code completion
// pretending there was no result builder to recover.
}
}
}
// If the context is a closure, type check the entire surrounding closure.
// Conjunction constraints ensure that statements unrelated to the one that
// contains the code completion token are not type checked.
if (auto CE = dyn_cast<ClosureExpr>(DC)) {
if (CE->getBodyState() == ClosureExpr::BodyState::Parsed) {
swift::typeCheckASTNodeAtLoc(
TypeCheckASTNodeAtLocContext::declContext(CE->getParent()),
CE->getLoc());
return false;
}
}
TypeChecker::typeCheckASTNode(finder.getRef(), DC, /*LeaveBodyUnchecked=*/false);
return false;
}
/// Insert an implicit return for a single expression body function if needed.
static void addImplicitReturnIfNeeded(BraceStmt *body, DeclContext *dc) {
if (body->empty())
return;
// Must have a single active element (which is guarenteed to be the last
// element), or we must be allowing implicit last expression results.
auto &ctx = dc->getASTContext();
if (!body->getSingleActiveElement() &&
!ctx.LangOpts.hasFeature(Feature::ImplicitLastExprResults)) {
return;
}
auto node = body->getLastElement();
if (!node)
return;
auto makeResult = [&](Expr *E) {
body->setLastElement(ReturnStmt::createImplied(ctx, E));
};
// For a constructor, we only support nil literals as the implicit result.
if (auto *ctor = dyn_cast<ConstructorDecl>(dc)) {
if (auto *E = node.dyn_cast<Expr *>()) {
if (ctor->isFailable() && isa<NilLiteralExpr>(E))
makeResult(E);
}
return;
}
// Otherwise, we only support implicit results for FuncDecls and ClosureExprs.
if (!isa<FuncDecl>(dc) && !isa<ClosureExpr>(dc))
return;
if (auto *fd = dyn_cast<FuncDecl>(dc)) {
// Don't apply if we have a result builder, or a Void return type.
if (getResultBuilderType(fd) || fd->getResultInterfaceType()->isVoid())
return;
}
// A statement can potentially become an expression.
if (auto *S = node.dyn_cast<Stmt *>()) {
if (S->mayProduceSingleValue(ctx)) {
auto *SVE = SingleValueStmtExpr::createWithWrappedBranches(
ctx, S, dc, /*mustBeExpr*/ false);
makeResult(SVE);
return;
}
}
if (auto *E = node.dyn_cast<Expr *>()) {
// Take any expression, except for assignments. This helps improves
// diagnostics.
// TODO: We probably ought to apply this to closures too, but that currently
// regresses a couple of diagnostics.
if (!isa<ClosureExpr>(dc)) {
auto *SemanticExpr = E->getSemanticsProvidingExpr();
if (auto *SE = dyn_cast<SequenceExpr>(SemanticExpr)) {
if (SE->getNumElements() > 1 && isa<AssignExpr>(SE->getElement(1)))
return;
}
if (isa<AssignExpr>(SemanticExpr))
return;
}
makeResult(E);
}
}
BraceStmt *
PreCheckFunctionBodyRequest::evaluate(Evaluator &evaluator,
AbstractFunctionDecl *AFD) const {
assert(!AFD->isBodySkipped());
auto &ctx = AFD->getASTContext();
auto *body = AFD->getBody();
assert(body && "Expected body");
assert(!AFD->isBodyTypeChecked() && "Body already type-checked?");
// Insert an implicit return if needed.
addImplicitReturnIfNeeded(body, AFD);
// For constructors, we make sure that the body ends with a "return"
// stmt, which we either implicitly synthesize, or the user can write.
// This simplifies SILGen.
if (auto *ctor = dyn_cast<ConstructorDecl>(AFD)) {
if (body->empty() || !isKnownEndOfConstructor(body->getLastElement())) {
SmallVector<ASTNode, 8> Elts(body->getElements().begin(),
body->getElements().end());
Elts.push_back(ReturnStmt::createImplicit(ctx, body->getRBraceLoc(),
/*value*/ nullptr));
body = BraceStmt::create(ctx, body->getLBraceLoc(), Elts,
body->getRBraceLoc(), body->isImplicit());
}
}
return body;
}
BraceStmt *PreCheckClosureBodyRequest::evaluate(Evaluator &evaluator,
ClosureExpr *closure) const {
auto *body = closure->getBody();
// If we have a single statement 'return', synthesize 'return ()' to ensure
// it's treated as a single expression body.
if (auto *S = body->getSingleActiveStatement()) {
if (auto *returnStmt = dyn_cast<ReturnStmt>(S)) {
if (!returnStmt->hasResult()) {
auto &ctx = closure->getASTContext();
auto *returnExpr = TupleExpr::createEmpty(ctx, SourceLoc(), SourceLoc(),
/*implicit*/ true);
returnStmt->setResult(returnExpr);
}
}
}
// Insert an implicit return if needed.
addImplicitReturnIfNeeded(body, closure);
return body;
}
/// Determine whether the given declaration requires a definition.
///
/// Only valid for declarations that can have definitions, i.e.,
/// functions, initializers, etc.
static bool requiresDefinition(Decl *decl) {
// Invalid, implicit, and Clang-imported declarations never
// require a definition.
if (decl->isInvalid() || decl->isImplicit() || decl->hasClangNode())
return false;
// Protocol requirements do not require definitions.
if (isa<ProtocolDecl>(decl->getDeclContext()))
return false;
// Functions can have _silgen_name, semantics, and NSManaged attributes.
if (auto func = dyn_cast<AbstractFunctionDecl>(decl)) {
if (func->getAttrs().hasAttribute<SILGenNameAttr>() ||
func->getAttrs().hasAttribute<ExternAttr>() ||
func->getAttrs().hasAttribute<SemanticsAttr>() ||
func->getAttrs().hasAttribute<NSManagedAttr>())
return false;
}
// Declarations in SIL and module interface files don't require
// definitions.
auto dc = decl->getDeclContext();
if (auto sourceFile = dc->getParentSourceFile()) {
switch (sourceFile->Kind) {
case SourceFileKind::SIL:
case SourceFileKind::Interface:
return false;
case SourceFileKind::Library:
case SourceFileKind::Main:
case SourceFileKind::MacroExpansion:
case SourceFileKind::DefaultArgument:
break;
}
}
// Declarations deserialized from a module file don't require definitions.
if (auto fileUnit = dyn_cast<FileUnit>(dc->getModuleScopeContext()))
if (fileUnit->getKind() == FileUnitKind::SerializedAST)
return false;
// Everything else requires a definition.
return true;
}
/// Determine whether the given declaration should not have a definition.
static bool requiresNoDefinition(Decl *decl) {
if (auto func = dyn_cast<AbstractFunctionDecl>(decl)) {
// Function with @_extern should not have a body.
return func->getAttrs().hasAttribute<ExternAttr>();
}
// Everything else can have a definition.
return false;
}
BraceStmt *
TypeCheckFunctionBodyRequest::evaluate(Evaluator &eval,
AbstractFunctionDecl *AFD) const {
ASTContext &ctx = AFD->getASTContext();
std::optional<FunctionBodyTimer> timer;
const auto &tyOpts = ctx.TypeCheckerOpts;
if (tyOpts.DebugTimeFunctionBodies || tyOpts.WarnLongFunctionBodies)
timer.emplace(AFD);
/// If the function body has been skipped, there's nothing to do here.
if (AFD->isBodySkipped())
return nullptr;
BraceStmt *body = AFD->getMacroExpandedBody();
// If there is no function body, there is nothing to type-check.
if (!body) {
// If a definition is required here, complain.
if (requiresDefinition(AFD)) {
if (isa<ConstructorDecl>(AFD))
AFD->diagnose(diag::missing_initializer_def);
else
AFD->diagnose(diag::func_decl_without_brace);
}
return nullptr;
}
// If the function body must not have a definition, complain and drop it.
if (requiresNoDefinition(AFD)) {
AFD->diagnose(diag::func_decl_no_body_expected);
return nullptr;
}
assert(body && "Expected body to type-check");
// It's possible we synthesized an already type-checked body, in which case
// we're done.
if (AFD->isBodyTypeChecked())
return body;
auto errorBody = [&]() {
// If we had an error, return an ErrorExpr body instead of returning the
// un-type-checked body.
// FIXME: This should be handled by typeCheckExpression.
auto range = AFD->getBodySourceRange();
return BraceStmt::create(ctx, range.Start,
{new (ctx) ErrorExpr(range, ErrorType::get(ctx))},
range.End);
};
// First do a pre-check of the body.
body = evaluateOrDefault(eval, PreCheckFunctionBodyRequest{AFD}, nullptr);
assert(body);
// Then apply a result builder if we have one, which if successful will
// produce a type-checked body.
bool alreadyTypeChecked = false;
if (auto *func = dyn_cast<FuncDecl>(AFD)) {
if (Type builderType = getResultBuilderType(func)) {
if (auto optBody =
TypeChecker::applyResultBuilderBodyTransform(
func, builderType)) {
if (!*optBody)
return errorBody();
body = *optBody;
alreadyTypeChecked = true;
body->walk(ContextualizeClosuresAndMacros(AFD));
}
}
}
// Typechecking, in particular ApplySolution is going to replace closures
// with OpaqueValueExprs and then try to do lookups into the closures.
// So, build out the body now.
ASTScope::expandFunctionBody(AFD);
if (AFD->isDistributedThunk()) {
if (auto func = dyn_cast<FuncDecl>(AFD)) {
if (TypeChecker::checkDistributedFunc(func)) {
return errorBody();
}
}
}
// Type check the function body if needed.
bool hadError = false;
if (!alreadyTypeChecked) {
StmtChecker SC(AFD);
hadError = SC.typeCheckBody(body);
}
// Class constructor checking.
if (auto *ctor = dyn_cast<ConstructorDecl>(AFD)) {
if (auto classDecl = ctor->getDeclContext()->getSelfClassDecl()) {
checkClassConstructorBody(classDecl, ctor, body);
}
}
// Temporarily wire up the function body for some extra checks.
// FIXME: Eliminate this.
AFD->setBody(body, AbstractFunctionDecl::BodyKind::TypeChecked);
// If nothing went wrong yet, perform extra checking.
if (!hadError)
performAbstractFuncDeclDiagnostics(AFD);
TypeChecker::computeCaptures(AFD);
if (!AFD->getDeclContext()->isLocalContext()) {
checkFunctionActorIsolation(AFD);
TypeChecker::checkFunctionEffects(AFD);
}
return hadError ? errorBody() : body;
}
bool TypeChecker::typeCheckClosureBody(ClosureExpr *closure) {
TypeChecker::checkClosureAttributes(closure);
TypeChecker::checkParameterList(closure->getParameters(), closure);
BraceStmt *body = closure->getBody();
std::optional<FunctionBodyTimer> timer;
const auto &tyOpts = closure->getASTContext().TypeCheckerOpts;
if (tyOpts.DebugTimeFunctionBodies || tyOpts.WarnLongFunctionBodies)
timer.emplace(closure);
bool HadError = StmtChecker(closure).typeCheckBody(body);
if (body) {
closure->setBody(body);
}
closure->setBodyState(ClosureExpr::BodyState::SeparatelyTypeChecked);
return HadError;
}
bool TypeChecker::typeCheckTapBody(TapExpr *expr, DeclContext *DC) {
// We intentionally use typeCheckStmt instead of typeCheckBody here
// because we want to contextualize TapExprs with the body they're in.
BraceStmt *body = expr->getBody();
bool HadError = StmtChecker(DC).typeCheckStmt(body);
if (body) {
expr->setBody(body);
}
return HadError;
}
void TypeChecker::typeCheckTopLevelCodeDecl(TopLevelCodeDecl *TLCD) {
BraceStmt *Body = TLCD->getBody();
StmtChecker(TLCD).typeCheckBody(Body);
TLCD->setBody(Body);
checkTopLevelActorIsolation(TLCD);
checkTopLevelEffects(TLCD);
performTopLevelDeclDiagnostics(TLCD);
}
namespace {
/// An ASTWalker that searches for any break/continue/return statements that
/// jump out of the context the walker starts at.
class JumpOutOfContextFinder : public ASTWalker {
TinyPtrVector<Stmt *> &Jumps;
SmallPtrSet<Stmt *, 4> ParentLabeledStmts;
public:
JumpOutOfContextFinder(TinyPtrVector<Stmt *> &jumps) : Jumps(jumps) {}
MacroWalking getMacroWalkingBehavior() const override {
return MacroWalking::Expansion;
}
PreWalkResult<Stmt *> walkToStmtPre(Stmt *S) override {
if (auto *LS = dyn_cast<LabeledStmt>(S))
ParentLabeledStmts.insert(LS);
// Cannot 'break', 'continue', or 'return' out of the statement. A jump to
// a statement within a branch however is fine.
if (auto *BS = dyn_cast<BreakStmt>(S)) {
if (!ParentLabeledStmts.contains(BS->getTarget()))
Jumps.push_back(BS);
}
if (auto *CS = dyn_cast<ContinueStmt>(S)) {
if (!ParentLabeledStmts.contains(CS->getTarget()))
Jumps.push_back(CS);
}
if (isa<ReturnStmt>(S) || isa<FailStmt>(S))
Jumps.push_back(S);
return Action::Continue(S);
}
PostWalkResult<Stmt *> walkToStmtPost(Stmt *S) override {
if (auto *LS = dyn_cast<LabeledStmt>(S)) {
auto removed = ParentLabeledStmts.erase(LS);
assert(removed);
(void)removed;
}
return Action::Continue(S);
}
PreWalkResult<Expr *> walkToExprPre(Expr *E) override {
// We don't need to walk into closures, you can't jump out of them.
return Action::SkipNodeIf(isa<AbstractClosureExpr>(E), E);
}
PreWalkAction walkToDeclPre(Decl *D) override {
// We don't need to walk into any nested local decls.
return Action::VisitNodeIf(isa<PatternBindingDecl>(D));
}
};
} // end anonymous namespace
/// Whether the given brace statement ends with a 'throw'.
static bool doesBraceEndWithThrow(BraceStmt *BS) {
if (BS->empty())
return false;
auto *S = BS->getLastElement().dyn_cast<Stmt *>();
if (!S)
return false;
return isa<ThrowStmt>(S);
}
IsSingleValueStmtResult
areBranchesValidForSingleValueStmt(ASTContext &ctx, ArrayRef<Stmt *> branches) {
TinyPtrVector<Stmt *> invalidJumps;
TinyPtrVector<Stmt *> unterminatedBranches;
JumpOutOfContextFinder jumpFinder(invalidJumps);
// Must have a single expression brace, and non-single-expression branches
// must end with a throw.
bool hadResult = false;
for (auto *branch : branches) {
auto *BS = dyn_cast<BraceStmt>(branch);
if (!BS)
return IsSingleValueStmtResult::unhandledStmt();
// Check to see if there are any invalid jumps.
BS->walk(jumpFinder);
// Must either have an explicit or implicit result for the branch.
if (SingleValueStmtExpr::hasResult(BS) ||
SingleValueStmtExpr::isLastElementImplicitResult(
BS, ctx, /*mustBeSingleValueStmt*/ false)) {
hadResult = true;
continue;
}
// If there was no result, the branch must end in a 'throw'.
if (!doesBraceEndWithThrow(BS))
unterminatedBranches.push_back(BS);
}
if (!invalidJumps.empty())
return IsSingleValueStmtResult::invalidJumps(std::move(invalidJumps));
if (!unterminatedBranches.empty()) {
return IsSingleValueStmtResult::unterminatedBranches(
std::move(unterminatedBranches));
}
// No branches produced a result, we can't turn this into an expression.
if (!hadResult)
return IsSingleValueStmtResult::noResult();
return IsSingleValueStmtResult::valid();
}
IsSingleValueStmtResult
IsSingleValueStmtRequest::evaluate(Evaluator &eval, const Stmt *S,
ASTContext *_ctx) const {
assert(_ctx);
auto &ctx = *_ctx;
// Statements must be unlabeled.
if (auto *LS = dyn_cast<LabeledStmt>(S)) {
if (LS->getLabelInfo())
return IsSingleValueStmtResult::hasLabel();
}
if (auto *IS = dyn_cast<IfStmt>(S)) {
// Must be exhaustive.
if (!IS->isSyntacticallyExhaustive())
return IsSingleValueStmtResult::nonExhaustiveIf();
SmallVector<Stmt *, 4> scratch;
return areBranchesValidForSingleValueStmt(ctx, IS->getBranches(scratch));
}
if (auto *SS = dyn_cast<SwitchStmt>(S)) {
SmallVector<Stmt *, 4> scratch;
return areBranchesValidForSingleValueStmt(ctx, SS->getBranches(scratch));
}
if (auto *DS = dyn_cast<DoStmt>(S)) {
if (!ctx.LangOpts.hasFeature(Feature::DoExpressions))
return IsSingleValueStmtResult::unhandledStmt();
return areBranchesValidForSingleValueStmt(ctx, DS->getBody());
}
if (auto *DCS = dyn_cast<DoCatchStmt>(S)) {
if (!ctx.LangOpts.hasFeature(Feature::DoExpressions))
return IsSingleValueStmtResult::unhandledStmt();
if (!DCS->isSyntacticallyExhaustive())
return IsSingleValueStmtResult::nonExhaustiveDoCatch();
SmallVector<Stmt *, 4> scratch;
return areBranchesValidForSingleValueStmt(ctx, DCS->getBranches(scratch));
}
return IsSingleValueStmtResult::unhandledStmt();
}
void swift::checkUnknownAttrRestrictions(
ASTContext &ctx, CaseStmt *caseBlock,
bool &limitExhaustivityChecks) {
CaseStmt *fallthroughDest = caseBlock->findNextCaseStmt();
if (caseBlock->getCaseLabelItems().size() != 1) {
assert(!caseBlock->getCaseLabelItems().empty() &&
"parser should not produce case blocks with no items");
ctx.Diags.diagnose(caseBlock->getLoc(),
diag::unknown_case_multiple_patterns)
.highlight(caseBlock->getCaseLabelItems()[1].getSourceRange());
limitExhaustivityChecks = true;
}
if (fallthroughDest != nullptr) {
if (!caseBlock->isDefault())
ctx.Diags.diagnose(caseBlock->getLoc(),
diag::unknown_case_must_be_last);
limitExhaustivityChecks = true;
}
const auto &labelItem = caseBlock->getCaseLabelItems().front();
if (labelItem.getGuardExpr() && !labelItem.isDefault()) {
ctx.Diags.diagnose(labelItem.getStartLoc(),
diag::unknown_case_where_clause)
.highlight(labelItem.getGuardExpr()->getSourceRange());
}
const Pattern *pattern =
labelItem.getPattern()->getSemanticsProvidingPattern();
if (!isa<AnyPattern>(pattern)) {
ctx.Diags.diagnose(labelItem.getStartLoc(),
diag::unknown_case_must_be_catchall)
.highlight(pattern->getSourceRange());
}
}
void swift::bindSwitchCasePatternVars(DeclContext *dc, CaseStmt *caseStmt) {
llvm::SmallDenseMap<Identifier, std::pair<VarDecl *, bool>, 4> latestVars;
auto recordVar = [&](Pattern *pattern, VarDecl *var) {
if (!var->hasName())
return;
// If there is an existing variable with this name, set it as the
// parent of this new variable.
auto &entry = latestVars[var->getName()];
if (entry.first) {
assert(!var->getParentVarDecl() ||
var->getParentVarDecl() == entry.first);
var->setParentVarDecl(entry.first);
// Check for a mutability mismatch.
if (pattern && entry.second != var->isLet()) {
// Find the original declaration.
auto initialCaseVarDecl = entry.first;
while (auto parentVar = initialCaseVarDecl->getParentVarDecl())
initialCaseVarDecl = parentVar;
auto diag = var->diagnose(diag::mutability_mismatch_multiple_pattern_list,
var->isLet(), initialCaseVarDecl->isLet());
BindingPattern *foundVP = nullptr;
pattern->forEachNode([&](Pattern *P) {
if (auto *VP = dyn_cast<BindingPattern>(P))
if (VP->getSingleVar() == var)
foundVP = VP;
});
if (foundVP)
diag.fixItReplace(foundVP->getLoc(),
initialCaseVarDecl->isLet() ? "let" : "var");
var->setInvalid();
initialCaseVarDecl->setInvalid();
}
} else {
entry.second = var->isLet();
}
// Record this variable as the latest with this name.
entry.first = var;
};
// Wire up the parent var decls for each variable that occurs within
// the patterns of each case item. in source order.
for (auto &caseItem : caseStmt->getMutableCaseLabelItems()) {
// Resolve the pattern.
auto *pattern = caseItem.getPattern();
if (!caseItem.isPatternResolved()) {
pattern = TypeChecker::resolvePattern(
pattern, dc, /*isStmtCondition=*/false);
if (!pattern)
continue;
}
caseItem.setPattern(pattern, /*resolved=*/true);
pattern->forEachVariable( [&](VarDecl *var) {
recordVar(pattern, var);
});
}
// Wire up the case body variables to the latest patterns.
for (auto bodyVar : caseStmt->getCaseBodyVariablesOrEmptyArray()) {
recordVar(nullptr, bodyVar);
}
}
FuncDecl *TypeChecker::getForEachIteratorNextFunction(
DeclContext *dc, SourceLoc loc, bool isAsync
) {
ASTContext &ctx = dc->getASTContext();
// A synchronous for..in loop uses IteratorProtocol.next().
if (!isAsync)
return ctx.getIteratorNext();
// If AsyncIteratorProtocol.next(_:) isn't available at all,
// we're stuck using AsyncIteratorProtocol.next().
auto nextElement = ctx.getAsyncIteratorNextIsolated();
if (!nextElement)
return ctx.getAsyncIteratorNext();
// If the enclosing function has @_unsafeInheritsExecutor, then #isolation
// does not work and we need to avoid relying on it.
if (enclosingUnsafeInheritsExecutor(dc))
return ctx.getAsyncIteratorNext();
// If availability checking is disabled, use next(_:).
if (ctx.LangOpts.DisableAvailabilityChecking || loc.isInvalid())
return nextElement;
// We can only call next(_:) if we are in an availability context
// that supports typed throws.
auto availability = overApproximateAvailabilityAtLocation(loc, dc);
if (availability.isContainedIn(ctx.getTypedThrowsAvailability()))
return nextElement;
// Fall back to AsyncIteratorProtocol.next().
return ctx.getAsyncIteratorNext();
}
|