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(
64 param_id: TypeParamId,
65 assoc_name: Option<Name>,
66 ) -> Arc<[Binders<QuantifiedWhereClause>]>;
68 #[salsa::invoke(crate::lower::generic_predicates_query)]
69 fn generic_predicates(&self, def: GenericDefId) -> Arc<[Binders<QuantifiedWhereClause>]>;
71 #[salsa::invoke(crate::lower::trait_environment_query)]
72 fn trait_environment(&self, def: GenericDefId) -> Arc<crate::TraitEnvironment>;
74 #[salsa::invoke(crate::lower::generic_defaults_query)]
75 #[salsa::cycle(crate::lower::generic_defaults_recover)]
76 fn generic_defaults(&self, def: GenericDefId) -> Arc<[Binders<Ty>]>;
78 #[salsa::invoke(InherentImpls::inherent_impls_in_crate_query)]
79 fn inherent_impls_in_crate(&self, krate: CrateId) -> Arc<InherentImpls>;
81 #[salsa::invoke(InherentImpls::inherent_impls_in_block_query)]
82 fn inherent_impls_in_block(&self, block: BlockId) -> Option<Arc<InherentImpls>>;
84 #[salsa::invoke(TraitImpls::trait_impls_in_crate_query)]
85 fn trait_impls_in_crate(&self, krate: CrateId) -> Arc<TraitImpls>;
87 #[salsa::invoke(TraitImpls::trait_impls_in_block_query)]
88 fn trait_impls_in_block(&self, krate: BlockId) -> Option<Arc<TraitImpls>>;
90 #[salsa::invoke(TraitImpls::trait_impls_in_deps_query)]
91 fn trait_impls_in_deps(&self, krate: CrateId) -> Arc<TraitImpls>;
93 // Interned IDs for Chalk integration
95 fn intern_callable_def(&self, callable_def: CallableDefId) -> InternedCallableDefId;
97 fn intern_type_param_id(&self, param_id: TypeParamId) -> InternedTypeParamId;
99 fn intern_lifetime_param_id(&self, param_id: LifetimeParamId) -> InternedLifetimeParamId;
101 fn intern_const_param_id(&self, param_id: ConstParamId) -> InternedConstParamId;
103 fn intern_impl_trait_id(&self, id: ImplTraitId) -> InternedOpaqueTyId;
105 fn intern_closure(&self, id: (DefWithBodyId, ExprId)) -> InternedClosureId;
107 #[salsa::invoke(chalk_db::associated_ty_data_query)]
108 fn associated_ty_data(&self, id: chalk_db::AssocTypeId) -> Arc<chalk_db::AssociatedTyDatum>;
110 #[salsa::invoke(chalk_db::trait_datum_query)]
111 fn trait_datum(&self, krate: CrateId, trait_id: chalk_db::TraitId)
112 -> Arc<chalk_db::TraitDatum>;
114 #[salsa::invoke(chalk_db::struct_datum_query)]
118 struct_id: chalk_db::AdtId,
119 ) -> Arc<chalk_db::StructDatum>;
121 #[salsa::invoke(chalk_db::impl_datum_query)]
122 fn impl_datum(&self, krate: CrateId, impl_id: chalk_db::ImplId) -> Arc<chalk_db::ImplDatum>;
124 #[salsa::invoke(chalk_db::fn_def_datum_query)]
125 fn fn_def_datum(&self, krate: CrateId, fn_def_id: FnDefId) -> Arc<chalk_db::FnDefDatum>;
127 #[salsa::invoke(chalk_db::fn_def_variance_query)]
128 fn fn_def_variance(&self, fn_def_id: FnDefId) -> chalk_db::Variances;
130 #[salsa::invoke(chalk_db::adt_variance_query)]
131 fn adt_variance(&self, adt_id: chalk_db::AdtId) -> chalk_db::Variances;
133 #[salsa::invoke(chalk_db::associated_ty_value_query)]
134 fn associated_ty_value(
137 id: chalk_db::AssociatedTyValueId,
138 ) -> Arc<chalk_db::AssociatedTyValue>;
140 #[salsa::invoke(trait_solve_wait)]
141 #[salsa::transparent]
145 goal: crate::Canonical<crate::InEnvironment<crate::Goal>>,
146 ) -> Option<crate::Solution>;
148 #[salsa::invoke(crate::traits::trait_solve_query)]
149 fn trait_solve_query(
152 goal: crate::Canonical<crate::InEnvironment<crate::Goal>>,
153 ) -> Option<crate::Solution>;
155 #[salsa::invoke(chalk_db::program_clauses_for_chalk_env_query)]
156 fn program_clauses_for_chalk_env(
159 env: chalk_ir::Environment<Interner>,
160 ) -> chalk_ir::ProgramClauses<Interner>;
163 fn infer_wait(db: &dyn HirDatabase, def: DefWithBodyId) -> Arc<InferenceResult> {
164 let _p = profile::span("infer:wait").detail(|| match def {
165 DefWithBodyId::FunctionId(it) => db.function_data(it).name.to_string(),
166 DefWithBodyId::StaticId(it) => db.static_data(it).name.clone().to_string(),
167 DefWithBodyId::ConstId(it) => {
168 db.const_data(it).name.clone().unwrap_or_else(Name::missing).to_string()
175 db: &dyn HirDatabase,
177 goal: crate::Canonical<crate::InEnvironment<crate::Goal>>,
178 ) -> Option<crate::Solution> {
179 let _p = profile::span("trait_solve::wait");
180 db.trait_solve_query(krate, goal)
184 fn hir_database_is_object_safe() {
185 fn _assert_object_safe(_: &dyn HirDatabase) {}
188 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
189 pub struct InternedTypeParamId(salsa::InternId);
190 impl_intern_key!(InternedTypeParamId);
192 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
193 pub struct InternedLifetimeParamId(salsa::InternId);
194 impl_intern_key!(InternedLifetimeParamId);
196 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
197 pub struct InternedConstParamId(salsa::InternId);
198 impl_intern_key!(InternedConstParamId);
200 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
201 pub struct InternedOpaqueTyId(salsa::InternId);
202 impl_intern_key!(InternedOpaqueTyId);
204 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
205 pub struct InternedClosureId(salsa::InternId);
206 impl_intern_key!(InternedClosureId);
208 /// This exists just for Chalk, because Chalk just has a single `FnDefId` where
209 /// we have different IDs for struct and enum variant constructors.
210 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Ord, PartialOrd)]
211 pub struct InternedCallableDefId(salsa::InternId);
212 impl_intern_key!(InternedCallableDefId);