From 0102fb41337ac0442e689d410bb424d215e9a7bd Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 21 Nov 2019 12:21:46 +0300 Subject: Decouple Resolver --- crates/ra_hir/src/code_model.rs | 18 ++--- crates/ra_hir/src/expr.rs | 4 +- crates/ra_hir/src/from_id.rs | 65 ++++++++++++++++- crates/ra_hir/src/resolve.rs | 117 ++++++++++++++++-------------- crates/ra_hir/src/source_binder.rs | 21 +++--- crates/ra_hir/src/ty/infer.rs | 23 +++--- crates/ra_hir/src/ty/infer/path.rs | 4 +- crates/ra_hir/src/ty/lower.rs | 31 ++++---- crates/ra_hir/src/ty/method_resolution.rs | 4 +- 9 files changed, 180 insertions(+), 107 deletions(-) (limited to 'crates/ra_hir/src') diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs index a132d128b..550ab1a98 100644 --- a/crates/ra_hir/src/code_model.rs +++ b/crates/ra_hir/src/code_model.rs @@ -12,8 +12,8 @@ use hir_def::{ builtin_type::BuiltinType, traits::TraitData, type_ref::{Mutability, TypeRef}, - AssocItemId, ContainerId, CrateModuleId, HasModule, ImplId, LocalEnumVariantId, - LocalStructFieldId, Lookup, ModuleId, UnionId, + ContainerId, CrateModuleId, HasModule, ImplId, LocalEnumVariantId, LocalStructFieldId, Lookup, + ModuleId, UnionId, }; use hir_expand::{ diagnostics::DiagnosticSink, @@ -842,9 +842,10 @@ impl Trait { _ => None, }) .filter_map(|path| match resolver.resolve_path_in_type_ns_fully(db, path) { - Some(TypeNs::Trait(t)) => Some(t), + Some(TypeNs::TraitId(t)) => Some(t), _ => None, }) + .map(Trait::from) .collect() } @@ -871,14 +872,9 @@ impl Trait { pub fn associated_type_by_name(self, db: &impl DefDatabase, name: &Name) -> Option { let trait_data = self.trait_data(db); - trait_data - .items - .iter() - .filter_map(|item| match item { - AssocItemId::TypeAliasId(t) => Some(TypeAlias::from(*t)), - _ => None, - }) - .find(|t| &t.name(db) == name) + let res = + trait_data.associated_types().map(TypeAlias::from).find(|t| &t.name(db) == name)?; + Some(res) } pub fn associated_type_by_name_including_super_traits( diff --git a/crates/ra_hir/src/expr.rs b/crates/ra_hir/src/expr.rs index 869879bdf..8c4c63fda 100644 --- a/crates/ra_hir/src/expr.rs +++ b/crates/ra_hir/src/expr.rs @@ -46,7 +46,7 @@ pub(crate) fn resolver_for_scope( let scopes = owner.expr_scopes(db); let scope_chain = scopes.scope_chain(scope_id).collect::>(); for scope in scope_chain.into_iter().rev() { - r = r.push_expr_scope(owner, Arc::clone(&scopes), scope); + r = r.push_expr_scope(owner.into(), Arc::clone(&scopes), scope); } r } @@ -152,7 +152,7 @@ impl<'a, 'b> ExprValidator<'a, 'b> { _ => return, }; - let std_result_ctor = TypeCtor::Adt(Adt::Enum(std_result_enum)); + let std_result_ctor = TypeCtor::Adt(Adt::Enum(std_result_enum.into())); let params = match &mismatch.expected { Ty::Apply(ApplicationTy { ctor, parameters }) if ctor == &std_result_ctor => parameters, _ => return, diff --git a/crates/ra_hir/src/from_id.rs b/crates/ra_hir/src/from_id.rs index b7692d407..7042422cc 100644 --- a/crates/ra_hir/src/from_id.rs +++ b/crates/ra_hir/src/from_id.rs @@ -3,9 +3,14 @@ //! It's unclear if we need this long-term, but it's definitelly useful while we //! are splitting the hir. -use hir_def::{AdtId, AssocItemId, DefWithBodyId, EnumVariantId, GenericDefId, ModuleDefId}; +use hir_def::{ + AdtId, AssocItemId, DefWithBodyId, EnumId, EnumVariantId, GenericDefId, ModuleDefId, StructId, + TypeAliasId, UnionId, +}; -use crate::{Adt, AssocItem, DefWithBody, EnumVariant, GenericDef, ModuleDef}; +use crate::{ + ty::TypableDef, Adt, AssocItem, DefWithBody, EnumVariant, GenericDef, ModuleDef, TypeAlias, +}; macro_rules! from_id { ($(($id:path, $ty:path)),*) => {$( @@ -83,6 +88,16 @@ impl From for DefWithBodyId { } } +impl From for DefWithBody { + fn from(def: DefWithBodyId) -> Self { + match def { + DefWithBodyId::FunctionId(it) => DefWithBody::Function(it.into()), + DefWithBodyId::StaticId(it) => DefWithBody::Static(it.into()), + DefWithBodyId::ConstId(it) => DefWithBody::Const(it.into()), + } + } +} + impl From for AssocItem { fn from(def: AssocItemId) -> Self { match def { @@ -122,3 +137,49 @@ impl From for GenericDef { } } } + +impl From for TypableDef { + fn from(id: AdtId) -> Self { + Adt::from(id).into() + } +} + +impl From for TypableDef { + fn from(id: StructId) -> Self { + AdtId::StructId(id).into() + } +} + +impl From for TypableDef { + fn from(id: UnionId) -> Self { + AdtId::UnionId(id).into() + } +} + +impl From for TypableDef { + fn from(id: EnumId) -> Self { + AdtId::EnumId(id).into() + } +} + +impl From for TypableDef { + fn from(id: EnumVariantId) -> Self { + EnumVariant::from(id).into() + } +} + +impl From for TypableDef { + fn from(id: TypeAliasId) -> Self { + TypeAlias::from(id).into() + } +} + +impl From for GenericDefId { + fn from(id: Adt) -> Self { + match id { + Adt::Struct(it) => it.id.into(), + Adt::Union(it) => it.id.into(), + Adt::Enum(it) => it.id.into(), + } + } +} diff --git a/crates/ra_hir/src/resolve.rs b/crates/ra_hir/src/resolve.rs index eca8e0596..f4165babd 100644 --- a/crates/ra_hir/src/resolve.rs +++ b/crates/ra_hir/src/resolve.rs @@ -3,19 +3,21 @@ use std::sync::Arc; use hir_def::{ builtin_type::BuiltinType, + db::DefDatabase2, + generics::GenericParams, nameres::CrateDefMap, path::{Path, PathKind}, - AdtId, CrateModuleId, ModuleDefId, + AdtId, CrateModuleId, DefWithBodyId, EnumId, EnumVariantId, GenericDefId, ImplId, ModuleDefId, + StructId, TraitId, TypeAliasId, }; use hir_expand::name::{self, Name}; use rustc_hash::FxHashSet; use crate::{ code_model::Crate, - db::{DefDatabase, HirDatabase}, + db::DefDatabase, expr::{ExprScopes, PatId, ScopeId}, - generics::{GenericParams, HasGenericParams}, - Adt, Const, Container, DefWithBody, Enum, EnumVariant, Function, GenericDef, ImplBlock, Local, + Adt, Const, Container, DefWithBody, EnumVariant, Function, GenericDef, ImplBlock, Local, MacroDef, Module, ModuleDef, PerNs, Static, Struct, Trait, TypeAlias, }; @@ -33,7 +35,7 @@ pub(crate) struct ModuleItemMap { #[derive(Debug, Clone)] pub(crate) struct ExprScope { - owner: DefWithBody, + owner: DefWithBodyId, expr_scopes: Arc, scope_id: ScopeId, } @@ -43,28 +45,28 @@ pub(crate) enum Scope { /// All the items and imported names of a module ModuleScope(ModuleItemMap), /// Brings the generic parameters of an item into scope - GenericParams { def: GenericDef, params: Arc }, + GenericParams { def: GenericDefId, params: Arc }, /// Brings `Self` in `impl` block into scope - ImplBlockScope(ImplBlock), + ImplBlockScope(ImplId), /// Brings `Self` in enum, struct and union definitions into scope - AdtScope(Adt), + AdtScope(AdtId), /// Local bindings ExprScope(ExprScope), } #[derive(Debug, Clone, PartialEq, Eq, Hash)] pub(crate) enum TypeNs { - SelfType(ImplBlock), + SelfType(ImplId), GenericParam(u32), - Adt(Adt), - AdtSelfType(Adt), - EnumVariant(EnumVariant), - TypeAlias(TypeAlias), + AdtId(AdtId), + AdtSelfType(AdtId), + EnumVariantId(EnumVariantId), + TypeAliasId(TypeAliasId), BuiltinType(BuiltinType), - Trait(Trait), + TraitId(TraitId), // Module belong to type ns, but the resolver is used when all module paths // are fully resolved. - // Module(Module) + // ModuleId(ModuleId) } #[derive(Debug, Clone, PartialEq, Eq, Hash)] @@ -85,10 +87,14 @@ pub(crate) enum ValueNs { impl Resolver { /// Resolve known trait from std, like `std::futures::Future` - pub(crate) fn resolve_known_trait(&self, db: &impl HirDatabase, path: &Path) -> Option { + pub(crate) fn resolve_known_trait( + &self, + db: &impl DefDatabase2, + path: &Path, + ) -> Option { let res = self.resolve_module_path(db, path).take_types()?; match res { - ModuleDefId::TraitId(it) => Some(it.into()), + ModuleDefId::TraitId(it) => Some(it), _ => None, } } @@ -96,27 +102,27 @@ impl Resolver { /// Resolve known struct from std, like `std::boxed::Box` pub(crate) fn resolve_known_struct( &self, - db: &impl HirDatabase, + db: &impl DefDatabase2, path: &Path, - ) -> Option { + ) -> Option { let res = self.resolve_module_path(db, path).take_types()?; match res { - ModuleDefId::AdtId(AdtId::StructId(it)) => Some(it.into()), + ModuleDefId::AdtId(AdtId::StructId(it)) => Some(it), _ => None, } } /// Resolve known enum from std, like `std::result::Result` - pub(crate) fn resolve_known_enum(&self, db: &impl HirDatabase, path: &Path) -> Option { + pub(crate) fn resolve_known_enum(&self, db: &impl DefDatabase2, path: &Path) -> Option { let res = self.resolve_module_path(db, path).take_types()?; match res { - ModuleDefId::AdtId(AdtId::EnumId(it)) => Some(it.into()), + ModuleDefId::AdtId(AdtId::EnumId(it)) => Some(it), _ => None, } } /// pub only for source-binder - pub(crate) fn resolve_module_path(&self, db: &impl HirDatabase, path: &Path) -> PerNs { + pub(crate) fn resolve_module_path(&self, db: &impl DefDatabase2, path: &Path) -> PerNs { let (item_map, module) = match self.module() { Some(it) => it, None => return PerNs::none(), @@ -130,7 +136,7 @@ impl Resolver { pub(crate) fn resolve_path_in_type_ns( &self, - db: &impl HirDatabase, + db: &impl DefDatabase2, path: &Path, ) -> Option<(TypeNs, Option)> { if path.is_type_relative() { @@ -164,13 +170,13 @@ impl Resolver { Scope::ModuleScope(m) => { let (module_def, idx) = m.crate_def_map.resolve_path(db, m.module_id, path); let res = match module_def.take_types()? { - ModuleDefId::AdtId(it) => TypeNs::Adt(it.into()), - ModuleDefId::EnumVariantId(it) => TypeNs::EnumVariant(it.into()), + ModuleDefId::AdtId(it) => TypeNs::AdtId(it), + ModuleDefId::EnumVariantId(it) => TypeNs::EnumVariantId(it), - ModuleDefId::TypeAliasId(it) => TypeNs::TypeAlias(it.into()), + ModuleDefId::TypeAliasId(it) => TypeNs::TypeAliasId(it), ModuleDefId::BuiltinType(it) => TypeNs::BuiltinType(it), - ModuleDefId::TraitId(it) => TypeNs::Trait(it.into()), + ModuleDefId::TraitId(it) => TypeNs::TraitId(it), ModuleDefId::FunctionId(_) | ModuleDefId::ConstId(_) @@ -186,7 +192,7 @@ impl Resolver { pub(crate) fn resolve_path_in_type_ns_fully( &self, - db: &impl HirDatabase, + db: &impl DefDatabase2, path: &Path, ) -> Option { let (res, unresolved) = self.resolve_path_in_type_ns(db, path)?; @@ -198,7 +204,7 @@ impl Resolver { pub(crate) fn resolve_path_in_value_ns<'p>( &self, - db: &impl HirDatabase, + db: &impl DefDatabase2, path: &'p Path, ) -> Option { if path.is_type_relative() { @@ -278,9 +284,9 @@ impl Resolver { } Some(idx) => { let ty = match module_def.take_types()? { - ModuleDefId::AdtId(it) => TypeNs::Adt(it.into()), - ModuleDefId::TraitId(it) => TypeNs::Trait(it.into()), - ModuleDefId::TypeAliasId(it) => TypeNs::TypeAlias(it.into()), + ModuleDefId::AdtId(it) => TypeNs::AdtId(it), + ModuleDefId::TraitId(it) => TypeNs::TraitId(it), + ModuleDefId::TypeAliasId(it) => TypeNs::TypeAliasId(it), ModuleDefId::BuiltinType(it) => TypeNs::BuiltinType(it), ModuleDefId::ModuleId(_) @@ -300,7 +306,7 @@ impl Resolver { pub(crate) fn resolve_path_in_value_ns_fully( &self, - db: &impl HirDatabase, + db: &impl DefDatabase2, path: &Path, ) -> Option { match self.resolve_path_in_value_ns(db, path)? { @@ -311,7 +317,7 @@ impl Resolver { pub(crate) fn resolve_path_as_macro( &self, - db: &impl DefDatabase, + db: &impl DefDatabase2, path: &Path, ) -> Option { let (item_map, module) = self.module()?; @@ -320,7 +326,7 @@ impl Resolver { pub(crate) fn process_all_names( &self, - db: &impl HirDatabase, + db: &impl DefDatabase2, f: &mut dyn FnMut(Name, ScopeDef), ) { for scope in self.scopes.iter().rev() { @@ -328,16 +334,15 @@ impl Resolver { } } - pub(crate) fn traits_in_scope(&self, db: &impl HirDatabase) -> FxHashSet { + pub(crate) fn traits_in_scope(&self, db: &impl DefDatabase2) -> FxHashSet { let mut traits = FxHashSet::default(); for scope in &self.scopes { if let Scope::ModuleScope(m) = scope { if let Some(prelude) = m.crate_def_map.prelude() { let prelude_def_map = db.crate_def_map(prelude.krate); - traits - .extend(prelude_def_map[prelude.module_id].scope.traits().map(Trait::from)); + traits.extend(prelude_def_map[prelude.module_id].scope.traits()); } - traits.extend(m.crate_def_map[m.module_id].scope.traits().map(Trait::from)); + traits.extend(m.crate_def_map[m.module_id].scope.traits()); } } traits @@ -367,7 +372,7 @@ impl Resolver { .flat_map(|params| params.where_predicates.iter()) } - pub(crate) fn generic_def(&self) -> Option { + pub(crate) fn generic_def(&self) -> Option { self.scopes.iter().find_map(|scope| match scope { Scope::GenericParams { def, .. } => Some(*def), _ => None, @@ -383,10 +388,10 @@ impl Resolver { pub(crate) fn push_generic_params_scope( self, - db: &impl DefDatabase, - def: GenericDef, + db: &impl DefDatabase2, + def: GenericDefId, ) -> Resolver { - let params = def.generic_params(db); + let params = db.generic_params(def); if params.params.is_empty() { self } else { @@ -394,7 +399,7 @@ impl Resolver { } } - pub(crate) fn push_impl_block_scope(self, impl_block: ImplBlock) -> Resolver { + pub(crate) fn push_impl_block_scope(self, impl_block: ImplId) -> Resolver { self.push_scope(Scope::ImplBlockScope(impl_block)) } @@ -408,7 +413,7 @@ impl Resolver { pub(crate) fn push_expr_scope( self, - owner: DefWithBody, + owner: DefWithBodyId, expr_scopes: Arc, scope_id: ScopeId, ) -> Resolver { @@ -440,7 +445,7 @@ impl From for ScopeDef { } impl Scope { - fn process_names(&self, db: &impl HirDatabase, f: &mut dyn FnMut(Name, ScopeDef)) { + fn process_names(&self, db: &impl DefDatabase2, f: &mut dyn FnMut(Name, ScopeDef)) { match self { Scope::ModuleScope(m) => { // FIXME: should we provide `self` here? @@ -472,14 +477,14 @@ impl Scope { } } Scope::ImplBlockScope(i) => { - f(name::SELF_TYPE, ScopeDef::ImplSelfType(*i)); + f(name::SELF_TYPE, ScopeDef::ImplSelfType((*i).into())); } Scope::AdtScope(i) => { - f(name::SELF_TYPE, ScopeDef::AdtSelfType(*i)); + f(name::SELF_TYPE, ScopeDef::AdtSelfType((*i).into())); } Scope::ExprScope(scope) => { scope.expr_scopes.entries(scope.scope_id).iter().for_each(|e| { - let local = Local { parent: scope.owner, pat_id: e.pat() }; + let local = Local { parent: scope.owner.into(), pat_id: e.pat() }; f(e.name().clone(), ScopeDef::Local(local)); }); } @@ -501,7 +506,7 @@ impl HasResolver for Module { impl HasResolver for Trait { fn resolver(self, db: &impl DefDatabase) -> Resolver { - self.module(db).resolver(db).push_generic_params_scope(db, self.into()) + self.module(db).resolver(db).push_generic_params_scope(db, self.id.into()) } } @@ -511,7 +516,7 @@ impl> HasResolver for T { def.module(db) .resolver(db) .push_generic_params_scope(db, def.into()) - .push_scope(Scope::AdtScope(def)) + .push_scope(Scope::AdtScope(def.into())) } } @@ -520,7 +525,7 @@ impl HasResolver for Function { self.container(db) .map(|c| c.resolver(db)) .unwrap_or_else(|| self.module(db).resolver(db)) - .push_generic_params_scope(db, self.into()) + .push_generic_params_scope(db, self.id.into()) } } @@ -551,7 +556,7 @@ impl HasResolver for TypeAlias { self.container(db) .map(|ib| ib.resolver(db)) .unwrap_or_else(|| self.module(db).resolver(db)) - .push_generic_params_scope(db, self.into()) + .push_generic_params_scope(db, self.id.into()) } } @@ -582,7 +587,7 @@ impl HasResolver for ImplBlock { fn resolver(self, db: &impl DefDatabase) -> Resolver { self.module(db) .resolver(db) - .push_generic_params_scope(db, self.into()) - .push_impl_block_scope(self) + .push_generic_params_scope(db, self.id.into()) + .push_impl_block_scope(self.id) } } diff --git a/crates/ra_hir/src/source_binder.rs b/crates/ra_hir/src/source_binder.rs index 727310f06..5abb8d693 100644 --- a/crates/ra_hir/src/source_binder.rs +++ b/crates/ra_hir/src/source_binder.rs @@ -25,8 +25,9 @@ use crate::{ ids::LocationCtx, resolve::{HasResolver, ScopeDef, TypeNs, ValueNs}, ty::method_resolution::{self, implements_trait}, - AssocItem, Const, DefWithBody, Either, Enum, FromSource, Function, GenericParam, HasBody, - HirFileId, Local, MacroDef, Module, Name, Path, Resolver, Static, Struct, Ty, + Adt, AssocItem, Const, DefWithBody, Either, Enum, EnumVariant, FromSource, Function, + GenericParam, HasBody, HirFileId, Local, MacroDef, Module, Name, Path, Resolver, Static, + Struct, Trait, Ty, TypeAlias, }; fn try_get_resolver_for_node(db: &impl HirDatabase, node: Source<&SyntaxNode>) -> Option { @@ -240,16 +241,18 @@ impl SourceAnalyzer { path: &crate::Path, ) -> Option { let types = self.resolver.resolve_path_in_type_ns_fully(db, &path).map(|ty| match ty { - TypeNs::SelfType(it) => PathResolution::SelfType(it), + TypeNs::SelfType(it) => PathResolution::SelfType(it.into()), TypeNs::GenericParam(idx) => PathResolution::GenericParam(GenericParam { - parent: self.resolver.generic_def().unwrap(), + parent: self.resolver.generic_def().unwrap().into(), idx, }), - TypeNs::AdtSelfType(it) | TypeNs::Adt(it) => PathResolution::Def(it.into()), - TypeNs::EnumVariant(it) => PathResolution::Def(it.into()), - TypeNs::TypeAlias(it) => PathResolution::Def(it.into()), + TypeNs::AdtSelfType(it) | TypeNs::AdtId(it) => { + PathResolution::Def(Adt::from(it).into()) + } + TypeNs::EnumVariantId(it) => PathResolution::Def(EnumVariant::from(it).into()), + TypeNs::TypeAliasId(it) => PathResolution::Def(TypeAlias::from(it).into()), TypeNs::BuiltinType(it) => PathResolution::Def(it.into()), - TypeNs::Trait(it) => PathResolution::Def(it.into()), + TypeNs::TraitId(it) => PathResolution::Def(Trait::from(it).into()), }); let values = self.resolver.resolve_path_in_value_ns_fully(db, &path).and_then(|val| { let res = match val { @@ -392,7 +395,7 @@ impl SourceAnalyzer { let std_future_path = known::std_future_future(); let std_future_trait = match self.resolver.resolve_known_trait(db, &std_future_path) { - Some(it) => it, + Some(it) => it.into(), _ => return false, }; diff --git a/crates/ra_hir/src/ty/infer.rs b/crates/ra_hir/src/ty/infer.rs index 7f9e81d64..684d66946 100644 --- a/crates/ra_hir/src/ty/infer.rs +++ b/crates/ra_hir/src/ty/infer.rs @@ -24,6 +24,7 @@ use rustc_hash::FxHashMap; use hir_def::{ path::known, type_ref::{Mutability, TypeRef}, + AdtId, }; use hir_expand::{diagnostics::DiagnosticSink, name}; use ra_arena::map::ArenaMap; @@ -43,7 +44,7 @@ use crate::{ resolve::{HasResolver, Resolver, TypeNs}, ty::infer::diagnostics::InferenceDiagnostic, Adt, AssocItem, ConstData, DefWithBody, FloatTy, FnData, Function, HasBody, IntTy, Path, - StructField, VariantDef, + StructField, Trait, VariantDef, }; macro_rules! ty_app { @@ -518,17 +519,17 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { // FIXME: this should resolve assoc items as well, see this example: // https://play.rust-lang.org/?gist=087992e9e22495446c01c0d4e2d69521 match resolver.resolve_path_in_type_ns_fully(self.db, &path) { - Some(TypeNs::Adt(Adt::Struct(it))) => it.into(), - Some(TypeNs::Adt(Adt::Union(it))) => it.into(), + Some(TypeNs::AdtId(AdtId::StructId(it))) => it.into(), + Some(TypeNs::AdtId(AdtId::UnionId(it))) => it.into(), Some(TypeNs::AdtSelfType(adt)) => adt.into(), - Some(TypeNs::EnumVariant(it)) => it.into(), - Some(TypeNs::TypeAlias(it)) => it.into(), + Some(TypeNs::EnumVariantId(it)) => it.into(), + Some(TypeNs::TypeAliasId(it)) => it.into(), Some(TypeNs::SelfType(_)) | Some(TypeNs::GenericParam(_)) | Some(TypeNs::BuiltinType(_)) | - Some(TypeNs::Trait(_)) | - Some(TypeNs::Adt(Adt::Enum(_))) | + Some(TypeNs::TraitId(_)) | + Some(TypeNs::AdtId(AdtId::EnumId(_))) | None => { return (Ty::Unknown, None) } @@ -576,26 +577,26 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { fn resolve_into_iter_item(&self) -> Option { let path = known::std_iter_into_iterator(); - let trait_ = self.resolver.resolve_known_trait(self.db, &path)?; + let trait_: Trait = self.resolver.resolve_known_trait(self.db, &path)?.into(); trait_.associated_type_by_name(self.db, &name::ITEM_TYPE) } fn resolve_ops_try_ok(&self) -> Option { let path = known::std_ops_try(); - let trait_ = self.resolver.resolve_known_trait(self.db, &path)?; + let trait_: Trait = self.resolver.resolve_known_trait(self.db, &path)?.into(); trait_.associated_type_by_name(self.db, &name::OK_TYPE) } fn resolve_future_future_output(&self) -> Option { let path = known::std_future_future(); - let trait_ = self.resolver.resolve_known_trait(self.db, &path)?; + let trait_: Trait = self.resolver.resolve_known_trait(self.db, &path)?.into(); trait_.associated_type_by_name(self.db, &name::OUTPUT_TYPE) } fn resolve_boxed_box(&self) -> Option { let path = known::std_boxed_box(); let struct_ = self.resolver.resolve_known_struct(self.db, &path)?; - Some(Adt::Struct(struct_)) + Some(Adt::Struct(struct_.into())) } } diff --git a/crates/ra_hir/src/ty/infer/path.rs b/crates/ra_hir/src/ty/infer/path.rs index 31ca675aa..f36a27929 100644 --- a/crates/ra_hir/src/ty/infer/path.rs +++ b/crates/ra_hir/src/ty/infer/path.rs @@ -94,13 +94,13 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { let is_before_last = remaining_segments.len() == 1; match (def, is_before_last) { - (TypeNs::Trait(trait_), true) => { + (TypeNs::TraitId(trait_), true) => { let segment = remaining_segments.last().expect("there should be at least one segment here"); let trait_ref = TraitRef::from_resolved_path( self.db, &self.resolver, - trait_, + trait_.into(), resolved_segment, None, ); diff --git a/crates/ra_hir/src/ty/lower.rs b/crates/ra_hir/src/ty/lower.rs index 397ee7d5f..d4fbddac0 100644 --- a/crates/ra_hir/src/ty/lower.rs +++ b/crates/ra_hir/src/ty/lower.rs @@ -28,8 +28,8 @@ use crate::{ Adt, }, util::make_mut_slice, - Const, Enum, EnumVariant, Function, ModuleDef, Path, Static, Struct, StructField, Trait, - TypeAlias, Union, VariantDef, + Const, Enum, EnumVariant, Function, ImplBlock, ModuleDef, Path, Static, Struct, StructField, + Trait, TypeAlias, Union, VariantDef, }; // FIXME: this is only really used in `type_for_def`, which contains a bunch of @@ -156,9 +156,14 @@ impl Ty { remaining_segments: &[PathSegment], ) -> Ty { let ty = match resolution { - TypeNs::Trait(trait_) => { - let trait_ref = - TraitRef::from_resolved_path(db, resolver, trait_, resolved_segment, None); + TypeNs::TraitId(trait_) => { + let trait_ref = TraitRef::from_resolved_path( + db, + resolver, + trait_.into(), + resolved_segment, + None, + ); return if remaining_segments.len() == 1 { let segment = &remaining_segments[0]; match trait_ref @@ -189,18 +194,18 @@ impl Ty { let name = resolved_segment.name.clone(); Ty::Param { idx, name } } - TypeNs::SelfType(impl_block) => impl_block.target_ty(db), - TypeNs::AdtSelfType(adt) => adt.ty(db), + TypeNs::SelfType(impl_block) => ImplBlock::from(impl_block).target_ty(db), + TypeNs::AdtSelfType(adt) => Adt::from(adt).ty(db), - TypeNs::Adt(it) => Ty::from_hir_path_inner(db, resolver, resolved_segment, it.into()), + TypeNs::AdtId(it) => Ty::from_hir_path_inner(db, resolver, resolved_segment, it.into()), TypeNs::BuiltinType(it) => { Ty::from_hir_path_inner(db, resolver, resolved_segment, it.into()) } - TypeNs::TypeAlias(it) => { + TypeNs::TypeAliasId(it) => { Ty::from_hir_path_inner(db, resolver, resolved_segment, it.into()) } // FIXME: report error - TypeNs::EnumVariant(_) => return Ty::Unknown, + TypeNs::EnumVariantId(_) => return Ty::Unknown, }; Ty::from_type_relative_path(db, resolver, ty, remaining_segments) @@ -247,7 +252,7 @@ impl Ty { Some(def) => def, None => return Ty::Unknown, // this can't actually happen }; - let predicates = db.generic_predicates_for_param(def, param_idx); + let predicates = db.generic_predicates_for_param(def.into(), param_idx); let traits_from_env = predicates.iter().filter_map(|pred| match pred { GenericPredicate::Implemented(tr) if tr.self_ty() == &self_ty => Some(tr.trait_), _ => None, @@ -391,11 +396,11 @@ impl TraitRef { explicit_self_ty: Option, ) -> Option { let resolved = match resolver.resolve_path_in_type_ns_fully(db, &path)? { - TypeNs::Trait(tr) => tr, + TypeNs::TraitId(tr) => tr, _ => return None, }; let segment = path.segments.last().expect("path should have at least one segment"); - Some(TraitRef::from_resolved_path(db, resolver, resolved, segment, explicit_self_ty)) + Some(TraitRef::from_resolved_path(db, resolver, resolved.into(), segment, explicit_self_ty)) } pub(super) fn from_resolved_path( diff --git a/crates/ra_hir/src/ty/method_resolution.rs b/crates/ra_hir/src/ty/method_resolution.rs index f377fca48..26dd06171 100644 --- a/crates/ra_hir/src/ty/method_resolution.rs +++ b/crates/ra_hir/src/ty/method_resolution.rs @@ -224,7 +224,9 @@ fn iterate_trait_method_candidates( .trait_predicates_for_self_ty(&ty.value) .map(|tr| tr.trait_) .flat_map(|t| t.all_super_traits(db)); - let traits = inherent_trait.chain(traits_from_env).chain(resolver.traits_in_scope(db)); + let traits = inherent_trait + .chain(traits_from_env) + .chain(resolver.traits_in_scope(db).into_iter().map(Trait::from)); 'traits: for t in traits { let data = t.trait_data(db); -- cgit v1.2.3 From 24964ca58e057b3f32d5cbb17d84e46f2a236ff6 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 21 Nov 2019 13:28:04 +0300 Subject: Move expr resolver to resolve --- crates/ra_hir/src/expr.rs | 26 +------------------------- crates/ra_hir/src/resolve.rs | 31 ++++++++++++++++++++++++++++--- crates/ra_hir/src/source_binder.rs | 6 +++--- crates/ra_hir/src/ty/infer/expr.rs | 5 +++-- 4 files changed, 35 insertions(+), 33 deletions(-) (limited to 'crates/ra_hir/src') diff --git a/crates/ra_hir/src/expr.rs b/crates/ra_hir/src/expr.rs index 8c4c63fda..3ca0a4de5 100644 --- a/crates/ra_hir/src/expr.rs +++ b/crates/ra_hir/src/expr.rs @@ -13,7 +13,7 @@ use crate::{ diagnostics::{MissingFields, MissingOkInTailExpr}, resolve::HasResolver, ty::{ApplicationTy, InferenceResult, Ty, TypeCtor}, - Adt, DefWithBody, Function, HasBody, Name, Path, Resolver, + Adt, Function, Name, Path, }; pub use hir_def::{ @@ -27,30 +27,6 @@ pub use hir_def::{ }, }; -// 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, - expr_id: ExprId, -) -> Resolver { - let scopes = owner.expr_scopes(db); - resolver_for_scope(db, owner, scopes.scope_for(expr_id)) -} - -pub(crate) fn resolver_for_scope( - db: &impl HirDatabase, - owner: DefWithBody, - scope_id: Option, -) -> Resolver { - let mut r = owner.resolver(db); - let scopes = owner.expr_scopes(db); - 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 -} - pub(crate) struct ExprValidator<'a, 'b: 'a> { func: Function, infer: Arc, diff --git a/crates/ra_hir/src/resolve.rs b/crates/ra_hir/src/resolve.rs index f4165babd..899959532 100644 --- a/crates/ra_hir/src/resolve.rs +++ b/crates/ra_hir/src/resolve.rs @@ -4,6 +4,7 @@ use std::sync::Arc; use hir_def::{ builtin_type::BuiltinType, db::DefDatabase2, + expr::ExprId, generics::GenericParams, nameres::CrateDefMap, path::{Path, PathKind}, @@ -15,10 +16,10 @@ use rustc_hash::FxHashSet; use crate::{ code_model::Crate, - db::DefDatabase, + db::{DefDatabase, HirDatabase}, expr::{ExprScopes, PatId, ScopeId}, - Adt, Const, Container, DefWithBody, EnumVariant, Function, GenericDef, ImplBlock, Local, - MacroDef, Module, ModuleDef, PerNs, Static, Struct, Trait, TypeAlias, + Adt, Const, Container, DefWithBody, EnumVariant, Function, GenericDef, HasBody, ImplBlock, + Local, MacroDef, Module, ModuleDef, PerNs, Static, Struct, Trait, TypeAlias, }; #[derive(Debug, Clone, Default)] @@ -492,6 +493,30 @@ 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, + expr_id: ExprId, +) -> Resolver { + let scopes = owner.expr_scopes(db); + resolver_for_scope(db, owner, scopes.scope_for(expr_id)) +} + +pub(crate) fn resolver_for_scope( + db: &impl HirDatabase, + owner: DefWithBody, + scope_id: Option, +) -> Resolver { + let mut r = owner.resolver(db); + let scopes = owner.expr_scopes(db); + 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 +} + pub(crate) trait HasResolver { /// Builds a resolver for type references inside this def. fn resolver(self, db: &impl DefDatabase) -> Resolver; diff --git a/crates/ra_hir/src/source_binder.rs b/crates/ra_hir/src/source_binder.rs index 5abb8d693..29c928d51 100644 --- a/crates/ra_hir/src/source_binder.rs +++ b/crates/ra_hir/src/source_binder.rs @@ -21,9 +21,9 @@ use ra_syntax::{ use crate::{ db::HirDatabase, - expr::{self, BodySourceMap, ExprScopes, ScopeId}, + expr::{BodySourceMap, ExprScopes, ScopeId}, ids::LocationCtx, - resolve::{HasResolver, ScopeDef, TypeNs, ValueNs}, + resolve::{resolver_for_scope, HasResolver, ScopeDef, TypeNs, ValueNs}, ty::method_resolution::{self, implements_trait}, Adt, AssocItem, Const, DefWithBody, Either, Enum, EnumVariant, FromSource, Function, GenericParam, HasBody, HirFileId, Local, MacroDef, Module, Name, Path, Resolver, Static, @@ -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 = expr::resolver_for_scope(db, def, scope); + let resolver = resolver_for_scope(db, def, scope); SourceAnalyzer { resolver, body_owner: Some(def), diff --git a/crates/ra_hir/src/ty/infer/expr.rs b/crates/ra_hir/src/ty/infer/expr.rs index 5e68a1678..414b06ba1 100644 --- a/crates/ra_hir/src/ty/infer/expr.rs +++ b/crates/ra_hir/src/ty/infer/expr.rs @@ -12,8 +12,9 @@ use hir_expand::name; use super::{BindingMode, Expectation, InferenceContext, InferenceDiagnostic, TypeMismatch}; use crate::{ db::HirDatabase, - expr::{self, Array, BinaryOp, Expr, ExprId, Literal, Statement, UnaryOp}, + expr::{Array, BinaryOp, Expr, ExprId, Literal, Statement, UnaryOp}, generics::{GenericParams, HasGenericParams}, + resolve::resolver_for_expr, ty::{ autoderef, method_resolution, op, CallableDef, InferTy, IntTy, Mutability, Namespace, Obligation, ProjectionPredicate, ProjectionTy, Substs, TraitRef, Ty, TypeCtor, TypeWalk, @@ -186,7 +187,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { } Expr::Path(p) => { // FIXME this could be more efficient... - let resolver = expr::resolver_for_expr(self.db, self.owner, tgt_expr); + let resolver = resolver_for_expr(self.db, self.owner, tgt_expr); self.infer_path(&resolver, p, tgt_expr.into()).unwrap_or(Ty::Unknown) } Expr::Continue => Ty::simple(TypeCtor::Never), -- cgit v1.2.3 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/src') 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 From 061e6c77b5fdb63aa1cad63f1420a7fc810fa17d Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 21 Nov 2019 14:13:49 +0300 Subject: Decouple --- crates/ra_hir/src/code_model.rs | 2 +- crates/ra_hir/src/expr.rs | 2 +- crates/ra_hir/src/impl_block.rs | 4 +- crates/ra_hir/src/resolve.rs | 148 ++++++++++++++++++++++--------------- crates/ra_hir/src/source_binder.rs | 8 +- crates/ra_hir/src/ty/lower.rs | 16 ++-- 6 files changed, 105 insertions(+), 75 deletions(-) (limited to 'crates/ra_hir/src') diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs index 550ab1a98..c4935c0d7 100644 --- a/crates/ra_hir/src/code_model.rs +++ b/crates/ra_hir/src/code_model.rs @@ -829,7 +829,7 @@ impl Trait { } fn direct_super_traits(self, db: &impl HirDatabase) -> Vec { - let resolver = self.resolver(db); + let resolver = self.id.resolver(db); // returning the iterator directly doesn't easily work because of // lifetime problems, but since there usually shouldn't be more than a // few direct traits this should be fine (we could even use some kind of diff --git a/crates/ra_hir/src/expr.rs b/crates/ra_hir/src/expr.rs index 3ca0a4de5..9cdc0c645 100644 --- a/crates/ra_hir/src/expr.rs +++ b/crates/ra_hir/src/expr.rs @@ -122,7 +122,7 @@ impl<'a, 'b> ExprValidator<'a, 'b> { let std_result_path = known::std_result_result(); - let resolver = self.func.resolver(db); + let resolver = self.func.id.resolver(db); let std_result_enum = match resolver.resolve_known_enum(db, &std_result_path) { Some(it) => it, _ => return, diff --git a/crates/ra_hir/src/impl_block.rs b/crates/ra_hir/src/impl_block.rs index 964a3da8c..c84ceee62 100644 --- a/crates/ra_hir/src/impl_block.rs +++ b/crates/ra_hir/src/impl_block.rs @@ -27,12 +27,12 @@ impl ImplBlock { } pub fn target_ty(&self, db: &impl HirDatabase) -> Ty { - Ty::from_hir(db, &self.resolver(db), &self.target_type(db)) + Ty::from_hir(db, &self.id.resolver(db), &self.target_type(db)) } pub fn target_trait_ref(&self, db: &impl HirDatabase) -> Option { let target_ty = self.target_ty(db); - TraitRef::from_hir(db, &self.resolver(db), &self.target_trait(db)?, Some(target_ty)) + TraitRef::from_hir(db, &self.id.resolver(db), &self.target_trait(db)?, Some(target_ty)) } pub fn items(&self, db: &impl DefDatabase) -> Vec { diff --git a/crates/ra_hir/src/resolve.rs b/crates/ra_hir/src/resolve.rs index 770864ec5..ed9fa0491 100644 --- a/crates/ra_hir/src/resolve.rs +++ b/crates/ra_hir/src/resolve.rs @@ -8,18 +8,18 @@ use hir_def::{ generics::GenericParams, nameres::CrateDefMap, path::{Path, PathKind}, - AdtId, ConstId, CrateModuleId, DefWithBodyId, EnumId, EnumVariantId, FunctionId, GenericDefId, - ImplId, ModuleDefId, StaticId, StructId, TraitId, TypeAliasId, + AdtId, AstItemDef, ConstId, ContainerId, CrateModuleId, DefWithBodyId, EnumId, EnumVariantId, + FunctionId, GenericDefId, ImplId, Lookup, ModuleDefId, ModuleId, StaticId, StructId, TraitId, + TypeAliasId, UnionId, }; use hir_expand::name::{self, Name}; use rustc_hash::FxHashSet; use crate::{ code_model::Crate, - db::{DefDatabase, HirDatabase}, + db::HirDatabase, expr::{ExprScopes, PatId, ScopeId}, - Adt, Const, Container, DefWithBody, Function, GenericDef, ImplBlock, Local, MacroDef, Module, - ModuleDef, PerNs, Static, Trait, TypeAlias, + Adt, DefWithBody, GenericDef, ImplBlock, Local, MacroDef, ModuleDef, PerNs, }; #[derive(Debug, Clone, Default)] @@ -506,7 +506,7 @@ pub(crate) fn resolver_for_scope( owner: DefWithBodyId, scope_id: Option, ) -> Resolver { - let mut r = DefWithBody::from(owner).resolver(db); + let mut r = 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() { @@ -517,100 +517,130 @@ pub(crate) fn resolver_for_scope( pub(crate) trait HasResolver { /// Builds a resolver for type references inside this def. - fn resolver(self, db: &impl DefDatabase) -> Resolver; + fn resolver(self, db: &impl DefDatabase2) -> Resolver; } -impl HasResolver for Module { - fn resolver(self, db: &impl DefDatabase) -> Resolver { - let def_map = db.crate_def_map(self.id.krate); - Resolver::default().push_module_scope(def_map, self.id.module_id) +impl HasResolver for ModuleId { + fn resolver(self, db: &impl DefDatabase2) -> Resolver { + let def_map = db.crate_def_map(self.krate); + Resolver::default().push_module_scope(def_map, self.module_id) } } -impl HasResolver for Trait { - fn resolver(self, db: &impl DefDatabase) -> Resolver { - self.module(db).resolver(db).push_generic_params_scope(db, self.id.into()) +impl HasResolver for TraitId { + fn resolver(self, db: &impl DefDatabase2) -> Resolver { + self.module(db).resolver(db).push_generic_params_scope(db, self.into()) } } -impl> HasResolver for T { - fn resolver(self, db: &impl DefDatabase) -> Resolver { - let def = self.into(); - def.module(db) +impl HasResolver for AdtId { + fn resolver(self, db: &impl DefDatabase2) -> Resolver { + let module = match self { + AdtId::StructId(it) => it.0.module(db), + AdtId::UnionId(it) => it.0.module(db), + AdtId::EnumId(it) => it.module(db), + }; + + module .resolver(db) - .push_generic_params_scope(db, def.into()) - .push_scope(Scope::AdtScope(def.into())) + .push_generic_params_scope(db, self.into()) + .push_scope(Scope::AdtScope(self.into())) } } -impl HasResolver for Function { - fn resolver(self, db: &impl DefDatabase) -> Resolver { - self.container(db) - .map(|c| c.resolver(db)) - .unwrap_or_else(|| self.module(db).resolver(db)) - .push_generic_params_scope(db, self.id.into()) +impl HasResolver for StructId { + fn resolver(self, db: &impl DefDatabase2) -> Resolver { + AdtId::from(self).resolver(db) } } -impl HasResolver for DefWithBody { - fn resolver(self, db: &impl DefDatabase) -> Resolver { +impl HasResolver for UnionId { + fn resolver(self, db: &impl DefDatabase2) -> Resolver { + AdtId::from(self).resolver(db) + } +} + +impl HasResolver for EnumId { + fn resolver(self, db: &impl DefDatabase2) -> Resolver { + AdtId::from(self).resolver(db) + } +} + +impl HasResolver for FunctionId { + fn resolver(self, db: &impl DefDatabase2) -> Resolver { + self.lookup(db).container.resolver(db).push_generic_params_scope(db, self.into()) + } +} + +impl HasResolver for DefWithBodyId { + fn resolver(self, db: &impl DefDatabase2) -> Resolver { match self { - DefWithBody::Const(c) => c.resolver(db), - DefWithBody::Function(f) => f.resolver(db), - DefWithBody::Static(s) => s.resolver(db), + DefWithBodyId::ConstId(c) => c.resolver(db), + DefWithBodyId::FunctionId(f) => f.resolver(db), + DefWithBodyId::StaticId(s) => s.resolver(db), } } } -impl HasResolver for Const { - fn resolver(self, db: &impl DefDatabase) -> Resolver { - self.container(db).map(|c| c.resolver(db)).unwrap_or_else(|| self.module(db).resolver(db)) +impl HasResolver for ConstId { + fn resolver(self, db: &impl DefDatabase2) -> Resolver { + self.lookup(db).container.resolver(db) } } -impl HasResolver for Static { - fn resolver(self, db: &impl DefDatabase) -> Resolver { +impl HasResolver for StaticId { + fn resolver(self, db: &impl DefDatabase2) -> Resolver { self.module(db).resolver(db) } } -impl HasResolver for TypeAlias { - fn resolver(self, db: &impl DefDatabase) -> Resolver { - self.container(db) - .map(|ib| ib.resolver(db)) - .unwrap_or_else(|| self.module(db).resolver(db)) - .push_generic_params_scope(db, self.id.into()) +impl HasResolver for TypeAliasId { + fn resolver(self, db: &impl DefDatabase2) -> Resolver { + self.lookup(db).container.resolver(db).push_generic_params_scope(db, self.into()) } } -impl HasResolver for Container { - fn resolver(self, db: &impl DefDatabase) -> Resolver { +impl HasResolver for ContainerId { + fn resolver(self, db: &impl DefDatabase2) -> Resolver { match self { - Container::Trait(trait_) => trait_.resolver(db), - Container::ImplBlock(impl_block) => impl_block.resolver(db), + ContainerId::TraitId(it) => it.resolver(db), + ContainerId::ImplId(it) => it.resolver(db), + ContainerId::ModuleId(it) => it.resolver(db), } } } -impl HasResolver for GenericDef { - fn resolver(self, db: &impl DefDatabase) -> crate::Resolver { +impl HasResolver for GenericDefId { + fn resolver(self, db: &impl DefDatabase2) -> crate::Resolver { match self { - GenericDef::Function(inner) => inner.resolver(db), - GenericDef::Adt(adt) => adt.resolver(db), - GenericDef::Trait(inner) => inner.resolver(db), - GenericDef::TypeAlias(inner) => inner.resolver(db), - GenericDef::ImplBlock(inner) => inner.resolver(db), - GenericDef::EnumVariant(inner) => inner.parent_enum(db).resolver(db), - GenericDef::Const(inner) => inner.resolver(db), + GenericDefId::FunctionId(inner) => inner.resolver(db), + GenericDefId::AdtId(adt) => adt.resolver(db), + GenericDefId::TraitId(inner) => inner.resolver(db), + GenericDefId::TypeAliasId(inner) => inner.resolver(db), + GenericDefId::ImplId(inner) => inner.resolver(db), + GenericDefId::EnumVariantId(inner) => inner.parent.resolver(db), + GenericDefId::ConstId(inner) => inner.resolver(db), } } } -impl HasResolver for ImplBlock { - fn resolver(self, db: &impl DefDatabase) -> Resolver { +impl HasResolver for ImplId { + fn resolver(self, db: &impl DefDatabase2) -> Resolver { self.module(db) .resolver(db) - .push_generic_params_scope(db, self.id.into()) - .push_impl_block_scope(self.id) + .push_generic_params_scope(db, self.into()) + .push_impl_block_scope(self) + } +} + +impl HasResolver for GenericDef { + fn resolver(self, db: &impl DefDatabase2) -> Resolver { + GenericDefId::from(self).resolver(db) + } +} + +impl HasResolver for DefWithBody { + fn resolver(self, db: &impl DefDatabase2) -> Resolver { + DefWithBodyId::from(self).resolver(db) } } diff --git a/crates/ra_hir/src/source_binder.rs b/crates/ra_hir/src/source_binder.rs index 09df4f0aa..8e379498a 100644 --- a/crates/ra_hir/src/source_binder.rs +++ b/crates/ra_hir/src/source_binder.rs @@ -35,19 +35,19 @@ fn try_get_resolver_for_node(db: &impl HirDatabase, node: Source<&SyntaxNode>) - match (node.value) { ast::Module(it) => { let src = node.with_value(it); - Some(crate::Module::from_declaration(db, src)?.resolver(db)) + Some(crate::Module::from_declaration(db, src)?.id.resolver(db)) }, ast::SourceFile(it) => { let src = node.with_value(crate::ModuleSource::SourceFile(it)); - Some(crate::Module::from_definition(db, src)?.resolver(db)) + Some(crate::Module::from_definition(db, src)?.id.resolver(db)) }, ast::StructDef(it) => { let src = node.with_value(it); - Some(Struct::from_source(db, src)?.resolver(db)) + Some(Struct::from_source(db, src)?.id.resolver(db)) }, ast::EnumDef(it) => { let src = node.with_value(it); - Some(Enum::from_source(db, src)?.resolver(db)) + Some(Enum::from_source(db, src)?.id.resolver(db)) }, _ => match node.value.kind() { FN_DEF | CONST_DEF | STATIC_DEF => { diff --git a/crates/ra_hir/src/ty/lower.rs b/crates/ra_hir/src/ty/lower.rs index d4fbddac0..8ea2806dd 100644 --- a/crates/ra_hir/src/ty/lower.rs +++ b/crates/ra_hir/src/ty/lower.rs @@ -553,8 +553,8 @@ pub(crate) fn callable_item_sig(db: &impl HirDatabase, def: CallableDef) -> FnSi pub(crate) fn type_for_field(db: &impl HirDatabase, field: StructField) -> Ty { let parent_def = field.parent_def(db); let resolver = match parent_def { - VariantDef::Struct(it) => it.resolver(db), - VariantDef::EnumVariant(it) => it.parent_enum(db).resolver(db), + VariantDef::Struct(it) => it.id.resolver(db), + VariantDef::EnumVariant(it) => it.parent.id.resolver(db), }; let var_data = parent_def.variant_data(db); let type_ref = &var_data.fields().unwrap()[field.id].type_ref; @@ -623,7 +623,7 @@ pub(crate) fn generic_defaults_query(db: &impl HirDatabase, def: GenericDef) -> fn fn_sig_for_fn(db: &impl HirDatabase, def: Function) -> FnSig { let data = def.data(db); - let resolver = def.resolver(db); + let resolver = def.id.resolver(db); let params = data.params().iter().map(|tr| Ty::from_hir(db, &resolver, tr)).collect::>(); let ret = Ty::from_hir(db, &resolver, data.ret_type()); FnSig::from_params_and_return(params, ret) @@ -640,7 +640,7 @@ fn type_for_fn(db: &impl HirDatabase, def: Function) -> Ty { /// Build the declared type of a const. fn type_for_const(db: &impl HirDatabase, def: Const) -> Ty { let data = def.data(db); - let resolver = def.resolver(db); + let resolver = def.id.resolver(db); Ty::from_hir(db, &resolver, data.type_ref()) } @@ -648,7 +648,7 @@ fn type_for_const(db: &impl HirDatabase, def: Const) -> Ty { /// Build the declared type of a static. fn type_for_static(db: &impl HirDatabase, def: Static) -> Ty { let data = def.data(db); - let resolver = def.resolver(db); + let resolver = def.id.resolver(db); Ty::from_hir(db, &resolver, data.type_ref()) } @@ -700,7 +700,7 @@ fn fn_sig_for_struct_constructor(db: &impl HirDatabase, def: Struct) -> FnSig { Some(fields) => fields, None => panic!("fn_sig_for_struct_constructor called on unit struct"), }; - let resolver = def.resolver(db); + let resolver = def.id.resolver(db); let params = fields .iter() .map(|(_, field)| Ty::from_hir(db, &resolver, &field.type_ref)) @@ -726,7 +726,7 @@ fn fn_sig_for_enum_variant_constructor(db: &impl HirDatabase, def: EnumVariant) Some(fields) => fields, None => panic!("fn_sig_for_enum_variant_constructor called for unit variant"), }; - let resolver = def.parent_enum(db).resolver(db); + let resolver = def.parent.id.resolver(db); let params = fields .iter() .map(|(_, field)| Ty::from_hir(db, &resolver, &field.type_ref)) @@ -755,7 +755,7 @@ fn type_for_adt(db: &impl HirDatabase, adt: impl Into + HasGenericParams) - fn type_for_type_alias(db: &impl HirDatabase, t: TypeAlias) -> Ty { let generics = t.generic_params(db); - let resolver = t.resolver(db); + let resolver = t.id.resolver(db); let type_ref = t.type_ref(db); let substs = Substs::identity(&generics); let inner = Ty::from_hir(db, &resolver, &type_ref.unwrap_or(TypeRef::Error)); -- cgit v1.2.3 From 1312c57d342730690a0181c53f82bb3e281114ff Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 21 Nov 2019 14:21:26 +0300 Subject: Move ScopeDef --- crates/ra_hir/src/code_model.rs | 26 +++++++++++++++++++++++++- crates/ra_hir/src/lib.rs | 5 ++--- crates/ra_hir/src/resolve.rs | 25 +------------------------ crates/ra_hir/src/source_binder.rs | 6 +++--- 4 files changed, 31 insertions(+), 31 deletions(-) (limited to 'crates/ra_hir/src') diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs index c4935c0d7..39b01d510 100644 --- a/crates/ra_hir/src/code_model.rs +++ b/crates/ra_hir/src/code_model.rs @@ -10,6 +10,7 @@ use hir_def::{ adt::VariantData, body::scope::ExprScopes, builtin_type::BuiltinType, + nameres::per_ns::PerNs, traits::TraitData, type_ref::{Mutability, TypeRef}, ContainerId, CrateModuleId, HasModule, ImplId, LocalEnumVariantId, LocalStructFieldId, Lookup, @@ -32,7 +33,7 @@ use crate::{ }, resolve::{HasResolver, TypeNs}, ty::{InferenceResult, Namespace, TraitRef}, - Either, HasSource, ImportId, Name, ScopeDef, Source, Ty, + Either, HasSource, ImportId, Name, Source, Ty, }; /// hir::Crate describes a single crate. It's the main interface with which @@ -1064,3 +1065,26 @@ pub struct GenericParam { pub struct ImplBlock { pub(crate) id: ImplId, } + +/// For IDE only +pub enum ScopeDef { + ModuleDef(ModuleDef), + MacroDef(MacroDef), + GenericParam(u32), + ImplSelfType(ImplBlock), + AdtSelfType(Adt), + Local(Local), + Unknown, +} + +impl From for ScopeDef { + fn from(def: PerNs) -> Self { + def.take_types() + .or_else(|| def.take_values()) + .map(|module_def_id| ScopeDef::ModuleDef(module_def_id.into())) + .or_else(|| { + def.get_macros().map(|macro_def_id| ScopeDef::MacroDef(macro_def_id.into())) + }) + .unwrap_or(ScopeDef::Unknown) + } +} diff --git a/crates/ra_hir/src/lib.rs b/crates/ra_hir/src/lib.rs index 31da74d2f..095d4964f 100644 --- a/crates/ra_hir/src/lib.rs +++ b/crates/ra_hir/src/lib.rs @@ -61,14 +61,13 @@ pub use crate::{ src::{HasBodySource, HasSource}, Adt, AssocItem, Const, ConstData, Container, Crate, CrateDependency, DefWithBody, Enum, EnumVariant, FieldSource, FnData, Function, GenericParam, HasBody, ImplBlock, Local, - MacroDef, Module, ModuleDef, ModuleSource, Static, Struct, StructField, Trait, TypeAlias, - Union, VariantDef, + MacroDef, Module, ModuleDef, ModuleSource, ScopeDef, Static, Struct, StructField, Trait, + TypeAlias, Union, VariantDef, }, expr::ExprScopes, from_source::FromSource, generics::GenericDef, ids::{HirFileId, MacroCallId, MacroCallLoc, MacroDefId, MacroFile}, - resolve::ScopeDef, source_binder::{PathResolution, ScopeEntryWithSyntax, SourceAnalyzer}, ty::{ display::HirDisplay, diff --git a/crates/ra_hir/src/resolve.rs b/crates/ra_hir/src/resolve.rs index ed9fa0491..5e04ca9b6 100644 --- a/crates/ra_hir/src/resolve.rs +++ b/crates/ra_hir/src/resolve.rs @@ -19,7 +19,7 @@ use crate::{ code_model::Crate, db::HirDatabase, expr::{ExprScopes, PatId, ScopeId}, - Adt, DefWithBody, GenericDef, ImplBlock, Local, MacroDef, ModuleDef, PerNs, + DefWithBody, GenericDef, Local, MacroDef, PerNs, ScopeDef, }; #[derive(Debug, Clone, Default)] @@ -420,29 +420,6 @@ impl Resolver { } } -/// For IDE only -pub enum ScopeDef { - ModuleDef(ModuleDef), - MacroDef(MacroDef), - GenericParam(u32), - ImplSelfType(ImplBlock), - AdtSelfType(Adt), - Local(Local), - Unknown, -} - -impl From for ScopeDef { - fn from(def: PerNs) -> Self { - def.take_types() - .or_else(|| def.take_values()) - .map(|module_def_id| ScopeDef::ModuleDef(module_def_id.into())) - .or_else(|| { - def.get_macros().map(|macro_def_id| ScopeDef::MacroDef(macro_def_id.into())) - }) - .unwrap_or(ScopeDef::Unknown) - } -} - impl Scope { fn process_names(&self, db: &impl DefDatabase2, f: &mut dyn FnMut(Name, ScopeDef)) { match self { diff --git a/crates/ra_hir/src/source_binder.rs b/crates/ra_hir/src/source_binder.rs index 8e379498a..467e46d8c 100644 --- a/crates/ra_hir/src/source_binder.rs +++ b/crates/ra_hir/src/source_binder.rs @@ -23,11 +23,11 @@ use crate::{ db::HirDatabase, expr::{BodySourceMap, ExprScopes, ScopeId}, ids::LocationCtx, - resolve::{resolver_for_scope, HasResolver, ScopeDef, TypeNs, ValueNs}, + resolve::{resolver_for_scope, HasResolver, TypeNs, ValueNs}, ty::method_resolution::{self, implements_trait}, Adt, AssocItem, Const, DefWithBody, Either, Enum, EnumVariant, FromSource, Function, - GenericParam, HasBody, HirFileId, Local, MacroDef, Module, Name, Path, Resolver, Static, - Struct, Trait, Ty, TypeAlias, + GenericParam, HasBody, HirFileId, Local, MacroDef, Module, Name, Path, Resolver, ScopeDef, + Static, Struct, Trait, Ty, TypeAlias, }; fn try_get_resolver_for_node(db: &impl HirDatabase, node: Source<&SyntaxNode>) -> Option { -- cgit v1.2.3 From ca1af86e7bb104533f9878f84a7a3a39e5107c30 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 21 Nov 2019 14:22:30 +0300 Subject: Use GenericParam in ScopeDef --- crates/ra_hir/src/code_model.rs | 2 +- crates/ra_hir/src/resolve.rs | 12 +++++++++--- 2 files changed, 10 insertions(+), 4 deletions(-) (limited to 'crates/ra_hir/src') diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs index 39b01d510..3c891547e 100644 --- a/crates/ra_hir/src/code_model.rs +++ b/crates/ra_hir/src/code_model.rs @@ -1070,7 +1070,7 @@ pub struct ImplBlock { pub enum ScopeDef { ModuleDef(ModuleDef), MacroDef(MacroDef), - GenericParam(u32), + GenericParam(GenericParam), ImplSelfType(ImplBlock), AdtSelfType(Adt), Local(Local), diff --git a/crates/ra_hir/src/resolve.rs b/crates/ra_hir/src/resolve.rs index 5e04ca9b6..a029513b5 100644 --- a/crates/ra_hir/src/resolve.rs +++ b/crates/ra_hir/src/resolve.rs @@ -19,7 +19,7 @@ use crate::{ code_model::Crate, db::HirDatabase, expr::{ExprScopes, PatId, ScopeId}, - DefWithBody, GenericDef, Local, MacroDef, PerNs, ScopeDef, + DefWithBody, GenericDef, GenericParam, Local, MacroDef, PerNs, ScopeDef, }; #[derive(Debug, Clone, Default)] @@ -447,9 +447,15 @@ impl Scope { }); } } - Scope::GenericParams { params, .. } => { + Scope::GenericParams { params, def } => { for param in params.params.iter() { - f(param.name.clone(), ScopeDef::GenericParam(param.idx)) + f( + param.name.clone(), + ScopeDef::GenericParam(GenericParam { + parent: (*def).into(), + idx: param.idx, + }), + ) } } Scope::ImplBlockScope(i) => { -- cgit v1.2.3 From eb53aa37a336b6c9369ea931812f6c491e8f4a71 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 21 Nov 2019 15:13:46 +0300 Subject: Split internal and user-visible ScopeDef --- crates/ra_hir/src/resolve.rs | 38 +++++++++++++++++++++++--------------- crates/ra_hir/src/source_binder.rs | 19 +++++++++++++++++-- 2 files changed, 40 insertions(+), 17 deletions(-) (limited to 'crates/ra_hir/src') diff --git a/crates/ra_hir/src/resolve.rs b/crates/ra_hir/src/resolve.rs index a029513b5..7ff0c43ae 100644 --- a/crates/ra_hir/src/resolve.rs +++ b/crates/ra_hir/src/resolve.rs @@ -19,7 +19,7 @@ use crate::{ code_model::Crate, db::HirDatabase, expr::{ExprScopes, PatId, ScopeId}, - DefWithBody, GenericDef, GenericParam, Local, MacroDef, PerNs, ScopeDef, + DefWithBody, GenericDef, MacroDef, PerNs, }; #[derive(Debug, Clone, Default)] @@ -377,6 +377,13 @@ impl Resolver { _ => None, }) } + + pub(crate) fn body_owner(&self) -> Option { + self.scopes.iter().find_map(|scope| match scope { + Scope::ExprScope(it) => Some(it.owner), + _ => None, + }) + } } impl Resolver { @@ -420,6 +427,14 @@ impl Resolver { } } +pub(crate) enum ScopeDef { + PerNs(PerNs), + ImplSelfType(ImplId), + AdtSelfType(AdtId), + GenericParam(u32), + Local(PatId), +} + impl Scope { fn process_names(&self, db: &impl DefDatabase2, f: &mut dyn FnMut(Name, ScopeDef)) { match self { @@ -432,30 +447,24 @@ impl Scope { // }), // ); m.crate_def_map[m.module_id].scope.entries().for_each(|(name, res)| { - f(name.clone(), res.def.into()); + f(name.clone(), ScopeDef::PerNs(res.def)); }); m.crate_def_map[m.module_id].scope.legacy_macros().for_each(|(name, macro_)| { - f(name.clone(), ScopeDef::MacroDef(macro_.into())); + f(name.clone(), ScopeDef::PerNs(PerNs::macros(macro_))); }); m.crate_def_map.extern_prelude().iter().for_each(|(name, &def)| { - f(name.clone(), ScopeDef::ModuleDef(def.into())); + f(name.clone(), ScopeDef::PerNs(PerNs::types(def.into()))); }); if let Some(prelude) = m.crate_def_map.prelude() { let prelude_def_map = db.crate_def_map(prelude.krate); prelude_def_map[prelude.module_id].scope.entries().for_each(|(name, res)| { - f(name.clone(), res.def.into()); + f(name.clone(), ScopeDef::PerNs(res.def)); }); } } - Scope::GenericParams { params, def } => { + Scope::GenericParams { params, .. } => { for param in params.params.iter() { - f( - param.name.clone(), - ScopeDef::GenericParam(GenericParam { - parent: (*def).into(), - idx: param.idx, - }), - ) + f(param.name.clone(), ScopeDef::GenericParam(param.idx)) } } Scope::ImplBlockScope(i) => { @@ -466,8 +475,7 @@ impl Scope { } Scope::ExprScope(scope) => { scope.expr_scopes.entries(scope.scope_id).iter().for_each(|e| { - let local = Local { parent: scope.owner.into(), pat_id: e.pat() }; - f(e.name().clone(), ScopeDef::Local(local)); + f(e.name().clone(), ScopeDef::Local(e.pat())); }); } } diff --git a/crates/ra_hir/src/source_binder.rs b/crates/ra_hir/src/source_binder.rs index 467e46d8c..f13d43740 100644 --- a/crates/ra_hir/src/source_binder.rs +++ b/crates/ra_hir/src/source_binder.rs @@ -23,7 +23,7 @@ use crate::{ db::HirDatabase, expr::{BodySourceMap, ExprScopes, ScopeId}, ids::LocationCtx, - resolve::{resolver_for_scope, HasResolver, TypeNs, ValueNs}, + resolve::{self, resolver_for_scope, HasResolver, TypeNs, ValueNs}, ty::method_resolution::{self, implements_trait}, Adt, AssocItem, Const, DefWithBody, Either, Enum, EnumVariant, FromSource, Function, GenericParam, HasBody, HirFileId, Local, MacroDef, Module, Name, Path, Resolver, ScopeDef, @@ -310,7 +310,22 @@ impl SourceAnalyzer { } pub fn process_all_names(&self, db: &impl HirDatabase, f: &mut dyn FnMut(Name, ScopeDef)) { - self.resolver.process_all_names(db, f) + self.resolver.process_all_names(db, &mut |name, def| { + let def = match def { + resolve::ScopeDef::PerNs(it) => it.into(), + resolve::ScopeDef::ImplSelfType(it) => ScopeDef::ImplSelfType(it.into()), + resolve::ScopeDef::AdtSelfType(it) => ScopeDef::AdtSelfType(it.into()), + resolve::ScopeDef::GenericParam(idx) => { + let parent = self.resolver.generic_def().unwrap().into(); + ScopeDef::GenericParam(GenericParam { parent, idx }) + } + resolve::ScopeDef::Local(pat_id) => { + let parent = self.resolver.body_owner().unwrap().into(); + ScopeDef::Local(Local { parent, pat_id }) + } + }; + f(name, def) + }) } // FIXME: we only use this in `inline_local_variable` assist, ideally, we -- cgit v1.2.3 From 00684d708b64fe81a0264795f27594d450a8d08d Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 21 Nov 2019 15:24:51 +0300 Subject: Decouple --- crates/ra_hir/src/from_id.rs | 8 +++++- crates/ra_hir/src/resolve.rs | 42 +++++++++++-------------------- crates/ra_hir/src/source_binder.rs | 13 +++++++--- crates/ra_hir/src/ty/autoderef.rs | 4 +-- crates/ra_hir/src/ty/infer.rs | 9 ++++--- crates/ra_hir/src/ty/infer/coerce.rs | 4 +-- crates/ra_hir/src/ty/lower.rs | 7 +++--- crates/ra_hir/src/ty/method_resolution.rs | 15 +++++++---- 8 files changed, 53 insertions(+), 49 deletions(-) (limited to 'crates/ra_hir/src') diff --git a/crates/ra_hir/src/from_id.rs b/crates/ra_hir/src/from_id.rs index e294e957b..a3e9d8525 100644 --- a/crates/ra_hir/src/from_id.rs +++ b/crates/ra_hir/src/from_id.rs @@ -9,10 +9,16 @@ use hir_def::{ }; use crate::{ - ty::TypableDef, Adt, AssocItem, Const, DefWithBody, EnumVariant, Function, GenericDef, + ty::TypableDef, Adt, AssocItem, Const, Crate, DefWithBody, EnumVariant, Function, GenericDef, ModuleDef, Static, TypeAlias, }; +impl From for Crate { + fn from(crate_id: ra_db::CrateId) -> Self { + Crate { crate_id } + } +} + macro_rules! from_id { ($(($id:path, $ty:path)),*) => {$( impl From<$id> for $ty { diff --git a/crates/ra_hir/src/resolve.rs b/crates/ra_hir/src/resolve.rs index 7ff0c43ae..a616f0ea5 100644 --- a/crates/ra_hir/src/resolve.rs +++ b/crates/ra_hir/src/resolve.rs @@ -2,25 +2,23 @@ use std::sync::Arc; use hir_def::{ + body::scope::{ExprScopes, ScopeId}, builtin_type::BuiltinType, db::DefDatabase2, - expr::ExprId, + expr::{ExprId, PatId}, generics::GenericParams, - nameres::CrateDefMap, + nameres::{per_ns::PerNs, CrateDefMap}, path::{Path, PathKind}, AdtId, AstItemDef, ConstId, ContainerId, CrateModuleId, DefWithBodyId, EnumId, EnumVariantId, FunctionId, GenericDefId, ImplId, Lookup, ModuleDefId, ModuleId, StaticId, StructId, TraitId, TypeAliasId, UnionId, }; -use hir_expand::name::{self, Name}; -use rustc_hash::FxHashSet; - -use crate::{ - code_model::Crate, - db::HirDatabase, - expr::{ExprScopes, PatId, ScopeId}, - DefWithBody, GenericDef, MacroDef, PerNs, +use hir_expand::{ + name::{self, Name}, + MacroDefId, }; +use ra_db::CrateId; +use rustc_hash::FxHashSet; #[derive(Debug, Clone, Default)] pub(crate) struct Resolver { @@ -318,9 +316,9 @@ impl Resolver { &self, db: &impl DefDatabase2, path: &Path, - ) -> Option { + ) -> Option { let (item_map, module) = self.module()?; - item_map.resolve_path(db, module, path).0.get_macros().map(MacroDef::from) + item_map.resolve_path(db, module, path).0.get_macros() } pub(crate) fn process_all_names( @@ -355,8 +353,8 @@ impl Resolver { }) } - pub(crate) fn krate(&self) -> Option { - self.module().map(|t| Crate { crate_id: t.0.krate() }) + pub(crate) fn krate(&self) -> Option { + self.module().map(|t| t.0.krate()) } pub(crate) fn where_predicates_in_scope<'a>( @@ -484,7 +482,7 @@ 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, + db: &impl DefDatabase2, owner: DefWithBodyId, expr_id: ExprId, ) -> Resolver { @@ -493,7 +491,7 @@ pub(crate) fn resolver_for_expr( } pub(crate) fn resolver_for_scope( - db: &impl HirDatabase, + db: &impl DefDatabase2, owner: DefWithBodyId, scope_id: Option, ) -> Resolver { @@ -623,15 +621,3 @@ impl HasResolver for ImplId { .push_impl_block_scope(self) } } - -impl HasResolver for GenericDef { - fn resolver(self, db: &impl DefDatabase2) -> Resolver { - GenericDefId::from(self).resolver(db) - } -} - -impl HasResolver for DefWithBody { - fn resolver(self, db: &impl DefDatabase2) -> Resolver { - DefWithBodyId::from(self).resolver(db) - } -} diff --git a/crates/ra_hir/src/source_binder.rs b/crates/ra_hir/src/source_binder.rs index f13d43740..898b823c0 100644 --- a/crates/ra_hir/src/source_binder.rs +++ b/crates/ra_hir/src/source_binder.rs @@ -10,6 +10,7 @@ use std::sync::Arc; use hir_def::{ expr::{ExprId, PatId}, path::known, + DefWithBodyId, }; use hir_expand::{name::AsName, AstId, MacroCallId, MacroCallLoc, MacroFileKind, Source}; use ra_syntax::{ @@ -51,7 +52,9 @@ fn try_get_resolver_for_node(db: &impl HirDatabase, node: Source<&SyntaxNode>) - }, _ => match node.value.kind() { FN_DEF | CONST_DEF | STATIC_DEF => { - Some(def_with_body_from_child_node(db, node)?.resolver(db)) + let def = def_with_body_from_child_node(db, node)?; + let def = DefWithBodyId::from(def); + Some(def.resolver(db)) } // FIXME add missing cases _ => None @@ -232,7 +235,7 @@ impl SourceAnalyzer { ) -> Option { // This must be a normal source file rather than macro file. let path = macro_call.path().and_then(Path::from_ast)?; - self.resolver.resolve_path_as_macro(db, &path) + self.resolver.resolve_path_as_macro(db, &path).map(|it| it.into()) } pub fn resolve_hir_path( @@ -275,7 +278,9 @@ impl SourceAnalyzer { .take_types() .map(|it| PathResolution::Def(it.into())); types.or(values).or(items).or_else(|| { - self.resolver.resolve_path_as_macro(db, &path).map(|def| PathResolution::Macro(def)) + self.resolver + .resolve_path_as_macro(db, &path) + .map(|def| PathResolution::Macro(def.into())) }) } @@ -420,7 +425,7 @@ impl SourceAnalyzer { }; let canonical_ty = crate::ty::Canonical { value: ty, num_vars: 0 }; - implements_trait(&canonical_ty, db, &self.resolver, krate, std_future_trait) + implements_trait(&canonical_ty, db, &self.resolver, krate.into(), std_future_trait) } pub fn expand( diff --git a/crates/ra_hir/src/ty/autoderef.rs b/crates/ra_hir/src/ty/autoderef.rs index 872a4517d..f77492170 100644 --- a/crates/ra_hir/src/ty/autoderef.rs +++ b/crates/ra_hir/src/ty/autoderef.rs @@ -39,7 +39,7 @@ fn deref_by_trait( ty: &Canonical, ) -> Option> { let krate = resolver.krate()?; - let deref_trait = match db.lang_item(krate, "deref".into())? { + let deref_trait = match db.lang_item(krate.into(), "deref".into())? { crate::lang_item::LangItemTarget::Trait(t) => t, _ => return None, }; @@ -71,7 +71,7 @@ fn deref_by_trait( let canonical = super::Canonical { num_vars: 1 + ty.num_vars, value: in_env }; - let solution = db.trait_solve(krate, canonical)?; + let solution = db.trait_solve(krate.into(), canonical)?; match &solution { Solution::Unique(vars) => { diff --git a/crates/ra_hir/src/ty/infer.rs b/crates/ra_hir/src/ty/infer.rs index 684d66946..c3d65afa6 100644 --- a/crates/ra_hir/src/ty/infer.rs +++ b/crates/ra_hir/src/ty/infer.rs @@ -24,7 +24,7 @@ use rustc_hash::FxHashMap; use hir_def::{ path::known, type_ref::{Mutability, TypeRef}, - AdtId, + AdtId, DefWithBodyId, }; use hir_expand::{diagnostics::DiagnosticSink, name}; use ra_arena::map::ArenaMap; @@ -65,7 +65,7 @@ mod coerce; /// The entry point of type inference. pub fn infer_query(db: &impl HirDatabase, def: DefWithBody) -> Arc { let _p = profile("infer_query"); - let resolver = def.resolver(db); + let resolver = DefWithBodyId::from(def).resolver(db); let mut ctx = InferenceContext::new(db, def, resolver); match def { @@ -378,8 +378,9 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { for obligation in obligations { let in_env = InEnvironment::new(self.trait_env.clone(), obligation.clone()); let canonicalized = self.canonicalizer().canonicalize_obligation(in_env); - let solution = - self.db.trait_solve(self.resolver.krate().unwrap(), canonicalized.value.clone()); + let solution = self + .db + .trait_solve(self.resolver.krate().unwrap().into(), canonicalized.value.clone()); match solution { Some(Solution::Unique(substs)) => { diff --git a/crates/ra_hir/src/ty/infer/coerce.rs b/crates/ra_hir/src/ty/infer/coerce.rs index 6ea135126..6d297c268 100644 --- a/crates/ra_hir/src/ty/infer/coerce.rs +++ b/crates/ra_hir/src/ty/infer/coerce.rs @@ -49,8 +49,8 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { resolver: &Resolver, ) -> FxHashMap<(TypeCtor, TypeCtor), usize> { let krate = resolver.krate().unwrap(); - let impls = match db.lang_item(krate, "coerce_unsized".into()) { - Some(LangItemTarget::Trait(trait_)) => db.impls_for_trait(krate, trait_), + let impls = match db.lang_item(krate.into(), "coerce_unsized".into()) { + Some(LangItemTarget::Trait(trait_)) => db.impls_for_trait(krate.into(), trait_), _ => return FxHashMap::default(), }; diff --git a/crates/ra_hir/src/ty/lower.rs b/crates/ra_hir/src/ty/lower.rs index 8ea2806dd..e477b2439 100644 --- a/crates/ra_hir/src/ty/lower.rs +++ b/crates/ra_hir/src/ty/lower.rs @@ -12,6 +12,7 @@ use hir_def::{ builtin_type::{BuiltinFloat, BuiltinInt, BuiltinType}, path::{GenericArg, PathSegment}, type_ref::{TypeBound, TypeRef}, + GenericDefId, }; use super::{ @@ -574,7 +575,7 @@ pub(crate) fn generic_predicates_for_param_query( def: GenericDef, param_idx: u32, ) -> Arc<[GenericPredicate]> { - let resolver = def.resolver(db); + let resolver = GenericDefId::from(def).resolver(db); resolver .where_predicates_in_scope() // we have to filter out all other predicates *first*, before attempting to lower them @@ -600,7 +601,7 @@ pub(crate) fn generic_predicates_query( db: &impl HirDatabase, def: GenericDef, ) -> Arc<[GenericPredicate]> { - let resolver = def.resolver(db); + let resolver = GenericDefId::from(def).resolver(db); resolver .where_predicates_in_scope() .flat_map(|pred| GenericPredicate::from_where_predicate(db, &resolver, pred)) @@ -609,7 +610,7 @@ pub(crate) fn generic_predicates_query( /// Resolve the default type params from generics pub(crate) fn generic_defaults_query(db: &impl HirDatabase, def: GenericDef) -> Substs { - let resolver = def.resolver(db); + let resolver = GenericDefId::from(def).resolver(db); let generic_params = def.generic_params(db); let defaults = generic_params diff --git a/crates/ra_hir/src/ty/method_resolution.rs b/crates/ra_hir/src/ty/method_resolution.rs index 26dd06171..5ad72ef9f 100644 --- a/crates/ra_hir/src/ty/method_resolution.rs +++ b/crates/ra_hir/src/ty/method_resolution.rs @@ -172,9 +172,14 @@ pub(crate) fn iterate_method_candidates( // rustc does an autoderef and then autoref again). for derefed_ty in autoderef::autoderef(db, resolver, ty.clone()) { - if let Some(result) = - iterate_inherent_methods(&derefed_ty, db, name, mode, krate, &mut callback) - { + if let Some(result) = iterate_inherent_methods( + &derefed_ty, + db, + name, + mode, + krate.into(), + &mut callback, + ) { return Some(result); } if let Some(result) = iterate_trait_method_candidates( @@ -192,7 +197,7 @@ pub(crate) fn iterate_method_candidates( LookupMode::Path => { // No autoderef for path lookups if let Some(result) = - iterate_inherent_methods(&ty, db, name, mode, krate, &mut callback) + iterate_inherent_methods(&ty, db, name, mode, krate.into(), &mut callback) { return Some(result); } @@ -240,7 +245,7 @@ fn iterate_trait_method_candidates( } if !known_implemented { let goal = generic_implements_goal(db, env.clone(), t, ty.clone()); - if db.trait_solve(krate, goal).is_none() { + if db.trait_solve(krate.into(), goal).is_none() { continue 'traits; } } -- cgit v1.2.3 From 6d64798a2300858c74b1cc0a22f6d3df578288b3 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 21 Nov 2019 15:39:09 +0300 Subject: Move resolver to hir_def --- crates/ra_hir/src/code_model.rs | 2 +- crates/ra_hir/src/expr.rs | 3 +- crates/ra_hir/src/impl_block.rs | 5 +- crates/ra_hir/src/lib.rs | 3 - crates/ra_hir/src/resolve.rs | 623 ------------------------------ crates/ra_hir/src/source_binder.rs | 16 +- crates/ra_hir/src/ty/autoderef.rs | 3 +- crates/ra_hir/src/ty/infer.rs | 2 +- crates/ra_hir/src/ty/infer/coerce.rs | 6 +- crates/ra_hir/src/ty/infer/expr.rs | 5 +- crates/ra_hir/src/ty/infer/path.rs | 9 +- crates/ra_hir/src/ty/lower.rs | 2 +- crates/ra_hir/src/ty/method_resolution.rs | 2 +- 13 files changed, 29 insertions(+), 652 deletions(-) delete mode 100644 crates/ra_hir/src/resolve.rs (limited to 'crates/ra_hir/src') diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs index 3c891547e..92860fb59 100644 --- a/crates/ra_hir/src/code_model.rs +++ b/crates/ra_hir/src/code_model.rs @@ -11,6 +11,7 @@ use hir_def::{ body::scope::ExprScopes, builtin_type::BuiltinType, nameres::per_ns::PerNs, + resolver::{HasResolver, TypeNs}, traits::TraitData, type_ref::{Mutability, TypeRef}, ContainerId, CrateModuleId, HasModule, ImplId, LocalEnumVariantId, LocalStructFieldId, Lookup, @@ -31,7 +32,6 @@ use crate::{ AstItemDef, ConstId, EnumId, FunctionId, MacroDefId, StaticId, StructId, TraitId, TypeAliasId, }, - resolve::{HasResolver, TypeNs}, ty::{InferenceResult, Namespace, TraitRef}, Either, HasSource, ImportId, Name, Source, Ty, }; diff --git a/crates/ra_hir/src/expr.rs b/crates/ra_hir/src/expr.rs index 9cdc0c645..6b703d8b4 100644 --- a/crates/ra_hir/src/expr.rs +++ b/crates/ra_hir/src/expr.rs @@ -2,7 +2,7 @@ use std::sync::Arc; -use hir_def::path::known; +use hir_def::{path::known, resolver::HasResolver}; use hir_expand::diagnostics::DiagnosticSink; use ra_syntax::ast; use ra_syntax::AstPtr; @@ -11,7 +11,6 @@ use rustc_hash::FxHashSet; use crate::{ db::HirDatabase, diagnostics::{MissingFields, MissingOkInTailExpr}, - resolve::HasResolver, ty::{ApplicationTy, InferenceResult, Ty, TypeCtor}, Adt, Function, Name, Path, }; diff --git a/crates/ra_hir/src/impl_block.rs b/crates/ra_hir/src/impl_block.rs index c84ceee62..774fa1d96 100644 --- a/crates/ra_hir/src/impl_block.rs +++ b/crates/ra_hir/src/impl_block.rs @@ -1,11 +1,10 @@ //! FIXME: write short doc here -use hir_def::{type_ref::TypeRef, AstItemDef}; -use ra_syntax::ast::{self}; +use hir_def::{resolver::HasResolver, type_ref::TypeRef, AstItemDef}; +use ra_syntax::ast; use crate::{ db::{AstDatabase, DefDatabase, HirDatabase}, - resolve::HasResolver, ty::Ty, AssocItem, Crate, HasSource, ImplBlock, Module, Source, TraitRef, }; diff --git a/crates/ra_hir/src/lib.rs b/crates/ra_hir/src/lib.rs index 095d4964f..76c96bdcf 100644 --- a/crates/ra_hir/src/lib.rs +++ b/crates/ra_hir/src/lib.rs @@ -38,7 +38,6 @@ mod impl_block; mod expr; mod lang_item; pub mod generics; -mod resolve; pub mod diagnostics; mod util; @@ -52,8 +51,6 @@ mod test_db; #[cfg(test)] mod marks; -use crate::resolve::Resolver; - pub use crate::{ code_model::{ attrs::{AttrDef, Attrs}, diff --git a/crates/ra_hir/src/resolve.rs b/crates/ra_hir/src/resolve.rs deleted file mode 100644 index a616f0ea5..000000000 --- a/crates/ra_hir/src/resolve.rs +++ /dev/null @@ -1,623 +0,0 @@ -//! Name resolution. -use std::sync::Arc; - -use hir_def::{ - body::scope::{ExprScopes, ScopeId}, - builtin_type::BuiltinType, - db::DefDatabase2, - expr::{ExprId, PatId}, - generics::GenericParams, - nameres::{per_ns::PerNs, CrateDefMap}, - path::{Path, PathKind}, - AdtId, AstItemDef, ConstId, ContainerId, CrateModuleId, DefWithBodyId, EnumId, EnumVariantId, - FunctionId, GenericDefId, ImplId, Lookup, ModuleDefId, ModuleId, StaticId, StructId, TraitId, - TypeAliasId, UnionId, -}; -use hir_expand::{ - name::{self, Name}, - MacroDefId, -}; -use ra_db::CrateId; -use rustc_hash::FxHashSet; - -#[derive(Debug, Clone, Default)] -pub(crate) struct Resolver { - scopes: Vec, -} - -// FIXME how to store these best -#[derive(Debug, Clone)] -pub(crate) struct ModuleItemMap { - crate_def_map: Arc, - module_id: CrateModuleId, -} - -#[derive(Debug, Clone)] -pub(crate) struct ExprScope { - owner: DefWithBodyId, - expr_scopes: Arc, - scope_id: ScopeId, -} - -#[derive(Debug, Clone)] -pub(crate) enum Scope { - /// All the items and imported names of a module - ModuleScope(ModuleItemMap), - /// Brings the generic parameters of an item into scope - GenericParams { def: GenericDefId, params: Arc }, - /// Brings `Self` in `impl` block into scope - ImplBlockScope(ImplId), - /// Brings `Self` in enum, struct and union definitions into scope - AdtScope(AdtId), - /// Local bindings - ExprScope(ExprScope), -} - -#[derive(Debug, Clone, PartialEq, Eq, Hash)] -pub(crate) enum TypeNs { - SelfType(ImplId), - GenericParam(u32), - AdtId(AdtId), - AdtSelfType(AdtId), - EnumVariantId(EnumVariantId), - TypeAliasId(TypeAliasId), - BuiltinType(BuiltinType), - TraitId(TraitId), - // Module belong to type ns, but the resolver is used when all module paths - // are fully resolved. - // ModuleId(ModuleId) -} - -#[derive(Debug, Clone, PartialEq, Eq, Hash)] -pub(crate) enum ResolveValueResult { - ValueNs(ValueNs), - Partial(TypeNs, usize), -} - -#[derive(Debug, Clone, PartialEq, Eq, Hash)] -pub(crate) enum ValueNs { - LocalBinding(PatId), - FunctionId(FunctionId), - ConstId(ConstId), - StaticId(StaticId), - StructId(StructId), - EnumVariantId(EnumVariantId), -} - -impl Resolver { - /// Resolve known trait from std, like `std::futures::Future` - pub(crate) fn resolve_known_trait( - &self, - db: &impl DefDatabase2, - path: &Path, - ) -> Option { - let res = self.resolve_module_path(db, path).take_types()?; - match res { - ModuleDefId::TraitId(it) => Some(it), - _ => None, - } - } - - /// Resolve known struct from std, like `std::boxed::Box` - pub(crate) fn resolve_known_struct( - &self, - db: &impl DefDatabase2, - path: &Path, - ) -> Option { - let res = self.resolve_module_path(db, path).take_types()?; - match res { - ModuleDefId::AdtId(AdtId::StructId(it)) => Some(it), - _ => None, - } - } - - /// Resolve known enum from std, like `std::result::Result` - pub(crate) fn resolve_known_enum(&self, db: &impl DefDatabase2, path: &Path) -> Option { - let res = self.resolve_module_path(db, path).take_types()?; - match res { - ModuleDefId::AdtId(AdtId::EnumId(it)) => Some(it), - _ => None, - } - } - - /// pub only for source-binder - pub(crate) fn resolve_module_path(&self, db: &impl DefDatabase2, path: &Path) -> PerNs { - let (item_map, module) = match self.module() { - Some(it) => it, - None => return PerNs::none(), - }; - let (module_res, segment_index) = item_map.resolve_path(db, module, path); - if segment_index.is_some() { - return PerNs::none(); - } - module_res - } - - pub(crate) fn resolve_path_in_type_ns( - &self, - db: &impl DefDatabase2, - path: &Path, - ) -> Option<(TypeNs, Option)> { - if path.is_type_relative() { - return None; - } - let first_name = &path.segments.first()?.name; - let skip_to_mod = path.kind != PathKind::Plain; - for scope in self.scopes.iter().rev() { - match scope { - Scope::ExprScope(_) => continue, - Scope::GenericParams { .. } | Scope::ImplBlockScope(_) if skip_to_mod => continue, - - Scope::GenericParams { params, .. } => { - if let Some(param) = params.find_by_name(first_name) { - let idx = if path.segments.len() == 1 { None } else { Some(1) }; - return Some((TypeNs::GenericParam(param.idx), idx)); - } - } - Scope::ImplBlockScope(impl_) => { - if first_name == &name::SELF_TYPE { - let idx = if path.segments.len() == 1 { None } else { Some(1) }; - return Some((TypeNs::SelfType(*impl_), idx)); - } - } - Scope::AdtScope(adt) => { - if first_name == &name::SELF_TYPE { - let idx = if path.segments.len() == 1 { None } else { Some(1) }; - return Some((TypeNs::AdtSelfType(*adt), idx)); - } - } - Scope::ModuleScope(m) => { - let (module_def, idx) = m.crate_def_map.resolve_path(db, m.module_id, path); - let res = match module_def.take_types()? { - ModuleDefId::AdtId(it) => TypeNs::AdtId(it), - ModuleDefId::EnumVariantId(it) => TypeNs::EnumVariantId(it), - - ModuleDefId::TypeAliasId(it) => TypeNs::TypeAliasId(it), - ModuleDefId::BuiltinType(it) => TypeNs::BuiltinType(it), - - ModuleDefId::TraitId(it) => TypeNs::TraitId(it), - - ModuleDefId::FunctionId(_) - | ModuleDefId::ConstId(_) - | ModuleDefId::StaticId(_) - | ModuleDefId::ModuleId(_) => return None, - }; - return Some((res, idx)); - } - } - } - None - } - - pub(crate) fn resolve_path_in_type_ns_fully( - &self, - db: &impl DefDatabase2, - path: &Path, - ) -> Option { - let (res, unresolved) = self.resolve_path_in_type_ns(db, path)?; - if unresolved.is_some() { - return None; - } - Some(res) - } - - pub(crate) fn resolve_path_in_value_ns<'p>( - &self, - db: &impl DefDatabase2, - path: &'p Path, - ) -> Option { - if path.is_type_relative() { - return None; - } - let n_segments = path.segments.len(); - let tmp = name::SELF_PARAM; - let first_name = if path.is_self() { &tmp } else { &path.segments.first()?.name }; - let skip_to_mod = path.kind != PathKind::Plain && !path.is_self(); - for scope in self.scopes.iter().rev() { - match scope { - Scope::AdtScope(_) - | Scope::ExprScope(_) - | Scope::GenericParams { .. } - | Scope::ImplBlockScope(_) - if skip_to_mod => - { - continue - } - - Scope::ExprScope(scope) if n_segments <= 1 => { - let entry = scope - .expr_scopes - .entries(scope.scope_id) - .iter() - .find(|entry| entry.name() == first_name); - - if let Some(e) = entry { - return Some(ResolveValueResult::ValueNs(ValueNs::LocalBinding(e.pat()))); - } - } - Scope::ExprScope(_) => continue, - - Scope::GenericParams { params, .. } if n_segments > 1 => { - if let Some(param) = params.find_by_name(first_name) { - let ty = TypeNs::GenericParam(param.idx); - return Some(ResolveValueResult::Partial(ty, 1)); - } - } - Scope::GenericParams { .. } => continue, - - Scope::ImplBlockScope(impl_) if n_segments > 1 => { - if first_name == &name::SELF_TYPE { - let ty = TypeNs::SelfType(*impl_); - return Some(ResolveValueResult::Partial(ty, 1)); - } - } - Scope::AdtScope(adt) if n_segments > 1 => { - if first_name == &name::SELF_TYPE { - let ty = TypeNs::AdtSelfType(*adt); - return Some(ResolveValueResult::Partial(ty, 1)); - } - } - Scope::ImplBlockScope(_) | Scope::AdtScope(_) => continue, - - Scope::ModuleScope(m) => { - let (module_def, idx) = m.crate_def_map.resolve_path(db, m.module_id, path); - return match idx { - None => { - let value = match module_def.take_values()? { - 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(_)) - | ModuleDefId::TraitId(_) - | ModuleDefId::TypeAliasId(_) - | ModuleDefId::BuiltinType(_) - | ModuleDefId::ModuleId(_) => return None, - }; - Some(ResolveValueResult::ValueNs(value)) - } - Some(idx) => { - let ty = match module_def.take_types()? { - ModuleDefId::AdtId(it) => TypeNs::AdtId(it), - ModuleDefId::TraitId(it) => TypeNs::TraitId(it), - ModuleDefId::TypeAliasId(it) => TypeNs::TypeAliasId(it), - ModuleDefId::BuiltinType(it) => TypeNs::BuiltinType(it), - - ModuleDefId::ModuleId(_) - | ModuleDefId::FunctionId(_) - | ModuleDefId::EnumVariantId(_) - | ModuleDefId::ConstId(_) - | ModuleDefId::StaticId(_) => return None, - }; - Some(ResolveValueResult::Partial(ty, idx)) - } - }; - } - } - } - None - } - - pub(crate) fn resolve_path_in_value_ns_fully( - &self, - db: &impl DefDatabase2, - path: &Path, - ) -> Option { - match self.resolve_path_in_value_ns(db, path)? { - ResolveValueResult::ValueNs(it) => Some(it), - ResolveValueResult::Partial(..) => None, - } - } - - pub(crate) fn resolve_path_as_macro( - &self, - db: &impl DefDatabase2, - path: &Path, - ) -> Option { - let (item_map, module) = self.module()?; - item_map.resolve_path(db, module, path).0.get_macros() - } - - pub(crate) fn process_all_names( - &self, - db: &impl DefDatabase2, - f: &mut dyn FnMut(Name, ScopeDef), - ) { - for scope in self.scopes.iter().rev() { - scope.process_names(db, f); - } - } - - pub(crate) fn traits_in_scope(&self, db: &impl DefDatabase2) -> FxHashSet { - let mut traits = FxHashSet::default(); - for scope in &self.scopes { - if let Scope::ModuleScope(m) = scope { - if let Some(prelude) = m.crate_def_map.prelude() { - let prelude_def_map = db.crate_def_map(prelude.krate); - traits.extend(prelude_def_map[prelude.module_id].scope.traits()); - } - traits.extend(m.crate_def_map[m.module_id].scope.traits()); - } - } - traits - } - - fn module(&self) -> Option<(&CrateDefMap, CrateModuleId)> { - self.scopes.iter().rev().find_map(|scope| match scope { - Scope::ModuleScope(m) => Some((&*m.crate_def_map, m.module_id)), - - _ => None, - }) - } - - pub(crate) fn krate(&self) -> Option { - self.module().map(|t| t.0.krate()) - } - - pub(crate) fn where_predicates_in_scope<'a>( - &'a self, - ) -> impl Iterator + 'a { - self.scopes - .iter() - .filter_map(|scope| match scope { - Scope::GenericParams { params, .. } => Some(params), - _ => None, - }) - .flat_map(|params| params.where_predicates.iter()) - } - - pub(crate) fn generic_def(&self) -> Option { - self.scopes.iter().find_map(|scope| match scope { - Scope::GenericParams { def, .. } => Some(*def), - _ => None, - }) - } - - pub(crate) fn body_owner(&self) -> Option { - self.scopes.iter().find_map(|scope| match scope { - Scope::ExprScope(it) => Some(it.owner), - _ => None, - }) - } -} - -impl Resolver { - pub(crate) fn push_scope(mut self, scope: Scope) -> Resolver { - self.scopes.push(scope); - self - } - - pub(crate) fn push_generic_params_scope( - self, - db: &impl DefDatabase2, - def: GenericDefId, - ) -> Resolver { - let params = db.generic_params(def); - if params.params.is_empty() { - self - } else { - self.push_scope(Scope::GenericParams { def, params }) - } - } - - pub(crate) fn push_impl_block_scope(self, impl_block: ImplId) -> Resolver { - self.push_scope(Scope::ImplBlockScope(impl_block)) - } - - pub(crate) fn push_module_scope( - self, - crate_def_map: Arc, - module_id: CrateModuleId, - ) -> Resolver { - self.push_scope(Scope::ModuleScope(ModuleItemMap { crate_def_map, module_id })) - } - - pub(crate) fn push_expr_scope( - self, - owner: DefWithBodyId, - expr_scopes: Arc, - scope_id: ScopeId, - ) -> Resolver { - self.push_scope(Scope::ExprScope(ExprScope { owner, expr_scopes, scope_id })) - } -} - -pub(crate) enum ScopeDef { - PerNs(PerNs), - ImplSelfType(ImplId), - AdtSelfType(AdtId), - GenericParam(u32), - Local(PatId), -} - -impl Scope { - fn process_names(&self, db: &impl DefDatabase2, f: &mut dyn FnMut(Name, ScopeDef)) { - match self { - Scope::ModuleScope(m) => { - // FIXME: should we provide `self` here? - // f( - // Name::self_param(), - // PerNs::types(Resolution::Def { - // def: m.module.into(), - // }), - // ); - m.crate_def_map[m.module_id].scope.entries().for_each(|(name, res)| { - f(name.clone(), ScopeDef::PerNs(res.def)); - }); - m.crate_def_map[m.module_id].scope.legacy_macros().for_each(|(name, macro_)| { - f(name.clone(), ScopeDef::PerNs(PerNs::macros(macro_))); - }); - m.crate_def_map.extern_prelude().iter().for_each(|(name, &def)| { - f(name.clone(), ScopeDef::PerNs(PerNs::types(def.into()))); - }); - if let Some(prelude) = m.crate_def_map.prelude() { - let prelude_def_map = db.crate_def_map(prelude.krate); - prelude_def_map[prelude.module_id].scope.entries().for_each(|(name, res)| { - f(name.clone(), ScopeDef::PerNs(res.def)); - }); - } - } - Scope::GenericParams { params, .. } => { - for param in params.params.iter() { - f(param.name.clone(), ScopeDef::GenericParam(param.idx)) - } - } - Scope::ImplBlockScope(i) => { - f(name::SELF_TYPE, ScopeDef::ImplSelfType((*i).into())); - } - Scope::AdtScope(i) => { - f(name::SELF_TYPE, ScopeDef::AdtSelfType((*i).into())); - } - Scope::ExprScope(scope) => { - scope.expr_scopes.entries(scope.scope_id).iter().for_each(|e| { - f(e.name().clone(), ScopeDef::Local(e.pat())); - }); - } - } - } -} - -// needs arbitrary_self_types to be a method... or maybe move to the def? -pub(crate) fn resolver_for_expr( - db: &impl DefDatabase2, - owner: DefWithBodyId, - expr_id: ExprId, -) -> Resolver { - let scopes = db.expr_scopes(owner); - resolver_for_scope(db, owner, scopes.scope_for(expr_id)) -} - -pub(crate) fn resolver_for_scope( - db: &impl DefDatabase2, - owner: DefWithBodyId, - scope_id: Option, -) -> Resolver { - let mut r = 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, Arc::clone(&scopes), scope); - } - r -} - -pub(crate) trait HasResolver { - /// Builds a resolver for type references inside this def. - fn resolver(self, db: &impl DefDatabase2) -> Resolver; -} - -impl HasResolver for ModuleId { - fn resolver(self, db: &impl DefDatabase2) -> Resolver { - let def_map = db.crate_def_map(self.krate); - Resolver::default().push_module_scope(def_map, self.module_id) - } -} - -impl HasResolver for TraitId { - fn resolver(self, db: &impl DefDatabase2) -> Resolver { - self.module(db).resolver(db).push_generic_params_scope(db, self.into()) - } -} - -impl HasResolver for AdtId { - fn resolver(self, db: &impl DefDatabase2) -> Resolver { - let module = match self { - AdtId::StructId(it) => it.0.module(db), - AdtId::UnionId(it) => it.0.module(db), - AdtId::EnumId(it) => it.module(db), - }; - - module - .resolver(db) - .push_generic_params_scope(db, self.into()) - .push_scope(Scope::AdtScope(self.into())) - } -} - -impl HasResolver for StructId { - fn resolver(self, db: &impl DefDatabase2) -> Resolver { - AdtId::from(self).resolver(db) - } -} - -impl HasResolver for UnionId { - fn resolver(self, db: &impl DefDatabase2) -> Resolver { - AdtId::from(self).resolver(db) - } -} - -impl HasResolver for EnumId { - fn resolver(self, db: &impl DefDatabase2) -> Resolver { - AdtId::from(self).resolver(db) - } -} - -impl HasResolver for FunctionId { - fn resolver(self, db: &impl DefDatabase2) -> Resolver { - self.lookup(db).container.resolver(db).push_generic_params_scope(db, self.into()) - } -} - -impl HasResolver for DefWithBodyId { - fn resolver(self, db: &impl DefDatabase2) -> Resolver { - match self { - DefWithBodyId::ConstId(c) => c.resolver(db), - DefWithBodyId::FunctionId(f) => f.resolver(db), - DefWithBodyId::StaticId(s) => s.resolver(db), - } - } -} - -impl HasResolver for ConstId { - fn resolver(self, db: &impl DefDatabase2) -> Resolver { - self.lookup(db).container.resolver(db) - } -} - -impl HasResolver for StaticId { - fn resolver(self, db: &impl DefDatabase2) -> Resolver { - self.module(db).resolver(db) - } -} - -impl HasResolver for TypeAliasId { - fn resolver(self, db: &impl DefDatabase2) -> Resolver { - self.lookup(db).container.resolver(db).push_generic_params_scope(db, self.into()) - } -} - -impl HasResolver for ContainerId { - fn resolver(self, db: &impl DefDatabase2) -> Resolver { - match self { - ContainerId::TraitId(it) => it.resolver(db), - ContainerId::ImplId(it) => it.resolver(db), - ContainerId::ModuleId(it) => it.resolver(db), - } - } -} - -impl HasResolver for GenericDefId { - fn resolver(self, db: &impl DefDatabase2) -> crate::Resolver { - match self { - GenericDefId::FunctionId(inner) => inner.resolver(db), - GenericDefId::AdtId(adt) => adt.resolver(db), - GenericDefId::TraitId(inner) => inner.resolver(db), - GenericDefId::TypeAliasId(inner) => inner.resolver(db), - GenericDefId::ImplId(inner) => inner.resolver(db), - GenericDefId::EnumVariantId(inner) => inner.parent.resolver(db), - GenericDefId::ConstId(inner) => inner.resolver(db), - } - } -} - -impl HasResolver for ImplId { - fn resolver(self, db: &impl DefDatabase2) -> Resolver { - self.module(db) - .resolver(db) - .push_generic_params_scope(db, self.into()) - .push_impl_block_scope(self) - } -} diff --git a/crates/ra_hir/src/source_binder.rs b/crates/ra_hir/src/source_binder.rs index 898b823c0..c42ceabdf 100644 --- a/crates/ra_hir/src/source_binder.rs +++ b/crates/ra_hir/src/source_binder.rs @@ -10,6 +10,7 @@ use std::sync::Arc; use hir_def::{ expr::{ExprId, PatId}, path::known, + resolver::{self, resolver_for_scope, HasResolver, Resolver, TypeNs, ValueNs}, DefWithBodyId, }; use hir_expand::{name::AsName, AstId, MacroCallId, MacroCallLoc, MacroFileKind, Source}; @@ -24,11 +25,10 @@ use crate::{ db::HirDatabase, expr::{BodySourceMap, ExprScopes, ScopeId}, ids::LocationCtx, - resolve::{self, resolver_for_scope, HasResolver, TypeNs, ValueNs}, ty::method_resolution::{self, implements_trait}, Adt, AssocItem, Const, DefWithBody, Either, Enum, EnumVariant, FromSource, Function, - GenericParam, HasBody, HirFileId, Local, MacroDef, Module, Name, Path, Resolver, ScopeDef, - Static, Struct, Trait, Ty, TypeAlias, + GenericParam, HasBody, HirFileId, Local, MacroDef, Module, Name, Path, ScopeDef, Static, + Struct, Trait, Ty, TypeAlias, }; fn try_get_resolver_for_node(db: &impl HirDatabase, node: Source<&SyntaxNode>) -> Option { @@ -317,14 +317,14 @@ impl SourceAnalyzer { pub fn process_all_names(&self, db: &impl HirDatabase, f: &mut dyn FnMut(Name, ScopeDef)) { self.resolver.process_all_names(db, &mut |name, def| { let def = match def { - resolve::ScopeDef::PerNs(it) => it.into(), - resolve::ScopeDef::ImplSelfType(it) => ScopeDef::ImplSelfType(it.into()), - resolve::ScopeDef::AdtSelfType(it) => ScopeDef::AdtSelfType(it.into()), - resolve::ScopeDef::GenericParam(idx) => { + resolver::ScopeDef::PerNs(it) => it.into(), + resolver::ScopeDef::ImplSelfType(it) => ScopeDef::ImplSelfType(it.into()), + resolver::ScopeDef::AdtSelfType(it) => ScopeDef::AdtSelfType(it.into()), + resolver::ScopeDef::GenericParam(idx) => { let parent = self.resolver.generic_def().unwrap().into(); ScopeDef::GenericParam(GenericParam { parent, idx }) } - resolve::ScopeDef::Local(pat_id) => { + resolver::ScopeDef::Local(pat_id) => { let parent = self.resolver.body_owner().unwrap().into(); ScopeDef::Local(Local { parent, pat_id }) } diff --git a/crates/ra_hir/src/ty/autoderef.rs b/crates/ra_hir/src/ty/autoderef.rs index f77492170..5d8518041 100644 --- a/crates/ra_hir/src/ty/autoderef.rs +++ b/crates/ra_hir/src/ty/autoderef.rs @@ -5,11 +5,12 @@ use std::iter::successors; +use hir_def::resolver::Resolver; use hir_expand::name; use log::{info, warn}; use super::{traits::Solution, Canonical, Substs, Ty, TypeWalk}; -use crate::{db::HirDatabase, generics::HasGenericParams, Resolver}; +use crate::{db::HirDatabase, generics::HasGenericParams}; const AUTODEREF_RECURSION_LIMIT: usize = 10; diff --git a/crates/ra_hir/src/ty/infer.rs b/crates/ra_hir/src/ty/infer.rs index c3d65afa6..69b13baef 100644 --- a/crates/ra_hir/src/ty/infer.rs +++ b/crates/ra_hir/src/ty/infer.rs @@ -23,6 +23,7 @@ use rustc_hash::FxHashMap; use hir_def::{ path::known, + resolver::{HasResolver, Resolver, TypeNs}, type_ref::{Mutability, TypeRef}, AdtId, DefWithBodyId, }; @@ -41,7 +42,6 @@ use crate::{ code_model::TypeAlias, db::HirDatabase, expr::{BindingAnnotation, Body, ExprId, PatId}, - resolve::{HasResolver, Resolver, TypeNs}, ty::infer::diagnostics::InferenceDiagnostic, Adt, AssocItem, ConstData, DefWithBody, FloatTy, FnData, Function, HasBody, IntTy, Path, StructField, Trait, VariantDef, diff --git a/crates/ra_hir/src/ty/infer/coerce.rs b/crates/ra_hir/src/ty/infer/coerce.rs index 6d297c268..0772b9df5 100644 --- a/crates/ra_hir/src/ty/infer/coerce.rs +++ b/crates/ra_hir/src/ty/infer/coerce.rs @@ -4,19 +4,19 @@ //! //! See: https://doc.rust-lang.org/nomicon/coercions.html +use hir_def::resolver::Resolver; use rustc_hash::FxHashMap; - use test_utils::tested_by; -use super::{InferTy, InferenceContext, TypeVarValue}; use crate::{ db::HirDatabase, lang_item::LangItemTarget, - resolve::Resolver, ty::{autoderef, Substs, Ty, TypeCtor, TypeWalk}, Adt, Mutability, }; +use super::{InferTy, InferenceContext, TypeVarValue}; + impl<'a, D: HirDatabase> InferenceContext<'a, D> { /// Unify two types, but may coerce the first one to the second one /// using "implicit coercion rules" if needed. diff --git a/crates/ra_hir/src/ty/infer/expr.rs b/crates/ra_hir/src/ty/infer/expr.rs index 1ac2709f5..ac570075f 100644 --- a/crates/ra_hir/src/ty/infer/expr.rs +++ b/crates/ra_hir/src/ty/infer/expr.rs @@ -6,15 +6,14 @@ use std::sync::Arc; use hir_def::{ builtin_type::Signedness, path::{GenericArg, GenericArgs}, + resolver::resolver_for_expr, }; use hir_expand::name; -use super::{BindingMode, Expectation, InferenceContext, InferenceDiagnostic, TypeMismatch}; use crate::{ db::HirDatabase, expr::{Array, BinaryOp, Expr, ExprId, Literal, Statement, UnaryOp}, generics::{GenericParams, HasGenericParams}, - resolve::resolver_for_expr, ty::{ autoderef, method_resolution, op, CallableDef, InferTy, IntTy, Mutability, Namespace, Obligation, ProjectionPredicate, ProjectionTy, Substs, TraitRef, Ty, TypeCtor, TypeWalk, @@ -23,6 +22,8 @@ use crate::{ Adt, Name, }; +use super::{BindingMode, Expectation, InferenceContext, InferenceDiagnostic, TypeMismatch}; + impl<'a, D: HirDatabase> InferenceContext<'a, D> { pub(super) fn infer_expr(&mut self, tgt_expr: ExprId, expected: &Expectation) -> Ty { let ty = self.infer_expr_inner(tgt_expr, expected); diff --git a/crates/ra_hir/src/ty/infer/path.rs b/crates/ra_hir/src/ty/infer/path.rs index 55a5dbec7..70136e514 100644 --- a/crates/ra_hir/src/ty/infer/path.rs +++ b/crates/ra_hir/src/ty/infer/path.rs @@ -1,16 +1,19 @@ //! Path expression resolution. -use hir_def::path::PathSegment; +use hir_def::{ + path::PathSegment, + resolver::{ResolveValueResult, Resolver, TypeNs, ValueNs}, +}; -use super::{ExprOrPatId, InferenceContext, TraitRef}; use crate::{ db::HirDatabase, generics::HasGenericParams, - resolve::{ResolveValueResult, Resolver, TypeNs, ValueNs}, ty::{method_resolution, Namespace, Substs, Ty, TypableDef, TypeWalk}, AssocItem, Container, Function, Name, Path, }; +use super::{ExprOrPatId, InferenceContext, TraitRef}; + impl<'a, D: HirDatabase> InferenceContext<'a, D> { pub(super) fn infer_path( &mut self, diff --git a/crates/ra_hir/src/ty/lower.rs b/crates/ra_hir/src/ty/lower.rs index e477b2439..c6ad0811b 100644 --- a/crates/ra_hir/src/ty/lower.rs +++ b/crates/ra_hir/src/ty/lower.rs @@ -11,6 +11,7 @@ use std::sync::Arc; use hir_def::{ builtin_type::{BuiltinFloat, BuiltinInt, BuiltinType}, path::{GenericArg, PathSegment}, + resolver::{HasResolver, Resolver, TypeNs}, type_ref::{TypeBound, TypeRef}, GenericDefId, }; @@ -23,7 +24,6 @@ use crate::{ db::HirDatabase, generics::HasGenericParams, generics::{GenericDef, WherePredicate}, - resolve::{HasResolver, Resolver, TypeNs}, ty::{ primitive::{FloatTy, IntTy, Uncertain}, Adt, diff --git a/crates/ra_hir/src/ty/method_resolution.rs b/crates/ra_hir/src/ty/method_resolution.rs index 5ad72ef9f..64adb814d 100644 --- a/crates/ra_hir/src/ty/method_resolution.rs +++ b/crates/ra_hir/src/ty/method_resolution.rs @@ -5,11 +5,11 @@ use std::sync::Arc; use arrayvec::ArrayVec; +use hir_def::resolver::Resolver; use rustc_hash::FxHashMap; use crate::{ db::HirDatabase, - resolve::Resolver, ty::primitive::{FloatBitness, Uncertain}, ty::{Ty, TypeCtor}, AssocItem, Crate, Function, ImplBlock, Module, Mutability, Name, Trait, -- cgit v1.2.3