mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-12-26 13:03:31 +00:00
More decoupling
This commit is contained in:
parent
d569869f7a
commit
17680f6060
4 changed files with 27 additions and 33 deletions
|
@ -618,7 +618,7 @@ impl Function {
|
|||
}
|
||||
|
||||
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.
|
||||
|
@ -672,7 +672,7 @@ impl Const {
|
|||
}
|
||||
|
||||
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.
|
||||
|
@ -715,7 +715,7 @@ impl Static {
|
|||
}
|
||||
|
||||
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 {
|
||||
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);
|
||||
|
|
|
@ -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<InferenceResult>;
|
||||
fn infer(&self, def: DefWithBodyId) -> Arc<InferenceResult>;
|
||||
|
||||
#[salsa::invoke(crate::ty::ty_query)]
|
||||
fn ty(&self, def: TyDefId) -> Ty;
|
||||
|
|
|
@ -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.
|
||||
|
|
|
@ -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<InferenceResult> {
|
||||
pub fn infer_query(db: &impl HirDatabase, def: DefWithBodyId) -> Arc<InferenceResult> {
|
||||
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<ExprOrPatId, VariantDef>,
|
||||
/// For each associated item record what it resolves to
|
||||
assoc_resolutions: FxHashMap<ExprOrPatId, AssocItem>,
|
||||
assoc_resolutions: FxHashMap<ExprOrPatId, AssocItemId>,
|
||||
diagnostics: Vec<InferenceDiagnostic>,
|
||||
pub(super) type_of_expr: ArenaMap<ExprId, 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> {
|
||||
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()
|
||||
}
|
||||
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()
|
||||
}
|
||||
pub fn type_mismatch_for_expr(&self, expr: ExprId) -> Option<&TypeMismatch> {
|
||||
|
@ -191,7 +188,7 @@ impl Index<PatId> for InferenceResult {
|
|||
#[derive(Clone, Debug)]
|
||||
struct InferenceContext<'a, D: HirDatabase> {
|
||||
db: &'a D,
|
||||
owner: DefWithBody,
|
||||
owner: DefWithBodyId,
|
||||
body: Arc<Body>,
|
||||
resolver: Resolver,
|
||||
var_unification_table: InPlaceUnificationTable<TypeVarId>,
|
||||
|
@ -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(),
|
||||
|
|
Loading…
Reference in a new issue