1 //! The home of `HirDatabase`, which is the Salsa database containing all the
2 //! type inference-related queries.
6 use base_db::{impl_intern_key, salsa, CrateId, Upcast};
8 db::DefDatabase, expr::ExprId, BlockId, ConstParamId, DefWithBodyId, FunctionId, GenericDefId,
9 ImplId, LifetimeParamId, LocalFieldId, TypeParamId, VariantId,
11 use la_arena::ArenaMap;
15 method_resolution::{InherentImpls, TraitImpls},
16 Binders, CallableDefId, FnDefId, ImplTraitId, InferenceResult, Interner, PolyFnSig,
17 QuantifiedWhereClause, ReturnTypeImplTraits, TraitRef, Ty, TyDefId, ValueTyDefId,
19 use hir_expand::name::Name;
21 #[salsa::query_group(HirDatabaseStorage)]
22 pub trait HirDatabase: DefDatabase + Upcast<dyn DefDatabase> {
23 #[salsa::invoke(infer_wait)]
25 fn infer(&self, def: DefWithBodyId) -> Arc<InferenceResult>;
27 #[salsa::invoke(crate::infer::infer_query)]
28 fn infer_query(&self, def: DefWithBodyId) -> Arc<InferenceResult>;
30 #[salsa::invoke(crate::lower::ty_query)]
31 #[salsa::cycle(crate::lower::ty_recover)]
32 fn ty(&self, def: TyDefId) -> Binders<Ty>;
34 #[salsa::invoke(crate::lower::value_ty_query)]
35 fn value_ty(&self, def: ValueTyDefId) -> Binders<Ty>;
37 #[salsa::invoke(crate::lower::impl_self_ty_query)]
38 #[salsa::cycle(crate::lower::impl_self_ty_recover)]
39 fn impl_self_ty(&self, def: ImplId) -> Binders<Ty>;
41 #[salsa::invoke(crate::lower::const_param_ty_query)]
42 fn const_param_ty(&self, def: ConstParamId) -> Ty;
44 #[salsa::invoke(crate::lower::impl_trait_query)]
45 fn impl_trait(&self, def: ImplId) -> Option<Binders<TraitRef>>;
47 #[salsa::invoke(crate::lower::field_types_query)]
48 fn field_types(&self, var: VariantId) -> Arc<ArenaMap<LocalFieldId, Binders<Ty>>>;
50 #[salsa::invoke(crate::callable_item_sig)]
51 fn callable_item_signature(&self, def: CallableDefId) -> PolyFnSig;
53 #[salsa::invoke(crate::lower::return_type_impl_traits)]
54 fn return_type_impl_traits(
57 ) -> Option<Arc<Binders<ReturnTypeImplTraits>>>;
59 #[salsa::invoke(crate::lower::generic_predicates_for_param_query)]
60 #[salsa::cycle(crate::lower::generic_predicates_for_param_recover)]
61 fn generic_predicates_for_param(
63 param_id: TypeParamId,
64 assoc_name: Option<Name>,
65 ) -> Arc<[Binders<QuantifiedWhereClause>]>;
67 #[salsa::invoke(crate::lower::generic_predicates_query)]
68 fn generic_predicates(&self, def: GenericDefId) -> Arc<[Binders<QuantifiedWhereClause>]>;
70 #[salsa::invoke(crate::lower::trait_environment_query)]
71 fn trait_environment(&self, def: GenericDefId) -> Arc<crate::TraitEnvironment>;
73 #[salsa::invoke(crate::lower::generic_defaults_query)]
74 #[salsa::cycle(crate::lower::generic_defaults_recover)]
75 fn generic_defaults(&self, def: GenericDefId) -> Arc<[Binders<Ty>]>;
77 #[salsa::invoke(InherentImpls::inherent_impls_in_crate_query)]
78 fn inherent_impls_in_crate(&self, krate: CrateId) -> Arc<InherentImpls>;
80 #[salsa::invoke(TraitImpls::trait_impls_in_crate_query)]
81 fn trait_impls_in_crate(&self, krate: CrateId) -> Arc<TraitImpls>;
83 #[salsa::invoke(TraitImpls::trait_impls_in_block_query)]
84 fn trait_impls_in_block(&self, krate: BlockId) -> Option<Arc<TraitImpls>>;
86 #[salsa::invoke(TraitImpls::trait_impls_in_deps_query)]
87 fn trait_impls_in_deps(&self, krate: CrateId) -> Arc<TraitImpls>;
89 // Interned IDs for Chalk integration
91 fn intern_callable_def(&self, callable_def: CallableDefId) -> InternedCallableDefId;
93 fn intern_type_param_id(&self, param_id: TypeParamId) -> InternedTypeParamId;
95 fn intern_lifetime_param_id(&self, param_id: LifetimeParamId) -> InternedLifetimeParamId;
97 fn intern_const_param_id(&self, param_id: ConstParamId) -> InternedConstParamId;
99 fn intern_impl_trait_id(&self, id: ImplTraitId) -> InternedOpaqueTyId;
101 fn intern_closure(&self, id: (DefWithBodyId, ExprId)) -> InternedClosureId;
103 #[salsa::invoke(chalk_db::associated_ty_data_query)]
104 fn associated_ty_data(&self, id: chalk_db::AssocTypeId) -> Arc<chalk_db::AssociatedTyDatum>;
106 #[salsa::invoke(chalk_db::trait_datum_query)]
107 fn trait_datum(&self, krate: CrateId, trait_id: chalk_db::TraitId)
108 -> Arc<chalk_db::TraitDatum>;
110 #[salsa::invoke(chalk_db::struct_datum_query)]
114 struct_id: chalk_db::AdtId,
115 ) -> Arc<chalk_db::StructDatum>;
117 #[salsa::invoke(chalk_db::impl_datum_query)]
118 fn impl_datum(&self, krate: CrateId, impl_id: chalk_db::ImplId) -> Arc<chalk_db::ImplDatum>;
120 #[salsa::invoke(chalk_db::fn_def_datum_query)]
121 fn fn_def_datum(&self, krate: CrateId, fn_def_id: FnDefId) -> Arc<chalk_db::FnDefDatum>;
123 #[salsa::invoke(chalk_db::fn_def_variance_query)]
124 fn fn_def_variance(&self, fn_def_id: FnDefId) -> chalk_db::Variances;
126 #[salsa::invoke(chalk_db::adt_variance_query)]
127 fn adt_variance(&self, adt_id: chalk_db::AdtId) -> chalk_db::Variances;
129 #[salsa::invoke(chalk_db::associated_ty_value_query)]
130 fn associated_ty_value(
133 id: chalk_db::AssociatedTyValueId,
134 ) -> Arc<chalk_db::AssociatedTyValue>;
136 #[salsa::invoke(trait_solve_wait)]
137 #[salsa::transparent]
141 goal: crate::Canonical<crate::InEnvironment<crate::Goal>>,
142 ) -> Option<crate::Solution>;
144 #[salsa::invoke(crate::traits::trait_solve_query)]
145 fn trait_solve_query(
148 goal: crate::Canonical<crate::InEnvironment<crate::Goal>>,
149 ) -> Option<crate::Solution>;
151 #[salsa::invoke(chalk_db::program_clauses_for_chalk_env_query)]
152 fn program_clauses_for_chalk_env(
155 env: chalk_ir::Environment<Interner>,
156 ) -> chalk_ir::ProgramClauses<Interner>;
159 fn infer_wait(db: &dyn HirDatabase, def: DefWithBodyId) -> Arc<InferenceResult> {
160 let _p = profile::span("infer:wait").detail(|| match def {
161 DefWithBodyId::FunctionId(it) => db.function_data(it).name.to_string(),
162 DefWithBodyId::StaticId(it) => {
163 db.static_data(it).name.clone().unwrap_or_else(Name::missing).to_string()
165 DefWithBodyId::ConstId(it) => {
166 db.const_data(it).name.clone().unwrap_or_else(Name::missing).to_string()
173 db: &dyn HirDatabase,
175 goal: crate::Canonical<crate::InEnvironment<crate::Goal>>,
176 ) -> Option<crate::Solution> {
177 let _p = profile::span("trait_solve::wait");
178 db.trait_solve_query(krate, goal)
182 fn hir_database_is_object_safe() {
183 fn _assert_object_safe(_: &dyn HirDatabase) {}
186 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
187 pub struct InternedTypeParamId(salsa::InternId);
188 impl_intern_key!(InternedTypeParamId);
190 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
191 pub struct InternedLifetimeParamId(salsa::InternId);
192 impl_intern_key!(InternedLifetimeParamId);
194 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
195 pub struct InternedConstParamId(salsa::InternId);
196 impl_intern_key!(InternedConstParamId);
198 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
199 pub struct InternedOpaqueTyId(salsa::InternId);
200 impl_intern_key!(InternedOpaqueTyId);
202 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
203 pub struct InternedClosureId(salsa::InternId);
204 impl_intern_key!(InternedClosureId);
206 /// This exists just for Chalk, because Chalk just has a single `FnDefId` where
207 /// we have different IDs for struct and enum variant constructors.
208 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Ord, PartialOrd)]
209 pub struct InternedCallableDefId(salsa::InternId);
210 impl_intern_key!(InternedCallableDefId);