diff options
198 files changed, 6054 insertions, 4469 deletions
diff --git a/Cargo.lock b/Cargo.lock index 39f27098a..b1fef2e80 100644 --- a/Cargo.lock +++ b/Cargo.lock | |||
@@ -107,9 +107,9 @@ checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693" | |||
107 | 107 | ||
108 | [[package]] | 108 | [[package]] |
109 | name = "byteorder" | 109 | name = "byteorder" |
110 | version = "1.4.2" | 110 | version = "1.4.3" |
111 | source = "registry+https://github.com/rust-lang/crates.io-index" | 111 | source = "registry+https://github.com/rust-lang/crates.io-index" |
112 | checksum = "ae44d1a3d5a19df61dd0c8beb138458ac2a53a7ac09eba97d55592540004306b" | 112 | checksum = "14c189c53d098945499cdfa7ecc63567cf3886b3332b312a5b4585d8d3a6a610" |
113 | 113 | ||
114 | [[package]] | 114 | [[package]] |
115 | name = "camino" | 115 | name = "camino" |
@@ -168,9 +168,9 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" | |||
168 | 168 | ||
169 | [[package]] | 169 | [[package]] |
170 | name = "chalk-derive" | 170 | name = "chalk-derive" |
171 | version = "0.59.0" | 171 | version = "0.60.0" |
172 | source = "registry+https://github.com/rust-lang/crates.io-index" | 172 | source = "registry+https://github.com/rust-lang/crates.io-index" |
173 | checksum = "4b9000fbcb67353dc8973ab9fd136277d321d85b79bd36b8756bb3ae0979a94a" | 173 | checksum = "ab0f74445d4fbeaf0217bc1d23978cc73b95b28e8a738b81894580dd646822d2" |
174 | dependencies = [ | 174 | dependencies = [ |
175 | "proc-macro2", | 175 | "proc-macro2", |
176 | "quote", | 176 | "quote", |
@@ -180,9 +180,9 @@ dependencies = [ | |||
180 | 180 | ||
181 | [[package]] | 181 | [[package]] |
182 | name = "chalk-ir" | 182 | name = "chalk-ir" |
183 | version = "0.59.0" | 183 | version = "0.60.0" |
184 | source = "registry+https://github.com/rust-lang/crates.io-index" | 184 | source = "registry+https://github.com/rust-lang/crates.io-index" |
185 | checksum = "b23528d61b3557c676eccf508fa0771a38453b379f0b780154eaa7f70afe8dfc" | 185 | checksum = "294b1fc6210a5b3bd06c1d01dda48a581e2cafec80b8d659139ce45456644be2" |
186 | dependencies = [ | 186 | dependencies = [ |
187 | "bitflags", | 187 | "bitflags", |
188 | "chalk-derive", | 188 | "chalk-derive", |
@@ -191,9 +191,9 @@ dependencies = [ | |||
191 | 191 | ||
192 | [[package]] | 192 | [[package]] |
193 | name = "chalk-recursive" | 193 | name = "chalk-recursive" |
194 | version = "0.59.0" | 194 | version = "0.60.0" |
195 | source = "registry+https://github.com/rust-lang/crates.io-index" | 195 | source = "registry+https://github.com/rust-lang/crates.io-index" |
196 | checksum = "a8bdd37afc666b771de8b4429fe014363d0e74aae5cc26f320f60a3eab34d744" | 196 | checksum = "1b9386936070be4545bfa22b094b7065af79aa2aeaccc945438f1c5ffe74c30a" |
197 | dependencies = [ | 197 | dependencies = [ |
198 | "chalk-derive", | 198 | "chalk-derive", |
199 | "chalk-ir", | 199 | "chalk-ir", |
@@ -204,9 +204,9 @@ dependencies = [ | |||
204 | 204 | ||
205 | [[package]] | 205 | [[package]] |
206 | name = "chalk-solve" | 206 | name = "chalk-solve" |
207 | version = "0.59.0" | 207 | version = "0.60.0" |
208 | source = "registry+https://github.com/rust-lang/crates.io-index" | 208 | source = "registry+https://github.com/rust-lang/crates.io-index" |
209 | checksum = "4182c42ca319cb71c89898ebc3d2671d1fa7d928123b171b66f1797a2000b9c8" | 209 | checksum = "7c12a1ec7e850b50a049f27ef9cf5df3056bbd1acbb3eeb44d024e501a641f3a" |
210 | dependencies = [ | 210 | dependencies = [ |
211 | "chalk-derive", | 211 | "chalk-derive", |
212 | "chalk-ir", | 212 | "chalk-ir", |
@@ -232,15 +232,6 @@ dependencies = [ | |||
232 | ] | 232 | ] |
233 | 233 | ||
234 | [[package]] | 234 | [[package]] |
235 | name = "cmake" | ||
236 | version = "0.1.45" | ||
237 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
238 | checksum = "eb6210b637171dfba4cda12e579ac6dc73f5165ad56133e5d72ef3131f320855" | ||
239 | dependencies = [ | ||
240 | "cc", | ||
241 | ] | ||
242 | |||
243 | [[package]] | ||
244 | name = "countme" | 235 | name = "countme" |
245 | version = "2.0.4" | 236 | version = "2.0.4" |
246 | source = "registry+https://github.com/rust-lang/crates.io-index" | 237 | source = "registry+https://github.com/rust-lang/crates.io-index" |
@@ -252,6 +243,12 @@ dependencies = [ | |||
252 | ] | 243 | ] |
253 | 244 | ||
254 | [[package]] | 245 | [[package]] |
246 | name = "cov-mark" | ||
247 | version = "1.1.0" | ||
248 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
249 | checksum = "9ffa3d3e0138386cd4361f63537765cac7ee40698028844635a54495a92f67f3" | ||
250 | |||
251 | [[package]] | ||
255 | name = "crc32fast" | 252 | name = "crc32fast" |
256 | version = "1.2.1" | 253 | version = "1.2.1" |
257 | source = "registry+https://github.com/rust-lang/crates.io-index" | 254 | source = "registry+https://github.com/rust-lang/crates.io-index" |
@@ -501,6 +498,7 @@ dependencies = [ | |||
501 | "anymap", | 498 | "anymap", |
502 | "base_db", | 499 | "base_db", |
503 | "cfg", | 500 | "cfg", |
501 | "cov-mark", | ||
504 | "drop_bomb", | 502 | "drop_bomb", |
505 | "either", | 503 | "either", |
506 | "expect-test", | 504 | "expect-test", |
@@ -526,7 +524,9 @@ name = "hir_expand" | |||
526 | version = "0.0.0" | 524 | version = "0.0.0" |
527 | dependencies = [ | 525 | dependencies = [ |
528 | "base_db", | 526 | "base_db", |
527 | "cfg", | ||
529 | "either", | 528 | "either", |
529 | "expect-test", | ||
530 | "la-arena", | 530 | "la-arena", |
531 | "log", | 531 | "log", |
532 | "mbe", | 532 | "mbe", |
@@ -547,6 +547,7 @@ dependencies = [ | |||
547 | "chalk-ir", | 547 | "chalk-ir", |
548 | "chalk-recursive", | 548 | "chalk-recursive", |
549 | "chalk-solve", | 549 | "chalk-solve", |
550 | "cov-mark", | ||
550 | "ena", | 551 | "ena", |
551 | "expect-test", | 552 | "expect-test", |
552 | "hir_def", | 553 | "hir_def", |
@@ -581,6 +582,7 @@ name = "ide" | |||
581 | version = "0.0.0" | 582 | version = "0.0.0" |
582 | dependencies = [ | 583 | dependencies = [ |
583 | "cfg", | 584 | "cfg", |
585 | "cov-mark", | ||
584 | "either", | 586 | "either", |
585 | "expect-test", | 587 | "expect-test", |
586 | "hir", | 588 | "hir", |
@@ -607,6 +609,7 @@ dependencies = [ | |||
607 | name = "ide_assists" | 609 | name = "ide_assists" |
608 | version = "0.0.0" | 610 | version = "0.0.0" |
609 | dependencies = [ | 611 | dependencies = [ |
612 | "cov-mark", | ||
610 | "either", | 613 | "either", |
611 | "expect-test", | 614 | "expect-test", |
612 | "hir", | 615 | "hir", |
@@ -625,6 +628,7 @@ name = "ide_completion" | |||
625 | version = "0.0.0" | 628 | version = "0.0.0" |
626 | dependencies = [ | 629 | dependencies = [ |
627 | "base_db", | 630 | "base_db", |
631 | "cov-mark", | ||
628 | "either", | 632 | "either", |
629 | "expect-test", | 633 | "expect-test", |
630 | "hir", | 634 | "hir", |
@@ -644,6 +648,7 @@ name = "ide_db" | |||
644 | version = "0.0.0" | 648 | version = "0.0.0" |
645 | dependencies = [ | 649 | dependencies = [ |
646 | "base_db", | 650 | "base_db", |
651 | "cov-mark", | ||
647 | "either", | 652 | "either", |
648 | "expect-test", | 653 | "expect-test", |
649 | "fst", | 654 | "fst", |
@@ -664,6 +669,7 @@ dependencies = [ | |||
664 | name = "ide_ssr" | 669 | name = "ide_ssr" |
665 | version = "0.0.0" | 670 | version = "0.0.0" |
666 | dependencies = [ | 671 | dependencies = [ |
672 | "cov-mark", | ||
667 | "expect-test", | 673 | "expect-test", |
668 | "hir", | 674 | "hir", |
669 | "ide_db", | 675 | "ide_db", |
@@ -687,9 +693,9 @@ dependencies = [ | |||
687 | 693 | ||
688 | [[package]] | 694 | [[package]] |
689 | name = "indexmap" | 695 | name = "indexmap" |
690 | version = "1.6.1" | 696 | version = "1.6.2" |
691 | source = "registry+https://github.com/rust-lang/crates.io-index" | 697 | source = "registry+https://github.com/rust-lang/crates.io-index" |
692 | checksum = "4fb1fa934250de4de8aef298d81c729a7d33d8c239daa3a7575e6b92bfc7313b" | 698 | checksum = "824845a0bf897a9042383849b02c1bc219c2383772efcd5c6f9766fa4b81aef3" |
693 | dependencies = [ | 699 | dependencies = [ |
694 | "autocfg", | 700 | "autocfg", |
695 | "hashbrown", | 701 | "hashbrown", |
@@ -805,11 +811,11 @@ dependencies = [ | |||
805 | 811 | ||
806 | [[package]] | 812 | [[package]] |
807 | name = "libmimalloc-sys" | 813 | name = "libmimalloc-sys" |
808 | version = "0.1.20" | 814 | version = "0.1.21" |
809 | source = "registry+https://github.com/rust-lang/crates.io-index" | 815 | source = "registry+https://github.com/rust-lang/crates.io-index" |
810 | checksum = "e58f42b6424a0ed536678c65fd97cd64b4344bcf86251e284f7c0ce9eee40e64" | 816 | checksum = "2396cf99d2f58611cd69f0efeee4af3d2e2c7b61bed433515029163aa567e65c" |
811 | dependencies = [ | 817 | dependencies = [ |
812 | "cmake", | 818 | "cc", |
813 | ] | 819 | ] |
814 | 820 | ||
815 | [[package]] | 821 | [[package]] |
@@ -874,6 +880,7 @@ checksum = "7ffc5c5338469d4d3ea17d269fa8ea3512ad247247c30bd2df69e68309ed0a08" | |||
874 | name = "mbe" | 880 | name = "mbe" |
875 | version = "0.0.0" | 881 | version = "0.0.0" |
876 | dependencies = [ | 882 | dependencies = [ |
883 | "cov-mark", | ||
877 | "log", | 884 | "log", |
878 | "parser", | 885 | "parser", |
879 | "profile", | 886 | "profile", |
@@ -892,6 +899,16 @@ source = "registry+https://github.com/rust-lang/crates.io-index" | |||
892 | checksum = "0ee1c47aaa256ecabcaea351eae4a9b01ef39ed810004e298d2511ed284b1525" | 899 | checksum = "0ee1c47aaa256ecabcaea351eae4a9b01ef39ed810004e298d2511ed284b1525" |
893 | 900 | ||
894 | [[package]] | 901 | [[package]] |
902 | name = "memmap" | ||
903 | version = "0.7.0" | ||
904 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
905 | checksum = "6585fd95e7bb50d6cc31e20d4cf9afb4e2ba16c5846fc76793f11218da9c475b" | ||
906 | dependencies = [ | ||
907 | "libc", | ||
908 | "winapi", | ||
909 | ] | ||
910 | |||
911 | [[package]] | ||
895 | name = "memmap2" | 912 | name = "memmap2" |
896 | version = "0.2.1" | 913 | version = "0.2.1" |
897 | source = "registry+https://github.com/rust-lang/crates.io-index" | 914 | source = "registry+https://github.com/rust-lang/crates.io-index" |
@@ -911,9 +928,9 @@ dependencies = [ | |||
911 | 928 | ||
912 | [[package]] | 929 | [[package]] |
913 | name = "mimalloc" | 930 | name = "mimalloc" |
914 | version = "0.1.24" | 931 | version = "0.1.25" |
915 | source = "registry+https://github.com/rust-lang/crates.io-index" | 932 | source = "registry+https://github.com/rust-lang/crates.io-index" |
916 | checksum = "757efec188b3d2088949d912e01ea2fe87164ed6376b6c5d7dd4f3ce1668a93d" | 933 | checksum = "1e7c6b11afd1e5e689ac96b6d18b1fc763398fe3d7eed99e8773426bc2033dfb" |
917 | dependencies = [ | 934 | dependencies = [ |
918 | "libmimalloc-sys", | 935 | "libmimalloc-sys", |
919 | ] | 936 | ] |
@@ -1154,8 +1171,11 @@ dependencies = [ | |||
1154 | "crossbeam-channel", | 1171 | "crossbeam-channel", |
1155 | "jod-thread", | 1172 | "jod-thread", |
1156 | "log", | 1173 | "log", |
1174 | "memmap", | ||
1175 | "object", | ||
1157 | "serde", | 1176 | "serde", |
1158 | "serde_json", | 1177 | "serde_json", |
1178 | "snap", | ||
1159 | "stdx", | 1179 | "stdx", |
1160 | "tt", | 1180 | "tt", |
1161 | ] | 1181 | ] |
@@ -1370,9 +1390,9 @@ dependencies = [ | |||
1370 | 1390 | ||
1371 | [[package]] | 1391 | [[package]] |
1372 | name = "rustc-ap-rustc_lexer" | 1392 | name = "rustc-ap-rustc_lexer" |
1373 | version = "709.0.0" | 1393 | version = "710.0.0" |
1374 | source = "registry+https://github.com/rust-lang/crates.io-index" | 1394 | source = "registry+https://github.com/rust-lang/crates.io-index" |
1375 | checksum = "f69f83314702aaccf29c7401cc63bb0d9fa7869a185a23b8379f08c91514b3f3" | 1395 | checksum = "b0bba1ca6787b6d4af505b7a940eae9ecb084dd03e07f03bf3ddbf78e738b617" |
1376 | dependencies = [ | 1396 | dependencies = [ |
1377 | "unicode-xid", | 1397 | "unicode-xid", |
1378 | ] | 1398 | ] |
@@ -1466,18 +1486,18 @@ dependencies = [ | |||
1466 | 1486 | ||
1467 | [[package]] | 1487 | [[package]] |
1468 | name = "serde" | 1488 | name = "serde" |
1469 | version = "1.0.123" | 1489 | version = "1.0.124" |
1470 | source = "registry+https://github.com/rust-lang/crates.io-index" | 1490 | source = "registry+https://github.com/rust-lang/crates.io-index" |
1471 | checksum = "92d5161132722baa40d802cc70b15262b98258453e85e5d1d365c757c73869ae" | 1491 | checksum = "bd761ff957cb2a45fbb9ab3da6512de9de55872866160b23c25f1a841e99d29f" |
1472 | dependencies = [ | 1492 | dependencies = [ |
1473 | "serde_derive", | 1493 | "serde_derive", |
1474 | ] | 1494 | ] |
1475 | 1495 | ||
1476 | [[package]] | 1496 | [[package]] |
1477 | name = "serde_derive" | 1497 | name = "serde_derive" |
1478 | version = "1.0.123" | 1498 | version = "1.0.124" |
1479 | source = "registry+https://github.com/rust-lang/crates.io-index" | 1499 | source = "registry+https://github.com/rust-lang/crates.io-index" |
1480 | checksum = "9391c295d64fc0abb2c556bad848f33cb8296276b1ad2677d1ae1ace4f258f31" | 1500 | checksum = "1800f7693e94e186f5e25a28291ae1570da908aff7d97a095dec1e56ff99069b" |
1481 | dependencies = [ | 1501 | dependencies = [ |
1482 | "proc-macro2", | 1502 | "proc-macro2", |
1483 | "quote", | 1503 | "quote", |
@@ -1541,6 +1561,12 @@ dependencies = [ | |||
1541 | ] | 1561 | ] |
1542 | 1562 | ||
1543 | [[package]] | 1563 | [[package]] |
1564 | name = "snap" | ||
1565 | version = "1.0.4" | ||
1566 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
1567 | checksum = "dc725476a1398f0480d56cd0ad381f6f32acf2642704456f8f59a35df464b59a" | ||
1568 | |||
1569 | [[package]] | ||
1544 | name = "socket2" | 1570 | name = "socket2" |
1545 | version = "0.3.19" | 1571 | version = "0.3.19" |
1546 | source = "registry+https://github.com/rust-lang/crates.io-index" | 1572 | source = "registry+https://github.com/rust-lang/crates.io-index" |
@@ -1561,9 +1587,9 @@ dependencies = [ | |||
1561 | 1587 | ||
1562 | [[package]] | 1588 | [[package]] |
1563 | name = "syn" | 1589 | name = "syn" |
1564 | version = "1.0.61" | 1590 | version = "1.0.63" |
1565 | source = "registry+https://github.com/rust-lang/crates.io-index" | 1591 | source = "registry+https://github.com/rust-lang/crates.io-index" |
1566 | checksum = "ed22b90a0e734a23a7610f4283ac9e5acfb96cbb30dfefa540d66f866f1c09c5" | 1592 | checksum = "8fd9bc7ccc2688b3344c2f48b9b546648b25ce0b20fc717ee7fa7981a8ca9717" |
1567 | dependencies = [ | 1593 | dependencies = [ |
1568 | "proc-macro2", | 1594 | "proc-macro2", |
1569 | "quote", | 1595 | "quote", |
@@ -1587,6 +1613,7 @@ name = "syntax" | |||
1587 | version = "0.0.0" | 1613 | version = "0.0.0" |
1588 | dependencies = [ | 1614 | dependencies = [ |
1589 | "arrayvec", | 1615 | "arrayvec", |
1616 | "cov-mark", | ||
1590 | "expect-test", | 1617 | "expect-test", |
1591 | "indexmap", | 1618 | "indexmap", |
1592 | "itertools", | 1619 | "itertools", |
@@ -1621,7 +1648,6 @@ dependencies = [ | |||
1621 | "dissimilar", | 1648 | "dissimilar", |
1622 | "profile", | 1649 | "profile", |
1623 | "rustc-hash", | 1650 | "rustc-hash", |
1624 | "serde_json", | ||
1625 | "stdx", | 1651 | "stdx", |
1626 | "text-size", | 1652 | "text-size", |
1627 | ] | 1653 | ] |
@@ -1756,9 +1782,9 @@ dependencies = [ | |||
1756 | 1782 | ||
1757 | [[package]] | 1783 | [[package]] |
1758 | name = "tracing-tree" | 1784 | name = "tracing-tree" |
1759 | version = "0.1.8" | 1785 | version = "0.1.9" |
1760 | source = "registry+https://github.com/rust-lang/crates.io-index" | 1786 | source = "registry+https://github.com/rust-lang/crates.io-index" |
1761 | checksum = "1a60657cfbf397c603257a8230b3f427e6a2a4e5911a59331b9bb4dffff5b608" | 1787 | checksum = "1712b40907f8d9bc2bc66763ab61dec914b7123d7149e59feb0d4e2a95fc4967" |
1762 | dependencies = [ | 1788 | dependencies = [ |
1763 | "ansi_term", | 1789 | "ansi_term", |
1764 | "atty", | 1790 | "atty", |
@@ -1919,18 +1945,18 @@ checksum = "06069a848f95fceae3e5e03c0ddc8cb78452b56654ee0c8e68f938cf790fb9e3" | |||
1919 | 1945 | ||
1920 | [[package]] | 1946 | [[package]] |
1921 | name = "xflags" | 1947 | name = "xflags" |
1922 | version = "0.1.4" | 1948 | version = "0.2.1" |
1923 | source = "registry+https://github.com/rust-lang/crates.io-index" | 1949 | source = "registry+https://github.com/rust-lang/crates.io-index" |
1924 | checksum = "222e914b43cec5d7305ac5116d10a14b3a52c50e9062d642c92631f3beabc729" | 1950 | checksum = "59ad6ce6a0b7224130015b4ebac796478ac04e0079f5d222a690efea06a9208a" |
1925 | dependencies = [ | 1951 | dependencies = [ |
1926 | "xflags-macros", | 1952 | "xflags-macros", |
1927 | ] | 1953 | ] |
1928 | 1954 | ||
1929 | [[package]] | 1955 | [[package]] |
1930 | name = "xflags-macros" | 1956 | name = "xflags-macros" |
1931 | version = "0.1.4" | 1957 | version = "0.2.1" |
1932 | source = "registry+https://github.com/rust-lang/crates.io-index" | 1958 | source = "registry+https://github.com/rust-lang/crates.io-index" |
1933 | checksum = "52f18f5b4aa7f95e209d5b9274f6164c3938920b4d5c75f97f0dd16daee25ddd" | 1959 | checksum = "c8037d3ca14996158b03c0fa905d0834906ef0fc7044df72c1f5ff690e5e62c9" |
1934 | dependencies = [ | 1960 | dependencies = [ |
1935 | "proc-macro2", | 1961 | "proc-macro2", |
1936 | ] | 1962 | ] |
diff --git a/crates/hir/src/code_model.rs b/crates/hir/src/code_model.rs deleted file mode 100644 index 9ee4b3059..000000000 --- a/crates/hir/src/code_model.rs +++ /dev/null | |||
@@ -1,2095 +0,0 @@ | |||
1 | //! FIXME: write short doc here | ||
2 | use std::{iter, sync::Arc}; | ||
3 | |||
4 | use arrayvec::ArrayVec; | ||
5 | use base_db::{CrateDisplayName, CrateId, Edition, FileId}; | ||
6 | use either::Either; | ||
7 | use hir_def::{ | ||
8 | adt::{ReprKind, StructKind, VariantData}, | ||
9 | expr::{BindingAnnotation, LabelId, Pat, PatId}, | ||
10 | import_map, | ||
11 | item_tree::ItemTreeNode, | ||
12 | lang_item::LangItemTarget, | ||
13 | path::ModPath, | ||
14 | per_ns::PerNs, | ||
15 | resolver::{HasResolver, Resolver}, | ||
16 | src::HasSource as _, | ||
17 | type_ref::TypeRef, | ||
18 | AdtId, AssocContainerId, AssocItemId, AssocItemLoc, AttrDefId, ConstId, ConstParamId, | ||
19 | DefWithBodyId, EnumId, FunctionId, GenericDefId, HasModule, ImplId, LifetimeParamId, | ||
20 | LocalEnumVariantId, LocalFieldId, Lookup, ModuleId, StaticId, StructId, TraitId, TypeAliasId, | ||
21 | TypeParamId, UnionId, | ||
22 | }; | ||
23 | use hir_def::{find_path::PrefixKind, item_scope::ItemInNs, visibility::Visibility}; | ||
24 | use hir_expand::{ | ||
25 | diagnostics::DiagnosticSink, | ||
26 | name::{name, AsName}, | ||
27 | MacroDefId, MacroDefKind, | ||
28 | }; | ||
29 | use hir_ty::{ | ||
30 | autoderef, | ||
31 | display::{write_bounds_like_dyn_trait_with_prefix, HirDisplayError, HirFormatter}, | ||
32 | method_resolution, | ||
33 | traits::{FnTrait, Solution, SolutionVariables}, | ||
34 | AliasTy, BoundVar, CallableDefId, CallableSig, Canonical, DebruijnIndex, GenericPredicate, | ||
35 | InEnvironment, Mutability, Obligation, ProjectionPredicate, ProjectionTy, Scalar, Substs, | ||
36 | TraitEnvironment, Ty, TyDefId, TyVariableKind, | ||
37 | }; | ||
38 | use rustc_hash::FxHashSet; | ||
39 | use stdx::{format_to, impl_from}; | ||
40 | use syntax::{ | ||
41 | ast::{self, AttrsOwner, NameOwner}, | ||
42 | AstNode, SmolStr, | ||
43 | }; | ||
44 | use tt::{Ident, Leaf, Literal, TokenTree}; | ||
45 | |||
46 | use crate::{ | ||
47 | db::{DefDatabase, HirDatabase}, | ||
48 | has_source::HasSource, | ||
49 | HirDisplay, InFile, Name, | ||
50 | }; | ||
51 | |||
52 | /// hir::Crate describes a single crate. It's the main interface with which | ||
53 | /// a crate's dependencies interact. Mostly, it should be just a proxy for the | ||
54 | /// root module. | ||
55 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | ||
56 | pub struct Crate { | ||
57 | pub(crate) id: CrateId, | ||
58 | } | ||
59 | |||
60 | #[derive(Debug)] | ||
61 | pub struct CrateDependency { | ||
62 | pub krate: Crate, | ||
63 | pub name: Name, | ||
64 | } | ||
65 | |||
66 | impl Crate { | ||
67 | pub fn dependencies(self, db: &dyn HirDatabase) -> Vec<CrateDependency> { | ||
68 | db.crate_graph()[self.id] | ||
69 | .dependencies | ||
70 | .iter() | ||
71 | .map(|dep| { | ||
72 | let krate = Crate { id: dep.crate_id }; | ||
73 | let name = dep.as_name(); | ||
74 | CrateDependency { krate, name } | ||
75 | }) | ||
76 | .collect() | ||
77 | } | ||
78 | |||
79 | // FIXME: add `transitive_reverse_dependencies`. | ||
80 | pub fn reverse_dependencies(self, db: &dyn HirDatabase) -> Vec<Crate> { | ||
81 | let crate_graph = db.crate_graph(); | ||
82 | crate_graph | ||
83 | .iter() | ||
84 | .filter(|&krate| { | ||
85 | crate_graph[krate].dependencies.iter().any(|it| it.crate_id == self.id) | ||
86 | }) | ||
87 | .map(|id| Crate { id }) | ||
88 | .collect() | ||
89 | } | ||
90 | |||
91 | pub fn root_module(self, db: &dyn HirDatabase) -> Module { | ||
92 | let def_map = db.crate_def_map(self.id); | ||
93 | Module { id: def_map.module_id(def_map.root()) } | ||
94 | } | ||
95 | |||
96 | pub fn root_file(self, db: &dyn HirDatabase) -> FileId { | ||
97 | db.crate_graph()[self.id].root_file_id | ||
98 | } | ||
99 | |||
100 | pub fn edition(self, db: &dyn HirDatabase) -> Edition { | ||
101 | db.crate_graph()[self.id].edition | ||
102 | } | ||
103 | |||
104 | pub fn display_name(self, db: &dyn HirDatabase) -> Option<CrateDisplayName> { | ||
105 | db.crate_graph()[self.id].display_name.clone() | ||
106 | } | ||
107 | |||
108 | pub fn query_external_importables( | ||
109 | self, | ||
110 | db: &dyn DefDatabase, | ||
111 | query: import_map::Query, | ||
112 | ) -> impl Iterator<Item = Either<ModuleDef, MacroDef>> { | ||
113 | import_map::search_dependencies(db, self.into(), query).into_iter().map(|item| match item { | ||
114 | ItemInNs::Types(mod_id) | ItemInNs::Values(mod_id) => Either::Left(mod_id.into()), | ||
115 | ItemInNs::Macros(mac_id) => Either::Right(mac_id.into()), | ||
116 | }) | ||
117 | } | ||
118 | |||
119 | pub fn all(db: &dyn HirDatabase) -> Vec<Crate> { | ||
120 | db.crate_graph().iter().map(|id| Crate { id }).collect() | ||
121 | } | ||
122 | |||
123 | /// Try to get the root URL of the documentation of a crate. | ||
124 | pub fn get_html_root_url(self: &Crate, db: &dyn HirDatabase) -> Option<String> { | ||
125 | // Look for #![doc(html_root_url = "...")] | ||
126 | let attrs = db.attrs(AttrDefId::ModuleId(self.root_module(db).into())); | ||
127 | let doc_attr_q = attrs.by_key("doc"); | ||
128 | |||
129 | if !doc_attr_q.exists() { | ||
130 | return None; | ||
131 | } | ||
132 | |||
133 | let doc_url = doc_attr_q.tt_values().map(|tt| { | ||
134 | let name = tt.token_trees.iter() | ||
135 | .skip_while(|tt| !matches!(tt, TokenTree::Leaf(Leaf::Ident(Ident{text: ref ident, ..})) if ident == "html_root_url")) | ||
136 | .skip(2) | ||
137 | .next(); | ||
138 | |||
139 | match name { | ||
140 | Some(TokenTree::Leaf(Leaf::Literal(Literal{ref text, ..}))) => Some(text), | ||
141 | _ => None | ||
142 | } | ||
143 | }).flat_map(|t| t).next(); | ||
144 | |||
145 | doc_url.map(|s| s.trim_matches('"').trim_end_matches('/').to_owned() + "/") | ||
146 | } | ||
147 | } | ||
148 | |||
149 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | ||
150 | pub struct Module { | ||
151 | pub(crate) id: ModuleId, | ||
152 | } | ||
153 | |||
154 | /// The defs which can be visible in the module. | ||
155 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | ||
156 | pub enum ModuleDef { | ||
157 | Module(Module), | ||
158 | Function(Function), | ||
159 | Adt(Adt), | ||
160 | // Can't be directly declared, but can be imported. | ||
161 | Variant(Variant), | ||
162 | Const(Const), | ||
163 | Static(Static), | ||
164 | Trait(Trait), | ||
165 | TypeAlias(TypeAlias), | ||
166 | BuiltinType(BuiltinType), | ||
167 | } | ||
168 | impl_from!( | ||
169 | Module, | ||
170 | Function, | ||
171 | Adt(Struct, Enum, Union), | ||
172 | Variant, | ||
173 | Const, | ||
174 | Static, | ||
175 | Trait, | ||
176 | TypeAlias, | ||
177 | BuiltinType | ||
178 | for ModuleDef | ||
179 | ); | ||
180 | |||
181 | impl From<VariantDef> for ModuleDef { | ||
182 | fn from(var: VariantDef) -> Self { | ||
183 | match var { | ||
184 | VariantDef::Struct(t) => Adt::from(t).into(), | ||
185 | VariantDef::Union(t) => Adt::from(t).into(), | ||
186 | VariantDef::Variant(t) => t.into(), | ||
187 | } | ||
188 | } | ||
189 | } | ||
190 | |||
191 | impl ModuleDef { | ||
192 | pub fn module(self, db: &dyn HirDatabase) -> Option<Module> { | ||
193 | match self { | ||
194 | ModuleDef::Module(it) => it.parent(db), | ||
195 | ModuleDef::Function(it) => Some(it.module(db)), | ||
196 | ModuleDef::Adt(it) => Some(it.module(db)), | ||
197 | ModuleDef::Variant(it) => Some(it.module(db)), | ||
198 | ModuleDef::Const(it) => Some(it.module(db)), | ||
199 | ModuleDef::Static(it) => Some(it.module(db)), | ||
200 | ModuleDef::Trait(it) => Some(it.module(db)), | ||
201 | ModuleDef::TypeAlias(it) => Some(it.module(db)), | ||
202 | ModuleDef::BuiltinType(_) => None, | ||
203 | } | ||
204 | } | ||
205 | |||
206 | pub fn canonical_path(&self, db: &dyn HirDatabase) -> Option<String> { | ||
207 | let mut segments = Vec::new(); | ||
208 | segments.push(self.name(db)?.to_string()); | ||
209 | for m in self.module(db)?.path_to_root(db) { | ||
210 | segments.extend(m.name(db).map(|it| it.to_string())) | ||
211 | } | ||
212 | segments.reverse(); | ||
213 | Some(segments.join("::")) | ||
214 | } | ||
215 | |||
216 | pub fn definition_visibility(&self, db: &dyn HirDatabase) -> Option<Visibility> { | ||
217 | let module = match self { | ||
218 | ModuleDef::Module(it) => it.parent(db)?, | ||
219 | ModuleDef::Function(it) => return Some(it.visibility(db)), | ||
220 | ModuleDef::Adt(it) => it.module(db), | ||
221 | ModuleDef::Variant(it) => { | ||
222 | let parent = it.parent_enum(db); | ||
223 | let module = it.module(db); | ||
224 | return module.visibility_of(db, &ModuleDef::Adt(Adt::Enum(parent))); | ||
225 | } | ||
226 | ModuleDef::Const(it) => return Some(it.visibility(db)), | ||
227 | ModuleDef::Static(it) => it.module(db), | ||
228 | ModuleDef::Trait(it) => it.module(db), | ||
229 | ModuleDef::TypeAlias(it) => return Some(it.visibility(db)), | ||
230 | ModuleDef::BuiltinType(_) => return None, | ||
231 | }; | ||
232 | |||
233 | module.visibility_of(db, self) | ||
234 | } | ||
235 | |||
236 | pub fn name(self, db: &dyn HirDatabase) -> Option<Name> { | ||
237 | match self { | ||
238 | ModuleDef::Adt(it) => Some(it.name(db)), | ||
239 | ModuleDef::Trait(it) => Some(it.name(db)), | ||
240 | ModuleDef::Function(it) => Some(it.name(db)), | ||
241 | ModuleDef::Variant(it) => Some(it.name(db)), | ||
242 | ModuleDef::TypeAlias(it) => Some(it.name(db)), | ||
243 | ModuleDef::Module(it) => it.name(db), | ||
244 | ModuleDef::Const(it) => it.name(db), | ||
245 | ModuleDef::Static(it) => it.name(db), | ||
246 | |||
247 | ModuleDef::BuiltinType(it) => Some(it.name()), | ||
248 | } | ||
249 | } | ||
250 | |||
251 | pub fn diagnostics(self, db: &dyn HirDatabase, sink: &mut DiagnosticSink) { | ||
252 | let id = match self { | ||
253 | ModuleDef::Adt(it) => match it { | ||
254 | Adt::Struct(it) => it.id.into(), | ||
255 | Adt::Enum(it) => it.id.into(), | ||
256 | Adt::Union(it) => it.id.into(), | ||
257 | }, | ||
258 | ModuleDef::Trait(it) => it.id.into(), | ||
259 | ModuleDef::Function(it) => it.id.into(), | ||
260 | ModuleDef::TypeAlias(it) => it.id.into(), | ||
261 | ModuleDef::Module(it) => it.id.into(), | ||
262 | ModuleDef::Const(it) => it.id.into(), | ||
263 | ModuleDef::Static(it) => it.id.into(), | ||
264 | _ => return, | ||
265 | }; | ||
266 | |||
267 | let module = match self.module(db) { | ||
268 | Some(it) => it, | ||
269 | None => return, | ||
270 | }; | ||
271 | |||
272 | hir_ty::diagnostics::validate_module_item(db, module.id.krate(), id, sink) | ||
273 | } | ||
274 | } | ||
275 | |||
276 | impl Module { | ||
277 | /// Name of this module. | ||
278 | pub fn name(self, db: &dyn HirDatabase) -> Option<Name> { | ||
279 | let def_map = self.id.def_map(db.upcast()); | ||
280 | let parent = def_map[self.id.local_id].parent?; | ||
281 | def_map[parent].children.iter().find_map(|(name, module_id)| { | ||
282 | if *module_id == self.id.local_id { | ||
283 | Some(name.clone()) | ||
284 | } else { | ||
285 | None | ||
286 | } | ||
287 | }) | ||
288 | } | ||
289 | |||
290 | /// Returns the crate this module is part of. | ||
291 | pub fn krate(self) -> Crate { | ||
292 | Crate { id: self.id.krate() } | ||
293 | } | ||
294 | |||
295 | /// Topmost parent of this module. Every module has a `crate_root`, but some | ||
296 | /// might be missing `krate`. This can happen if a module's file is not included | ||
297 | /// in the module tree of any target in `Cargo.toml`. | ||
298 | pub fn crate_root(self, db: &dyn HirDatabase) -> Module { | ||
299 | let def_map = db.crate_def_map(self.id.krate()); | ||
300 | Module { id: def_map.module_id(def_map.root()) } | ||
301 | } | ||
302 | |||
303 | /// Iterates over all child modules. | ||
304 | pub fn children(self, db: &dyn HirDatabase) -> impl Iterator<Item = Module> { | ||
305 | let def_map = self.id.def_map(db.upcast()); | ||
306 | let children = def_map[self.id.local_id] | ||
307 | .children | ||
308 | .iter() | ||
309 | .map(|(_, module_id)| Module { id: def_map.module_id(*module_id) }) | ||
310 | .collect::<Vec<_>>(); | ||
311 | children.into_iter() | ||
312 | } | ||
313 | |||
314 | /// Finds a parent module. | ||
315 | pub fn parent(self, db: &dyn HirDatabase) -> Option<Module> { | ||
316 | // FIXME: handle block expressions as modules (their parent is in a different DefMap) | ||
317 | let def_map = self.id.def_map(db.upcast()); | ||
318 | let parent_id = def_map[self.id.local_id].parent?; | ||
319 | Some(Module { id: def_map.module_id(parent_id) }) | ||
320 | } | ||
321 | |||
322 | pub fn path_to_root(self, db: &dyn HirDatabase) -> Vec<Module> { | ||
323 | let mut res = vec![self]; | ||
324 | let mut curr = self; | ||
325 | while let Some(next) = curr.parent(db) { | ||
326 | res.push(next); | ||
327 | curr = next | ||
328 | } | ||
329 | res | ||
330 | } | ||
331 | |||
332 | /// Returns a `ModuleScope`: a set of items, visible in this module. | ||
333 | pub fn scope( | ||
334 | self, | ||
335 | db: &dyn HirDatabase, | ||
336 | visible_from: Option<Module>, | ||
337 | ) -> Vec<(Name, ScopeDef)> { | ||
338 | self.id.def_map(db.upcast())[self.id.local_id] | ||
339 | .scope | ||
340 | .entries() | ||
341 | .filter_map(|(name, def)| { | ||
342 | if let Some(m) = visible_from { | ||
343 | let filtered = | ||
344 | def.filter_visibility(|vis| vis.is_visible_from(db.upcast(), m.id)); | ||
345 | if filtered.is_none() && !def.is_none() { | ||
346 | None | ||
347 | } else { | ||
348 | Some((name, filtered)) | ||
349 | } | ||
350 | } else { | ||
351 | Some((name, def)) | ||
352 | } | ||
353 | }) | ||
354 | .flat_map(|(name, def)| { | ||
355 | ScopeDef::all_items(def).into_iter().map(move |item| (name.clone(), item)) | ||
356 | }) | ||
357 | .collect() | ||
358 | } | ||
359 | |||
360 | pub fn visibility_of(self, db: &dyn HirDatabase, def: &ModuleDef) -> Option<Visibility> { | ||
361 | self.id.def_map(db.upcast())[self.id.local_id].scope.visibility_of(def.clone().into()) | ||
362 | } | ||
363 | |||
364 | pub fn diagnostics(self, db: &dyn HirDatabase, sink: &mut DiagnosticSink) { | ||
365 | let _p = profile::span("Module::diagnostics").detail(|| { | ||
366 | format!("{:?}", self.name(db).map_or("<unknown>".into(), |name| name.to_string())) | ||
367 | }); | ||
368 | let def_map = self.id.def_map(db.upcast()); | ||
369 | def_map.add_diagnostics(db.upcast(), self.id.local_id, sink); | ||
370 | for decl in self.declarations(db) { | ||
371 | match decl { | ||
372 | crate::ModuleDef::Function(f) => f.diagnostics(db, sink), | ||
373 | crate::ModuleDef::Module(m) => { | ||
374 | // Only add diagnostics from inline modules | ||
375 | if def_map[m.id.local_id].origin.is_inline() { | ||
376 | m.diagnostics(db, sink) | ||
377 | } | ||
378 | } | ||
379 | _ => { | ||
380 | decl.diagnostics(db, sink); | ||
381 | } | ||
382 | } | ||
383 | } | ||
384 | |||
385 | for impl_def in self.impl_defs(db) { | ||
386 | for item in impl_def.items(db) { | ||
387 | if let AssocItem::Function(f) = item { | ||
388 | f.diagnostics(db, sink); | ||
389 | } | ||
390 | } | ||
391 | } | ||
392 | } | ||
393 | |||
394 | pub fn declarations(self, db: &dyn HirDatabase) -> Vec<ModuleDef> { | ||
395 | let def_map = self.id.def_map(db.upcast()); | ||
396 | def_map[self.id.local_id].scope.declarations().map(ModuleDef::from).collect() | ||
397 | } | ||
398 | |||
399 | pub fn impl_defs(self, db: &dyn HirDatabase) -> Vec<Impl> { | ||
400 | let def_map = self.id.def_map(db.upcast()); | ||
401 | def_map[self.id.local_id].scope.impls().map(Impl::from).collect() | ||
402 | } | ||
403 | |||
404 | /// Finds a path that can be used to refer to the given item from within | ||
405 | /// this module, if possible. | ||
406 | pub fn find_use_path(self, db: &dyn DefDatabase, item: impl Into<ItemInNs>) -> Option<ModPath> { | ||
407 | hir_def::find_path::find_path(db, item.into(), self.into()) | ||
408 | } | ||
409 | |||
410 | /// Finds a path that can be used to refer to the given item from within | ||
411 | /// this module, if possible. This is used for returning import paths for use-statements. | ||
412 | pub fn find_use_path_prefixed( | ||
413 | self, | ||
414 | db: &dyn DefDatabase, | ||
415 | item: impl Into<ItemInNs>, | ||
416 | prefix_kind: PrefixKind, | ||
417 | ) -> Option<ModPath> { | ||
418 | hir_def::find_path::find_path_prefixed(db, item.into(), self.into(), prefix_kind) | ||
419 | } | ||
420 | } | ||
421 | |||
422 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | ||
423 | pub struct Field { | ||
424 | pub(crate) parent: VariantDef, | ||
425 | pub(crate) id: LocalFieldId, | ||
426 | } | ||
427 | |||
428 | #[derive(Debug, PartialEq, Eq)] | ||
429 | pub enum FieldSource { | ||
430 | Named(ast::RecordField), | ||
431 | Pos(ast::TupleField), | ||
432 | } | ||
433 | |||
434 | impl Field { | ||
435 | pub fn name(&self, db: &dyn HirDatabase) -> Name { | ||
436 | self.parent.variant_data(db).fields()[self.id].name.clone() | ||
437 | } | ||
438 | |||
439 | /// Returns the type as in the signature of the struct (i.e., with | ||
440 | /// placeholder types for type parameters). This is good for showing | ||
441 | /// signature help, but not so good to actually get the type of the field | ||
442 | /// when you actually have a variable of the struct. | ||
443 | pub fn signature_ty(&self, db: &dyn HirDatabase) -> Type { | ||
444 | let var_id = self.parent.into(); | ||
445 | let generic_def_id: GenericDefId = match self.parent { | ||
446 | VariantDef::Struct(it) => it.id.into(), | ||
447 | VariantDef::Union(it) => it.id.into(), | ||
448 | VariantDef::Variant(it) => it.parent.id.into(), | ||
449 | }; | ||
450 | let substs = Substs::type_params(db, generic_def_id); | ||
451 | let ty = db.field_types(var_id)[self.id].clone().subst(&substs); | ||
452 | Type::new(db, self.parent.module(db).id.krate(), var_id, ty) | ||
453 | } | ||
454 | |||
455 | pub fn parent_def(&self, _db: &dyn HirDatabase) -> VariantDef { | ||
456 | self.parent | ||
457 | } | ||
458 | } | ||
459 | |||
460 | impl HasVisibility for Field { | ||
461 | fn visibility(&self, db: &dyn HirDatabase) -> Visibility { | ||
462 | let variant_data = self.parent.variant_data(db); | ||
463 | let visibility = &variant_data.fields()[self.id].visibility; | ||
464 | let parent_id: hir_def::VariantId = self.parent.into(); | ||
465 | visibility.resolve(db.upcast(), &parent_id.resolver(db.upcast())) | ||
466 | } | ||
467 | } | ||
468 | |||
469 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | ||
470 | pub struct Struct { | ||
471 | pub(crate) id: StructId, | ||
472 | } | ||
473 | |||
474 | impl Struct { | ||
475 | pub fn module(self, db: &dyn HirDatabase) -> Module { | ||
476 | Module { id: self.id.lookup(db.upcast()).container.module(db.upcast()) } | ||
477 | } | ||
478 | |||
479 | pub fn krate(self, db: &dyn HirDatabase) -> Option<Crate> { | ||
480 | Some(self.module(db).krate()) | ||
481 | } | ||
482 | |||
483 | pub fn name(self, db: &dyn HirDatabase) -> Name { | ||
484 | db.struct_data(self.id).name.clone() | ||
485 | } | ||
486 | |||
487 | pub fn fields(self, db: &dyn HirDatabase) -> Vec<Field> { | ||
488 | db.struct_data(self.id) | ||
489 | .variant_data | ||
490 | .fields() | ||
491 | .iter() | ||
492 | .map(|(id, _)| Field { parent: self.into(), id }) | ||
493 | .collect() | ||
494 | } | ||
495 | |||
496 | pub fn ty(self, db: &dyn HirDatabase) -> Type { | ||
497 | Type::from_def( | ||
498 | db, | ||
499 | self.id.lookup(db.upcast()).container.module(db.upcast()).krate(), | ||
500 | self.id, | ||
501 | ) | ||
502 | } | ||
503 | |||
504 | pub fn repr(self, db: &dyn HirDatabase) -> Option<ReprKind> { | ||
505 | db.struct_data(self.id).repr.clone() | ||
506 | } | ||
507 | |||
508 | pub fn kind(self, db: &dyn HirDatabase) -> StructKind { | ||
509 | self.variant_data(db).kind() | ||
510 | } | ||
511 | |||
512 | fn variant_data(self, db: &dyn HirDatabase) -> Arc<VariantData> { | ||
513 | db.struct_data(self.id).variant_data.clone() | ||
514 | } | ||
515 | } | ||
516 | |||
517 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | ||
518 | pub struct Union { | ||
519 | pub(crate) id: UnionId, | ||
520 | } | ||
521 | |||
522 | impl Union { | ||
523 | pub fn name(self, db: &dyn HirDatabase) -> Name { | ||
524 | db.union_data(self.id).name.clone() | ||
525 | } | ||
526 | |||
527 | pub fn module(self, db: &dyn HirDatabase) -> Module { | ||
528 | Module { id: self.id.lookup(db.upcast()).container.module(db.upcast()) } | ||
529 | } | ||
530 | |||
531 | pub fn ty(self, db: &dyn HirDatabase) -> Type { | ||
532 | Type::from_def( | ||
533 | db, | ||
534 | self.id.lookup(db.upcast()).container.module(db.upcast()).krate(), | ||
535 | self.id, | ||
536 | ) | ||
537 | } | ||
538 | |||
539 | pub fn fields(self, db: &dyn HirDatabase) -> Vec<Field> { | ||
540 | db.union_data(self.id) | ||
541 | .variant_data | ||
542 | .fields() | ||
543 | .iter() | ||
544 | .map(|(id, _)| Field { parent: self.into(), id }) | ||
545 | .collect() | ||
546 | } | ||
547 | |||
548 | fn variant_data(self, db: &dyn HirDatabase) -> Arc<VariantData> { | ||
549 | db.union_data(self.id).variant_data.clone() | ||
550 | } | ||
551 | } | ||
552 | |||
553 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | ||
554 | pub struct Enum { | ||
555 | pub(crate) id: EnumId, | ||
556 | } | ||
557 | |||
558 | impl Enum { | ||
559 | pub fn module(self, db: &dyn HirDatabase) -> Module { | ||
560 | Module { id: self.id.lookup(db.upcast()).container.module(db.upcast()) } | ||
561 | } | ||
562 | |||
563 | pub fn krate(self, db: &dyn HirDatabase) -> Option<Crate> { | ||
564 | Some(self.module(db).krate()) | ||
565 | } | ||
566 | |||
567 | pub fn name(self, db: &dyn HirDatabase) -> Name { | ||
568 | db.enum_data(self.id).name.clone() | ||
569 | } | ||
570 | |||
571 | pub fn variants(self, db: &dyn HirDatabase) -> Vec<Variant> { | ||
572 | db.enum_data(self.id).variants.iter().map(|(id, _)| Variant { parent: self, id }).collect() | ||
573 | } | ||
574 | |||
575 | pub fn ty(self, db: &dyn HirDatabase) -> Type { | ||
576 | Type::from_def( | ||
577 | db, | ||
578 | self.id.lookup(db.upcast()).container.module(db.upcast()).krate(), | ||
579 | self.id, | ||
580 | ) | ||
581 | } | ||
582 | } | ||
583 | |||
584 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | ||
585 | pub struct Variant { | ||
586 | pub(crate) parent: Enum, | ||
587 | pub(crate) id: LocalEnumVariantId, | ||
588 | } | ||
589 | |||
590 | impl Variant { | ||
591 | pub fn module(self, db: &dyn HirDatabase) -> Module { | ||
592 | self.parent.module(db) | ||
593 | } | ||
594 | pub fn parent_enum(self, _db: &dyn HirDatabase) -> Enum { | ||
595 | self.parent | ||
596 | } | ||
597 | |||
598 | pub fn name(self, db: &dyn HirDatabase) -> Name { | ||
599 | db.enum_data(self.parent.id).variants[self.id].name.clone() | ||
600 | } | ||
601 | |||
602 | pub fn fields(self, db: &dyn HirDatabase) -> Vec<Field> { | ||
603 | self.variant_data(db) | ||
604 | .fields() | ||
605 | .iter() | ||
606 | .map(|(id, _)| Field { parent: self.into(), id }) | ||
607 | .collect() | ||
608 | } | ||
609 | |||
610 | pub fn kind(self, db: &dyn HirDatabase) -> StructKind { | ||
611 | self.variant_data(db).kind() | ||
612 | } | ||
613 | |||
614 | pub(crate) fn variant_data(self, db: &dyn HirDatabase) -> Arc<VariantData> { | ||
615 | db.enum_data(self.parent.id).variants[self.id].variant_data.clone() | ||
616 | } | ||
617 | } | ||
618 | |||
619 | /// A Data Type | ||
620 | #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] | ||
621 | pub enum Adt { | ||
622 | Struct(Struct), | ||
623 | Union(Union), | ||
624 | Enum(Enum), | ||
625 | } | ||
626 | impl_from!(Struct, Union, Enum for Adt); | ||
627 | |||
628 | impl Adt { | ||
629 | pub fn has_non_default_type_params(self, db: &dyn HirDatabase) -> bool { | ||
630 | let subst = db.generic_defaults(self.into()); | ||
631 | subst.iter().any(|ty| &ty.value == &Ty::Unknown) | ||
632 | } | ||
633 | |||
634 | /// Turns this ADT into a type. Any type parameters of the ADT will be | ||
635 | /// turned into unknown types, which is good for e.g. finding the most | ||
636 | /// general set of completions, but will not look very nice when printed. | ||
637 | pub fn ty(self, db: &dyn HirDatabase) -> Type { | ||
638 | let id = AdtId::from(self); | ||
639 | Type::from_def(db, id.module(db.upcast()).krate(), id) | ||
640 | } | ||
641 | |||
642 | pub fn module(self, db: &dyn HirDatabase) -> Module { | ||
643 | match self { | ||
644 | Adt::Struct(s) => s.module(db), | ||
645 | Adt::Union(s) => s.module(db), | ||
646 | Adt::Enum(e) => e.module(db), | ||
647 | } | ||
648 | } | ||
649 | |||
650 | pub fn krate(self, db: &dyn HirDatabase) -> Option<Crate> { | ||
651 | Some(self.module(db).krate()) | ||
652 | } | ||
653 | |||
654 | pub fn name(self, db: &dyn HirDatabase) -> Name { | ||
655 | match self { | ||
656 | Adt::Struct(s) => s.name(db), | ||
657 | Adt::Union(u) => u.name(db), | ||
658 | Adt::Enum(e) => e.name(db), | ||
659 | } | ||
660 | } | ||
661 | } | ||
662 | |||
663 | #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] | ||
664 | pub enum VariantDef { | ||
665 | Struct(Struct), | ||
666 | Union(Union), | ||
667 | Variant(Variant), | ||
668 | } | ||
669 | impl_from!(Struct, Union, Variant for VariantDef); | ||
670 | |||
671 | impl VariantDef { | ||
672 | pub fn fields(self, db: &dyn HirDatabase) -> Vec<Field> { | ||
673 | match self { | ||
674 | VariantDef::Struct(it) => it.fields(db), | ||
675 | VariantDef::Union(it) => it.fields(db), | ||
676 | VariantDef::Variant(it) => it.fields(db), | ||
677 | } | ||
678 | } | ||
679 | |||
680 | pub fn module(self, db: &dyn HirDatabase) -> Module { | ||
681 | match self { | ||
682 | VariantDef::Struct(it) => it.module(db), | ||
683 | VariantDef::Union(it) => it.module(db), | ||
684 | VariantDef::Variant(it) => it.module(db), | ||
685 | } | ||
686 | } | ||
687 | |||
688 | pub fn name(&self, db: &dyn HirDatabase) -> Name { | ||
689 | match self { | ||
690 | VariantDef::Struct(s) => s.name(db), | ||
691 | VariantDef::Union(u) => u.name(db), | ||
692 | VariantDef::Variant(e) => e.name(db), | ||
693 | } | ||
694 | } | ||
695 | |||
696 | pub(crate) fn variant_data(self, db: &dyn HirDatabase) -> Arc<VariantData> { | ||
697 | match self { | ||
698 | VariantDef::Struct(it) => it.variant_data(db), | ||
699 | VariantDef::Union(it) => it.variant_data(db), | ||
700 | VariantDef::Variant(it) => it.variant_data(db), | ||
701 | } | ||
702 | } | ||
703 | } | ||
704 | |||
705 | /// The defs which have a body. | ||
706 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | ||
707 | pub enum DefWithBody { | ||
708 | Function(Function), | ||
709 | Static(Static), | ||
710 | Const(Const), | ||
711 | } | ||
712 | impl_from!(Function, Const, Static for DefWithBody); | ||
713 | |||
714 | impl DefWithBody { | ||
715 | pub fn module(self, db: &dyn HirDatabase) -> Module { | ||
716 | match self { | ||
717 | DefWithBody::Const(c) => c.module(db), | ||
718 | DefWithBody::Function(f) => f.module(db), | ||
719 | DefWithBody::Static(s) => s.module(db), | ||
720 | } | ||
721 | } | ||
722 | |||
723 | pub fn name(self, db: &dyn HirDatabase) -> Option<Name> { | ||
724 | match self { | ||
725 | DefWithBody::Function(f) => Some(f.name(db)), | ||
726 | DefWithBody::Static(s) => s.name(db), | ||
727 | DefWithBody::Const(c) => c.name(db), | ||
728 | } | ||
729 | } | ||
730 | } | ||
731 | |||
732 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | ||
733 | pub struct Function { | ||
734 | pub(crate) id: FunctionId, | ||
735 | } | ||
736 | |||
737 | impl Function { | ||
738 | pub fn module(self, db: &dyn HirDatabase) -> Module { | ||
739 | self.id.lookup(db.upcast()).module(db.upcast()).into() | ||
740 | } | ||
741 | |||
742 | pub fn krate(self, db: &dyn HirDatabase) -> Option<Crate> { | ||
743 | Some(self.module(db).krate()) | ||
744 | } | ||
745 | |||
746 | pub fn name(self, db: &dyn HirDatabase) -> Name { | ||
747 | db.function_data(self.id).name.clone() | ||
748 | } | ||
749 | |||
750 | /// Get this function's return type | ||
751 | pub fn ret_type(self, db: &dyn HirDatabase) -> Type { | ||
752 | let resolver = self.id.resolver(db.upcast()); | ||
753 | let ret_type = &db.function_data(self.id).ret_type; | ||
754 | let ctx = hir_ty::TyLoweringContext::new(db, &resolver); | ||
755 | let environment = TraitEnvironment::lower(db, &resolver); | ||
756 | Type { | ||
757 | krate: self.id.lookup(db.upcast()).container.module(db.upcast()).krate(), | ||
758 | ty: InEnvironment { value: Ty::from_hir_ext(&ctx, ret_type).0, environment }, | ||
759 | } | ||
760 | } | ||
761 | |||
762 | pub fn self_param(self, db: &dyn HirDatabase) -> Option<SelfParam> { | ||
763 | if !db.function_data(self.id).has_self_param { | ||
764 | return None; | ||
765 | } | ||
766 | Some(SelfParam { func: self.id }) | ||
767 | } | ||
768 | |||
769 | pub fn assoc_fn_params(self, db: &dyn HirDatabase) -> Vec<Param> { | ||
770 | let resolver = self.id.resolver(db.upcast()); | ||
771 | let ctx = hir_ty::TyLoweringContext::new(db, &resolver); | ||
772 | let environment = TraitEnvironment::lower(db, &resolver); | ||
773 | db.function_data(self.id) | ||
774 | .params | ||
775 | .iter() | ||
776 | .map(|type_ref| { | ||
777 | let ty = Type { | ||
778 | krate: self.id.lookup(db.upcast()).container.module(db.upcast()).krate(), | ||
779 | ty: InEnvironment { | ||
780 | value: Ty::from_hir_ext(&ctx, type_ref).0, | ||
781 | environment: environment.clone(), | ||
782 | }, | ||
783 | }; | ||
784 | Param { ty } | ||
785 | }) | ||
786 | .collect() | ||
787 | } | ||
788 | pub fn method_params(self, db: &dyn HirDatabase) -> Option<Vec<Param>> { | ||
789 | if self.self_param(db).is_none() { | ||
790 | return None; | ||
791 | } | ||
792 | let mut res = self.assoc_fn_params(db); | ||
793 | res.remove(0); | ||
794 | Some(res) | ||
795 | } | ||
796 | |||
797 | pub fn is_unsafe(self, db: &dyn HirDatabase) -> bool { | ||
798 | db.function_data(self.id).is_unsafe | ||
799 | } | ||
800 | |||
801 | pub fn diagnostics(self, db: &dyn HirDatabase, sink: &mut DiagnosticSink) { | ||
802 | let krate = self.module(db).id.krate(); | ||
803 | hir_def::diagnostics::validate_body(db.upcast(), self.id.into(), sink); | ||
804 | hir_ty::diagnostics::validate_module_item(db, krate, self.id.into(), sink); | ||
805 | hir_ty::diagnostics::validate_body(db, self.id.into(), sink); | ||
806 | } | ||
807 | |||
808 | /// Whether this function declaration has a definition. | ||
809 | /// | ||
810 | /// This is false in the case of required (not provided) trait methods. | ||
811 | pub fn has_body(self, db: &dyn HirDatabase) -> bool { | ||
812 | db.function_data(self.id).has_body | ||
813 | } | ||
814 | |||
815 | /// A textual representation of the HIR of this function for debugging purposes. | ||
816 | pub fn debug_hir(self, db: &dyn HirDatabase) -> String { | ||
817 | let body = db.body(self.id.into()); | ||
818 | |||
819 | let mut result = String::new(); | ||
820 | format_to!(result, "HIR expressions in the body of `{}`:\n", self.name(db)); | ||
821 | for (id, expr) in body.exprs.iter() { | ||
822 | format_to!(result, "{:?}: {:?}\n", id, expr); | ||
823 | } | ||
824 | |||
825 | result | ||
826 | } | ||
827 | } | ||
828 | |||
829 | // Note: logically, this belongs to `hir_ty`, but we are not using it there yet. | ||
830 | pub enum Access { | ||
831 | Shared, | ||
832 | Exclusive, | ||
833 | Owned, | ||
834 | } | ||
835 | |||
836 | impl From<Mutability> for Access { | ||
837 | fn from(mutability: Mutability) -> Access { | ||
838 | match mutability { | ||
839 | Mutability::Not => Access::Shared, | ||
840 | Mutability::Mut => Access::Exclusive, | ||
841 | } | ||
842 | } | ||
843 | } | ||
844 | |||
845 | #[derive(Debug)] | ||
846 | pub struct Param { | ||
847 | ty: Type, | ||
848 | } | ||
849 | |||
850 | impl Param { | ||
851 | pub fn ty(&self) -> &Type { | ||
852 | &self.ty | ||
853 | } | ||
854 | } | ||
855 | |||
856 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | ||
857 | pub struct SelfParam { | ||
858 | func: FunctionId, | ||
859 | } | ||
860 | |||
861 | impl SelfParam { | ||
862 | pub fn access(self, db: &dyn HirDatabase) -> Access { | ||
863 | let func_data = db.function_data(self.func); | ||
864 | func_data | ||
865 | .params | ||
866 | .first() | ||
867 | .map(|param| match *param { | ||
868 | TypeRef::Reference(.., mutability) => match mutability { | ||
869 | hir_def::type_ref::Mutability::Shared => Access::Shared, | ||
870 | hir_def::type_ref::Mutability::Mut => Access::Exclusive, | ||
871 | }, | ||
872 | _ => Access::Owned, | ||
873 | }) | ||
874 | .unwrap_or(Access::Owned) | ||
875 | } | ||
876 | } | ||
877 | |||
878 | impl HasVisibility for Function { | ||
879 | fn visibility(&self, db: &dyn HirDatabase) -> Visibility { | ||
880 | let function_data = db.function_data(self.id); | ||
881 | let visibility = &function_data.visibility; | ||
882 | visibility.resolve(db.upcast(), &self.id.resolver(db.upcast())) | ||
883 | } | ||
884 | } | ||
885 | |||
886 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | ||
887 | pub struct Const { | ||
888 | pub(crate) id: ConstId, | ||
889 | } | ||
890 | |||
891 | impl Const { | ||
892 | pub fn module(self, db: &dyn HirDatabase) -> Module { | ||
893 | Module { id: self.id.lookup(db.upcast()).module(db.upcast()) } | ||
894 | } | ||
895 | |||
896 | pub fn krate(self, db: &dyn HirDatabase) -> Option<Crate> { | ||
897 | Some(self.module(db).krate()) | ||
898 | } | ||
899 | |||
900 | pub fn name(self, db: &dyn HirDatabase) -> Option<Name> { | ||
901 | db.const_data(self.id).name.clone() | ||
902 | } | ||
903 | } | ||
904 | |||
905 | impl HasVisibility for Const { | ||
906 | fn visibility(&self, db: &dyn HirDatabase) -> Visibility { | ||
907 | let function_data = db.const_data(self.id); | ||
908 | let visibility = &function_data.visibility; | ||
909 | visibility.resolve(db.upcast(), &self.id.resolver(db.upcast())) | ||
910 | } | ||
911 | } | ||
912 | |||
913 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | ||
914 | pub struct Static { | ||
915 | pub(crate) id: StaticId, | ||
916 | } | ||
917 | |||
918 | impl Static { | ||
919 | pub fn module(self, db: &dyn HirDatabase) -> Module { | ||
920 | Module { id: self.id.lookup(db.upcast()).module(db.upcast()) } | ||
921 | } | ||
922 | |||
923 | pub fn krate(self, db: &dyn HirDatabase) -> Option<Crate> { | ||
924 | Some(self.module(db).krate()) | ||
925 | } | ||
926 | |||
927 | pub fn name(self, db: &dyn HirDatabase) -> Option<Name> { | ||
928 | db.static_data(self.id).name.clone() | ||
929 | } | ||
930 | |||
931 | pub fn is_mut(self, db: &dyn HirDatabase) -> bool { | ||
932 | db.static_data(self.id).mutable | ||
933 | } | ||
934 | } | ||
935 | |||
936 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | ||
937 | pub struct Trait { | ||
938 | pub(crate) id: TraitId, | ||
939 | } | ||
940 | |||
941 | impl Trait { | ||
942 | pub fn module(self, db: &dyn HirDatabase) -> Module { | ||
943 | Module { id: self.id.lookup(db.upcast()).container.module(db.upcast()) } | ||
944 | } | ||
945 | |||
946 | pub fn name(self, db: &dyn HirDatabase) -> Name { | ||
947 | db.trait_data(self.id).name.clone() | ||
948 | } | ||
949 | |||
950 | pub fn items(self, db: &dyn HirDatabase) -> Vec<AssocItem> { | ||
951 | db.trait_data(self.id).items.iter().map(|(_name, it)| (*it).into()).collect() | ||
952 | } | ||
953 | |||
954 | pub fn is_auto(self, db: &dyn HirDatabase) -> bool { | ||
955 | db.trait_data(self.id).auto | ||
956 | } | ||
957 | } | ||
958 | |||
959 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | ||
960 | pub struct TypeAlias { | ||
961 | pub(crate) id: TypeAliasId, | ||
962 | } | ||
963 | |||
964 | impl TypeAlias { | ||
965 | pub fn has_non_default_type_params(self, db: &dyn HirDatabase) -> bool { | ||
966 | let subst = db.generic_defaults(self.id.into()); | ||
967 | subst.iter().any(|ty| &ty.value == &Ty::Unknown) | ||
968 | } | ||
969 | |||
970 | pub fn module(self, db: &dyn HirDatabase) -> Module { | ||
971 | Module { id: self.id.lookup(db.upcast()).module(db.upcast()) } | ||
972 | } | ||
973 | |||
974 | pub fn krate(self, db: &dyn HirDatabase) -> Option<Crate> { | ||
975 | Some(self.module(db).krate()) | ||
976 | } | ||
977 | |||
978 | pub fn type_ref(self, db: &dyn HirDatabase) -> Option<TypeRef> { | ||
979 | db.type_alias_data(self.id).type_ref.clone() | ||
980 | } | ||
981 | |||
982 | pub fn ty(self, db: &dyn HirDatabase) -> Type { | ||
983 | Type::from_def(db, self.id.lookup(db.upcast()).module(db.upcast()).krate(), self.id) | ||
984 | } | ||
985 | |||
986 | pub fn name(self, db: &dyn HirDatabase) -> Name { | ||
987 | db.type_alias_data(self.id).name.clone() | ||
988 | } | ||
989 | } | ||
990 | |||
991 | impl HasVisibility for TypeAlias { | ||
992 | fn visibility(&self, db: &dyn HirDatabase) -> Visibility { | ||
993 | let function_data = db.type_alias_data(self.id); | ||
994 | let visibility = &function_data.visibility; | ||
995 | visibility.resolve(db.upcast(), &self.id.resolver(db.upcast())) | ||
996 | } | ||
997 | } | ||
998 | |||
999 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | ||
1000 | pub struct BuiltinType { | ||
1001 | pub(crate) inner: hir_def::builtin_type::BuiltinType, | ||
1002 | } | ||
1003 | |||
1004 | impl BuiltinType { | ||
1005 | pub fn ty(self, db: &dyn HirDatabase, module: Module) -> Type { | ||
1006 | let resolver = module.id.resolver(db.upcast()); | ||
1007 | Type::new_with_resolver(db, &resolver, Ty::builtin(self.inner)) | ||
1008 | .expect("crate not present in resolver") | ||
1009 | } | ||
1010 | |||
1011 | pub fn name(self) -> Name { | ||
1012 | self.inner.as_name() | ||
1013 | } | ||
1014 | } | ||
1015 | |||
1016 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | ||
1017 | pub struct MacroDef { | ||
1018 | pub(crate) id: MacroDefId, | ||
1019 | } | ||
1020 | |||
1021 | impl MacroDef { | ||
1022 | /// FIXME: right now, this just returns the root module of the crate that | ||
1023 | /// defines this macro. The reasons for this is that macros are expanded | ||
1024 | /// early, in `hir_expand`, where modules simply do not exist yet. | ||
1025 | pub fn module(self, db: &dyn HirDatabase) -> Option<Module> { | ||
1026 | let krate = self.id.krate; | ||
1027 | let def_map = db.crate_def_map(krate); | ||
1028 | let module_id = def_map.root(); | ||
1029 | Some(Module { id: def_map.module_id(module_id) }) | ||
1030 | } | ||
1031 | |||
1032 | /// XXX: this parses the file | ||
1033 | pub fn name(self, db: &dyn HirDatabase) -> Option<Name> { | ||
1034 | self.source(db)?.value.name().map(|it| it.as_name()) | ||
1035 | } | ||
1036 | |||
1037 | /// Indicate it is a proc-macro | ||
1038 | pub fn is_proc_macro(&self) -> bool { | ||
1039 | matches!(self.id.kind, MacroDefKind::ProcMacro(_)) | ||
1040 | } | ||
1041 | |||
1042 | /// Indicate it is a derive macro | ||
1043 | pub fn is_derive_macro(&self) -> bool { | ||
1044 | matches!(self.id.kind, MacroDefKind::ProcMacro(_) | MacroDefKind::BuiltInDerive(_)) | ||
1045 | } | ||
1046 | } | ||
1047 | |||
1048 | /// Invariant: `inner.as_assoc_item(db).is_some()` | ||
1049 | /// We do not actively enforce this invariant. | ||
1050 | #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] | ||
1051 | pub enum AssocItem { | ||
1052 | Function(Function), | ||
1053 | Const(Const), | ||
1054 | TypeAlias(TypeAlias), | ||
1055 | } | ||
1056 | pub enum AssocItemContainer { | ||
1057 | Trait(Trait), | ||
1058 | Impl(Impl), | ||
1059 | } | ||
1060 | pub trait AsAssocItem { | ||
1061 | fn as_assoc_item(self, db: &dyn HirDatabase) -> Option<AssocItem>; | ||
1062 | } | ||
1063 | |||
1064 | impl AsAssocItem for Function { | ||
1065 | fn as_assoc_item(self, db: &dyn HirDatabase) -> Option<AssocItem> { | ||
1066 | as_assoc_item(db, AssocItem::Function, self.id) | ||
1067 | } | ||
1068 | } | ||
1069 | impl AsAssocItem for Const { | ||
1070 | fn as_assoc_item(self, db: &dyn HirDatabase) -> Option<AssocItem> { | ||
1071 | as_assoc_item(db, AssocItem::Const, self.id) | ||
1072 | } | ||
1073 | } | ||
1074 | impl AsAssocItem for TypeAlias { | ||
1075 | fn as_assoc_item(self, db: &dyn HirDatabase) -> Option<AssocItem> { | ||
1076 | as_assoc_item(db, AssocItem::TypeAlias, self.id) | ||
1077 | } | ||
1078 | } | ||
1079 | impl AsAssocItem for ModuleDef { | ||
1080 | fn as_assoc_item(self, db: &dyn HirDatabase) -> Option<AssocItem> { | ||
1081 | match self { | ||
1082 | ModuleDef::Function(it) => it.as_assoc_item(db), | ||
1083 | ModuleDef::Const(it) => it.as_assoc_item(db), | ||
1084 | ModuleDef::TypeAlias(it) => it.as_assoc_item(db), | ||
1085 | _ => None, | ||
1086 | } | ||
1087 | } | ||
1088 | } | ||
1089 | fn as_assoc_item<ID, DEF, CTOR, AST>(db: &dyn HirDatabase, ctor: CTOR, id: ID) -> Option<AssocItem> | ||
1090 | where | ||
1091 | ID: Lookup<Data = AssocItemLoc<AST>>, | ||
1092 | DEF: From<ID>, | ||
1093 | CTOR: FnOnce(DEF) -> AssocItem, | ||
1094 | AST: ItemTreeNode, | ||
1095 | { | ||
1096 | match id.lookup(db.upcast()).container { | ||
1097 | AssocContainerId::TraitId(_) | AssocContainerId::ImplId(_) => Some(ctor(DEF::from(id))), | ||
1098 | AssocContainerId::ContainerId(_) => None, | ||
1099 | } | ||
1100 | } | ||
1101 | |||
1102 | impl AssocItem { | ||
1103 | pub fn name(self, db: &dyn HirDatabase) -> Option<Name> { | ||
1104 | match self { | ||
1105 | AssocItem::Function(it) => Some(it.name(db)), | ||
1106 | AssocItem::Const(it) => it.name(db), | ||
1107 | AssocItem::TypeAlias(it) => Some(it.name(db)), | ||
1108 | } | ||
1109 | } | ||
1110 | pub fn module(self, db: &dyn HirDatabase) -> Module { | ||
1111 | match self { | ||
1112 | AssocItem::Function(f) => f.module(db), | ||
1113 | AssocItem::Const(c) => c.module(db), | ||
1114 | AssocItem::TypeAlias(t) => t.module(db), | ||
1115 | } | ||
1116 | } | ||
1117 | pub fn container(self, db: &dyn HirDatabase) -> AssocItemContainer { | ||
1118 | let container = match self { | ||
1119 | AssocItem::Function(it) => it.id.lookup(db.upcast()).container, | ||
1120 | AssocItem::Const(it) => it.id.lookup(db.upcast()).container, | ||
1121 | AssocItem::TypeAlias(it) => it.id.lookup(db.upcast()).container, | ||
1122 | }; | ||
1123 | match container { | ||
1124 | AssocContainerId::TraitId(id) => AssocItemContainer::Trait(id.into()), | ||
1125 | AssocContainerId::ImplId(id) => AssocItemContainer::Impl(id.into()), | ||
1126 | AssocContainerId::ContainerId(_) => panic!("invalid AssocItem"), | ||
1127 | } | ||
1128 | } | ||
1129 | |||
1130 | pub fn containing_trait(self, db: &dyn HirDatabase) -> Option<Trait> { | ||
1131 | match self.container(db) { | ||
1132 | AssocItemContainer::Trait(t) => Some(t), | ||
1133 | _ => None, | ||
1134 | } | ||
1135 | } | ||
1136 | } | ||
1137 | |||
1138 | impl HasVisibility for AssocItem { | ||
1139 | fn visibility(&self, db: &dyn HirDatabase) -> Visibility { | ||
1140 | match self { | ||
1141 | AssocItem::Function(f) => f.visibility(db), | ||
1142 | AssocItem::Const(c) => c.visibility(db), | ||
1143 | AssocItem::TypeAlias(t) => t.visibility(db), | ||
1144 | } | ||
1145 | } | ||
1146 | } | ||
1147 | |||
1148 | #[derive(Clone, Copy, PartialEq, Eq, Debug, Hash)] | ||
1149 | pub enum GenericDef { | ||
1150 | Function(Function), | ||
1151 | Adt(Adt), | ||
1152 | Trait(Trait), | ||
1153 | TypeAlias(TypeAlias), | ||
1154 | Impl(Impl), | ||
1155 | // enum variants cannot have generics themselves, but their parent enums | ||
1156 | // can, and this makes some code easier to write | ||
1157 | Variant(Variant), | ||
1158 | // consts can have type parameters from their parents (i.e. associated consts of traits) | ||
1159 | Const(Const), | ||
1160 | } | ||
1161 | impl_from!( | ||
1162 | Function, | ||
1163 | Adt(Struct, Enum, Union), | ||
1164 | Trait, | ||
1165 | TypeAlias, | ||
1166 | Impl, | ||
1167 | Variant, | ||
1168 | Const | ||
1169 | for GenericDef | ||
1170 | ); | ||
1171 | |||
1172 | impl GenericDef { | ||
1173 | pub fn params(self, db: &dyn HirDatabase) -> Vec<GenericParam> { | ||
1174 | let generics = db.generic_params(self.into()); | ||
1175 | let ty_params = generics | ||
1176 | .types | ||
1177 | .iter() | ||
1178 | .map(|(local_id, _)| TypeParam { id: TypeParamId { parent: self.into(), local_id } }) | ||
1179 | .map(GenericParam::TypeParam); | ||
1180 | let lt_params = generics | ||
1181 | .lifetimes | ||
1182 | .iter() | ||
1183 | .map(|(local_id, _)| LifetimeParam { | ||
1184 | id: LifetimeParamId { parent: self.into(), local_id }, | ||
1185 | }) | ||
1186 | .map(GenericParam::LifetimeParam); | ||
1187 | let const_params = generics | ||
1188 | .consts | ||
1189 | .iter() | ||
1190 | .map(|(local_id, _)| ConstParam { id: ConstParamId { parent: self.into(), local_id } }) | ||
1191 | .map(GenericParam::ConstParam); | ||
1192 | ty_params.chain(lt_params).chain(const_params).collect() | ||
1193 | } | ||
1194 | |||
1195 | pub fn type_params(self, db: &dyn HirDatabase) -> Vec<TypeParam> { | ||
1196 | let generics = db.generic_params(self.into()); | ||
1197 | generics | ||
1198 | .types | ||
1199 | .iter() | ||
1200 | .map(|(local_id, _)| TypeParam { id: TypeParamId { parent: self.into(), local_id } }) | ||
1201 | .collect() | ||
1202 | } | ||
1203 | } | ||
1204 | |||
1205 | #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] | ||
1206 | pub struct Local { | ||
1207 | pub(crate) parent: DefWithBodyId, | ||
1208 | pub(crate) pat_id: PatId, | ||
1209 | } | ||
1210 | |||
1211 | impl Local { | ||
1212 | pub fn is_param(self, db: &dyn HirDatabase) -> bool { | ||
1213 | let src = self.source(db); | ||
1214 | match src.value { | ||
1215 | Either::Left(bind_pat) => { | ||
1216 | bind_pat.syntax().ancestors().any(|it| ast::Param::can_cast(it.kind())) | ||
1217 | } | ||
1218 | Either::Right(_self_param) => true, | ||
1219 | } | ||
1220 | } | ||
1221 | |||
1222 | // FIXME: why is this an option? It shouldn't be? | ||
1223 | pub fn name(self, db: &dyn HirDatabase) -> Option<Name> { | ||
1224 | let body = db.body(self.parent.into()); | ||
1225 | match &body[self.pat_id] { | ||
1226 | Pat::Bind { name, .. } => Some(name.clone()), | ||
1227 | _ => None, | ||
1228 | } | ||
1229 | } | ||
1230 | |||
1231 | pub fn is_self(self, db: &dyn HirDatabase) -> bool { | ||
1232 | self.name(db) == Some(name![self]) | ||
1233 | } | ||
1234 | |||
1235 | pub fn is_mut(self, db: &dyn HirDatabase) -> bool { | ||
1236 | let body = db.body(self.parent.into()); | ||
1237 | match &body[self.pat_id] { | ||
1238 | Pat::Bind { mode, .. } => match mode { | ||
1239 | BindingAnnotation::Mutable | BindingAnnotation::RefMut => true, | ||
1240 | _ => false, | ||
1241 | }, | ||
1242 | _ => false, | ||
1243 | } | ||
1244 | } | ||
1245 | |||
1246 | pub fn parent(self, _db: &dyn HirDatabase) -> DefWithBody { | ||
1247 | self.parent.into() | ||
1248 | } | ||
1249 | |||
1250 | pub fn module(self, db: &dyn HirDatabase) -> Module { | ||
1251 | self.parent(db).module(db) | ||
1252 | } | ||
1253 | |||
1254 | pub fn ty(self, db: &dyn HirDatabase) -> Type { | ||
1255 | let def = DefWithBodyId::from(self.parent); | ||
1256 | let infer = db.infer(def); | ||
1257 | let ty = infer[self.pat_id].clone(); | ||
1258 | let krate = def.module(db.upcast()).krate(); | ||
1259 | Type::new(db, krate, def, ty) | ||
1260 | } | ||
1261 | |||
1262 | pub fn source(self, db: &dyn HirDatabase) -> InFile<Either<ast::IdentPat, ast::SelfParam>> { | ||
1263 | let (_body, source_map) = db.body_with_source_map(self.parent.into()); | ||
1264 | let src = source_map.pat_syntax(self.pat_id).unwrap(); // Hmm... | ||
1265 | let root = src.file_syntax(db.upcast()); | ||
1266 | src.map(|ast| { | ||
1267 | ast.map_left(|it| it.cast().unwrap().to_node(&root)).map_right(|it| it.to_node(&root)) | ||
1268 | }) | ||
1269 | } | ||
1270 | } | ||
1271 | |||
1272 | #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] | ||
1273 | pub struct Label { | ||
1274 | pub(crate) parent: DefWithBodyId, | ||
1275 | pub(crate) label_id: LabelId, | ||
1276 | } | ||
1277 | |||
1278 | impl Label { | ||
1279 | pub fn module(self, db: &dyn HirDatabase) -> Module { | ||
1280 | self.parent(db).module(db) | ||
1281 | } | ||
1282 | |||
1283 | pub fn parent(self, _db: &dyn HirDatabase) -> DefWithBody { | ||
1284 | self.parent.into() | ||
1285 | } | ||
1286 | |||
1287 | pub fn name(self, db: &dyn HirDatabase) -> Name { | ||
1288 | let body = db.body(self.parent.into()); | ||
1289 | body[self.label_id].name.clone() | ||
1290 | } | ||
1291 | |||
1292 | pub fn source(self, db: &dyn HirDatabase) -> InFile<ast::Label> { | ||
1293 | let (_body, source_map) = db.body_with_source_map(self.parent.into()); | ||
1294 | let src = source_map.label_syntax(self.label_id); | ||
1295 | let root = src.file_syntax(db.upcast()); | ||
1296 | src.map(|ast| ast.to_node(&root)) | ||
1297 | } | ||
1298 | } | ||
1299 | |||
1300 | #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] | ||
1301 | pub enum GenericParam { | ||
1302 | TypeParam(TypeParam), | ||
1303 | LifetimeParam(LifetimeParam), | ||
1304 | ConstParam(ConstParam), | ||
1305 | } | ||
1306 | impl_from!(TypeParam, LifetimeParam, ConstParam for GenericParam); | ||
1307 | |||
1308 | impl GenericParam { | ||
1309 | pub fn module(self, db: &dyn HirDatabase) -> Module { | ||
1310 | match self { | ||
1311 | GenericParam::TypeParam(it) => it.module(db), | ||
1312 | GenericParam::LifetimeParam(it) => it.module(db), | ||
1313 | GenericParam::ConstParam(it) => it.module(db), | ||
1314 | } | ||
1315 | } | ||
1316 | |||
1317 | pub fn name(self, db: &dyn HirDatabase) -> Name { | ||
1318 | match self { | ||
1319 | GenericParam::TypeParam(it) => it.name(db), | ||
1320 | GenericParam::LifetimeParam(it) => it.name(db), | ||
1321 | GenericParam::ConstParam(it) => it.name(db), | ||
1322 | } | ||
1323 | } | ||
1324 | } | ||
1325 | |||
1326 | #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] | ||
1327 | pub struct TypeParam { | ||
1328 | pub(crate) id: TypeParamId, | ||
1329 | } | ||
1330 | |||
1331 | impl TypeParam { | ||
1332 | pub fn name(self, db: &dyn HirDatabase) -> Name { | ||
1333 | let params = db.generic_params(self.id.parent); | ||
1334 | params.types[self.id.local_id].name.clone().unwrap_or_else(Name::missing) | ||
1335 | } | ||
1336 | |||
1337 | pub fn module(self, db: &dyn HirDatabase) -> Module { | ||
1338 | self.id.parent.module(db.upcast()).into() | ||
1339 | } | ||
1340 | |||
1341 | pub fn ty(self, db: &dyn HirDatabase) -> Type { | ||
1342 | let resolver = self.id.parent.resolver(db.upcast()); | ||
1343 | let environment = TraitEnvironment::lower(db, &resolver); | ||
1344 | let ty = Ty::Placeholder(self.id); | ||
1345 | Type { | ||
1346 | krate: self.id.parent.module(db.upcast()).krate(), | ||
1347 | ty: InEnvironment { value: ty, environment }, | ||
1348 | } | ||
1349 | } | ||
1350 | |||
1351 | pub fn trait_bounds(self, db: &dyn HirDatabase) -> Vec<Trait> { | ||
1352 | db.generic_predicates_for_param(self.id) | ||
1353 | .into_iter() | ||
1354 | .filter_map(|pred| match &pred.value { | ||
1355 | hir_ty::GenericPredicate::Implemented(trait_ref) => { | ||
1356 | Some(Trait::from(trait_ref.trait_)) | ||
1357 | } | ||
1358 | _ => None, | ||
1359 | }) | ||
1360 | .collect() | ||
1361 | } | ||
1362 | |||
1363 | pub fn default(self, db: &dyn HirDatabase) -> Option<Type> { | ||
1364 | let params = db.generic_defaults(self.id.parent); | ||
1365 | let local_idx = hir_ty::param_idx(db, self.id)?; | ||
1366 | let resolver = self.id.parent.resolver(db.upcast()); | ||
1367 | let environment = TraitEnvironment::lower(db, &resolver); | ||
1368 | let ty = params.get(local_idx)?.clone(); | ||
1369 | let subst = Substs::type_params(db, self.id.parent); | ||
1370 | let ty = ty.subst(&subst.prefix(local_idx)); | ||
1371 | Some(Type { | ||
1372 | krate: self.id.parent.module(db.upcast()).krate(), | ||
1373 | ty: InEnvironment { value: ty, environment }, | ||
1374 | }) | ||
1375 | } | ||
1376 | } | ||
1377 | |||
1378 | impl HirDisplay for TypeParam { | ||
1379 | fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> { | ||
1380 | write!(f, "{}", self.name(f.db))?; | ||
1381 | let bounds = f.db.generic_predicates_for_param(self.id); | ||
1382 | let substs = Substs::type_params(f.db, self.id.parent); | ||
1383 | let predicates = bounds.iter().cloned().map(|b| b.subst(&substs)).collect::<Vec<_>>(); | ||
1384 | if !(predicates.is_empty() || f.omit_verbose_types()) { | ||
1385 | write_bounds_like_dyn_trait_with_prefix(":", &predicates, f)?; | ||
1386 | } | ||
1387 | Ok(()) | ||
1388 | } | ||
1389 | } | ||
1390 | |||
1391 | #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] | ||
1392 | pub struct LifetimeParam { | ||
1393 | pub(crate) id: LifetimeParamId, | ||
1394 | } | ||
1395 | |||
1396 | impl LifetimeParam { | ||
1397 | pub fn name(self, db: &dyn HirDatabase) -> Name { | ||
1398 | let params = db.generic_params(self.id.parent); | ||
1399 | params.lifetimes[self.id.local_id].name.clone() | ||
1400 | } | ||
1401 | |||
1402 | pub fn module(self, db: &dyn HirDatabase) -> Module { | ||
1403 | self.id.parent.module(db.upcast()).into() | ||
1404 | } | ||
1405 | |||
1406 | pub fn parent(self, _db: &dyn HirDatabase) -> GenericDef { | ||
1407 | self.id.parent.into() | ||
1408 | } | ||
1409 | } | ||
1410 | |||
1411 | #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] | ||
1412 | pub struct ConstParam { | ||
1413 | pub(crate) id: ConstParamId, | ||
1414 | } | ||
1415 | |||
1416 | impl ConstParam { | ||
1417 | pub fn name(self, db: &dyn HirDatabase) -> Name { | ||
1418 | let params = db.generic_params(self.id.parent); | ||
1419 | params.consts[self.id.local_id].name.clone() | ||
1420 | } | ||
1421 | |||
1422 | pub fn module(self, db: &dyn HirDatabase) -> Module { | ||
1423 | self.id.parent.module(db.upcast()).into() | ||
1424 | } | ||
1425 | |||
1426 | pub fn parent(self, _db: &dyn HirDatabase) -> GenericDef { | ||
1427 | self.id.parent.into() | ||
1428 | } | ||
1429 | |||
1430 | pub fn ty(self, db: &dyn HirDatabase) -> Type { | ||
1431 | let def = self.id.parent; | ||
1432 | let krate = def.module(db.upcast()).krate(); | ||
1433 | Type::new(db, krate, def, db.const_param_ty(self.id)) | ||
1434 | } | ||
1435 | } | ||
1436 | |||
1437 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | ||
1438 | pub struct Impl { | ||
1439 | pub(crate) id: ImplId, | ||
1440 | } | ||
1441 | |||
1442 | impl Impl { | ||
1443 | pub fn all_in_crate(db: &dyn HirDatabase, krate: Crate) -> Vec<Impl> { | ||
1444 | let inherent = db.inherent_impls_in_crate(krate.id); | ||
1445 | let trait_ = db.trait_impls_in_crate(krate.id); | ||
1446 | |||
1447 | inherent.all_impls().chain(trait_.all_impls()).map(Self::from).collect() | ||
1448 | } | ||
1449 | pub fn for_trait(db: &dyn HirDatabase, krate: Crate, trait_: Trait) -> Vec<Impl> { | ||
1450 | let impls = db.trait_impls_in_crate(krate.id); | ||
1451 | impls.for_trait(trait_.id).map(Self::from).collect() | ||
1452 | } | ||
1453 | |||
1454 | // FIXME: the return type is wrong. This should be a hir version of | ||
1455 | // `TraitRef` (ie, resolved `TypeRef`). | ||
1456 | pub fn target_trait(self, db: &dyn HirDatabase) -> Option<TypeRef> { | ||
1457 | db.impl_data(self.id).target_trait.clone() | ||
1458 | } | ||
1459 | |||
1460 | pub fn target_ty(self, db: &dyn HirDatabase) -> Type { | ||
1461 | let impl_data = db.impl_data(self.id); | ||
1462 | let resolver = self.id.resolver(db.upcast()); | ||
1463 | let ctx = hir_ty::TyLoweringContext::new(db, &resolver); | ||
1464 | let environment = TraitEnvironment::lower(db, &resolver); | ||
1465 | let ty = Ty::from_hir(&ctx, &impl_data.target_type); | ||
1466 | Type { | ||
1467 | krate: self.id.lookup(db.upcast()).container.module(db.upcast()).krate(), | ||
1468 | ty: InEnvironment { value: ty, environment }, | ||
1469 | } | ||
1470 | } | ||
1471 | |||
1472 | pub fn items(self, db: &dyn HirDatabase) -> Vec<AssocItem> { | ||
1473 | db.impl_data(self.id).items.iter().map(|it| (*it).into()).collect() | ||
1474 | } | ||
1475 | |||
1476 | pub fn is_negative(self, db: &dyn HirDatabase) -> bool { | ||
1477 | db.impl_data(self.id).is_negative | ||
1478 | } | ||
1479 | |||
1480 | pub fn module(self, db: &dyn HirDatabase) -> Module { | ||
1481 | self.id.lookup(db.upcast()).container.module(db.upcast()).into() | ||
1482 | } | ||
1483 | |||
1484 | pub fn krate(self, db: &dyn HirDatabase) -> Crate { | ||
1485 | Crate { id: self.module(db).id.krate() } | ||
1486 | } | ||
1487 | |||
1488 | pub fn is_builtin_derive(self, db: &dyn HirDatabase) -> Option<InFile<ast::Attr>> { | ||
1489 | let src = self.source(db)?; | ||
1490 | let item = src.file_id.is_builtin_derive(db.upcast())?; | ||
1491 | let hygenic = hir_expand::hygiene::Hygiene::new(db.upcast(), item.file_id); | ||
1492 | |||
1493 | // FIXME: handle `cfg_attr` | ||
1494 | let attr = item | ||
1495 | .value | ||
1496 | .attrs() | ||
1497 | .filter_map(|it| { | ||
1498 | let path = ModPath::from_src(it.path()?, &hygenic)?; | ||
1499 | if path.as_ident()?.to_string() == "derive" { | ||
1500 | Some(it) | ||
1501 | } else { | ||
1502 | None | ||
1503 | } | ||
1504 | }) | ||
1505 | .last()?; | ||
1506 | |||
1507 | Some(item.with_value(attr)) | ||
1508 | } | ||
1509 | } | ||
1510 | |||
1511 | #[derive(Clone, PartialEq, Eq, Debug)] | ||
1512 | pub struct Type { | ||
1513 | krate: CrateId, | ||
1514 | ty: InEnvironment<Ty>, | ||
1515 | } | ||
1516 | |||
1517 | impl Type { | ||
1518 | pub(crate) fn new_with_resolver( | ||
1519 | db: &dyn HirDatabase, | ||
1520 | resolver: &Resolver, | ||
1521 | ty: Ty, | ||
1522 | ) -> Option<Type> { | ||
1523 | let krate = resolver.krate()?; | ||
1524 | Some(Type::new_with_resolver_inner(db, krate, resolver, ty)) | ||
1525 | } | ||
1526 | pub(crate) fn new_with_resolver_inner( | ||
1527 | db: &dyn HirDatabase, | ||
1528 | krate: CrateId, | ||
1529 | resolver: &Resolver, | ||
1530 | ty: Ty, | ||
1531 | ) -> Type { | ||
1532 | let environment = TraitEnvironment::lower(db, &resolver); | ||
1533 | Type { krate, ty: InEnvironment { value: ty, environment } } | ||
1534 | } | ||
1535 | |||
1536 | fn new(db: &dyn HirDatabase, krate: CrateId, lexical_env: impl HasResolver, ty: Ty) -> Type { | ||
1537 | let resolver = lexical_env.resolver(db.upcast()); | ||
1538 | let environment = TraitEnvironment::lower(db, &resolver); | ||
1539 | Type { krate, ty: InEnvironment { value: ty, environment } } | ||
1540 | } | ||
1541 | |||
1542 | fn from_def( | ||
1543 | db: &dyn HirDatabase, | ||
1544 | krate: CrateId, | ||
1545 | def: impl HasResolver + Into<TyDefId> + Into<GenericDefId>, | ||
1546 | ) -> Type { | ||
1547 | let substs = Substs::build_for_def(db, def).fill_with_unknown().build(); | ||
1548 | let ty = db.ty(def.into()).subst(&substs); | ||
1549 | Type::new(db, krate, def, ty) | ||
1550 | } | ||
1551 | |||
1552 | pub fn is_unit(&self) -> bool { | ||
1553 | matches!(self.ty.value, Ty::Tuple(0, ..)) | ||
1554 | } | ||
1555 | pub fn is_bool(&self) -> bool { | ||
1556 | matches!(self.ty.value, Ty::Scalar(Scalar::Bool)) | ||
1557 | } | ||
1558 | |||
1559 | pub fn is_mutable_reference(&self) -> bool { | ||
1560 | matches!(self.ty.value, Ty::Ref(Mutability::Mut, ..)) | ||
1561 | } | ||
1562 | |||
1563 | pub fn remove_ref(&self) -> Option<Type> { | ||
1564 | if let Ty::Ref(.., substs) = &self.ty.value { | ||
1565 | Some(self.derived(substs[0].clone())) | ||
1566 | } else { | ||
1567 | None | ||
1568 | } | ||
1569 | } | ||
1570 | |||
1571 | pub fn is_unknown(&self) -> bool { | ||
1572 | matches!(self.ty.value, Ty::Unknown) | ||
1573 | } | ||
1574 | |||
1575 | /// Checks that particular type `ty` implements `std::future::Future`. | ||
1576 | /// This function is used in `.await` syntax completion. | ||
1577 | pub fn impls_future(&self, db: &dyn HirDatabase) -> bool { | ||
1578 | // No special case for the type of async block, since Chalk can figure it out. | ||
1579 | |||
1580 | let krate = self.krate; | ||
1581 | |||
1582 | let std_future_trait = | ||
1583 | db.lang_item(krate, "future_trait".into()).and_then(|it| it.as_trait()); | ||
1584 | let std_future_trait = match std_future_trait { | ||
1585 | Some(it) => it, | ||
1586 | None => return false, | ||
1587 | }; | ||
1588 | |||
1589 | let canonical_ty = Canonical { value: self.ty.value.clone(), kinds: Arc::new([]) }; | ||
1590 | method_resolution::implements_trait( | ||
1591 | &canonical_ty, | ||
1592 | db, | ||
1593 | self.ty.environment.clone(), | ||
1594 | krate, | ||
1595 | std_future_trait, | ||
1596 | ) | ||
1597 | } | ||
1598 | |||
1599 | /// Checks that particular type `ty` implements `std::ops::FnOnce`. | ||
1600 | /// | ||
1601 | /// This function can be used to check if a particular type is callable, since FnOnce is a | ||
1602 | /// supertrait of Fn and FnMut, so all callable types implements at least FnOnce. | ||
1603 | pub fn impls_fnonce(&self, db: &dyn HirDatabase) -> bool { | ||
1604 | let krate = self.krate; | ||
1605 | |||
1606 | let fnonce_trait = match FnTrait::FnOnce.get_id(db, krate) { | ||
1607 | Some(it) => it, | ||
1608 | None => return false, | ||
1609 | }; | ||
1610 | |||
1611 | let canonical_ty = Canonical { value: self.ty.value.clone(), kinds: Arc::new([]) }; | ||
1612 | method_resolution::implements_trait_unique( | ||
1613 | &canonical_ty, | ||
1614 | db, | ||
1615 | self.ty.environment.clone(), | ||
1616 | krate, | ||
1617 | fnonce_trait, | ||
1618 | ) | ||
1619 | } | ||
1620 | |||
1621 | pub fn impls_trait(&self, db: &dyn HirDatabase, trait_: Trait, args: &[Type]) -> bool { | ||
1622 | let trait_ref = hir_ty::TraitRef { | ||
1623 | trait_: trait_.id, | ||
1624 | substs: Substs::build_for_def(db, trait_.id) | ||
1625 | .push(self.ty.value.clone()) | ||
1626 | .fill(args.iter().map(|t| t.ty.value.clone())) | ||
1627 | .build(), | ||
1628 | }; | ||
1629 | |||
1630 | let goal = Canonical { | ||
1631 | value: hir_ty::InEnvironment::new( | ||
1632 | self.ty.environment.clone(), | ||
1633 | hir_ty::Obligation::Trait(trait_ref), | ||
1634 | ), | ||
1635 | kinds: Arc::new([]), | ||
1636 | }; | ||
1637 | |||
1638 | db.trait_solve(self.krate, goal).is_some() | ||
1639 | } | ||
1640 | |||
1641 | pub fn normalize_trait_assoc_type( | ||
1642 | &self, | ||
1643 | db: &dyn HirDatabase, | ||
1644 | trait_: Trait, | ||
1645 | args: &[Type], | ||
1646 | alias: TypeAlias, | ||
1647 | ) -> Option<Type> { | ||
1648 | let subst = Substs::build_for_def(db, trait_.id) | ||
1649 | .push(self.ty.value.clone()) | ||
1650 | .fill(args.iter().map(|t| t.ty.value.clone())) | ||
1651 | .build(); | ||
1652 | let predicate = ProjectionPredicate { | ||
1653 | projection_ty: ProjectionTy { associated_ty: alias.id, parameters: subst }, | ||
1654 | ty: Ty::BoundVar(BoundVar::new(DebruijnIndex::INNERMOST, 0)), | ||
1655 | }; | ||
1656 | let goal = Canonical { | ||
1657 | value: InEnvironment::new( | ||
1658 | self.ty.environment.clone(), | ||
1659 | Obligation::Projection(predicate), | ||
1660 | ), | ||
1661 | kinds: Arc::new([TyVariableKind::General]), | ||
1662 | }; | ||
1663 | |||
1664 | match db.trait_solve(self.krate, goal)? { | ||
1665 | Solution::Unique(SolutionVariables(subst)) => subst.value.first().cloned(), | ||
1666 | Solution::Ambig(_) => None, | ||
1667 | } | ||
1668 | .map(|ty| Type { | ||
1669 | krate: self.krate, | ||
1670 | ty: InEnvironment { value: ty, environment: Arc::clone(&self.ty.environment) }, | ||
1671 | }) | ||
1672 | } | ||
1673 | |||
1674 | pub fn is_copy(&self, db: &dyn HirDatabase) -> bool { | ||
1675 | let lang_item = db.lang_item(self.krate, SmolStr::new("copy")); | ||
1676 | let copy_trait = match lang_item { | ||
1677 | Some(LangItemTarget::TraitId(it)) => it, | ||
1678 | _ => return false, | ||
1679 | }; | ||
1680 | self.impls_trait(db, copy_trait.into(), &[]) | ||
1681 | } | ||
1682 | |||
1683 | pub fn as_callable(&self, db: &dyn HirDatabase) -> Option<Callable> { | ||
1684 | let def = match self.ty.value { | ||
1685 | Ty::FnDef(def, _) => Some(def), | ||
1686 | _ => None, | ||
1687 | }; | ||
1688 | |||
1689 | let sig = self.ty.value.callable_sig(db)?; | ||
1690 | Some(Callable { ty: self.clone(), sig, def, is_bound_method: false }) | ||
1691 | } | ||
1692 | |||
1693 | pub fn is_closure(&self) -> bool { | ||
1694 | matches!(&self.ty.value, Ty::Closure { .. }) | ||
1695 | } | ||
1696 | |||
1697 | pub fn is_fn(&self) -> bool { | ||
1698 | matches!(&self.ty.value, Ty::FnDef(..) | Ty::Function { .. }) | ||
1699 | } | ||
1700 | |||
1701 | pub fn is_packed(&self, db: &dyn HirDatabase) -> bool { | ||
1702 | let adt_id = match self.ty.value { | ||
1703 | Ty::Adt(hir_ty::AdtId(adt_id), ..) => adt_id, | ||
1704 | _ => return false, | ||
1705 | }; | ||
1706 | |||
1707 | let adt = adt_id.into(); | ||
1708 | match adt { | ||
1709 | Adt::Struct(s) => matches!(s.repr(db), Some(ReprKind::Packed)), | ||
1710 | _ => false, | ||
1711 | } | ||
1712 | } | ||
1713 | |||
1714 | pub fn is_raw_ptr(&self) -> bool { | ||
1715 | matches!(&self.ty.value, Ty::Raw(..)) | ||
1716 | } | ||
1717 | |||
1718 | pub fn contains_unknown(&self) -> bool { | ||
1719 | return go(&self.ty.value); | ||
1720 | |||
1721 | fn go(ty: &Ty) -> bool { | ||
1722 | match ty { | ||
1723 | Ty::Unknown => true, | ||
1724 | _ => ty.substs().map_or(false, |substs| substs.iter().any(go)), | ||
1725 | } | ||
1726 | } | ||
1727 | } | ||
1728 | |||
1729 | pub fn fields(&self, db: &dyn HirDatabase) -> Vec<(Field, Type)> { | ||
1730 | let (variant_id, substs) = match self.ty.value { | ||
1731 | Ty::Adt(hir_ty::AdtId(AdtId::StructId(s)), ref substs) => (s.into(), substs), | ||
1732 | Ty::Adt(hir_ty::AdtId(AdtId::UnionId(u)), ref substs) => (u.into(), substs), | ||
1733 | _ => return Vec::new(), | ||
1734 | }; | ||
1735 | |||
1736 | db.field_types(variant_id) | ||
1737 | .iter() | ||
1738 | .map(|(local_id, ty)| { | ||
1739 | let def = Field { parent: variant_id.into(), id: local_id }; | ||
1740 | let ty = ty.clone().subst(substs); | ||
1741 | (def, self.derived(ty)) | ||
1742 | }) | ||
1743 | .collect() | ||
1744 | } | ||
1745 | |||
1746 | pub fn tuple_fields(&self, _db: &dyn HirDatabase) -> Vec<Type> { | ||
1747 | if let Ty::Tuple(_, substs) = &self.ty.value { | ||
1748 | substs.iter().map(|ty| self.derived(ty.clone())).collect() | ||
1749 | } else { | ||
1750 | Vec::new() | ||
1751 | } | ||
1752 | } | ||
1753 | |||
1754 | pub fn autoderef<'a>(&'a self, db: &'a dyn HirDatabase) -> impl Iterator<Item = Type> + 'a { | ||
1755 | // There should be no inference vars in types passed here | ||
1756 | // FIXME check that? | ||
1757 | let canonical = Canonical { value: self.ty.value.clone(), kinds: Arc::new([]) }; | ||
1758 | let environment = self.ty.environment.clone(); | ||
1759 | let ty = InEnvironment { value: canonical, environment }; | ||
1760 | autoderef(db, Some(self.krate), ty) | ||
1761 | .map(|canonical| canonical.value) | ||
1762 | .map(move |ty| self.derived(ty)) | ||
1763 | } | ||
1764 | |||
1765 | // This would be nicer if it just returned an iterator, but that runs into | ||
1766 | // lifetime problems, because we need to borrow temp `CrateImplDefs`. | ||
1767 | pub fn iterate_assoc_items<T>( | ||
1768 | self, | ||
1769 | db: &dyn HirDatabase, | ||
1770 | krate: Crate, | ||
1771 | mut callback: impl FnMut(AssocItem) -> Option<T>, | ||
1772 | ) -> Option<T> { | ||
1773 | for krate in self.ty.value.def_crates(db, krate.id)? { | ||
1774 | let impls = db.inherent_impls_in_crate(krate); | ||
1775 | |||
1776 | for impl_def in impls.for_self_ty(&self.ty.value) { | ||
1777 | for &item in db.impl_data(*impl_def).items.iter() { | ||
1778 | if let Some(result) = callback(item.into()) { | ||
1779 | return Some(result); | ||
1780 | } | ||
1781 | } | ||
1782 | } | ||
1783 | } | ||
1784 | None | ||
1785 | } | ||
1786 | |||
1787 | pub fn type_parameters(&self) -> impl Iterator<Item = Type> + '_ { | ||
1788 | self.ty | ||
1789 | .value | ||
1790 | .strip_references() | ||
1791 | .substs() | ||
1792 | .into_iter() | ||
1793 | .flat_map(|substs| substs.iter()) | ||
1794 | .map(move |ty| self.derived(ty.clone())) | ||
1795 | } | ||
1796 | |||
1797 | pub fn iterate_method_candidates<T>( | ||
1798 | &self, | ||
1799 | db: &dyn HirDatabase, | ||
1800 | krate: Crate, | ||
1801 | traits_in_scope: &FxHashSet<TraitId>, | ||
1802 | name: Option<&Name>, | ||
1803 | mut callback: impl FnMut(&Ty, Function) -> Option<T>, | ||
1804 | ) -> Option<T> { | ||
1805 | // There should be no inference vars in types passed here | ||
1806 | // FIXME check that? | ||
1807 | // FIXME replace Unknown by bound vars here | ||
1808 | let canonical = Canonical { value: self.ty.value.clone(), kinds: Arc::new([]) }; | ||
1809 | |||
1810 | let env = self.ty.environment.clone(); | ||
1811 | let krate = krate.id; | ||
1812 | |||
1813 | method_resolution::iterate_method_candidates( | ||
1814 | &canonical, | ||
1815 | db, | ||
1816 | env, | ||
1817 | krate, | ||
1818 | traits_in_scope, | ||
1819 | name, | ||
1820 | method_resolution::LookupMode::MethodCall, | ||
1821 | |ty, it| match it { | ||
1822 | AssocItemId::FunctionId(f) => callback(ty, f.into()), | ||
1823 | _ => None, | ||
1824 | }, | ||
1825 | ) | ||
1826 | } | ||
1827 | |||
1828 | pub fn iterate_path_candidates<T>( | ||
1829 | &self, | ||
1830 | db: &dyn HirDatabase, | ||
1831 | krate: Crate, | ||
1832 | traits_in_scope: &FxHashSet<TraitId>, | ||
1833 | name: Option<&Name>, | ||
1834 | mut callback: impl FnMut(&Ty, AssocItem) -> Option<T>, | ||
1835 | ) -> Option<T> { | ||
1836 | // There should be no inference vars in types passed here | ||
1837 | // FIXME check that? | ||
1838 | // FIXME replace Unknown by bound vars here | ||
1839 | let canonical = Canonical { value: self.ty.value.clone(), kinds: Arc::new([]) }; | ||
1840 | |||
1841 | let env = self.ty.environment.clone(); | ||
1842 | let krate = krate.id; | ||
1843 | |||
1844 | method_resolution::iterate_method_candidates( | ||
1845 | &canonical, | ||
1846 | db, | ||
1847 | env, | ||
1848 | krate, | ||
1849 | traits_in_scope, | ||
1850 | name, | ||
1851 | method_resolution::LookupMode::Path, | ||
1852 | |ty, it| callback(ty, it.into()), | ||
1853 | ) | ||
1854 | } | ||
1855 | |||
1856 | pub fn as_adt(&self) -> Option<Adt> { | ||
1857 | let (adt, _subst) = self.ty.value.as_adt()?; | ||
1858 | Some(adt.into()) | ||
1859 | } | ||
1860 | |||
1861 | pub fn as_dyn_trait(&self) -> Option<Trait> { | ||
1862 | self.ty.value.dyn_trait().map(Into::into) | ||
1863 | } | ||
1864 | |||
1865 | pub fn as_impl_traits(&self, db: &dyn HirDatabase) -> Option<Vec<Trait>> { | ||
1866 | self.ty.value.impl_trait_bounds(db).map(|it| { | ||
1867 | it.into_iter() | ||
1868 | .filter_map(|pred| match pred { | ||
1869 | hir_ty::GenericPredicate::Implemented(trait_ref) => { | ||
1870 | Some(Trait::from(trait_ref.trait_)) | ||
1871 | } | ||
1872 | _ => None, | ||
1873 | }) | ||
1874 | .collect() | ||
1875 | }) | ||
1876 | } | ||
1877 | |||
1878 | pub fn as_associated_type_parent_trait(&self, db: &dyn HirDatabase) -> Option<Trait> { | ||
1879 | self.ty.value.associated_type_parent_trait(db).map(Into::into) | ||
1880 | } | ||
1881 | |||
1882 | // FIXME: provide required accessors such that it becomes implementable from outside. | ||
1883 | pub fn is_equal_for_find_impls(&self, other: &Type) -> bool { | ||
1884 | let rref = other.remove_ref(); | ||
1885 | self.ty.value.equals_ctor(rref.as_ref().map_or(&other.ty.value, |it| &it.ty.value)) | ||
1886 | } | ||
1887 | |||
1888 | fn derived(&self, ty: Ty) -> Type { | ||
1889 | Type { | ||
1890 | krate: self.krate, | ||
1891 | ty: InEnvironment { value: ty, environment: self.ty.environment.clone() }, | ||
1892 | } | ||
1893 | } | ||
1894 | |||
1895 | pub fn walk(&self, db: &dyn HirDatabase, mut cb: impl FnMut(Type)) { | ||
1896 | // TypeWalk::walk for a Ty at first visits parameters and only after that the Ty itself. | ||
1897 | // We need a different order here. | ||
1898 | |||
1899 | fn walk_substs( | ||
1900 | db: &dyn HirDatabase, | ||
1901 | type_: &Type, | ||
1902 | substs: &Substs, | ||
1903 | cb: &mut impl FnMut(Type), | ||
1904 | ) { | ||
1905 | for ty in substs.iter() { | ||
1906 | walk_type(db, &type_.derived(ty.clone()), cb); | ||
1907 | } | ||
1908 | } | ||
1909 | |||
1910 | fn walk_bounds( | ||
1911 | db: &dyn HirDatabase, | ||
1912 | type_: &Type, | ||
1913 | bounds: &[GenericPredicate], | ||
1914 | cb: &mut impl FnMut(Type), | ||
1915 | ) { | ||
1916 | for pred in bounds { | ||
1917 | match pred { | ||
1918 | GenericPredicate::Implemented(trait_ref) => { | ||
1919 | cb(type_.clone()); | ||
1920 | walk_substs(db, type_, &trait_ref.substs, cb); | ||
1921 | } | ||
1922 | _ => (), | ||
1923 | } | ||
1924 | } | ||
1925 | } | ||
1926 | |||
1927 | fn walk_type(db: &dyn HirDatabase, type_: &Type, cb: &mut impl FnMut(Type)) { | ||
1928 | let ty = type_.ty.value.strip_references(); | ||
1929 | match ty { | ||
1930 | Ty::Adt(..) => { | ||
1931 | cb(type_.derived(ty.clone())); | ||
1932 | } | ||
1933 | Ty::AssociatedType(..) => { | ||
1934 | if let Some(_) = ty.associated_type_parent_trait(db) { | ||
1935 | cb(type_.derived(ty.clone())); | ||
1936 | } | ||
1937 | } | ||
1938 | Ty::OpaqueType(..) => { | ||
1939 | if let Some(bounds) = ty.impl_trait_bounds(db) { | ||
1940 | walk_bounds(db, &type_.derived(ty.clone()), &bounds, cb); | ||
1941 | } | ||
1942 | } | ||
1943 | Ty::Alias(AliasTy::Opaque(opaque_ty)) => { | ||
1944 | if let Some(bounds) = ty.impl_trait_bounds(db) { | ||
1945 | walk_bounds(db, &type_.derived(ty.clone()), &bounds, cb); | ||
1946 | } | ||
1947 | |||
1948 | walk_substs(db, type_, &opaque_ty.parameters, cb); | ||
1949 | } | ||
1950 | Ty::Placeholder(_) => { | ||
1951 | if let Some(bounds) = ty.impl_trait_bounds(db) { | ||
1952 | walk_bounds(db, &type_.derived(ty.clone()), &bounds, cb); | ||
1953 | } | ||
1954 | } | ||
1955 | Ty::Dyn(bounds) => { | ||
1956 | walk_bounds(db, &type_.derived(ty.clone()), bounds.as_ref(), cb); | ||
1957 | } | ||
1958 | |||
1959 | _ => {} | ||
1960 | } | ||
1961 | if let Some(substs) = ty.substs() { | ||
1962 | walk_substs(db, type_, &substs, cb); | ||
1963 | } | ||
1964 | } | ||
1965 | |||
1966 | walk_type(db, self, &mut cb); | ||
1967 | } | ||
1968 | } | ||
1969 | |||
1970 | impl HirDisplay for Type { | ||
1971 | fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> { | ||
1972 | self.ty.value.hir_fmt(f) | ||
1973 | } | ||
1974 | } | ||
1975 | |||
1976 | // FIXME: closures | ||
1977 | #[derive(Debug)] | ||
1978 | pub struct Callable { | ||
1979 | ty: Type, | ||
1980 | sig: CallableSig, | ||
1981 | def: Option<CallableDefId>, | ||
1982 | pub(crate) is_bound_method: bool, | ||
1983 | } | ||
1984 | |||
1985 | pub enum CallableKind { | ||
1986 | Function(Function), | ||
1987 | TupleStruct(Struct), | ||
1988 | TupleEnumVariant(Variant), | ||
1989 | Closure, | ||
1990 | } | ||
1991 | |||
1992 | impl Callable { | ||
1993 | pub fn kind(&self) -> CallableKind { | ||
1994 | match self.def { | ||
1995 | Some(CallableDefId::FunctionId(it)) => CallableKind::Function(it.into()), | ||
1996 | Some(CallableDefId::StructId(it)) => CallableKind::TupleStruct(it.into()), | ||
1997 | Some(CallableDefId::EnumVariantId(it)) => CallableKind::TupleEnumVariant(it.into()), | ||
1998 | None => CallableKind::Closure, | ||
1999 | } | ||
2000 | } | ||
2001 | pub fn receiver_param(&self, db: &dyn HirDatabase) -> Option<ast::SelfParam> { | ||
2002 | let func = match self.def { | ||
2003 | Some(CallableDefId::FunctionId(it)) if self.is_bound_method => it, | ||
2004 | _ => return None, | ||
2005 | }; | ||
2006 | let src = func.lookup(db.upcast()).source(db.upcast()); | ||
2007 | let param_list = src.value.param_list()?; | ||
2008 | param_list.self_param() | ||
2009 | } | ||
2010 | pub fn n_params(&self) -> usize { | ||
2011 | self.sig.params().len() - if self.is_bound_method { 1 } else { 0 } | ||
2012 | } | ||
2013 | pub fn params( | ||
2014 | &self, | ||
2015 | db: &dyn HirDatabase, | ||
2016 | ) -> Vec<(Option<Either<ast::SelfParam, ast::Pat>>, Type)> { | ||
2017 | let types = self | ||
2018 | .sig | ||
2019 | .params() | ||
2020 | .iter() | ||
2021 | .skip(if self.is_bound_method { 1 } else { 0 }) | ||
2022 | .map(|ty| self.ty.derived(ty.clone())); | ||
2023 | let patterns = match self.def { | ||
2024 | Some(CallableDefId::FunctionId(func)) => { | ||
2025 | let src = func.lookup(db.upcast()).source(db.upcast()); | ||
2026 | src.value.param_list().map(|param_list| { | ||
2027 | param_list | ||
2028 | .self_param() | ||
2029 | .map(|it| Some(Either::Left(it))) | ||
2030 | .filter(|_| !self.is_bound_method) | ||
2031 | .into_iter() | ||
2032 | .chain(param_list.params().map(|it| it.pat().map(Either::Right))) | ||
2033 | }) | ||
2034 | } | ||
2035 | _ => None, | ||
2036 | }; | ||
2037 | patterns.into_iter().flatten().chain(iter::repeat(None)).zip(types).collect() | ||
2038 | } | ||
2039 | pub fn return_type(&self) -> Type { | ||
2040 | self.ty.derived(self.sig.ret().clone()) | ||
2041 | } | ||
2042 | } | ||
2043 | |||
2044 | /// For IDE only | ||
2045 | #[derive(Debug, PartialEq, Eq, Hash)] | ||
2046 | pub enum ScopeDef { | ||
2047 | ModuleDef(ModuleDef), | ||
2048 | MacroDef(MacroDef), | ||
2049 | GenericParam(GenericParam), | ||
2050 | ImplSelfType(Impl), | ||
2051 | AdtSelfType(Adt), | ||
2052 | Local(Local), | ||
2053 | Unknown, | ||
2054 | } | ||
2055 | |||
2056 | impl ScopeDef { | ||
2057 | pub fn all_items(def: PerNs) -> ArrayVec<[Self; 3]> { | ||
2058 | let mut items = ArrayVec::new(); | ||
2059 | |||
2060 | match (def.take_types(), def.take_values()) { | ||
2061 | (Some(m1), None) => items.push(ScopeDef::ModuleDef(m1.into())), | ||
2062 | (None, Some(m2)) => items.push(ScopeDef::ModuleDef(m2.into())), | ||
2063 | (Some(m1), Some(m2)) => { | ||
2064 | // Some items, like unit structs and enum variants, are | ||
2065 | // returned as both a type and a value. Here we want | ||
2066 | // to de-duplicate them. | ||
2067 | if m1 != m2 { | ||
2068 | items.push(ScopeDef::ModuleDef(m1.into())); | ||
2069 | items.push(ScopeDef::ModuleDef(m2.into())); | ||
2070 | } else { | ||
2071 | items.push(ScopeDef::ModuleDef(m1.into())); | ||
2072 | } | ||
2073 | } | ||
2074 | (None, None) => {} | ||
2075 | }; | ||
2076 | |||
2077 | if let Some(macro_def_id) = def.take_macros() { | ||
2078 | items.push(ScopeDef::MacroDef(macro_def_id.into())); | ||
2079 | } | ||
2080 | |||
2081 | if items.is_empty() { | ||
2082 | items.push(ScopeDef::Unknown); | ||
2083 | } | ||
2084 | |||
2085 | items | ||
2086 | } | ||
2087 | } | ||
2088 | |||
2089 | pub trait HasVisibility { | ||
2090 | fn visibility(&self, db: &dyn HirDatabase) -> Visibility; | ||
2091 | fn is_visible_from(&self, db: &dyn HirDatabase, module: Module) -> bool { | ||
2092 | let vis = self.visibility(db); | ||
2093 | vis.is_visible_from(db.upcast(), module.id) | ||
2094 | } | ||
2095 | } | ||
diff --git a/crates/hir/src/from_id.rs b/crates/hir/src/from_id.rs index b5814da11..179b9d51e 100644 --- a/crates/hir/src/from_id.rs +++ b/crates/hir/src/from_id.rs | |||
@@ -11,9 +11,8 @@ use hir_def::{ | |||
11 | }; | 11 | }; |
12 | 12 | ||
13 | use crate::{ | 13 | use crate::{ |
14 | code_model::{BuiltinType, GenericParam}, | 14 | Adt, AssocItem, BuiltinType, DefWithBody, Field, GenericDef, GenericParam, Label, Local, |
15 | Adt, AssocItem, DefWithBody, Field, GenericDef, Label, Local, MacroDef, ModuleDef, Variant, | 15 | MacroDef, ModuleDef, Variant, VariantDef, |
16 | VariantDef, | ||
17 | }; | 16 | }; |
18 | 17 | ||
19 | macro_rules! from_id { | 18 | macro_rules! from_id { |
diff --git a/crates/hir/src/lib.rs b/crates/hir/src/lib.rs index 69fcdab07..58adc8fd3 100644 --- a/crates/hir/src/lib.rs +++ b/crates/hir/src/lib.rs | |||
@@ -20,49 +20,2117 @@ | |||
20 | #![recursion_limit = "512"] | 20 | #![recursion_limit = "512"] |
21 | 21 | ||
22 | mod semantics; | 22 | mod semantics; |
23 | pub mod db; | ||
24 | mod source_analyzer; | 23 | mod source_analyzer; |
25 | 24 | ||
26 | pub mod diagnostics; | ||
27 | |||
28 | mod from_id; | 25 | mod from_id; |
29 | mod code_model; | ||
30 | mod attrs; | 26 | mod attrs; |
31 | mod has_source; | 27 | mod has_source; |
32 | 28 | ||
29 | pub mod diagnostics; | ||
30 | pub mod db; | ||
31 | |||
32 | use std::{iter, sync::Arc}; | ||
33 | |||
34 | use arrayvec::ArrayVec; | ||
35 | use base_db::{CrateDisplayName, CrateId, Edition, FileId}; | ||
36 | use either::Either; | ||
37 | use hir_def::{ | ||
38 | adt::{ReprKind, VariantData}, | ||
39 | expr::{BindingAnnotation, LabelId, Pat, PatId}, | ||
40 | item_tree::ItemTreeNode, | ||
41 | lang_item::LangItemTarget, | ||
42 | per_ns::PerNs, | ||
43 | resolver::{HasResolver, Resolver}, | ||
44 | src::HasSource as _, | ||
45 | AdtId, AssocContainerId, AssocItemId, AssocItemLoc, AttrDefId, ConstId, ConstParamId, | ||
46 | DefWithBodyId, EnumId, FunctionId, GenericDefId, HasModule, ImplId, LifetimeParamId, | ||
47 | LocalEnumVariantId, LocalFieldId, Lookup, ModuleId, StaticId, StructId, TraitId, TypeAliasId, | ||
48 | TypeParamId, UnionId, | ||
49 | }; | ||
50 | use hir_expand::{diagnostics::DiagnosticSink, name::name, MacroDefKind}; | ||
51 | use hir_ty::{ | ||
52 | autoderef, | ||
53 | display::{write_bounds_like_dyn_trait_with_prefix, HirDisplayError, HirFormatter}, | ||
54 | method_resolution, | ||
55 | traits::{FnTrait, Solution, SolutionVariables}, | ||
56 | AliasTy, BoundVar, CallableDefId, CallableSig, Canonical, DebruijnIndex, GenericPredicate, | ||
57 | InEnvironment, Obligation, ProjectionPredicate, ProjectionTy, Scalar, Substs, TraitEnvironment, | ||
58 | Ty, TyDefId, TyVariableKind, | ||
59 | }; | ||
60 | use rustc_hash::FxHashSet; | ||
61 | use stdx::{format_to, impl_from}; | ||
62 | use syntax::{ | ||
63 | ast::{self, AttrsOwner, NameOwner}, | ||
64 | AstNode, SmolStr, | ||
65 | }; | ||
66 | use tt::{Ident, Leaf, Literal, TokenTree}; | ||
67 | |||
68 | use crate::db::{DefDatabase, HirDatabase}; | ||
69 | |||
33 | pub use crate::{ | 70 | pub use crate::{ |
34 | attrs::{HasAttrs, Namespace}, | 71 | attrs::{HasAttrs, Namespace}, |
35 | code_model::{ | ||
36 | Access, Adt, AsAssocItem, AssocItem, AssocItemContainer, BuiltinType, Callable, | ||
37 | CallableKind, Const, ConstParam, Crate, CrateDependency, DefWithBody, Enum, Field, | ||
38 | FieldSource, Function, GenericDef, GenericParam, HasVisibility, Impl, Label, LifetimeParam, | ||
39 | Local, MacroDef, Module, ModuleDef, ScopeDef, Static, Struct, Trait, Type, TypeAlias, | ||
40 | TypeParam, Union, Variant, VariantDef, | ||
41 | }, | ||
42 | has_source::HasSource, | 72 | has_source::HasSource, |
43 | semantics::{PathResolution, Semantics, SemanticsScope}, | 73 | semantics::{PathResolution, Semantics, SemanticsScope}, |
44 | }; | 74 | }; |
45 | 75 | ||
46 | pub use hir_def::{ | 76 | // Be careful with these re-exports. |
47 | adt::StructKind, | 77 | // |
48 | attr::{Attrs, Documentation}, | 78 | // `hir` is the boundary between the compiler and the IDE. It should try hard to |
49 | body::scope::ExprScopes, | 79 | // isolate the compiler from the ide, to allow the two to be refactored |
50 | find_path::PrefixKind, | 80 | // independently. Re-exporting something from the compiler is the sure way to |
51 | import_map, | 81 | // breach the boundary. |
52 | item_scope::ItemInNs, | 82 | // |
53 | nameres::ModuleSource, | 83 | // Generally, a refactoring which *removes* a name from this list is a good |
54 | path::{ModPath, PathKind}, | 84 | // idea! |
55 | type_ref::{Mutability, TypeRef}, | 85 | pub use { |
56 | visibility::Visibility, | 86 | hir_def::{ |
57 | }; | 87 | adt::StructKind, |
58 | pub use hir_expand::{ | 88 | attr::{Attrs, Documentation}, |
59 | name::{known, AsName, Name}, | 89 | body::scope::ExprScopes, |
60 | ExpandResult, HirFileId, InFile, MacroCallId, MacroCallLoc, /* FIXME */ MacroDefId, | 90 | find_path::PrefixKind, |
61 | MacroFile, Origin, | 91 | import_map, |
92 | item_scope::ItemInNs, | ||
93 | nameres::ModuleSource, | ||
94 | path::{ModPath, PathKind}, | ||
95 | type_ref::{Mutability, TypeRef}, | ||
96 | visibility::Visibility, | ||
97 | }, | ||
98 | hir_expand::{ | ||
99 | name::{known, Name}, | ||
100 | ExpandResult, HirFileId, InFile, MacroCallId, MacroCallLoc, /* FIXME */ MacroDefId, | ||
101 | MacroFile, Origin, | ||
102 | }, | ||
103 | hir_ty::display::HirDisplay, | ||
62 | }; | 104 | }; |
63 | pub use hir_ty::display::HirDisplay; | ||
64 | 105 | ||
65 | // These are negative re-exports: pub using these names is forbidden, they | 106 | // These are negative re-exports: pub using these names is forbidden, they |
66 | // should remain private to hir internals. | 107 | // should remain private to hir internals. |
67 | #[allow(unused)] | 108 | #[allow(unused)] |
68 | use {hir_def::path::Path, hir_expand::hygiene::Hygiene}; | 109 | use { |
110 | hir_def::path::Path, | ||
111 | hir_expand::{hygiene::Hygiene, name::AsName}, | ||
112 | }; | ||
113 | |||
114 | /// hir::Crate describes a single crate. It's the main interface with which | ||
115 | /// a crate's dependencies interact. Mostly, it should be just a proxy for the | ||
116 | /// root module. | ||
117 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | ||
118 | pub struct Crate { | ||
119 | pub(crate) id: CrateId, | ||
120 | } | ||
121 | |||
122 | #[derive(Debug)] | ||
123 | pub struct CrateDependency { | ||
124 | pub krate: Crate, | ||
125 | pub name: Name, | ||
126 | } | ||
127 | |||
128 | impl Crate { | ||
129 | pub fn dependencies(self, db: &dyn HirDatabase) -> Vec<CrateDependency> { | ||
130 | db.crate_graph()[self.id] | ||
131 | .dependencies | ||
132 | .iter() | ||
133 | .map(|dep| { | ||
134 | let krate = Crate { id: dep.crate_id }; | ||
135 | let name = dep.as_name(); | ||
136 | CrateDependency { krate, name } | ||
137 | }) | ||
138 | .collect() | ||
139 | } | ||
140 | |||
141 | // FIXME: add `transitive_reverse_dependencies`. | ||
142 | pub fn reverse_dependencies(self, db: &dyn HirDatabase) -> Vec<Crate> { | ||
143 | let crate_graph = db.crate_graph(); | ||
144 | crate_graph | ||
145 | .iter() | ||
146 | .filter(|&krate| { | ||
147 | crate_graph[krate].dependencies.iter().any(|it| it.crate_id == self.id) | ||
148 | }) | ||
149 | .map(|id| Crate { id }) | ||
150 | .collect() | ||
151 | } | ||
152 | |||
153 | pub fn root_module(self, db: &dyn HirDatabase) -> Module { | ||
154 | let def_map = db.crate_def_map(self.id); | ||
155 | Module { id: def_map.module_id(def_map.root()) } | ||
156 | } | ||
157 | |||
158 | pub fn root_file(self, db: &dyn HirDatabase) -> FileId { | ||
159 | db.crate_graph()[self.id].root_file_id | ||
160 | } | ||
161 | |||
162 | pub fn edition(self, db: &dyn HirDatabase) -> Edition { | ||
163 | db.crate_graph()[self.id].edition | ||
164 | } | ||
165 | |||
166 | pub fn display_name(self, db: &dyn HirDatabase) -> Option<CrateDisplayName> { | ||
167 | db.crate_graph()[self.id].display_name.clone() | ||
168 | } | ||
169 | |||
170 | pub fn query_external_importables( | ||
171 | self, | ||
172 | db: &dyn DefDatabase, | ||
173 | query: import_map::Query, | ||
174 | ) -> impl Iterator<Item = Either<ModuleDef, MacroDef>> { | ||
175 | import_map::search_dependencies(db, self.into(), query).into_iter().map(|item| match item { | ||
176 | ItemInNs::Types(mod_id) | ItemInNs::Values(mod_id) => Either::Left(mod_id.into()), | ||
177 | ItemInNs::Macros(mac_id) => Either::Right(mac_id.into()), | ||
178 | }) | ||
179 | } | ||
180 | |||
181 | pub fn all(db: &dyn HirDatabase) -> Vec<Crate> { | ||
182 | db.crate_graph().iter().map(|id| Crate { id }).collect() | ||
183 | } | ||
184 | |||
185 | /// Try to get the root URL of the documentation of a crate. | ||
186 | pub fn get_html_root_url(self: &Crate, db: &dyn HirDatabase) -> Option<String> { | ||
187 | // Look for #![doc(html_root_url = "...")] | ||
188 | let attrs = db.attrs(AttrDefId::ModuleId(self.root_module(db).into())); | ||
189 | let doc_attr_q = attrs.by_key("doc"); | ||
190 | |||
191 | if !doc_attr_q.exists() { | ||
192 | return None; | ||
193 | } | ||
194 | |||
195 | let doc_url = doc_attr_q.tt_values().map(|tt| { | ||
196 | let name = tt.token_trees.iter() | ||
197 | .skip_while(|tt| !matches!(tt, TokenTree::Leaf(Leaf::Ident(Ident{text: ref ident, ..})) if ident == "html_root_url")) | ||
198 | .skip(2) | ||
199 | .next(); | ||
200 | |||
201 | match name { | ||
202 | Some(TokenTree::Leaf(Leaf::Literal(Literal{ref text, ..}))) => Some(text), | ||
203 | _ => None | ||
204 | } | ||
205 | }).flat_map(|t| t).next(); | ||
206 | |||
207 | doc_url.map(|s| s.trim_matches('"').trim_end_matches('/').to_owned() + "/") | ||
208 | } | ||
209 | } | ||
210 | |||
211 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | ||
212 | pub struct Module { | ||
213 | pub(crate) id: ModuleId, | ||
214 | } | ||
215 | |||
216 | /// The defs which can be visible in the module. | ||
217 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | ||
218 | pub enum ModuleDef { | ||
219 | Module(Module), | ||
220 | Function(Function), | ||
221 | Adt(Adt), | ||
222 | // Can't be directly declared, but can be imported. | ||
223 | Variant(Variant), | ||
224 | Const(Const), | ||
225 | Static(Static), | ||
226 | Trait(Trait), | ||
227 | TypeAlias(TypeAlias), | ||
228 | BuiltinType(BuiltinType), | ||
229 | } | ||
230 | impl_from!( | ||
231 | Module, | ||
232 | Function, | ||
233 | Adt(Struct, Enum, Union), | ||
234 | Variant, | ||
235 | Const, | ||
236 | Static, | ||
237 | Trait, | ||
238 | TypeAlias, | ||
239 | BuiltinType | ||
240 | for ModuleDef | ||
241 | ); | ||
242 | |||
243 | impl From<VariantDef> for ModuleDef { | ||
244 | fn from(var: VariantDef) -> Self { | ||
245 | match var { | ||
246 | VariantDef::Struct(t) => Adt::from(t).into(), | ||
247 | VariantDef::Union(t) => Adt::from(t).into(), | ||
248 | VariantDef::Variant(t) => t.into(), | ||
249 | } | ||
250 | } | ||
251 | } | ||
252 | |||
253 | impl ModuleDef { | ||
254 | pub fn module(self, db: &dyn HirDatabase) -> Option<Module> { | ||
255 | match self { | ||
256 | ModuleDef::Module(it) => it.parent(db), | ||
257 | ModuleDef::Function(it) => Some(it.module(db)), | ||
258 | ModuleDef::Adt(it) => Some(it.module(db)), | ||
259 | ModuleDef::Variant(it) => Some(it.module(db)), | ||
260 | ModuleDef::Const(it) => Some(it.module(db)), | ||
261 | ModuleDef::Static(it) => Some(it.module(db)), | ||
262 | ModuleDef::Trait(it) => Some(it.module(db)), | ||
263 | ModuleDef::TypeAlias(it) => Some(it.module(db)), | ||
264 | ModuleDef::BuiltinType(_) => None, | ||
265 | } | ||
266 | } | ||
267 | |||
268 | pub fn canonical_path(&self, db: &dyn HirDatabase) -> Option<String> { | ||
269 | let mut segments = Vec::new(); | ||
270 | segments.push(self.name(db)?.to_string()); | ||
271 | for m in self.module(db)?.path_to_root(db) { | ||
272 | segments.extend(m.name(db).map(|it| it.to_string())) | ||
273 | } | ||
274 | segments.reverse(); | ||
275 | Some(segments.join("::")) | ||
276 | } | ||
277 | |||
278 | pub fn definition_visibility(&self, db: &dyn HirDatabase) -> Option<Visibility> { | ||
279 | let module = match self { | ||
280 | ModuleDef::Module(it) => it.parent(db)?, | ||
281 | ModuleDef::Function(it) => return Some(it.visibility(db)), | ||
282 | ModuleDef::Adt(it) => it.module(db), | ||
283 | ModuleDef::Variant(it) => { | ||
284 | let parent = it.parent_enum(db); | ||
285 | let module = it.module(db); | ||
286 | return module.visibility_of(db, &ModuleDef::Adt(Adt::Enum(parent))); | ||
287 | } | ||
288 | ModuleDef::Const(it) => return Some(it.visibility(db)), | ||
289 | ModuleDef::Static(it) => it.module(db), | ||
290 | ModuleDef::Trait(it) => it.module(db), | ||
291 | ModuleDef::TypeAlias(it) => return Some(it.visibility(db)), | ||
292 | ModuleDef::BuiltinType(_) => return None, | ||
293 | }; | ||
294 | |||
295 | module.visibility_of(db, self) | ||
296 | } | ||
297 | |||
298 | pub fn name(self, db: &dyn HirDatabase) -> Option<Name> { | ||
299 | match self { | ||
300 | ModuleDef::Adt(it) => Some(it.name(db)), | ||
301 | ModuleDef::Trait(it) => Some(it.name(db)), | ||
302 | ModuleDef::Function(it) => Some(it.name(db)), | ||
303 | ModuleDef::Variant(it) => Some(it.name(db)), | ||
304 | ModuleDef::TypeAlias(it) => Some(it.name(db)), | ||
305 | ModuleDef::Module(it) => it.name(db), | ||
306 | ModuleDef::Const(it) => it.name(db), | ||
307 | ModuleDef::Static(it) => it.name(db), | ||
308 | ModuleDef::BuiltinType(it) => Some(it.name()), | ||
309 | } | ||
310 | } | ||
311 | |||
312 | pub fn diagnostics(self, db: &dyn HirDatabase, sink: &mut DiagnosticSink) { | ||
313 | let id = match self { | ||
314 | ModuleDef::Adt(it) => match it { | ||
315 | Adt::Struct(it) => it.id.into(), | ||
316 | Adt::Enum(it) => it.id.into(), | ||
317 | Adt::Union(it) => it.id.into(), | ||
318 | }, | ||
319 | ModuleDef::Trait(it) => it.id.into(), | ||
320 | ModuleDef::Function(it) => it.id.into(), | ||
321 | ModuleDef::TypeAlias(it) => it.id.into(), | ||
322 | ModuleDef::Module(it) => it.id.into(), | ||
323 | ModuleDef::Const(it) => it.id.into(), | ||
324 | ModuleDef::Static(it) => it.id.into(), | ||
325 | _ => return, | ||
326 | }; | ||
327 | |||
328 | let module = match self.module(db) { | ||
329 | Some(it) => it, | ||
330 | None => return, | ||
331 | }; | ||
332 | |||
333 | hir_ty::diagnostics::validate_module_item(db, module.id.krate(), id, sink) | ||
334 | } | ||
335 | } | ||
336 | |||
337 | impl Module { | ||
338 | /// Name of this module. | ||
339 | pub fn name(self, db: &dyn HirDatabase) -> Option<Name> { | ||
340 | let def_map = self.id.def_map(db.upcast()); | ||
341 | let parent = def_map[self.id.local_id].parent?; | ||
342 | def_map[parent].children.iter().find_map(|(name, module_id)| { | ||
343 | if *module_id == self.id.local_id { | ||
344 | Some(name.clone()) | ||
345 | } else { | ||
346 | None | ||
347 | } | ||
348 | }) | ||
349 | } | ||
350 | |||
351 | /// Returns the crate this module is part of. | ||
352 | pub fn krate(self) -> Crate { | ||
353 | Crate { id: self.id.krate() } | ||
354 | } | ||
355 | |||
356 | /// Topmost parent of this module. Every module has a `crate_root`, but some | ||
357 | /// might be missing `krate`. This can happen if a module's file is not included | ||
358 | /// in the module tree of any target in `Cargo.toml`. | ||
359 | pub fn crate_root(self, db: &dyn HirDatabase) -> Module { | ||
360 | let def_map = db.crate_def_map(self.id.krate()); | ||
361 | Module { id: def_map.module_id(def_map.root()) } | ||
362 | } | ||
363 | |||
364 | /// Iterates over all child modules. | ||
365 | pub fn children(self, db: &dyn HirDatabase) -> impl Iterator<Item = Module> { | ||
366 | let def_map = self.id.def_map(db.upcast()); | ||
367 | let children = def_map[self.id.local_id] | ||
368 | .children | ||
369 | .iter() | ||
370 | .map(|(_, module_id)| Module { id: def_map.module_id(*module_id) }) | ||
371 | .collect::<Vec<_>>(); | ||
372 | children.into_iter() | ||
373 | } | ||
374 | |||
375 | /// Finds a parent module. | ||
376 | pub fn parent(self, db: &dyn HirDatabase) -> Option<Module> { | ||
377 | // FIXME: handle block expressions as modules (their parent is in a different DefMap) | ||
378 | let def_map = self.id.def_map(db.upcast()); | ||
379 | let parent_id = def_map[self.id.local_id].parent?; | ||
380 | Some(Module { id: def_map.module_id(parent_id) }) | ||
381 | } | ||
382 | |||
383 | pub fn path_to_root(self, db: &dyn HirDatabase) -> Vec<Module> { | ||
384 | let mut res = vec![self]; | ||
385 | let mut curr = self; | ||
386 | while let Some(next) = curr.parent(db) { | ||
387 | res.push(next); | ||
388 | curr = next | ||
389 | } | ||
390 | res | ||
391 | } | ||
392 | |||
393 | /// Returns a `ModuleScope`: a set of items, visible in this module. | ||
394 | pub fn scope( | ||
395 | self, | ||
396 | db: &dyn HirDatabase, | ||
397 | visible_from: Option<Module>, | ||
398 | ) -> Vec<(Name, ScopeDef)> { | ||
399 | self.id.def_map(db.upcast())[self.id.local_id] | ||
400 | .scope | ||
401 | .entries() | ||
402 | .filter_map(|(name, def)| { | ||
403 | if let Some(m) = visible_from { | ||
404 | let filtered = | ||
405 | def.filter_visibility(|vis| vis.is_visible_from(db.upcast(), m.id)); | ||
406 | if filtered.is_none() && !def.is_none() { | ||
407 | None | ||
408 | } else { | ||
409 | Some((name, filtered)) | ||
410 | } | ||
411 | } else { | ||
412 | Some((name, def)) | ||
413 | } | ||
414 | }) | ||
415 | .flat_map(|(name, def)| { | ||
416 | ScopeDef::all_items(def).into_iter().map(move |item| (name.clone(), item)) | ||
417 | }) | ||
418 | .collect() | ||
419 | } | ||
420 | |||
421 | pub fn visibility_of(self, db: &dyn HirDatabase, def: &ModuleDef) -> Option<Visibility> { | ||
422 | self.id.def_map(db.upcast())[self.id.local_id].scope.visibility_of(def.clone().into()) | ||
423 | } | ||
424 | |||
425 | pub fn diagnostics(self, db: &dyn HirDatabase, sink: &mut DiagnosticSink) { | ||
426 | let _p = profile::span("Module::diagnostics").detail(|| { | ||
427 | format!("{:?}", self.name(db).map_or("<unknown>".into(), |name| name.to_string())) | ||
428 | }); | ||
429 | let def_map = self.id.def_map(db.upcast()); | ||
430 | def_map.add_diagnostics(db.upcast(), self.id.local_id, sink); | ||
431 | for decl in self.declarations(db) { | ||
432 | match decl { | ||
433 | crate::ModuleDef::Function(f) => f.diagnostics(db, sink), | ||
434 | crate::ModuleDef::Module(m) => { | ||
435 | // Only add diagnostics from inline modules | ||
436 | if def_map[m.id.local_id].origin.is_inline() { | ||
437 | m.diagnostics(db, sink) | ||
438 | } | ||
439 | } | ||
440 | _ => { | ||
441 | decl.diagnostics(db, sink); | ||
442 | } | ||
443 | } | ||
444 | } | ||
445 | |||
446 | for impl_def in self.impl_defs(db) { | ||
447 | for item in impl_def.items(db) { | ||
448 | if let AssocItem::Function(f) = item { | ||
449 | f.diagnostics(db, sink); | ||
450 | } | ||
451 | } | ||
452 | } | ||
453 | } | ||
454 | |||
455 | pub fn declarations(self, db: &dyn HirDatabase) -> Vec<ModuleDef> { | ||
456 | let def_map = self.id.def_map(db.upcast()); | ||
457 | def_map[self.id.local_id].scope.declarations().map(ModuleDef::from).collect() | ||
458 | } | ||
459 | |||
460 | pub fn impl_defs(self, db: &dyn HirDatabase) -> Vec<Impl> { | ||
461 | let def_map = self.id.def_map(db.upcast()); | ||
462 | def_map[self.id.local_id].scope.impls().map(Impl::from).collect() | ||
463 | } | ||
464 | |||
465 | /// Finds a path that can be used to refer to the given item from within | ||
466 | /// this module, if possible. | ||
467 | pub fn find_use_path(self, db: &dyn DefDatabase, item: impl Into<ItemInNs>) -> Option<ModPath> { | ||
468 | hir_def::find_path::find_path(db, item.into(), self.into()) | ||
469 | } | ||
470 | |||
471 | /// Finds a path that can be used to refer to the given item from within | ||
472 | /// this module, if possible. This is used for returning import paths for use-statements. | ||
473 | pub fn find_use_path_prefixed( | ||
474 | self, | ||
475 | db: &dyn DefDatabase, | ||
476 | item: impl Into<ItemInNs>, | ||
477 | prefix_kind: PrefixKind, | ||
478 | ) -> Option<ModPath> { | ||
479 | hir_def::find_path::find_path_prefixed(db, item.into(), self.into(), prefix_kind) | ||
480 | } | ||
481 | } | ||
482 | |||
483 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | ||
484 | pub struct Field { | ||
485 | pub(crate) parent: VariantDef, | ||
486 | pub(crate) id: LocalFieldId, | ||
487 | } | ||
488 | |||
489 | #[derive(Debug, PartialEq, Eq)] | ||
490 | pub enum FieldSource { | ||
491 | Named(ast::RecordField), | ||
492 | Pos(ast::TupleField), | ||
493 | } | ||
494 | |||
495 | impl Field { | ||
496 | pub fn name(&self, db: &dyn HirDatabase) -> Name { | ||
497 | self.parent.variant_data(db).fields()[self.id].name.clone() | ||
498 | } | ||
499 | |||
500 | /// Returns the type as in the signature of the struct (i.e., with | ||
501 | /// placeholder types for type parameters). This is good for showing | ||
502 | /// signature help, but not so good to actually get the type of the field | ||
503 | /// when you actually have a variable of the struct. | ||
504 | pub fn signature_ty(&self, db: &dyn HirDatabase) -> Type { | ||
505 | let var_id = self.parent.into(); | ||
506 | let generic_def_id: GenericDefId = match self.parent { | ||
507 | VariantDef::Struct(it) => it.id.into(), | ||
508 | VariantDef::Union(it) => it.id.into(), | ||
509 | VariantDef::Variant(it) => it.parent.id.into(), | ||
510 | }; | ||
511 | let substs = Substs::type_params(db, generic_def_id); | ||
512 | let ty = db.field_types(var_id)[self.id].clone().subst(&substs); | ||
513 | Type::new(db, self.parent.module(db).id.krate(), var_id, ty) | ||
514 | } | ||
515 | |||
516 | pub fn parent_def(&self, _db: &dyn HirDatabase) -> VariantDef { | ||
517 | self.parent | ||
518 | } | ||
519 | } | ||
520 | |||
521 | impl HasVisibility for Field { | ||
522 | fn visibility(&self, db: &dyn HirDatabase) -> Visibility { | ||
523 | let variant_data = self.parent.variant_data(db); | ||
524 | let visibility = &variant_data.fields()[self.id].visibility; | ||
525 | let parent_id: hir_def::VariantId = self.parent.into(); | ||
526 | visibility.resolve(db.upcast(), &parent_id.resolver(db.upcast())) | ||
527 | } | ||
528 | } | ||
529 | |||
530 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | ||
531 | pub struct Struct { | ||
532 | pub(crate) id: StructId, | ||
533 | } | ||
534 | |||
535 | impl Struct { | ||
536 | pub fn module(self, db: &dyn HirDatabase) -> Module { | ||
537 | Module { id: self.id.lookup(db.upcast()).container } | ||
538 | } | ||
539 | |||
540 | pub fn krate(self, db: &dyn HirDatabase) -> Option<Crate> { | ||
541 | Some(self.module(db).krate()) | ||
542 | } | ||
543 | |||
544 | pub fn name(self, db: &dyn HirDatabase) -> Name { | ||
545 | db.struct_data(self.id).name.clone() | ||
546 | } | ||
547 | |||
548 | pub fn fields(self, db: &dyn HirDatabase) -> Vec<Field> { | ||
549 | db.struct_data(self.id) | ||
550 | .variant_data | ||
551 | .fields() | ||
552 | .iter() | ||
553 | .map(|(id, _)| Field { parent: self.into(), id }) | ||
554 | .collect() | ||
555 | } | ||
556 | |||
557 | pub fn ty(self, db: &dyn HirDatabase) -> Type { | ||
558 | Type::from_def(db, self.id.lookup(db.upcast()).container.krate(), self.id) | ||
559 | } | ||
560 | |||
561 | pub fn repr(self, db: &dyn HirDatabase) -> Option<ReprKind> { | ||
562 | db.struct_data(self.id).repr.clone() | ||
563 | } | ||
564 | |||
565 | pub fn kind(self, db: &dyn HirDatabase) -> StructKind { | ||
566 | self.variant_data(db).kind() | ||
567 | } | ||
568 | |||
569 | fn variant_data(self, db: &dyn HirDatabase) -> Arc<VariantData> { | ||
570 | db.struct_data(self.id).variant_data.clone() | ||
571 | } | ||
572 | } | ||
573 | |||
574 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | ||
575 | pub struct Union { | ||
576 | pub(crate) id: UnionId, | ||
577 | } | ||
578 | |||
579 | impl Union { | ||
580 | pub fn name(self, db: &dyn HirDatabase) -> Name { | ||
581 | db.union_data(self.id).name.clone() | ||
582 | } | ||
583 | |||
584 | pub fn module(self, db: &dyn HirDatabase) -> Module { | ||
585 | Module { id: self.id.lookup(db.upcast()).container } | ||
586 | } | ||
587 | |||
588 | pub fn ty(self, db: &dyn HirDatabase) -> Type { | ||
589 | Type::from_def(db, self.id.lookup(db.upcast()).container.krate(), self.id) | ||
590 | } | ||
591 | |||
592 | pub fn fields(self, db: &dyn HirDatabase) -> Vec<Field> { | ||
593 | db.union_data(self.id) | ||
594 | .variant_data | ||
595 | .fields() | ||
596 | .iter() | ||
597 | .map(|(id, _)| Field { parent: self.into(), id }) | ||
598 | .collect() | ||
599 | } | ||
600 | |||
601 | fn variant_data(self, db: &dyn HirDatabase) -> Arc<VariantData> { | ||
602 | db.union_data(self.id).variant_data.clone() | ||
603 | } | ||
604 | } | ||
605 | |||
606 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | ||
607 | pub struct Enum { | ||
608 | pub(crate) id: EnumId, | ||
609 | } | ||
610 | |||
611 | impl Enum { | ||
612 | pub fn module(self, db: &dyn HirDatabase) -> Module { | ||
613 | Module { id: self.id.lookup(db.upcast()).container } | ||
614 | } | ||
615 | |||
616 | pub fn krate(self, db: &dyn HirDatabase) -> Option<Crate> { | ||
617 | Some(self.module(db).krate()) | ||
618 | } | ||
619 | |||
620 | pub fn name(self, db: &dyn HirDatabase) -> Name { | ||
621 | db.enum_data(self.id).name.clone() | ||
622 | } | ||
623 | |||
624 | pub fn variants(self, db: &dyn HirDatabase) -> Vec<Variant> { | ||
625 | db.enum_data(self.id).variants.iter().map(|(id, _)| Variant { parent: self, id }).collect() | ||
626 | } | ||
627 | |||
628 | pub fn ty(self, db: &dyn HirDatabase) -> Type { | ||
629 | Type::from_def(db, self.id.lookup(db.upcast()).container.krate(), self.id) | ||
630 | } | ||
631 | } | ||
632 | |||
633 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | ||
634 | pub struct Variant { | ||
635 | pub(crate) parent: Enum, | ||
636 | pub(crate) id: LocalEnumVariantId, | ||
637 | } | ||
638 | |||
639 | impl Variant { | ||
640 | pub fn module(self, db: &dyn HirDatabase) -> Module { | ||
641 | self.parent.module(db) | ||
642 | } | ||
643 | pub fn parent_enum(self, _db: &dyn HirDatabase) -> Enum { | ||
644 | self.parent | ||
645 | } | ||
646 | |||
647 | pub fn name(self, db: &dyn HirDatabase) -> Name { | ||
648 | db.enum_data(self.parent.id).variants[self.id].name.clone() | ||
649 | } | ||
650 | |||
651 | pub fn fields(self, db: &dyn HirDatabase) -> Vec<Field> { | ||
652 | self.variant_data(db) | ||
653 | .fields() | ||
654 | .iter() | ||
655 | .map(|(id, _)| Field { parent: self.into(), id }) | ||
656 | .collect() | ||
657 | } | ||
658 | |||
659 | pub fn kind(self, db: &dyn HirDatabase) -> StructKind { | ||
660 | self.variant_data(db).kind() | ||
661 | } | ||
662 | |||
663 | pub(crate) fn variant_data(self, db: &dyn HirDatabase) -> Arc<VariantData> { | ||
664 | db.enum_data(self.parent.id).variants[self.id].variant_data.clone() | ||
665 | } | ||
666 | } | ||
667 | |||
668 | /// A Data Type | ||
669 | #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] | ||
670 | pub enum Adt { | ||
671 | Struct(Struct), | ||
672 | Union(Union), | ||
673 | Enum(Enum), | ||
674 | } | ||
675 | impl_from!(Struct, Union, Enum for Adt); | ||
676 | |||
677 | impl Adt { | ||
678 | pub fn has_non_default_type_params(self, db: &dyn HirDatabase) -> bool { | ||
679 | let subst = db.generic_defaults(self.into()); | ||
680 | subst.iter().any(|ty| &ty.value == &Ty::Unknown) | ||
681 | } | ||
682 | |||
683 | /// Turns this ADT into a type. Any type parameters of the ADT will be | ||
684 | /// turned into unknown types, which is good for e.g. finding the most | ||
685 | /// general set of completions, but will not look very nice when printed. | ||
686 | pub fn ty(self, db: &dyn HirDatabase) -> Type { | ||
687 | let id = AdtId::from(self); | ||
688 | Type::from_def(db, id.module(db.upcast()).krate(), id) | ||
689 | } | ||
690 | |||
691 | pub fn module(self, db: &dyn HirDatabase) -> Module { | ||
692 | match self { | ||
693 | Adt::Struct(s) => s.module(db), | ||
694 | Adt::Union(s) => s.module(db), | ||
695 | Adt::Enum(e) => e.module(db), | ||
696 | } | ||
697 | } | ||
698 | |||
699 | pub fn krate(self, db: &dyn HirDatabase) -> Option<Crate> { | ||
700 | Some(self.module(db).krate()) | ||
701 | } | ||
702 | |||
703 | pub fn name(self, db: &dyn HirDatabase) -> Name { | ||
704 | match self { | ||
705 | Adt::Struct(s) => s.name(db), | ||
706 | Adt::Union(u) => u.name(db), | ||
707 | Adt::Enum(e) => e.name(db), | ||
708 | } | ||
709 | } | ||
710 | } | ||
711 | |||
712 | #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] | ||
713 | pub enum VariantDef { | ||
714 | Struct(Struct), | ||
715 | Union(Union), | ||
716 | Variant(Variant), | ||
717 | } | ||
718 | impl_from!(Struct, Union, Variant for VariantDef); | ||
719 | |||
720 | impl VariantDef { | ||
721 | pub fn fields(self, db: &dyn HirDatabase) -> Vec<Field> { | ||
722 | match self { | ||
723 | VariantDef::Struct(it) => it.fields(db), | ||
724 | VariantDef::Union(it) => it.fields(db), | ||
725 | VariantDef::Variant(it) => it.fields(db), | ||
726 | } | ||
727 | } | ||
728 | |||
729 | pub fn module(self, db: &dyn HirDatabase) -> Module { | ||
730 | match self { | ||
731 | VariantDef::Struct(it) => it.module(db), | ||
732 | VariantDef::Union(it) => it.module(db), | ||
733 | VariantDef::Variant(it) => it.module(db), | ||
734 | } | ||
735 | } | ||
736 | |||
737 | pub fn name(&self, db: &dyn HirDatabase) -> Name { | ||
738 | match self { | ||
739 | VariantDef::Struct(s) => s.name(db), | ||
740 | VariantDef::Union(u) => u.name(db), | ||
741 | VariantDef::Variant(e) => e.name(db), | ||
742 | } | ||
743 | } | ||
744 | |||
745 | pub(crate) fn variant_data(self, db: &dyn HirDatabase) -> Arc<VariantData> { | ||
746 | match self { | ||
747 | VariantDef::Struct(it) => it.variant_data(db), | ||
748 | VariantDef::Union(it) => it.variant_data(db), | ||
749 | VariantDef::Variant(it) => it.variant_data(db), | ||
750 | } | ||
751 | } | ||
752 | } | ||
753 | |||
754 | /// The defs which have a body. | ||
755 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | ||
756 | pub enum DefWithBody { | ||
757 | Function(Function), | ||
758 | Static(Static), | ||
759 | Const(Const), | ||
760 | } | ||
761 | impl_from!(Function, Const, Static for DefWithBody); | ||
762 | |||
763 | impl DefWithBody { | ||
764 | pub fn module(self, db: &dyn HirDatabase) -> Module { | ||
765 | match self { | ||
766 | DefWithBody::Const(c) => c.module(db), | ||
767 | DefWithBody::Function(f) => f.module(db), | ||
768 | DefWithBody::Static(s) => s.module(db), | ||
769 | } | ||
770 | } | ||
771 | |||
772 | pub fn name(self, db: &dyn HirDatabase) -> Option<Name> { | ||
773 | match self { | ||
774 | DefWithBody::Function(f) => Some(f.name(db)), | ||
775 | DefWithBody::Static(s) => s.name(db), | ||
776 | DefWithBody::Const(c) => c.name(db), | ||
777 | } | ||
778 | } | ||
779 | } | ||
780 | |||
781 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | ||
782 | pub struct Function { | ||
783 | pub(crate) id: FunctionId, | ||
784 | } | ||
785 | |||
786 | impl Function { | ||
787 | pub fn module(self, db: &dyn HirDatabase) -> Module { | ||
788 | self.id.lookup(db.upcast()).module(db.upcast()).into() | ||
789 | } | ||
790 | |||
791 | pub fn krate(self, db: &dyn HirDatabase) -> Option<Crate> { | ||
792 | Some(self.module(db).krate()) | ||
793 | } | ||
794 | |||
795 | pub fn name(self, db: &dyn HirDatabase) -> Name { | ||
796 | db.function_data(self.id).name.clone() | ||
797 | } | ||
798 | |||
799 | /// Get this function's return type | ||
800 | pub fn ret_type(self, db: &dyn HirDatabase) -> Type { | ||
801 | let resolver = self.id.resolver(db.upcast()); | ||
802 | let krate = self.id.lookup(db.upcast()).container.module(db.upcast()).krate(); | ||
803 | let ret_type = &db.function_data(self.id).ret_type; | ||
804 | let ctx = hir_ty::TyLoweringContext::new(db, &resolver); | ||
805 | let ty = Ty::from_hir_ext(&ctx, ret_type).0; | ||
806 | Type::new_with_resolver_inner(db, krate, &resolver, ty) | ||
807 | } | ||
808 | |||
809 | pub fn self_param(self, db: &dyn HirDatabase) -> Option<SelfParam> { | ||
810 | if !db.function_data(self.id).has_self_param { | ||
811 | return None; | ||
812 | } | ||
813 | Some(SelfParam { func: self.id }) | ||
814 | } | ||
815 | |||
816 | pub fn assoc_fn_params(self, db: &dyn HirDatabase) -> Vec<Param> { | ||
817 | let resolver = self.id.resolver(db.upcast()); | ||
818 | let krate = self.id.lookup(db.upcast()).container.module(db.upcast()).krate(); | ||
819 | let ctx = hir_ty::TyLoweringContext::new(db, &resolver); | ||
820 | let environment = TraitEnvironment::lower(db, &resolver); | ||
821 | db.function_data(self.id) | ||
822 | .params | ||
823 | .iter() | ||
824 | .map(|type_ref| { | ||
825 | let ty = Type { | ||
826 | krate, | ||
827 | ty: InEnvironment { | ||
828 | value: Ty::from_hir_ext(&ctx, type_ref).0, | ||
829 | environment: environment.clone(), | ||
830 | }, | ||
831 | }; | ||
832 | Param { ty } | ||
833 | }) | ||
834 | .collect() | ||
835 | } | ||
836 | pub fn method_params(self, db: &dyn HirDatabase) -> Option<Vec<Param>> { | ||
837 | if self.self_param(db).is_none() { | ||
838 | return None; | ||
839 | } | ||
840 | let mut res = self.assoc_fn_params(db); | ||
841 | res.remove(0); | ||
842 | Some(res) | ||
843 | } | ||
844 | |||
845 | pub fn is_unsafe(self, db: &dyn HirDatabase) -> bool { | ||
846 | db.function_data(self.id).is_unsafe | ||
847 | } | ||
848 | |||
849 | pub fn diagnostics(self, db: &dyn HirDatabase, sink: &mut DiagnosticSink) { | ||
850 | let krate = self.module(db).id.krate(); | ||
851 | hir_def::diagnostics::validate_body(db.upcast(), self.id.into(), sink); | ||
852 | hir_ty::diagnostics::validate_module_item(db, krate, self.id.into(), sink); | ||
853 | hir_ty::diagnostics::validate_body(db, self.id.into(), sink); | ||
854 | } | ||
855 | |||
856 | /// Whether this function declaration has a definition. | ||
857 | /// | ||
858 | /// This is false in the case of required (not provided) trait methods. | ||
859 | pub fn has_body(self, db: &dyn HirDatabase) -> bool { | ||
860 | db.function_data(self.id).has_body | ||
861 | } | ||
862 | |||
863 | /// A textual representation of the HIR of this function for debugging purposes. | ||
864 | pub fn debug_hir(self, db: &dyn HirDatabase) -> String { | ||
865 | let body = db.body(self.id.into()); | ||
866 | |||
867 | let mut result = String::new(); | ||
868 | format_to!(result, "HIR expressions in the body of `{}`:\n", self.name(db)); | ||
869 | for (id, expr) in body.exprs.iter() { | ||
870 | format_to!(result, "{:?}: {:?}\n", id, expr); | ||
871 | } | ||
872 | |||
873 | result | ||
874 | } | ||
875 | } | ||
876 | |||
877 | // Note: logically, this belongs to `hir_ty`, but we are not using it there yet. | ||
878 | pub enum Access { | ||
879 | Shared, | ||
880 | Exclusive, | ||
881 | Owned, | ||
882 | } | ||
883 | |||
884 | impl From<hir_ty::Mutability> for Access { | ||
885 | fn from(mutability: hir_ty::Mutability) -> Access { | ||
886 | match mutability { | ||
887 | hir_ty::Mutability::Not => Access::Shared, | ||
888 | hir_ty::Mutability::Mut => Access::Exclusive, | ||
889 | } | ||
890 | } | ||
891 | } | ||
892 | |||
893 | #[derive(Debug)] | ||
894 | pub struct Param { | ||
895 | ty: Type, | ||
896 | } | ||
897 | |||
898 | impl Param { | ||
899 | pub fn ty(&self) -> &Type { | ||
900 | &self.ty | ||
901 | } | ||
902 | } | ||
903 | |||
904 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | ||
905 | pub struct SelfParam { | ||
906 | func: FunctionId, | ||
907 | } | ||
908 | |||
909 | impl SelfParam { | ||
910 | pub fn access(self, db: &dyn HirDatabase) -> Access { | ||
911 | let func_data = db.function_data(self.func); | ||
912 | func_data | ||
913 | .params | ||
914 | .first() | ||
915 | .map(|param| match *param { | ||
916 | TypeRef::Reference(.., mutability) => match mutability { | ||
917 | hir_def::type_ref::Mutability::Shared => Access::Shared, | ||
918 | hir_def::type_ref::Mutability::Mut => Access::Exclusive, | ||
919 | }, | ||
920 | _ => Access::Owned, | ||
921 | }) | ||
922 | .unwrap_or(Access::Owned) | ||
923 | } | ||
924 | } | ||
925 | |||
926 | impl HasVisibility for Function { | ||
927 | fn visibility(&self, db: &dyn HirDatabase) -> Visibility { | ||
928 | let function_data = db.function_data(self.id); | ||
929 | let visibility = &function_data.visibility; | ||
930 | visibility.resolve(db.upcast(), &self.id.resolver(db.upcast())) | ||
931 | } | ||
932 | } | ||
933 | |||
934 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | ||
935 | pub struct Const { | ||
936 | pub(crate) id: ConstId, | ||
937 | } | ||
938 | |||
939 | impl Const { | ||
940 | pub fn module(self, db: &dyn HirDatabase) -> Module { | ||
941 | Module { id: self.id.lookup(db.upcast()).module(db.upcast()) } | ||
942 | } | ||
943 | |||
944 | pub fn krate(self, db: &dyn HirDatabase) -> Option<Crate> { | ||
945 | Some(self.module(db).krate()) | ||
946 | } | ||
947 | |||
948 | pub fn name(self, db: &dyn HirDatabase) -> Option<Name> { | ||
949 | db.const_data(self.id).name.clone() | ||
950 | } | ||
951 | } | ||
952 | |||
953 | impl HasVisibility for Const { | ||
954 | fn visibility(&self, db: &dyn HirDatabase) -> Visibility { | ||
955 | let function_data = db.const_data(self.id); | ||
956 | let visibility = &function_data.visibility; | ||
957 | visibility.resolve(db.upcast(), &self.id.resolver(db.upcast())) | ||
958 | } | ||
959 | } | ||
960 | |||
961 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | ||
962 | pub struct Static { | ||
963 | pub(crate) id: StaticId, | ||
964 | } | ||
965 | |||
966 | impl Static { | ||
967 | pub fn module(self, db: &dyn HirDatabase) -> Module { | ||
968 | Module { id: self.id.lookup(db.upcast()).module(db.upcast()) } | ||
969 | } | ||
970 | |||
971 | pub fn krate(self, db: &dyn HirDatabase) -> Option<Crate> { | ||
972 | Some(self.module(db).krate()) | ||
973 | } | ||
974 | |||
975 | pub fn name(self, db: &dyn HirDatabase) -> Option<Name> { | ||
976 | db.static_data(self.id).name.clone() | ||
977 | } | ||
978 | |||
979 | pub fn is_mut(self, db: &dyn HirDatabase) -> bool { | ||
980 | db.static_data(self.id).mutable | ||
981 | } | ||
982 | } | ||
983 | |||
984 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | ||
985 | pub struct Trait { | ||
986 | pub(crate) id: TraitId, | ||
987 | } | ||
988 | |||
989 | impl Trait { | ||
990 | pub fn module(self, db: &dyn HirDatabase) -> Module { | ||
991 | Module { id: self.id.lookup(db.upcast()).container } | ||
992 | } | ||
993 | |||
994 | pub fn name(self, db: &dyn HirDatabase) -> Name { | ||
995 | db.trait_data(self.id).name.clone() | ||
996 | } | ||
997 | |||
998 | pub fn items(self, db: &dyn HirDatabase) -> Vec<AssocItem> { | ||
999 | db.trait_data(self.id).items.iter().map(|(_name, it)| (*it).into()).collect() | ||
1000 | } | ||
1001 | |||
1002 | pub fn is_auto(self, db: &dyn HirDatabase) -> bool { | ||
1003 | db.trait_data(self.id).auto | ||
1004 | } | ||
1005 | } | ||
1006 | |||
1007 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | ||
1008 | pub struct TypeAlias { | ||
1009 | pub(crate) id: TypeAliasId, | ||
1010 | } | ||
1011 | |||
1012 | impl TypeAlias { | ||
1013 | pub fn has_non_default_type_params(self, db: &dyn HirDatabase) -> bool { | ||
1014 | let subst = db.generic_defaults(self.id.into()); | ||
1015 | subst.iter().any(|ty| &ty.value == &Ty::Unknown) | ||
1016 | } | ||
1017 | |||
1018 | pub fn module(self, db: &dyn HirDatabase) -> Module { | ||
1019 | Module { id: self.id.lookup(db.upcast()).module(db.upcast()) } | ||
1020 | } | ||
1021 | |||
1022 | pub fn krate(self, db: &dyn HirDatabase) -> Option<Crate> { | ||
1023 | Some(self.module(db).krate()) | ||
1024 | } | ||
1025 | |||
1026 | pub fn type_ref(self, db: &dyn HirDatabase) -> Option<TypeRef> { | ||
1027 | db.type_alias_data(self.id).type_ref.clone() | ||
1028 | } | ||
1029 | |||
1030 | pub fn ty(self, db: &dyn HirDatabase) -> Type { | ||
1031 | Type::from_def(db, self.id.lookup(db.upcast()).module(db.upcast()).krate(), self.id) | ||
1032 | } | ||
1033 | |||
1034 | pub fn name(self, db: &dyn HirDatabase) -> Name { | ||
1035 | db.type_alias_data(self.id).name.clone() | ||
1036 | } | ||
1037 | } | ||
1038 | |||
1039 | impl HasVisibility for TypeAlias { | ||
1040 | fn visibility(&self, db: &dyn HirDatabase) -> Visibility { | ||
1041 | let function_data = db.type_alias_data(self.id); | ||
1042 | let visibility = &function_data.visibility; | ||
1043 | visibility.resolve(db.upcast(), &self.id.resolver(db.upcast())) | ||
1044 | } | ||
1045 | } | ||
1046 | |||
1047 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | ||
1048 | pub struct BuiltinType { | ||
1049 | pub(crate) inner: hir_def::builtin_type::BuiltinType, | ||
1050 | } | ||
1051 | |||
1052 | impl BuiltinType { | ||
1053 | pub fn ty(self, db: &dyn HirDatabase, module: Module) -> Type { | ||
1054 | let resolver = module.id.resolver(db.upcast()); | ||
1055 | Type::new_with_resolver(db, &resolver, Ty::builtin(self.inner)) | ||
1056 | .expect("crate not present in resolver") | ||
1057 | } | ||
1058 | |||
1059 | pub fn name(self) -> Name { | ||
1060 | self.inner.as_name() | ||
1061 | } | ||
1062 | } | ||
1063 | |||
1064 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | ||
1065 | pub struct MacroDef { | ||
1066 | pub(crate) id: MacroDefId, | ||
1067 | } | ||
1068 | |||
1069 | impl MacroDef { | ||
1070 | /// FIXME: right now, this just returns the root module of the crate that | ||
1071 | /// defines this macro. The reasons for this is that macros are expanded | ||
1072 | /// early, in `hir_expand`, where modules simply do not exist yet. | ||
1073 | pub fn module(self, db: &dyn HirDatabase) -> Option<Module> { | ||
1074 | let krate = self.id.krate; | ||
1075 | let def_map = db.crate_def_map(krate); | ||
1076 | let module_id = def_map.root(); | ||
1077 | Some(Module { id: def_map.module_id(module_id) }) | ||
1078 | } | ||
1079 | |||
1080 | /// XXX: this parses the file | ||
1081 | pub fn name(self, db: &dyn HirDatabase) -> Option<Name> { | ||
1082 | self.source(db)?.value.name().map(|it| it.as_name()) | ||
1083 | } | ||
1084 | |||
1085 | /// Indicate it is a proc-macro | ||
1086 | pub fn is_proc_macro(&self) -> bool { | ||
1087 | matches!(self.id.kind, MacroDefKind::ProcMacro(_)) | ||
1088 | } | ||
1089 | |||
1090 | /// Indicate it is a derive macro | ||
1091 | pub fn is_derive_macro(&self) -> bool { | ||
1092 | matches!(s |