Skip to content

Commit 4946169

Browse files
bors[bot]matklad
andauthored
Merge #2429
2429: Move type inference to a separate crate r=matklad a=matklad Co-authored-by: Aleksey Kladov <[email protected]>
2 parents 2798bee + 47ec2ce commit 4946169

37 files changed

+1629
-1582
lines changed

Cargo.lock

Lines changed: 1 addition & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/ra_hir/Cargo.toml

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,31 +8,11 @@ authors = ["rust-analyzer developers"]
88
doctest = false
99

1010
[dependencies]
11-
arrayvec = "0.5.1"
1211
log = "0.4.5"
1312
rustc-hash = "1.0"
14-
parking_lot = "0.10.0"
15-
ena = "0.13"
16-
once_cell = "1.0.1"
1713

1814
ra_syntax = { path = "../ra_syntax" }
19-
ra_arena = { path = "../ra_arena" }
20-
ra_cfg = { path = "../ra_cfg" }
2115
ra_db = { path = "../ra_db" }
22-
mbe = { path = "../ra_mbe", package = "ra_mbe" }
23-
tt = { path = "../ra_tt", package = "ra_tt" }
2416
hir_expand = { path = "../ra_hir_expand", package = "ra_hir_expand" }
2517
hir_def = { path = "../ra_hir_def", package = "ra_hir_def" }
2618
hir_ty = { path = "../ra_hir_ty", package = "ra_hir_ty" }
27-
test_utils = { path = "../test_utils" }
28-
ra_prof = { path = "../ra_prof" }
29-
30-
# https://github.com/rust-lang/chalk/pull/294
31-
chalk-solve = { git = "https://github.com/jackh726/chalk.git", rev = "095cd38a4f16337913bba487f2055b9ca0179f30" }
32-
chalk-rust-ir = { git = "https://github.com/jackh726/chalk.git", rev = "095cd38a4f16337913bba487f2055b9ca0179f30" }
33-
chalk-ir = { git = "https://github.com/jackh726/chalk.git", rev = "095cd38a4f16337913bba487f2055b9ca0179f30" }
34-
35-
lalrpop-intern = "0.15.1"
36-
37-
[dev-dependencies]
38-
insta = "0.12.0"

crates/ra_hir/src/code_model.rs

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@ use std::sync::Arc;
66

