Unnamed repository; edit this file 'description' to name the repository.
Diffstat (limited to 'crates/hir-ty/src/lower.rs')
-rw-r--r--crates/hir-ty/src/lower.rs275
1 files changed, 16 insertions, 259 deletions
diff --git a/crates/hir-ty/src/lower.rs b/crates/hir-ty/src/lower.rs
index 20f421dbbc..b55f9cd145 100644
--- a/crates/hir-ty/src/lower.rs
+++ b/crates/hir-ty/src/lower.rs
@@ -24,16 +24,15 @@ use chalk_ir::{
use either::Either;
use hir_def::{
- AdtId, AssocItemId, CallableDefId, ConstId, ConstParamId, EnumId, EnumVariantId, FunctionId,
- GenericDefId, GenericParamId, ImplId, ItemContainerId, LocalFieldId, Lookup, StaticId,
- StructId, TypeAliasId, TypeOrConstParamId, UnionId, VariantId,
+ AdtId, AssocItemId, ConstId, ConstParamId, EnumId, EnumVariantId, FunctionId, GenericDefId,
+ GenericParamId, ImplId, ItemContainerId, LocalFieldId, Lookup, StaticId, StructId, TypeAliasId,
+ TypeOrConstParamId, UnionId, VariantId,
builtin_type::BuiltinType,
expr_store::{ExpressionStore, path::Path},
hir::generics::{GenericParamDataRef, TypeOrConstParamData, WherePredicate},
- item_tree::FieldsShape,
lang_item::LangItem,
resolver::{HasResolver, LifetimeNs, Resolver, TypeNs},
- signatures::{FunctionSignature, TraitFlags, TypeAliasFlags},
+ signatures::{FunctionSignature, TraitFlags},
type_ref::{
ConstRef, LifetimeRefId, LiteralConstRef, PathId, TraitBoundModifier,
TraitRef as HirTraitRef, TypeBound, TypeRef, TypeRefId,
@@ -46,10 +45,10 @@ use stdx::{impl_from, never};
use triomphe::{Arc, ThinArc};
use crate::{
- AliasTy, Binders, BoundVar, CallableSig, Const, DebruijnIndex, DynTy, FnAbi, FnPointer, FnSig,
- FnSubst, ImplTrait, ImplTraitId, ImplTraits, Interner, Lifetime, LifetimeData,
- LifetimeOutlives, PolyFnSig, QuantifiedWhereClause, QuantifiedWhereClauses, Substitution,
- TraitRef, TraitRefExt, Ty, TyBuilder, TyKind, WhereClause, all_super_traits,
+ AliasTy, Binders, BoundVar, Const, DebruijnIndex, DynTy, FnAbi, FnPointer, FnSig, FnSubst,
+ ImplTrait, ImplTraitId, ImplTraits, Interner, Lifetime, LifetimeData, LifetimeOutlives,
+ QuantifiedWhereClause, QuantifiedWhereClauses, Substitution, TraitRef, TraitRefExt, Ty,
+ TyBuilder, TyKind, WhereClause, all_super_traits,
consteval::{intern_const_ref, path_to_const, unknown_const, unknown_const_as_generic},
db::HirDatabase,
error_lifetime,
@@ -59,7 +58,7 @@ use crate::{
path::{PathDiagnosticCallback, PathLoweringContext},
},
make_binders,
- mapping::{ToChalk, from_chalk_trait_id, lt_to_placeholder_idx},
+ mapping::{from_chalk_trait_id, lt_to_placeholder_idx},
static_lifetime, to_chalk_trait_id, to_placeholder_idx,
utils::all_super_trait_refs,
variable_kinds_from_iter,
@@ -825,15 +824,6 @@ impl<'a> TyLoweringContext<'a> {
}
}
-/// Build the signature of a callable item (function, struct or enum variant).
-pub(crate) fn callable_item_signature_query(db: &dyn HirDatabase, def: CallableDefId) -> PolyFnSig {
- match def {
- CallableDefId::FunctionId(f) => fn_sig_for_fn(db, f),
- CallableDefId::StructId(s) => fn_sig_for_struct_constructor(db, s),
- CallableDefId::EnumVariantId(e) => fn_sig_for_enum_variant_constructor(db, e),
- }
-}
-
fn named_associated_type_shorthand_candidates<R>(
db: &dyn HirDatabase,
// If the type parameter is defined in an impl and we're in a method, there
@@ -918,7 +908,7 @@ pub(crate) fn field_types_query(
db: &dyn HirDatabase,
variant_id: VariantId,
) -> Arc<ArenaMap<LocalFieldId, Binders<Ty>>> {
- db.field_types_with_diagnostics(variant_id).0
+ field_types_with_diagnostics_query(db, variant_id).0
}
/// Build the type of all specific fields of a struct or enum variant.
@@ -1313,208 +1303,6 @@ pub(crate) fn generic_defaults_with_diagnostics_cycle_result(
(GenericDefaults(None), None)
}
-fn fn_sig_for_fn(db: &dyn HirDatabase, def: FunctionId) -> PolyFnSig {
- let data = db.function_signature(def);
- let resolver = def.resolver(db);
- let mut ctx_params = TyLoweringContext::new(
- db,
- &resolver,
- &data.store,
- def.into(),
- LifetimeElisionKind::for_fn_params(&data),
- )
- .with_type_param_mode(ParamLoweringMode::Variable);
- let params = data.params.iter().map(|&tr| ctx_params.lower_ty(tr));
-
- let ret = match data.ret_type {
- Some(ret_type) => {
- let mut ctx_ret = TyLoweringContext::new(
- db,
- &resolver,
- &data.store,
- def.into(),
- LifetimeElisionKind::for_fn_ret(),
- )
- .with_impl_trait_mode(ImplTraitLoweringMode::Opaque)
- .with_type_param_mode(ParamLoweringMode::Variable);
- ctx_ret.lower_ty(ret_type)
- }
- None => TyKind::Tuple(0, Substitution::empty(Interner)).intern(Interner),
- };
- let generics = generics(db, def.into());
- let sig = CallableSig::from_params_and_return(
- params,
- ret,
- data.is_varargs(),
- if data.is_unsafe() { Safety::Unsafe } else { Safety::Safe },
- data.abi.as_ref().map_or(FnAbi::Rust, FnAbi::from_symbol),
- );
- make_binders(db, &generics, sig)
-}
-
-/// Build the declared type of a function. This should not need to look at the
-/// function body.
-fn type_for_fn(db: &dyn HirDatabase, def: FunctionId) -> Binders<Ty> {
- let generics = generics(db, def.into());
- let substs = generics.bound_vars_subst(db, DebruijnIndex::INNERMOST);
- make_binders(
- db,
- &generics,
- TyKind::FnDef(CallableDefId::FunctionId(def).to_chalk(db), substs).intern(Interner),
- )
-}
-
-/// Build the declared type of a const.
-fn type_for_const(db: &dyn HirDatabase, def: ConstId) -> Binders<Ty> {
- let data = db.const_signature(def);
- let generics = generics(db, def.into());
- let resolver = def.resolver(db);
- let parent = def.loc(db).container;
- let mut ctx = TyLoweringContext::new(
- db,
- &resolver,
- &data.store,
- def.into(),
- LifetimeElisionKind::for_const(parent),
- )
- .with_type_param_mode(ParamLoweringMode::Variable);
-
- make_binders(db, &generics, ctx.lower_ty(data.type_ref))
-}
-
-/// Build the declared type of a static.
-fn type_for_static(db: &dyn HirDatabase, def: StaticId) -> Binders<Ty> {
- let data = db.static_signature(def);
- let resolver = def.resolver(db);
- let mut ctx = TyLoweringContext::new(
- db,
- &resolver,
- &data.store,
- def.into(),
- LifetimeElisionKind::Elided(static_lifetime()),
- );
-
- Binders::empty(Interner, ctx.lower_ty(data.type_ref))
-}
-
-fn fn_sig_for_struct_constructor(db: &dyn HirDatabase, def: StructId) -> PolyFnSig {
- let field_tys = db.field_types(def.into());
- let params = field_tys.iter().map(|(_, ty)| ty.skip_binders().clone());
- let (ret, binders) = type_for_adt(db, def.into()).into_value_and_skipped_binders();
- Binders::new(
- binders,
- CallableSig::from_params_and_return(params, ret, false, Safety::Safe, FnAbi::RustCall),
- )
-}
-
-/// Build the type of a tuple struct constructor.
-fn type_for_struct_constructor(db: &dyn HirDatabase, def: StructId) -> Option<Binders<Ty>> {
- let struct_data = def.fields(db);
- match struct_data.shape {
- FieldsShape::Record => None,
- FieldsShape::Unit => Some(type_for_adt(db, def.into())),
- FieldsShape::Tuple => {
- let generics = generics(db, AdtId::from(def).into());
- let substs = generics.bound_vars_subst(db, DebruijnIndex::INNERMOST);
- Some(make_binders(
- db,
- &generics,
- TyKind::FnDef(CallableDefId::StructId(def).to_chalk(db), substs).intern(Interner),
- ))
- }
- }
-}
-
-fn fn_sig_for_enum_variant_constructor(db: &dyn HirDatabase, def: EnumVariantId) -> PolyFnSig {
- let field_tys = db.field_types(def.into());
- let params = field_tys.iter().map(|(_, ty)| ty.skip_binders().clone());
- let parent = def.lookup(db).parent;
- let (ret, binders) = type_for_adt(db, parent.into()).into_value_and_skipped_binders();
- Binders::new(
- binders,
- CallableSig::from_params_and_return(params, ret, false, Safety::Safe, FnAbi::RustCall),
- )
-}
-
-/// Build the type of a tuple enum variant constructor.
-fn type_for_enum_variant_constructor(
- db: &dyn HirDatabase,
- def: EnumVariantId,
-) -> Option<Binders<Ty>> {
- let e = def.lookup(db).parent;
- match def.fields(db).shape {
- FieldsShape::Record => None,
- FieldsShape::Unit => Some(type_for_adt(db, e.into())),
- FieldsShape::Tuple => {
- let generics = generics(db, e.into());
- let substs = generics.bound_vars_subst(db, DebruijnIndex::INNERMOST);
- Some(make_binders(
- db,
- &generics,
- TyKind::FnDef(CallableDefId::EnumVariantId(def).to_chalk(db), substs)
- .intern(Interner),
- ))
- }
- }
-}
-
-#[salsa_macros::tracked(cycle_result = type_for_adt_cycle_result)]
-fn type_for_adt_tracked(db: &dyn HirDatabase, adt: AdtId) -> Binders<Ty> {
- type_for_adt(db, adt)
-}
-
-fn type_for_adt_cycle_result(db: &dyn HirDatabase, adt: AdtId) -> Binders<Ty> {
- let generics = generics(db, adt.into());
- make_binders(db, &generics, TyKind::Error.intern(Interner))
-}
-
-fn type_for_adt(db: &dyn HirDatabase, adt: AdtId) -> Binders<Ty> {
- let generics = generics(db, adt.into());
- let subst = generics.bound_vars_subst(db, DebruijnIndex::INNERMOST);
- let ty = TyKind::Adt(crate::AdtId(adt), subst).intern(Interner);
- make_binders(db, &generics, ty)
-}
-
-pub(crate) fn type_for_type_alias_with_diagnostics_query(
- db: &dyn HirDatabase,
- t: TypeAliasId,
-) -> (Binders<Ty>, Diagnostics) {
- let generics = generics(db, t.into());
- let type_alias_data = db.type_alias_signature(t);
- let mut diags = None;
- let inner = if type_alias_data.flags.contains(TypeAliasFlags::IS_EXTERN) {
- TyKind::Foreign(crate::to_foreign_def_id(t)).intern(Interner)
- } else {
- let resolver = t.resolver(db);
- let alias = db.type_alias_signature(t);
- let mut ctx = TyLoweringContext::new(
- db,
- &resolver,
- &alias.store,
- t.into(),
- LifetimeElisionKind::AnonymousReportError,
- )
- .with_impl_trait_mode(ImplTraitLoweringMode::Opaque)
- .with_type_param_mode(ParamLoweringMode::Variable);
- let res = alias
- .ty
- .map(|type_ref| ctx.lower_ty(type_ref))
- .unwrap_or_else(|| TyKind::Error.intern(Interner));
- diags = create_diagnostics(ctx.diagnostics);
- res
- };
-
- (make_binders(db, &generics, inner), diags)
-}
-
-pub(crate) fn type_for_type_alias_with_diagnostics_cycle_result(
- db: &dyn HirDatabase,
- adt: TypeAliasId,
-) -> (Binders<Ty>, Diagnostics) {
- let generics = generics(db, adt.into());
- (make_binders(db, &generics, TyKind::Error.intern(Interner)), None)
-}
-
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum TyDefId {
BuiltinType(BuiltinType),
@@ -1547,31 +1335,8 @@ impl ValueTyDefId {
}
}
-/// Build the declared type of an item. This depends on the namespace; e.g. for
-/// `struct Foo(usize)`, we have two types: The type of the struct itself, and
-/// the constructor function `(usize) -> Foo` which lives in the values
-/// namespace.
-pub(crate) fn ty_query(db: &dyn HirDatabase, def: TyDefId) -> Binders<Ty> {
- match def {
- TyDefId::BuiltinType(it) => Binders::empty(Interner, TyBuilder::builtin(it)),
- TyDefId::AdtId(it) => type_for_adt_tracked(db, it),
- TyDefId::TypeAliasId(it) => db.type_for_type_alias_with_diagnostics(it).0,
- }
-}
-
-pub(crate) fn value_ty_query(db: &dyn HirDatabase, def: ValueTyDefId) -> Option<Binders<Ty>> {
- match def {
- ValueTyDefId::FunctionId(it) => Some(type_for_fn(db, it)),
- ValueTyDefId::StructId(it) => type_for_struct_constructor(db, it),
- ValueTyDefId::UnionId(it) => Some(type_for_adt(db, it.into())),
- ValueTyDefId::EnumVariantId(it) => type_for_enum_variant_constructor(db, it),
- ValueTyDefId::ConstId(it) => Some(type_for_const(db, it)),
- ValueTyDefId::StaticId(it) => Some(type_for_static(db, it)),
- }
-}
-
pub(crate) fn impl_self_ty_query(db: &dyn HirDatabase, impl_id: ImplId) -> Binders<Ty> {
- db.impl_self_ty_with_diagnostics(impl_id).0
+ impl_self_ty_with_diagnostics_query(db, impl_id).0
}
pub(crate) fn impl_self_ty_with_diagnostics_query(
@@ -1595,16 +1360,8 @@ pub(crate) fn impl_self_ty_with_diagnostics_query(
)
}
-pub(crate) fn impl_self_ty_with_diagnostics_cycle_result(
- db: &dyn HirDatabase,
- impl_id: ImplId,
-) -> (Binders<Ty>, Diagnostics) {
- let generics = generics(db, impl_id.into());
- (make_binders(db, &generics, TyKind::Error.intern(Interner)), None)
-}
-
pub(crate) fn const_param_ty_query(db: &dyn HirDatabase, def: ConstParamId) -> Ty {
- db.const_param_ty_with_diagnostics(def).0
+ const_param_ty_with_diagnostics_query(db, def).0
}
// returns None if def is a type arg
@@ -1632,16 +1389,16 @@ pub(crate) fn const_param_ty_with_diagnostics_query(
(ty, create_diagnostics(ctx.diagnostics))
}
-pub(crate) fn const_param_ty_with_diagnostics_cycle_result(
+pub(crate) fn const_param_ty_cycle_result(
_: &dyn HirDatabase,
_: crate::db::HirDatabaseData,
_: ConstParamId,
-) -> (Ty, Diagnostics) {
- (TyKind::Error.intern(Interner), None)
+) -> Ty {
+ TyKind::Error.intern(Interner)
}
pub(crate) fn impl_trait_query(db: &dyn HirDatabase, impl_id: ImplId) -> Option<Binders<TraitRef>> {
- db.impl_trait_with_diagnostics(impl_id).map(|it| it.0)
+ impl_trait_with_diagnostics_query(db, impl_id).map(|it| it.0)
}
pub(crate) fn impl_trait_with_diagnostics_query(