From 6d64798a2300858c74b1cc0a22f6d3df578288b3 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 21 Nov 2019 15:39:09 +0300 Subject: [PATCH] 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/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 +- crates/ra_hir_def/src/lib.rs | 1 + .../resolve.rs => ra_hir_def/src/resolver.rs} | 81 ++++++++----------- 14 files changed, 63 insertions(+), 77 deletions(-) rename crates/{ra_hir/src/resolve.rs => ra_hir_def/src/resolver.rs} (92%) diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs index 3c891547e5..92860fb598 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 9cdc0c645f..6b703d8b4b 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 c84ceee62b..774fa1d964 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 095d4964f4..76c96bdcfb 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/source_binder.rs b/crates/ra_hir/src/source_binder.rs index 898b823c0b..c42ceabdfc 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 f774921701..5d8518041f 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 c3d65afa68..69b13baefb 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 6d297c268e..0772b9df57 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 1ac2709f59..ac570075f0 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 55a5dbec76..70136e514e 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 e477b2439a..c6ad0811b9 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 5ad72ef9f5..64adb814d3 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, diff --git a/crates/ra_hir_def/src/lib.rs b/crates/ra_hir_def/src/lib.rs index 0af41de879..d579f5c7ea 100644 --- a/crates/ra_hir_def/src/lib.rs +++ b/crates/ra_hir_def/src/lib.rs @@ -19,6 +19,7 @@ pub mod expr; pub mod body; pub mod generics; pub mod traits; +pub mod resolver; #[cfg(test)] mod test_db; diff --git a/crates/ra_hir/src/resolve.rs b/crates/ra_hir_def/src/resolver.rs similarity index 92% rename from crates/ra_hir/src/resolve.rs rename to crates/ra_hir_def/src/resolver.rs index a616f0ea57..840785baa4 100644 --- a/crates/ra_hir/src/resolve.rs +++ b/crates/ra_hir_def/src/resolver.rs @@ -1,7 +1,14 @@ -//! Name resolution. +//! Name resolution façade. use std::sync::Arc; -use hir_def::{ +use hir_expand::{ + name::{self, Name}, + MacroDefId, +}; +use ra_db::CrateId; +use rustc_hash::FxHashSet; + +use crate::{ body::scope::{ExprScopes, ScopeId}, builtin_type::BuiltinType, db::DefDatabase2, @@ -13,15 +20,9 @@ use hir_def::{ 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 { +pub struct Resolver { scopes: Vec, } @@ -54,7 +55,7 @@ pub(crate) enum Scope { } #[derive(Debug, Clone, PartialEq, Eq, Hash)] -pub(crate) enum TypeNs { +pub enum TypeNs { SelfType(ImplId), GenericParam(u32), AdtId(AdtId), @@ -69,13 +70,13 @@ pub(crate) enum TypeNs { } #[derive(Debug, Clone, PartialEq, Eq, Hash)] -pub(crate) enum ResolveValueResult { +pub enum ResolveValueResult { ValueNs(ValueNs), Partial(TypeNs, usize), } #[derive(Debug, Clone, PartialEq, Eq, Hash)] -pub(crate) enum ValueNs { +pub enum ValueNs { LocalBinding(PatId), FunctionId(FunctionId), ConstId(ConstId), @@ -86,11 +87,7 @@ pub(crate) enum ValueNs { impl Resolver { /// Resolve known trait from std, like `std::futures::Future` - pub(crate) fn resolve_known_trait( - &self, - db: &impl DefDatabase2, - path: &Path, - ) -> Option { + pub 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), @@ -99,11 +96,7 @@ impl Resolver { } /// Resolve known struct from std, like `std::boxed::Box` - pub(crate) fn resolve_known_struct( - &self, - db: &impl DefDatabase2, - path: &Path, - ) -> Option { + pub 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), @@ -112,7 +105,7 @@ impl Resolver { } /// Resolve known enum from std, like `std::result::Result` - pub(crate) fn resolve_known_enum(&self, db: &impl DefDatabase2, path: &Path) -> Option { + pub 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), @@ -121,7 +114,7 @@ impl Resolver { } /// pub only for source-binder - pub(crate) fn resolve_module_path(&self, db: &impl DefDatabase2, path: &Path) -> PerNs { + pub 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(), @@ -133,7 +126,7 @@ impl Resolver { module_res } - pub(crate) fn resolve_path_in_type_ns( + pub fn resolve_path_in_type_ns( &self, db: &impl DefDatabase2, path: &Path, @@ -189,7 +182,7 @@ impl Resolver { None } - pub(crate) fn resolve_path_in_type_ns_fully( + pub fn resolve_path_in_type_ns_fully( &self, db: &impl DefDatabase2, path: &Path, @@ -201,7 +194,7 @@ impl Resolver { Some(res) } - pub(crate) fn resolve_path_in_value_ns<'p>( + pub fn resolve_path_in_value_ns<'p>( &self, db: &impl DefDatabase2, path: &'p Path, @@ -301,7 +294,7 @@ impl Resolver { None } - pub(crate) fn resolve_path_in_value_ns_fully( + pub fn resolve_path_in_value_ns_fully( &self, db: &impl DefDatabase2, path: &Path, @@ -312,26 +305,18 @@ impl Resolver { } } - pub(crate) fn resolve_path_as_macro( - &self, - db: &impl DefDatabase2, - path: &Path, - ) -> Option { + pub 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), - ) { + pub 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 { + pub 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 { @@ -353,11 +338,11 @@ impl Resolver { }) } - pub(crate) fn krate(&self) -> Option { + pub fn krate(&self) -> Option { self.module().map(|t| t.0.krate()) } - pub(crate) fn where_predicates_in_scope<'a>( + pub fn where_predicates_in_scope<'a>( &'a self, ) -> impl Iterator + 'a { self.scopes @@ -369,14 +354,14 @@ impl Resolver { .flat_map(|params| params.where_predicates.iter()) } - pub(crate) fn generic_def(&self) -> Option { + pub 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 { + pub fn body_owner(&self) -> Option { self.scopes.iter().find_map(|scope| match scope { Scope::ExprScope(it) => Some(it.owner), _ => None, @@ -425,7 +410,7 @@ impl Resolver { } } -pub(crate) enum ScopeDef { +pub enum ScopeDef { PerNs(PerNs), ImplSelfType(ImplId), AdtSelfType(AdtId), @@ -481,7 +466,7 @@ impl Scope { } // needs arbitrary_self_types to be a method... or maybe move to the def? -pub(crate) fn resolver_for_expr( +pub fn resolver_for_expr( db: &impl DefDatabase2, owner: DefWithBodyId, expr_id: ExprId, @@ -490,7 +475,7 @@ pub(crate) fn resolver_for_expr( resolver_for_scope(db, owner, scopes.scope_for(expr_id)) } -pub(crate) fn resolver_for_scope( +pub fn resolver_for_scope( db: &impl DefDatabase2, owner: DefWithBodyId, scope_id: Option, @@ -504,7 +489,7 @@ pub(crate) fn resolver_for_scope( r } -pub(crate) trait HasResolver { +pub trait HasResolver { /// Builds a resolver for type references inside this def. fn resolver(self, db: &impl DefDatabase2) -> Resolver; } @@ -600,7 +585,7 @@ impl HasResolver for ContainerId { } impl HasResolver for GenericDefId { - fn resolver(self, db: &impl DefDatabase2) -> crate::Resolver { + fn resolver(self, db: &impl DefDatabase2) -> Resolver { match self { GenericDefId::FunctionId(inner) => inner.resolver(db), GenericDefId::AdtId(adt) => adt.resolver(db),