aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Cargo.lock85
-rw-r--r--crates/hir/src/code_model.rs21
-rw-r--r--crates/hir_def/src/find_path.rs59
-rw-r--r--crates/hir_def/src/item_scope.rs22
-rw-r--r--crates/hir_def/src/lib.rs23
-rw-r--r--crates/hir_def/src/nameres.rs12
-rw-r--r--crates/hir_def/src/path/lower.rs13
-rw-r--r--crates/hir_ty/Cargo.toml6
-rw-r--r--crates/hir_ty/src/autoderef.rs5
-rw-r--r--crates/hir_ty/src/diagnostics/unsafe_check.rs2
-rw-r--r--crates/hir_ty/src/display.rs38
-rw-r--r--crates/hir_ty/src/infer.rs9
-rw-r--r--crates/hir_ty/src/infer/coerce.rs18
-rw-r--r--crates/hir_ty/src/infer/expr.rs21
-rw-r--r--crates/hir_ty/src/infer/pat.rs11
-rw-r--r--crates/hir_ty/src/infer/unify.rs6
-rw-r--r--crates/hir_ty/src/lib.rs86
-rw-r--r--crates/hir_ty/src/lower.rs45
-rw-r--r--crates/hir_ty/src/method_resolution.rs15
-rw-r--r--crates/hir_ty/src/traits.rs2
-rw-r--r--crates/hir_ty/src/traits/chalk.rs10
-rw-r--r--crates/hir_ty/src/traits/chalk/mapping.rs46
-rw-r--r--crates/ide/src/hover.rs29
-rw-r--r--crates/mbe/Cargo.toml7
-rw-r--r--docs/dev/README.md2
-rw-r--r--xtask/Cargo.toml5
-rw-r--r--xtask/src/codegen.rs14
-rw-r--r--xtask/src/codegen/gen_assists_docs.rs4
-rw-r--r--xtask/src/codegen/gen_diagnostic_docs.rs2
-rw-r--r--xtask/src/codegen/gen_feature_docs.rs2
-rw-r--r--xtask/src/codegen/gen_lint_completions.rs2
-rw-r--r--xtask/src/codegen/gen_parser_tests.rs2
-rw-r--r--xtask/src/codegen/gen_syntax.rs2
-rw-r--r--xtask/src/dist.rs8
-rw-r--r--xtask/src/flags.rs139
-rw-r--r--xtask/src/install.rs18
-rw-r--r--xtask/src/lib.rs131
-rw-r--r--xtask/src/main.rs273
-rw-r--r--xtask/src/metrics.rs10
-rw-r--r--xtask/src/pre_cache.rs6
-rw-r--r--xtask/src/pre_commit.rs38
-rw-r--r--xtask/src/release.rs18
-rw-r--r--xtask/src/tidy.rs (renamed from xtask/tests/tidy.rs)3
43 files changed, 687 insertions, 583 deletions
diff --git a/Cargo.lock b/Cargo.lock
index dbd7a746e..b6f75aad0 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -13,9 +13,9 @@ dependencies = [
13 13
14[[package]] 14[[package]]
15name = "adler" 15name = "adler"
16version = "1.0.1" 16version = "1.0.2"
17source = "registry+https://github.com/rust-lang/crates.io-index" 17source = "registry+https://github.com/rust-lang/crates.io-index"
18checksum = "bedc89c5c7b5550ffb9372eb5c5ffc7f9f705cc3f4a128bd4669b9745f555093" 18checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe"
19 19
20[[package]] 20[[package]]
21name = "always-assert" 21name = "always-assert"
@@ -158,9 +158,9 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
158 158
159[[package]] 159[[package]]
160name = "chalk-derive" 160name = "chalk-derive"
161version = "0.58.0" 161version = "0.59.0"
162source = "registry+https://github.com/rust-lang/crates.io-index" 162source = "registry+https://github.com/rust-lang/crates.io-index"
163checksum = "e625b7c688272783140509a0de8f7aa9000217cb0982c9b10606a12b0b747ba8" 163checksum = "4b9000fbcb67353dc8973ab9fd136277d321d85b79bd36b8756bb3ae0979a94a"
164dependencies = [ 164dependencies = [
165 "proc-macro2", 165 "proc-macro2",
166 "quote", 166 "quote",
@@ -170,9 +170,9 @@ dependencies = [
170 170
171[[package]] 171[[package]]
172name = "chalk-ir" 172name = "chalk-ir"
173version = "0.58.0" 173version = "0.59.0"
174source = "registry+https://github.com/rust-lang/crates.io-index" 174source = "registry+https://github.com/rust-lang/crates.io-index"
175checksum = "c220d870128959d7d56667060d556ffdebd490f32ee0fc9f4060a76c1193f206" 175checksum = "b23528d61b3557c676eccf508fa0771a38453b379f0b780154eaa7f70afe8dfc"
176dependencies = [ 176dependencies = [
177 "bitflags", 177 "bitflags",
178 "chalk-derive", 178 "chalk-derive",
@@ -181,9 +181,9 @@ dependencies = [
181 181
182[[package]] 182[[package]]
183name = "chalk-recursive" 183name = "chalk-recursive"
184version = "0.58.0" 184version = "0.59.0"
185source = "registry+https://github.com/rust-lang/crates.io-index" 185source = "registry+https://github.com/rust-lang/crates.io-index"
186checksum = "7d8cd81a15aa936215378e695a8907b9f1af8626a27a32ee22e97a50984960da" 186checksum = "a8bdd37afc666b771de8b4429fe014363d0e74aae5cc26f320f60a3eab34d744"
187dependencies = [ 187dependencies = [
188 "chalk-derive", 188 "chalk-derive",
189 "chalk-ir", 189 "chalk-ir",
@@ -194,14 +194,14 @@ dependencies = [
194 194
195[[package]] 195[[package]]
196name = "chalk-solve" 196name = "chalk-solve"
197version = "0.58.0" 197version = "0.59.0"
198source = "registry+https://github.com/rust-lang/crates.io-index" 198source = "registry+https://github.com/rust-lang/crates.io-index"
199checksum = "55571250dfe096a4c899be88c81418284c952ce1c8a06aa16afb5781b298e9c9" 199checksum = "4182c42ca319cb71c89898ebc3d2671d1fa7d928123b171b66f1797a2000b9c8"
200dependencies = [ 200dependencies = [
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]]
718name = "itertools" 718name = "itertools"
719version = "0.9.0"
720source = "registry+https://github.com/rust-lang/crates.io-index"
721checksum = "284f18f85651fe11e8a991b2adb42cb078325c996ed026d994719efcfca1d54b"
722dependencies = [
723 "either",
724]
725
726[[package]]
727name = "itertools"
728version = "0.10.0" 719version = "0.10.0"
729source = "registry+https://github.com/rust-lang/crates.io-index" 720source = "registry+https://github.com/rust-lang/crates.io-index"
730checksum = "37d572918e350e82412fe766d24b15e6682fb2ed2bbe018280caa810397cb319" 721checksum = "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]]
1492name = "serde_json" 1483name = "serde_json"
1493version = "1.0.62" 1484version = "1.0.64"
1494source = "registry+https://github.com/rust-lang/crates.io-index" 1485source = "registry+https://github.com/rust-lang/crates.io-index"
1495checksum = "ea1c6153794552ea7cf7cf63b1231a25de00ec90db326ba6264440fa08e31486" 1486checksum = "799e97dc9fdae36a5c8b8f2cae9ce2ee9fdce2058c57a93e6099d919fd982f79"
1496dependencies = [ 1487dependencies = [
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",
@@ -1922,19 +1913,37 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
1922checksum = "06069a848f95fceae3e5e03c0ddc8cb78452b56654ee0c8e68f938cf790fb9e3" 1913checksum = "06069a848f95fceae3e5e03c0ddc8cb78452b56654ee0c8e68f938cf790fb9e3"
1923 1914
1924[[package]] 1915[[package]]
1916name = "xflags"
1917version = "0.1.2"
1918source = "registry+https://github.com/rust-lang/crates.io-index"
1919checksum = "1a6292b9528efc06cb25a41b8a0814dd3a9590c0fe2cd95341fe41bbe034fafb"
1920dependencies = [
1921 "xflags-macros",
1922]
1923
1924[[package]]
1925name = "xflags-macros"
1926version = "0.1.2"
1927source = "registry+https://github.com/rust-lang/crates.io-index"
1928checksum = "ba2108d40e49a0653f2ee4eda59f51447e0cab5cc2cc197a5abd96525c6bd89e"
1929dependencies = [
1930 "proc-macro2",
1931]
1932
1933[[package]]
1925name = "xshell" 1934name = "xshell"
1926version = "0.1.8" 1935version = "0.1.9"
1927source = "registry+https://github.com/rust-lang/crates.io-index" 1936source = "registry+https://github.com/rust-lang/crates.io-index"
1928checksum = "ed373ede30cea03e8c0af22f48ee1ba80efbf06fec8b4746977e6ee703878de0" 1937checksum = "6f18102278453c8f70ea5c514ac78cb4c73a0ef72a8273d17094b52f9584c0c1"
1929dependencies = [ 1938dependencies = [
1930 "xshell-macros", 1939 "xshell-macros",
1931] 1940]
1932 1941
1933[[package]] 1942[[package]]
1934name = "xshell-macros" 1943name = "xshell-macros"
1935version = "0.1.8" 1944version = "0.1.9"
1936source = "registry+https://github.com/rust-lang/crates.io-index" 1945source = "registry+https://github.com/rust-lang/crates.io-index"
1937checksum = "7f6af9f8119104697b0105989a73c578ce33f922d9d6f3dae0e8ae3d538db321" 1946checksum = "6093c460064572007f885facc70bb0ca5e40a83ea7ff8b16c1abbee56fd2e767"
1938 1947
1939[[package]] 1948[[package]]
1940name = "xtask" 1949name = "xtask"
@@ -1942,11 +1951,11 @@ version = "0.1.0"
1942dependencies = [ 1951dependencies = [
1943 "anyhow", 1952 "anyhow",
1944 "flate2", 1953 "flate2",
1945 "pico-args",
1946 "proc-macro2", 1954 "proc-macro2",
1947 "quote", 1955 "quote",
1948 "ungrammar", 1956 "ungrammar",
1949 "walkdir", 1957 "walkdir",
1950 "write-json", 1958 "write-json",
1959 "xflags",
1951 "xshell", 1960 "xshell",
1952] 1961]
diff --git a/crates/hir/src/code_model.rs b/crates/hir/src/code_model.rs
index 351ba75ff..fc1a74641 100644
--- a/crates/hir/src/code_model.rs
+++ b/crates/hir/src/code_model.rs
@@ -14,7 +14,7 @@ use hir_def::{
14 per_ns::PerNs, 14 per_ns::PerNs,
15 resolver::{HasResolver, Resolver}, 15 resolver::{HasResolver, Resolver},
16 src::HasSource as _, 16 src::HasSource as _,
17 type_ref::{Mutability, TypeRef}, 17 type_ref::TypeRef,
18 AdtId, AssocContainerId, AssocItemId, AssocItemLoc, AttrDefId, ConstId, ConstParamId, 18 AdtId, AssocContainerId, AssocItemId, AssocItemLoc, AttrDefId, ConstId, ConstParamId,
19 DefWithBodyId, EnumId, FunctionId, GenericDefId, HasModule, ImplId, LifetimeParamId, 19 DefWithBodyId, EnumId, FunctionId, GenericDefId, HasModule, ImplId, LifetimeParamId,
20 LocalEnumVariantId, LocalFieldId, Lookup, ModuleId, StaticId, StructId, TraitId, TypeAliasId, 20 LocalEnumVariantId, LocalFieldId, Lookup, ModuleId, StaticId, StructId, TraitId, TypeAliasId,
@@ -31,9 +31,9 @@ use hir_ty::{
31 display::{write_bounds_like_dyn_trait_with_prefix, HirDisplayError, HirFormatter}, 31 display::{write_bounds_like_dyn_trait_with_prefix, HirDisplayError, HirFormatter},
32 method_resolution, 32 method_resolution,
33 traits::{FnTrait, Solution, SolutionVariables}, 33 traits::{FnTrait, Solution, SolutionVariables},
34 BoundVar, CallableDefId, CallableSig, Canonical, DebruijnIndex, GenericPredicate, 34 AliasTy, BoundVar, CallableDefId, CallableSig, Canonical, DebruijnIndex, GenericPredicate,
35 InEnvironment, Obligation, ProjectionPredicate, ProjectionTy, Scalar, Substs, TraitEnvironment, 35 InEnvironment, Mutability, Obligation, ProjectionPredicate, ProjectionTy, Scalar, Substs,
36 Ty, TyDefId, TyVariableKind, 36 TraitEnvironment, Ty, TyDefId, TyVariableKind,
37}; 37};
38use rustc_hash::FxHashSet; 38use rustc_hash::FxHashSet;
39use stdx::{format_to, impl_from}; 39use stdx::{format_to, impl_from};
@@ -836,7 +836,7 @@ pub enum Access {
836impl From<Mutability> for Access { 836impl From<Mutability> for Access {
837 fn from(mutability: Mutability) -> Access { 837 fn from(mutability: Mutability) -> Access {
838 match mutability { 838 match mutability {
839 Mutability::Shared => Access::Shared, 839 Mutability::Not => Access::Shared,
840 Mutability::Mut => Access::Exclusive, 840 Mutability::Mut => Access::Exclusive,
841 } 841 }
842 } 842 }
@@ -865,7 +865,10 @@ impl SelfParam {
865 .params 865 .params
866 .first() 866 .first()
867 .map(|param| match *param { 867 .map(|param| match *param {
868 TypeRef::Reference(.., mutability) => mutability.into(), 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 },
869 _ => Access::Owned, 872 _ => Access::Owned,
870 }) 873 })
871 .unwrap_or(Access::Owned) 874 .unwrap_or(Access::Owned)
@@ -1648,7 +1651,7 @@ impl Type {
1648 .build(); 1651 .build();
1649 let predicate = ProjectionPredicate { 1652 let predicate = ProjectionPredicate {
1650 projection_ty: ProjectionTy { associated_ty: alias.id, parameters: subst }, 1653 projection_ty: ProjectionTy { associated_ty: alias.id, parameters: subst },
1651 ty: Ty::Bound(BoundVar::new(DebruijnIndex::INNERMOST, 0)), 1654 ty: Ty::BoundVar(BoundVar::new(DebruijnIndex::INNERMOST, 0)),
1652 }; 1655 };
1653 let goal = Canonical { 1656 let goal = Canonical {
1654 value: InEnvironment::new( 1657 value: InEnvironment::new(
@@ -1709,7 +1712,7 @@ impl Type {
1709 } 1712 }
1710 1713
1711 pub fn is_raw_ptr(&self) -> bool { 1714 pub fn is_raw_ptr(&self) -> bool {
1712 matches!(&self.ty.value, Ty::RawPtr(..)) 1715 matches!(&self.ty.value, Ty::Raw(..))
1713 } 1716 }
1714 1717
1715 pub fn contains_unknown(&self) -> bool { 1718 pub fn contains_unknown(&self) -> bool {
@@ -1937,7 +1940,7 @@ impl Type {
1937 walk_bounds(db, &type_.derived(ty.clone()), &bounds, cb); 1940 walk_bounds(db, &type_.derived(ty.clone()), &bounds, cb);
1938 } 1941 }
1939 } 1942 }
1940 Ty::Opaque(opaque_ty) => { 1943 Ty::Alias(AliasTy::Opaque(opaque_ty)) => {
1941 if let Some(bounds) = ty.impl_trait_bounds(db) { 1944 if let Some(bounds) = ty.impl_trait_bounds(db) {
1942 walk_bounds(db, &type_.derived(ty.clone()), &bounds, cb); 1945 walk_bounds(db, &type_.derived(ty.clone()), &bounds, cb);
1943 } 1946 }
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
3use std::iter;
4
3use hir_expand::name::{known, AsName, Name}; 5use hir_expand::name::{known, AsName, Name};
4use rustc_hash::FxHashSet; 6use rustc_hash::FxHashSet;
5use test_utils::mark; 7use 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;
12use test_utils::mark; 12use test_utils::mark;
13 13
14use crate::{ 14use 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
536impl 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
532impl AttrDefId { 555impl 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"
17log = "0.4.8" 17log = "0.4.8"
18rustc-hash = "1.1.0" 18rustc-hash = "1.1.0"
19scoped-tls = "1" 19scoped-tls = "1"
20chalk-solve = { version = "0.58", default-features = false } 20chalk-solve = { version = "0.59", default-features = false }
21chalk-ir = "0.58" 21chalk-ir = "0.59"
22chalk-recursive = "0.58" 22chalk-recursive = "0.59"
23la-arena = { version = "0.2.0", path = "../../lib/arena" } 23la-arena = { version = "0.2.0", path = "../../lib/arena" }
24 24
25stdx = { path = "../stdx", version = "0.0.0" } 25stdx = { path = "../stdx", version = "0.0.0" }
diff --git a/crates/hir_ty/src/autoderef.rs b/crates/hir_ty/src/autoderef.rs
index 21d1e5446..be1fd1f13 100644
--- a/crates/hir_ty/src/autoderef.rs
+++ b/crates/hir_ty/src/autoderef.rs
@@ -81,7 +81,7 @@ fn deref_by_trait(
81 81
82 // Now do the assoc type projection 82 // Now do the assoc type projection
83 let projection = super::traits::ProjectionPredicate { 83 let projection = super::traits::ProjectionPredicate {
84 ty: Ty::Bound(BoundVar::new(DebruijnIndex::INNERMOST, ty.value.kinds.len())), 84 ty: Ty::BoundVar(BoundVar::new(DebruijnIndex::INNERMOST, ty.value.kinds.len())),
85 projection_ty: super::ProjectionTy { associated_ty: target, parameters }, 85 projection_ty: super::ProjectionTy { associated_ty: target, parameters },
86 }; 86 };
87 87
@@ -114,7 +114,8 @@ fn deref_by_trait(
114 // new variables in that case 114 // new variables in that case
115 115
116 for i in 1..vars.0.kinds.len() { 116 for i in 1..vars.0.kinds.len() {
117 if vars.0.value[i - 1] != Ty::Bound(BoundVar::new(DebruijnIndex::INNERMOST, i - 1)) 117 if vars.0.value[i - 1]
118 != Ty::BoundVar(BoundVar::new(DebruijnIndex::INNERMOST, i - 1))
118 { 119 {
119 warn!("complex solution for derefing {:?}: {:?}, ignoring", ty.value, solution); 120 warn!("complex solution for derefing {:?}: {:?}, ignoring", ty.value, solution);
120 return None; 121 return None;
diff --git a/crates/hir_ty/src/diagnostics/unsafe_check.rs b/crates/hir_ty/src/diagnostics/unsafe_check.rs
index b439915c7..e77a20fea 100644
--- a/crates/hir_ty/src/diagnostics/unsafe_check.rs
+++ b/crates/hir_ty/src/diagnostics/unsafe_check.rs
@@ -110,7 +110,7 @@ fn walk_unsafe(
110 } 110 }
111 } 111 }
112 Expr::UnaryOp { expr, op: UnaryOp::Deref } => { 112 Expr::UnaryOp { expr, op: UnaryOp::Deref } => {
113 if let Ty::RawPtr(..) = &infer[*expr] { 113 if let Ty::Raw(..) = &infer[*expr] {
114 unsafe_exprs.push(UnsafeExpr { expr: current, inside_unsafe_block }); 114 unsafe_exprs.push(UnsafeExpr { expr: current, inside_unsafe_block });
115 } 115 }
116 } 116 }
diff --git a/crates/hir_ty/src/display.rs b/crates/hir_ty/src/display.rs
index ff8211094..d4a8b48e6 100644
--- a/crates/hir_ty/src/display.rs
+++ b/crates/hir_ty/src/display.rs
@@ -3,10 +3,12 @@
3use std::{borrow::Cow, fmt}; 3use std::{borrow::Cow, fmt};
4 4
5use crate::{ 5use crate::{
6 db::HirDatabase, primitive, utils::generics, CallableDefId, CallableSig, GenericPredicate, 6 db::HirDatabase, primitive, utils::generics, AliasTy, CallableDefId, CallableSig,
7 Lifetime, Obligation, OpaqueTy, OpaqueTyId, ProjectionTy, Scalar, Substs, TraitRef, Ty, 7 GenericPredicate, Lifetime, Obligation, OpaqueTy, OpaqueTyId, ProjectionTy, Scalar, Substs,
8 TraitRef, Ty,
8}; 9};
9use arrayvec::ArrayVec; 10use arrayvec::ArrayVec;
11use chalk_ir::Mutability;
10use hir_def::{ 12use hir_def::{
11 db::DefDatabase, find_path, generics::TypeParamProvenance, item_scope::ItemInNs, AdtId, 13 db::DefDatabase, find_path, generics::TypeParamProvenance, item_scope::ItemInNs, AdtId,
12 AssocContainerId, HasModule, Lookup, ModuleId, TraitId, 14 AssocContainerId, HasModule, Lookup, ModuleId, TraitId,
@@ -284,15 +286,29 @@ impl HirDisplay for Ty {
284 t.hir_fmt(f)?; 286 t.hir_fmt(f)?;
285 write!(f, "; _]")?; 287 write!(f, "; _]")?;
286 } 288 }
287 Ty::RawPtr(m, parameters) | Ty::Ref(m, parameters) => { 289 Ty::Raw(m, parameters) | Ty::Ref(m, parameters) => {
288 let t = parameters.as_single(); 290 let t = parameters.as_single();
289 let ty_display = 291 let ty_display =
290 t.into_displayable(f.db, f.max_size, f.omit_verbose_types, f.display_target); 292 t.into_displayable(f.db, f.max_size, f.omit_verbose_types, f.display_target);
291 293
292 if matches!(self, Ty::RawPtr(..)) { 294 if matches!(self, Ty::Raw(..)) {
293 write!(f, "*{}", m.as_keyword_for_ptr())?; 295 write!(
296 f,
297 "*{}",
298 match m {
299 Mutability::Not => "const ",
300 Mutability::Mut => "mut ",
301 }
302 )?;
294 } else { 303 } else {
295 write!(f, "&{}", m.as_keyword_for_ref())?; 304 write!(
305 f,
306 "&{}",
307 match m {
308 Mutability::Not => "",
309 Mutability::Mut => "mut ",
310 }
311 )?;
296 } 312 }
297 313
298 let datas; 314 let datas;
@@ -300,10 +316,10 @@ impl HirDisplay for Ty {
300 Ty::Dyn(predicates) if predicates.len() > 1 => { 316 Ty::Dyn(predicates) if predicates.len() > 1 => {
301 Cow::Borrowed(predicates.as_ref()) 317 Cow::Borrowed(predicates.as_ref())
302 } 318 }
303 &Ty::Opaque(OpaqueTy { 319 &Ty::Alias(AliasTy::Opaque(OpaqueTy {
304 opaque_ty_id: OpaqueTyId::ReturnTypeImplTrait(func, idx), 320 opaque_ty_id: OpaqueTyId::ReturnTypeImplTrait(func, idx),
305 ref parameters, 321 ref parameters,
306 }) => { 322 })) => {
307 datas = 323 datas =
308 f.db.return_type_impl_traits(func).expect("impl trait id without data"); 324 f.db.return_type_impl_traits(func).expect("impl trait id without data");
309 let data = (*datas) 325 let data = (*datas)
@@ -518,7 +534,6 @@ impl HirDisplay for Ty {
518 write!(f, "{{closure}}")?; 534 write!(f, "{{closure}}")?;
519 } 535 }
520 } 536 }
521 Ty::Projection(p_ty) => p_ty.hir_fmt(f)?,
522 Ty::Placeholder(id) => { 537 Ty::Placeholder(id) => {
523 let generics = generics(f.db.upcast(), id.parent); 538 let generics = generics(f.db.upcast(), id.parent);
524 let param_data = &generics.params.types[id.local_id]; 539 let param_data = &generics.params.types[id.local_id];
@@ -537,11 +552,12 @@ impl HirDisplay for Ty {
537 } 552 }
538 } 553 }
539 } 554 }
540 Ty::Bound(idx) => write!(f, "?{}.{}", idx.debruijn.depth(), idx.index)?, 555 Ty::BoundVar(idx) => write!(f, "?{}.{}", idx.debruijn.depth(), idx.index)?,
541 Ty::Dyn(predicates) => { 556 Ty::Dyn(predicates) => {
542 write_bounds_like_dyn_trait_with_prefix("dyn", predicates, f)?; 557 write_bounds_like_dyn_trait_with_prefix("dyn", predicates, f)?;
543 } 558 }
544 Ty::Opaque(opaque_ty) => { 559 Ty::Alias(AliasTy::Projection(p_ty)) => p_ty.hir_fmt(f)?,
560 Ty::Alias(AliasTy::Opaque(opaque_ty)) => {
545 match opaque_ty.opaque_ty_id { 561 match opaque_ty.opaque_ty_id {
546 OpaqueTyId::ReturnTypeImplTrait(func, idx) => { 562 OpaqueTyId::ReturnTypeImplTrait(func, idx) => {
547 let datas = 563 let datas =
diff --git a/crates/hir_ty/src/infer.rs b/crates/hir_ty/src/infer.rs
index 1d78d1feb..4d771a91e 100644
--- a/crates/hir_ty/src/infer.rs
+++ b/crates/hir_ty/src/infer.rs
@@ -18,6 +18,7 @@ use std::mem;
18use std::ops::Index; 18use std::ops::Index;
19use std::sync::Arc; 19use std::sync::Arc;
20 20
21use chalk_ir::Mutability;
21use hir_def::{ 22use hir_def::{
22 body::Body, 23 body::Body,
23 data::{ConstData, FunctionData, StaticData}, 24 data::{ConstData, FunctionData, StaticData},
@@ -25,7 +26,7 @@ use hir_def::{
25 lang_item::LangItemTarget, 26 lang_item::LangItemTarget,
26 path::{path, Path}, 27 path::{path, Path},
27 resolver::{HasResolver, Resolver, TypeNs}, 28 resolver::{HasResolver, Resolver, TypeNs},
28 type_ref::{Mutability, TypeRef}, 29 type_ref::TypeRef,
29 AdtId, AssocItemId, DefWithBodyId, EnumVariantId, FieldId, FunctionId, Lookup, TraitId, 30 AdtId, AssocItemId, DefWithBodyId, EnumVariantId, FieldId, FunctionId, Lookup, TraitId,
30 TypeAliasId, VariantId, 31 TypeAliasId, VariantId,
31}; 32};
@@ -40,7 +41,7 @@ use super::{
40 InEnvironment, ProjectionTy, Substs, TraitEnvironment, TraitRef, Ty, TypeWalk, 41 InEnvironment, ProjectionTy, Substs, TraitEnvironment, TraitRef, Ty, TypeWalk,
41}; 42};
42use crate::{ 43use crate::{
43 db::HirDatabase, infer::diagnostics::InferenceDiagnostic, lower::ImplTraitLoweringMode, 44 db::HirDatabase, infer::diagnostics::InferenceDiagnostic, lower::ImplTraitLoweringMode, AliasTy,
44}; 45};
45 46
46pub(crate) use unify::unify; 47pub(crate) use unify::unify;
@@ -87,7 +88,7 @@ impl BindingMode {
87 fn convert(annotation: BindingAnnotation) -> BindingMode { 88 fn convert(annotation: BindingAnnotation) -> BindingMode {
88 match annotation { 89 match annotation {
89 BindingAnnotation::Unannotated | BindingAnnotation::Mutable => BindingMode::Move, 90 BindingAnnotation::Unannotated | BindingAnnotation::Mutable => BindingMode::Move,
90 BindingAnnotation::Ref => BindingMode::Ref(Mutability::Shared), 91 BindingAnnotation::Ref => BindingMode::Ref(Mutability::Not),
91 BindingAnnotation::RefMut => BindingMode::Ref(Mutability::Mut), 92 BindingAnnotation::RefMut => BindingMode::Ref(Mutability::Mut),
92 } 93 }
93 } 94 }
@@ -395,7 +396,7 @@ impl<'a> InferenceContext<'a> {
395 fn normalize_associated_types_in(&mut self, ty: Ty) -> Ty { 396 fn normalize_associated_types_in(&mut self, ty: Ty) -> Ty {
396 let ty = self.resolve_ty_as_possible(ty); 397 let ty = self.resolve_ty_as_possible(ty);
397 ty.fold(&mut |ty| match ty { 398 ty.fold(&mut |ty| match ty {
398 Ty::Projection(proj_ty) => self.normalize_projection_ty(proj_ty), 399 Ty::Alias(AliasTy::Projection(proj_ty)) => self.normalize_projection_ty(proj_ty),
399 _ => ty, 400 _ => ty,
400 }) 401 })
401 } 402 }
diff --git a/crates/hir_ty/src/infer/coerce.rs b/crates/hir_ty/src/infer/coerce.rs
index 667b26a76..cf0a3add4 100644
--- a/crates/hir_ty/src/infer/coerce.rs
+++ b/crates/hir_ty/src/infer/coerce.rs
@@ -4,8 +4,8 @@
4//! 4//!
5//! See: https://doc.rust-lang.org/nomicon/coercions.html 5//! See: https://doc.rust-lang.org/nomicon/coercions.html
6 6
7use chalk_ir::TyVariableKind; 7use chalk_ir::{Mutability, TyVariableKind};
8use hir_def::{lang_item::LangItemTarget, type_ref::Mutability}; 8use hir_def::lang_item::LangItemTarget;
9use test_utils::mark; 9use test_utils::mark;
10 10
11use crate::{autoderef, traits::Solution, Obligation, Substs, TraitRef, Ty}; 11use crate::{autoderef, traits::Solution, Obligation, Substs, TraitRef, Ty};
@@ -73,20 +73,20 @@ impl<'a> InferenceContext<'a> {
73 match (&mut from_ty, to_ty) { 73 match (&mut from_ty, to_ty) {
74 // `*mut T` -> `*const T` 74 // `*mut T` -> `*const T`
75 // `&mut T` -> `&T` 75 // `&mut T` -> `&T`
76 (Ty::RawPtr(m1, ..), Ty::RawPtr(m2 @ Mutability::Shared, ..)) 76 (Ty::Raw(m1, ..), Ty::Raw(m2 @ Mutability::Not, ..))
77 | (Ty::Ref(m1, ..), Ty::Ref(m2 @ Mutability::Shared, ..)) => { 77 | (Ty::Ref(m1, ..), Ty::Ref(m2 @ Mutability::Not, ..)) => {
78 *m1 = *m2; 78 *m1 = *m2;
79 } 79 }
80 // `&T` -> `*const T` 80 // `&T` -> `*const T`
81 // `&mut T` -> `*mut T`/`*const T` 81 // `&mut T` -> `*mut T`/`*const T`
82 (Ty::Ref(.., substs), &Ty::RawPtr(m2 @ Mutability::Shared, ..)) 82 (Ty::Ref(.., substs), &Ty::Raw(m2 @ Mutability::Not, ..))
83 | (Ty::Ref(Mutability::Mut, substs), &Ty::RawPtr(m2, ..)) => { 83 | (Ty::Ref(Mutability::Mut, substs), &Ty::Raw(m2, ..)) => {
84 from_ty = Ty::RawPtr(m2, substs.clone()); 84 from_ty = Ty::Raw(m2, substs.clone());
85 } 85 }
86 86
87 // Illegal mutability conversion 87 // Illegal mutability conversion
88 (Ty::RawPtr(Mutability::Shared, ..), Ty::RawPtr(Mutability::Mut, ..)) 88 (Ty::Raw(Mutability::Not, ..), Ty::Raw(Mutability::Mut, ..))
89 | (Ty::Ref(Mutability::Shared, ..), Ty::Ref(Mutability::Mut, ..)) => return false, 89 | (Ty::Ref(Mutability::Not, ..), Ty::Ref(Mutability::Mut, ..)) => return false,
90 90
91 // `{function_type}` -> `fn()` 91 // `{function_type}` -> `fn()`
92 (Ty::FnDef(..), Ty::Function { .. }) => match from_ty.callable_sig(self.db) { 92 (Ty::FnDef(..), Ty::Function { .. }) => match from_ty.callable_sig(self.db) {
diff --git a/crates/hir_ty/src/infer/expr.rs b/crates/hir_ty/src/infer/expr.rs
index 928ad37a3..cf1f1038a 100644
--- a/crates/hir_ty/src/infer/expr.rs
+++ b/crates/hir_ty/src/infer/expr.rs
@@ -3,7 +3,7 @@
3use std::iter::{repeat, repeat_with}; 3use std::iter::{repeat, repeat_with};
4use std::{mem, sync::Arc}; 4use std::{mem, sync::Arc};
5 5
6use chalk_ir::TyVariableKind; 6use chalk_ir::{Mutability, TyVariableKind};
7use hir_def::{ 7use hir_def::{
8 expr::{Array, BinaryOp, Expr, ExprId, Literal, Statement, UnaryOp}, 8 expr::{Array, BinaryOp, Expr, ExprId, Literal, Statement, UnaryOp},
9 path::{GenericArg, GenericArgs}, 9 path::{GenericArg, GenericArgs},
@@ -15,12 +15,14 @@ use syntax::ast::RangeOp;
15use test_utils::mark; 15use test_utils::mark;
16 16
17use crate::{ 17use crate::{
18 autoderef, method_resolution, op, 18 autoderef,
19 lower::lower_to_chalk_mutability,
20 method_resolution, op,
19 primitive::{self, UintTy}, 21 primitive::{self, UintTy},
20 traits::{FnTrait, InEnvironment}, 22 traits::{FnTrait, InEnvironment},
21 utils::{generics, variant_data, Generics}, 23 utils::{generics, variant_data, Generics},
22 Binders, CallableDefId, FnPointer, FnSig, Mutability, Obligation, OpaqueTyId, Rawness, Scalar, 24 Binders, CallableDefId, FnPointer, FnSig, Obligation, OpaqueTyId, Rawness, Scalar, Substs,
23 Substs, TraitRef, Ty, 25 TraitRef, Ty,
24}; 26};
25 27
26use super::{ 28use super::{
@@ -462,10 +464,11 @@ impl<'a> InferenceContext<'a> {
462 cast_ty 464 cast_ty
463 } 465 }
464 Expr::Ref { expr, rawness, mutability } => { 466 Expr::Ref { expr, rawness, mutability } => {
467 let mutability = lower_to_chalk_mutability(*mutability);
465 let expectation = if let Some((exp_inner, exp_rawness, exp_mutability)) = 468 let expectation = if let Some((exp_inner, exp_rawness, exp_mutability)) =
466 &expected.ty.as_reference_or_ptr() 469 &expected.ty.as_reference_or_ptr()
467 { 470 {
468 if *exp_mutability == Mutability::Mut && *mutability == Mutability::Shared { 471 if *exp_mutability == Mutability::Mut && mutability == Mutability::Not {
469 // FIXME: throw type error - expected mut reference but found shared ref, 472 // FIXME: throw type error - expected mut reference but found shared ref,
470 // which cannot be coerced 473 // which cannot be coerced
471 } 474 }
@@ -479,8 +482,8 @@ impl<'a> InferenceContext<'a> {
479 }; 482 };
480 let inner_ty = self.infer_expr_inner(*expr, &expectation); 483 let inner_ty = self.infer_expr_inner(*expr, &expectation);
481 match rawness { 484 match rawness {
482 Rawness::RawPtr => Ty::RawPtr(*mutability, Substs::single(inner_ty)), 485 Rawness::RawPtr => Ty::Raw(mutability, Substs::single(inner_ty)),
483 Rawness::Ref => Ty::Ref(*mutability, Substs::single(inner_ty)), 486 Rawness::Ref => Ty::Ref(mutability, Substs::single(inner_ty)),
484 } 487 }
485 } 488 }
486 Expr::Box { expr } => { 489 Expr::Box { expr } => {
@@ -684,11 +687,11 @@ impl<'a> InferenceContext<'a> {
684 } 687 }
685 Expr::Literal(lit) => match lit { 688 Expr::Literal(lit) => match lit {
686 Literal::Bool(..) => Ty::Scalar(Scalar::Bool), 689 Literal::Bool(..) => Ty::Scalar(Scalar::Bool),
687 Literal::String(..) => Ty::Ref(Mutability::Shared, Substs::single(Ty::Str)), 690 Literal::String(..) => Ty::Ref(Mutability::Not, Substs::single(Ty::Str)),
688 Literal::ByteString(..) => { 691 Literal::ByteString(..) => {
689 let byte_type = Ty::Scalar(Scalar::Uint(UintTy::U8)); 692 let byte_type = Ty::Scalar(Scalar::Uint(UintTy::U8));
690 let array_type = Ty::Array(Substs::single(byte_type)); 693 let array_type = Ty::Array(Substs::single(byte_type));
691 Ty::Ref(Mutability::Shared, Substs::single(array_type)) 694 Ty::Ref(Mutability::Not, Substs::single(array_type))
692 } 695 }
693 Literal::Char(..) => Ty::Scalar(Scalar::Char), 696 Literal::Char(..) => Ty::Scalar(Scalar::Char),
694 Literal::Int(_v, ty) => match ty { 697 Literal::Int(_v, ty) => match ty {
diff --git a/crates/hir_ty/src/infer/pat.rs b/crates/hir_ty/src/infer/pat.rs
index a318e47f3..eb099311c 100644
--- a/crates/hir_ty/src/infer/pat.rs
+++ b/crates/hir_ty/src/infer/pat.rs
@@ -3,17 +3,17 @@
3use std::iter::repeat; 3use std::iter::repeat;
4use std::sync::Arc; 4use std::sync::Arc;
5 5
6use chalk_ir::Mutability;
6use hir_def::{ 7use hir_def::{
7 expr::{BindingAnnotation, Expr, Literal, Pat, PatId, RecordFieldPat}, 8 expr::{BindingAnnotation, Expr, Literal, Pat, PatId, RecordFieldPat},
8 path::Path, 9 path::Path,
9 type_ref::Mutability,
10 FieldId, 10 FieldId,
11}; 11};
12use hir_expand::name::Name; 12use hir_expand::name::Name;
13use test_utils::mark; 13use test_utils::mark;
14 14
15use super::{BindingMode, Expectation, InferenceContext}; 15use super::{BindingMode, Expectation, InferenceContext};
16use crate::{utils::variant_data, Substs, Ty}; 16use crate::{lower::lower_to_chalk_mutability, utils::variant_data, Substs, Ty};
17 17
18impl<'a> InferenceContext<'a> { 18impl<'a> InferenceContext<'a> {
19 fn infer_tuple_struct_pat( 19 fn infer_tuple_struct_pat(
@@ -103,7 +103,7 @@ impl<'a> InferenceContext<'a> {
103 expected = inner; 103 expected = inner;
104 default_bm = match default_bm { 104 default_bm = match default_bm {
105 BindingMode::Move => BindingMode::Ref(mutability), 105 BindingMode::Move => BindingMode::Ref(mutability),
106 BindingMode::Ref(Mutability::Shared) => BindingMode::Ref(Mutability::Shared), 106 BindingMode::Ref(Mutability::Not) => BindingMode::Ref(Mutability::Not),
107 BindingMode::Ref(Mutability::Mut) => BindingMode::Ref(mutability), 107 BindingMode::Ref(Mutability::Mut) => BindingMode::Ref(mutability),
108 } 108 }
109 } 109 }
@@ -152,9 +152,10 @@ impl<'a> InferenceContext<'a> {
152 } 152 }
153 } 153 }
154 Pat::Ref { pat, mutability } => { 154 Pat::Ref { pat, mutability } => {
155 let mutability = lower_to_chalk_mutability(*mutability);
155 let expectation = match expected.as_reference() { 156 let expectation = match expected.as_reference() {
156 Some((inner_ty, exp_mut)) => { 157 Some((inner_ty, exp_mut)) => {
157 if *mutability != exp_mut { 158 if mutability != exp_mut {
158 // FIXME: emit type error? 159 // FIXME: emit type error?
159 } 160 }
160 inner_ty 161 inner_ty
@@ -162,7 +163,7 @@ impl<'a> InferenceContext<'a> {
162 _ => &Ty::Unknown, 163 _ => &Ty::Unknown,
163 }; 164 };
164 let subty = self.infer_pat(*pat, expectation, default_bm); 165 let subty = self.infer_pat(*pat, expectation, default_bm);
165 Ty::Ref(*mutability, Substs::single(subty)) 166 Ty::Ref(mutability, Substs::single(subty))
166 } 167 }
167 Pat::TupleStruct { path: p, args: subpats, ellipsis } => self.infer_tuple_struct_pat( 168 Pat::TupleStruct { path: p, args: subpats, ellipsis } => self.infer_tuple_struct_pat(
168 p.as_ref(), 169 p.as_ref(),
diff --git a/crates/hir_ty/src/infer/unify.rs b/crates/hir_ty/src/infer/unify.rs
index b481aa1b3..99a89a7f3 100644
--- a/crates/hir_ty/src/infer/unify.rs
+++ b/crates/hir_ty/src/infer/unify.rs
@@ -68,7 +68,7 @@ impl<'a, 'b> Canonicalizer<'a, 'b> {
68 } else { 68 } else {
69 let root = self.ctx.table.var_unification_table.find(inner); 69 let root = self.ctx.table.var_unification_table.find(inner);
70 let position = self.add(InferenceVar::from_inner(root), kind); 70 let position = self.add(InferenceVar::from_inner(root), kind);
71 Ty::Bound(BoundVar::new(binders, position)) 71 Ty::BoundVar(BoundVar::new(binders, position))
72 } 72 }
73 } 73 }
74 _ => ty, 74 _ => ty,
@@ -110,7 +110,7 @@ impl<T> Canonicalized<T> {
110 pub(super) fn decanonicalize_ty(&self, mut ty: Ty) -> Ty { 110 pub(super) fn decanonicalize_ty(&self, mut ty: Ty) -> Ty {
111 ty.walk_mut_binders( 111 ty.walk_mut_binders(
112 &mut |ty, binders| { 112 &mut |ty, binders| {
113 if let &mut Ty::Bound(bound) = ty { 113 if let &mut Ty::BoundVar(bound) = ty {
114 if bound.debruijn >= binders { 114 if bound.debruijn >= binders {
115 let (v, k) = self.free_vars[bound.index]; 115 let (v, k) = self.free_vars[bound.index];
116 *ty = Ty::InferenceVar(v, k); 116 *ty = Ty::InferenceVar(v, k);
@@ -168,7 +168,7 @@ pub(crate) fn unify(tys: &Canonical<(Ty, Ty)>) -> Option<Substs> {
168 // (kind of hacky) 168 // (kind of hacky)
169 for (i, var) in vars.iter().enumerate() { 169 for (i, var) in vars.iter().enumerate() {
170 if &*table.resolve_ty_shallow(var) == var { 170 if &*table.resolve_ty_shallow(var) == var {
171 table.unify(var, &Ty::Bound(BoundVar::new(DebruijnIndex::INNERMOST, i))); 171 table.unify(var, &Ty::BoundVar(BoundVar::new(DebruijnIndex::INNERMOST, i)));
172 } 172 }
173 } 173 }
174 Some( 174 Some(
diff --git a/crates/hir_ty/src/lib.rs b/crates/hir_ty/src/lib.rs
index 1131eaf92..c2a20c480 100644
--- a/crates/hir_ty/src/lib.rs
+++ b/crates/hir_ty/src/lib.rs
@@ -27,11 +27,9 @@ use std::{iter, mem, ops::Deref, sync::Arc};
27 27
28use base_db::salsa; 28use base_db::salsa;
29use hir_def::{ 29use hir_def::{
30 builtin_type::BuiltinType, 30 builtin_type::BuiltinType, expr::ExprId, type_ref::Rawness, AdtId, AssocContainerId,
31 expr::ExprId, 31 DefWithBodyId, FunctionId, GenericDefId, HasModule, LifetimeParamId, Lookup, TraitId,
32 type_ref::{Mutability, Rawness}, 32 TypeAliasId, TypeParamId,
33 AdtId, AssocContainerId, DefWithBodyId, FunctionId, GenericDefId, HasModule, LifetimeParamId,
34 Lookup, TraitId, TypeAliasId, TypeParamId,
35}; 33};
36use itertools::Itertools; 34use itertools::Itertools;
37 35
@@ -49,7 +47,7 @@ pub use lower::{
49}; 47};
50pub use traits::{InEnvironment, Obligation, ProjectionPredicate, TraitEnvironment}; 48pub use traits::{InEnvironment, Obligation, ProjectionPredicate, TraitEnvironment};
51 49
52pub use chalk_ir::{BoundVar, DebruijnIndex, Scalar, TyVariableKind}; 50pub use chalk_ir::{BoundVar, DebruijnIndex, Mutability, Scalar, TyVariableKind};
53 51
54#[derive(Clone, PartialEq, Eq, Debug, Hash)] 52#[derive(Clone, PartialEq, Eq, Debug, Hash)]
55pub enum Lifetime { 53pub enum Lifetime {
@@ -111,6 +109,19 @@ pub struct FnPointer {
111 pub substs: Substs, 109 pub substs: Substs,
112} 110}
113 111
112#[derive(Clone, PartialEq, Eq, Debug, Hash)]
113pub enum AliasTy {
114 /// A "projection" type corresponds to an (unnormalized)
115 /// projection like `<P0 as Trait<P1..Pn>>::Foo`. Note that the
116 /// trait and all its parameters are fully known.
117 Projection(ProjectionTy),
118 /// An opaque type (`impl Trait`).
119 ///
120 /// This is currently only used for return type impl trait; each instance of
121 /// `impl Trait` in a return type gets its own ID.
122 Opaque(OpaqueTy),
123}
124
114/// A type. 125/// A type.
115/// 126///
116/// See also the `TyKind` enum in rustc (librustc/ty/sty.rs), which represents 127/// See also the `TyKind` enum in rustc (librustc/ty/sty.rs), which represents
@@ -141,7 +152,7 @@ pub enum Ty {
141 Slice(Substs), 152 Slice(Substs),
142 153
143 /// A raw pointer. Written as `*mut T` or `*const T` 154 /// A raw pointer. Written as `*mut T` or `*const T`
144 RawPtr(Mutability, Substs), 155 Raw(Mutability, Substs),
145 156
146 /// A reference; a pointer with an associated lifetime. Written as 157 /// A reference; a pointer with an associated lifetime. Written as
147 /// `&'a mut T` or `&'a T`. 158 /// `&'a mut T` or `&'a T`.
@@ -193,16 +204,11 @@ pub enum Ty {
193 /// ``` 204 /// ```
194 Function(FnPointer), 205 Function(FnPointer),
195 206
196 /// A "projection" type corresponds to an (unnormalized) 207 /// An "alias" type represents some form of type alias, such as:
197 /// projection like `<P0 as Trait<P1..Pn>>::Foo`. Note that the 208 /// - An associated type projection like `<T as Iterator>::Item`
198 /// trait and all its parameters are fully known. 209 /// - `impl Trait` types
199 Projection(ProjectionTy), 210 /// - Named type aliases like `type Foo<X> = Vec<X>`
200 211 Alias(AliasTy),
201 /// An opaque type (`impl Trait`).
202 ///
203 /// This is currently only used for return type impl trait; each instance of
204 /// `impl Trait` in a return type gets its own ID.
205 Opaque(OpaqueTy),
206 212
207 /// A placeholder for a type parameter; for example, `T` in `fn f<T>(x: T) 213 /// A placeholder for a type parameter; for example, `T` in `fn f<T>(x: T)
208 /// {}` when we're type-checking the body of that function. In this 214 /// {}` when we're type-checking the body of that function. In this
@@ -215,7 +221,7 @@ pub enum Ty {
215 /// parameters get turned into variables; during trait resolution, inference 221 /// parameters get turned into variables; during trait resolution, inference
216 /// variables get turned into bound variables and back; and in `Dyn` the 222 /// variables get turned into bound variables and back; and in `Dyn` the
217 /// `Self` type is represented with a bound variable as well. 223 /// `Self` type is represented with a bound variable as well.
218 Bound(BoundVar), 224 BoundVar(BoundVar),
219 225
220 /// A type variable used during type checking. 226 /// A type variable used during type checking.
221 InferenceVar(InferenceVar, TyVariableKind), 227 InferenceVar(InferenceVar, TyVariableKind),
@@ -299,7 +305,7 @@ impl Substs {
299 generic_params 305 generic_params
300 .iter() 306 .iter()
301 .enumerate() 307 .enumerate()
302 .map(|(idx, _)| Ty::Bound(BoundVar::new(debruijn, idx))) 308 .map(|(idx, _)| Ty::BoundVar(BoundVar::new(debruijn, idx)))
303 .collect(), 309 .collect(),
304 ) 310 )
305 } 311 }
@@ -347,7 +353,7 @@ impl SubstsBuilder {
347 } 353 }
348 354
349 pub fn fill_with_bound_vars(self, debruijn: DebruijnIndex, starting_from: usize) -> Self { 355 pub fn fill_with_bound_vars(self, debruijn: DebruijnIndex, starting_from: usize) -> Self {
350 self.fill((starting_from..).map(|idx| Ty::Bound(BoundVar::new(debruijn, idx)))) 356 self.fill((starting_from..).map(|idx| Ty::BoundVar(BoundVar::new(debruijn, idx))))
351 } 357 }
352 358
353 pub fn fill_with_unknown(self) -> Self { 359 pub fn fill_with_unknown(self) -> Self {
@@ -627,7 +633,7 @@ impl Ty {
627 Ty::Ref(mutability, parameters) => { 633 Ty::Ref(mutability, parameters) => {
628 Some((parameters.as_single(), Rawness::Ref, *mutability)) 634 Some((parameters.as_single(), Rawness::Ref, *mutability))
629 } 635 }
630 Ty::RawPtr(mutability, parameters) => { 636 Ty::Raw(mutability, parameters) => {
631 Some((parameters.as_single(), Rawness::RawPtr, *mutability)) 637 Some((parameters.as_single(), Rawness::RawPtr, *mutability))
632 } 638 }
633 _ => None, 639 _ => None,
@@ -688,9 +694,7 @@ impl Ty {
688 expr == expr2 && def == def2 694 expr == expr2 && def == def2
689 } 695 }
690 (Ty::Ref(mutability, ..), Ty::Ref(mutability2, ..)) 696 (Ty::Ref(mutability, ..), Ty::Ref(mutability2, ..))
691 | (Ty::RawPtr(mutability, ..), Ty::RawPtr(mutability2, ..)) => { 697 | (Ty::Raw(mutability, ..), Ty::Raw(mutability2, ..)) => mutability == mutability2,
692 mutability == mutability2
693 }
694 ( 698 (
695 Ty::Function(FnPointer { num_args, sig, .. }), 699 Ty::Function(FnPointer { num_args, sig, .. }),
696 Ty::Function(FnPointer { num_args: num_args2, sig: sig2, .. }), 700 Ty::Function(FnPointer { num_args: num_args2, sig: sig2, .. }),
@@ -721,7 +725,7 @@ impl Ty {
721 fn builtin_deref(&self) -> Option<Ty> { 725 fn builtin_deref(&self) -> Option<Ty> {
722 match self { 726 match self {
723 Ty::Ref(.., parameters) => Some(Ty::clone(parameters.as_single())), 727 Ty::Ref(.., parameters) => Some(Ty::clone(parameters.as_single())),
724 Ty::RawPtr(.., parameters) => Some(Ty::clone(parameters.as_single())), 728 Ty::Raw(.., parameters) => Some(Ty::clone(parameters.as_single())),
725 _ => None, 729 _ => None,
726 } 730 }
727 } 731 }
@@ -757,7 +761,7 @@ impl Ty {
757 Ty::Adt(_, substs) 761 Ty::Adt(_, substs)
758 | Ty::Slice(substs) 762 | Ty::Slice(substs)
759 | Ty::Array(substs) 763 | Ty::Array(substs)
760 | Ty::RawPtr(_, substs) 764 | Ty::Raw(_, substs)
761 | Ty::Ref(_, substs) 765 | Ty::Ref(_, substs)
762 | Ty::FnDef(_, substs) 766 | Ty::FnDef(_, substs)
763 | Ty::Function(FnPointer { substs, .. }) 767 | Ty::Function(FnPointer { substs, .. })
@@ -780,7 +784,7 @@ impl Ty {
780 Ty::Adt(_, substs) 784 Ty::Adt(_, substs)
781 | Ty::Slice(substs) 785 | Ty::Slice(substs)
782 | Ty::Array(substs) 786 | Ty::Array(substs)
783 | Ty::RawPtr(_, substs) 787 | Ty::Raw(_, substs)
784 | Ty::Ref(_, substs) 788 | Ty::Ref(_, substs)
785 | Ty::FnDef(_, substs) 789 | Ty::FnDef(_, substs)
786 | Ty::Function(FnPointer { substs, .. }) 790 | Ty::Function(FnPointer { substs, .. })
@@ -797,7 +801,7 @@ impl Ty {
797 Ty::Adt(_, substs) 801 Ty::Adt(_, substs)
798 | Ty::Slice(substs) 802 | Ty::Slice(substs)
799 | Ty::Array(substs) 803 | Ty::Array(substs)
800 | Ty::RawPtr(_, substs) 804 | Ty::Raw(_, substs)
801 | Ty::Ref(_, substs) 805 | Ty::Ref(_, substs)
802 | Ty::FnDef(_, substs) 806 | Ty::FnDef(_, substs)
803 | Ty::Function(FnPointer { substs, .. }) 807 | Ty::Function(FnPointer { substs, .. })
@@ -834,7 +838,7 @@ impl Ty {
834 OpaqueTyId::ReturnTypeImplTrait(..) => None, 838 OpaqueTyId::ReturnTypeImplTrait(..) => None,
835 } 839 }
836 } 840 }
837 Ty::Opaque(opaque_ty) => { 841 Ty::Alias(AliasTy::Opaque(opaque_ty)) => {
838 let predicates = match opaque_ty.opaque_ty_id { 842 let predicates = match opaque_ty.opaque_ty_id {
839 OpaqueTyId::ReturnTypeImplTrait(func, idx) => { 843 OpaqueTyId::ReturnTypeImplTrait(func, idx) => {
840 db.return_type_impl_traits(func).map(|it| { 844 db.return_type_impl_traits(func).map(|it| {
@@ -878,7 +882,7 @@ impl Ty {
878 _ => None, 882 _ => None,
879 } 883 }
880 } 884 }
881 Ty::Projection(projection_ty) => { 885 Ty::Alias(AliasTy::Projection(projection_ty)) => {
882 match projection_ty.associated_ty.lookup(db.upcast()).container { 886 match projection_ty.associated_ty.lookup(db.upcast()).container {
883 AssocContainerId::TraitId(trait_id) => Some(trait_id), 887 AssocContainerId::TraitId(trait_id) => Some(trait_id),
884 _ => None, 888 _ => None,
@@ -956,7 +960,7 @@ pub trait TypeWalk {
956 { 960 {
957 self.walk_mut_binders( 961 self.walk_mut_binders(
958 &mut |ty, binders| { 962 &mut |ty, binders| {
959 if let &mut Ty::Bound(bound) = ty { 963 if let &mut Ty::BoundVar(bound) = ty {
960 if bound.debruijn >= binders { 964 if bound.debruijn >= binders {
961 *ty = substs.0[bound.index].clone().shift_bound_vars(binders); 965 *ty = substs.0[bound.index].clone().shift_bound_vars(binders);
962 } 966 }
@@ -974,8 +978,8 @@ pub trait TypeWalk {
974 { 978 {
975 self.fold_binders( 979 self.fold_binders(
976 &mut |ty, binders| match ty { 980 &mut |ty, binders| match ty {
977 Ty::Bound(bound) if bound.debruijn >= binders => { 981 Ty::BoundVar(bound) if bound.debruijn >= binders => {
978 Ty::Bound(bound.shifted_in_from(n)) 982 Ty::BoundVar(bound.shifted_in_from(n))
979 } 983 }
980 ty => ty, 984 ty => ty,
981 }, 985 },
@@ -987,21 +991,21 @@ pub trait TypeWalk {
987impl TypeWalk for Ty { 991impl TypeWalk for Ty {
988 fn walk(&self, f: &mut impl FnMut(&Ty)) { 992 fn walk(&self, f: &mut impl FnMut(&Ty)) {
989 match self { 993 match self {
990 Ty::Projection(p_ty) => { 994 Ty::Alias(AliasTy::Projection(p_ty)) => {
991 for t in p_ty.parameters.iter() { 995 for t in p_ty.parameters.iter() {
992 t.walk(f); 996 t.walk(f);
993 } 997 }
994 } 998 }
999 Ty::Alias(AliasTy::Opaque(o_ty)) => {
1000 for t in o_ty.parameters.iter() {
1001 t.walk(f);
1002 }
1003 }
995 Ty::Dyn(predicates) => { 1004 Ty::Dyn(predicates) => {
996 for p in predicates.iter() { 1005 for p in predicates.iter() {
997 p.walk(f); 1006 p.walk(f);
998 } 1007 }
999 } 1008 }
1000 Ty::Opaque(o_ty) => {
1001 for t in o_ty.parameters.iter() {
1002 t.walk(f);
1003 }
1004 }
1005 _ => { 1009 _ => {
1006 if let Some(substs) = self.substs() { 1010 if let Some(substs) = self.substs() {
1007 for t in substs.iter() { 1011 for t in substs.iter() {
@@ -1019,7 +1023,7 @@ impl TypeWalk for Ty {
1019 binders: DebruijnIndex, 1023 binders: DebruijnIndex,
1020 ) { 1024 ) {
1021 match self { 1025 match self {
1022 Ty::Projection(p_ty) => { 1026 Ty::Alias(AliasTy::Projection(p_ty)) => {
1023 p_ty.parameters.walk_mut_binders(f, binders); 1027 p_ty.parameters.walk_mut_binders(f, binders);
1024 } 1028 }
1025 Ty::Dyn(predicates) => { 1029 Ty::Dyn(predicates) => {
@@ -1027,7 +1031,7 @@ impl TypeWalk for Ty {
1027 p.walk_mut_binders(f, binders.shifted_in()); 1031 p.walk_mut_binders(f, binders.shifted_in());
1028 } 1032 }
1029 } 1033 }
1030 Ty::Opaque(o_ty) => { 1034 Ty::Alias(AliasTy::Opaque(o_ty)) => {
1031 o_ty.parameters.walk_mut_binders(f, binders); 1035 o_ty.parameters.walk_mut_binders(f, binders);
1032 } 1036 }
1033 _ => { 1037 _ => {
diff --git a/crates/hir_ty/src/lower.rs b/crates/hir_ty/src/lower.rs
index 44bd95a9a..1b5843d48 100644
--- a/crates/hir_ty/src/lower.rs
+++ b/crates/hir_ty/src/lower.rs
@@ -8,6 +8,7 @@
8use std::{iter, sync::Arc}; 8use std::{iter, sync::Arc};
9 9
10use base_db::CrateId; 10use base_db::CrateId;
11use chalk_ir::Mutability;
11use hir_def::{ 12use hir_def::{
12 adt::StructKind, 13 adt::StructKind,
13 builtin_type::BuiltinType, 14 builtin_type::BuiltinType,
@@ -31,8 +32,8 @@ use crate::{
31 all_super_trait_refs, associated_type_by_name_including_super_traits, generics, 32 all_super_trait_refs, associated_type_by_name_including_super_traits, generics,
32 make_mut_slice, variant_data, 33 make_mut_slice, variant_data,
33 }, 34 },
34 Binders, BoundVar, CallableSig, DebruijnIndex, FnPointer, FnSig, GenericPredicate, OpaqueTy, 35 AliasTy, Binders, BoundVar, CallableSig, DebruijnIndex, FnPointer, FnSig, GenericPredicate,
35 OpaqueTyId, PolyFnSig, ProjectionPredicate, ProjectionTy, ReturnTypeImplTrait, 36 OpaqueTy, OpaqueTyId, PolyFnSig, ProjectionPredicate, ProjectionTy, ReturnTypeImplTrait,
36 ReturnTypeImplTraits, Substs, TraitEnvironment, TraitRef, Ty, TypeWalk, 37 ReturnTypeImplTraits, Substs, TraitEnvironment, TraitRef, Ty, TypeWalk,
37}; 38};
38 39
@@ -157,7 +158,7 @@ impl Ty {
157 } 158 }
158 TypeRef::RawPtr(inner, mutability) => { 159 TypeRef::RawPtr(inner, mutability) => {
159 let inner_ty = Ty::from_hir(ctx, inner); 160 let inner_ty = Ty::from_hir(ctx, inner);
160 Ty::RawPtr(*mutability, Substs::single(inner_ty)) 161 Ty::Raw(lower_to_chalk_mutability(*mutability), Substs::single(inner_ty))
161 } 162 }
162 TypeRef::Array(inner) => { 163 TypeRef::Array(inner) => {
163 let inner_ty = Ty::from_hir(ctx, inner); 164 let inner_ty = Ty::from_hir(ctx, inner);
@@ -169,7 +170,7 @@ impl Ty {
169 } 170 }
170 TypeRef::Reference(inner, _, mutability) => { 171 TypeRef::Reference(inner, _, mutability) => {
171 let inner_ty = Ty::from_hir(ctx, inner); 172 let inner_ty = Ty::from_hir(ctx, inner);
172 Ty::Ref(*mutability, Substs::single(inner_ty)) 173 Ty::Ref(lower_to_chalk_mutability(*mutability), Substs::single(inner_ty))
173 } 174 }
174 TypeRef::Placeholder => Ty::Unknown, 175 TypeRef::Placeholder => Ty::Unknown,
175 TypeRef::Fn(params, is_varargs) => { 176 TypeRef::Fn(params, is_varargs) => {
@@ -181,7 +182,7 @@ impl Ty {
181 }) 182 })
182 } 183 }
183 TypeRef::DynTrait(bounds) => { 184 TypeRef::DynTrait(bounds) => {
184 let self_ty = Ty::Bound(BoundVar::new(DebruijnIndex::INNERMOST, 0)); 185 let self_ty = Ty::BoundVar(BoundVar::new(DebruijnIndex::INNERMOST, 0));
185 let predicates = ctx.with_shifted_in(DebruijnIndex::ONE, |ctx| { 186 let predicates = ctx.with_shifted_in(DebruijnIndex::ONE, |ctx| {
186 bounds 187 bounds
187 .iter() 188 .iter()
@@ -225,7 +226,10 @@ impl Ty {
225 let impl_trait_id = OpaqueTyId::ReturnTypeImplTrait(func, idx); 226 let impl_trait_id = OpaqueTyId::ReturnTypeImplTrait(func, idx);
226 let generics = generics(ctx.db.upcast(), func.into()); 227 let generics = generics(ctx.db.upcast(), func.into());
227 let parameters = Substs::bound_vars(&generics, ctx.in_binders); 228 let parameters = Substs::bound_vars(&generics, ctx.in_binders);
228 Ty::Opaque(OpaqueTy { opaque_ty_id: impl_trait_id, parameters }) 229 Ty::Alias(AliasTy::Opaque(OpaqueTy {
230 opaque_ty_id: impl_trait_id,
231 parameters,
232 }))
229 } 233 }
230 ImplTraitLoweringMode::Param => { 234 ImplTraitLoweringMode::Param => {
231 let idx = ctx.impl_trait_counter.get(); 235 let idx = ctx.impl_trait_counter.get();
@@ -256,7 +260,7 @@ impl Ty {
256 } else { 260 } else {
257 (0, 0, 0, 0) 261 (0, 0, 0, 0)
258 }; 262 };
259 Ty::Bound(BoundVar::new( 263 Ty::BoundVar(BoundVar::new(
260 ctx.in_binders, 264 ctx.in_binders,
261 idx as usize + parent_params + self_params + list_params, 265 idx as usize + parent_params + self_params + list_params,
262 )) 266 ))
@@ -328,7 +332,7 @@ impl Ty {
328 TypeNs::TraitId(trait_) => { 332 TypeNs::TraitId(trait_) => {
329 // if this is a bare dyn Trait, we'll directly put the required ^0 for the self type in there 333 // if this is a bare dyn Trait, we'll directly put the required ^0 for the self type in there
330 let self_ty = if remaining_segments.len() == 0 { 334 let self_ty = if remaining_segments.len() == 0 {
331 Some(Ty::Bound(BoundVar::new(DebruijnIndex::INNERMOST, 0))) 335 Some(Ty::BoundVar(BoundVar::new(DebruijnIndex::INNERMOST, 0)))
332 } else { 336 } else {
333 None 337 None
334 }; 338 };
@@ -344,10 +348,10 @@ impl Ty {
344 match found { 348 match found {
345 Some((super_trait_ref, associated_ty)) => { 349 Some((super_trait_ref, associated_ty)) => {
346 // FIXME handle type parameters on the segment 350 // FIXME handle type parameters on the segment
347 Ty::Projection(ProjectionTy { 351 Ty::Alias(AliasTy::Projection(ProjectionTy {
348 associated_ty, 352 associated_ty,
349 parameters: super_trait_ref.substs, 353 parameters: super_trait_ref.substs,
350 }) 354 }))
351 } 355 }
352 None => { 356 None => {
353 // FIXME: report error (associated type not found) 357 // FIXME: report error (associated type not found)
@@ -371,7 +375,7 @@ impl Ty {
371 TypeParamLoweringMode::Placeholder => Ty::Placeholder(param_id), 375 TypeParamLoweringMode::Placeholder => Ty::Placeholder(param_id),
372 TypeParamLoweringMode::Variable => { 376 TypeParamLoweringMode::Variable => {
373 let idx = generics.param_idx(param_id).expect("matching generics"); 377 let idx = generics.param_idx(param_id).expect("matching generics");
374 Ty::Bound(BoundVar::new(ctx.in_binders, idx)) 378 Ty::BoundVar(BoundVar::new(ctx.in_binders, idx))
375 } 379 }
376 } 380 }
377 } 381 }
@@ -469,10 +473,10 @@ impl Ty {
469 // associated_type_shorthand_candidates does not do that 473 // associated_type_shorthand_candidates does not do that
470 let substs = substs.shift_bound_vars(ctx.in_binders); 474 let substs = substs.shift_bound_vars(ctx.in_binders);
471 // FIXME handle type parameters on the segment 475 // FIXME handle type parameters on the segment
472 return Some(Ty::Projection(ProjectionTy { 476 return Some(Ty::Alias(AliasTy::Projection(ProjectionTy {
473 associated_ty, 477 associated_ty,
474 parameters: substs, 478 parameters: substs,
475 })); 479 })));
476 } 480 }
477 481
478 None 482 None
@@ -673,7 +677,7 @@ impl GenericPredicate {
673 TypeParamLoweringMode::Placeholder => Ty::Placeholder(param_id), 677 TypeParamLoweringMode::Placeholder => Ty::Placeholder(param_id),
674 TypeParamLoweringMode::Variable => { 678 TypeParamLoweringMode::Variable => {
675 let idx = generics.param_idx(param_id).expect("matching generics"); 679 let idx = generics.param_idx(param_id).expect("matching generics");
676 Ty::Bound(BoundVar::new(DebruijnIndex::INNERMOST, idx)) 680 Ty::BoundVar(BoundVar::new(DebruijnIndex::INNERMOST, idx))
677 } 681 }
678 } 682 }
679 } 683 }
@@ -747,7 +751,7 @@ fn assoc_type_bindings_from_type_bound<'a>(
747 preds.extend(GenericPredicate::from_type_bound( 751 preds.extend(GenericPredicate::from_type_bound(
748 ctx, 752 ctx,
749 bound, 753 bound,
750 Ty::Projection(projection_ty.clone()), 754 Ty::Alias(AliasTy::Projection(projection_ty.clone())),
751 )); 755 ));
752 } 756 }
753 preds 757 preds
@@ -757,7 +761,7 @@ fn assoc_type_bindings_from_type_bound<'a>(
757impl ReturnTypeImplTrait { 761impl ReturnTypeImplTrait {
758 fn from_hir(ctx: &TyLoweringContext, bounds: &[TypeBound]) -> Self { 762 fn from_hir(ctx: &TyLoweringContext, bounds: &[TypeBound]) -> Self {
759 mark::hit!(lower_rpit); 763 mark::hit!(lower_rpit);
760 let self_ty = Ty::Bound(BoundVar::new(DebruijnIndex::INNERMOST, 0)); 764 let self_ty = Ty::BoundVar(BoundVar::new(DebruijnIndex::INNERMOST, 0));
761 let predicates = ctx.with_shifted_in(DebruijnIndex::ONE, |ctx| { 765 let predicates = ctx.with_shifted_in(DebruijnIndex::ONE, |ctx| {
762 bounds 766 bounds
763 .iter() 767 .iter()
@@ -981,7 +985,7 @@ pub(crate) fn generic_defaults_query(
981 // Each default can only refer to previous parameters. 985 // Each default can only refer to previous parameters.
982 ty.walk_mut_binders( 986 ty.walk_mut_binders(
983 &mut |ty, binders| match ty { 987 &mut |ty, binders| match ty {
984 Ty::Bound(BoundVar { debruijn, index }) if *debruijn == binders => { 988 Ty::BoundVar(BoundVar { debruijn, index }) if *debruijn == binders => {
985 if *index >= idx { 989 if *index >= idx {
986 // type variable default referring to parameter coming 990 // type variable default referring to parameter coming
987 // after it. This is forbidden (FIXME: report 991 // after it. This is forbidden (FIXME: report
@@ -1256,3 +1260,10 @@ pub(crate) fn return_type_impl_traits(
1256 Some(Arc::new(Binders::new(num_binders, return_type_impl_traits))) 1260 Some(Arc::new(Binders::new(num_binders, return_type_impl_traits)))
1257 } 1261 }
1258} 1262}
1263
1264pub(crate) fn lower_to_chalk_mutability(m: hir_def::type_ref::Mutability) -> Mutability {
1265 match m {
1266 hir_def::type_ref::Mutability::Shared => Mutability::Not,
1267 hir_def::type_ref::Mutability::Mut => Mutability::Mut,
1268 }
1269}
diff --git a/crates/hir_ty/src/method_resolution.rs b/crates/hir_ty/src/method_resolution.rs
index 8b1717873..f301a8477 100644
--- a/crates/hir_ty/src/method_resolution.rs
+++ b/crates/hir_ty/src/method_resolution.rs
@@ -6,9 +6,10 @@ use std::{iter, sync::Arc};
6 6
7use arrayvec::ArrayVec; 7use arrayvec::ArrayVec;
8use base_db::CrateId; 8use base_db::CrateId;
9use chalk_ir::Mutability;
9use hir_def::{ 10use hir_def::{
10 lang_item::LangItemTarget, type_ref::Mutability, AdtId, AssocContainerId, AssocItemId, 11 lang_item::LangItemTarget, AdtId, AssocContainerId, AssocItemId, FunctionId, GenericDefId,
11 FunctionId, GenericDefId, HasModule, ImplId, Lookup, ModuleId, TraitId, TypeAliasId, 12 HasModule, ImplId, Lookup, ModuleId, TraitId, TypeAliasId,
12}; 13};
13use hir_expand::name::Name; 14use hir_expand::name::Name;
14use rustc_hash::{FxHashMap, FxHashSet}; 15use rustc_hash::{FxHashMap, FxHashSet};
@@ -51,7 +52,7 @@ impl TyFingerprint {
51 &Ty::Scalar(scalar) => TyFingerprint::Scalar(scalar), 52 &Ty::Scalar(scalar) => TyFingerprint::Scalar(scalar),
52 &Ty::Adt(adt, _) => TyFingerprint::Adt(adt), 53 &Ty::Adt(adt, _) => TyFingerprint::Adt(adt),
53 &Ty::Tuple(cardinality, _) => TyFingerprint::Tuple(cardinality), 54 &Ty::Tuple(cardinality, _) => TyFingerprint::Tuple(cardinality),
54 &Ty::RawPtr(mutability, ..) => TyFingerprint::RawPtr(mutability), 55 &Ty::Raw(mutability, ..) => TyFingerprint::RawPtr(mutability),
55 &Ty::ForeignType(alias_id, ..) => TyFingerprint::ForeignType(alias_id), 56 &Ty::ForeignType(alias_id, ..) => TyFingerprint::ForeignType(alias_id),
56 &Ty::Function(FnPointer { num_args, sig, .. }) => TyFingerprint::FnPtr(num_args, sig), 57 &Ty::Function(FnPointer { num_args, sig, .. }) => TyFingerprint::FnPtr(num_args, sig),
57 Ty::Dyn(_) => ty.dyn_trait().map(|trait_| TyFingerprint::Dyn(trait_))?, 58 Ty::Dyn(_) => ty.dyn_trait().map(|trait_| TyFingerprint::Dyn(trait_))?,
@@ -251,8 +252,8 @@ impl Ty {
251 } 252 }
252 Ty::Str => lang_item_crate!("str_alloc", "str"), 253 Ty::Str => lang_item_crate!("str_alloc", "str"),
253 Ty::Slice(_) => lang_item_crate!("slice_alloc", "slice"), 254 Ty::Slice(_) => lang_item_crate!("slice_alloc", "slice"),
254 Ty::RawPtr(Mutability::Shared, _) => lang_item_crate!("const_ptr"), 255 Ty::Raw(Mutability::Not, _) => lang_item_crate!("const_ptr"),
255 Ty::RawPtr(Mutability::Mut, _) => lang_item_crate!("mut_ptr"), 256 Ty::Raw(Mutability::Mut, _) => lang_item_crate!("mut_ptr"),
256 Ty::Dyn(_) => { 257 Ty::Dyn(_) => {
257 return self.dyn_trait().and_then(|trait_| { 258 return self.dyn_trait().and_then(|trait_| {
258 mod_to_crate_ids(GenericDefId::TraitId(trait_).module(db.upcast())) 259 mod_to_crate_ids(GenericDefId::TraitId(trait_).module(db.upcast()))
@@ -429,7 +430,7 @@ fn iterate_method_candidates_with_autoref(
429 } 430 }
430 let refed = Canonical { 431 let refed = Canonical {
431 kinds: deref_chain[0].kinds.clone(), 432 kinds: deref_chain[0].kinds.clone(),
432 value: Ty::Ref(Mutability::Shared, Substs::single(deref_chain[0].value.clone())), 433 value: Ty::Ref(Mutability::Not, Substs::single(deref_chain[0].value.clone())),
433 }; 434 };
434 if iterate_method_candidates_by_receiver( 435 if iterate_method_candidates_by_receiver(
435 &refed, 436 &refed,
@@ -683,7 +684,7 @@ pub(crate) fn inherent_impl_substs(
683fn fallback_bound_vars(s: Substs, num_vars_to_keep: usize) -> Substs { 684fn fallback_bound_vars(s: Substs, num_vars_to_keep: usize) -> Substs {
684 s.fold_binders( 685 s.fold_binders(
685 &mut |ty, binders| { 686 &mut |ty, binders| {
686 if let Ty::Bound(bound) = &ty { 687 if let Ty::BoundVar(bound) = &ty {
687 if bound.index >= num_vars_to_keep && bound.debruijn >= binders { 688 if bound.index >= num_vars_to_keep && bound.debruijn >= binders {
688 Ty::Unknown 689 Ty::Unknown
689 } else { 690 } else {
diff --git a/crates/hir_ty/src/traits.rs b/crates/hir_ty/src/traits.rs
index dfa51896b..e4cdb6d53 100644
--- a/crates/hir_ty/src/traits.rs
+++ b/crates/hir_ty/src/traits.rs
@@ -129,7 +129,7 @@ pub(crate) fn trait_solve_query(
129 log::info!("trait_solve_query({})", goal.value.value.display(db)); 129 log::info!("trait_solve_query({})", goal.value.value.display(db));
130 130
131 if let Obligation::Projection(pred) = &goal.value.value { 131 if let Obligation::Projection(pred) = &goal.value.value {
132 if let Ty::Bound(_) = &pred.projection_ty.parameters[0] { 132 if let Ty::BoundVar(_) = &pred.projection_ty.parameters[0] {
133 // Hack: don't ask Chalk to normalize with an unknown self type, it'll say that's impossible 133 // Hack: don't ask Chalk to normalize with an unknown self type, it'll say that's impossible
134 return Some(Solution::Ambig(Guidance::Unknown)); 134 return Some(Solution::Ambig(Guidance::Unknown));
135 } 135 }
diff --git a/crates/hir_ty/src/traits/chalk.rs b/crates/hir_ty/src/traits/chalk.rs
index 3f5f5091f..e513fa8f4 100644
--- a/crates/hir_ty/src/traits/chalk.rs
+++ b/crates/hir_ty/src/traits/chalk.rs
@@ -90,7 +90,7 @@ impl<'a> chalk_solve::RustIrDatabase<Interner> for ChalkContext<'a> {
90 ty: &Ty, 90 ty: &Ty,
91 binders: &CanonicalVarKinds<Interner>, 91 binders: &CanonicalVarKinds<Interner>,
92 ) -> Option<chalk_ir::TyVariableKind> { 92 ) -> Option<chalk_ir::TyVariableKind> {
93 if let Ty::Bound(bv) = ty { 93 if let Ty::BoundVar(bv) = ty {
94 let binders = binders.as_slice(&Interner); 94 let binders = binders.as_slice(&Interner);
95 if bv.debruijn == DebruijnIndex::INNERMOST { 95 if bv.debruijn == DebruijnIndex::INNERMOST {
96 if let chalk_ir::VariableKind::Ty(tk) = binders[bv.index].kind { 96 if let chalk_ir::VariableKind::Ty(tk) = binders[bv.index].kind {
@@ -220,18 +220,18 @@ impl<'a> chalk_solve::RustIrDatabase<Interner> for ChalkContext<'a> {
220 let impl_bound = GenericPredicate::Implemented(TraitRef { 220 let impl_bound = GenericPredicate::Implemented(TraitRef {
221 trait_: future_trait, 221 trait_: future_trait,
222 // Self type as the first parameter. 222 // Self type as the first parameter.
223 substs: Substs::single(Ty::Bound(BoundVar { 223 substs: Substs::single(Ty::BoundVar(BoundVar {
224 debruijn: DebruijnIndex::INNERMOST, 224 debruijn: DebruijnIndex::INNERMOST,
225 index: 0, 225 index: 0,
226 })), 226 })),
227 }); 227 });
228 let proj_bound = GenericPredicate::Projection(ProjectionPredicate { 228 let proj_bound = GenericPredicate::Projection(ProjectionPredicate {
229 // The parameter of the opaque type. 229 // The parameter of the opaque type.
230 ty: Ty::Bound(BoundVar { debruijn: DebruijnIndex::ONE, index: 0 }), 230 ty: Ty::BoundVar(BoundVar { debruijn: DebruijnIndex::ONE, index: 0 }),
231 projection_ty: ProjectionTy { 231 projection_ty: ProjectionTy {
232 associated_ty: future_output, 232 associated_ty: future_output,
233 // Self type as the first parameter. 233 // Self type as the first parameter.
234 parameters: Substs::single(Ty::Bound(BoundVar::new( 234 parameters: Substs::single(Ty::BoundVar(BoundVar::new(
235 DebruijnIndex::INNERMOST, 235 DebruijnIndex::INNERMOST,
236 0, 236 0,
237 ))), 237 ))),
@@ -392,7 +392,7 @@ pub(crate) fn associated_ty_data_query(
392 let resolver = hir_def::resolver::HasResolver::resolver(type_alias, db.upcast()); 392 let resolver = hir_def::resolver::HasResolver::resolver(type_alias, db.upcast());
393 let ctx = crate::TyLoweringContext::new(db, &resolver) 393 let ctx = crate::TyLoweringContext::new(db, &resolver)
394 .with_type_param_mode(crate::lower::TypeParamLoweringMode::Variable); 394 .with_type_param_mode(crate::lower::TypeParamLoweringMode::Variable);
395 let self_ty = Ty::Bound(crate::BoundVar::new(crate::DebruijnIndex::INNERMOST, 0)); 395 let self_ty = Ty::BoundVar(crate::BoundVar::new(crate::DebruijnIndex::INNERMOST, 0));
396 let bounds = type_alias_data 396 let bounds = type_alias_data
397 .bounds 397 .bounds
398 .iter() 398 .iter()
diff --git a/crates/hir_ty/src/traits/chalk/mapping.rs b/crates/hir_ty/src/traits/chalk/mapping.rs
index 995ff6a9a..db1760e6c 100644
--- a/crates/hir_ty/src/traits/chalk/mapping.rs
+++ b/crates/hir_ty/src/traits/chalk/mapping.rs
@@ -10,14 +10,14 @@ use chalk_ir::{
10use chalk_solve::rust_ir; 10use chalk_solve::rust_ir;
11 11
12use base_db::salsa::InternKey; 12use base_db::salsa::InternKey;
13use hir_def::{type_ref::Mutability, AssocContainerId, GenericDefId, Lookup, TypeAliasId}; 13use hir_def::{AssocContainerId, GenericDefId, Lookup, TypeAliasId};
14 14
15use crate::{ 15use crate::{
16 db::HirDatabase, 16 db::HirDatabase,
17 primitive::UintTy, 17 primitive::UintTy,
18 traits::{Canonical, Obligation}, 18 traits::{Canonical, Obligation},
19 CallableDefId, FnPointer, FnSig, GenericPredicate, InEnvironment, OpaqueTy, OpaqueTyId, 19 AliasTy, CallableDefId, FnPointer, FnSig, GenericPredicate, InEnvironment, OpaqueTy,
20 ProjectionPredicate, ProjectionTy, Scalar, Substs, TraitEnvironment, TraitRef, Ty, 20 OpaqueTyId, ProjectionPredicate, ProjectionTy, Scalar, Substs, TraitEnvironment, TraitRef, Ty,
21}; 21};
22 22
23use super::interner::*; 23use super::interner::*;
@@ -63,9 +63,9 @@ impl ToChalk for Ty {
63 let substitution = substs.to_chalk(db); 63 let substitution = substs.to_chalk(db);
64 chalk_ir::TyKind::Tuple(cardinality.into(), substitution).intern(&Interner) 64 chalk_ir::TyKind::Tuple(cardinality.into(), substitution).intern(&Interner)
65 } 65 }
66 Ty::RawPtr(mutability, substs) => { 66 Ty::Raw(mutability, substs) => {
67 let ty = substs[0].clone().to_chalk(db); 67 let ty = substs[0].clone().to_chalk(db);
68 chalk_ir::TyKind::Raw(mutability.to_chalk(db), ty).intern(&Interner) 68 chalk_ir::TyKind::Raw(mutability, ty).intern(&Interner)
69 } 69 }
70 Ty::Slice(substs) => { 70 Ty::Slice(substs) => {
71 chalk_ir::TyKind::Slice(substs[0].clone().to_chalk(db)).intern(&Interner) 71 chalk_ir::TyKind::Slice(substs[0].clone().to_chalk(db)).intern(&Interner)
@@ -88,7 +88,7 @@ impl ToChalk for Ty {
88 let substitution = substs.to_chalk(db); 88 let substitution = substs.to_chalk(db);
89 chalk_ir::TyKind::Adt(chalk_ir::AdtId(adt_id), substitution).intern(&Interner) 89 chalk_ir::TyKind::Adt(chalk_ir::AdtId(adt_id), substitution).intern(&Interner)
90 } 90 }
91 Ty::Projection(proj_ty) => { 91 Ty::Alias(AliasTy::Projection(proj_ty)) => {
92 let associated_ty_id = TypeAliasAsAssocType(proj_ty.associated_ty).to_chalk(db); 92 let associated_ty_id = TypeAliasAsAssocType(proj_ty.associated_ty).to_chalk(db);
93 let substitution = proj_ty.parameters.to_chalk(db); 93 let substitution = proj_ty.parameters.to_chalk(db);
94 chalk_ir::AliasTy::Projection(chalk_ir::ProjectionTy { 94 chalk_ir::AliasTy::Projection(chalk_ir::ProjectionTy {
@@ -106,7 +106,7 @@ impl ToChalk for Ty {
106 } 106 }
107 .to_ty::<Interner>(&Interner) 107 .to_ty::<Interner>(&Interner)
108 } 108 }
109 Ty::Bound(idx) => chalk_ir::TyKind::BoundVar(idx).intern(&Interner), 109 Ty::BoundVar(idx) => chalk_ir::TyKind::BoundVar(idx).intern(&Interner),
110 Ty::InferenceVar(..) => panic!("uncanonicalized infer ty"), 110 Ty::InferenceVar(..) => panic!("uncanonicalized infer ty"),
111 Ty::Dyn(predicates) => { 111 Ty::Dyn(predicates) => {
112 let where_clauses = chalk_ir::QuantifiedWhereClauses::from_iter( 112 let where_clauses = chalk_ir::QuantifiedWhereClauses::from_iter(
@@ -119,7 +119,7 @@ impl ToChalk for Ty {
119 }; 119 };
120 chalk_ir::TyKind::Dyn(bounded_ty).intern(&Interner) 120 chalk_ir::TyKind::Dyn(bounded_ty).intern(&Interner)
121 } 121 }
122 Ty::Opaque(opaque_ty) => { 122 Ty::Alias(AliasTy::Opaque(opaque_ty)) => {
123 let opaque_ty_id = opaque_ty.opaque_ty_id.to_chalk(db); 123 let opaque_ty_id = opaque_ty.opaque_ty_id.to_chalk(db);
124 let substitution = opaque_ty.parameters.to_chalk(db); 124 let substitution = opaque_ty.parameters.to_chalk(db);
125 chalk_ir::TyKind::Alias(chalk_ir::AliasTy::Opaque(chalk_ir::OpaqueTy { 125 chalk_ir::TyKind::Alias(chalk_ir::AliasTy::Opaque(chalk_ir::OpaqueTy {
@@ -146,12 +146,12 @@ impl ToChalk for Ty {
146 let associated_ty = 146 let associated_ty =
147 from_chalk::<TypeAliasAsAssocType, _>(db, proj.associated_ty_id).0; 147 from_chalk::<TypeAliasAsAssocType, _>(db, proj.associated_ty_id).0;
148 let parameters = from_chalk(db, proj.substitution); 148 let parameters = from_chalk(db, proj.substitution);
149 Ty::Projection(ProjectionTy { associated_ty, parameters }) 149 Ty::Alias(AliasTy::Projection(ProjectionTy { associated_ty, parameters }))
150 } 150 }
151 chalk_ir::TyKind::Alias(chalk_ir::AliasTy::Opaque(opaque_ty)) => { 151 chalk_ir::TyKind::Alias(chalk_ir::AliasTy::Opaque(opaque_ty)) => {
152 let impl_trait_id = from_chalk(db, opaque_ty.opaque_ty_id); 152 let impl_trait_id = from_chalk(db, opaque_ty.opaque_ty_id);
153 let parameters = from_chalk(db, opaque_ty.substitution); 153 let parameters = from_chalk(db, opaque_ty.substitution);
154 Ty::Opaque(OpaqueTy { opaque_ty_id: impl_trait_id, parameters }) 154 Ty::Alias(AliasTy::Opaque(OpaqueTy { opaque_ty_id: impl_trait_id, parameters }))
155 } 155 }
156 chalk_ir::TyKind::Function(chalk_ir::FnPointer { 156 chalk_ir::TyKind::Function(chalk_ir::FnPointer {
157 num_binders, 157 num_binders,
@@ -170,7 +170,7 @@ impl ToChalk for Ty {
170 substs, 170 substs,
171 }) 171 })
172 } 172 }
173 chalk_ir::TyKind::BoundVar(idx) => Ty::Bound(idx), 173 chalk_ir::TyKind::BoundVar(idx) => Ty::BoundVar(idx),
174 chalk_ir::TyKind::InferenceVar(_iv, _kind) => Ty::Unknown, 174 chalk_ir::TyKind::InferenceVar(_iv, _kind) => Ty::Unknown,
175 chalk_ir::TyKind::Dyn(where_clauses) => { 175 chalk_ir::TyKind::Dyn(where_clauses) => {
176 assert_eq!(where_clauses.bounds.binders.len(&Interner), 1); 176 assert_eq!(where_clauses.bounds.binders.len(&Interner), 1);
@@ -198,11 +198,11 @@ impl ToChalk for Ty {
198 Ty::Tuple(cardinality, from_chalk(db, subst)) 198 Ty::Tuple(cardinality, from_chalk(db, subst))
199 } 199 }
200 chalk_ir::TyKind::Raw(mutability, ty) => { 200 chalk_ir::TyKind::Raw(mutability, ty) => {
201 Ty::RawPtr(from_chalk(db, mutability), Substs::single(from_chalk(db, ty))) 201 Ty::Raw(mutability, Substs::single(from_chalk(db, ty)))
202 } 202 }
203 chalk_ir::TyKind::Slice(ty) => Ty::Slice(Substs::single(from_chalk(db, ty))), 203 chalk_ir::TyKind::Slice(ty) => Ty::Slice(Substs::single(from_chalk(db, ty))),
204 chalk_ir::TyKind::Ref(mutability, _lifetime, ty) => { 204 chalk_ir::TyKind::Ref(mutability, _lifetime, ty) => {
205 Ty::Ref(from_chalk(db, mutability), Substs::single(from_chalk(db, ty))) 205 Ty::Ref(mutability, Substs::single(from_chalk(db, ty)))
206 } 206 }
207 chalk_ir::TyKind::Str => Ty::Str, 207 chalk_ir::TyKind::Str => Ty::Str,
208 chalk_ir::TyKind::Never => Ty::Never, 208 chalk_ir::TyKind::Never => Ty::Never,
@@ -230,12 +230,12 @@ impl ToChalk for Ty {
230/// fake lifetime here, because Chalks built-in logic may expect it to be there. 230/// fake lifetime here, because Chalks built-in logic may expect it to be there.
231fn ref_to_chalk( 231fn ref_to_chalk(
232 db: &dyn HirDatabase, 232 db: &dyn HirDatabase,
233 mutability: Mutability, 233 mutability: chalk_ir::Mutability,
234 subst: Substs, 234 subst: Substs,
235) -> chalk_ir::Ty<Interner> { 235) -> chalk_ir::Ty<Interner> {
236 let arg = subst[0].clone().to_chalk(db); 236 let arg = subst[0].clone().to_chalk(db);
237 let lifetime = LifetimeData::Static.intern(&Interner); 237 let lifetime = LifetimeData::Static.intern(&Interner);
238 chalk_ir::TyKind::Ref(mutability.to_chalk(db), lifetime, arg).intern(&Interner) 238 chalk_ir::TyKind::Ref(mutability, lifetime, arg).intern(&Interner)
239} 239}
240 240
241/// We currently don't model constants, but Chalk does. So, we have to insert a 241/// We currently don't model constants, but Chalk does. So, we have to insert a
@@ -313,22 +313,6 @@ impl ToChalk for OpaqueTyId {
313 } 313 }
314} 314}
315 315
316impl ToChalk for Mutability {
317 type Chalk = chalk_ir::Mutability;
318 fn to_chalk(self, _db: &dyn HirDatabase) -> Self::Chalk {
319 match self {
320 Mutability::Shared => chalk_ir::Mutability::Not,
321 Mutability::Mut => chalk_ir::Mutability::Mut,
322 }
323 }
324 fn from_chalk(_db: &dyn HirDatabase, chalk: Self::Chalk) -> Self {
325 match chalk {
326 chalk_ir::Mutability::Mut => Mutability::Mut,
327 chalk_ir::Mutability::Not => Mutability::Shared,
328 }
329 }
330}
331
332impl ToChalk for hir_def::ImplId { 316impl ToChalk for hir_def::ImplId {
333 type Chalk = ImplId; 317 type Chalk = ImplId;
334 318
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
3505mod foo {
3506 /// But this should appear
3507 pub mod bar {}
3508}
3509use 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" }
19tt = { path = "../tt", version = "0.0.0" } 19tt = { path = "../tt", version = "0.0.0" }
20test_utils = { path = "../test_utils", version = "0.0.0" } 20test_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 23profile = { path = "../profile" }
24# [dev-dependencies]
25profile = { 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
59We use bors-ng to enforce the [not rocket science](https://graydon2.dreamwidth.org/1597.html) rule. 59We use bors-ng to enforce the [not rocket science](https://graydon2.dreamwidth.org/1597.html) rule.
60 60
61You 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
65Debugging the language server can be tricky. 63Debugging the language server can be tricky.
diff --git a/xtask/Cargo.toml b/xtask/Cargo.toml
index b379797f9..b17dde598 100644
--- a/xtask/Cargo.toml
+++ b/xtask/Cargo.toml
@@ -6,17 +6,14 @@ authors = ["rust-analyzer developers"]
6publish = false 6publish = false
7license = "MIT OR Apache-2.0" 7license = "MIT OR Apache-2.0"
8 8
9[lib]
10doctest = false
11
12[dependencies] 9[dependencies]
13anyhow = "1.0.26" 10anyhow = "1.0.26"
14flate2 = "1.0" 11flate2 = "1.0"
15pico-args = "0.4.0"
16proc-macro2 = "1.0.8" 12proc-macro2 = "1.0.8"
17quote = "1.0.2" 13quote = "1.0.2"
18ungrammar = "=1.11" 14ungrammar = "=1.11"
19walkdir = "2.3.1" 15walkdir = "2.3.1"
20write-json = "0.1.0" 16write-json = "0.1.0"
21xshell = "0.1" 17xshell = "0.1"
18xflags = "0.1.2"
22# Avoid adding more dependencies to this crate 19# Avoid adding more dependencies to this crate
diff --git a/xtask/src/codegen.rs b/xtask/src/codegen.rs
index adea053b6..2f56c5ad0 100644
--- a/xtask/src/codegen.rs
+++ b/xtask/src/codegen.rs
@@ -18,9 +18,9 @@ use std::{
18}; 18};
19use xshell::{cmd, pushenv, read_file, write_file}; 19use xshell::{cmd, pushenv, read_file, write_file};
20 20
21use crate::{ensure_rustfmt, project_root, Result}; 21use crate::{ensure_rustfmt, flags, project_root, Result};
22 22
23pub use self::{ 23pub(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,13 @@ pub use self::{
30}; 30};
31 31
32#[derive(Debug, PartialEq, Eq, Clone, Copy)] 32#[derive(Debug, PartialEq, Eq, Clone, Copy)]
33pub enum Mode { 33pub(crate) enum Mode {
34 Overwrite, 34 Overwrite,
35 Verify, 35 Verify,
36} 36}
37 37
38pub struct CodegenCmd { 38impl flags::Codegen {
39 pub features: bool, 39 pub(crate) fn run(self) -> Result<()> {
40}
41
42impl CodegenCmd {
43 pub fn run(self) -> Result<()> {
44 if self.features { 40 if self.features {
45 generate_lint_completions(Mode::Overwrite)?; 41 generate_lint_completions(Mode::Overwrite)?;
46 } 42 }
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
10pub fn generate_assists_tests(mode: Mode) -> Result<()> { 10pub(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
15pub fn generate_assists_docs(mode: Mode) -> Result<()> { 15pub(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
10pub fn generate_diagnostic_docs(mode: Mode) -> Result<()> { 10pub(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
10pub fn generate_feature_docs(mode: Mode) -> Result<()> { 10pub(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
13pub fn generate_lint_completions(mode: Mode) -> Result<()> { 13pub(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
15pub fn generate_parser_tests(mode: Mode) -> Result<()> { 15pub(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
21pub fn generate_syntax(mode: Mode) -> Result<()> { 21pub(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
12use crate::{date_iso, project_root}; 12use crate::{date_iso, project_root};
13 13
14pub struct DistCmd { 14pub(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
19impl DistCmd { 19impl 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/flags.rs b/xtask/src/flags.rs
new file mode 100644
index 000000000..5710fbdb5
--- /dev/null
+++ b/xtask/src/flags.rs
@@ -0,0 +1,139 @@
1#![allow(unreachable_pub)]
2
3xflags::args_parser! {
4 /// Run custom build command.
5 cmd xtask {
6 default cmd help {
7 /// Print help information.
8 optional -h, --help
9 }
10
11 /// Install rust-analyzer server or editor plugin.
12 cmd install {
13 /// Install only VS Code plugin.
14 optional --client
15 /// One of 'code', 'code-exploration', 'code-insiders', 'codium', or 'code-oss'.
16 optional --code-bin name: String
17
18 /// Install only the language server.
19 optional --server
20 /// Use mimalloc allocator for server
21 optional --mimalloc
22 /// Use jemalloc allocator for server
23 optional --jemalloc
24 }
25
26 cmd codegen {
27 optional --features
28 }
29
30 cmd lint {}
31 cmd fuzz-tests {}
32 cmd pre-cache {}
33
34 cmd release {
35 optional --dry-run
36 }
37 cmd promote {
38 optional --dry-run
39 }
40 cmd dist {
41 optional --nightly
42 optional --client version: String
43 }
44 cmd metrics {
45 optional --dry-run
46 }
47 /// Builds a benchmark version of rust-analyzer and puts it into `./target`.
48 cmd bb
49 required suffix: String
50 {}
51 }
52}
53
54// generated start
55// The following code is generated by `xflags` macro.
56// Run `env XFLAGS_DUMP= cargo build` to regenerate.
57#[derive(Debug)]
58pub struct Xtask {
59 pub subcommand: XtaskCmd,
60}
61
62#[derive(Debug)]
63pub enum XtaskCmd {
64 Help(Help),
65 Install(Install),
66 Codegen(Codegen),
67 Lint(Lint),
68 FuzzTests(FuzzTests),
69 PreCache(PreCache),
70 Release(Release),
71 Promote(Promote),
72 Dist(Dist),
73 Metrics(Metrics),
74 Bb(Bb),
75}
76
77#[derive(Debug)]
78pub struct Help {
79 pub help: bool,
80}
81
82#[derive(Debug)]
83pub struct Install {
84 pub client: bool,
85 pub code_bin: Option<String>,
86 pub server: bool,
87 pub mimalloc: bool,
88 pub jemalloc: bool,
89}
90
91#[derive(Debug)]
92pub struct Codegen {
93 pub features: bool,
94}
95
96#[derive(Debug)]
97pub struct Lint {}
98
99#[derive(Debug)]
100pub struct FuzzTests {}
101
102#[derive(Debug)]
103pub struct PreCache {}
104
105#[derive(Debug)]
106pub struct Release {
107 pub dry_run: bool,
108}
109
110#[derive(Debug)]
111pub struct Promote {
112 pub dry_run: bool,
113}
114
115#[derive(Debug)]
116pub struct Dist {
117 pub nightly: bool,
118 pub client: Option<String>,
119}
120
121#[derive(Debug)]
122pub struct Metrics {
123 pub dry_run: bool,
124}
125
126#[derive(Debug)]
127pub struct Bb {
128 pub suffix: String,
129}
130
131impl Xtask {
132 pub const HELP: &'static str = Self::_HELP;
133
134 pub fn from_env() -> xflags::Result<Self> {
135 let mut p = xflags::rt::Parser::new_from_env();
136 Self::_parse(&mut p)
137 }
138}
139// generated end
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.
9const REQUIRED_RUST_VERSION: u32 = 50; 9const REQUIRED_RUST_VERSION: u32 = 50;
10 10
11pub struct InstallCmd { 11pub(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)]
17pub enum ClientOpt { 17pub(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
26impl ClientOpt { 26impl 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
63pub struct ServerOpt { 63pub(crate) struct ServerOpt {
64 pub malloc: Malloc, 64 pub(crate) malloc: Malloc,
65} 65}
66 66
67pub enum Malloc { 67pub(crate) enum Malloc {
68 System, 68 System,
69 Mimalloc, 69 Mimalloc,
70 Jemalloc, 70 Jemalloc,
71} 71}
72 72
73impl InstallCmd { 73impl 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
5pub mod codegen;
6mod ast_src;
7
8pub mod install;
9pub mod release;
10pub mod dist;
11pub mod pre_commit;
12pub mod metrics;
13pub mod pre_cache;
14
15use std::{
16 env,
17 path::{Path, PathBuf},
18};
19
20use walkdir::{DirEntry, WalkDir};
21use xshell::{cmd, pushd, pushenv};
22
23use crate::codegen::Mode;
24
25pub use anyhow::{bail, Context as _, Result};
26
27pub 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
37pub fn rust_files() -> impl Iterator<Item = PathBuf> {
38 rust_files_in(&project_root().join("crates"))
39}
40
41pub 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
46pub fn rust_files_in(path: &Path) -> impl Iterator<Item = PathBuf> {
47 files_in(path, "rs")
48}
49
50pub 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
62fn 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
73pub 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
92pub 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
109fn date_iso() -> Result<String> {
110 let res = cmd!("date --iso --utc").read()?;
111 Ok(res)
112}
113
114fn is_release_tag(tag: &str) -> bool {
115 tag.len() == "2020-02-24".len() && tag.starts_with(|c: char| c.is_ascii_digit())
116}
117
118fn 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..e419db7a7 100644
--- a/xtask/src/main.rs
+++ b/xtask/src/main.rs
@@ -7,59 +7,44 @@
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`.
10mod flags;
10 11
11use std::env; 12mod codegen;
13mod ast_src;
14#[cfg(test)]
15mod tidy;
12 16
13use anyhow::bail; 17mod install;
14use codegen::CodegenCmd; 18mod release;
15use pico_args::Arguments; 19mod dist;
16use xshell::{cmd, cp, pushd}; 20mod metrics;
17use xtask::{ 21mod pre_cache;
18 codegen::{self, Mode}, 22
23use anyhow::{bail, Result};
24use std::{
25 env,
26 path::{Path, PathBuf},
27};
28use walkdir::{DirEntry, WalkDir};
29use xshell::{cmd, cp, pushd, pushenv};
30
31use crate::{
32 codegen::Mode,
19 dist::DistCmd, 33 dist::DistCmd,
20 install::{InstallCmd, Malloc, ServerOpt}, 34 install::{InstallCmd, Malloc, ServerOpt},
21 metrics::MetricsCmd,
22 pre_cache::PreCacheCmd,
23 pre_commit, project_root,
24 release::{PromoteCmd, ReleaseCmd},
25 run_clippy, run_fuzzer, run_rustfmt, Result,
26}; 35};
27 36
28fn main() -> Result<()> { 37fn 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())?; 38 let _d = pushd(project_root())?;
34 39
35 let mut args = Arguments::from_env(); 40 let flags = flags::Xtask::from_env()?;
36 let subcommand = args.subcommand()?.unwrap_or_default(); 41 match flags.subcommand {
37 42 flags::XtaskCmd::Help(_) => {
38 match subcommand.as_str() { 43 println!("{}", flags::Xtask::HELP);
39 "install" => { 44 return Ok(());
40 if args.contains(["-h", "--help"]) { 45 }
41 eprintln!( 46 flags::XtaskCmd::Install(flags) => {
42 "\ 47 if flags.server && flags.client {
43cargo xtask install
44Install rust-analyzer server or editor plugin.
45
46USAGE:
47 cargo xtask install [FLAGS]
48
49FLAGS:
50 --client[=CLIENT] Install only VS Code plugin.
51 CLIENT is one of 'code', 'code-exploration', 'code-insiders', 'codium', or 'code-oss'
52 --server Install only the language server
53 --mimalloc Use mimalloc allocator for server
54 --jemalloc Use jemalloc allocator for server
55 -h, --help Prints help information
56 "
57 );
58 return Ok(());
59 }
60 let server = args.contains("--server");
61 let client_code = args.contains("--client");
62 if server && client_code {
63 eprintln!( 48 eprintln!(
64 "error: The argument `--server` cannot be used with `--client`\n\n\ 49 "error: The argument `--server` cannot be used with `--client`\n\n\
65 For more information try --help" 50 For more information try --help"
@@ -67,108 +52,146 @@ FLAGS:
67 return Ok(()); 52 return Ok(());
68 } 53 }
69 54
70 let malloc = if args.contains("--mimalloc") { 55 let malloc = if flags.mimalloc {
71 Malloc::Mimalloc 56 Malloc::Mimalloc
72 } else if args.contains("--jemalloc") { 57 } else if flags.jemalloc {
73 Malloc::Jemalloc 58 Malloc::Jemalloc
74 } else { 59 } else {
75 Malloc::System 60 Malloc::System
76 }; 61 };
77 62
78 let client_opt = args.opt_value_from_str("--client")?; 63 let client_bin = flags.code_bin.map(|it| it.parse()).transpose()?;
79
80 finish_args(args)?;
81 64
82 InstallCmd { 65 InstallCmd {
83 client: if server { None } else { Some(client_opt.unwrap_or_default()) }, 66 client: if flags.server { None } else { client_bin },
84 server: if client_code { None } else { Some(ServerOpt { malloc }) }, 67 server: if flags.client { None } else { Some(ServerOpt { malloc }) },
85 } 68 }
86 .run() 69 .run()
87 } 70 }
88 "codegen" => { 71 flags::XtaskCmd::Codegen(cmd) => cmd.run(),
89 let features = args.contains("--features"); 72 flags::XtaskCmd::Lint(_) => run_clippy(),
90 finish_args(args)?; 73 flags::XtaskCmd::FuzzTests(_) => run_fuzzer(),
91 CodegenCmd { features }.run() 74 flags::XtaskCmd::PreCache(cmd) => cmd.run(),
92 } 75 flags::XtaskCmd::Release(cmd) => cmd.run(),
93 "format" => { 76 flags::XtaskCmd::Promote(cmd) => cmd.run(),
94 finish_args(args)?; 77 flags::XtaskCmd::Dist(flags) => {
95 run_rustfmt(Mode::Overwrite) 78 DistCmd { nightly: flags.nightly, client_version: flags.client }.run()
96 }
97 "install-pre-commit-hook" => {
98 finish_args(args)?;
99 pre_commit::install_hook()
100 } 79 }
101 "lint" => { 80 flags::XtaskCmd::Metrics(cmd) => cmd.run(),
102 finish_args(args)?; 81 flags::XtaskCmd::Bb(cmd) => {
103 run_clippy()
104 }
105 "fuzz-tests" => {
106 finish_args(args)?;
107 run_fuzzer()
108 }
109 "pre-cache" => {
110 finish_args(args)?;
111 PreCacheCmd.run()
112 }
113 "release" => {
114 let dry_run = args.contains("--dry-run");
115 finish_args(args)?;
116 ReleaseCmd { dry_run }.run()
117 }
118 "promote" => {
119 let dry_run = args.contains("--dry-run");
120 finish_args(args)?;
121 PromoteCmd { dry_run }.run()
122 }
123 "dist" => {
124 let nightly = args.contains("--nightly");
125 let client_version: Option<String> = args.opt_value_from_str("--client")?;
126 finish_args(args)?;
127 DistCmd { nightly, client_version }.run()
128 }
129 "metrics" => {
130 let dry_run = args.contains("--dry-run");
131 finish_args(args)?;
132 MetricsCmd { dry_run }.run()
133 }
134 "bb" => {
135 let suffix: String = args.free_from_str()?;
136 finish_args(args)?;
137 { 82 {
138 let _d = pushd("./crates/rust-analyzer")?; 83 let _d = pushd("./crates/rust-analyzer")?;
139 cmd!("cargo build --release --features jemalloc").run()?; 84 cmd!("cargo build --release --features jemalloc").run()?;
140 } 85 }
141 cp("./target/release/rust-analyzer", format!("./target/rust-analyzer-{}", suffix))?; 86 cp("./target/release/rust-analyzer", format!("./target/rust-analyzer-{}", cmd.suffix))?;
142 Ok(())
143 }
144 _ => {
145 eprintln!(
146 "\
147cargo xtask
148Run custom build command.
149
150USAGE:
151 cargo xtask <SUBCOMMAND>
152
153SUBCOMMANDS:
154 format
155 install-pre-commit-hook
156 fuzz-tests
157 codegen
158 install
159 lint
160 dist
161 promote
162 bb"
163 );
164 Ok(()) 87 Ok(())
165 } 88 }
166 } 89 }
167} 90}
168 91
169fn finish_args(args: Arguments) -> Result<()> { 92fn project_root() -> PathBuf {
170 if !args.finish().is_empty() { 93 Path::new(
171 bail!("Unused arguments."); 94 &env::var("CARGO_MANIFEST_DIR").unwrap_or_else(|_| env!("CARGO_MANIFEST_DIR").to_owned()),
95 )
96 .ancestors()
97 .nth(1)
98 .unwrap()
99 .to_path_buf()
100}
101
102fn rust_files() -> impl Iterator<Item = PathBuf> {
103 rust_files_in(&project_root().join("crates"))
104}
105
106#[cfg(test)]
107fn cargo_files() -> impl Iterator<Item = PathBuf> {
108 files_in(&project_root(), "toml")
109 .filter(|path| path.file_name().map(|it| it == "Cargo.toml").unwrap_or(false))
110}
111
112fn rust_files_in(path: &Path) -> impl Iterator<Item = PathBuf> {
113 files_in(path, "rs")
114}
115
116fn run_rustfmt(mode: Mode) -> Result<()> {
117 let _dir = pushd(project_root())?;
118 let _e = pushenv("RUSTUP_TOOLCHAIN", "stable");
119 ensure_rustfmt()?;
120 let check = match mode {
121 Mode::Overwrite => &[][..],
122 Mode::Verify => &["--", "--check"],
123 };
124 cmd!("cargo fmt {check...}").run()?;
125 Ok(())
126}
127
128fn ensure_rustfmt() -> Result<()> {
129 let out = cmd!("rustfmt --version").read()?;
130 if !out.contains("stable") {
131 bail!(
132 "Failed to run rustfmt from toolchain 'stable'. \
133 Please run `rustup component add rustfmt --toolchain stable` to install it.",
134 )
135 }
136 Ok(())
137}
138
139fn run_clippy() -> Result<()> {
140 if cmd!("cargo clippy --version").read().is_err() {
141 bail!(
142 "Failed run cargo clippy. \
143 Please run `rustup component add clippy` to install it.",
144 )
145 }
146
147 let allowed_lints = "
148 -A clippy::collapsible_if
149 -A clippy::needless_pass_by_value
150 -A clippy::nonminimal_bool
151 -A clippy::redundant_pattern_matching
152 "
153 .split_ascii_whitespace();
154 cmd!("cargo clippy --all-features --all-targets -- {allowed_lints...}").run()?;
155 Ok(())
156}
157
158fn run_fuzzer() -> Result<()> {
159 let _d = pushd("./crates/syntax")?;
160 let _e = pushenv("RUSTUP_TOOLCHAIN", "nightly");
161 if cmd!("cargo fuzz --help").read().is_err() {
162 cmd!("cargo install cargo-fuzz").run()?;
163 };
164
165 // Expecting nightly rustc
166 let out = cmd!("rustc --version").read()?;
167 if !out.contains("nightly") {
168 bail!("fuzz tests require nightly rustc")
172 } 169 }
170
171 cmd!("cargo fuzz run parser").run()?;
173 Ok(()) 172 Ok(())
174} 173}
174
175fn date_iso() -> Result<String> {
176 let res = cmd!("date --iso --utc").read()?;
177 Ok(res)
178}
179
180fn is_release_tag(tag: &str) -> bool {
181 tag.len() == "2020-02-24".len() && tag.starts_with(|c: char| c.is_ascii_digit())
182}
183
184fn files_in(path: &Path, ext: &'static str) -> impl Iterator<Item = PathBuf> {
185 let iter = WalkDir::new(path);
186 return iter
187 .into_iter()
188 .filter_entry(|e| !is_hidden(e))
189 .map(|e| e.unwrap())
190 .filter(|e| !e.file_type().is_dir())
191 .map(|e| e.into_path())
192 .filter(move |path| path.extension().map(|it| it == ext).unwrap_or(false));
193
194 fn is_hidden(entry: &DirEntry) -> bool {
195 entry.file_name().to_str().map(|s| s.starts_with('.')).unwrap_or(false)
196 }
197}
diff --git a/xtask/src/metrics.rs b/xtask/src/metrics.rs
index 624ad3b7e..eb58b3274 100644
--- a/xtask/src/metrics.rs
+++ b/xtask/src/metrics.rs
@@ -9,14 +9,12 @@ use std::{
9use anyhow::{bail, format_err, Result}; 9use anyhow::{bail, format_err, Result};
10use xshell::{cmd, mkdir_p, pushd, pushenv, read_file, rm_rf}; 10use xshell::{cmd, mkdir_p, pushd, pushenv, read_file, rm_rf};
11 11
12type Unit = String; 12use crate::flags;
13 13
14pub struct MetricsCmd { 14type Unit = String;
15 pub dry_run: bool,
16}
17 15
18impl MetricsCmd { 16impl flags::Metrics {
19 pub fn run(self) -> Result<()> { 17 pub(crate) fn run(self) -> Result<()> {
20 let mut metrics = Metrics::new()?; 18 let mut metrics = Metrics::new()?;
21 if !self.dry_run { 19 if !self.dry_run {
22 rm_rf("./target/release")?; 20 rm_rf("./target/release")?;
diff --git a/xtask/src/pre_cache.rs b/xtask/src/pre_cache.rs
index 569f88f68..b456224fd 100644
--- a/xtask/src/pre_cache.rs
+++ b/xtask/src/pre_cache.rs
@@ -6,12 +6,12 @@ use std::{
6use anyhow::Result; 6use anyhow::Result;
7use xshell::rm_rf; 7use xshell::rm_rf;
8 8
9pub struct PreCacheCmd; 9use crate::flags;
10 10
11impl PreCacheCmd { 11impl flags::PreCache {
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
3use std::{fs, path::PathBuf};
4
5use anyhow::{bail, Result};
6use xshell::cmd;
7
8use 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`).
12pub 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
26pub 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..d8d86fd63 100644
--- a/xtask/src/release.rs
+++ b/xtask/src/release.rs
@@ -2,14 +2,10 @@ use std::fmt::Write;
2 2
3use xshell::{cmd, cp, pushd, read_dir, write_file}; 3use xshell::{cmd, cp, pushd, read_dir, write_file};
4 4
5use crate::{codegen, date_iso, is_release_tag, project_root, Mode, Result}; 5use crate::{codegen, date_iso, flags, is_release_tag, project_root, Mode, Result};
6 6
7pub struct ReleaseCmd { 7impl flags::Release {
8 pub dry_run: bool, 8 pub(crate) fn run(self) -> Result<()> {
9}
10
11impl ReleaseCmd {
12 pub fn run(self) -> Result<()> {
13 if !self.dry_run { 9 if !self.dry_run {
14 cmd!("git switch release").run()?; 10 cmd!("git switch release").run()?;
15 cmd!("git fetch upstream --tags --force").run()?; 11 cmd!("git fetch upstream --tags --force").run()?;
@@ -86,12 +82,8 @@ https://github.com/sponsors/rust-analyzer[GitHub Sponsors].
86 } 82 }
87} 83}
88 84
89pub struct PromoteCmd { 85impl flags::Promote {
90 pub dry_run: bool, 86 pub(crate) fn run(self) -> Result<()> {
91}
92
93impl PromoteCmd {
94 pub fn run(self) -> Result<()> {
95 let _dir = pushd("../rust-rust-analyzer")?; 87 let _dir = pushd("../rust-rust-analyzer")?;
96 cmd!("git switch master").run()?; 88 cmd!("git switch master").run()?;
97 cmd!("git fetch upstream").run()?; 89 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
6use xshell::{cmd, read_file}; 6use xshell::{cmd, read_file};
7use xtask::{ 7
8use 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,