diff options
Diffstat (limited to 'crates')
111 files changed, 4733 insertions, 4169 deletions
diff --git a/crates/base_db/src/fixture.rs b/crates/base_db/src/fixture.rs index 8d4641355..04e2be390 100644 --- a/crates/base_db/src/fixture.rs +++ b/crates/base_db/src/fixture.rs | |||
@@ -1,62 +1,4 @@ | |||
1 | //! Fixtures are strings containing rust source code with optional metadata. | 1 | //! A set of high-level utility fixture methods to use in tests. |
2 | //! A fixture without metadata is parsed into a single source file. | ||
3 | //! Use this to test functionality local to one file. | ||
4 | //! | ||
5 | //! Simple Example: | ||
6 | //! ``` | ||
7 | //! r#" | ||
8 | //! fn main() { | ||
9 | //! println!("Hello World") | ||
10 | //! } | ||
11 | //! "# | ||
12 | //! ``` | ||
13 | //! | ||
14 | //! Metadata can be added to a fixture after a `//-` comment. | ||
15 | //! The basic form is specifying filenames, | ||
16 | //! which is also how to define multiple files in a single test fixture | ||
17 | //! | ||
18 | //! Example using two files in the same crate: | ||
19 | //! ``` | ||
20 | //! " | ||
21 | //! //- /main.rs | ||
22 | //! mod foo; | ||
23 | //! fn main() { | ||
24 | //! foo::bar(); | ||
25 | //! } | ||
26 | //! | ||
27 | //! //- /foo.rs | ||
28 | //! pub fn bar() {} | ||
29 | //! " | ||
30 | //! ``` | ||
31 | //! | ||
32 | //! Example using two crates with one file each, with one crate depending on the other: | ||
33 | //! ``` | ||
34 | //! r#" | ||
35 | //! //- /main.rs crate:a deps:b | ||
36 | //! fn main() { | ||
37 | //! b::foo(); | ||
38 | //! } | ||
39 | //! //- /lib.rs crate:b | ||
40 | //! pub fn b() { | ||
41 | //! println!("Hello World") | ||
42 | //! } | ||
43 | //! "# | ||
44 | //! ``` | ||
45 | //! | ||
46 | //! Metadata allows specifying all settings and variables | ||
47 | //! that are available in a real rust project: | ||
48 | //! - crate names via `crate:cratename` | ||
49 | //! - dependencies via `deps:dep1,dep2` | ||
50 | //! - configuration settings via `cfg:dbg=false,opt_level=2` | ||
51 | //! - environment variables via `env:PATH=/bin,RUST_LOG=debug` | ||
52 | //! | ||
53 | //! Example using all available metadata: | ||
54 | //! ``` | ||
55 | //! " | ||
56 | //! //- /lib.rs crate:foo deps:bar,baz cfg:foo=a,bar=b env:OUTDIR=path/to,OTHER=foo | ||
57 | //! fn insert_source_code_here() {} | ||
58 | //! " | ||
59 | //! ``` | ||
60 | use std::{mem, str::FromStr, sync::Arc}; | 2 | use std::{mem, str::FromStr, sync::Arc}; |
61 | 3 | ||
62 | use cfg::CfgOptions; | 4 | use cfg::CfgOptions; |
diff --git a/crates/hir/src/lib.rs b/crates/hir/src/lib.rs index 003821981..eba46a056 100644 --- a/crates/hir/src/lib.rs +++ b/crates/hir/src/lib.rs | |||
@@ -53,13 +53,14 @@ use hir_def::{ | |||
53 | use hir_expand::{diagnostics::DiagnosticSink, name::name, MacroDefKind}; | 53 | use hir_expand::{diagnostics::DiagnosticSink, name::name, MacroDefKind}; |
54 | use hir_ty::{ | 54 | use hir_ty::{ |
55 | autoderef, could_unify, | 55 | autoderef, could_unify, |
56 | method_resolution::{self, TyFingerprint}, | 56 | method_resolution::{self, def_crates, TyFingerprint}, |
57 | primitive::UintTy, | 57 | primitive::UintTy, |
58 | subst_prefix, | ||
58 | traits::FnTrait, | 59 | traits::FnTrait, |
59 | AliasEq, AliasTy, BoundVar, CallableDefId, CallableSig, Canonical, CanonicalVarKinds, Cast, | 60 | AliasEq, AliasTy, BoundVar, CallableDefId, CallableSig, Canonical, CanonicalVarKinds, Cast, |
60 | DebruijnIndex, InEnvironment, Interner, QuantifiedWhereClause, Scalar, Solution, | 61 | DebruijnIndex, InEnvironment, Interner, QuantifiedWhereClause, Scalar, Solution, Substitution, |
61 | SolutionVariables, Substitution, TraitEnvironment, Ty, TyBuilder, TyDefId, TyKind, | 62 | TraitEnvironment, TraitRefExt, Ty, TyBuilder, TyDefId, TyExt, TyKind, TyVariableKind, |
62 | TyVariableKind, WhereClause, | 63 | WhereClause, |
63 | }; | 64 | }; |
64 | use itertools::Itertools; | 65 | use itertools::Itertools; |
65 | use rustc_hash::FxHashSet; | 66 | use rustc_hash::FxHashSet; |
@@ -1503,7 +1504,7 @@ impl TypeParam { | |||
1503 | let krate = self.id.parent.module(db.upcast()).krate(); | 1504 | let krate = self.id.parent.module(db.upcast()).krate(); |
1504 | let ty = params.get(local_idx)?.clone(); | 1505 | let ty = params.get(local_idx)?.clone(); |
1505 | let subst = TyBuilder::type_params_subst(db, self.id.parent); | 1506 | let subst = TyBuilder::type_params_subst(db, self.id.parent); |
1506 | let ty = ty.substitute(&Interner, &subst.prefix(local_idx)); | 1507 | let ty = ty.substitute(&Interner, &subst_prefix(&subst, local_idx)); |
1507 | Some(Type::new_with_resolver_inner(db, krate, &resolver, ty)) | 1508 | Some(Type::new_with_resolver_inner(db, krate, &resolver, ty)) |
1508 | } | 1509 | } |
1509 | } | 1510 | } |
@@ -1568,7 +1569,7 @@ impl Impl { | |||
1568 | } | 1569 | } |
1569 | 1570 | ||
1570 | pub fn all_for_type(db: &dyn HirDatabase, Type { krate, ty, .. }: Type) -> Vec<Impl> { | 1571 | pub fn all_for_type(db: &dyn HirDatabase, Type { krate, ty, .. }: Type) -> Vec<Impl> { |
1571 | let def_crates = match ty.def_crates(db, krate) { | 1572 | let def_crates = match def_crates(db, &ty, krate) { |
1572 | Some(def_crates) => def_crates, | 1573 | Some(def_crates) => def_crates, |
1573 | None => return Vec::new(), | 1574 | None => return Vec::new(), |
1574 | }; | 1575 | }; |
@@ -1579,11 +1580,24 @@ impl Impl { | |||
1579 | ty.equals_ctor(rref.as_ref().map_or(&self_ty.ty, |it| &it.ty)) | 1580 | ty.equals_ctor(rref.as_ref().map_or(&self_ty.ty, |it| &it.ty)) |
1580 | }; | 1581 | }; |
1581 | 1582 | ||
1583 | let fp = TyFingerprint::for_inherent_impl(&ty); | ||
1584 | let fp = if let Some(fp) = fp { | ||
1585 | fp | ||
1586 | } else { | ||
1587 | return Vec::new(); | ||
1588 | }; | ||
1589 | |||
1582 | let mut all = Vec::new(); | 1590 | let mut all = Vec::new(); |
1583 | def_crates.iter().for_each(|&id| { | 1591 | def_crates.iter().for_each(|&id| { |
1584 | all.extend(db.inherent_impls_in_crate(id).all_impls().map(Self::from).filter(filter)) | 1592 | all.extend( |
1593 | db.inherent_impls_in_crate(id) | ||
1594 | .for_self_ty(&ty) | ||
1595 | .into_iter() | ||
1596 | .cloned() | ||
1597 | .map(Self::from) | ||
1598 | .filter(filter), | ||
1599 | ) | ||
1585 | }); | 1600 | }); |
1586 | let fp = TyFingerprint::for_impl(&ty); | ||
1587 | for id in def_crates | 1601 | for id in def_crates |
1588 | .iter() | 1602 | .iter() |
1589 | .flat_map(|&id| Crate { id }.transitive_reverse_dependencies(db)) | 1603 | .flat_map(|&id| Crate { id }.transitive_reverse_dependencies(db)) |
@@ -1591,13 +1605,12 @@ impl Impl { | |||
1591 | .chain(def_crates.iter().copied()) | 1605 | .chain(def_crates.iter().copied()) |
1592 | .unique() | 1606 | .unique() |
1593 | { | 1607 | { |
1594 | match fp { | 1608 | all.extend( |
1595 | Some(fp) => all.extend( | 1609 | db.trait_impls_in_crate(id) |
1596 | db.trait_impls_in_crate(id).for_self_ty(fp).map(Self::from).filter(filter), | 1610 | .for_self_ty_without_blanket_impls(fp) |
1597 | ), | 1611 | .map(Self::from) |
1598 | None => all | 1612 | .filter(filter), |
1599 | .extend(db.trait_impls_in_crate(id).all_impls().map(Self::from).filter(filter)), | 1613 | ); |
1600 | } | ||
1601 | } | 1614 | } |
1602 | all | 1615 | all |
1603 | } | 1616 | } |
@@ -1790,7 +1803,7 @@ impl Type { | |||
1790 | .build(); | 1803 | .build(); |
1791 | 1804 | ||
1792 | let goal = Canonical { | 1805 | let goal = Canonical { |
1793 | value: hir_ty::InEnvironment::new(self.env.env.clone(), trait_ref.cast(&Interner)), | 1806 | value: hir_ty::InEnvironment::new(&self.env.env, trait_ref.cast(&Interner)), |
1794 | binders: CanonicalVarKinds::empty(&Interner), | 1807 | binders: CanonicalVarKinds::empty(&Interner), |
1795 | }; | 1808 | }; |
1796 | 1809 | ||
@@ -1807,9 +1820,9 @@ impl Type { | |||
1807 | .push(self.ty.clone()) | 1820 | .push(self.ty.clone()) |
1808 | .fill(args.iter().map(|t| t.ty.clone())) | 1821 | .fill(args.iter().map(|t| t.ty.clone())) |
1809 | .build(); | 1822 | .build(); |
1810 | let goal = Canonical::new( | 1823 | let goal = hir_ty::make_canonical( |
1811 | InEnvironment::new( | 1824 | InEnvironment::new( |
1812 | self.env.env.clone(), | 1825 | &self.env.env, |
1813 | AliasEq { | 1826 | AliasEq { |
1814 | alias: AliasTy::Projection(projection), | 1827 | alias: AliasTy::Projection(projection), |
1815 | ty: TyKind::BoundVar(BoundVar::new(DebruijnIndex::INNERMOST, 0)) | 1828 | ty: TyKind::BoundVar(BoundVar::new(DebruijnIndex::INNERMOST, 0)) |
@@ -1821,9 +1834,10 @@ impl Type { | |||
1821 | ); | 1834 | ); |
1822 | 1835 | ||
1823 | match db.trait_solve(self.krate, goal)? { | 1836 | match db.trait_solve(self.krate, goal)? { |
1824 | Solution::Unique(SolutionVariables(subst)) => subst | 1837 | Solution::Unique(s) => s |
1825 | .value | 1838 | .value |
1826 | .interned() | 1839 | .subst |
1840 | .as_slice(&Interner) | ||
1827 | .first() | 1841 | .first() |
1828 | .map(|ty| self.derived(ty.assert_ty_ref(&Interner).clone())), | 1842 | .map(|ty| self.derived(ty.assert_ty_ref(&Interner).clone())), |
1829 | Solution::Ambig(_) => None, | 1843 | Solution::Ambig(_) => None, |
@@ -1887,9 +1901,10 @@ impl Type { | |||
1887 | substs.iter(&Interner).filter_map(|a| a.ty(&Interner)).any(go) | 1901 | substs.iter(&Interner).filter_map(|a| a.ty(&Interner)).any(go) |
1888 | } | 1902 | } |
1889 | 1903 | ||
1890 | TyKind::Array(ty) | TyKind::Slice(ty) | TyKind::Raw(_, ty) | TyKind::Ref(_, ty) => { | 1904 | TyKind::Array(ty, _) |
1891 | go(ty) | 1905 | | TyKind::Slice(ty) |
1892 | } | 1906 | | TyKind::Raw(_, ty) |
1907 | | TyKind::Ref(_, _, ty) => go(ty), | ||
1893 | 1908 | ||
1894 | TyKind::Scalar(_) | 1909 | TyKind::Scalar(_) |
1895 | | TyKind::Str | 1910 | | TyKind::Str |
@@ -1900,7 +1915,9 @@ impl Type { | |||
1900 | | TyKind::Dyn(_) | 1915 | | TyKind::Dyn(_) |
1901 | | TyKind::Function(_) | 1916 | | TyKind::Function(_) |
1902 | | TyKind::Alias(_) | 1917 | | TyKind::Alias(_) |
1903 | | TyKind::Foreign(_) => false, | 1918 | | TyKind::Foreign(_) |
1919 | | TyKind::Generator(..) | ||
1920 | | TyKind::GeneratorWitness(..) => false, | ||
1904 | } | 1921 | } |
1905 | } | 1922 | } |
1906 | } | 1923 | } |
@@ -1953,7 +1970,7 @@ impl Type { | |||
1953 | krate: Crate, | 1970 | krate: Crate, |
1954 | mut callback: impl FnMut(AssocItem) -> Option<T>, | 1971 | mut callback: impl FnMut(AssocItem) -> Option<T>, |
1955 | ) -> Option<T> { | 1972 | ) -> Option<T> { |
1956 | for krate in self.ty.def_crates(db, krate.id)? { | 1973 | for krate in def_crates(db, &self.ty, krate.id)? { |
1957 | let impls = db.inherent_impls_in_crate(krate); | 1974 | let impls = db.inherent_impls_in_crate(krate); |
1958 | 1975 | ||
1959 | for impl_def in impls.for_self_ty(&self.ty) { | 1976 | for impl_def in impls.for_self_ty(&self.ty) { |
@@ -1970,9 +1987,9 @@ impl Type { | |||
1970 | pub fn type_parameters(&self) -> impl Iterator<Item = Type> + '_ { | 1987 | pub fn type_parameters(&self) -> impl Iterator<Item = Type> + '_ { |
1971 | self.ty | 1988 | self.ty |
1972 | .strip_references() | 1989 | .strip_references() |
1973 | .substs() | 1990 | .as_adt() |
1974 | .into_iter() | 1991 | .into_iter() |
1975 | .flat_map(|substs| substs.iter(&Interner)) | 1992 | .flat_map(|(_, substs)| substs.iter(&Interner)) |
1976 | .filter_map(|arg| arg.ty(&Interner).cloned()) | 1993 | .filter_map(|arg| arg.ty(&Interner).cloned()) |
1977 | .map(move |ty| self.derived(ty)) | 1994 | .map(move |ty| self.derived(ty)) |
1978 | } | 1995 | } |
@@ -2113,18 +2130,22 @@ impl Type { | |||
2113 | fn walk_type(db: &dyn HirDatabase, type_: &Type, cb: &mut impl FnMut(Type)) { | 2130 | fn walk_type(db: &dyn HirDatabase, type_: &Type, cb: &mut impl FnMut(Type)) { |
2114 | let ty = type_.ty.strip_references(); | 2131 | let ty = type_.ty.strip_references(); |
2115 | match ty.kind(&Interner) { | 2132 | match ty.kind(&Interner) { |
2116 | TyKind::Adt(..) => { | 2133 | TyKind::Adt(_, substs) => { |
2117 | cb(type_.derived(ty.clone())); | 2134 | cb(type_.derived(ty.clone())); |
2135 | walk_substs(db, type_, &substs, cb); | ||
2118 | } | 2136 | } |
2119 | TyKind::AssociatedType(..) => { | 2137 | TyKind::AssociatedType(_, substs) => { |
2120 | if let Some(_) = ty.associated_type_parent_trait(db) { | 2138 | if let Some(_) = ty.associated_type_parent_trait(db) { |
2121 | cb(type_.derived(ty.clone())); | 2139 | cb(type_.derived(ty.clone())); |
2122 | } | 2140 | } |
2141 | walk_substs(db, type_, &substs, cb); | ||
2123 | } | 2142 | } |
2124 | TyKind::OpaqueType(..) => { | 2143 | TyKind::OpaqueType(_, subst) => { |
2125 | if let Some(bounds) = ty.impl_trait_bounds(db) { | 2144 | if let Some(bounds) = ty.impl_trait_bounds(db) { |
2126 | walk_bounds(db, &type_.derived(ty.clone()), &bounds, cb); | 2145 | walk_bounds(db, &type_.derived(ty.clone()), &bounds, cb); |
2127 | } | 2146 | } |
2147 | |||
2148 | walk_substs(db, type_, subst, cb); | ||
2128 | } | 2149 | } |
2129 | TyKind::Alias(AliasTy::Opaque(opaque_ty)) => { | 2150 | TyKind::Alias(AliasTy::Opaque(opaque_ty)) => { |
2130 | if let Some(bounds) = ty.impl_trait_bounds(db) { | 2151 | if let Some(bounds) = ty.impl_trait_bounds(db) { |
@@ -2147,15 +2168,24 @@ impl Type { | |||
2147 | ); | 2168 | ); |
2148 | } | 2169 | } |
2149 | 2170 | ||
2150 | TyKind::Ref(_, ty) | TyKind::Raw(_, ty) | TyKind::Array(ty) | TyKind::Slice(ty) => { | 2171 | TyKind::Ref(_, _, ty) |
2172 | | TyKind::Raw(_, ty) | ||
2173 | | TyKind::Array(ty, _) | ||
2174 | | TyKind::Slice(ty) => { | ||
2151 | walk_type(db, &type_.derived(ty.clone()), cb); | 2175 | walk_type(db, &type_.derived(ty.clone()), cb); |
2152 | } | 2176 | } |
2153 | 2177 | ||
2178 | TyKind::FnDef(_, substs) | ||
2179 | | TyKind::Tuple(_, substs) | ||
2180 | | TyKind::Closure(.., substs) => { | ||
2181 | walk_substs(db, type_, &substs, cb); | ||
2182 | } | ||
2183 | TyKind::Function(hir_ty::FnPointer { substitution, .. }) => { | ||
2184 | walk_substs(db, type_, &substitution.0, cb); | ||
2185 | } | ||
2186 | |||
2154 | _ => {} | 2187 | _ => {} |
2155 | } | 2188 | } |
2156 | if let Some(substs) = ty.substs() { | ||
2157 | walk_substs(db, type_, &substs, cb); | ||
2158 | } | ||
2159 | } | 2189 | } |
2160 | 2190 | ||
2161 | walk_type(db, self, &mut cb); | 2191 | walk_type(db, self, &mut cb); |
diff --git a/crates/hir/src/source_analyzer.rs b/crates/hir/src/source_analyzer.rs index 4ce1c2080..847d2537d 100644 --- a/crates/hir/src/source_analyzer.rs +++ b/crates/hir/src/source_analyzer.rs | |||
@@ -20,7 +20,7 @@ use hir_def::{ | |||
20 | use hir_expand::{hygiene::Hygiene, name::AsName, HirFileId, InFile}; | 20 | use hir_expand::{hygiene::Hygiene, name::AsName, HirFileId, InFile}; |
21 | use hir_ty::{ | 21 | use hir_ty::{ |
22 | diagnostics::{record_literal_missing_fields, record_pattern_missing_fields}, | 22 | diagnostics::{record_literal_missing_fields, record_pattern_missing_fields}, |
23 | InferenceResult, Interner, Substitution, TyLoweringContext, | 23 | InferenceResult, Interner, Substitution, TyExt, TyLoweringContext, |
24 | }; | 24 | }; |
25 | use syntax::{ | 25 | use syntax::{ |
26 | ast::{self, AstNode}, | 26 | ast::{self, AstNode}, |
@@ -161,14 +161,15 @@ impl SourceAnalyzer { | |||
161 | db: &dyn HirDatabase, | 161 | db: &dyn HirDatabase, |
162 | field: &ast::RecordExprField, | 162 | field: &ast::RecordExprField, |
163 | ) -> Option<(Field, Option<Local>)> { | 163 | ) -> Option<(Field, Option<Local>)> { |
164 | let expr_id = | 164 | let record_expr = ast::RecordExpr::cast(field.syntax().parent().and_then(|p| p.parent())?)?; |
165 | self.body_source_map.as_ref()?.node_field(InFile::new(self.file_id, field))?; | 165 | let expr = ast::Expr::from(record_expr); |
166 | let expr_id = self.body_source_map.as_ref()?.node_expr(InFile::new(self.file_id, &expr))?; | ||
166 | 167 | ||
168 | let local_name = field.field_name()?.as_name(); | ||
167 | let local = if field.name_ref().is_some() { | 169 | let local = if field.name_ref().is_some() { |
168 | None | 170 | None |
169 | } else { | 171 | } else { |
170 | let local_name = field.field_name()?.as_name(); | 172 | let path = ModPath::from_segments(PathKind::Plain, once(local_name.clone())); |
171 | let path = ModPath::from_segments(PathKind::Plain, once(local_name)); | ||
172 | match self.resolver.resolve_path_in_value_ns_fully(db.upcast(), &path) { | 173 | match self.resolver.resolve_path_in_value_ns_fully(db.upcast(), &path) { |
173 | Some(ValueNs::LocalBinding(pat_id)) => { | 174 | Some(ValueNs::LocalBinding(pat_id)) => { |
174 | Some(Local { pat_id, parent: self.resolver.body_owner()? }) | 175 | Some(Local { pat_id, parent: self.resolver.body_owner()? }) |
@@ -176,18 +177,24 @@ impl SourceAnalyzer { | |||
176 | _ => None, | 177 | _ => None, |
177 | } | 178 | } |
178 | }; | 179 | }; |
179 | let struct_field = self.infer.as_ref()?.record_field_resolution(expr_id)?; | 180 | let variant = self.infer.as_ref()?.variant_resolution_for_expr(expr_id)?; |
180 | Some((struct_field.into(), local)) | 181 | let variant_data = variant.variant_data(db.upcast()); |
182 | let field = FieldId { parent: variant, local_id: variant_data.field(&local_name)? }; | ||
183 | Some((field.into(), local)) | ||
181 | } | 184 | } |
182 | 185 | ||
183 | pub(crate) fn resolve_record_pat_field( | 186 | pub(crate) fn resolve_record_pat_field( |
184 | &self, | 187 | &self, |
185 | _db: &dyn HirDatabase, | 188 | db: &dyn HirDatabase, |
186 | field: &ast::RecordPatField, | 189 | field: &ast::RecordPatField, |
187 | ) -> Option<Field> { | 190 | ) -> Option<Field> { |
188 | let pat_id = self.pat_id(&field.pat()?)?; | 191 | let field_name = field.field_name()?.as_name(); |
189 | let struct_field = self.infer.as_ref()?.record_pat_field_resolution(pat_id)?; | 192 | let record_pat = ast::RecordPat::cast(field.syntax().parent().and_then(|p| p.parent())?)?; |
190 | Some(struct_field.into()) | 193 | let pat_id = self.pat_id(&record_pat.into())?; |
194 | let variant = self.infer.as_ref()?.variant_resolution_for_pat(pat_id)?; | ||
195 | let variant_data = variant.variant_data(db.upcast()); | ||
196 | let field = FieldId { parent: variant, local_id: variant_data.field(&field_name)? }; | ||
197 | Some(field.into()) | ||
191 | } | 198 | } |
192 | 199 | ||
193 | pub(crate) fn resolve_macro_call( | 200 | pub(crate) fn resolve_macro_call( |
@@ -299,7 +306,7 @@ impl SourceAnalyzer { | |||
299 | let infer = self.infer.as_ref()?; | 306 | let infer = self.infer.as_ref()?; |
300 | 307 | ||
301 | let expr_id = self.expr_id(db, &literal.clone().into())?; | 308 | let expr_id = self.expr_id(db, &literal.clone().into())?; |
302 | let substs = infer.type_of_expr[expr_id].substs()?; | 309 | let substs = infer.type_of_expr[expr_id].as_adt()?.1; |
303 | 310 | ||
304 | let (variant, missing_fields, _exhaustive) = | 311 | let (variant, missing_fields, _exhaustive) = |
305 | record_literal_missing_fields(db, infer, expr_id, &body[expr_id])?; | 312 | record_literal_missing_fields(db, infer, expr_id, &body[expr_id])?; |
@@ -317,7 +324,7 @@ impl SourceAnalyzer { | |||
317 | let infer = self.infer.as_ref()?; | 324 | let infer = self.infer.as_ref()?; |
318 | 325 | ||
319 | let pat_id = self.pat_id(&pattern.clone().into())?; | 326 | let pat_id = self.pat_id(&pattern.clone().into())?; |
320 | let substs = infer.type_of_pat[pat_id].substs()?; | 327 | let substs = infer.type_of_pat[pat_id].as_adt()?.1; |
321 | 328 | ||
322 | let (variant, missing_fields, _exhaustive) = | 329 | let (variant, missing_fields, _exhaustive) = |
323 | record_pattern_missing_fields(db, infer, pat_id, &body[pat_id])?; | 330 | record_pattern_missing_fields(db, infer, pat_id, &body[pat_id])?; |
diff --git a/crates/hir_def/src/attr.rs b/crates/hir_def/src/attr.rs index ab77d924a..786fad6e1 100644 --- a/crates/hir_def/src/attr.rs +++ b/crates/hir_def/src/attr.rs | |||
@@ -9,14 +9,14 @@ use std::{ | |||
9 | use base_db::CrateId; | 9 | use base_db::CrateId; |
10 | use cfg::{CfgExpr, CfgOptions}; | 10 | use cfg::{CfgExpr, CfgOptions}; |
11 | use either::Either; | 11 | use either::Either; |
12 | use hir_expand::{hygiene::Hygiene, name::AsName, AstId, InFile}; | 12 | use hir_expand::{hygiene::Hygiene, name::AsName, AstId, AttrId, InFile}; |
13 | use itertools::Itertools; | 13 | use itertools::Itertools; |
14 | use la_arena::ArenaMap; | 14 | use la_arena::ArenaMap; |
15 | use mbe::ast_to_token_tree; | 15 | use mbe::ast_to_token_tree; |
16 | use smallvec::{smallvec, SmallVec}; | 16 | use smallvec::{smallvec, SmallVec}; |
17 | use syntax::{ | 17 | use syntax::{ |
18 | ast::{self, AstNode, AttrsOwner}, | 18 | ast::{self, AstNode, AttrsOwner}, |
19 | match_ast, AstToken, SmolStr, SyntaxNode, TextRange, TextSize, | 19 | match_ast, AstPtr, AstToken, SmolStr, SyntaxNode, TextRange, TextSize, |
20 | }; | 20 | }; |
21 | use tt::Subtree; | 21 | use tt::Subtree; |
22 | 22 | ||
@@ -98,13 +98,16 @@ impl RawAttrs { | |||
98 | pub(crate) fn new(owner: &dyn ast::AttrsOwner, hygiene: &Hygiene) -> Self { | 98 | pub(crate) fn new(owner: &dyn ast::AttrsOwner, hygiene: &Hygiene) -> Self { |
99 | let entries = collect_attrs(owner) | 99 | let entries = collect_attrs(owner) |
100 | .enumerate() | 100 | .enumerate() |
101 | .flat_map(|(i, attr)| match attr { | 101 | .flat_map(|(i, attr)| { |
102 | Either::Left(attr) => Attr::from_src(attr, hygiene, i as u32), | 102 | let index = AttrId(i as u32); |
103 | Either::Right(comment) => comment.doc_comment().map(|doc| Attr { | 103 | match attr { |
104 | index: i as u32, | 104 | Either::Left(attr) => Attr::from_src(attr, hygiene, index), |
105 | input: Some(AttrInput::Literal(SmolStr::new(doc))), | 105 | Either::Right(comment) => comment.doc_comment().map(|doc| Attr { |
106 | path: Interned::new(ModPath::from(hir_expand::name!(doc))), | 106 | id: index, |
107 | }), | 107 | input: Some(AttrInput::Literal(SmolStr::new(doc))), |
108 | path: Interned::new(ModPath::from(hir_expand::name!(doc))), | ||
109 | }), | ||
110 | } | ||
108 | }) | 111 | }) |
109 | .collect::<Arc<_>>(); | 112 | .collect::<Arc<_>>(); |
110 | 113 | ||
@@ -161,7 +164,7 @@ impl RawAttrs { | |||
161 | let cfg = parts.next().unwrap(); | 164 | let cfg = parts.next().unwrap(); |
162 | let cfg = Subtree { delimiter: subtree.delimiter, token_trees: cfg.to_vec() }; | 165 | let cfg = Subtree { delimiter: subtree.delimiter, token_trees: cfg.to_vec() }; |
163 | let cfg = CfgExpr::parse(&cfg); | 166 | let cfg = CfgExpr::parse(&cfg); |
164 | let index = attr.index; | 167 | let index = attr.id; |
165 | let attrs = parts.filter(|a| !a.is_empty()).filter_map(|attr| { | 168 | let attrs = parts.filter(|a| !a.is_empty()).filter_map(|attr| { |
166 | let tree = Subtree { delimiter: None, token_trees: attr.to_vec() }; | 169 | let tree = Subtree { delimiter: None, token_trees: attr.to_vec() }; |
167 | let attr = ast::Attr::parse(&format!("#[{}]", tree)).ok()?; | 170 | let attr = ast::Attr::parse(&format!("#[{}]", tree)).ok()?; |
@@ -215,12 +218,11 @@ impl Attrs { | |||
215 | let mut res = ArenaMap::default(); | 218 | let mut res = ArenaMap::default(); |
216 | 219 | ||
217 | for (id, fld) in src.value.iter() { | 220 | for (id, fld) in src.value.iter() { |
218 | let attrs = match fld { | 221 | let owner: &dyn AttrsOwner = match fld { |
219 | Either::Left(_tuple) => Attrs::default(), | 222 | Either::Left(tuple) => tuple, |
220 | Either::Right(record) => { | 223 | Either::Right(record) => record, |
221 | RawAttrs::from_attrs_owner(db, src.with_value(record)).filter(db, krate) | ||
222 | } | ||
223 | }; | 224 | }; |
225 | let attrs = RawAttrs::from_attrs_owner(db, src.with_value(owner)).filter(db, krate); | ||
224 | 226 | ||
225 | res.insert(id, attrs); | 227 | res.insert(id, attrs); |
226 | } | 228 | } |
@@ -404,10 +406,14 @@ impl AttrsWithOwner { | |||
404 | return AttrSourceMap { attrs }; | 406 | return AttrSourceMap { attrs }; |
405 | } | 407 | } |
406 | AttrDefId::FieldId(id) => { | 408 | AttrDefId::FieldId(id) => { |
407 | id.parent.child_source(db).map(|source| match &source[id.local_id] { | 409 | let map = db.fields_attrs_source_map(id.parent); |
408 | Either::Left(field) => ast::AttrsOwnerNode::new(field.clone()), | 410 | let file_id = id.parent.file_id(db); |
409 | Either::Right(field) => ast::AttrsOwnerNode::new(field.clone()), | 411 | let root = db.parse_or_expand(file_id).unwrap(); |
410 | }) | 412 | let owner = match &map[id.local_id] { |
413 | Either::Left(it) => ast::AttrsOwnerNode::new(it.to_node(&root)), | ||
414 | Either::Right(it) => ast::AttrsOwnerNode::new(it.to_node(&root)), | ||
415 | }; | ||
416 | InFile::new(file_id, owner) | ||
411 | } | 417 | } |
412 | AttrDefId::AdtId(adt) => match adt { | 418 | AttrDefId::AdtId(adt) => match adt { |
413 | AdtId::StructId(id) => id.lookup(db).source(db).map(ast::AttrsOwnerNode::new), | 419 | AdtId::StructId(id) => id.lookup(db).source(db).map(ast::AttrsOwnerNode::new), |
@@ -415,10 +421,12 @@ impl AttrsWithOwner { | |||
415 | AdtId::EnumId(id) => id.lookup(db).source(db).map(ast::AttrsOwnerNode::new), | 421 | AdtId::EnumId(id) => id.lookup(db).source(db).map(ast::AttrsOwnerNode::new), |
416 | }, | 422 | }, |
417 | AttrDefId::FunctionId(id) => id.lookup(db).source(db).map(ast::AttrsOwnerNode::new), | 423 | AttrDefId::FunctionId(id) => id.lookup(db).source(db).map(ast::AttrsOwnerNode::new), |
418 | AttrDefId::EnumVariantId(id) => id | 424 | AttrDefId::EnumVariantId(id) => { |
419 | .parent | 425 | let map = db.variants_attrs_source_map(id.parent); |
420 | .child_source(db) | 426 | let file_id = id.parent.lookup(db).id.file_id(); |
421 | .map(|source| ast::AttrsOwnerNode::new(source[id.local_id].clone())), | 427 | let root = db.parse_or_expand(file_id).unwrap(); |
428 | InFile::new(file_id, ast::AttrsOwnerNode::new(map[id.local_id].to_node(&root))) | ||
429 | } | ||
422 | AttrDefId::StaticId(id) => id.lookup(db).source(db).map(ast::AttrsOwnerNode::new), | 430 | AttrDefId::StaticId(id) => id.lookup(db).source(db).map(ast::AttrsOwnerNode::new), |
423 | AttrDefId::ConstId(id) => id.lookup(db).source(db).map(ast::AttrsOwnerNode::new), | 431 | AttrDefId::ConstId(id) => id.lookup(db).source(db).map(ast::AttrsOwnerNode::new), |
424 | AttrDefId::TraitId(id) => id.lookup(db).source(db).map(ast::AttrsOwnerNode::new), | 432 | AttrDefId::TraitId(id) => id.lookup(db).source(db).map(ast::AttrsOwnerNode::new), |
@@ -463,7 +471,7 @@ impl AttrsWithOwner { | |||
463 | ) -> Option<(Documentation, DocsRangeMap)> { | 471 | ) -> Option<(Documentation, DocsRangeMap)> { |
464 | // FIXME: code duplication in `docs` above | 472 | // FIXME: code duplication in `docs` above |
465 | let docs = self.by_key("doc").attrs().flat_map(|attr| match attr.input.as_ref()? { | 473 | let docs = self.by_key("doc").attrs().flat_map(|attr| match attr.input.as_ref()? { |
466 | AttrInput::Literal(s) => Some((s, attr.index)), | 474 | AttrInput::Literal(s) => Some((s, attr.id)), |
467 | AttrInput::TokenTree(_) => None, | 475 | AttrInput::TokenTree(_) => None, |
468 | }); | 476 | }); |
469 | let indent = docs | 477 | let indent = docs |
@@ -555,8 +563,8 @@ impl AttrSourceMap { | |||
555 | /// the attribute represented by `Attr`. | 563 | /// the attribute represented by `Attr`. |
556 | pub fn source_of(&self, attr: &Attr) -> InFile<&Either<ast::Attr, ast::Comment>> { | 564 | pub fn source_of(&self, attr: &Attr) -> InFile<&Either<ast::Attr, ast::Comment>> { |
557 | self.attrs | 565 | self.attrs |
558 | .get(attr.index as usize) | 566 | .get(attr.id.0 as usize) |
559 | .unwrap_or_else(|| panic!("cannot find `Attr` at index {}", attr.index)) | 567 | .unwrap_or_else(|| panic!("cannot find `Attr` at index {:?}", attr.id)) |
560 | .as_ref() | 568 | .as_ref() |
561 | } | 569 | } |
562 | } | 570 | } |
@@ -567,7 +575,7 @@ pub struct DocsRangeMap { | |||
567 | // (docstring-line-range, attr_index, attr-string-range) | 575 | // (docstring-line-range, attr_index, attr-string-range) |
568 | // a mapping from the text range of a line of the [`Documentation`] to the attribute index and | 576 | // a mapping from the text range of a line of the [`Documentation`] to the attribute index and |
569 | // the original (untrimmed) syntax doc line | 577 | // the original (untrimmed) syntax doc line |
570 | mapping: Vec<(TextRange, u32, TextRange)>, | 578 | mapping: Vec<(TextRange, AttrId, TextRange)>, |
571 | } | 579 | } |
572 | 580 | ||
573 | impl DocsRangeMap { | 581 | impl DocsRangeMap { |
@@ -580,7 +588,7 @@ impl DocsRangeMap { | |||
580 | 588 | ||
581 | let relative_range = range - line_docs_range.start(); | 589 | let relative_range = range - line_docs_range.start(); |
582 | 590 | ||
583 | let &InFile { file_id, value: ref source } = &self.source[idx as usize]; | 591 | let &InFile { file_id, value: ref source } = &self.source[idx.0 as usize]; |
584 | match source { | 592 | match source { |
585 | Either::Left(_) => None, // FIXME, figure out a nice way to handle doc attributes here | 593 | Either::Left(_) => None, // FIXME, figure out a nice way to handle doc attributes here |
586 | // as well as for whats done in syntax highlight doc injection | 594 | // as well as for whats done in syntax highlight doc injection |
@@ -601,7 +609,7 @@ impl DocsRangeMap { | |||
601 | 609 | ||
602 | #[derive(Debug, Clone, PartialEq, Eq)] | 610 | #[derive(Debug, Clone, PartialEq, Eq)] |
603 | pub struct Attr { | 611 | pub struct Attr { |
604 | index: u32, | 612 | pub(crate) id: AttrId, |
605 | pub(crate) path: Interned<ModPath>, | 613 | pub(crate) path: Interned<ModPath>, |
606 | pub(crate) input: Option<AttrInput>, | 614 | pub(crate) input: Option<AttrInput>, |
607 | } | 615 | } |
@@ -615,7 +623,7 @@ pub enum AttrInput { | |||
615 | } | 623 | } |
616 | 624 | ||
617 | impl Attr { | 625 | impl Attr { |
618 | fn from_src(ast: ast::Attr, hygiene: &Hygiene, index: u32) -> Option<Attr> { | 626 | fn from_src(ast: ast::Attr, hygiene: &Hygiene, id: AttrId) -> Option<Attr> { |
619 | let path = Interned::new(ModPath::from_src(ast.path()?, hygiene)?); | 627 | let path = Interned::new(ModPath::from_src(ast.path()?, hygiene)?); |
620 | let input = if let Some(ast::Expr::Literal(lit)) = ast.expr() { | 628 | let input = if let Some(ast::Expr::Literal(lit)) = ast.expr() { |
621 | let value = match lit.kind() { | 629 | let value = match lit.kind() { |
@@ -628,7 +636,7 @@ impl Attr { | |||
628 | } else { | 636 | } else { |
629 | None | 637 | None |
630 | }; | 638 | }; |
631 | Some(Attr { index, path, input }) | 639 | Some(Attr { id, path, input }) |
632 | } | 640 | } |
633 | 641 | ||
634 | /// Parses this attribute as a `#[derive]`, returns an iterator that yields all contained paths | 642 | /// Parses this attribute as a `#[derive]`, returns an iterator that yields all contained paths |
@@ -743,7 +751,38 @@ fn collect_attrs( | |||
743 | .chain(inner_docs.into_iter().flatten()) | 751 | .chain(inner_docs.into_iter().flatten()) |
744 | .map(|docs_text| (docs_text.syntax().text_range().start(), Either::Right(docs_text))); | 752 | .map(|docs_text| (docs_text.syntax().text_range().start(), Either::Right(docs_text))); |
745 | // sort here by syntax node offset because the source can have doc attributes and doc strings be interleaved | 753 | // sort here by syntax node offset because the source can have doc attributes and doc strings be interleaved |
746 | let attrs: Vec<_> = docs.chain(attrs).sorted_by_key(|&(offset, _)| offset).collect(); | 754 | docs.chain(attrs).sorted_by_key(|&(offset, _)| offset).map(|(_, attr)| attr) |
755 | } | ||
756 | |||
757 | pub(crate) fn variants_attrs_source_map( | ||
758 | db: &dyn DefDatabase, | ||
759 | def: EnumId, | ||
760 | ) -> Arc<ArenaMap<LocalEnumVariantId, AstPtr<ast::Variant>>> { | ||
761 | let mut res = ArenaMap::default(); | ||
762 | let child_source = def.child_source(db); | ||
763 | |||
764 | for (idx, variant) in child_source.value.iter() { | ||
765 | res.insert(idx, AstPtr::new(variant)); | ||
766 | } | ||
767 | |||
768 | Arc::new(res) | ||
769 | } | ||
770 | |||
771 | pub(crate) fn fields_attrs_source_map( | ||
772 | db: &dyn DefDatabase, | ||
773 | def: VariantId, | ||
774 | ) -> Arc<ArenaMap<LocalFieldId, Either<AstPtr<ast::TupleField>, AstPtr<ast::RecordField>>>> { | ||
775 | let mut res = ArenaMap::default(); | ||
776 | let child_source = def.child_source(db); | ||
777 | |||
778 | for (idx, variant) in child_source.value.iter() { | ||
779 | res.insert( | ||
780 | idx, | ||
781 | variant | ||
782 | .as_ref() | ||
783 | .either(|l| Either::Left(AstPtr::new(l)), |r| Either::Right(AstPtr::new(r))), | ||
784 | ); | ||
785 | } | ||
747 | 786 | ||
748 | attrs.into_iter().map(|(_, attr)| attr) | 787 | Arc::new(res) |
749 | } | 788 | } |
diff --git a/crates/hir_def/src/body/lower.rs b/crates/hir_def/src/body/lower.rs index 63e89a1f4..ed07d6928 100644 --- a/crates/hir_def/src/body/lower.rs +++ b/crates/hir_def/src/body/lower.rs | |||
@@ -30,6 +30,7 @@ use crate::{ | |||
30 | LabelId, Literal, LogicOp, MatchArm, Ordering, Pat, PatId, RecordFieldPat, RecordLitField, | 30 | LabelId, Literal, LogicOp, MatchArm, Ordering, Pat, PatId, RecordFieldPat, RecordLitField, |
31 | Statement, | 31 | Statement, |
32 | }, | 32 | }, |
33 | intern::Interned, | ||
33 | item_scope::BuiltinShadowMode, | 34 | item_scope::BuiltinShadowMode, |
34 | path::{GenericArgs, Path}, | 35 | path::{GenericArgs, Path}, |
35 | type_ref::{Mutability, Rawness, TypeRef}, | 36 | type_ref::{Mutability, Rawness, TypeRef}, |
@@ -322,8 +323,10 @@ impl ExprCollector<'_> { | |||
322 | Vec::new() | 323 | Vec::new() |
323 | }; | 324 | }; |
324 | let method_name = e.name_ref().map(|nr| nr.as_name()).unwrap_or_else(Name::missing); | 325 | let method_name = e.name_ref().map(|nr| nr.as_name()).unwrap_or_else(Name::missing); |
325 | let generic_args = | 326 | let generic_args = e |
326 | e.generic_arg_list().and_then(|it| GenericArgs::from_ast(&self.ctx(), it)); | 327 | .generic_arg_list() |
328 | .and_then(|it| GenericArgs::from_ast(&self.ctx(), it)) | ||
329 | .map(Box::new); | ||
327 | self.alloc_expr( | 330 | self.alloc_expr( |
328 | Expr::MethodCall { receiver, method_name, args, generic_args }, | 331 | Expr::MethodCall { receiver, method_name, args, generic_args }, |
329 | syntax_ptr, | 332 | syntax_ptr, |
@@ -385,7 +388,7 @@ impl ExprCollector<'_> { | |||
385 | self.alloc_expr(Expr::Yield { expr }, syntax_ptr) | 388 | self.alloc_expr(Expr::Yield { expr }, syntax_ptr) |
386 | } | 389 | } |
387 | ast::Expr::RecordExpr(e) => { | 390 | ast::Expr::RecordExpr(e) => { |
388 | let path = e.path().and_then(|path| self.expander.parse_path(path)); | 391 | let path = e.path().and_then(|path| self.expander.parse_path(path)).map(Box::new); |
389 | let record_lit = if let Some(nfl) = e.record_expr_field_list() { | 392 | let record_lit = if let Some(nfl) = e.record_expr_field_list() { |
390 | let fields = nfl | 393 | let fields = nfl |
391 | .fields() | 394 | .fields() |
@@ -430,7 +433,7 @@ impl ExprCollector<'_> { | |||
430 | } | 433 | } |
431 | ast::Expr::CastExpr(e) => { | 434 | ast::Expr::CastExpr(e) => { |
432 | let expr = self.collect_expr_opt(e.expr()); | 435 | let expr = self.collect_expr_opt(e.expr()); |
433 | let type_ref = TypeRef::from_ast_opt(&self.ctx(), e.ty()); | 436 | let type_ref = Interned::new(TypeRef::from_ast_opt(&self.ctx(), e.ty())); |
434 | self.alloc_expr(Expr::Cast { expr, type_ref }, syntax_ptr) | 437 | self.alloc_expr(Expr::Cast { expr, type_ref }, syntax_ptr) |
435 | } | 438 | } |
436 | ast::Expr::RefExpr(e) => { | 439 | ast::Expr::RefExpr(e) => { |
@@ -464,13 +467,16 @@ impl ExprCollector<'_> { | |||
464 | if let Some(pl) = e.param_list() { | 467 | if let Some(pl) = e.param_list() { |
465 | for param in pl.params() { | 468 | for param in pl.params() { |
466 | let pat = self.collect_pat_opt(param.pat()); | 469 | let pat = self.collect_pat_opt(param.pat()); |
467 | let type_ref = param.ty().map(|it| TypeRef::from_ast(&self.ctx(), it)); | 470 | let type_ref = |
471 | param.ty().map(|it| Interned::new(TypeRef::from_ast(&self.ctx(), it))); | ||
468 | args.push(pat); | 472 | args.push(pat); |
469 | arg_types.push(type_ref); | 473 | arg_types.push(type_ref); |
470 | } | 474 | } |
471 | } | 475 | } |
472 | let ret_type = | 476 | let ret_type = e |
473 | e.ret_type().and_then(|r| r.ty()).map(|it| TypeRef::from_ast(&self.ctx(), it)); | 477 | .ret_type() |
478 | .and_then(|r| r.ty()) | ||
479 | .map(|it| Interned::new(TypeRef::from_ast(&self.ctx(), it))); | ||
474 | let body = self.collect_expr_opt(e.body()); | 480 | let body = self.collect_expr_opt(e.body()); |
475 | self.alloc_expr(Expr::Lambda { args, arg_types, ret_type, body }, syntax_ptr) | 481 | self.alloc_expr(Expr::Lambda { args, arg_types, ret_type, body }, syntax_ptr) |
476 | } | 482 | } |
@@ -525,8 +531,9 @@ impl ExprCollector<'_> { | |||
525 | } | 531 | } |
526 | } | 532 | } |
527 | ast::Expr::MacroCall(e) => { | 533 | ast::Expr::MacroCall(e) => { |
534 | let macro_ptr = AstPtr::new(&e); | ||
528 | let mut ids = vec![]; | 535 | let mut ids = vec![]; |
529 | self.collect_macro_call(e, syntax_ptr.clone(), true, |this, expansion| { | 536 | self.collect_macro_call(e, macro_ptr, true, |this, expansion| { |
530 | ids.push(match expansion { | 537 | ids.push(match expansion { |
531 | Some(it) => this.collect_expr(it), | 538 | Some(it) => this.collect_expr(it), |
532 | None => this.alloc_expr(Expr::Missing, syntax_ptr.clone()), | 539 | None => this.alloc_expr(Expr::Missing, syntax_ptr.clone()), |
@@ -549,7 +556,7 @@ impl ExprCollector<'_> { | |||
549 | fn collect_macro_call<F: FnMut(&mut Self, Option<T>), T: ast::AstNode>( | 556 | fn collect_macro_call<F: FnMut(&mut Self, Option<T>), T: ast::AstNode>( |
550 | &mut self, | 557 | &mut self, |
551 | e: ast::MacroCall, | 558 | e: ast::MacroCall, |
552 | syntax_ptr: AstPtr<ast::Expr>, | 559 | syntax_ptr: AstPtr<ast::MacroCall>, |
553 | is_error_recoverable: bool, | 560 | is_error_recoverable: bool, |
554 | mut collector: F, | 561 | mut collector: F, |
555 | ) { | 562 | ) { |
@@ -625,7 +632,8 @@ impl ExprCollector<'_> { | |||
625 | return; | 632 | return; |
626 | } | 633 | } |
627 | let pat = self.collect_pat_opt(stmt.pat()); | 634 | let pat = self.collect_pat_opt(stmt.pat()); |
628 | let type_ref = stmt.ty().map(|it| TypeRef::from_ast(&self.ctx(), it)); | 635 | let type_ref = |
636 | stmt.ty().map(|it| Interned::new(TypeRef::from_ast(&self.ctx(), it))); | ||
629 | let initializer = stmt.initializer().map(|e| self.collect_expr(e)); | 637 | let initializer = stmt.initializer().map(|e| self.collect_expr(e)); |
630 | self.statements_in_scope.push(Statement::Let { pat, type_ref, initializer }); | 638 | self.statements_in_scope.push(Statement::Let { pat, type_ref, initializer }); |
631 | } | 639 | } |
@@ -636,10 +644,14 @@ impl ExprCollector<'_> { | |||
636 | 644 | ||
637 | // Note that macro could be expended to multiple statements | 645 | // Note that macro could be expended to multiple statements |
638 | if let Some(ast::Expr::MacroCall(m)) = stmt.expr() { | 646 | if let Some(ast::Expr::MacroCall(m)) = stmt.expr() { |
647 | let macro_ptr = AstPtr::new(&m); | ||
639 | let syntax_ptr = AstPtr::new(&stmt.expr().unwrap()); | 648 | let syntax_ptr = AstPtr::new(&stmt.expr().unwrap()); |
640 | 649 | ||
641 | self.collect_macro_call(m, syntax_ptr.clone(), false, |this, expansion| { | 650 | self.collect_macro_call( |
642 | match expansion { | 651 | m, |
652 | macro_ptr, | ||
653 | false, | ||
654 | |this, expansion| match expansion { | ||
643 | Some(expansion) => { | 655 | Some(expansion) => { |
644 | let statements: ast::MacroStmts = expansion; | 656 | let statements: ast::MacroStmts = expansion; |
645 | 657 | ||
@@ -653,8 +665,8 @@ impl ExprCollector<'_> { | |||
653 | let expr = this.alloc_expr(Expr::Missing, syntax_ptr.clone()); | 665 | let expr = this.alloc_expr(Expr::Missing, syntax_ptr.clone()); |
654 | this.statements_in_scope.push(Statement::Expr(expr)); | 666 | this.statements_in_scope.push(Statement::Expr(expr)); |
655 | } | 667 | } |
656 | } | 668 | }, |
657 | }); | 669 | ); |
658 | } else { | 670 | } else { |
659 | let expr = self.collect_expr_opt(stmt.expr()); | 671 | let expr = self.collect_expr_opt(stmt.expr()); |
660 | self.statements_in_scope.push(Statement::Expr(expr)); | 672 | self.statements_in_scope.push(Statement::Expr(expr)); |
@@ -755,7 +767,7 @@ impl ExprCollector<'_> { | |||
755 | } | 767 | } |
756 | } | 768 | } |
757 | ast::Pat::TupleStructPat(p) => { | 769 | ast::Pat::TupleStructPat(p) => { |
758 | let path = p.path().and_then(|path| self.expander.parse_path(path)); | 770 | let path = p.path().and_then(|path| self.expander.parse_path(path)).map(Box::new); |
759 | let (args, ellipsis) = self.collect_tuple_pat(p.fields()); | 771 | let (args, ellipsis) = self.collect_tuple_pat(p.fields()); |
760 | Pat::TupleStruct { path, args, ellipsis } | 772 | Pat::TupleStruct { path, args, ellipsis } |
761 | } | 773 | } |
@@ -765,7 +777,7 @@ impl ExprCollector<'_> { | |||
765 | Pat::Ref { pat, mutability } | 777 | Pat::Ref { pat, mutability } |
766 | } | 778 | } |
767 | ast::Pat::PathPat(p) => { | 779 | ast::Pat::PathPat(p) => { |
768 | let path = p.path().and_then(|path| self.expander.parse_path(path)); | 780 | let path = p.path().and_then(|path| self.expander.parse_path(path)).map(Box::new); |
769 | path.map(Pat::Path).unwrap_or(Pat::Missing) | 781 | path.map(Pat::Path).unwrap_or(Pat::Missing) |
770 | } | 782 | } |
771 | ast::Pat::OrPat(p) => { | 783 | ast::Pat::OrPat(p) => { |
@@ -779,7 +791,7 @@ impl ExprCollector<'_> { | |||
779 | } | 791 | } |
780 | ast::Pat::WildcardPat(_) => Pat::Wild, | 792 | ast::Pat::WildcardPat(_) => Pat::Wild, |
781 | ast::Pat::RecordPat(p) => { | 793 | ast::Pat::RecordPat(p) => { |
782 | let path = p.path().and_then(|path| self.expander.parse_path(path)); | 794 | let path = p.path().and_then(|path| self.expander.parse_path(path)).map(Box::new); |
783 | let args: Vec<_> = p | 795 | let args: Vec<_> = p |
784 | .record_pat_field_list() | 796 | .record_pat_field_list() |
785 | .expect("every struct should have a field list") | 797 | .expect("every struct should have a field list") |
@@ -841,8 +853,23 @@ impl ExprCollector<'_> { | |||
841 | Pat::Missing | 853 | Pat::Missing |
842 | } | 854 | } |
843 | } | 855 | } |
856 | ast::Pat::MacroPat(mac) => match mac.macro_call() { | ||
857 | Some(call) => { | ||
858 | let macro_ptr = AstPtr::new(&call); | ||
859 | let mut pat = None; | ||
860 | self.collect_macro_call(call, macro_ptr, true, |this, expanded_pat| { | ||
861 | pat = Some(this.collect_pat_opt(expanded_pat)); | ||
862 | }); | ||
863 | |||
864 | match pat { | ||
865 | Some(pat) => return pat, | ||
866 | None => Pat::Missing, | ||
867 | } | ||
868 | } | ||
869 | None => Pat::Missing, | ||
870 | }, | ||
844 | // FIXME: implement | 871 | // FIXME: implement |
845 | ast::Pat::RangePat(_) | ast::Pat::MacroPat(_) => Pat::Missing, | 872 | ast::Pat::RangePat(_) => Pat::Missing, |
846 | }; | 873 | }; |
847 | let ptr = AstPtr::new(&pat); | 874 | let ptr = AstPtr::new(&pat); |
848 | self.alloc_pat(pattern, Either::Left(ptr)) | 875 | self.alloc_pat(pattern, Either::Left(ptr)) |
diff --git a/crates/hir_def/src/body/tests.rs b/crates/hir_def/src/body/tests.rs index faa133297..c1d3e998f 100644 --- a/crates/hir_def/src/body/tests.rs +++ b/crates/hir_def/src/body/tests.rs | |||
@@ -143,7 +143,7 @@ fn f() { | |||
143 | //^^^^^^^^^^^^^ could not convert tokens | 143 | //^^^^^^^^^^^^^ could not convert tokens |
144 | 144 | ||
145 | env!("OUT_DIR"); | 145 | env!("OUT_DIR"); |
146 | //^^^^^^^^^^^^^^^ `OUT_DIR` not set, enable "load out dirs from check" to fix | 146 | //^^^^^^^^^^^^^^^ `OUT_DIR` not set, enable "run build scripts" to fix |
147 | 147 | ||
148 | compile_error!("compile_error works"); | 148 | compile_error!("compile_error works"); |
149 | //^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ compile_error works | 149 | //^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ compile_error works |
diff --git a/crates/hir_def/src/child_by_source.rs b/crates/hir_def/src/child_by_source.rs index f40a7f80d..f2e809ca9 100644 --- a/crates/hir_def/src/child_by_source.rs +++ b/crates/hir_def/src/child_by_source.rs | |||
@@ -80,6 +80,10 @@ impl ChildBySource for ModuleId { | |||
80 | impl ChildBySource for ItemScope { | 80 | impl ChildBySource for ItemScope { |
81 | fn child_by_source_to(&self, db: &dyn DefDatabase, res: &mut DynMap) { | 81 | fn child_by_source_to(&self, db: &dyn DefDatabase, res: &mut DynMap) { |
82 | self.declarations().for_each(|item| add_module_def(db, res, item)); | 82 | self.declarations().for_each(|item| add_module_def(db, res, item)); |
83 | self.unnamed_consts().for_each(|konst| { | ||
84 | let src = konst.lookup(db).source(db); | ||
85 | res[keys::CONST].insert(src, konst); | ||
86 | }); | ||
83 | self.impls().for_each(|imp| add_impl(db, res, imp)); | 87 | self.impls().for_each(|imp| add_impl(db, res, imp)); |
84 | 88 | ||
85 | fn add_module_def(db: &dyn DefDatabase, map: &mut DynMap, item: ModuleDefId) { | 89 | fn add_module_def(db: &dyn DefDatabase, map: &mut DynMap, item: ModuleDefId) { |
diff --git a/crates/hir_def/src/db.rs b/crates/hir_def/src/db.rs index 9b7a213a1..7eadc8e0d 100644 --- a/crates/hir_def/src/db.rs +++ b/crates/hir_def/src/db.rs | |||
@@ -2,9 +2,10 @@ | |||
2 | use std::sync::Arc; | 2 | use std::sync::Arc; |
3 | 3 | ||
4 | use base_db::{salsa, CrateId, SourceDatabase, Upcast}; | 4 | use base_db::{salsa, CrateId, SourceDatabase, Upcast}; |
5 | use either::Either; | ||
5 | use hir_expand::{db::AstDatabase, HirFileId}; | 6 | use hir_expand::{db::AstDatabase, HirFileId}; |
6 | use la_arena::ArenaMap; | 7 | use la_arena::ArenaMap; |
7 | use syntax::SmolStr; | 8 | use syntax::{ast, AstPtr, SmolStr}; |
8 | 9 | ||
9 | use crate::{ | 10 | use crate::{ |
10 | adt::{EnumData, StructData}, | 11 | adt::{EnumData, StructData}, |
@@ -122,6 +123,18 @@ pub trait DefDatabase: InternDatabase + AstDatabase + Upcast<dyn AstDatabase> { | |||
122 | #[salsa::invoke(Attrs::fields_attrs_query)] | 123 | #[salsa::invoke(Attrs::fields_attrs_query)] |
123 | fn fields_attrs(&self, def: VariantId) -> Arc<ArenaMap<LocalFieldId, Attrs>>; | 124 | fn fields_attrs(&self, def: VariantId) -> Arc<ArenaMap<LocalFieldId, Attrs>>; |
124 | 125 | ||
126 | #[salsa::invoke(crate::attr::variants_attrs_source_map)] | ||
127 | fn variants_attrs_source_map( | ||
128 | &self, | ||
129 | def: EnumId, | ||
130 | ) -> Arc<ArenaMap<LocalEnumVariantId, AstPtr<ast::Variant>>>; | ||
131 | |||
132 | #[salsa::invoke(crate::attr::fields_attrs_source_map)] | ||
133 | fn fields_attrs_source_map( | ||
134 | &self, | ||
135 | def: VariantId, | ||
136 | ) -> Arc<ArenaMap<LocalFieldId, Either<AstPtr<ast::TupleField>, AstPtr<ast::RecordField>>>>; | ||
137 | |||
125 | #[salsa::invoke(AttrsWithOwner::attrs_query)] | 138 | #[salsa::invoke(AttrsWithOwner::attrs_query)] |
126 | fn attrs(&self, def: AttrDefId) -> AttrsWithOwner; | 139 | fn attrs(&self, def: AttrDefId) -> AttrsWithOwner; |
127 | 140 | ||
diff --git a/crates/hir_def/src/expr.rs b/crates/hir_def/src/expr.rs index 6c7376fad..b4ad984bd 100644 --- a/crates/hir_def/src/expr.rs +++ b/crates/hir_def/src/expr.rs | |||
@@ -18,6 +18,7 @@ use syntax::ast::RangeOp; | |||
18 | 18 | ||
19 | use crate::{ | 19 | use crate::{ |
20 | builtin_type::{BuiltinFloat, BuiltinInt, BuiltinUint}, | 20 | builtin_type::{BuiltinFloat, BuiltinInt, BuiltinUint}, |
21 | intern::Interned, | ||
21 | path::{GenericArgs, Path}, | 22 | path::{GenericArgs, Path}, |
22 | type_ref::{Mutability, Rawness, TypeRef}, | 23 | type_ref::{Mutability, Rawness, TypeRef}, |
23 | BlockId, | 24 | BlockId, |
@@ -86,7 +87,7 @@ pub enum Expr { | |||
86 | receiver: ExprId, | 87 | receiver: ExprId, |
87 | method_name: Name, | 88 | method_name: Name, |
88 | args: Vec<ExprId>, | 89 | args: Vec<ExprId>, |
89 | generic_args: Option<GenericArgs>, | 90 | generic_args: Option<Box<GenericArgs>>, |
90 | }, | 91 | }, |
91 | Match { | 92 | Match { |
92 | expr: ExprId, | 93 | expr: ExprId, |
@@ -106,7 +107,7 @@ pub enum Expr { | |||
106 | expr: Option<ExprId>, | 107 | expr: Option<ExprId>, |
107 | }, | 108 | }, |
108 | RecordLit { | 109 | RecordLit { |
109 | path: Option<Path>, | 110 | path: Option<Box<Path>>, |
110 | fields: Vec<RecordLitField>, | 111 | fields: Vec<RecordLitField>, |
111 | spread: Option<ExprId>, | 112 | spread: Option<ExprId>, |
112 | }, | 113 | }, |
@@ -131,7 +132,7 @@ pub enum Expr { | |||
131 | }, | 132 | }, |
132 | Cast { | 133 | Cast { |
133 | expr: ExprId, | 134 | expr: ExprId, |
134 | type_ref: TypeRef, | 135 | type_ref: Interned<TypeRef>, |
135 | }, | 136 | }, |
136 | Ref { | 137 | Ref { |
137 | expr: ExprId, | 138 | expr: ExprId, |
@@ -161,8 +162,8 @@ pub enum Expr { | |||
161 | }, | 162 | }, |
162 | Lambda { | 163 | Lambda { |
163 | args: Vec<PatId>, | 164 | args: Vec<PatId>, |
164 | arg_types: Vec<Option<TypeRef>>, | 165 | arg_types: Vec<Option<Interned<TypeRef>>>, |
165 | ret_type: Option<TypeRef>, | 166 | ret_type: Option<Interned<TypeRef>>, |
166 | body: ExprId, | 167 | body: ExprId, |
167 | }, | 168 | }, |
168 | Tuple { | 169 | Tuple { |
@@ -240,7 +241,7 @@ pub struct RecordLitField { | |||
240 | 241 | ||
241 | #[derive(Debug, Clone, Eq, PartialEq)] | 242 | #[derive(Debug, Clone, Eq, PartialEq)] |
242 | pub enum Statement { | 243 | pub enum Statement { |
243 | Let { pat: PatId, type_ref: Option<TypeRef>, initializer: Option<ExprId> }, | 244 | Let { pat: PatId, type_ref: Option<Interned<TypeRef>>, initializer: Option<ExprId> }, |
244 | Expr(ExprId), | 245 | Expr(ExprId), |
245 | } | 246 | } |
246 | 247 | ||
@@ -412,13 +413,13 @@ pub enum Pat { | |||
412 | Wild, | 413 | Wild, |
413 | Tuple { args: Vec<PatId>, ellipsis: Option<usize> }, | 414 | Tuple { args: Vec<PatId>, ellipsis: Option<usize> }, |
414 | Or(Vec<PatId>), | 415 | Or(Vec<PatId>), |
415 | Record { path: Option<Path>, args: Vec<RecordFieldPat>, ellipsis: bool }, | 416 | Record { path: Option<Box<Path>>, args: Vec<RecordFieldPat>, ellipsis: bool }, |
416 | Range { start: ExprId, end: ExprId }, | 417 | Range { start: ExprId, end: ExprId }, |
417 | Slice { prefix: Vec<PatId>, slice: Option<PatId>, suffix: Vec<PatId> }, | 418 | Slice { prefix: Vec<PatId>, slice: Option<PatId>, suffix: Vec<PatId> }, |
418 | Path(Path), | 419 | Path(Box<Path>), |
419 | Lit(ExprId), | 420 | Lit(ExprId), |
420 | Bind { mode: BindingAnnotation, name: Name, subpat: Option<PatId> }, | 421 | Bind { mode: BindingAnnotation, name: Name, subpat: Option<PatId> }, |
421 | TupleStruct { path: Option<Path>, args: Vec<PatId>, ellipsis: Option<usize> }, | 422 | TupleStruct { path: Option<Box<Path>>, args: Vec<PatId>, ellipsis: Option<usize> }, |
422 | Ref { pat: PatId, mutability: Mutability }, | 423 | Ref { pat: PatId, mutability: Mutability }, |
423 | Box { inner: PatId }, | 424 | Box { inner: PatId }, |
424 | ConstBlock(ExprId), | 425 | ConstBlock(ExprId), |
diff --git a/crates/hir_def/src/item_scope.rs b/crates/hir_def/src/item_scope.rs index a8ee5eeac..9014468ea 100644 --- a/crates/hir_def/src/item_scope.rs +++ b/crates/hir_def/src/item_scope.rs | |||
@@ -11,7 +11,7 @@ use rustc_hash::{FxHashMap, FxHashSet}; | |||
11 | use stdx::format_to; | 11 | use stdx::format_to; |
12 | 12 | ||
13 | use crate::{ | 13 | use crate::{ |
14 | db::DefDatabase, per_ns::PerNs, visibility::Visibility, AdtId, BuiltinType, ImplId, | 14 | db::DefDatabase, per_ns::PerNs, visibility::Visibility, AdtId, BuiltinType, ConstId, ImplId, |
15 | LocalModuleId, MacroDefId, ModuleDefId, ModuleId, TraitId, | 15 | LocalModuleId, MacroDefId, ModuleDefId, ModuleId, TraitId, |
16 | }; | 16 | }; |
17 | 17 | ||
@@ -37,6 +37,7 @@ pub struct ItemScope { | |||
37 | 37 | ||
38 | defs: Vec<ModuleDefId>, | 38 | defs: Vec<ModuleDefId>, |
39 | impls: Vec<ImplId>, | 39 | impls: Vec<ImplId>, |
40 | unnamed_consts: Vec<ConstId>, | ||
40 | /// Traits imported via `use Trait as _;`. | 41 | /// Traits imported via `use Trait as _;`. |
41 | unnamed_trait_imports: FxHashMap<TraitId, Visibility>, | 42 | unnamed_trait_imports: FxHashMap<TraitId, Visibility>, |
42 | /// Macros visible in current module in legacy textual scope | 43 | /// Macros visible in current module in legacy textual scope |
@@ -106,6 +107,10 @@ impl ItemScope { | |||
106 | .map(|(_, v)| v) | 107 | .map(|(_, v)| v) |
107 | } | 108 | } |
108 | 109 | ||
110 | pub fn unnamed_consts(&self) -> impl Iterator<Item = ConstId> + '_ { | ||
111 | self.unnamed_consts.iter().copied() | ||
112 | } | ||
113 | |||
109 | /// Iterate over all module scoped macros | 114 | /// Iterate over all module scoped macros |
110 | pub(crate) fn macros<'a>(&'a self) -> impl Iterator<Item = (&'a Name, MacroDefId)> + 'a { | 115 | pub(crate) fn macros<'a>(&'a self) -> impl Iterator<Item = (&'a Name, MacroDefId)> + 'a { |
111 | self.entries().filter_map(|(name, def)| def.take_macros().map(|macro_| (name, macro_))) | 116 | self.entries().filter_map(|(name, def)| def.take_macros().map(|macro_| (name, macro_))) |
@@ -156,6 +161,10 @@ impl ItemScope { | |||
156 | self.impls.push(imp) | 161 | self.impls.push(imp) |
157 | } | 162 | } |
158 | 163 | ||
164 | pub(crate) fn define_unnamed_const(&mut self, konst: ConstId) { | ||
165 | self.unnamed_consts.push(konst); | ||
166 | } | ||
167 | |||
159 | pub(crate) fn define_legacy_macro(&mut self, name: Name, mac: MacroDefId) { | 168 | pub(crate) fn define_legacy_macro(&mut self, name: Name, mac: MacroDefId) { |
160 | self.legacy_macros.insert(name, mac); | 169 | self.legacy_macros.insert(name, mac); |
161 | } | 170 | } |
@@ -295,6 +304,7 @@ impl ItemScope { | |||
295 | unresolved, | 304 | unresolved, |
296 | defs, | 305 | defs, |
297 | impls, | 306 | impls, |
307 | unnamed_consts, | ||
298 | unnamed_trait_imports, | 308 | unnamed_trait_imports, |
299 | legacy_macros, | 309 | legacy_macros, |
300 | } = self; | 310 | } = self; |
@@ -304,6 +314,7 @@ impl ItemScope { | |||
304 | unresolved.shrink_to_fit(); | 314 | unresolved.shrink_to_fit(); |
305 | defs.shrink_to_fit(); | 315 | defs.shrink_to_fit(); |
306 | impls.shrink_to_fit(); | 316 | impls.shrink_to_fit(); |
317 | unnamed_consts.shrink_to_fit(); | ||
307 | unnamed_trait_imports.shrink_to_fit(); | 318 | unnamed_trait_imports.shrink_to_fit(); |
308 | legacy_macros.shrink_to_fit(); | 319 | legacy_macros.shrink_to_fit(); |
309 | } | 320 | } |
diff --git a/crates/hir_def/src/item_tree.rs b/crates/hir_def/src/item_tree.rs index 240662486..94e08f835 100644 --- a/crates/hir_def/src/item_tree.rs +++ b/crates/hir_def/src/item_tree.rs | |||
@@ -99,6 +99,11 @@ impl ItemTree { | |||
99 | // items. | 99 | // items. |
100 | ctx.lower_macro_stmts(stmts) | 100 | ctx.lower_macro_stmts(stmts) |
101 | }, | 101 | }, |
102 | ast::Pat(_pat) => { | ||
103 | // FIXME: This occurs because macros in pattern position are treated as inner | ||
104 | // items and expanded during block DefMap computation | ||
105 | return Default::default(); | ||
106 | }, | ||
102 | ast::Expr(e) => { | 107 | ast::Expr(e) => { |
103 | // Macros can expand to expressions. We return an empty item tree in this case, but | 108 | // Macros can expand to expressions. We return an empty item tree in this case, but |
104 | // still need to collect inner items. | 109 | // still need to collect inner items. |
diff --git a/crates/hir_def/src/item_tree/lower.rs b/crates/hir_def/src/item_tree/lower.rs index c5629af24..45b099cf3 100644 --- a/crates/hir_def/src/item_tree/lower.rs +++ b/crates/hir_def/src/item_tree/lower.rs | |||
@@ -189,7 +189,7 @@ impl Ctx { | |||
189 | block_stack.push(self.source_ast_id_map.ast_id(&block)); | 189 | block_stack.push(self.source_ast_id_map.ast_id(&block)); |
190 | }, | 190 | }, |
191 | ast::Item(item) => { | 191 | ast::Item(item) => { |
192 | // FIXME: This triggers for macro calls in expression position | 192 | // FIXME: This triggers for macro calls in expression/pattern/type position |
193 | let mod_items = self.lower_mod_item(&item, true); | 193 | let mod_items = self.lower_mod_item(&item, true); |
194 | let current_block = block_stack.last(); | 194 | let current_block = block_stack.last(); |
195 | if let (Some(mod_items), Some(block)) = (mod_items, current_block) { | 195 | if let (Some(mod_items), Some(block)) = (mod_items, current_block) { |
diff --git a/crates/hir_def/src/lib.rs b/crates/hir_def/src/lib.rs index be9a5e1a0..d69116d51 100644 --- a/crates/hir_def/src/lib.rs +++ b/crates/hir_def/src/lib.rs | |||
@@ -56,12 +56,13 @@ use std::{ | |||
56 | sync::Arc, | 56 | sync::Arc, |
57 | }; | 57 | }; |
58 | 58 | ||
59 | use adt::VariantData; | ||
59 | use base_db::{impl_intern_key, salsa, CrateId}; | 60 | use base_db::{impl_intern_key, salsa, CrateId}; |
60 | use hir_expand::{ | 61 | use hir_expand::{ |
61 | ast_id_map::FileAstId, | 62 | ast_id_map::FileAstId, |
62 | eager::{expand_eager_macro, ErrorEmitted, ErrorSink}, | 63 | eager::{expand_eager_macro, ErrorEmitted, ErrorSink}, |
63 | hygiene::Hygiene, | 64 | hygiene::Hygiene, |
64 | AstId, HirFileId, InFile, MacroCallId, MacroCallKind, MacroDefId, MacroDefKind, | 65 | AstId, AttrId, HirFileId, InFile, MacroCallId, MacroCallKind, MacroDefId, MacroDefKind, |
65 | }; | 66 | }; |
66 | use la_arena::Idx; | 67 | use la_arena::Idx; |
67 | use nameres::DefMap; | 68 | use nameres::DefMap; |
@@ -442,6 +443,26 @@ pub enum VariantId { | |||
442 | } | 443 | } |
443 | impl_from!(EnumVariantId, StructId, UnionId for VariantId); | 444 | impl_from!(EnumVariantId, StructId, UnionId for VariantId); |
444 | 445 | ||
446 | impl VariantId { | ||
447 | pub fn variant_data(self, db: &dyn db::DefDatabase) -> Arc<VariantData> { | ||
448 | match self { | ||
449 | VariantId::StructId(it) => db.struct_data(it).variant_data.clone(), | ||
450 | VariantId::UnionId(it) => db.union_data(it).variant_data.clone(), | ||
451 | VariantId::EnumVariantId(it) => { | ||
452 | db.enum_data(it.parent).variants[it.local_id].variant_data.clone() | ||
453 | } | ||
454 | } | ||
455 | } | ||
456 | |||
457 | pub fn file_id(self, db: &dyn db::DefDatabase) -> HirFileId { | ||
458 | match self { | ||
459 | VariantId::EnumVariantId(it) => it.parent.lookup(db).id.file_id(), | ||
460 | VariantId::StructId(it) => it.lookup(db).id.file_id(), | ||
461 | VariantId::UnionId(it) => it.lookup(db).id.file_id(), | ||
462 | } | ||
463 | } | ||
464 | } | ||
465 | |||
445 | trait Intern { | 466 | trait Intern { |
446 | type ID; | 467 | type ID; |
447 | fn intern(self, db: &dyn db::DefDatabase) -> Self::ID; | 468 | fn intern(self, db: &dyn db::DefDatabase) -> Self::ID; |
@@ -669,13 +690,16 @@ fn macro_call_as_call_id( | |||
669 | ) | 690 | ) |
670 | .map(MacroCallId::from) | 691 | .map(MacroCallId::from) |
671 | } else { | 692 | } else { |
672 | Ok(def.as_lazy_macro(db.upcast(), krate, MacroCallKind::FnLike(call.ast_id)).into()) | 693 | Ok(def |
694 | .as_lazy_macro(db.upcast(), krate, MacroCallKind::FnLike { ast_id: call.ast_id }) | ||
695 | .into()) | ||
673 | }; | 696 | }; |
674 | Ok(res) | 697 | Ok(res) |
675 | } | 698 | } |
676 | 699 | ||
677 | fn derive_macro_as_call_id( | 700 | fn derive_macro_as_call_id( |
678 | item_attr: &AstIdWithPath<ast::Item>, | 701 | item_attr: &AstIdWithPath<ast::Item>, |
702 | derive_attr: AttrId, | ||
679 | db: &dyn db::DefDatabase, | 703 | db: &dyn db::DefDatabase, |
680 | krate: CrateId, | 704 | krate: CrateId, |
681 | resolver: impl Fn(path::ModPath) -> Option<MacroDefId>, | 705 | resolver: impl Fn(path::ModPath) -> Option<MacroDefId>, |
@@ -686,7 +710,11 @@ fn derive_macro_as_call_id( | |||
686 | .as_lazy_macro( | 710 | .as_lazy_macro( |
687 | db.upcast(), | 711 | db.upcast(), |
688 | krate, | 712 | krate, |
689 | MacroCallKind::Derive(item_attr.ast_id, last_segment.to_string()), | 713 | MacroCallKind::Derive { |
714 | ast_id: item_attr.ast_id, | ||
715 | derive_name: last_segment.to_string(), | ||
716 | derive_attr, | ||
717 | }, | ||
690 | ) | 718 | ) |
691 | .into(); | 719 | .into(); |
692 | Ok(res) | 720 | Ok(res) |
diff --git a/crates/hir_def/src/nameres.rs b/crates/hir_def/src/nameres.rs index 7dd68219f..9e181751c 100644 --- a/crates/hir_def/src/nameres.rs +++ b/crates/hir_def/src/nameres.rs | |||
@@ -613,12 +613,12 @@ mod diagnostics { | |||
613 | DiagnosticKind::UnresolvedProcMacro { ast } => { | 613 | DiagnosticKind::UnresolvedProcMacro { ast } => { |
614 | let mut precise_location = None; | 614 | let mut precise_location = None; |
615 | let (file, ast, name) = match ast { | 615 | let (file, ast, name) = match ast { |
616 | MacroCallKind::FnLike(ast) => { | 616 | MacroCallKind::FnLike { ast_id } => { |
617 | let node = ast.to_node(db.upcast()); | 617 | let node = ast_id.to_node(db.upcast()); |
618 | (ast.file_id, SyntaxNodePtr::from(AstPtr::new(&node)), None) | 618 | (ast_id.file_id, SyntaxNodePtr::from(AstPtr::new(&node)), None) |
619 | } | 619 | } |
620 | MacroCallKind::Derive(ast, name) => { | 620 | MacroCallKind::Derive { ast_id, derive_name, .. } => { |
621 | let node = ast.to_node(db.upcast()); | 621 | let node = ast_id.to_node(db.upcast()); |
622 | 622 | ||
623 | // Compute the precise location of the macro name's token in the derive | 623 | // Compute the precise location of the macro name's token in the derive |
624 | // list. | 624 | // list. |
@@ -639,7 +639,7 @@ mod diagnostics { | |||
639 | }); | 639 | }); |
640 | for token in tokens { | 640 | for token in tokens { |
641 | if token.kind() == SyntaxKind::IDENT | 641 | if token.kind() == SyntaxKind::IDENT |
642 | && token.text() == name.as_str() | 642 | && token.text() == derive_name.as_str() |
643 | { | 643 | { |
644 | precise_location = Some(token.text_range()); | 644 | precise_location = Some(token.text_range()); |
645 | break 'outer; | 645 | break 'outer; |
@@ -648,9 +648,9 @@ mod diagnostics { | |||
648 | } | 648 | } |
649 | 649 | ||
650 | ( | 650 | ( |
651 | ast.file_id, | 651 | ast_id.file_id, |
652 | SyntaxNodePtr::from(AstPtr::new(&node)), | 652 | SyntaxNodePtr::from(AstPtr::new(&node)), |
653 | Some(name.clone()), | 653 | Some(derive_name.clone()), |
654 | ) | 654 | ) |
655 | } | 655 | } |
656 | }; | 656 | }; |
@@ -669,13 +669,13 @@ mod diagnostics { | |||
669 | 669 | ||
670 | DiagnosticKind::MacroError { ast, message } => { | 670 | DiagnosticKind::MacroError { ast, message } => { |
671 | let (file, ast) = match ast { | 671 | let (file, ast) = match ast { |
672 | MacroCallKind::FnLike(ast) => { | 672 | MacroCallKind::FnLike { ast_id, .. } => { |
673 | let node = ast.to_node(db.upcast()); | 673 | let node = ast_id.to_node(db.upcast()); |
674 | (ast.file_id, SyntaxNodePtr::from(AstPtr::new(&node))) | 674 | (ast_id.file_id, SyntaxNodePtr::from(AstPtr::new(&node))) |
675 | } | 675 | } |
676 | MacroCallKind::Derive(ast, _) => { | 676 | MacroCallKind::Derive { ast_id, .. } => { |
677 | let node = ast.to_node(db.upcast()); | 677 | let node = ast_id.to_node(db.upcast()); |
678 | (ast.file_id, SyntaxNodePtr::from(AstPtr::new(&node))) | 678 | (ast_id.file_id, SyntaxNodePtr::from(AstPtr::new(&node))) |
679 | } | 679 | } |
680 | }; | 680 | }; |
681 | sink.push(MacroError { file, node: ast, message: message.clone() }); | 681 | sink.push(MacroError { file, node: ast, message: message.clone() }); |
diff --git a/crates/hir_def/src/nameres/collector.rs b/crates/hir_def/src/nameres/collector.rs index f42f92702..fb4ddff5e 100644 --- a/crates/hir_def/src/nameres/collector.rs +++ b/crates/hir_def/src/nameres/collector.rs | |||
@@ -13,7 +13,7 @@ use hir_expand::{ | |||
13 | builtin_macro::find_builtin_macro, | 13 | builtin_macro::find_builtin_macro, |
14 | name::{AsName, Name}, | 14 | name::{AsName, Name}, |
15 | proc_macro::ProcMacroExpander, | 15 | proc_macro::ProcMacroExpander, |
16 | HirFileId, MacroCallId, MacroCallKind, MacroDefId, MacroDefKind, | 16 | AttrId, HirFileId, MacroCallId, MacroCallKind, MacroDefId, MacroDefKind, |
17 | }; | 17 | }; |
18 | use hir_expand::{InFile, MacroCallLoc}; | 18 | use hir_expand::{InFile, MacroCallLoc}; |
19 | use rustc_hash::{FxHashMap, FxHashSet}; | 19 | use rustc_hash::{FxHashMap, FxHashSet}; |
@@ -216,7 +216,7 @@ struct MacroDirective { | |||
216 | #[derive(Clone, Debug, Eq, PartialEq)] | 216 | #[derive(Clone, Debug, Eq, PartialEq)] |
217 | enum MacroDirectiveKind { | 217 | enum MacroDirectiveKind { |
218 | FnLike { ast_id: AstIdWithPath<ast::MacroCall> }, | 218 | FnLike { ast_id: AstIdWithPath<ast::MacroCall> }, |
219 | Derive { ast_id: AstIdWithPath<ast::Item> }, | 219 | Derive { ast_id: AstIdWithPath<ast::Item>, derive_attr: AttrId }, |
220 | } | 220 | } |
221 | 221 | ||
222 | struct DefData<'a> { | 222 | struct DefData<'a> { |
@@ -478,7 +478,7 @@ impl DefCollector<'_> { | |||
478 | self.def_map.edition, | 478 | self.def_map.edition, |
479 | ); | 479 | ); |
480 | 480 | ||
481 | let res = self.def_map.resolve_name_in_extern_prelude(&extern_crate.name); | 481 | let res = self.def_map.resolve_name_in_extern_prelude(self.db, &extern_crate.name); |
482 | 482 | ||
483 | if let Some(ModuleDefId::ModuleId(m)) = res.take_types() { | 483 | if let Some(ModuleDefId::ModuleId(m)) = res.take_types() { |
484 | cov_mark::hit!(macro_rules_from_other_crates_are_visible_with_macro_use); | 484 | cov_mark::hit!(macro_rules_from_other_crates_are_visible_with_macro_use); |
@@ -534,6 +534,7 @@ impl DefCollector<'_> { | |||
534 | log::debug!("resolving import: {:?} ({:?})", import, self.def_map.edition); | 534 | log::debug!("resolving import: {:?} ({:?})", import, self.def_map.edition); |
535 | if import.is_extern_crate { | 535 | if import.is_extern_crate { |
536 | let res = self.def_map.resolve_name_in_extern_prelude( | 536 | let res = self.def_map.resolve_name_in_extern_prelude( |
537 | self.db, | ||
537 | &import | 538 | &import |
538 | .path | 539 | .path |
539 | .as_ident() | 540 | .as_ident() |
@@ -831,12 +832,16 @@ impl DefCollector<'_> { | |||
831 | Err(UnresolvedMacro) | Ok(Err(_)) => {} | 832 | Err(UnresolvedMacro) | Ok(Err(_)) => {} |
832 | } | 833 | } |
833 | } | 834 | } |
834 | MacroDirectiveKind::Derive { ast_id } => { | 835 | MacroDirectiveKind::Derive { ast_id, derive_attr } => { |
835 | match derive_macro_as_call_id(ast_id, self.db, self.def_map.krate, |path| { | 836 | match derive_macro_as_call_id( |
836 | self.resolve_derive_macro(directive.module_id, &path) | 837 | ast_id, |
837 | }) { | 838 | *derive_attr, |
839 | self.db, | ||
840 | self.def_map.krate, | ||
841 | |path| self.resolve_derive_macro(directive.module_id, &path), | ||
842 | ) { | ||
838 | Ok(call_id) => { | 843 | Ok(call_id) => { |
839 | resolved.push((directive.module_id, call_id, 0)); | 844 | resolved.push((directive.module_id, call_id, directive.depth)); |
840 | res = ReachedFixedPoint::No; | 845 | res = ReachedFixedPoint::No; |
841 | return false; | 846 | return false; |
842 | } | 847 | } |
@@ -1163,19 +1168,27 @@ impl ModCollector<'_, '_> { | |||
1163 | } | 1168 | } |
1164 | ModItem::Const(id) => { | 1169 | ModItem::Const(id) => { |
1165 | let it = &self.item_tree[id]; | 1170 | let it = &self.item_tree[id]; |
1166 | 1171 | let const_id = ConstLoc { | |
1167 | if let Some(name) = &it.name { | 1172 | container: module.into(), |
1168 | def = Some(DefData { | 1173 | id: ItemTreeId::new(self.file_id, id), |
1169 | id: ConstLoc { | 1174 | } |
1170 | container: module.into(), | 1175 | .intern(self.def_collector.db); |
1171 | id: ItemTreeId::new(self.file_id, id), | 1176 | |
1172 | } | 1177 | match &it.name { |
1173 | .intern(self.def_collector.db) | 1178 | Some(name) => { |
1174 | .into(), | 1179 | def = Some(DefData { |
1175 | name, | 1180 | id: const_id.into(), |
1176 | visibility: &self.item_tree[it.visibility], | 1181 | name, |
1177 | has_constructor: false, | 1182 | visibility: &self.item_tree[it.visibility], |
1178 | }); | 1183 | has_constructor: false, |
1184 | }); | ||
1185 | } | ||
1186 | None => { | ||
1187 | // const _: T = ...; | ||
1188 | self.def_collector.def_map.modules[self.module_id] | ||
1189 | .scope | ||
1190 | .define_unnamed_const(const_id); | ||
1191 | } | ||
1179 | } | 1192 | } |
1180 | } | 1193 | } |
1181 | ModItem::Static(id) => { | 1194 | ModItem::Static(id) => { |
@@ -1360,7 +1373,7 @@ impl ModCollector<'_, '_> { | |||
1360 | self.def_collector.unexpanded_macros.push(MacroDirective { | 1373 | self.def_collector.unexpanded_macros.push(MacroDirective { |
1361 | module_id: self.module_id, | 1374 | module_id: self.module_id, |
1362 | depth: self.macro_depth + 1, | 1375 | depth: self.macro_depth + 1, |
1363 | kind: MacroDirectiveKind::Derive { ast_id }, | 1376 | kind: MacroDirectiveKind::Derive { ast_id, derive_attr: derive.id }, |
1364 | }); | 1377 | }); |
1365 | } | 1378 | } |
1366 | } | 1379 | } |
@@ -1512,7 +1525,7 @@ impl ModCollector<'_, '_> { | |||
1512 | // Built-in macro failed eager expansion. | 1525 | // Built-in macro failed eager expansion. |
1513 | self.def_collector.def_map.diagnostics.push(DefDiagnostic::macro_error( | 1526 | self.def_collector.def_map.diagnostics.push(DefDiagnostic::macro_error( |
1514 | self.module_id, | 1527 | self.module_id, |
1515 | MacroCallKind::FnLike(ast_id.ast_id), | 1528 | MacroCallKind::FnLike { ast_id: ast_id.ast_id }, |
1516 | error.unwrap().to_string(), | 1529 | error.unwrap().to_string(), |
1517 | )); | 1530 | )); |
1518 | return; | 1531 | return; |
diff --git a/crates/hir_def/src/nameres/path_resolution.rs b/crates/hir_def/src/nameres/path_resolution.rs index 60471937c..ccc9f22eb 100644 --- a/crates/hir_def/src/nameres/path_resolution.rs +++ b/crates/hir_def/src/nameres/path_resolution.rs | |||
@@ -60,12 +60,26 @@ impl ResolvePathResult { | |||
60 | } | 60 | } |
61 | 61 | ||
62 | impl DefMap { | 62 | impl DefMap { |
63 | pub(super) fn resolve_name_in_extern_prelude(&self, name: &Name) -> PerNs { | 63 | pub(super) fn resolve_name_in_extern_prelude( |
64 | &self, | ||
65 | db: &dyn DefDatabase, | ||
66 | name: &Name, | ||
67 | ) -> PerNs { | ||
64 | if name == &name!(self) { | 68 | if name == &name!(self) { |
65 | cov_mark::hit!(extern_crate_self_as); | 69 | cov_mark::hit!(extern_crate_self_as); |
66 | return PerNs::types(self.module_id(self.root).into(), Visibility::Public); | 70 | return PerNs::types(self.module_id(self.root).into(), Visibility::Public); |
67 | } | 71 | } |
68 | self.extern_prelude | 72 | |
73 | let arc; | ||
74 | let root = match self.block { | ||
75 | Some(_) => { | ||
76 | arc = self.crate_root(db).def_map(db); | ||
77 | &*arc | ||
78 | } | ||
79 | None => self, | ||
80 | }; | ||
81 | |||
82 | root.extern_prelude | ||
69 | .get(name) | 83 | .get(name) |
70 | .map_or(PerNs::none(), |&it| PerNs::types(it, Visibility::Public)) | 84 | .map_or(PerNs::none(), |&it| PerNs::types(it, Visibility::Public)) |
71 | } | 85 | } |
@@ -191,7 +205,7 @@ impl DefMap { | |||
191 | None => return ResolvePathResult::empty(ReachedFixedPoint::Yes), | 205 | None => return ResolvePathResult::empty(ReachedFixedPoint::Yes), |
192 | }; | 206 | }; |
193 | log::debug!("resolving {:?} in crate root (+ extern prelude)", segment); | 207 | log::debug!("resolving {:?} in crate root (+ extern prelude)", segment); |
194 | self.resolve_name_in_crate_root_or_extern_prelude(&segment) | 208 | self.resolve_name_in_crate_root_or_extern_prelude(db, &segment) |
195 | } | 209 | } |
196 | PathKind::Plain => { | 210 | PathKind::Plain => { |
197 | let (_, segment) = match segments.next() { | 211 | let (_, segment) = match segments.next() { |
@@ -384,24 +398,31 @@ impl DefMap { | |||
384 | } | 398 | } |
385 | } | 399 | } |
386 | }; | 400 | }; |
387 | // Give precedence to names in outer `DefMap`s over the extern prelude; only check prelude | 401 | let from_extern_prelude = self |
388 | // from the crate DefMap. | 402 | .extern_prelude |
389 | let from_extern_prelude = match self.block { | 403 | .get(name) |
390 | Some(_) => PerNs::none(), | 404 | .map_or(PerNs::none(), |&it| PerNs::types(it, Visibility::Public)); |
391 | None => self | ||
392 | .extern_prelude | ||
393 | .get(name) | ||
394 | .map_or(PerNs::none(), |&it| PerNs::types(it, Visibility::Public)), | ||
395 | }; | ||
396 | 405 | ||
397 | let from_prelude = self.resolve_in_prelude(db, name); | 406 | let from_prelude = self.resolve_in_prelude(db, name); |
398 | 407 | ||
399 | from_legacy_macro.or(from_scope_or_builtin).or(from_extern_prelude).or(from_prelude) | 408 | from_legacy_macro.or(from_scope_or_builtin).or(from_extern_prelude).or(from_prelude) |
400 | } | 409 | } |
401 | 410 | ||
402 | fn resolve_name_in_crate_root_or_extern_prelude(&self, name: &Name) -> PerNs { | 411 | fn resolve_name_in_crate_root_or_extern_prelude( |
403 | let from_crate_root = self[self.root].scope.get(name); | 412 | &self, |
404 | let from_extern_prelude = self.resolve_name_in_extern_prelude(name); | 413 | db: &dyn DefDatabase, |
414 | name: &Name, | ||
415 | ) -> PerNs { | ||
416 | let arc; | ||
417 | let crate_def_map = match self.block { | ||
418 | Some(_) => { | ||
419 | arc = self.crate_root(db).def_map(db); | ||
420 | &arc | ||
421 | } | ||
422 | None => self, | ||
423 | }; | ||
424 | let from_crate_root = crate_def_map[crate_def_map.root].scope.get(name); | ||
425 | let from_extern_prelude = self.resolve_name_in_extern_prelude(db, name); | ||
405 | 426 | ||
406 | from_crate_root.or(from_extern_prelude) | 427 | from_crate_root.or(from_extern_prelude) |
407 | } | 428 | } |
diff --git a/crates/hir_def/src/nameres/tests/diagnostics.rs b/crates/hir_def/src/nameres/tests/diagnostics.rs index fefdadb22..1ac88fc89 100644 --- a/crates/hir_def/src/nameres/tests/diagnostics.rs +++ b/crates/hir_def/src/nameres/tests/diagnostics.rs | |||
@@ -233,7 +233,7 @@ fn good_out_dir_diagnostic() { | |||
233 | macro_rules! concat { () => {} } | 233 | macro_rules! concat { () => {} } |
234 | 234 | ||
235 | include!(concat!(env!("OUT_DIR"), "/out.rs")); | 235 | include!(concat!(env!("OUT_DIR"), "/out.rs")); |
236 | //^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `OUT_DIR` not set, enable "load out dirs from check" to fix | 236 | //^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `OUT_DIR` not set, enable "run build scripts" to fix |
237 | "#, | 237 | "#, |
238 | ); | 238 | ); |
239 | } | 239 | } |
diff --git a/crates/hir_def/src/path.rs b/crates/hir_def/src/path.rs index f9c8328f0..b528ff8ba 100644 --- a/crates/hir_def/src/path.rs +++ b/crates/hir_def/src/path.rs | |||
@@ -289,6 +289,12 @@ impl From<Name> for Path { | |||
289 | } | 289 | } |
290 | } | 290 | } |
291 | 291 | ||
292 | impl From<Name> for Box<Path> { | ||
293 | fn from(name: Name) -> Box<Path> { | ||
294 | Box::new(Path::from(name)) | ||
295 | } | ||
296 | } | ||
297 | |||
292 | impl From<Name> for ModPath { | 298 | impl From<Name> for ModPath { |
293 | fn from(name: Name) -> ModPath { | 299 | fn from(name: Name) -> ModPath { |
294 | ModPath::from_segments(PathKind::Plain, iter::once(name)) | 300 | ModPath::from_segments(PathKind::Plain, iter::once(name)) |
diff --git a/crates/hir_expand/src/builtin_derive.rs b/crates/hir_expand/src/builtin_derive.rs index 6ece4b289..537c03028 100644 --- a/crates/hir_expand/src/builtin_derive.rs +++ b/crates/hir_expand/src/builtin_derive.rs | |||
@@ -269,7 +269,7 @@ mod tests { | |||
269 | use expect_test::{expect, Expect}; | 269 | use expect_test::{expect, Expect}; |
270 | use name::AsName; | 270 | use name::AsName; |
271 | 271 | ||
272 | use crate::{test_db::TestDB, AstId, MacroCallId, MacroCallKind, MacroCallLoc}; | 272 | use crate::{test_db::TestDB, AstId, AttrId, MacroCallId, MacroCallKind, MacroCallLoc}; |
273 | 273 | ||
274 | use super::*; | 274 | use super::*; |
275 | 275 | ||
@@ -308,7 +308,7 @@ $0 | |||
308 | 308 | ||
309 | let expander = BuiltinDeriveExpander::find_by_name(&name).unwrap(); | 309 | let expander = BuiltinDeriveExpander::find_by_name(&name).unwrap(); |
310 | 310 | ||
311 | let attr_id = AstId::new(file_id.into(), ast_id_map.ast_id(&items[0])); | 311 | let ast_id = AstId::new(file_id.into(), ast_id_map.ast_id(&items[0])); |
312 | 312 | ||
313 | let loc = MacroCallLoc { | 313 | let loc = MacroCallLoc { |
314 | def: MacroDefId { | 314 | def: MacroDefId { |
@@ -317,7 +317,11 @@ $0 | |||
317 | local_inner: false, | 317 | local_inner: false, |
318 | }, | 318 | }, |
319 | krate: CrateId(0), | 319 | krate: CrateId(0), |
320 | kind: MacroCallKind::Derive(attr_id, name.to_string()), | 320 | kind: MacroCallKind::Derive { |
321 | ast_id, | ||
322 | derive_name: name.to_string(), | ||
323 | derive_attr: AttrId(0), | ||
324 | }, | ||
321 | }; | 325 | }; |
322 | 326 | ||
323 | let id: MacroCallId = db.intern_macro(loc).into(); | 327 | let id: MacroCallId = db.intern_macro(loc).into(); |
diff --git a/crates/hir_expand/src/builtin_macro.rs b/crates/hir_expand/src/builtin_macro.rs index 75ec4196b..80365fc16 100644 --- a/crates/hir_expand/src/builtin_macro.rs +++ b/crates/hir_expand/src/builtin_macro.rs | |||
@@ -490,7 +490,7 @@ fn env_expand( | |||
490 | // unnecessary diagnostics for eg. `CARGO_PKG_NAME`. | 490 | // unnecessary diagnostics for eg. `CARGO_PKG_NAME`. |
491 | if key == "OUT_DIR" { | 491 | if key == "OUT_DIR" { |
492 | err = Some(mbe::ExpandError::Other( | 492 | err = Some(mbe::ExpandError::Other( |
493 | r#"`OUT_DIR` not set, enable "load out dirs from check" to fix"#.into(), | 493 | r#"`OUT_DIR` not set, enable "run build scripts" to fix"#.into(), |
494 | )); | 494 | )); |
495 | } | 495 | } |
496 | 496 | ||
@@ -566,10 +566,9 @@ mod tests { | |||
566 | let loc = MacroCallLoc { | 566 | let loc = MacroCallLoc { |
567 | def, | 567 | def, |
568 | krate, | 568 | krate, |
569 | kind: MacroCallKind::FnLike(AstId::new( | 569 | kind: MacroCallKind::FnLike { |
570 | file_id.into(), | 570 | ast_id: AstId::new(file_id.into(), ast_id_map.ast_id(¯o_call)), |
571 | ast_id_map.ast_id(¯o_call), | 571 | }, |
572 | )), | ||
573 | }; | 572 | }; |
574 | 573 | ||
575 | let id: MacroCallId = db.intern_macro(loc).into(); | 574 | let id: MacroCallId = db.intern_macro(loc).into(); |
diff --git a/crates/hir_expand/src/db.rs b/crates/hir_expand/src/db.rs index 10fe60821..ca705ee9d 100644 --- a/crates/hir_expand/src/db.rs +++ b/crates/hir_expand/src/db.rs | |||
@@ -439,6 +439,7 @@ fn to_fragment_kind(db: &dyn AstDatabase, id: MacroCallId) -> FragmentKind { | |||
439 | match parent.kind() { | 439 | match parent.kind() { |
440 | MACRO_ITEMS | SOURCE_FILE => FragmentKind::Items, | 440 | MACRO_ITEMS | SOURCE_FILE => FragmentKind::Items, |
441 | MACRO_STMTS => FragmentKind::Statements, | 441 | MACRO_STMTS => FragmentKind::Statements, |
442 | MACRO_PAT => FragmentKind::Pattern, | ||
442 | ITEM_LIST => FragmentKind::Items, | 443 | ITEM_LIST => FragmentKind::Items, |
443 | LET_STMT => { | 444 | LET_STMT => { |
444 | // FIXME: Handle LHS Pattern | 445 | // FIXME: Handle LHS Pattern |
diff --git a/crates/hir_expand/src/eager.rs b/crates/hir_expand/src/eager.rs index 9705526fa..ef126e4ad 100644 --- a/crates/hir_expand/src/eager.rs +++ b/crates/hir_expand/src/eager.rs | |||
@@ -174,8 +174,9 @@ fn lazy_expand( | |||
174 | ) -> ExpandResult<Option<InFile<SyntaxNode>>> { | 174 | ) -> ExpandResult<Option<InFile<SyntaxNode>>> { |
175 | let ast_id = db.ast_id_map(macro_call.file_id).ast_id(¯o_call.value); | 175 | let ast_id = db.ast_id_map(macro_call.file_id).ast_id(¯o_call.value); |
176 | 176 | ||
177 | let id: MacroCallId = | 177 | let id: MacroCallId = def |
178 | def.as_lazy_macro(db, krate, MacroCallKind::FnLike(macro_call.with_value(ast_id))).into(); | 178 | .as_lazy_macro(db, krate, MacroCallKind::FnLike { ast_id: macro_call.with_value(ast_id) }) |
179 | .into(); | ||
179 | 180 | ||
180 | let err = db.macro_expand_error(id); | 181 | let err = db.macro_expand_error(id); |
181 | let value = db.parse_or_expand(id.as_file()).map(|node| InFile::new(id.as_file(), node)); | 182 | let value = db.parse_or_expand(id.as_file()).map(|node| InFile::new(id.as_file(), node)); |
diff --git a/crates/hir_expand/src/lib.rs b/crates/hir_expand/src/lib.rs index 3e332ee47..a0e6aec62 100644 --- a/crates/hir_expand/src/lib.rs +++ b/crates/hir_expand/src/lib.rs | |||
@@ -290,22 +290,27 @@ pub struct MacroCallLoc { | |||
290 | 290 | ||
291 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | 291 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] |
292 | pub enum MacroCallKind { | 292 | pub enum MacroCallKind { |
293 | FnLike(AstId<ast::MacroCall>), | 293 | FnLike { ast_id: AstId<ast::MacroCall> }, |
294 | Derive(AstId<ast::Item>, String), | 294 | Derive { ast_id: AstId<ast::Item>, derive_name: String, derive_attr: AttrId }, |
295 | } | 295 | } |
296 | 296 | ||
297 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | ||
298 | pub struct AttrId(pub u32); | ||
299 | |||
297 | impl MacroCallKind { | 300 | impl MacroCallKind { |
298 | fn file_id(&self) -> HirFileId { | 301 | fn file_id(&self) -> HirFileId { |
299 | match self { | 302 | match self { |
300 | MacroCallKind::FnLike(ast_id) => ast_id.file_id, | 303 | MacroCallKind::FnLike { ast_id, .. } => ast_id.file_id, |
301 | MacroCallKind::Derive(ast_id, _) => ast_id.file_id, | 304 | MacroCallKind::Derive { ast_id, .. } => ast_id.file_id, |
302 | } | 305 | } |
303 | } | 306 | } |
304 | 307 | ||
305 | fn node(&self, db: &dyn db::AstDatabase) -> InFile<SyntaxNode> { | 308 | fn node(&self, db: &dyn db::AstDatabase) -> InFile<SyntaxNode> { |
306 | match self { | 309 | match self { |
307 | MacroCallKind::FnLike(ast_id) => ast_id.with_value(ast_id.to_node(db).syntax().clone()), | 310 | MacroCallKind::FnLike { ast_id, .. } => { |
308 | MacroCallKind::Derive(ast_id, _) => { | 311 | ast_id.with_value(ast_id.to_node(db).syntax().clone()) |
312 | } | ||
313 | MacroCallKind::Derive { ast_id, .. } => { | ||
309 | ast_id.with_value(ast_id.to_node(db).syntax().clone()) | 314 | ast_id.with_value(ast_id.to_node(db).syntax().clone()) |
310 | } | 315 | } |
311 | } | 316 | } |
@@ -313,10 +318,10 @@ impl MacroCallKind { | |||
313 | 318 | ||
314 | fn arg(&self, db: &dyn db::AstDatabase) -> Option<SyntaxNode> { | 319 | fn arg(&self, db: &dyn db::AstDatabase) -> Option<SyntaxNode> { |
315 | match self { | 320 | match self { |
316 | MacroCallKind::FnLike(ast_id) => { | 321 | MacroCallKind::FnLike { ast_id, .. } => { |
317 | Some(ast_id.to_node(db).token_tree()?.syntax().clone()) | 322 | Some(ast_id.to_node(db).token_tree()?.syntax().clone()) |
318 | } | 323 | } |
319 | MacroCallKind::Derive(ast_id, _) => Some(ast_id.to_node(db).syntax().clone()), | 324 | MacroCallKind::Derive { ast_id, .. } => Some(ast_id.to_node(db).syntax().clone()), |
320 | } | 325 | } |
321 | } | 326 | } |
322 | } | 327 | } |
diff --git a/crates/hir_ty/src/autoderef.rs b/crates/hir_ty/src/autoderef.rs index 7ca4af80e..71bc436e6 100644 --- a/crates/hir_ty/src/autoderef.rs +++ b/crates/hir_ty/src/autoderef.rs | |||
@@ -6,14 +6,15 @@ | |||
6 | use std::iter::successors; | 6 | use std::iter::successors; |
7 | 7 | ||
8 | use base_db::CrateId; | 8 | use base_db::CrateId; |
9 | use chalk_ir::cast::Cast; | 9 | use chalk_ir::{cast::Cast, fold::Fold, interner::HasInterner, VariableKind}; |
10 | use hir_def::lang_item::LangItemTarget; | 10 | use hir_def::lang_item::LangItemTarget; |
11 | use hir_expand::name::name; | 11 | use hir_expand::name::name; |
12 | use log::{info, warn}; | 12 | use log::{info, warn}; |
13 | 13 | ||
14 | use crate::{ | 14 | use crate::{ |
15 | db::HirDatabase, AliasEq, AliasTy, BoundVar, Canonical, CanonicalVarKinds, DebruijnIndex, | 15 | db::HirDatabase, static_lifetime, AliasEq, AliasTy, BoundVar, Canonical, CanonicalVarKinds, |
16 | InEnvironment, Interner, Solution, Ty, TyBuilder, TyKind, | 16 | DebruijnIndex, InEnvironment, Interner, ProjectionTyExt, Solution, Substitution, Ty, TyBuilder, |
17 | TyKind, | ||
17 | }; | 18 | }; |
18 | 19 | ||
19 | const AUTODEREF_RECURSION_LIMIT: usize = 10; | 20 | const AUTODEREF_RECURSION_LIMIT: usize = 10; |
@@ -35,13 +36,21 @@ pub(crate) fn deref( | |||
35 | krate: CrateId, | 36 | krate: CrateId, |
36 | ty: InEnvironment<&Canonical<Ty>>, | 37 | ty: InEnvironment<&Canonical<Ty>>, |
37 | ) -> Option<Canonical<Ty>> { | 38 | ) -> Option<Canonical<Ty>> { |
38 | if let Some(derefed) = ty.goal.value.builtin_deref() { | 39 | if let Some(derefed) = builtin_deref(&ty.goal.value) { |
39 | Some(Canonical { value: derefed, binders: ty.goal.binders.clone() }) | 40 | Some(Canonical { value: derefed, binders: ty.goal.binders.clone() }) |
40 | } else { | 41 | } else { |
41 | deref_by_trait(db, krate, ty) | 42 | deref_by_trait(db, krate, ty) |
42 | } | 43 | } |
43 | } | 44 | } |
44 | 45 | ||
46 | fn builtin_deref(ty: &Ty) -> Option<Ty> { | ||
47 | match ty.kind(&Interner) { | ||
48 | TyKind::Ref(.., ty) => Some(ty.clone()), | ||
49 | TyKind::Raw(.., ty) => Some(ty.clone()), | ||
50 | _ => None, | ||
51 | } | ||
52 | } | ||
53 | |||
45 | fn deref_by_trait( | 54 | fn deref_by_trait( |
46 | db: &dyn HirDatabase, | 55 | db: &dyn HirDatabase, |
47 | krate: CrateId, | 56 | krate: CrateId, |
@@ -95,7 +104,7 @@ fn deref_by_trait( | |||
95 | binders: CanonicalVarKinds::from_iter( | 104 | binders: CanonicalVarKinds::from_iter( |
96 | &Interner, | 105 | &Interner, |
97 | ty.goal.binders.iter(&Interner).cloned().chain(Some(chalk_ir::WithKind::new( | 106 | ty.goal.binders.iter(&Interner).cloned().chain(Some(chalk_ir::WithKind::new( |
98 | chalk_ir::VariableKind::Ty(chalk_ir::TyVariableKind::General), | 107 | VariableKind::Ty(chalk_ir::TyVariableKind::General), |
99 | chalk_ir::UniverseIndex::ROOT, | 108 | chalk_ir::UniverseIndex::ROOT, |
100 | ))), | 109 | ))), |
101 | ), | 110 | ), |
@@ -120,23 +129,25 @@ fn deref_by_trait( | |||
120 | // assumptions will be broken. We would need to properly introduce | 129 | // assumptions will be broken. We would need to properly introduce |
121 | // new variables in that case | 130 | // new variables in that case |
122 | 131 | ||
123 | for i in 1..vars.0.binders.len(&Interner) { | 132 | for i in 1..vars.binders.len(&Interner) { |
124 | if vars.0.value.at(&Interner, i - 1).assert_ty_ref(&Interner).kind(&Interner) | 133 | if vars.value.subst.at(&Interner, i - 1).assert_ty_ref(&Interner).kind(&Interner) |
125 | != &TyKind::BoundVar(BoundVar::new(DebruijnIndex::INNERMOST, i - 1)) | 134 | != &TyKind::BoundVar(BoundVar::new(DebruijnIndex::INNERMOST, i - 1)) |
126 | { | 135 | { |
127 | warn!("complex solution for derefing {:?}: {:?}, ignoring", ty.goal, solution); | 136 | warn!("complex solution for derefing {:?}: {:?}, ignoring", ty.goal, solution); |
128 | return None; | 137 | return None; |
129 | } | 138 | } |
130 | } | 139 | } |
131 | Some(Canonical { | 140 | // FIXME: we remove lifetime variables here since they can confuse |
141 | // the method resolution code later | ||
142 | Some(fixup_lifetime_variables(Canonical { | ||
132 | value: vars | 143 | value: vars |
133 | .0 | ||
134 | .value | 144 | .value |
135 | .at(&Interner, vars.0.value.len(&Interner) - 1) | 145 | .subst |
146 | .at(&Interner, vars.value.subst.len(&Interner) - 1) | ||
136 | .assert_ty_ref(&Interner) | 147 | .assert_ty_ref(&Interner) |
137 | .clone(), | 148 | .clone(), |
138 | binders: vars.0.binders.clone(), | 149 | binders: vars.binders.clone(), |
139 | }) | 150 | })) |
140 | } | 151 | } |
141 | Solution::Ambig(_) => { | 152 | Solution::Ambig(_) => { |
142 | info!("Ambiguous solution for derefing {:?}: {:?}", ty.goal, solution); | 153 | info!("Ambiguous solution for derefing {:?}: {:?}", ty.goal, solution); |
@@ -144,3 +155,32 @@ fn deref_by_trait( | |||
144 | } | 155 | } |
145 | } | 156 | } |
146 | } | 157 | } |
158 | |||
159 | fn fixup_lifetime_variables<T: Fold<Interner, Result = T> + HasInterner<Interner = Interner>>( | ||
160 | c: Canonical<T>, | ||
161 | ) -> Canonical<T> { | ||
162 | // Removes lifetime variables from the Canonical, replacing them by static lifetimes. | ||
163 | let mut i = 0; | ||
164 | let subst = Substitution::from_iter( | ||
165 | &Interner, | ||
166 | c.binders.iter(&Interner).map(|vk| match vk.kind { | ||
167 | VariableKind::Ty(_) => { | ||
168 | let index = i; | ||
169 | i += 1; | ||
170 | BoundVar::new(DebruijnIndex::INNERMOST, index).to_ty(&Interner).cast(&Interner) | ||
171 | } | ||
172 | VariableKind::Lifetime => static_lifetime().cast(&Interner), | ||
173 | VariableKind::Const(_) => unimplemented!(), | ||
174 | }), | ||
175 | ); | ||
176 | let binders = CanonicalVarKinds::from_iter( | ||
177 | &Interner, | ||
178 | c.binders.iter(&Interner).filter(|vk| match vk.kind { | ||
179 | VariableKind::Ty(_) => true, | ||
180 | VariableKind::Lifetime => false, | ||
181 | VariableKind::Const(_) => true, | ||
182 | }), | ||
183 | ); | ||
184 | let value = subst.apply(c.value, &Interner); | ||
185 | Canonical { binders, value } | ||
186 | } | ||
diff --git a/crates/hir_ty/src/builder.rs b/crates/hir_ty/src/builder.rs index 9b2c6975a..e25ef866d 100644 --- a/crates/hir_ty/src/builder.rs +++ b/crates/hir_ty/src/builder.rs | |||
@@ -4,6 +4,7 @@ use std::iter; | |||
4 | 4 | ||
5 | use chalk_ir::{ | 5 | use chalk_ir::{ |
6 | cast::{Cast, CastTo, Caster}, | 6 | cast::{Cast, CastTo, Caster}, |
7 | fold::Fold, | ||
7 | interner::HasInterner, | 8 | interner::HasInterner, |
8 | AdtId, BoundVar, DebruijnIndex, Safety, Scalar, | 9 | AdtId, BoundVar, DebruijnIndex, Safety, Scalar, |
9 | }; | 10 | }; |
@@ -12,8 +13,8 @@ use smallvec::SmallVec; | |||
12 | 13 | ||
13 | use crate::{ | 14 | use crate::{ |
14 | db::HirDatabase, primitive, to_assoc_type_id, to_chalk_trait_id, utils::generics, Binders, | 15 | db::HirDatabase, primitive, to_assoc_type_id, to_chalk_trait_id, utils::generics, Binders, |
15 | CallableSig, FnPointer, FnSig, GenericArg, Interner, ProjectionTy, Substitution, TraitRef, Ty, | 16 | CallableSig, FnPointer, FnSig, FnSubst, GenericArg, Interner, ProjectionTy, Substitution, |
16 | TyDefId, TyKind, TypeWalk, ValueTyDefId, | 17 | TraitRef, Ty, TyDefId, TyExt, TyKind, ValueTyDefId, |
17 | }; | 18 | }; |
18 | 19 | ||
19 | /// This is a builder for `Ty` or anything that needs a `Substitution`. | 20 | /// This is a builder for `Ty` or anything that needs a `Substitution`. |
@@ -32,8 +33,7 @@ impl<D> TyBuilder<D> { | |||
32 | 33 | ||
33 | fn build_internal(self) -> (D, Substitution) { | 34 | fn build_internal(self) -> (D, Substitution) { |
34 | assert_eq!(self.vec.len(), self.param_count); | 35 | assert_eq!(self.vec.len(), self.param_count); |
35 | // FIXME: would be good to have a way to construct a chalk_ir::Substitution from the interned form | 36 | let subst = Substitution::from_iter(&Interner, self.vec); |
36 | let subst = Substitution::intern(self.vec); | ||
37 | (self.data, subst) | 37 | (self.data, subst) |
38 | } | 38 | } |
39 | 39 | ||
@@ -78,9 +78,12 @@ impl TyBuilder<()> { | |||
78 | 78 | ||
79 | pub fn fn_ptr(sig: CallableSig) -> Ty { | 79 | pub fn fn_ptr(sig: CallableSig) -> Ty { |
80 | TyKind::Function(FnPointer { | 80 | TyKind::Function(FnPointer { |
81 | num_args: sig.params().len(), | 81 | num_binders: 0, |
82 | sig: FnSig { abi: (), safety: Safety::Safe, variadic: sig.is_varargs }, | 82 | sig: FnSig { abi: (), safety: Safety::Safe, variadic: sig.is_varargs }, |
83 | substs: Substitution::from_iter(&Interner, sig.params_and_return.iter().cloned()), | 83 | substitution: FnSubst(Substitution::from_iter( |
84 | &Interner, | ||
85 | sig.params_and_return.iter().cloned(), | ||
86 | )), | ||
84 | }) | 87 | }) |
85 | .intern(&Interner) | 88 | .intern(&Interner) |
86 | } | 89 | } |
@@ -138,7 +141,7 @@ impl TyBuilder<hir_def::AdtId> { | |||
138 | self.vec.push(fallback().cast(&Interner)); | 141 | self.vec.push(fallback().cast(&Interner)); |
139 | } else { | 142 | } else { |
140 | // each default can depend on the previous parameters | 143 | // each default can depend on the previous parameters |
141 | let subst_so_far = Substitution::intern(self.vec.clone()); | 144 | let subst_so_far = Substitution::from_iter(&Interner, self.vec.clone()); |
142 | self.vec | 145 | self.vec |
143 | .push(default_ty.clone().substitute(&Interner, &subst_so_far).cast(&Interner)); | 146 | .push(default_ty.clone().substitute(&Interner, &subst_so_far).cast(&Interner)); |
144 | } | 147 | } |
@@ -193,13 +196,13 @@ impl TyBuilder<TypeAliasId> { | |||
193 | } | 196 | } |
194 | } | 197 | } |
195 | 198 | ||
196 | impl<T: TypeWalk + HasInterner<Interner = Interner>> TyBuilder<Binders<T>> { | 199 | impl<T: HasInterner<Interner = Interner> + Fold<Interner>> TyBuilder<Binders<T>> { |
197 | fn subst_binders(b: Binders<T>) -> Self { | 200 | fn subst_binders(b: Binders<T>) -> Self { |
198 | let param_count = b.binders.len(&Interner); | 201 | let param_count = b.binders.len(&Interner); |
199 | TyBuilder::new(b, param_count) | 202 | TyBuilder::new(b, param_count) |
200 | } | 203 | } |
201 | 204 | ||
202 | pub fn build(self) -> T { | 205 | pub fn build(self) -> <T as Fold<Interner>>::Result { |
203 | let (b, subst) = self.build_internal(); | 206 | let (b, subst) = self.build_internal(); |
204 | b.substitute(&Interner, &subst) | 207 | b.substitute(&Interner, &subst) |
205 | } | 208 | } |
diff --git a/crates/hir_ty/src/chalk_cast.rs b/crates/hir_ty/src/chalk_cast.rs deleted file mode 100644 index df6492113..000000000 --- a/crates/hir_ty/src/chalk_cast.rs +++ /dev/null | |||
@@ -1,73 +0,0 @@ | |||
1 | //! Implementations of the Chalk `Cast` trait for our types. | ||
2 | |||
3 | use chalk_ir::{ | ||
4 | cast::{Cast, CastTo}, | ||
5 | interner::HasInterner, | ||
6 | }; | ||
7 | |||
8 | use crate::{AliasEq, DomainGoal, GenericArg, GenericArgData, Interner, TraitRef, Ty, WhereClause}; | ||
9 | |||
10 | macro_rules! has_interner { | ||
11 | ($t:ty) => { | ||
12 | impl HasInterner for $t { | ||
13 | type Interner = crate::Interner; | ||
14 | } | ||
15 | }; | ||
16 | } | ||
17 | |||
18 | has_interner!(WhereClause); | ||
19 | has_interner!(DomainGoal); | ||
20 | has_interner!(GenericArg); | ||
21 | has_interner!(Ty); | ||
22 | |||
23 | impl CastTo<WhereClause> for TraitRef { | ||
24 | fn cast_to(self, _interner: &Interner) -> WhereClause { | ||
25 | WhereClause::Implemented(self) | ||
26 | } | ||
27 | } | ||
28 | |||
29 | impl CastTo<WhereClause> for AliasEq { | ||
30 | fn cast_to(self, _interner: &Interner) -> WhereClause { | ||
31 | WhereClause::AliasEq(self) | ||
32 | } | ||
33 | } | ||
34 | |||
35 | impl CastTo<DomainGoal> for WhereClause { | ||
36 | fn cast_to(self, _interner: &Interner) -> DomainGoal { | ||
37 | DomainGoal::Holds(self) | ||
38 | } | ||
39 | } | ||
40 | |||
41 | impl CastTo<GenericArg> for Ty { | ||
42 | fn cast_to(self, interner: &Interner) -> GenericArg { | ||
43 | GenericArg::new(interner, GenericArgData::Ty(self)) | ||
44 | } | ||
45 | } | ||
46 | |||
47 | macro_rules! transitive_impl { | ||
48 | ($a:ty, $b:ty, $c:ty) => { | ||
49 | impl CastTo<$c> for $a { | ||
50 | fn cast_to(self, interner: &Interner) -> $c { | ||
51 | self.cast::<$b>(interner).cast(interner) | ||
52 | } | ||
53 | } | ||
54 | }; | ||
55 | } | ||
56 | |||
57 | // In Chalk, these can be done as blanket impls, but that doesn't work here | ||
58 | // because of coherence | ||
59 | |||
60 | transitive_impl!(TraitRef, WhereClause, DomainGoal); | ||
61 | transitive_impl!(AliasEq, WhereClause, DomainGoal); | ||
62 | |||
63 | macro_rules! reflexive_impl { | ||
64 | ($a:ty) => { | ||
65 | impl CastTo<$a> for $a { | ||
66 | fn cast_to(self, _interner: &Interner) -> $a { | ||
67 | self | ||
68 | } | ||
69 | } | ||
70 | }; | ||
71 | } | ||
72 | |||
73 | reflexive_impl!(GenericArg); | ||
diff --git a/crates/hir_ty/src/traits/chalk.rs b/crates/hir_ty/src/chalk_db.rs index dff87ef70..8f054d06b 100644 --- a/crates/hir_ty/src/traits/chalk.rs +++ b/crates/hir_ty/src/chalk_db.rs | |||
@@ -1,52 +1,47 @@ | |||
1 | //! Conversion code from/to Chalk. | 1 | //! The implementation of `RustIrDatabase` for Chalk, which provides information |
2 | //! about the code that Chalk needs. | ||
2 | use std::sync::Arc; | 3 | use std::sync::Arc; |
3 | 4 | ||
4 | use log::debug; | 5 | use log::debug; |
5 | 6 | ||
6 | use chalk_ir::{fold::shift::Shift, CanonicalVarKinds}; | 7 | use chalk_ir::{cast::Cast, fold::shift::Shift, CanonicalVarKinds}; |
7 | use chalk_solve::rust_ir::{self, OpaqueTyDatumBound, WellKnownTrait}; | 8 | use chalk_solve::rust_ir::{self, OpaqueTyDatumBound, WellKnownTrait}; |
8 | 9 | ||
9 | use base_db::{salsa::InternKey, CrateId}; | 10 | use base_db::CrateId; |
10 | use hir_def::{ | 11 | use hir_def::{ |
11 | lang_item::{lang_attr, LangItemTarget}, | 12 | lang_item::{lang_attr, LangItemTarget}, |
12 | AssocContainerId, AssocItemId, HasModule, Lookup, TypeAliasId, | 13 | AssocContainerId, AssocItemId, GenericDefId, HasModule, Lookup, TypeAliasId, |
13 | }; | 14 | }; |
14 | use hir_expand::name::name; | 15 | use hir_expand::name::name; |
15 | 16 | ||
16 | use super::ChalkContext; | ||
17 | use crate::{ | 17 | use crate::{ |
18 | db::HirDatabase, | 18 | db::HirDatabase, |
19 | display::HirDisplay, | 19 | display::HirDisplay, |
20 | from_assoc_type_id, | 20 | from_assoc_type_id, from_chalk_trait_id, make_only_type_binders, |
21 | mapping::{from_chalk, ToChalk, TypeAliasAsValue}, | ||
21 | method_resolution::{TyFingerprint, ALL_FLOAT_FPS, ALL_INT_FPS}, | 22 | method_resolution::{TyFingerprint, ALL_FLOAT_FPS, ALL_INT_FPS}, |
22 | to_assoc_type_id, to_chalk_trait_id, | 23 | to_assoc_type_id, to_chalk_trait_id, |
24 | traits::ChalkContext, | ||
23 | utils::generics, | 25 | utils::generics, |
24 | AliasEq, AliasTy, BoundVar, CallableDefId, DebruijnIndex, FnDefId, ProjectionTy, Substitution, | 26 | AliasEq, AliasTy, BoundVar, CallableDefId, DebruijnIndex, FnDefId, Interner, ProjectionTy, |
25 | TraitRef, Ty, TyBuilder, TyKind, WhereClause, | 27 | ProjectionTyExt, QuantifiedWhereClause, Substitution, TraitRef, TraitRefExt, Ty, TyBuilder, |
28 | TyExt, TyKind, WhereClause, | ||
26 | }; | 29 | }; |
27 | use mapping::{ | ||
28 | convert_where_clauses, generic_predicate_to_inline_bound, make_binders, TypeAliasAsValue, | ||
29 | }; | ||
30 | |||
31 | pub use self::interner::Interner; | ||
32 | pub(crate) use self::interner::*; | ||
33 | 30 | ||
34 | pub(super) mod tls; | 31 | pub(crate) type AssociatedTyDatum = chalk_solve::rust_ir::AssociatedTyDatum<Interner>; |
35 | mod interner; | 32 | pub(crate) type TraitDatum = chalk_solve::rust_ir::TraitDatum<Interner>; |
36 | mod mapping; | 33 | pub(crate) type StructDatum = chalk_solve::rust_ir::AdtDatum<Interner>; |
37 | 34 | pub(crate) type ImplDatum = chalk_solve::rust_ir::ImplDatum<Interner>; | |
38 | pub(crate) trait ToChalk { | 35 | pub(crate) type OpaqueTyDatum = chalk_solve::rust_ir::OpaqueTyDatum<Interner>; |
39 | type Chalk; | 36 | |
40 | fn to_chalk(self, db: &dyn HirDatabase) -> Self::Chalk; | 37 | pub(crate) type AssocTypeId = chalk_ir::AssocTypeId<Interner>; |
41 | fn from_chalk(db: &dyn HirDatabase, chalk: Self::Chalk) -> Self; | 38 | pub(crate) type TraitId = chalk_ir::TraitId<Interner>; |
42 | } | 39 | pub(crate) type AdtId = chalk_ir::AdtId<Interner>; |
43 | 40 | pub(crate) type ImplId = chalk_ir::ImplId<Interner>; | |
44 | pub(crate) fn from_chalk<T, ChalkT>(db: &dyn HirDatabase, chalk: ChalkT) -> T | 41 | pub(crate) type AssociatedTyValueId = chalk_solve::rust_ir::AssociatedTyValueId<Interner>; |
45 | where | 42 | pub(crate) type AssociatedTyValue = chalk_solve::rust_ir::AssociatedTyValue<Interner>; |
46 | T: ToChalk<Chalk = ChalkT>, | 43 | pub(crate) type FnDefDatum = chalk_solve::rust_ir::FnDefDatum<Interner>; |
47 | { | 44 | pub(crate) type Variances = chalk_ir::Variances<Interner>; |
48 | T::from_chalk(db, chalk) | ||
49 | } | ||
50 | 45 | ||
51 | impl<'a> chalk_solve::RustIrDatabase<Interner> for ChalkContext<'a> { | 46 | impl<'a> chalk_solve::RustIrDatabase<Interner> for ChalkContext<'a> { |
52 | fn associated_ty_data(&self, id: AssocTypeId) -> Arc<AssociatedTyDatum> { | 47 | fn associated_ty_data(&self, id: AssocTypeId) -> Arc<AssociatedTyDatum> { |
@@ -84,9 +79,9 @@ impl<'a> chalk_solve::RustIrDatabase<Interner> for ChalkContext<'a> { | |||
84 | binders: &CanonicalVarKinds<Interner>, | 79 | binders: &CanonicalVarKinds<Interner>, |
85 | ) -> Vec<ImplId> { | 80 | ) -> Vec<ImplId> { |
86 | debug!("impls_for_trait {:?}", trait_id); | 81 | debug!("impls_for_trait {:?}", trait_id); |
87 | let trait_: hir_def::TraitId = from_chalk(self.db, trait_id); | 82 | let trait_: hir_def::TraitId = from_chalk_trait_id(trait_id); |
88 | 83 | ||
89 | let ty: Ty = from_chalk(self.db, parameters[0].assert_ty_ref(&Interner).clone()); | 84 | let ty: Ty = parameters[0].assert_ty_ref(&Interner).clone(); |
90 | 85 | ||
91 | fn binder_kind( | 86 | fn binder_kind( |
92 | ty: &Ty, | 87 | ty: &Ty, |
@@ -103,7 +98,7 @@ impl<'a> chalk_solve::RustIrDatabase<Interner> for ChalkContext<'a> { | |||
103 | None | 98 | None |
104 | } | 99 | } |
105 | 100 | ||
106 | let self_ty_fp = TyFingerprint::for_impl(&ty); | 101 | let self_ty_fp = TyFingerprint::for_trait_impl(&ty); |
107 | let fps: &[TyFingerprint] = match binder_kind(&ty, binders) { | 102 | let fps: &[TyFingerprint] = match binder_kind(&ty, binders) { |
108 | Some(chalk_ir::TyVariableKind::Integer) => &ALL_INT_FPS, | 103 | Some(chalk_ir::TyVariableKind::Integer) => &ALL_INT_FPS, |
109 | Some(chalk_ir::TyVariableKind::Float) => &ALL_FLOAT_FPS, | 104 | Some(chalk_ir::TyVariableKind::Float) => &ALL_FLOAT_FPS, |
@@ -166,7 +161,7 @@ impl<'a> chalk_solve::RustIrDatabase<Interner> for ChalkContext<'a> { | |||
166 | Some(LangItemTarget::TraitId(trait_)) => trait_, | 161 | Some(LangItemTarget::TraitId(trait_)) => trait_, |
167 | _ => return None, | 162 | _ => return None, |
168 | }; | 163 | }; |
169 | Some(trait_.to_chalk(self.db)) | 164 | Some(to_chalk_trait_id(trait_)) |
170 | } | 165 | } |
171 | 166 | ||
172 | fn program_clauses_for_env( | 167 | fn program_clauses_for_env( |
@@ -187,16 +182,11 @@ impl<'a> chalk_solve::RustIrDatabase<Interner> for ChalkContext<'a> { | |||
187 | let (datas, binders) = (*datas).as_ref().into_value_and_skipped_binders(); | 182 | let (datas, binders) = (*datas).as_ref().into_value_and_skipped_binders(); |
188 | let data = &datas.impl_traits[idx as usize]; | 183 | let data = &datas.impl_traits[idx as usize]; |
189 | let bound = OpaqueTyDatumBound { | 184 | let bound = OpaqueTyDatumBound { |
190 | bounds: make_binders( | 185 | bounds: make_only_type_binders( |
191 | data.bounds | ||
192 | .skip_binders() | ||
193 | .iter() | ||
194 | .cloned() | ||
195 | .map(|b| b.to_chalk(self.db)) | ||
196 | .collect(), | ||
197 | 1, | 186 | 1, |
187 | data.bounds.skip_binders().iter().cloned().collect(), | ||
198 | ), | 188 | ), |
199 | where_clauses: make_binders(vec![], 0), | 189 | where_clauses: make_only_type_binders(0, vec![]), |
200 | }; | 190 | }; |
201 | chalk_ir::Binders::new(binders, bound) | 191 | chalk_ir::Binders::new(binders, bound) |
202 | } | 192 | } |
@@ -220,7 +210,8 @@ impl<'a> chalk_solve::RustIrDatabase<Interner> for ChalkContext<'a> { | |||
220 | let impl_bound = WhereClause::Implemented(TraitRef { | 210 | let impl_bound = WhereClause::Implemented(TraitRef { |
221 | trait_id: to_chalk_trait_id(future_trait), | 211 | trait_id: to_chalk_trait_id(future_trait), |
222 | // Self type as the first parameter. | 212 | // Self type as the first parameter. |
223 | substitution: Substitution::single( | 213 | substitution: Substitution::from1( |
214 | &Interner, | ||
224 | TyKind::BoundVar(BoundVar { | 215 | TyKind::BoundVar(BoundVar { |
225 | debruijn: DebruijnIndex::INNERMOST, | 216 | debruijn: DebruijnIndex::INNERMOST, |
226 | index: 0, | 217 | index: 0, |
@@ -232,7 +223,8 @@ impl<'a> chalk_solve::RustIrDatabase<Interner> for ChalkContext<'a> { | |||
232 | alias: AliasTy::Projection(ProjectionTy { | 223 | alias: AliasTy::Projection(ProjectionTy { |
233 | associated_ty_id: to_assoc_type_id(future_output), | 224 | associated_ty_id: to_assoc_type_id(future_output), |
234 | // Self type as the first parameter. | 225 | // Self type as the first parameter. |
235 | substitution: Substitution::single( | 226 | substitution: Substitution::from1( |
227 | &Interner, | ||
236 | TyKind::BoundVar(BoundVar::new(DebruijnIndex::INNERMOST, 0)) | 228 | TyKind::BoundVar(BoundVar::new(DebruijnIndex::INNERMOST, 0)) |
237 | .intern(&Interner), | 229 | .intern(&Interner), |
238 | ), | 230 | ), |
@@ -242,25 +234,25 @@ impl<'a> chalk_solve::RustIrDatabase<Interner> for ChalkContext<'a> { | |||
242 | .intern(&Interner), | 234 | .intern(&Interner), |
243 | }); | 235 | }); |
244 | let bound = OpaqueTyDatumBound { | 236 | let bound = OpaqueTyDatumBound { |
245 | bounds: make_binders( | 237 | bounds: make_only_type_binders( |
238 | 1, | ||
246 | vec![ | 239 | vec![ |
247 | wrap_in_empty_binders(impl_bound).to_chalk(self.db), | 240 | crate::wrap_empty_binders(impl_bound), |
248 | wrap_in_empty_binders(proj_bound).to_chalk(self.db), | 241 | crate::wrap_empty_binders(proj_bound), |
249 | ], | 242 | ], |
250 | 1, | ||
251 | ), | 243 | ), |
252 | where_clauses: make_binders(vec![], 0), | 244 | where_clauses: make_only_type_binders(0, vec![]), |
253 | }; | 245 | }; |
254 | // The opaque type has 1 parameter. | 246 | // The opaque type has 1 parameter. |
255 | make_binders(bound, 1) | 247 | make_only_type_binders(1, bound) |
256 | } else { | 248 | } else { |
257 | // If failed to find Symbol’s value as variable is void: Future::Output, return empty bounds as fallback. | 249 | // If failed to find Symbol’s value as variable is void: Future::Output, return empty bounds as fallback. |
258 | let bound = OpaqueTyDatumBound { | 250 | let bound = OpaqueTyDatumBound { |
259 | bounds: make_binders(vec![], 0), | 251 | bounds: make_only_type_binders(0, vec![]), |
260 | where_clauses: make_binders(vec![], 0), | 252 | where_clauses: make_only_type_binders(0, vec![]), |
261 | }; | 253 | }; |
262 | // The opaque type has 1 parameter. | 254 | // The opaque type has 1 parameter. |
263 | make_binders(bound, 1) | 255 | make_only_type_binders(1, bound) |
264 | } | 256 | } |
265 | } | 257 | } |
266 | }; | 258 | }; |
@@ -270,7 +262,7 @@ impl<'a> chalk_solve::RustIrDatabase<Interner> for ChalkContext<'a> { | |||
270 | 262 | ||
271 | fn hidden_opaque_type(&self, _id: chalk_ir::OpaqueTyId<Interner>) -> chalk_ir::Ty<Interner> { | 263 | fn hidden_opaque_type(&self, _id: chalk_ir::OpaqueTyId<Interner>) -> chalk_ir::Ty<Interner> { |
272 | // FIXME: actually provide the hidden type; it is relevant for auto traits | 264 | // FIXME: actually provide the hidden type; it is relevant for auto traits |
273 | TyKind::Error.intern(&Interner).to_chalk(self.db) | 265 | TyKind::Error.intern(&Interner) |
274 | } | 266 | } |
275 | 267 | ||
276 | fn is_object_safe(&self, _trait_id: chalk_ir::TraitId<Interner>) -> bool { | 268 | fn is_object_safe(&self, _trait_id: chalk_ir::TraitId<Interner>) -> bool { |
@@ -291,33 +283,32 @@ impl<'a> chalk_solve::RustIrDatabase<Interner> for ChalkContext<'a> { | |||
291 | _closure_id: chalk_ir::ClosureId<Interner>, | 283 | _closure_id: chalk_ir::ClosureId<Interner>, |
292 | substs: &chalk_ir::Substitution<Interner>, | 284 | substs: &chalk_ir::Substitution<Interner>, |
293 | ) -> chalk_ir::Binders<rust_ir::FnDefInputsAndOutputDatum<Interner>> { | 285 | ) -> chalk_ir::Binders<rust_ir::FnDefInputsAndOutputDatum<Interner>> { |
294 | let sig_ty: Ty = | 286 | let sig_ty = substs.at(&Interner, 0).assert_ty_ref(&Interner).clone(); |
295 | from_chalk(self.db, substs.at(&Interner, 0).assert_ty_ref(&Interner).clone()); | ||
296 | let sig = &sig_ty.callable_sig(self.db).expect("first closure param should be fn ptr"); | 287 | let sig = &sig_ty.callable_sig(self.db).expect("first closure param should be fn ptr"); |
297 | let io = rust_ir::FnDefInputsAndOutputDatum { | 288 | let io = rust_ir::FnDefInputsAndOutputDatum { |
298 | argument_types: sig.params().iter().map(|ty| ty.clone().to_chalk(self.db)).collect(), | 289 | argument_types: sig.params().iter().cloned().collect(), |
299 | return_type: sig.ret().clone().to_chalk(self.db), | 290 | return_type: sig.ret().clone(), |
300 | }; | 291 | }; |
301 | make_binders(io.shifted_in(&Interner), 0) | 292 | make_only_type_binders(0, io.shifted_in(&Interner)) |
302 | } | 293 | } |
303 | fn closure_upvars( | 294 | fn closure_upvars( |
304 | &self, | 295 | &self, |
305 | _closure_id: chalk_ir::ClosureId<Interner>, | 296 | _closure_id: chalk_ir::ClosureId<Interner>, |
306 | _substs: &chalk_ir::Substitution<Interner>, | 297 | _substs: &chalk_ir::Substitution<Interner>, |
307 | ) -> chalk_ir::Binders<chalk_ir::Ty<Interner>> { | 298 | ) -> chalk_ir::Binders<chalk_ir::Ty<Interner>> { |
308 | let ty = TyBuilder::unit().to_chalk(self.db); | 299 | let ty = TyBuilder::unit(); |
309 | make_binders(ty, 0) | 300 | make_only_type_binders(0, ty) |
310 | } | 301 | } |
311 | fn closure_fn_substitution( | 302 | fn closure_fn_substitution( |
312 | &self, | 303 | &self, |
313 | _closure_id: chalk_ir::ClosureId<Interner>, | 304 | _closure_id: chalk_ir::ClosureId<Interner>, |
314 | _substs: &chalk_ir::Substitution<Interner>, | 305 | _substs: &chalk_ir::Substitution<Interner>, |
315 | ) -> chalk_ir::Substitution<Interner> { | 306 | ) -> chalk_ir::Substitution<Interner> { |
316 | Substitution::empty(&Interner).to_chalk(self.db) | 307 | Substitution::empty(&Interner) |
317 | } | 308 | } |
318 | 309 | ||
319 | fn trait_name(&self, trait_id: chalk_ir::TraitId<Interner>) -> String { | 310 | fn trait_name(&self, trait_id: chalk_ir::TraitId<Interner>) -> String { |
320 | let id = from_chalk(self.db, trait_id); | 311 | let id = from_chalk_trait_id(trait_id); |
321 | self.db.trait_data(id).name.to_string() | 312 | self.db.trait_data(id).name.to_string() |
322 | } | 313 | } |
323 | fn adt_name(&self, chalk_ir::AdtId(adt_id): AdtId) -> String { | 314 | fn adt_name(&self, chalk_ir::AdtId(adt_id): AdtId) -> String { |
@@ -408,10 +399,10 @@ pub(crate) fn associated_ty_data_query( | |||
408 | let where_clauses = convert_where_clauses(db, type_alias.into(), &bound_vars); | 399 | let where_clauses = convert_where_clauses(db, type_alias.into(), &bound_vars); |
409 | let bound_data = rust_ir::AssociatedTyDatumBound { bounds, where_clauses }; | 400 | let bound_data = rust_ir::AssociatedTyDatumBound { bounds, where_clauses }; |
410 | let datum = AssociatedTyDatum { | 401 | let datum = AssociatedTyDatum { |
411 | trait_id: trait_.to_chalk(db), | 402 | trait_id: to_chalk_trait_id(trait_), |
412 | id, | 403 | id, |
413 | name: type_alias, | 404 | name: type_alias, |
414 | binders: make_binders(bound_data, generic_params.len()), | 405 | binders: make_only_type_binders(generic_params.len(), bound_data), |
415 | }; | 406 | }; |
416 | Arc::new(datum) | 407 | Arc::new(datum) |
417 | } | 408 | } |
@@ -422,7 +413,7 @@ pub(crate) fn trait_datum_query( | |||
422 | trait_id: TraitId, | 413 | trait_id: TraitId, |
423 | ) -> Arc<TraitDatum> { | 414 | ) -> Arc<TraitDatum> { |
424 | debug!("trait_datum {:?}", trait_id); | 415 | debug!("trait_datum {:?}", trait_id); |
425 | let trait_: hir_def::TraitId = from_chalk(db, trait_id); | 416 | let trait_ = from_chalk_trait_id(trait_id); |
426 | let trait_data = db.trait_data(trait_); | 417 | let trait_data = db.trait_data(trait_); |
427 | debug!("trait {:?} = {:?}", trait_id, trait_data.name); | 418 | debug!("trait {:?} = {:?}", trait_id, trait_data.name); |
428 | let generic_params = generics(db.upcast(), trait_.into()); | 419 | let generic_params = generics(db.upcast(), trait_.into()); |
@@ -444,7 +435,7 @@ pub(crate) fn trait_datum_query( | |||
444 | lang_attr(db.upcast(), trait_).and_then(|name| well_known_trait_from_lang_attr(&name)); | 435 | lang_attr(db.upcast(), trait_).and_then(|name| well_known_trait_from_lang_attr(&name)); |
445 | let trait_datum = TraitDatum { | 436 | let trait_datum = TraitDatum { |
446 | id: trait_id, | 437 | id: trait_id, |
447 | binders: make_binders(trait_datum_bound, bound_vars.len(&Interner)), | 438 | binders: make_only_type_binders(bound_vars.len(&Interner), trait_datum_bound), |
448 | flags, | 439 | flags, |
449 | associated_ty_ids, | 440 | associated_ty_ids, |
450 | well_known, | 441 | well_known, |
@@ -513,7 +504,7 @@ pub(crate) fn struct_datum_query( | |||
513 | // FIXME set ADT kind | 504 | // FIXME set ADT kind |
514 | kind: rust_ir::AdtKind::Struct, | 505 | kind: rust_ir::AdtKind::Struct, |
515 | id: struct_id, | 506 | id: struct_id, |
516 | binders: make_binders(struct_datum_bound, num_params), | 507 | binders: make_only_type_binders(num_params, struct_datum_bound), |
517 | flags, | 508 | flags, |
518 | }; | 509 | }; |
519 | Arc::new(struct_datum) | 510 | Arc::new(struct_datum) |
@@ -561,7 +552,6 @@ fn impl_def_datum( | |||
561 | trait_ref.display(db), | 552 | trait_ref.display(db), |
562 | where_clauses | 553 | where_clauses |
563 | ); | 554 | ); |
564 | let trait_ref = trait_ref.to_chalk(db); | ||
565 | 555 | ||
566 | let polarity = if negative { rust_ir::Polarity::Negative } else { rust_ir::Polarity::Positive }; | 556 | let polarity = if negative { rust_ir::Polarity::Negative } else { rust_ir::Polarity::Positive }; |
567 | 557 | ||
@@ -583,7 +573,7 @@ fn impl_def_datum( | |||
583 | .collect(); | 573 | .collect(); |
584 | debug!("impl_datum: {:?}", impl_datum_bound); | 574 | debug!("impl_datum: {:?}", impl_datum_bound); |
585 | let impl_datum = ImplDatum { | 575 | let impl_datum = ImplDatum { |
586 | binders: make_binders(impl_datum_bound, bound_vars.len(&Interner)), | 576 | binders: make_only_type_binders(bound_vars.len(&Interner), impl_datum_bound), |
587 | impl_type, | 577 | impl_type, |
588 | polarity, | 578 | polarity, |
589 | associated_ty_value_ids, | 579 | associated_ty_value_ids, |
@@ -622,7 +612,7 @@ fn type_alias_associated_ty_value( | |||
622 | .associated_type_by_name(&type_alias_data.name) | 612 | .associated_type_by_name(&type_alias_data.name) |
623 | .expect("assoc ty value should not exist"); // validated when building the impl data as well | 613 | .expect("assoc ty value should not exist"); // validated when building the impl data as well |
624 | let (ty, binders) = db.ty(type_alias.into()).into_value_and_skipped_binders(); | 614 | let (ty, binders) = db.ty(type_alias.into()).into_value_and_skipped_binders(); |
625 | let value_bound = rust_ir::AssociatedTyValueBound { ty: ty.to_chalk(db) }; | 615 | let value_bound = rust_ir::AssociatedTyValueBound { ty }; |
626 | let value = rust_ir::AssociatedTyValue { | 616 | let value = rust_ir::AssociatedTyValue { |
627 | impl_id: impl_id.to_chalk(db), | 617 | impl_id: impl_id.to_chalk(db), |
628 | associated_ty_id: to_assoc_type_id(assoc_ty), | 618 | associated_ty_id: to_assoc_type_id(assoc_ty), |
@@ -643,13 +633,13 @@ pub(crate) fn fn_def_datum_query( | |||
643 | let where_clauses = convert_where_clauses(db, callable_def.into(), &bound_vars); | 633 | let where_clauses = convert_where_clauses(db, callable_def.into(), &bound_vars); |
644 | let bound = rust_ir::FnDefDatumBound { | 634 | let bound = rust_ir::FnDefDatumBound { |
645 | // Note: Chalk doesn't actually use this information yet as far as I am aware, but we provide it anyway | 635 | // Note: Chalk doesn't actually use this information yet as far as I am aware, but we provide it anyway |
646 | inputs_and_output: make_binders( | 636 | inputs_and_output: make_only_type_binders( |
637 | 0, | ||
647 | rust_ir::FnDefInputsAndOutputDatum { | 638 | rust_ir::FnDefInputsAndOutputDatum { |
648 | argument_types: sig.params().iter().map(|ty| ty.clone().to_chalk(db)).collect(), | 639 | argument_types: sig.params().iter().cloned().collect(), |
649 | return_type: sig.ret().clone().to_chalk(db), | 640 | return_type: sig.ret().clone(), |
650 | } | 641 | } |
651 | .shifted_in(&Interner), | 642 | .shifted_in(&Interner), |
652 | 0, | ||
653 | ), | 643 | ), |
654 | where_clauses, | 644 | where_clauses, |
655 | }; | 645 | }; |
@@ -686,42 +676,65 @@ pub(crate) fn adt_variance_query( | |||
686 | ) | 676 | ) |
687 | } | 677 | } |
688 | 678 | ||
689 | impl From<FnDefId> for crate::db::InternedCallableDefId { | 679 | pub(super) fn convert_where_clauses( |
690 | fn from(fn_def_id: FnDefId) -> Self { | 680 | db: &dyn HirDatabase, |
691 | InternKey::from_intern_id(fn_def_id.0) | 681 | def: GenericDefId, |
692 | } | 682 | substs: &Substitution, |
693 | } | 683 | ) -> Vec<chalk_ir::QuantifiedWhereClause<Interner>> { |
694 | 684 | let generic_predicates = db.generic_predicates(def); | |
695 | impl From<crate::db::InternedCallableDefId> for FnDefId { | 685 | let mut result = Vec::with_capacity(generic_predicates.len()); |
696 | fn from(callable_def_id: crate::db::InternedCallableDefId) -> Self { | 686 | for pred in generic_predicates.iter() { |
697 | chalk_ir::FnDefId(callable_def_id.as_intern_id()) | 687 | result.push(pred.clone().substitute(&Interner, substs)); |
698 | } | 688 | } |
699 | } | 689 | result |
700 | |||
701 | impl From<OpaqueTyId> for crate::db::InternedOpaqueTyId { | ||
702 | fn from(id: OpaqueTyId) -> Self { | ||
703 | InternKey::from_intern_id(id.0) | ||
704 | } | ||
705 | } | ||
706 | |||
707 | impl From<crate::db::InternedOpaqueTyId> for OpaqueTyId { | ||
708 | fn from(id: crate::db::InternedOpaqueTyId) -> Self { | ||
709 | chalk_ir::OpaqueTyId(id.as_intern_id()) | ||
710 | } | ||
711 | } | ||
712 | |||
713 | impl From<chalk_ir::ClosureId<Interner>> for crate::db::InternedClosureId { | ||
714 | fn from(id: chalk_ir::ClosureId<Interner>) -> Self { | ||
715 | Self::from_intern_id(id.0) | ||
716 | } | ||
717 | } | 690 | } |
718 | 691 | ||
719 | impl From<crate::db::InternedClosureId> for chalk_ir::ClosureId<Interner> { | 692 | pub(super) fn generic_predicate_to_inline_bound( |
720 | fn from(id: crate::db::InternedClosureId) -> Self { | 693 | db: &dyn HirDatabase, |
721 | chalk_ir::ClosureId(id.as_intern_id()) | 694 | pred: &QuantifiedWhereClause, |
695 | self_ty: &Ty, | ||
696 | ) -> Option<chalk_ir::Binders<rust_ir::InlineBound<Interner>>> { | ||
697 | // An InlineBound is like a GenericPredicate, except the self type is left out. | ||
698 | // We don't have a special type for this, but Chalk does. | ||
699 | let self_ty_shifted_in = self_ty.clone().shifted_in_from(&Interner, DebruijnIndex::ONE); | ||
700 | let (pred, binders) = pred.as_ref().into_value_and_skipped_binders(); | ||
701 | match pred { | ||
702 | WhereClause::Implemented(trait_ref) => { | ||
703 | if trait_ref.self_type_parameter(&Interner) != self_ty_shifted_in { | ||
704 | // we can only convert predicates back to type bounds if they | ||
705 | // have the expected self type | ||
706 | return None; | ||
707 | } | ||
708 | let args_no_self = trait_ref.substitution.as_slice(&Interner)[1..] | ||
709 | .iter() | ||
710 | .map(|ty| ty.clone().cast(&Interner)) | ||
711 | .collect(); | ||
712 | let trait_bound = rust_ir::TraitBound { trait_id: trait_ref.trait_id, args_no_self }; | ||
713 | Some(chalk_ir::Binders::new(binders, rust_ir::InlineBound::TraitBound(trait_bound))) | ||
714 | } | ||
715 | WhereClause::AliasEq(AliasEq { alias: AliasTy::Projection(projection_ty), ty }) => { | ||
716 | if projection_ty.self_type_parameter(&Interner) != self_ty_shifted_in { | ||
717 | return None; | ||
718 | } | ||
719 | let trait_ = projection_ty.trait_(db); | ||
720 | let args_no_self = projection_ty.substitution.as_slice(&Interner)[1..] | ||
721 | .iter() | ||
722 | .map(|ty| ty.clone().cast(&Interner)) | ||
723 | .collect(); | ||
724 | let alias_eq_bound = rust_ir::AliasEqBound { | ||
725 | value: ty.clone(), | ||
726 | trait_bound: rust_ir::TraitBound { | ||
727 | trait_id: to_chalk_trait_id(trait_), | ||
728 | args_no_self, | ||
729 | }, | ||
730 | associated_ty_id: projection_ty.associated_ty_id, | ||
731 | parameters: Vec::new(), // FIXME we don't support generic associated types yet | ||
732 | }; | ||
733 | Some(chalk_ir::Binders::new( | ||
734 | binders, | ||
735 | rust_ir::InlineBound::AliasEqBound(alias_eq_bound), | ||
736 | )) | ||
737 | } | ||
738 | _ => None, | ||
722 | } | 739 | } |
723 | } | 740 | } |
724 | |||
725 | fn wrap_in_empty_binders<T: crate::TypeWalk>(value: T) -> crate::Binders<T> { | ||
726 | crate::Binders::wrap_empty(value) | ||
727 | } | ||
diff --git a/crates/hir_ty/src/chalk_ext.rs b/crates/hir_ty/src/chalk_ext.rs index b7463366b..8c4542956 100644 --- a/crates/hir_ty/src/chalk_ext.rs +++ b/crates/hir_ty/src/chalk_ext.rs | |||
@@ -1,13 +1,305 @@ | |||
1 | //! Various extensions traits for Chalk types. | 1 | //! Various extensions traits for Chalk types. |
2 | 2 | ||
3 | use crate::{Interner, Ty, TyKind}; | 3 | use chalk_ir::Mutability; |
4 | use hir_def::{ | ||
5 | type_ref::Rawness, AssocContainerId, FunctionId, GenericDefId, HasModule, Lookup, TraitId, | ||
6 | }; | ||
7 | |||
8 | use crate::{ | ||
9 | db::HirDatabase, from_assoc_type_id, from_chalk_trait_id, from_foreign_def_id, | ||
10 | from_placeholder_idx, to_chalk_trait_id, AdtId, AliasEq, AliasTy, Binders, CallableDefId, | ||
11 | CallableSig, FnPointer, ImplTraitId, Interner, Lifetime, ProjectionTy, QuantifiedWhereClause, | ||
12 | Substitution, TraitRef, Ty, TyBuilder, TyKind, WhereClause, | ||
13 | }; | ||
4 | 14 | ||
5 | pub trait TyExt { | 15 | pub trait TyExt { |
6 | fn is_unit(&self) -> bool; | 16 | fn is_unit(&self) -> bool; |
17 | fn is_never(&self) -> bool; | ||
18 | fn is_unknown(&self) -> bool; | ||
19 | |||
20 | fn as_adt(&self) -> Option<(hir_def::AdtId, &Substitution)>; | ||
21 | fn as_tuple(&self) -> Option<&Substitution>; | ||
22 | fn as_fn_def(&self, db: &dyn HirDatabase) -> Option<FunctionId>; | ||
23 | fn as_reference(&self) -> Option<(&Ty, Lifetime, Mutability)>; | ||
24 | fn as_reference_or_ptr(&self) -> Option<(&Ty, Rawness, Mutability)>; | ||
25 | fn as_generic_def(&self, db: &dyn HirDatabase) -> Option<GenericDefId>; | ||
26 | |||
27 | fn callable_def(&self, db: &dyn HirDatabase) -> Option<CallableDefId>; | ||
28 | fn callable_sig(&self, db: &dyn HirDatabase) -> Option<CallableSig>; | ||
29 | |||
30 | fn strip_references(&self) -> &Ty; | ||
31 | |||
32 | /// If this is a `dyn Trait`, returns that trait. | ||
33 | fn dyn_trait(&self) -> Option<TraitId>; | ||
34 | |||
35 | fn impl_trait_bounds(&self, db: &dyn HirDatabase) -> Option<Vec<QuantifiedWhereClause>>; | ||
36 | fn associated_type_parent_trait(&self, db: &dyn HirDatabase) -> Option<TraitId>; | ||
37 | |||
38 | /// FIXME: Get rid of this, it's not a good abstraction | ||
39 | fn equals_ctor(&self, other: &Ty) -> bool; | ||
7 | } | 40 | } |
8 | 41 | ||
9 | impl TyExt for Ty { | 42 | impl TyExt for Ty { |
10 | fn is_unit(&self) -> bool { | 43 | fn is_unit(&self) -> bool { |
11 | matches!(self.kind(&Interner), TyKind::Tuple(0, _)) | 44 | matches!(self.kind(&Interner), TyKind::Tuple(0, _)) |
12 | } | 45 | } |
46 | |||
47 | fn is_never(&self) -> bool { | ||
48 | matches!(self.kind(&Interner), TyKind::Never) | ||
49 | } | ||
50 | |||
51 | fn is_unknown(&self) -> bool { | ||
52 | matches!(self.kind(&Interner), TyKind::Error) | ||
53 | } | ||
54 | |||
55 | fn as_adt(&self) -> Option<(hir_def::AdtId, &Substitution)> { | ||
56 | match self.kind(&Interner) { | ||
57 | TyKind::Adt(AdtId(adt), parameters) => Some((*adt, parameters)), | ||
58 | _ => None, | ||
59 | } | ||
60 | } | ||
61 | |||
62 | fn as_tuple(&self) -> Option<&Substitution> { | ||
63 | match self.kind(&Interner) { | ||
64 | TyKind::Tuple(_, substs) => Some(substs), | ||
65 | _ => None, | ||
66 | } | ||
67 | } | ||
68 | |||
69 | fn as_fn_def(&self, db: &dyn HirDatabase) -> Option<FunctionId> { | ||
70 | if let Some(CallableDefId::FunctionId(func)) = self.callable_def(db) { | ||
71 | Some(func) | ||
72 | } else { | ||
73 | None | ||
74 | } | ||
75 | } | ||
76 | fn as_reference(&self) -> Option<(&Ty, Lifetime, Mutability)> { | ||
77 | match self.kind(&Interner) { | ||
78 | TyKind::Ref(mutability, lifetime, ty) => Some((ty, lifetime.clone(), *mutability)), | ||
79 | _ => None, | ||
80 | } | ||
81 | } | ||
82 | |||
83 | fn as_reference_or_ptr(&self) -> Option<(&Ty, Rawness, Mutability)> { | ||
84 | match self.kind(&Interner) { | ||
85 | TyKind::Ref(mutability, _, ty) => Some((ty, Rawness::Ref, *mutability)), | ||
86 | TyKind::Raw(mutability, ty) => Some((ty, Rawness::RawPtr, *mutability)), | ||
87 | _ => None, | ||
88 | } | ||
89 | } | ||
90 | |||
91 | fn as_generic_def(&self, db: &dyn HirDatabase) -> Option<GenericDefId> { | ||
92 | match *self.kind(&Interner) { | ||
93 | TyKind::Adt(AdtId(adt), ..) => Some(adt.into()), | ||
94 | TyKind::FnDef(callable, ..) => { | ||
95 | Some(db.lookup_intern_callable_def(callable.into()).into()) | ||
96 | } | ||
97 | TyKind::AssociatedType(type_alias, ..) => Some(from_assoc_type_id(type_alias).into()), | ||
98 | TyKind::Foreign(type_alias, ..) => Some(from_foreign_def_id(type_alias).into()), | ||
99 | _ => None, | ||
100 | } | ||
101 | } | ||
102 | |||
103 | fn callable_def(&self, db: &dyn HirDatabase) -> Option<CallableDefId> { | ||
104 | match self.kind(&Interner) { | ||
105 | &TyKind::FnDef(def, ..) => Some(db.lookup_intern_callable_def(def.into())), | ||
106 | _ => None, | ||
107 | } | ||
108 | } | ||
109 | |||
110 | fn callable_sig(&self, db: &dyn HirDatabase) -> Option<CallableSig> { | ||
111 | match self.kind(&Interner) { | ||
112 | TyKind::Function(fn_ptr) => Some(CallableSig::from_fn_ptr(fn_ptr)), | ||
113 | TyKind::FnDef(def, parameters) => { | ||
114 | let callable_def = db.lookup_intern_callable_def((*def).into()); | ||
115 | let sig = db.callable_item_signature(callable_def); | ||
116 | Some(sig.substitute(&Interner, ¶meters)) | ||
117 | } | ||
118 | TyKind::Closure(.., substs) => { | ||
119 | let sig_param = substs.at(&Interner, 0).assert_ty_ref(&Interner); | ||
120 | sig_param.callable_sig(db) | ||
121 | } | ||
122 | _ => None, | ||
123 | } | ||
124 | } | ||
125 | |||
126 | fn dyn_trait(&self) -> Option<TraitId> { | ||
127 | let trait_ref = match self.kind(&Interner) { | ||
128 | TyKind::Dyn(dyn_ty) => dyn_ty.bounds.skip_binders().interned().get(0).and_then(|b| { | ||
129 | match b.skip_binders() { | ||
130 | WhereClause::Implemented(trait_ref) => Some(trait_ref), | ||
131 | _ => None, | ||
132 | } | ||
133 | }), | ||
134 | _ => None, | ||
135 | }?; | ||
136 | Some(from_chalk_trait_id(trait_ref.trait_id)) | ||
137 | } | ||
138 | |||
139 | fn strip_references(&self) -> &Ty { | ||
140 | let mut t: &Ty = self; | ||
141 | while let TyKind::Ref(_mutability, _lifetime, ty) = t.kind(&Interner) { | ||
142 | t = ty; | ||
143 | } | ||
144 | t | ||
145 | } | ||
146 | |||
147 | fn impl_trait_bounds(&self, db: &dyn HirDatabase) -> Option<Vec<QuantifiedWhereClause>> { | ||
148 | match self.kind(&Interner) { | ||
149 | TyKind::OpaqueType(opaque_ty_id, ..) => { | ||
150 | match db.lookup_intern_impl_trait_id((*opaque_ty_id).into()) { | ||
151 | ImplTraitId::AsyncBlockTypeImplTrait(def, _expr) => { | ||
152 | let krate = def.module(db.upcast()).krate(); | ||
153 | if let Some(future_trait) = db | ||
154 | .lang_item(krate, "future_trait".into()) | ||
155 | .and_then(|item| item.as_trait()) | ||
156 | { | ||
157 | // This is only used by type walking. | ||
158 | // Parameters will be walked outside, and projection predicate is not used. | ||
159 | // So just provide the Future trait. | ||
160 | let impl_bound = Binders::empty( | ||
161 | &Interner, | ||
162 | WhereClause::Implemented(TraitRef { | ||
163 | trait_id: to_chalk_trait_id(future_trait), | ||
164 | substitution: Substitution::empty(&Interner), | ||
165 | }), | ||
166 | ); | ||
167 | Some(vec![impl_bound]) | ||
168 | } else { | ||
169 | None | ||
170 | } | ||
171 | } | ||
172 | ImplTraitId::ReturnTypeImplTrait(..) => None, | ||
173 | } | ||
174 | } | ||
175 | TyKind::Alias(AliasTy::Opaque(opaque_ty)) => { | ||
176 | let predicates = match db.lookup_intern_impl_trait_id(opaque_ty.opaque_ty_id.into()) | ||
177 | { | ||
178 | ImplTraitId::ReturnTypeImplTrait(func, idx) => { | ||
179 | db.return_type_impl_traits(func).map(|it| { | ||
180 | let data = (*it) | ||
181 | .as_ref() | ||
182 | .map(|rpit| rpit.impl_traits[idx as usize].bounds.clone()); | ||
183 | data.substitute(&Interner, &opaque_ty.substitution) | ||
184 | }) | ||
185 | } | ||
186 | // It always has an parameter for Future::Output type. | ||
187 | ImplTraitId::AsyncBlockTypeImplTrait(..) => unreachable!(), | ||
188 | }; | ||
189 | |||
190 | predicates.map(|it| it.into_value_and_skipped_binders().0) | ||
191 | } | ||
192 | TyKind::Placeholder(idx) => { | ||
193 | let id = from_placeholder_idx(db, *idx); | ||
194 | let generic_params = db.generic_params(id.parent); | ||
195 | let param_data = &generic_params.types[id.local_id]; | ||
196 | match param_data.provenance { | ||
197 | hir_def::generics::TypeParamProvenance::ArgumentImplTrait => { | ||
198 | let substs = TyBuilder::type_params_subst(db, id.parent); | ||
199 | let predicates = db | ||
200 | .generic_predicates(id.parent) | ||
201 | .into_iter() | ||
202 | .map(|pred| pred.clone().substitute(&Interner, &substs)) | ||
203 | .filter(|wc| match &wc.skip_binders() { | ||
204 | WhereClause::Implemented(tr) => { | ||
205 | &tr.self_type_parameter(&Interner) == self | ||
206 | } | ||
207 | WhereClause::AliasEq(AliasEq { | ||
208 | alias: AliasTy::Projection(proj), | ||
209 | ty: _, | ||
210 | }) => &proj.self_type_parameter(&Interner) == self, | ||
211 | _ => false, | ||
212 | }) | ||
213 | .collect::<Vec<_>>(); | ||
214 | |||
215 | Some(predicates) | ||
216 | } | ||
217 | _ => None, | ||
218 | } | ||
219 | } | ||
220 | _ => None, | ||
221 | } | ||
222 | } | ||
223 | |||
224 | fn associated_type_parent_trait(&self, db: &dyn HirDatabase) -> Option<TraitId> { | ||
225 | match self.kind(&Interner) { | ||
226 | TyKind::AssociatedType(id, ..) => { | ||
227 | match from_assoc_type_id(*id).lookup(db.upcast()).container { | ||
228 | AssocContainerId::TraitId(trait_id) => Some(trait_id), | ||
229 | _ => None, | ||
230 | } | ||
231 | } | ||
232 | TyKind::Alias(AliasTy::Projection(projection_ty)) => { | ||
233 | match from_assoc_type_id(projection_ty.associated_ty_id) | ||
234 | .lookup(db.upcast()) | ||
235 | .container | ||
236 | { | ||
237 | AssocContainerId::TraitId(trait_id) => Some(trait_id), | ||
238 | _ => None, | ||
239 | } | ||
240 | } | ||
241 | _ => None, | ||
242 | } | ||
243 | } | ||
244 | |||
245 | fn equals_ctor(&self, other: &Ty) -> bool { | ||
246 | match (self.kind(&Interner), other.kind(&Interner)) { | ||
247 | (TyKind::Adt(adt, ..), TyKind::Adt(adt2, ..)) => adt == adt2, | ||
248 | (TyKind::Slice(_), TyKind::Slice(_)) | (TyKind::Array(_, _), TyKind::Array(_, _)) => { | ||
249 | true | ||
250 | } | ||
251 | (TyKind::FnDef(def_id, ..), TyKind::FnDef(def_id2, ..)) => def_id == def_id2, | ||
252 | (TyKind::OpaqueType(ty_id, ..), TyKind::OpaqueType(ty_id2, ..)) => ty_id == ty_id2, | ||
253 | (TyKind::AssociatedType(ty_id, ..), TyKind::AssociatedType(ty_id2, ..)) => { | ||
254 | ty_id == ty_id2 | ||
255 | } | ||
256 | (TyKind::Foreign(ty_id, ..), TyKind::Foreign(ty_id2, ..)) => ty_id == ty_id2, | ||
257 | (TyKind::Closure(id1, _), TyKind::Closure(id2, _)) => id1 == id2, | ||
258 | (TyKind::Ref(mutability, ..), TyKind::Ref(mutability2, ..)) | ||
259 | | (TyKind::Raw(mutability, ..), TyKind::Raw(mutability2, ..)) => { | ||
260 | mutability == mutability2 | ||
261 | } | ||
262 | ( | ||
263 | TyKind::Function(FnPointer { num_binders, sig, .. }), | ||
264 | TyKind::Function(FnPointer { num_binders: num_binders2, sig: sig2, .. }), | ||
265 | ) => num_binders == num_binders2 && sig == sig2, | ||
266 | (TyKind::Tuple(cardinality, _), TyKind::Tuple(cardinality2, _)) => { | ||
267 | cardinality == cardinality2 | ||
268 | } | ||
269 | (TyKind::Str, TyKind::Str) | (TyKind::Never, TyKind::Never) => true, | ||
270 | (TyKind::Scalar(scalar), TyKind::Scalar(scalar2)) => scalar == scalar2, | ||
271 | _ => false, | ||
272 | } | ||
273 | } | ||
274 | } | ||
275 | |||
276 | pub trait ProjectionTyExt { | ||
277 | fn trait_ref(&self, db: &dyn HirDatabase) -> TraitRef; | ||
278 | fn trait_(&self, db: &dyn HirDatabase) -> TraitId; | ||
279 | } | ||
280 | |||
281 | impl ProjectionTyExt for ProjectionTy { | ||
282 | fn trait_ref(&self, db: &dyn HirDatabase) -> TraitRef { | ||
283 | TraitRef { | ||
284 | trait_id: to_chalk_trait_id(self.trait_(db)), | ||
285 | substitution: self.substitution.clone(), | ||
286 | } | ||
287 | } | ||
288 | |||
289 | fn trait_(&self, db: &dyn HirDatabase) -> TraitId { | ||
290 | match from_assoc_type_id(self.associated_ty_id).lookup(db.upcast()).container { | ||
291 | AssocContainerId::TraitId(it) => it, | ||
292 | _ => panic!("projection ty without parent trait"), | ||
293 | } | ||
294 | } | ||
295 | } | ||
296 | |||
297 | pub trait TraitRefExt { | ||
298 | fn hir_trait_id(&self) -> TraitId; | ||
299 | } | ||
300 | |||
301 | impl TraitRefExt for TraitRef { | ||
302 | fn hir_trait_id(&self) -> TraitId { | ||
303 | from_chalk_trait_id(self.trait_id) | ||
304 | } | ||
13 | } | 305 | } |
diff --git a/crates/hir_ty/src/db.rs b/crates/hir_ty/src/db.rs index 4300680d9..1690926ad 100644 --- a/crates/hir_ty/src/db.rs +++ b/crates/hir_ty/src/db.rs | |||
@@ -1,18 +1,19 @@ | |||
1 | //! FIXME: write short doc here | 1 | //! The home of `HirDatabase`, which is the Salsa database containing all the |
2 | //! type inference-related queries. | ||
2 | 3 | ||
3 | use std::sync::Arc; | 4 | use std::sync::Arc; |
4 | 5 | ||
5 | use base_db::{impl_intern_key, salsa, CrateId, Upcast}; | 6 | use base_db::{impl_intern_key, salsa, CrateId, Upcast}; |
6 | use hir_def::{ | 7 | use hir_def::{ |
7 | db::DefDatabase, expr::ExprId, ConstParamId, DefWithBodyId, FunctionId, GenericDefId, ImplId, | 8 | db::DefDatabase, expr::ExprId, ConstParamId, DefWithBodyId, FunctionId, GenericDefId, ImplId, |
8 | LocalFieldId, TypeParamId, VariantId, | 9 | LifetimeParamId, LocalFieldId, TypeParamId, VariantId, |
9 | }; | 10 | }; |
10 | use la_arena::ArenaMap; | 11 | use la_arena::ArenaMap; |
11 | 12 | ||
12 | use crate::{ | 13 | use crate::{ |
14 | chalk_db, | ||
13 | method_resolution::{InherentImpls, TraitImpls}, | 15 | method_resolution::{InherentImpls, TraitImpls}, |
14 | traits::chalk, | 16 | Binders, CallableDefId, FnDefId, ImplTraitId, InferenceResult, Interner, PolyFnSig, |
15 | Binders, CallableDefId, FnDefId, ImplTraitId, InferenceResult, PolyFnSig, | ||
16 | QuantifiedWhereClause, ReturnTypeImplTraits, TraitRef, Ty, TyDefId, ValueTyDefId, | 17 | QuantifiedWhereClause, ReturnTypeImplTraits, TraitRef, Ty, TyDefId, ValueTyDefId, |
17 | }; | 18 | }; |
18 | use hir_expand::name::Name; | 19 | use hir_expand::name::Name; |
@@ -86,37 +87,46 @@ pub trait HirDatabase: DefDatabase + Upcast<dyn DefDatabase> { | |||
86 | #[salsa::interned] | 87 | #[salsa::interned] |
87 | fn intern_type_param_id(&self, param_id: TypeParamId) -> InternedTypeParamId; | 88 | fn intern_type_param_id(&self, param_id: TypeParamId) -> InternedTypeParamId; |
88 | #[salsa::interned] | 89 | #[salsa::interned] |
90 | fn intern_lifetime_param_id(&self, param_id: LifetimeParamId) -> InternedLifetimeParamId; | ||
91 | #[salsa::interned] | ||
92 | fn intern_const_param_id(&self, param_id: ConstParamId) -> InternedConstParamId; | ||
93 | #[salsa::interned] | ||
89 | fn intern_impl_trait_id(&self, id: ImplTraitId) -> InternedOpaqueTyId; | 94 | fn intern_impl_trait_id(&self, id: ImplTraitId) -> InternedOpaqueTyId; |
90 | #[salsa::interned] | 95 | #[salsa::interned] |
91 | fn intern_closure(&self, id: (DefWithBodyId, ExprId)) -> InternedClosureId; | 96 | fn intern_closure(&self, id: (DefWithBodyId, ExprId)) -> InternedClosureId; |
92 | 97 | ||
93 | #[salsa::invoke(chalk::associated_ty_data_query)] | 98 | #[salsa::invoke(chalk_db::associated_ty_data_query)] |
94 | fn associated_ty_data(&self, id: chalk::AssocTypeId) -> Arc<chalk::AssociatedTyDatum>; | 99 | fn associated_ty_data(&self, id: chalk_db::AssocTypeId) -> Arc<chalk_db::AssociatedTyDatum>; |
95 | 100 | ||
96 | #[salsa::invoke(chalk::trait_datum_query)] | 101 | #[salsa::invoke(chalk_db::trait_datum_query)] |
97 | fn trait_datum(&self, krate: CrateId, trait_id: chalk::TraitId) -> Arc<chalk::TraitDatum>; | 102 | fn trait_datum(&self, krate: CrateId, trait_id: chalk_db::TraitId) |
103 | -> Arc<chalk_db::TraitDatum>; | ||
98 | 104 | ||
99 | #[salsa::invoke(chalk::struct_datum_query)] | 105 | #[salsa::invoke(chalk_db::struct_datum_query)] |
100 | fn struct_datum(&self, krate: CrateId, struct_id: chalk::AdtId) -> Arc<chalk::StructDatum>; | 106 | fn struct_datum( |
107 | &self, | ||
108 | krate: CrateId, | ||
109 | struct_id: chalk_db::AdtId, | ||
110 | ) -> Arc<chalk_db::StructDatum>; | ||
101 | 111 | ||
102 | #[salsa::invoke(crate::traits::chalk::impl_datum_query)] | 112 | #[salsa::invoke(chalk_db::impl_datum_query)] |
103 | fn impl_datum(&self, krate: CrateId, impl_id: chalk::ImplId) -> Arc<chalk::ImplDatum>; | 113 | fn impl_datum(&self, krate: CrateId, impl_id: chalk_db::ImplId) -> Arc<chalk_db::ImplDatum>; |
104 | 114 | ||
105 | #[salsa::invoke(crate::traits::chalk::fn_def_datum_query)] | 115 | #[salsa::invoke(chalk_db::fn_def_datum_query)] |
106 | fn fn_def_datum(&self, krate: CrateId, fn_def_id: FnDefId) -> Arc<chalk::FnDefDatum>; | 116 | fn fn_def_datum(&self, krate: CrateId, fn_def_id: FnDefId) -> Arc<chalk_db::FnDefDatum>; |
107 | 117 | ||
108 | #[salsa::invoke(crate::traits::chalk::fn_def_variance_query)] | 118 | #[salsa::invoke(chalk_db::fn_def_variance_query)] |
109 | fn fn_def_variance(&self, krate: CrateId, fn_def_id: FnDefId) -> chalk::Variances; | 119 | fn fn_def_variance(&self, krate: CrateId, fn_def_id: FnDefId) -> chalk_db::Variances; |
110 | 120 | ||
111 | #[salsa::invoke(crate::traits::chalk::adt_variance_query)] | 121 | #[salsa::invoke(chalk_db::adt_variance_query)] |
112 | fn adt_variance(&self, krate: CrateId, adt_id: chalk::AdtId) -> chalk::Variances; | 122 | fn adt_variance(&self, krate: CrateId, adt_id: chalk_db::AdtId) -> chalk_db::Variances; |
113 | 123 | ||
114 | #[salsa::invoke(crate::traits::chalk::associated_ty_value_query)] | 124 | #[salsa::invoke(chalk_db::associated_ty_value_query)] |
115 | fn associated_ty_value( | 125 | fn associated_ty_value( |
116 | &self, | 126 | &self, |
117 | krate: CrateId, | 127 | krate: CrateId, |
118 | id: chalk::AssociatedTyValueId, | 128 | id: chalk_db::AssociatedTyValueId, |
119 | ) -> Arc<chalk::AssociatedTyValue>; | 129 | ) -> Arc<chalk_db::AssociatedTyValue>; |
120 | 130 | ||
121 | #[salsa::invoke(crate::traits::trait_solve_query)] | 131 | #[salsa::invoke(crate::traits::trait_solve_query)] |
122 | fn trait_solve( | 132 | fn trait_solve( |
@@ -125,12 +135,12 @@ pub trait HirDatabase: DefDatabase + Upcast<dyn DefDatabase> { | |||
125 | goal: crate::Canonical<crate::InEnvironment<crate::DomainGoal>>, | 135 | goal: crate::Canonical<crate::InEnvironment<crate::DomainGoal>>, |
126 | ) -> Option<crate::Solution>; | 136 | ) -> Option<crate::Solution>; |
127 | 137 | ||
128 | #[salsa::invoke(crate::traits::chalk::program_clauses_for_chalk_env_query)] | 138 | #[salsa::invoke(chalk_db::program_clauses_for_chalk_env_query)] |
129 | fn program_clauses_for_chalk_env( | 139 | fn program_clauses_for_chalk_env( |
130 | &self, | 140 | &self, |
131 | krate: CrateId, | 141 | krate: CrateId, |
132 | env: chalk_ir::Environment<chalk::Interner>, | 142 | env: chalk_ir::Environment<Interner>, |
133 | ) -> chalk_ir::ProgramClauses<chalk::Interner>; | 143 | ) -> chalk_ir::ProgramClauses<Interner>; |
134 | } | 144 | } |
135 | 145 | ||
136 | fn infer_wait(db: &dyn HirDatabase, def: DefWithBodyId) -> Arc<InferenceResult> { | 146 | fn infer_wait(db: &dyn HirDatabase, def: DefWithBodyId) -> Arc<InferenceResult> { |
@@ -156,6 +166,14 @@ pub struct InternedTypeParamId(salsa::InternId); | |||
156 | impl_intern_key!(InternedTypeParamId); | 166 | impl_intern_key!(InternedTypeParamId); |
157 | 167 | ||
158 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | 168 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
169 | pub struct InternedLifetimeParamId(salsa::InternId); | ||
170 | impl_intern_key!(InternedLifetimeParamId); | ||
171 | |||
172 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | ||
173 | pub struct InternedConstParamId(salsa::InternId); | ||
174 | impl_intern_key!(InternedConstParamId); | ||
175 | |||
176 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | ||
159 | pub struct InternedOpaqueTyId(salsa::InternId); | 177 | pub struct InternedOpaqueTyId(salsa::InternId); |
160 | impl_intern_key!(InternedOpaqueTyId); | 178 | impl_intern_key!(InternedOpaqueTyId); |
161 | 179 | ||
diff --git a/crates/hir_ty/src/diagnostics.rs b/crates/hir_ty/src/diagnostics.rs index 86f937e1d..84fc8ce14 100644 --- a/crates/hir_ty/src/diagnostics.rs +++ b/crates/hir_ty/src/diagnostics.rs | |||
@@ -1,4 +1,4 @@ | |||
1 | //! FIXME: write short doc here | 1 | //! Type inference-based diagnostics. |
2 | mod expr; | 2 | mod expr; |
3 | mod match_check; | 3 | mod match_check; |
4 | mod unsafe_check; | 4 | mod unsafe_check; |
diff --git a/crates/hir_ty/src/diagnostics/expr.rs b/crates/hir_ty/src/diagnostics/expr.rs index db278d0db..79602c3dd 100644 --- a/crates/hir_ty/src/diagnostics/expr.rs +++ b/crates/hir_ty/src/diagnostics/expr.rs | |||
@@ -14,7 +14,6 @@ use crate::{ | |||
14 | MismatchedArgCount, MissingFields, MissingMatchArms, MissingOkOrSomeInTailExpr, | 14 | MismatchedArgCount, MissingFields, MissingMatchArms, MissingOkOrSomeInTailExpr, |