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::{
|
2022-10-23 08:12:05 +00:00
|
|
|
db::DefDatabase,
|
|
|
|
expr::ExprId,
|
|
|
|
layout::{Layout, LayoutError, TargetDataLayout},
|
|
|
|
AdtId, BlockId, ConstId, ConstParamId, DefWithBodyId, EnumVariantId, FunctionId, GenericDefId,
|
|
|
|
ImplId, LifetimeParamId, LocalFieldId, TypeOrConstParamId, VariantId,
|
2019-11-27 14:46:02 +00:00
|
|
|
};
|
2021-01-15 00:11:07 +00:00
|
|
|
use la_arena::ArenaMap;
|
2022-12-04 19:12:11 +00:00
|
|
|
use smallvec::SmallVec;
|
2019-11-27 14:46:02 +00:00
|
|
|
|
2021-04-09 12:15:26 +00:00
|
|
|
use crate::{
|
|
|
|
chalk_db,
|
2023-02-03 11:16:25 +00:00
|
|
|
consteval::ConstEvalError,
|
2022-04-02 13:32:40 +00:00
|
|
|
method_resolution::{InherentImpls, TraitImpls, TyFingerprint},
|
2023-02-26 12:34:41 +00:00
|
|
|
mir::{BorrowckResult, MirBody, MirLowerError},
|
2023-02-03 11:16:25 +00:00
|
|
|
Binders, CallableDefId, Const, FnDefId, GenericArg, ImplTraitId, InferenceResult, Interner,
|
|
|
|
PolyFnSig, QuantifiedWhereClause, ReturnTypeImplTraits, Substitution, TraitRef, Ty, TyDefId,
|
|
|
|
ValueTyDefId,
|
2021-04-09 12:15:26 +00:00
|
|
|
};
|
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
|
|
|
|
2023-02-03 11:16:25 +00:00
|
|
|
#[salsa::invoke(crate::mir::mir_body_query)]
|
|
|
|
#[salsa::cycle(crate::mir::mir_body_recover)]
|
|
|
|
fn mir_body(&self, def: DefWithBodyId) -> Result<Arc<MirBody>, MirLowerError>;
|
|
|
|
|
2023-02-26 12:34:41 +00:00
|
|
|
#[salsa::invoke(crate::mir::borrowck_query)]
|
|
|
|
fn borrowck(&self, def: DefWithBodyId) -> Result<Arc<BorrowckResult>, MirLowerError>;
|
|
|
|
|
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;
|
|
|
|
|
2023-02-03 11:16:25 +00:00
|
|
|
#[salsa::invoke(crate::consteval::const_eval_query)]
|
2022-03-20 13:45:28 +00:00
|
|
|
#[salsa::cycle(crate::consteval::const_eval_recover)]
|
2023-03-20 22:50:30 +00:00
|
|
|
fn const_eval(&self, def: ConstId, subst: Substitution) -> Result<Const, ConstEvalError>;
|
2022-03-20 13:45:28 +00:00
|
|
|
|
2023-02-03 11:16:25 +00:00
|
|
|
#[salsa::invoke(crate::consteval::const_eval_discriminant_variant)]
|
|
|
|
#[salsa::cycle(crate::consteval::const_eval_discriminant_recover)]
|
|
|
|
fn const_eval_discriminant(&self, def: EnumVariantId) -> Result<i128, ConstEvalError>;
|
2022-08-06 16:50:21 +00:00
|
|
|
|
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
|
|
|
|
2022-10-23 08:12:05 +00:00
|
|
|
#[salsa::invoke(crate::layout::layout_of_adt_query)]
|
|
|
|
#[salsa::cycle(crate::layout::layout_of_adt_recover)]
|
|
|
|
fn layout_of_adt(&self, def: AdtId, subst: Substitution) -> Result<Layout, LayoutError>;
|
|
|
|
|
2022-12-21 14:11:24 +00:00
|
|
|
#[salsa::invoke(crate::layout::target_data_layout_query)]
|
2023-01-16 19:13:27 +00:00
|
|
|
fn target_data_layout(&self, krate: CrateId) -> Option<Arc<TargetDataLayout>>;
|
2022-10-23 08:12:05 +00:00
|
|
|
|
2022-07-21 08:17:57 +00:00
|
|
|
#[salsa::invoke(crate::lower::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,
|
2022-02-03 11:43:15 +00:00
|
|
|
def: GenericDefId,
|
2021-12-29 13:35:59 +00:00
|
|
|
param_id: TypeOrConstParamId,
|
2021-10-04 15:39:55 +00:00
|
|
|
assoc_name: Option<Name>,
|
2021-03-21 16:40:14 +00:00
|
|
|
) -> 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
|
|
|
|
2023-03-08 17:28:52 +00:00
|
|
|
#[salsa::invoke(crate::lower::trait_environment_for_body_query)]
|
|
|
|
#[salsa::transparent]
|
|
|
|
fn trait_environment_for_body(&self, def: DefWithBodyId) -> Arc<crate::TraitEnvironment>;
|
|
|
|
|
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)]
|
2022-03-09 18:50:24 +00:00
|
|
|
fn generic_defaults(&self, def: GenericDefId) -> Arc<[Binders<GenericArg>]>;
|
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
|
|
|
|
2021-11-09 10:13:42 +00:00
|
|
|
#[salsa::invoke(InherentImpls::inherent_impls_in_block_query)]
|
|
|
|
fn inherent_impls_in_block(&self, block: BlockId) -> Option<Arc<InherentImpls>>;
|
|
|
|
|
2022-04-02 13:32:40 +00:00
|
|
|
/// Collects all crates in the dependency graph that have impls for the
|
2022-12-04 19:12:11 +00:00
|
|
|
/// given fingerprint. This is only used for primitive types and types
|
|
|
|
/// annotated with `rustc_has_incoherent_inherent_impls`; for other types
|
|
|
|
/// we just look at the crate where the type is defined.
|
|
|
|
#[salsa::invoke(crate::method_resolution::incoherent_inherent_impl_crates)]
|
|
|
|
fn incoherent_inherent_impl_crates(
|
|
|
|
&self,
|
|
|
|
krate: CrateId,
|
|
|
|
fp: TyFingerprint,
|
|
|
|
) -> SmallVec<[CrateId; 2]>;
|
2022-04-02 13:32:40 +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-12-29 13:35:59 +00:00
|
|
|
fn intern_type_or_const_param_id(
|
|
|
|
&self,
|
|
|
|
param_id: TypeOrConstParamId,
|
|
|
|
) -> InternedTypeOrConstParamId;
|
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-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;
|
2022-09-06 08:48:06 +00:00
|
|
|
#[salsa::interned]
|
|
|
|
fn intern_generator(&self, id: (DefWithBodyId, ExprId)) -> InternedGeneratorId;
|
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
|
|
|
|
2022-09-12 12:56:46 +00:00
|
|
|
#[salsa::invoke(crate::traits::normalize_projection_query)]
|
|
|
|
#[salsa::transparent]
|
|
|
|
fn normalize_projection(
|
|
|
|
&self,
|
|
|
|
projection: crate::ProjectionTy,
|
|
|
|
env: Arc<crate::TraitEnvironment>,
|
2022-09-12 16:52:16 +00:00
|
|
|
) -> Ty;
|
2022-09-12 12:56:46 +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(),
|
2021-11-10 21:02:50 +00:00
|
|
|
DefWithBodyId::StaticId(it) => db.static_data(it).name.clone().to_string(),
|
2020-03-06 16:23:08 +00:00
|
|
|
DefWithBodyId::ConstId(it) => {
|
|
|
|
db.const_data(it).name.clone().unwrap_or_else(Name::missing).to_string()
|
|
|
|
}
|
2022-08-06 16:50:21 +00:00
|
|
|
DefWithBodyId::VariantId(it) => {
|
2022-09-20 15:29:35 +00:00
|
|
|
db.enum_data(it.parent).variants[it.local_id].name.to_string()
|
2022-08-06 16:50:21 +00:00
|
|
|
}
|
2020-03-06 16:23:08 +00:00
|
|
|
});
|
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-12-29 13:35:59 +00:00
|
|
|
pub struct InternedTypeOrConstParamId(salsa::InternId);
|
|
|
|
impl_intern_key!(InternedTypeOrConstParamId);
|
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
|
|
|
|
2022-09-06 08:48:06 +00:00
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
|
|
|
pub struct InternedGeneratorId(salsa::InternId);
|
|
|
|
impl_intern_key!(InternedGeneratorId);
|
|
|
|
|
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);
|