diff options
author | Aleksey Kladov <[email protected]> | 2019-11-21 10:32:03 +0000 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2019-11-21 10:48:05 +0000 |
commit | 621cf06156975f8bd75e35af46da034f72e11fad (patch) | |
tree | f06e26fc377bd36961a5c8fb41e01423e4e1a01d /crates/ra_hir | |
parent | 24964ca58e057b3f32d5cbb17d84e46f2a236ff6 (diff) |
Decouple
Diffstat (limited to 'crates/ra_hir')
-rw-r--r-- | crates/ra_hir/src/from_id.rs | 23 | ||||
-rw-r--r-- | crates/ra_hir/src/resolve.rs | 42 | ||||
-rw-r--r-- | crates/ra_hir/src/source_binder.rs | 12 | ||||
-rw-r--r-- | crates/ra_hir/src/ty/infer/expr.rs | 2 | ||||
-rw-r--r-- | crates/ra_hir/src/ty/infer/path.rs | 23 |
5 files changed, 59 insertions, 43 deletions
diff --git a/crates/ra_hir/src/from_id.rs b/crates/ra_hir/src/from_id.rs index 7042422cc..e294e957b 100644 --- a/crates/ra_hir/src/from_id.rs +++ b/crates/ra_hir/src/from_id.rs | |||
@@ -4,12 +4,13 @@ | |||
4 | //! are splitting the hir. | 4 | //! are splitting the hir. |
5 | 5 | ||
6 | use hir_def::{ | 6 | use hir_def::{ |
7 | AdtId, AssocItemId, DefWithBodyId, EnumId, EnumVariantId, GenericDefId, ModuleDefId, StructId, | 7 | AdtId, AssocItemId, ConstId, DefWithBodyId, EnumId, EnumVariantId, FunctionId, GenericDefId, |
8 | TypeAliasId, UnionId, | 8 | ModuleDefId, StaticId, StructId, TypeAliasId, UnionId, |
9 | }; | 9 | }; |
10 | 10 | ||
11 | use crate::{ | 11 | use crate::{ |
12 | ty::TypableDef, Adt, AssocItem, DefWithBody, EnumVariant, GenericDef, ModuleDef, TypeAlias, | 12 | ty::TypableDef, Adt, AssocItem, Const, DefWithBody, EnumVariant, Function, GenericDef, |
13 | ModuleDef, Static, TypeAlias, | ||
13 | }; | 14 | }; |
14 | 15 | ||
15 | macro_rules! from_id { | 16 | macro_rules! from_id { |
@@ -174,6 +175,22 @@ impl From<TypeAliasId> for TypableDef { | |||
174 | } | 175 | } |
175 | } | 176 | } |
176 | 177 | ||
178 | impl From<FunctionId> for TypableDef { | ||
179 | fn from(id: FunctionId) -> Self { | ||
180 | Function::from(id).into() | ||
181 | } | ||
182 | } | ||
183 | impl From<ConstId> for TypableDef { | ||
184 | fn from(id: ConstId) -> Self { | ||
185 | Const::from(id).into() | ||
186 | } | ||
187 | } | ||
188 | impl From<StaticId> for TypableDef { | ||
189 | fn from(id: StaticId) -> Self { | ||
190 | Static::from(id).into() | ||
191 | } | ||
192 | } | ||
193 | |||
177 | impl From<Adt> for GenericDefId { | 194 | impl From<Adt> for GenericDefId { |
178 | fn from(id: Adt) -> Self { | 195 | fn from(id: Adt) -> Self { |
179 | match id { | 196 | match id { |
diff --git a/crates/ra_hir/src/resolve.rs b/crates/ra_hir/src/resolve.rs index 899959532..770864ec5 100644 --- a/crates/ra_hir/src/resolve.rs +++ b/crates/ra_hir/src/resolve.rs | |||
@@ -8,8 +8,8 @@ use hir_def::{ | |||
8 | generics::GenericParams, | 8 | generics::GenericParams, |
9 | nameres::CrateDefMap, | 9 | nameres::CrateDefMap, |
10 | path::{Path, PathKind}, | 10 | path::{Path, PathKind}, |
11 | AdtId, CrateModuleId, DefWithBodyId, EnumId, EnumVariantId, GenericDefId, ImplId, ModuleDefId, | 11 | AdtId, ConstId, CrateModuleId, DefWithBodyId, EnumId, EnumVariantId, FunctionId, GenericDefId, |
12 | StructId, TraitId, TypeAliasId, | 12 | ImplId, ModuleDefId, StaticId, StructId, TraitId, TypeAliasId, |
13 | }; | 13 | }; |
14 | use hir_expand::name::{self, Name}; | 14 | use hir_expand::name::{self, Name}; |
15 | use rustc_hash::FxHashSet; | 15 | use rustc_hash::FxHashSet; |
@@ -18,8 +18,8 @@ use crate::{ | |||
18 | code_model::Crate, | 18 | code_model::Crate, |
19 | db::{DefDatabase, HirDatabase}, | 19 | db::{DefDatabase, HirDatabase}, |
20 | expr::{ExprScopes, PatId, ScopeId}, | 20 | expr::{ExprScopes, PatId, ScopeId}, |
21 | Adt, Const, Container, DefWithBody, EnumVariant, Function, GenericDef, HasBody, ImplBlock, | 21 | Adt, Const, Container, DefWithBody, Function, GenericDef, ImplBlock, Local, MacroDef, Module, |
22 | Local, MacroDef, Module, ModuleDef, PerNs, Static, Struct, Trait, TypeAlias, | 22 | ModuleDef, PerNs, Static, Trait, TypeAlias, |
23 | }; | 23 | }; |
24 | 24 | ||
25 | #[derive(Debug, Clone, Default)] | 25 | #[derive(Debug, Clone, Default)] |
@@ -79,11 +79,11 @@ pub(crate) enum ResolveValueResult { | |||
79 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | 79 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] |
80 | pub(crate) enum ValueNs { | 80 | pub(crate) enum ValueNs { |
81 | LocalBinding(PatId), | 81 | LocalBinding(PatId), |
82 | Function(Function), | 82 | FunctionId(FunctionId), |
83 | Const(Const), | 83 | ConstId(ConstId), |
84 | Static(Static), | 84 | StaticId(StaticId), |
85 | Struct(Struct), | 85 | StructId(StructId), |
86 | EnumVariant(EnumVariant), | 86 | EnumVariantId(EnumVariantId), |
87 | } | 87 | } |
88 | 88 | ||
89 | impl Resolver { | 89 | impl Resolver { |
@@ -266,13 +266,11 @@ impl Resolver { | |||
266 | return match idx { | 266 | return match idx { |
267 | None => { | 267 | None => { |
268 | let value = match module_def.take_values()? { | 268 | let value = match module_def.take_values()? { |
269 | ModuleDefId::FunctionId(it) => ValueNs::Function(it.into()), | 269 | ModuleDefId::FunctionId(it) => ValueNs::FunctionId(it), |
270 | ModuleDefId::AdtId(AdtId::StructId(it)) => { | 270 | ModuleDefId::AdtId(AdtId::StructId(it)) => ValueNs::StructId(it), |
271 | ValueNs::Struct(it.into()) | 271 | ModuleDefId::EnumVariantId(it) => ValueNs::EnumVariantId(it), |
272 | } | 272 | ModuleDefId::ConstId(it) => ValueNs::ConstId(it), |
273 | ModuleDefId::EnumVariantId(it) => ValueNs::EnumVariant(it.into()), | 273 | ModuleDefId::StaticId(it) => ValueNs::StaticId(it), |
274 | ModuleDefId::ConstId(it) => ValueNs::Const(it.into()), | ||
275 | ModuleDefId::StaticId(it) => ValueNs::Static(it.into()), | ||
276 | 274 | ||
277 | ModuleDefId::AdtId(AdtId::EnumId(_)) | 275 | ModuleDefId::AdtId(AdtId::EnumId(_)) |
278 | | ModuleDefId::AdtId(AdtId::UnionId(_)) | 276 | | ModuleDefId::AdtId(AdtId::UnionId(_)) |
@@ -496,23 +494,23 @@ impl Scope { | |||
496 | // needs arbitrary_self_types to be a method... or maybe move to the def? | 494 | // needs arbitrary_self_types to be a method... or maybe move to the def? |
497 | pub(crate) fn resolver_for_expr( | 495 | pub(crate) fn resolver_for_expr( |
498 | db: &impl HirDatabase, | 496 | db: &impl HirDatabase, |
499 | owner: DefWithBody, | 497 | owner: DefWithBodyId, |
500 | expr_id: ExprId, | 498 | expr_id: ExprId, |
501 | ) -> Resolver { | 499 | ) -> Resolver { |
502 | let scopes = owner.expr_scopes(db); | 500 | let scopes = db.expr_scopes(owner); |
503 | resolver_for_scope(db, owner, scopes.scope_for(expr_id)) | 501 | resolver_for_scope(db, owner, scopes.scope_for(expr_id)) |
504 | } | 502 | } |
505 | 503 | ||
506 | pub(crate) fn resolver_for_scope( | 504 | pub(crate) fn resolver_for_scope( |
507 | db: &impl HirDatabase, | 505 | db: &impl HirDatabase, |
508 | owner: DefWithBody, | 506 | owner: DefWithBodyId, |
509 | scope_id: Option<ScopeId>, | 507 | scope_id: Option<ScopeId>, |
510 | ) -> Resolver { | 508 | ) -> Resolver { |
511 | let mut r = owner.resolver(db); | 509 | let mut r = DefWithBody::from(owner).resolver(db); |
512 | let scopes = owner.expr_scopes(db); | 510 | let scopes = db.expr_scopes(owner); |
513 | let scope_chain = scopes.scope_chain(scope_id).collect::<Vec<_>>(); | 511 | let scope_chain = scopes.scope_chain(scope_id).collect::<Vec<_>>(); |
514 | for scope in scope_chain.into_iter().rev() { | 512 | for scope in scope_chain.into_iter().rev() { |
515 | r = r.push_expr_scope(owner.into(), Arc::clone(&scopes), scope); | 513 | r = r.push_expr_scope(owner, Arc::clone(&scopes), scope); |
516 | } | 514 | } |
517 | r | 515 | r |
518 | } | 516 | } |
diff --git a/crates/ra_hir/src/source_binder.rs b/crates/ra_hir/src/source_binder.rs index 29c928d51..09df4f0aa 100644 --- a/crates/ra_hir/src/source_binder.rs +++ b/crates/ra_hir/src/source_binder.rs | |||
@@ -160,7 +160,7 @@ impl SourceAnalyzer { | |||
160 | None => scope_for(&scopes, &source_map, node), | 160 | None => scope_for(&scopes, &source_map, node), |
161 | Some(offset) => scope_for_offset(&scopes, &source_map, node.with_value(offset)), | 161 | Some(offset) => scope_for_offset(&scopes, &source_map, node.with_value(offset)), |
162 | }; | 162 | }; |
163 | let resolver = resolver_for_scope(db, def, scope); | 163 | let resolver = resolver_for_scope(db, def.into(), scope); |
164 | SourceAnalyzer { | 164 | SourceAnalyzer { |
165 | resolver, | 165 | resolver, |
166 | body_owner: Some(def), | 166 | body_owner: Some(def), |
@@ -260,11 +260,11 @@ impl SourceAnalyzer { | |||
260 | let var = Local { parent: self.body_owner?, pat_id }; | 260 | let var = Local { parent: self.body_owner?, pat_id }; |
261 | PathResolution::Local(var) | 261 | PathResolution::Local(var) |
262 | } | 262 | } |
263 | ValueNs::Function(it) => PathResolution::Def(it.into()), | 263 | ValueNs::FunctionId(it) => PathResolution::Def(Function::from(it).into()), |
264 | ValueNs::Const(it) => PathResolution::Def(it.into()), | 264 | ValueNs::ConstId(it) => PathResolution::Def(Const::from(it).into()), |
265 | ValueNs::Static(it) => PathResolution::Def(it.into()), | 265 | ValueNs::StaticId(it) => PathResolution::Def(Static::from(it).into()), |
266 | ValueNs::Struct(it) => PathResolution::Def(it.into()), | 266 | ValueNs::StructId(it) => PathResolution::Def(Struct::from(it).into()), |
267 | ValueNs::EnumVariant(it) => PathResolution::Def(it.into()), | 267 | ValueNs::EnumVariantId(it) => PathResolution::Def(EnumVariant::from(it).into()), |
268 | }; | 268 | }; |
269 | Some(res) | 269 | Some(res) |
270 | }); | 270 | }); |
diff --git a/crates/ra_hir/src/ty/infer/expr.rs b/crates/ra_hir/src/ty/infer/expr.rs index 414b06ba1..1ac2709f5 100644 --- a/crates/ra_hir/src/ty/infer/expr.rs +++ b/crates/ra_hir/src/ty/infer/expr.rs | |||
@@ -187,7 +187,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { | |||
187 | } | 187 | } |
188 | Expr::Path(p) => { | 188 | Expr::Path(p) => { |
189 | // FIXME this could be more efficient... | 189 | // FIXME this could be more efficient... |
190 | let resolver = resolver_for_expr(self.db, self.owner, tgt_expr); | 190 | let resolver = resolver_for_expr(self.db, self.owner.into(), tgt_expr); |
191 | self.infer_path(&resolver, p, tgt_expr.into()).unwrap_or(Ty::Unknown) | 191 | self.infer_path(&resolver, p, tgt_expr.into()).unwrap_or(Ty::Unknown) |
192 | } | 192 | } |
193 | Expr::Continue => Ty::simple(TypeCtor::Never), | 193 | Expr::Continue => Ty::simple(TypeCtor::Never), |
diff --git a/crates/ra_hir/src/ty/infer/path.rs b/crates/ra_hir/src/ty/infer/path.rs index f36a27929..55a5dbec7 100644 --- a/crates/ra_hir/src/ty/infer/path.rs +++ b/crates/ra_hir/src/ty/infer/path.rs | |||
@@ -8,7 +8,7 @@ use crate::{ | |||
8 | generics::HasGenericParams, | 8 | generics::HasGenericParams, |
9 | resolve::{ResolveValueResult, Resolver, TypeNs, ValueNs}, | 9 | resolve::{ResolveValueResult, Resolver, TypeNs, ValueNs}, |
10 | ty::{method_resolution, Namespace, Substs, Ty, TypableDef, TypeWalk}, | 10 | ty::{method_resolution, Namespace, Substs, Ty, TypableDef, TypeWalk}, |
11 | AssocItem, Container, Name, Path, | 11 | AssocItem, Container, Function, Name, Path, |
12 | }; | 12 | }; |
13 | 13 | ||
14 | impl<'a, D: HirDatabase> InferenceContext<'a, D> { | 14 | impl<'a, D: HirDatabase> InferenceContext<'a, D> { |
@@ -60,11 +60,11 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { | |||
60 | let ty = self.resolve_ty_as_possible(&mut vec![], ty); | 60 | let ty = self.resolve_ty_as_possible(&mut vec![], ty); |
61 | return Some(ty); | 61 | return Some(ty); |
62 | } | 62 | } |
63 | ValueNs::Function(it) => it.into(), | 63 | ValueNs::FunctionId(it) => it.into(), |
64 | ValueNs::Const(it) => it.into(), | 64 | ValueNs::ConstId(it) => it.into(), |
65 | ValueNs::Static(it) => it.into(), | 65 | ValueNs::StaticId(it) => it.into(), |
66 | ValueNs::Struct(it) => it.into(), | 66 | ValueNs::StructId(it) => it.into(), |
67 | ValueNs::EnumVariant(it) => it.into(), | 67 | ValueNs::EnumVariantId(it) => it.into(), |
68 | }; | 68 | }; |
69 | 69 | ||
70 | let mut ty = self.db.type_for_def(typable, Namespace::Values); | 70 | let mut ty = self.db.type_for_def(typable, Namespace::Values); |
@@ -160,8 +160,8 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { | |||
160 | AssocItem::TypeAlias(_) => None, | 160 | AssocItem::TypeAlias(_) => None, |
161 | })?; | 161 | })?; |
162 | let def = match item { | 162 | let def = match item { |
163 | AssocItem::Function(f) => ValueNs::Function(f), | 163 | AssocItem::Function(f) => ValueNs::FunctionId(f.id), |
164 | AssocItem::Const(c) => ValueNs::Const(c), | 164 | AssocItem::Const(c) => ValueNs::ConstId(c.id), |
165 | AssocItem::TypeAlias(_) => unreachable!(), | 165 | AssocItem::TypeAlias(_) => unreachable!(), |
166 | }; | 166 | }; |
167 | let substs = Substs::build_for_def(self.db, item) | 167 | let substs = Substs::build_for_def(self.db, item) |
@@ -193,8 +193,8 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { | |||
193 | method_resolution::LookupMode::Path, | 193 | method_resolution::LookupMode::Path, |
194 | move |_ty, item| { | 194 | move |_ty, item| { |
195 | let def = match item { | 195 | let def = match item { |
196 | AssocItem::Function(f) => ValueNs::Function(f), | 196 | AssocItem::Function(f) => ValueNs::FunctionId(f.id), |
197 | AssocItem::Const(c) => ValueNs::Const(c), | 197 | AssocItem::Const(c) => ValueNs::ConstId(c.id), |
198 | AssocItem::TypeAlias(_) => unreachable!(), | 198 | AssocItem::TypeAlias(_) => unreachable!(), |
199 | }; | 199 | }; |
200 | let substs = match item.container(self.db) { | 200 | let substs = match item.container(self.db) { |
@@ -224,7 +224,8 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { | |||
224 | } | 224 | } |
225 | 225 | ||
226 | fn find_self_types(&self, def: &ValueNs, actual_def_ty: Ty) -> Option<Substs> { | 226 | fn find_self_types(&self, def: &ValueNs, actual_def_ty: Ty) -> Option<Substs> { |
227 | if let ValueNs::Function(func) = def { | 227 | if let ValueNs::FunctionId(func) = def { |
228 | let func = Function::from(*func); | ||
228 | // We only do the infer if parent has generic params | 229 | // We only do the infer if parent has generic params |
229 | let gen = func.generic_params(self.db); | 230 | let gen = func.generic_params(self.db); |
230 | if gen.count_parent_params() == 0 { | 231 | if gen.count_parent_params() == 0 { |