1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078
|
//===--- TypeCheckEffects.cpp - Type Checking for Effects Coverage --------===//
//
// 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 to ensure that various effects (such
// as throwing and async) are properly handled.
//
//===----------------------------------------------------------------------===//
#include "TypeChecker.h"
#include "TypeCheckConcurrency.h"
#include "TypeCheckEffects.h"
#include "swift/AST/ASTWalker.h"
#include "swift/AST/DiagnosticsSema.h"
#include "swift/AST/Effects.h"
#include "swift/AST/GenericEnvironment.h"
#include "swift/AST/Initializer.h"
#include "swift/AST/ParameterList.h"
#include "swift/AST/Pattern.h"
#include "swift/AST/PrettyStackTrace.h"
#include "swift/AST/ProtocolConformance.h"
#include "swift/AST/TypeCheckRequests.h"
using namespace swift;
static bool hasFunctionParameterWithEffect(EffectKind kind, Type type) {
// Look through Optional types.
type = type->lookThroughAllOptionalTypes();
// Only consider function types with this effect.
if (auto fnType = type->getAs<AnyFunctionType>()) {
return fnType->hasEffect(kind);
}
// Look through tuples.
if (auto tuple = type->getAs<TupleType>()) {
for (auto eltType : tuple->getElementTypes()) {
if (hasFunctionParameterWithEffect(kind, eltType))
return true;
}
return false;
}
// Suppress diagnostics in the presence of errors.
if (type->hasError()) {
return true;
}
return false;
}
PolymorphicEffectRequirementList
PolymorphicEffectRequirementsRequest::evaluate(Evaluator &evaluator,
EffectKind kind,
ProtocolDecl *proto) const {
ASTContext &ctx = proto->getASTContext();
// only allow rethrowing requirements to be determined from marked protocols
if (!proto->hasPolymorphicEffect(kind)) {
return PolymorphicEffectRequirementList();
}
SmallVector<AbstractFunctionDecl *, 2> requirements;
SmallVector<std::pair<Type, ProtocolDecl *>, 2> conformances;
// check if immediate members of protocol are 'throws'
for (auto member : proto->getMembers()) {
auto fnDecl = dyn_cast<AbstractFunctionDecl>(member);
if (!fnDecl || !fnDecl->hasEffect(kind))
continue;
requirements.push_back(fnDecl);
}
// check associated conformances of associated types or inheritance
for (auto requirement : proto->getRequirementSignature().getRequirements()) {
if (requirement.getKind() != RequirementKind::Conformance)
continue;
auto *protoDecl = requirement.getProtocolDecl();
if (!protoDecl->hasPolymorphicEffect(kind))
continue;
conformances.emplace_back(requirement.getFirstType(), protoDecl);
}
return PolymorphicEffectRequirementList(ctx.AllocateCopy(requirements),
ctx.AllocateCopy(conformances));
}
/// Determine whether the given protocol inherits from either
/// AsyncIteratorProtocol or AsyncSequence.
static bool inheritsFromAsyncSequenceProtocol(ProtocolDecl *proto) {
// If it's exactly one of these, shortcut.
if (proto->isSpecificProtocol(KnownProtocolKind::AsyncIteratorProtocol) ||
proto->isSpecificProtocol(KnownProtocolKind::AsyncSequence))
return false;
auto &ctx = proto->getASTContext();
if (auto iter = ctx.getProtocol(KnownProtocolKind::AsyncIteratorProtocol))
if (proto->inheritsFrom(iter))
return true;
if (auto seq = ctx.getProtocol(KnownProtocolKind::AsyncSequence))
if (proto->inheritsFrom(seq))
return true;
return false;
}
PolymorphicEffectKind
PolymorphicEffectKindRequest::evaluate(Evaluator &evaluator,
EffectKind kind,
AbstractFunctionDecl *decl) const {
if (!decl->hasEffect(kind))
return PolymorphicEffectKind::None;
if (!decl->hasPolymorphicEffect(kind)) {
if (auto proto = dyn_cast<ProtocolDecl>(decl->getDeclContext())) {
if (proto->hasPolymorphicEffect(kind))
return PolymorphicEffectKind::ByConformance;
}
return PolymorphicEffectKind::Always;
}
for (auto req : decl->getGenericSignature().getRequirements()) {
if (req.getKind() == RequirementKind::Conformance) {
auto proto = req.getProtocolDecl();
if (proto->hasPolymorphicEffect(kind)) {
// @rethrows protocols that inherit from AsyncIteratorProtocol or
// AsyncSequence should be categorized like AsyncIteratorProtocol or
// AsyncSequence.
if (kind == EffectKind::Throws &&
inheritsFromAsyncSequenceProtocol(proto))
return PolymorphicEffectKind::AsyncSequenceRethrows;
return PolymorphicEffectKind::ByConformance;
}
// Specifically recognize functions that are rethrows and would
// have been ByConformance polymorphic when AsyncIteratorProtocol
// and AsyncSequence were rethrowing protocols.
if (kind == EffectKind::Throws &&
(proto->isSpecificProtocol(
KnownProtocolKind::AsyncIteratorProtocol) ||
proto->isSpecificProtocol(KnownProtocolKind::AsyncSequence))) {
// FIXME: We should diagnose that this function should use typed
// throws instead.
return PolymorphicEffectKind::AsyncSequenceRethrows;
}
}
}
for (auto param : *decl->getParameters()) {
auto interfaceTy = param->getInterfaceType();
if (hasFunctionParameterWithEffect(kind, interfaceTy)) {
return PolymorphicEffectKind::ByClosure;
}
}
return PolymorphicEffectKind::Invalid;
}
static bool classifyWitness(ModuleDecl *module,
ProtocolConformance *conformance,
AbstractFunctionDecl *req,
EffectKind kind) {
auto declRef = conformance->getWitnessDeclRef(req);
if (!declRef) {
// Invalid conformance.
return true;
}
auto witnessDecl = dyn_cast<AbstractFunctionDecl>(declRef.getDecl());
if (!witnessDecl) {
// Enum element constructors do not have effects.
assert(isa<EnumElementDecl>(declRef.getDecl()));
return false;
}
switch (witnessDecl->getPolymorphicEffectKind(kind)) {
case PolymorphicEffectKind::None:
// Witness doesn't have this effect at all, so it contributes nothing.
return false;
case PolymorphicEffectKind::AsyncSequenceRethrows: {
// Witnesses that can only be polymorphic due to an
// AsyncSequence/AsyncIteratorProtocol conformance don't contribute
// anything; the thrown error result is captured by the Failure
// type.
return false;
}
case PolymorphicEffectKind::ByConformance: {
// Witness has the effect if the concrete type's conformances
// recursively have the effect.
auto substitutions = conformance->getSubstitutionMap();
for (auto conformanceRef : substitutions.getConformances()) {
if (conformanceRef.hasEffect(kind)) {
return true;
}
}
return false;
}
case PolymorphicEffectKind::ByClosure:
// Witness only has the effect if a closure argument has the effect,
// so it contributes nothing to the conformance`s effect.
return false;
case PolymorphicEffectKind::Always:
// Witness always has the effect.
// If the witness's thrown type is explicitly specified as a type
// parameter, then check whether the substituted type is `Never`.
if (kind == EffectKind::Throws) {
if (Type thrownError = witnessDecl->getThrownInterfaceType()) {
if (thrownError->hasTypeParameter())
thrownError = thrownError.subst(declRef.getSubstitutions());
if (thrownError->isNever())
return false;
}
}
return true;
case PolymorphicEffectKind::Invalid:
// If something was invalid, just assume it has the effect.
return true;
}
}
bool ConformanceHasEffectRequest::evaluate(
Evaluator &evaluator, EffectKind kind,
ProtocolConformance *conformance) const {
auto *module = conformance->getDeclContext()->getParentModule();
llvm::SmallDenseSet<ProtocolConformance *, 2> visited;
SmallVector<ProtocolConformance *, 2> worklist;
worklist.push_back(conformance);
while (!worklist.empty()) {
auto *current = worklist.back();
worklist.pop_back();
if (!visited.insert(current).second)
continue;
auto protoDecl = current->getProtocol();
auto list = protoDecl->getPolymorphicEffectRequirements(kind);
for (auto req : list.getRequirements()) {
if (classifyWitness(module, current, req, kind))
return true;
}
for (auto pair : list.getConformances()) {
auto assocConf =
current->getAssociatedConformance(
pair.first, pair.second);
if (!assocConf.isConcrete())
return true;
worklist.push_back(assocConf.getConcrete());
}
}
return false;
}
/// \returns the getter decl iff its a prop/subscript with an effectful 'get'
static ConcreteDeclRef getEffectfulGetOnlyAccessor(ConcreteDeclRef cdr) {
if (!cdr)
return nullptr;
if (auto storageDecl = dyn_cast<AbstractStorageDecl>(cdr.getDecl())) {
if (auto getAccessor = storageDecl->getEffectfulGetAccessor())
return ConcreteDeclRef(getAccessor, cdr.getSubstitutions());
}
return nullptr;
}
/// Determine whether this is the "Never" type that's used to indicate that the
/// function never throws.
static bool isNeverThrownError(Type type) {
if (!type)
return true;
return type->isNever();
}
namespace {
/// A function reference.
class AbstractFunction {
public:
enum Kind : uint8_t {
Opaque, Function, Closure, Parameter,
};
private:
union {
AbstractFunctionDecl *TheFunction;
AbstractClosureExpr *TheClosure;
ParamDecl *TheParameter;
Expr *TheExpr;
};
Kind TheKind;
PolymorphicEffectKind RethrowsKind = PolymorphicEffectKind::None;
PolymorphicEffectKind ReasyncKind = PolymorphicEffectKind::None;
SubstitutionMap Substitutions;
public:
explicit AbstractFunction(Kind kind, Expr *fn)
: TheKind(kind) {
TheExpr = fn;
}
explicit AbstractFunction(AbstractFunctionDecl *fn, SubstitutionMap subs)
: TheKind(Kind::Function),
RethrowsKind(fn->getPolymorphicEffectKind(EffectKind::Throws)),
ReasyncKind(fn->getPolymorphicEffectKind(EffectKind::Async)),
Substitutions(subs) {
TheFunction = fn;
}
explicit AbstractFunction(AbstractClosureExpr *closure)
: TheKind(Kind::Closure) {
TheClosure = closure;
}
explicit AbstractFunction(ParamDecl *parameter, SubstitutionMap subs)
: TheKind(Kind::Parameter), Substitutions(subs) {
TheParameter = parameter;
}
Kind getKind() const { return TheKind; }
PolymorphicEffectKind getPolymorphicEffectKind(EffectKind kind) const {
switch (kind) {
case EffectKind::Throws: return RethrowsKind;
case EffectKind::Async: return ReasyncKind;
}
llvm_unreachable("Bad effect kind");
}
Type getType() const {
switch (getKind()) {
case Kind::Opaque: return getOpaqueFunction()->getType();
case Kind::Function: {
auto *AFD = getFunction();
if (AFD->hasImplicitSelfDecl())
return AFD->getMethodInterfaceType();
return AFD->getInterfaceType();
}
case Kind::Closure: return getClosure()->getType();
case Kind::Parameter: return getParameter()->getInterfaceType();
}
llvm_unreachable("bad kind");
}
/// Retrieve the interface type for a parameter based on an index into the
/// substituted parameter type. This
Type getOrigParamInterfaceType(unsigned substIndex) const {
switch (getKind()) {
case Kind::Opaque:
case Kind::Closure:
case Kind::Parameter:
return getType()->castTo<AnyFunctionType>()->getParams()[substIndex]
.getParameterType();
case Kind::Function: {
auto params = getParameterList(static_cast<ValueDecl *>(getFunction()));
auto origIndex = params->getOrigParamIndex(getSubstitutions(), substIndex);
return params->get(origIndex)->getInterfaceType();
}
}
}
bool isAutoClosure() const {
if (getKind() == Kind::Closure)
return isa<AutoClosureExpr>(getClosure());
return false;
}
AbstractFunctionDecl *getFunction() const {
assert(getKind() == Kind::Function);
return TheFunction;
}
AbstractClosureExpr *getClosure() const {
assert(getKind() == Kind::Closure);
return TheClosure;
}
ParamDecl *getParameter() const {
assert(getKind() == Kind::Parameter);
return TheParameter;
}
Expr *getOpaqueFunction() const {
assert(getKind() == Kind::Opaque);
return TheExpr;
}
SubstitutionMap getSubstitutions() const {
return Substitutions;
}
static AbstractFunction getAppliedFn(ApplyExpr *apply) {
Expr *fn = apply->getFn()->getValueProvidingExpr();
if (auto *selfCall = dyn_cast<SelfApplyExpr>(fn))
fn = selfCall->getFn()->getValueProvidingExpr();
return decomposeFunction(fn);
}
bool isPreconcurrency() const {
switch (getKind()) {
case Kind::Closure: {
auto *closure = dyn_cast<ClosureExpr>(getClosure());
return closure && closure->isIsolatedByPreconcurrency();
}
case Kind::Function:
return getActorIsolation(getFunction()).preconcurrency();
case Kind::Opaque:
case Kind::Parameter:
return false;
}
}
static AbstractFunction decomposeFunction(Expr *fn) {
assert(fn->getValueProvidingExpr() == fn);
while (true) {
// Look through Optional unwraps.
if (auto conversion = dyn_cast<ForceValueExpr>(fn)) {
fn = conversion->getSubExpr()->getValueProvidingExpr();
} else if (auto conversion = dyn_cast<BindOptionalExpr>(fn)) {
fn = conversion->getSubExpr()->getValueProvidingExpr();
// Look through optional injections.
} else if (auto injection = dyn_cast<InjectIntoOptionalExpr>(fn)) {
fn = injection->getSubExpr()->getValueProvidingExpr();
// Look through function conversions.
} else if (auto conversion = dyn_cast<FunctionConversionExpr>(fn)) {
fn = conversion->getSubExpr()->getValueProvidingExpr();
// Look through base-ignored qualified references (Module.methodName).
} else if (auto baseIgnored = dyn_cast<DotSyntaxBaseIgnoredExpr>(fn)) {
fn = baseIgnored->getRHS();
// Look through closure capture lists.
} else if (auto captureList = dyn_cast<CaptureListExpr>(fn)) {
fn = captureList->getClosureBody();
// Look through optional evaluations.
} else if (auto optionalEval = dyn_cast<OptionalEvaluationExpr>(fn)) {
fn = optionalEval->getSubExpr()->getValueProvidingExpr();
} else {
break;
}
}
// Constructor delegation.
if (auto otherCtorDeclRef = dyn_cast<OtherConstructorDeclRefExpr>(fn)) {
return AbstractFunction(otherCtorDeclRef->getDecl(),
otherCtorDeclRef->getDeclRef().getSubstitutions());
}
// Normal function references.
if (auto DRE = dyn_cast<DeclRefExpr>(fn)) {
ValueDecl *decl = DRE->getDecl();
if (auto fn = dyn_cast<AbstractFunctionDecl>(decl)) {
return AbstractFunction(fn, DRE->getDeclRef().getSubstitutions());
} else if (auto param = dyn_cast<ParamDecl>(decl)) {
SubstitutionMap subs;
if (auto genericEnv = param->getDeclContext()->getGenericEnvironmentOfContext())
subs = genericEnv->getForwardingSubstitutionMap();
return AbstractFunction(param, subs);
}
// Closures.
} else if (auto closure = dyn_cast<AbstractClosureExpr>(fn)) {
return AbstractFunction(closure);
}
// Everything else is opaque.
return AbstractFunction(Kind::Opaque, fn);
}
};
enum ShouldRecurse_t : bool {
ShouldNotRecurse = false, ShouldRecurse = true
};
/// A CRTP ASTWalker implementation that looks for interesting
/// nodes for effects handling.
template <class Impl>
class EffectsHandlingWalker : public ASTWalker {
Impl &asImpl() { return *static_cast<Impl*>(this); }
public:
LazyInitializerWalking getLazyInitializerWalkingBehavior() override {
return LazyInitializerWalking::InAccessor;
}
/// Only look at the expansions for effects checking.
MacroWalking getMacroWalkingBehavior() const override {
return MacroWalking::Expansion;
}
PreWalkAction walkToDeclPre(Decl *D) override {
ShouldRecurse_t recurse = ShouldRecurse;
// Skip the implementations of all local declarations... except
// PBD. We should really just have a PatternBindingStmt.
if (auto ic = dyn_cast<IfConfigDecl>(D)) {
recurse = asImpl().checkIfConfig(ic);
} else if (auto patternBinding = dyn_cast<PatternBindingDecl>(D)) {
if (patternBinding->isAsyncLet())
recurse = asImpl().checkAsyncLet(patternBinding);
} else if (auto macroExpansionDecl = dyn_cast<MacroExpansionDecl>(D)) {
recurse = ShouldRecurse;
} else {
recurse = ShouldNotRecurse;
}
return Action::VisitNodeIf(bool(recurse));
}
PreWalkResult<Expr *> walkToExprPre(Expr *E) override {
visitExprPre(E);
ShouldRecurse_t recurse = ShouldRecurse;
if (isa<ErrorExpr>(E)) {
asImpl().flagInvalidCode();
} else if (auto closure = dyn_cast<ClosureExpr>(E)) {
recurse = asImpl().checkClosure(closure);
} else if (auto autoclosure = dyn_cast<AutoClosureExpr>(E)) {
recurse = asImpl().checkAutoClosure(autoclosure);
} else if (auto awaitExpr = dyn_cast<AwaitExpr>(E)) {
recurse = asImpl().checkAwait(awaitExpr);
} else if (auto tryExpr = dyn_cast<TryExpr>(E)) {
recurse = asImpl().checkTry(tryExpr);
} else if (auto forceTryExpr = dyn_cast<ForceTryExpr>(E)) {
recurse = asImpl().checkForceTry(forceTryExpr);
} else if (auto optionalTryExpr = dyn_cast<OptionalTryExpr>(E)) {
recurse = asImpl().checkOptionalTry(optionalTryExpr);
} else if (auto apply = dyn_cast<ApplyExpr>(E)) {
recurse = asImpl().checkApply(apply);
} else if (auto lookup = dyn_cast<LookupExpr>(E)) {
recurse = asImpl().checkLookup(lookup);
} else if (auto declRef = dyn_cast<DeclRefExpr>(E)) {
recurse = asImpl().checkDeclRef(declRef);
} else if (auto interpolated = dyn_cast<InterpolatedStringLiteralExpr>(E)) {
recurse = asImpl().checkInterpolatedStringLiteral(interpolated);
} else if (auto macroExpansionExpr = dyn_cast<MacroExpansionExpr>(E)) {
recurse = ShouldRecurse;
} else if (auto *SVE = dyn_cast<SingleValueStmtExpr>(E)) {
recurse = asImpl().checkSingleValueStmtExpr(SVE);
}
// Error handling validation (via checkTopLevelEffects) happens after
// type checking. If an unchecked expression is still around, the code was
// invalid.
#define UNCHECKED_EXPR(KIND, BASE) \
else if (isa<KIND##Expr>(E)) return Action::Stop();
#include "swift/AST/ExprNodes.def"
return Action::VisitNodeIf(bool(recurse), E);
}
PreWalkResult<Stmt *> walkToStmtPre(Stmt *S) override {
ShouldRecurse_t recurse = ShouldRecurse;
if (auto doCatch = dyn_cast<DoCatchStmt>(S)) {
recurse = asImpl().checkDoCatch(doCatch);
} else if (auto thr = dyn_cast<ThrowStmt>(S)) {
recurse = asImpl().checkThrow(thr);
} else if (auto forEach = dyn_cast<ForEachStmt>(S)) {
recurse = asImpl().checkForEach(forEach);
}
if (!recurse)
return Action::SkipNode(S);
return Action::Continue(S);
}
ShouldRecurse_t checkDoCatch(DoCatchStmt *S) {
auto bodyResult = (S->isSyntacticallyExhaustive()
? asImpl().checkExhaustiveDoBody(S)
: asImpl().checkNonExhaustiveDoBody(S));
for (auto clause : S->getCatches()) {
asImpl().checkCatch(clause, bodyResult);
}
return ShouldNotRecurse;
}
ShouldRecurse_t checkForEach(ForEachStmt *S) {
return ShouldRecurse;
}
void visitExprPre(Expr *expr) { asImpl().visitExprPre(expr); }
};
/// A potential reason why something might have an effect.
class PotentialEffectReason {
public:
enum class Kind : uint8_t {
/// The function calls an unconditionally throws/async function.
Apply,
/// The function is rethrows/reasync, and it was passed an explicit
/// argument that was not rethrows/reasync-only in this context.
ByClosure,
/// The function is rethrows/reasync, and it was passed a default
/// argument that was not rethrows/reasync-only in this context.
ByDefaultClosure,
/// The function is rethrows/reasync, and it was called with
/// a throwing conformance as one of its generic arguments.
ByConformance,
/// The initializer of an 'async let' can throw.
AsyncLet,
/// The function accesses an unconditionally throws/async property.
PropertyAccess,
SubscriptAccess
};
static StringRef kindToString(Kind k) {
switch (k) {
case Kind::Apply:
return "Apply";
case Kind::PropertyAccess:
return "PropertyAccess";
case Kind::SubscriptAccess:
return "SubscriptAccess";
case Kind::ByClosure:
return "ByClosure";
case Kind::ByDefaultClosure:
return "ByDefaultClosure";
case Kind::ByConformance:
return "ByConformance";
case Kind::AsyncLet:
return "AsyncLet";
}
}
private:
Expr *TheExpression;
Kind TheKind;
explicit PotentialEffectReason(Kind kind) : TheKind(kind) {}
public:
static PotentialEffectReason forApply() {
return PotentialEffectReason(Kind::Apply);
}
static PotentialEffectReason forPropertyAccess() {
return PotentialEffectReason(Kind::PropertyAccess);
}
static PotentialEffectReason forSubscriptAccess() {
return PotentialEffectReason(Kind::SubscriptAccess);
}
static PotentialEffectReason forClosure(Expr *E) {
PotentialEffectReason result(Kind::ByClosure);
result.TheExpression = E;
return result;
}
static PotentialEffectReason forDefaultClosure() {
return PotentialEffectReason(Kind::ByDefaultClosure);
}
static PotentialEffectReason forConformance() {
return PotentialEffectReason(Kind::ByConformance);
}
static PotentialEffectReason forAsyncLet() {
return PotentialEffectReason(Kind::AsyncLet);
}
Kind getKind() const { return TheKind; }
bool hasPolymorphicEffect() const {
return (getKind() == Kind::ByClosure ||
getKind() == Kind::ByDefaultClosure ||
getKind() == Kind::ByConformance);
}
/// If this was built with forRethrowsArgument, return the expression.
Expr *getArgument() const {
assert(getKind() == Kind::ByClosure);
return TheExpression;
}
};
enum class ConditionalEffectKind {
/// The call/function can't have this effect.
None,
/// The call/function can only have this effect if one of the parameters
/// or conformances in the current context can throw.
Conditional,
/// The call/function can have this effect.
Always,
};
static void simple_display(llvm::raw_ostream &out, ConditionalEffectKind kind) {
out << "ConditionalEffectKind::";
switch(kind) {
case ConditionalEffectKind::None: out << "None"; return;
case ConditionalEffectKind::Conditional: out << "Conditional"; return;
case ConditionalEffectKind::Always: out << "Always"; return;
}
llvm_unreachable("Bad conditional effect kind");
}
/// Remove the type erasure to an existential error, to extract the
/// underlying error.
static Expr *removeErasureToExistentialError(Expr *expr) {
Type type = expr->getType();
if (!type)
return expr;
ASTContext &ctx = type->getASTContext();
if (!ctx.LangOpts.hasFeature(Feature::FullTypedThrows))
return expr;
// Look for an outer erasure expression.
if (auto erasure = dyn_cast<ErasureExpr>(expr)) {
if (type->isEqual(ctx.getErrorExistentialType()))
return erasure->getSubExpr();
}
return expr;
}
}
bool swift::isRethrowLikeTypedThrows(AbstractFunctionDecl *func) {
// This notion is only for compatibility in Swift 5 and is disabled
// when FullTypedThrows is enabled.
ASTContext &ctx = func->getASTContext();
if (ctx.LangOpts.hasFeature(Feature::FullTypedThrows))
return false;
// It must have a thrown error type...
auto thrownError = func->getThrownInterfaceType();
if (!thrownError)
return false;
/// ... that is a generic parameter type (call it E)
auto thrownErrorGP = thrownError->getAs<GenericTypeParamType>();
if (!thrownErrorGP)
return false;
/// ... of the generic function.
auto genericParams = func->getGenericParams();
if (!genericParams ||
thrownErrorGP->getDepth() !=
genericParams->getParams().front()->getDepth())
return false;
// E: Error must be the only conformance requirement on the generic parameter.
auto genericSig = func->getGenericSignature();
if (!genericSig)
return false;
auto requiredProtocols = genericSig->getRequiredProtocols(thrownErrorGP);
if (requiredProtocols.size() != 1 ||
requiredProtocols[0]->getKnownProtocolKind() != KnownProtocolKind::Error)
return false;
// Any parameters that are of throwing function type must also throw 'E'.
for (auto param : *func->getParameters()) {
auto paramTy = param->getInterfaceType();
if (auto paramFuncTy = paramTy->getAs<AnyFunctionType>()) {
if (auto paramThrownErrorTy = paramFuncTy->getEffectiveThrownErrorType())
if (!(*paramThrownErrorTy)->isEqual(thrownError))
return false;
}
}
return true;
}
namespace {
/// Determine whether the given rethrows context is only allowed to be
/// rethrowing because of the historically-rethrowing behavior of
/// AsyncSequence and AsyncIteratorProtocol.
static bool isRethrowingDueToAsyncSequence(DeclContext *rethrowsDC) {
auto rethrowsFunc = dyn_cast<AbstractFunctionDecl>(rethrowsDC);
if (!rethrowsFunc)
return false;
if (rethrowsFunc->getPolymorphicEffectKind(EffectKind::Throws) !=
PolymorphicEffectKind::AsyncSequenceRethrows)
return false;
return true;
}
/// Type-erase the opened archetypes in the given type, if there is one.
static Type typeEraseOpenedArchetypes(Type type) {
if (!type || !type->hasOpenedExistential())
return type;
const OpenedArchetypeType *root = nullptr;
type.visit([&](Type type) {
if (auto opened = dyn_cast<OpenedArchetypeType>(type.getPointer())) {
root = opened->getRoot();
}
});
if (!root)
return type;
return constraints::typeEraseOpenedArchetypesWithRoot(type, root);
}
/// A type expressing the result of classifying whether a call or function
/// throws or is async.
class Classification {
bool IsInvalid = false; // The AST is malformed. Don't diagnose.
bool downgradeToWarning = false;
// Throwing
ConditionalEffectKind ThrowKind = ConditionalEffectKind::None;
std::optional<PotentialEffectReason> ThrowReason;
Type ThrownError;
// Async
ConditionalEffectKind AsyncKind = ConditionalEffectKind::None;
std::optional<PotentialEffectReason> AsyncReason;
void print(raw_ostream &out) const {
out << "{ IsInvalid = " << IsInvalid
<< ", ThrowKind = ";
simple_display(out, ThrowKind);
out << ", ThrowReason = ";
if (!ThrowReason)
out << "nil";
else
out << PotentialEffectReason::kindToString(ThrowReason->getKind());
if (ThrownError)
out << ", ThrownError = " << ThrownError.getString();
out << ", AsyncKind = ";
simple_display(out, AsyncKind);
out << ", AsyncReason = ";
if (!AsyncReason)
out << "nil";
else
out << PotentialEffectReason::kindToString(AsyncReason->getKind());
out << " }";
}
public:
Classification() {}
/// Whether this classification involves any effects.
bool hasAnyEffects() const { return hasAsync() || hasThrows(); }
explicit operator bool() const { return hasAnyEffects(); }
/// Whether there is an async effect.
bool hasAsync() const { return AsyncKind != ConditionalEffectKind::None; }
/// Whether there is a throws effect.
bool hasThrows() const { return ThrowKind != ConditionalEffectKind::None; }
/// Return a classification that only retains the async parts of the
/// given classification.
Classification onlyAsync() const {
Classification result(*this);
result.ThrowKind = ConditionalEffectKind::None;
result.ThrowReason = std::nullopt;
result.ThrownError = Type();
return result;
}
/// Return a classification that only retains the throwing parts of the
/// given classification.
Classification onlyThrowing(std::optional<PotentialEffectReason>
newThrowReason = std::nullopt) const {
Classification result(*this);
result.AsyncKind = ConditionalEffectKind::None;
result.AsyncReason = std::nullopt;
if (result.hasThrows() && newThrowReason)
result.ThrowReason = newThrowReason;
return result;
}
/// Return a classification that promotes a typed throws effect to an
/// untyped throws effect.
Classification promoteToUntypedThrows() const {
if (!hasThrows())
return *this;
Classification result(*this);
result.ThrownError = ThrownError->getASTContext().getErrorExistentialType();
return result;
}
/// Return a classification that only retains the parts of this
/// classification for the requested effect kind.
Classification onlyEffect(EffectKind kind) const {
switch (kind) {
case EffectKind::Async: return onlyAsync();
case EffectKind::Throws: return onlyThrowing();
}
}
static Classification forConditionalPlaceholder(
ASTContext &ctx, EffectKind kind,
PotentialEffectReason reason
) {
switch (kind) {
case EffectKind::Async:
return forAsync(ConditionalEffectKind::Conditional, reason);
case EffectKind::Throws:
return forThrows(
ctx.getErrorExistentialType(), ConditionalEffectKind::Conditional,
reason);
}
}
/// Return a throwing classification.
static Classification forThrows(Type thrownError,
ConditionalEffectKind conditionalKind,
PotentialEffectReason reason) {
Classification result;
if (isNeverThrownError(thrownError))
return result;
assert(!thrownError->hasError());
result.ThrowKind = conditionalKind;
result.ThrowReason = reason;
result.ThrownError = typeEraseOpenedArchetypes(thrownError);
return result;
}
/// Return an async classification.
static Classification forAsync(ConditionalEffectKind conditionalKind,
PotentialEffectReason reason) {
Classification result;
result.AsyncKind = conditionalKind;
result.AsyncReason = reason;
return result;
}
/// Return a classification for a given declaration reference.
static Classification
forDeclRef(ConcreteDeclRef declRef, ConditionalEffectKind conditionalKind,
PotentialEffectReason reason,
std::optional<EffectKind> onlyEffect = std::nullopt) {
Classification result;
bool considerAsync = !onlyEffect || *onlyEffect == EffectKind::Async;
bool considerThrows = !onlyEffect || *onlyEffect == EffectKind::Throws;
// Consider functions based on their specified effects.
if (auto func = dyn_cast<AbstractFunctionDecl>(declRef.getDecl())) {
if (considerAsync && func->hasAsync()) {
result.merge(Classification::forAsync(conditionalKind, reason));
}
if (considerThrows) {
if (auto thrownInterfaceType =
func->getEffectiveThrownErrorType()) {
Type thrownType =
thrownInterfaceType->subst(declRef.getSubstitutions());
result.merge(Classification::forThrows(thrownType,
conditionalKind,
reason));
}
}
return result;
}
// Consider async let declarations.
if (auto var = dyn_cast<VarDecl>(declRef.getDecl())) {
// "Async let" declarations are treated as an asynchronous call
// (to the underlying task's "get"). If the initializer was throwing,
// then the access is also treated as throwing.
if (var->isAsyncLet()) {
// If the initializer could throw, we will have a 'try' in the
// application of its autoclosure.
// FIXME: The type checker should record the thrown error type in
// the AST.
bool throws = false;
if (auto init = var->getParentInitializer()) {
if (auto await = dyn_cast<AwaitExpr>(init))
init = await->getSubExpr();
if (isa<TryExpr>(init))
throws = true;
}
result.merge(Classification::forAsync(
ConditionalEffectKind::Always,
PotentialEffectReason::forAsyncLet()));
if (throws) {
ASTContext &ctx = var->getASTContext();
result.merge(Classification::forThrows(
/*FIXME:*/ctx.getErrorExistentialType(),
ConditionalEffectKind::Always,
PotentialEffectReason::forAsyncLet()));
}
return result;
}
}
return result;
}
/// Used when invalid AST was detected.
static Classification forInvalidCode() {
Classification result;
result.IsInvalid = true;
return result;
}
void merge(Classification other) {
if (other.isInvalid())
IsInvalid = true;
if (other.AsyncKind > AsyncKind) {
AsyncKind = other.AsyncKind;
AsyncReason = other.AsyncReason;
}
if (ThrowKind != ConditionalEffectKind::None ||
other.ThrowKind != ConditionalEffectKind::None) {
ThrownError =
TypeChecker::errorUnion(ThrownError, other.ThrownError, nullptr);
}
if (other.ThrowKind > ThrowKind) {
ThrowKind = other.ThrowKind;
ThrowReason = other.ThrowReason;
}
}
/// Merge implicitly-added effects.
void mergeImplicitEffects(ASTContext &ctx,
bool implicitlyAsync, bool implicitlyThrows,
PotentialEffectReason reason) {
if (implicitlyAsync) {
merge(Classification::forAsync(ConditionalEffectKind::Always, reason));
}
if (implicitlyThrows) {
// FIXME: Implicit throwing could be typed?
merge(Classification::forThrows(ctx.getErrorExistentialType(),
ConditionalEffectKind::Always, reason));
}
}
void limitThrowingKind(ConditionalEffectKind otherKind) {
ThrowKind = std::min(ThrowKind, otherKind);
}
bool isInvalid() const { return IsInvalid; }
void makeInvalid() { IsInvalid = true; }
bool shouldDowngradeToWarning() const {
return downgradeToWarning;
}
void setDowngradeToWarning(bool downgrade) {
downgradeToWarning = downgrade;
}
ConditionalEffectKind getConditionalKind(EffectKind kind) const {
switch (kind) {
case EffectKind::Throws: return ThrowKind;
case EffectKind::Async: return AsyncKind;
}
llvm_unreachable("Bad effect kind");
}
Type getThrownError() const {
assert(ThrowKind == ConditionalEffectKind::Always ||
ThrowKind == ConditionalEffectKind::Conditional);
return ThrownError;
}
PotentialEffectReason getThrowReason() const {
assert(ThrowKind == ConditionalEffectKind::Always ||
ThrowKind == ConditionalEffectKind::Conditional);
return *ThrowReason;
}
PotentialEffectReason getAsyncReason() const {
assert(AsyncKind == ConditionalEffectKind::Always ||
AsyncKind == ConditionalEffectKind::Conditional);
return *AsyncReason;
}
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
LLVM_DUMP_METHOD void dump() const { print(llvm::errs()); }
#endif
};
/// A class for collecting information about rethrowing and reasync functions.
class ApplyClassifier {
/// The key to this cache is a local function decl or closure.
llvm::DenseMap<AnyFunctionRef, Classification> ThrowsCache;
llvm::DenseMap<AnyFunctionRef, Classification> AsyncCache;
/// check the kind of property with an effect to give better diagnostics
static PotentialEffectReason getKindOfEffectfulProp(ConcreteDeclRef cdr) {
if (isa<SubscriptDecl>(cdr.getDecl()))
return PotentialEffectReason::forSubscriptAccess();
assert(isa<VarDecl>(cdr.getDecl()));
return PotentialEffectReason::forPropertyAccess();
}
public:
ASTContext &Ctx;
DeclContext *DC = nullptr;
DeclContext *RethrowsDC = nullptr;
DeclContext *ReasyncDC = nullptr;
ApplyClassifier(ASTContext &ctx) : Ctx(ctx) { }
DeclContext *getPolymorphicEffectDeclContext(EffectKind kind) const {
switch (kind) {
case EffectKind::Throws: return RethrowsDC;
case EffectKind::Async: return ReasyncDC;
}
}
Classification classifyConformance(Type type,
ProtocolConformanceRef conformanceRef,
EffectKind kind) {
if (conformanceRef.isInvalid())
return Classification::forInvalidCode();
auto proto = conformanceRef.getRequirement();
if (kind == EffectKind::Throws &&
(proto->isSpecificProtocol(KnownProtocolKind::AsyncSequence) ||
proto->isSpecificProtocol(
KnownProtocolKind::AsyncIteratorProtocol))) {
auto failureAssocType = proto->getAssociatedType(Ctx.Id_Failure);
if (failureAssocType) {
// Determine whether the async 'for' loop's thrown error is
// conditional.
ConditionalEffectKind conditional;
if (RethrowsDC && isRethrowingDueToAsyncSequence(RethrowsDC))
conditional = ConditionalEffectKind::Conditional;
else
conditional = ConditionalEffectKind::Always;
// Use the Failure type witness, when present.
Type thrownError = conformanceRef.getAssociatedType(
type, failureAssocType->getDeclaredInterfaceType());
return Classification::forThrows(
thrownError, conditional,
/*FIXME*/PotentialEffectReason::forConformance());
}
}
if (conformanceRef.hasEffect(kind)) {
assert(kind == EffectKind::Throws); // there is no async
ASTContext &ctx = conformanceRef.getRequirement()->getASTContext();
// FIXME: typed throws, if it becomes a thing for conformances
return Classification::forThrows(
ctx.getErrorExistentialType(),
ConditionalEffectKind::Conditional,
PotentialEffectReason::forConformance());
}
return Classification();
}
bool isContextPreconcurrency() const {
if (!DC)
return false;
return getActorIsolationOfContext(DC).preconcurrency();
}
/// Whether a missing 'await' error on accessing an async var should be
/// downgraded to a warning.
///
/// Missing 'await' errors are downgraded for synchronous access to isolated
/// global or static 'let' variables, which was previously accepted in
/// compiler versions before 5.10, or for declarations marked preconcurrency.
bool downgradeAsyncAccessToWarning(Decl *decl) {
if (decl->preconcurrency() || isContextPreconcurrency()) {
return true;
}
if (auto *var = dyn_cast<VarDecl>(decl)) {
ActorReferenceResult::Options options = std::nullopt;
ModuleDecl *module;
if (DC != nullptr) {
module = DC->getParentModule();
} else {
module = var->getDeclContext()->getParentModule();
}
if (!isLetAccessibleAnywhere(module, var, options)) {
return options.contains(ActorReferenceResult::Flags::Preconcurrency);
}
}
return false;
}
Classification classifyLookup(LookupExpr *E) {
auto member = E->getMember();
if (!member)
return Classification::forInvalidCode();
PotentialEffectReason reason =
PotentialEffectReason::forPropertyAccess();
Classification classification;
if (auto getter = getEffectfulGetOnlyAccessor(member)) {
reason = getKindOfEffectfulProp(member);
classification = Classification::forDeclRef(
getter, ConditionalEffectKind::Always, reason);
} else if (isa<SubscriptExpr>(E) || isa<DynamicSubscriptExpr>(E)) {
reason = PotentialEffectReason::forSubscriptAccess();
}
classification.setDowngradeToWarning(
downgradeAsyncAccessToWarning(member.getDecl()));
classification.mergeImplicitEffects(
member.getDecl()->getASTContext(),
E->isImplicitlyAsync().has_value(), E->isImplicitlyThrows(),
reason);
return classification;
}
Classification classifyDeclRef(DeclRefExpr *E) {
if (!E->getDecl())
return Classification::forInvalidCode();
Classification classification;
PotentialEffectReason reason = PotentialEffectReason::forPropertyAccess();
ConcreteDeclRef declRef = E->getDeclRef();
if (auto getter = getEffectfulGetOnlyAccessor(declRef)) {
reason = getKindOfEffectfulProp(declRef);
classification = Classification::forDeclRef(
getter, ConditionalEffectKind::Always, reason);
} else if (isa<VarDecl>(declRef.getDecl())) {
// Handle async let.
reason = PotentialEffectReason::forAsyncLet();
classification = Classification::forDeclRef(
declRef, ConditionalEffectKind::Always, reason);
}
classification.setDowngradeToWarning(
downgradeAsyncAccessToWarning(E->getDecl()));
classification.mergeImplicitEffects(
E->getDeclRef().getDecl()->getASTContext(),
E->isImplicitlyAsync().has_value(), E->isImplicitlyThrows(),
reason);
return classification;
}
Classification classifyThrow(ThrowStmt *S) {
Expr *thrownValue = S->getSubExpr();
if (!thrownValue)
return Classification::forInvalidCode();
// If we are doing full typed throws, look through an existential
// conversion to find the underlying type.
thrownValue = removeErasureToExistentialError(thrownValue);
Type thrownType = thrownValue->getType();
if (!thrownType)
return Classification::forInvalidCode();
// FIXME: Add a potential effect reason for a throw site.
return Classification::forThrows(
thrownType, ConditionalEffectKind::Always,
PotentialEffectReason::forApply());
}
/// Check to see if the given function application throws or is async.
Classification classifyApply(ApplyExpr *E) {
// An apply expression is a potential throw site if the function throws.
// But if the expression didn't type-check, suppress diagnostics.
if (!E->getType() || E->getType()->hasError())
return Classification::forInvalidCode();
if (auto *SAE = dyn_cast<SelfApplyExpr>(E)) {
assert(!E->isImplicitlyAsync());
}
auto type = E->getFn()->getType();
if (!type) return Classification::forInvalidCode();
auto fnType = type->getAs<AnyFunctionType>();
if (!fnType) return Classification::forInvalidCode();
auto fnRef = AbstractFunction::getAppliedFn(E);
auto substitutions = fnRef.getSubstitutions();
const bool hasAnyConformances =
llvm::any_of(substitutions.getConformances(),
[](const ProtocolConformanceRef conformance) {
auto *requirement = conformance.getRequirement();
return !requirement->getInvertibleProtocolKind();
});
// If the function doesn't have any effects or conformances, we're done
// here.
if (!fnType->isThrowing() &&
!E->implicitlyThrows() &&
!fnType->isAsync() &&
!E->isImplicitlyAsync() &&
!hasAnyConformances) {
return Classification();
}
ASTContext &ctx = type->getASTContext();
// Decompose the application.
auto *args = E->getArgs();
// If any of the arguments didn't type check, fail.
for (auto arg : *args) {
auto *argExpr = arg.getExpr();
if (!argExpr->getType() || argExpr->getType()->hasError())
return Classification::forInvalidCode();
}
Classification result;
result.mergeImplicitEffects(
ctx, E->isImplicitlyAsync().has_value(), E->implicitlyThrows(),
PotentialEffectReason::forApply());
// Downgrade missing 'await' errors for preconcurrency references.
result.setDowngradeToWarning(
result.hasAsync() &&
(fnRef.isPreconcurrency() || isContextPreconcurrency()));
auto classifyApplyEffect = [&](EffectKind kind) {
if (!fnType->hasEffect(kind) &&
!(kind == EffectKind::Async && E->isImplicitlyAsync()) &&
!(kind == EffectKind::Throws && E->implicitlyThrows())) {
return;
}
// Handle rethrowing and reasync functions.
switch (auto polyKind = fnRef.getPolymorphicEffectKind(kind)) {
case PolymorphicEffectKind::AsyncSequenceRethrows:
case PolymorphicEffectKind::ByConformance: {
auto requirements = substitutions.getGenericSignature()
.withoutMarkerProtocols()
.getRequirements();
for (const auto &req : requirements) {
if (req.getKind() != RequirementKind::Conformance)
continue;
Type type = req.getFirstType().subst(substitutions);
auto conformanceRef = substitutions.lookupConformance(
req.getFirstType()->getCanonicalType(), req.getProtocolDecl());
assert(conformanceRef);
result.merge(classifyConformance(type, conformanceRef, kind));
}
// 'ByConformance' is a superset of 'ByClosure', so check for
// closure arguments too.
LLVM_FALLTHROUGH;
}
case PolymorphicEffectKind::Always:
if (polyKind == PolymorphicEffectKind::ByConformance ||
polyKind == PolymorphicEffectKind::AsyncSequenceRethrows) {
LLVM_FALLTHROUGH;
} else if (RethrowsDC &&
fnRef.getKind() == AbstractFunction::Function &&
isRethrowLikeTypedThrows(fnRef.getFunction())) {
// If we are in a rethrowing context and the function we're referring
// to is a rethrow-like function using typed throws or we are
// calling the next() or next(_:) of an async iterator,
// then look at all of the closure arguments.
LLVM_FALLTHROUGH;
} else {
break;
}
case PolymorphicEffectKind::ByClosure: {
// We need to walk the original parameter types in parallel
// because it only counts for rethrows/reasync purposes if it
// lines up with a throws/async function parameter in the
// original type.
Type fnInterfaceType = fnRef.getType();
if (!fnInterfaceType) {
result.merge(Classification::forInvalidCode());
return;
}
// Use the most significant result from the arguments.
auto *fnSubstType = fnInterfaceType.subst(fnRef.getSubstitutions())
->getAs<AnyFunctionType>();
if (!fnSubstType) {
result.merge(Classification::forInvalidCode());
return;
}
if (fnSubstType->getParams().size() != args->size()) {
result.merge(Classification::forInvalidCode());
return;
}
for (unsigned i = 0, e = args->size(); i < e; ++i) {
Type origParamType = fnRef.getOrigParamInterfaceType(i);
auto argClassification = classifyArgument(
args->getExpr(i), origParamType, fnRef.getSubstitutions(), kind);
// Rethrows is untyped, so adjust the thrown error type.
if (kind == EffectKind::Throws &&
polyKind == PolymorphicEffectKind::ByClosure) {
argClassification = argClassification.promoteToUntypedThrows();
}
result.merge(argClassification);
}
return;
}
case PolymorphicEffectKind::None:
case PolymorphicEffectKind::Invalid:
break;
}
// Try to classify the implementation of functions that we have
// local knowledge of.
//
// An autoclosure callee here only appears in a narrow case where
// we're in the initializer of an 'async let'.
// FIXME: handle typed async let
if (fnRef.isAutoClosure()) {
switch (kind) {
case EffectKind::Async:
result.merge(Classification::forAsync(
ConditionalEffectKind::Always,
PotentialEffectReason::forApply()));
break;
case EffectKind::Throws:
result.merge(Classification::forThrows(
/*FIXME:*/ctx.getErrorExistentialType(),
ConditionalEffectKind::Always,
PotentialEffectReason::forApply()));
}
} else {
result.merge(
classifyFunctionBody(fnRef,
PotentialEffectReason::forApply(),
kind));
}
};
classifyApplyEffect(EffectKind::Throws);
classifyApplyEffect(EffectKind::Async);
return result;
}
/// Classify a single expression without considering its enclosing context.
Classification classifyExpr(Expr *expr, EffectKind kind) {
switch (kind) {
case EffectKind::Throws: {
FunctionThrowsClassifier classifier(*this);
expr->walk(classifier);
return classifier.classification.onlyThrowing();
}
case EffectKind::Async: {
FunctionAsyncClassifier classifier(*this);
expr->walk(classifier);
return Classification::forAsync(
classifier.AsyncKind, /*FIXME:*/PotentialEffectReason::forApply());
}
}
llvm_unreachable("Bad effect");
}
// Classify a single statement without considering its enclosing context.
Classification classifyStmt(Stmt *stmt, EffectKind kind) {
switch (kind) {
case EffectKind::Throws: {
FunctionThrowsClassifier classifier(*this);
stmt->walk(classifier);
return classifier.classification.onlyThrowing();
}
case EffectKind::Async: {
FunctionAsyncClassifier classifier(*this);
stmt->walk(classifier);
return Classification::forAsync(
classifier.AsyncKind, /*FIXME:*/PotentialEffectReason::forApply());
}
}
}
/// Check to see if the given for-each statement to determine if it
/// throws or is async.
Classification classifyForEach(ForEachStmt *stmt) {
// Only async for-each loops have effects.
if (!stmt->getAwaitLoc().isValid())
return Classification();
// For-each loops with effects are always async.
Classification result = Classification::forAsync(
ConditionalEffectKind::Always,
PotentialEffectReason::forApply());
if (!stmt->getNextCall())
return Classification::forInvalidCode();
// Merge the thrown result from the next/nextElement call.
result.merge(classifyExpr(stmt->getNextCall(), EffectKind::Throws));
return result;
}
private:
/// Classify a throwing or async function according to our local
/// knowledge of its implementation.
Classification
classifyFunctionBody(const AbstractFunction &fn,
PotentialEffectReason reason,
EffectKind kind) {
switch (fn.getKind()) {
case AbstractFunction::Opaque: {
return classifyArgumentByType(fn.getType(), fn.getSubstitutions(),
ConditionalEffectKind::Always, reason,
kind);
}
case AbstractFunction::Parameter:
return classifyParameterBody(fn.getParameter(), fn.getSubstitutions(),
reason, kind);
case AbstractFunction::Function:
return classifyFunctionBody(fn.getFunction(), fn.getSubstitutions(),
reason, kind);
case AbstractFunction::Closure:
return classifyFunctionBody(fn.getClosure(), reason, kind);
}
llvm_unreachable("bad abstract function kind");
}
Classification classifyParameterBody(ParamDecl *param,
SubstitutionMap subs,
PotentialEffectReason reason,
EffectKind kind) {
assert(param->getInterfaceType()
->lookThroughAllOptionalTypes()
->castTo<AnyFunctionType>()
->hasEffect(kind) ||
!param->getInterfaceType()
->lookThroughAllOptionalTypes()
->castTo<AnyFunctionType>()
->getIsolation().isNonIsolated());
ConditionalEffectKind conditional;
// If we're currently doing rethrows-checking on the body of the
// function which declares the parameter, it's rethrowing-only.
auto *ParentDC = getPolymorphicEffectDeclContext(kind);
if (ParentDC == param->getDeclContext())
conditional = ConditionalEffectKind::Conditional;
else // otherwise, it throws unconditionally.
conditional = ConditionalEffectKind::Always;
return classifyArgumentByType(
param->getInterfaceType(), subs, conditional, reason, kind);
}
bool isLocallyDefinedInPolymorphicEffectDeclContext(DeclContext *DC,
EffectKind kind) {
auto *ParentDC = getPolymorphicEffectDeclContext(kind);
if (ParentDC == nullptr)
return false;
while (true) {
assert(DC->isLocalContext());
if (DC == ParentDC) return true;
DC = DC->getParent();
if (!DC->isLocalContext()) return false;
}
}
Classification classifyFunctionBody(AbstractFunctionDecl *fn,
SubstitutionMap subs,
PotentialEffectReason reason,
EffectKind kind) {
// Functions can't be rethrowing-only unless they're defined
// within the rethrows context.
if (!isLocallyDefinedInPolymorphicEffectDeclContext(fn, kind) ||
!fn->hasBody()) {
auto conditional = ConditionalEffectKind::Always;
// If we are within a rethrows context prior, treat some typed-throws
// functions and async iterator functions as conditionally throwing.
if (kind == EffectKind::Throws && RethrowsDC &&
(isRethrowLikeTypedThrows(fn) ||
isRethrowingDueToAsyncSequence(RethrowsDC)))
conditional = ConditionalEffectKind::Conditional;
return Classification::forDeclRef(
ConcreteDeclRef(fn, subs), conditional, reason)
.onlyEffect(kind);
}
return classifyFunctionBodyImpl(fn, fn->getBody(), /*allowNone*/ false,
kind, reason);
}
Classification classifyFunctionBody(AbstractClosureExpr *closure,
PotentialEffectReason reason,
EffectKind kind) {
bool isAutoClosure = isa<AutoClosureExpr>(closure);
// Closures can't be rethrowing-only unless they're defined
// within the rethrows context.
if (!isAutoClosure &&
!isLocallyDefinedInPolymorphicEffectDeclContext(closure, kind)) {
switch (kind) {
case EffectKind::Async:
return Classification::forAsync(ConditionalEffectKind::Always, reason);
case EffectKind::Throws:
if (auto thrownType = closure->getEffectiveThrownType())
return Classification::forThrows(*thrownType,
ConditionalEffectKind::Always,
reason);
return Classification();
}
}
BraceStmt *body;
if (auto autoclosure = dyn_cast<AutoClosureExpr>(closure)) {
body = autoclosure->getBody();
} else {
body = cast<ClosureExpr>(closure)->getBody();
}
if (!body) return Classification::forInvalidCode();
return classifyFunctionBodyImpl(closure, body, /*allowNone*/ isAutoClosure,
kind, reason);
}
class FunctionThrowsClassifier
: public EffectsHandlingWalker<FunctionThrowsClassifier> {
ApplyClassifier &Self;
public:
Classification classification;
FunctionThrowsClassifier(ApplyClassifier &self) : Self(self) {}
void flagInvalidCode() {
classification.makeInvalid();
}
ShouldRecurse_t checkClosure(ClosureExpr *closure) {
return ShouldNotRecurse;
}
ShouldRecurse_t checkAutoClosure(AutoClosureExpr *closure) {
return ShouldNotRecurse;
}
ShouldRecurse_t checkAwait(AwaitExpr *E) {
return ShouldRecurse;
}
ShouldRecurse_t checkTry(TryExpr *E) {
return ShouldRecurse;
}
ShouldRecurse_t checkForceTry(ForceTryExpr *E) {
return ShouldNotRecurse;
}
ShouldRecurse_t checkOptionalTry(OptionalTryExpr *E) {
return ShouldNotRecurse;
}
ShouldRecurse_t checkApply(ApplyExpr *E) {
classification.merge(Self.classifyApply(E).onlyThrowing());
return ShouldRecurse;
}
ShouldRecurse_t checkLookup(LookupExpr *E) {
classification.merge(Self.classifyLookup(E).onlyThrowing());
return ShouldRecurse;
}
ShouldRecurse_t checkDeclRef(DeclRefExpr *E) {
classification.merge(Self.classifyDeclRef(E).onlyThrowing());
return ShouldNotRecurse;
}
ShouldRecurse_t checkAsyncLet(PatternBindingDecl *patternBinding) {
return ShouldRecurse;
}
ShouldRecurse_t checkThrow(ThrowStmt *S) {
classification.merge(Self.classifyThrow(S).onlyThrowing());
return ShouldRecurse;
}
ShouldRecurse_t checkInterpolatedStringLiteral(InterpolatedStringLiteralExpr *E) {
return ShouldRecurse;
}
ShouldRecurse_t checkIfConfig(IfConfigDecl *D) {
return ShouldRecurse;
}
ShouldRecurse_t checkForEach(ForEachStmt *S) {
classification.merge(Self.classifyForEach(S));
return ShouldRecurse;
}
ShouldRecurse_t checkSingleValueStmtExpr(SingleValueStmtExpr *SVE) {
return ShouldRecurse;
}
ConditionalEffectKind checkExhaustiveDoBody(DoCatchStmt *S) {
// All errors thrown by the do body are caught, but any errors thrown
// by the catch bodies are bounded by the throwing kind of the do body.
llvm::SaveAndRestore<Classification> savedClassification(
classification, Classification());
S->getBody()->walk(*this);
return classification.getConditionalKind(EffectKind::Throws);
}
ConditionalEffectKind checkNonExhaustiveDoBody(DoCatchStmt *S) {
S->getBody()->walk(*this);
// Because catch bodies can only be executed if the do body throws an
// error, and because the do is non-exhaustive, we can skip checking the
// catch bodies entirely.
return ConditionalEffectKind::None;
}
void checkCatch(CaseStmt *S, ConditionalEffectKind doThrowingKind) {
if (doThrowingKind != ConditionalEffectKind::None) {
// This was an exhaustive do body, so bound our throwing kind by its
// throwing kind.
auto savedClassification = classification;
classification = Classification();
S->getBody()->walk(*this);
classification.limitThrowingKind(doThrowingKind);
classification.merge(savedClassification);
} else {
// We can skip the catch body, since bounding the result by None is
// guaranteed to give back None, which leaves our ThrowKind unchanged.
}
}
void visitExprPre(Expr *expr) { return; }
};
class FunctionAsyncClassifier
: public EffectsHandlingWalker<FunctionAsyncClassifier> {
ApplyClassifier &Self;
public:
bool IsInvalid = false;
ConditionalEffectKind AsyncKind = ConditionalEffectKind::None;
FunctionAsyncClassifier(ApplyClassifier &self) : Self(self) {}
void flagInvalidCode() {
IsInvalid = true;
}
ShouldRecurse_t checkClosure(ClosureExpr *closure) {
return ShouldNotRecurse;
}
ShouldRecurse_t checkAutoClosure(AutoClosureExpr *closure) {
return ShouldNotRecurse;
}
ShouldRecurse_t checkAwait(AwaitExpr *E) {
return ShouldRecurse;
}
ShouldRecurse_t checkTry(TryExpr *E) {
return ShouldRecurse;
}
ShouldRecurse_t checkForceTry(ForceTryExpr *E) {
return ShouldRecurse;
}
ShouldRecurse_t checkOptionalTry(OptionalTryExpr *E) {
return ShouldRecurse;
}
ShouldRecurse_t checkApply(ApplyExpr *E) {
auto classification = Self.classifyApply(E);
IsInvalid |= classification.isInvalid();
AsyncKind = std::max(AsyncKind, classification.getConditionalKind(EffectKind::Async));
return ShouldRecurse;
}
ShouldRecurse_t checkLookup(LookupExpr *E) {
if (E->isImplicitlyAsync()) {
AsyncKind = ConditionalEffectKind::Always;
} else if (auto getter = getEffectfulGetOnlyAccessor(E->getMember())) {
if (cast<AccessorDecl>(getter.getDecl())->hasAsync())
AsyncKind = ConditionalEffectKind::Always;
}
return ShouldRecurse;
}
ShouldRecurse_t checkDeclRef(DeclRefExpr *E) {
if (E->isImplicitlyAsync()) {
AsyncKind = ConditionalEffectKind::Always;
} else if (auto getter = getEffectfulGetOnlyAccessor(E->getDeclRef())) {
if (cast<AccessorDecl>(getter.getDecl())->hasAsync())
AsyncKind = ConditionalEffectKind::Always;
}
return ShouldNotRecurse;
}
ShouldRecurse_t checkAsyncLet(PatternBindingDecl *patternBinding) {
AsyncKind = ConditionalEffectKind::Always;
return ShouldRecurse;
}
ShouldRecurse_t checkThrow(ThrowStmt *E) {
return ShouldRecurse;
}
ShouldRecurse_t checkInterpolatedStringLiteral(InterpolatedStringLiteralExpr *E) {
return ShouldRecurse;
}
ShouldRecurse_t checkIfConfig(IfConfigDecl *D) {
return ShouldRecurse;
}
ShouldRecurse_t checkDoCatch(DoCatchStmt *S) {
return ShouldRecurse;
}
ShouldRecurse_t checkForEach(ForEachStmt *S) {
if (S->getAwaitLoc().isValid()) {
AsyncKind = std::max(AsyncKind, ConditionalEffectKind::Always);
}
return ShouldRecurse;
}
ShouldRecurse_t checkSingleValueStmtExpr(SingleValueStmtExpr *SVE) {
return ShouldRecurse;
}
void visitExprPre(Expr *expr) { return; }
};
Classification
classifyFunctionBodyImpl(AnyFunctionRef key, BraceStmt *body, bool allowNone,
EffectKind kind, PotentialEffectReason reason) {
auto &Cache = (kind == EffectKind::Throws
? ThrowsCache
: AsyncCache);
// Look for the key in the cache.
auto existingIter = Cache.find(key);
if (existingIter != Cache.end())
return existingIter->second;
// For the purposes of finding a fixed point, consider the
// function to be rethrowing-only within its body. Autoclosures
// aren't recursively referenceable, so their special treatment
// isn't a problem for this.
// FIXME: Look up type for typed rethrows?
Cache.insert({key, Classification::forConditionalPlaceholder(
Ctx, kind, reason)});
// Walk the body.
Classification result;
switch (kind) {
case EffectKind::Throws: {
FunctionThrowsClassifier classifier(*this);
body->walk(classifier);
result = classifier.classification.onlyThrowing(reason);
break;
}
case EffectKind::Async: {
FunctionAsyncClassifier classifier(*this);
body->walk(classifier);
if (classifier.IsInvalid)
result = Classification::forInvalidCode();
else {
result = Classification::forAsync(classifier.AsyncKind, reason);
}
break;
}
}
// The body result cannot be 'none' unless it's an autoclosure.
// FIXME: This actually allows us to accept always-throwing closures
// within rethrows functions, which is tracked by SR-680
// (https://bugs.swift.org/browse/SR-680). That can be fixed by adding
// the following condition:
// \code
// result.getConditionalKind(kind) == ConditionalEffectKind::None
// \endcode
if (!result.isInvalid() && !allowNone) {
result = Classification::forConditionalPlaceholder(Ctx, kind, reason);
}
// Remember the result.
Cache[key] = result;
return result;
}
/// Classify an argument being passed to a rethrows/reasync function.
Classification classifyArgument(
Expr *arg, Type paramType, SubstitutionMap subs, EffectKind kind) {
arg = arg->getValueProvidingExpr();
if (auto *defaultArg = dyn_cast<DefaultArgumentExpr>(arg)) {
// Special-case a 'nil' default argument, which is known not to throw.
if (defaultArg->isCallerSide()) {
auto *callerSideArg = defaultArg->getCallerSideDefaultExpr();
if (isa<NilLiteralExpr>(callerSideArg)) {
if (callerSideArg->getType()->getOptionalObjectType())
return Classification();
}
}
return classifyArgumentByType(arg->getType(), subs,
ConditionalEffectKind::Always,
PotentialEffectReason::forDefaultClosure(),
kind);
}
// If this argument is `nil` literal, it doesn't cause the call to throw.
if (isa<NilLiteralExpr>(arg)) {
if (arg->getType()->getOptionalObjectType())
return Classification();
}
// Neither does 'Optional<T>.none'.
if (auto *DSCE = dyn_cast<DotSyntaxCallExpr>(arg)) {
if (auto *DE = dyn_cast<DeclRefExpr>(DSCE->getFn())) {
auto &ctx = paramType->getASTContext();
if (DE->getDecl() == ctx.getOptionalNoneDecl())
return Classification();
}
}
// If the parameter was structurally a tuple, try to look through the
// various tuple operations.
if (auto paramTupleType = dyn_cast<TupleType>(paramType.getPointer())) {
if (auto tuple = dyn_cast<TupleExpr>(arg)) {
return classifyTupleArgument(tuple, paramTupleType, subs, kind);
}
if (paramTupleType->getNumElements() != 1) {
// Otherwise, we're passing an opaque tuple expression, and we
// should treat it as contributing to 'rethrows' if the original
// parameter type included a throwing function type.
return classifyArgumentByType(
paramType,
subs,
ConditionalEffectKind::Always,
PotentialEffectReason::forClosure(arg),
kind);
}
// FIXME: There's a case where we can end up with an ApplyExpr that
// has a single-element-tuple argument type, but the argument is just
// a ClosureExpr and not a TupleExpr.
paramType = paramTupleType->getElementType(0);
}
// Otherwise, if the original parameter type was not a throwing
// function type, it does not contribute to 'rethrows'.
auto paramFnType = paramType->lookThroughAllOptionalTypes()->getAs<AnyFunctionType>();
if (!paramFnType || !paramFnType->hasEffect(kind))
return Classification();
PotentialEffectReason reason = PotentialEffectReason::forClosure(arg);
// TODO: partial applications?
// Decompose the function reference, then consider the type
// of the decomposed function.
AbstractFunction fn = AbstractFunction::decomposeFunction(arg);
// If it doesn't have function type, we must have invalid code.
Type argType = fn.getType();
if (!argType) return Classification::forInvalidCode();
auto argFnType =
argType->lookThroughAllOptionalTypes()->getAs<AnyFunctionType>();
if (!argFnType) return Classification::forInvalidCode();
// If it doesn't throw, this argument does not cause the call to throw.
if (!argFnType->hasEffect(kind))
return Classification();
// Otherwise, classify the function implementation.
return classifyFunctionBody(fn, reason, kind);
}
/// Classify an argument to a rethrows/reasync function that's a tuple literal.
Classification classifyTupleArgument(TupleExpr *tuple,
TupleType *paramTupleType,
SubstitutionMap subs,
EffectKind kind) {
if (paramTupleType->getNumElements() != tuple->getNumElements())
return Classification::forInvalidCode();
Classification result;
for (unsigned i : indices(tuple->getElements())) {
result.merge(classifyArgument(tuple->getElement(i),
paramTupleType->getElementType(i),
subs, kind));
}
return result;
}
/// Given the type of an argument, try to determine if it contains
/// a throws/async function in a way that is permitted to cause a
/// rethrows/reasync function to throw/async.
static Classification
classifyArgumentByType(Type paramType, SubstitutionMap subs,
ConditionalEffectKind conditional,
PotentialEffectReason reason, EffectKind kind) {
if (!paramType || paramType->hasError())
return Classification::forInvalidCode();
// Look through Optional types.
paramType = paramType->lookThroughAllOptionalTypes();
// Consider function types.
if (auto fnType = paramType->getAs<AnyFunctionType>()) {
switch (kind) {
case EffectKind::Async:
if (fnType->isAsync())
return Classification::forAsync(conditional, reason);
return Classification();
case EffectKind::Throws:
if (auto thrownError = fnType->getEffectiveThrownErrorType()) {
Type thrown = *thrownError;
if (subs)
thrown = thrown.subst(subs);
return Classification::forThrows(thrown, conditional, reason);
}
return Classification();
}
}
// Look through tuples.
if (auto tuple = paramType->getAs<TupleType>()) {
Classification result;
for (auto eltType : tuple->getElementTypes()) {
result.merge(
classifyArgumentByType(eltType, subs, conditional, reason, kind));
}
return result;
}
return Classification();
}
};
/// An context in which effects might be handled.
class Context {
public:
enum class Kind : uint8_t {
/// A context that potentially handles errors or async calls.
PotentiallyHandled,
/// A default argument expression.
DefaultArgument,
/// A property wrapper initialization expression.
PropertyWrapper,
/// The initializer for an instance variable.
IVarInitializer,
/// The initializer for a global variable.
GlobalVarInitializer,
/// The initializer for a `lazy` variable.
LazyVarInitializer,
/// The initializer for an enum element.
EnumElementInitializer,
/// The pattern of a catch.
CatchPattern,
/// The guard expression controlling a catch.
CatchGuard,
/// A defer body
DeferBody,
};
private:
static Context getContextForPatternBinding(PatternBindingDecl *pbd) {
auto *var = pbd->getSingleVar();
if (!pbd->isStatic() && pbd->getDeclContext()->isTypeContext()) {
return Context(Kind::IVarInitializer, pbd->getDeclContext());
} else if (var && var->getAttrs().hasAttribute<LazyAttr>()) {
return Context(Kind::LazyVarInitializer, pbd->getDeclContext());
} else {
return Context(Kind::GlobalVarInitializer, pbd->getDeclContext());
}
}
Kind TheKind;
std::optional<AnyFunctionRef> Function;
DeclContext *DC;
bool HandlesErrors = false;
bool HandlesAsync = false;
/// Whether error-handling queries should ignore the function context, e.g.,
/// for autoclosure and rethrows checks.
bool ErrorHandlingIgnoresFunction = false;
bool IsNonExhaustiveCatch = false;
bool DiagnoseErrorOnTry = false;
InterpolatedStringLiteralExpr *InterpolatedString = nullptr;
explicit Context(Kind kind, DeclContext *dc)
: TheKind(kind), Function(std::nullopt), DC(dc), HandlesErrors(false) {
assert(TheKind != Kind::PotentiallyHandled);
}
explicit Context(bool handlesErrors, bool handlesAsync,
std::optional<AnyFunctionRef> function, DeclContext *dc)
: TheKind(Kind::PotentiallyHandled), Function(function), DC(dc),
HandlesErrors(handlesErrors), HandlesAsync(handlesAsync) {}
public:
bool shouldDiagnoseErrorOnTry() const {
return DiagnoseErrorOnTry;
}
void setDiagnoseErrorOnTry(bool b) {
DiagnoseErrorOnTry = b;
}
/// Return true when the current context is under an interpolated string
bool isWithinInterpolatedString() const {
return InterpolatedString != nullptr;
}
/// Stores the location of the innermost await
SourceLoc awaitLoc = SourceLoc();
/// Whether this is a function that rethrows.
bool hasPolymorphicEffect(EffectKind kind) const {
if (!Function)
return false;
auto fn = Function->getAbstractFunctionDecl();
if (!fn)
return false;
switch (kind) {
case EffectKind::Throws:
if (!HandlesErrors)
return false;
if (ErrorHandlingIgnoresFunction)
return false;
break;
case EffectKind::Async:
if (!HandlesAsync)
return false;
break;
}
switch (fn->getPolymorphicEffectKind(kind)) {
case PolymorphicEffectKind::ByClosure:
case PolymorphicEffectKind::ByConformance:
case PolymorphicEffectKind::AsyncSequenceRethrows:
return true;
case PolymorphicEffectKind::None:
case PolymorphicEffectKind::Always:
case PolymorphicEffectKind::Invalid:
return false;
}
llvm_unreachable("Bad polymorphic effect kind");
}
/// Whether this is an autoclosure.
bool isAutoClosure() const {
if (!Function)
return false;
if (ErrorHandlingIgnoresFunction)
return false;
auto closure = Function->getAbstractClosureExpr();
if (!closure)
return false;
return isa<AutoClosureExpr>(closure);
}
static Context forTopLevelCode(TopLevelCodeDecl *D) {
// Top-level code implicitly handles errors.
return Context(/*handlesErrors=*/true,
/*handlesAsync=*/D->isAsyncContext(), std::nullopt, D);
}
static Context forFunction(AbstractFunctionDecl *D) {
// HACK: If the decl is the synthesized getter for a 'lazy' property, then
// treat the context as a property initializer in order to produce a better
// diagnostic; the only code we should be diagnosing on is within the
// initializer expression that has been transplanted from the var's pattern
// binding decl. We don't perform the analysis on the initializer while it's
// still a part of that PBD, as it doesn't get a solution applied there.
if (auto *accessor = dyn_cast<AccessorDecl>(D)) {
if (auto *var = dyn_cast<VarDecl>(accessor->getStorage())) {
if (accessor->isGetter() && var->getAttrs().hasAttribute<LazyAttr>()) {
auto *pbd = var->getParentPatternBinding();
assert(pbd && "lazy var didn't have a pattern binding decl");
return getContextForPatternBinding(pbd);
}
}
}
return Context(D->hasThrows(), D->isAsyncContext(), AnyFunctionRef(D), D);
}
static Context forDeferBody(DeclContext *dc) {
return Context(Kind::DeferBody, dc);
}
static Context forInitializer(Initializer *init) {
if (isa<DefaultArgumentInitializer>(init)) {
return Context(Kind::DefaultArgument, init);
}
if (isa<PropertyWrapperInitializer>(init)) {
return Context(Kind::PropertyWrapper, init);
}
auto *binding = cast<PatternBindingInitializer>(init)->getBinding();
assert(!binding->getDeclContext()->isLocalContext() &&
"setting up error context for local pattern binding?");
return getContextForPatternBinding(binding);
}
static Context forDefaultArgument(DeclContext *dc) {
return Context(Kind::DefaultArgument, dc);
}
static Context forEnumElementInitializer(EnumElementDecl *elt) {
return Context(Kind::EnumElementInitializer, elt);
}
static Context forClosure(AbstractClosureExpr *E) {
// Determine whether the closure has throwing function type.
bool closureTypeThrows = true;
bool closureTypeIsAsync = true;
if (auto closureType = E->getType()) {
if (auto fnType = closureType->getAs<AnyFunctionType>()) {
closureTypeThrows = fnType->isThrowing();
closureTypeIsAsync = fnType->isAsync();
}
}
return Context(closureTypeThrows, closureTypeIsAsync, AnyFunctionRef(E), E);
}
static Context forCatchPattern(CaseStmt *S, DeclContext *dc) {
return Context(Kind::CatchPattern, dc);
}
static Context forCatchGuard(CaseStmt *S, DeclContext *dc) {
return Context(Kind::CatchGuard, dc);
}
static Context forPatternBinding(PatternBindingDecl *binding) {
return getContextForPatternBinding(binding);
}
Context withInterpolatedString(InterpolatedStringLiteralExpr *E) const {
Context copy = *this;
copy.InterpolatedString = E;
return copy;
}
/// Form a subcontext that handles all errors, e.g., for the body of a
/// do-catch with exhaustive catch clauses.
Context withHandlesErrors() const {
Context copy = *this;
copy.HandlesErrors = true;
copy.ErrorHandlingIgnoresFunction = true;
return copy;
}
Kind getKind() const { return TheKind; }
DeclContext *getDeclContext() const { return DC; }
bool handlesThrows(ConditionalEffectKind errorKind) const {
switch (errorKind) {
case ConditionalEffectKind::None:
return true;
// A call that's rethrowing-only can be handled by 'rethrows'.
case ConditionalEffectKind::Conditional:
return HandlesErrors;
// An operation that always throws can only be handled by an
// all-handling context.
case ConditionalEffectKind::Always:
return HandlesErrors && !hasPolymorphicEffect(EffectKind::Throws);
}
llvm_unreachable("bad error kind");
}
bool handlesAsync(ConditionalEffectKind errorKind) const {
switch (errorKind) {
case ConditionalEffectKind::None:
return true;
// A call that's rethrowing-only can be handled by 'rethrows'.
case ConditionalEffectKind::Conditional:
return HandlesAsync;
// An operation that always throws can only be handled by an
// all-handling context.
case ConditionalEffectKind::Always:
return HandlesAsync && !hasPolymorphicEffect(EffectKind::Async);
}
llvm_unreachable("bad error kind");
}
DeclContext *getPolymorphicEffectDeclContext(EffectKind kind) const {
if (!hasPolymorphicEffect(kind))
return nullptr;
return Function->getAbstractFunctionDecl();
}
InterpolatedStringLiteralExpr * getInterpolatedString() const {
return InterpolatedString;
}
void setNonExhaustiveCatch(bool value) {
IsNonExhaustiveCatch = value;
}
static void maybeAddRethrowsNote(DiagnosticEngine &Diags, SourceLoc loc,
const PotentialEffectReason &reason) {
switch (reason.getKind()) {
case PotentialEffectReason::Kind::Apply:
case PotentialEffectReason::Kind::PropertyAccess:
case PotentialEffectReason::Kind::SubscriptAccess:
case PotentialEffectReason::Kind::AsyncLet:
// Already fully diagnosed.
return;
case PotentialEffectReason::Kind::ByClosure:
Diags.diagnose(reason.getArgument()->getLoc(),
diag::because_rethrows_argument_throws);
return;
case PotentialEffectReason::Kind::ByDefaultClosure:
Diags.diagnose(loc, diag::because_rethrows_default_argument_throws);
return;
case PotentialEffectReason::Kind::ByConformance:
Diags.diagnose(loc, diag::because_rethrows_conformance_throws);
return;
}
llvm_unreachable("bad reason kind");
}
/// get a user-friendly name for the source of the effect
static StringRef getEffectSourceName(const PotentialEffectReason &reason) {
switch (reason.getKind()) {
case PotentialEffectReason::Kind::Apply:
case PotentialEffectReason::Kind::ByClosure:
case PotentialEffectReason::Kind::ByDefaultClosure:
case PotentialEffectReason::Kind::ByConformance:
case PotentialEffectReason::Kind::AsyncLet: // FIXME: not really the right name?
return "call";
case PotentialEffectReason::Kind::PropertyAccess:
return "property access";
case PotentialEffectReason::Kind::SubscriptAccess:
return "subscript access";
}
}
void diagnoseUncoveredThrowSite(ASTContext &ctx, ASTNode E,
const Classification &classification) {
auto &Diags = ctx.Diags;
auto message = diag::throwing_call_without_try;
const auto &reason = classification.getThrowReason();
auto reasonKind = reason.getKind();
bool suggestTryFixIt = reasonKind == PotentialEffectReason::Kind::Apply;
if (reasonKind == PotentialEffectReason::Kind::AsyncLet) {
message = diag::throwing_async_let_without_try;
} else if (reasonKind == PotentialEffectReason::Kind::PropertyAccess) {
message = diag::throwing_prop_access_without_try;
suggestTryFixIt = true;
} else if (reasonKind == PotentialEffectReason::Kind::SubscriptAccess) {
message = diag::throwing_subscript_access_without_try;
suggestTryFixIt = true;
}
auto loc = E.getStartLoc();
SourceLoc insertLoc;
SourceRange highlight;
// Generate more specific messages in some cases.
if (auto e = dyn_cast_or_null<ApplyExpr>(E.dyn_cast<Expr*>())) {
if (isa<PrefixUnaryExpr>(e) || isa<PostfixUnaryExpr>(e) ||
isa<BinaryExpr>(e)) {
loc = e->getFn()->getStartLoc();
message = diag::throwing_operator_without_try;
}
insertLoc = loc;
highlight = e->getSourceRange();
if (InterpolatedString &&
e->getCalledValue() &&
e->getCalledValue()->getBaseName() ==
ctx.Id_appendInterpolation) {
message = diag::throwing_interpolation_without_try;
insertLoc = InterpolatedString->getLoc();
}
}
Diags.diagnose(loc, message).highlight(highlight)
.warnUntilSwiftVersionIf(classification.shouldDowngradeToWarning(), 6);
maybeAddRethrowsNote(Diags, loc, reason);
// If this is a call without expected 'try[?|!]', like this:
//
// func foo() throws {}
// [let _ = ]foo()
//
// Let's suggest couple of alternative fix-its
// because complete context is unavailable.
if (!suggestTryFixIt)
return;
// 'try' should go before 'await'
if (awaitLoc.isValid())
insertLoc = awaitLoc;
Diags.diagnose(loc, diag::note_forgot_try)
.fixItInsert(insertLoc, "try ");
Diags.diagnose(loc, diag::note_error_to_optional)
.fixItInsert(insertLoc, "try? ");
Diags.diagnose(loc, diag::note_disable_error_propagation)
.fixItInsert(insertLoc, "try! ");
}
void diagnoseThrowInLegalContext(DiagnosticEngine &Diags, ASTNode node,
bool isTryCovered,
const PotentialEffectReason &reason,
Diag<StringRef> diagForThrowingCall,
Diag<StringRef> diagForTrylessThrowingCall) {
auto loc = node.getStartLoc();
// Allow the diagnostic to fire on the 'try' if we don't have
// anything else to say.
if (isTryCovered &&
!reason.hasPolymorphicEffect() &&
!hasPolymorphicEffect(EffectKind::Throws) &&
!isAutoClosure()) {
DiagnoseErrorOnTry = true;
return;
}
auto effectSource = getEffectSourceName(reason);
if (isTryCovered) {
Diags.diagnose(loc, diagForThrowingCall, effectSource);
} else {
Diags.diagnose(loc, diagForTrylessThrowingCall, effectSource);
}
maybeAddRethrowsNote(Diags, loc, reason);
}
void diagnoseUnhandledThrowSite(DiagnosticEngine &Diags, ASTNode E,
bool isTryCovered,
const PotentialEffectReason &reason) {
switch (getKind()) {
case Kind::PotentiallyHandled:
if (IsNonExhaustiveCatch) {
diagnoseThrowInLegalContext(Diags, E, isTryCovered, reason,
diag::throwing_call_in_nonexhaustive_catch,
diag::tryless_throwing_call_in_nonexhaustive_catch);
return;
}
if (isAutoClosure()) {
diagnoseThrowInLegalContext(Diags, E, isTryCovered, reason,
diag::throwing_call_in_nonthrowing_autoclosure,
diag::tryless_throwing_call_in_nonthrowing_autoclosure);
return;
}
if (hasPolymorphicEffect(EffectKind::Throws)) {
diagnoseThrowInLegalContext(Diags, E, isTryCovered, reason,
diag::throwing_call_in_rethrows_function,
diag::tryless_throwing_call_in_rethrows_function);
return;
}
diagnoseThrowInLegalContext(Diags, E, isTryCovered, reason,
diag::throwing_call_unhandled,
diag::tryless_throwing_call_unhandled);
return;
case Kind::EnumElementInitializer:
case Kind::GlobalVarInitializer:
case Kind::LazyVarInitializer:
case Kind::IVarInitializer:
case Kind::DefaultArgument:
case Kind::PropertyWrapper:
case Kind::CatchPattern:
case Kind::CatchGuard:
case Kind::DeferBody:
Diags.diagnose(E.getStartLoc(), diag::throwing_op_in_illegal_context,
static_cast<unsigned>(getKind()), getEffectSourceName(reason));
return;
}
llvm_unreachable("bad context kind");
}
void diagnoseUnhandledThrowStmt(DiagnosticEngine &Diags, Stmt *S) {
switch (getKind()) {
case Kind::PotentiallyHandled:
if (IsNonExhaustiveCatch) {
Diags.diagnose(S->getStartLoc(), diag::throw_in_nonexhaustive_catch);
return;
}
if (isAutoClosure()) {
Diags.diagnose(S->getStartLoc(), diag::throw_in_nonthrowing_autoclosure);
return;
}
if (hasPolymorphicEffect(EffectKind::Throws)) {
Diags.diagnose(S->getStartLoc(), diag::throw_in_rethrows_function);
return;
}
Diags.diagnose(S->getStartLoc(), diag::throw_in_nonthrowing_function);
return;
case Kind::EnumElementInitializer:
case Kind::GlobalVarInitializer:
case Kind::LazyVarInitializer:
case Kind::IVarInitializer:
case Kind::DefaultArgument:
case Kind::PropertyWrapper:
case Kind::CatchPattern:
case Kind::CatchGuard:
case Kind::DeferBody:
Diags.diagnose(S->getStartLoc(), diag::throw_in_illegal_context,
static_cast<unsigned>(getKind()));
return;
}
llvm_unreachable("bad context kind");
}
void diagnoseUnhandledTry(DiagnosticEngine &Diags, TryExpr *E) {
switch (getKind()) {
case Kind::PotentiallyHandled:
if (DiagnoseErrorOnTry) {
Diags.diagnose(
E->getTryLoc(),
IsNonExhaustiveCatch ? diag::try_unhandled_in_nonexhaustive_catch
: diag::try_unhandled);
}
return;
case Kind::EnumElementInitializer:
case Kind::GlobalVarInitializer:
case Kind::LazyVarInitializer:
case Kind::IVarInitializer:
case Kind::DefaultArgument:
case Kind::PropertyWrapper:
case Kind::CatchPattern:
case Kind::CatchGuard:
case Kind::DeferBody:
assert(!DiagnoseErrorOnTry);
// Diagnosed at the call sites.
return;
}
llvm_unreachable("bad context kind");
}
/// I did not want to add 'await' as a PotentialEffectReason, since it's
/// not actually an effect. So, we have this odd boolean hanging around.
unsigned effectReasonToIndex(std::optional<PotentialEffectReason> maybeReason,
bool forAwait = false) {
// while not actually an effect, in some instances we diagnose the
// appearance of an await within a non-async context.
if (forAwait)
return 2;
if (!maybeReason.has_value())
return 0; // Unspecified
switch(maybeReason.value().getKind()) {
case PotentialEffectReason::Kind::ByClosure:
case PotentialEffectReason::Kind::ByDefaultClosure:
case PotentialEffectReason::Kind::ByConformance:
case PotentialEffectReason::Kind::Apply:
return 1;
case PotentialEffectReason::Kind::AsyncLet:
return 3;
case PotentialEffectReason::Kind::PropertyAccess:
return 4;
case PotentialEffectReason::Kind::SubscriptAccess:
return 5;
}
}
void diagnoseAsyncInIllegalContext(DiagnosticEngine &Diags, ASTNode node) {
if (auto *e = node.dyn_cast<Expr*>()) {
if (isa<ApplyExpr>(e)) {
Diags.diagnose(e->getLoc(), diag::async_call_in_illegal_context,
static_cast<unsigned>(getKind()));
return;
}
if (auto declRef = dyn_cast<DeclRefExpr>(e)) {
if (auto var = dyn_cast<VarDecl>(declRef->getDecl())) {
if (var->isAsyncLet()) {
Diags.diagnose(
e->getLoc(), diag::async_let_in_illegal_context,
var, static_cast<unsigned>(getKind()));
return;
}
}
}
} else if (auto patternBinding = dyn_cast_or_null<PatternBindingDecl>(
node.dyn_cast<Decl *>())) {
if (patternBinding->isAsyncLet()) {
Diags.diagnose(patternBinding->getLoc(),
diag::async_let_binding_illegal_context,
static_cast<unsigned>(getKind()));
return;
}
}
Diags.diagnose(node.getStartLoc(), diag::await_in_illegal_context,
static_cast<unsigned>(getKind()));
}
void maybeAddAsyncNote(DiagnosticEngine &Diags) {
if (!Function)
return;
if (auto func = Function->getAbstractFunctionDecl())
addAsyncNotes(func);
}
/// providing a \c kind helps tailor the emitted message.
void diagnoseUnhandledAsyncSite(DiagnosticEngine &Diags, ASTNode node,
std::optional<PotentialEffectReason> maybeReason,
bool forAwait = false) {
if (node.isImplicit()) {
// The reason we return early on implicit nodes is that sometimes we
// inject implicit closures, e.g. in 'async let' and we'd end up
// "double reporting" some errors, with no great way to make sure the
// "more specific diagnostic" is emitted. So instead, we avoid emitting
// about implicit code.
//
// Some synthesized code, like macros, are NOT marked implicit, so we will
// report about errors in them properly.
return;
}
switch (getKind()) {
case Kind::PotentiallyHandled: {
Diags.diagnose(node.getStartLoc(), diag::async_in_nonasync_function,
effectReasonToIndex(maybeReason, forAwait),
isAutoClosure());
maybeAddAsyncNote(Diags);
return;
}
case Kind::EnumElementInitializer:
case Kind::GlobalVarInitializer:
case Kind::LazyVarInitializer:
case Kind::IVarInitializer:
case Kind::DefaultArgument:
case Kind::PropertyWrapper:
case Kind::CatchPattern:
case Kind::CatchGuard:
case Kind::DeferBody:
diagnoseAsyncInIllegalContext(Diags, node);
return;
}
}
};
/// A class to walk over a local context and validate the correctness
/// of its error coverage.
class CheckEffectsCoverage : public EffectsHandlingWalker<CheckEffectsCoverage> {
friend class EffectsHandlingWalker<CheckEffectsCoverage>;
ASTContext &Ctx;
DeclContext *RethrowsDC = nullptr;
DeclContext *ReasyncDC = nullptr;
Context CurContext;
class ContextFlags {
public:
enum ContextFlag : unsigned {
/// Is the current context considered 'try'-covered?
IsTryCovered = 0x1,
/// Is the current context within a 'try' expression?
IsInTry = 0x2,
/// Is the current context top-level in a debugger function? This
/// causes 'try' suppression to apply recursively within a single
/// level of do/catch.
IsTopLevelDebuggerFunction = 0x4,
/// Do we have any throw site in this context?
HasAnyThrowSite = 0x8,
/// Do we have a throw site using 'try' in this context?
HasTryThrowSite = 0x10,
/// Are we in the context of an 'await'?
IsAsyncCovered = 0x20,
/// Do we have any calls to 'async' functions in this context?
HasAnyAsyncSite = 0x40,
/// Do we have any 'await's in this context?
HasAnyAwait = 0x80,
/// Are we in an 'async let' initializer context?
InAsyncLet = 0x100,
/// Does an enclosing 'if' or 'switch' expr have a 'try'?
StmtExprCoversTry = 0x200,
/// Does an enclosing 'if' or 'switch' expr have an 'await'?
StmtExprCoversAwait = 0x400,
};
private:
unsigned Bits;
public:
ContextFlags() : Bits(0) {}
void reset() { Bits = 0; }
bool has(ContextFlag flag) const { return Bits & flag; }
void set(ContextFlag flag) { Bits |= flag; }
void clear(ContextFlag flag) { Bits &= ~flag; }
void mergeFrom(ContextFlag flag, ContextFlags other) {
Bits |= (other.Bits & flag);
}
void mergeFrom(ContextFlags flags, ContextFlags other) {
Bits |= (other.Bits & flags.Bits);
}
// All of the flags that can be set by throw checking.
static ContextFlags throwFlags() {
ContextFlags result;
result.set(IsTryCovered);
result.set(IsInTry);
result.set(HasAnyThrowSite);
result.set(HasTryThrowSite);
return result;
}
// All of the flags that can be set by async/await checking.
static ContextFlags asyncAwaitFlags() {
ContextFlags result;
result.set(IsAsyncCovered);
result.set(HasAnyAsyncSite);
result.set(HasAnyAwait);
return result;
}
};
ContextFlags Flags;
/// The maximum combined value of all throwing expressions in the current
/// context.
ConditionalEffectKind MaxThrowingKind;
struct DiagnosticInfo {
DiagnosticInfo(Expr &failingExpr,
PotentialEffectReason reason,
bool downgradeToWarning) :
reason(reason),
expr(failingExpr),
downgradeToWarning(downgradeToWarning) {}
/// Reason for throwing
PotentialEffectReason reason;
/// Failing expression
Expr &expr;
/// Whether the error should be downgraded to a warning.
bool downgradeToWarning;
};
SmallVector<Expr *, 4> errorOrder;
llvm::DenseMap<Expr *, std::vector<DiagnosticInfo>> uncoveredAsync;
llvm::DenseMap<Expr *, Expr *> parentMap;
static bool isEffectAnchor(Expr *e) {
return isa<AbstractClosureExpr>(e) || isa<DiscardAssignmentExpr>(e) ||
isa<AssignExpr>(e);
}
static bool isAnchorTooEarly(Expr *e) {
return isa<AssignExpr>(e) || isa<DiscardAssignmentExpr>(e);
}
/// Find the top location where we should put the await
static Expr *walkToAnchor(Expr *e, llvm::DenseMap<Expr *, Expr *> &parentMap,
bool isInterpolatedString) {
Expr *parent = e;
Expr *lastParent = e;
while (parent && !isEffectAnchor(parent)) {
lastParent = parent;
parent = parentMap[parent];
}
if (parent && !isAnchorTooEarly(parent)) {
return parent;
}
if (isInterpolatedString) {
assert(parent == nullptr && "Expected to be at top of expression");
if (ArgumentList *args = lastParent->getArgs()) {
if (Expr *unaryArg = args->getUnlabeledUnaryExpr())
return unaryArg;
}
}
return lastParent;
}
void flagInvalidCode() {
// Suppress warnings about useless try or catch.
Flags.set(ContextFlags::HasAnyThrowSite);
Flags.set(ContextFlags::HasTryThrowSite);
}
/// An RAII object for restoring all the interesting state in an
/// error-coverage.
class ContextScope {
CheckEffectsCoverage &Self;
Context OldContext;
DeclContext *OldRethrowsDC;
DeclContext *OldReasyncDC;
ContextFlags OldFlags;
ConditionalEffectKind OldMaxThrowingKind;
SourceLoc OldAwaitLoc;
public:
ContextScope(CheckEffectsCoverage &self, std::optional<Context> newContext)
: Self(self), OldContext(self.CurContext),
OldRethrowsDC(self.RethrowsDC), OldReasyncDC(self.ReasyncDC),
OldFlags(self.Flags), OldMaxThrowingKind(self.MaxThrowingKind),
OldAwaitLoc(self.CurContext.awaitLoc) {
if (newContext) self.CurContext = *newContext;
}
ContextScope(const ContextScope &) = delete;
ContextScope &operator=(const ContextScope &) = delete;
void enterSubFunction() {
Self.RethrowsDC = nullptr;
Self.ReasyncDC = nullptr;
}
void enterTry() {
Self.Flags.set(ContextFlags::IsInTry);
Self.Flags.set(ContextFlags::IsTryCovered);
Self.Flags.clear(ContextFlags::HasTryThrowSite);
}
void enterAwait(SourceLoc awaitLoc) {
Self.Flags.set(ContextFlags::IsAsyncCovered);
Self.Flags.clear(ContextFlags::HasAnyAsyncSite);
Self.CurContext.awaitLoc = awaitLoc;
}
void enterAsyncLet() {
Self.Flags.set(ContextFlags::InAsyncLet);
}
void refineLocalContext(Context newContext) {
Self.CurContext = newContext;
}
void resetCoverage() {
Self.Flags.reset();
Self.MaxThrowingKind = ConditionalEffectKind::None;
}
void resetCoverageForAutoclosureBody() {
Self.Flags.clear(ContextFlags::IsAsyncCovered);
Self.Flags.clear(ContextFlags::HasAnyAsyncSite);
Self.Flags.clear(ContextFlags::HasAnyAwait);
}
void resetCoverageForDoCatch() {
Self.Flags.reset();
Self.MaxThrowingKind = ConditionalEffectKind::None;
Self.Flags.mergeFrom(ContextFlags::StmtExprCoversTry, OldFlags);
Self.Flags.mergeFrom(ContextFlags::StmtExprCoversAwait, OldFlags);
// Suppress 'try' coverage checking within a single level of
// do/catch in debugger functions.
if (OldFlags.has(ContextFlags::IsTopLevelDebuggerFunction))
Self.Flags.set(ContextFlags::IsTryCovered);
}
void preserveCoverageFromAutoclosureBody() {
// An autoclosure body is the part of the enclosing function
// body for the purposes of deciding whether a try contained
// a throwing call.
OldFlags.mergeFrom(ContextFlags::HasTryThrowSite, Self.Flags);
// "await" doesn't work this way; the "await" needs to be part of
// the autoclosure expression itself, and the autoclosure must be
// 'async'.
}
void setCoverageForSingleValueStmtExpr() {
resetCoverage();
Self.Flags.mergeFrom(ContextFlags::InAsyncLet, OldFlags);
if (OldFlags.has(ContextFlags::IsTryCovered))
Self.Flags.set(ContextFlags::StmtExprCoversTry);
if (OldFlags.has(ContextFlags::IsAsyncCovered))
Self.Flags.set(ContextFlags::StmtExprCoversAwait);
}
void preserveCoverageFromSingleValueStmtExpr() {
// We need to preserve whether we saw any throwing sites, to avoid warning
// on 'do { let x = if .random() { try ... } else { ... } } catch { ... }'
OldFlags.mergeFrom(ContextFlags::HasAnyThrowSite, Self.Flags);
// We need to preserve the throwing kind to correctly handle rethrows.
OldMaxThrowingKind = std::max(OldMaxThrowingKind, Self.MaxThrowingKind);
}
void preserveCoverageFromNonExhaustiveCatch() {
OldFlags.mergeFrom(ContextFlags::HasAnyThrowSite, Self.Flags);
OldMaxThrowingKind = std::max(OldMaxThrowingKind, Self.MaxThrowingKind);
}
void preserveDiagnoseErrorOnTryFlag() {
// The "DiagnoseErrorOnTry" flag is a bit of mutable state
// in the Context itself, used to postpone diagnostic emission
// to a parent "try" expression. If something was diagnosed
// during this ContextScope, the flag may have been set, and
// we need to preserve its value when restoring the old Context.
bool DiagnoseErrorOnTry = Self.CurContext.shouldDiagnoseErrorOnTry();
OldContext.setDiagnoseErrorOnTry(DiagnoseErrorOnTry);
}
void preserveCoverageFromAwaitOperand() {
OldFlags.mergeFrom(ContextFlags::HasAnyAwait, Self.Flags);
OldFlags.mergeFrom(ContextFlags::throwFlags(), Self.Flags);
OldMaxThrowingKind = std::max(OldMaxThrowingKind, Self.MaxThrowingKind);
preserveDiagnoseErrorOnTryFlag();
}
void preserveCoverageFromTryOperand() {
OldFlags.mergeFrom(ContextFlags::HasAnyThrowSite, Self.Flags);
OldFlags.mergeFrom(ContextFlags::asyncAwaitFlags(), Self.Flags);
OldMaxThrowingKind = std::max(OldMaxThrowingKind, Self.MaxThrowingKind);
}
void preserveCoverageFromOptionalOrForcedTryOperand() {
OldFlags.mergeFrom(ContextFlags::asyncAwaitFlags(), Self.Flags);
}
void preserveCoverageFromInterpolatedString() {
OldFlags.mergeFrom(ContextFlags::HasAnyThrowSite, Self.Flags);
OldFlags.mergeFrom(ContextFlags::HasTryThrowSite, Self.Flags);
OldFlags.mergeFrom(ContextFlags::HasAnyAsyncSite, Self.Flags);
OldFlags.mergeFrom(ContextFlags::HasAnyAwait, Self.Flags);
OldMaxThrowingKind = std::max(OldMaxThrowingKind, Self.MaxThrowingKind);
preserveDiagnoseErrorOnTryFlag();
}
bool wasTopLevelDebuggerFunction() const {
return OldFlags.has(ContextFlags::IsTopLevelDebuggerFunction);
}
~ContextScope() {
Self.CurContext = OldContext;
Self.RethrowsDC = OldRethrowsDC;
Self.ReasyncDC = OldReasyncDC;
Self.Flags = OldFlags;
Self.MaxThrowingKind = OldMaxThrowingKind;
Self.CurContext.awaitLoc = OldAwaitLoc;
}
};
/// Retrieve the type of the error that can be caught when an error is
/// thrown from the given location.
Type getCaughtErrorTypeAt(SourceLoc loc) {
auto dc = CurContext.getDeclContext();
auto module = dc->getParentModule();
// Autoclosures can't be found via ASTScope lookup.
if (CurContext.isAutoClosure()) {
auto *closure = dyn_cast<AutoClosureExpr>(CurContext.getDeclContext());
if (auto type = closure->getEffectiveThrownType())
return *type;
// Otherwise, the closure does not throw.
return Ctx.getNeverType();
}
if (CatchNode catchNode = ASTScope::lookupCatchNode(module, loc)) {
if (auto caughtType = catchNode.getThrownErrorTypeInContext(Ctx))
return *caughtType;
// If a catch node returns null for its thrown error type, we're
// in a non-throwing context.
return Ctx.getNeverType();
}
// Fall back to the error existential.
return Ctx.getErrorExistentialType();
}
public:
CheckEffectsCoverage(ASTContext &ctx, Context initialContext)
: Ctx(ctx), CurContext(initialContext),
MaxThrowingKind(ConditionalEffectKind::None) {
if (auto rethrowsDC = initialContext.getPolymorphicEffectDeclContext(
EffectKind::Throws)) {
RethrowsDC = rethrowsDC;
}
if (auto reasyncDC = initialContext.getPolymorphicEffectDeclContext(
EffectKind::Async)) {
ReasyncDC = reasyncDC;
}
}
~CheckEffectsCoverage() {
for (Expr *anchor: errorOrder) {
diagnoseUncoveredAsyncSite(anchor);
}
}
/// Mark that the current context is top-level code with
/// throw-without-try enabled.
void setTopLevelThrowWithoutTry() {
Flags.set(ContextFlags::IsTryCovered);
}
/// Mark that the current context is covered by a 'try', as
/// appropriate for a debugger function.
///
/// Top level code in the debugger is actually implicitly wrapped in
/// a function with a do/catch block.
void setTopLevelDebuggerFunction() {
Flags.set(ContextFlags::IsTryCovered);
Flags.set(ContextFlags::IsTopLevelDebuggerFunction);
}
private:
void visitExprPre(Expr *expr) {
if (parentMap.count(expr) == 0)
parentMap = expr->getParentMap();
return;
}
ShouldRecurse_t checkClosure(ClosureExpr *E) {
ContextScope scope(*this, Context::forClosure(E));
scope.enterSubFunction();
scope.resetCoverage();
E->getBody()->walk(*this);
return ShouldNotRecurse;
}
ShouldRecurse_t checkAutoClosure(AutoClosureExpr *E) {
ContextScope scope(*this, Context::forClosure(E));
scope.enterSubFunction();
bool shouldPreserveCoverage = true;
switch (E->getThunkKind()) {
case AutoClosureExpr::Kind::DoubleCurryThunk:
case AutoClosureExpr::Kind::SingleCurryThunk:
// Curry thunks aren't actually a call to the asynchronous function.
// Assume that async is covered in such contexts.
scope.resetCoverageForAutoclosureBody();
Flags.set(ContextFlags::IsAsyncCovered);
break;
case AutoClosureExpr::Kind::None:
scope.resetCoverageForAutoclosureBody();
break;
case AutoClosureExpr::Kind::AsyncLet:
scope.resetCoverage();
scope.enterAsyncLet();
shouldPreserveCoverage = false;
break;
}
E->getBody()->walk(*this);
if (shouldPreserveCoverage)
scope.preserveCoverageFromAutoclosureBody();
return ShouldNotRecurse;
}
ShouldRecurse_t
checkSingleValueStmtExpr(SingleValueStmtExpr *SVE) {
// For an if/switch expression, we reset coverage such that a 'try'/'await'
// does not cover the branches.
ContextScope scope(*this, /*newContext*/ std::nullopt);
scope.setCoverageForSingleValueStmtExpr();
SVE->getStmt()->walk(*this);
scope.preserveCoverageFromSingleValueStmtExpr();
return ShouldNotRecurse;
}
ConditionalEffectKind checkExhaustiveDoBody(DoCatchStmt *S) {
// This is a context where errors are handled.
ContextScope scope(*this, CurContext.withHandlesErrors());
assert(!Flags.has(ContextFlags::IsInTry) && "do/catch within try?");
scope.resetCoverageForDoCatch();
S->getBody()->walk(*this);
diagnoseNoThrowInDo(S, scope);
return MaxThrowingKind;
}
ConditionalEffectKind checkNonExhaustiveDoBody(DoCatchStmt *S) {
ContextScope scope(*this, std::nullopt);
assert(!Flags.has(ContextFlags::IsInTry) && "do/catch within try?");
scope.resetCoverageForDoCatch();
// If the enclosing context doesn't handle anything, use a
// specialized diagnostic about non-exhaustive catches.
if (!CurContext.handlesThrows(ConditionalEffectKind::Conditional)) {
CurContext.setNonExhaustiveCatch(true);
} else if (Type rethrownErrorType = S->getCaughtErrorType()) {
// We're implicitly rethrowing the error out of this do..catch, so make
// sure that we can throw an error of this type out of this context.
auto catches = S->getCatches();
S->setRethrows(
checkThrownErrorType(catches.back()->getEndLoc(), rethrownErrorType));
}
S->getBody()->walk(*this);
diagnoseNoThrowInDo(S, scope);
scope.preserveCoverageFromNonExhaustiveCatch();
return MaxThrowingKind;
}
void diagnoseNoThrowInDo(DoCatchStmt *S, ContextScope &scope) {
// Warn if nothing threw within the body, unless this is the
// implicit do/catch in a debugger function.
if (!Flags.has(ContextFlags::HasAnyThrowSite) &&
!scope.wasTopLevelDebuggerFunction()) {
Ctx.Diags.diagnose(S->getCatches().front()->getStartLoc(),
diag::no_throw_in_do_with_catch);
}
}
void checkCatch(CaseStmt *S, ConditionalEffectKind doThrowingKind) {
auto dc = CurContext.getDeclContext();
for (auto &LabelItem : S->getMutableCaseLabelItems()) {
// The pattern and guard aren't allowed to throw.
{
ContextScope scope(*this, Context::forCatchPattern(S, dc));
LabelItem.getPattern()->walk(*this);
}
if (auto guard = LabelItem.getGuardExpr()) {
ContextScope scope(*this, Context::forCatchGuard(S, dc));
guard->walk(*this);
}
}
auto savedContext = CurContext;
if (doThrowingKind != ConditionalEffectKind::Always &&
CurContext.hasPolymorphicEffect(EffectKind::Throws)) {
// If this catch clause is reachable at all, it's because a function
// parameter throws. So let's temporarily state that the body is allowed
// to throw.
CurContext = CurContext.withHandlesErrors();
}
// The catch body just happens in the enclosing context.
S->getBody()->walk(*this);
CurContext = savedContext;
}
ShouldRecurse_t checkApply(ApplyExpr *E) {
// An apply expression is a potential throw site if the function throws.
// But if the expression didn't type-check, suppress diagnostics.
auto classification = getApplyClassifier().classifyApply(E);
auto throwDest = checkThrowAsyncSite(
E, /*requiresTry*/ true, classification);
if (!classification.isInvalid()) {
// HACK: functions can get queued multiple times in
// definedFunctions, so be sure to be idempotent.
if (!E->isThrowsSet()) {
E->setThrows(throwDest);
}
auto asyncKind = classification.getConditionalKind(EffectKind::Async);
E->setNoAsync(asyncKind == ConditionalEffectKind::None);
} else {
// HACK: functions can get queued multiple times in
// definedFunctions, so be sure to be idempotent.
if (!E->isThrowsSet()) {
E->setThrows(ThrownErrorDestination());
}
E->setNoAsync(true);
}
// If current apply expression did not type-check, don't attempt
// walking inside of it. This accounts for the fact that we don't
// erase types without type variables to enable better code complication,
// so DeclRefExpr(s) or ApplyExpr with DeclRefExpr as function contained
// inside would have their types preserved, which makes classification
// incorrect.
auto type = E->getType();
return !type || type->hasError() ? ShouldNotRecurse : ShouldRecurse;
}
ApplyClassifier getApplyClassifier() const {
ApplyClassifier classifier(Ctx);
classifier.DC = CurContext.getDeclContext();
classifier.RethrowsDC = RethrowsDC;
classifier.ReasyncDC = ReasyncDC;
return classifier;
}
ShouldRecurse_t checkLookup(LookupExpr *E) {
if (auto classification = getApplyClassifier().classifyLookup(E)) {
auto throwDest = checkThrowAsyncSite(
E, classification.hasThrows(), classification);
E->setThrows(throwDest);
}
return ShouldRecurse;
}
ShouldRecurse_t checkDeclRef(DeclRefExpr *E) {
if (auto classification = getApplyClassifier().classifyDeclRef(E)) {
auto throwDest = checkThrowAsyncSite(
E, classification.hasThrows(), classification);
E->setThrows(throwDest);
}
return ShouldNotRecurse;
}
ShouldRecurse_t checkAsyncLet(PatternBindingDecl *patternBinding) {
// Diagnose async let in a context that doesn't handle async.
if (!CurContext.handlesAsync(ConditionalEffectKind::Always)) {
CurContext.diagnoseUnhandledAsyncSite(Ctx.Diags, patternBinding,
PotentialEffectReason::forAsyncLet());
}
return ShouldRecurse;
}
ShouldRecurse_t
checkInterpolatedStringLiteral(InterpolatedStringLiteralExpr *E) {
ContextScope scope(*this, CurContext.withInterpolatedString(E));
if (E->getAppendingExpr())
E->getAppendingExpr()->walk(*this);
scope.preserveCoverageFromInterpolatedString();
return ShouldNotRecurse;
}
ShouldRecurse_t checkIfConfig(IfConfigDecl *ICD) {
// Check the inactive regions of a #if block to disable warnings that may
// be due to platform specific code.
struct ConservativeThrowChecker : public ASTWalker {
CheckEffectsCoverage &CEC;
ConservativeThrowChecker(CheckEffectsCoverage &CEC) : CEC(CEC) {}
MacroWalking getMacroWalkingBehavior() const override {
return MacroWalking::Arguments;
}
PostWalkResult<Expr *> walkToExprPost(Expr *E) override {
if (isa<TryExpr>(E))
CEC.Flags.set(ContextFlags::HasAnyThrowSite);
return Action::Continue(E);
}
PostWalkResult<Stmt *> walkToStmtPost(Stmt *S) override {
if (isa<ThrowStmt>(S))
CEC.Flags.set(ContextFlags::HasAnyThrowSite);
return Action::Continue(S);
}
};
for (auto &clause : ICD->getClauses()) {
// Active clauses are handled by the normal AST walk.
if (clause.isActive) continue;
for (auto elt : clause.Elements)
elt.walk(ConservativeThrowChecker(*this));
}
return ShouldRecurse;
}
ShouldRecurse_t checkThrow(ThrowStmt *S) {
if (auto classification = getApplyClassifier().classifyThrow(S)) {
Flags.set(ContextFlags::HasAnyThrowSite);
MaxThrowingKind = std::max(MaxThrowingKind, ConditionalEffectKind::Always);
if (!CurContext.handlesThrows(ConditionalEffectKind::Always))
CurContext.diagnoseUnhandledThrowStmt(Ctx.Diags, S);
else {
SourceLoc loc = S->getThrowLoc();
Expr *thrownValue = S->getSubExpr();
Type thrownErrorType = thrownValue->getType();
Type caughtErrorType = getCaughtErrorTypeAt(loc);
if (!caughtErrorType->isEqual(thrownErrorType)) {
thrownValue = removeErasureToExistentialError(thrownValue);
Type thrownErrorType = thrownValue->getType();
if (checkThrownErrorType(loc, thrownErrorType))
S->setSubExpr(thrownValue);
}
}
}
return ShouldRecurse;
}
ThrownErrorDestination
checkThrowAsyncSite(ASTNode E, bool requiresTry,
Classification &classification) {
// Suppress all diagnostics when there's an un-analyzable throw/async site.
if (classification.isInvalid()) {
Flags.set(ContextFlags::HasAnyThrowSite);
Flags.set(ContextFlags::HasAnyAsyncSite);
if (requiresTry) Flags.set(ContextFlags::HasTryThrowSite);
return ThrownErrorDestination();
}
auto asyncKind = classification.getConditionalKind(EffectKind::Async);
auto throwsKind = classification.getConditionalKind(EffectKind::Throws);
// Check async calls.
switch (asyncKind) {
case ConditionalEffectKind::None:
break;
case ConditionalEffectKind::Conditional:
case ConditionalEffectKind::Always:
// Remember that we've seen an async call.
Flags.set(ContextFlags::HasAnyAsyncSite);
// Diagnose async calls in a context that doesn't handle async.
if (!CurContext.handlesAsync(asyncKind)) {
CurContext.diagnoseUnhandledAsyncSite(Ctx.Diags, E,
classification.getAsyncReason());
}
// Diagnose async calls that are outside of an await context.
else if (!(Flags.has(ContextFlags::IsAsyncCovered) ||
Flags.has(ContextFlags::InAsyncLet))) {
Expr *expr = E.dyn_cast<Expr*>();
Expr *anchor = walkToAnchor(expr, parentMap,
CurContext.isWithinInterpolatedString());
if (Flags.has(ContextFlags::StmtExprCoversAwait))
classification.setDowngradeToWarning(true);
if (uncoveredAsync.find(anchor) == uncoveredAsync.end())
errorOrder.push_back(anchor);
uncoveredAsync[anchor].emplace_back(
*expr, classification.getAsyncReason(),
classification.shouldDowngradeToWarning());
}
}
// Check throwing calls.
MaxThrowingKind = std::max(MaxThrowingKind, throwsKind); // FIXME: why is the left Never for property?
switch (throwsKind) {
// Completely ignores sites that don't throw.
case ConditionalEffectKind::None:
break;
// For the purposes of handling and try-coverage diagnostics,
// being rethrowing-only still makes this a throw site.
case ConditionalEffectKind::Conditional:
case ConditionalEffectKind::Always:
Flags.set(ContextFlags::HasAnyThrowSite);
if (requiresTry) Flags.set(ContextFlags::HasTryThrowSite);
// We set the throwing bit of an apply expr after performing this
// analysis, so ensure we don't emit duplicate diagnostics for functions
// that have been queued multiple times.
if (auto expr = E.dyn_cast<Expr*>())
if (auto apply = dyn_cast<ApplyExpr>(expr))
if (apply->isThrowsSet())
break;
bool isTryCovered =
(!requiresTry || Flags.has(ContextFlags::IsTryCovered) ||
Flags.has(ContextFlags::InAsyncLet));
if (!CurContext.handlesThrows(throwsKind)) {
CurContext.diagnoseUnhandledThrowSite(Ctx.Diags, E, isTryCovered,
classification.getThrowReason());
} else if (!isTryCovered) {
if (Flags.has(ContextFlags::StmtExprCoversTry))
classification.setDowngradeToWarning(true);
CurContext.diagnoseUncoveredThrowSite(Ctx, E, // we want this one to trigger
classification);
} else {
return checkThrownErrorType(
E.getStartLoc(), classification.getThrownError());
}
break;
}
return ThrownErrorDestination();
}
/// Check the thrown error type against the type that can be caught or
/// rethrown by the context.
///
/// Returns a thrown error destination, which will be non-throwing if there
/// was an error.
ThrownErrorDestination
checkThrownErrorType(SourceLoc loc, Type thrownErrorType) {
Type caughtErrorType = getCaughtErrorTypeAt(loc);
if (caughtErrorType->isEqual(thrownErrorType))
return ThrownErrorDestination::forMatchingContextType(thrownErrorType);
OpaqueValueExpr *opaque = new (Ctx) OpaqueValueExpr(loc, thrownErrorType);
Expr *rethrowExpr = opaque;
Type resultType = TypeChecker::typeCheckExpression(
rethrowExpr, CurContext.getDeclContext(),
{caughtErrorType, /*FIXME:*/CTP_ThrowStmt});
if (resultType.isNull())
return ThrownErrorDestination();
return ThrownErrorDestination::forConversion(opaque, rethrowExpr);
}
ShouldRecurse_t checkAwait(AwaitExpr *E) {
// Walk the operand.
ContextScope scope(*this, std::nullopt);
scope.enterAwait(E->getAwaitLoc());
E->getSubExpr()->walk(*this);
// Warn about 'await' expressions that weren't actually needed, unless of
// course we're in a context that could never handle an 'async'. Then, we
// produce an error.
if (!Flags.has(ContextFlags::HasAnyAsyncSite)) {
if (CurContext.handlesAsync(ConditionalEffectKind::Conditional)) {
diagnoseRedundantAwait(E);
} else {
CurContext.diagnoseUnhandledAsyncSite(Ctx.Diags, E, std::nullopt,
/*forAwait=*/true);
}
}
// Inform the parent of the walk that an 'await' exists here.
scope.preserveCoverageFromAwaitOperand();
return ShouldNotRecurse;
}
ShouldRecurse_t checkTry(TryExpr *E) {
// Walk the operand.
ContextScope scope(*this, std::nullopt);
scope.enterTry();
E->getSubExpr()->walk(*this);
// Warn about 'try' expressions that weren't actually needed.
if (!Flags.has(ContextFlags::HasTryThrowSite)) {
if (!E->isImplicit())
diagnoseRedundantTry(E);
// Diagnose all the call sites within a single unhandled 'try'
// at the same time.
} else if (!CurContext.handlesThrows(ConditionalEffectKind::Conditional)) {
CurContext.diagnoseUnhandledTry(Ctx.Diags, E);
}
scope.preserveCoverageFromTryOperand();
return ShouldNotRecurse;
}
ShouldRecurse_t checkForceTry(ForceTryExpr *E) {
// Walk the operand. 'try!' handles errors.
ContextScope scope(*this, CurContext.withHandlesErrors());
scope.enterTry();
E->getSubExpr()->walk(*this);
// Warn about 'try' expressions that weren't actually needed.
if (!Flags.has(ContextFlags::HasTryThrowSite))
diagnoseRedundantTry(E);
if (auto thrownError = TypeChecker::canThrow(Ctx, E->getSubExpr())) {
E->setThrownError(*thrownError);
} else {
E->setThrownError(Ctx.getNeverType());
}
scope.preserveCoverageFromOptionalOrForcedTryOperand();
return ShouldNotRecurse;
}
ShouldRecurse_t checkOptionalTry(OptionalTryExpr *E) {
// Walk the operand. 'try?' handles errors.
ContextScope scope(*this, CurContext.withHandlesErrors());
scope.enterTry();
E->getSubExpr()->walk(*this);
// Warn about 'try' expressions that weren't actually needed.
if (!Flags.has(ContextFlags::HasTryThrowSite))
diagnoseRedundantTry(E);
if (auto thrownError = TypeChecker::canThrow(Ctx, E->getSubExpr())) {
E->setThrownError(*thrownError);
} else {
E->setThrownError(Ctx.getNeverType());
}
scope.preserveCoverageFromOptionalOrForcedTryOperand();
return ShouldNotRecurse;
}
ShouldRecurse_t checkForEach(ForEachStmt *S) {
if (!S->getAwaitLoc().isValid())
return ShouldRecurse;
// A 'for await' is always async. There's no effect polymorphism
// via the conformance in a 'reasync' function body.
Flags.set(ContextFlags::HasAnyAsyncSite);
if (!CurContext.handlesAsync(ConditionalEffectKind::Always))
CurContext.diagnoseUnhandledAsyncSite(Ctx.Diags, S, std::nullopt);
// A 'for try await' has a thrown error type that depends on the
// AsyncSequence conformance.
auto classification =
getApplyClassifier().classifyForEach(S).onlyThrowing();
if (classification) {
auto throwsKind = classification.getConditionalKind(EffectKind::Throws);
if (throwsKind != ConditionalEffectKind::None)
Flags.set(ContextFlags::HasAnyThrowSite);
// Note: we don't need to check whether the throw error is handled,
// because we will also be checking the generated next/nextElement
// call.
}
return ShouldRecurse;
}
void diagnoseRedundantTry(AnyTryExpr *E) const {
if (auto *SVE = SingleValueStmtExpr::tryDigOutSingleValueStmtExpr(E)) {
// For an if/switch expression, produce a tailored warning.
Ctx.Diags.diagnose(E->getTryLoc(),
diag::effect_marker_on_single_value_stmt,
"try", SVE->getStmt()->getKind())
.highlight(E->getTryLoc());
return;
}
Ctx.Diags.diagnose(E->getTryLoc(), diag::no_throw_in_try);
}
void diagnoseRedundantAwait(AwaitExpr *E) const {
if (auto *SVE = SingleValueStmtExpr::tryDigOutSingleValueStmtExpr(E)) {
// For an if/switch expression, produce a tailored warning.
Ctx.Diags.diagnose(E->getAwaitLoc(),
diag::effect_marker_on_single_value_stmt,
"await", SVE->getStmt()->getKind())
.highlight(E->getAwaitLoc());
return;
}
Ctx.Diags.diagnose(E->getAwaitLoc(), diag::no_async_in_await);
}
void diagnoseUncoveredAsyncSite(const Expr *anchor) const {
auto asyncPointIter = uncoveredAsync.find(anchor);
if (asyncPointIter == uncoveredAsync.end())
return;
const std::vector<DiagnosticInfo> &errors = asyncPointIter->getSecond();
SourceLoc awaitInsertLoc = anchor->getStartLoc();
if (const AnyTryExpr *tryExpr = dyn_cast<AnyTryExpr>(anchor))
awaitInsertLoc = tryExpr->getSubExpr()->getStartLoc();
else if (const AutoClosureExpr *autoClosure = dyn_cast<AutoClosureExpr>(anchor)) {
if (const AnyTryExpr *tryExpr = dyn_cast<AnyTryExpr>(autoClosure->getSingleExpressionBody()))
awaitInsertLoc = tryExpr->getSubExpr()->getStartLoc();
}
bool downgradeToWarning = llvm::all_of(errors,
[&](DiagnosticInfo diag) -> bool {
return diag.downgradeToWarning;
});
Ctx.Diags.diagnose(anchor->getStartLoc(), diag::async_expr_without_await)
.warnUntilSwiftVersionIf(downgradeToWarning, 6)
.fixItInsert(awaitInsertLoc, "await ")
.highlight(anchor->getSourceRange());
for (const DiagnosticInfo &diag: errors) {
switch (diag.reason.getKind()) {
case PotentialEffectReason::Kind::AsyncLet:
if (auto declR = dyn_cast<DeclRefExpr>(&diag.expr)) {
if (auto var = dyn_cast<VarDecl>(declR->getDecl())) {
if (var->isAsyncLet()) {
Ctx.Diags.diagnose(declR->getLoc(),
diag::async_let_without_await, var);
continue;
}
}
}
LLVM_FALLTHROUGH; // fallthrough to a message about PropertyAccess
case PotentialEffectReason::Kind::PropertyAccess:
Ctx.Diags.diagnose(diag.expr.getStartLoc(),
diag::async_access_without_await, 1);
continue;
case PotentialEffectReason::Kind::SubscriptAccess:
Ctx.Diags.diagnose(diag.expr.getStartLoc(),
diag::async_access_without_await, 2);
continue;
case PotentialEffectReason::Kind::ByClosure:
case PotentialEffectReason::Kind::ByDefaultClosure:
case PotentialEffectReason::Kind::ByConformance:
case PotentialEffectReason::Kind::Apply: {
if (auto autoclosure = dyn_cast<AutoClosureExpr>(anchor)) {
switch(autoclosure->getThunkKind()) {
case AutoClosureExpr::Kind::None:
Ctx.Diags.diagnose(diag.expr.getStartLoc(),
diag::async_call_without_await_in_autoclosure);
break;
case AutoClosureExpr::Kind::AsyncLet:
Ctx.Diags.diagnose(diag.expr.getStartLoc(),
diag::async_call_without_await_in_async_let);
break;
case AutoClosureExpr::Kind::SingleCurryThunk:
case AutoClosureExpr::Kind::DoubleCurryThunk:
Ctx.Diags.diagnose(diag.expr.getStartLoc(),
diag::async_access_without_await, 0);
break;
}
continue;
}
auto *call = dyn_cast<ApplyExpr>(&diag.expr);
if (call && call->isImplicitlyAsync()) {
// Emit a tailored note if the call is implicitly async, meaning the
// callee is isolated to an actor.
auto callee = call->getCalledValue(/*skipFunctionConversions=*/true);
if (callee) {
Ctx.Diags.diagnose(diag.expr.getStartLoc(), diag::actor_isolated_sync_func,
callee);
} else {
Ctx.Diags.diagnose(
diag.expr.getStartLoc(), diag::actor_isolated_sync_func_value,
call->getFn()->getType());
}
} else {
Ctx.Diags.diagnose(diag.expr.getStartLoc(),
diag::async_access_without_await, 0);
}
continue;
}
}
}
}
};
// Find nested functions and perform effects checking on them.
struct LocalFunctionEffectsChecker : ASTWalker {
MacroWalking getMacroWalkingBehavior() const override {
return MacroWalking::Expansion;
}
PreWalkAction walkToDeclPre(Decl *D) override {
if (auto func = dyn_cast<AbstractFunctionDecl>(D)) {
if (func->getDeclContext()->isLocalContext())
TypeChecker::checkFunctionEffects(func);
return Action::SkipNode();
}
return Action::Continue();
}
};
} // end anonymous namespace
void TypeChecker::checkTopLevelEffects(TopLevelCodeDecl *code) {
auto &ctx = code->getDeclContext()->getASTContext();
CheckEffectsCoverage checker(ctx, Context::forTopLevelCode(code));
// In some language modes, we allow top-level code to omit 'try' marking.
if (ctx.LangOpts.EnableThrowWithoutTry)
checker.setTopLevelThrowWithoutTry();
if (auto *body = code->getBody()) {
body->walk(checker);
body->walk(LocalFunctionEffectsChecker());
}
}
void TypeChecker::checkFunctionEffects(AbstractFunctionDecl *fn) {
#ifndef NDEBUG
PrettyStackTraceDecl debugStack("checking effects handling for", fn);
#endif
auto isDeferBody = isa<FuncDecl>(fn) && cast<FuncDecl>(fn)->isDeferBody();
auto context =
isDeferBody ? Context::forDeferBody(fn) : Context::forFunction(fn);
auto &ctx = fn->getASTContext();
CheckEffectsCoverage checker(ctx, context);
// If this is a debugger function, suppress 'try' marking at the top level.
if (fn->getAttrs().hasAttribute<LLDBDebuggerFunctionAttr>())
checker.setTopLevelDebuggerFunction();
if (auto body = fn->getBody()) {
body->walk(checker);
body->walk(LocalFunctionEffectsChecker());
}
if (auto ctor = dyn_cast<ConstructorDecl>(fn))
if (auto superInit = ctor->getSuperInitCall())
superInit->walk(checker);
}
void TypeChecker::checkInitializerEffects(Initializer *initCtx,
Expr *init) {
auto &ctx = initCtx->getASTContext();
CheckEffectsCoverage checker(ctx, Context::forInitializer(initCtx));
init->walk(checker);
init->walk(LocalFunctionEffectsChecker());
}
void TypeChecker::checkCallerSideDefaultArgumentEffects(DeclContext *initCtx,
Expr *init) {
auto &ctx = initCtx->getASTContext();
CheckEffectsCoverage checker(ctx, Context::forDefaultArgument(initCtx));
init->walk(checker);
init->walk(LocalFunctionEffectsChecker());
}
/// Check the correctness of effects within the given enum
/// element's raw value expression.
///
/// The syntactic restrictions on such expressions should make it
/// impossible for errors to ever arise, but checking them anyway (1)
/// ensures correctness if those restrictions are ever loosened,
/// perhaps accidentally, and (2) allows the verifier to assert that
/// all calls have been checked.
void TypeChecker::checkEnumElementEffects(EnumElementDecl *elt, Expr *E) {
auto &ctx = elt->getASTContext();
CheckEffectsCoverage checker(ctx, Context::forEnumElementInitializer(elt));
E->walk(checker);
E->walk(LocalFunctionEffectsChecker());
}
void TypeChecker::checkPropertyWrapperEffects(
PatternBindingDecl *binding, Expr *expr) {
auto &ctx = binding->getASTContext();
CheckEffectsCoverage checker(ctx, Context::forPatternBinding(binding));
expr->walk(checker);
expr->walk(LocalFunctionEffectsChecker());
}
std::optional<Type> TypeChecker::canThrow(ASTContext &ctx, Expr *expr) {
ApplyClassifier classifier(ctx);
auto classification = classifier.classifyExpr(expr, EffectKind::Throws);
if (classification.getConditionalKind(EffectKind::Throws) ==
ConditionalEffectKind::None)
return std::nullopt;
return classification.getThrownError();
}
std::optional<Type> TypeChecker::canThrow(ASTContext &ctx,
ForEachStmt *forEach) {
ApplyClassifier classifier(ctx);
auto classification = classifier.classifyForEach(forEach).onlyThrowing();
if (classification.getConditionalKind(EffectKind::Throws) ==
ConditionalEffectKind::None)
return std::nullopt;
return classification.getThrownError();
}
Type TypeChecker::catchErrorType(DeclContext *dc, DoCatchStmt *stmt) {
ASTContext &ctx = dc->getASTContext();
// If the do..catch statement explicitly specifies that it throws, use
// that type.
if (Type explicitError = stmt->getExplicitCaughtType()) {
return explicitError;
}
// Otherwise, infer the thrown error type from the "do" body.
ApplyClassifier classifier(ctx);
Classification classification = classifier.classifyStmt(
stmt->getBody(), EffectKind::Throws);
// If it doesn't throw at all, the type is Never.
if (!classification.hasThrows()) {
// Source compatibility: if the do..catch was already exhaustive,
// and we aren't doing full typed throws, treat the caught error
// type as 'any Error' to allow pattern-matches to continue to
// type check.
if (!ctx.LangOpts.hasFeature(Feature::FullTypedThrows) &&
stmt->isSyntacticallyExhaustive())
return ctx.getErrorExistentialType();
return ctx.getNeverType();
}
return classification.getThrownError();
}
/// Explode the given type into the set of error unions.
///
/// \returns \c true if any of the types is the error existential type, which
/// means the entire error union is any Error.
static bool expandErrorUnions(Type type,
llvm::function_ref<Type(Type)> simplifyType,
SmallVectorImpl<Type> &terms) {
// If we have a type variable in the type and a type simplification function,
// apply it first.
if (type->hasTypeVariable() && simplifyType)
type = simplifyType(type);
// If we have an error union type, handle it's terms individually.
if (auto errorUnionType = type->getAs<ErrorUnionType>()) {
for (auto term : errorUnionType->getTerms())
if (expandErrorUnions(term, simplifyType, terms))
return true;
return false;
}
// If we have 'any Error', we're done.
if (type->isErrorExistentialType())
return true;
// If we have anything other than 'Never', record it.
if (!isNeverThrownError(type))
terms.push_back(type);
return false;
}
Type TypeChecker::errorUnion(Type type1, Type type2,
llvm::function_ref<Type(Type)> simplifyType) {
// If one type is NULL, return the other.
if (!type1)
return type2;
if (!type2)
return type1;
// Expand the error types we're given.
// - If any term is 'any Error', early return 'any Error'
// - Every 'Never' term is dropped.
SmallVector<Type, 2> terms;
if (expandErrorUnions(type1, simplifyType, terms))
return type1->getASTContext().getErrorExistentialType();
if (expandErrorUnions(type2, simplifyType, terms))
return type1->getASTContext().getErrorExistentialType();
// If we have more than one term, filter out duplicates and look to see if
// we have obviously-different types.
if (terms.size() > 1) {
llvm::SmallDenseMap<CanType, Type> knownTypes;
unsigned distinctConcreteTypes = 0;
auto newEnd = std::remove_if(terms.begin(), terms.end(),
[&](Type type) -> bool {
// If we have already seen this type, remove it from the list of terms.
if (!knownTypes.insert({type->getCanonicalType(), type}).second)
return true;
// We have not seen this type before. If it doesn't involve any
// type variables, note that we've seen another concrete type.
if (!type->hasTypeVariable())
++distinctConcreteTypes;
return false;
});
// If we saw more than one distinct concrete type, return 'any Error'.
if (distinctConcreteTypes > 1)
return type1->getASTContext().getErrorExistentialType();
// Remove any duplicated terms.
terms.erase(newEnd, terms.end());
}
return ErrorUnionType::get(type1->getASTContext(), terms);
}
namespace {
/// Classifies a thrown error kind as Never, a specific type, or 'any Error'.
enum class ThrownErrorClassification {
/// The `Never` type, which represents a non-throwing function.
Never,
/// A specific error type that is neither `Never` nor `any Error`.
Specific,
/// A specific error type that depends on a type variable or type parameter,
/// and therefore we cannot determine whether it is a subtype of another
/// type or not.
Dependent,
/// The type `any Error`, used for untyped throws.
AnyError,
};
}
/// Classify the given thrown error type.
static ThrownErrorClassification classifyThrownErrorType(Type type) {
if (type->isNever())
return ThrownErrorClassification::Never;
if (type->isExistentialType()) {
Type anyError = type->getASTContext().getErrorExistentialType();
if (anyError->isEqual(type))
return ThrownErrorClassification::AnyError;
}
if (type->hasTypeVariable() || type->hasTypeParameter())
return ThrownErrorClassification::Dependent;
return ThrownErrorClassification::Specific;
}
ThrownErrorSubtyping
swift::compareThrownErrorsForSubtyping(
Type subThrownError, Type superThrownError, DeclContext *dc
) {
// Deal with NULL errors. This should only occur when there is no standard
// library.
if (!subThrownError || !superThrownError) {
assert(!dc->getASTContext().getStdlibModule() && "NULL thrown error type");
return ThrownErrorSubtyping::ExactMatch;
}
// Easy case: exact match.
if (superThrownError->isEqual(subThrownError))
return ThrownErrorSubtyping::ExactMatch;
auto superThrownErrorKind = classifyThrownErrorType(superThrownError);
auto subThrownErrorKind = classifyThrownErrorType(subThrownError);
switch (subThrownErrorKind) {
case ThrownErrorClassification::Dependent:
switch (superThrownErrorKind) {
case ThrownErrorClassification::AnyError:
// This is a clear subtype relationship, because the supertype throws
// anything.
return ThrownErrorSubtyping::Subtype;
case ThrownErrorClassification::Never:
case ThrownErrorClassification::Dependent:
case ThrownErrorClassification::Specific:
// We have to compare the types. Do so below.
break;
}
break;
case ThrownErrorClassification::Specific:
switch (superThrownErrorKind) {
case ThrownErrorClassification::AnyError:
// This is a clear subtype relationship, because the supertype throws
// anything.
return ThrownErrorSubtyping::Subtype;
case ThrownErrorClassification::Never:
// The supertype doesn't throw, so this has to drop 'throws' to work.
return ThrownErrorSubtyping::DropsThrows;
case ThrownErrorClassification::Dependent:
case ThrownErrorClassification::Specific:
// We have to compare the types. Do so below.
break;
}
break;
case ThrownErrorClassification::Never:
// A function type throwing 'Never' is a subtype of all function types.
return ThrownErrorSubtyping::Subtype;
case ThrownErrorClassification::AnyError:
switch (superThrownErrorKind) {
case ThrownErrorClassification::Dependent:
case ThrownErrorClassification::Specific:
// We have to compare the types. Do so below.
break;
case ThrownErrorClassification::Never:
// We're going to have to drop the "throws" entirely.
return ThrownErrorSubtyping::DropsThrows;
case ThrownErrorClassification::AnyError:
llvm_unreachable("The thrown error types should have been equal");
}
break;
}
// If either of the types was dependent on a type variable or type parameter,
// we can't do the comparison at all.
if (superThrownErrorKind == ThrownErrorClassification::Dependent ||
subThrownErrorKind == ThrownErrorClassification::Dependent)
return ThrownErrorSubtyping::Dependent;
// Check whether the subtype's thrown error type is convertible to the
// supertype's thrown error type.
if (TypeChecker::isConvertibleTo(subThrownError, superThrownError, dc))
return ThrownErrorSubtyping::Subtype;
// We know it doesn't work.
return ThrownErrorSubtyping::Mismatch;
}
|