From 9719ce9fc731a400c9744ba1a6569e978c1a97e7 Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Sat, 13 Mar 2021 17:55:50 +0100 Subject: Use chalk_ir::FnDefId --- crates/hir_ty/src/db.rs | 6 +++--- crates/hir_ty/src/diagnostics/unsafe_check.rs | 2 +- crates/hir_ty/src/display.rs | 9 +++++---- crates/hir_ty/src/infer/expr.rs | 7 ++++--- crates/hir_ty/src/lib.rs | 24 ++++++++++++++++++------ crates/hir_ty/src/lower.rs | 15 ++++++++++++--- crates/hir_ty/src/traits/chalk.rs | 4 ++-- crates/hir_ty/src/traits/chalk/interner.rs | 1 - crates/hir_ty/src/traits/chalk/mapping.rs | 5 ++--- 9 files changed, 47 insertions(+), 26 deletions(-) (limited to 'crates/hir_ty') diff --git a/crates/hir_ty/src/db.rs b/crates/hir_ty/src/db.rs index 06714409f..a038674cf 100644 --- a/crates/hir_ty/src/db.rs +++ b/crates/hir_ty/src/db.rs @@ -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 { fn impl_datum(&self, krate: CrateId, impl_id: chalk::ImplId) -> Arc; #[salsa::invoke(crate::traits::chalk::fn_def_datum_query)] - fn fn_def_datum(&self, krate: CrateId, fn_def_id: chalk::FnDefId) -> Arc; + fn fn_def_datum(&self, krate: CrateId, fn_def_id: FnDefId) -> Arc; #[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; diff --git a/crates/hir_ty/src/diagnostics/unsafe_check.rs b/crates/hir_ty/src/diagnostics/unsafe_check.rs index e095bee28..20bb64827 100644 --- a/crates/hir_ty/src/diagnostics/unsafe_check.rs +++ b/crates/hir_ty/src/diagnostics/unsafe_check.rs @@ -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 }); } diff --git a/crates/hir_ty/src/display.rs b/crates/hir_ty/src/display.rs index 2022069d8..b7e85e024 100644 --- a/crates/hir_ty/src/display.rs +++ b/crates/hir_ty/src/display.rs @@ -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()) { diff --git a/crates/hir_ty/src/infer/expr.rs b/crates/hir_ty/src/infer/expr.rs index a72424dc7..153f22f25 100644 --- a/crates/hir_ty/src/infer/expr.rs +++ b/crates/hir_ty/src/infer/expr.rs @@ -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) { diff --git a/crates/hir_ty/src/lib.rs b/crates/hir_ty/src/lib.rs index 5752ddc4e..6b3485264 100644 --- a/crates/hir_ty/src/lib.rs +++ b/crates/hir_ty/src/lib.rs @@ -53,6 +53,7 @@ pub use crate::traits::chalk::Interner; pub type ForeignDefId = chalk_ir::ForeignDefId; pub type AssocTypeId = chalk_ir::AssocTypeId; +pub(crate) type FnDefId = chalk_ir::FnDefId; #[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 { + pub fn as_generic_def(&self, db: &dyn HirDatabase) -> Option { 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 { + pub fn callable_def(&self, db: &dyn HirDatabase) -> Option { 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 { + if let Some(CallableDefId::FunctionId(func)) = self.callable_def(db) { + Some(func) + } else { + None + } + } + pub fn callable_sig(&self, db: &dyn HirDatabase) -> Option { 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) => { diff --git a/crates/hir_ty/src/lower.rs b/crates/hir_ty/src/lower.rs index a5ab1ff70..b8b1400eb 100644 --- a/crates/hir_ty/src/lower.rs +++ b/crates/hir_ty/src/lower.rs @@ -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 { 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 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 { diff --git a/crates/hir_ty/src/traits/chalk.rs b/crates/hir_ty/src/traits/chalk.rs index 2d3cd6719..bb92d8e2a 100644 --- a/crates/hir_ty/src/traits/chalk.rs +++ b/crates/hir_ty/src/traits/chalk.rs @@ -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, diff --git a/crates/hir_ty/src/traits/chalk/interner.rs b/crates/hir_ty/src/traits/chalk/interner.rs index e891efb7b..1dc3f497d 100644 --- a/crates/hir_ty/src/traits/chalk/interner.rs +++ b/crates/hir_ty/src/traits/chalk/interner.rs @@ -20,7 +20,6 @@ pub(crate) type ImplId = chalk_ir::ImplId; pub(crate) type ImplDatum = chalk_solve::rust_ir::ImplDatum; pub(crate) type AssociatedTyValueId = chalk_solve::rust_ir::AssociatedTyValueId; pub(crate) type AssociatedTyValue = chalk_solve::rust_ir::AssociatedTyValue; -pub(crate) type FnDefId = chalk_ir::FnDefId; pub(crate) type FnDefDatum = chalk_solve::rust_ir::FnDefDatum; pub(crate) type OpaqueTyId = chalk_ir::OpaqueTyId; pub(crate) type OpaqueTyDatum = chalk_solve::rust_ir::OpaqueTyDatum; diff --git a/crates/hir_ty/src/traits/chalk/mapping.rs b/crates/hir_ty/src/traits/chalk/mapping.rs index 67ac95a0e..23ef07d77 100644 --- a/crates/hir_ty/src/traits/chalk/mapping.rs +++ b/crates/hir_ty/src/traits/chalk/mapping.rs @@ -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) => { -- cgit v1.2.3