More decoupling

This commit is contained in:
Aleksey Kladov 2019-11-27 16:02:33 +03:00
parent d569869f7a
commit 17680f6060
4 changed files with 27 additions and 33 deletions

View file

@ -618,7 +618,7 @@ impl Function {
} }
pub fn infer(self, db: &impl HirDatabase) -> Arc<InferenceResult> { pub fn infer(self, db: &impl HirDatabase) -> Arc<InferenceResult> {
db.infer(self.into()) db.infer(self.id.into())
} }
/// The containing impl block, if this is a method. /// The containing impl block, if this is a method.
@ -672,7 +672,7 @@ impl Const {
} }
pub fn infer(self, db: &impl HirDatabase) -> Arc<InferenceResult> { pub fn infer(self, db: &impl HirDatabase) -> Arc<InferenceResult> {
db.infer(self.into()) db.infer(self.id.into())
} }
/// The containing impl block, if this is a type alias. /// The containing impl block, if this is a type alias.
@ -715,7 +715,7 @@ impl Static {
} }
pub fn infer(self, db: &impl HirDatabase) -> Arc<InferenceResult> { pub fn infer(self, db: &impl HirDatabase) -> Arc<InferenceResult> {
db.infer(self.into()) db.infer(self.id.into())
} }
} }
@ -908,9 +908,9 @@ impl Local {
} }
pub fn ty(self, db: &impl HirDatabase) -> Type { 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 def = DefWithBodyId::from(self.parent);
let infer = db.infer(def);
let ty = infer[self.pat_id].clone();
let resolver = def.resolver(db); let resolver = def.resolver(db);
let krate = def.module(db).krate; let krate = def.module(db).krate;
let environment = TraitEnvironment::lower(db, &resolver); let environment = TraitEnvironment::lower(db, &resolver);

View file

@ -2,18 +2,15 @@
use std::sync::Arc; 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_arena::map::ArenaMap;
use ra_db::{salsa, CrateId}; use ra_db::{salsa, CrateId};
use crate::{ use crate::ty::{
ty::{
method_resolution::CrateImplBlocks, method_resolution::CrateImplBlocks,
traits::{AssocTyValue, Impl}, traits::{AssocTyValue, Impl},
CallableDef, FnSig, GenericPredicate, InferenceResult, Substs, Ty, TyDefId, TypeCtor, CallableDef, FnSig, GenericPredicate, InferenceResult, Substs, Ty, TyDefId, TypeCtor,
ValueTyDefId, ValueTyDefId,
},
DefWithBody,
}; };
pub use hir_def::db::{ pub use hir_def::db::{
@ -32,7 +29,7 @@ pub use hir_expand::db::{
#[salsa::requires(salsa::Database)] #[salsa::requires(salsa::Database)]
pub trait HirDatabase: DefDatabase { pub trait HirDatabase: DefDatabase {
#[salsa::invoke(crate::ty::infer_query)] #[salsa::invoke(crate::ty::infer_query)]
fn infer(&self, def: DefWithBody) -> Arc<InferenceResult>; fn infer(&self, def: DefWithBodyId) -> Arc<InferenceResult>;
#[salsa::invoke(crate::ty::ty_query)] #[salsa::invoke(crate::ty::ty_query)]
fn ty(&self, def: TyDefId) -> Ty; fn ty(&self, def: TyDefId) -> Ty;

View file

@ -168,7 +168,7 @@ impl SourceAnalyzer {
resolver, resolver,
body_owner: Some(def), body_owner: Some(def),
body_source_map: Some(source_map), body_source_map: Some(source_map),
infer: Some(db.infer(def)), infer: Some(db.infer(def.into())),
scopes: Some(scopes), scopes: Some(scopes),
file_id: node.file_id, file_id: node.file_id,
} }
@ -297,13 +297,13 @@ impl SourceAnalyzer {
if let Some(path_expr) = path.syntax().parent().and_then(ast::PathExpr::cast) { if let Some(path_expr) = path.syntax().parent().and_then(ast::PathExpr::cast) {
let expr_id = self.expr_id(&path_expr.into())?; let expr_id = self.expr_id(&path_expr.into())?;
if let Some(assoc) = self.infer.as_ref()?.assoc_resolutions_for_expr(expr_id) { 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) { if let Some(path_pat) = path.syntax().parent().and_then(ast::PathPat::cast) {
let pat_id = self.pat_id(&path_pat.into())?; let pat_id = self.pat_id(&path_pat.into())?;
if let Some(assoc) = self.infer.as_ref()?.assoc_resolutions_for_pat(pat_id) { 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. // This must be a normal source file rather than macro file.

View file

@ -41,10 +41,7 @@ use super::{
ApplicationTy, InEnvironment, ProjectionTy, Substs, TraitEnvironment, TraitRef, Ty, TypeCtor, ApplicationTy, InEnvironment, ProjectionTy, Substs, TraitEnvironment, TraitRef, Ty, TypeCtor,
TypeWalk, Uncertain, TypeWalk, Uncertain,
}; };
use crate::{ use crate::{db::HirDatabase, ty::infer::diagnostics::InferenceDiagnostic, VariantDef};
db::HirDatabase, ty::infer::diagnostics::InferenceDiagnostic, AssocItem, DefWithBody,
VariantDef,
};
macro_rules! ty_app { macro_rules! ty_app {
($ctor:pat, $param:pat) => { ($ctor:pat, $param:pat) => {
@ -62,15 +59,15 @@ mod pat;
mod coerce; mod coerce;
/// The entry point of type inference. /// The entry point of type inference.
pub fn infer_query(db: &impl HirDatabase, def: DefWithBody) -> Arc<InferenceResult> { pub fn infer_query(db: &impl HirDatabase, def: DefWithBodyId) -> Arc<InferenceResult> {
let _p = profile("infer_query"); 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); let mut ctx = InferenceContext::new(db, def, resolver);
match &def { match def {
DefWithBody::Const(c) => ctx.collect_const(&db.const_data(c.id)), DefWithBodyId::ConstId(c) => ctx.collect_const(&db.const_data(c)),
DefWithBody::Function(f) => ctx.collect_fn(&db.function_data(f.id)), DefWithBodyId::FunctionId(f) => ctx.collect_fn(&db.function_data(f)),
DefWithBody::Static(s) => ctx.collect_const(&db.static_data(s.id)), DefWithBodyId::StaticId(s) => ctx.collect_const(&db.static_data(s)),
} }
ctx.infer_body(); ctx.infer_body();
@ -129,7 +126,7 @@ pub struct InferenceResult {
/// For each struct literal, records the variant it resolves to. /// For each struct literal, records the variant it resolves to.
variant_resolutions: FxHashMap<ExprOrPatId, VariantDef>, variant_resolutions: FxHashMap<ExprOrPatId, VariantDef>,
/// For each associated item record what it resolves to /// For each associated item record what it resolves to
assoc_resolutions: FxHashMap<ExprOrPatId, AssocItem>, assoc_resolutions: FxHashMap<ExprOrPatId, AssocItemId>,
diagnostics: Vec<InferenceDiagnostic>, diagnostics: Vec<InferenceDiagnostic>,
pub(super) type_of_expr: ArenaMap<ExprId, Ty>, pub(super) type_of_expr: ArenaMap<ExprId, Ty>,
pub(super) type_of_pat: ArenaMap<PatId, Ty>, pub(super) type_of_pat: ArenaMap<PatId, Ty>,
@ -152,10 +149,10 @@ impl InferenceResult {
pub fn variant_resolution_for_pat(&self, id: PatId) -> Option<VariantDef> { pub fn variant_resolution_for_pat(&self, id: PatId) -> Option<VariantDef> {
self.variant_resolutions.get(&id.into()).copied() self.variant_resolutions.get(&id.into()).copied()
} }
pub fn assoc_resolutions_for_expr(&self, id: ExprId) -> Option<AssocItem> { pub fn assoc_resolutions_for_expr(&self, id: ExprId) -> Option<AssocItemId> {
self.assoc_resolutions.get(&id.into()).copied() self.assoc_resolutions.get(&id.into()).copied()
} }
pub fn assoc_resolutions_for_pat(&self, id: PatId) -> Option<AssocItem> { pub fn assoc_resolutions_for_pat(&self, id: PatId) -> Option<AssocItemId> {
self.assoc_resolutions.get(&id.into()).copied() self.assoc_resolutions.get(&id.into()).copied()
} }
pub fn type_mismatch_for_expr(&self, expr: ExprId) -> Option<&TypeMismatch> { pub fn type_mismatch_for_expr(&self, expr: ExprId) -> Option<&TypeMismatch> {
@ -191,7 +188,7 @@ impl Index<PatId> for InferenceResult {
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
struct InferenceContext<'a, D: HirDatabase> { struct InferenceContext<'a, D: HirDatabase> {
db: &'a D, db: &'a D,
owner: DefWithBody, owner: DefWithBodyId,
body: Arc<Body>, body: Arc<Body>,
resolver: Resolver, resolver: Resolver,
var_unification_table: InPlaceUnificationTable<TypeVarId>, var_unification_table: InPlaceUnificationTable<TypeVarId>,
@ -209,7 +206,7 @@ struct InferenceContext<'a, D: HirDatabase> {
} }
impl<'a, D: HirDatabase> InferenceContext<'a, D> { 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 { InferenceContext {
result: InferenceResult::default(), result: InferenceResult::default(),
var_unification_table: InPlaceUnificationTable::new(), var_unification_table: InPlaceUnificationTable::new(),