rust-analyzer/crates/hir-ty/src/db.rs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

325 lines
12 KiB
Rust
Raw Normal View History

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
2023-05-02 14:12:22 +00:00
use std::sync;
2019-11-27 14:46:02 +00:00
use base_db::{
impl_intern_key,
2024-10-04 19:48:11 +00:00
ra_salsa::{self, InternValueTrivial},
CrateId, Upcast,
};
2019-11-27 14:46:02 +00:00
use hir_def::{
2024-06-23 13:22:42 +00:00
db::DefDatabase, hir::ExprId, layout::TargetDataLayout, AdtId, BlockId, CallableDefId,
ConstParamId, DefWithBodyId, EnumVariantId, FunctionId, GeneralConstId, GenericDefId, ImplId,
2024-08-06 18:16:24 +00:00
LifetimeParamId, LocalFieldId, StaticId, TraitId, TypeAliasId, TypeOrConstParamId, VariantId,
2019-11-27 14:46:02 +00:00
};
use la_arena::ArenaMap;
use smallvec::SmallVec;
2023-05-02 14:12:22 +00:00
use triomphe::Arc;
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,
dyn_compatibility::DynCompatibilityViolation,
layout::{Layout, LayoutError},
lower::{GenericDefaults, GenericPredicates},
method_resolution::{InherentImpls, TraitImpls, TyFingerprint},
mir::{BorrowckResult, MirBody, MirLowerError},
2024-06-23 13:22:42 +00:00
Binders, ClosureId, Const, FnDefId, ImplTraitId, ImplTraits, InferenceResult, Interner,
PolyFnSig, Substitution, TraitEnvironment, TraitRef, Ty, TyDefId, ValueTyDefId,
2021-04-09 12:15:26 +00:00
};
use hir_expand::name::Name;
2019-11-27 14:46:02 +00:00
2024-10-04 19:48:11 +00:00
#[ra_salsa::query_group(HirDatabaseStorage)]
pub trait HirDatabase: DefDatabase + Upcast<dyn DefDatabase> {
2024-10-04 19:48:11 +00:00
#[ra_salsa::invoke(crate::infer::infer_query)]
fn infer(&self, def: DefWithBodyId) -> Arc<InferenceResult>;
// region:mir
2024-10-04 19:48:11 +00:00
#[ra_salsa::invoke(crate::mir::mir_body_query)]
#[ra_salsa::cycle(crate::mir::mir_body_recover)]
2023-02-03 11:16:25 +00:00
fn mir_body(&self, def: DefWithBodyId) -> Result<Arc<MirBody>, MirLowerError>;
2024-10-04 19:48:11 +00:00
#[ra_salsa::invoke(crate::mir::mir_body_for_closure_query)]
2023-04-06 12:44:38 +00:00
fn mir_body_for_closure(&self, def: ClosureId) -> Result<Arc<MirBody>, MirLowerError>;
2024-10-04 19:48:11 +00:00
#[ra_salsa::invoke(crate::mir::monomorphized_mir_body_query)]
#[ra_salsa::cycle(crate::mir::monomorphized_mir_body_recover)]
2023-05-25 21:15:37 +00:00
fn monomorphized_mir_body(
&self,
def: DefWithBodyId,
subst: Substitution,
2023-11-14 09:08:19 +00:00
env: Arc<TraitEnvironment>,
2023-05-25 21:15:37 +00:00
) -> Result<Arc<MirBody>, MirLowerError>;
2024-10-04 19:48:11 +00:00
#[ra_salsa::invoke(crate::mir::monomorphized_mir_body_for_closure_query)]
2023-05-25 21:15:37 +00:00
fn monomorphized_mir_body_for_closure(
&self,
def: ClosureId,
subst: Substitution,
2023-11-14 09:08:19 +00:00
env: Arc<TraitEnvironment>,
2023-05-25 21:15:37 +00:00
) -> Result<Arc<MirBody>, MirLowerError>;
2024-10-04 19:48:11 +00:00
#[ra_salsa::invoke(crate::mir::borrowck_query)]
#[ra_salsa::lru]
2023-04-06 12:44:38 +00:00
fn borrowck(&self, def: DefWithBodyId) -> Result<Arc<[BorrowckResult]>, MirLowerError>;
2024-10-04 19:48:11 +00:00
#[ra_salsa::invoke(crate::consteval::const_eval_query)]
#[ra_salsa::cycle(crate::consteval::const_eval_recover)]
fn const_eval(
&self,
def: GeneralConstId,
subst: Substitution,
2023-11-14 09:08:19 +00:00
trait_env: Option<Arc<TraitEnvironment>>,
) -> Result<Const, ConstEvalError>;
2023-05-12 14:47:15 +00:00
2024-10-04 19:48:11 +00:00
#[ra_salsa::invoke(crate::consteval::const_eval_static_query)]
#[ra_salsa::cycle(crate::consteval::const_eval_static_recover)]
2023-05-12 14:47:15 +00:00
fn const_eval_static(&self, def: StaticId) -> Result<Const, ConstEvalError>;
2022-03-20 13:45:28 +00:00
2024-10-04 19:48:11 +00:00
#[ra_salsa::invoke(crate::consteval::const_eval_discriminant_variant)]
#[ra_salsa::cycle(crate::consteval::const_eval_discriminant_recover)]
2023-02-03 11:16:25 +00:00
fn const_eval_discriminant(&self, def: EnumVariantId) -> Result<i128, ConstEvalError>;
2022-08-06 16:50:21 +00:00
2024-10-04 19:48:11 +00:00
#[ra_salsa::invoke(crate::method_resolution::lookup_impl_method_query)]
fn lookup_impl_method(
&self,
env: Arc<TraitEnvironment>,
func: FunctionId,
fn_subst: Substitution,
) -> (FunctionId, Substitution);
// endregion:mir
2024-10-04 19:48:11 +00:00
#[ra_salsa::invoke(crate::layout::layout_of_adt_query)]
#[ra_salsa::cycle(crate::layout::layout_of_adt_recover)]
fn layout_of_adt(
&self,
def: AdtId,
subst: Substitution,
env: Arc<TraitEnvironment>,
) -> Result<Arc<Layout>, LayoutError>;
2024-10-04 19:48:11 +00:00
#[ra_salsa::invoke(crate::layout::layout_of_ty_query)]
#[ra_salsa::cycle(crate::layout::layout_of_ty_recover)]
fn layout_of_ty(&self, ty: Ty, env: Arc<TraitEnvironment>) -> Result<Arc<Layout>, LayoutError>;
2024-10-04 19:48:11 +00:00
#[ra_salsa::invoke(crate::layout::target_data_layout_query)]
fn target_data_layout(&self, krate: CrateId) -> Result<Arc<TargetDataLayout>, Arc<str>>;
2024-10-04 19:48:11 +00:00
#[ra_salsa::invoke(crate::dyn_compatibility::dyn_compatibility_of_trait_query)]
fn dyn_compatibility_of_trait(&self, trait_: TraitId) -> Option<DynCompatibilityViolation>;
2024-08-06 18:16:24 +00:00
2024-10-04 19:48:11 +00:00
#[ra_salsa::invoke(crate::lower::ty_query)]
#[ra_salsa::cycle(crate::lower::ty_recover)]
fn ty(&self, def: TyDefId) -> Binders<Ty>;
/// Returns the type of the value of the given constant, or `None` if the `ValueTyDefId` is
2024-01-16 11:09:40 +00:00
/// a `StructId` or `EnumVariantId` with a record constructor.
2024-10-04 19:48:11 +00:00
#[ra_salsa::invoke(crate::lower::value_ty_query)]
2024-01-16 11:09:40 +00:00
fn value_ty(&self, def: ValueTyDefId) -> Option<Binders<Ty>>;
2024-10-04 19:48:11 +00:00
#[ra_salsa::invoke(crate::lower::impl_self_ty_query)]
#[ra_salsa::cycle(crate::lower::impl_self_ty_recover)]
fn impl_self_ty(&self, def: ImplId) -> Binders<Ty>;
2024-10-04 19:48:11 +00:00
#[ra_salsa::invoke(crate::lower::const_param_ty_query)]
fn const_param_ty(&self, def: ConstParamId) -> Ty;
2024-10-04 19:48:11 +00:00
#[ra_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
2024-10-04 19:48:11 +00:00
#[ra_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
2024-10-04 19:48:11 +00:00
#[ra_salsa::invoke(crate::lower::callable_item_sig)]
fn callable_item_signature(&self, def: CallableDefId) -> PolyFnSig;
2019-11-27 14:46:02 +00:00
2024-10-04 19:48:11 +00:00
#[ra_salsa::invoke(crate::lower::return_type_impl_traits)]
2024-03-15 18:31:12 +00:00
fn return_type_impl_traits(&self, def: FunctionId) -> Option<Arc<Binders<ImplTraits>>>;
2024-10-04 19:48:11 +00:00
#[ra_salsa::invoke(crate::lower::type_alias_impl_traits)]
2024-03-15 18:31:12 +00:00
fn type_alias_impl_traits(&self, def: TypeAliasId) -> Option<Arc<Binders<ImplTraits>>>;
2024-10-04 19:48:11 +00:00
#[ra_salsa::invoke(crate::lower::generic_predicates_for_param_query)]
#[ra_salsa::cycle(crate::lower::generic_predicates_for_param_recover)]
fn generic_predicates_for_param(
&self,
def: GenericDefId,
2021-12-29 13:35:59 +00:00
param_id: TypeOrConstParamId,
assoc_name: Option<Name>,
2024-06-23 13:22:42 +00:00
) -> GenericPredicates;
2019-11-27 14:46:02 +00:00
2024-10-04 19:48:11 +00:00
#[ra_salsa::invoke(crate::lower::generic_predicates_query)]
fn generic_predicates(&self, def: GenericDefId) -> GenericPredicates;
2024-10-04 19:48:11 +00:00
#[ra_salsa::invoke(crate::lower::generic_predicates_without_parent_query)]
fn generic_predicates_without_parent(&self, def: GenericDefId) -> GenericPredicates;
2019-11-27 14:46:02 +00:00
2024-10-04 19:48:11 +00:00
#[ra_salsa::invoke(crate::lower::trait_environment_for_body_query)]
#[ra_salsa::transparent]
2023-11-14 09:08:19 +00:00
fn trait_environment_for_body(&self, def: DefWithBodyId) -> Arc<TraitEnvironment>;
2023-03-08 17:28:52 +00:00
2024-10-04 19:48:11 +00:00
#[ra_salsa::invoke(crate::lower::trait_environment_query)]
2023-11-14 09:08:19 +00:00
fn trait_environment(&self, def: GenericDefId) -> Arc<TraitEnvironment>;
2024-10-04 19:48:11 +00:00
#[ra_salsa::invoke(crate::lower::generic_defaults_query)]
#[ra_salsa::cycle(crate::lower::generic_defaults_recover)]
fn generic_defaults(&self, def: GenericDefId) -> GenericDefaults;
2019-11-27 14:46:02 +00:00
2024-10-04 19:48:11 +00:00
#[ra_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
2024-10-04 19:48:11 +00:00
#[ra_salsa::invoke(InherentImpls::inherent_impls_in_block_query)]
fn inherent_impls_in_block(&self, block: BlockId) -> Option<Arc<InherentImpls>>;
2021-11-09 10:13:42 +00:00
/// Collects all crates in the dependency graph that have impls for the
/// 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.
2024-10-04 19:48:11 +00:00
#[ra_salsa::invoke(crate::method_resolution::incoherent_inherent_impl_crates)]
fn incoherent_inherent_impl_crates(
&self,
krate: CrateId,
fp: TyFingerprint,
) -> SmallVec<[CrateId; 2]>;
2024-10-04 19:48:11 +00:00
#[ra_salsa::invoke(TraitImpls::trait_impls_in_crate_query)]
fn trait_impls_in_crate(&self, krate: CrateId) -> Arc<TraitImpls>;
2024-10-04 19:48:11 +00:00
#[ra_salsa::invoke(TraitImpls::trait_impls_in_block_query)]
fn trait_impls_in_block(&self, block: BlockId) -> Option<Arc<TraitImpls>>;
2024-10-04 19:48:11 +00:00
#[ra_salsa::invoke(TraitImpls::trait_impls_in_deps_query)]
fn trait_impls_in_deps(&self, krate: CrateId) -> Arc<[Arc<TraitImpls>]>;
2019-11-27 14:46:02 +00:00
// Interned IDs for Chalk integration
2024-10-04 19:48:11 +00:00
#[ra_salsa::interned]
fn intern_callable_def(&self, callable_def: CallableDefId) -> InternedCallableDefId;
2024-10-04 19:48:11 +00:00
#[ra_salsa::interned]
2021-12-29 13:35:59 +00:00
fn intern_type_or_const_param_id(
&self,
param_id: TypeOrConstParamId,
) -> InternedTypeOrConstParamId;
2024-10-04 19:48:11 +00:00
#[ra_salsa::interned]
fn intern_lifetime_param_id(&self, param_id: LifetimeParamId) -> InternedLifetimeParamId;
2024-10-04 19:48:11 +00:00
#[ra_salsa::interned]
2021-03-13 19:05:47 +00:00
fn intern_impl_trait_id(&self, id: ImplTraitId) -> InternedOpaqueTyId;
2024-10-04 19:48:11 +00:00
#[ra_salsa::interned]
fn intern_closure(&self, id: InternedClosure) -> InternedClosureId;
2024-10-04 19:48:11 +00:00
#[ra_salsa::interned]
fn intern_coroutine(&self, id: InternedCoroutine) -> InternedCoroutineId;
2019-11-27 14:46:02 +00:00
2024-10-04 19:48:11 +00:00
#[ra_salsa::invoke(chalk_db::associated_ty_data_query)]
2023-05-02 14:12:22 +00:00
fn associated_ty_data(
&self,
id: chalk_db::AssocTypeId,
) -> sync::Arc<chalk_db::AssociatedTyDatum>;
2019-11-27 14:46:02 +00:00
2024-10-04 19:48:11 +00:00
#[ra_salsa::invoke(chalk_db::trait_datum_query)]
2023-05-02 14:12:22 +00:00
fn trait_datum(
&self,
krate: CrateId,
trait_id: chalk_db::TraitId,
) -> sync::Arc<chalk_db::TraitDatum>;
2019-11-27 14:46:02 +00:00
2024-10-04 19:48:11 +00:00
#[ra_salsa::invoke(chalk_db::adt_datum_query)]
2022-08-23 12:56:56 +00:00
fn adt_datum(
2021-04-09 12:15:26 +00:00
&self,
krate: CrateId,
struct_id: chalk_db::AdtId,
2022-08-23 12:56:56 +00:00
) -> sync::Arc<chalk_db::AdtDatum>;
2019-11-27 14:46:02 +00:00
2024-10-04 19:48:11 +00:00
#[ra_salsa::invoke(chalk_db::impl_datum_query)]
2023-05-02 14:12:22 +00:00
fn impl_datum(
&self,
krate: CrateId,
impl_id: chalk_db::ImplId,
) -> sync::Arc<chalk_db::ImplDatum>;
2019-11-27 14:46:02 +00:00
2024-10-04 19:48:11 +00:00
#[ra_salsa::invoke(chalk_db::fn_def_datum_query)]
2024-06-23 13:22:42 +00:00
fn fn_def_datum(&self, fn_def_id: FnDefId) -> sync::Arc<chalk_db::FnDefDatum>;
2024-10-04 19:48:11 +00:00
#[ra_salsa::invoke(chalk_db::fn_def_variance_query)]
fn fn_def_variance(&self, fn_def_id: FnDefId) -> chalk_db::Variances;
2024-10-04 19:48:11 +00:00
#[ra_salsa::invoke(chalk_db::adt_variance_query)]
fn adt_variance(&self, adt_id: chalk_db::AdtId) -> chalk_db::Variances;
2024-10-04 19:48:11 +00:00
#[ra_salsa::invoke(chalk_db::associated_ty_value_query)]
2019-11-27 14:46:02 +00:00
fn associated_ty_value(
&self,
krate: CrateId,
id: chalk_db::AssociatedTyValueId,
2023-05-02 14:12:22 +00:00
) -> sync::Arc<chalk_db::AssociatedTyValue>;
2019-11-27 14:46:02 +00:00
2024-10-04 19:48:11 +00:00
#[ra_salsa::invoke(crate::traits::normalize_projection_query)]
#[ra_salsa::transparent]
fn normalize_projection(
&self,
projection: crate::ProjectionTy,
2023-11-14 09:08:19 +00:00
env: Arc<TraitEnvironment>,
2022-09-12 16:52:16 +00:00
) -> Ty;
2024-10-04 19:48:11 +00:00
#[ra_salsa::invoke(crate::traits::trait_solve_query)]
fn trait_solve(
&self,
krate: CrateId,
2023-03-28 06:29:07 +00:00
block: Option<BlockId>,
goal: crate::Canonical<crate::InEnvironment<crate::Goal>>,
) -> Option<crate::Solution>;
2024-10-04 19:48:11 +00:00
#[ra_salsa::invoke(chalk_db::program_clauses_for_chalk_env_query)]
fn program_clauses_for_chalk_env(
&self,
krate: CrateId,
2023-03-28 06:29:07 +00:00
block: Option<BlockId>,
env: chalk_ir::Environment<Interner>,
) -> chalk_ir::ProgramClauses<Interner>;
2019-11-27 14:46:02 +00:00
}
#[test]
fn hir_database_is_dyn_compatible() {
fn _assert_dyn_compatible(_: &dyn HirDatabase) {}
2019-11-27 14:46:02 +00:00
}
2020-01-31 15:52:43 +00:00
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
2024-10-04 19:48:11 +00:00
pub struct InternedTypeOrConstParamId(ra_salsa::InternId);
2021-12-29 13:35:59 +00:00
impl_intern_key!(InternedTypeOrConstParamId);
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
2024-10-04 19:48:11 +00:00
pub struct InternedLifetimeParamId(ra_salsa::InternId);
impl_intern_key!(InternedLifetimeParamId);
2021-04-06 09:45:41 +00:00
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
2024-10-04 19:48:11 +00:00
pub struct InternedConstParamId(ra_salsa::InternId);
2021-04-06 09:45:41 +00:00
impl_intern_key!(InternedConstParamId);
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
2024-10-04 19:48:11 +00:00
pub struct InternedOpaqueTyId(ra_salsa::InternId);
impl_intern_key!(InternedOpaqueTyId);
2020-07-12 13:26:02 +00:00
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
2024-10-04 19:48:11 +00:00
pub struct InternedClosureId(ra_salsa::InternId);
2021-03-13 18:27:09 +00:00
impl_intern_key!(InternedClosureId);
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct InternedClosure(pub DefWithBodyId, pub ExprId);
impl InternValueTrivial for InternedClosure {}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
2024-10-04 19:48:11 +00:00
pub struct InternedCoroutineId(ra_salsa::InternId);
impl_intern_key!(InternedCoroutineId);
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct InternedCoroutine(pub DefWithBodyId, pub ExprId);
impl InternValueTrivial for InternedCoroutine {}
/// 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)]
2024-10-04 19:48:11 +00:00
pub struct InternedCallableDefId(ra_salsa::InternId);
impl_intern_key!(InternedCallableDefId);