Use chalk_ir::FnDefId
This commit is contained in:
parent
19664e276a
commit
9719ce9fc7
10 changed files with 48 additions and 30 deletions
|
@ -1715,10 +1715,7 @@ impl Type {
|
|||
}
|
||||
|
||||
pub fn as_callable(&self, db: &dyn HirDatabase) -> Option<Callable> {
|
||||
let def = match self.ty.value.interned(&Interner) {
|
||||
&TyKind::FnDef(def, _) => Some(def),
|
||||
_ => None,
|
||||
};
|
||||
let def = self.ty.value.callable_def(db);
|
||||
|
||||
let sig = self.ty.value.callable_sig(db)?;
|
||||
Some(Callable { ty: self.clone(), sig, def, is_bound_method: false })
|
||||
|
|
|
@ -12,7 +12,7 @@ use la_arena::ArenaMap;
|
|||
use crate::{
|
||||
method_resolution::{InherentImpls, TraitImpls},
|
||||
traits::chalk,
|
||||
Binders, CallableDefId, GenericPredicate, InferenceResult, OpaqueTyId, PolyFnSig,
|
||||
Binders, CallableDefId, FnDefId, GenericPredicate, InferenceResult, OpaqueTyId, PolyFnSig,
|
||||
ReturnTypeImplTraits, TraitRef, Ty, TyDefId, ValueTyDefId,
|
||||
};
|
||||
use hir_expand::name::Name;
|
||||
|
@ -100,10 +100,10 @@ pub trait HirDatabase: DefDatabase + Upcast<dyn DefDatabase> {
|
|||
fn impl_datum(&self, krate: CrateId, impl_id: chalk::ImplId) -> Arc<chalk::ImplDatum>;
|
||||
|
||||
#[salsa::invoke(crate::traits::chalk::fn_def_datum_query)]
|
||||
fn fn_def_datum(&self, krate: CrateId, fn_def_id: chalk::FnDefId) -> Arc<chalk::FnDefDatum>;
|
||||
fn fn_def_datum(&self, krate: CrateId, fn_def_id: FnDefId) -> Arc<chalk::FnDefDatum>;
|
||||
|
||||
#[salsa::invoke(crate::traits::chalk::fn_def_variance_query)]
|
||||
fn fn_def_variance(&self, krate: CrateId, fn_def_id: chalk::FnDefId) -> chalk::Variances;
|
||||
fn fn_def_variance(&self, krate: CrateId, fn_def_id: FnDefId) -> chalk::Variances;
|
||||
|
||||
#[salsa::invoke(crate::traits::chalk::adt_variance_query)]
|
||||
fn adt_variance(&self, krate: CrateId, adt_id: chalk::AdtId) -> chalk::Variances;
|
||||
|
|
|
@ -85,7 +85,7 @@ fn walk_unsafe(
|
|||
let expr = &body.exprs[current];
|
||||
match expr {
|
||||
&Expr::Call { callee, .. } => {
|
||||
if let Some(func) = infer[callee].as_fn_def() {
|
||||
if let Some(func) = infer[callee].as_fn_def(db) {
|
||||
if db.function_data(func).is_unsafe {
|
||||
unsafe_exprs.push(UnsafeExpr { expr: current, inside_unsafe_block });
|
||||
}
|
||||
|
|
|
@ -12,8 +12,9 @@ use hir_expand::name::Name;
|
|||
|
||||
use crate::{
|
||||
db::HirDatabase, from_assoc_type_id, from_foreign_def_id, primitive, to_assoc_type_id,
|
||||
utils::generics, AdtId, AliasTy, CallableDefId, CallableSig, GenericPredicate, Interner,
|
||||
Lifetime, Obligation, OpaqueTy, OpaqueTyId, ProjectionTy, Scalar, Substs, TraitRef, Ty, TyKind,
|
||||
traits::chalk::from_chalk, utils::generics, AdtId, AliasTy, CallableDefId, CallableSig,
|
||||
GenericPredicate, Interner, Lifetime, Obligation, OpaqueTy, OpaqueTyId, ProjectionTy, Scalar,
|
||||
Substs, TraitRef, Ty, TyKind,
|
||||
};
|
||||
|
||||
pub struct HirFormatter<'a> {
|
||||
|
@ -363,7 +364,7 @@ impl HirDisplay for Ty {
|
|||
sig.hir_fmt(f)?;
|
||||
}
|
||||
TyKind::FnDef(def, parameters) => {
|
||||
let def = *def;
|
||||
let def = from_chalk(f.db, *def);
|
||||
let sig = f.db.callable_item_signature(def).subst(parameters);
|
||||
match def {
|
||||
CallableDefId::FunctionId(ff) => {
|
||||
|
@ -431,7 +432,7 @@ impl HirDisplay for Ty {
|
|||
|| f.omit_verbose_types()
|
||||
{
|
||||
match self
|
||||
.as_generic_def()
|
||||
.as_generic_def(f.db)
|
||||
.map(|generic_def_id| f.db.generic_defaults(generic_def_id))
|
||||
.filter(|defaults| !defaults.is_empty())
|
||||
{
|
||||
|
|
|
@ -19,7 +19,7 @@ use crate::{
|
|||
method_resolution, op,
|
||||
primitive::{self, UintTy},
|
||||
to_assoc_type_id,
|
||||
traits::{FnTrait, InEnvironment},
|
||||
traits::{chalk::from_chalk, FnTrait, InEnvironment},
|
||||
utils::{generics, variant_data, Generics},
|
||||
AdtId, Binders, CallableDefId, FnPointer, FnSig, Interner, Obligation, OpaqueTyId, Rawness,
|
||||
Scalar, Substs, TraitRef, Ty, TyKind,
|
||||
|
@ -932,8 +932,9 @@ impl<'a> InferenceContext<'a> {
|
|||
}
|
||||
|
||||
fn register_obligations_for_call(&mut self, callable_ty: &Ty) {
|
||||
if let TyKind::FnDef(def, parameters) = callable_ty.interned(&Interner) {
|
||||
let generic_predicates = self.db.generic_predicates((*def).into());
|
||||
if let TyKind::FnDef(fn_def, parameters) = callable_ty.interned(&Interner) {
|
||||
let def: CallableDefId = from_chalk(self.db, *fn_def);
|
||||
let generic_predicates = self.db.generic_predicates(def.into());
|
||||
for predicate in generic_predicates.iter() {
|
||||
let predicate = predicate.clone().subst(parameters);
|
||||
if let Some(obligation) = Obligation::from_predicate(predicate) {
|
||||
|
|
|
@ -53,6 +53,7 @@ pub use crate::traits::chalk::Interner;
|
|||
|
||||
pub type ForeignDefId = chalk_ir::ForeignDefId<Interner>;
|
||||
pub type AssocTypeId = chalk_ir::AssocTypeId<Interner>;
|
||||
pub(crate) type FnDefId = chalk_ir::FnDefId<Interner>;
|
||||
|
||||
#[derive(Clone, PartialEq, Eq, Debug, Hash)]
|
||||
pub enum Lifetime {
|
||||
|
@ -182,7 +183,7 @@ pub enum TyKind {
|
|||
/// fn foo() -> i32 { 1 }
|
||||
/// let bar = foo; // bar: fn() -> i32 {foo}
|
||||
/// ```
|
||||
FnDef(CallableDefId, Substs),
|
||||
FnDef(FnDefId, Substs),
|
||||
|
||||
/// The pointee of a string slice. Written as `str`.
|
||||
Str,
|
||||
|
@ -703,10 +704,12 @@ impl Ty {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn as_generic_def(&self) -> Option<GenericDefId> {
|
||||
pub fn as_generic_def(&self, db: &dyn HirDatabase) -> Option<GenericDefId> {
|
||||
match *self.interned(&Interner) {
|
||||
TyKind::Adt(AdtId(adt), ..) => Some(adt.into()),
|
||||
TyKind::FnDef(callable, ..) => Some(callable.into()),
|
||||
TyKind::FnDef(callable, ..) => {
|
||||
Some(db.lookup_intern_callable_def(callable.into()).into())
|
||||
}
|
||||
TyKind::AssociatedType(type_alias, ..) => Some(from_assoc_type_id(type_alias).into()),
|
||||
TyKind::ForeignType(type_alias, ..) => Some(from_foreign_def_id(type_alias).into()),
|
||||
_ => None,
|
||||
|
@ -775,18 +778,27 @@ impl Ty {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn as_fn_def(&self) -> Option<FunctionId> {
|
||||
pub fn callable_def(&self, db: &dyn HirDatabase) -> Option<CallableDefId> {
|
||||
match self.interned(&Interner) {
|
||||
&TyKind::FnDef(CallableDefId::FunctionId(func), ..) => Some(func),
|
||||
&TyKind::FnDef(def, ..) => Some(db.lookup_intern_callable_def(def.into())),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn as_fn_def(&self, db: &dyn HirDatabase) -> Option<FunctionId> {
|
||||
if let Some(CallableDefId::FunctionId(func)) = self.callable_def(db) {
|
||||
Some(func)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
pub fn callable_sig(&self, db: &dyn HirDatabase) -> Option<CallableSig> {
|
||||
match self.interned(&Interner) {
|
||||
TyKind::Function(fn_ptr) => Some(CallableSig::from_fn_ptr(fn_ptr)),
|
||||
TyKind::FnDef(def, parameters) => {
|
||||
let sig = db.callable_item_signature(*def);
|
||||
let callable_def = db.lookup_intern_callable_def((*def).into());
|
||||
let sig = db.callable_item_signature(callable_def);
|
||||
Some(sig.subst(¶meters))
|
||||
}
|
||||
TyKind::Closure(.., substs) => {
|
||||
|
|
|
@ -1064,7 +1064,10 @@ fn fn_sig_for_fn(db: &dyn HirDatabase, def: FunctionId) -> PolyFnSig {
|
|||
fn type_for_fn(db: &dyn HirDatabase, def: FunctionId) -> Binders<Ty> {
|
||||
let generics = generics(db.upcast(), def.into());
|
||||
let substs = Substs::bound_vars(&generics, DebruijnIndex::INNERMOST);
|
||||
Binders::new(substs.len(), TyKind::FnDef(def.into(), substs).intern(&Interner))
|
||||
Binders::new(
|
||||
substs.len(),
|
||||
TyKind::FnDef(CallableDefId::FunctionId(def).to_chalk(db), substs).intern(&Interner),
|
||||
)
|
||||
}
|
||||
|
||||
/// Build the declared type of a const.
|
||||
|
@ -1107,7 +1110,10 @@ fn type_for_struct_constructor(db: &dyn HirDatabase, def: StructId) -> Binders<T
|
|||
}
|
||||
let generics = generics(db.upcast(), def.into());
|
||||
let substs = Substs::bound_vars(&generics, DebruijnIndex::INNERMOST);
|
||||
Binders::new(substs.len(), TyKind::FnDef(def.into(), substs).intern(&Interner))
|
||||
Binders::new(
|
||||
substs.len(),
|
||||
TyKind::FnDef(CallableDefId::StructId(def).to_chalk(db), substs).intern(&Interner),
|
||||
)
|
||||
}
|
||||
|
||||
fn fn_sig_for_enum_variant_constructor(db: &dyn HirDatabase, def: EnumVariantId) -> PolyFnSig {
|
||||
|
@ -1132,7 +1138,10 @@ fn type_for_enum_variant_constructor(db: &dyn HirDatabase, def: EnumVariantId) -
|
|||
}
|
||||
let generics = generics(db.upcast(), def.parent.into());
|
||||
let substs = Substs::bound_vars(&generics, DebruijnIndex::INNERMOST);
|
||||
Binders::new(substs.len(), TyKind::FnDef(def.into(), substs).intern(&Interner))
|
||||
Binders::new(
|
||||
substs.len(),
|
||||
TyKind::FnDef(CallableDefId::EnumVariantId(def).to_chalk(db), substs).intern(&Interner),
|
||||
)
|
||||
}
|
||||
|
||||
fn type_for_adt(db: &dyn HirDatabase, adt: AdtId) -> Binders<Ty> {
|
||||
|
|
|
@ -21,8 +21,8 @@ use crate::{
|
|||
method_resolution::{TyFingerprint, ALL_FLOAT_FPS, ALL_INT_FPS},
|
||||
to_assoc_type_id,
|
||||
utils::generics,
|
||||
BoundVar, CallableDefId, CallableSig, DebruijnIndex, GenericPredicate, ProjectionPredicate,
|
||||
ProjectionTy, Substs, TraitRef, Ty, TyKind,
|
||||
BoundVar, CallableDefId, CallableSig, DebruijnIndex, FnDefId, GenericPredicate,
|
||||
ProjectionPredicate, ProjectionTy, Substs, TraitRef, Ty, TyKind,
|
||||
};
|
||||
use mapping::{
|
||||
convert_where_clauses, generic_predicate_to_inline_bound, make_binders, TypeAliasAsValue,
|
||||
|
|
|
@ -20,7 +20,6 @@ pub(crate) type ImplId = chalk_ir::ImplId<Interner>;
|
|||
pub(crate) type ImplDatum = chalk_solve::rust_ir::ImplDatum<Interner>;
|
||||
pub(crate) type AssociatedTyValueId = chalk_solve::rust_ir::AssociatedTyValueId<Interner>;
|
||||
pub(crate) type AssociatedTyValue = chalk_solve::rust_ir::AssociatedTyValue<Interner>;
|
||||
pub(crate) type FnDefId = chalk_ir::FnDefId<Interner>;
|
||||
pub(crate) type FnDefDatum = chalk_solve::rust_ir::FnDefDatum<Interner>;
|
||||
pub(crate) type OpaqueTyId = chalk_ir::OpaqueTyId<Interner>;
|
||||
pub(crate) type OpaqueTyDatum = chalk_solve::rust_ir::OpaqueTyDatum<Interner>;
|
||||
|
|
|
@ -66,8 +66,7 @@ impl ToChalk for Ty {
|
|||
chalk_ir::TyKind::Slice(substs[0].clone().to_chalk(db)).intern(&Interner)
|
||||
}
|
||||
TyKind::Str => chalk_ir::TyKind::Str.intern(&Interner),
|
||||
TyKind::FnDef(callable_def, substs) => {
|
||||
let id = callable_def.to_chalk(db);
|
||||
TyKind::FnDef(id, substs) => {
|
||||
let substitution = substs.to_chalk(db);
|
||||
chalk_ir::TyKind::FnDef(id, substitution).intern(&Interner)
|
||||
}
|
||||
|
@ -201,7 +200,7 @@ impl ToChalk for Ty {
|
|||
chalk_ir::TyKind::Never => TyKind::Never,
|
||||
|
||||
chalk_ir::TyKind::FnDef(fn_def_id, subst) => {
|
||||
TyKind::FnDef(from_chalk(db, fn_def_id), from_chalk(db, subst))
|
||||
TyKind::FnDef(fn_def_id, from_chalk(db, subst))
|
||||
}
|
||||
|
||||
chalk_ir::TyKind::Closure(id, subst) => {
|
||||
|
|
Loading…
Add table
Reference in a new issue