diff options
27 files changed, 325 insertions, 307 deletions
diff --git a/Cargo.lock b/Cargo.lock index dbd7a746e..0476a15eb 100644 --- a/Cargo.lock +++ b/Cargo.lock | |||
@@ -13,9 +13,9 @@ dependencies = [ | |||
13 | 13 | ||
14 | [[package]] | 14 | [[package]] |
15 | name = "adler" | 15 | name = "adler" |
16 | version = "1.0.1" | 16 | version = "1.0.2" |
17 | source = "registry+https://github.com/rust-lang/crates.io-index" | 17 | source = "registry+https://github.com/rust-lang/crates.io-index" |
18 | checksum = "bedc89c5c7b5550ffb9372eb5c5ffc7f9f705cc3f4a128bd4669b9745f555093" | 18 | checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe" |
19 | 19 | ||
20 | [[package]] | 20 | [[package]] |
21 | name = "always-assert" | 21 | name = "always-assert" |
@@ -158,9 +158,9 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" | |||
158 | 158 | ||
159 | [[package]] | 159 | [[package]] |
160 | name = "chalk-derive" | 160 | name = "chalk-derive" |
161 | version = "0.58.0" | 161 | version = "0.59.0" |
162 | source = "registry+https://github.com/rust-lang/crates.io-index" | 162 | source = "registry+https://github.com/rust-lang/crates.io-index" |
163 | checksum = "e625b7c688272783140509a0de8f7aa9000217cb0982c9b10606a12b0b747ba8" | 163 | checksum = "4b9000fbcb67353dc8973ab9fd136277d321d85b79bd36b8756bb3ae0979a94a" |
164 | dependencies = [ | 164 | dependencies = [ |
165 | "proc-macro2", | 165 | "proc-macro2", |
166 | "quote", | 166 | "quote", |
@@ -170,9 +170,9 @@ dependencies = [ | |||
170 | 170 | ||
171 | [[package]] | 171 | [[package]] |
172 | name = "chalk-ir" | 172 | name = "chalk-ir" |
173 | version = "0.58.0" | 173 | version = "0.59.0" |
174 | source = "registry+https://github.com/rust-lang/crates.io-index" | 174 | source = "registry+https://github.com/rust-lang/crates.io-index" |
175 | checksum = "c220d870128959d7d56667060d556ffdebd490f32ee0fc9f4060a76c1193f206" | 175 | checksum = "b23528d61b3557c676eccf508fa0771a38453b379f0b780154eaa7f70afe8dfc" |
176 | dependencies = [ | 176 | dependencies = [ |
177 | "bitflags", | 177 | "bitflags", |
178 | "chalk-derive", | 178 | "chalk-derive", |
@@ -181,9 +181,9 @@ dependencies = [ | |||
181 | 181 | ||
182 | [[package]] | 182 | [[package]] |
183 | name = "chalk-recursive" | 183 | name = "chalk-recursive" |
184 | version = "0.58.0" | 184 | version = "0.59.0" |
185 | source = "registry+https://github.com/rust-lang/crates.io-index" | 185 | source = "registry+https://github.com/rust-lang/crates.io-index" |
186 | checksum = "7d8cd81a15aa936215378e695a8907b9f1af8626a27a32ee22e97a50984960da" | 186 | checksum = "a8bdd37afc666b771de8b4429fe014363d0e74aae5cc26f320f60a3eab34d744" |
187 | dependencies = [ | 187 | dependencies = [ |
188 | "chalk-derive", | 188 | "chalk-derive", |
189 | "chalk-ir", | 189 | "chalk-ir", |
@@ -194,14 +194,14 @@ dependencies = [ | |||
194 | 194 | ||
195 | [[package]] | 195 | [[package]] |
196 | name = "chalk-solve" | 196 | name = "chalk-solve" |
197 | version = "0.58.0" | 197 | version = "0.59.0" |
198 | source = "registry+https://github.com/rust-lang/crates.io-index" | 198 | source = "registry+https://github.com/rust-lang/crates.io-index" |
199 | checksum = "55571250dfe096a4c899be88c81418284c952ce1c8a06aa16afb5781b298e9c9" | 199 | checksum = "4182c42ca319cb71c89898ebc3d2671d1fa7d928123b171b66f1797a2000b9c8" |
200 | dependencies = [ | 200 | dependencies = [ |
201 | "chalk-derive", | 201 | "chalk-derive", |
202 | "chalk-ir", | 202 | "chalk-ir", |
203 | "ena", | 203 | "ena", |
204 | "itertools 0.9.0", | 204 | "itertools", |
205 | "petgraph", | 205 | "petgraph", |
206 | "rustc-hash", | 206 | "rustc-hash", |
207 | "tracing", | 207 | "tracing", |
@@ -475,7 +475,7 @@ dependencies = [ | |||
475 | "hir_def", | 475 | "hir_def", |
476 | "hir_expand", | 476 | "hir_expand", |
477 | "hir_ty", | 477 | "hir_ty", |
478 | "itertools 0.10.0", | 478 | "itertools", |
479 | "log", | 479 | "log", |
480 | "profile", | 480 | "profile", |
481 | "rustc-hash", | 481 | "rustc-hash", |
@@ -497,7 +497,7 @@ dependencies = [ | |||
497 | "fst", | 497 | "fst", |
498 | "hir_expand", | 498 | "hir_expand", |
499 | "indexmap", | 499 | "indexmap", |
500 | "itertools 0.10.0", | 500 | "itertools", |
501 | "la-arena", | 501 | "la-arena", |
502 | "log", | 502 | "log", |
503 | "mbe", | 503 | "mbe", |
@@ -541,7 +541,7 @@ dependencies = [ | |||
541 | "expect-test", | 541 | "expect-test", |
542 | "hir_def", | 542 | "hir_def", |
543 | "hir_expand", | 543 | "hir_expand", |
544 | "itertools 0.10.0", | 544 | "itertools", |
545 | "la-arena", | 545 | "la-arena", |
546 | "log", | 546 | "log", |
547 | "once_cell", | 547 | "once_cell", |
@@ -579,7 +579,7 @@ dependencies = [ | |||
579 | "ide_db", | 579 | "ide_db", |
580 | "ide_ssr", | 580 | "ide_ssr", |
581 | "indexmap", | 581 | "indexmap", |
582 | "itertools 0.10.0", | 582 | "itertools", |
583 | "log", | 583 | "log", |
584 | "oorandom", | 584 | "oorandom", |
585 | "profile", | 585 | "profile", |
@@ -601,7 +601,7 @@ dependencies = [ | |||
601 | "expect-test", | 601 | "expect-test", |
602 | "hir", | 602 | "hir", |
603 | "ide_db", | 603 | "ide_db", |
604 | "itertools 0.10.0", | 604 | "itertools", |
605 | "profile", | 605 | "profile", |
606 | "rustc-hash", | 606 | "rustc-hash", |
607 | "stdx", | 607 | "stdx", |
@@ -619,7 +619,7 @@ dependencies = [ | |||
619 | "expect-test", | 619 | "expect-test", |
620 | "hir", | 620 | "hir", |
621 | "ide_db", | 621 | "ide_db", |
622 | "itertools 0.10.0", | 622 | "itertools", |
623 | "log", | 623 | "log", |
624 | "profile", | 624 | "profile", |
625 | "rustc-hash", | 625 | "rustc-hash", |
@@ -638,7 +638,7 @@ dependencies = [ | |||
638 | "expect-test", | 638 | "expect-test", |
639 | "fst", | 639 | "fst", |
640 | "hir", | 640 | "hir", |
641 | "itertools 0.10.0", | 641 | "itertools", |
642 | "log", | 642 | "log", |
643 | "once_cell", | 643 | "once_cell", |
644 | "profile", | 644 | "profile", |
@@ -657,7 +657,7 @@ dependencies = [ | |||
657 | "expect-test", | 657 | "expect-test", |
658 | "hir", | 658 | "hir", |
659 | "ide_db", | 659 | "ide_db", |
660 | "itertools 0.10.0", | 660 | "itertools", |
661 | "rustc-hash", | 661 | "rustc-hash", |
662 | "syntax", | 662 | "syntax", |
663 | "test_utils", | 663 | "test_utils", |
@@ -716,15 +716,6 @@ dependencies = [ | |||
716 | 716 | ||
717 | [[package]] | 717 | [[package]] |
718 | name = "itertools" | 718 | name = "itertools" |
719 | version = "0.9.0" | ||
720 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
721 | checksum = "284f18f85651fe11e8a991b2adb42cb078325c996ed026d994719efcfca1d54b" | ||
722 | dependencies = [ | ||
723 | "either", | ||
724 | ] | ||
725 | |||
726 | [[package]] | ||
727 | name = "itertools" | ||
728 | version = "0.10.0" | 719 | version = "0.10.0" |
729 | source = "registry+https://github.com/rust-lang/crates.io-index" | 720 | source = "registry+https://github.com/rust-lang/crates.io-index" |
730 | checksum = "37d572918e350e82412fe766d24b15e6682fb2ed2bbe018280caa810397cb319" | 721 | checksum = "37d572918e350e82412fe766d24b15e6682fb2ed2bbe018280caa810397cb319" |
@@ -1206,7 +1197,7 @@ dependencies = [ | |||
1206 | "base_db", | 1197 | "base_db", |
1207 | "cargo_metadata", | 1198 | "cargo_metadata", |
1208 | "cfg", | 1199 | "cfg", |
1209 | "itertools 0.10.0", | 1200 | "itertools", |
1210 | "la-arena", | 1201 | "la-arena", |
1211 | "log", | 1202 | "log", |
1212 | "paths", | 1203 | "paths", |
@@ -1338,7 +1329,7 @@ dependencies = [ | |||
1338 | "ide", | 1329 | "ide", |
1339 | "ide_db", | 1330 | "ide_db", |
1340 | "ide_ssr", | 1331 | "ide_ssr", |
1341 | "itertools 0.10.0", | 1332 | "itertools", |
1342 | "jemallocator", | 1333 | "jemallocator", |
1343 | "jod-thread", | 1334 | "jod-thread", |
1344 | "log", | 1335 | "log", |
@@ -1490,9 +1481,9 @@ dependencies = [ | |||
1490 | 1481 | ||
1491 | [[package]] | 1482 | [[package]] |
1492 | name = "serde_json" | 1483 | name = "serde_json" |
1493 | version = "1.0.62" | 1484 | version = "1.0.64" |
1494 | source = "registry+https://github.com/rust-lang/crates.io-index" | 1485 | source = "registry+https://github.com/rust-lang/crates.io-index" |
1495 | checksum = "ea1c6153794552ea7cf7cf63b1231a25de00ec90db326ba6264440fa08e31486" | 1486 | checksum = "799e97dc9fdae36a5c8b8f2cae9ce2ee9fdce2058c57a93e6099d919fd982f79" |
1496 | dependencies = [ | 1487 | dependencies = [ |
1497 | "indexmap", | 1488 | "indexmap", |
1498 | "itoa", | 1489 | "itoa", |
@@ -1593,7 +1584,7 @@ dependencies = [ | |||
1593 | "arrayvec", | 1584 | "arrayvec", |
1594 | "expect-test", | 1585 | "expect-test", |
1595 | "indexmap", | 1586 | "indexmap", |
1596 | "itertools 0.10.0", | 1587 | "itertools", |
1597 | "once_cell", | 1588 | "once_cell", |
1598 | "parser", | 1589 | "parser", |
1599 | "profile", | 1590 | "profile", |
@@ -1923,18 +1914,18 @@ checksum = "06069a848f95fceae3e5e03c0ddc8cb78452b56654ee0c8e68f938cf790fb9e3" | |||
1923 | 1914 | ||
1924 | [[package]] | 1915 | [[package]] |
1925 | name = "xshell" | 1916 | name = "xshell" |
1926 | version = "0.1.8" | 1917 | version = "0.1.9" |
1927 | source = "registry+https://github.com/rust-lang/crates.io-index" | 1918 | source = "registry+https://github.com/rust-lang/crates.io-index" |
1928 | checksum = "ed373ede30cea03e8c0af22f48ee1ba80efbf06fec8b4746977e6ee703878de0" | 1919 | checksum = "6f18102278453c8f70ea5c514ac78cb4c73a0ef72a8273d17094b52f9584c0c1" |
1929 | dependencies = [ | 1920 | dependencies = [ |
1930 | "xshell-macros", | 1921 | "xshell-macros", |
1931 | ] | 1922 | ] |
1932 | 1923 | ||
1933 | [[package]] | 1924 | [[package]] |
1934 | name = "xshell-macros" | 1925 | name = "xshell-macros" |
1935 | version = "0.1.8" | 1926 | version = "0.1.9" |
1936 | source = "registry+https://github.com/rust-lang/crates.io-index" | 1927 | source = "registry+https://github.com/rust-lang/crates.io-index" |
1937 | checksum = "7f6af9f8119104697b0105989a73c578ce33f922d9d6f3dae0e8ae3d538db321" | 1928 | checksum = "6093c460064572007f885facc70bb0ca5e40a83ea7ff8b16c1abbee56fd2e767" |
1938 | 1929 | ||
1939 | [[package]] | 1930 | [[package]] |
1940 | name = "xtask" | 1931 | name = "xtask" |
diff --git a/crates/hir_def/src/find_path.rs b/crates/hir_def/src/find_path.rs index 5e2a711b8..3e19a7702 100644 --- a/crates/hir_def/src/find_path.rs +++ b/crates/hir_def/src/find_path.rs | |||
@@ -1,5 +1,7 @@ | |||
1 | //! An algorithm to find a path to refer to a certain item. | 1 | //! An algorithm to find a path to refer to a certain item. |
2 | 2 | ||
3 | use std::iter; | ||
4 | |||
3 | use hir_expand::name::{known, AsName, Name}; | 5 | use hir_expand::name::{known, AsName, Name}; |
4 | use rustc_hash::FxHashSet; | 6 | use rustc_hash::FxHashSet; |
5 | use test_utils::mark; | 7 | use test_utils::mark; |
@@ -95,7 +97,7 @@ fn find_path_inner( | |||
95 | item: ItemInNs, | 97 | item: ItemInNs, |
96 | from: ModuleId, | 98 | from: ModuleId, |
97 | max_len: usize, | 99 | max_len: usize, |
98 | prefixed: Option<PrefixKind>, | 100 | mut prefixed: Option<PrefixKind>, |
99 | ) -> Option<ModPath> { | 101 | ) -> Option<ModPath> { |
100 | if max_len == 0 { | 102 | if max_len == 0 { |
101 | return None; | 103 | return None; |
@@ -114,8 +116,9 @@ fn find_path_inner( | |||
114 | } | 116 | } |
115 | 117 | ||
116 | // - if the item is the crate root, return `crate` | 118 | // - if the item is the crate root, return `crate` |
117 | let root = def_map.module_id(def_map.root()); | 119 | let root = def_map.crate_root(db); |
118 | if item == ItemInNs::Types(ModuleDefId::ModuleId(root)) && def_map.block_id().is_none() { | 120 | if item == ItemInNs::Types(ModuleDefId::ModuleId(root)) && def_map.block_id().is_none() { |
121 | // FIXME: the `block_id()` check should be unnecessary, but affects the result | ||
119 | return Some(ModPath::from_segments(PathKind::Crate, Vec::new())); | 122 | return Some(ModPath::from_segments(PathKind::Crate, Vec::new())); |
120 | } | 123 | } |
121 | 124 | ||
@@ -165,7 +168,7 @@ fn find_path_inner( | |||
165 | 168 | ||
166 | // - otherwise, look for modules containing (reexporting) it and import it from one of those | 169 | // - otherwise, look for modules containing (reexporting) it and import it from one of those |
167 | 170 | ||
168 | let crate_root = def_map.module_id(def_map.root()); | 171 | let crate_root = def_map.crate_root(db); |
169 | let crate_attrs = db.attrs(crate_root.into()); | 172 | let crate_attrs = db.attrs(crate_root.into()); |
170 | let prefer_no_std = crate_attrs.by_key("no_std").exists(); | 173 | let prefer_no_std = crate_attrs.by_key("no_std").exists(); |
171 | let mut best_path = None; | 174 | let mut best_path = None; |
@@ -228,12 +231,16 @@ fn find_path_inner( | |||
228 | } | 231 | } |
229 | } | 232 | } |
230 | 233 | ||
231 | if let Some(mut prefix) = prefixed.map(PrefixKind::prefix) { | 234 | // If the item is declared inside a block expression, don't use a prefix, as we don't handle |
232 | if matches!(prefix, PathKind::Crate | PathKind::Super(0)) && def_map.block_id().is_some() { | 235 | // that correctly (FIXME). |
233 | // Inner items cannot be referred to via `crate::` or `self::` paths. | 236 | if let Some(item_module) = item.as_module_def_id().and_then(|did| did.module(db)) { |
234 | prefix = PathKind::Plain; | 237 | if item_module.def_map(db).block_id().is_some() && prefixed.is_some() { |
238 | mark::hit!(prefixed_in_block_expression); | ||
239 | prefixed = Some(PrefixKind::Plain); | ||
235 | } | 240 | } |
241 | } | ||
236 | 242 | ||
243 | if let Some(prefix) = prefixed.map(PrefixKind::prefix) { | ||
237 | best_path.or_else(|| { | 244 | best_path.or_else(|| { |
238 | scope_name.map(|scope_name| ModPath::from_segments(prefix, vec![scope_name])) | 245 | scope_name.map(|scope_name| ModPath::from_segments(prefix, vec![scope_name])) |
239 | }) | 246 | }) |
@@ -285,12 +292,12 @@ fn find_local_import_locations( | |||
285 | let data = &def_map[from.local_id]; | 292 | let data = &def_map[from.local_id]; |
286 | let mut worklist = | 293 | let mut worklist = |
287 | data.children.values().map(|child| def_map.module_id(*child)).collect::<Vec<_>>(); | 294 | data.children.values().map(|child| def_map.module_id(*child)).collect::<Vec<_>>(); |
288 | let mut parent = data.parent; | 295 | for ancestor in iter::successors(from.containing_module(db), |m| m.containing_module(db)) { |
289 | while let Some(p) = parent { | 296 | worklist.push(ancestor); |
290 | worklist.push(def_map.module_id(p)); | ||
291 | parent = def_map[p].parent; | ||
292 | } | 297 | } |
293 | 298 | ||
299 | let def_map = def_map.crate_root(db).def_map(db); | ||
300 | |||
294 | let mut seen: FxHashSet<_> = FxHashSet::default(); | 301 | let mut seen: FxHashSet<_> = FxHashSet::default(); |
295 | 302 | ||
296 | let mut locations = Vec::new(); | 303 | let mut locations = Vec::new(); |
@@ -301,7 +308,14 @@ fn find_local_import_locations( | |||
301 | 308 | ||
302 | let ext_def_map; | 309 | let ext_def_map; |
303 | let data = if module.krate == from.krate { | 310 | let data = if module.krate == from.krate { |
304 | &def_map[module.local_id] | 311 | if module.block.is_some() { |
312 | // Re-query the block's DefMap | ||
313 | ext_def_map = module.def_map(db); | ||
314 | &ext_def_map[module.local_id] | ||
315 | } else { | ||
316 | // Reuse the root DefMap | ||
317 | &def_map[module.local_id] | ||
318 | } | ||
305 | } else { | 319 | } else { |
306 | // The crate might reexport a module defined in another crate. | 320 | // The crate might reexport a module defined in another crate. |
307 | ext_def_map = module.def_map(db); | 321 | ext_def_map = module.def_map(db); |
@@ -828,6 +842,7 @@ mod tests { | |||
828 | 842 | ||
829 | #[test] | 843 | #[test] |
830 | fn inner_items_from_inner_module() { | 844 | fn inner_items_from_inner_module() { |
845 | mark::check!(prefixed_in_block_expression); | ||
831 | check_found_path( | 846 | check_found_path( |
832 | r#" | 847 | r#" |
833 | fn main() { | 848 | fn main() { |
@@ -869,4 +884,24 @@ mod tests { | |||
869 | "super::Struct", | 884 | "super::Struct", |
870 | ); | 885 | ); |
871 | } | 886 | } |
887 | |||
888 | #[test] | ||
889 | fn outer_items_with_inner_items_present() { | ||
890 | check_found_path( | ||
891 | r#" | ||
892 | mod module { | ||
893 | pub struct CompleteMe; | ||
894 | } | ||
895 | |||
896 | fn main() { | ||
897 | fn inner() {} | ||
898 | $0 | ||
899 | } | ||
900 | "#, | ||
901 | "module::CompleteMe", | ||
902 | "module::CompleteMe", | ||
903 | "crate::module::CompleteMe", | ||
904 | "self::module::CompleteMe", | ||
905 | ) | ||
906 | } | ||
872 | } | 907 | } |
diff --git a/crates/hir_def/src/item_scope.rs b/crates/hir_def/src/item_scope.rs index ee46c3330..4e5daa2ff 100644 --- a/crates/hir_def/src/item_scope.rs +++ b/crates/hir_def/src/item_scope.rs | |||
@@ -12,8 +12,8 @@ use stdx::format_to; | |||
12 | use test_utils::mark; | 12 | use test_utils::mark; |
13 | 13 | ||
14 | use crate::{ | 14 | use crate::{ |
15 | db::DefDatabase, per_ns::PerNs, visibility::Visibility, AdtId, BuiltinType, HasModule, ImplId, | 15 | db::DefDatabase, per_ns::PerNs, visibility::Visibility, AdtId, BuiltinType, ImplId, |
16 | LocalModuleId, Lookup, MacroDefId, ModuleDefId, ModuleId, TraitId, | 16 | LocalModuleId, MacroDefId, ModuleDefId, ModuleId, TraitId, |
17 | }; | 17 | }; |
18 | 18 | ||
19 | #[derive(Copy, Clone)] | 19 | #[derive(Copy, Clone)] |
@@ -375,19 +375,9 @@ impl ItemInNs { | |||
375 | 375 | ||
376 | /// Returns the crate defining this item (or `None` if `self` is built-in). | 376 | /// Returns the crate defining this item (or `None` if `self` is built-in). |
377 | pub fn krate(&self, db: &dyn DefDatabase) -> Option<CrateId> { | 377 | pub fn krate(&self, db: &dyn DefDatabase) -> Option<CrateId> { |
378 | Some(match self { | 378 | match self { |
379 | ItemInNs::Types(did) | ItemInNs::Values(did) => match did { | 379 | ItemInNs::Types(did) | ItemInNs::Values(did) => did.module(db).map(|m| m.krate), |
380 | ModuleDefId::ModuleId(id) => id.krate, | 380 | ItemInNs::Macros(id) => Some(id.krate), |
381 | ModuleDefId::FunctionId(id) => id.lookup(db).module(db).krate, | 381 | } |
382 | ModuleDefId::AdtId(id) => id.module(db).krate, | ||
383 | ModuleDefId::EnumVariantId(id) => id.parent.lookup(db).container.module(db).krate, | ||
384 | ModuleDefId::ConstId(id) => id.lookup(db).container.module(db).krate, | ||
385 | ModuleDefId::StaticId(id) => id.lookup(db).container.module(db).krate, | ||
386 | ModuleDefId::TraitId(id) => id.lookup(db).container.module(db).krate, | ||
387 | ModuleDefId::TypeAliasId(id) => id.lookup(db).module(db).krate, | ||
388 | ModuleDefId::BuiltinType(_) => return None, | ||
389 | }, | ||
390 | ItemInNs::Macros(id) => return Some(id.krate), | ||
391 | }) | ||
392 | } | 382 | } |
393 | } | 383 | } |
diff --git a/crates/hir_def/src/lib.rs b/crates/hir_def/src/lib.rs index 6802bc250..4498d94bb 100644 --- a/crates/hir_def/src/lib.rs +++ b/crates/hir_def/src/lib.rs | |||
@@ -97,6 +97,10 @@ impl ModuleId { | |||
97 | pub fn krate(&self) -> CrateId { | 97 | pub fn krate(&self) -> CrateId { |
98 | self.krate | 98 | self.krate |
99 | } | 99 | } |
100 | |||
101 | pub fn containing_module(&self, db: &dyn db::DefDatabase) -> Option<ModuleId> { | ||
102 | self.def_map(db).containing_module(self.local_id) | ||
103 | } | ||
100 | } | 104 | } |
101 | 105 | ||
102 | /// An ID of a module, **local** to a specific crate | 106 | /// An ID of a module, **local** to a specific crate |
@@ -529,6 +533,25 @@ impl HasModule for StaticLoc { | |||
529 | } | 533 | } |
530 | } | 534 | } |
531 | 535 | ||
536 | impl ModuleDefId { | ||
537 | /// Returns the module containing `self` (or `self`, if `self` is itself a module). | ||
538 | /// | ||
539 | /// Returns `None` if `self` refers to a primitive type. | ||
540 | pub fn module(&self, db: &dyn db::DefDatabase) -> Option<ModuleId> { | ||
541 | Some(match self { | ||
542 | ModuleDefId::ModuleId(id) => *id, | ||
543 | ModuleDefId::FunctionId(id) => id.lookup(db).module(db), | ||
544 | ModuleDefId::AdtId(id) => id.module(db), | ||
545 | ModuleDefId::EnumVariantId(id) => id.parent.lookup(db).container.module(db), | ||
546 | ModuleDefId::ConstId(id) => id.lookup(db).container.module(db), | ||
547 | ModuleDefId::StaticId(id) => id.lookup(db).container.module(db), | ||
548 | ModuleDefId::TraitId(id) => id.lookup(db).container.module(db), | ||
549 | ModuleDefId::TypeAliasId(id) => id.lookup(db).module(db), | ||
550 | ModuleDefId::BuiltinType(_) => return None, | ||
551 | }) | ||
552 | } | ||
553 | } | ||
554 | |||
532 | impl AttrDefId { | 555 | impl AttrDefId { |
533 | pub fn krate(&self, db: &dyn db::DefDatabase) -> CrateId { | 556 | pub fn krate(&self, db: &dyn db::DefDatabase) -> CrateId { |
534 | match self { | 557 | match self { |
diff --git a/crates/hir_def/src/nameres.rs b/crates/hir_def/src/nameres.rs index 6a3456f2e..003d668ca 100644 --- a/crates/hir_def/src/nameres.rs +++ b/crates/hir_def/src/nameres.rs | |||
@@ -343,6 +343,18 @@ impl DefMap { | |||
343 | Some(self.block?.parent) | 343 | Some(self.block?.parent) |
344 | } | 344 | } |
345 | 345 | ||
346 | /// Returns the module containing `local_mod`, either the parent `mod`, or the module containing | ||
347 | /// the block, if `self` corresponds to a block expression. | ||
348 | pub fn containing_module(&self, local_mod: LocalModuleId) -> Option<ModuleId> { | ||
349 | match &self[local_mod].parent { | ||
350 | Some(parent) => Some(self.module_id(*parent)), | ||
351 | None => match &self.block { | ||
352 | Some(block) => Some(block.parent), | ||
353 | None => None, | ||
354 | }, | ||
355 | } | ||
356 | } | ||
357 | |||
346 | // FIXME: this can use some more human-readable format (ideally, an IR | 358 | // FIXME: this can use some more human-readable format (ideally, an IR |
347 | // even), as this should be a great debugging aid. | 359 | // even), as this should be a great debugging aid. |
348 | pub fn dump(&self, db: &dyn DefDatabase) -> String { | 360 | pub fn dump(&self, db: &dyn DefDatabase) -> String { |
diff --git a/crates/hir_def/src/path/lower.rs b/crates/hir_def/src/path/lower.rs index a469546c1..505493a74 100644 --- a/crates/hir_def/src/path/lower.rs +++ b/crates/hir_def/src/path/lower.rs | |||
@@ -101,8 +101,12 @@ pub(super) fn lower_path(mut path: ast::Path, hygiene: &Hygiene) -> Option<Path> | |||
101 | break; | 101 | break; |
102 | } | 102 | } |
103 | ast::PathSegmentKind::SelfKw => { | 103 | ast::PathSegmentKind::SelfKw => { |
104 | kind = PathKind::Super(0); | 104 | // don't break out if `self` is the last segment of a path, this mean we got an |
105 | break; | 105 | // use tree like `foo::{self}` which we want to resolve as `foo` |
106 | if !segments.is_empty() { | ||
107 | kind = PathKind::Super(0); | ||
108 | break; | ||
109 | } | ||
106 | } | 110 | } |
107 | ast::PathSegmentKind::SuperKw => { | 111 | ast::PathSegmentKind::SuperKw => { |
108 | let nested_super_count = if let PathKind::Super(n) = kind { n } else { 0 }; | 112 | let nested_super_count = if let PathKind::Super(n) = kind { n } else { 0 }; |
@@ -117,6 +121,11 @@ pub(super) fn lower_path(mut path: ast::Path, hygiene: &Hygiene) -> Option<Path> | |||
117 | segments.reverse(); | 121 | segments.reverse(); |
118 | generic_args.reverse(); | 122 | generic_args.reverse(); |
119 | 123 | ||
124 | if segments.is_empty() && kind == PathKind::Plain && type_anchor.is_none() { | ||
125 | // plain empty paths don't exist, this means we got a single `self` segment as our path | ||
126 | kind = PathKind::Super(0); | ||
127 | } | ||
128 | |||
120 | // handle local_inner_macros : | 129 | // handle local_inner_macros : |
121 | // Basically, even in rustc it is quite hacky: | 130 | // Basically, even in rustc it is quite hacky: |
122 | // https://github.com/rust-lang/rust/blob/614f273e9388ddd7804d5cbc80b8865068a3744e/src/librustc_resolve/macros.rs#L456 | 131 | // https://github.com/rust-lang/rust/blob/614f273e9388ddd7804d5cbc80b8865068a3744e/src/librustc_resolve/macros.rs#L456 |
diff --git a/crates/hir_ty/Cargo.toml b/crates/hir_ty/Cargo.toml index 6ef9d1e7e..d1302d749 100644 --- a/crates/hir_ty/Cargo.toml +++ b/crates/hir_ty/Cargo.toml | |||
@@ -17,9 +17,9 @@ ena = "0.14.0" | |||
17 | log = "0.4.8" | 17 | log = "0.4.8" |
18 | rustc-hash = "1.1.0" | 18 | rustc-hash = "1.1.0" |
19 | scoped-tls = "1" | 19 | scoped-tls = "1" |
20 | chalk-solve = { version = "0.58", default-features = false } | 20 | chalk-solve = { version = "0.59", default-features = false } |
21 | chalk-ir = "0.58" | 21 | chalk-ir = "0.59" |
22 | chalk-recursive = "0.58" | 22 | chalk-recursive = "0.59" |
23 | la-arena = { version = "0.2.0", path = "../../lib/arena" } | 23 | la-arena = { version = "0.2.0", path = "../../lib/arena" } |
24 | 24 | ||
25 | stdx = { path = "../stdx", version = "0.0.0" } | 25 | stdx = { path = "../stdx", version = "0.0.0" } |
diff --git a/crates/ide/src/hover.rs b/crates/ide/src/hover.rs index 9a605b09d..20b799490 100644 --- a/crates/ide/src/hover.rs +++ b/crates/ide/src/hover.rs | |||
@@ -3496,4 +3496,33 @@ mod foo$0; | |||
3496 | "#]], | 3496 | "#]], |
3497 | ); | 3497 | ); |
3498 | } | 3498 | } |
3499 | |||
3500 | #[test] | ||
3501 | fn hover_self_in_use() { | ||
3502 | check( | ||
3503 | r#" | ||
3504 | //! This should not appear | ||
3505 | mod foo { | ||
3506 | /// But this should appear | ||
3507 | pub mod bar {} | ||
3508 | } | ||
3509 | use foo::bar::{self$0}; | ||
3510 | "#, | ||
3511 | expect![[r#" | ||
3512 | *self* | ||
3513 | |||
3514 | ```rust | ||
3515 | test::foo | ||
3516 | ``` | ||
3517 | |||
3518 | ```rust | ||
3519 | pub mod bar | ||
3520 | ``` | ||
3521 | |||
3522 | --- | ||
3523 | |||
3524 | But this should appear | ||
3525 | "#]], | ||
3526 | ); | ||
3527 | } | ||
3499 | } | 3528 | } |
diff --git a/crates/mbe/Cargo.toml b/crates/mbe/Cargo.toml index bbee2e32c..8b5cca22c 100644 --- a/crates/mbe/Cargo.toml +++ b/crates/mbe/Cargo.toml | |||
@@ -19,8 +19,5 @@ parser = { path = "../parser", version = "0.0.0" } | |||
19 | tt = { path = "../tt", version = "0.0.0" } | 19 | tt = { path = "../tt", version = "0.0.0" } |
20 | test_utils = { path = "../test_utils", version = "0.0.0" } | 20 | test_utils = { path = "../test_utils", version = "0.0.0" } |
21 | 21 | ||
22 | # FIXME: Paper over a bug in cargo-worspaces which block publishing | 22 | [dev-dependencies] |
23 | # https://github.com/pksunkara/cargo-workspaces/issues/39 | 23 | profile = { path = "../profile" } |
24 | # [dev-dependencies] | ||
25 | profile = { path = "../profile", version = "0.0.0" } | ||
26 | |||
diff --git a/docs/dev/README.md b/docs/dev/README.md index d6fae5295..b91013f13 100644 --- a/docs/dev/README.md +++ b/docs/dev/README.md | |||
@@ -58,8 +58,6 @@ Use `env RUN_SLOW_TESTS=1 cargo test` to run the full suite. | |||
58 | 58 | ||
59 | We use bors-ng to enforce the [not rocket science](https://graydon2.dreamwidth.org/1597.html) rule. | 59 | We use bors-ng to enforce the [not rocket science](https://graydon2.dreamwidth.org/1597.html) rule. |
60 | 60 | ||
61 | You can run `cargo xtask install-pre-commit-hook` to install git-hook to run rustfmt on commit. | ||
62 | |||
63 | # Launching rust-analyzer | 61 | # Launching rust-analyzer |
64 | 62 | ||
65 | Debugging the language server can be tricky. | 63 | Debugging the language server can be tricky. |
diff --git a/xtask/Cargo.toml b/xtask/Cargo.toml index b379797f9..0455dd2eb 100644 --- a/xtask/Cargo.toml +++ b/xtask/Cargo.toml | |||
@@ -6,9 +6,6 @@ authors = ["rust-analyzer developers"] | |||
6 | publish = false | 6 | publish = false |
7 | license = "MIT OR Apache-2.0" | 7 | license = "MIT OR Apache-2.0" |
8 | 8 | ||
9 | [lib] | ||
10 | doctest = false | ||
11 | |||
12 | [dependencies] | 9 | [dependencies] |
13 | anyhow = "1.0.26" | 10 | anyhow = "1.0.26" |
14 | flate2 = "1.0" | 11 | flate2 = "1.0" |
diff --git a/xtask/src/codegen.rs b/xtask/src/codegen.rs index adea053b6..743e83e76 100644 --- a/xtask/src/codegen.rs +++ b/xtask/src/codegen.rs | |||
@@ -20,7 +20,7 @@ use xshell::{cmd, pushenv, read_file, write_file}; | |||
20 | 20 | ||
21 | use crate::{ensure_rustfmt, project_root, Result}; | 21 | use crate::{ensure_rustfmt, project_root, Result}; |
22 | 22 | ||
23 | pub use self::{ | 23 | pub(crate) use self::{ |
24 | gen_assists_docs::{generate_assists_docs, generate_assists_tests}, | 24 | gen_assists_docs::{generate_assists_docs, generate_assists_tests}, |
25 | gen_diagnostic_docs::generate_diagnostic_docs, | 25 | gen_diagnostic_docs::generate_diagnostic_docs, |
26 | gen_feature_docs::generate_feature_docs, | 26 | gen_feature_docs::generate_feature_docs, |
@@ -30,17 +30,17 @@ pub use self::{ | |||
30 | }; | 30 | }; |
31 | 31 | ||
32 | #[derive(Debug, PartialEq, Eq, Clone, Copy)] | 32 | #[derive(Debug, PartialEq, Eq, Clone, Copy)] |
33 | pub enum Mode { | 33 | pub(crate) enum Mode { |
34 | Overwrite, | 34 | Overwrite, |
35 | Verify, | 35 | Verify, |
36 | } | 36 | } |
37 | 37 | ||
38 | pub struct CodegenCmd { | 38 | pub(crate) struct CodegenCmd { |
39 | pub features: bool, | 39 | pub(crate) features: bool, |
40 | } | 40 | } |
41 | 41 | ||
42 | impl CodegenCmd { | 42 | impl CodegenCmd { |
43 | pub fn run(self) -> Result<()> { | 43 | pub(crate) fn run(self) -> Result<()> { |
44 | if self.features { | 44 | if self.features { |
45 | generate_lint_completions(Mode::Overwrite)?; | 45 | generate_lint_completions(Mode::Overwrite)?; |
46 | } | 46 | } |
diff --git a/xtask/src/codegen/gen_assists_docs.rs b/xtask/src/codegen/gen_assists_docs.rs index 1ae1343a5..c469b388d 100644 --- a/xtask/src/codegen/gen_assists_docs.rs +++ b/xtask/src/codegen/gen_assists_docs.rs | |||
@@ -7,12 +7,12 @@ use crate::{ | |||
7 | project_root, rust_files_in, Result, | 7 | project_root, rust_files_in, Result, |
8 | }; | 8 | }; |
9 | 9 | ||
10 | pub fn generate_assists_tests(mode: Mode) -> Result<()> { | 10 | pub(crate) fn generate_assists_tests(mode: Mode) -> Result<()> { |
11 | let assists = Assist::collect()?; | 11 | let assists = Assist::collect()?; |
12 | generate_tests(&assists, mode) | 12 | generate_tests(&assists, mode) |
13 | } | 13 | } |
14 | 14 | ||
15 | pub fn generate_assists_docs(mode: Mode) -> Result<()> { | 15 | pub(crate) fn generate_assists_docs(mode: Mode) -> Result<()> { |
16 | let assists = Assist::collect()?; | 16 | let assists = Assist::collect()?; |
17 | let contents = assists.into_iter().map(|it| it.to_string()).collect::<Vec<_>>().join("\n\n"); | 17 | let contents = assists.into_iter().map(|it| it.to_string()).collect::<Vec<_>>().join("\n\n"); |
18 | let contents = format!("//{}\n{}\n", PREAMBLE, contents.trim()); | 18 | let contents = format!("//{}\n{}\n", PREAMBLE, contents.trim()); |
diff --git a/xtask/src/codegen/gen_diagnostic_docs.rs b/xtask/src/codegen/gen_diagnostic_docs.rs index 7c14d4a07..a2561817b 100644 --- a/xtask/src/codegen/gen_diagnostic_docs.rs +++ b/xtask/src/codegen/gen_diagnostic_docs.rs | |||
@@ -7,7 +7,7 @@ use crate::{ | |||
7 | project_root, rust_files, Result, | 7 | project_root, rust_files, Result, |
8 | }; | 8 | }; |
9 | 9 | ||
10 | pub fn generate_diagnostic_docs(mode: Mode) -> Result<()> { | 10 | pub(crate) fn generate_diagnostic_docs(mode: Mode) -> Result<()> { |
11 | let diagnostics = Diagnostic::collect()?; | 11 | let diagnostics = Diagnostic::collect()?; |
12 | let contents = | 12 | let contents = |
13 | diagnostics.into_iter().map(|it| it.to_string()).collect::<Vec<_>>().join("\n\n"); | 13 | diagnostics.into_iter().map(|it| it.to_string()).collect::<Vec<_>>().join("\n\n"); |
diff --git a/xtask/src/codegen/gen_feature_docs.rs b/xtask/src/codegen/gen_feature_docs.rs index 61081063b..cad7ff477 100644 --- a/xtask/src/codegen/gen_feature_docs.rs +++ b/xtask/src/codegen/gen_feature_docs.rs | |||
@@ -7,7 +7,7 @@ use crate::{ | |||
7 | project_root, rust_files, Result, | 7 | project_root, rust_files, Result, |
8 | }; | 8 | }; |
9 | 9 | ||
10 | pub fn generate_feature_docs(mode: Mode) -> Result<()> { | 10 | pub(crate) fn generate_feature_docs(mode: Mode) -> Result<()> { |
11 | let features = Feature::collect()?; | 11 | let features = Feature::collect()?; |
12 | let contents = features.into_iter().map(|it| it.to_string()).collect::<Vec<_>>().join("\n\n"); | 12 | let contents = features.into_iter().map(|it| it.to_string()).collect::<Vec<_>>().join("\n\n"); |
13 | let contents = format!("//{}\n{}\n", PREAMBLE, contents.trim()); | 13 | let contents = format!("//{}\n{}\n", PREAMBLE, contents.trim()); |
diff --git a/xtask/src/codegen/gen_lint_completions.rs b/xtask/src/codegen/gen_lint_completions.rs index 8c51d35c7..b1c057037 100644 --- a/xtask/src/codegen/gen_lint_completions.rs +++ b/xtask/src/codegen/gen_lint_completions.rs | |||
@@ -10,7 +10,7 @@ use crate::{ | |||
10 | run_rustfmt, | 10 | run_rustfmt, |
11 | }; | 11 | }; |
12 | 12 | ||
13 | pub fn generate_lint_completions(mode: Mode) -> Result<()> { | 13 | pub(crate) fn generate_lint_completions(mode: Mode) -> Result<()> { |
14 | if !Path::new("./target/rust").exists() { | 14 | if !Path::new("./target/rust").exists() { |
15 | cmd!("git clone --depth=1 https://github.com/rust-lang/rust ./target/rust").run()?; | 15 | cmd!("git clone --depth=1 https://github.com/rust-lang/rust ./target/rust").run()?; |
16 | } | 16 | } |
diff --git a/xtask/src/codegen/gen_parser_tests.rs b/xtask/src/codegen/gen_parser_tests.rs index 6e4abd10c..cb8939063 100644 --- a/xtask/src/codegen/gen_parser_tests.rs +++ b/xtask/src/codegen/gen_parser_tests.rs | |||
@@ -12,7 +12,7 @@ use crate::{ | |||
12 | project_root, Result, | 12 | project_root, Result, |
13 | }; | 13 | }; |
14 | 14 | ||
15 | pub fn generate_parser_tests(mode: Mode) -> Result<()> { | 15 | pub(crate) fn generate_parser_tests(mode: Mode) -> Result<()> { |
16 | let tests = tests_from_dir(&project_root().join(Path::new("crates/parser/src/grammar")))?; | 16 | let tests = tests_from_dir(&project_root().join(Path::new("crates/parser/src/grammar")))?; |
17 | fn install_tests(tests: &HashMap<String, Test>, into: &str, mode: Mode) -> Result<()> { | 17 | fn install_tests(tests: &HashMap<String, Test>, into: &str, mode: Mode) -> Result<()> { |
18 | let tests_dir = project_root().join(into); | 18 | let tests_dir = project_root().join(into); |
diff --git a/xtask/src/codegen/gen_syntax.rs b/xtask/src/codegen/gen_syntax.rs index eb524d85a..191bc0e9d 100644 --- a/xtask/src/codegen/gen_syntax.rs +++ b/xtask/src/codegen/gen_syntax.rs | |||
@@ -18,7 +18,7 @@ use crate::{ | |||
18 | project_root, Result, | 18 | project_root, Result, |
19 | }; | 19 | }; |
20 | 20 | ||
21 | pub fn generate_syntax(mode: Mode) -> Result<()> { | 21 | pub(crate) fn generate_syntax(mode: Mode) -> Result<()> { |
22 | let grammar = rust_grammar(); | 22 | let grammar = rust_grammar(); |
23 | let ast = lower(&grammar); | 23 | let ast = lower(&grammar); |
24 | 24 | ||
diff --git a/xtask/src/dist.rs b/xtask/src/dist.rs index 56bf9f99d..f2503f807 100644 --- a/xtask/src/dist.rs +++ b/xtask/src/dist.rs | |||
@@ -11,13 +11,13 @@ use xshell::{cmd, cp, mkdir_p, pushd, read_file, rm_rf, write_file}; | |||
11 | 11 | ||
12 | use crate::{date_iso, project_root}; | 12 | use crate::{date_iso, project_root}; |
13 | 13 | ||
14 | pub struct DistCmd { | 14 | pub(crate) struct DistCmd { |
15 | pub nightly: bool, | 15 | pub(crate) nightly: bool, |
16 | pub client_version: Option<String>, | 16 | pub(crate) client_version: Option<String>, |
17 | } | 17 | } |
18 | 18 | ||
19 | impl DistCmd { | 19 | impl DistCmd { |
20 | pub fn run(self) -> Result<()> { | 20 | pub(crate) fn run(self) -> Result<()> { |
21 | let dist = project_root().join("dist"); | 21 | let dist = project_root().join("dist"); |
22 | rm_rf(&dist)?; | 22 | rm_rf(&dist)?; |
23 | mkdir_p(&dist)?; | 23 | mkdir_p(&dist)?; |
diff --git a/xtask/src/install.rs b/xtask/src/install.rs index 4c5c2673c..ea2194248 100644 --- a/xtask/src/install.rs +++ b/xtask/src/install.rs | |||
@@ -8,13 +8,13 @@ use xshell::{cmd, pushd}; | |||
8 | // Latest stable, feel free to send a PR if this lags behind. | 8 | // Latest stable, feel free to send a PR if this lags behind. |
9 | const REQUIRED_RUST_VERSION: u32 = 50; | 9 | const REQUIRED_RUST_VERSION: u32 = 50; |
10 | 10 | ||
11 | pub struct InstallCmd { | 11 | pub(crate) struct InstallCmd { |
12 | pub client: Option<ClientOpt>, | 12 | pub(crate) client: Option<ClientOpt>, |
13 | pub server: Option<ServerOpt>, | 13 | pub(crate) server: Option<ServerOpt>, |
14 | } | 14 | } |
15 | 15 | ||
16 | #[derive(Clone, Copy)] | 16 | #[derive(Clone, Copy)] |
17 | pub enum ClientOpt { | 17 | pub(crate) enum ClientOpt { |
18 | VsCode, | 18 | VsCode, |
19 | VsCodeExploration, | 19 | VsCodeExploration, |
20 | VsCodeInsiders, | 20 | VsCodeInsiders, |
@@ -24,7 +24,7 @@ pub enum ClientOpt { | |||
24 | } | 24 | } |
25 | 25 | ||
26 | impl ClientOpt { | 26 | impl ClientOpt { |
27 | pub const fn as_cmds(&self) -> &'static [&'static str] { | 27 | pub(crate) const fn as_cmds(&self) -> &'static [&'static str] { |
28 | match self { | 28 | match self { |
29 | ClientOpt::VsCode => &["code"], | 29 | ClientOpt::VsCode => &["code"], |
30 | ClientOpt::VsCodeExploration => &["code-exploration"], | 30 | ClientOpt::VsCodeExploration => &["code-exploration"], |
@@ -60,18 +60,18 @@ impl std::str::FromStr for ClientOpt { | |||
60 | } | 60 | } |
61 | } | 61 | } |
62 | 62 | ||
63 | pub struct ServerOpt { | 63 | pub(crate) struct ServerOpt { |
64 | pub malloc: Malloc, | 64 | pub(crate) malloc: Malloc, |
65 | } | 65 | } |
66 | 66 | ||
67 | pub enum Malloc { | 67 | pub(crate) enum Malloc { |
68 | System, | 68 | System, |
69 | Mimalloc, | 69 | Mimalloc, |
70 | Jemalloc, | 70 | Jemalloc, |
71 | } | 71 | } |
72 | 72 | ||
73 | impl InstallCmd { | 73 | impl InstallCmd { |
74 | pub fn run(self) -> Result<()> { | 74 | pub(crate) fn run(self) -> Result<()> { |
75 | if cfg!(target_os = "macos") { | 75 | if cfg!(target_os = "macos") { |
76 | fix_path_for_mac().context("Fix path for mac")? | 76 | fix_path_for_mac().context("Fix path for mac")? |
77 | } | 77 | } |
diff --git a/xtask/src/lib.rs b/xtask/src/lib.rs deleted file mode 100644 index b19985fb2..000000000 --- a/xtask/src/lib.rs +++ /dev/null | |||
@@ -1,131 +0,0 @@ | |||
1 | //! Support library for `cargo xtask` command. | ||
2 | //! | ||
3 | //! See https://github.com/matklad/cargo-xtask/ | ||
4 | |||
5 | pub mod codegen; | ||
6 | mod ast_src; | ||
7 | |||
8 | pub mod install; | ||
9 | pub mod release; | ||
10 | pub mod dist; | ||
11 | pub mod pre_commit; | ||
12 | pub mod metrics; | ||
13 | pub mod pre_cache; | ||
14 | |||
15 | use std::{ | ||
16 | env, | ||
17 | path::{Path, PathBuf}, | ||
18 | }; | ||
19 | |||
20 | use walkdir::{DirEntry, WalkDir}; | ||
21 | use xshell::{cmd, pushd, pushenv}; | ||
22 | |||
23 | use crate::codegen::Mode; | ||
24 | |||
25 | pub use anyhow::{bail, Context as _, Result}; | ||
26 | |||
27 | pub fn project_root() -> PathBuf { | ||
28 | Path::new( | ||
29 | &env::var("CARGO_MANIFEST_DIR").unwrap_or_else(|_| env!("CARGO_MANIFEST_DIR").to_owned()), | ||
30 | ) | ||
31 | .ancestors() | ||
32 | .nth(1) | ||
33 | .unwrap() | ||
34 | .to_path_buf() | ||
35 | } | ||
36 | |||
37 | pub fn rust_files() -> impl Iterator<Item = PathBuf> { | ||
38 | rust_files_in(&project_root().join("crates")) | ||
39 | } | ||
40 | |||
41 | pub fn cargo_files() -> impl Iterator<Item = PathBuf> { | ||
42 | files_in(&project_root(), "toml") | ||
43 | .filter(|path| path.file_name().map(|it| it == "Cargo.toml").unwrap_or(false)) | ||
44 | } | ||
45 | |||
46 | pub fn rust_files_in(path: &Path) -> impl Iterator<Item = PathBuf> { | ||
47 | files_in(path, "rs") | ||
48 | } | ||
49 | |||
50 | pub fn run_rustfmt(mode: Mode) -> Result<()> { | ||
51 | let _dir = pushd(project_root())?; | ||
52 | let _e = pushenv("RUSTUP_TOOLCHAIN", "stable"); | ||
53 | ensure_rustfmt()?; | ||
54 | let check = match mode { | ||
55 | Mode::Overwrite => &[][..], | ||
56 | Mode::Verify => &["--", "--check"], | ||
57 | }; | ||
58 | cmd!("cargo fmt {check...}").run()?; | ||
59 | Ok(()) | ||
60 | } | ||
61 | |||
62 | fn ensure_rustfmt() -> Result<()> { | ||
63 | let out = cmd!("rustfmt --version").read()?; | ||
64 | if !out.contains("stable") { | ||
65 | bail!( | ||
66 | "Failed to run rustfmt from toolchain 'stable'. \ | ||
67 | Please run `rustup component add rustfmt --toolchain stable` to install it.", | ||
68 | ) | ||
69 | } | ||
70 | Ok(()) | ||
71 | } | ||
72 | |||
73 | pub fn run_clippy() -> Result<()> { | ||
74 | if cmd!("cargo clippy --version").read().is_err() { | ||
75 | bail!( | ||
76 | "Failed run cargo clippy. \ | ||
77 | Please run `rustup component add clippy` to install it.", | ||
78 | ) | ||
79 | } | ||
80 | |||
81 | let allowed_lints = " | ||
82 | -A clippy::collapsible_if | ||
83 | -A clippy::needless_pass_by_value | ||
84 | -A clippy::nonminimal_bool | ||
85 | -A clippy::redundant_pattern_matching | ||
86 | " | ||
87 | .split_ascii_whitespace(); | ||
88 | cmd!("cargo clippy --all-features --all-targets -- {allowed_lints...}").run()?; | ||
89 | Ok(()) | ||
90 | } | ||
91 | |||
92 | pub fn run_fuzzer() -> Result<()> { | ||
93 | let _d = pushd("./crates/syntax")?; | ||
94 | let _e = pushenv("RUSTUP_TOOLCHAIN", "nightly"); | ||
95 | if cmd!("cargo fuzz --help").read().is_err() { | ||
96 | cmd!("cargo install cargo-fuzz").run()?; | ||
97 | }; | ||
98 | |||
99 | // Expecting nightly rustc | ||
100 | let out = cmd!("rustc --version").read()?; | ||
101 | if !out.contains("nightly") { | ||
102 | bail!("fuzz tests require nightly rustc") | ||
103 | } | ||
104 | |||
105 | cmd!("cargo fuzz run parser").run()?; | ||
106 | Ok(()) | ||
107 | } | ||
108 | |||
109 | fn date_iso() -> Result<String> { | ||
110 | let res = cmd!("date --iso --utc").read()?; | ||
111 | Ok(res) | ||
112 | } | ||
113 | |||
114 | fn is_release_tag(tag: &str) -> bool { | ||
115 | tag.len() == "2020-02-24".len() && tag.starts_with(|c: char| c.is_ascii_digit()) | ||
116 | } | ||
117 | |||
118 | fn files_in(path: &Path, ext: &'static str) -> impl Iterator<Item = PathBuf> { | ||
119 | let iter = WalkDir::new(path); | ||
120 | return iter | ||
121 | .into_iter() | ||
122 | .filter_entry(|e| !is_hidden(e)) | ||
123 | .map(|e| e.unwrap()) | ||
124 | .filter(|e| !e.file_type().is_dir()) | ||
125 | .map(|e| e.into_path()) | ||
126 | .filter(move |path| path.extension().map(|it| it == ext).unwrap_or(false)); | ||
127 | |||
128 | fn is_hidden(entry: &DirEntry) -> bool { | ||
129 | entry.file_name().to_str().map(|s| s.starts_with('.')).unwrap_or(false) | ||
130 | } | ||
131 | } | ||
diff --git a/xtask/src/main.rs b/xtask/src/main.rs index cbb9b315e..84b17ce23 100644 --- a/xtask/src/main.rs +++ b/xtask/src/main.rs | |||
@@ -7,29 +7,37 @@ | |||
7 | //! | 7 | //! |
8 | //! This binary is integrated into the `cargo` command line by using an alias in | 8 | //! This binary is integrated into the `cargo` command line by using an alias in |
9 | //! `.cargo/config`. | 9 | //! `.cargo/config`. |
10 | mod codegen; | ||
11 | mod ast_src; | ||
12 | #[cfg(test)] | ||
13 | mod tidy; | ||
10 | 14 | ||
11 | use std::env; | 15 | mod install; |
16 | mod release; | ||
17 | mod dist; | ||
18 | mod metrics; | ||
19 | mod pre_cache; | ||
12 | 20 | ||
13 | use anyhow::bail; | 21 | use anyhow::{bail, Result}; |
14 | use codegen::CodegenCmd; | 22 | use codegen::CodegenCmd; |
15 | use pico_args::Arguments; | 23 | use pico_args::Arguments; |
16 | use xshell::{cmd, cp, pushd}; | 24 | use std::{ |
17 | use xtask::{ | 25 | env, |
18 | codegen::{self, Mode}, | 26 | path::{Path, PathBuf}, |
27 | }; | ||
28 | use walkdir::{DirEntry, WalkDir}; | ||
29 | use xshell::{cmd, cp, pushd, pushenv}; | ||
30 | |||
31 | use crate::{ | ||
32 | codegen::Mode, | ||
19 | dist::DistCmd, | 33 | dist::DistCmd, |
20 | install::{InstallCmd, Malloc, ServerOpt}, | 34 | install::{InstallCmd, Malloc, ServerOpt}, |
21 | metrics::MetricsCmd, | 35 | metrics::MetricsCmd, |
22 | pre_cache::PreCacheCmd, | 36 | pre_cache::PreCacheCmd, |
23 | pre_commit, project_root, | ||
24 | release::{PromoteCmd, ReleaseCmd}, | 37 | release::{PromoteCmd, ReleaseCmd}, |
25 | run_clippy, run_fuzzer, run_rustfmt, Result, | ||
26 | }; | 38 | }; |
27 | 39 | ||
28 | fn main() -> Result<()> { | 40 | fn main() -> Result<()> { |
29 | if env::args().next().map(|it| it.contains("pre-commit")) == Some(true) { | ||
30 | return pre_commit::run_hook(); | ||
31 | } | ||
32 | |||
33 | let _d = pushd(project_root())?; | 41 | let _d = pushd(project_root())?; |
34 | 42 | ||
35 | let mut args = Arguments::from_env(); | 43 | let mut args = Arguments::from_env(); |
@@ -90,14 +98,6 @@ FLAGS: | |||
90 | finish_args(args)?; | 98 | finish_args(args)?; |
91 | CodegenCmd { features }.run() | 99 | CodegenCmd { features }.run() |
92 | } | 100 | } |
93 | "format" => { | ||
94 | finish_args(args)?; | ||
95 | run_rustfmt(Mode::Overwrite) | ||
96 | } | ||
97 | "install-pre-commit-hook" => { | ||
98 | finish_args(args)?; | ||
99 | pre_commit::install_hook() | ||
100 | } | ||
101 | "lint" => { | 101 | "lint" => { |
102 | finish_args(args)?; | 102 | finish_args(args)?; |
103 | run_clippy() | 103 | run_clippy() |
@@ -151,8 +151,6 @@ USAGE: | |||
151 | cargo xtask <SUBCOMMAND> | 151 | cargo xtask <SUBCOMMAND> |
152 | 152 | ||
153 | SUBCOMMANDS: | 153 | SUBCOMMANDS: |
154 | format | ||
155 | install-pre-commit-hook | ||
156 | fuzz-tests | 154 | fuzz-tests |
157 | codegen | 155 | codegen |
158 | install | 156 | install |
@@ -172,3 +170,110 @@ fn finish_args(args: Arguments) -> Result<()> { | |||
172 | } | 170 | } |
173 | Ok(()) | 171 | Ok(()) |
174 | } | 172 | } |
173 | |||
174 | fn project_root() -> PathBuf { | ||
175 | Path::new( | ||
176 | &env::var("CARGO_MANIFEST_DIR").unwrap_or_else(|_| env!("CARGO_MANIFEST_DIR").to_owned()), | ||
177 | ) | ||
178 | .ancestors() | ||
179 | .nth(1) | ||
180 | .unwrap() | ||
181 | .to_path_buf() | ||
182 | } | ||
183 | |||
184 | fn rust_files() -> impl Iterator<Item = PathBuf> { | ||
185 | rust_files_in(&project_root().join("crates")) | ||
186 | } | ||
187 | |||
188 | #[cfg(test)] | ||
189 | fn cargo_files() -> impl Iterator<Item = PathBuf> { | ||
190 | files_in(&project_root(), "toml") | ||
191 | .filter(|path| path.file_name().map(|it| it == "Cargo.toml").unwrap_or(false)) | ||
192 | } | ||
193 | |||
194 | fn rust_files_in(path: &Path) -> impl Iterator<Item = PathBuf> { | ||
195 | files_in(path, "rs") | ||
196 | } | ||
197 | |||
198 | fn run_rustfmt(mode: Mode) -> Result<()> { | ||
199 | let _dir = pushd(project_root())?; | ||
200 | let _e = pushenv("RUSTUP_TOOLCHAIN", "stable"); | ||
201 | ensure_rustfmt()?; | ||
202 | let check = match mode { | ||
203 | Mode::Overwrite => &[][..], | ||
204 | Mode::Verify => &["--", "--check"], | ||
205 | }; | ||
206 | cmd!("cargo fmt {check...}").run()?; | ||
207 | Ok(()) | ||
208 | } | ||
209 | |||
210 | fn ensure_rustfmt() -> Result<()> { | ||
211 | let out = cmd!("rustfmt --version").read()?; | ||
212 | if !out.contains("stable") { | ||
213 | bail!( | ||
214 | "Failed to run rustfmt from toolchain 'stable'. \ | ||
215 | Please run `rustup component add rustfmt --toolchain stable` to install it.", | ||
216 | ) | ||
217 | } | ||
218 | Ok(()) | ||
219 | } | ||
220 | |||
221 | fn run_clippy() -> Result<()> { | ||
222 | if cmd!("cargo clippy --version").read().is_err() { | ||
223 | bail!( | ||
224 | "Failed run cargo clippy. \ | ||
225 | Please run `rustup component add clippy` to install it.", | ||
226 | ) | ||
227 | } | ||
228 | |||
229 | let allowed_lints = " | ||
230 | -A clippy::collapsible_if | ||
231 | -A clippy::needless_pass_by_value | ||
232 | -A clippy::nonminimal_bool | ||
233 | -A clippy::redundant_pattern_matching | ||
234 | " | ||
235 | .split_ascii_whitespace(); | ||
236 | cmd!("cargo clippy --all-features --all-targets -- {allowed_lints...}").run()?; | ||
237 | Ok(()) | ||
238 | } | ||
239 | |||
240 | fn run_fuzzer() -> Result<()> { | ||
241 | let _d = pushd("./crates/syntax")?; | ||
242 | let _e = pushenv("RUSTUP_TOOLCHAIN", "nightly"); | ||
243 | if cmd!("cargo fuzz --help").read().is_err() { | ||
244 | cmd!("cargo install cargo-fuzz").run()?; | ||
245 | }; | ||
246 | |||
247 | // Expecting nightly rustc | ||
248 | let out = cmd!("rustc --version").read()?; | ||
249 | if !out.contains("nightly") { | ||
250 | bail!("fuzz tests require nightly rustc") | ||
251 | } | ||
252 | |||
253 | cmd!("cargo fuzz run parser").run()?; | ||
254 | Ok(()) | ||
255 | } | ||
256 | |||
257 | fn date_iso() -> Result<String> { | ||
258 | let res = cmd!("date --iso --utc").read()?; | ||
259 | Ok(res) | ||
260 | } | ||
261 | |||
262 | fn is_release_tag(tag: &str) -> bool { | ||
263 | tag.len() == "2020-02-24".len() && tag.starts_with(|c: char| c.is_ascii_digit()) | ||
264 | } | ||
265 | |||
266 | fn files_in(path: &Path, ext: &'static str) -> impl Iterator<Item = PathBuf> { | ||
267 | let iter = WalkDir::new(path); | ||
268 | return iter | ||
269 | .into_iter() | ||
270 | .filter_entry(|e| !is_hidden(e)) | ||
271 | .map(|e| e.unwrap()) | ||
272 | .filter(|e| !e.file_type().is_dir()) | ||
273 | .map(|e| e.into_path()) | ||
274 | .filter(move |path| path.extension().map(|it| it == ext).unwrap_or(false)); | ||
275 | |||
276 | fn is_hidden(entry: &DirEntry) -> bool { | ||
277 | entry.file_name().to_str().map(|s| s.starts_with('.')).unwrap_or(false) | ||
278 | } | ||
279 | } | ||
diff --git a/xtask/src/metrics.rs b/xtask/src/metrics.rs index 624ad3b7e..babc2a6d4 100644 --- a/xtask/src/metrics.rs +++ b/xtask/src/metrics.rs | |||
@@ -11,12 +11,12 @@ use xshell::{cmd, mkdir_p, pushd, pushenv, read_file, rm_rf}; | |||
11 | 11 | ||
12 | type Unit = String; | 12 | type Unit = String; |
13 | 13 | ||
14 | pub struct MetricsCmd { | 14 | pub(crate) struct MetricsCmd { |
15 | pub dry_run: bool, | 15 | pub(crate) dry_run: bool, |
16 | } | 16 | } |
17 | 17 | ||
18 | impl MetricsCmd { | 18 | impl MetricsCmd { |
19 | pub fn run(self) -> Result<()> { | 19 | pub(crate) fn run(self) -> Result<()> { |
20 | let mut metrics = Metrics::new()?; | 20 | let mut metrics = Metrics::new()?; |
21 | if !self.dry_run { | 21 | if !self.dry_run { |
22 | rm_rf("./target/release")?; | 22 | rm_rf("./target/release")?; |
diff --git a/xtask/src/pre_cache.rs b/xtask/src/pre_cache.rs index 569f88f68..54f4a95a9 100644 --- a/xtask/src/pre_cache.rs +++ b/xtask/src/pre_cache.rs | |||
@@ -6,12 +6,12 @@ use std::{ | |||
6 | use anyhow::Result; | 6 | use anyhow::Result; |
7 | use xshell::rm_rf; | 7 | use xshell::rm_rf; |
8 | 8 | ||
9 | pub struct PreCacheCmd; | 9 | pub(crate) struct PreCacheCmd; |
10 | 10 | ||
11 | impl PreCacheCmd { | 11 | impl PreCacheCmd { |
12 | /// Cleans the `./target` dir after the build such that only | 12 | /// Cleans the `./target` dir after the build such that only |
13 | /// dependencies are cached on CI. | 13 | /// dependencies are cached on CI. |
14 | pub fn run(self) -> Result<()> { | 14 | pub(crate) fn run(self) -> Result<()> { |
15 | let slow_tests_cookie = Path::new("./target/.slow_tests_cookie"); | 15 | let slow_tests_cookie = Path::new("./target/.slow_tests_cookie"); |
16 | if !slow_tests_cookie.exists() { | 16 | if !slow_tests_cookie.exists() { |
17 | panic!("slow tests were skipped on CI!") | 17 | panic!("slow tests were skipped on CI!") |
diff --git a/xtask/src/pre_commit.rs b/xtask/src/pre_commit.rs deleted file mode 100644 index 8f2dbea19..000000000 --- a/xtask/src/pre_commit.rs +++ /dev/null | |||
@@ -1,38 +0,0 @@ | |||
1 | //! pre-commit hook for code formatting. | ||
2 | |||
3 | use std::{fs, path::PathBuf}; | ||
4 | |||
5 | use anyhow::{bail, Result}; | ||
6 | use xshell::cmd; | ||
7 | |||
8 | use crate::{project_root, run_rustfmt, Mode}; | ||
9 | |||
10 | // FIXME: if there are changed `.ts` files, also reformat TypeScript (by | ||
11 | // shelling out to `npm fmt`). | ||
12 | pub fn run_hook() -> Result<()> { | ||
13 | run_rustfmt(Mode::Overwrite)?; | ||
14 | |||
15 | let diff = cmd!("git diff --diff-filter=MAR --name-only --cached").read()?; | ||
16 | |||
17 | let root = project_root(); | ||
18 | for line in diff.lines() { | ||
19 | let file = root.join(line); | ||
20 | cmd!("git update-index --add {file}").run()?; | ||
21 | } | ||
22 | |||
23 | Ok(()) | ||
24 | } | ||
25 | |||
26 | pub fn install_hook() -> Result<()> { | ||
27 | let hook_path: PathBuf = | ||
28 | format!("./.git/hooks/pre-commit{}", std::env::consts::EXE_SUFFIX).into(); | ||
29 | |||
30 | if hook_path.exists() { | ||
31 | bail!("Git hook already created"); | ||
32 | } | ||
33 | |||
34 | let me = std::env::current_exe()?; | ||
35 | fs::copy(me, hook_path)?; | ||
36 | |||
37 | Ok(()) | ||
38 | } | ||
diff --git a/xtask/src/release.rs b/xtask/src/release.rs index 63556476d..5008881e4 100644 --- a/xtask/src/release.rs +++ b/xtask/src/release.rs | |||
@@ -4,12 +4,12 @@ use xshell::{cmd, cp, pushd, read_dir, write_file}; | |||
4 | 4 | ||
5 | use crate::{codegen, date_iso, is_release_tag, project_root, Mode, Result}; | 5 | use crate::{codegen, date_iso, is_release_tag, project_root, Mode, Result}; |
6 | 6 | ||
7 | pub struct ReleaseCmd { | 7 | pub(crate) struct ReleaseCmd { |
8 | pub dry_run: bool, | 8 | pub(crate) dry_run: bool, |
9 | } | 9 | } |
10 | 10 | ||
11 | impl ReleaseCmd { | 11 | impl ReleaseCmd { |
12 | pub fn run(self) -> Result<()> { | 12 | pub(crate) fn run(self) -> Result<()> { |
13 | if !self.dry_run { | 13 | if !self.dry_run { |
14 | cmd!("git switch release").run()?; | 14 | cmd!("git switch release").run()?; |
15 | cmd!("git fetch upstream --tags --force").run()?; | 15 | cmd!("git fetch upstream --tags --force").run()?; |
@@ -86,12 +86,12 @@ https://github.com/sponsors/rust-analyzer[GitHub Sponsors]. | |||
86 | } | 86 | } |
87 | } | 87 | } |
88 | 88 | ||
89 | pub struct PromoteCmd { | 89 | pub(crate) struct PromoteCmd { |
90 | pub dry_run: bool, | 90 | pub(crate) dry_run: bool, |
91 | } | 91 | } |
92 | 92 | ||
93 | impl PromoteCmd { | 93 | impl PromoteCmd { |
94 | pub fn run(self) -> Result<()> { | 94 | pub(crate) fn run(self) -> Result<()> { |
95 | let _dir = pushd("../rust-rust-analyzer")?; | 95 | let _dir = pushd("../rust-rust-analyzer")?; |
96 | cmd!("git switch master").run()?; | 96 | cmd!("git switch master").run()?; |
97 | cmd!("git fetch upstream").run()?; | 97 | cmd!("git fetch upstream").run()?; |
diff --git a/xtask/tests/tidy.rs b/xtask/src/tidy.rs index a72498a38..63116ec6b 100644 --- a/xtask/tests/tidy.rs +++ b/xtask/src/tidy.rs | |||
@@ -4,7 +4,8 @@ use std::{ | |||
4 | }; | 4 | }; |
5 | 5 | ||
6 | use xshell::{cmd, read_file}; | 6 | use xshell::{cmd, read_file}; |
7 | use xtask::{ | 7 | |
8 | use crate::{ | ||
8 | cargo_files, | 9 | cargo_files, |
9 | codegen::{self, Mode}, | 10 | codegen::{self, Mode}, |
10 | project_root, run_rustfmt, rust_files, | 11 | project_root, run_rustfmt, rust_files, |