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