From 825049bc6247f6d596910cd99f76f891d5435a86 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 27 Nov 2019 12:31:40 +0300 Subject: Decouple --- crates/ra_hir/src/source_binder.rs | 8 +-- crates/ra_hir/src/ty/infer.rs | 6 +-- crates/ra_hir/src/ty/infer/path.rs | 86 ++++++++++++++++++------------- crates/ra_hir/src/ty/method_resolution.rs | 10 ++-- crates/ra_hir_def/src/lib.rs | 10 ++++ 5 files changed, 71 insertions(+), 49 deletions(-) (limited to 'crates') diff --git a/crates/ra_hir/src/source_binder.rs b/crates/ra_hir/src/source_binder.rs index 9f3e6c43f..b9d3a1713 100644 --- a/crates/ra_hir/src/source_binder.rs +++ b/crates/ra_hir/src/source_binder.rs @@ -11,7 +11,7 @@ use hir_def::{ expr::{ExprId, PatId}, path::known, resolver::{self, resolver_for_scope, HasResolver, Resolver, TypeNs, ValueNs}, - DefWithBodyId, + AssocItemId, DefWithBodyId, }; use hir_expand::{ hygiene::Hygiene, name::AsName, AstId, HirFileId, MacroCallId, MacroFileKind, Source, @@ -380,7 +380,7 @@ impl SourceAnalyzer { name, method_resolution::LookupMode::MethodCall, |ty, it| match it { - AssocItem::Function(f) => callback(ty, f), + AssocItemId::FunctionId(f) => callback(ty, f.into()), _ => None, }, ) @@ -391,7 +391,7 @@ impl SourceAnalyzer { db: &impl HirDatabase, ty: &Type, name: Option<&Name>, - callback: impl FnMut(&Ty, AssocItem) -> Option, + mut callback: impl FnMut(&Ty, AssocItem) -> Option, ) -> Option { // There should be no inference vars in types passed here // FIXME check that? @@ -403,7 +403,7 @@ impl SourceAnalyzer { &self.resolver, name, method_resolution::LookupMode::Path, - callback, + |ty, it| callback(ty, it.into()), ) } diff --git a/crates/ra_hir/src/ty/infer.rs b/crates/ra_hir/src/ty/infer.rs index beb2efb7a..db9a8c9d1 100644 --- a/crates/ra_hir/src/ty/infer.rs +++ b/crates/ra_hir/src/ty/infer.rs @@ -26,7 +26,7 @@ use hir_def::{ path::known, resolver::{HasResolver, Resolver, TypeNs}, type_ref::{Mutability, TypeRef}, - AdtId, DefWithBodyId, + AdtId, AssocItemId, DefWithBodyId, }; use hir_expand::{diagnostics::DiagnosticSink, name}; use ra_arena::map::ArenaMap; @@ -255,8 +255,8 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { self.result.variant_resolutions.insert(id, variant); } - fn write_assoc_resolution(&mut self, id: ExprOrPatId, item: AssocItem) { - self.result.assoc_resolutions.insert(id, item); + fn write_assoc_resolution(&mut self, id: ExprOrPatId, item: AssocItemId) { + self.result.assoc_resolutions.insert(id, item.into()); } fn write_pat_ty(&mut self, pat: PatId, ty: Ty) { diff --git a/crates/ra_hir/src/ty/infer/path.rs b/crates/ra_hir/src/ty/infer/path.rs index 8d8b56d2d..09ff79728 100644 --- a/crates/ra_hir/src/ty/infer/path.rs +++ b/crates/ra_hir/src/ty/infer/path.rs @@ -2,14 +2,14 @@ use hir_def::{ path::{Path, PathSegment}, - resolver::{ResolveValueResult, Resolver, TypeNs, ValueNs}, + resolver::{HasResolver, ResolveValueResult, Resolver, TypeNs, ValueNs}, + AssocItemId, ContainerId, Lookup, }; use hir_expand::name::Name; use crate::{ db::HirDatabase, ty::{method_resolution, Substs, Ty, TypeWalk, ValueTyDefId}, - AssocItem, Container, Function, }; use super::{ExprOrPatId, InferenceContext, TraitRef}; @@ -143,31 +143,35 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { id: ExprOrPatId, ) -> Option<(ValueNs, Option)> { let trait_ = trait_ref.trait_; - let item = - self.db.trait_data(trait_).items.iter().map(|(_name, id)| (*id).into()).find_map( - |item| match item { - AssocItem::Function(func) => { - if segment.name == func.name(self.db) { - Some(AssocItem::Function(func)) - } else { - None - } + let item = self + .db + .trait_data(trait_) + .items + .iter() + .map(|(_name, id)| (*id).into()) + .find_map(|item| match item { + AssocItemId::FunctionId(func) => { + if segment.name == self.db.function_data(func).name { + Some(AssocItemId::FunctionId(func)) + } else { + None } + } - AssocItem::Const(konst) => { - if konst.name(self.db).map_or(false, |n| n == segment.name) { - Some(AssocItem::Const(konst)) - } else { - None - } + AssocItemId::ConstId(konst) => { + if self.db.const_data(konst).name.as_ref().map_or(false, |n| n == &segment.name) + { + Some(AssocItemId::ConstId(konst)) + } else { + None } - AssocItem::TypeAlias(_) => None, - }, - )?; + } + AssocItemId::TypeAliasId(_) => None, + })?; let def = match item { - AssocItem::Function(f) => ValueNs::FunctionId(f.id), - AssocItem::Const(c) => ValueNs::ConstId(c.id), - AssocItem::TypeAlias(_) => unreachable!(), + AssocItemId::FunctionId(f) => ValueNs::FunctionId(f), + AssocItemId::ConstId(c) => ValueNs::ConstId(c), + AssocItemId::TypeAliasId(_) => unreachable!(), }; let substs = Substs::build_for_def(self.db, item) .use_parent_substs(&trait_ref.substs) @@ -197,16 +201,18 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { Some(name), method_resolution::LookupMode::Path, move |_ty, item| { - let def = match item { - AssocItem::Function(f) => ValueNs::FunctionId(f.id), - AssocItem::Const(c) => ValueNs::ConstId(c.id), - AssocItem::TypeAlias(_) => unreachable!(), + let (def, container) = match item { + AssocItemId::FunctionId(f) => { + (ValueNs::FunctionId(f), f.lookup(self.db).container) + } + AssocItemId::ConstId(c) => (ValueNs::ConstId(c), c.lookup(self.db).container), + AssocItemId::TypeAliasId(_) => unreachable!(), }; - let substs = match item.container(self.db) { - Container::ImplBlock(_) => self.find_self_types(&def, ty.clone()), - Container::Trait(t) => { + let substs = match container { + ContainerId::ImplId(_) => self.find_self_types(&def, ty.clone()), + ContainerId::TraitId(trait_) => { // we're picking this method - let trait_substs = Substs::build_for_def(self.db, t.id) + let trait_substs = Substs::build_for_def(self.db, trait_) .push(ty.clone()) .fill(std::iter::repeat_with(|| self.new_type_var())) .build(); @@ -215,29 +221,35 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { .fill_with_params() .build(); self.obligations.push(super::Obligation::Trait(TraitRef { - trait_: t.id, + trait_, substs: trait_substs, })); Some(substs) } + ContainerId::ModuleId(_) => None, }; - self.write_assoc_resolution(id, item); + self.write_assoc_resolution(id, item.into()); Some((def, substs)) }, ) } fn find_self_types(&self, def: &ValueNs, actual_def_ty: Ty) -> Option { - if let ValueNs::FunctionId(func) = def { - let func = Function::from(*func); + if let ValueNs::FunctionId(func) = *def { // We only do the infer if parent has generic params - let gen = self.db.generic_params(func.id.into()); + let gen = self.db.generic_params(func.into()); if gen.count_parent_params() == 0 { return None; } - let impl_block = func.impl_block(self.db)?.target_ty(self.db); + let impl_id = match func.lookup(self.db).container { + ContainerId::ImplId(it) => it, + _ => return None, + }; + let resolver = impl_id.resolver(self.db); + let impl_data = self.db.impl_data(impl_id); + let impl_block = Ty::from_hir(self.db, &resolver, &impl_data.target_type); let impl_block_substs = impl_block.substs()?; let actual_substs = actual_def_ty.substs()?; diff --git a/crates/ra_hir/src/ty/method_resolution.rs b/crates/ra_hir/src/ty/method_resolution.rs index 7df2649c9..02e81fb34 100644 --- a/crates/ra_hir/src/ty/method_resolution.rs +++ b/crates/ra_hir/src/ty/method_resolution.rs @@ -18,7 +18,7 @@ use crate::{ db::HirDatabase, ty::primitive::{FloatBitness, Uncertain}, ty::{utils::all_super_traits, Ty, TypeCtor}, - AssocItem, Function, + Function, }; use super::{autoderef, Canonical, InEnvironment, TraitEnvironment, TraitRef}; @@ -157,7 +157,7 @@ pub(crate) fn lookup_method( ) -> Option<(Ty, Function)> { iterate_method_candidates(ty, db, resolver, Some(name), LookupMode::MethodCall, |ty, f| match f { - AssocItem::Function(f) => Some((ty.clone(), f)), + AssocItemId::FunctionId(f) => Some((ty.clone(), f.into())), _ => None, }) } @@ -183,7 +183,7 @@ pub(crate) fn iterate_method_candidates( resolver: &Resolver, name: Option<&Name>, mode: LookupMode, - mut callback: impl FnMut(&Ty, AssocItem) -> Option, + mut callback: impl FnMut(&Ty, AssocItemId) -> Option, ) -> Option { let krate = resolver.krate()?; match mode { @@ -239,7 +239,7 @@ fn iterate_trait_method_candidates( resolver: &Resolver, name: Option<&Name>, mode: LookupMode, - mut callback: impl FnMut(&Ty, AssocItem) -> Option, + mut callback: impl FnMut(&Ty, AssocItemId) -> Option, ) -> Option { let krate = resolver.krate()?; // FIXME: maybe put the trait_env behind a query (need to figure out good input parameters for that) @@ -285,7 +285,7 @@ fn iterate_inherent_methods( name: Option<&Name>, mode: LookupMode, krate: CrateId, - mut callback: impl FnMut(&Ty, AssocItem) -> Option, + mut callback: impl FnMut(&Ty, AssocItemId) -> Option, ) -> Option { for krate in ty.value.def_crates(db, krate)? { let impls = db.impls_in_crate(krate); diff --git a/crates/ra_hir_def/src/lib.rs b/crates/ra_hir_def/src/lib.rs index 274dd1467..ea3f00bb3 100644 --- a/crates/ra_hir_def/src/lib.rs +++ b/crates/ra_hir_def/src/lib.rs @@ -398,6 +398,16 @@ impl_froms!( ConstId ); +impl From for GenericDefId { + fn from(item: AssocItemId) -> Self { + match item { + AssocItemId::FunctionId(f) => f.into(), + AssocItemId::ConstId(c) => c.into(), + AssocItemId::TypeAliasId(t) => t.into(), + } + } +} + #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] pub enum AttrDefId { ModuleId(ModuleId), -- cgit v1.2.3