From a48843a16a2306399f2f6a78c69d9192a6480c88 Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Sun, 12 Jul 2020 15:26:02 +0200 Subject: Use Chalk closure support --- crates/ra_hir_ty/src/traits/chalk/mapping.rs | 97 ++++++++++------------------ 1 file changed, 33 insertions(+), 64 deletions(-) (limited to 'crates/ra_hir_ty/src/traits/chalk') diff --git a/crates/ra_hir_ty/src/traits/chalk/mapping.rs b/crates/ra_hir_ty/src/traits/chalk/mapping.rs index 3ebb55f77..796947e69 100644 --- a/crates/ra_hir_ty/src/traits/chalk/mapping.rs +++ b/crates/ra_hir_ty/src/traits/chalk/mapping.rs @@ -15,7 +15,7 @@ use ra_db::salsa::InternKey; use crate::{ db::HirDatabase, primitive::{FloatBitness, FloatTy, IntBitness, IntTy, Signedness}, - traits::{builtin, AssocTyValue, Canonical, Impl, Obligation}, + traits::{Canonical, Obligation}, ApplicationTy, CallableDef, GenericPredicate, InEnvironment, OpaqueTy, OpaqueTyId, ProjectionPredicate, ProjectionTy, Substs, TraitEnvironment, TraitRef, Ty, TyKind, TypeCtor, }; @@ -311,8 +311,15 @@ impl ToChalk for TypeCtor { } TypeCtor::Never => TypeName::Never, - // FIXME convert these - TypeCtor::Adt(_) | TypeCtor::FnPtr { .. } | TypeCtor::Closure { .. } => { + TypeCtor::Closure { def, expr } => { + let closure_id = db.intern_closure((def, expr)); + TypeName::Closure(closure_id.into()) + } + + TypeCtor::FnPtr { .. } => panic!("Trying to convert FnPtr to TypeName"), + + TypeCtor::Adt(_) => { + // FIXME no interning needed anymore // other TypeCtors get interned and turned into a chalk StructId let struct_id = db.intern_type_ctor(self).into(); TypeName::Adt(struct_id) @@ -355,13 +362,16 @@ impl ToChalk for TypeCtor { let callable_def = from_chalk(db, fn_def_id); TypeCtor::FnDef(callable_def) } + TypeName::Array => TypeCtor::Array, - TypeName::Array | TypeName::Error => { - // this should not be reached, since we don't represent TypeName::Error with TypeCtor - unreachable!() + TypeName::Closure(id) => { + let id: crate::db::ClosureId = id.into(); + let (def, expr) = db.lookup_intern_closure(id); + TypeCtor::Closure { def, expr } } - TypeName::Closure(_) => { - // FIXME: implement closure support + + TypeName::Error => { + // this should not be reached, since we don't represent TypeName::Error with TypeCtor unreachable!() } } @@ -433,15 +443,15 @@ impl ToChalk for Mutability { } } -impl ToChalk for Impl { +impl ToChalk for hir_def::ImplId { type Chalk = ImplId; - fn to_chalk(self, db: &dyn HirDatabase) -> ImplId { - db.intern_chalk_impl(self).into() + fn to_chalk(self, _db: &dyn HirDatabase) -> ImplId { + chalk_ir::ImplId(self.as_intern_id()) } - fn from_chalk(db: &dyn HirDatabase, impl_id: ImplId) -> Impl { - db.lookup_intern_chalk_impl(impl_id.into()) + fn from_chalk(_db: &dyn HirDatabase, impl_id: ImplId) -> hir_def::ImplId { + InternKey::from_intern_id(impl_id.0) } } @@ -469,15 +479,20 @@ impl ToChalk for TypeAliasId { } } -impl ToChalk for AssocTyValue { +pub struct TypeAliasAsValue(pub TypeAliasId); + +impl ToChalk for TypeAliasAsValue { type Chalk = AssociatedTyValueId; - fn to_chalk(self, db: &dyn HirDatabase) -> AssociatedTyValueId { - db.intern_assoc_ty_value(self).into() + fn to_chalk(self, _db: &dyn HirDatabase) -> AssociatedTyValueId { + rust_ir::AssociatedTyValueId(self.0.as_intern_id()) } - fn from_chalk(db: &dyn HirDatabase, assoc_ty_value_id: AssociatedTyValueId) -> AssocTyValue { - db.lookup_intern_assoc_ty_value(assoc_ty_value_id.into()) + fn from_chalk( + _db: &dyn HirDatabase, + assoc_ty_value_id: AssociatedTyValueId, + ) -> TypeAliasAsValue { + TypeAliasAsValue(TypeAliasId::from_intern_id(assoc_ty_value_id.0)) } } @@ -686,52 +701,6 @@ where } } -impl ToChalk for builtin::BuiltinImplData { - type Chalk = ImplDatum; - - fn to_chalk(self, db: &dyn HirDatabase) -> ImplDatum { - let impl_type = rust_ir::ImplType::External; - let where_clauses = self.where_clauses.into_iter().map(|w| w.to_chalk(db)).collect(); - - let impl_datum_bound = - rust_ir::ImplDatumBound { trait_ref: self.trait_ref.to_chalk(db), where_clauses }; - let associated_ty_value_ids = - self.assoc_ty_values.into_iter().map(|v| v.to_chalk(db)).collect(); - rust_ir::ImplDatum { - binders: make_binders(impl_datum_bound, self.num_vars), - impl_type, - polarity: rust_ir::Polarity::Positive, - associated_ty_value_ids, - } - } - - fn from_chalk(_db: &dyn HirDatabase, _data: ImplDatum) -> Self { - unimplemented!() - } -} - -impl ToChalk for builtin::BuiltinImplAssocTyValueData { - type Chalk = AssociatedTyValue; - - fn to_chalk(self, db: &dyn HirDatabase) -> AssociatedTyValue { - let ty = self.value.to_chalk(db); - let value_bound = rust_ir::AssociatedTyValueBound { ty }; - - rust_ir::AssociatedTyValue { - associated_ty_id: self.assoc_ty_id.to_chalk(db), - impl_id: self.impl_.to_chalk(db), - value: make_binders(value_bound, self.num_vars), - } - } - - fn from_chalk( - _db: &dyn HirDatabase, - _data: AssociatedTyValue, - ) -> builtin::BuiltinImplAssocTyValueData { - unimplemented!() - } -} - pub(super) fn make_binders(value: T, num_vars: usize) -> chalk_ir::Binders where T: HasInterner, -- cgit v1.2.3 From 20770044631fd0c21caa12f9bc87489ea6c848ee Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Wed, 15 Jul 2020 21:47:45 +0200 Subject: Remove TypeCtor interning Our TypeCtor and Chalk's TypeName match now! --- crates/ra_hir_ty/src/traits/chalk/interner.rs | 14 +++++++++++++- crates/ra_hir_ty/src/traits/chalk/mapping.rs | 13 ++++++------- 2 files changed, 19 insertions(+), 8 deletions(-) (limited to 'crates/ra_hir_ty/src/traits/chalk') diff --git a/crates/ra_hir_ty/src/traits/chalk/interner.rs b/crates/ra_hir_ty/src/traits/chalk/interner.rs index 156b691b4..8d4c51a8f 100644 --- a/crates/ra_hir_ty/src/traits/chalk/interner.rs +++ b/crates/ra_hir_ty/src/traits/chalk/interner.rs @@ -41,7 +41,7 @@ impl chalk_ir::interner::Interner for Interner { type InternedCanonicalVarKinds = Vec>; type InternedConstraints = Vec>>; type DefId = InternId; - type InternedAdtId = crate::TypeCtorId; + type InternedAdtId = hir_def::AdtId; type Identifier = TypeAliasId; type FnAbi = (); @@ -364,6 +364,18 @@ impl chalk_ir::interner::Interner for Interner { ) -> &'a [chalk_ir::InEnvironment>] { constraints } + fn debug_closure_id( + _fn_def_id: chalk_ir::ClosureId, + _fmt: &mut fmt::Formatter<'_>, + ) -> Option { + None + } + fn debug_constraints( + _clauses: &chalk_ir::Constraints, + _fmt: &mut fmt::Formatter<'_>, + ) -> Option { + None + } } impl chalk_ir::interner::HasInterner for Interner { diff --git a/crates/ra_hir_ty/src/traits/chalk/mapping.rs b/crates/ra_hir_ty/src/traits/chalk/mapping.rs index 796947e69..a852ce2ac 100644 --- a/crates/ra_hir_ty/src/traits/chalk/mapping.rs +++ b/crates/ra_hir_ty/src/traits/chalk/mapping.rs @@ -316,20 +316,19 @@ impl ToChalk for TypeCtor { TypeName::Closure(closure_id.into()) } - TypeCtor::FnPtr { .. } => panic!("Trying to convert FnPtr to TypeName"), + TypeCtor::Adt(adt_id) => TypeName::Adt(chalk_ir::AdtId(adt_id)), - TypeCtor::Adt(_) => { - // FIXME no interning needed anymore - // other TypeCtors get interned and turned into a chalk StructId - let struct_id = db.intern_type_ctor(self).into(); - TypeName::Adt(struct_id) + TypeCtor::FnPtr { .. } => { + // This should not be reached, since Chalk doesn't represent + // function pointers with TypeName + unreachable!() } } } fn from_chalk(db: &dyn HirDatabase, type_name: TypeName) -> TypeCtor { match type_name { - TypeName::Adt(struct_id) => db.lookup_intern_type_ctor(struct_id.into()), + TypeName::Adt(struct_id) => TypeCtor::Adt(struct_id.0), TypeName::AssociatedType(type_id) => TypeCtor::AssociatedType(from_chalk(db, type_id)), TypeName::OpaqueType(opaque_type_id) => { TypeCtor::OpaqueType(from_chalk(db, opaque_type_id)) -- cgit v1.2.3