2021-04-09 12:39:07 +00:00
|
|
|
//! The home of `HirDatabase`, which is the Salsa database containing all the
|
|
|
|
//! type inference-related queries.
|
2019-11-27 14:46:02 +00:00
|
|
|
|
|
|
|
use std::sync::Arc;
|
|
|
|
|
2020-08-13 14:25:38 +00:00
|
|
|
use base_db::{impl_intern_key, salsa, CrateId, Upcast};
|
2019-11-27 14:46:02 +00:00
|
|
|
use hir_def::{
|
2021-06-13 11:00:34 +00:00
|
|
|
db::DefDatabase, expr::ExprId, BlockId, ConstParamId, DefWithBodyId, FunctionId, GenericDefId,
|
|
|
|
ImplId, LifetimeParamId, LocalFieldId, TypeParamId, VariantId,
|
2019-11-27 14:46:02 +00:00
|
|
|
};
|
2021-01-15 00:11:07 +00:00
|
|
|
use la_arena::ArenaMap;
|
2019-11-27 14:46:02 +00:00
|
|
|
|
2021-04-09 12:15:26 +00:00
|
|
|
use crate::{
|
|
|
|
chalk_db,
|
|
|
|
method_resolution::{InherentImpls, TraitImpls},
|
|
|
|
Binders, CallableDefId, FnDefId, ImplTraitId, InferenceResult, Interner, PolyFnSig,
|
|
|
|
QuantifiedWhereClause, ReturnTypeImplTraits, TraitRef, Ty, TyDefId, ValueTyDefId,
|
|
|
|
};
|
2020-03-06 16:23:08 +00:00
|
|
|
use hir_expand::name::Name;
|
2019-11-27 14:46:02 +00:00
|
|
|
|
|
|
|
#[salsa::query_group(HirDatabaseStorage)]
|
2020-03-13 15:05:46 +00:00
|
|
|
pub trait HirDatabase: DefDatabase + Upcast<dyn DefDatabase> {
|
2020-03-06 23:11:52 +00:00
|
|
|
#[salsa::invoke(infer_wait)]
|
2020-03-25 17:41:46 +00:00
|
|
|
#[salsa::transparent]
|
2019-11-27 14:46:02 +00:00
|
|
|
fn infer(&self, def: DefWithBodyId) -> Arc<InferenceResult>;
|
|
|
|
|
2020-03-06 23:11:52 +00:00
|
|
|
#[salsa::invoke(crate::infer::infer_query)]
|
|
|
|
fn infer_query(&self, def: DefWithBodyId) -> Arc<InferenceResult>;
|
2020-01-03 16:02:08 +00:00
|
|
|
|
2019-11-27 14:46:02 +00:00
|
|
|
#[salsa::invoke(crate::lower::ty_query)]
|
2019-11-30 11:48:51 +00:00
|
|
|
#[salsa::cycle(crate::lower::ty_recover)]
|
2020-01-25 22:38:33 +00:00
|
|
|
fn ty(&self, def: TyDefId) -> Binders<Ty>;
|
2019-11-27 14:46:02 +00:00
|
|
|
|
|
|
|
#[salsa::invoke(crate::lower::value_ty_query)]
|
2020-01-25 22:38:33 +00:00
|
|
|
fn value_ty(&self, def: ValueTyDefId) -> Binders<Ty>;
|
2019-11-27 14:46:02 +00:00
|
|
|
|
2019-11-30 11:35:37 +00:00
|
|
|
#[salsa::invoke(crate::lower::impl_self_ty_query)]
|
|
|
|
#[salsa::cycle(crate::lower::impl_self_ty_recover)]
|
2020-01-25 22:38:33 +00:00
|
|
|
fn impl_self_ty(&self, def: ImplId) -> Binders<Ty>;
|
2019-11-30 11:35:37 +00:00
|
|
|
|
2021-01-01 09:06:42 +00:00
|
|
|
#[salsa::invoke(crate::lower::const_param_ty_query)]
|
|
|
|
fn const_param_ty(&self, def: ConstParamId) -> Ty;
|
|
|
|
|
2019-11-30 11:35:37 +00:00
|
|
|
#[salsa::invoke(crate::lower::impl_trait_query)]
|
2020-01-31 15:52:43 +00:00
|
|
|
fn impl_trait(&self, def: ImplId) -> Option<Binders<TraitRef>>;
|
2019-11-27 19:12:09 +00:00
|
|
|
|
2019-11-27 14:46:02 +00:00
|
|
|
#[salsa::invoke(crate::lower::field_types_query)]
|
2020-04-25 12:23:34 +00:00
|
|
|
fn field_types(&self, var: VariantId) -> Arc<ArenaMap<LocalFieldId, Binders<Ty>>>;
|
2019-11-27 14:46:02 +00:00
|
|
|
|
|
|
|
#[salsa::invoke(crate::callable_item_sig)]
|
2020-07-16 11:15:00 +00:00
|
|
|
fn callable_item_signature(&self, def: CallableDefId) -> PolyFnSig;
|
2019-11-27 14:46:02 +00:00
|
|
|
|
2020-03-04 22:00:44 +00:00
|
|
|
#[salsa::invoke(crate::lower::return_type_impl_traits)]
|
|
|
|
fn return_type_impl_traits(
|
|
|
|
&self,
|
|
|
|
def: FunctionId,
|
|
|
|
) -> Option<Arc<Binders<ReturnTypeImplTraits>>>;
|
|
|
|
|
2019-11-27 14:46:02 +00:00
|
|
|
#[salsa::invoke(crate::lower::generic_predicates_for_param_query)]
|
2019-11-30 11:39:21 +00:00
|
|
|
#[salsa::cycle(crate::lower::generic_predicates_for_param_recover)]
|
2021-03-21 16:40:14 +00:00
|
|
|
fn generic_predicates_for_param(
|
|
|
|
&self,
|
|
|
|
param_id: TypeParamId,
|
|
|
|
) -> Arc<[Binders<QuantifiedWhereClause>]>;
|
2019-11-27 14:46:02 +00:00
|
|
|
|
|
|
|
#[salsa::invoke(crate::lower::generic_predicates_query)]
|
2021-03-21 16:40:14 +00:00
|
|
|
fn generic_predicates(&self, def: GenericDefId) -> Arc<[Binders<QuantifiedWhereClause>]>;
|
2019-11-27 14:46:02 +00:00
|
|
|
|
2021-03-13 19:38:11 +00:00
|
|
|
#[salsa::invoke(crate::lower::trait_environment_query)]
|
|
|
|
fn trait_environment(&self, def: GenericDefId) -> Arc<crate::TraitEnvironment>;
|
|
|
|
|
2019-11-27 14:46:02 +00:00
|
|
|
#[salsa::invoke(crate::lower::generic_defaults_query)]
|
2021-04-29 18:00:43 +00:00
|
|
|
#[salsa::cycle(crate::lower::generic_defaults_recover)]
|
2020-06-26 14:36:59 +00:00
|
|
|
fn generic_defaults(&self, def: GenericDefId) -> Arc<[Binders<Ty>]>;
|
2019-11-27 14:46:02 +00:00
|
|
|
|
2020-07-01 15:15:20 +00:00
|
|
|
#[salsa::invoke(InherentImpls::inherent_impls_in_crate_query)]
|
|
|
|
fn inherent_impls_in_crate(&self, krate: CrateId) -> Arc<InherentImpls>;
|
2019-11-27 14:46:02 +00:00
|
|
|
|
2020-07-01 15:15:20 +00:00
|
|
|
#[salsa::invoke(TraitImpls::trait_impls_in_crate_query)]
|
|
|
|
fn trait_impls_in_crate(&self, krate: CrateId) -> Arc<TraitImpls>;
|
|
|
|
|
2021-06-13 11:00:34 +00:00
|
|
|
#[salsa::invoke(TraitImpls::trait_impls_in_block_query)]
|
|
|
|
fn trait_impls_in_block(&self, krate: BlockId) -> Option<Arc<TraitImpls>>;
|
|
|
|
|
2020-07-01 15:15:20 +00:00
|
|
|
#[salsa::invoke(TraitImpls::trait_impls_in_deps_query)]
|
|
|
|
fn trait_impls_in_deps(&self, krate: CrateId) -> Arc<TraitImpls>;
|
2019-11-27 14:46:02 +00:00
|
|
|
|
|
|
|
// Interned IDs for Chalk integration
|
|
|
|
#[salsa::interned]
|
2020-07-16 11:15:00 +00:00
|
|
|
fn intern_callable_def(&self, callable_def: CallableDefId) -> InternedCallableDefId;
|
2020-05-22 16:15:53 +00:00
|
|
|
#[salsa::interned]
|
2021-03-13 18:47:34 +00:00
|
|
|
fn intern_type_param_id(&self, param_id: TypeParamId) -> InternedTypeParamId;
|
2020-01-31 15:52:43 +00:00
|
|
|
#[salsa::interned]
|
2021-04-05 18:46:15 +00:00
|
|
|
fn intern_lifetime_param_id(&self, param_id: LifetimeParamId) -> InternedLifetimeParamId;
|
|
|
|
#[salsa::interned]
|
2021-04-06 09:45:41 +00:00
|
|
|
fn intern_const_param_id(&self, param_id: ConstParamId) -> InternedConstParamId;
|
|
|
|
#[salsa::interned]
|
2021-03-13 19:05:47 +00:00
|
|
|
fn intern_impl_trait_id(&self, id: ImplTraitId) -> InternedOpaqueTyId;
|
2020-03-04 22:00:44 +00:00
|
|
|
#[salsa::interned]
|
2021-03-13 18:27:09 +00:00
|
|
|
fn intern_closure(&self, id: (DefWithBodyId, ExprId)) -> InternedClosureId;
|
2019-11-27 14:46:02 +00:00
|
|
|
|
2021-04-09 12:11:37 +00:00
|
|
|
#[salsa::invoke(chalk_db::associated_ty_data_query)]
|
|
|
|
fn associated_ty_data(&self, id: chalk_db::AssocTypeId) -> Arc<chalk_db::AssociatedTyDatum>;
|
2019-11-27 14:46:02 +00:00
|
|
|
|
2021-04-09 12:11:37 +00:00
|
|
|
#[salsa::invoke(chalk_db::trait_datum_query)]
|
2021-04-09 12:15:26 +00:00
|
|
|
fn trait_datum(&self, krate: CrateId, trait_id: chalk_db::TraitId)
|
|
|
|
-> Arc<chalk_db::TraitDatum>;
|
2019-11-27 14:46:02 +00:00
|
|
|
|
2021-04-09 12:11:37 +00:00
|
|
|
#[salsa::invoke(chalk_db::struct_datum_query)]
|
2021-04-09 12:15:26 +00:00
|
|
|
fn struct_datum(
|
|
|
|
&self,
|
|
|
|
krate: CrateId,
|
|
|
|
struct_id: chalk_db::AdtId,
|
|
|
|
) -> Arc<chalk_db::StructDatum>;
|
2019-11-27 14:46:02 +00:00
|
|
|
|
2021-04-09 12:11:37 +00:00
|
|
|
#[salsa::invoke(chalk_db::impl_datum_query)]
|
|
|
|
fn impl_datum(&self, krate: CrateId, impl_id: chalk_db::ImplId) -> Arc<chalk_db::ImplDatum>;
|
2019-11-27 14:46:02 +00:00
|
|
|
|
2021-04-09 12:11:37 +00:00
|
|
|
#[salsa::invoke(chalk_db::fn_def_datum_query)]
|
|
|
|
fn fn_def_datum(&self, krate: CrateId, fn_def_id: FnDefId) -> Arc<chalk_db::FnDefDatum>;
|
2020-11-20 17:00:34 +00:00
|
|
|
|
2021-04-09 12:11:37 +00:00
|
|
|
#[salsa::invoke(chalk_db::fn_def_variance_query)]
|
2021-04-11 09:20:45 +00:00
|
|
|
fn fn_def_variance(&self, fn_def_id: FnDefId) -> chalk_db::Variances;
|
2020-11-20 17:00:34 +00:00
|
|
|
|
2021-04-09 12:11:37 +00:00
|
|
|
#[salsa::invoke(chalk_db::adt_variance_query)]
|
2021-04-11 09:20:45 +00:00
|
|
|
fn adt_variance(&self, adt_id: chalk_db::AdtId) -> chalk_db::Variances;
|
2020-05-22 16:15:53 +00:00
|
|
|
|
2021-04-09 12:11:37 +00:00
|
|
|
#[salsa::invoke(chalk_db::associated_ty_value_query)]
|
2019-11-27 14:46:02 +00:00
|
|
|
fn associated_ty_value(
|
|
|
|
&self,
|
|
|
|
krate: CrateId,
|
2021-04-09 12:11:37 +00:00
|
|
|
id: chalk_db::AssociatedTyValueId,
|
|
|
|
) -> Arc<chalk_db::AssociatedTyValue>;
|
2019-11-27 14:46:02 +00:00
|
|
|
|
2021-04-14 16:11:17 +00:00
|
|
|
#[salsa::invoke(trait_solve_wait)]
|
|
|
|
#[salsa::transparent]
|
2019-11-27 14:46:02 +00:00
|
|
|
fn trait_solve(
|
|
|
|
&self,
|
|
|
|
krate: CrateId,
|
2021-05-16 13:50:28 +00:00
|
|
|
goal: crate::Canonical<crate::InEnvironment<crate::Goal>>,
|
2021-04-04 18:27:40 +00:00
|
|
|
) -> Option<crate::Solution>;
|
2020-04-18 11:36:35 +00:00
|
|
|
|
2021-04-14 16:11:17 +00:00
|
|
|
#[salsa::invoke(crate::traits::trait_solve_query)]
|
|
|
|
fn trait_solve_query(
|
|
|
|
&self,
|
|
|
|
krate: CrateId,
|
2021-05-16 13:50:28 +00:00
|
|
|
goal: crate::Canonical<crate::InEnvironment<crate::Goal>>,
|
2021-04-14 16:11:17 +00:00
|
|
|
) -> Option<crate::Solution>;
|
|
|
|
|
2021-04-09 12:11:37 +00:00
|
|
|
#[salsa::invoke(chalk_db::program_clauses_for_chalk_env_query)]
|
2020-04-18 11:36:35 +00:00
|
|
|
fn program_clauses_for_chalk_env(
|
|
|
|
&self,
|
|
|
|
krate: CrateId,
|
2021-04-09 12:11:37 +00:00
|
|
|
env: chalk_ir::Environment<Interner>,
|
|
|
|
) -> chalk_ir::ProgramClauses<Interner>;
|
2019-11-27 14:46:02 +00:00
|
|
|
}
|
|
|
|
|
2021-03-05 18:25:24 +00:00
|
|
|
fn infer_wait(db: &dyn HirDatabase, def: DefWithBodyId) -> Arc<InferenceResult> {
|
2020-08-12 14:32:36 +00:00
|
|
|
let _p = profile::span("infer:wait").detail(|| match def {
|
2020-03-06 16:23:08 +00:00
|
|
|
DefWithBodyId::FunctionId(it) => db.function_data(it).name.to_string(),
|
|
|
|
DefWithBodyId::StaticId(it) => {
|
|
|
|
db.static_data(it).name.clone().unwrap_or_else(Name::missing).to_string()
|
|
|
|
}
|
|
|
|
DefWithBodyId::ConstId(it) => {
|
|
|
|
db.const_data(it).name.clone().unwrap_or_else(Name::missing).to_string()
|
|
|
|
}
|
|
|
|
});
|
2020-03-06 23:11:52 +00:00
|
|
|
db.infer_query(def)
|
2020-01-03 16:02:08 +00:00
|
|
|
}
|
|
|
|
|
2021-04-14 16:11:17 +00:00
|
|
|
fn trait_solve_wait(
|
|
|
|
db: &dyn HirDatabase,
|
|
|
|
krate: CrateId,
|
2021-05-16 13:50:28 +00:00
|
|
|
goal: crate::Canonical<crate::InEnvironment<crate::Goal>>,
|
2021-04-14 16:11:17 +00:00
|
|
|
) -> Option<crate::Solution> {
|
|
|
|
let _p = profile::span("trait_solve::wait");
|
|
|
|
db.trait_solve_query(krate, goal)
|
|
|
|
}
|
|
|
|
|
2019-11-27 14:46:02 +00:00
|
|
|
#[test]
|
|
|
|
fn hir_database_is_object_safe() {
|
|
|
|
fn _assert_object_safe(_: &dyn HirDatabase) {}
|
|
|
|
}
|
2020-01-31 15:52:43 +00:00
|
|
|
|
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
2021-03-13 18:47:34 +00:00
|
|
|
pub struct InternedTypeParamId(salsa::InternId);
|
|
|
|
impl_intern_key!(InternedTypeParamId);
|
2020-03-04 22:00:44 +00:00
|
|
|
|
2021-04-05 18:46:15 +00:00
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
|
|
|
pub struct InternedLifetimeParamId(salsa::InternId);
|
|
|
|
impl_intern_key!(InternedLifetimeParamId);
|
|
|
|
|
2021-04-06 09:45:41 +00:00
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
|
|
|
pub struct InternedConstParamId(salsa::InternId);
|
|
|
|
impl_intern_key!(InternedConstParamId);
|
|
|
|
|
2020-03-04 22:00:44 +00:00
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
|
|
|
pub struct InternedOpaqueTyId(salsa::InternId);
|
|
|
|
impl_intern_key!(InternedOpaqueTyId);
|
2020-07-12 13:26:02 +00:00
|
|
|
|
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
2021-03-13 18:27:09 +00:00
|
|
|
pub struct InternedClosureId(salsa::InternId);
|
|
|
|
impl_intern_key!(InternedClosureId);
|
2020-07-16 11:13:17 +00:00
|
|
|
|
|
|
|
/// This exists just for Chalk, because Chalk just has a single `FnDefId` where
|
|
|
|
/// we have different IDs for struct and enum variant constructors.
|
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Ord, PartialOrd)]
|
|
|
|
pub struct InternedCallableDefId(salsa::InternId);
|
|
|
|
impl_intern_key!(InternedCallableDefId);
|