1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899 4900 4901 4902 4903 4904 4905 4906 4907 4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933 4934 4935 4936 4937 4938 4939 4940 4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958 4959 4960 4961 4962 4963 4964 4965 4966 4967 4968 4969 4970 4971 4972 4973 4974 4975 4976 4977 4978 4979 4980 4981 4982 4983 4984 4985 4986 4987 4988 4989 4990 4991 4992 4993 4994 4995 4996 4997 4998 4999 5000 5001 5002 5003 5004 5005 5006 5007 5008 5009 5010 5011 5012 5013 5014 5015 5016 5017 5018 5019 5020 5021 5022 5023 5024 5025 5026 5027 5028 5029 5030 5031 5032 5033 5034 5035 5036 5037 5038 5039 5040 5041 5042 5043 5044 5045 5046 5047 5048 5049 5050 5051 5052 5053 5054 5055 5056 5057 5058 5059 5060 5061 5062 5063 5064 5065 5066 5067 5068 5069 5070 5071 5072 5073 5074 5075 5076 5077 5078 5079 5080 5081 5082 5083 5084 5085 5086 5087 5088 5089 5090 5091 5092 5093 5094 5095 5096 5097 5098 5099 5100 5101 5102 5103 5104 5105 5106 5107 5108 5109 5110 5111 5112 5113 5114 5115 5116 5117 5118 5119 5120 5121 5122 5123 5124 5125 5126 5127 5128 5129 5130 5131 5132 5133 5134 5135 5136 5137 5138 5139 5140 5141 5142 5143 5144 5145 5146 5147 5148 5149 5150 5151 5152 5153 5154 5155 5156 5157 5158 5159 5160 5161 5162 5163 5164 5165 5166 5167 5168 5169 5170 5171 5172 5173 5174 5175 5176 5177 5178 5179 5180 5181 5182 5183 5184 5185 5186 5187 5188 5189 5190 5191 5192 5193 5194 5195 5196 5197 5198 5199 5200 5201 5202 5203 5204 5205 5206 5207 5208 5209 5210 5211 5212 5213 5214 5215 5216 5217 5218 5219 5220 5221 5222 5223 5224 5225 5226 5227 5228 5229 5230 5231 5232 5233 5234 5235 5236 5237 5238 5239 5240 5241 5242 5243 5244 5245 5246 5247 5248 5249 5250 5251 5252 5253 5254 5255 5256 5257 5258 5259 5260 5261 5262 5263 5264 5265 5266 5267 5268 5269 5270 5271 5272 5273 5274 5275 5276 5277 5278 5279 5280 5281 5282 5283 5284 5285 5286 5287 5288 5289 5290 5291 5292 5293 5294 5295 5296 5297 5298 5299 5300 5301 5302 5303 5304 5305 5306 5307 5308 5309 5310 5311 5312 5313 5314 5315 5316 5317 5318 5319 5320 5321 5322 5323 5324 5325 5326 5327 5328 5329 5330 5331 5332 5333 5334 5335 5336 5337 5338 5339 5340 5341 5342 5343 5344 5345 5346 5347 5348 5349 5350 5351 5352 5353 5354 5355 5356 5357 5358 5359 5360 5361 5362 5363
|
//===--- ConstraintSystem.cpp - Constraint-based Type Checking ------------===//
//
// 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 the constraint-based type checker, anchored by the
// \c ConstraintSystem class, which provides type checking and type
// inference for expressions.
//
//===----------------------------------------------------------------------===//
#include "swift/Sema/ConstraintSystem.h"
#include "CSDiagnostics.h"
#include "OpenedExistentials.h"
#include "TypeCheckAvailability.h"
#include "TypeCheckConcurrency.h"
#include "TypeCheckMacros.h"
#include "TypeCheckType.h"
#include "TypeChecker.h"
#include "swift/AST/ConformanceLookup.h"
#include "swift/AST/ExistentialLayout.h"
#include "swift/AST/GenericEnvironment.h"
#include "swift/AST/Initializer.h"
#include "swift/AST/MacroDefinition.h"
#include "swift/AST/ParameterList.h"
#include "swift/AST/ProtocolConformance.h"
#include "swift/AST/TypeCheckRequests.h"
#include "swift/AST/TypeTransform.h"
#include "swift/Basic/Assertions.h"
#include "swift/Basic/Defer.h"
#include "swift/Basic/Statistic.h"
#include "swift/Sema/CSFix.h"
#include "swift/Sema/ConstraintGraph.h"
#include "swift/Sema/IDETypeChecking.h"
#include "swift/Sema/SolutionResult.h"
#include "llvm/ADT/SetVector.h"
#include "llvm/ADT/SmallSet.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/Support/Compiler.h"
#include "llvm/Support/Format.h"
#include <cmath>
using namespace swift;
using namespace constraints;
using namespace inference;
#define DEBUG_TYPE "ConstraintSystem"
ExpressionTimer::ExpressionTimer(AnchorType Anchor, ConstraintSystem &CS)
: ExpressionTimer(
Anchor, CS,
CS.getASTContext().TypeCheckerOpts.ExpressionTimeoutThreshold) {}
ExpressionTimer::ExpressionTimer(AnchorType Anchor, ConstraintSystem &CS,
unsigned thresholdInSecs)
: Anchor(Anchor), Context(CS.getASTContext()),
StartTime(llvm::TimeRecord::getCurrentTime()),
ThresholdInSecs(thresholdInSecs),
PrintDebugTiming(CS.getASTContext().TypeCheckerOpts.DebugTimeExpressions),
PrintWarning(true) {}
SourceRange ExpressionTimer::getAffectedRange() const {
ASTNode anchor;
if (auto *locator = Anchor.dyn_cast<ConstraintLocator *>()) {
anchor = simplifyLocatorToAnchor(locator);
// If locator couldn't be simplified down to a single AST
// element, let's use its root.
if (!anchor)
anchor = locator->getAnchor();
} else {
anchor = Anchor.get<Expr *>();
}
return anchor.getSourceRange();
}
ExpressionTimer::~ExpressionTimer() {
auto elapsed = getElapsedProcessTimeInFractionalSeconds();
unsigned elapsedMS = static_cast<unsigned>(elapsed * 1000);
if (PrintDebugTiming) {
// Round up to the nearest 100th of a millisecond.
llvm::errs() << llvm::format("%0.2f", std::ceil(elapsed * 100000) / 100)
<< "ms\t";
if (auto *E = Anchor.dyn_cast<Expr *>()) {
E->getLoc().print(llvm::errs(), Context.SourceMgr);
} else {
auto *locator = Anchor.get<ConstraintLocator *>();
locator->dump(&Context.SourceMgr, llvm::errs());
}
llvm::errs() << "\n";
}
if (!PrintWarning)
return;
const auto WarnLimit = getWarnLimit();
if (WarnLimit == 0 || elapsedMS < WarnLimit)
return;
auto sourceRange = getAffectedRange();
if (sourceRange.Start.isValid()) {
Context.Diags
.diagnose(sourceRange.Start, diag::debug_long_expression, elapsedMS,
WarnLimit)
.highlight(sourceRange);
}
}
ConstraintSystem::ConstraintSystem(DeclContext *dc,
ConstraintSystemOptions options,
DiagnosticTransaction *diagnosticTransaction)
: Context(dc->getASTContext()), DC(dc), Options(options),
diagnosticTransaction(diagnosticTransaction),
Arena(dc->getASTContext(), Allocator),
CG(*new ConstraintGraph(*this))
{
assert(DC && "context required");
// Respect the global debugging flag, but turn off debugging while
// parsing and loading other modules.
if (Context.TypeCheckerOpts.DebugConstraintSolver &&
DC->getParentModule()->isMainModule()) {
Options |= ConstraintSystemFlags::DebugConstraints;
}
if (Context.LangOpts.UseClangFunctionTypes)
Options |= ConstraintSystemFlags::UseClangFunctionTypes;
}
ConstraintSystem::~ConstraintSystem() {
delete &CG;
}
void ConstraintSystem::incrementScopeCounter() {
++CountScopes;
// FIXME: (transitional) increment the redundant "always-on" counter.
if (auto *Stats = getASTContext().Stats)
++Stats->getFrontendCounters().NumConstraintScopes;
}
void ConstraintSystem::incrementLeafScopes() {
if (auto *Stats = getASTContext().Stats)
++Stats->getFrontendCounters().NumLeafScopes;
}
bool ConstraintSystem::hasFreeTypeVariables() {
// Look for any free type variables.
return llvm::any_of(TypeVariables, [](const TypeVariableType *typeVar) {
return !typeVar->getImpl().hasRepresentativeOrFixed();
});
}
void ConstraintSystem::addTypeVariable(TypeVariableType *typeVar) {
TypeVariables.insert(typeVar);
// Notify the constraint graph.
(void)CG[typeVar];
}
void ConstraintSystem::mergeEquivalenceClasses(TypeVariableType *typeVar1,
TypeVariableType *typeVar2,
bool updateWorkList) {
assert(typeVar1 == getRepresentative(typeVar1) &&
"typeVar1 is not the representative");
assert(typeVar2 == getRepresentative(typeVar2) &&
"typeVar2 is not the representative");
assert(typeVar1 != typeVar2 && "cannot merge type with itself");
typeVar1->getImpl().mergeEquivalenceClasses(typeVar2, getTrail());
// Merge nodes in the constraint graph.
CG.mergeNodes(typeVar1, typeVar2);
if (updateWorkList) {
addTypeVariableConstraintsToWorkList(typeVar1);
}
}
/// Determine whether the given type variables occurs in the given type.
bool ConstraintSystem::typeVarOccursInType(TypeVariableType *typeVar,
Type type,
bool *involvesOtherTypeVariables) {
SmallPtrSet<TypeVariableType *, 4> typeVars;
type->getTypeVariables(typeVars);
bool occurs = typeVars.count(typeVar);
if (involvesOtherTypeVariables) {
*involvesOtherTypeVariables =
occurs ? typeVars.size() > 1 : !typeVars.empty();
}
return occurs;
}
void ConstraintSystem::assignFixedType(TypeVariableType *typeVar, Type type,
bool updateState,
bool notifyBindingInference) {
assert(!type->hasError() &&
"Should not be assigning a type involving ErrorType!");
typeVar->getImpl().assignFixedType(type, getTrail());
if (!updateState)
return;
if (!type->isTypeVariableOrMember()) {
// If this type variable represents a literal, check whether we picked the
// default literal type. First, find the corresponding protocol.
//
// If we have the constraint graph, we can check all type variables in
// the equivalence class. This is the More Correct path.
// FIXME: Eliminate the less-correct path.
auto typeVarRep = getRepresentative(typeVar);
for (auto *tv : CG[typeVarRep].getEquivalenceClass()) {
auto locator = tv->getImpl().getLocator();
if (!(locator && (locator->directlyAt<CollectionExpr>() ||
locator->directlyAt<LiteralExpr>())))
continue;
auto *literalProtocol = TypeChecker::getLiteralProtocol(
getASTContext(), castToExpr(locator->getAnchor()));
if (!literalProtocol)
continue;
// If the protocol has a default type, check it.
if (auto defaultType = TypeChecker::getDefaultType(literalProtocol, DC)) {
// Check whether the nominal types match. This makes sure that we
// properly handle Array vs. Array<T>.
if (defaultType->getAnyNominal() != type->getAnyNominal()) {
increaseScore(SK_NonDefaultLiteral, locator);
}
}
break;
}
}
// Notify the constraint graph.
CG.bindTypeVariable(typeVar, type);
addTypeVariableConstraintsToWorkList(typeVar);
if (notifyBindingInference)
CG.introduceToInference(typeVar, type);
}
void ConstraintSystem::addTypeVariableConstraintsToWorkList(
TypeVariableType *typeVar) {
// Activate the constraints affected by a change to this type variable.
auto gatheringKind = ConstraintGraph::GatheringKind::AllMentions;
for (auto *constraint : CG.gatherConstraints(typeVar, gatheringKind))
if (!constraint->isActive())
activateConstraint(constraint);
}
void ConstraintSystem::addConversionRestriction(
Type srcType, Type dstType,
ConversionRestrictionKind restriction) {
auto key = std::make_pair(srcType.getPointer(), dstType.getPointer());
bool inserted = ConstraintRestrictions.insert(
std::make_pair(key, restriction)).second;
if (!inserted)
return;
if (solverState) {
recordChange(SolverTrail::Change::AddedConversionRestriction(
srcType, dstType));
}
}
void ConstraintSystem::removeConversionRestriction(
Type srcType, Type dstType) {
auto key = std::make_pair(srcType.getPointer(), dstType.getPointer());
bool erased = ConstraintRestrictions.erase(key);
ASSERT(erased);
}
void ConstraintSystem::addFix(ConstraintFix *fix) {
bool inserted = Fixes.insert(fix);
ASSERT(inserted);
if (solverState)
recordChange(SolverTrail::Change::AddedFix(fix));
}
void ConstraintSystem::removeFix(ConstraintFix *fix) {
ASSERT(Fixes.back() == fix);
Fixes.pop_back();
}
void ConstraintSystem::recordDisjunctionChoice(
ConstraintLocator *locator, unsigned index) {
bool inserted = DisjunctionChoices.insert({locator, index}).second;
ASSERT(inserted);
if (solverState)
recordChange(SolverTrail::Change::RecordedDisjunctionChoice(locator));
}
void ConstraintSystem::recordAppliedDisjunction(
ConstraintLocator *locator, FunctionType *fnType) {
// We shouldn't ever register disjunction choices multiple times.
bool inserted = AppliedDisjunctions.insert(
std::make_pair(locator, fnType)).second;
ASSERT(inserted);
if (solverState)
recordChange(SolverTrail::Change::RecordedAppliedDisjunction(locator));
}
/// Retrieve a dynamic result signature for the given declaration.
static std::tuple<char, ObjCSelector, CanType>
getDynamicResultSignature(ValueDecl *decl) {
if (auto func = dyn_cast<AbstractFunctionDecl>(decl)) {
// Handle functions.
auto type = func->getMethodInterfaceType();
return std::make_tuple(func->isStatic(), func->getObjCSelector(),
type->getCanonicalType());
}
if (auto asd = dyn_cast<AbstractStorageDecl>(decl)) {
auto ty = asd->getInterfaceType();
// Strip off a generic signature if we have one. This matches the logic
// for methods, and ensures that we don't take a protocol's generic
// signature into account for a subscript requirement.
if (auto *genericFn = ty->getAs<GenericFunctionType>()) {
ty = FunctionType::get(genericFn->getParams(), genericFn->getResult(),
genericFn->getExtInfo());
}
// Handle properties and subscripts, anchored by the getter's selector.
return std::make_tuple(asd->isStatic(), asd->getObjCGetterSelector(),
ty->getCanonicalType());
}
llvm_unreachable("Not a valid @objc member");
}
LookupResult &ConstraintSystem::lookupMember(Type base, DeclNameRef name,
SourceLoc loc) {
// Check whether we've already performed this lookup.
auto &result = MemberLookups[{base, name}];
if (result) return *result;
// Lookup the member.
result = TypeChecker::lookupMember(
DC, base, name, loc, defaultConstraintSolverMemberLookupOptions);
// If we are in an @_unsafeInheritExecutor context, swap out
// declarations for their _unsafeInheritExecutor_ counterparts if they
// exist.
if (enclosingUnsafeInheritsExecutor(DC)) {
introduceUnsafeInheritExecutorReplacements(DC, base, loc, *result);
}
// If we aren't performing dynamic lookup, we're done.
if (!*result || !base->isAnyObject())
return *result;
// We are performing dynamic lookup. Filter out redundant results early.
llvm::DenseMap<std::tuple<char, ObjCSelector, CanType>, ValueDecl *> known;
bool anyRemovals = false;
for (const auto &entry : *result) {
auto *decl = entry.getValueDecl();
// Remove invalid declarations so the constraint solver doesn't need to
// cope with them.
if (decl->isInvalid()) {
anyRemovals = true;
continue;
}
// If this is the first entry with the signature, record it.
auto &uniqueEntry = known[getDynamicResultSignature(decl)];
if (!uniqueEntry) {
uniqueEntry = decl;
continue;
}
// We have duplication; note that we'll need to remove something,
anyRemovals = true;
// If the entry we recorded was unavailable but this new entry is not,
// replace the recorded entry with this one.
if (isDeclUnavailable(uniqueEntry) && !isDeclUnavailable(decl)) {
uniqueEntry = decl;
}
}
// If there's anything to remove, filter it out now.
if (anyRemovals) {
result->filter([&](LookupResultEntry entry, bool isOuter) -> bool {
auto *decl = entry.getValueDecl();
// Remove invalid declarations so the constraint solver doesn't need to
// cope with them.
if (decl->isInvalid())
return false;
return known[getDynamicResultSignature(decl)] == decl;
});
}
return *result;
}
ArrayRef<Type>
ConstraintSystem::getAlternativeLiteralTypes(KnownProtocolKind kind,
SmallVectorImpl<Type> &scratch) {
assert(scratch.empty());
if (kind == KnownProtocolKind::ExpressibleByIntegerLiteral) {
// Integer literals can be treated as floating point literals.
if (auto floatProto = getASTContext().getProtocol(
KnownProtocolKind::ExpressibleByFloatLiteral)) {
if (auto defaultType = TypeChecker::getDefaultType(floatProto, DC))
scratch.push_back(defaultType);
}
}
return scratch;
}
bool ConstraintSystem::containsIDEInspectionTarget(ASTNode node) const {
return swift::containsIDEInspectionTarget(node.getSourceRange(),
Context.SourceMgr);
}
bool ConstraintSystem::containsIDEInspectionTarget(
const ArgumentList *args) const {
return swift::containsIDEInspectionTarget(args->getSourceRange(),
Context.SourceMgr);
}
void ConstraintSystem::recordPotentialThrowSite(
CatchNode catchNode, PotentialThrowSite site) {
potentialThrowSites.push_back({catchNode, site});
if (solverState)
recordChange(SolverTrail::Change::RecordedPotentialThrowSite(catchNode));
}
void ConstraintSystem::removePotentialThrowSite(CatchNode catchNode) {
ASSERT(potentialThrowSites.back().first == catchNode);
potentialThrowSites.pop_back();
}
void ConstraintSystem::recordPotentialThrowSite(
PotentialThrowSite::Kind kind, Type type,
ConstraintLocatorBuilder locator) {
ASTContext &ctx = getASTContext();
// Only record potential throw sites when typed throws is enabled.
if (!ctx.LangOpts.hasFeature(Feature::FullTypedThrows))
return;
// Catch node location is determined by the source location.
auto sourceLoc = locator.getAnchor().getStartLoc();
if (!sourceLoc)
return;
auto catchNode = ASTScope::lookupCatchNode(DC->getParentModule(), sourceLoc);
if (!catchNode)
return;
// If there is an explicit caught type for this node, we don't need to
// record a potential throw site.
if (Type explicitCaughtType = catchNode.getExplicitCaughtType(ctx))
return;
// do..catch statements without an explicit `throws` clause do infer
// thrown types.
if (auto doCatch = catchNode.dyn_cast<DoCatchStmt *>()) {
PotentialThrowSite site{kind, type, getConstraintLocator(locator)};
recordPotentialThrowSite(catchNode, site);
return;
}
// Closures without an explicit `throws` clause, and which syntactically
// appear that they can throw, do infer thrown types.
auto closure = catchNode.get<ClosureExpr *>();
// Check whether the closure syntactically throws. If not, there is no
// need to record a throw site.
if (!closureEffects(closure).isThrowing())
return;
PotentialThrowSite site{kind, type, getConstraintLocator(locator)};
recordPotentialThrowSite(catchNode, site);
}
Type ConstraintSystem::getCaughtErrorType(CatchNode catchNode) {
ASTContext &ctx = getASTContext();
// If there is an explicit caught type for this node, use it.
if (Type explicitCaughtType = catchNode.getExplicitCaughtType(ctx)) {
if (explicitCaughtType->hasTypeParameter())
explicitCaughtType = DC->mapTypeIntoContext(explicitCaughtType);
return explicitCaughtType;
}
// Retrieve the thrown error type of a closure.
// FIXME: This will need to change when we do inference of thrown error
// types in closures.
if (auto closure = catchNode.dyn_cast<ClosureExpr *>()) {
return getClosureType(closure)->getEffectiveThrownErrorTypeOrNever();
}
if (!ctx.LangOpts.hasFeature(Feature::FullTypedThrows))
return ctx.getErrorExistentialType();
// Handle inference of caught error types.
// Collect all of the potential throw sites for this catch node.
SmallVector<PotentialThrowSite, 2> throwSites;
for (const auto &potentialThrowSite : potentialThrowSites) {
if (potentialThrowSite.first == catchNode) {
throwSites.push_back(potentialThrowSite.second);
}
}
Type caughtErrorType = ctx.getNeverType();
for (const auto &throwSite : throwSites) {
Type type = simplifyType(throwSite.type);
Type thrownErrorType;
switch (throwSite.kind) {
case PotentialThrowSite::Application: {
auto fnType = type->castTo<AnyFunctionType>();
thrownErrorType = fnType->getEffectiveThrownErrorTypeOrNever();
break;
}
case PotentialThrowSite::ExplicitThrow:
case PotentialThrowSite::NonExhaustiveDoCatch:
case PotentialThrowSite::PropertyAccess:
thrownErrorType = type;
break;
}
// Perform the errorUnion() of the caught error type so far with the
// thrown error type of this potential throw site.
caughtErrorType = TypeChecker::errorUnion(
caughtErrorType, thrownErrorType,
[&](Type type) {
return simplifyType(type);
});
// If we ended up at 'any Error', we're done.
if (caughtErrorType->isErrorExistentialType())
break;
}
return caughtErrorType;
}
ConstraintLocator *ConstraintSystem::getConstraintLocator(
ASTNode anchor, ArrayRef<ConstraintLocator::PathElement> path) {
auto summaryFlags = ConstraintLocator::getSummaryFlagsForPath(path);
return getConstraintLocator(anchor, path, summaryFlags);
}
ConstraintLocator *ConstraintSystem::getConstraintLocator(
ASTNode anchor, ArrayRef<ConstraintLocator::PathElement> path,
unsigned summaryFlags) {
assert(summaryFlags == ConstraintLocator::getSummaryFlagsForPath(path));
// Check whether a locator with this anchor + path already exists.
llvm::FoldingSetNodeID id;
ConstraintLocator::Profile(id, anchor, path);
void *insertPos = nullptr;
auto locator = ConstraintLocators.FindNodeOrInsertPos(id, insertPos);
if (locator)
return locator;
// Allocate a new locator and add it to the set.
locator = ConstraintLocator::create(getAllocator(), anchor, path,
summaryFlags);
ConstraintLocators.InsertNode(locator, insertPos);
return locator;
}
ConstraintLocator *ConstraintSystem::getConstraintLocator(
const ConstraintLocatorBuilder &builder) {
// If the builder has an empty path, just extract its base locator.
if (builder.hasEmptyPath()) {
return builder.getBaseLocator();
}
// We have to build a new locator. Extract the paths from the builder.
SmallVector<LocatorPathElt, 4> path;
auto anchor = builder.getLocatorParts(path);
return getConstraintLocator(anchor, path, builder.getSummaryFlags());
}
ConstraintLocator *ConstraintSystem::getConstraintLocator(
ConstraintLocator *locator,
ArrayRef<ConstraintLocator::PathElement> newElts) {
auto oldPath = locator->getPath();
SmallVector<ConstraintLocator::PathElement, 4> newPath;
newPath.append(oldPath.begin(), oldPath.end());
newPath.append(newElts.begin(), newElts.end());
return getConstraintLocator(locator->getAnchor(), newPath);
}
ConstraintLocator *ConstraintSystem::getConstraintLocator(
const ConstraintLocatorBuilder &builder,
ArrayRef<ConstraintLocator::PathElement> newElts) {
SmallVector<ConstraintLocator::PathElement, 4> newPath;
auto anchor = builder.getLocatorParts(newPath);
newPath.append(newElts.begin(), newElts.end());
return getConstraintLocator(anchor, newPath);
}
ConstraintLocator *ConstraintSystem::getImplicitValueConversionLocator(
ConstraintLocatorBuilder root, ConversionRestrictionKind restriction) {
SmallVector<LocatorPathElt, 4> path;
auto anchor = root.getLocatorParts(path);
{
if (isExpr<DictionaryExpr>(anchor) && path.size() > 1) {
// Drop everything except for first `tuple element #`.
path.pop_back_n(path.size() - 1);
}
// Drop any value-to-optional conversions that were applied along the
// way to reach this one.
while (!path.empty()) {
if (path.back().is<LocatorPathElt::OptionalPayload>()) {
path.pop_back();
continue;
}
break;
}
// If conversion is for a tuple element, let's drop `TupleType`
// components from the path since they carry information for
// diagnostics that `ExprRewriter` won't be able to re-construct
// during solution application.
if (!path.empty() && path.back().is<LocatorPathElt::TupleElement>()) {
path.erase(llvm::remove_if(path,
[](const LocatorPathElt &elt) {
return elt.is<LocatorPathElt::TupleType>();
}),
path.end());
}
}
return getConstraintLocator(/*base=*/getConstraintLocator(anchor, path),
LocatorPathElt::ImplicitConversion(restriction));
}
ConstraintLocator *ConstraintSystem::getCalleeLocator(
ConstraintLocator *locator, bool lookThroughApply,
llvm::function_ref<Type(Expr *)> getType,
llvm::function_ref<Type(Type)> simplifyType,
llvm::function_ref<std::optional<SelectedOverload>(ConstraintLocator *)>
getOverloadFor) {
if (locator->findLast<LocatorPathElt::ImplicitConversion>())
return locator;
auto anchor = locator->getAnchor();
auto path = locator->getPath();
{
// If we have an implicit x[dynamicMember:] subscript call, the callee
// is given by the original member locator it is based on, which we can get
// by stripping away the implicit member element and everything after it.
auto iter = path.rbegin();
using ImplicitSubscriptElt = LocatorPathElt::ImplicitDynamicMemberSubscript;
if (locator->findLast<ImplicitSubscriptElt>(iter)) {
auto newPath = path.drop_back(iter - path.rbegin() + 1);
return getConstraintLocator(anchor, newPath);
}
}
{
// If we have a locator for a member found through key path dynamic member
// lookup, then we need to chop off the elements after the
// KeyPathDynamicMember element to get the callee locator.
auto iter = path.rbegin();
if (locator->findLast<LocatorPathElt::KeyPathDynamicMember>(iter)) {
auto newPath = path.drop_back(iter - path.rbegin());
return getConstraintLocator(anchor, newPath);
}
}
{
// Pattern match is always a callee regardless of what comes after it.
auto iter = path.rbegin();
if (locator->findLast<LocatorPathElt::PatternMatch>(iter)) {
auto newPath = path.drop_back(iter - path.rbegin());
return getConstraintLocator(anchor, newPath);
}
}
if (locator->findLast<LocatorPathElt::DynamicCallable>()) {
return getConstraintLocator(anchor, LocatorPathElt::ApplyFunction());
}
if (locator->isLastElement<LocatorPathElt::ArgumentAttribute>()) {
return getConstraintLocator(anchor, path.drop_back());
}
// If we have a locator that starts with a key path component element, we
// may have a callee given by a property or subscript component.
if (auto componentElt =
locator->getFirstElementAs<LocatorPathElt::KeyPathComponent>()) {
auto *kpExpr = castToExpr<KeyPathExpr>(anchor);
auto component = kpExpr->getComponents()[componentElt->getIndex()];
using ComponentKind = KeyPathExpr::Component::Kind;
switch (component.getKind()) {
case ComponentKind::UnresolvedSubscript:
case ComponentKind::Subscript:
// For a subscript the callee is given by 'component -> subscript member'.
return getConstraintLocator(
anchor, {*componentElt, ConstraintLocator::SubscriptMember});
case ComponentKind::UnresolvedProperty:
case ComponentKind::Property:
// For a property, the choice is just given by the component.
return getConstraintLocator(anchor, *componentElt);
case ComponentKind::TupleElement:
llvm_unreachable("Not implemented by CSGen");
break;
case ComponentKind::Invalid:
case ComponentKind::OptionalForce:
case ComponentKind::OptionalChain:
case ComponentKind::OptionalWrap:
case ComponentKind::Identity:
case ComponentKind::DictionaryKey:
case ComponentKind::CodeCompletion:
// These components don't have any callee associated, so just continue.
break;
}
}
// Make sure we handle subscripts before looking at apply exprs. We don't
// want to return a subscript member locator for an expression such as x[](y),
// as its callee is not the subscript, but rather the function it returns.
if (isExpr<SubscriptExpr>(anchor))
return getConstraintLocator(anchor, ConstraintLocator::SubscriptMember);
auto getSpecialFnCalleeLoc = [&](Type fnTy) -> ConstraintLocator * {
fnTy = simplifyType(fnTy);
// It's okay for function type to contain type variable(s) e.g.
// opened generic function types, but not to be one.
assert(!fnTy->is<TypeVariableType>());
// For an apply of a metatype, we have a short-form constructor. Unlike
// other locators to callees, these are anchored on the apply expression
// rather than the function expr.
if (fnTy->is<AnyMetatypeType>()) {
return getConstraintLocator(anchor,
{LocatorPathElt::ApplyFunction(),
LocatorPathElt::ConstructorMember()});
}
// Handle an apply of a nominal type which supports callAsFunction.
if (fnTy->isCallAsFunctionType(DC)) {
return getConstraintLocator(anchor,
{LocatorPathElt::ApplyFunction(),
LocatorPathElt::ImplicitCallAsFunction()});
}
// Handling an apply for a nominal type that supports @dynamicCallable.
if (fnTy->hasDynamicCallableAttribute()) {
return getConstraintLocator(anchor, LocatorPathElt::ApplyFunction());
}
return nullptr;
};
if (lookThroughApply) {
if (auto *applyExpr = getAsExpr<ApplyExpr>(anchor)) {
auto *fnExpr = applyExpr->getFn();
// Handle special cases for applies of non-function types.
if (auto *loc = getSpecialFnCalleeLoc(getType(fnExpr)))
return loc;
// Otherwise fall through and look for locators anchored on the function
// expr. For CallExprs, this can look through things like parens and
// optional chaining.
if (auto *callExpr = getAsExpr<CallExpr>(anchor)) {
anchor = callExpr->getDirectCallee();
} else {
anchor = fnExpr;
}
}
}
if (auto *UDE = getAsExpr<UnresolvedDotExpr>(anchor)) {
if (UDE->isImplicit() &&
UDE->getName().getBaseName() == Context.Id_callAsFunction) {
return getConstraintLocator(anchor,
{LocatorPathElt::ApplyFunction(),
LocatorPathElt::ImplicitCallAsFunction()});
}
return getConstraintLocator(
anchor, TypeChecker::getSelfForInitDelegationInConstructor(DC, UDE)
? ConstraintLocator::ConstructorMember
: ConstraintLocator::Member);
}
if (auto *UME = getAsExpr<UnresolvedMemberExpr>(anchor)) {
return getConstraintLocator(UME, ConstraintLocator::UnresolvedMember);
}
if (isExpr<MemberRefExpr>(anchor))
return getConstraintLocator(anchor, ConstraintLocator::Member);
if (isExpr<ObjectLiteralExpr>(anchor))
return getConstraintLocator(anchor, ConstraintLocator::ConstructorMember);
if (locator->isFirstElement<LocatorPathElt::CoercionOperand>()) {
auto *CE = castToExpr<CoerceExpr>(anchor);
locator = getConstraintLocator(CE->getSubExpr()->getValueProvidingExpr(),
path.drop_front());
return getCalleeLocator(locator, lookThroughApply, getType, simplifyType,
getOverloadFor);
}
if (auto FVE = getAsExpr<ForceValueExpr>(anchor))
return getConstraintLocator(FVE->getSubExpr(), ConstraintLocator::Member);
return getConstraintLocator(anchor);
}
ConstraintLocator *ConstraintSystem::getOpenOpaqueLocator(
ConstraintLocatorBuilder locator, OpaqueTypeDecl *opaqueDecl) {
// Use only the opaque type declaration.
return getConstraintLocator(
ASTNode(opaqueDecl),
{ LocatorPathElt::OpenedOpaqueArchetype(opaqueDecl) }, 0);
}
std::pair<Type, OpenedArchetypeType *> ConstraintSystem::openExistentialType(
Type type, ConstraintLocator *locator) {
Type result = OpenedArchetypeType::getAny(type);
Type t = result;
while (t->is<MetatypeType>())
t = t->getMetatypeInstanceType();
auto *opened = t->castTo<OpenedArchetypeType>();
recordOpenedExistentialType(locator, opened);
return {result, opened};
}
void ConstraintSystem::recordOpenedExistentialType(
ConstraintLocator *locator, OpenedArchetypeType *opened) {
bool inserted = OpenedExistentialTypes.insert({locator, opened}).second;
ASSERT(inserted);
if (solverState)
recordChange(SolverTrail::Change::RecordedOpenedExistentialType(locator));
}
GenericEnvironment *
ConstraintSystem::getPackElementEnvironment(ConstraintLocator *locator,
CanType shapeClass) {
assert(locator->directlyAt<PackExpansionExpr>());
std::pair<UUID, Type> uuidAndShape;
auto result = PackExpansionEnvironments.find(locator);
if (result == PackExpansionEnvironments.end()) {
uuidAndShape = std::make_pair(UUID::fromTime(), shapeClass);
recordPackExpansionEnvironment(locator, uuidAndShape);
} else {
uuidAndShape = result->second;
}
if (!shapeClass->is<PackArchetypeType>() ||
!shapeClass->isEqual(uuidAndShape.second))
return nullptr;
auto shapeParam = cast<GenericTypeParamType>(
shapeClass->mapTypeOutOfContext()->getCanonicalType());
auto &ctx = getASTContext();
auto *contextEnv = PackElementGenericEnvironments.empty()
? DC->getGenericEnvironmentOfContext()
: PackElementGenericEnvironments.back();
auto elementSig = ctx.getOpenedElementSignature(
contextEnv->getGenericSignature().getCanonicalSignature(), shapeParam);
auto contextSubs = contextEnv->getForwardingSubstitutionMap();
return GenericEnvironment::forOpenedElement(elementSig, uuidAndShape.first,
shapeParam, contextSubs);
}
void ConstraintSystem::recordPackExpansionEnvironment(
ConstraintLocator *locator, std::pair<UUID, Type> uuidAndShape) {
bool inserted = PackExpansionEnvironments.insert({locator, uuidAndShape}).second;
ASSERT(inserted);
if (solverState)
recordChange(SolverTrail::Change::RecordedPackExpansionEnvironment(locator));
}
PackExpansionExpr *
ConstraintSystem::getPackEnvironment(PackElementExpr *packElement) const {
const auto match = PackEnvironments.find(packElement);
return (match == PackEnvironments.end()) ? nullptr : match->second;
}
void ConstraintSystem::addPackEnvironment(PackElementExpr *packElement,
PackExpansionExpr *packExpansion) {
bool inserted = PackEnvironments.insert({packElement, packExpansion}).second;
ASSERT(inserted);
if (solverState)
recordChange(SolverTrail::Change::RecordedPackEnvironment(packElement));
}
/// Extend the given depth map by adding depths for all of the subexpressions
/// of the given expression.
static void extendDepthMap(
Expr *expr,
llvm::DenseMap<Expr *, std::pair<unsigned, Expr *>> &depthMap) {
// If we already have an entry in the map, we don't need to update it. This
// avoids invalidating previous entries when solving a smaller component of a
// larger AST node, e.g during conjunction solving.
if (depthMap.contains(expr))
return;
class RecordingTraversal : public ASTWalker {
SmallVector<ClosureExpr *, 4> Closures;
public:
llvm::DenseMap<Expr *, std::pair<unsigned, Expr *>> &DepthMap;
unsigned Depth = 0;
explicit RecordingTraversal(
llvm::DenseMap<Expr *, std::pair<unsigned, Expr *>> &depthMap)
: DepthMap(depthMap) {}
MacroWalking getMacroWalkingBehavior() const override {
return MacroWalking::ArgumentsAndExpansion;
}
// For argument lists, bump the depth of the arguments, as they are
// effectively nested within the argument list. It's debatable whether we
// should actually do this, as it doesn't reflect the true expression depth,
// but it's needed to preserve compatibility with the behavior from when
// TupleExpr and ParenExpr were used to represent argument lists.
PreWalkResult<ArgumentList *>
walkToArgumentListPre(ArgumentList *ArgList) override {
++Depth;
return Action::Continue(ArgList);
}
PostWalkResult<ArgumentList *>
walkToArgumentListPost(ArgumentList *ArgList) override {
--Depth;
return Action::Continue(ArgList);
}
PreWalkResult<Expr *> walkToExprPre(Expr *E) override {
DepthMap[E] = {Depth, Parent.getAsExpr()};
++Depth;
if (auto CE = dyn_cast<ClosureExpr>(E))
Closures.push_back(CE);
return Action::Continue(E);
}
PostWalkResult<Expr *> walkToExprPost(Expr *E) override {
if (auto CE = dyn_cast<ClosureExpr>(E)) {
assert(Closures.back() == CE);
Closures.pop_back();
}
--Depth;
return Action::Continue(E);
}
PreWalkResult<Stmt *> walkToStmtPre(Stmt *S) override {
if (auto RS = dyn_cast<ReturnStmt>(S)) {
// For return statements, treat the parent of the return expression
// as the closure itself.
if (RS->hasResult() && !Closures.empty()) {
llvm::SaveAndRestore<ParentTy> SavedParent(Parent, Closures.back());
auto E = RS->getResult();
E->walk(*this);
return Action::SkipNode(S);
}
}
return Action::Continue(S);
}
};
RecordingTraversal traversal(depthMap);
expr->walk(traversal);
}
std::optional<std::pair<unsigned, Expr *>>
ConstraintSystem::getExprDepthAndParent(Expr *expr) {
// Bring the set of expression weights up to date.
while (NumInputExprsInWeights < InputExprs.size()) {
extendDepthMap(InputExprs[NumInputExprsInWeights], ExprWeights);
++NumInputExprsInWeights;
}
auto e = ExprWeights.find(expr);
if (e != ExprWeights.end())
return e->second;
return std::nullopt;
}
std::optional<std::pair<Type, Type>>
ConstraintSystem::isDictionaryType(Type type) {
if (auto boundStruct = type->getAs<BoundGenericStructType>()) {
if (boundStruct->getDecl() == type->getASTContext().getDictionaryDecl()) {
auto genericArgs = boundStruct->getGenericArgs();
return std::make_pair(genericArgs[0], genericArgs[1]);
}
}
return std::nullopt;
}
std::optional<Type> ConstraintSystem::isSetType(Type type) {
if (auto boundStruct = type->getAs<BoundGenericStructType>()) {
if (boundStruct->getDecl() == type->getASTContext().getSetDecl())
return boundStruct->getGenericArgs()[0];
}
return std::nullopt;
}
Type ConstraintSystem::getFixedTypeRecursive(Type type, TypeMatchOptions &flags,
bool wantRValue) {
if (wantRValue)
type = type->getRValueType();
if (auto depMemType = type->getAs<DependentMemberType>()) {
auto baseTy = depMemType->getBase();
if (!baseTy->hasTypeVariable() && !baseTy->hasDependentMember())
return type;
// FIXME: Perform a more limited simplification?
Type newType = simplifyType(type);
if (newType.getPointer() == type.getPointer()) return type;
// Once we've simplified a dependent member type, we need to generate a
// new constraint.
flags |= TMF_GenerateConstraints;
return getFixedTypeRecursive(newType, flags, wantRValue);
}
// Tuple types can lose their tuple structure under substitution
// when a parameter pack is substituted with one element.
if (auto tuple = type->getAs<TupleType>()) {
auto simplified = simplifyType(type);
if (simplified.getPointer() == type.getPointer())
return type;
return getFixedTypeRecursive(simplified, flags, wantRValue);
}
if (auto metatype = type->getAs<AnyMetatypeType>()) {
auto simplified = simplifyType(type);
if (simplified.getPointer() == type.getPointer())
return type;
return getFixedTypeRecursive(simplified, flags, wantRValue);
}
if (auto typeVar = type->getAs<TypeVariableType>()) {
if (auto fixed = getFixedType(typeVar))
return getFixedTypeRecursive(fixed, flags, wantRValue);
return getRepresentative(typeVar);
}
return type;
}
TypeVariableType *ConstraintSystem::isRepresentativeFor(
TypeVariableType *typeVar, ConstraintLocator::PathElementKind kind) const {
// We only attempt to look for this if type variable is
// a representative.
if (getRepresentative(typeVar) != typeVar)
return nullptr;
auto &CG = getConstraintGraph();
auto result = CG.lookupNode(typeVar);
auto equivalence = result.first.getEquivalenceClass();
auto member = llvm::find_if(equivalence, [=](TypeVariableType *eq) {
auto *loc = eq->getImpl().getLocator();
if (!loc)
return false;
auto path = loc->getPath();
return !path.empty() && path.back().getKind() == kind;
});
if (member == equivalence.end())
return nullptr;
return *member;
}
static std::optional<std::pair<VarDecl *, Type>>
getPropertyWrapperInformationFromOverload(
SelectedOverload resolvedOverload, DeclContext *DC,
llvm::function_ref<std::optional<std::pair<VarDecl *, Type>>(VarDecl *)>
getInformation) {
if (auto *decl =
dyn_cast_or_null<VarDecl>(resolvedOverload.choice.getDeclOrNull())) {
if (auto declInformation = getInformation(decl)) {
Type type;
VarDecl *memberDecl;
std::tie(memberDecl, type) = *declInformation;
if (Type baseType = resolvedOverload.choice.getBaseType()) {
type = baseType->getRValueType()->getTypeOfMember(memberDecl, type);
}
return std::make_pair(decl, type);
}
}
return std::nullopt;
}
std::optional<std::pair<VarDecl *, Type>>
ConstraintSystem::getPropertyWrapperProjectionInfo(
SelectedOverload resolvedOverload) {
return getPropertyWrapperInformationFromOverload(
resolvedOverload, DC,
[](VarDecl *decl) -> std::optional<std::pair<VarDecl *, Type>> {
if (!decl->hasAttachedPropertyWrapper())
return std::nullopt;
auto projectionVar = decl->getPropertyWrapperProjectionVar();
if (!projectionVar)
return std::nullopt;
return std::make_pair(projectionVar,
projectionVar->getInterfaceType());
});
}
std::optional<std::pair<VarDecl *, Type>>
ConstraintSystem::getPropertyWrapperInformation(
SelectedOverload resolvedOverload) {
return getPropertyWrapperInformationFromOverload(
resolvedOverload, DC,
[](VarDecl *decl) -> std::optional<std::pair<VarDecl *, Type>> {
if (!decl->hasAttachedPropertyWrapper())
return std::nullopt;
auto backingTy = decl->getPropertyWrapperBackingPropertyType();
if (!backingTy)
return std::nullopt;
return std::make_pair(decl, backingTy);
});
}
std::optional<std::pair<VarDecl *, Type>>
ConstraintSystem::getWrappedPropertyInformation(
SelectedOverload resolvedOverload) {
return getPropertyWrapperInformationFromOverload(
resolvedOverload, DC,
[](VarDecl *decl) -> std::optional<std::pair<VarDecl *, Type>> {
if (auto wrapped = decl->getOriginalWrappedProperty())
return std::make_pair(decl, wrapped->getInterfaceType());
return std::nullopt;
});
}
void ConstraintSystem::addOverloadSet(Type boundType,
ArrayRef<OverloadChoice> choices,
DeclContext *useDC,
ConstraintLocator *locator,
std::optional<unsigned> favoredIndex) {
// If there is a single choice, add the bind overload directly.
if (choices.size() == 1) {
addBindOverloadConstraint(boundType, choices.front(), locator, useDC);
return;
}
SmallVector<Constraint *, 4> candidates;
generateOverloadConstraints(candidates, boundType, choices, useDC, locator,
favoredIndex);
// For an overload set (disjunction) from newly generated candidates.
addOverloadSet(candidates, locator);
}
void ConstraintSystem::addOverloadSet(ArrayRef<Constraint *> choices,
ConstraintLocator *locator) {
assert(!choices.empty() && "Empty overload set");
// If there is a single choice, attempt it right away.
if (choices.size() == 1) {
simplifyConstraint(*choices.front());
return;
}
auto *disjunction =
Constraint::createDisjunction(*this, choices, locator, ForgetChoice);
addUnsolvedConstraint(disjunction);
if (simplifyAppliedOverloads(disjunction, locator))
retireFailedConstraint(disjunction);
}
FunctionType::ExtInfo ConstraintSystem::closureEffects(ClosureExpr *expr) {
return evaluateOrDefault(
getASTContext().evaluator, ClosureEffectsRequest{expr},
FunctionType::ExtInfo());
}
FunctionType::ExtInfo ClosureEffectsRequest::evaluate(
Evaluator &evaluator, ClosureExpr *expr) const {
// A walker that looks for 'try' and 'throw' expressions
// that aren't nested within closures, nested declarations,
// or exhaustive catches.
class FindInnerThrows : public ASTWalker {
DeclContext *DC;
bool FoundThrow = false;
MacroWalking getMacroWalkingBehavior() const override {
return MacroWalking::Expansion;
}
PreWalkResult<Expr *> walkToExprPre(Expr *expr) override {
// If we've found a 'try', record it and terminate the traversal.
if (isa<TryExpr>(expr)) {
FoundThrow = true;
return Action::Stop();
}
// Don't walk into a 'try!' or 'try?'.
if (isa<ForceTryExpr>(expr) || isa<OptionalTryExpr>(expr)) {
return Action::SkipNode(expr);
}
// Do not recurse into other closures.
if (isa<ClosureExpr>(expr))
return Action::SkipNode(expr);
return Action::Continue(expr);
}
PreWalkAction walkToDeclPre(Decl *decl) override {
// Do not walk into function or type declarations.
return Action::VisitNodeIf(isa<PatternBindingDecl>(decl));
}
bool isSyntacticallyExhaustive(DoCatchStmt *stmt) {
for (auto catchClause : stmt->getCatches()) {
for (auto &LabelItem : catchClause->getMutableCaseLabelItems()) {
if (isSyntacticallyExhaustive(catchClause->getStartLoc(),
LabelItem))
return true;
}
}
return false;
}
bool isSyntacticallyExhaustive(SourceLoc CatchLoc,
CaseLabelItem &LabelItem) {
// If it's obviously non-exhaustive, great.
if (LabelItem.getGuardExpr())
return false;
// If we can show that it's exhaustive without full
// type-checking, great.
if (LabelItem.isSyntacticallyExhaustive())
return true;
// Okay, resolve the pattern.
Pattern *pattern = LabelItem.getPattern();
if (!LabelItem.isPatternResolved()) {
pattern = TypeChecker::resolvePattern(pattern, DC,
/*isStmtCondition*/false);
if (!pattern) return false;
// Save that aside while we explore the type.
LabelItem.setPattern(pattern, /*resolved=*/true);
}
// Require the pattern to have a particular shape: a number
// of is-patterns applied to an irrefutable pattern.
pattern = pattern->getSemanticsProvidingPattern();
while (auto isp = dyn_cast<IsPattern>(pattern)) {
Type castType;
if (auto castTypeRepr = isp->getCastTypeRepr()) {
castType = TypeResolution::resolveContextualType(
castTypeRepr, DC, TypeResolverContext::InExpression,
/*unboundTyOpener*/ nullptr,
/*placeholderHandler*/ nullptr,
/*packElementOpener*/ nullptr);
} else {
castType = isp->getCastType();
}
if (castType->hasError()) {
return false;
}
if (!isp->hasSubPattern()) {
pattern = nullptr;
break;
} else {
pattern = isp->getSubPattern()->getSemanticsProvidingPattern();
}
}
if (pattern && pattern->isRefutablePattern()) {
return false;
}
// Okay, now it should be safe to coerce the pattern.
// Pull the top-level pattern back out.
pattern = LabelItem.getPattern();
auto &ctx = DC->getASTContext();
if (!ctx.getErrorDecl())
return false;
auto contextualPattern =
ContextualPattern::forRawPattern(pattern, DC);
pattern = TypeChecker::coercePatternToType(
contextualPattern, ctx.getErrorExistentialType(),
TypeResolverContext::InExpression);
if (!pattern)
return false;
LabelItem.setPattern(pattern, /*resolved=*/true);
return LabelItem.isSyntacticallyExhaustive();
}
PreWalkResult<Stmt *> walkToStmtPre(Stmt *stmt) override {
// If we've found a 'throw', record it and terminate the traversal.
if (isa<ThrowStmt>(stmt)) {
FoundThrow = true;
return Action::Stop();
}
// Handle do/catch differently.
if (auto doCatch = dyn_cast<DoCatchStmt>(stmt)) {
// Only walk into the 'do' clause of a do/catch statement
// if the catch isn't syntactically exhaustive.
if (!isSyntacticallyExhaustive(doCatch)) {
if (!doCatch->getBody()->walk(*this))
return Action::Stop();
}
// Walk into all the catch clauses.
for (auto catchClause : doCatch->getCatches()) {
if (!catchClause->walk(*this))
return Action::Stop();
}
// We've already walked all the children we care about.
return Action::SkipNode(stmt);
}
if (auto forEach = dyn_cast<ForEachStmt>(stmt)) {
if (forEach->getTryLoc().isValid()) {
FoundThrow = true;
return Action::Stop();
}
}
return Action::Continue(stmt);
}
public:
FindInnerThrows(DeclContext *dc)
: DC(dc) {}
bool foundThrow() { return FoundThrow; }
};
// If either 'throws' or 'async' was explicitly specified, use that
// set of effects.
bool throws = expr->getThrowsLoc().isValid();
bool async = expr->getAsyncLoc().isValid();
bool sendable = expr->getAttrs().hasAttribute<SendableAttr>();
if (throws || async) {
return ASTExtInfoBuilder()
.withThrows(throws, /*FIXME:*/Type())
.withAsync(async)
.withSendable(sendable)
.build();
}
// Scan the body to determine the effects.
auto body = expr->getBody();
if (!body)
return ASTExtInfoBuilder().withSendable(sendable).build();
auto throwFinder = FindInnerThrows(expr);
body->walk(throwFinder);
return ASTExtInfoBuilder()
.withThrows(throwFinder.foundThrow(), /*FIXME:*/Type())
.withAsync(bool(findAsyncNode(expr)))
.withSendable(sendable)
.build();
}
bool ConstraintSystem::isAsynchronousContext(DeclContext *dc) {
if (auto func = dyn_cast<AbstractFunctionDecl>(dc))
return func->isAsyncContext();
if (auto closure = dyn_cast<ClosureExpr>(dc)) {
return evaluateOrDefault(
getASTContext().evaluator,
ClosureEffectsRequest{const_cast<ClosureExpr *>(closure)},
FunctionType::ExtInfo()).isAsync();
}
return false;
}
void ConstraintSystem::buildDisjunctionForOptionalVsUnderlying(
Type boundTy, Type ty, ConstraintLocator *locator) {
// NOTE: If we use other locator kinds for these disjunctions, we
// need to account for it in solution scores for forced-unwraps.
assert(locator->getPath().back().getKind() ==
ConstraintLocator::ImplicitlyUnwrappedDisjunctionChoice ||
locator->getPath().back().getKind() ==
ConstraintLocator::DynamicLookupResult);
assert(!ty->is<InOutType>());
auto rvalueTy = ty->getWithoutSpecifierType();
// If the type to bind is a placeholder, we can propagate it, as we don't know
// whether it can be optional or non-optional, and we would have already
// recorded a fix for it.
if (rvalueTy->isPlaceholder()) {
addConstraint(ConstraintKind::Bind, boundTy, ty, locator);
return;
}
// Create the constraint to bind to the optional type and make it the favored
// choice.
auto *bindToOptional =
Constraint::create(*this, ConstraintKind::Bind, boundTy, ty, locator);
bindToOptional->setFavored();
auto underlyingType = rvalueTy->getOptionalObjectType();
if (!underlyingType) {
// If we don't have an optional, `ty` hasn't been resolved yet.
auto *typeVar = rvalueTy->castTo<TypeVariableType>();
auto *locator = typeVar->getImpl().getLocator();
// We need to allocate a type variable to represent an object type of a
// future optional, and add a constraint between `ty` and `underlyingType`
// to model it.
underlyingType = createTypeVariable(
getConstraintLocator(locator, LocatorPathElt::GenericArgument(0)),
TVO_PrefersSubtypeBinding | TVO_CanBindToLValue |
TVO_CanBindToNoEscape);
// Using a `typeVar` here because l-value is going to be applied
// to the underlying type below.
addConstraint(ConstraintKind::OptionalObject, typeVar, underlyingType,
locator);
}
if (ty->is<LValueType>())
underlyingType = LValueType::get(underlyingType);
auto *bindToUnderlying = Constraint::create(*this, ConstraintKind::Bind,
boundTy, underlyingType, locator);
llvm::SmallVector<Constraint *, 2> choices = {bindToOptional,
bindToUnderlying};
// Create the disjunction
addDisjunctionConstraint(choices, locator, RememberChoice);
}
namespace {
struct TypeSimplifier {
ConstraintSystem &CS;
llvm::function_ref<Type(TypeVariableType *)> GetFixedTypeFn;
struct ActivePackExpansion {
bool isPackExpansion = false;
unsigned index = 0;
};
SmallVector<ActivePackExpansion, 4> ActivePackExpansions;
TypeSimplifier(ConstraintSystem &CS,
llvm::function_ref<Type(TypeVariableType *)> getFixedTypeFn)
: CS(CS), GetFixedTypeFn(getFixedTypeFn) {}
std::optional<Type> operator()(TypeBase *type) {
if (auto tvt = dyn_cast<TypeVariableType>(type)) {
auto fixedTy = GetFixedTypeFn(tvt);
// TODO: the following logic should be applied when rewriting
// PackElementType.
if (ActivePackExpansions.empty()) {
return fixedTy;
}
if (auto fixedPack = fixedTy->getAs<PackType>()) {
auto &activeExpansion = ActivePackExpansions.back();
if (activeExpansion.index >= fixedPack->getNumElements()) {
return std::nullopt;
}
auto fixedElt = fixedPack->getElementType(activeExpansion.index);
auto fixedExpansion = fixedElt->getAs<PackExpansionType>();
if (activeExpansion.isPackExpansion && fixedExpansion) {
return fixedExpansion->getPatternType();
} else if (!activeExpansion.isPackExpansion && !fixedExpansion) {
return fixedElt;
} else {
return std::nullopt;
}
}
return fixedTy;
}
if (auto tuple = dyn_cast<TupleType>(type)) {
if (tuple->getNumElements() == 1) {
auto element = tuple->getElement(0);
auto elementType = element.getType();
auto resolvedType = elementType.transformRec(*this);
// If this is a single-element tuple with pack expansion
// variable inside, let's unwrap it if pack is flattened.
if (!element.hasName()) {
if (auto *typeVar = elementType->getAs<TypeVariableType>()) {
if (typeVar->getImpl().isPackExpansion() &&
!resolvedType->isEqual(typeVar) &&
!resolvedType->is<PackExpansionType>() &&
!resolvedType->is<PackType>() &&
!resolvedType->is<PackArchetypeType>()) {
return resolvedType;
}
}
}
// Flatten single-element tuples containing type variables that cannot
// bind to packs.
auto typeVar = resolvedType->getAs<TypeVariableType>();
if (!element.hasName() && typeVar &&
!typeVar->getImpl().canBindToPack() &&
!typeVar->getImpl().isPackExpansion()) {
return typeVar;
}
}
}
if (auto expansion = dyn_cast<PackExpansionType>(type)) {
auto patternType = expansion->getPatternType();
// First, let's check whether pattern type has all of the type variables
// that represent packs resolved, otherwise we don't have enough information
// to flatten this pack expansion type.
//
// Note that we don't actually need to do deep transformation here
// because pack variables can only appear in structural positions.
if (patternType.findIf([&](Type type) {
if (auto *typeVar = type->getAs<TypeVariableType>()) {
if (typeVar->getImpl().canBindToPack())
return GetFixedTypeFn(typeVar)->is<TypeVariableType>();
}
return false;
})) {
return std::nullopt;
}
// Transform the count type, ignoring any active pack expansions.
auto countType = expansion->getCountType().transformRec(
TypeSimplifier(CS, GetFixedTypeFn));
if (!countType->is<PackType>() &&
!countType->is<PackArchetypeType>()) {
SmallVector<Type, 2> rootParameterPacks;
countType->getTypeParameterPacks(rootParameterPacks);
if (!rootParameterPacks.empty())
countType = rootParameterPacks[0];
}
// If both pattern and count are resolves, let's just return
// the pattern type for `transformWithPosition` to take care
// of the rest.
if (patternType->is<PackType>() && countType->is<PackType>())
return patternType;
if (auto countPack = countType->getAs<PackType>()) {
SmallVector<Type, 4> elts;
ActivePackExpansions.push_back({false, 0});
for (auto countElt : countPack->getElementTypes()) {
auto countExpansion = countElt->getAs<PackExpansionType>();
ActivePackExpansions.back().isPackExpansion =
(countExpansion != nullptr);
auto elt = expansion->getPatternType().transformRec(*this);
if (countExpansion)
elt = PackExpansionType::get(elt, countExpansion->getCountType());
elts.push_back(elt);
ActivePackExpansions.back().index++;
}
ActivePackExpansions.pop_back();
if (elts.size() == 1)
return elts[0];
return PackType::get(CS.getASTContext(), elts);
} else {
ActivePackExpansions.push_back({true, 0});
auto patternType = expansion->getPatternType().transformRec(*this);
ActivePackExpansions.pop_back();
return PackExpansionType::get(patternType, countType);
}
}
// If this is a dependent member type for which we end up simplifying
// the base to a non-type-variable, perform lookup.
if (auto depMemTy = dyn_cast<DependentMemberType>(type)) {
// Simplify the base.
Type newBase = depMemTy->getBase().transformRec(*this);
if (newBase->isPlaceholder()) {
return PlaceholderType::get(CS.getASTContext(), depMemTy);
}
// If nothing changed, we're done.
if (newBase.getPointer() == depMemTy->getBase().getPointer())
return std::nullopt;
// Dependent member types should only be created for associated types.
auto assocType = depMemTy->getAssocType();
assert(depMemTy->getAssocType() && "Expected associated type!");
// FIXME: It's kind of weird in general that we have to look
// through lvalue, inout and IUO types here
Type lookupBaseType = newBase->getWithoutSpecifierType();
if (auto selfType = lookupBaseType->getAs<DynamicSelfType>())
lookupBaseType = selfType->getSelfType();
if (lookupBaseType->mayHaveMembers() ||
lookupBaseType->is<PackType>()) {
auto *proto = assocType->getProtocol();
auto conformance = CS.lookupConformance(lookupBaseType, proto);
if (!conformance) {
// If the base type doesn't conform to the associatedtype's protocol,
// there will be a missing conformance fix applied in diagnostic mode,
// so the concrete dependent member type is considered a "hole" in
// order to continue solving.
auto memberTy = DependentMemberType::get(lookupBaseType, assocType);
if (CS.shouldAttemptFixes() &&
CS.getPhase() == ConstraintSystemPhase::Solving) {
return PlaceholderType::get(CS.getASTContext(), memberTy);
}
return memberTy;
}
auto result = conformance.getTypeWitness(lookupBaseType, assocType);
if (result && !result->hasError())
return result;
}
return DependentMemberType::get(lookupBaseType, assocType);
}
return std::nullopt;
}
};
} // end anonymous namespace
Type ConstraintSystem::simplifyTypeImpl(Type type,
llvm::function_ref<Type(TypeVariableType *)> getFixedTypeFn) {
return type.transformRec(TypeSimplifier(*this, getFixedTypeFn));
}
Type ConstraintSystem::simplifyType(Type type) {
if (!type->hasTypeVariable())
return type;
// Map type variables down to the fixed types of their representatives.
return simplifyTypeImpl(type,
[&](TypeVariableType *tvt) -> Type {
if (auto fixed = getFixedType(tvt))
return simplifyType(fixed);
return getRepresentative(tvt);
});
}
void Solution::recordSingleArgMatchingChoice(ConstraintLocator *locator) {
auto &cs = getConstraintSystem();
assert(argumentMatchingChoices.find(locator) ==
argumentMatchingChoices.end() &&
"recording multiple bindings for same locator");
argumentMatchingChoices.insert(
{cs.getConstraintLocator(locator, ConstraintLocator::ApplyArgument),
MatchCallArgumentResult::forArity(1)});
}
Type Solution::simplifyType(Type type) const {
if (!(type->hasTypeVariable() || type->hasPlaceholder()))
return type;
// Map type variables to fixed types from bindings.
auto &cs = getConstraintSystem();
auto resolvedType = cs.simplifyTypeImpl(
type, [&](TypeVariableType *tvt) -> Type { return getFixedType(tvt); });
// Placeholders shouldn't be reachable through a solution, they are only
// useful to determine what went wrong exactly.
if (resolvedType->hasPlaceholder()) {
return resolvedType.transformRec([&](Type type) -> std::optional<Type> {
if (type->isPlaceholder())
return Type(cs.getASTContext().TheUnresolvedType);
return std::nullopt;
});
}
return resolvedType;
}
Type Solution::simplifyTypeForCodeCompletion(Type Ty) const {
auto &CS = getConstraintSystem();
// First, instantiate all type variables that we know, but don't replace
// placeholders by unresolved types.
Ty = CS.simplifyTypeImpl(Ty, [this](TypeVariableType *typeVar) -> Type {
return getFixedType(typeVar);
});
// Next, replace all placeholders by type variables. We know that all type
// variables now in the type originate from placeholders.
Ty = Ty.transformRec([](Type type) -> std::optional<Type> {
if (auto *placeholder = type->getAs<PlaceholderType>()) {
if (auto *typeVar =
placeholder->getOriginator().dyn_cast<TypeVariableType *>()) {
return Type(typeVar);
}
}
return std::nullopt;
});
// Replace all type variables (which must come from placeholders) by their
// generic parameters. Because we call into simplifyTypeImpl
Ty = CS.simplifyTypeImpl(Ty, [&CS, this](TypeVariableType *typeVar) -> Type {
// Code completion depends on generic parameter type being represented in
// terms of `ArchetypeType` since it's easy to extract protocol requirements
// from it.
auto getTypeVarAsArchetype = [](TypeVariableType *typeVar) -> Type {
if (auto *GP = typeVar->getImpl().getGenericParameter()) {
if (auto *GPD = GP->getDecl()) {
return GPD->getInnermostDeclContext()->mapTypeIntoContext(GP);
}
}
return Type();
};
if (auto archetype = getTypeVarAsArchetype(typeVar)) {
return archetype;
}
// Sometimes the type variable itself doesn't have have an originator that
// can be replaced by an archetype but one of its equivalent type variable
// does.
// Search thorough all equivalent type variables, looking for one that can
// be replaced by a generic parameter.
std::vector<std::pair<TypeVariableType *, Type>> bindings(
typeBindings.begin(), typeBindings.end());
// Make sure we iterate the bindings in a deterministic order.
llvm::sort(bindings, [](const std::pair<TypeVariableType *, Type> &lhs,
const std::pair<TypeVariableType *, Type> &rhs) {
return lhs.first->getID() < rhs.first->getID();
});
for (auto binding : bindings) {
if (auto placeholder = binding.second->getAs<PlaceholderType>()) {
if (placeholder->getOriginator().dyn_cast<TypeVariableType *>() ==
typeVar) {
if (auto archetype = getTypeVarAsArchetype(binding.first)) {
return archetype;
}
}
}
}
// When applying the logic below to get contextual types inside result
// builders, the code completion type variable is connected by a one-way
// constraint to a type variable in the buildBlock call, but that is not the
// type variable that represents the argument type. We need to find the type
// variable representing the argument to retrieve protocol requirements from
// it. Look for a ArgumentConversion constraint that allows us to retrieve
// the argument type var.
for (auto argConstraint :
CS.getConstraintGraph()[typeVar].getConstraints()) {
if (argConstraint->getKind() == ConstraintKind::ArgumentConversion &&
argConstraint->getFirstType()->getRValueType()->isEqual(typeVar)) {
if (auto argTV =
argConstraint->getSecondType()->getAs<TypeVariableType>()) {
if (auto archetype = getTypeVarAsArchetype(argTV)) {
return archetype;
}
}
}
}
return typeVar;
});
// Logic to determine the contextual type inside buildBlock result builders:
//
// When completing inside a result builder, the result builder
// @ViewBuilder var body: some View {
// Text("Foo")
// #^COMPLETE^#
// }
// gets rewritten to
// @ViewBuilder var body: some View {
// let $__builder2: Text
// let $__builder0 = Text("Foo")
// let $__builder1 = #^COMPLETE^#
// $__builder2 = ViewBuilder.buildBlock($__builder0, $__builder1)
// return $__builder2
// }
// Inside the constraint system
// let $__builder1 = #^COMPLETE^#
// gets type checked without context, so we can't know the contextual type for
// the code completion token. But we know that $__builder1 (and thus the type
// of #^COMPLETE^#) is used as the second argument to ViewBuilder.buildBlock,
// so we can extract the contextual type from that call. To do this, figure
// out the type variable that is used for $__builder1 in the buildBlock call.
// This type variable is connected to the type variable of $__builder1's
// definition by a one-way constraint.
if (auto TV = Ty->getAs<TypeVariableType>()) {
for (auto constraint : CS.getConstraintGraph()[TV].getConstraints()) {
if (constraint->getKind() == ConstraintKind::OneWayEqual &&
constraint->getSecondType()->isEqual(TV)) {
return simplifyTypeForCodeCompletion(constraint->getFirstType());
}
}
}
// Remove any remaining type variables and placeholders
Ty = simplifyType(Ty);
return Ty->getRValueType();
}
template <typename T>
static inline size_t size_in_bytes(const T &x) {
return (x.size() * (sizeof(typename T::key_type) + sizeof(unsigned))) +
(x.size() * (sizeof(typename T::value_type)));
}
size_t Solution::getTotalMemory() const {
if (TotalMemory)
return *TotalMemory;
const_cast<Solution *>(this)->TotalMemory
= sizeof(*this) + typeBindings.getMemorySize() +
overloadChoices.getMemorySize() +
ConstraintRestrictions.getMemorySize() +
(Fixes.size() * sizeof(void *)) + DisjunctionChoices.getMemorySize() +
AppliedDisjunctions.getMemorySize() +
OpenedTypes.getMemorySize() + OpenedExistentialTypes.getMemorySize() +
OpenedPackExpansionTypes.getMemorySize() +
PackExpansionEnvironments.getMemorySize() +
size_in_bytes(PackEnvironments) +
(DefaultedConstraints.size() * sizeof(void *)) +
nodeTypes.getMemorySize() +
keyPathComponentTypes.getMemorySize() +
size_in_bytes(KeyPaths) +
(contextualTypes.size() * sizeof(ASTNode)) +
size_in_bytes(targets) +
size_in_bytes(caseLabelItems) +
size_in_bytes(exprPatterns) +
size_in_bytes(isolatedParams) +
size_in_bytes(preconcurrencyClosures) +
size_in_bytes(resultBuilderTransformed) +
size_in_bytes(appliedPropertyWrappers) +
size_in_bytes(argumentLists) +
size_in_bytes(ImplicitCallAsFunctionRoots) +
size_in_bytes(SynthesizedConformances);
return *TotalMemory;
}
DeclContext *Solution::getDC() const { return constraintSystem->DC; }
DeclName OverloadChoice::getName() const {
switch (getKind()) {
case OverloadChoiceKind::Decl:
case OverloadChoiceKind::DeclViaDynamic:
case OverloadChoiceKind::DeclViaBridge:
case OverloadChoiceKind::DeclViaUnwrappedOptional:
return getDecl()->getName();
case OverloadChoiceKind::KeyPathApplication:
// TODO: This should probably produce subscript(keyPath:), but we
// don't currently pre-filter subscript overload sets by argument
// keywords, so "subscript" is still the name that keypath subscripts
// are looked up by.
return DeclBaseName::createSubscript();
case OverloadChoiceKind::DynamicMemberLookup:
case OverloadChoiceKind::KeyPathDynamicMemberLookup:
return DeclName(DynamicMember.getPointer());
case OverloadChoiceKind::MaterializePack:
case OverloadChoiceKind::TupleIndex:
case OverloadChoiceKind::ExtractFunctionIsolation:
llvm_unreachable("no name!");
}
llvm_unreachable("Unhandled OverloadChoiceKind in switch.");
}
std::optional<IUOReferenceKind>
OverloadChoice::getIUOReferenceKind(ConstraintSystem &cs,
bool forSecondApplication) const {
auto *decl = getDeclOrNull();
if (!decl || !decl->isImplicitlyUnwrappedOptional())
return std::nullopt;
// If this isn't an IUO return () -> T!, it's an IUO value.
if (!decl->getInterfaceType()->is<AnyFunctionType>())
return IUOReferenceKind::Value;
auto refKind = getFunctionRefKind();
assert(!forSecondApplication || refKind == FunctionRefKind::DoubleApply);
switch (refKind) {
case FunctionRefKind::Unapplied:
case FunctionRefKind::Compound:
// Such references never produce IUOs.
return std::nullopt;
case FunctionRefKind::SingleApply:
case FunctionRefKind::DoubleApply: {
// Check whether this is a curried function reference e.g
// (Self) -> (Args...) -> Ret. Such a function reference can only produce
// an IUO on the second application.
auto isCurried = decl->hasCurriedSelf() && !hasAppliedSelf(cs, *this);
if (forSecondApplication != isCurried)
return std::nullopt;
break;
}
}
return IUOReferenceKind::ReturnValue;
}
SolutionResult ConstraintSystem::salvage() {
if (isDebugMode()) {
llvm::errs() << "---Attempting to salvage and emit diagnostics---\n";
}
setPhase(ConstraintSystemPhase::Diagnostics);
// Attempt to solve again, capturing all states that come from our attempts to
// select overloads or bind type variables.
//
// FIXME: can this be removed? We need to arrange for recordFixes to be
// eliminated.
SmallVector<Solution, 2> viable;
viable.clear();
{
// Set up solver state.
SolverState state(*this, FreeTypeVariableBinding::Disallow);
state.recordFixes = true;
// Solve the system.
solveImpl(viable);
// If we hit a threshold, we're done.
if (isTooComplex(viable))
return SolutionResult::forTooComplex(getTooComplexRange());
// Before removing any "fixed" solutions, let's check
// if ambiguity is caused by fixes and diagnose if possible.
if (diagnoseAmbiguityWithFixes(viable))
return SolutionResult::forAmbiguous(viable);
// Check whether we have a best solution; this can happen if we found
// a series of fixes that worked.
if (auto best = findBestSolution(viable, /*minimize=*/true)) {
if (*best != 0)
viable[0] = std::move(viable[*best]);
viable.erase(viable.begin() + 1, viable.end());
return SolutionResult::forSolved(std::move(viable[0]));
}
if (shouldSuppressDiagnostics())
return viable.empty() ? SolutionResult::forUndiagnosedError()
: SolutionResult::forAmbiguous(viable);
// FIXME: If we were able to actually fix things along the way,
// we may have to hunt for the best solution. For now, we don't care.
// Remove solutions that require fixes; the fixes in those systems should
// be diagnosed rather than any ambiguity.
auto hasFixes = [](const Solution &sol) { return !sol.Fixes.empty(); };
auto newEnd = std::remove_if(viable.begin(), viable.end(), hasFixes);
viable.erase(newEnd, viable.end());
// If there are multiple solutions, try to diagnose an ambiguity.
if (viable.size() > 1) {
if (isDebugMode()) {
auto &log = llvm::errs();
log << "---Ambiguity error: " << viable.size()
<< " solutions found---\n";
int i = 0;
for (auto &solution : viable) {
log << "---Ambiguous solution #" << i++ << "---\n";
solution.dump(log, solverState->getCurrentIndent());
log << "\n";
}
}
if (diagnoseAmbiguity(viable)) {
return SolutionResult::forAmbiguous(viable);
}
}
// Fall through to produce diagnostics.
}
// Could not produce a specific diagnostic; punt to the client.
return SolutionResult::forUndiagnosedError();
}
static void diagnoseOperatorAmbiguity(ConstraintSystem &cs,
Identifier operatorName,
ArrayRef<Solution> solutions,
ConstraintLocator *locator) {
auto &ctx = cs.getASTContext();
auto &DE = ctx.Diags;
auto *anchor = castToExpr(locator->getAnchor());
auto *applyExpr = cast<ApplyExpr>(cs.getParentExpr(anchor));
auto isEnumWithAssociatedValues = [](Type type) -> bool {
if (auto *enumType = type->getAs<EnumType>())
return !enumType->getDecl()->hasOnlyCasesWithoutAssociatedValues();
return false;
};
const auto &solution = solutions.front();
if (auto *binaryOp = dyn_cast<BinaryExpr>(applyExpr)) {
auto *lhs = binaryOp->getLHS();
auto *rhs = binaryOp->getRHS();
auto lhsType =
solution.simplifyType(solution.getType(lhs))->getRValueType();
auto rhsType =
solution.simplifyType(solution.getType(rhs))->getRValueType();
if (lhsType->isEqual(rhsType)) {
DE.diagnose(anchor->getLoc(), diag::cannot_apply_binop_to_same_args,
operatorName.str(), lhsType)
.highlight(lhs->getSourceRange())
.highlight(rhs->getSourceRange());
if (isStandardComparisonOperator(binaryOp->getFn()) &&
isEnumWithAssociatedValues(lhsType)) {
DE.diagnose(applyExpr->getLoc(),
diag::no_binary_op_overload_for_enum_with_payload,
operatorName.str());
return;
}
} else if (operatorName == ctx.Id_MatchOperator) {
DE.diagnose(anchor->getLoc(), diag::cannot_match_expr_pattern_with_value,
lhsType, rhsType);
} else {
DE.diagnose(anchor->getLoc(), diag::cannot_apply_binop_to_args,
operatorName.str(), lhsType, rhsType)
.highlight(lhs->getSourceRange())
.highlight(rhs->getSourceRange());
}
} else {
auto *arg = applyExpr->getArgs()->getUnlabeledUnaryExpr();
assert(arg && "Expected a unary arg");
auto argType = solution.simplifyType(solution.getType(arg));
DE.diagnose(anchor->getLoc(), diag::cannot_apply_unop_to_arg,
operatorName.str(), argType->getRValueType());
}
std::set<std::string> parameters;
for (const auto &solution : solutions) {
auto overload = solution.getOverloadChoice(locator);
auto overloadType = overload.adjustedOpenedType;
// Let's suggest only concrete overloads here.
// Notes are going to take care of the rest,
// since printing types like `(Self, Self)` is not
// really useful.
if (overloadType->hasTypeVariable())
continue;
auto overloadFnTy = overloadType->getAs<FunctionType>();
if (!overloadFnTy)
continue;
// If arguments to all parameters have been fixed then there is nothing
// to note about in this overload.
std::set<unsigned> fixedParams;
llvm::for_each(solution.Fixes, [&](const ConstraintFix *fix) {
auto *locator = fix->getLocator();
if (getAsExpr(locator->getAnchor()) != applyExpr)
return;
if (auto argLoc = locator->findLast<LocatorPathElt::ApplyArgToParam>()) {
fixedParams.insert(argLoc->getParamIdx());
}
});
if (fixedParams.size() == overloadFnTy->getNumParams())
continue;
parameters.insert(
FunctionType::getParamListAsString(overloadFnTy->getParams()));
}
// All of the overload choices had generic parameters like `Self`.
if (parameters.empty())
return;
DE.diagnose(anchor->getLoc(), diag::suggest_partial_overloads,
/*isResult=*/false, operatorName.str(),
llvm::join(parameters, ", "));
}
std::string swift::describeGenericType(ValueDecl *GP, bool includeName) {
if (!GP)
return "";
Decl *parent = nullptr;
if (auto *AT = dyn_cast<AssociatedTypeDecl>(GP)) {
parent = AT->getProtocol();
} else {
auto *dc = GP->getDeclContext();
parent = dc->getInnermostDeclarationDeclContext();
}
if (!parent)
return "";
llvm::SmallString<64> result;
llvm::raw_svector_ostream OS(result);
OS << Decl::getDescriptiveKindName(GP->getDescriptiveKind());
if (includeName && GP->hasName())
OS << " '" << GP->getBaseName() << "'";
OS << " of ";
OS << Decl::getDescriptiveKindName(parent->getDescriptiveKind());
if (auto *decl = dyn_cast<ValueDecl>(parent)) {
if (decl->hasName())
OS << " '" << decl->getName() << "'";
}
return OS.str().str();
}
/// Special handling of conflicts associated with generic arguments.
///
/// func foo<T>(_: T, _: T) {}
/// func bar(x: Int, y: Float) {
/// foo(x, y)
/// }
///
/// It's done by first retrieving all generic parameters from each solution,
/// filtering bindings into a distinct set and diagnosing any differences.
static bool diagnoseConflictingGenericArguments(ConstraintSystem &cs,
const SolutionDiff &diff,
ArrayRef<Solution> solutions) {
if (!diff.overloads.empty())
return false;
bool noFixes = llvm::all_of(solutions, [](const Solution &solution) -> bool {
const auto score = solution.getFixedScore();
return score.Data[SK_Fix] == 0 && solution.Fixes.empty();
});
bool allMismatches =
llvm::all_of(solutions, [](const Solution &solution) -> bool {
return llvm::all_of(
solution.Fixes, [](const ConstraintFix *fix) -> bool {
return fix->getKind() == FixKind::AllowArgumentTypeMismatch ||
fix->getKind() == FixKind::AllowFunctionTypeMismatch ||
fix->getKind() == FixKind::AllowTupleTypeMismatch ||
fix->getKind() == FixKind::GenericArgumentsMismatch ||
fix->getKind() == FixKind::InsertCall ||
fix->getKind() == FixKind::IgnoreCollectionElementContextualMismatch;
});
});
if (!noFixes && !allMismatches)
return false;
auto &DE = cs.getASTContext().Diags;
llvm::SmallDenseMap<TypeVariableType *,
std::pair<GenericTypeParamType *, SourceLoc>, 4>
genericParams;
// Consider all representative type variables across all solutions.
for (auto &solution : solutions) {
for (auto &typeBinding : solution.typeBindings) {
auto *typeVar = typeBinding.first;
if (auto *GP = typeVar->getImpl().getGenericParameter()) {
auto *locator = typeVar->getImpl().getLocator();
auto *repr = cs.getRepresentative(typeVar);
// If representative is another generic parameter let's
// use its generic parameter type instead of originator's,
// but it's possible that generic parameter is equated to
// some other type e.g.
//
// func foo<T>(_: T) -> T {}
//
// In this case when reference to function `foo` is "opened"
// type variable representing `T` would be equated to
// type variable representing a result type of the reference.
if (auto *reprGP = repr->getImpl().getGenericParameter())
GP = reprGP;
genericParams[repr] = {GP, getLoc(locator->getAnchor())};
}
}
}
llvm::SmallDenseMap<std::pair<GenericTypeParamType *, SourceLoc>,
SmallVector<Type, 4>>
conflicts;
for (const auto &entry : genericParams) {
auto *typeVar = entry.first;
auto GP = entry.second;
swift::SmallSetVector<Type, 4> arguments;
for (const auto &solution : solutions) {
auto type = solution.typeBindings.lookup(typeVar);
// Type variables gathered from a solution's type binding context may not
// exist in another given solution because some solutions may have
// additional type variables not present in other solutions due to taking
// different paths in the solver.
if (!type)
continue;
// Contextual opaque result type is uniquely identified by
// declaration it's associated with, so we have to compare
// declarations instead of using pointer equality on such types.
if (auto *opaque = type->getAs<OpaqueTypeArchetypeType>()) {
auto *decl = opaque->getDecl();
arguments.remove_if([&](Type argType) -> bool {
if (auto *otherOpaque = argType->getAs<OpaqueTypeArchetypeType>()) {
return decl == otherOpaque->getDecl();
}
return false;
});
}
arguments.insert(type);
}
if (arguments.size() > 1)
conflicts[GP].append(arguments.begin(), arguments.end());
}
auto getGenericTypeDecl = [&](ArchetypeType *archetype) -> ValueDecl * {
auto type = archetype->getInterfaceType();
if (auto *GTPT = type->getAs<GenericTypeParamType>())
return GTPT->getDecl();
if (auto *DMT = type->getAs<DependentMemberType>())
return DMT->getAssocType();
return nullptr;
};
bool diagnosed = false;
for (auto &conflict : conflicts) {
SourceLoc loc;
GenericTypeParamType *GP;
std::tie(GP, loc) = conflict.first;
auto conflictingArguments = conflict.second;
// If there are any substitutions that are not fully resolved
// solutions cannot be considered conflicting for the given parameter.
if (llvm::any_of(conflictingArguments,
[](const auto &arg) { return arg->hasPlaceholder(); }))
continue;
llvm::SmallString<64> arguments;
llvm::raw_svector_ostream OS(arguments);
interleave(
conflictingArguments,
[&](Type argType) {
OS << "'" << argType << "'";
if (auto *opaque = argType->getAs<OpaqueTypeArchetypeType>()) {
auto *decl = opaque->getDecl()->getNamingDecl();
OS << " (result type of '" << decl->getBaseName().userFacingName()
<< "')";
return;
}
if (auto archetype = argType->getAs<ArchetypeType>()) {
if (auto *GTD = getGenericTypeDecl(archetype))
OS << " (" << describeGenericType(GTD) << ")";
}
},
[&OS] { OS << " vs. "; });
DE.diagnose(loc, diag::conflicting_arguments_for_generic_parameter, GP,
OS.str());
diagnosed = true;
}
return diagnosed;
}
/// Diagnose ambiguity related to overloaded declarations where only
/// *some* of the overload choices have ephemeral pointer warnings/errors
/// associated with them. Such situations have be handled specifically
/// because ephemeral fixes do not affect the score.
///
/// If all of the overloads have ephemeral fixes associated with them
/// it's much easier to diagnose through notes associated with each fix.
static bool
diagnoseAmbiguityWithEphemeralPointers(ConstraintSystem &cs,
ArrayRef<Solution> solutions) {
unsigned numSolutionsWithFixes = 0;
for (const auto &solution : solutions) {
if (solution.Fixes.empty()) {
continue;
}
if (!llvm::all_of(solution.Fixes, [](const ConstraintFix *fix) {
return fix->getKind() == FixKind::TreatEphemeralAsNonEphemeral;
}))
return false;
numSolutionsWithFixes += 1;
}
// If all or no solutions have fixes for ephemeral pointers, let's
// let `diagnoseAmbiguityWithFixes` diagnose the problem.
if (numSolutionsWithFixes == 0 ||
numSolutionsWithFixes == solutions.size())
return false;
// If only some of the solutions have ephemeral pointer fixes
// let's let `diagnoseAmbiguity` diagnose the problem either
// with affected argument or related declaration e.g. function ref.
return cs.diagnoseAmbiguity(solutions);
}
static bool diagnoseAmbiguityWithContextualType(
ConstraintSystem &cs, SolutionDiff &solutionDiff,
ArrayRef<std::pair<const Solution *, const ConstraintFix *>> aggregateFix,
ArrayRef<Solution> solutions) {
// Diagnose only if contextual failure is associated with every solution.
if (aggregateFix.size() < solutions.size())
return false;
auto getResultType =
[](const std::pair<const Solution *, const ConstraintFix *> &entry)
-> Type {
auto &solution = *entry.first;
auto anchor = entry.second->getLocator()->getAnchor();
return solution.simplifyType(solution.getType(anchor));
};
auto resultType = getResultType(aggregateFix.front());
// If right-hand side of the conversion (result of the AST node)
// is the same across all of the solutions let's diagnose it as if
// it it as a single failure.
if (llvm::all_of(
aggregateFix,
[&](const std::pair<const Solution *, const ConstraintFix *> &entry) {
return resultType->isEqual(getResultType(entry));
})) {
auto &fix = aggregateFix.front();
return fix.second->diagnose(*fix.first, /*asNote=*/false);
}
// If result types are different it could only mean that this is an attempt
// to convert a reference to, or call of overloaded declaration to a
// particular type.
auto &solution = *aggregateFix.front().first;
auto *locator = aggregateFix.front().second->getLocator();
auto *calleeLocator = solution.getCalleeLocator(locator);
auto result =
llvm::find_if(solutionDiff.overloads,
[&calleeLocator](const SolutionDiff::OverloadDiff &entry) {
return entry.locator == calleeLocator;
});
if (result == solutionDiff.overloads.end())
return false;
auto &DE = cs.getASTContext().Diags;
auto anchor = locator->getAnchor();
auto name = result->choices.front().getName();
auto contextualTy = solution.getContextualType(anchor);
// In some situations `getContextualType` for a contextual type
// locator is going to return then empty type. This happens because
// e.g. optional-some patterns and patterns with incorrect type don't
// have a contextual type for initialization expression but use
// a conversion with contextual locator nevertheless to indicate
// the purpose. This doesn't affect non-ambiguity diagnostics
// because mismatches carry both `from` and `to` types.
if (!contextualTy)
return false;
DE.diagnose(getLoc(anchor),
contextualTy->is<ProtocolType>()
? diag::no_overloads_have_result_type_conformance
: diag::no_candidates_match_result_type,
name.getBaseName().userFacingName(), contextualTy);
for (const auto &solution : solutions) {
auto overload = solution.getOverloadChoice(calleeLocator);
if (auto *decl = overload.choice.getDeclOrNull()) {
auto type = solution.simplifyType(overload.boundType);
if (isExpr<ApplyExpr>(anchor) || isExpr<SubscriptExpr>(anchor)) {
auto fnType = type->castTo<FunctionType>();
DE.diagnose(
decl,
contextualTy->is<ProtocolType>()
? diag::overload_result_type_does_not_conform
: diag::cannot_convert_candidate_result_to_contextual_type,
decl, fnType->getResult(), contextualTy);
} else {
DE.diagnose(decl, diag::found_candidate_type, type);
}
}
}
return true;
}
/// Diagnose problems with generic requirement fixes that are anchored on
/// one callee location. The list could contain different kinds of fixes
/// i.e. missing protocol conformances at different positions,
/// same-type requirement mismatches, etc.
static bool diagnoseAmbiguityWithGenericRequirements(
ConstraintSystem &cs,
ArrayRef<std::pair<const Solution *, const ConstraintFix *>> aggregate) {
// If all of the fixes point to the same overload choice,
// we can diagnose this an a single error.
bool hasNonDeclOverloads = false;
llvm::SmallSet<ValueDecl *, 4> overloadChoices;
for (const auto &entry : aggregate) {
const auto &solution = *entry.first;
auto *calleeLocator = solution.getCalleeLocator(entry.second->getLocator());
if (auto overload = solution.getOverloadChoiceIfAvailable(calleeLocator)) {
if (auto *D = overload->choice.getDeclOrNull()) {
overloadChoices.insert(D);
} else {
hasNonDeclOverloads = true;
}
}
}
auto &primaryFix = aggregate.front();
{
if (overloadChoices.size() > 0) {
// Some of the choices are non-declaration,
// let's delegate that to ambiguity diagnostics.
if (hasNonDeclOverloads)
return false;
if (overloadChoices.size() == 1)
return primaryFix.second->diagnose(*primaryFix.first);
// fall through to the tailored ambiguity diagnostic.
} else {
// If there are no overload choices it means that
// the issue is with types, delegate that to the primary fix.
return primaryFix.second->diagnoseForAmbiguity(aggregate);
}
}
// Produce "no exact matches" diagnostic.
auto &ctx = cs.getASTContext();
auto *choice = *overloadChoices.begin();
ctx.Diags.diagnose(getLoc(primaryFix.second->getLocator()->getAnchor()),
diag::no_overloads_match_exactly_in_call,
/*isApplication=*/false, choice,
choice->getName().isSpecial());
for (const auto &entry : aggregate) {
entry.second->diagnose(*entry.first, /*asNote=*/true);
}
return true;
}
static bool diagnoseAmbiguity(
ConstraintSystem &cs, const SolutionDiff::OverloadDiff &ambiguity,
ArrayRef<std::pair<const Solution *, const ConstraintFix *>> aggregateFix,
ArrayRef<Solution> solutions) {
auto *locator = aggregateFix.front().second->getLocator();
auto anchor = aggregateFix.front().second->getAnchor();
auto &DE = cs.getASTContext().Diags;
llvm::SmallPtrSet<ValueDecl *, 4> localAmbiguity;
{
for (auto &entry : aggregateFix) {
const auto &solution = entry.first;
const auto &overload = solution->getOverloadChoice(ambiguity.locator);
auto *choice = overload.choice.getDeclOrNull();
// It's not possible to diagnose different kinds of overload choices.
if (!choice)
return false;
localAmbiguity.insert(choice);
}
}
if (localAmbiguity.empty())
return false;
// If all of the fixes are rooted in the same choice.
if (localAmbiguity.size() == 1) {
auto &primaryFix = aggregateFix.front();
return primaryFix.second->diagnose(*primaryFix.first);
}
{
auto fixKind = aggregateFix.front().second->getKind();
if (llvm::all_of(
aggregateFix, [&](const std::pair<const Solution *,
const ConstraintFix *> &entry) {
auto &fix = entry.second;
return fix->getKind() == fixKind && fix->getLocator() == locator;
})) {
auto *primaryFix = aggregateFix.front().second;
if (primaryFix->diagnoseForAmbiguity(aggregateFix))
return true;
}
}
auto *decl = *localAmbiguity.begin();
auto *commonCalleeLocator = ambiguity.locator;
bool diagnosed = true;
{
DiagnosticTransaction transaction(DE);
auto commonAnchor = commonCalleeLocator->getAnchor();
if (auto *callExpr = getAsExpr<CallExpr>(commonAnchor))
commonAnchor = callExpr->getDirectCallee();
const auto name = decl->getName();
// Emit an error message for the ambiguity.
if (locator->isForContextualType()) {
auto baseName = name.getBaseName();
DE.diagnose(getLoc(commonAnchor), diag::no_candidates_match_result_type,
baseName.userFacingName(),
cs.getContextualType(anchor, /*forConstraint=*/false));
} else if (name.isOperator()) {
auto *anchor = castToExpr(commonCalleeLocator->getAnchor());
// If operator is "applied" e.g. `1 + 2` there are tailored
// diagnostics in case of ambiguity, but if it's referenced
// e.g. `arr.sort(by: <)` it's better to produce generic error
// and a note per candidate.
if (auto *parentExpr = cs.getParentExpr(anchor)) {
if (auto *apply = dyn_cast<ApplyExpr>(parentExpr)) {
if (apply->getFn() == anchor) {
diagnoseOperatorAmbiguity(cs, name.getBaseIdentifier(), solutions,
commonCalleeLocator);
return true;
}
}
}
DE.diagnose(anchor->getLoc(), diag::no_overloads_match_exactly_in_call,
/*isApplication=*/false, decl, name.isSpecial());
} else {
bool isApplication = llvm::any_of(solutions, [&](const auto &S) {
return llvm::any_of(S.argumentLists, [&](const auto &pair) {
return pair.first->getAnchor() == commonAnchor;
});
});
DE.diagnose(getLoc(commonAnchor),
diag::no_overloads_match_exactly_in_call, isApplication,
decl, name.isSpecial());
}
// Produce candidate notes
SmallPtrSet<ValueDecl *, 4> distinctChoices;
llvm::SmallSet<CanType, 4> candidateTypes;
for (const auto &solution : solutions) {
auto overload = solution.getOverloadChoice(commonCalleeLocator);
auto *decl = overload.choice.getDecl();
auto type = solution.simplifyType(overload.adjustedOpenedType);
// Skip if we've already produced a note for this overload
if (!distinctChoices.insert(decl).second)
continue;
auto noteLoc =
decl->getLoc().isInvalid() ? getLoc(commonAnchor) : decl->getLoc();
SmallVector<const ConstraintFix *, 4> fixes;
for (const auto &entry : aggregateFix) {
if (entry.first == &solution)
fixes.push_back(entry.second);
}
auto emitGeneralFoundCandidateNote = [&]() {
// Emit a general "found candidate" note
if (decl->getLoc().isInvalid()) {
if (candidateTypes.insert(type->getCanonicalType()).second)
DE.diagnose(getLoc(commonAnchor), diag::found_candidate_type, type);
} else {
DE.diagnose(noteLoc, diag::found_candidate);
}
};
if (fixes.size() == 1) {
diagnosed &= fixes.front()->diagnose(solution, /*asNote*/ true);
} else if (!fixes.empty() &&
llvm::all_of(fixes, [&](const ConstraintFix *fix) {
// Ignore coercion fixes in this context, to
// focus on the argument mismatches.
if (fix->getLocator()->isForCoercion())
return true;
return fix->getLocator()
->findLast<LocatorPathElt::ApplyArgument>()
.has_value();
})) {
// All fixes have to do with arguments, so let's show the parameter
// lists.
//
// It's possible that function type is wrapped in an optional
// if it's from `@objc optional` method, so we need to ignore that.
auto *fn =
type->lookThroughAllOptionalTypes()->getAs<AnyFunctionType>();
assert(fn);
auto first = llvm::find_if(fixes, [&](const ConstraintFix *fix) {
return fix->getLocator()
->findLast<LocatorPathElt::ApplyArgument>()
.has_value();
});
if (first != fixes.end()) {
auto *argList = solution.getArgumentList((*first)->getLocator());
assert(argList);
if (fn->getNumParams() == 1 && argList->isUnary()) {
const auto ¶m = fn->getParams()[0];
auto argTy = solution.getResolvedType(argList->getUnaryExpr());
DE.diagnose(noteLoc,
diag::candidate_has_invalid_argument_at_position,
solution.simplifyType(param.getPlainType()),
/*position=*/1, param.isInOut(), argTy);
} else {
DE.diagnose(noteLoc, diag::candidate_partial_match,
fn->getParamListAsString(fn->getParams()));
}
} else {
// Only coercion ambiguity fixes.
emitGeneralFoundCandidateNote();
}
} else {
emitGeneralFoundCandidateNote();
}
}
// If not all of the fixes produced a note, we can't diagnose this.
if (!diagnosed)
transaction.abort();
}
return diagnosed;
}
using FixInContext = std::pair<const Solution *, const ConstraintFix *>;
// Attempts to diagnose function call ambiguities of types inferred for a result
// generic parameter from contextual type and a closure argument that
// conflicting infer a different type for the same argument. Example:
// func callit<T>(_ f: () -> T) -> T {
// f()
// }
//
// func context() -> Int {
// callit {
// print("hello")
// }
// }
// Where generic argument `T` can be inferred both as `Int` from contextual
// result and `Void` from the closure argument result.
static bool diagnoseContextualFunctionCallGenericAmbiguity(
ConstraintSystem &cs, ArrayRef<FixInContext> contextualFixes,
ArrayRef<FixInContext> allFixes) {
if (contextualFixes.empty())
return false;
auto contextualFix = contextualFixes.front();
if (!std::all_of(contextualFixes.begin() + 1, contextualFixes.end(),
[&contextualFix](FixInContext fix) {
return fix.second->getLocator() ==
contextualFix.second->getLocator();
}))
return false;
auto fixLocator = contextualFix.second->getLocator();
auto contextualAnchor = fixLocator->getAnchor();
auto *AE = getAsExpr<ApplyExpr>(contextualAnchor);
// All contextual failures anchored on the same function call.
if (!AE)
return false;
auto fnLocator = cs.getConstraintLocator(AE->getSemanticFn());
auto overload = contextualFix.first->getOverloadChoiceIfAvailable(fnLocator);
if (!overload)
return false;
auto applyFnType = overload->adjustedOpenedType->castTo<FunctionType>();
auto resultTypeVar = applyFnType->getResult()->getAs<TypeVariableType>();
if (!resultTypeVar)
return false;
auto *GP = resultTypeVar->getImpl().getGenericParameter();
if (!GP)
return false;
auto applyLoc =
cs.getConstraintLocator(AE, {LocatorPathElt::ApplyArgument()});
auto argMatching =
contextualFix.first->argumentMatchingChoices.find(applyLoc);
if (argMatching == contextualFix.first->argumentMatchingChoices.end()) {
return false;
}
auto *args = AE->getArgs();
llvm::SmallVector<ClosureExpr *, 2> closureArguments;
for (auto i : indices(*args)) {
auto *closure = getAsExpr<ClosureExpr>(args->getExpr(i));
if (!closure)
continue;
auto argParamMatch = argMatching->second.parameterBindings[i];
auto param = applyFnType->getParams()[argParamMatch.front()];
auto paramFnType = param.getPlainType()->getAs<FunctionType>();
if (!paramFnType)
continue;
if (cs.typeVarOccursInType(resultTypeVar, paramFnType->getResult()))
closureArguments.push_back(closure);
}
// If no closure result's involves the generic parameter, just bail because we
// won't find a conflict.
if (closureArguments.empty())
return false;
// At least one closure where result type involves the generic parameter.
// So let's try to collect the set of fixed types for the generic parameter
// from all the closure contextual fix/solutions and if there are more than
// one fixed type diagnose it.
swift::SmallSetVector<Type, 4> genericParamInferredTypes;
for (auto &fix : contextualFixes)
genericParamInferredTypes.insert(fix.first->getFixedType(resultTypeVar));
if (llvm::all_of(allFixes, [&](FixInContext fix) {
auto fixLocator = fix.second->getLocator();
if (fixLocator->isForContextualType())
return true;
if (!(fix.second->getKind() == FixKind::IgnoreContextualType ||
fix.second->getKind() == FixKind::AllowTupleTypeMismatch))
return false;
auto anchor = fixLocator->getAnchor();
if (!(anchor == contextualAnchor ||
fixLocator->isLastElement<LocatorPathElt::ClosureResult>() ||
fixLocator->isLastElement<LocatorPathElt::ClosureBody>()))
return false;
genericParamInferredTypes.insert(
fix.first->getFixedType(resultTypeVar));
return true;
})) {
if (genericParamInferredTypes.size() != 2)
return false;
auto &DE = cs.getASTContext().Diags;
llvm::SmallString<64> arguments;
llvm::raw_svector_ostream OS(arguments);
interleave(
genericParamInferredTypes,
[&](Type argType) { OS << "'" << argType << "'"; },
[&OS] { OS << " vs. "; });
DE.diagnose(AE->getLoc(), diag::conflicting_arguments_for_generic_parameter,
GP, OS.str());
DE.diagnose(AE->getLoc(),
diag::generic_parameter_inferred_from_result_context, GP,
genericParamInferredTypes.back());
DE.diagnose(closureArguments.front()->getStartLoc(),
diag::generic_parameter_inferred_from_closure, GP,
genericParamInferredTypes.front());
return true;
}
return false;
}
bool ConstraintSystem::diagnoseAmbiguityWithFixes(
SmallVectorImpl<Solution> &solutions) {
if (solutions.empty() || shouldSuppressDiagnostics())
return false;
SolutionDiff solutionDiff(solutions);
if (diagnoseConflictingGenericArguments(*this, solutionDiff, solutions))
return true;
if (auto bestScore = solverState->BestScore) {
solutions.erase(llvm::remove_if(solutions,
[&](const Solution &solution) {
return solution.getFixedScore() >
*bestScore;
}),
solutions.end());
if (llvm::all_of(solutions, [&](const Solution &solution) {
auto score = solution.getFixedScore();
return score.Data[SK_Fix] == 0 && solution.Fixes.empty();
}))
return false;
}
if (solutions.size() < 2)
return false;
if (diagnoseAmbiguityWithEphemeralPointers(*this, solutions))
return true;
if (isDebugMode()) {
auto indent = solverState->getCurrentIndent();
auto &log = llvm::errs().indent(indent);
log << "--- Ambiguity: Considering #" << solutions.size()
<< " solutions with fixes ---\n";
int i = 0;
for (auto &solution : solutions) {
log << "\n";
log.indent(indent) << "--- Solution #" << i++ << "---\n";
solution.dump(log, indent);
log << "\n";
}
}
// If there either no fixes at all or all of the are warnings,
// let's diagnose this as regular ambiguity.
if (llvm::all_of(solutions, [](const Solution &solution) {
return llvm::all_of(solution.Fixes, [](const ConstraintFix *fix) {
return !fix->isFatal();
});
})) {
return diagnoseAmbiguity(solutions);
}
// Algorithm is as follows:
//
// a. Aggregate all of the available fixes based on callee locator;
// b. For each ambiguous overload match aggregated fixes and diagnose;
// c. Discard all of the fixes which have been already considered
// as part of overload diagnostics;
// d. Diagnose remaining (uniqued based on kind + locator) fixes
// iff they appear in all of the solutions.
llvm::SmallSetVector<FixInContext, 4> fixes;
for (auto &solution : solutions) {
for (auto *fix : solution.Fixes) {
// If the fix doesn't affect the solution score, it is not the
// source of ambiguity or failures.
// Ignore warnings in favor of actual error fixes,
// because they are not the source of ambiguity/failures.
if (!fix->impact())
continue;
fixes.insert({&solution, fix});
}
}
llvm::MapVector<ConstraintLocator *, SmallVector<FixInContext, 4>>
fixesByCallee;
llvm::SmallVector<FixInContext, 4> contextualFixes;
for (const auto &entry : fixes) {
const auto &solution = *entry.first;
const auto *fix = entry.second;
auto *locator = fix->getLocator();
if (locator->isForContextualType()) {
contextualFixes.push_back({&solution, fix});
continue;
}
auto *calleeLocator = solution.getCalleeLocator(locator);
fixesByCallee[calleeLocator].push_back({&solution, fix});
}
bool diagnosed = false;
// All of the fixes which have been considered already.
llvm::SmallSetVector<FixInContext, 4> consideredFixes;
for (const auto &ambiguity : solutionDiff.overloads) {
auto fixes = fixesByCallee.find(ambiguity.locator);
if (fixes == fixesByCallee.end())
continue;
auto aggregate = fixes->second;
diagnosed |= ::diagnoseAmbiguity(*this, ambiguity, aggregate, solutions);
consideredFixes.insert(aggregate.begin(), aggregate.end());
}
if (diagnoseAmbiguityWithContextualType(*this, solutionDiff, contextualFixes,
solutions)) {
consideredFixes.insert(contextualFixes.begin(), contextualFixes.end());
diagnosed |= true;
}
// Remove all of the fixes which have been attached to ambiguous
// overload choices.
fixes.set_subtract(consideredFixes);
// Aggregate all requirement fixes that belong to the same callee
// and attempt to diagnose possible ambiguities.
{
// Aggregates fixes fixes attached to `buildExpression` and `buildBlock`
// methods at the particular source location.
llvm::MapVector<SourceLoc, SmallVector<FixInContext, 4>>
builderMethodRequirementFixes;
llvm::MapVector<ConstraintLocator *, SmallVector<FixInContext, 4>>
perCalleeRequirementFixes;
for (const auto &entry : fixes) {
auto *fix = entry.second;
if (!fix->getLocator()->isLastElement<LocatorPathElt::AnyRequirement>())
continue;
auto *calleeLoc = entry.first->getCalleeLocator(fix->getLocator());
auto *UDE = getAsExpr<UnresolvedDotExpr>(calleeLoc->getAnchor());
if (UDE && isResultBuilderMethodReference(getASTContext(), UDE)) {
auto *anchor = castToExpr<Expr>(calleeLoc->getAnchor());
builderMethodRequirementFixes[anchor->getLoc()].push_back(entry);
} else {
perCalleeRequirementFixes[calleeLoc].push_back(entry);
}
}
SmallVector<SmallVector<FixInContext, 4>, 4> viableGroups;
{
auto takeAggregateIfViable =
[&](SmallVector<FixInContext, 4> &aggregate) {
// Ambiguity only if all of the solutions have a requirement
// fix at the given location.
if (aggregate.size() == solutions.size())
viableGroups.push_back(std::move(aggregate));
};
for (auto &entry : builderMethodRequirementFixes)
takeAggregateIfViable(entry.second);
for (auto &entry : perCalleeRequirementFixes)
takeAggregateIfViable(entry.second);
}
for (auto &aggregate : viableGroups) {
if (diagnoseAmbiguityWithGenericRequirements(*this, aggregate)) {
// Remove diagnosed fixes.
fixes.set_subtract(aggregate);
diagnosed = true;
}
}
}
llvm::MapVector<std::pair<FixKind, ConstraintLocator *>,
SmallVector<FixInContext, 4>>
fixesByKind;
for (const auto &entry : fixes) {
const auto *fix = entry.second;
fixesByKind[{fix->getKind(), fix->getLocator()}].push_back(entry);
}
// If leftover fix is contained in all of the solutions let's
// diagnose it as ambiguity.
for (const auto &entry : fixesByKind) {
if (llvm::all_of(solutions, [&](const Solution &solution) -> bool {
return llvm::any_of(
solution.Fixes, [&](const ConstraintFix *fix) -> bool {
return std::make_pair(fix->getKind(), fix->getLocator()) ==
entry.first;
});
})) {
auto &aggregate = entry.second;
diagnosed |= aggregate.front().second->diagnoseForAmbiguity(aggregate);
}
}
if (!diagnosed && diagnoseContextualFunctionCallGenericAmbiguity(
*this, contextualFixes, fixes.getArrayRef()))
return true;
return diagnosed;
}
/// Determine the number of distinct overload choices in the
/// provided set.
static unsigned countDistinctOverloads(ArrayRef<OverloadChoice> choices) {
llvm::SmallPtrSet<void *, 4> uniqueChoices;
for (auto choice : choices) {
uniqueChoices.insert(choice.getOpaqueChoiceSimple());
}
return uniqueChoices.size();
}
static Type getOverloadChoiceType(ConstraintLocator *overloadLoc,
const Solution &solution) {
auto selectedOverload = solution.overloadChoices.find(overloadLoc);
if (selectedOverload == solution.overloadChoices.end())
return Type();
return solution.simplifyType(selectedOverload->second.adjustedOpenedType);
}
/// Determine the name of the overload in a set of overload choices.
static DeclName getOverloadChoiceName(ArrayRef<OverloadChoice> choices) {
DeclName name;
for (auto choice : choices) {
if (!choice.isDecl())
continue;
const DeclName nextName = choice.getDecl()->getName();
if (!name) {
name = nextName;
continue;
}
if (name != nextName) {
// Assume all choices have the same base name and only differ in
// argument labels. This may not be a great assumption, but we don't
// really have a way to recover for diagnostics otherwise.
return name.getBaseName();
}
}
return name;
}
/// Extend the given index map with all of the subexpressions in the given
/// expression.
static void extendPreorderIndexMap(
Expr *expr, llvm::DenseMap<Expr *, unsigned> &indexMap) {
class RecordingTraversal : public ASTWalker {
public:
llvm::DenseMap<Expr *, unsigned> &IndexMap;
unsigned Index = 0;
explicit RecordingTraversal(llvm::DenseMap<Expr *, unsigned> &indexMap)
: IndexMap(indexMap) { }
MacroWalking getMacroWalkingBehavior() const override {
return MacroWalking::ArgumentsAndExpansion;
}
PreWalkResult<Expr *> walkToExprPre(Expr *E) override {
IndexMap[E] = Index;
++Index;
return Action::Continue(E);
}
};
RecordingTraversal traversal(indexMap);
expr->walk(traversal);
}
bool ConstraintSystem::diagnoseAmbiguity(ArrayRef<Solution> solutions) {
// Produce a diff of the solutions.
SolutionDiff diff(solutions);
// Find the locators which have the largest numbers of distinct overloads.
std::optional<unsigned> bestOverload;
// Overloads are scored by lexicographical comparison of (# of distinct
// overloads, depth, *reverse* of the index). N.B. - cannot be used for the
// reversing: the score version of index == 0 should be > than that of 1, but
// -0 == 0 < UINT_MAX == -1, whereas ~0 == UINT_MAX > UINT_MAX - 1 == ~1.
auto score = [](unsigned depth, unsigned index, unsigned distinctOverloads) {
return std::make_tuple(depth, ~index, distinctOverloads);
};
auto bestScore = score(0, std::numeric_limits<unsigned>::max(), 0);
// Get a map of expressions to their depths and post-order traversal indices.
// Heuristically, all other things being equal, we should complain about the
// ambiguous expression that (1) is deepest, (2) comes earliest in the
// expression, or (3) has the most overloads.
llvm::DenseMap<Expr *, unsigned> indexMap;
for (auto expr : InputExprs) {
extendPreorderIndexMap(expr, indexMap);
}
for (unsigned i = 0, n = diff.overloads.size(); i != n; ++i) {
auto &overload = diff.overloads[i];
auto *locator = overload.locator;
// If there is only one overload difference, it's the best.
if (n == 1) {
bestOverload = i;
break;
}
// If there are multiple overload sets involved, let's pick the
// one that has choices with different types, because that is
// most likely the source of ambiguity.
{
auto overloadTy = getOverloadChoiceType(locator, solutions.front());
if (std::all_of(solutions.begin() + 1, solutions.end(),
[&](const Solution &solution) {
return overloadTy->isEqual(
getOverloadChoiceType(locator, solution));
}))
continue;
}
ASTNode anchor;
// Simplification of member locator would produce a base expression,
// this is what we want for diagnostics but not for comparisons here
// because base expression is located at a different depth which would
// lead to incorrect results if both reference and base expression are
// ambiguous e.g. `test[x].count` if both `[x]` and `count` are ambiguous
// than simplification of `count` would produce `[x]` which is incorrect.
if (locator->isLastElement<LocatorPathElt::Member>() ||
locator->isLastElement<LocatorPathElt::ConstructorMember>()) {
anchor = locator->getAnchor();
} else {
anchor = simplifyLocatorToAnchor(overload.locator);
}
// If we can't resolve the locator to an anchor with no path,
// we can't diagnose this well.
if (!anchor)
continue;
// Index and Depth is only applicable to expressions.
unsigned index = 0;
unsigned depth = 0;
if (auto *expr = getAsExpr(anchor)) {
auto it = indexMap.find(expr);
if (it == indexMap.end())
continue;
index = it->second;
auto optDepth = getExprDepth(expr);
if (!optDepth)
continue;
depth = *optDepth;
}
// If we don't have a name to hang on to, it'll be hard to diagnose this
// overload.
if (!getOverloadChoiceName(overload.choices))
continue;
unsigned distinctOverloads = countDistinctOverloads(overload.choices);
// We need at least two overloads to make this interesting.
if (distinctOverloads < 2)
continue;
// If we have more distinct overload choices for this locator than for
// prior locators, just keep this locator.
auto thisScore = score(depth, index, distinctOverloads);
if (thisScore > bestScore) {
bestScore = thisScore;
bestOverload = i;
continue;
}
// We have better results. Ignore this one.
}
// FIXME: Should be able to pick the best locator, e.g., based on some
// depth-first numbering of expressions.
if (bestOverload) {
auto &overload = diff.overloads[*bestOverload];
// FIXME: We would prefer to emit the name as written, but that information
// is not sufficiently centralized in the AST.
DeclNameRef name(getOverloadChoiceName(overload.choices));
auto anchor = simplifyLocatorToAnchor(overload.locator);
if (!anchor) {
// It's not clear that this is actually valid. Just use the overload's
// anchor for release builds, but assert so we can properly diagnose
// this case if it happens to be hit. Note that the overload will
// *always* be anchored, otherwise everything would be broken, ie. this
// assertion would be the least of our worries.
anchor = overload.locator->getAnchor();
assert(false && "locator could not be simplified to anchor");
}
// Emit the ambiguity diagnostic.
auto &DE = getASTContext().Diags;
DE.diagnose(getLoc(anchor),
name.isOperator() ? diag::ambiguous_operator_ref
: diag::ambiguous_decl_ref,
name);
TrailingClosureAmbiguityFailure failure(solutions, anchor,
overload.choices);
if (failure.diagnoseAsNote())
return true;
// Emit candidates. Use a SmallPtrSet to make sure only emit a particular
// candidate once. FIXME: Why is one candidate getting into the overload
// set multiple times? (See also tryDiagnoseTrailingClosureAmbiguity.)
SmallPtrSet<Decl *, 8> EmittedDecls;
for (auto choice : overload.choices) {
switch (choice.getKind()) {
case OverloadChoiceKind::Decl:
case OverloadChoiceKind::DeclViaDynamic:
case OverloadChoiceKind::DeclViaBridge:
case OverloadChoiceKind::DeclViaUnwrappedOptional: {
// FIXME: show deduced types, etc, etc.
auto decl = choice.getDecl();
if (EmittedDecls.insert(decl).second) {
auto declModule = decl->getDeclContext()->getParentModule();
bool printModuleName = declModule != DC->getParentModule();
DE.diagnose(decl, diag::found_candidate_in_module,
printModuleName, declModule);
}
break;
}
case OverloadChoiceKind::KeyPathApplication:
case OverloadChoiceKind::DynamicMemberLookup:
case OverloadChoiceKind::KeyPathDynamicMemberLookup:
// Skip key path applications and dynamic member lookups, since we don't
// want them to noise up unrelated subscript diagnostics.
break;
case OverloadChoiceKind::TupleIndex:
case OverloadChoiceKind::MaterializePack:
case OverloadChoiceKind::ExtractFunctionIsolation:
// FIXME: Actually diagnose something here.
break;
}
}
return true;
}
// FIXME: If we inferred different types for literals (for example),
// could diagnose ambiguity that way as well.
return false;
}
ConstraintLocator *
constraints::simplifyLocator(ConstraintSystem &cs, ConstraintLocator *locator,
SourceRange &range) {
auto path = locator->getPath();
auto anchor = locator->getAnchor();
simplifyLocator(anchor, path, range);
// If we didn't simplify anything, just return the input.
if (anchor == locator->getAnchor() &&
path.size() == locator->getPath().size()) {
return locator;
}
// If the old locator didn't have any summary flags, neither will the
// simplified version, as it must contain a subset of the path elements.
if (locator->getSummaryFlags() == 0)
return cs.getConstraintLocator(anchor, path, /*summaryFlags*/ 0);
return cs.getConstraintLocator(anchor, path);
}
void constraints::simplifyLocator(ASTNode &anchor,
ArrayRef<LocatorPathElt> &path,
SourceRange &range) {
range = SourceRange();
while (!path.empty()) {
switch (path[0].getKind()) {
case ConstraintLocator::ApplyArgument: {
auto *anchorExpr = castToExpr(anchor);
// If the next element is an ApplyArgToParam, we can simplify by looking
// into the index expression.
if (path.size() < 2)
break;
auto elt = path[1].getAs<LocatorPathElt::ApplyArgToParam>();
if (!elt)
break;
// If the 3rd element is an PackElement, add the index of pack element
// within packs to locate the correct element.
std::optional<unsigned> eltPackIdx;
if (path.size() > 2) {
if (auto eltPack = path[2].getAs<LocatorPathElt::PackElement>()) {
eltPackIdx = eltPack->getIndex();
}
}
// Extract application argument.
if (auto *args = anchorExpr->getArgs()) {
if (eltPackIdx.has_value()) {
if (elt->getArgIdx() + eltPackIdx.value() < args->size()) {
anchor = args->getExpr(elt->getArgIdx() + eltPackIdx.value());
path = path.slice(3);
continue;
}
} else if (elt->getArgIdx() < args->size()) {
anchor = args->getExpr(elt->getArgIdx());
path = path.slice(2);
continue;
}
}
break;
}
case ConstraintLocator::ApplyArgToParam:
llvm_unreachable("Cannot appear without ApplyArgument");
case ConstraintLocator::DynamicCallable: {
path = path.slice(1);
continue;
}
case ConstraintLocator::ApplyFunction:
case ConstraintLocator::FunctionResult:
// Extract application function.
if (auto applyExpr = getAsExpr<ApplyExpr>(anchor)) {
anchor = applyExpr->getFn();
path = path.slice(1);
continue;
}
// The subscript itself is the function.
if (auto subscriptExpr = getAsExpr<SubscriptExpr>(anchor)) {
anchor = subscriptExpr;
path = path.slice(1);
continue;
}
// If the anchor is an unapplied decl ref, there's nothing to extract.
if (isExpr<DeclRefExpr>(anchor) || isExpr<OverloadedDeclRefExpr>(anchor)) {
path = path.slice(1);
continue;
}
break;
case ConstraintLocator::AutoclosureResult:
case ConstraintLocator::LValueConversion:
case ConstraintLocator::DynamicType:
case ConstraintLocator::UnresolvedMember:
case ConstraintLocator::ImplicitCallAsFunction:
// Arguments in autoclosure positions, lvalue and rvalue adjustments,
// unresolved members, and implicit callAsFunction references are
// implicit.
path = path.slice(1);
continue;
case ConstraintLocator::TupleType:
case ConstraintLocator::GenericType:
path = path.slice(1);
continue;
case ConstraintLocator::NamedTupleElement:
case ConstraintLocator::TupleElement: {
// Extract tuple element.
auto elt = path[0].castTo<LocatorPathElt::AnyTupleElement>();
unsigned index = elt.getIndex();
if (auto *AE = getAsExpr<AssignExpr>(anchor)) {
if (isa<TupleExpr>(AE->getSrc())) {
anchor = AE->getSrc();
}
}
if (auto tupleExpr = getAsExpr<TupleExpr>(anchor)) {
if (index < tupleExpr->getNumElements()) {
anchor = tupleExpr->getElement(index);
path = path.slice(1);
continue;
}
}
if (auto *CE = getAsExpr<CollectionExpr>(anchor)) {
if (index < CE->getNumElements()) {
anchor = CE->getElement(index);
path = path.slice(1);
continue;
}
}
break;
}
case ConstraintLocator::ConstructorMember:
// Look through specialization first, because it doesn't play a
// functional role here.
if (auto *USE = getAsExpr<UnresolvedSpecializeExpr>(anchor)) {
anchor = USE->getSubExpr();
range = anchor.getSourceRange();
}
// - This is really an implicit 'init' MemberRef, so point at the base,
// i.e. the TypeExpr.
// - For re-declarations we'd get an overloaded reference
// with multiple choices for the same type.
if (isExpr<TypeExpr>(anchor) || isExpr<OverloadedDeclRefExpr>(anchor)) {
range = SourceRange();
path = path.slice(1);
continue;
}
LLVM_FALLTHROUGH;
case ConstraintLocator::Member:
if (auto UDE = getAsExpr<UnresolvedDotExpr>(anchor)) {
path = path.slice(1);
continue;
}
if (anchor.is<Pattern *>()) {
path = path.slice(1);
continue;
}
break;
case ConstraintLocator::MemberRefBase:
if (auto UDE = getAsExpr<UnresolvedDotExpr>(anchor)) {
range = UDE->getNameLoc().getSourceRange();
anchor = UDE->getBase();
path = path.slice(1);
continue;
}
if (anchor.is<Pattern *>()) {
path = path.slice(1);
continue;
}
break;
case ConstraintLocator::SubscriptMember:
if (isExpr<SubscriptExpr>(anchor)) {
path = path.slice(1);
continue;
}
break;
case ConstraintLocator::ClosureBody:
case ConstraintLocator::ClosureResult:
if (auto CE = getAsExpr<ClosureExpr>(anchor)) {
if (CE->hasSingleExpressionBody()) {
anchor = CE->getSingleExpressionBody();
path = path.slice(1);
continue;
}
}
break;
case ConstraintLocator::ClosureThrownError:
if (auto CE = getAsExpr<ClosureExpr>(anchor)) {
if (auto thrownTypeRepr = CE->getExplicitThrownTypeRepr()) {
anchor = thrownTypeRepr;
path = path.slice(1);
break;
}
}
break;
case ConstraintLocator::CoercionOperand: {
auto *CE = castToExpr<CoerceExpr>(anchor);
anchor = CE->getSubExpr()->getValueProvidingExpr();
path = path.slice(1);
// When in a argument function type on a coercion context
// look past the argument, because is just for identify the
// argument type that is being matched.
if (!path.empty() && path[0].is<LocatorPathElt::FunctionArgument>()) {
path = path.slice(1);
}
continue;
}
case ConstraintLocator::GlobalActorType:
case ConstraintLocator::ContextualType: {
// This was just for identifying purposes, strip it off.
path = path.slice(1);
continue;
}
case ConstraintLocator::KeyPathComponent: {
auto elt = path[0].castTo<LocatorPathElt::KeyPathComponent>();
// If the next element is an ApplyArgument, we can simplify by looking
// into the index expression.
if (path.size() < 3 ||
path[1].getKind() != ConstraintLocator::ApplyArgument)
break;
auto applyArgElt = path[2].getAs<LocatorPathElt::ApplyArgToParam>();
if (!applyArgElt)
break;
auto argIdx = applyArgElt->getArgIdx();
if (auto *kpe = getAsExpr<KeyPathExpr>(anchor)) {
auto component = kpe->getComponents()[elt.getIndex()];
auto *args = component.getSubscriptArgs();
assert(args && "Trying to apply a component without args?");
if (argIdx < args->size()) {
anchor = args->getExpr(argIdx);
path = path.slice(3);
continue;
}
}
break;
}
case ConstraintLocator::Condition: {
if (auto *condStmt = getAsStmt<LabeledConditionalStmt>(anchor)) {
anchor = &condStmt->getCond().front();
} else {
anchor = castToExpr<TernaryExpr>(anchor)->getCondExpr();
}
path = path.slice(1);
continue;
}
case ConstraintLocator::TernaryBranch: {
auto branch = path[0].castTo<LocatorPathElt::TernaryBranch>();
if (auto *ifStmt = getAsStmt<IfStmt>(anchor)) {
anchor =
branch.forThen() ? ifStmt->getThenStmt() : ifStmt->getElseStmt();
} else {
auto *ifExpr = castToExpr<TernaryExpr>(anchor);
anchor =
branch.forThen() ? ifExpr->getThenExpr() : ifExpr->getElseExpr();
}
path = path.slice(1);
continue;
}
case ConstraintLocator::SingleValueStmtResult: {
auto branchElt = path[0].castTo<LocatorPathElt::SingleValueStmtResult>();
auto exprIdx = branchElt.getIndex();
auto *SVE = castToExpr<SingleValueStmtExpr>(anchor);
SmallVector<Expr *, 4> scratch;
anchor = SVE->getResultExprs(scratch)[exprIdx];
path = path.slice(1);
continue;
}
case ConstraintLocator::KeyPathDynamicMember:
case ConstraintLocator::ImplicitDynamicMemberSubscript: {
// Key path dynamic member lookup should be completely transparent.
path = path.slice(1);
continue;
}
case ConstraintLocator::ArgumentAttribute: {
// At this point we should have already found argument expression
// this attribute belongs to, so we can leave this element in place
// because it points out exact location useful for diagnostics.
break;
}
case ConstraintLocator::ResultBuilderBodyResult: {
path = path.slice(1);
break;
}
case ConstraintLocator::UnresolvedMemberChainResult: {
auto *resultExpr = castToExpr<UnresolvedMemberChainResultExpr>(anchor);
anchor = resultExpr->getSubExpr();
path = path.slice(1);
continue;
}
case ConstraintLocator::SyntacticElement: {
auto bodyElt = path[0].castTo<LocatorPathElt::SyntacticElement>();
anchor = bodyElt.getElement();
path = path.slice(1);
continue;
}
case ConstraintLocator::PatternMatch: {
auto patternElt = path[0].castTo<LocatorPathElt::PatternMatch>();
anchor = patternElt.getPattern();
path = path.slice(1);
continue;
}
case ConstraintLocator::EnumPatternImplicitCastMatch: {
path = path.slice(1);
continue;
}
case ConstraintLocator::PackType:
case ConstraintLocator::ParentType:
case ConstraintLocator::KeyPathType:
case ConstraintLocator::InstanceType:
case ConstraintLocator::PlaceholderType:
case ConstraintLocator::SequenceElementType:
case ConstraintLocator::ConstructorMemberType:
case ConstraintLocator::ExistentialConstraintType:
case ConstraintLocator::ProtocolCompositionMemberType:
break;
case ConstraintLocator::GenericArgument:
case ConstraintLocator::FunctionArgument:
case ConstraintLocator::SynthesizedArgument:
break;
case ConstraintLocator::DynamicLookupResult:
case ConstraintLocator::KeyPathComponentResult:
break;
case ConstraintLocator::GenericParameter:
break;
case ConstraintLocator::ThrownErrorType:
break;
case ConstraintLocator::OpenedGeneric:
case ConstraintLocator::OpenedOpaqueArchetype:
break;
case ConstraintLocator::KeyPathRoot:
case ConstraintLocator::KeyPathValue:
break;
case ConstraintLocator::ProtocolRequirement:
case ConstraintLocator::ConditionalRequirement:
case ConstraintLocator::ConformanceRequirement:
case ConstraintLocator::TypeParameterRequirement:
break;
case ConstraintLocator::PackElement:
case ConstraintLocator::PackShape:
case ConstraintLocator::PackExpansionType:
break;
case ConstraintLocator::PackExpansionPattern: {
if (auto *expansion = getAsExpr<PackExpansionExpr>(anchor))
anchor = expansion->getPatternExpr();
path = path.slice(1);
break;
}
case ConstraintLocator::PatternBindingElement: {
auto pattern = path[0].castTo<LocatorPathElt::PatternBindingElement>();
auto *patternBinding = cast<PatternBindingDecl>(anchor.get<Decl *>());
anchor = patternBinding->getInit(pattern.getIndex());
// If this pattern is uninitialized, let's use it as anchor.
if (!anchor)
anchor = patternBinding->getPattern(pattern.getIndex());
path = path.slice(1);
continue;
}
case ConstraintLocator::NamedPatternDecl: {
auto pattern = cast<NamedPattern>(anchor.get<Pattern *>());
anchor = pattern->getDecl();
path = path.slice(1);
break;
}
case ConstraintLocator::AnyPatternDecl: {
// This element is just a marker for `_` pattern since it doesn't
// have a declaration. We need to make sure that it only appaears
// when anchored on `AnyPattern`.
assert(getAsPattern<AnyPattern>(anchor));
path = path.slice(1);
break;
}
case ConstraintLocator::ImplicitConversion:
break;
case ConstraintLocator::Witness:
case ConstraintLocator::WrappedValue:
case ConstraintLocator::OptionalPayload:
case ConstraintLocator::ImplicitlyUnwrappedDisjunctionChoice:
case ConstraintLocator::FallbackType:
case ConstraintLocator::KeyPathSubscriptIndex:
case ConstraintLocator::ExistentialMemberAccessConversion:
break;
}
// If we get here, we couldn't simplify the path further.
break;
}
}
ASTNode constraints::simplifyLocatorToAnchor(ConstraintLocator *locator) {
if (!locator)
return nullptr;
auto anchor = locator->getAnchor();
if (!anchor)
return {};
SourceRange range;
auto path = locator->getPath();
simplifyLocator(anchor, path, range);
// We only want the new anchor if all the path elements have been simplified
// away.
return path.empty() ? anchor : nullptr;
}
Expr *constraints::getArgumentExpr(ASTNode node, unsigned index) {
auto *expr = getAsExpr(node);
if (!expr)
return nullptr;
auto *argList = expr->getArgs();
if (!argList)
return nullptr;
if (index >= argList->size())
return nullptr;
return argList->getExpr(index);
}
bool constraints::isAutoClosureArgument(Expr *argExpr) {
if (!argExpr)
return false;
if (auto *DRE = dyn_cast<DeclRefExpr>(argExpr)) {
if (auto *param = dyn_cast<ParamDecl>(DRE->getDecl()))
return param->isAutoClosure();
}
return false;
}
bool constraints::hasAppliedSelf(ConstraintSystem &cs,
const OverloadChoice &choice) {
return hasAppliedSelf(choice, [&cs](Type type) -> Type {
return cs.getFixedTypeRecursive(type, /*wantRValue=*/true);
});
}
bool constraints::hasAppliedSelf(const OverloadChoice &choice,
llvm::function_ref<Type(Type)> getFixedType) {
auto *decl = choice.getDeclOrNull();
if (!decl)
return false;
auto baseType = choice.getBaseType();
if (baseType)
baseType = getFixedType(baseType)->getRValueType();
// In most cases where we reference a declaration with a curried self
// parameter, it gets dropped from the type of the reference.
return decl->hasCurriedSelf() &&
doesMemberRefApplyCurriedSelf(baseType, decl);
}
/// Check whether given type conforms to `RawRepresentable` protocol
/// and return the witness type.
Type constraints::isRawRepresentable(ConstraintSystem &cs, Type type) {
auto rawReprType = TypeChecker::getProtocol(
cs.getASTContext(), SourceLoc(), KnownProtocolKind::RawRepresentable);
if (!rawReprType)
return Type();
auto conformance = cs.lookupConformance(type, rawReprType);
if (conformance.isInvalid())
return Type();
return conformance.getTypeWitnessByName(type, cs.getASTContext().Id_RawValue);
}
void ConstraintSystem::generateOverloadConstraints(
SmallVectorImpl<Constraint *> &constraints, Type type,
ArrayRef<OverloadChoice> choices, DeclContext *useDC,
ConstraintLocator *locator, std::optional<unsigned> favoredIndex,
bool requiresFix,
llvm::function_ref<ConstraintFix *(unsigned, const OverloadChoice &)>
getFix) {
auto recordChoice = [&](SmallVectorImpl<Constraint *> &choices,
unsigned index, const OverloadChoice &overload,
bool isFavored = false) {
auto *fix = getFix(index, overload);
// If fix is required but it couldn't be determined, this
// choice has be filtered out.
if (requiresFix && !fix)
return;
auto *choice = Constraint::createBindOverload(*this, type, overload,
useDC, fix, locator);
if (isFavored)
choice->setFavored();
choices.push_back(choice);
};
if (favoredIndex) {
const auto &choice = choices[*favoredIndex];
assert(
(!choice.isDecl() || !isDeclUnavailable(choice.getDecl(), locator)) &&
"Cannot make unavailable decl favored!");
recordChoice(constraints, *favoredIndex, choice, /*isFavored=*/true);
}
for (auto index : indices(choices)) {
if (favoredIndex && (*favoredIndex == index))
continue;
recordChoice(constraints, index, choices[index]);
}
}
ConstraintLocator *
ConstraintSystem::getArgumentInfoLocator(ConstraintLocator *locator) {
auto anchor = locator->getAnchor();
// An empty locator which code completion uses for member references.
if (anchor.isNull() && locator->getPath().empty())
return nullptr;
if (locator->findLast<LocatorPathElt::ImplicitConversion>())
return locator;
// Applies and unresolved member exprs can have callee locators that are
// dependent on the type of their function, which may not have been resolved
// yet. Therefore we need to handle them specially.
if (auto *apply = getAsExpr<ApplyExpr>(anchor)) {
auto *fnExpr = getArgumentLabelTargetExpr(apply->getFn());
return getConstraintLocator(fnExpr);
}
if (auto *UME = getAsExpr<UnresolvedMemberExpr>(anchor))
return getConstraintLocator(UME);
// All implicit x[dynamicMember:] subscript calls can share the same argument
// list.
if (locator->findLast<LocatorPathElt::ImplicitDynamicMemberSubscript>()) {
return getConstraintLocator(
ASTNode(), LocatorPathElt::ImplicitDynamicMemberSubscript());
}
auto path = locator->getPath();
{
// If this is for a dynamic member reference, the argument info is for the
// original call-site, which we can get by stripping away the
// KeyPathDynamicMember elements.
auto iter = path.begin();
if (locator->findFirst<LocatorPathElt::KeyPathDynamicMember>(iter)) {
ArrayRef<LocatorPathElt> newPath(path.begin(), iter);
return getConstraintLocator(anchor, newPath);
}
}
return getCalleeLocator(locator);
}
ArgumentList *ConstraintSystem::getArgumentList(ConstraintLocator *locator) {
if (!locator)
return nullptr;
if (auto *infoLocator = getArgumentInfoLocator(locator)) {
auto known = ArgumentLists.find(infoLocator);
if (known != ArgumentLists.end())
return known->second;
}
return nullptr;
}
void ConstraintSystem::recordArgumentList(ConstraintLocator *locator,
ArgumentList *args) {
bool inserted = ArgumentLists.insert({locator, args}).second;
ASSERT(inserted);
if (solverState)
recordChange(SolverTrail::Change::RecordedArgumentList(locator));
}
void ConstraintSystem::associateArgumentList(ConstraintLocator *locator,
ArgumentList *args) {
ASSERT(locator && locator->getAnchor());
auto *argLoc = getArgumentInfoLocator(locator);
recordArgumentList(argLoc, args);
}
ArgumentList *Solution::getArgumentList(ConstraintLocator *locator) const {
if (!locator)
return nullptr;
if (auto *infoLocator = constraintSystem->getArgumentInfoLocator(locator)) {
auto known = argumentLists.find(infoLocator);
if (known != argumentLists.end())
return known->second;
}
return nullptr;
}
std::optional<ConversionRestrictionKind>
Solution::getConversionRestriction(CanType type1, CanType type2) const {
auto restriction = ConstraintRestrictions.find({type1, type2});
if (restriction != ConstraintRestrictions.end())
return restriction->second;
return std::nullopt;
}
#ifndef NDEBUG
/// Given an apply expr, returns true if it is expected to have a direct callee
/// overload, resolvable using `getChoiceFor`. Otherwise, returns false.
static bool shouldHaveDirectCalleeOverload(const CallExpr *callExpr) {
auto *fnExpr = callExpr->getDirectCallee();
// An apply of an apply/subscript doesn't have a direct callee.
if (isa<ApplyExpr>(fnExpr) || isa<SubscriptExpr>(fnExpr))
return false;
// Applies of closures don't have callee overloads.
if (isa<ClosureExpr>(fnExpr))
return false;
// No direct callee for a try!/try?.
if (isa<ForceTryExpr>(fnExpr) || isa<OptionalTryExpr>(fnExpr))
return false;
// If we have an intermediate cast, there's no direct callee.
if (isa<ExplicitCastExpr>(fnExpr))
return false;
// No direct callee for a ternary expr.
if (isa<TernaryExpr>(fnExpr))
return false;
// Assume that anything else would have a direct callee.
return true;
}
#endif
ASTNode ConstraintSystem::includingParentApply(ASTNode node) {
if (auto *expr = getAsExpr(node)) {
if (auto apply = getAsExpr<ApplyExpr>(getParentExpr(expr))) {
if (apply->getFn() == expr)
return apply;
}
}
return node;
}
Type Solution::resolveInterfaceType(Type type) const {
auto resolvedType = type.transformRec([&](Type type) -> std::optional<Type> {
if (auto *tvt = type->getAs<TypeVariableType>()) {
// If this type variable is for a generic parameter, return that.
if (auto *gp = tvt->getImpl().getGenericParameter())
return gp;
// Otherwise resolve its fixed type, mapped out of context.
auto fixed = simplifyType(tvt);
return resolveInterfaceType(fixed->mapTypeOutOfContext());
}
if (auto *dmt = type->getAs<DependentMemberType>()) {
// For a dependent member, first resolve the base.
auto newBase = resolveInterfaceType(dmt->getBase());
// Then reconstruct using its associated type.
assert(dmt->getAssocType());
return DependentMemberType::get(newBase, dmt->getAssocType());
}
return std::nullopt;
});
assert(!resolvedType->hasArchetype());
return resolvedType;
}
std::optional<FunctionArgApplyInfo>
Solution::getFunctionArgApplyInfo(ConstraintLocator *locator) const {
// It's only valid to use `&` in argument positions, but we need
// to figure out exactly where it was used.
if (auto *argExpr = getAsExpr<InOutExpr>(locator->getAnchor())) {
auto *argLoc = getConstraintSystem().getArgumentLocator(argExpr);
if (!argLoc)
return std::nullopt;
locator = argLoc;
}
auto anchor = locator->getAnchor();
auto path = locator->getPath();
// Look for the apply-arg-to-param element in the locator's path. We may
// have to look through other elements that are generated from an argument
// conversion such as GenericArgument for an optional-to-optional conversion,
// and OptionalPayload for a value-to-optional conversion.
auto iter = path.rbegin();
auto applyArgElt = locator->findLast<LocatorPathElt::ApplyArgToParam>(iter);
if (!applyArgElt)
return std::nullopt;
#ifndef NDEBUG
auto nextIter = iter + 1;
assert(!locator->findLast<LocatorPathElt::ApplyArgToParam>(nextIter) &&
"Multiple ApplyArgToParam components?");
#endif
// Form a new locator that ends at the apply-arg-to-param element, and
// simplify it to get the full argument expression.
auto argPath = path.drop_back(iter - path.rbegin());
auto *argLocator = getConstraintLocator(anchor, argPath);
auto *argExpr = castToExpr(simplifyLocatorToAnchor(argLocator));
// If we were unable to simplify down to the argument expression, we don't
// know what this is.
if (!argExpr)
return std::nullopt;
auto *argList = getArgumentList(argLocator);
if (!argList)
return std::nullopt;
std::optional<OverloadChoice> choice;
Type rawFnType;
auto *calleeLocator = getCalleeLocator(argLocator);
if (auto overload = getOverloadChoiceIfAvailable(calleeLocator)) {
// If we have resolved an overload for the callee, then use that to get the
// function type and callee.
choice = overload->choice;
rawFnType = overload->adjustedOpenedType;
} else {
// If we didn't resolve an overload for the callee, we should be dealing
// with a call of an arbitrary function expr.
auto *call = castToExpr<CallExpr>(anchor);
rawFnType = getType(call->getFn());
// If callee couldn't be resolved due to expression
// issues e.g. it's a reference to an invalid member
// let's just return here.
if (simplifyType(rawFnType)->is<UnresolvedType>())
return std::nullopt;
// A tuple construction is spelled in the AST as a function call, but
// is really more like a tuple conversion.
if (auto metaTy = simplifyType(rawFnType)->getAs<MetatypeType>()) {
if (metaTy->getInstanceType()->is<TupleType>())
return std::nullopt;
}
assert(!shouldHaveDirectCalleeOverload(call) &&
"Should we have resolved a callee for this?");
}
// Try to resolve the function type by loading lvalues and looking through
// optional types, which can occur for expressions like `fn?(5)`.
auto *fnType = simplifyType(rawFnType)
->getRValueType()
->lookThroughAllOptionalTypes()
->getAs<FunctionType>();
if (!fnType)
return std::nullopt;
// Resolve the interface type for the function. Note that this may not be a
// function type, for example it could be a generic parameter.
Type fnInterfaceType;
auto *callee = choice ? choice->getDeclOrNull() : nullptr;
if (callee && callee->hasInterfaceType()) {
// If we have a callee with an interface type, we can use it. This is
// preferable to resolveInterfaceType, as this will allow us to get a
// GenericFunctionType for generic decls.
//
// Note that it's possible to find a callee without an interface type. This
// can happen for example with closure parameters, where the interface type
// isn't set until the solution is applied. In that case, use
// resolveInterfaceType.
fnInterfaceType = callee->getInterfaceType();
// Strip off the curried self parameter if necessary.
if (hasAppliedSelf(
*choice, [this](Type type) -> Type { return simplifyType(type); }))
fnInterfaceType = fnInterfaceType->castTo<AnyFunctionType>()->getResult();
#ifndef NDEBUG
// If variadic generics are not involved, interface type should
// always match applied type.
if (auto *fn = fnInterfaceType->getAs<AnyFunctionType>()) {
if (llvm::none_of(fn->getParams(), [&](const auto ¶m) {
return param.getPlainType()->hasParameterPack();
})) {
assert(fn->getNumParams() == fnType->getNumParams() &&
"Parameter mismatch?");
}
}
#endif
} else {
fnInterfaceType = resolveInterfaceType(rawFnType);
}
auto argIdx = applyArgElt->getArgIdx();
auto paramIdx = applyArgElt->getParamIdx();
return FunctionArgApplyInfo::get(argList, argExpr, argIdx,
simplifyType(getType(argExpr)), paramIdx,
fnInterfaceType, fnType, callee);
}
bool constraints::isKnownKeyPathType(Type type) {
return type->isKeyPath() || type->isWritableKeyPath() ||
type->isReferenceWritableKeyPath() || type->isPartialKeyPath() ||
type->isAnyKeyPath();
}
bool constraints::isTypeErasedKeyPathType(Type type) {
assert(type);
if (type->isPartialKeyPath() || type->isAnyKeyPath())
return true;
if (!type->isExistentialType())
return false;
auto superclass = type->getSuperclass();
return superclass ? isTypeErasedKeyPathType(superclass) : false;
}
bool constraints::hasResultExpr(ClosureExpr *closure) {
auto &ctx = closure->getASTContext();
return evaluateOrDefault(ctx.evaluator, ClosureHasResultExprRequest{closure},
false);
}
Type constraints::getConcreteReplacementForProtocolSelfType(ValueDecl *member) {
auto *DC = member->getDeclContext();
if (!DC->getSelfProtocolDecl())
return Type();
GenericSignature signature;
if (auto *genericContext = member->getAsGenericContext()) {
signature = genericContext->getGenericSignature();
} else {
signature = DC->getGenericSignatureOfContext();
}
auto selfTy = DC->getSelfInterfaceType();
return signature->getConcreteType(selfTy);
}
static bool isOperator(Expr *expr, StringRef expectedName) {
auto name = getOperatorName(expr);
return name ? name->is(expectedName) : false;
}
std::optional<Identifier> constraints::getOperatorName(Expr *expr) {
ValueDecl *choice = nullptr;
if (auto *ODRE = dyn_cast_or_null<OverloadedDeclRefExpr>(expr)) {
choice = ODRE->getDecls().front();
} else if (auto *DRE = dyn_cast_or_null<DeclRefExpr>(expr)) {
choice = DRE->getDecl();
} else {
return std::nullopt;
}
if (auto *FD = dyn_cast_or_null<AbstractFunctionDecl>(choice))
return FD->getBaseIdentifier();
return std::nullopt;
}
bool constraints::isPatternMatchingOperator(ASTNode node) {
auto *expr = getAsExpr(node);
if (!expr) return false;
return isOperator(expr, "~=");
}
bool constraints::isStandardComparisonOperator(ASTNode node) {
auto *expr = getAsExpr(node);
if (!expr) return false;
if (auto opName = getOperatorName(expr)) {
return opName->isStandardComparisonOperator();
}
return false;
}
ConstraintLocator *ConstraintSystem::getArgumentLocator(Expr *expr) {
auto *application = getParentExpr(expr);
if (!application)
return nullptr;
// Drop all of the semantically insignificant exprs that might be wrapping an
// argument e.g. `test(((42)))`
while (application->getSemanticsProvidingExpr() == expr) {
application = getParentExpr(application);
if (!application)
return nullptr;
}
ArgumentList *argList = application->getArgs();
if (!argList && !isa<KeyPathExpr>(application))
return nullptr;
ConstraintLocator *loc = nullptr;
if (auto *KP = dyn_cast<KeyPathExpr>(application)) {
auto idx = KP->findComponentWithSubscriptArg(expr);
if (!idx)
return nullptr;
loc = getConstraintLocator(KP, {LocatorPathElt::KeyPathComponent(*idx)});
argList = KP->getComponents()[*idx].getSubscriptArgs();
} else {
loc = getConstraintLocator(application);
}
assert(argList);
auto argIdx = argList->findArgumentExpr(expr);
if (!argIdx)
return nullptr;
ParameterTypeFlags flags;
flags = flags.withInOut(argList->get(*argIdx).isInOut());
return getConstraintLocator(
loc, {LocatorPathElt::ApplyArgument(),
LocatorPathElt::ApplyArgToParam(*argIdx, *argIdx, flags)});
}
bool constraints::isOperatorArgument(ConstraintLocator *locator,
StringRef expectedOperator) {
if (!locator->findLast<LocatorPathElt::ApplyArgToParam>())
return false;
if (auto *AE = getAsExpr<ApplyExpr>(locator->getAnchor())) {
if (isa<PrefixUnaryExpr>(AE) || isa<BinaryExpr>(AE) ||
isa<PostfixUnaryExpr>(AE))
return expectedOperator.empty() ||
isOperator(AE->getFn(), expectedOperator);
}
return false;
}
bool constraints::isArgumentOfPatternMatchingOperator(
ConstraintLocator *locator) {
auto *binaryOp = getAsExpr<BinaryExpr>(locator->getAnchor());
if (!(binaryOp && binaryOp->isImplicit()))
return false;
return isPatternMatchingOperator(binaryOp->getFn());
}
bool constraints::isArgumentOfReferenceEqualityOperator(
ConstraintLocator *locator) {
return isOperatorArgument(locator, "===") ||
isOperatorArgument(locator, "!==");
}
bool ConstraintSystem::isArgumentOfImportedDecl(
ConstraintLocatorBuilder locator) {
SmallVector<LocatorPathElt, 4> path;
auto anchor = locator.getLocatorParts(path);
if (path.empty())
return false;
while (!path.empty()) {
const auto &last = path.back();
// Drop all of the `optional payload` or `generic argument`
// locator elements at the end of the path, they came from
// either value-to-optional promotion or optional-to-optional
// conversion.
if (last.is<LocatorPathElt::OptionalPayload>() ||
last.is<LocatorPathElt::GenericArgument>()) {
path.pop_back();
continue;
}
break;
}
auto *application = getCalleeLocator(getConstraintLocator(anchor, path));
auto overload = findSelectedOverloadFor(application);
if (!(overload && overload->choice.isDecl()))
return false;
auto *choice = overload->choice.getDecl();
return choice->hasClangNode();
}
ConversionEphemeralness
ConstraintSystem::isConversionEphemeral(ConversionRestrictionKind conversion,
ConstraintLocatorBuilder locator) {
switch (conversion) {
case ConversionRestrictionKind::ArrayToPointer:
case ConversionRestrictionKind::ArrayToCPointer:
case ConversionRestrictionKind::StringToPointer:
// Always ephemeral.
return ConversionEphemeralness::Ephemeral;
case ConversionRestrictionKind::InoutToPointer:
case ConversionRestrictionKind::InoutToCPointer: {
// Ephemeral, except if the expression is a reference to a global or
// static stored variable, or a directly accessed stored property on such a
// variable.
auto isDirectlyAccessedStoredVar = [&](ValueDecl *decl) -> bool {
auto *asd = dyn_cast_or_null<AbstractStorageDecl>(decl);
if (!asd)
return false;
// Check what access strategy is used for a read-write access. It must be
// direct-to-storage in order for the conversion to be non-ephemeral.
auto access = asd->getAccessStrategy(
AccessSemantics::Ordinary, AccessKind::ReadWrite,
DC->getParentModule(), DC->getResilienceExpansion(),
/*useOldABI=*/false);
return access.getKind() == AccessStrategy::Storage;
};
SourceRange range;
auto *argLoc = simplifyLocator(*this, getConstraintLocator(locator), range);
auto *subExpr =
castToExpr(argLoc->getAnchor())->getSemanticsProvidingExpr();
// Look through an InOutExpr if we have one. This is usually the case, but
// might not be if e.g we're applying an 'add missing &' fix.
if (auto *ioe = dyn_cast<InOutExpr>(subExpr))
subExpr = ioe->getSubExpr();
while (true) {
subExpr = subExpr->getSemanticsProvidingExpr();
// Look through force unwraps, which can be modelled as physical lvalue
// components.
if (auto *fve = dyn_cast<ForceValueExpr>(subExpr)) {
subExpr = fve->getSubExpr();
continue;
}
// Look through a member reference if it's directly accessed.
if (auto *ude = dyn_cast<UnresolvedDotExpr>(subExpr)) {
auto overload = findSelectedOverloadFor(ude);
// If we didn't find an overload, it hasn't been resolved yet.
if (!overload)
return ConversionEphemeralness::Unresolved;
// Tuple indices are always non-ephemeral.
auto *base = ude->getBase();
if (overload->choice.getKind() == OverloadChoiceKind::TupleIndex) {
subExpr = base;
continue;
}
// If we don't have a directly accessed declaration associated with the
// choice, it's ephemeral.
auto *member = overload->choice.getDeclOrNull();
if (!isDirectlyAccessedStoredVar(member))
return ConversionEphemeralness::Ephemeral;
// If we found a static member, the conversion is non-ephemeral. We can
// stop iterating as there's nothing interesting about the base.
if (member->isStatic())
return ConversionEphemeralness::NonEphemeral;
// For an instance member, the base must be an @lvalue struct type.
if (auto *lvt = simplifyType(getType(base))->getAs<LValueType>()) {
auto *nominal = lvt->getObjectType()->getAnyNominal();
if (isa_and_nonnull<StructDecl>(nominal)) {
subExpr = base;
continue;
}
}
return ConversionEphemeralness::Ephemeral;
}
break;
}
auto getBaseEphemeralness =
[&](ValueDecl *base) -> ConversionEphemeralness {
// We must have a base decl that's directly accessed.
if (!isDirectlyAccessedStoredVar(base))
return ConversionEphemeralness::Ephemeral;
// The base decl must either be static or global in order for it to be
// non-ephemeral.
if (base->isStatic() || base->getDeclContext()->isModuleScopeContext()) {
return ConversionEphemeralness::NonEphemeral;
} else {
return ConversionEphemeralness::Ephemeral;
}
};
// Fast path: We have a direct decl ref.
if (auto *dre = dyn_cast<DeclRefExpr>(subExpr))
return getBaseEphemeralness(dre->getDecl());
// Otherwise, try to find an overload for the base.
if (auto baseOverload = findSelectedOverloadFor(subExpr))
return getBaseEphemeralness(baseOverload->choice.getDeclOrNull());
// If we didn't find a base overload for a unresolved member or overloaded
// decl, it hasn't been resolved yet.
if (isa<UnresolvedMemberExpr>(subExpr) ||
isa<OverloadedDeclRefExpr>(subExpr))
return ConversionEphemeralness::Unresolved;
// Otherwise, we don't know what we're dealing with. Default to ephemeral.
return ConversionEphemeralness::Ephemeral;
}
case ConversionRestrictionKind::DeepEquality:
case ConversionRestrictionKind::Superclass:
case ConversionRestrictionKind::Existential:
case ConversionRestrictionKind::MetatypeToExistentialMetatype:
case ConversionRestrictionKind::ExistentialMetatypeToMetatype:
case ConversionRestrictionKind::ValueToOptional:
case ConversionRestrictionKind::OptionalToOptional:
case ConversionRestrictionKind::ClassMetatypeToAnyObject:
case ConversionRestrictionKind::ExistentialMetatypeToAnyObject:
case ConversionRestrictionKind::ProtocolMetatypeToProtocolClass:
case ConversionRestrictionKind::PointerToPointer:
case ConversionRestrictionKind::PointerToCPointer:
case ConversionRestrictionKind::ArrayUpcast:
case ConversionRestrictionKind::DictionaryUpcast:
case ConversionRestrictionKind::SetUpcast:
case ConversionRestrictionKind::HashableToAnyHashable:
case ConversionRestrictionKind::CFTollFreeBridgeToObjC:
case ConversionRestrictionKind::ObjCTollFreeBridgeToCF:
case ConversionRestrictionKind::CGFloatToDouble:
case ConversionRestrictionKind::DoubleToCGFloat:
// @_nonEphemeral has no effect on these conversions, so treat them as all
// being non-ephemeral in order to allow their passing to an @_nonEphemeral
// parameter.
return ConversionEphemeralness::NonEphemeral;
}
llvm_unreachable("invalid conversion restriction kind");
}
Expr *ConstraintSystem::buildAutoClosureExpr(Expr *expr,
FunctionType *closureType,
DeclContext *ClosureContext,
bool isDefaultWrappedValue,
bool isAsyncLetWrapper) {
auto &Context = DC->getASTContext();
bool isInDefaultArgumentContext = false;
if (auto *init = dyn_cast<Initializer>(DC)) {
auto initKind = init->getInitializerKind();
isInDefaultArgumentContext =
initKind == InitializerKind::DefaultArgument ||
(initKind == InitializerKind::PatternBinding && isDefaultWrappedValue);
}
auto info = closureType->getExtInfo();
auto newClosureType = closureType;
if (isInDefaultArgumentContext && info.isNoEscape())
newClosureType = closureType->withExtInfo(info.withNoEscape(false))
->castTo<FunctionType>();
auto *closure = new (Context)
AutoClosureExpr(expr, newClosureType, ClosureContext);
closure->setParameterList(ParameterList::createEmpty(Context));
if (isAsyncLetWrapper)
closure->setThunkKind(AutoClosureExpr::Kind::AsyncLet);
Expr *result = closure;
if (!newClosureType->isEqual(closureType)) {
assert(isInDefaultArgumentContext);
assert(newClosureType
->withExtInfo(newClosureType->getExtInfo().withNoEscape(true))
->isEqual(closureType));
result = new (Context) FunctionConversionExpr(closure, closureType);
}
cacheExprTypes(result);
return result;
}
Expr *ConstraintSystem::buildTypeErasedExpr(Expr *expr, DeclContext *dc,
Type contextualType,
ContextualTypePurpose purpose) {
if (purpose != CTP_ReturnStmt)
return expr;
auto *decl = dyn_cast_or_null<ValueDecl>(dc->getAsDecl());
if (!decl ||
(!Context.LangOpts.hasFeature(Feature::OpaqueTypeErasure) &&
!(decl->isDynamic() || decl->getDynamicallyReplacedDecl())))
return expr;
auto *opaque = contextualType->getAs<OpaqueTypeArchetypeType>();
if (!opaque)
return expr;
auto protocols = opaque->getConformsTo();
if (protocols.size() != 1)
return expr;
auto *PD = protocols.front();
auto *attr = PD->getAttrs().getAttribute<TypeEraserAttr>();
if (!attr)
return expr;
auto typeEraser = attr->getResolvedType(PD);
assert(typeEraser && "Failed to resolve eraser type!");
auto &ctx = dc->getASTContext();
auto *argList = ArgumentList::forImplicitSingle(ctx, ctx.Id_erasing, expr);
return CallExpr::createImplicit(
ctx, TypeExpr::createImplicit(typeEraser, ctx), argList);
}
/// If an UnresolvedDotExpr, SubscriptMember, etc has been resolved by the
/// constraint system, return the decl that it references.
ValueDecl *ConstraintSystem::findResolvedMemberRef(ConstraintLocator *locator) {
// See if we have a resolution for this member.
auto overload = findSelectedOverloadFor(locator);
if (!overload)
return nullptr;
// We only want to handle the simplest decl binding.
auto choice = overload->choice;
if (choice.getKind() != OverloadChoiceKind::Decl)
return nullptr;
return choice.getDecl();
}
void SyntacticElementTargetKey::dump() const { dump(llvm::errs()); }
void SyntacticElementTargetKey::dump(raw_ostream &OS) const {
switch (kind) {
case Kind::empty:
OS << "<empty>\n";
return;
case Kind::tombstone:
OS << "<tombstone>\n";
return;
case Kind::stmtCondElement:
// TODO: Implement a proper dump function for StmtConditionElement
OS << "statement condition element\n";
return;
case Kind::expr:
case Kind::closure:
storage.expr->dump(OS);
return;
case Kind::stmt:
storage.stmt->dump(OS);
return;
case Kind::pattern:
storage.pattern->dump(OS);
return;
case Kind::patternBindingEntry:
OS << "pattern binding entry " << storage.patternBindingEntry.index
<< " in\n";
storage.patternBindingEntry.patternBinding->dump(OS);
return;
case Kind::varDecl:
storage.varDecl->dump(OS);
return;
case Kind::functionRef:
OS << "<function>\n";
storage.functionRef->printContext(OS);
return;
}
llvm_unreachable("invalid statement kind");
}
/// Given a specific expression and the remnants of the failed constraint
/// system, produce a specific diagnostic.
///
/// This is guaranteed to always emit an error message.
///
void ConstraintSystem::diagnoseFailureFor(SyntacticElementTarget target) {
setPhase(ConstraintSystemPhase::Diagnostics);
SWIFT_DEFER { setPhase(ConstraintSystemPhase::Finalization); };
auto &DE = getASTContext().Diags;
// If constraint system is in invalid state always produce
// a fallback diagnostic that asks to file a bug.
if (inInvalidState()) {
DE.diagnose(target.getLoc(), diag::failed_to_produce_diagnostic);
return;
}
if (auto expr = target.getAsExpr()) {
if (auto *assignment = dyn_cast<AssignExpr>(expr)) {
if (isa<DiscardAssignmentExpr>(assignment->getDest()))
expr = assignment->getSrc();
}
// Look through RebindSelfInConstructorExpr to avoid weird Sema issues.
if (auto *RB = dyn_cast<RebindSelfInConstructorExpr>(expr))
expr = RB->getSubExpr();
// Unresolved/Anonymous ClosureExprs are common enough that we should give
// them tailored diagnostics.
if (auto *closure = dyn_cast<ClosureExpr>(expr->getValueProvidingExpr())) {
DE.diagnose(closure->getLoc(), diag::cannot_infer_closure_type)
.highlight(closure->getSourceRange());
return;
}
// If no one could find a problem with this expression or constraint system,
// then it must be well-formed... but is ambiguous. Handle this by
// diagnostic various cases that come up.
DE.diagnose(expr->getLoc(), diag::type_of_expression_is_ambiguous)
.highlight(expr->getSourceRange());
} else if (auto *wrappedVar = target.getAsUninitializedWrappedVar()) {
auto *outerWrapper = wrappedVar->getOutermostAttachedPropertyWrapper();
Type propertyType = wrappedVar->getInterfaceType();
Type wrapperType = outerWrapper->getType();
// Emit the property wrapper fallback diagnostic
wrappedVar->diagnose(diag::property_wrapper_incompatible_property,
propertyType, wrapperType);
if (auto nominal = wrapperType->getAnyNominal()) {
nominal->diagnose(diag::property_wrapper_declared_here,
nominal->getName());
}
} else if (auto *var = target.getAsUninitializedVar()) {
DE.diagnose(target.getLoc(), diag::failed_to_produce_diagnostic);
} else if (target.isForEachPreamble()) {
DE.diagnose(target.getLoc(), diag::failed_to_produce_diagnostic);
} else {
// Emit a poor fallback message.
DE.diagnose(target.getAsFunction()->getLoc(),
diag::failed_to_produce_diagnostic);
}
}
bool ConstraintSystem::isDeclUnavailable(const Decl *D,
ConstraintLocator *locator) const {
SourceLoc loc;
if (locator) {
if (auto anchor = locator->getAnchor())
loc = getLoc(anchor);
}
return getUnsatisfiedAvailabilityConstraint(D, DC, loc).has_value();
}
bool ConstraintSystem::isConformanceUnavailable(ProtocolConformanceRef conformance,
ConstraintLocator *locator) const {
if (!conformance.isConcrete())
return false;
auto *concrete = conformance.getConcrete();
auto *rootConf = concrete->getRootConformance();
auto *ext = dyn_cast<ExtensionDecl>(rootConf->getDeclContext());
if (ext == nullptr)
return false;
return isDeclUnavailable(ext, locator);
}
/// If we aren't certain that we've emitted a diagnostic, emit a fallback
/// diagnostic.
void ConstraintSystem::maybeProduceFallbackDiagnostic(
SyntacticElementTarget target) const {
if (Options.contains(ConstraintSystemFlags::SuppressDiagnostics))
return;
// Before producing fatal error here, let's check if there are any "error"
// diagnostics already emitted or waiting to be emitted. Because they are
// a better indication of the problem.
ASTContext &ctx = getASTContext();
if (ctx.hadError() ||
(diagnosticTransaction && diagnosticTransaction->hasErrors()))
return;
ctx.Diags.diagnose(target.getLoc(), diag::failed_to_produce_diagnostic);
}
SourceLoc constraints::getLoc(ASTNode anchor) {
if (auto *E = anchor.dyn_cast<Expr *>()) {
return E->getLoc();
} else if (auto *T = anchor.dyn_cast<TypeRepr *>()) {
return T->getLoc();
} else if (auto *V = anchor.dyn_cast<Decl *>()) {
if (auto VD = dyn_cast<VarDecl>(V))
return VD->getNameLoc();
return anchor.getStartLoc();
} else if (auto *S = anchor.dyn_cast<Stmt *>()) {
return S->getStartLoc();
} else if (auto *P = anchor.dyn_cast<Pattern *>()) {
return P->getLoc();
} else if (auto *C = anchor.dyn_cast<StmtConditionElement *>()) {
return C->getStartLoc();
} else {
auto *I = anchor.get<CaseLabelItem *>();
return I->getStartLoc();
}
}
SourceRange constraints::getSourceRange(ASTNode anchor) {
return anchor.getSourceRange();
}
static std::optional<Requirement>
getRequirement(ConstraintSystem &cs, ConstraintLocator *reqLocator) {
ArrayRef<LocatorPathElt> path = reqLocator->getPath();
// If we have something like ... -> type req # -> pack element #, we're
// solving a requirement of the form T : P where T is a type parameter pack
if (!path.empty() && path.back().is<LocatorPathElt::PackElement>())
path = path.drop_back();
if (path.empty())
return std::nullopt;
auto reqLoc = path.back().getAs<LocatorPathElt::AnyRequirement>();
if (!reqLoc)
return std::nullopt;
if (reqLoc->isConditionalRequirement()) {
auto conformanceRef =
reqLocator->findLast<LocatorPathElt::ConformanceRequirement>();
assert(conformanceRef && "Invalid locator for a conditional requirement");
auto conformance = conformanceRef->getConformance();
return conformance->getConditionalRequirements()[reqLoc->getIndex()];
}
if (auto openedGeneric =
reqLocator->findLast<LocatorPathElt::OpenedGeneric>()) {
auto signature = openedGeneric->getSignature();
return signature.getRequirements()[reqLoc->getIndex()];
}
return std::nullopt;
}
static std::optional<std::pair<GenericTypeParamType *, RequirementKind>>
getRequirementInfo(ConstraintSystem &cs, ConstraintLocator *reqLocator) {
auto requirement = getRequirement(cs, reqLocator);
if (!requirement)
return std::nullopt;
auto *GP = requirement->getFirstType()->getAs<GenericTypeParamType>();
if (!GP)
return std::nullopt;
auto path = reqLocator->getPath();
auto iter = path.rbegin();
auto openedGeneric =
reqLocator->findLast<LocatorPathElt::OpenedGeneric>(iter);
assert(openedGeneric);
(void)openedGeneric;
auto newPath = path.drop_back(iter - path.rbegin() + 1);
auto *baseLoc = cs.getConstraintLocator(reqLocator->getAnchor(), newPath);
auto substitutions = cs.getOpenedTypes(baseLoc);
auto replacement =
llvm::find_if(substitutions, [&GP](const OpenedType &entry) {
auto *typeVar = entry.second;
return typeVar->getImpl().getGenericParameter() == GP;
});
if (replacement == substitutions.end())
return std::nullopt;
auto *repr = cs.getRepresentative(replacement->second);
return std::make_pair(repr->getImpl().getGenericParameter(),
requirement->getKind());
}
bool ConstraintSystem::isFixedRequirement(ConstraintLocator *reqLocator,
Type requirementTy) {
if (auto reqInfo = getRequirementInfo(*this, reqLocator)) {
auto *GP = reqInfo->first;
auto reqKind = static_cast<unsigned>(reqInfo->second);
return FixedRequirements.count(
std::make_tuple(GP, reqKind, requirementTy.getPointer()));
}
return false;
}
void ConstraintSystem::recordFixedRequirement(ConstraintLocator *reqLocator,
Type requirementTy) {
if (auto reqInfo = getRequirementInfo(*this, reqLocator)) {
auto *GP = reqInfo->first;
auto reqKind = static_cast<unsigned>(reqInfo->second);
recordFixedRequirement(GP, reqKind, requirementTy);
}
}
void ConstraintSystem::recordFixedRequirement(GenericTypeParamType *GP,
unsigned reqKind,
Type requirementTy) {
bool inserted = FixedRequirements.insert(
std::make_tuple(GP, reqKind, requirementTy.getPointer())).second;
if (inserted) {
if (solverState) {
recordChange(SolverTrail::Change::AddedFixedRequirement(
GP, reqKind, requirementTy));
}
}
}
void ConstraintSystem::removeFixedRequirement(GenericTypeParamType *GP,
unsigned reqKind,
Type requirementTy) {
auto key = std::make_tuple(GP, reqKind, requirementTy.getPointer());
bool erased = FixedRequirements.erase(key);
ASSERT(erased);
}
bool ConstraintSystem::isReadOnlyKeyPathComponent(
const AbstractStorageDecl *storage, SourceLoc referenceLoc) {
// See whether key paths can store to this component. (Key paths don't
// get any special power from being formed in certain contexts, such
// as the ability to assign to `let`s in initialization contexts, so
// we pass null for the DC to `isSettable` here.)
if (!getASTContext().isSwiftVersionAtLeast(5)) {
// As a source-compatibility measure, continue to allow
// WritableKeyPaths to be formed in the same conditions we did
// in previous releases even if we should not be able to set
// the value in this context.
if (!storage->isSettableInSwift(DC)) {
// A non-settable component makes the key path read-only, unless
// a reference-writable component shows up later.
return true;
}
} else if (!storage->isSettableInSwift(nullptr) ||
!storage->isSetterAccessibleFrom(DC)) {
// A non-settable component makes the key path read-only, unless
// a reference-writable component shows up later.
return true;
}
// If the setter is unavailable, then the keypath ought to be read-only
// in this context.
if (auto setter = storage->getOpaqueAccessor(AccessorKind::Set)) {
// FIXME: Fully unavailable setters should cause the key path to be
// readonly too.
auto constraint =
getUnsatisfiedAvailabilityConstraint(setter, DC, referenceLoc);
if (constraint && constraint->isConditionallySatisfiable())
return true;
}
return false;
}
bool ConstraintSystem::isArgumentGenericFunction(Type argType, Expr *argExpr) {
// Only makes sense if the argument type involves type variables somehow.
if (!argType->hasTypeVariable())
return false;
// Have we bound an overload for the argument already?
if (argExpr) {
auto locator = getConstraintLocator(argExpr);
auto knownOverloadBinding = ResolvedOverloads.find(locator);
if (knownOverloadBinding != ResolvedOverloads.end()) {
// If the overload choice is a generic function, then we have a generic
// function reference.
auto choice = knownOverloadBinding->second;
if (auto func = dyn_cast_or_null<AbstractFunctionDecl>(
choice.choice.getDeclOrNull())) {
if (func->isGeneric())
return true;
}
return false;
}
}
// We might have a type variable referring to an overload set.
auto argTypeVar = argType->getAs<TypeVariableType>();
if (!argTypeVar)
return false;
auto disjunction = getUnboundBindOverloadDisjunction(argTypeVar);
if (!disjunction)
return false;
for (auto constraint : disjunction->getNestedConstraints()) {
auto *decl = constraint->getOverloadChoice().getDeclOrNull();
if (!decl)
continue;
if (auto func = dyn_cast<AbstractFunctionDecl>(decl))
if (func->isGeneric())
return true;
}
return false;
}
ProtocolConformanceRef
ConstraintSystem::lookupConformance(Type type, ProtocolDecl *protocol) {
auto cacheKey = std::make_pair(type.getPointer(), protocol);
auto cachedConformance = Conformances.find(cacheKey);
if (cachedConformance != Conformances.end())
return cachedConformance->second;
auto conformance =
swift::lookupConformance(type, protocol, /*allowMissing=*/true);
Conformances[cacheKey] = conformance;
return conformance;
}
std::pair<bool, std::optional<KeyPathCapability>>
ConstraintSystem::inferKeyPathLiteralCapability(TypeVariableType *keyPathType) {
auto *typeLocator = keyPathType->getImpl().getLocator();
assert(typeLocator->isLastElement<LocatorPathElt::KeyPathType>());
auto *keyPath = castToExpr<KeyPathExpr>(typeLocator->getAnchor());
return inferKeyPathLiteralCapability(keyPath);
}
std::pair<bool, std::optional<KeyPathCapability>>
ConstraintSystem::inferKeyPathLiteralCapability(KeyPathExpr *keyPath) {
bool didOptionalChain = false;
bool isSendable = true;
auto fail = []() -> std::pair<bool, std::optional<KeyPathCapability>> {
return std::make_pair(false, std::nullopt);
};
auto delay = []() -> std::pair<bool, std::optional<KeyPathCapability>> {
return std::make_pair(true, std::nullopt);
};
auto success =
[](KeyPathMutability mutability,
bool isSendable) -> std::pair<bool, std::optional<KeyPathCapability>> {
KeyPathCapability capability(mutability, isSendable);
return std::make_pair(true, capability);
};
if (keyPath->hasSingleInvalidComponent())
return fail();
// If root is determined to be a hole it means that none of the components
// are resolvable and key path is not viable.
auto rootTy =
getFixedTypeRecursive(getKeyPathRootType(keyPath), /*wantRValue=*/false);
if (rootTy->isPlaceholder())
return fail();
auto mutability = KeyPathMutability::Writable;
for (unsigned i : indices(keyPath->getComponents())) {
auto &component = keyPath->getComponents()[i];
switch (component.getKind()) {
case KeyPathExpr::Component::Kind::Invalid:
case KeyPathExpr::Component::Kind::Identity:
break;
case KeyPathExpr::Component::Kind::CodeCompletion: {
return fail();
}
case KeyPathExpr::Component::Kind::UnresolvedSubscript:
case KeyPathExpr::Component::Kind::Subscript: {
if (Context.LangOpts.hasFeature(Feature::InferSendableFromCaptures)) {
// Key path is sendable only when all of its captures are sendable.
if (auto *args = component.getSubscriptArgs()) {
auto *sendable = Context.getProtocol(KnownProtocolKind::Sendable);
for (const auto &arg : *args) {
// No need to check more or delay since we already known
// that the type is not Sendable.
if (!isSendable)
break;
auto argTy = simplifyType(getType(arg.getExpr()));
// Sendability cannot be determined until the argument
// is fully resolved.
if (argTy->hasTypeVariable())
return delay();
auto conformance = lookupConformance(argTy, sendable);
isSendable &=
bool(conformance) &&
!conformance.hasMissingConformance();
}
}
}
LLVM_FALLTHROUGH;
}
case KeyPathExpr::Component::Kind::Property:
case KeyPathExpr::Component::Kind::UnresolvedProperty: {
auto *componentLoc =
getConstraintLocator(keyPath, LocatorPathElt::KeyPathComponent(i));
auto *calleeLoc = getCalleeLocator(componentLoc);
auto overload = findSelectedOverloadFor(calleeLoc);
if (!overload) {
// If overload cannot be found because member is missing,
// that's a failure.
if (hasFixFor(componentLoc, FixKind::DefineMemberBasedOnUse))
return fail();
return delay();
}
// tuple elements do not change the capability of the key path
auto choice = overload->choice;
if (choice.getKind() == OverloadChoiceKind::TupleIndex) {
continue;
}
// Discarded unsupported non-decl member lookups.
if (!choice.isDecl())
return fail();
auto storage = dyn_cast<AbstractStorageDecl>(choice.getDecl());
if (hasFixFor(componentLoc, FixKind::AllowInvalidRefInKeyPath) ||
hasFixFor(componentLoc, FixKind::UnwrapOptionalBase) ||
hasFixFor(componentLoc,
FixKind::UnwrapOptionalBaseWithOptionalResult))
return fail();
if (!storage)
return fail();
switch (getActorIsolation(storage)) {
case ActorIsolation::Unspecified:
case ActorIsolation::Nonisolated:
case ActorIsolation::NonisolatedUnsafe:
break;
case ActorIsolation::Erased:
llvm_unreachable("storage cannot have opaque isolation");
// A reference to an actor isolated state make key path non-Sendable.
case ActorIsolation::ActorInstance:
case ActorIsolation::GlobalActor:
isSendable = false;
break;
}
if (isReadOnlyKeyPathComponent(storage, component.getLoc())) {
mutability = KeyPathMutability::ReadOnly;
continue;
}
// A nonmutating setter indicates a reference-writable base.
if (!storage->isSetterMutating()) {
mutability = KeyPathMutability::ReferenceWritable;
continue;
}
// Otherwise, the key path maintains its current capability.
break;
}
case KeyPathExpr::Component::Kind::OptionalChain:
didOptionalChain = true;
break;
case KeyPathExpr::Component::Kind::OptionalForce:
// Forcing an optional preserves its lvalue-ness.
break;
case KeyPathExpr::Component::Kind::OptionalWrap:
// An optional chain should already have been recorded.
assert(didOptionalChain);
break;
case KeyPathExpr::Component::Kind::TupleElement:
llvm_unreachable("not implemented");
break;
case KeyPathExpr::Component::Kind::DictionaryKey:
llvm_unreachable("DictionaryKey only valid in #keyPath");
break;
}
}
// Optional chains force the entire key path to be read-only.
if (didOptionalChain)
mutability = KeyPathMutability::ReadOnly;
return success(mutability, isSendable);
}
TypeVarBindingProducer::TypeVarBindingProducer(BindingSet &bindings)
: BindingProducer(bindings.getConstraintSystem(),
bindings.getTypeVariable()->getImpl().getLocator()),
TypeVar(bindings.getTypeVariable()), CanBeNil(bindings.canBeNil()) {
if (bindings.isDirectHole()) {
auto *locator = getLocator();
// If this type variable is associated with a code completion token
// and it failed to infer any bindings let's adjust holes's locator
// to point to a code completion token to avoid attempting to "fix"
// this problem since its rooted in the fact that constraint system
// is under-constrained.
if (bindings.getAssociatedCodeCompletionToken()) {
locator =
CS.getConstraintLocator(bindings.getAssociatedCodeCompletionToken());
}
Bindings.push_back(Binding::forHole(TypeVar, locator));
return;
}
// A binding to `Any` which should always be considered as a last resort.
std::optional<Binding> Any;
auto addBinding = [&](const Binding &binding) {
// Adjust optionality of existing bindings based on presence of
// `ExpressibleByNilLiteral` requirement.
if (requiresOptionalAdjustment(binding)) {
Bindings.push_back(
binding.withType(OptionalType::get(binding.BindingType)));
} else if (binding.BindingType->isAny()) {
Any.emplace(binding);
} else {
Bindings.push_back(binding);
}
};
if (TypeVar->getImpl().isPackExpansion()) {
SmallVector<Binding> viableBindings;
// Collect possible contextual types (keep in mind that pack
// expansion type variable gets bound to its "opened" type
// regardless). To be viable the binding has to come from `bind`
// or `equal` constraint (i.e. same-type constraint or explicit
// generic argument) and be fully resolved.
llvm::copy_if(bindings.Bindings, std::back_inserter(viableBindings),
[&](const Binding &binding) {
auto *source = binding.getSource();
if (source->getKind() == ConstraintKind::Bind ||
source->getKind() == ConstraintKind::Equal) {
auto type = binding.BindingType;
return type->is<PackExpansionType>() &&
!type->hasTypeVariable();
}
return false;
});
// If there is a single fully resolved contextual type, let's
// use it as a binding to help with performance and diagnostics.
if (viableBindings.size() == 1) {
addBinding(viableBindings.front());
} else {
for (const auto &entry : bindings.Defaults) {
auto *constraint = entry.second;
Bindings.push_back(getDefaultBinding(constraint));
}
}
return;
}
for (const auto &binding : bindings.Bindings) {
addBinding(binding);
}
// Infer defaults based on "uncovered" literal protocol requirements.
for (const auto &info : bindings.Literals) {
const auto &literal = info.second;
if (!literal.viableAsBinding())
continue;
// We need to figure out whether this is a direct conformance
// requirement or inferred transitive one to identify binding
// kind correctly.
addBinding({literal.getDefaultType(),
literal.isDirectRequirement() ? BindingKind::Subtypes
: BindingKind::Supertypes,
literal.getSource()});
}
// Let's always consider `Any` to be a last resort binding because
// it's always better to infer concrete type and erase it if required
// by the context.
if (Any) {
Bindings.push_back(*Any);
}
{
bool noBindings = Bindings.empty();
for (const auto &entry : bindings.Defaults) {
auto *constraint = entry.second;
if (noBindings) {
// If there are no direct or transitive bindings to attempt
// let's add defaults to the list right away.
Bindings.push_back(getDefaultBinding(constraint));
} else {
// Otherwise let's delay attempting default bindings
// until all of the direct & transitive bindings and
// their derivatives have been attempted.
DelayedDefaults.push_back(constraint);
}
}
}
}
bool TypeVarBindingProducer::requiresOptionalAdjustment(
const Binding &binding) const {
// If type variable can't be `nil` then adjustment is
// not required.
if (!CanBeNil)
return false;
if (binding.Kind == BindingKind::Supertypes) {
auto type = binding.BindingType->getRValueType();
// If the type doesn't conform to ExpressibleByNilLiteral,
// produce an optional of that type as a potential binding. We
// overwrite the binding in place because the non-optional type
// will fail to type-check against the nil-literal conformance.
auto *proto = CS.getASTContext().getProtocol(
KnownProtocolKind::ExpressibleByNilLiteral);
return !CS.lookupConformance(type, proto);
} else if (binding.isDefaultableBinding() && binding.BindingType->isAny()) {
return true;
}
return false;
}
PotentialBinding
TypeVarBindingProducer::getDefaultBinding(Constraint *constraint) const {
assert(constraint->getKind() == ConstraintKind::Defaultable ||
constraint->getKind() == ConstraintKind::FallbackType);
auto type = constraint->getSecondType();
Binding binding{type, BindingKind::Exact, constraint};
return requiresOptionalAdjustment(binding)
? binding.withType(OptionalType::get(type))
: binding;
}
ValueDecl *constraints::getOverloadChoiceDecl(Constraint *choice) {
if (choice->getKind() != ConstraintKind::BindOverload)
return nullptr;
return choice->getOverloadChoice().getDeclOrNull();
}
bool constraints::isOperatorDisjunction(Constraint *disjunction) {
assert(disjunction->getKind() == ConstraintKind::Disjunction);
auto choices = disjunction->getNestedConstraints();
assert(!choices.empty());
auto *decl = getOverloadChoiceDecl(choices.front());
return decl ? decl->isOperator() : false;
}
ASTNode constraints::findAsyncNode(ClosureExpr *closure) {
auto *body = closure->getBody();
if (!body)
return ASTNode();
return body->findAsyncNode();
}
void constraints::dumpAnchor(ASTNode anchor, SourceManager *SM,
raw_ostream &out) {
if (auto *expr = anchor.dyn_cast<Expr *>()) {
out << Expr::getKindName(expr->getKind());
if (SM) {
out << '@';
expr->getLoc().print(out, *SM);
}
} else if (auto *pattern = anchor.dyn_cast<Pattern *>()) {
out << Pattern::getKindName(pattern->getKind()) << "Pattern";
if (SM) {
out << '@';
pattern->getLoc().print(out, *SM);
}
} else if (auto *decl = anchor.dyn_cast<Decl *>()) {
if (auto *VD = dyn_cast<ValueDecl>(decl)) {
VD->dumpRef(out);
} else {
out << "<<" << Decl::getKindName(decl->getKind()) << ">>";
if (SM) {
out << "@";
decl->getLoc().print(out, *SM);
}
}
}
// TODO(diagnostics): Implement the rest of the cases.
}
bool constraints::isResultBuilderMethodReference(ASTContext &ctx,
UnresolvedDotExpr *UDE) {
if (!(UDE && UDE->isImplicit()))
return false;
SmallVector<Identifier, 5> builderMethods(
{ctx.Id_buildBlock, ctx.Id_buildExpression, ctx.Id_buildPartialBlock,
ctx.Id_buildFinalResult, ctx.Id_buildIf});
return llvm::any_of(builderMethods, [&](const Identifier &methodId) {
return UDE->getName().compare(DeclNameRef(methodId)) == 0;
});
}
|