77
use hir_def::{
88
adt::VariantData,
9+
body::{Body, BodySourceMap},
910
builtin_type::BuiltinType,
1011
docs::Documentation,
12+
expr::{BindingAnnotation, Pat, PatId},
1113
per_ns::PerNs,
1214
resolver::HasResolver,
1315
type_ref::{Mutability, TypeRef},
@@ -20,12 +22,12 @@ use hir_expand::{
2022
name::{self, AsName},
2123
AstId, MacroDefId,
2224
};
25+
use hir_ty::expr::ExprValidator;
2326
use ra_db::{CrateId, Edition, FileId, FilePosition};
2427
use ra_syntax::{ast, AstNode, SyntaxNode};
2528

2629
use crate::{
2730
db::{DefDatabase, HirDatabase},
28-
expr::{BindingAnnotation, Body, BodySourceMap, ExprValidator, Pat, PatId},
2931
ty::display::HirFormatter,
3032
ty::{
3133
self, InEnvironment, InferenceResult, TraitEnvironment, TraitRef, Ty, TyDefId, TypeCtor,
@@ -353,8 +355,8 @@ impl Struct {
353355
.map(|(id, _)| StructField { parent: self.into(), id })
354356
}
355357

356-
pub fn ty(self, db: &impl HirDatabase) -> Ty {
357-
db.ty(self.id.into())
358+
pub fn ty(self, db: &impl HirDatabase) -> Type {
359+
Type::from_def(db, self.id.module(db).krate, self.id)
358360
}
359361

360362
pub fn constructor_ty(self, db: &impl HirDatabase) -> Ty {
@@ -380,8 +382,8 @@ impl Union {
380382
Module { id: self.id.module(db) }
381383
}
382384

383-
pub fn ty(self, db: &impl HirDatabase) -> Ty {
384-
db.ty(self.id.into())
385+
pub fn ty(self, db: &impl HirDatabase) -> Type {
386+
Type::from_def(db, self.id.module(db).krate, self.id)
385387
}
386388

387389
pub fn fields(self, db: &impl HirDatabase) -> Vec<StructField> {
@@ -441,8 +443,8 @@ impl Enum {
441443
.map(|(id, _)| EnumVariant { parent: self, id })
442444
}
443445

444-
pub fn ty(self, db: &impl HirDatabase) -> Ty {
445-
db.ty(self.id.into())
446+
pub fn ty(self, db: &impl HirDatabase) -> Type {
447+
Type::from_def(db, self.id.module(db).krate, self.id)
446448
}
447449
}
448450

@@ -640,7 +642,7 @@ impl Function {
640642
pub fn diagnostics(self, db: &impl HirDatabase, sink: &mut DiagnosticSink) {
641643
let infer = self.infer(db);
642644
infer.add_diagnostics(db, self.id, sink);
643-
let mut validator = ExprValidator::new(self, infer, sink);
645+
let mut validator = ExprValidator::new(self.id, infer, sink);
644646
validator.validate_body(db);
645647
}
646648
}
@@ -946,13 +948,12 @@ impl ImplBlock {
946948
db.impl_data(self.id).target_type.clone()
947949
}
948950

949-
pub fn target_ty(&self, db: &impl HirDatabase) -> Ty {
950-
Ty::from_hir(db, &self.id.resolver(db), &self.target_type(db))
951-
}
952-
953-
pub fn target_trait_ref(&self, db: &impl HirDatabase) -> Option<TraitRef> {
954-
let target_ty = self.target_ty(db);
955-
TraitRef::from_hir(db, &self.id.resolver(db), &self.target_trait(db)?, Some(target_ty))
951+
pub fn target_ty(&self, db: &impl HirDatabase) -> Type {
952+
let impl_data = db.impl_data(self.id);
953+
let resolver = self.id.resolver(db);
954+
let environment = TraitEnvironment::lower(db, &resolver);
955+
let ty = Ty::from_hir(db, &resolver, &impl_data.target_type);
956+
Type { krate: self.id.module(db).krate, ty: InEnvironment { value: ty, environment } }
956957
}
957958

958959
pub fn items(&self, db: &impl DefDatabase) -> Vec<AssocItem> {
@@ -1130,6 +1131,22 @@ impl Type {
11301131
Some(adt.into())
11311132
}
11321133

1134+
// FIXME: provide required accessors such that it becomes implementable from outside.
1135+
pub fn is_equal_for_find_impls(&self, other: &Type) -> bool {
1136+
match (&self.ty.value, &other.ty.value) {
1137+
(Ty::Apply(a_original_ty), Ty::Apply(ty::ApplicationTy { ctor, parameters })) => {
1138+
match ctor {
1139+
TypeCtor::Ref(..) => match parameters.as_single() {
1140+
Ty::Apply(a_ty) => a_original_ty.ctor == a_ty.ctor,
1141+
_ => false,
1142+
},
1143+
_ => a_original_ty.ctor == *ctor,
1144+
}
1145+
}
1146+
_ => false,
1147+
}
1148+
}
1149+
11331150
fn derived(&self, ty: Ty) -> Type {
11341151
Type {
11351152
krate: self.krate,

crates/ra_hir/src/db.rs

Lines changed: 6 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,5 @@
11
//! FIXME: write short doc here
22
3-
use std::sync::Arc;
4-
5-
use hir_def::{DefWithBodyId, GenericDefId, ImplId, LocalStructFieldId, TraitId, VariantId};
6-
use ra_arena::map::ArenaMap;
7-
use ra_db::{salsa, CrateId};
8-
9-
use crate::ty::{
10-
method_resolution::CrateImplBlocks,
11-
traits::{AssocTyValue, Impl},
12-
CallableDef, FnSig, GenericPredicate, InferenceResult, Substs, Ty, TyDefId, TypeCtor,
13-
ValueTyDefId,
14-
};
15-
163
pub use hir_def::db::{
174
BodyQuery, BodyWithSourceMapQuery, ConstDataQuery, CrateDefMapQuery, CrateLangItemsQuery,
185
DefDatabase, DefDatabaseStorage, DocumentationQuery, EnumDataQuery, ExprScopesQuery,
@@ -24,104 +11,12 @@ pub use hir_expand::db::{
2411
AstDatabase, AstDatabaseStorage, AstIdMapQuery, MacroArgQuery, MacroDefQuery, MacroExpandQuery,
2512
ParseMacroQuery,
2613
};
27-
28-
#[salsa::query_group(HirDatabaseStorage)]
29-
#[salsa::requires(salsa::Database)]
30-
pub trait HirDatabase: DefDatabase {
31-
#[salsa::invoke(crate::ty::infer_query)]
32-
fn infer(&self, def: DefWithBodyId) -> Arc<InferenceResult>;
33-
34-
#[salsa::invoke(crate::ty::ty_query)]
35-
fn ty(&self, def: TyDefId) -> Ty;
36-
37-
#[salsa::invoke(crate::ty::value_ty_query)]
38-
fn value_ty(&self, def: ValueTyDefId) -> Ty;
39-
40-
#[salsa::invoke(crate::ty::field_types_query)]
41-
fn field_types(&self, var: VariantId) -> Arc<ArenaMap<LocalStructFieldId, Ty>>;
42-
43-
#[salsa::invoke(crate::ty::callable_item_sig)]
44-
fn callable_item_signature(&self, def: CallableDef) -> FnSig;
45-
46-
#[salsa::invoke(crate::ty::generic_predicates_for_param_query)]
47-
fn generic_predicates_for_param(
48-
&self,
49-
def: GenericDefId,
50-
param_idx: u32,
51-
) -> Arc<[GenericPredicate]>;
52-
53-
#[salsa::invoke(crate::ty::generic_predicates_query)]
54-
fn generic_predicates(&self, def: GenericDefId) -> Arc<[GenericPredicate]>;
55-
56-
#[salsa::invoke(crate::ty::generic_defaults_query)]
57-
fn generic_defaults(&self, def: GenericDefId) -> Substs;
58-
59-
#[salsa::invoke(crate::ty::method_resolution::CrateImplBlocks::impls_in_crate_query)]
60-
fn impls_in_crate(&self, krate: CrateId) -> Arc<CrateImplBlocks>;
61-
62-
#[salsa::invoke(crate::ty::traits::impls_for_trait_query)]
63-
fn impls_for_trait(&self, krate: CrateId, trait_: TraitId) -> Arc<[ImplId]>;
64-
65-
/// This provides the Chalk trait solver instance. Because Chalk always
66-
/// works from a specific crate, this query is keyed on the crate; and
67-
/// because Chalk does its own internal caching, the solver is wrapped in a
68-
/// Mutex and the query does an untracked read internally, to make sure the
69-
/// cached state is thrown away when input facts change.
70-
#[salsa::invoke(crate::ty::traits::trait_solver_query)]
71-
fn trait_solver(&self, krate: CrateId) -> crate::ty::traits::TraitSolver;
72-
73-
// Interned IDs for Chalk integration
74-
#[salsa::interned]
75-
fn intern_type_ctor(&self, type_ctor: TypeCtor) -> crate::ty::TypeCtorId;
76-
#[salsa::interned]
77-
fn intern_chalk_impl(&self, impl_: Impl) -> crate::ty::traits::GlobalImplId;
78-
#[salsa::interned]
79-
fn intern_assoc_ty_value(
80-
&self,
81-
assoc_ty_value: AssocTyValue,
82-
) -> crate::ty::traits::AssocTyValueId;
83-
84-
#[salsa::invoke(crate::ty::traits::chalk::associated_ty_data_query)]
85-
fn associated_ty_data(
86-
&self,
87-
id: chalk_ir::TypeId,
88-
) -> Arc<chalk_rust_ir::AssociatedTyDatum<chalk_ir::family::ChalkIr>>;
89-
90-
#[salsa::invoke(crate::ty::traits::chalk::trait_datum_query)]
91-
fn trait_datum(
92-
&self,
93-
krate: CrateId,
94-
trait_id: chalk_ir::TraitId,
95-
) -> Arc<chalk_rust_ir::TraitDatum<chalk_ir::family::ChalkIr>>;
96-
97-
#[salsa::invoke(crate::ty::traits::chalk::struct_datum_query)]
98-
fn struct_datum(
99-
&self,
100-
krate: CrateId,
101-
struct_id: chalk_ir::StructId,
102-
) -> Arc<chalk_rust_ir::StructDatum<chalk_ir::family::ChalkIr>>;
103-
104-
#[salsa::invoke(crate::ty::traits::chalk::impl_datum_query)]
105-
fn impl_datum(
106-
&self,
107-
krate: CrateId,
108-
impl_id: chalk_ir::ImplId,
109-
) -> Arc<chalk_rust_ir::ImplDatum<chalk_ir::family::ChalkIr>>;
110-
111-
#[salsa::invoke(crate::ty::traits::chalk::associated_ty_value_query)]
112-
fn associated_ty_value(
113-
&self,
114-
krate: CrateId,
115-
id: chalk_rust_ir::AssociatedTyValueId,
116-
) -> Arc<chalk_rust_ir::AssociatedTyValue<chalk_ir::family::ChalkIr>>;
117-
118-
#[salsa::invoke(crate::ty::traits::trait_solve_query)]
119-
fn trait_solve(
120-
&self,
121-
krate: CrateId,
122-
goal: crate::ty::Canonical<crate::ty::InEnvironment<crate::ty::Obligation>>,
123-
) -> Option<crate::ty::traits::Solution>;
124-
}
14+
pub use hir_ty::db::{
15+
AssociatedTyDataQuery, CallableItemSignatureQuery, FieldTypesQuery, GenericDefaultsQuery,
16+
GenericPredicatesQuery, HirDatabase, HirDatabaseStorage, ImplDatumQuery, ImplsForTraitQuery,
17+
ImplsInCrateQuery, InferQuery, StructDatumQuery, TraitDatumQuery, TraitSolveQuery, TyQuery,
18+
ValueTyQuery,
19+
};
12520

12621
#[test]
12722
fn hir_database_is_object_safe() {

0 commit comments

Comments
 (0)