From 17680f6060be1abe8f021538aeff0a95e9c569da Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 27 Nov 2019 16:02:33 +0300 Subject: More decoupling --- crates/ra_hir/src/code_model.rs | 10 +++++----- crates/ra_hir/src/db.rs | 17 +++++++---------- crates/ra_hir/src/source_binder.rs | 6 +++--- crates/ra_hir/src/ty/infer.rs | 27 ++++++++++++--------------- 4 files changed, 27 insertions(+), 33 deletions(-) diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs index 6d71bde92..0e987240a 100644 --- a/crates/ra_hir/src/code_model.rs +++ b/crates/ra_hir/src/code_model.rs @@ -618,7 +618,7 @@ impl Function { } pub fn infer(self, db: &impl HirDatabase) -> Arc { - db.infer(self.into()) + db.infer(self.id.into()) } /// The containing impl block, if this is a method. @@ -672,7 +672,7 @@ impl Const { } pub fn infer(self, db: &impl HirDatabase) -> Arc { - db.infer(self.into()) + db.infer(self.id.into()) } /// The containing impl block, if this is a type alias. @@ -715,7 +715,7 @@ impl Static { } pub fn infer(self, db: &impl HirDatabase) -> Arc { - db.infer(self.into()) + db.infer(self.id.into()) } } @@ -908,9 +908,9 @@ impl Local { } pub fn ty(self, db: &impl HirDatabase) -> Type { - let infer = db.infer(self.parent); - let ty = infer[self.pat_id].clone(); let def = DefWithBodyId::from(self.parent); + let infer = db.infer(def); + let ty = infer[self.pat_id].clone(); let resolver = def.resolver(db); let krate = def.module(db).krate; let environment = TraitEnvironment::lower(db, &resolver); diff --git a/crates/ra_hir/src/db.rs b/crates/ra_hir/src/db.rs index a5bfef91f..e192c8f47 100644 --- a/crates/ra_hir/src/db.rs +++ b/crates/ra_hir/src/db.rs @@ -2,18 +2,15 @@ use std::sync::Arc; -use hir_def::{GenericDefId, ImplId, LocalStructFieldId, TraitId, VariantId}; +use hir_def::{DefWithBodyId, GenericDefId, ImplId, LocalStructFieldId, TraitId, VariantId}; use ra_arena::map::ArenaMap; use ra_db::{salsa, CrateId}; -use crate::{ - ty::{ - method_resolution::CrateImplBlocks, - traits::{AssocTyValue, Impl}, - CallableDef, FnSig, GenericPredicate, InferenceResult, Substs, Ty, TyDefId, TypeCtor, - ValueTyDefId, - }, - DefWithBody, +use crate::ty::{ + method_resolution::CrateImplBlocks, + traits::{AssocTyValue, Impl}, + CallableDef, FnSig, GenericPredicate, InferenceResult, Substs, Ty, TyDefId, TypeCtor, + ValueTyDefId, }; pub use hir_def::db::{ @@ -32,7 +29,7 @@ pub use hir_expand::db::{ #[salsa::requires(salsa::Database)] pub trait HirDatabase: DefDatabase { #[salsa::invoke(crate::ty::infer_query)] - fn infer(&self, def: DefWithBody) -> Arc; + fn infer(&self, def: DefWithBodyId) -> Arc; #[salsa::invoke(crate::ty::ty_query)] fn ty(&self, def: TyDefId) -> Ty; diff --git a/crates/ra_hir/src/source_binder.rs b/crates/ra_hir/src/source_binder.rs index 0de36abd1..c85e38a0d 100644 --- a/crates/ra_hir/src/source_binder.rs +++ b/crates/ra_hir/src/source_binder.rs @@ -168,7 +168,7 @@ impl SourceAnalyzer { resolver, body_owner: Some(def), body_source_map: Some(source_map), - infer: Some(db.infer(def)), + infer: Some(db.infer(def.into())), scopes: Some(scopes), file_id: node.file_id, } @@ -297,13 +297,13 @@ impl SourceAnalyzer { if let Some(path_expr) = path.syntax().parent().and_then(ast::PathExpr::cast) { let expr_id = self.expr_id(&path_expr.into())?; if let Some(assoc) = self.infer.as_ref()?.assoc_resolutions_for_expr(expr_id) { - return Some(PathResolution::AssocItem(assoc)); + return Some(PathResolution::AssocItem(assoc.into())); } } if let Some(path_pat) = path.syntax().parent().and_then(ast::PathPat::cast) { let pat_id = self.pat_id(&path_pat.into())?; if let Some(assoc) = self.infer.as_ref()?.assoc_resolutions_for_pat(pat_id) { - return Some(PathResolution::AssocItem(assoc)); + return Some(PathResolution::AssocItem(assoc.into())); } } // This must be a normal source file rather than macro file. diff --git a/crates/ra_hir/src/ty/infer.rs b/crates/ra_hir/src/ty/infer.rs index 7b6dfd61b..1eca4883d 100644 --- a/crates/ra_hir/src/ty/infer.rs +++ b/crates/ra_hir/src/ty/infer.rs @@ -41,10 +41,7 @@ use super::{ ApplicationTy, InEnvironment, ProjectionTy, Substs, TraitEnvironment, TraitRef, Ty, TypeCtor, TypeWalk, Uncertain, }; -use crate::{ - db::HirDatabase, ty::infer::diagnostics::InferenceDiagnostic, AssocItem, DefWithBody, - VariantDef, -}; +use crate::{db::HirDatabase, ty::infer::diagnostics::InferenceDiagnostic, VariantDef}; macro_rules! ty_app { ($ctor:pat, $param:pat) => { @@ -62,15 +59,15 @@ mod pat; mod coerce; /// The entry point of type inference. -pub fn infer_query(db: &impl HirDatabase, def: DefWithBody) -> Arc { +pub fn infer_query(db: &impl HirDatabase, def: DefWithBodyId) -> Arc { let _p = profile("infer_query"); - let resolver = DefWithBodyId::from(def).resolver(db); + let resolver = def.resolver(db); let mut ctx = InferenceContext::new(db, def, resolver); - match &def { - DefWithBody::Const(c) => ctx.collect_const(&db.const_data(c.id)), - DefWithBody::Function(f) => ctx.collect_fn(&db.function_data(f.id)), - DefWithBody::Static(s) => ctx.collect_const(&db.static_data(s.id)), + match def { + DefWithBodyId::ConstId(c) => ctx.collect_const(&db.const_data(c)), + DefWithBodyId::FunctionId(f) => ctx.collect_fn(&db.function_data(f)), + DefWithBodyId::StaticId(s) => ctx.collect_const(&db.static_data(s)), } ctx.infer_body(); @@ -129,7 +126,7 @@ pub struct InferenceResult { /// For each struct literal, records the variant it resolves to. variant_resolutions: FxHashMap, /// For each associated item record what it resolves to - assoc_resolutions: FxHashMap, + assoc_resolutions: FxHashMap, diagnostics: Vec, pub(super) type_of_expr: ArenaMap, pub(super) type_of_pat: ArenaMap, @@ -152,10 +149,10 @@ impl InferenceResult { pub fn variant_resolution_for_pat(&self, id: PatId) -> Option { self.variant_resolutions.get(&id.into()).copied() } - pub fn assoc_resolutions_for_expr(&self, id: ExprId) -> Option { + pub fn assoc_resolutions_for_expr(&self, id: ExprId) -> Option { self.assoc_resolutions.get(&id.into()).copied() } - pub fn assoc_resolutions_for_pat(&self, id: PatId) -> Option { + pub fn assoc_resolutions_for_pat(&self, id: PatId) -> Option { self.assoc_resolutions.get(&id.into()).copied() } pub fn type_mismatch_for_expr(&self, expr: ExprId) -> Option<&TypeMismatch> { @@ -191,7 +188,7 @@ impl Index for InferenceResult { #[derive(Clone, Debug)] struct InferenceContext<'a, D: HirDatabase> { db: &'a D, - owner: DefWithBody, + owner: DefWithBodyId, body: Arc, resolver: Resolver, var_unification_table: InPlaceUnificationTable, @@ -209,7 +206,7 @@ struct InferenceContext<'a, D: HirDatabase> { } impl<'a, D: HirDatabase> InferenceContext<'a, D> { - fn new(db: &'a D, owner: DefWithBody, resolver: Resolver) -> Self { + fn new(db: &'a D, owner: DefWithBodyId, resolver: Resolver) -> Self { InferenceContext { result: InferenceResult::default(), var_unification_table: InPlaceUnificationTable::new(), -- cgit v1.2.3