From 621cf06156975f8bd75e35af46da034f72e11fad Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 21 Nov 2019 13:32:03 +0300 Subject: Decouple --- crates/ra_hir/src/from_id.rs | 23 ++++++++++++++++++--- crates/ra_hir/src/resolve.rs | 42 ++++++++++++++++++-------------------- crates/ra_hir/src/source_binder.rs | 12 +++++------ crates/ra_hir/src/ty/infer/expr.rs | 2 +- crates/ra_hir/src/ty/infer/path.rs | 23 +++++++++++---------- 5 files changed, 59 insertions(+), 43 deletions(-) (limited to 'crates/ra_hir') 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 @@ //! are splitting the hir. use hir_def::{ - AdtId, AssocItemId, DefWithBodyId, EnumId, EnumVariantId, GenericDefId, ModuleDefId, StructId, - TypeAliasId, UnionId, + AdtId, AssocItemId, ConstId, DefWithBodyId, EnumId, EnumVariantId, FunctionId, GenericDefId, + ModuleDefId, StaticId, StructId, TypeAliasId, UnionId, }; use crate::{ - ty::TypableDef, Adt, AssocItem, DefWithBody, EnumVariant, GenericDef, ModuleDef, TypeAlias, + ty::TypableDef, Adt, AssocItem, Const, DefWithBody, EnumVariant, Function, GenericDef, + ModuleDef, Static, TypeAlias, }; macro_rules! from_id { @@ -174,6 +175,22 @@ impl From for TypableDef { } } +impl From for TypableDef { + fn from(id: FunctionId) -> Self { + Function::from(id).into() + } +} +impl From for TypableDef { + fn from(id: ConstId) -> Self { + Const::from(id).into() + } +} +impl From for TypableDef { + fn from(id: StaticId) -> Self { + Static::from(id).into() + } +} + impl From for GenericDefId { fn from(id: Adt) -> Self { 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::{ generics::GenericParams, nameres::CrateDefMap, path::{Path, PathKind}, - AdtId, CrateModuleId, DefWithBodyId, EnumId, EnumVariantId, GenericDefId, ImplId, ModuleDefId, - StructId, TraitId, TypeAliasId, + AdtId, ConstId, CrateModuleId, DefWithBodyId, EnumId, EnumVariantId, FunctionId, GenericDefId, + ImplId, ModuleDefId, StaticId, StructId, TraitId, TypeAliasId, }; use hir_expand::name::{self, Name}; use rustc_hash::FxHashSet; @@ -18,8 +18,8 @@ use crate::{ code_model::Crate, db::{DefDatabase, HirDatabase}, expr::{ExprScopes, PatId, ScopeId}, - Adt, Const, Container, DefWithBody, EnumVariant, Function, GenericDef, HasBody, ImplBlock, - Local, MacroDef, Module, ModuleDef, PerNs, Static, Struct, Trait, TypeAlias, + Adt, Const, Container, DefWithBody, Function, GenericDef, ImplBlock, Local, MacroDef, Module, + ModuleDef, PerNs, Static, Trait, TypeAlias, }; #[derive(Debug, Clone, Default)] @@ -79,11 +79,11 @@ pub(crate) enum ResolveValueResult { #[derive(Debug, Clone, PartialEq, Eq, Hash)] pub(crate) enum ValueNs { LocalBinding(PatId), - Function(Function), - Const(Const), - Static(Static), - Struct(Struct), - EnumVariant(EnumVariant), + FunctionId(FunctionId), + ConstId(ConstId), + StaticId(StaticId), + StructId(StructId), + EnumVariantId(EnumVariantId), } impl Resolver { @@ -266,13 +266,11 @@ impl Resolver { return match idx { None => { let value = match module_def.take_values()? { - ModuleDefId::FunctionId(it) => ValueNs::Function(it.into()), - ModuleDefId::AdtId(AdtId::StructId(it)) => { - ValueNs::Struct(it.into()) - } - ModuleDefId::EnumVariantId(it) => ValueNs::EnumVariant(it.into()), - ModuleDefId::ConstId(it) => ValueNs::Const(it.into()), - ModuleDefId::StaticId(it) => ValueNs::Static(it.into()), + ModuleDefId::FunctionId(it) => ValueNs::FunctionId(it), + ModuleDefId::AdtId(AdtId::StructId(it)) => ValueNs::StructId(it), + ModuleDefId::EnumVariantId(it) => ValueNs::EnumVariantId(it), + ModuleDefId::ConstId(it) => ValueNs::ConstId(it), + ModuleDefId::StaticId(it) => ValueNs::StaticId(it), ModuleDefId::AdtId(AdtId::EnumId(_)) | ModuleDefId::AdtId(AdtId::UnionId(_)) @@ -496,23 +494,23 @@ impl Scope { // needs arbitrary_self_types to be a method... or maybe move to the def? pub(crate) fn resolver_for_expr( db: &impl HirDatabase, - owner: DefWithBody, + owner: DefWithBodyId, expr_id: ExprId, ) -> Resolver { - let scopes = owner.expr_scopes(db); + let scopes = db.expr_scopes(owner); resolver_for_scope(db, owner, scopes.scope_for(expr_id)) } pub(crate) fn resolver_for_scope( db: &impl HirDatabase, - owner: DefWithBody, + owner: DefWithBodyId, scope_id: Option, ) -> Resolver { - let mut r = owner.resolver(db); - let scopes = owner.expr_scopes(db); + let mut r = DefWithBody::from(owner).resolver(db); + let scopes = db.expr_scopes(owner); let scope_chain = scopes.scope_chain(scope_id).collect::>(); for scope in scope_chain.into_iter().rev() { - r = r.push_expr_scope(owner.into(), Arc::clone(&scopes), scope); + r = r.push_expr_scope(owner, Arc::clone(&scopes), scope); } r } 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 { None => scope_for(&scopes, &source_map, node), Some(offset) => scope_for_offset(&scopes, &source_map, node.with_value(offset)), }; - let resolver = resolver_for_scope(db, def, scope); + let resolver = resolver_for_scope(db, def.into(), scope); SourceAnalyzer { resolver, body_owner: Some(def), @@ -260,11 +260,11 @@ impl SourceAnalyzer { let var = Local { parent: self.body_owner?, pat_id }; PathResolution::Local(var) } - ValueNs::Function(it) => PathResolution::Def(it.into()), - ValueNs::Const(it) => PathResolution::Def(it.into()), - ValueNs::Static(it) => PathResolution::Def(it.into()), - ValueNs::Struct(it) => PathResolution::Def(it.into()), - ValueNs::EnumVariant(it) => PathResolution::Def(it.into()), + ValueNs::FunctionId(it) => PathResolution::Def(Function::from(it).into()), + ValueNs::ConstId(it) => PathResolution::Def(Const::from(it).into()), + ValueNs::StaticId(it) => PathResolution::Def(Static::from(it).into()), + ValueNs::StructId(it) => PathResolution::Def(Struct::from(it).into()), + ValueNs::EnumVariantId(it) => PathResolution::Def(EnumVariant::from(it).into()), }; Some(res) }); 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> { } Expr::Path(p) => { // FIXME this could be more efficient... - let resolver = resolver_for_expr(self.db, self.owner, tgt_expr); + let resolver = resolver_for_expr(self.db, self.owner.into(), tgt_expr); self.infer_path(&resolver, p, tgt_expr.into()).unwrap_or(Ty::Unknown) } 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::{ generics::HasGenericParams, resolve::{ResolveValueResult, Resolver, TypeNs, ValueNs}, ty::{method_resolution, Namespace, Substs, Ty, TypableDef, TypeWalk}, - AssocItem, Container, Name, Path, + AssocItem, Container, Function, Name, Path, }; impl<'a, D: HirDatabase> InferenceContext<'a, D> { @@ -60,11 +60,11 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { let ty = self.resolve_ty_as_possible(&mut vec![], ty); return Some(ty); } - ValueNs::Function(it) => it.into(), - ValueNs::Const(it) => it.into(), - ValueNs::Static(it) => it.into(), - ValueNs::Struct(it) => it.into(), - ValueNs::EnumVariant(it) => it.into(), + ValueNs::FunctionId(it) => it.into(), + ValueNs::ConstId(it) => it.into(), + ValueNs::StaticId(it) => it.into(), + ValueNs::StructId(it) => it.into(), + ValueNs::EnumVariantId(it) => it.into(), }; let mut ty = self.db.type_for_def(typable, Namespace::Values); @@ -160,8 +160,8 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { AssocItem::TypeAlias(_) => None, })?; let def = match item { - AssocItem::Function(f) => ValueNs::Function(f), - AssocItem::Const(c) => ValueNs::Const(c), + AssocItem::Function(f) => ValueNs::FunctionId(f.id), + AssocItem::Const(c) => ValueNs::ConstId(c.id), AssocItem::TypeAlias(_) => unreachable!(), }; let substs = Substs::build_for_def(self.db, item) @@ -193,8 +193,8 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { method_resolution::LookupMode::Path, move |_ty, item| { let def = match item { - AssocItem::Function(f) => ValueNs::Function(f), - AssocItem::Const(c) => ValueNs::Const(c), + AssocItem::Function(f) => ValueNs::FunctionId(f.id), + AssocItem::Const(c) => ValueNs::ConstId(c.id), AssocItem::TypeAlias(_) => unreachable!(), }; let substs = match item.container(self.db) { @@ -224,7 +224,8 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { } fn find_self_types(&self, def: &ValueNs, actual_def_ty: Ty) -> Option { - if let ValueNs::Function(func) = def { + if let ValueNs::FunctionId(func) = def { + let func = Function::from(*func); // We only do the infer if parent has generic params let gen = func.generic_params(self.db); if gen.count_parent_params() == 0 { -- cgit v1.2.3