Skip to content

Commit 17680f6

Browse files
committed
More decoupling
1 parent d569869 commit 17680f6

File tree

4 files changed

+27
-33
lines changed

4 files changed

+27
-33
lines changed

crates/ra_hir/src/code_model.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -618,7 +618,7 @@ impl Function {
618618
}
619619

620620
pub fn infer(self, db: &impl HirDatabase) -> Arc<InferenceResult> {
621-
db.infer(self.into())
621+
db.infer(self.id.into())
622622
}
623623

624624
/// The containing impl block, if this is a method.
@@ -672,7 +672,7 @@ impl Const {
672672
}
673673

674674
pub fn infer(self, db: &impl HirDatabase) -> Arc<InferenceResult> {
675-
db.infer(self.into())
675+
db.infer(self.id.into())
676676
}
677677

678678
/// The containing impl block, if this is a type alias.
@@ -715,7 +715,7 @@ impl Static {
715715
}
716716

717717
pub fn infer(self, db: &impl HirDatabase) -> Arc<InferenceResult> {
718-
db.infer(self.into())
718+
db.infer(self.id.into())
719719
}
720720
}
721721

@@ -908,9 +908,9 @@ impl Local {
908908
}
909909

910910
pub fn ty(self, db: &impl HirDatabase) -> Type {
911-
let infer = db.infer(self.parent);
912-
let ty = infer[self.pat_id].clone();
913911
let def = DefWithBodyId::from(self.parent);
912+
let infer = db.infer(def);
913+
let ty = infer[self.pat_id].clone();
914914
let resolver = def.resolver(db);
915915
let krate = def.module(db).krate;
916916
let environment = TraitEnvironment::lower(db, &resolver);

crates/ra_hir/src/db.rs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,15 @@
22
33
use std::sync::Arc;
44

5-
use hir_def::{GenericDefId, ImplId, LocalStructFieldId, TraitId, VariantId};
5+
use hir_def::{DefWithBodyId, GenericDefId, ImplId, LocalStructFieldId, TraitId, VariantId};
66
use ra_arena::map::ArenaMap;
77
use ra_db::{salsa, CrateId};
88

9-
use crate::{
10-
ty::{
11-
method_resolution::CrateImplBlocks,
12-
traits::{AssocTyValue, Impl},
13-
CallableDef, FnSig, GenericPredicate, InferenceResult, Substs, Ty, TyDefId, TypeCtor,
14-
ValueTyDefId,
15-
},
16-
DefWithBody,
9+
use crate::ty::{
10+
method_resolution::CrateImplBlocks,
11+
traits::{AssocTyValue, Impl},
12+
CallableDef, FnSig, GenericPredicate, InferenceResult, Substs, Ty, TyDefId, TypeCtor,
13+
ValueTyDefId,
1714
};
1815

1916
pub use hir_def::db::{
@@ -32,7 +29,7 @@ pub use hir_expand::db::{
3229
#[salsa::requires(salsa::Database)]
3330
pub trait HirDatabase: DefDatabase {
3431
#[salsa::invoke(crate::ty::infer_query)]
35-
fn infer(&self, def: DefWithBody) -> Arc<InferenceResult>;
32+
fn infer(&self, def: DefWithBodyId) -> Arc<InferenceResult>;
3633

3734
#[salsa::invoke(crate::ty::ty_query)]
3835
fn ty(&self, def: TyDefId) -> Ty;

crates/ra_hir/src/source_binder.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ impl SourceAnalyzer {
168168
resolver,
169169
body_owner: Some(def),
170170
body_source_map: Some(source_map),
171-
infer: Some(db.infer(def)),
171+
infer: Some(db.infer(def.into())),
172172
scopes: Some(scopes),
173173
file_id: node.file_id,
174174
}
@@ -297,13 +297,13 @@ impl SourceAnalyzer {
297297
if let Some(path_expr) = path.syntax().parent().and_then(ast::PathExpr::cast) {
298298
let expr_id = self.expr_id(&path_expr.into())?;
299299
if let Some(assoc) = self.infer.as_ref()?.assoc_resolutions_for_expr(expr_id) {
300-
return Some(PathResolution::AssocItem(assoc));
300+
return Some(PathResolution::AssocItem(assoc.into()));
301301
}
302302
}
303303
if let Some(path_pat) = path.syntax().parent().and_then(ast::PathPat::cast) {
304304
let pat_id = self.pat_id(&path_pat.into())?;
305305
if let Some(assoc) = self.infer.as_ref()?.assoc_resolutions_for_pat(pat_id) {
306-
return Some(PathResolution::AssocItem(assoc));
306+
return Some(PathResolution::AssocItem(assoc.into()));
307307
}
308308
}
309309
// This must be a normal source file rather than macro file.

crates/ra_hir/src/ty/infer.rs

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,7 @@ use super::{
4141
ApplicationTy, InEnvironment, ProjectionTy, Substs, TraitEnvironment, TraitRef, Ty, TypeCtor,
4242
TypeWalk, Uncertain,
4343
};
44-
use crate::{
45-
db::HirDatabase, ty::infer::diagnostics::InferenceDiagnostic, AssocItem, DefWithBody,
46-
VariantDef,
47-
};
44+
use crate::{db::HirDatabase, ty::infer::diagnostics::InferenceDiagnostic, VariantDef};
4845

4946
macro_rules! ty_app {
5047
($ctor:pat, $param:pat) => {
@@ -62,15 +59,15 @@ mod pat;
6259
mod coerce;
6360

6461
/// The entry point of type inference.
65-
pub fn infer_query(db: &impl HirDatabase, def: DefWithBody) -> Arc<InferenceResult> {
62+
pub fn infer_query(db: &impl HirDatabase, def: DefWithBodyId) -> Arc<InferenceResult> {
6663
let _p = profile("infer_query");
67-
let resolver = DefWithBodyId::from(def).resolver(db);
64+
let resolver = def.resolver(db);
6865
let mut ctx = InferenceContext::new(db, def, resolver);
6966

70-
match &def {
71-
DefWithBody::Const(c) => ctx.collect_const(&db.const_data(c.id)),
72-
DefWithBody::Function(f) => ctx.collect_fn(&db.function_data(f.id)),
73-
DefWithBody::Static(s) => ctx.collect_const(&db.static_data(s.id)),
67+
match def {
68+
DefWithBodyId::ConstId(c) => ctx.collect_const(&db.const_data(c)),
69+
DefWithBodyId::FunctionId(f) => ctx.collect_fn(&db.function_data(f)),
70+
DefWithBodyId::StaticId(s) => ctx.collect_const(&db.static_data(s)),
7471
}
7572

7673
ctx.infer_body();
@@ -129,7 +126,7 @@ pub struct InferenceResult {
129126
/// For each struct literal, records the variant it resolves to.
130127
variant_resolutions: FxHashMap<ExprOrPatId, VariantDef>,
131128
/// For each associated item record what it resolves to
132-
assoc_resolutions: FxHashMap<ExprOrPatId, AssocItem>,
129+
assoc_resolutions: FxHashMap<ExprOrPatId, AssocItemId>,
133130
diagnostics: Vec<InferenceDiagnostic>,
134131
pub(super) type_of_expr: ArenaMap<ExprId, Ty>,
135132
pub(super) type_of_pat: ArenaMap<PatId, Ty>,
@@ -152,10 +149,10 @@ impl InferenceResult {
152149
pub fn variant_resolution_for_pat(&self, id: PatId) -> Option<VariantDef> {
153150
self.variant_resolutions.get(&id.into()).copied()
154151
}
155-
pub fn assoc_resolutions_for_expr(&self, id: ExprId) -> Option<AssocItem> {
152+
pub fn assoc_resolutions_for_expr(&self, id: ExprId) -> Option<AssocItemId> {
156153
self.assoc_resolutions.get(&id.into()).copied()
157154
}
158-
pub fn assoc_resolutions_for_pat(&self, id: PatId) -> Option<AssocItem> {
155+
pub fn assoc_resolutions_for_pat(&self, id: PatId) -> Option<AssocItemId> {
159156
self.assoc_resolutions.get(&id.into()).copied()
160157
}
161158
pub fn type_mismatch_for_expr(&self, expr: ExprId) -> Option<&TypeMismatch> {
@@ -191,7 +188,7 @@ impl Index<PatId> for InferenceResult {
191188
#[derive(Clone, Debug)]
192189
struct InferenceContext<'a, D: HirDatabase> {
193190
db: &'a D,
194-
owner: DefWithBody,
191+
owner: DefWithBodyId,
195192
body: Arc<Body>,
196193
resolver: Resolver,
197194
var_unification_table: InPlaceUnificationTable<TypeVarId>,
@@ -209,7 +206,7 @@ struct InferenceContext<'a, D: HirDatabase> {
209206
}
210207

211208
impl<'a, D: HirDatabase> InferenceContext<'a, D> {
212-
fn new(db: &'a D, owner: DefWithBody, resolver: Resolver) -> Self {
209+
fn new(db: &'a D, owner: DefWithBodyId, resolver: Resolver) -> Self {
213210
InferenceContext {
214211
result: InferenceResult::default(),
215212
var_unification_table: InPlaceUnificationTable::new(),

0 commit comments

Comments
 (0)