1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
use alloc::borrow::ToOwned;
use alloc::collections::BTreeMap;
use alloc::string::{String, ToString};
use alloc::vec::Vec;
use alloc::{format, vec};
use core::cmp::Ordering;
use core::fmt;
use core::mem;
use crate::*;
use anyhow::{Context, Result, anyhow, bail};
#[cfg(not(feature = "std"))]
use hashbrown::hash_map::Entry;
use id_arena::{Arena, Id};
use semver::Version;
#[cfg(feature = "serde")]
use serde_derive::Serialize;
#[cfg(feature = "std")]
use std::collections::hash_map::Entry;
use crate::ast::lex::Span;
use crate::ast::{ParsedUsePath, parse_use_path};
#[cfg(feature = "serde")]
use crate::serde_::{serialize_arena, serialize_id_map};
use crate::{
AstItem, Docs, Error, Function, FunctionKind, Handle, IncludeName, Interface, InterfaceId,
LiftLowerAbi, ManglingAndAbi, PackageName, PackageNotFoundError, SourceMap, Stability, Type,
TypeDef, TypeDefKind, TypeId, TypeIdVisitor, TypeOwner, UnresolvedPackage,
UnresolvedPackageGroup, World, WorldId, WorldItem, WorldKey,
};
pub use clone::CloneMaps;
mod clone;
#[cfg(feature = "std")]
mod fs;
#[cfg(feature = "std")]
pub use fs::PackageSourceMap;
/// Representation of a fully resolved set of WIT packages.
///
/// This structure contains a graph of WIT packages and all of their contents
/// merged together into the contained arenas. All items are sorted
/// topologically and everything here is fully resolved, so with a `Resolve` no
/// name lookups are necessary and instead everything is index-based.
///
/// Working with a WIT package requires inserting it into a `Resolve` to ensure
/// that all of its dependencies are satisfied. This will give the full picture
/// of that package's types and such.
///
/// Each item in a `Resolve` has a parent link to trace it back to the original
/// package as necessary.
#[derive(Default, Clone, Debug)]
#[cfg_attr(feature = "serde", derive(Serialize))]
pub struct Resolve {
/// All known worlds within this `Resolve`.
///
/// Each world points at a `PackageId` which is stored below. No ordering is
/// guaranteed between this list of worlds.
#[cfg_attr(feature = "serde", serde(serialize_with = "serialize_arena"))]
pub worlds: Arena<World>,
/// All known interfaces within this `Resolve`.
///
/// Each interface points at a `PackageId` which is stored below. No
/// ordering is guaranteed between this list of interfaces.
#[cfg_attr(feature = "serde", serde(serialize_with = "serialize_arena"))]
pub interfaces: Arena<Interface>,
/// All known types within this `Resolve`.
///
/// Types are topologically sorted such that any type referenced from one
/// type is guaranteed to be defined previously. Otherwise though these are
/// not sorted by interface for example.
#[cfg_attr(feature = "serde", serde(serialize_with = "serialize_arena"))]
pub types: Arena<TypeDef>,
/// All known packages within this `Resolve`.
///
/// This list of packages is not sorted. Sorted packages can be queried
/// through [`Resolve::topological_packages`].
#[cfg_attr(feature = "serde", serde(serialize_with = "serialize_arena"))]
pub packages: Arena<Package>,
/// A map of package names to the ID of the package with that name.
#[cfg_attr(feature = "serde", serde(skip))]
pub package_names: IndexMap<PackageName, PackageId>,
/// Activated features for this [`Resolve`].
///
/// This set of features is empty by default. This is consulted for
/// `@unstable` annotations in loaded WIT documents. Any items with
/// `@unstable` are filtered out unless their feature is present within this
/// set.
#[cfg_attr(feature = "serde", serde(skip))]
pub features: IndexSet<String>,
/// Activate all features for this [`Resolve`].
#[cfg_attr(feature = "serde", serde(skip))]
pub all_features: bool,
/// Source map for converting spans to file locations.
#[cfg_attr(feature = "serde", serde(skip))]
pub source_map: SourceMap,
}
/// A WIT package within a `Resolve`.
///
/// A package is a collection of interfaces and worlds. Packages additionally
/// have a unique identifier that affects generated components and uniquely
/// identifiers this particular package.
#[derive(Clone, Debug)]
#[cfg_attr(feature = "serde", derive(Serialize))]
pub struct Package {
/// A unique name corresponding to this package.
pub name: PackageName,
/// Documentation associated with this package.
#[cfg_attr(feature = "serde", serde(skip_serializing_if = "Docs::is_empty"))]
pub docs: Docs,
/// All interfaces contained in this packaged, keyed by the interface's
/// name.
#[cfg_attr(feature = "serde", serde(serialize_with = "serialize_id_map"))]
pub interfaces: IndexMap<String, InterfaceId>,
/// All worlds contained in this package, keyed by the world's name.
#[cfg_attr(feature = "serde", serde(serialize_with = "serialize_id_map"))]
pub worlds: IndexMap<String, WorldId>,
}
pub type PackageId = Id<Package>;
/// Source name mappings for resolved packages (no_std compatible).
#[derive(Clone, Debug)]
pub struct PackageSources {
sources: Vec<Vec<String>>,
package_id_to_source_map_idx: BTreeMap<PackageId, usize>,
}
impl PackageSources {
pub fn from_single_source(package_id: PackageId, source: &str) -> Self {
Self {
sources: vec![vec![source.to_owned()]],
package_id_to_source_map_idx: BTreeMap::from([(package_id, 0)]),
}
}
pub fn from_source_maps(
source_maps: Vec<SourceMap>,
package_id_to_source_map_idx: BTreeMap<PackageId, usize>,
) -> PackageSources {
for (package_id, idx) in &package_id_to_source_map_idx {
if *idx >= source_maps.len() {
panic!(
"Invalid source map index: {}, package id: {:?}, source maps size: {}",
idx,
package_id,
source_maps.len()
)
}
}
Self {
sources: source_maps
.into_iter()
.map(|source_map| source_map.source_names().map(|s| s.to_owned()).collect())
.collect(),
package_id_to_source_map_idx,
}
}
/// All unique source names.
pub fn source_names(&self) -> impl Iterator<Item = &str> {
self.sources
.iter()
.flatten()
.map(|s| s.as_str())
.collect::<IndexSet<&str>>()
.into_iter()
}
/// Source names for a specific package.
pub fn package_source_names(&self, id: PackageId) -> Option<impl Iterator<Item = &str>> {
self.package_id_to_source_map_idx
.get(&id)
.map(|&idx| self.sources[idx].iter().map(|s| s.as_str()))
}
}
/// Visitor helper for performing topological sort on a group of packages.
fn visit<'a>(
pkg: &'a UnresolvedPackage,
pkg_details_map: &'a BTreeMap<PackageName, (UnresolvedPackage, usize)>,
order: &mut IndexSet<PackageName>,
visiting: &mut HashSet<&'a PackageName>,
source_maps: &[SourceMap],
) -> Result<()> {
if order.contains(&pkg.name) {
return Ok(());
}
match pkg_details_map.get(&pkg.name) {
Some(pkg_details) => {
let (_, source_maps_index) = pkg_details;
source_maps[*source_maps_index].rewrite_error(|| {
for (i, (dep, _)) in pkg.foreign_deps.iter().enumerate() {
let span = pkg.foreign_dep_spans[i];
if !visiting.insert(dep) {
bail!(Error::new(span, "package depends on itself"));
}
if let Some(dep) = pkg_details_map.get(dep) {
let (dep_pkg, _) = dep;
visit(dep_pkg, pkg_details_map, order, visiting, source_maps)?;
}
assert!(visiting.remove(dep));
}
assert!(order.insert(pkg.name.clone()));
Ok(())
})
}
None => panic!("No pkg_details found for package when doing topological sort"),
}
}
impl Resolve {
/// Creates a new [`Resolve`] with no packages/items inside of it.
pub fn new() -> Resolve {
Resolve::default()
}
/// Parse WIT packages from the input `path`.
///
/// The input `path` can be one of:
///
/// * A directory containing a WIT package with an optional `deps` directory
/// for any dependent WIT packages it references.
/// * A single standalone WIT file.
/// * A wasm-encoded WIT package as a single file in the wasm binary format.
/// * A wasm-encoded WIT package as a single file in the wasm text format.
///
/// In all of these cases packages are allowed to depend on previously
/// inserted packages into this `Resolve`. Resolution for packages is based
/// on the name of each package and reference.
///
/// This method returns a `PackageId` and additionally a `PackageSourceMap`.
/// The `PackageId` represent the main package that was parsed. For example if a single WIT
/// file was specified this will be the main package found in the file. For a directory this
/// will be all the main package in the directory itself. The `PackageId` value is useful
/// to pass to [`Resolve::select_world`] to take a user-specified world in a
/// conventional fashion and select which to use for bindings generation.
fn sort_unresolved_packages(
&mut self,
main: UnresolvedPackageGroup,
deps: Vec<UnresolvedPackageGroup>,
) -> Result<(PackageId, PackageSources)> {
let mut pkg_details_map = BTreeMap::new();
let mut source_maps = Vec::new();
let mut insert = |group: UnresolvedPackageGroup| {
let UnresolvedPackageGroup {
main,
nested,
source_map,
} = group;
let i = source_maps.len();
source_maps.push(source_map);
for pkg in nested.into_iter().chain([main]) {
let name = pkg.name.clone();
let my_span = pkg.package_name_span;
let (prev_pkg, prev_i) = match pkg_details_map.insert(name.clone(), (pkg, i)) {
Some(pair) => pair,
None => continue,
};
let loc1 = source_maps[i].render_location(my_span);
let loc2 = source_maps[prev_i].render_location(prev_pkg.package_name_span);
bail!(
"\
package {name} is defined in two different locations:\n\
* {loc1}\n\
* {loc2}\n\
"
)
}
Ok(())
};
let main_name = main.main.name.clone();
insert(main)?;
for dep in deps {
insert(dep)?;
}
// Perform a simple topological sort which will bail out on cycles
// and otherwise determine the order that packages must be added to
// this `Resolve`.
let mut order = IndexSet::default();
{
let mut visiting = HashSet::new();
for pkg_details in pkg_details_map.values() {
let (pkg, _) = pkg_details;
visit(
pkg,
&pkg_details_map,
&mut order,
&mut visiting,
&source_maps,
)?;
}
}
// Ensure that the final output is topologically sorted. Track which source maps
// have been appended and their byte offsets to avoid duplicating them.
let mut package_id_to_source_map_idx = BTreeMap::new();
let mut main_pkg_id = None;
let mut source_map_offsets: HashMap<usize, u32> = HashMap::new();
for name in order {
let (pkg, source_map_index) = pkg_details_map.remove(&name).unwrap();
let source_map = &source_maps[source_map_index];
let is_main = pkg.name == main_name;
// Get or compute the span offset for this source map
let span_offset = *source_map_offsets
.entry(source_map_index)
.or_insert_with(|| self.push_source_map(source_map.clone()));
let id = self.push(pkg, span_offset)?;
if is_main {
assert!(main_pkg_id.is_none());
main_pkg_id = Some(id);
}
package_id_to_source_map_idx.insert(id, source_map_index);
}
Ok((
main_pkg_id.unwrap(),
PackageSources::from_source_maps(source_maps, package_id_to_source_map_idx),
))
}
/// Appends a source map to this [`Resolve`]'s internal source map.
///
/// Returns the byte offset that should be passed to [`Resolve::push`] for
/// packages parsed from this source map. This offset ensures that spans
/// in the resolved package point to the correct location in the combined
/// source map.
pub fn push_source_map(&mut self, source_map: SourceMap) -> u32 {
self.source_map.append(source_map)
}
/// Appends a new [`UnresolvedPackage`] to this [`Resolve`], creating a
/// fully resolved package with no dangling references.
///
/// All the dependencies of `unresolved` must already have been loaded
/// within this `Resolve` via previous calls to `push` or other methods such
/// as [`Resolve::push_path`].
///
/// The `span_offset` should be the value returned by
/// [`Resolve::push_source_map`] if the source map was appended to this
/// resolve, or `0` if this is a standalone package.
///
/// Any dependency resolution error or otherwise world-elaboration error
/// will be returned here, if successful a package identifier is returned
/// which corresponds to the package that was just inserted.
pub fn push(
&mut self,
mut unresolved: UnresolvedPackage,
span_offset: u32,
) -> Result<PackageId> {
unresolved.adjust_spans(span_offset);
let ret = Remap::default().append(self, unresolved);
if ret.is_ok() {
#[cfg(debug_assertions)]
self.assert_valid();
}
self.source_map.rewrite_error(|| ret)
}
/// Appends new [`UnresolvedPackageGroup`] to this [`Resolve`], creating a
/// fully resolved package with no dangling references.
///
/// Any dependency resolution error or otherwise world-elaboration error
/// will be returned here, if successful a package identifier is returned
/// which corresponds to the package that was just inserted.
///
/// The returned [`PackageId`]s are listed in topologically sorted order.
pub fn push_group(&mut self, unresolved_group: UnresolvedPackageGroup) -> Result<PackageId> {
let (pkg_id, _) = self.sort_unresolved_packages(unresolved_group, Vec::new())?;
Ok(pkg_id)
}
/// Convenience method for combining [`SourceMap`] and [`Resolve::push_group`].
///
/// The `path` provided is used for error messages but otherwise is not
/// read. This method does not touch the filesystem. The `contents` provided
/// are the contents of a WIT package.
pub fn push_source(&mut self, path: &str, contents: &str) -> Result<PackageId> {
let mut map = SourceMap::default();
map.push_str(path, contents);
self.push_group(
map.parse()
.map_err(|(map, e)| anyhow::anyhow!("{}", e.highlight(&map)))?,
)
}
/// Renders a span as a human-readable location string (e.g., "file.wit:10:5").
pub fn render_location(&self, span: Span) -> String {
self.source_map.render_location(span)
}
pub fn all_bits_valid(&self, ty: &Type) -> bool {
match ty {
Type::U8
| Type::S8
| Type::U16
| Type::S16
| Type::U32
| Type::S32
| Type::U64
| Type::S64
| Type::F32
| Type::F64 => true,
Type::Bool | Type::Char | Type::String | Type::ErrorContext => false,
Type::Id(id) => match &self.types[*id].kind {
TypeDefKind::List(_)
| TypeDefKind::Map(_, _)
| TypeDefKind::Variant(_)
| TypeDefKind::Enum(_)
| TypeDefKind::Option(_)
| TypeDefKind::Result(_)
| TypeDefKind::Future(_)
| TypeDefKind::Stream(_) => false,
TypeDefKind::Type(t) | TypeDefKind::FixedLengthList(t, ..) => {
self.all_bits_valid(t)
}
TypeDefKind::Handle(h) => match h {
crate::Handle::Own(_) => true,
crate::Handle::Borrow(_) => true,
},
TypeDefKind::Resource => false,
TypeDefKind::Record(r) => r.fields.iter().all(|f| self.all_bits_valid(&f.ty)),
TypeDefKind::Tuple(t) => t.types.iter().all(|t| self.all_bits_valid(t)),
// FIXME: this could perhaps be `true` for multiples-of-32 but
// seems better to probably leave this as unconditionally
// `false` for now, may want to reconsider later?
TypeDefKind::Flags(_) => false,
TypeDefKind::Unknown => unreachable!(),
},
}
}
/// Merges all the contents of a different `Resolve` into this one. The
/// `Remap` structure returned provides a mapping from all old indices to
/// new indices
///
/// This operation can fail if `resolve` disagrees with `self` about the
/// packages being inserted. Otherwise though this will additionally attempt
/// to "union" packages found in `resolve` with those found in `self`.
/// Unioning packages is keyed on the name/url of packages for those with
/// URLs present. If found then it's assumed that both `Resolve` instances
/// were originally created from the same contents and are two views
/// of the same package.
pub fn merge(&mut self, resolve: Resolve) -> Result<Remap> {
log::trace!(
"merging {} packages into {} packages",
resolve.packages.len(),
self.packages.len()
);
let mut map = MergeMap::new(&resolve, &self);
map.build()?;
let MergeMap {
package_map,
interface_map,
type_map,
world_map,
interfaces_to_add,
worlds_to_add,
..
} = map;
// With a set of maps from ids in `resolve` to ids in `self` the next
// operation is to start moving over items and building a `Remap` to
// update ids.
//
// Each component field of `resolve` is moved into `self` so long as
// its ID is not within one of the maps above. If it's present in a map
// above then that means the item is already present in `self` so a new
// one need not be added. If it's not present in a map that means it's
// not present in `self` so it must be added to an arena.
//
// When adding an item to an arena one of the `remap.update_*` methods
// is additionally called to update all identifiers from pointers within
// `resolve` to becoming pointers within `self`.
//
// Altogether this should weave all the missing items in `self` from
// `resolve` into one structure while updating all identifiers to
// be local within `self`.
let mut remap = Remap::default();
let Resolve {
types,
worlds,
interfaces,
packages,
package_names,
features: _,
source_map,
..
} = resolve;
let span_offset = self.source_map.append(source_map);
let mut moved_types = Vec::new();
for (id, mut ty) in types {
let new_id = match type_map.get(&id).copied() {
Some(id) => {
update_stability(&ty.stability, &mut self.types[id].stability)?;
id
}
None => {
log::debug!("moving type {:?}", ty.name);
moved_types.push(id);
remap.update_typedef(self, &mut ty, Default::default())?;
ty.adjust_spans(span_offset);
self.types.alloc(ty)
}
};
assert_eq!(remap.types.len(), id.index());
remap.types.push(Some(new_id));
}
let mut moved_interfaces = Vec::new();
for (id, mut iface) in interfaces {
let new_id = match interface_map.get(&id).copied() {
Some(into_id) => {
update_stability(&iface.stability, &mut self.interfaces[into_id].stability)?;
// Add any extra types from `from`'s interface that
// don't exist in `into`'s interface. These types were
// already moved as new types above (since they weren't
// in `type_map`), but they still need to be registered
// in the target interface's `types` map.
for (name, from_type_id) in iface.types.iter() {
if self.interfaces[into_id].types.contains_key(name) {
continue;
}
let new_type_id = remap.map_type(*from_type_id, Default::default())?;
self.interfaces[into_id]
.types
.insert(name.clone(), new_type_id);
}
// Add any extra functions from `from`'s interface that
// don't exist in `into`'s interface. These need their
// type references remapped and spans adjusted.
let extra_funcs: Vec<_> = iface
.functions
.into_iter()
.filter(|(name, _)| {
!self.interfaces[into_id]
.functions
.contains_key(name.as_str())
})
.collect();
for (name, mut func) in extra_funcs {
remap.update_function(self, &mut func, Default::default())?;
func.adjust_spans(span_offset);
self.interfaces[into_id].functions.insert(name, func);
}
into_id
}
None => {
log::debug!("moving interface {:?}", iface.name);
moved_interfaces.push(id);
remap.update_interface(self, &mut iface)?;
iface.adjust_spans(span_offset);
self.interfaces.alloc(iface)
}
};
assert_eq!(remap.interfaces.len(), id.index());
remap.interfaces.push(Some(new_id));
}
let mut moved_worlds = Vec::new();
for (id, mut world) in worlds {
let new_id = match world_map.get(&id).copied() {
Some(world_id) => {
update_stability(&world.stability, &mut self.worlds[world_id].stability)?;
for from_import in world.imports.iter() {
Resolve::update_world_imports_stability(
from_import,
&mut self.worlds[world_id].imports,
&interface_map,
)?;
}
for from_export in world.exports.iter() {
Resolve::update_world_imports_stability(
from_export,
&mut self.worlds[world_id].exports,
&interface_map,
)?;
}
world_id
}
None => {
log::debug!("moving world {}", world.name);
moved_worlds.push(id);
let mut update = |map: &mut IndexMap<WorldKey, WorldItem>| -> Result<_> {
for (mut name, mut item) in mem::take(map) {
remap.update_world_key(&mut name, Default::default())?;
match &mut item {
WorldItem::Function(f) => {
remap.update_function(self, f, Default::default())?
}
WorldItem::Interface { id, .. } => {
*id = remap.map_interface(*id, Default::default())?
}
WorldItem::Type { id, .. } => {
*id = remap.map_type(*id, Default::default())?
}
}
map.insert(name, item);
}
Ok(())
};
update(&mut world.imports)?;
update(&mut world.exports)?;
world.adjust_spans(span_offset);
self.worlds.alloc(world)
}
};
assert_eq!(remap.worlds.len(), id.index());
remap.worlds.push(Some(new_id));
}
for (id, mut pkg) in packages {
let new_id = match package_map.get(&id).copied() {
Some(id) => id,
None => {
for (_, id) in pkg.interfaces.iter_mut() {
*id = remap.map_interface(*id, Default::default())?;
}
for (_, id) in pkg.worlds.iter_mut() {
*id = remap.map_world(*id, Default::default())?;
}
self.packages.alloc(pkg)
}
};
assert_eq!(remap.packages.len(), id.index());
remap.packages.push(new_id);
}
for (name, id) in package_names {
let id = remap.packages[id.index()];
if let Some(prev) = self.package_names.insert(name, id) {
assert_eq!(prev, id);
}
}
// Fixup all "parent" links now.
//
// Note that this is only done for items that are actually moved from
// `resolve` into `self`, which is tracked by the various `moved_*`
// lists built incrementally above. The ids in the `moved_*` lists
// are ids within `resolve`, so they're translated through `remap` to
// ids within `self`.
for id in moved_worlds {
let id = remap.map_world(id, Default::default())?;
if let Some(pkg) = self.worlds[id].package.as_mut() {
*pkg = remap.packages[pkg.index()];
}
}
for id in moved_interfaces {
let id = remap.map_interface(id, Default::default())?;
if let Some(pkg) = self.interfaces[id].package.as_mut() {
*pkg = remap.packages[pkg.index()];
}
}
for id in moved_types {
let id = remap.map_type(id, Default::default())?;
match &mut self.types[id].owner {
TypeOwner::Interface(id) => *id = remap.map_interface(*id, Default::default())?,
TypeOwner::World(id) => *id = remap.map_world(*id, Default::default())?,
TypeOwner::None => {}
}
}
// And finally process items that were present in `resolve` but were
// not present in `self`. This is only done for merged packages as
// documents may be added to `self.documents` but wouldn't otherwise be
// present in the `documents` field of the corresponding package.
for (name, pkg, iface) in interfaces_to_add {
let prev = self.packages[pkg]
.interfaces
.insert(name, remap.map_interface(iface, Default::default())?);
assert!(prev.is_none());
}
for (name, pkg, world) in worlds_to_add {
let prev = self.packages[pkg]
.worlds
.insert(name, remap.map_world(world, Default::default())?);
assert!(prev.is_none());
}
log::trace!("now have {} packages", self.packages.len());
// Re-elaborate all worlds after the merge. Merging may have added
// extra types to existing interfaces that introduce new interface
// dependencies not yet present in the world's imports.
let world_ids: Vec<_> = self.worlds.iter().map(|(id, _)| id).collect();
for world_id in world_ids {
self.elaborate_world(world_id)?;
}
#[cfg(debug_assertions)]
self.assert_valid();
Ok(remap)
}
fn update_world_imports_stability(
from_item: (&WorldKey, &WorldItem),
into_items: &mut IndexMap<WorldKey, WorldItem>,
interface_map: &HashMap<Id<Interface>, Id<Interface>>,
) -> Result<()> {
match from_item.0 {
WorldKey::Name(_) => {
// No stability info to update here, only updating import/include stability
Ok(())
}
key @ WorldKey::Interface(_) => {
let new_key = MergeMap::map_name(key, interface_map);
if let Some(into) = into_items.get_mut(&new_key) {
match (from_item.1, into) {
(
WorldItem::Interface {
id: aid,
stability: astability,
..
},
WorldItem::Interface {
id: bid,
stability: bstability,
..
},
) => {
let aid = interface_map.get(aid).copied().unwrap_or(*aid);
assert_eq!(aid, *bid);
update_stability(astability, bstability)?;
Ok(())
}
_ => unreachable!(),
}
} else {
// we've already matched all the imports/exports by the time we are calling this
// so this is unreachable since we should always find the item
unreachable!()
}
}
}
}
/// Merges the world `from` into the world `into`.
///
/// This will attempt to merge one world into another, unioning all of its
/// imports and exports together. This is an operation performed by
/// `wit-component`, for example where two different worlds from two
/// different libraries were linked into the same core wasm file and are
/// producing a singular world that will be the final component's
/// interface.
///
/// During the merge operation, some of the types and/or interfaces in
/// `from` might need to be cloned so that backreferences point to `into`
/// instead of `from`. Any such clones will be added to `clone_maps`.
///
/// This operation can fail if the imports/exports overlap.
pub fn merge_worlds(
&mut self,
from: WorldId,
into: WorldId,
clone_maps: &mut CloneMaps,
) -> Result<()> {
let mut new_imports = Vec::new();
let mut new_exports = Vec::new();
let from_world = &self.worlds[from];
let into_world = &self.worlds[into];
log::trace!("merging {} into {}", from_world.name, into_world.name);
// First walk over all the imports of `from` world and figure out what
// to do with them.
//
// If the same item exists in `from` and `into` then merge it together
// below with `merge_world_item` which basically asserts they're the
// same. Otherwise queue up a new import since if `from` has more
// imports than `into` then it's fine to add new imports.
for (name, from_import) in from_world.imports.iter() {
let name_str = self.name_world_key(name);
match into_world.imports.get(name) {
Some(into_import) => {
log::trace!("info/from shared import on `{name_str}`");
self.merge_world_item(from_import, into_import)
.with_context(|| format!("failed to merge world import {name_str}"))?;
}
None => {
log::trace!("new import: `{name_str}`");
new_imports.push((name.clone(), from_import.clone()));
}
}
}
// Build a set of interfaces which are required to be imported because
// of `into`'s exports. This set is then used below during
// `ensure_can_add_world_export`.
//
// This is the set of interfaces which exports depend on that are
// themselves not exports.
let mut must_be_imported = HashMap::new();
for (key, export) in into_world.exports.iter() {
for dep in self.world_item_direct_deps(export) {
if into_world.exports.contains_key(&WorldKey::Interface(dep)) {
continue;
}
self.foreach_interface_dep(dep, &mut |id| {
must_be_imported.insert(id, key.clone());
});
}
}
// Next walk over exports of `from` and process these similarly to
// imports.
for (name, from_export) in from_world.exports.iter() {
let name_str = self.name_world_key(name);
match into_world.exports.get(name) {
Some(into_export) => {
log::trace!("info/from shared export on `{name_str}`");
self.merge_world_item(from_export, into_export)
.with_context(|| format!("failed to merge world export {name_str}"))?;
}
None => {
log::trace!("new export `{name_str}`");
// See comments in `ensure_can_add_world_export` for why
// this is slightly different than imports.
self.ensure_can_add_world_export(
into_world,
name,
from_export,
&must_be_imported,
)
.with_context(|| {
format!("failed to add export `{}`", self.name_world_key(name))
})?;
new_exports.push((name.clone(), from_export.clone()));
}
}
}
// For all the new imports and exports they may need to be "cloned" to
// be able to belong to the new world. For example:
//
// * Anonymous interfaces have a `package` field which points to the
// package of the containing world, but `from` and `into` may not be
// in the same package.
//
// * Type imports have an `owner` field that point to `from`, but they
// now need to point to `into` instead.
//
// Cloning is no trivial task, however, so cloning is delegated to a
// submodule to perform a "deep" clone and copy items into new arena
// entries as necessary.
let mut cloner = clone::Cloner::new(
self,
clone_maps,
TypeOwner::World(from),
TypeOwner::World(into),
);
cloner.register_world_type_overlap(from, into);
for (name, item) in new_imports.iter_mut().chain(&mut new_exports) {
cloner.world_item(name, item);
}
// Insert any new imports and new exports found first.
let into_world = &mut self.worlds[into];
for (name, import) in new_imports {
let prev = into_world.imports.insert(name, import);
assert!(prev.is_none());
}
for (name, export) in new_exports {
let prev = into_world.exports.insert(name, export);
assert!(prev.is_none());
}
#[cfg(debug_assertions)]
self.assert_valid();
Ok(())
}
fn merge_world_item(&self, from: &WorldItem, into: &WorldItem) -> Result<()> {
let mut map = MergeMap::new(self, self);
match (from, into) {
(WorldItem::Interface { id: from, .. }, WorldItem::Interface { id: into, .. }) => {
// If these imports are the same that can happen, for
// example, when both worlds to `import foo:bar/baz;`. That
// foreign interface will point to the same interface within
// `Resolve`.
if from == into {
return Ok(());
}
// .. otherwise this MUST be a case of
// `import foo: interface { ... }`. If `from != into` but
// both `from` and `into` have the same name then the
// `WorldKey::Interface` case is ruled out as otherwise
// they'd have different names.
//
// In the case of an anonymous interface all we can do is
// ensure that the interfaces both match, so use `MergeMap`
// for that.
map.build_interface(*from, *into)
.context("failed to merge interfaces")?;
}
// Like `WorldKey::Name` interfaces for functions and types the
// structure is asserted to be the same.
(WorldItem::Function(from), WorldItem::Function(into)) => {
map.build_function(from, into)
.context("failed to merge functions")?;
}
(WorldItem::Type { id: from, .. }, WorldItem::Type { id: into, .. }) => {
map.build_type_id(*from, *into)
.context("failed to merge types")?;
}
// Kind-level mismatches are caught here.
(WorldItem::Interface { .. }, _)
| (WorldItem::Function { .. }, _)
| (WorldItem::Type { .. }, _) => {
bail!("different kinds of items");
}
}
assert!(map.interfaces_to_add.is_empty());
assert!(map.worlds_to_add.is_empty());
Ok(())
}
/// This method ensures that the world export of `name` and `item` can be
/// added to the world `into` without changing the meaning of `into`.
///
/// All dependencies of world exports must either be:
///
/// * An export themselves
/// * An import with all transitive dependencies of the import also imported
///
/// It's not possible to depend on an import which then also depends on an
/// export at some point, for example. This method ensures that if `name`
/// and `item` are added that this property is upheld.
fn ensure_can_add_world_export(
&self,
into: &World,
name: &WorldKey,
item: &WorldItem,
must_be_imported: &HashMap<InterfaceId, WorldKey>,
) -> Result<()> {
assert!(!into.exports.contains_key(name));
let name = self.name_world_key(name);
// First make sure that all of this item's dependencies are either
// exported or the entire chain of imports rooted at that dependency are
// all imported.
for dep in self.world_item_direct_deps(item) {
if into.exports.contains_key(&WorldKey::Interface(dep)) {
continue;
}
self.ensure_not_exported(into, dep)
.with_context(|| format!("failed validating export of `{name}`"))?;
}
// Second make sure that this item, if it's an interface, will not alter
// the meaning of the preexisting world by ensuring that it's not in the
// set of "must be imported" items.
if let WorldItem::Interface { id, .. } = item {
if let Some(export) = must_be_imported.get(id) {
let export_name = self.name_world_key(export);
bail!(
"export `{export_name}` depends on `{name}` \
previously as an import which will change meaning \
if `{name}` is added as an export"
);
}
}
Ok(())
}
fn ensure_not_exported(&self, world: &World, id: InterfaceId) -> Result<()> {
let key = WorldKey::Interface(id);
let name = self.name_world_key(&key);
if world.exports.contains_key(&key) {
bail!(
"world exports `{name}` but it's also transitively used by an \
import \
which means that this is not valid"
)
}
for dep in self.interface_direct_deps(id) {
self.ensure_not_exported(world, dep)
.with_context(|| format!("failed validating transitive import dep `{name}`"))?;
}
Ok(())
}
/// Returns an iterator of all the direct interface dependencies of this
/// `item`.
///
/// Note that this doesn't include transitive dependencies, that must be
/// followed manually.
fn world_item_direct_deps(&self, item: &WorldItem) -> impl Iterator<Item = InterfaceId> + '_ {
let mut interface = None;
let mut ty = None;
match item {
WorldItem::Function(_) => {}
WorldItem::Type { id, .. } => ty = Some(*id),
WorldItem::Interface { id, .. } => interface = Some(*id),
}
interface
.into_iter()
.flat_map(move |id| self.interface_direct_deps(id))
.chain(ty.and_then(|t| self.type_interface_dep(t)))
}
/// Invokes `f` with `id` and all transitive interface dependencies of `id`.
///
/// Note that `f` may be called with the same id multiple times.
fn foreach_interface_dep(&self, id: InterfaceId, f: &mut dyn FnMut(InterfaceId)) {
self._foreach_interface_dep(id, f, &mut HashSet::new())
}
// Internal detail of `foreach_interface_dep` which uses a hash map to prune
// the visit tree to ensure that this doesn't visit an exponential number of
// interfaces.
fn _foreach_interface_dep(
&self,
id: InterfaceId,
f: &mut dyn FnMut(InterfaceId),
visited: &mut HashSet<InterfaceId>,
) {
if !visited.insert(id) {
return;
}
f(id);
for dep in self.interface_direct_deps(id) {
self._foreach_interface_dep(dep, f, visited);
}
}
/// Returns the ID of the specified `interface`.
///
/// Returns `None` for unnamed interfaces.
pub fn id_of(&self, interface: InterfaceId) -> Option<String> {
let interface = &self.interfaces[interface];
Some(self.id_of_name(interface.package.unwrap(), interface.name.as_ref()?))
}
/// Returns the "canonicalized interface name" of `interface`.
///
/// Returns `None` for unnamed interfaces. See `BuildTargets.md` in the
/// upstream component model repository for more information about this.
pub fn canonicalized_id_of(&self, interface: InterfaceId) -> Option<String> {
let interface = &self.interfaces[interface];
Some(self.canonicalized_id_of_name(interface.package.unwrap(), interface.name.as_ref()?))
}
/// Helper to rename a world and update the package's world map.
///
/// Used by both [`Resolve::importize`] and [`Resolve::exportize`] to
/// rename the world to avoid confusion with the original world name.
fn rename_world(
&mut self,
world_id: WorldId,
out_world_name: Option<String>,
default_suffix: &str,
) {
let world = &mut self.worlds[world_id];
let pkg = &mut self.packages[world.package.unwrap()];
pkg.worlds.shift_remove(&world.name);
if let Some(name) = out_world_name {
world.name = name.clone();
pkg.worlds.insert(name, world_id);
} else {
world.name.push_str(default_suffix);
pkg.worlds.insert(world.name.clone(), world_id);
}
}
/// Convert a world to an "importized" version where the world is updated
/// in-place to reflect what it would look like to be imported.
///
/// This is a transformation which is used as part of the process of
/// importing a component today. For example when a component depends on
/// another component this is useful for generating WIT which can be use to
/// represent the component being imported. The general idea is that this
/// function will update the `world_id` specified such it imports the
/// functionality that it previously exported. The world will be left with
/// no exports.
///
/// This world is then suitable for merging into other worlds or generating
/// bindings in a context that is importing the original world. This
/// is intended to be used as part of language tooling when depending on
/// other components.
pub fn importize(&mut self, world_id: WorldId, out_world_name: Option<String>) -> Result<()> {
self.rename_world(world_id, out_world_name, "-importized");
// Trim all non-type definitions from imports. Types can be used by
// exported functions, for example, so they're preserved.
let world = &mut self.worlds[world_id];
world.imports.retain(|_, item| match item {
WorldItem::Type { .. } => true,
_ => false,
});
for (name, export) in mem::take(&mut world.exports) {
match (name.clone(), world.imports.insert(name, export)) {
// no previous item? this insertion was ok
(_, None) => {}
// cannot overwrite an import with an export
(WorldKey::Name(name), Some(_)) => {
bail!("world export `{name}` conflicts with import of same name");
}
// Exports already don't overlap each other and the only imports
// preserved above were types so this shouldn't be reachable.
(WorldKey::Interface(_), _) => unreachable!(),
}
}
// Fill out any missing transitive interface imports by elaborating this
// world which does that for us.
self.elaborate_world(world_id)?;
#[cfg(debug_assertions)]
self.assert_valid();
Ok(())
}
/// Convert a world to an "exportized" version where the world is updated
/// in-place to reflect what it would look like to be exported.
///
/// This is the inverse of [`Resolve::importize`]. The general idea is that
/// this function will update the `world_id` specified such that it exports
/// the functionality that it previously imported. The world will be left
/// with no imports (except for transitive interface dependencies which may
/// be needed by exported interfaces).
///
/// An optional `filter` can be provided to control which imports are moved.
/// When `Some`, only imports for which the filter returns `true` are moved
/// to exports; remaining imports are left as-is. When `None`, all imports
/// are moved.
///
/// This world is then suitable for merging into other worlds or generating
/// bindings in a context that is exporting the original world. This is
/// intended to be used as part of language tooling when implementing
/// components.
pub fn exportize(
&mut self,
world_id: WorldId,
out_world_name: Option<String>,
filter: Option<&dyn Fn(&WorldKey, &WorldItem) -> bool>,
) -> Result<()> {
self.rename_world(world_id, out_world_name, "-exportized");
let world = &mut self.worlds[world_id];
world.exports.clear();
let old_imports = mem::take(&mut world.imports);
for (name, import) in old_imports {
let should_move = match &filter {
Some(f) => f(&name, &import),
None => true,
};
if should_move {
world.exports.insert(name, import);
} else {
world.imports.insert(name, import);
}
}
// Fill out any missing transitive interface imports by elaborating this
// world which does that for us.
self.elaborate_world(world_id)?;
#[cfg(debug_assertions)]
self.assert_valid();
Ok(())
}
/// Returns the ID of the specified `name` within the `pkg`.
pub fn id_of_name(&self, pkg: PackageId, name: &str) -> String {
let package = &self.packages[pkg];
let mut base = String::new();
base.push_str(&package.name.namespace);
base.push_str(":");
base.push_str(&package.name.name);
base.push_str("/");
base.push_str(name);
if let Some(version) = &package.name.version {
base.push_str(&format!("@{version}"));
}
base
}
/// Returns the "canonicalized interface name" of the specified `name`
/// within the `pkg`.
///
/// See `BuildTargets.md` in the upstream component model repository for
/// more information about this.
pub fn canonicalized_id_of_name(&self, pkg: PackageId, name: &str) -> String {
let package = &self.packages[pkg];
let mut base = String::new();
base.push_str(&package.name.namespace);
base.push_str(":");
base.push_str(&package.name.name);
base.push_str("/");
base.push_str(name);
if let Some(version) = &package.name.version {
base.push_str("@");
let string = PackageName::version_compat_track_string(version);
base.push_str(&string);
}
base
}
/// Selects a world from among the packages in a `Resolve`.
///
/// A `Resolve` may have many packages, each with many worlds. Many WIT
/// tools need a specific world to operate on. This function chooses a
/// world, failing if the choice is ambiguous.
///
/// `main_packages` provides the package IDs returned by
/// [`push_path`](Resolve::push_path), [`push_dir`](Resolve::push_dir),
/// [`push_file`](Resolve::push_file), [`push_group`](Resolve::push_group),
/// and [`push_str`](Resolve::push_str), which are the "main packages",
/// as distinguished from any packages nested inside them.
///
/// `world` is a world name such as from a `--world` command-line option or
/// a `world:` macro parameter. `world` can be:
///
/// * A kebab-name of a world, for example `"the-world"`. It is resolved
/// within the "main package", if there is exactly one.
///
/// * An ID-based form of a world, for example `"wasi:http/proxy"`. Note
/// that a version does not need to be specified in this string if
/// there's only one package of the same name and it has a version. In
/// this situation the version can be omitted.
///
/// * `None`. If there's exactly one "main package" and it contains exactly
/// one world, that world is chosen.
///
/// If successful, the chosen `WorldId` is returned.
///
/// # Examples
///
/// ```
/// use anyhow::Result;
/// use wit_parser::Resolve;
///
/// fn main() -> Result<()> {
/// let mut resolve = Resolve::default();
///
/// // If there's a single package and only one world, that world is
/// // the obvious choice.
/// let wit1 = resolve.push_str(
/// "./my-test.wit",
/// r#"
/// package example:wit1;
///
/// world foo {
/// // ...
/// }
/// "#,
/// )?;
/// assert!(resolve.select_world(&[wit1], None).is_ok());
///
/// // If there are multiple packages, we need to be told which package
/// // to use, either by a "main package" or by a fully-qualified name.
/// let wit2 = resolve.push_str(
/// "./my-test.wit",
/// r#"
/// package example:wit2;
///
/// world foo { /* ... */ }
/// "#,
/// )?;
/// assert!(resolve.select_world(&[wit1, wit2], None).is_err());
/// assert!(resolve.select_world(&[wit1, wit2], Some("foo")).is_err());
/// // Fix: use fully-qualified names.
/// assert!(resolve.select_world(&[wit1, wit2], Some("example:wit1/foo")).is_ok());
/// assert!(resolve.select_world(&[wit1, wit2], Some("example:wit2/foo")).is_ok());
///
/// // If a package has multiple worlds, then we can't guess the world
/// // even if we know the package.
/// let wit3 = resolve.push_str(
/// "./my-test.wit",
/// r#"
/// package example:wit3;
///
/// world foo { /* ... */ }
///
/// world bar { /* ... */ }
/// "#,
/// )?;
/// assert!(resolve.select_world(&[wit3], None).is_err());
/// // Fix: pick between "foo" and "bar" here.
/// assert!(resolve.select_world(&[wit3], Some("foo")).is_ok());
///
/// // When selecting with a version it's ok to drop the version when
/// // there's only a single copy of that package in `Resolve`.
/// let wit5_1 = resolve.push_str(
/// "./my-test.wit",
/// r#"
/// package example:wit5@1.0.0;
///
/// world foo { /* ... */ }
/// "#,
/// )?;
/// assert!(resolve.select_world(&[wit5_1], Some("foo")).is_ok());
/// assert!(resolve.select_world(&[wit5_1], Some("example:wit5/foo")).is_ok());
///
/// // However when a single package has multiple versions in a resolve
/// // it's required to specify the version to select which one.
/// let wit5_2 = resolve.push_str(
/// "./my-test.wit",
/// r#"
/// package example:wit5@2.0.0;
///
/// world foo { /* ... */ }
/// "#,
/// )?;
/// assert!(resolve.select_world(&[wit5_1, wit5_2], Some("example:wit5/foo")).is_err());
/// // Fix: Pass explicit versions.
/// assert!(resolve.select_world(&[wit5_1, wit5_2], Some("example:wit5/foo@1.0.0")).is_ok());
/// assert!(resolve.select_world(&[wit5_1, wit5_2], Some("example:wit5/foo@2.0.0")).is_ok());
///
/// Ok(())
/// }
/// ```
pub fn select_world(
&self,
main_packages: &[PackageId],
world: Option<&str>,
) -> Result<WorldId> {
// Determine if `world` is a kebab-name or an ID.
let world_path = match world {
Some(world) => Some(
parse_use_path(world)
.with_context(|| format!("failed to parse world specifier `{world}`"))?,
),
None => None,
};
match world_path {
// We have a world path. If needed, pick a package to resolve it in.
Some(world_path) => {
let (pkg, world_name) = match (main_packages, world_path) {
// We have no main packages; fail.
([], _) => bail!("No main packages defined"),
// We have exactly one main package.
([main_package], ParsedUsePath::Name(name)) => (*main_package, name),
// We have more than one main package; fail.
(_, ParsedUsePath::Name(_name)) => {
bail!(
"There are multiple main packages; a world must be explicitly chosen:{}",
self.worlds
.iter()
.map(|world| format!(
"\n {}",
self.id_of_name(world.1.package.unwrap(), &world.1.name)
))
.collect::<String>()
)
}
// The world name is fully-qualified.
(_, ParsedUsePath::Package(pkg, world_name)) => {
let pkg = match self.package_names.get(&pkg) {
Some(pkg) => *pkg,
None => {
let mut candidates =
self.package_names.iter().filter(|(name, _)| {
pkg.version.is_none()
&& pkg.name == name.name
&& pkg.namespace == name.namespace
&& name.version.is_some()
});
let candidate = candidates.next();
if let Some((c2, _)) = candidates.next() {
let (c1, _) = candidate.unwrap();
bail!(
"package name `{pkg}` is available at both \
versions {} and {} but which is not specified",
c1.version.as_ref().unwrap(),
c2.version.as_ref().unwrap(),
);
}
match candidate {
Some((_, id)) => *id,
None => bail!("unknown package `{pkg}`"),
}
}
};
(pkg, world_name.to_string())
}
};
// Now that we've picked the package, resolve the world name.
let pkg = &self.packages[pkg];
pkg.worlds.get(&world_name).copied().ok_or_else(|| {
anyhow!("World `{world_name}` not found in package `{}`", pkg.name)
})
}
// With no specified `world`, try to find a single obvious world.
None => match main_packages {
[] => bail!("No main packages defined"),
// Check for exactly one main package with exactly one world.
[main_package] => {
let pkg = &self.packages[*main_package];
match pkg.worlds.len() {
0 => bail!("The main package `{}` contains no worlds", pkg.name),
1 => Ok(pkg.worlds[0]),
_ => bail!(
"There are multiple worlds in `{}`; one must be explicitly chosen:{}",
pkg.name,
pkg.worlds
.values()
.map(|world| format!(
"\n {}",
self.id_of_name(*main_package, &self.worlds[*world].name)
))
.collect::<String>()
),
}
}
// Multiple main packages and no world name; fail.
_ => {
bail!(
"There are multiple main packages; a world must be explicitly chosen:{}",
self.worlds
.iter()
.map(|world| format!(
"\n {}",
self.id_of_name(world.1.package.unwrap(), &world.1.name)
))
.collect::<String>()
)
}
},
}
}
/// Assigns a human readable name to the `WorldKey` specified.
pub fn name_world_key(&self, key: &WorldKey) -> String {
match key {
WorldKey::Name(s) => s.to_string(),
WorldKey::Interface(i) => self.id_of(*i).expect("unexpected anonymous interface"),
}
}
/// Same as [`Resolve::name_world_key`] except that `WorldKey::Interfaces`
/// uses [`Resolve::canonicalized_id_of`].
pub fn name_canonicalized_world_key(&self, key: &WorldKey) -> String {
match key {
WorldKey::Name(s) => s.to_string(),
WorldKey::Interface(i) => self
.canonicalized_id_of(*i)
.expect("unexpected anonymous interface"),
}
}
/// Returns the interface that `id` uses a type from, if it uses a type from
/// a different interface than `id` is defined within.
///
/// If `id` is not a use-of-a-type or it's using a type in the same
/// interface then `None` is returned.
pub fn type_interface_dep(&self, id: TypeId) -> Option<InterfaceId> {
let ty = &self.types[id];
let dep = match ty.kind {
TypeDefKind::Type(Type::Id(id)) => id,
_ => return None,
};
let other = &self.types[dep];
if ty.owner == other.owner {
None
} else {
match other.owner {
TypeOwner::Interface(id) => Some(id),
_ => unreachable!(),
}
}
}
/// Returns an iterator of all interfaces that the interface `id` depends
/// on.
///
/// Interfaces may depend on others for type information to resolve type
/// imports.
///
/// Note that the returned iterator may yield the same interface as a
/// dependency multiple times. Additionally only direct dependencies of `id`
/// are yielded, not transitive dependencies.
pub fn interface_direct_deps(&self, id: InterfaceId) -> impl Iterator<Item = InterfaceId> + '_ {
self.interfaces[id]
.types
.iter()
.filter_map(move |(_name, ty)| self.type_interface_dep(*ty))
}
/// Returns an iterator of all packages that the package `id` depends
/// on.
///
/// Packages may depend on others for type information to resolve type
/// imports or interfaces to resolve worlds.
///
/// Note that the returned iterator may yield the same package as a
/// dependency multiple times. Additionally only direct dependencies of `id`
/// are yielded, not transitive dependencies.
pub fn package_direct_deps(&self, id: PackageId) -> impl Iterator<Item = PackageId> + '_ {
let pkg = &self.packages[id];
pkg.interfaces
.iter()
.flat_map(move |(_name, id)| self.interface_direct_deps(*id))
.chain(pkg.worlds.iter().flat_map(move |(_name, id)| {
let world = &self.worlds[*id];
world
.imports
.iter()
.chain(world.exports.iter())
.filter_map(move |(_name, item)| match item {
WorldItem::Interface { id, .. } => Some(*id),
WorldItem::Function(_) => None,
WorldItem::Type { id, .. } => self.type_interface_dep(*id),
})
}))
.filter_map(move |iface_id| {
let pkg = self.interfaces[iface_id].package?;
if pkg == id { None } else { Some(pkg) }
})
}
/// Returns a topological ordering of packages contained in this `Resolve`.
///
/// This returns a list of `PackageId` such that when visited in order it's
/// guaranteed that all dependencies will have been defined by prior items
/// in the list.
pub fn topological_packages(&self) -> Vec<PackageId> {
let mut pushed = vec![false; self.packages.len()];
let mut order = Vec::new();
for (id, _) in self.packages.iter() {
self.build_topological_package_ordering(id, &mut pushed, &mut order);
}
order
}
fn build_topological_package_ordering(
&self,
id: PackageId,
pushed: &mut Vec<bool>,
order: &mut Vec<PackageId>,
) {
if pushed[id.index()] {
return;
}
for dep in self.package_direct_deps(id) {
self.build_topological_package_ordering(dep, pushed, order);
}
order.push(id);
pushed[id.index()] = true;
}
#[doc(hidden)]
pub fn assert_valid(&self) {
let mut package_interfaces = Vec::new();
let mut package_worlds = Vec::new();
for (id, pkg) in self.packages.iter() {
let mut interfaces = HashSet::new();
for (name, iface) in pkg.interfaces.iter() {
assert!(interfaces.insert(*iface));
let iface = &self.interfaces[*iface];
assert_eq!(name, iface.name.as_ref().unwrap());
assert_eq!(iface.package.unwrap(), id);
}
package_interfaces.push(pkg.interfaces.values().copied().collect::<HashSet<_>>());
let mut worlds = HashSet::new();
for (name, world) in pkg.worlds.iter() {
assert!(worlds.insert(*world));
assert_eq!(
pkg.worlds.get_key_value(name),
Some((name, world)),
"`MutableKeys` impl may have been used to change a key's hash or equality"
);
let world = &self.worlds[*world];
assert_eq!(*name, world.name);
assert_eq!(world.package.unwrap(), id);
}
package_worlds.push(pkg.worlds.values().copied().collect::<HashSet<_>>());
}
let mut interface_types = Vec::new();
for (id, iface) in self.interfaces.iter() {
assert!(self.packages.get(iface.package.unwrap()).is_some());
if iface.name.is_some() {
match iface.clone_of {
Some(other) => {
assert_eq!(iface.name, self.interfaces[other].name);
}
None => {
assert!(package_interfaces[iface.package.unwrap().index()].contains(&id));
}
}
}
for (name, ty) in iface.types.iter() {
let ty = &self.types[*ty];
assert_eq!(ty.name.as_ref(), Some(name));
assert_eq!(ty.owner, TypeOwner::Interface(id));
}
interface_types.push(iface.types.values().copied().collect::<HashSet<_>>());
for (name, f) in iface.functions.iter() {
assert_eq!(*name, f.name);
}
}
let mut world_types = Vec::new();
for (id, world) in self.worlds.iter() {
log::debug!("validating world {}", &world.name);
if let Some(package) = world.package {
assert!(self.packages.get(package).is_some());
assert!(package_worlds[package.index()].contains(&id));
}
assert!(world.includes.is_empty());
let mut types = HashSet::new();
for (name, item) in world.imports.iter().chain(world.exports.iter()) {
log::debug!("validating world item: {}", self.name_world_key(name));
match item {
WorldItem::Interface { id, .. } => {
// anonymous interfaces must belong to the same package
// as the world's package.
if matches!(name, WorldKey::Name(_)) {
assert_eq!(self.interfaces[*id].package, world.package);
}
}
WorldItem::Function(f) => {
assert!(!matches!(name, WorldKey::Interface(_)));
assert_eq!(f.name, name.clone().unwrap_name());
}
WorldItem::Type { id: ty, .. } => {
assert!(!matches!(name, WorldKey::Interface(_)));
assert!(types.insert(*ty));
let ty = &self.types[*ty];
assert_eq!(ty.name, Some(name.clone().unwrap_name()));
assert_eq!(ty.owner, TypeOwner::World(id));
}
}
}
self.assert_world_elaborated(world);
world_types.push(types);
}
for (ty_id, ty) in self.types.iter() {
match ty.owner {
TypeOwner::Interface(id) => {
assert!(self.interfaces.get(id).is_some());
assert!(interface_types[id.index()].contains(&ty_id));
}
TypeOwner::World(id) => {
assert!(self.worlds.get(id).is_some());
assert!(world_types[id.index()].contains(&ty_id));
}
TypeOwner::None => {}
}
}
self.assert_topologically_sorted();
}
fn assert_topologically_sorted(&self) {
let mut positions = IndexMap::default();
for id in self.topological_packages() {
let pkg = &self.packages[id];
log::debug!("pkg {}", pkg.name);
let prev = positions.insert(Some(id), IndexSet::default());
assert!(prev.is_none());
}
positions.insert(None, IndexSet::default());
for (id, iface) in self.interfaces.iter() {
log::debug!("iface {:?}", iface.name);
let ok = positions.get_mut(&iface.package).unwrap().insert(id);
assert!(ok);
}
for (_, world) in self.worlds.iter() {
log::debug!("world {:?}", world.name);
let my_package = world.package;
let my_package_pos = positions.get_index_of(&my_package).unwrap();
for (_, item) in world.imports.iter().chain(&world.exports) {
let id = match item {
WorldItem::Interface { id, .. } => *id,
_ => continue,
};
let other_package = self.interfaces[id].package;
let other_package_pos = positions.get_index_of(&other_package).unwrap();
assert!(other_package_pos <= my_package_pos);
}
}
for (_id, ty) in self.types.iter() {
log::debug!("type {:?} {:?}", ty.name, ty.owner);
let other_id = match ty.kind {
TypeDefKind::Type(Type::Id(ty)) => ty,
_ => continue,
};
let other = &self.types[other_id];
if ty.kind == other.kind {
continue;
}
let my_interface = match ty.owner {
TypeOwner::Interface(id) => id,
_ => continue,
};
let other_interface = match other.owner {
TypeOwner::Interface(id) => id,
_ => continue,
};
let my_package = self.interfaces[my_interface].package;
let other_package = self.interfaces[other_interface].package;
let my_package_pos = positions.get_index_of(&my_package).unwrap();
let other_package_pos = positions.get_index_of(&other_package).unwrap();
assert!(other_package_pos <= my_package_pos);
}
}
fn assert_world_elaborated(&self, world: &World) {
for (key, item) in world.imports.iter() {
log::debug!(
"asserting elaborated world import {}",
self.name_world_key(key)
);
match item {
WorldItem::Type { id, .. } => self.assert_world_imports_type_deps(world, key, *id),
// All types referred to must be imported.
WorldItem::Function(f) => self.assert_world_function_imports_types(world, key, f),
// All direct dependencies of this interface must be imported.
WorldItem::Interface { id, .. } => {
for dep in self.interface_direct_deps(*id) {
assert!(
world.imports.contains_key(&WorldKey::Interface(dep)),
"world import of {} is missing transitive dep of {}",
self.name_world_key(key),
self.id_of(dep).unwrap(),
);
}
}
}
}
for (key, item) in world.exports.iter() {
log::debug!(
"asserting elaborated world export {}",
self.name_world_key(key)
);
match item {
// Types referred to by this function must be imported.
WorldItem::Function(f) => self.assert_world_function_imports_types(world, key, f),
// Dependencies of exported interfaces must also be exported, or
// if imported then that entire chain of imports must be
// imported and not exported.
WorldItem::Interface { id, .. } => {
for dep in self.interface_direct_deps(*id) {
let dep_key = WorldKey::Interface(dep);
if world.exports.contains_key(&dep_key) {
continue;
}
self.foreach_interface_dep(dep, &mut |dep| {
let dep_key = WorldKey::Interface(dep);
assert!(
world.imports.contains_key(&dep_key),
"world should import {} (required by {})",
self.name_world_key(&dep_key),
self.name_world_key(key),
);
assert!(
!world.exports.contains_key(&dep_key),
"world should not export {} (required by {})",
self.name_world_key(&dep_key),
self.name_world_key(key),
);
});
}
}
// exported types not allowed at this time
WorldItem::Type { .. } => unreachable!(),
}
}
}
fn assert_world_imports_type_deps(&self, world: &World, key: &WorldKey, ty: TypeId) {
// If this is a `use` statement then the referred-to interface must be
// imported into this world.
let ty = &self.types[ty];
if let TypeDefKind::Type(Type::Id(other)) = ty.kind {
if let TypeOwner::Interface(id) = self.types[other].owner {
let key = WorldKey::Interface(id);
assert!(world.imports.contains_key(&key));
return;
}
}
// ... otherwise any named type that this type refers to, one level
// deep, must be imported into this world under that name.
let mut visitor = MyVisit(self, Vec::new());
visitor.visit_type_def(self, ty);
for ty in visitor.1 {
let ty = &self.types[ty];
let Some(name) = ty.name.clone() else {
continue;
};
let dep_key = WorldKey::Name(name);
assert!(
world.imports.contains_key(&dep_key),
"world import `{}` should also force an import of `{}`",
self.name_world_key(key),
self.name_world_key(&dep_key),
);
}
struct MyVisit<'a>(&'a Resolve, Vec<TypeId>);
impl TypeIdVisitor for MyVisit<'_> {
fn before_visit_type_id(&mut self, id: TypeId) -> bool {
self.1.push(id);
// recurse into unnamed types to look at all named types
self.0.types[id].name.is_none()
}
}
}
/// This asserts that all types referred to by `func` are imported into
/// `world` under `WorldKey::Name`. Note that this is only applicable to
/// named type
fn assert_world_function_imports_types(&self, world: &World, key: &WorldKey, func: &Function) {
for ty in func
.parameter_and_result_types()
.chain(func.kind.resource().map(Type::Id))
{
let Type::Id(id) = ty else {
continue;
};
self.assert_world_imports_type_deps(world, key, id);
}
}
/// Returns whether the `stability` annotation contained within `pkg_id`
/// should be included or not.
///
/// The `span` provided here is an optional span pointing to the item that
/// is annotated with `stability`.
///
/// Returns `Ok(true)` if the item is included, or `Ok(false)` if the item
/// is not.
///
/// # Errors
///
/// Returns an error if the `pkg_id` isn't annotated with sufficient version
/// information to have a `stability` annotation. For example if `pkg_id`
/// has no version listed then an error will be returned if `stability`
/// mentions a version.
fn include_stability(
&self,
stability: &Stability,
pkg_id: &PackageId,
span: Span,
) -> Result<bool> {
let err = |msg: String| -> anyhow::Error { Error::new(span, msg).into() };
Ok(match stability {
Stability::Unknown => true,
// NOTE: deprecations are intentionally omitted -- an existing
// `@since` takes precedence over `@deprecated`
Stability::Stable { since, .. } => {
let Some(p) = self.packages.get(*pkg_id) else {
// We can't check much without a package (possibly dealing
// with an item in an `UnresolvedPackage`), @since version &
// deprecations can't be checked because there's no package
// version to compare to.
//
// Feature requirements on stabilized features are ignored
// in resolved packages, so we do the same here.
return Ok(true);
};
// Use of feature gating with version specifiers inside a
// package that is not versioned is not allowed
let package_version = p.name.version.as_ref().ok_or_else(|| {
err(format!(
"package [{}] contains a feature gate with a version \
specifier, so it must have a version",
p.name
))
})?;
// If the version on the feature gate is:
// - released, then we can include it
// - unreleased, then we must check the feature (if present)
if since > package_version {
return Err(err(format!(
"feature gate cannot reference unreleased version \
{since} of package [{}] (current version {package_version})",
p.name
)));
}
true
}
Stability::Unstable { feature, .. } => {
self.features.contains(feature) || self.all_features
}
})
}
/// Convenience wrapper around `include_stability` specialized for types
/// with a more targeted error message.
fn include_type(&self, ty: &TypeDef, pkgid: PackageId, span: Span) -> Result<bool> {
self.include_stability(&ty.stability, &pkgid, span)
.with_context(|| {
format!(
"failed to process feature gate for type [{}] in package [{}]",
ty.name.as_ref().map(String::as_str).unwrap_or("<unknown>"),
self.packages[pkgid].name,
)
})
}
/// Performs the "elaboration process" necessary for the `world_id`
/// specified to ensure that all of its transitive imports are listed.
///
/// This function will take the unordered lists of the specified world's
/// imports and exports and "elaborate" them to ensure that they're
/// topographically sorted where all transitively required interfaces by
/// imports, or exports, are listed. This will additionally validate that
/// the exports are all valid and present, specifically with the restriction
/// noted on `elaborate_world_exports`.
///
/// The world is mutated in-place in this `Resolve`.
fn elaborate_world(&mut self, world_id: WorldId) -> Result<()> {
// First process all imports. This is easier than exports since the only
// requirement here is that all interfaces need to be added with a
// topological order between them.
let mut new_imports = IndexMap::default();
let world = &self.worlds[world_id];
// Sort the imports by "class" to ensure that this matches the order
// that items are printed and that items are in topological order.
//
// When printing worlds in WIT:
//
// * interfaces come first
// * types are next
// * type imports are first
// * type definitions are next
// * resource definitions have methods printed inline
// * freestanding functions are last
//
// This reflects the topological order between items where types
// can refer to imports and functions can refer to these types. Ordering
// within a single class (e.g. imports depending on each other, types
// referring to each other) is already preserved by other passes in this
// file and general AST resolution. That means that a stable sort here
// can be used to ensure that each class is in the right location
// relative to the others.
//
// Overall this ensures that round-trips of WIT through wasm should
// always produce the same result.
let sort_key = |resolve: &Resolve, item: &WorldItem| match item {
WorldItem::Interface { .. } => 0,
WorldItem::Type { id, .. } => {
let ty = &resolve.types[*id];
match ty.kind {
TypeDefKind::Type(Type::Id(t)) if resolve.types[t].owner != ty.owner => 1,
_ => 2,
}
}
WorldItem::Function(f) => {
if f.kind.resource().is_none() {
3
} else {
4
}
}
};
// Sort world items when we start to elaborate the world to start with a
// topological view of items.
let mut world_imports = world.imports.iter().collect::<Vec<_>>();
world_imports.sort_by_key(|(_name, import)| sort_key(self, import));
for (name, item) in world_imports {
match item {
// Interfaces get their dependencies added first followed by the
// interface itself.
WorldItem::Interface { id, stability, .. } => {
self.elaborate_world_import(&mut new_imports, name.clone(), *id, &stability);
}
// Functions are added as-is since their dependence on types in
// the world should already be satisfied.
WorldItem::Function(_) => {
let prev = new_imports.insert(name.clone(), item.clone());
assert!(prev.is_none());
}
// Types may depend on an interface, in which case a (possibly)
// recursive addition of that interface happens here. Afterwards
// the type itself can be added safely.
WorldItem::Type { id, .. } => {
if let Some(dep) = self.type_interface_dep(*id) {
self.elaborate_world_import(
&mut new_imports,
WorldKey::Interface(dep),
dep,
&self.types[*id].stability,
);
}
let prev = new_imports.insert(name.clone(), item.clone());
assert!(prev.is_none());
}
}
}
// Exports are trickier than imports, notably to uphold the invariant
// required by `elaborate_world_exports`. To do this the exports are
// partitioned into interfaces/functions. All functions are added to
// the new exports list during this loop but interfaces are all deferred
// to be handled in the `elaborate_world_exports` function.
let mut new_exports = IndexMap::default();
let mut export_interfaces = IndexMap::default();
for (name, item) in world.exports.iter() {
match item {
WorldItem::Interface { id, stability, .. } => {
let prev = export_interfaces.insert(*id, (name.clone(), stability));
assert!(prev.is_none());
}
WorldItem::Function(_) => {
let prev = new_exports.insert(name.clone(), item.clone());
assert!(prev.is_none());
}
WorldItem::Type { .. } => unreachable!(),
}
}
self.elaborate_world_exports(&export_interfaces, &mut new_imports, &mut new_exports)?;
// In addition to sorting at the start of elaboration also sort here at
// the end of elaboration to handle types being interspersed with
// interfaces as they're found.
new_imports.sort_by_cached_key(|_name, import| sort_key(self, import));
// And with all that done the world is updated in-place with
// imports/exports.
log::trace!("imports = {new_imports:?}");
log::trace!("exports = {new_exports:?}");
let world = &mut self.worlds[world_id];
world.imports = new_imports;
world.exports = new_exports;
Ok(())
}
fn elaborate_world_import(
&self,
imports: &mut IndexMap<WorldKey, WorldItem>,
key: WorldKey,
id: InterfaceId,
stability: &Stability,
) {
if imports.contains_key(&key) {
return;
}
for dep in self.interface_direct_deps(id) {
self.elaborate_world_import(imports, WorldKey::Interface(dep), dep, stability);
}
let prev = imports.insert(
key,
WorldItem::Interface {
id,
stability: stability.clone(),
span: Default::default(),
},
);
assert!(prev.is_none());
}
/// This function adds all of the interfaces in `export_interfaces` to the
/// list of exports of the `world` specified.
///
/// This method is more involved than adding imports because it is fallible.
/// Chiefly what can happen is that the dependencies of all exports must be
/// satisfied by other exports or imports, but not both. For example given a
/// situation such as:
///
/// ```wit
/// interface a {
/// type t = u32
/// }
/// interface b {
/// use a.{t}
/// }
/// interface c {
/// use a.{t}
/// use b.{t as t2}
/// }
/// ```
///
/// where `c` depends on `b` and `a` where `b` depends on `a`, then the
/// purpose of this method is to reject this world:
///
/// ```wit
/// world foo {
/// export a
/// export c
/// }
/// ```
///
/// The reasoning here is unfortunately subtle and is additionally the
/// subject of WebAssembly/component-model#208. Effectively the `c`
/// interface depends on `b`, but it's not listed explicitly as an import,
/// so it's then implicitly added as an import. This then transitively
/// depends on `a` so it's also added as an import. At this point though `c`
/// also depends on `a`, and it's also exported, so naively it should depend
/// on the export and not implicitly add an import. This means though that
/// `c` has access to two copies of `a`, one imported and one exported. This
/// is not valid, especially in the face of resource types.
///
/// Overall this method is tasked with rejecting the above world by walking
/// over all the exports and adding their dependencies. Each dependency is
/// recorded with whether it's required to be imported, and then if an
/// export is added for something that's required to be an error then the
/// operation fails.
fn elaborate_world_exports(
&self,
export_interfaces: &IndexMap<InterfaceId, (WorldKey, &Stability)>,
imports: &mut IndexMap<WorldKey, WorldItem>,
exports: &mut IndexMap<WorldKey, WorldItem>,
) -> Result<()> {
let mut required_imports = HashSet::new();
for (id, (key, stability)) in export_interfaces.iter() {
let name = self.name_world_key(&key);
let ok = add_world_export(
self,
imports,
exports,
export_interfaces,
&mut required_imports,
*id,
key,
true,
stability,
);
if !ok {
bail!(
// FIXME: this is not a great error message and basically no
// one will know what to do when it gets printed. Improving
// this error message, however, is a chunk of work that may
// not be best spent doing this at this time, so I'm writing
// this comment instead.
//
// More-or-less what should happen here is that a "path"
// from this interface to the conflicting interface should
// be printed. It should be explained why an import is being
// injected, why that's conflicting with an export, and
// ideally with a suggestion of "add this interface to the
// export list to fix this error".
//
// That's a lot of info that's not easy to get at without
// more refactoring, so it's left to a future date in the
// hopes that most folks won't actually run into this for
// the time being.
InvalidTransitiveDependency(name),
);
}
}
return Ok(());
fn add_world_export(
resolve: &Resolve,
imports: &mut IndexMap<WorldKey, WorldItem>,
exports: &mut IndexMap<WorldKey, WorldItem>,
export_interfaces: &IndexMap<InterfaceId, (WorldKey, &Stability)>,
required_imports: &mut HashSet<InterfaceId>,
id: InterfaceId,
key: &WorldKey,
add_export: bool,
stability: &Stability,
) -> bool {
if exports.contains_key(key) {
if add_export {
return true;
} else {
return false;
}
}
// If this is an import and it's already in the `required_imports`
// set then we can skip it as we've already visited this interface.
if !add_export && required_imports.contains(&id) {
return true;
}
let ok = resolve.interface_direct_deps(id).all(|dep| {
let key = WorldKey::Interface(dep);
let add_export = add_export && export_interfaces.contains_key(&dep);
add_world_export(
resolve,
imports,
exports,
export_interfaces,
required_imports,
dep,
&key,
add_export,
stability,
)
});
if !ok {
return false;
}
let item = WorldItem::Interface {
id,
stability: stability.clone(),
span: Default::default(),
};
if add_export {
if required_imports.contains(&id) {
return false;
}
exports.insert(key.clone(), item);
} else {
required_imports.insert(id);
imports.insert(key.clone(), item);
}
true
}
}
/// Remove duplicate imports from a world if they import from the same
/// interface with semver-compatible versions.
///
/// This will merge duplicate interfaces present at multiple versions in
/// both a world by selecting the larger version of the two interfaces. This
/// requires that the interfaces are indeed semver-compatible and it means
/// that some imports might be removed and replaced. Note that this is only
/// done within a single semver track, for example the world imports 0.2.0
/// and 0.2.1 then the result afterwards will be that it imports
/// 0.2.1. If, however, 0.3.0 where imported then the final result would
/// import both 0.2.0 and 0.3.0.
pub fn merge_world_imports_based_on_semver(&mut self, world_id: WorldId) -> Result<()> {
let world = &self.worlds[world_id];
// The first pass here is to build a map of "semver tracks" where they
// key is per-interface and the value is the maximal version found in
// that semver-compatible-track plus the interface which is the maximal
// version.
//
// At the same time a `to_remove` set is maintained to remember what
// interfaces are being removed from `from` and `into`. All of
// `to_remove` are placed with a known other version.
let mut semver_tracks = HashMap::new();
let mut to_remove = HashSet::new();
for (key, _) in world.imports.iter() {
let iface_id = match key {
WorldKey::Interface(id) => *id,
WorldKey::Name(_) => continue,
};
let (track, version) = match self.semver_track(iface_id) {
Some(track) => track,
None => continue,
};
log::debug!(
"{} is on track {}/{}",
self.id_of(iface_id).unwrap(),
track.0,
track.1,
);
match semver_tracks.entry(track.clone()) {
Entry::Vacant(e) => {
e.insert((version, iface_id));
}
Entry::Occupied(mut e) => match version.cmp(&e.get().0) {
Ordering::Greater => {
to_remove.insert(e.get().1);
e.insert((version, iface_id));
}
Ordering::Equal => {}
Ordering::Less => {
to_remove.insert(iface_id);
}
},
}
}
// Build a map of "this interface is replaced with this interface" using
// the results of the loop above.
let mut replacements = HashMap::new();
for id in to_remove {
let (track, _) = self.semver_track(id).unwrap();
let (_, latest) = semver_tracks[&track];
let prev = replacements.insert(id, latest);
assert!(prev.is_none());
}
// Explicit drop needed for hashbrown compatibility - hashbrown's HashMap
// destructor may access stored references, extending the borrow.
drop(semver_tracks);
// Validate that `merge_world_item` succeeds for merging all removed
// interfaces with their replacement. This is a double-check that the
// semver version is actually correct and all items present in the old
// interface are in the new.
for (to_replace, replace_with) in replacements.iter() {
self.merge_world_item(
&WorldItem::Interface {
id: *to_replace,
stability: Default::default(),
span: Default::default(),
},
&WorldItem::Interface {
id: *replace_with,
stability: Default::default(),
span: Default::default(),
},
)
.with_context(|| {
let old_name = self.id_of(*to_replace).unwrap();
let new_name = self.id_of(*replace_with).unwrap();
format!(
"failed to upgrade `{old_name}` to `{new_name}`, was \
this semver-compatible update not semver compatible?"
)
})?;
}
for (to_replace, replace_with) in replacements.iter() {
log::debug!(
"REPLACE {} => {}",
self.id_of(*to_replace).unwrap(),
self.id_of(*replace_with).unwrap(),
);
}
// Finally perform the actual transformation of the imports/exports.
// Here all imports are removed if they're replaced and otherwise all
// imports have their dependencies updated, possibly transitively, to
// point to the new interfaces in `replacements`.
//
// Afterwards exports are additionally updated, but only their
// dependencies on imports which were remapped. Exports themselves are
// not deduplicated and/or removed.
for (key, item) in mem::take(&mut self.worlds[world_id].imports) {
if let WorldItem::Interface { id, .. } = item {
if replacements.contains_key(&id) {
continue;
}
}
self.update_interface_deps_of_world_item(&item, &replacements);
let prev = self.worlds[world_id].imports.insert(key, item);
assert!(prev.is_none());
}
for (key, item) in mem::take(&mut self.worlds[world_id].exports) {
self.update_interface_deps_of_world_item(&item, &replacements);
let prev = self.worlds[world_id].exports.insert(key, item);
assert!(prev.is_none());
}
// Run through `elaborate_world` to reorder imports as appropriate and
// fill anything back in if it's actually required by exports. For now
// this doesn't tamper with exports at all. Also note that this is
// applied to all worlds in this `Resolve` because interfaces were
// modified directly.
let ids = self.worlds.iter().map(|(id, _)| id).collect::<Vec<_>>();
for world_id in ids {
self.elaborate_world(world_id).with_context(|| {
let name = &self.worlds[world_id].name;
format!(
"failed to elaborate world `{name}` after deduplicating imports \
based on semver"
)
})?;
}
#[cfg(debug_assertions)]
self.assert_valid();
Ok(())
}
fn update_interface_deps_of_world_item(
&mut self,
item: &WorldItem,
replacements: &HashMap<InterfaceId, InterfaceId>,
) {
match *item {
WorldItem::Type { id, .. } => self.update_interface_dep_of_type(id, &replacements),
WorldItem::Interface { id, .. } => {
let types = self.interfaces[id]
.types
.values()
.copied()
.collect::<Vec<_>>();
for ty in types {
self.update_interface_dep_of_type(ty, &replacements);
}
}
WorldItem::Function(_) => {}
}
}
/// Returns the "semver track" of an interface plus the interface's version.
///
/// This function returns `None` if the interface `id` has a package without
/// a version. If the version is present, however, the first element of the
/// tuple returned is a "semver track" for the specific interface. The
/// version listed in `PackageName` will be modified so all
/// semver-compatible versions are listed the same way.
///
/// The second element in the returned tuple is this interface's package's
/// version.
fn semver_track(&self, id: InterfaceId) -> Option<((PackageName, String), &Version)> {
let iface = &self.interfaces[id];
let pkg = &self.packages[iface.package?];
let version = pkg.name.version.as_ref()?;
let mut name = pkg.name.clone();
name.version = Some(PackageName::version_compat_track(version));
Some(((name, iface.name.clone()?), version))
}
/// If `ty` is a definition where it's a `use` from another interface, then
/// change what interface it's using from according to the pairs in the
/// `replacements` map.
fn update_interface_dep_of_type(
&mut self,
ty: TypeId,
replacements: &HashMap<InterfaceId, InterfaceId>,
) {
let to_replace = match self.type_interface_dep(ty) {
Some(id) => id,
None => return,
};
let replace_with = match replacements.get(&to_replace) {
Some(id) => id,
None => return,
};
let dep = match self.types[ty].kind {
TypeDefKind::Type(Type::Id(id)) => id,
_ => return,
};
let name = self.types[dep].name.as_ref().unwrap();
// Note the infallible name indexing happening here. This should be
// previously validated with `merge_world_item` to succeed.
let replacement_id = self.interfaces[*replace_with].types[name];
self.types[ty].kind = TypeDefKind::Type(Type::Id(replacement_id));
}
/// Returns the core wasm module/field names for the specified `import`.
///
/// This function will return the core wasm module/field that can be used to
/// use `import` with the name `mangling` scheme specified as well. This can
/// be useful for bindings generators, for example, and these names are
/// recognized by `wit-component` and `wasm-tools component new`.
pub fn wasm_import_name(
&self,
mangling: ManglingAndAbi,
import: WasmImport<'_>,
) -> (String, String) {
match mangling {
ManglingAndAbi::Standard32 => match import {
WasmImport::Func { interface, func } => {
let module = match interface {
Some(key) => format!("cm32p2|{}", self.name_canonicalized_world_key(key)),
None => format!("cm32p2"),
};
(module, func.name.clone())
}
WasmImport::ResourceIntrinsic {
interface,
resource,
intrinsic,
} => {
let name = self.types[resource].name.as_ref().unwrap();
let (prefix, name) = match intrinsic {
ResourceIntrinsic::ImportedDrop => ("", format!("{name}_drop")),
ResourceIntrinsic::ExportedDrop => ("_ex_", format!("{name}_drop")),
ResourceIntrinsic::ExportedNew => ("_ex_", format!("{name}_new")),
ResourceIntrinsic::ExportedRep => ("_ex_", format!("{name}_rep")),
};
let module = match interface {
Some(key) => {
format!("cm32p2|{prefix}{}", self.name_canonicalized_world_key(key))
}
None => {
assert_eq!(prefix, "");
format!("cm32p2")
}
};
(module, name)
}
WasmImport::FutureIntrinsic { .. } | WasmImport::StreamIntrinsic { .. } => {
panic!(
"at the time of writing, standard32 name mangling only supports the \
synchronous ABI and does not define future/stream intrinsic imports; \
use legacy mangling for these imports"
)
}
},
ManglingAndAbi::Legacy(abi) => match import {
WasmImport::Func { interface, func } => {
let module = match interface {
Some(key) => self.name_world_key(key),
None => format!("$root"),
};
(module, format!("{}{}", abi.import_prefix(), func.name))
}
WasmImport::ResourceIntrinsic {
interface,
resource,
intrinsic,
} => {
let name = self.types[resource].name.as_ref().unwrap();
let (prefix, name) = match intrinsic {
ResourceIntrinsic::ImportedDrop => ("", format!("[resource-drop]{name}")),
ResourceIntrinsic::ExportedDrop => {
("[export]", format!("[resource-drop]{name}"))
}
ResourceIntrinsic::ExportedNew => {
("[export]", format!("[resource-new]{name}"))
}
ResourceIntrinsic::ExportedRep => {
("[export]", format!("[resource-rep]{name}"))
}
};
let module = match interface {
Some(key) => format!("{prefix}{}", self.name_world_key(key)),
None => {
assert_eq!(prefix, "");
format!("$root")
}
};
(module, format!("{}{name}", abi.import_prefix()))
}
WasmImport::FutureIntrinsic {
interface,
func,
ty,
intrinsic,
exported,
async_,
} => {
let module_prefix = if exported { "[export]" } else { "" };
let module = match interface {
Some(key) => format!("{module_prefix}{}", self.name_world_key(key)),
None => format!("{module_prefix}$root"),
};
let type_index = match ty {
Some(ty) => func
.find_futures_and_streams(self)
.into_iter()
.position(|candidate| candidate == ty)
.unwrap_or_else(|| {
panic!(
"future type {ty:?} not found in `find_futures_and_streams` for `{}`",
func.name
)
})
.to_string(),
None => "unit".to_string(),
};
let (async_prefix, name) = match intrinsic {
FutureIntrinsic::New => {
assert!(!async_, "future.new cannot be async-lowered");
("", "new")
}
FutureIntrinsic::Read => {
(if async_ { "[async-lower]" } else { "" }, "read")
}
FutureIntrinsic::Write => {
(if async_ { "[async-lower]" } else { "" }, "write")
}
FutureIntrinsic::CancelRead => {
(if async_ { "[async-lower]" } else { "" }, "cancel-read")
}
FutureIntrinsic::CancelWrite => {
(if async_ { "[async-lower]" } else { "" }, "cancel-write")
}
FutureIntrinsic::DropReadable => {
assert!(!async_, "future.drop-readable cannot be async-lowered");
("", "drop-readable")
}
FutureIntrinsic::DropWritable => {
assert!(!async_, "future.drop-writable cannot be async-lowered");
("", "drop-writable")
}
};
(
module,
format!("{async_prefix}[future-{name}-{type_index}]{}", func.name),
)
}
WasmImport::StreamIntrinsic {
interface,
func,
ty,
intrinsic,
exported,
async_,
} => {
let module_prefix = if exported { "[export]" } else { "" };
let module = match interface {
Some(key) => format!("{module_prefix}{}", self.name_world_key(key)),
None => format!("{module_prefix}$root"),
};
let type_index = match ty {
Some(ty) => func
.find_futures_and_streams(self)
.into_iter()
.position(|candidate| candidate == ty)
.unwrap_or_else(|| {
panic!(
"stream type {ty:?} not found in `find_futures_and_streams` for `{}`",
func.name
)
})
.to_string(),
None => "unit".to_string(),
};
let (async_prefix, name) = match intrinsic {
StreamIntrinsic::New => {
assert!(!async_, "stream.new cannot be async-lowered");
("", "new")
}
StreamIntrinsic::Read => {
(if async_ { "[async-lower]" } else { "" }, "read")
}
StreamIntrinsic::Write => {
(if async_ { "[async-lower]" } else { "" }, "write")
}
StreamIntrinsic::CancelRead => {
(if async_ { "[async-lower]" } else { "" }, "cancel-read")
}
StreamIntrinsic::CancelWrite => {
(if async_ { "[async-lower]" } else { "" }, "cancel-write")
}
StreamIntrinsic::DropReadable => {
assert!(!async_, "stream.drop-readable cannot be async-lowered");
("", "drop-readable")
}
StreamIntrinsic::DropWritable => {
assert!(!async_, "stream.drop-writable cannot be async-lowered");
("", "drop-writable")
}
};
(
module,
format!("{async_prefix}[stream-{name}-{type_index}]{}", func.name),
)
}
},
}
}
/// Returns the core wasm export name for the specified `export`.
///
/// This is the same as [`Resolve::wasm_import_name`], except for exports.
pub fn wasm_export_name(&self, mangling: ManglingAndAbi, export: WasmExport<'_>) -> String {
match mangling {
ManglingAndAbi::Standard32 => match export {
WasmExport::Func {
interface,
func,
kind,
} => {
let mut name = String::from("cm32p2|");
if let Some(interface) = interface {
let s = self.name_canonicalized_world_key(interface);
name.push_str(&s);
}
name.push_str("|");
name.push_str(&func.name);
match kind {
WasmExportKind::Normal => {}
WasmExportKind::PostReturn => name.push_str("_post"),
WasmExportKind::Callback => todo!(
"not yet supported: \
async callback functions using standard name mangling"
),
}
name
}
WasmExport::ResourceDtor {
interface,
resource,
} => {
let name = self.types[resource].name.as_ref().unwrap();
let interface = self.name_canonicalized_world_key(interface);
format!("cm32p2|{interface}|{name}_dtor")
}
WasmExport::Memory => "cm32p2_memory".to_string(),
WasmExport::Initialize => "cm32p2_initialize".to_string(),
WasmExport::Realloc => "cm32p2_realloc".to_string(),
},
ManglingAndAbi::Legacy(abi) => match export {
WasmExport::Func {
interface,
func,
kind,
} => {
let mut name = abi.export_prefix().to_string();
match kind {
WasmExportKind::Normal => {}
WasmExportKind::PostReturn => name.push_str("cabi_post_"),
WasmExportKind::Callback => {
assert!(matches!(abi, LiftLowerAbi::AsyncCallback));
name = format!("[callback]{name}")
}
}
if let Some(interface) = interface {
let s = self.name_world_key(interface);
name.push_str(&s);
name.push_str("#");
}
name.push_str(&func.name);
name
}
WasmExport::ResourceDtor {
interface,
resource,
} => {
let name = self.types[resource].name.as_ref().unwrap();
let interface = self.name_world_key(interface);
format!("{}{interface}#[dtor]{name}", abi.export_prefix())
}
WasmExport::Memory => "memory".to_string(),
WasmExport::Initialize => "_initialize".to_string(),
WasmExport::Realloc => "cabi_realloc".to_string(),
},
}
}
/// This method will rewrite the `world` provided to ensure that, where
/// necessary, all types in interfaces referred to by the `world` have
/// nominal type ids for bindings generation.
///
/// The need for this method primarily arises from bindings generators
/// generating types in a programming language. Bindings generators try to
/// generate a type-per-WIT-type but this becomes problematic in situations
/// such as when an `interface` is both imported and exported. For example:
///
/// ```wit
/// interface x {
/// resource r;
/// }
///
/// world foo {
/// import x;
/// export x;
/// }
/// ```
///
/// Here the `r` resource, before this method, exists once within this
/// [`Resolve`]. This is a problem for bindings generators because guest
/// languages typically want to represent this world with two types: one
/// for the import and one for the export. This matches component model
/// semantics where `r` is a different type between the import and the
/// export.
///
/// The purpose of this method is to ensure that languages with nominal
/// types, where type identity is unique based on definition not structure,
/// will have an easier time generating bindings. This method will
/// duplicate the interface `x`, for example, and everything it contains.
/// This means that the `world foo` above will have a different
/// `InterfaceId` for the import and the export of `x`, despite them using
/// the same interface in WIT. This is intended to make bindings generators'
/// jobs much easier because now Id-uniqueness matches the semantic meaning
/// of the world as well.
///
/// This function will rewrite exported interfaces, as appropriate, to all
/// have unique ids if they would otherwise overlap with the imports.
pub fn generate_nominal_type_ids(&mut self, world: WorldId) {
let mut imports = HashSet::new();
// Build up a list of all imported interfaces, they're not changing and
// this is used to test for overlap between imports/exports.
for import in self.worlds[world].imports.values() {
if let WorldItem::Interface { id, .. } = import {
imports.insert(*id);
}
}
let mut to_clone = IndexMap::default();
for (i, export) in self.worlds[world].exports.values().enumerate() {
let id = match export {
WorldItem::Interface { id, .. } => *id,
// Functions can only refer to imported types so there's no need
// to rewrite anything as imports always stay as-is.
WorldItem::Function(_) => continue,
WorldItem::Type { .. } => unreachable!(),
};
// If this interface itself is both imported and exported, or if any
// dependency of this interface is rewritten, then the interface
// itself needs to be rewritten. Otherwise continue onwards.
let imported_and_exported = imports.contains(&id);
let any_dep_rewritten = self
.interface_direct_deps(id)
.any(|dep| to_clone.contains_key(&dep));
if !(imported_and_exported || any_dep_rewritten) {
continue;
}
to_clone.insert(id, i);
}
let mut maps = CloneMaps::default();
let mut cloner = clone::Cloner::new(
self,
&mut maps,
TypeOwner::World(world),
TypeOwner::World(world),
);
for (id, i) in to_clone {
// First, clone the interface. This'll make a `new_id`, and then we
// need to update the world to point to this new id. Note that the
// clones happen topologically here (due to iterating in-order
// above) and the `CloneMaps` are shared amongst interfaces. This
// means that future clones will use the types produced here too.
let mut new_id = id;
cloner.new_package = cloner.resolve.interfaces[new_id].package;
cloner.interface(&mut new_id);
// Load up the previous `key` and go ahead and mutate the
// `WorldItem` in place which is guaranteed to be an `Interface`
// because of the loop above.
let exports = &mut cloner.resolve.worlds[world].exports;
let (key, prev) = exports.get_index_mut(i).unwrap();
match prev {
WorldItem::Interface { id, .. } => *id = new_id,
_ => unreachable!(),
}
match key {
// If the key for this is an `Interface` then that means we
// need to update the key as well. Here that's replaced by-index
// in the `IndexMap` to preserve the same ordering as before,
// and this operation should always succeed since `new_id` is
// fresh, hence the `unwrap()`.
WorldKey::Interface(_) => {
exports
.replace_index(i, WorldKey::Interface(new_id))
.unwrap();
}
// Name-based keys don't need updating as they only contain a
// string, no ids.
WorldKey::Name(_) => {}
}
}
#[cfg(debug_assertions)]
self.assert_valid();
}
}
/// Possible imports that can be passed to [`Resolve::wasm_import_name`].
#[derive(Debug)]
pub enum WasmImport<'a> {
/// A WIT function is being imported. Optionally from an interface.
Func {
/// The name of the interface that the function is being imported from.
///
/// If the function is imported directly from the world then this is
/// `None`.
interface: Option<&'a WorldKey>,
/// The function being imported.
func: &'a Function,
},
/// A resource-related intrinsic is being imported.
ResourceIntrinsic {
/// The optional interface to import from, same as `WasmImport::Func`.
interface: Option<&'a WorldKey>,
/// The resource that's being operated on.
resource: TypeId,
/// The intrinsic that's being imported.
intrinsic: ResourceIntrinsic,
},
/// A future-related intrinsic is being imported.
FutureIntrinsic {
/// The optional interface to import from, same as `WasmImport::Func`.
interface: Option<&'a WorldKey>,
/// The function whose signature this future type appears in.
func: &'a Function,
/// The future type appearing in `func.find_futures_and_streams(resolve)`.
///
/// Use `None` for the special `unit` payload case.
ty: Option<TypeId>,
/// The intrinsic that's being imported.
intrinsic: FutureIntrinsic,
/// Whether this import is for an exported WIT function.
///
/// This controls whether the module name is prefixed with `[export]`.
exported: bool,
/// Whether this intrinsic import is async-lowered.
///
/// This is only valid for read/write/cancel intrinsics.
async_: bool,
},
/// A stream-related intrinsic is being imported.
StreamIntrinsic {
/// The optional interface to import from, same as `WasmImport::Func`.
interface: Option<&'a WorldKey>,
/// The function whose signature this stream type appears in.
func: &'a Function,
/// The stream type appearing in `func.find_futures_and_streams(resolve)`.
///
/// Use `None` for the special `unit` payload case.
ty: Option<TypeId>,
/// The intrinsic that's being imported.
intrinsic: StreamIntrinsic,
/// Whether this import is for an exported WIT function.
///
/// This controls whether the module name is prefixed with `[export]`.
exported: bool,
/// Whether this intrinsic import is async-lowered.
///
/// This is only valid for read/write/cancel intrinsics.
async_: bool,
},
}
/// Intrinsic definitions to go with [`WasmImport::ResourceIntrinsic`] which
/// also goes with [`Resolve::wasm_import_name`].
#[derive(Debug)]
pub enum ResourceIntrinsic {
ImportedDrop,
ExportedDrop,
ExportedNew,
ExportedRep,
}
/// Intrinsic definitions to go with [`WasmImport::FutureIntrinsic`] which
/// also goes with [`Resolve::wasm_import_name`].
#[derive(Debug)]
pub enum FutureIntrinsic {
New,
Read,
Write,
CancelRead,
CancelWrite,
DropReadable,
DropWritable,
}
/// Intrinsic definitions to go with [`WasmImport::StreamIntrinsic`] which
/// also goes with [`Resolve::wasm_import_name`].
#[derive(Debug)]
pub enum StreamIntrinsic {
New,
Read,
Write,
CancelRead,
CancelWrite,
DropReadable,
DropWritable,
}
/// Indicates whether a function export is a normal export, a post-return
/// function, or a callback function.
#[derive(Debug)]
pub enum WasmExportKind {
/// Normal function export.
Normal,
/// Post-return function.
PostReturn,
/// Async callback function.
Callback,
}
/// Different kinds of exports that can be passed to
/// [`Resolve::wasm_export_name`] to export from core wasm modules.
#[derive(Debug)]
pub enum WasmExport<'a> {
/// A WIT function is being exported, optionally from an interface.
Func {
/// An optional interface which owns `func`. Use `None` for top-level
/// world function.
interface: Option<&'a WorldKey>,
/// The function being exported.
func: &'a Function,
/// Kind of function (normal, post-return, or callback) being exported.
kind: WasmExportKind,
},
/// A destructor for a resource exported from this module.
ResourceDtor {
/// The interface that owns the resource.
interface: &'a WorldKey,
/// The resource itself that the destructor is for.
resource: TypeId,
},
/// Linear memory, the one that the canonical ABI uses.
Memory,
/// An initialization function (not the core wasm `start`).
Initialize,
/// The general-purpose realloc hook.
Realloc,
}
/// Structure returned by [`Resolve::merge`] which contains mappings from
/// old-ids to new-ids after the merge.
#[derive(Default)]
pub struct Remap {
pub types: Vec<Option<TypeId>>,
pub interfaces: Vec<Option<InterfaceId>>,
pub worlds: Vec<Option<WorldId>>,
pub packages: Vec<PackageId>,
/// A cache of anonymous `own<T>` handles for resource types.
///
/// The appending operation of `Remap` is the one responsible for
/// translating references to `T` where `T` is a resource into `own<T>`
/// instead. This map is used to deduplicate the `own<T>` types generated
/// to generate as few as possible.
///
/// The key of this map is the resource id `T` in the new resolve, and
/// the value is the `own<T>` type pointing to `T`.
own_handles: HashMap<TypeId, TypeId>,
type_has_borrow: Vec<Option<bool>>,
}
fn apply_map<T>(map: &[Option<Id<T>>], id: Id<T>, desc: &str, span: Span) -> Result<Id<T>> {
match map.get(id.index()) {
Some(Some(id)) => Ok(*id),
Some(None) => {
let msg = format!(
"found a reference to a {desc} which is excluded \
due to its feature not being activated"
);
Err(Error::new(span, msg).into())
}
None => panic!("request to remap a {desc} that has not yet been registered"),
}
}
fn rename(original_name: &str, include_name: &IncludeName) -> Option<String> {
if original_name == include_name.name {
return Some(include_name.as_.to_string());
}
let (kind, rest) = original_name.split_once(']')?;
match rest.split_once('.') {
Some((name, rest)) if name == include_name.name => {
Some(format!("{kind}]{}.{rest}", include_name.as_))
}
_ if rest == include_name.name => Some(format!("{kind}]{}", include_name.as_)),
_ => None,
}
}
impl Remap {
pub fn map_type(&self, id: TypeId, span: Span) -> Result<TypeId> {
apply_map(&self.types, id, "type", span)
}
pub fn map_interface(&self, id: InterfaceId, span: Span) -> Result<InterfaceId> {
apply_map(&self.interfaces, id, "interface", span)
}
pub fn map_world(&self, id: WorldId, span: Span) -> Result<WorldId> {
apply_map(&self.worlds, id, "world", span)
}
fn append(
&mut self,
resolve: &mut Resolve,
unresolved: UnresolvedPackage,
) -> Result<PackageId> {
let pkgid = resolve.packages.alloc(Package {
name: unresolved.name.clone(),
docs: unresolved.docs.clone(),
interfaces: Default::default(),
worlds: Default::default(),
});
let prev = resolve.package_names.insert(unresolved.name.clone(), pkgid);
if let Some(prev) = prev {
resolve.package_names.insert(unresolved.name.clone(), prev);
bail!(
"attempting to re-add package `{}` when it's already present in this `Resolve`",
unresolved.name,
);
}
self.process_foreign_deps(resolve, pkgid, &unresolved)?;
let foreign_types = self.types.len();
let foreign_interfaces = self.interfaces.len();
let foreign_worlds = self.worlds.len();
// Copy over all types first, updating any intra-type references. Note
// that types are sorted topologically which means this iteration
// order should be sufficient. Also note though that the interface
// owner of a type isn't updated here due to interfaces not being known
// yet.
for (id, mut ty) in unresolved.types.into_iter().skip(foreign_types) {
let span = ty.span;
if !resolve.include_type(&ty, pkgid, span)? {
self.types.push(None);
continue;
}
self.update_typedef(resolve, &mut ty, span)?;
let new_id = resolve.types.alloc(ty);
assert_eq!(self.types.len(), id.index());
let new_id = match resolve.types[new_id] {
// If this is an `own<T>` handle then either replace it with a
// preexisting `own<T>` handle which may have been generated in
// `update_ty`. If that doesn't exist though then insert it into
// the `own_handles` cache.
TypeDef {
name: None,
owner: TypeOwner::None,
kind: TypeDefKind::Handle(Handle::Own(id)),
docs: _,
stability: _,
span: _,
} => *self.own_handles.entry(id).or_insert(new_id),
// Everything not-related to `own<T>` doesn't get its ID
// modified.
_ => new_id,
};
self.types.push(Some(new_id));
}
// Next transfer all interfaces into `Resolve`, updating type ids
// referenced along the way.
for (id, mut iface) in unresolved.interfaces.into_iter().skip(foreign_interfaces) {
let span = iface.span;
if !resolve
.include_stability(&iface.stability, &pkgid, span)
.with_context(|| {
format!(
"failed to process feature gate for interface [{}] in package [{}]",
iface
.name
.as_ref()
.map(String::as_str)
.unwrap_or("<unknown>"),
resolve.packages[pkgid].name,
)
})?
{
self.interfaces.push(None);
continue;
}
assert!(iface.package.is_none());
iface.package = Some(pkgid);
self.update_interface(resolve, &mut iface)?;
let new_id = resolve.interfaces.alloc(iface);
assert_eq!(self.interfaces.len(), id.index());
self.interfaces.push(Some(new_id));
}
// Now that interfaces are identified go back through the types and
// update their interface owners.
for id in self.types.iter().skip(foreign_types) {
let id = match id {
Some(id) => *id,
None => continue,
};
let span = resolve.types[id].span;
match &mut resolve.types[id].owner {
TypeOwner::Interface(iface_id) => {
*iface_id = self.map_interface(*iface_id, span)
.with_context(|| {
"this type is not gated by a feature but its interface is gated by a feature"
})?;
}
TypeOwner::World(_) | TypeOwner::None => {}
}
}
// Expand worlds. Note that this does NOT process `include` statements,
// that's handled below. Instead this just handles world item updates
// and resolves references to types/items within `Resolve`.
//
// This is done after types/interfaces are fully settled so the
// transitive relation between interfaces, through types, is understood
// here.
for (id, mut world) in unresolved.worlds.into_iter().skip(foreign_worlds) {
let world_span = world.span;
if !resolve
.include_stability(&world.stability, &pkgid, world_span)
.with_context(|| {
format!(
"failed to process feature gate for world [{}] in package [{}]",
world.name, resolve.packages[pkgid].name,
)
})?
{
self.worlds.push(None);
continue;
}
self.update_world(&mut world, resolve, &pkgid)?;
let new_id = resolve.worlds.alloc(world);
assert_eq!(self.worlds.len(), id.index());
self.worlds.push(Some(new_id));
}
// As with interfaces, now update the ids of world-owned types.
for id in self.types.iter().skip(foreign_types) {
let id = match id {
Some(id) => *id,
None => continue,
};
let span = resolve.types[id].span;
match &mut resolve.types[id].owner {
TypeOwner::World(world_id) => {
*world_id = self.map_world(*world_id, span)
.with_context(|| {
"this type is not gated by a feature but its interface is gated by a feature"
})?;
}
TypeOwner::Interface(_) | TypeOwner::None => {}
}
}
// After the above, process `include` statements for worlds and
// additionally fully elaborate them. Processing of `include` is
// deferred until after the steps above so the fully resolved state of
// local types in this package are all available. This is required
// because `include` may copy types between worlds when the type is
// defined in the world itself.
//
// This step, after processing `include`, will also use
// `elaborate_world` to fully expand the world in terms of
// imports/exports and ensure that all necessary imports/exports are all
// listed.
//
// Note that `self.worlds` is already sorted in topological order so if
// one world refers to another via `include` then it's guaranteed that
// the one we're referring to is already expanded and ready to be
// included.
for id in self.worlds.iter().skip(foreign_worlds) {
let Some(id) = *id else {
continue;
};
self.process_world_includes(id, resolve, &pkgid)?;
let world_span = resolve.worlds[id].span;
resolve.elaborate_world(id).with_context(|| {
Error::new(
world_span,
format!(
"failed to elaborate world imports/exports of `{}`",
resolve.worlds[id].name
),
)
})?;
}
// Fixup "parent" ids now that everything has been identified
for id in self.interfaces.iter().skip(foreign_interfaces) {
let id = match id {
Some(id) => *id,
None => continue,
};
let iface = &mut resolve.interfaces[id];
iface.package = Some(pkgid);
if let Some(name) = &iface.name {
let prev = resolve.packages[pkgid].interfaces.insert(name.clone(), id);
assert!(prev.is_none());
}
}
for id in self.worlds.iter().skip(foreign_worlds) {
let id = match id {
Some(id) => *id,
None => continue,
};
let world = &mut resolve.worlds[id];
world.package = Some(pkgid);
let prev = resolve.packages[pkgid]
.worlds
.insert(world.name.clone(), id);
assert!(prev.is_none());
}
Ok(pkgid)
}
fn process_foreign_deps(
&mut self,
resolve: &mut Resolve,
pkgid: PackageId,
unresolved: &UnresolvedPackage,
) -> Result<()> {
// Invert the `foreign_deps` map to be keyed by world id to get
// used in the loops below.
let mut world_to_package = HashMap::new();
let mut interface_to_package = HashMap::new();
for (i, (pkg_name, worlds_or_ifaces)) in unresolved.foreign_deps.iter().enumerate() {
for (name, (item, stabilities)) in worlds_or_ifaces {
match item {
AstItem::Interface(unresolved_interface_id) => {
let prev = interface_to_package.insert(
*unresolved_interface_id,
(pkg_name, name, unresolved.foreign_dep_spans[i], stabilities),
);
assert!(prev.is_none());
}
AstItem::World(unresolved_world_id) => {
let prev = world_to_package.insert(
*unresolved_world_id,
(pkg_name, name, unresolved.foreign_dep_spans[i], stabilities),
);
assert!(prev.is_none());
}
}
}
}
// Connect all interfaces referred to in `interface_to_package`, which
// are at the front of `unresolved.interfaces`, to interfaces already
// contained within `resolve`.
self.process_foreign_interfaces(unresolved, &interface_to_package, resolve, &pkgid)?;
// Connect all worlds referred to in `world_to_package`, which
// are at the front of `unresolved.worlds`, to worlds already
// contained within `resolve`.
self.process_foreign_worlds(unresolved, &world_to_package, resolve, &pkgid)?;
// Finally, iterate over all foreign-defined types and determine
// what they map to.
self.process_foreign_types(unresolved, pkgid, resolve)?;
for (id, span) in unresolved.required_resource_types.iter() {
// Note that errors are ignored here because an error represents a
// type that has been configured away. If a type is configured away
// then any future use of it will generate an error so there's no
// need to validate that it's a resource here.
let Ok(mut id) = self.map_type(*id, *span) else {
continue;
};
loop {
match resolve.types[id].kind {
TypeDefKind::Type(Type::Id(i)) => id = i,
TypeDefKind::Resource => break,
_ => bail!(Error::new(
*span,
format!("type used in a handle must be a resource"),
)),
}
}
}
#[cfg(debug_assertions)]
resolve.assert_valid();
Ok(())
}
fn process_foreign_interfaces(
&mut self,
unresolved: &UnresolvedPackage,
interface_to_package: &HashMap<InterfaceId, (&PackageName, &String, Span, &Vec<Stability>)>,
resolve: &mut Resolve,
parent_pkg_id: &PackageId,
) -> Result<(), anyhow::Error> {
for (unresolved_iface_id, unresolved_iface) in unresolved.interfaces.iter() {
let (pkg_name, interface, span, stabilities) =
match interface_to_package.get(&unresolved_iface_id) {
Some(items) => *items,
// All foreign interfaces are defined first, so the first one
// which is defined in a non-foreign document means that all
// further interfaces will be non-foreign as well.
None => break,
};
let pkgid = resolve
.package_names
.get(pkg_name)
.copied()
.ok_or_else(|| {
PackageNotFoundError::new(
span,
pkg_name.clone(),
resolve.package_names.keys().cloned().collect(),
)
})?;
// Functions can't be imported so this should be empty.
assert!(unresolved_iface.functions.is_empty());
let pkg = &resolve.packages[pkgid];
let iface_span = unresolved_iface.span;
let mut enabled = false;
for stability in stabilities {
if resolve.include_stability(stability, parent_pkg_id, iface_span)? {
enabled = true;
break;
}
}
if !enabled {
self.interfaces.push(None);
continue;
}
let iface_id = pkg
.interfaces
.get(interface)
.copied()
.ok_or_else(|| Error::new(iface_span, "interface not found in package"))?;
assert_eq!(self.interfaces.len(), unresolved_iface_id.index());
self.interfaces.push(Some(iface_id));
}
for (id, _) in unresolved.interfaces.iter().skip(self.interfaces.len()) {
assert!(
interface_to_package.get(&id).is_none(),
"found foreign interface after local interface"
);
}
Ok(())
}
fn process_foreign_worlds(
&mut self,
unresolved: &UnresolvedPackage,
world_to_package: &HashMap<WorldId, (&PackageName, &String, Span, &Vec<Stability>)>,
resolve: &mut Resolve,
parent_pkg_id: &PackageId,
) -> Result<(), anyhow::Error> {
for (unresolved_world_id, unresolved_world) in unresolved.worlds.iter() {
let (pkg_name, world, span, stabilities) =
match world_to_package.get(&unresolved_world_id) {
Some(items) => *items,
// Same as above, all worlds are foreign until we find a
// non-foreign one.
None => break,
};
let pkgid = resolve
.package_names
.get(pkg_name)
.copied()
.ok_or_else(|| Error::new(span, "package not found"))?;
let pkg = &resolve.packages[pkgid];
let world_span = unresolved_world.span;
let mut enabled = false;
for stability in stabilities {
if resolve.include_stability(stability, parent_pkg_id, world_span)? {
enabled = true;
break;
}
}
if !enabled {
self.worlds.push(None);
continue;
}
let world_id = pkg
.worlds
.get(world)
.copied()
.ok_or_else(|| Error::new(world_span, "world not found in package"))?;
assert_eq!(self.worlds.len(), unresolved_world_id.index());
self.worlds.push(Some(world_id));
}
for (id, _) in unresolved.worlds.iter().skip(self.worlds.len()) {
assert!(
world_to_package.get(&id).is_none(),
"found foreign world after local world"
);
}
Ok(())
}
fn process_foreign_types(
&mut self,
unresolved: &UnresolvedPackage,
pkgid: PackageId,
resolve: &mut Resolve,
) -> Result<(), anyhow::Error> {
for (unresolved_type_id, unresolved_ty) in unresolved.types.iter() {
// All "Unknown" types should appear first so once we're no longer
// in unknown territory it's package-defined types so break out of
// this loop.
match unresolved_ty.kind {
TypeDefKind::Unknown => {}
_ => break,
}
let span = unresolved_ty.span;
if !resolve.include_type(unresolved_ty, pkgid, span)? {
self.types.push(None);
continue;
}
let unresolved_iface_id = match unresolved_ty.owner {
TypeOwner::Interface(id) => id,
_ => unreachable!(),
};
let iface_id = self.map_interface(unresolved_iface_id, Default::default())?;
let name = unresolved_ty.name.as_ref().unwrap();
let span = unresolved.unknown_type_spans[unresolved_type_id.index()];
let type_id = *resolve.interfaces[iface_id]
.types
.get(name)
.ok_or_else(|| {
Error::new(span, format!("type `{name}` not defined in interface"))
})?;
assert_eq!(self.types.len(), unresolved_type_id.index());
self.types.push(Some(type_id));
}
for (_, ty) in unresolved.types.iter().skip(self.types.len()) {
if let TypeDefKind::Unknown = ty.kind {
panic!("unknown type after defined type");
}
}
Ok(())
}
fn update_typedef(
&mut self,
resolve: &mut Resolve,
ty: &mut TypeDef,
span: Span,
) -> Result<()> {
// NB: note that `ty.owner` is not updated here since interfaces
// haven't been mapped yet and that's done in a separate step.
use crate::TypeDefKind::*;
match &mut ty.kind {
Handle(handle) => match handle {
crate::Handle::Own(ty) | crate::Handle::Borrow(ty) => {
self.update_type_id(ty, span)?
}
},
Resource => {}
Record(r) => {
for field in r.fields.iter_mut() {
self.update_ty(resolve, &mut field.ty, span)
.with_context(|| format!("failed to update field `{}`", field.name))?;
}
}
Tuple(t) => {
for ty in t.types.iter_mut() {
self.update_ty(resolve, ty, span)?;
}
}
Variant(v) => {
for case in v.cases.iter_mut() {
if let Some(t) = &mut case.ty {
self.update_ty(resolve, t, span)?;
}
}
}
Option(t)
| List(t, ..)
| FixedLengthList(t, ..)
| Future(Some(t))
| Stream(Some(t)) => self.update_ty(resolve, t, span)?,
Map(k, v) => {
self.update_ty(resolve, k, span)?;
self.update_ty(resolve, v, span)?;
}
Result(r) => {
if let Some(ty) = &mut r.ok {
self.update_ty(resolve, ty, span)?;
}
if let Some(ty) = &mut r.err {
self.update_ty(resolve, ty, span)?;
}
}
// Note that `update_ty` is specifically not used here as typedefs
// because for the `type a = b` form that doesn't force `a` to be a
// handle type if `b` is a resource type, instead `a` is
// simultaneously usable as a resource and a handle type
Type(crate::Type::Id(id)) => self.update_type_id(id, span)?,
Type(_) => {}
// nothing to do for these as they're just names or empty
Flags(_) | Enum(_) | Future(None) | Stream(None) => {}
Unknown => unreachable!(),
}
Ok(())
}
fn update_ty(&mut self, resolve: &mut Resolve, ty: &mut Type, span: Span) -> Result<()> {
let id = match ty {
Type::Id(id) => id,
_ => return Ok(()),
};
self.update_type_id(id, span)?;
// If `id` points to a `Resource` type then this means that what was
// just discovered was a reference to what will implicitly become an
// `own<T>` handle. This `own` handle is implicitly allocated here
// and handled during the merging process.
let mut cur = *id;
let points_to_resource = loop {
match resolve.types[cur].kind {
TypeDefKind::Type(Type::Id(id)) => cur = id,
TypeDefKind::Resource => break true,
_ => break false,
}
};
if points_to_resource {
*id = *self.own_handles.entry(*id).or_insert_with(|| {
resolve.types.alloc(TypeDef {
name: None,
owner: TypeOwner::None,
kind: TypeDefKind::Handle(Handle::Own(*id)),
docs: Default::default(),
stability: Default::default(),
span: Default::default(),
})
});
}
Ok(())
}
fn update_type_id(&self, id: &mut TypeId, span: Span) -> Result<()> {
*id = self.map_type(*id, span)?;
Ok(())
}
fn update_interface(&mut self, resolve: &mut Resolve, iface: &mut Interface) -> Result<()> {
iface.types.retain(|_, ty| self.types[ty.index()].is_some());
let iface_pkg_id = iface.package.as_ref().unwrap_or_else(|| {
panic!(
"unexpectedly missing package on interface [{}]",
iface
.name
.as_ref()
.map(String::as_str)
.unwrap_or("<unknown>"),
)
});
// NB: note that `iface.doc` is not updated here since interfaces
// haven't been mapped yet and that's done in a separate step.
for (_name, ty) in iface.types.iter_mut() {
self.update_type_id(ty, iface.span)?;
}
for (func_name, func) in iface.functions.iter_mut() {
let span = func.span;
if !resolve
.include_stability(&func.stability, iface_pkg_id, span)
.with_context(|| {
format!(
"failed to process feature gate for function [{func_name}] in package [{}]",
resolve.packages[*iface_pkg_id].name,
)
})?
{
continue;
}
self.update_function(resolve, func, span)
.with_context(|| format!("failed to update function `{}`", func.name))?;
}
// Filter out all of the existing functions in interface which fail the
// `include_stability()` check, as they shouldn't be available.
for (name, func) in mem::take(&mut iface.functions) {
if resolve.include_stability(&func.stability, iface_pkg_id, func.span)? {
iface.functions.insert(name, func);
}
}
Ok(())
}
fn update_function(
&mut self,
resolve: &mut Resolve,
func: &mut Function,
span: Span,
) -> Result<()> {
if let Some(id) = func.kind.resource_mut() {
self.update_type_id(id, span)?;
}
for param in func.params.iter_mut() {
self.update_ty(resolve, &mut param.ty, span)?;
}
if let Some(ty) = &mut func.result {
self.update_ty(resolve, ty, span)?;
}
if let Some(ty) = &func.result {
if self.type_has_borrow(resolve, ty) {
bail!(Error::new(
span,
format!(
"function returns a type which contains \
a `borrow<T>` which is not supported"
)
))
}
}
Ok(())
}
fn update_world(
&mut self,
world: &mut World,
resolve: &mut Resolve,
pkg_id: &PackageId,
) -> Result<()> {
// Rewrite imports/exports with their updated versions. Note that this
// may involve updating the key of the imports/exports maps so this
// starts by emptying them out and then everything is re-inserted.
let imports = mem::take(&mut world.imports).into_iter().map(|p| (p, true));
let exports = mem::take(&mut world.exports)
.into_iter()
.map(|p| (p, false));
for ((mut name, mut item), import) in imports.chain(exports) {
let span = item.span();
// Update the `id` eagerly here so `item.stability(..)` below
// works.
if let WorldItem::Type { id, .. } = &mut item {
*id = self.map_type(*id, span)?;
}
let stability = item.stability(resolve);
if !resolve
.include_stability(stability, pkg_id, span)
.with_context(|| format!("failed to process world item in `{}`", world.name))?
{
continue;
}
self.update_world_key(&mut name, span)?;
match &mut item {
WorldItem::Interface { id, .. } => {
*id = self.map_interface(*id, span)?;
}
WorldItem::Function(f) => {
self.update_function(resolve, f, span)?;
}
WorldItem::Type { .. } => {
// already mapped above
}
}
let dst = if import {
&mut world.imports
} else {
&mut world.exports
};
let prev = dst.insert(name, item);
assert!(prev.is_none());
}
Ok(())
}
fn process_world_includes(
&self,
id: WorldId,
resolve: &mut Resolve,
pkg_id: &PackageId,
) -> Result<()> {
let world = &mut resolve.worlds[id];
// Resolve all `include` statements of the world which will add more
// entries to the imports/exports list for this world.
let includes = mem::take(&mut world.includes);
for include in includes {
if !resolve
.include_stability(&include.stability, pkg_id, include.span)
.with_context(|| {
format!(
"failed to process feature gate for included world [{}] in package [{}]",
resolve.worlds[include.id].name.as_str(),
resolve.packages[*pkg_id].name
)
})?
{
continue;
}
self.resolve_include(
id,
include.id,
&include.names,
include.span,
pkg_id,
resolve,
)?;
}
// Validate that there are no case-insensitive duplicate names in imports/exports
Self::validate_world_case_insensitive_names(resolve, id)?;
Ok(())
}
/// Validates that a world's imports and exports don't have case-insensitive
/// duplicate names. Per the WIT specification, kebab-case identifiers are
/// case-insensitive within the same scope.
fn validate_world_case_insensitive_names(resolve: &Resolve, world_id: WorldId) -> Result<()> {
let world = &resolve.worlds[world_id];
// Helper closure to check for case-insensitive duplicates in a map
let validate_names = |items: &IndexMap<WorldKey, WorldItem>,
item_type: &str|
-> Result<()> {
let mut seen_lowercase: HashMap<String, String> = HashMap::new();
for key in items.keys() {
// Only WorldKey::Name variants can have case-insensitive conflicts
if let WorldKey::Name(name) = key {
let lowercase_name = name.to_lowercase();
if let Some(existing_name) = seen_lowercase.get(&lowercase_name) {
// Only error on case-insensitive duplicates (e.g., "foo" vs "FOO").
// Exact duplicates would have been caught earlier.
if existing_name != name {
bail!(
"{item_type} `{name}` conflicts with {item_type} `{existing_name}` \
(kebab-case identifiers are case-insensitive)"
);
}
}
seen_lowercase.insert(lowercase_name, name.clone());
}
}
Ok(())
};
validate_names(&world.imports, "import")
.with_context(|| format!("failed to validate imports in world `{}`", world.name))?;
validate_names(&world.exports, "export")
.with_context(|| format!("failed to validate exports in world `{}`", world.name))?;
Ok(())
}
fn update_world_key(&self, key: &mut WorldKey, span: Span) -> Result<()> {
match key {
WorldKey::Name(_) => {}
WorldKey::Interface(id) => {
*id = self.map_interface(*id, span)?;
}
}
Ok(())
}
fn resolve_include(
&self,
id: WorldId,
include_world_id_orig: WorldId,
names: &[IncludeName],
span: Span,
pkg_id: &PackageId,
resolve: &mut Resolve,
) -> Result<()> {
let world = &resolve.worlds[id];
let include_world_id = self.map_world(include_world_id_orig, span)?;
let include_world = resolve.worlds[include_world_id].clone();
let mut names_ = names.to_owned();
let is_external_include = world.package != include_world.package;
// remove all imports and exports that match the names we're including
for import in include_world.imports.iter() {
self.remove_matching_name(import, &mut names_);
}
for export in include_world.exports.iter() {
self.remove_matching_name(export, &mut names_);
}
if !names_.is_empty() {
bail!(Error::new(
span,
format!(
"no import or export kebab-name `{}`. Note that an ID does not support renaming",
names_[0].name
),
));
}
let mut maps = Default::default();
let mut cloner = clone::Cloner::new(
resolve,
&mut maps,
TypeOwner::World(if is_external_include {
include_world_id
} else {
include_world_id
// include_world_id_orig
}),
TypeOwner::World(id),
);
cloner.new_package = Some(*pkg_id);
// copy the imports and exports from the included world into the current world
for import in include_world.imports.iter() {
self.resolve_include_item(
&mut cloner,
names,
|resolve| &mut resolve.worlds[id].imports,
import,
span,
"import",
is_external_include,
)?;
}
for export in include_world.exports.iter() {
self.resolve_include_item(
&mut cloner,
names,
|resolve| &mut resolve.worlds[id].exports,
export,
span,
"export",
is_external_include,
)?;
}
Ok(())
}
fn resolve_include_item(
&self,
cloner: &mut clone::Cloner<'_>,
names: &[IncludeName],
get_items: impl Fn(&mut Resolve) -> &mut IndexMap<WorldKey, WorldItem>,
item: (&WorldKey, &WorldItem),
span: Span,
item_type: &str,
is_external_include: bool,
) -> Result<()> {
match item.0 {
WorldKey::Name(n) => {
let n = names
.into_iter()
.find_map(|include_name| rename(n, include_name))
.unwrap_or(n.clone());
// When the `with` option to the `include` directive is
// specified and is used to rename a function that means that
// the function's own original name needs to be updated, so
// reflect the change not only in the world key but additionally
// in the function itself.
let mut new_item = item.1.clone();
let key = WorldKey::Name(n.clone());
cloner.world_item(&key, &mut new_item);
match &mut new_item {
WorldItem::Function(f) => f.name = n.clone(),
WorldItem::Type { id, .. } => cloner.resolve.types[*id].name = Some(n.clone()),
WorldItem::Interface { .. } => {}
}
let prev = get_items(cloner.resolve).insert(key, new_item);
if prev.is_some() {
bail!(Error::new(
span,
format!("{item_type} of `{n}` shadows previously {item_type}ed items"),
))
}
}
key @ WorldKey::Interface(_) => {
let prev = get_items(cloner.resolve)
.entry(key.clone())
.or_insert(item.1.clone());
match (&item.1, prev) {
(
WorldItem::Interface {
id: aid,
stability: astability,
..
},
WorldItem::Interface {
id: bid,
stability: bstability,
..
},
) => {
assert_eq!(*aid, *bid);
merge_include_stability(astability, bstability, is_external_include)?;
}
(WorldItem::Interface { .. }, _) => unreachable!(),
(WorldItem::Function(_), _) => unreachable!(),
(WorldItem::Type { .. }, _) => unreachable!(),
}
}
};
Ok(())
}
fn remove_matching_name(&self, item: (&WorldKey, &WorldItem), names: &mut Vec<IncludeName>) {
match item.0 {
WorldKey::Name(n) => {
names.retain(|name| rename(n, name).is_none());
}
_ => {}
}
}
fn type_has_borrow(&mut self, resolve: &Resolve, ty: &Type) -> bool {
let id = match ty {
Type::Id(id) => *id,
_ => return false,
};
if let Some(Some(has_borrow)) = self.type_has_borrow.get(id.index()) {
return *has_borrow;
}
let result = self.typedef_has_borrow(resolve, &resolve.types[id]);
if self.type_has_borrow.len() <= id.index() {
self.type_has_borrow.resize(id.index() + 1, None);
}
self.type_has_borrow[id.index()] = Some(result);
result
}
fn typedef_has_borrow(&mut self, resolve: &Resolve, ty: &TypeDef) -> bool {
match &ty.kind {
TypeDefKind::Type(t) => self.type_has_borrow(resolve, t),
TypeDefKind::Variant(v) => v
.cases
.iter()
.filter_map(|case| case.ty.as_ref())
.any(|ty| self.type_has_borrow(resolve, ty)),
TypeDefKind::Handle(Handle::Borrow(_)) => true,
TypeDefKind::Handle(Handle::Own(_)) => false,
TypeDefKind::Resource => false,
TypeDefKind::Record(r) => r
.fields
.iter()
.any(|case| self.type_has_borrow(resolve, &case.ty)),
TypeDefKind::Flags(_) => false,
TypeDefKind::Tuple(t) => t.types.iter().any(|t| self.type_has_borrow(resolve, t)),
TypeDefKind::Enum(_) => false,
TypeDefKind::List(ty)
| TypeDefKind::FixedLengthList(ty, ..)
| TypeDefKind::Future(Some(ty))
| TypeDefKind::Stream(Some(ty))
| TypeDefKind::Option(ty) => self.type_has_borrow(resolve, ty),
TypeDefKind::Map(k, v) => {
self.type_has_borrow(resolve, k) || self.type_has_borrow(resolve, v)
}
TypeDefKind::Result(r) => [&r.ok, &r.err]
.iter()
.filter_map(|t| t.as_ref())
.any(|t| self.type_has_borrow(resolve, t)),
TypeDefKind::Future(None) | TypeDefKind::Stream(None) => false,
TypeDefKind::Unknown => unreachable!(),
}
}
}
struct MergeMap<'a> {
/// A map of package ids in `from` to those in `into` for those that are
/// found to be equivalent.
package_map: HashMap<PackageId, PackageId>,
/// A map of interface ids in `from` to those in `into` for those that are
/// found to be equivalent.
interface_map: HashMap<InterfaceId, InterfaceId>,
/// A map of type ids in `from` to those in `into` for those that are
/// found to be equivalent.
type_map: HashMap<TypeId, TypeId>,
/// A map of world ids in `from` to those in `into` for those that are
/// found to be equivalent.
world_map: HashMap<WorldId, WorldId>,
/// A list of documents that need to be added to packages in `into`.
///
/// The elements here are:
///
/// * The name of the interface/world
/// * The ID within `into` of the package being added to
/// * The ID within `from` of the item being added.
interfaces_to_add: Vec<(String, PackageId, InterfaceId)>,
worlds_to_add: Vec<(String, PackageId, WorldId)>,
/// Which `Resolve` is being merged from.
from: &'a Resolve,
/// Which `Resolve` is being merged into.
into: &'a Resolve,
}
impl<'a> MergeMap<'a> {
fn new(from: &'a Resolve, into: &'a Resolve) -> MergeMap<'a> {
MergeMap {
package_map: Default::default(),
interface_map: Default::default(),
type_map: Default::default(),
world_map: Default::default(),
interfaces_to_add: Default::default(),
worlds_to_add: Default::default(),
from,
into,
}
}
fn build(&mut self) -> Result<()> {
for from_id in self.from.topological_packages() {
let from = &self.from.packages[from_id];
let into_id = match self.into.package_names.get(&from.name) {
Some(id) => *id,
// This package, according to its name and url, is not present
// in `self` so it needs to get added below.
None => {
log::trace!("adding unique package {}", from.name);
continue;
}
};
log::trace!("merging duplicate package {}", from.name);
self.build_package(from_id, into_id).with_context(|| {
format!("failed to merge package `{}` into existing copy", from.name)
})?;
}
Ok(())
}
fn build_package(&mut self, from_id: PackageId, into_id: PackageId) -> Result<()> {
let prev = self.package_map.insert(from_id, into_id);
assert!(prev.is_none());
let from = &self.from.packages[from_id];
let into = &self.into.packages[into_id];
// If an interface is present in `from_id` but not present in `into_id`
// then it can be copied over wholesale. That copy is scheduled to
// happen within the `self.interfaces_to_add` list.
for (name, from_interface_id) in from.interfaces.iter() {
let into_interface_id = match into.interfaces.get(name) {
Some(id) => *id,
None => {
log::trace!("adding unique interface {name}");
self.interfaces_to_add
.push((name.clone(), into_id, *from_interface_id));
continue;
}
};
log::trace!("merging duplicate interfaces {name}");
self.build_interface(*from_interface_id, into_interface_id)
.with_context(|| format!("failed to merge interface `{name}`"))?;
}
for (name, from_world_id) in from.worlds.iter() {
let into_world_id = match into.worlds.get(name) {
Some(id) => *id,
None => {
log::trace!("adding unique world {name}");
self.worlds_to_add
.push((name.clone(), into_id, *from_world_id));
continue;
}
};
log::trace!("merging duplicate worlds {name}");
self.build_world(*from_world_id, into_world_id)
.with_context(|| format!("failed to merge world `{name}`"))?;
}
Ok(())
}
fn build_interface(&mut self, from_id: InterfaceId, into_id: InterfaceId) -> Result<()> {
let prev = self.interface_map.insert(from_id, into_id);
assert!(prev.is_none());
let from_interface = &self.from.interfaces[from_id];
let into_interface = &self.into.interfaces[into_id];
// When merging interfaces, types and functions that exist in both
// `from` and `into` must match structurally. Either side is allowed
// to have extra types or functions not present in the other, which
// enables commutative merging of partial views of the same
// interface. The only requirement is that the intersection of the
// two interfaces is compatible.
for (name, from_type_id) in from_interface.types.iter() {
let into_type_id = match into_interface.types.get(name) {
Some(id) => *id,
// Extra type in `from` not present in `into`; it will be
// moved as a new type and added to the interface later.
None => continue,
};
let prev = self.type_map.insert(*from_type_id, into_type_id);
assert!(prev.is_none());
self.build_type_id(*from_type_id, into_type_id)
.with_context(|| format!("mismatch in type `{name}`"))?;
}
for (name, from_func) in from_interface.functions.iter() {
let into_func = match into_interface.functions.get(name) {
Some(func) => func,
// Extra function in `from` not present in `into`; it will
// be added to the interface during the merge phase.
None => continue,
};
self.build_function(from_func, into_func)
.with_context(|| format!("mismatch in function `{name}`"))?;
}
Ok(())
}
fn build_type_id(&mut self, from_id: TypeId, into_id: TypeId) -> Result<()> {
// FIXME: ideally the types should be "structurally
// equal" but that's not trivial to do in the face of
// resources.
let _ = from_id;
let _ = into_id;
Ok(())
}
fn build_type(&mut self, from_ty: &Type, into_ty: &Type) -> Result<()> {
match (from_ty, into_ty) {
(Type::Id(from), Type::Id(into)) => {
self.build_type_id(*from, *into)?;
}
(from, into) if from != into => bail!("different kinds of types"),
_ => {}
}
Ok(())
}
fn build_function(&mut self, from_func: &Function, into_func: &Function) -> Result<()> {
if from_func.name != into_func.name {
bail!(
"different function names `{}` and `{}`",
from_func.name,
into_func.name
);
}
match (&from_func.kind, &into_func.kind) {
(FunctionKind::Freestanding, FunctionKind::Freestanding) => {}
(FunctionKind::AsyncFreestanding, FunctionKind::AsyncFreestanding) => {}
(FunctionKind::Method(from), FunctionKind::Method(into))
| (FunctionKind::Static(from), FunctionKind::Static(into))
| (FunctionKind::AsyncMethod(from), FunctionKind::AsyncMethod(into))
| (FunctionKind::AsyncStatic(from), FunctionKind::AsyncStatic(into))
| (FunctionKind::Constructor(from), FunctionKind::Constructor(into)) => {
self.build_type_id(*from, *into)
.context("different function kind types")?;
}
(FunctionKind::Method(_), _)
| (FunctionKind::Constructor(_), _)
| (FunctionKind::Static(_), _)
| (FunctionKind::Freestanding, _)
| (FunctionKind::AsyncFreestanding, _)
| (FunctionKind::AsyncMethod(_), _)
| (FunctionKind::AsyncStatic(_), _) => {
bail!("different function kind types")
}
}
if from_func.params.len() != into_func.params.len() {
bail!("different number of function parameters");
}
for (from_param, into_param) in from_func.params.iter().zip(&into_func.params) {
if from_param.name != into_param.name {
bail!(
"different function parameter names: {} != {}",
from_param.name,
into_param.name
);
}
self.build_type(&from_param.ty, &into_param.ty)
.with_context(|| {
format!(
"different function parameter types for `{}`",
from_param.name
)
})?;
}
match (&from_func.result, &into_func.result) {
(Some(from_ty), Some(into_ty)) => {
self.build_type(from_ty, into_ty)
.context("different function result types")?;
}
(None, None) => {}
(Some(_), None) | (None, Some(_)) => bail!("different number of function results"),
}
Ok(())
}
fn build_world(&mut self, from_id: WorldId, into_id: WorldId) -> Result<()> {
let prev = self.world_map.insert(from_id, into_id);
assert!(prev.is_none());
let from_world = &self.from.worlds[from_id];
let into_world = &self.into.worlds[into_id];
// Same as interfaces worlds are expected to exactly match to avoid
// unexpectedly changing a particular component's view of imports and
// exports.
//
// FIXME: this should probably share functionality with
// `Resolve::merge_worlds` to support adding imports but not changing
// exports.
if from_world.imports.len() != into_world.imports.len() {
bail!("world contains different number of imports than expected");
}
if from_world.exports.len() != into_world.exports.len() {
bail!("world contains different number of exports than expected");
}
for (from_name, from) in from_world.imports.iter() {
let into_name = MergeMap::map_name(from_name, &self.interface_map);
let name_str = self.from.name_world_key(from_name);
let into = into_world
.imports
.get(&into_name)
.ok_or_else(|| anyhow!("import `{name_str}` not found in target world"))?;
self.match_world_item(from, into)
.with_context(|| format!("import `{name_str}` didn't match target world"))?;
}
for (from_name, from) in from_world.exports.iter() {
let into_name = MergeMap::map_name(from_name, &self.interface_map);
let name_str = self.from.name_world_key(from_name);
let into = into_world
.exports
.get(&into_name)
.ok_or_else(|| anyhow!("export `{name_str}` not found in target world"))?;
self.match_world_item(from, into)
.with_context(|| format!("export `{name_str}` didn't match target world"))?;
}
Ok(())
}
fn map_name(
from_name: &WorldKey,
interface_map: &HashMap<InterfaceId, InterfaceId>,
) -> WorldKey {
match from_name {
WorldKey::Name(s) => WorldKey::Name(s.clone()),
WorldKey::Interface(id) => {
WorldKey::Interface(interface_map.get(id).copied().unwrap_or(*id))
}
}
}
fn match_world_item(&mut self, from: &WorldItem, into: &WorldItem) -> Result<()> {
match (from, into) {
(WorldItem::Interface { id: from, .. }, WorldItem::Interface { id: into, .. }) => {
match (
&self.from.interfaces[*from].name,
&self.into.interfaces[*into].name,
) {
// If one interface is unnamed then they must both be
// unnamed and they must both have the same structure for
// now.
(None, None) => self.build_interface(*from, *into)?,
// Otherwise both interfaces must be named and they must
// have been previously found to be equivalent. Note that
// if either is unnamed it won't be present in
// `interface_map` so this'll return an error.
_ => {
if self.interface_map.get(from) != Some(into) {
bail!("interfaces are not the same");
}
}
}
}
(WorldItem::Function(from), WorldItem::Function(into)) => {
let _ = (from, into);
// FIXME: should assert an check that `from` structurally
// matches `into`
}
(WorldItem::Type { id: from, .. }, WorldItem::Type { id: into, .. }) => {
// FIXME: should assert an check that `from` structurally
// matches `into`
let prev = self.type_map.insert(*from, *into);
assert!(prev.is_none());
}
(WorldItem::Interface { .. }, _)
| (WorldItem::Function(_), _)
| (WorldItem::Type { .. }, _) => {
bail!("world items do not have the same type")
}
}
Ok(())
}
}
/// Updates stability annotations when merging `from` into `into`.
///
/// This is done to keep up-to-date stability information if possible.
/// Components for example don't carry stability information but WIT does so
/// this tries to move from "unknown" to stable/unstable if possible.
fn update_stability(from: &Stability, into: &mut Stability) -> Result<()> {
// If `from` is unknown or the two stability annotations are equal then
// there's nothing to do here.
if from == into || from.is_unknown() {
return Ok(());
}
// Otherwise if `into` is unknown then inherit the stability listed in
// `from`.
if into.is_unknown() {
*into = from.clone();
return Ok(());
}
// Failing all that this means that the two attributes are different so
// generate an error.
bail!("mismatch in stability from '{from:?}' to '{into:?}'")
}
fn merge_include_stability(
from: &Stability,
into: &mut Stability,
is_external_include: bool,
) -> Result<()> {
if is_external_include && from.is_stable() {
log::trace!("dropped stability from external package");
*into = Stability::Unknown;
return Ok(());
}
return update_stability(from, into);
}
/// An error that can be returned during "world elaboration" during various
/// [`Resolve`] operations.
///
/// Methods on [`Resolve`] which mutate its internals, such as
/// [`Resolve::push_dir`] or [`Resolve::importize`] can fail if `world` imports
/// in WIT packages are invalid. This error indicates one of these situations
/// where an invalid dependency graph between imports and exports are detected.
///
/// Note that at this time this error is subtle and not easy to understand, and
/// work needs to be done to explain this better and additionally provide a
/// better error message. For now though this type enables callers to test for
/// the exact kind of error emitted.
#[derive(Debug, Clone)]
pub struct InvalidTransitiveDependency(String);
impl fmt::Display for InvalidTransitiveDependency {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"interface `{}` transitively depends on an interface in \
incompatible ways",
self.0
)
}
}
impl core::error::Error for InvalidTransitiveDependency {}
#[cfg(test)]
mod tests {
use crate::alloc::format;
use crate::alloc::string::{String, ToString};
use crate::alloc::vec::Vec;
use crate::{Resolve, WorldItem, WorldKey};
use anyhow::Result;
#[test]
fn select_world() -> Result<()> {
let mut resolve = Resolve::default();
resolve.push_str(
"test.wit",
r#"
package foo:bar@0.1.0;
world foo {}
"#,
)?;
resolve.push_str(
"test.wit",
r#"
package foo:baz@0.1.0;
world foo {}
"#,
)?;
resolve.push_str(
"test.wit",
r#"
package foo:baz@0.2.0;
world foo {}
"#,
)?;
let dummy = resolve.push_str(
"test.wit",
r#"
package foo:dummy;
world foo {}
"#,
)?;
assert!(resolve.select_world(&[dummy], None).is_ok());
assert!(resolve.select_world(&[dummy], Some("xx")).is_err());
assert!(resolve.select_world(&[dummy], Some("")).is_err());
assert!(resolve.select_world(&[dummy], Some("foo:bar/foo")).is_ok());
assert!(
resolve
.select_world(&[dummy], Some("foo:bar/foo@0.1.0"))
.is_ok()
);
assert!(resolve.select_world(&[dummy], Some("foo:baz/foo")).is_err());
assert!(
resolve
.select_world(&[dummy], Some("foo:baz/foo@0.1.0"))
.is_ok()
);
assert!(
resolve
.select_world(&[dummy], Some("foo:baz/foo@0.2.0"))
.is_ok()
);
Ok(())
}
#[test]
fn wasm_import_name_future_and_stream_intrinsics() -> Result<()> {
use crate::{FutureIntrinsic, LiftLowerAbi, ManglingAndAbi, StreamIntrinsic, WasmImport};
let mut resolve = Resolve::default();
let pkg = resolve.push_str(
"test.wit",
r#"
package foo:bar;
interface iface {
iface-func: func(x: future<u32>) -> stream<u32>;
}
world w {
import import-func: func(x: future<future<u32>>, y: u32) -> stream<string>;
export export-func: func(x: future, y: stream);
import iface;
export iface;
}
"#,
)?;
let world = resolve.packages[pkg].worlds["w"];
let world = &resolve.worlds[world];
let mangling = ManglingAndAbi::Legacy(LiftLowerAbi::AsyncStackful);
let WorldItem::Function(import_func) =
&world.imports[&WorldKey::Name("import-func".to_string())]
else {
panic!("expected `import-func` to be a top-level world import");
};
let WorldItem::Function(export_func) =
&world.exports[&WorldKey::Name("export-func".to_string())]
else {
panic!("expected `export-func` to be a top-level world export");
};
let import_types = import_func.find_futures_and_streams(&resolve);
assert_eq!(import_types.len(), 3);
let (interface_key, interface_func) = world
.imports
.iter()
.find_map(|(key, item)| match item {
WorldItem::Interface { id, .. } => Some((
key.clone(),
&resolve.interfaces[*id].functions["iface-func"],
)),
_ => None,
})
.expect("expected interface import");
let interface_types = interface_func.find_futures_and_streams(&resolve);
assert_eq!(interface_types.len(), 2);
let (module, name) = resolve.wasm_import_name(
mangling,
WasmImport::FutureIntrinsic {
interface: None,
func: import_func,
ty: Some(import_types[0]),
intrinsic: FutureIntrinsic::New,
exported: false,
async_: false,
},
);
assert_eq!(module, "$root");
assert_eq!(name, "[future-new-0]import-func");
let (module, name) = resolve.wasm_import_name(
mangling,
WasmImport::FutureIntrinsic {
interface: None,
func: import_func,
ty: Some(import_types[1]),
intrinsic: FutureIntrinsic::Read,
exported: false,
async_: true,
},
);
assert_eq!(module, "$root");
assert_eq!(name, "[async-lower][future-read-1]import-func");
let (module, name) = resolve.wasm_import_name(
mangling,
WasmImport::StreamIntrinsic {
interface: None,
func: import_func,
ty: Some(import_types[2]),
intrinsic: StreamIntrinsic::CancelRead,
exported: false,
async_: true,
},
);
assert_eq!(module, "$root");
assert_eq!(name, "[async-lower][stream-cancel-read-2]import-func");
let (module, name) = resolve.wasm_import_name(
mangling,
WasmImport::FutureIntrinsic {
interface: None,
func: export_func,
ty: None,
intrinsic: FutureIntrinsic::DropReadable,
exported: true,
async_: false,
},
);
assert_eq!(module, "[export]$root");
assert_eq!(name, "[future-drop-readable-unit]export-func");
let (module, name) = resolve.wasm_import_name(
mangling,
WasmImport::StreamIntrinsic {
interface: None,
func: export_func,
ty: None,
intrinsic: StreamIntrinsic::Write,
exported: true,
async_: true,
},
);
assert_eq!(module, "[export]$root");
assert_eq!(name, "[async-lower][stream-write-unit]export-func");
let (module, name) = resolve.wasm_import_name(
mangling,
WasmImport::StreamIntrinsic {
interface: Some(&interface_key),
func: interface_func,
ty: Some(interface_types[1]),
intrinsic: StreamIntrinsic::Read,
exported: true,
async_: false,
},
);
assert_eq!(
module,
format!("[export]{}", resolve.name_world_key(&interface_key))
);
assert_eq!(name, "[stream-read-1]iface-func");
Ok(())
}
/// When there are multiple packages and there's no main package, don't
/// pick a world just based on it being the only one that matches.
#[test]
fn select_world_multiple_packages() -> Result<()> {
use wit_parser::Resolve;
let mut resolve = Resolve::default();
// Just one world in one package; we always succeed.
let stuff = resolve.push_str(
"./my-test.wit",
r#"
package test:stuff;
world foo {
// ...
}
"#,
)?;
assert!(resolve.select_world(&[stuff], None).is_ok());
assert!(resolve.select_world(&[stuff], Some("foo")).is_ok());
// Multiple packages, but still just one total world. Lookups
// without a main package now fail.
let empty = resolve.push_str(
"./my-test.wit",
r#"
package test:empty;
"#,
)?;
assert!(resolve.select_world(&[stuff, empty], None).is_err());
assert!(resolve.select_world(&[stuff, empty], Some("foo")).is_err());
assert!(resolve.select_world(&[empty], None).is_err());
assert!(resolve.select_world(&[empty], Some("foo")).is_err());
Ok(())
}
/// Test selecting a world with multiple versions of a package name.
#[test]
fn select_world_versions() -> Result<()> {
use wit_parser::Resolve;
let mut resolve = Resolve::default();
let _id = resolve.push_str(
"./my-test.wit",
r#"
package example:distraction;
"#,
)?;
// When selecting with a version it's ok to drop the version when
// there's only a single copy of that package in `Resolve`.
let versions_1 = resolve.push_str(
"./my-test.wit",
r#"
package example:versions@1.0.0;
world foo { /* ... */ }
"#,
)?;
assert!(resolve.select_world(&[versions_1], Some("foo")).is_ok());
assert!(
resolve
.select_world(&[versions_1], Some("foo@1.0.0"))
.is_err()
);
assert!(
resolve
.select_world(&[versions_1], Some("example:versions/foo"))
.is_ok()
);
assert!(
resolve
.select_world(&[versions_1], Some("example:versions/foo@1.0.0"))
.is_ok()
);
// However when a single package has multiple versions in a resolve
// it's required to specify the version to select which one.
let versions_2 = resolve.push_str(
"./my-test.wit",
r#"
package example:versions@2.0.0;
world foo { /* ... */ }
"#,
)?;
assert!(
resolve
.select_world(&[versions_1, versions_2], Some("foo"))
.is_err()
);
assert!(
resolve
.select_world(&[versions_1, versions_2], Some("foo@1.0.0"))
.is_err()
);
assert!(
resolve
.select_world(&[versions_1, versions_2], Some("foo@2.0.0"))
.is_err()
);
assert!(
resolve
.select_world(&[versions_1, versions_2], Some("example:versions/foo"))
.is_err()
);
assert!(
resolve
.select_world(
&[versions_1, versions_2],
Some("example:versions/foo@1.0.0")
)
.is_ok()
);
assert!(
resolve
.select_world(
&[versions_1, versions_2],
Some("example:versions/foo@2.0.0")
)
.is_ok()
);
Ok(())
}
/// Test overriding a main package using name qualification
#[test]
fn select_world_override_qualification() -> Result<()> {
use wit_parser::Resolve;
let mut resolve = Resolve::default();
let other = resolve.push_str(
"./my-test.wit",
r#"
package example:other;
world foo { }
"#,
)?;
// A fully-qualified name overrides a main package.
let fq = resolve.push_str(
"./my-test.wit",
r#"
package example:fq;
world bar { }
"#,
)?;
assert!(resolve.select_world(&[other, fq], Some("foo")).is_err());
assert!(resolve.select_world(&[other, fq], Some("bar")).is_err());
assert!(
resolve
.select_world(&[other, fq], Some("example:other/foo"))
.is_ok()
);
assert!(
resolve
.select_world(&[other, fq], Some("example:fq/bar"))
.is_ok()
);
assert!(
resolve
.select_world(&[other, fq], Some("example:other/bar"))
.is_err()
);
assert!(
resolve
.select_world(&[other, fq], Some("example:fq/foo"))
.is_err()
);
Ok(())
}
/// Test selecting with fully-qualified world names.
#[test]
fn select_world_fully_qualified() -> Result<()> {
use wit_parser::Resolve;
let mut resolve = Resolve::default();
let distraction = resolve.push_str(
"./my-test.wit",
r#"
package example:distraction;
"#,
)?;
// If a package has multiple worlds, then we can't guess the world
// even if we know the package.
let multiworld = resolve.push_str(
"./my-test.wit",
r#"
package example:multiworld;
world foo { /* ... */ }
world bar { /* ... */ }
"#,
)?;
assert!(
resolve
.select_world(&[distraction, multiworld], None)
.is_err()
);
assert!(
resolve
.select_world(&[distraction, multiworld], Some("foo"))
.is_err()
);
assert!(
resolve
.select_world(&[distraction, multiworld], Some("example:multiworld/foo"))
.is_ok()
);
assert!(
resolve
.select_world(&[distraction, multiworld], Some("bar"))
.is_err()
);
assert!(
resolve
.select_world(&[distraction, multiworld], Some("example:multiworld/bar"))
.is_ok()
);
Ok(())
}
/// Test `select_world` with single and multiple packages.
#[test]
fn select_world_packages() -> Result<()> {
use wit_parser::Resolve;
let mut resolve = Resolve::default();
// If there's a single package and only one world, that world is
// the obvious choice.
let wit1 = resolve.push_str(
"./my-test.wit",
r#"
package example:wit1;
world foo {
// ...
}
"#,
)?;
assert!(resolve.select_world(&[wit1], None).is_ok());
assert!(resolve.select_world(&[wit1], Some("foo")).is_ok());
assert!(
resolve
.select_world(&[wit1], Some("example:wit1/foo"))
.is_ok()
);
assert!(resolve.select_world(&[wit1], Some("bar")).is_err());
assert!(
resolve
.select_world(&[wit1], Some("example:wit2/foo"))
.is_err()
);
// If there are multiple packages, we need to be told which package
// to use.
let wit2 = resolve.push_str(
"./my-test.wit",
r#"
package example:wit2;
world foo { /* ... */ }
"#,
)?;
assert!(resolve.select_world(&[wit1, wit2], None).is_err());
assert!(resolve.select_world(&[wit1, wit2], Some("foo")).is_err());
assert!(
resolve
.select_world(&[wit1, wit2], Some("example:wit1/foo"))
.is_ok()
);
assert!(resolve.select_world(&[wit2], None).is_ok());
assert!(resolve.select_world(&[wit2], Some("foo")).is_ok());
assert!(
resolve
.select_world(&[wit2], Some("example:wit1/foo"))
.is_ok()
);
assert!(resolve.select_world(&[wit1, wit2], Some("bar")).is_err());
assert!(
resolve
.select_world(&[wit1, wit2], Some("example:wit2/foo"))
.is_ok()
);
assert!(resolve.select_world(&[wit2], Some("bar")).is_err());
assert!(
resolve
.select_world(&[wit2], Some("example:wit2/foo"))
.is_ok()
);
Ok(())
}
#[test]
fn span_preservation() -> Result<()> {
let mut resolve = Resolve::default();
let pkg = resolve.push_str(
"test.wit",
r#"
package foo:bar;
interface my-iface {
type my-type = u32;
my-func: func();
}
world my-world {
export my-export: func();
}
"#,
)?;
let iface_id = resolve.packages[pkg].interfaces["my-iface"];
assert!(resolve.interfaces[iface_id].span.is_known());
let type_id = resolve.interfaces[iface_id].types["my-type"];
assert!(resolve.types[type_id].span.is_known());
assert!(
resolve.interfaces[iface_id].functions["my-func"]
.span
.is_known()
);
let world_id = resolve.packages[pkg].worlds["my-world"];
assert!(resolve.worlds[world_id].span.is_known());
let WorldItem::Function(f) =
&resolve.worlds[world_id].exports[&WorldKey::Name("my-export".to_string())]
else {
panic!("expected function");
};
assert!(f.span.is_known());
Ok(())
}
#[test]
fn span_preservation_through_merge() -> Result<()> {
let mut resolve1 = Resolve::default();
resolve1.push_str(
"test1.wit",
r#"
package foo:bar;
interface iface1 {
type type1 = u32;
func1: func();
}
"#,
)?;
let mut resolve2 = Resolve::default();
let pkg2 = resolve2.push_str(
"test2.wit",
r#"
package foo:baz;
interface iface2 {
type type2 = string;
func2: func();
}
"#,
)?;
let iface2_old_id = resolve2.packages[pkg2].interfaces["iface2"];
let remap = resolve1.merge(resolve2)?;
let iface2_id = remap.interfaces[iface2_old_id.index()].unwrap();
assert!(resolve1.interfaces[iface2_id].span.is_known());
let type2_id = resolve1.interfaces[iface2_id].types["type2"];
assert!(resolve1.types[type2_id].span.is_known());
assert!(
resolve1.interfaces[iface2_id].functions["func2"]
.span
.is_known()
);
Ok(())
}
#[test]
fn span_preservation_through_include() -> Result<()> {
let mut resolve = Resolve::default();
let pkg = resolve.push_str(
"test.wit",
r#"
package foo:bar;
world base {
export my-func: func();
}
world extended {
include base;
}
"#,
)?;
let base_id = resolve.packages[pkg].worlds["base"];
let extended_id = resolve.packages[pkg].worlds["extended"];
let WorldItem::Function(base_func) =
&resolve.worlds[base_id].exports[&WorldKey::Name("my-func".to_string())]
else {
panic!("expected function");
};
assert!(base_func.span.is_known());
let WorldItem::Function(extended_func) =
&resolve.worlds[extended_id].exports[&WorldKey::Name("my-func".to_string())]
else {
panic!("expected function");
};
assert!(extended_func.span.is_known());
Ok(())
}
#[test]
fn span_preservation_through_include_with_rename() -> Result<()> {
let mut resolve = Resolve::default();
let pkg = resolve.push_str(
"test.wit",
r#"
package foo:bar;
world base {
export original-name: func();
}
world extended {
include base with { original-name as renamed-func }
}
"#,
)?;
let extended_id = resolve.packages[pkg].worlds["extended"];
let WorldItem::Function(f) =
&resolve.worlds[extended_id].exports[&WorldKey::Name("renamed-func".to_string())]
else {
panic!("expected function");
};
assert!(f.span.is_known());
assert!(
!resolve.worlds[extended_id]
.exports
.contains_key(&WorldKey::Name("original-name".to_string()))
);
Ok(())
}
/// Test that spans work when included world is defined after the including world
#[test]
fn span_preservation_through_include_reverse_order() -> Result<()> {
let mut resolve = Resolve::default();
let pkg = resolve.push_str(
"test.wit",
r#"
package foo:bar;
world extended {
include base;
}
world base {
export my-func: func();
}
"#,
)?;
let base_id = resolve.packages[pkg].worlds["base"];
let extended_id = resolve.packages[pkg].worlds["extended"];
let WorldItem::Function(base_func) =
&resolve.worlds[base_id].exports[&WorldKey::Name("my-func".to_string())]
else {
panic!("expected function");
};
assert!(base_func.span.is_known());
let WorldItem::Function(extended_func) =
&resolve.worlds[extended_id].exports[&WorldKey::Name("my-func".to_string())]
else {
panic!("expected function");
};
assert!(extended_func.span.is_known());
Ok(())
}
#[test]
fn span_line_numbers() -> Result<()> {
let mut resolve = Resolve::default();
let pkg = resolve.push_source(
"test.wit",
"package foo:bar;
interface my-iface {
type my-type = u32;
my-func: func();
}
world my-world {
export my-export: func();
}
",
)?;
let iface_id = resolve.packages[pkg].interfaces["my-iface"];
let iface_span = resolve.interfaces[iface_id].span;
let iface_loc = resolve.render_location(iface_span);
assert!(
iface_loc.contains(":3:"),
"interface location was {iface_loc}"
);
let type_id = resolve.interfaces[iface_id].types["my-type"];
let type_span = resolve.types[type_id].span;
let type_loc = resolve.render_location(type_span);
assert!(type_loc.contains(":4:"), "type location was {type_loc}");
let func_span = resolve.interfaces[iface_id].functions["my-func"].span;
let func_loc = resolve.render_location(func_span);
assert!(func_loc.contains(":5:"), "function location was {func_loc}");
let world_id = resolve.packages[pkg].worlds["my-world"];
let world_span = resolve.worlds[world_id].span;
let world_loc = resolve.render_location(world_span);
assert!(world_loc.contains(":8:"), "world location was {world_loc}");
let WorldItem::Function(export_func) =
&resolve.worlds[world_id].exports[&WorldKey::Name("my-export".to_string())]
else {
panic!("expected function");
};
let export_loc = resolve.render_location(export_func.span);
assert!(
export_loc.contains(":9:"),
"export location was {export_loc}"
);
Ok(())
}
#[test]
fn span_line_numbers_through_merge() -> Result<()> {
let mut resolve1 = Resolve::default();
resolve1.push_source(
"first.wit",
"package foo:first;
interface iface1 {
func1: func();
}
",
)?;
let mut resolve2 = Resolve::default();
let pkg2 = resolve2.push_source(
"second.wit",
"package foo:second;
interface iface2 {
func2: func();
}
",
)?;
let iface2_old_id = resolve2.packages[pkg2].interfaces["iface2"];
let remap = resolve1.merge(resolve2)?;
let iface2_id = remap.interfaces[iface2_old_id.index()].unwrap();
let iface2_span = resolve1.interfaces[iface2_id].span;
let iface2_loc = resolve1.render_location(iface2_span);
assert!(
iface2_loc.contains("second.wit"),
"should reference second.wit, got {iface2_loc}"
);
assert!(
iface2_loc.contains(":3:"),
"interface should be on line 3, got {iface2_loc}"
);
let func2_span = resolve1.interfaces[iface2_id].functions["func2"].span;
let func2_loc = resolve1.render_location(func2_span);
assert!(
func2_loc.contains("second.wit"),
"should reference second.wit, got {func2_loc}"
);
assert!(
func2_loc.contains(":4:"),
"function should be on line 4, got {func2_loc}"
);
Ok(())
}
#[test]
fn span_line_numbers_multiple_sources() -> Result<()> {
let mut resolve = Resolve::default();
let pkg1 = resolve.push_source(
"first.wit",
"package test:first;
interface first-iface {
first-func: func();
}
",
)?;
let pkg2 = resolve.push_source(
"second.wit",
"package test:second;
interface second-iface {
second-func: func();
}
",
)?;
let iface1_id = resolve.packages[pkg1].interfaces["first-iface"];
let iface1_span = resolve.interfaces[iface1_id].span;
let iface1_loc = resolve.render_location(iface1_span);
assert!(
iface1_loc.contains("first.wit"),
"should reference first.wit, got {iface1_loc}"
);
assert!(
iface1_loc.contains(":3:"),
"interface should be on line 3, got {iface1_loc}"
);
let func1_span = resolve.interfaces[iface1_id].functions["first-func"].span;
let func1_loc = resolve.render_location(func1_span);
assert!(
func1_loc.contains("first.wit"),
"should reference first.wit, got {func1_loc}"
);
assert!(
func1_loc.contains(":4:"),
"function should be on line 4, got {func1_loc}"
);
let iface2_id = resolve.packages[pkg2].interfaces["second-iface"];
let iface2_span = resolve.interfaces[iface2_id].span;
let iface2_loc = resolve.render_location(iface2_span);
assert!(
iface2_loc.contains("second.wit"),
"should reference second.wit, got {iface2_loc}"
);
assert!(
iface2_loc.contains(":3:"),
"interface should be on line 3, got {iface2_loc}"
);
let func2_span = resolve.interfaces[iface2_id].functions["second-func"].span;
let func2_loc = resolve.render_location(func2_span);
assert!(
func2_loc.contains("second.wit"),
"should reference second.wit, got {func2_loc}"
);
assert!(
func2_loc.contains(":4:"),
"function should be on line 4, got {func2_loc}"
);
Ok(())
}
#[test]
fn span_preservation_for_fields_and_cases() -> Result<()> {
use crate::TypeDefKind;
let mut resolve = Resolve::default();
let pkg = resolve.push_str(
"test.wit",
r#"
package foo:bar;
interface my-iface {
record my-record {
field1: u32,
field2: string,
}
flags my-flags {
flag1,
flag2,
}
variant my-variant {
case1,
case2(u32),
}
enum my-enum {
val1,
val2,
}
}
"#,
)?;
let iface_id = resolve.packages[pkg].interfaces["my-iface"];
// Check record fields have spans
let record_id = resolve.interfaces[iface_id].types["my-record"];
let TypeDefKind::Record(record) = &resolve.types[record_id].kind else {
panic!("expected record");
};
assert!(record.fields[0].span.is_known(), "field1 should have span");
assert!(record.fields[1].span.is_known(), "field2 should have span");
// Check flags have spans
let flags_id = resolve.interfaces[iface_id].types["my-flags"];
let TypeDefKind::Flags(flags) = &resolve.types[flags_id].kind else {
panic!("expected flags");
};
assert!(flags.flags[0].span.is_known(), "flag1 should have span");
assert!(flags.flags[1].span.is_known(), "flag2 should have span");
// Check variant cases have spans
let variant_id = resolve.interfaces[iface_id].types["my-variant"];
let TypeDefKind::Variant(variant) = &resolve.types[variant_id].kind else {
panic!("expected variant");
};
assert!(variant.cases[0].span.is_known(), "case1 should have span");
assert!(variant.cases[1].span.is_known(), "case2 should have span");
// Check enum cases have spans
let enum_id = resolve.interfaces[iface_id].types["my-enum"];
let TypeDefKind::Enum(e) = &resolve.types[enum_id].kind else {
panic!("expected enum");
};
assert!(e.cases[0].span.is_known(), "val1 should have span");
assert!(e.cases[1].span.is_known(), "val2 should have span");
Ok(())
}
#[test]
fn span_preservation_for_fields_through_merge() -> Result<()> {
use crate::TypeDefKind;
let mut resolve1 = Resolve::default();
resolve1.push_str(
"test1.wit",
r#"
package foo:bar;
interface iface1 {
record rec1 {
f1: u32,
}
}
"#,
)?;
let mut resolve2 = Resolve::default();
let pkg2 = resolve2.push_str(
"test2.wit",
r#"
package foo:baz;
interface iface2 {
record rec2 {
f2: string,
}
variant var2 {
c2,
}
}
"#,
)?;
let iface2_old_id = resolve2.packages[pkg2].interfaces["iface2"];
let rec2_old_id = resolve2.interfaces[iface2_old_id].types["rec2"];
let var2_old_id = resolve2.interfaces[iface2_old_id].types["var2"];
let remap = resolve1.merge(resolve2)?;
let rec2_id = remap.types[rec2_old_id.index()].unwrap();
let TypeDefKind::Record(record) = &resolve1.types[rec2_id].kind else {
panic!("expected record");
};
assert!(
record.fields[0].span.is_known(),
"field should have span after merge"
);
let var2_id = remap.types[var2_old_id.index()].unwrap();
let TypeDefKind::Variant(variant) = &resolve1.types[var2_id].kind else {
panic!("expected variant");
};
assert!(
variant.cases[0].span.is_known(),
"case should have span after merge"
);
Ok(())
}
#[test]
fn param_spans_point_to_names() -> Result<()> {
let source = "\
package foo:bar;
interface iface {
my-func: func(a: u32, b: string);
}
";
let mut resolve = Resolve::default();
let pkg = resolve.push_str("test.wit", source)?;
let iface_id = resolve.packages[pkg].interfaces["iface"];
let func = &resolve.interfaces[iface_id].functions["my-func"];
assert_eq!(func.params.len(), 2);
for param in &func.params {
let start = param.span.start() as usize;
let end = param.span.end() as usize;
let snippet = &source[start..end];
assert_eq!(
snippet, param.name,
"param `{}` span points to {:?}",
param.name, snippet
);
}
Ok(())
}
#[test]
fn param_spans_preserved_through_merge() -> Result<()> {
let mut resolve1 = Resolve::default();
resolve1.push_str(
"test1.wit",
r#"
package foo:bar;
interface iface1 {
f1: func(x: u32);
}
"#,
)?;
let mut resolve2 = Resolve::default();
let pkg2 = resolve2.push_str(
"test2.wit",
r#"
package foo:baz;
interface iface2 {
f2: func(y: string, z: bool);
}
"#,
)?;
let iface2_old_id = resolve2.packages[pkg2].interfaces["iface2"];
let remap = resolve1.merge(resolve2)?;
let iface2_id = remap.interfaces[iface2_old_id.index()].unwrap();
let func = &resolve1.interfaces[iface2_id].functions["f2"];
for param in &func.params {
assert!(
param.span.is_known(),
"param `{}` should have span after merge",
param.name
);
}
Ok(())
}
/// Demonstrates the round-trip property: starting from a world with only
/// exports, `importize` turns them into imports, then `exportize` turns
/// them back. The resulting world has the same set of exports (by key)
/// as the original.
#[test]
fn exportize_importize_roundtrip() -> Result<()> {
let mut resolve = Resolve::default();
let pkg = resolve.push_str(
"test.wit",
r#"
package foo:bar;
interface types {
type my-type = u32;
}
interface api {
use types.{my-type};
do-something: func(a: my-type) -> my-type;
}
world w {
export api;
}
"#,
)?;
let world_id = resolve.packages[pkg].worlds["w"];
// Snapshot original export keys.
let original_export_keys: Vec<String> = resolve.worlds[world_id]
.exports
.keys()
.map(|k| resolve.name_world_key(k))
.collect();
assert!(!original_export_keys.is_empty());
assert!(resolve.worlds[world_id].imports.iter().all(|(_, item)| {
// Before importize the only imports should be elaborated
// interface deps (all interface items).
matches!(item, WorldItem::Interface { .. })
}));
// importize: exports -> imports, no exports remain.
resolve.importize(world_id, Some("w-temp".to_string()))?;
assert!(
resolve.worlds[world_id].exports.is_empty(),
"importize should leave no exports"
);
// The original exports should now appear as imports.
for key in &original_export_keys {
assert!(
resolve.worlds[world_id]
.imports
.keys()
.any(|k| resolve.name_world_key(k) == *key),
"expected `{key}` to be an import after importize"
);
}
// exportize: imports -> exports, round-tripping back.
resolve.exportize(world_id, Some("w-final".to_string()), None)?;
assert!(
!resolve.worlds[world_id].exports.is_empty(),
"exportize should produce exports"
);
// The original export keys should be present as exports again.
let final_export_keys: Vec<String> = resolve.worlds[world_id]
.exports
.keys()
.map(|k| resolve.name_world_key(k))
.collect();
for key in &original_export_keys {
assert!(
final_export_keys.contains(key),
"expected `{key}` to be an export after round-trip, got exports: {final_export_keys:?}"
);
}
Ok(())
}
}