From d5d485ef9289589332893f2c0ad96cb366afe9d6 Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Sun, 28 Jun 2020 21:17:27 +0200 Subject: Implement Chalk variable kinds This means we need to keep track of the kinds (general/int/float) of variables in `Canonical`, which requires some more ceremony. (It also exposes some places where we're not really dealing with canonicalization correctly -- another thing to be cleaned up when we switch to using Chalk's types directly.) Should fix the last remaining issue of #2534. --- crates/ra_hir_ty/src/traits/chalk/mapping.rs | 43 +++++++++++++++++++--------- 1 file changed, 30 insertions(+), 13 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 ac82ea831..433d6aa03 100644 --- a/crates/ra_hir_ty/src/traits/chalk/mapping.rs +++ b/crates/ra_hir_ty/src/traits/chalk/mapping.rs @@ -17,7 +17,7 @@ use crate::{ primitive::{FloatBitness, FloatTy, IntBitness, IntTy, Signedness}, traits::{builtin, AssocTyValue, Canonical, Impl, Obligation}, ApplicationTy, CallableDef, GenericPredicate, InEnvironment, OpaqueTy, OpaqueTyId, - ProjectionPredicate, ProjectionTy, Substs, TraitEnvironment, TraitRef, Ty, TypeCtor, + ProjectionPredicate, ProjectionTy, Substs, TraitEnvironment, TraitRef, Ty, TyKind, TypeCtor, }; use super::interner::*; @@ -555,22 +555,39 @@ where type Chalk = chalk_ir::Canonical; fn to_chalk(self, db: &dyn HirDatabase) -> chalk_ir::Canonical { - let parameter = chalk_ir::CanonicalVarKind::new( - chalk_ir::VariableKind::Ty(chalk_ir::TyKind::General), - chalk_ir::UniverseIndex::ROOT, - ); + let kinds = self + .kinds + .iter() + .map(|k| match k { + TyKind::General => chalk_ir::TyKind::General, + TyKind::Integer => chalk_ir::TyKind::Integer, + TyKind::Float => chalk_ir::TyKind::Float, + }) + .map(|tk| { + chalk_ir::CanonicalVarKind::new( + chalk_ir::VariableKind::Ty(tk), + chalk_ir::UniverseIndex::ROOT, + ) + }); let value = self.value.to_chalk(db); - chalk_ir::Canonical { - value, - binders: chalk_ir::CanonicalVarKinds::from(&Interner, vec![parameter; self.num_vars]), - } + chalk_ir::Canonical { value, binders: chalk_ir::CanonicalVarKinds::from(&Interner, kinds) } } fn from_chalk(db: &dyn HirDatabase, canonical: chalk_ir::Canonical) -> Canonical { - Canonical { - num_vars: canonical.binders.len(&Interner), - value: from_chalk(db, canonical.value), - } + let kinds = canonical + .binders + .iter(&Interner) + .map(|k| match k.kind { + chalk_ir::VariableKind::Ty(tk) => match tk { + chalk_ir::TyKind::General => TyKind::General, + chalk_ir::TyKind::Integer => TyKind::Integer, + chalk_ir::TyKind::Float => TyKind::Float, + }, + chalk_ir::VariableKind::Lifetime => panic!("unexpected lifetime from Chalk"), + chalk_ir::VariableKind::Const(_) => panic!("unexpected const from Chalk"), + }) + .collect(); + Canonical { kinds, value: from_chalk(db, canonical.value) } } } -- cgit v1.2.3 From 4bbc385277bcab509c321b1374f72f1ef19d7750 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 7 Jul 2020 10:14:48 +0200 Subject: Switch to fully dynamically dispatched salsa This improves compile times quite a bit --- crates/ra_hir_ty/src/traits/chalk/tls.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'crates/ra_hir_ty/src/traits/chalk') diff --git a/crates/ra_hir_ty/src/traits/chalk/tls.rs b/crates/ra_hir_ty/src/traits/chalk/tls.rs index 556af7098..e6a9d3211 100644 --- a/crates/ra_hir_ty/src/traits/chalk/tls.rs +++ b/crates/ra_hir_ty/src/traits/chalk/tls.rs @@ -10,7 +10,7 @@ use hir_def::{AdtId, AssocContainerId, DefWithBodyId, Lookup, TypeAliasId}; pub use unsafe_tls::{set_current_program, with_current_program}; -pub struct DebugContext<'a>(&'a (dyn HirDatabase + 'a)); +pub struct DebugContext<'a>(&'a dyn HirDatabase); impl DebugContext<'_> { pub fn debug_struct_id( -- cgit v1.2.3 From 8a72e40ca91cc51a93b8145582feaccb7254abb6 Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Fri, 10 Jul 2020 19:14:33 +0200 Subject: Fix #4966 We add a level of binders when converting our function pointer to Chalk's; we need to remove it again on the way back. --- crates/ra_hir_ty/src/traits/chalk/mapping.rs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 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 433d6aa03..cb354d586 100644 --- a/crates/ra_hir_ty/src/traits/chalk/mapping.rs +++ b/crates/ra_hir_ty/src/traits/chalk/mapping.rs @@ -115,8 +115,12 @@ impl ToChalk for Ty { let parameters = from_chalk(db, opaque_ty.substitution); Ty::Opaque(OpaqueTy { opaque_ty_id: impl_trait_id, parameters }) } - chalk_ir::TyData::Function(chalk_ir::Fn { num_binders: _, substitution }) => { - let parameters: Substs = from_chalk(db, substitution); + chalk_ir::TyData::Function(chalk_ir::Fn { num_binders, substitution }) => { + assert_eq!(num_binders, 0); + let parameters: Substs = from_chalk( + db, + substitution.shifted_out(&Interner).expect("fn ptr should have no binders"), + ); Ty::Apply(ApplicationTy { ctor: TypeCtor::FnPtr { num_args: (parameters.len() - 1) as u16 }, parameters, -- cgit v1.2.3 From 209c492432c15b017f99dba06d5937389c1f9546 Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Sat, 11 Jul 2020 15:22:46 +0200 Subject: Upgrade Chalk --- crates/ra_hir_ty/src/traits/chalk/interner.rs | 15 +++++++++++++++ crates/ra_hir_ty/src/traits/chalk/mapping.rs | 15 ++++++++++----- crates/ra_hir_ty/src/traits/chalk/tls.rs | 2 +- 3 files changed, 26 insertions(+), 6 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 15426b022..156b691b4 100644 --- a/crates/ra_hir_ty/src/traits/chalk/interner.rs +++ b/crates/ra_hir_ty/src/traits/chalk/interner.rs @@ -39,6 +39,7 @@ impl chalk_ir::interner::Interner for Interner { type InternedQuantifiedWhereClauses = Vec>; type InternedVariableKinds = Vec>; type InternedCanonicalVarKinds = Vec>; + type InternedConstraints = Vec>>; type DefId = InternId; type InternedAdtId = crate::TypeCtorId; type Identifier = TypeAliasId; @@ -349,6 +350,20 @@ impl chalk_ir::interner::Interner for Interner { ) -> &'a [chalk_ir::CanonicalVarKind] { &canonical_var_kinds } + + fn intern_constraints( + &self, + data: impl IntoIterator>, E>>, + ) -> Result { + data.into_iter().collect() + } + + fn constraints_data<'a>( + &self, + constraints: &'a Self::InternedConstraints, + ) -> &'a [chalk_ir::InEnvironment>] { + constraints + } } 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 433d6aa03..bc0c6de17 100644 --- a/crates/ra_hir_ty/src/traits/chalk/mapping.rs +++ b/crates/ra_hir_ty/src/traits/chalk/mapping.rs @@ -61,7 +61,7 @@ impl ToChalk for Ty { Ty::Bound(idx) => chalk_ir::TyData::BoundVar(idx).intern(&Interner), Ty::Infer(_infer_ty) => panic!("uncanonicalized infer ty"), Ty::Dyn(predicates) => { - let where_clauses = chalk_ir::QuantifiedWhereClauses::from( + let where_clauses = chalk_ir::QuantifiedWhereClauses::from_iter( &Interner, predicates.iter().filter(|p| !p.is_error()).cloned().map(|p| p.to_chalk(db)), ); @@ -152,7 +152,7 @@ fn ref_to_chalk( let lifetime = LIFETIME_PLACEHOLDER.to_lifetime(&Interner); chalk_ir::ApplicationTy { name: TypeName::Ref(mutability.to_chalk(db)), - substitution: chalk_ir::Substitution::from( + substitution: chalk_ir::Substitution::from_iter( &Interner, vec![lifetime.cast(&Interner), arg.cast(&Interner)], ), @@ -177,7 +177,7 @@ impl ToChalk for Substs { type Chalk = chalk_ir::Substitution; fn to_chalk(self, db: &dyn HirDatabase) -> chalk_ir::Substitution { - chalk_ir::Substitution::from(&Interner, self.iter().map(|ty| ty.clone().to_chalk(db))) + chalk_ir::Substitution::from_iter(&Interner, self.iter().map(|ty| ty.clone().to_chalk(db))) } fn from_chalk(db: &dyn HirDatabase, parameters: chalk_ir::Substitution) -> Substs { @@ -492,6 +492,11 @@ impl ToChalk for GenericPredicate { // we shouldn't get these from Chalk panic!("encountered LifetimeOutlives from Chalk") } + + chalk_ir::WhereClause::TypeOutlives(_) => { + // we shouldn't get these from Chalk + panic!("encountered TypeOutlives from Chalk") + } } } } @@ -570,7 +575,7 @@ where ) }); let value = self.value.to_chalk(db); - chalk_ir::Canonical { value, binders: chalk_ir::CanonicalVarKinds::from(&Interner, kinds) } + chalk_ir::Canonical { value, binders: chalk_ir::CanonicalVarKinds::from_iter(&Interner, kinds) } } fn from_chalk(db: &dyn HirDatabase, canonical: chalk_ir::Canonical) -> Canonical { @@ -691,7 +696,7 @@ where T: HasInterner, { chalk_ir::Binders::new( - chalk_ir::VariableKinds::from( + chalk_ir::VariableKinds::from_iter( &Interner, std::iter::repeat(chalk_ir::VariableKind::Ty(chalk_ir::TyKind::General)).take(num_vars), ), diff --git a/crates/ra_hir_ty/src/traits/chalk/tls.rs b/crates/ra_hir_ty/src/traits/chalk/tls.rs index e6a9d3211..1e226baea 100644 --- a/crates/ra_hir_ty/src/traits/chalk/tls.rs +++ b/crates/ra_hir_ty/src/traits/chalk/tls.rs @@ -157,7 +157,7 @@ impl DebugContext<'_> { _ => panic!("associated type not in trait"), }; let trait_data = self.0.trait_data(trait_); - let params = projection_ty.substitution.parameters(&Interner); + let params = projection_ty.substitution.as_slice(&Interner); write!(fmt, "<{:?} as {}", ¶ms[0], trait_data.name,)?; if params.len() > 1 { write!( -- cgit v1.2.3 From c82f5379de49344eb418cc6aaf5bf8c35bc4aaef Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Fri, 10 Jul 2020 18:30:32 +0200 Subject: Enable Chalk tracing in hir_ty tests --- crates/ra_hir_ty/src/traits/chalk/mapping.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (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 bc0c6de17..848cb6e7d 100644 --- a/crates/ra_hir_ty/src/traits/chalk/mapping.rs +++ b/crates/ra_hir_ty/src/traits/chalk/mapping.rs @@ -575,7 +575,10 @@ where ) }); let value = self.value.to_chalk(db); - chalk_ir::Canonical { value, binders: chalk_ir::CanonicalVarKinds::from_iter(&Interner, kinds) } + chalk_ir::Canonical { + value, + binders: chalk_ir::CanonicalVarKinds::from_iter(&Interner, kinds), + } } fn from_chalk(db: &dyn HirDatabase, canonical: chalk_ir::Canonical) -> Canonical { -- cgit v1.2.3 From 2a4166501d8990d3a489e89af3d92002540c288c Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Sat, 11 Jul 2020 16:29:09 +0200 Subject: Remove built-in Unsize impls They exist in Chalk now. --- crates/ra_hir_ty/src/traits/chalk/mapping.rs | 1 + 1 file changed, 1 insertion(+) (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 848cb6e7d..7bdb6264e 100644 --- a/crates/ra_hir_ty/src/traits/chalk/mapping.rs +++ b/crates/ra_hir_ty/src/traits/chalk/mapping.rs @@ -271,6 +271,7 @@ impl ToChalk for TypeCtor { } TypeCtor::Never => TypeName::Never, + // FIXME convert these TypeCtor::Adt(_) | TypeCtor::Array | TypeCtor::FnPtr { .. } -- cgit v1.2.3 From 94f5f69ff4b3185e559339c86f9528b9443ca67d Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Sat, 11 Jul 2020 18:33:50 +0200 Subject: Use Chalk built-in representation for array types --- crates/ra_hir_ty/src/traits/chalk/mapping.rs | 43 +++++++++++++++++++++++----- 1 file changed, 36 insertions(+), 7 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 7bdb6264e..7dc9ee759 100644 --- a/crates/ra_hir_ty/src/traits/chalk/mapping.rs +++ b/crates/ra_hir_ty/src/traits/chalk/mapping.rs @@ -29,6 +29,7 @@ impl ToChalk for Ty { match self { Ty::Apply(apply_ty) => match apply_ty.ctor { TypeCtor::Ref(m) => ref_to_chalk(db, m, apply_ty.parameters), + TypeCtor::Array => array_to_chalk(db, apply_ty.parameters), TypeCtor::FnPtr { num_args: _ } => { let substitution = apply_ty.parameters.to_chalk(db).shifted_in(&Interner); chalk_ir::TyData::Function(chalk_ir::Fn { num_binders: 0, substitution }) @@ -67,7 +68,7 @@ impl ToChalk for Ty { ); let bounded_ty = chalk_ir::DynTy { bounds: make_binders(where_clauses, 1), - lifetime: LIFETIME_PLACEHOLDER.to_lifetime(&Interner), + lifetime: FAKE_PLACEHOLDER.to_lifetime(&Interner), }; chalk_ir::TyData::Dyn(bounded_ty).intern(&Interner) } @@ -92,6 +93,7 @@ impl ToChalk for Ty { chalk_ir::TyData::Apply(apply_ty) => match apply_ty.name { TypeName::Error => Ty::Unknown, TypeName::Ref(m) => ref_from_chalk(db, m, apply_ty.substitution), + TypeName::Array => array_from_chalk(db, apply_ty.substitution), _ => { let ctor = from_chalk(db, apply_ty.name); let parameters = from_chalk(db, apply_ty.substitution); @@ -138,7 +140,7 @@ impl ToChalk for Ty { } } -const LIFETIME_PLACEHOLDER: PlaceholderIndex = +const FAKE_PLACEHOLDER: PlaceholderIndex = PlaceholderIndex { ui: UniverseIndex::ROOT, idx: usize::MAX }; /// We currently don't model lifetimes, but Chalk does. So, we have to insert a @@ -149,7 +151,7 @@ fn ref_to_chalk( subst: Substs, ) -> chalk_ir::Ty { let arg = subst[0].clone().to_chalk(db); - let lifetime = LIFETIME_PLACEHOLDER.to_lifetime(&Interner); + let lifetime = FAKE_PLACEHOLDER.to_lifetime(&Interner); chalk_ir::ApplicationTy { name: TypeName::Ref(mutability.to_chalk(db)), substitution: chalk_ir::Substitution::from_iter( @@ -173,6 +175,35 @@ fn ref_from_chalk( Ty::apply(TypeCtor::Ref(from_chalk(db, mutability)), Substs(tys)) } +/// We currently don't model constants, but Chalk does. So, we have to insert a +/// fake constant here, because Chalks built-in logic may expect it to be there. +fn array_to_chalk(db: &dyn HirDatabase, subst: Substs) -> chalk_ir::Ty { + let arg = subst[0].clone().to_chalk(db); + let usize_ty = chalk_ir::ApplicationTy { + name: TypeName::Scalar(Scalar::Uint(chalk_ir::UintTy::Usize)), + substitution: chalk_ir::Substitution::empty(&Interner), + } + .intern(&Interner); + let const_ = FAKE_PLACEHOLDER.to_const(&Interner, usize_ty); + chalk_ir::ApplicationTy { + name: TypeName::Array, + substitution: chalk_ir::Substitution::from_iter( + &Interner, + vec![arg.cast(&Interner), const_.cast(&Interner)], + ), + } + .intern(&Interner) +} + +/// Here we remove the const from the type we got from Chalk. +fn array_from_chalk(db: &dyn HirDatabase, subst: chalk_ir::Substitution) -> Ty { + let tys = subst + .iter(&Interner) + .filter_map(|p| Some(from_chalk(db, p.ty(&Interner)?.clone()))) + .collect(); + Ty::apply(TypeCtor::Array, Substs(tys)) +} + impl ToChalk for Substs { type Chalk = chalk_ir::Substitution; @@ -263,6 +294,7 @@ impl ToChalk for TypeCtor { TypeCtor::Tuple { cardinality } => TypeName::Tuple(cardinality.into()), TypeCtor::RawPtr(mutability) => TypeName::Raw(mutability.to_chalk(db)), TypeCtor::Slice => TypeName::Slice, + TypeCtor::Array => TypeName::Array, TypeCtor::Ref(mutability) => TypeName::Ref(mutability.to_chalk(db)), TypeCtor::Str => TypeName::Str, TypeCtor::FnDef(callable_def) => { @@ -272,10 +304,7 @@ impl ToChalk for TypeCtor { TypeCtor::Never => TypeName::Never, // FIXME convert these - TypeCtor::Adt(_) - | TypeCtor::Array - | TypeCtor::FnPtr { .. } - | TypeCtor::Closure { .. } => { + TypeCtor::Adt(_) | TypeCtor::FnPtr { .. } | TypeCtor::Closure { .. } => { // other TypeCtors get interned and turned into a chalk StructId let struct_id = db.intern_type_ctor(self).into(); TypeName::Adt(struct_id) -- cgit v1.2.3 From a09d48380204fa948a3af397dc2188b93bf5793f Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Tue, 14 Jul 2020 18:23:45 +0200 Subject: Thread varargs through r-a --- crates/ra_hir_ty/src/traits/chalk/mapping.rs | 7 +++++-- 1 file changed, 5 insertions(+), 2 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 06453ef82..5ba2ff51b 100644 --- a/crates/ra_hir_ty/src/traits/chalk/mapping.rs +++ b/crates/ra_hir_ty/src/traits/chalk/mapping.rs @@ -30,7 +30,7 @@ impl ToChalk for Ty { Ty::Apply(apply_ty) => match apply_ty.ctor { TypeCtor::Ref(m) => ref_to_chalk(db, m, apply_ty.parameters), TypeCtor::Array => array_to_chalk(db, apply_ty.parameters), - TypeCtor::FnPtr { num_args: _ } => { + TypeCtor::FnPtr { num_args: _, is_varargs: _ } => { let substitution = apply_ty.parameters.to_chalk(db).shifted_in(&Interner); chalk_ir::TyData::Function(chalk_ir::Fn { num_binders: 0, substitution }) .intern(&Interner) @@ -124,7 +124,10 @@ impl ToChalk for Ty { substitution.shifted_out(&Interner).expect("fn ptr should have no binders"), ); Ty::Apply(ApplicationTy { - ctor: TypeCtor::FnPtr { num_args: (parameters.len() - 1) as u16 }, + ctor: TypeCtor::FnPtr { + num_args: (parameters.len() - 1) as u16, + is_varargs: false, + }, parameters, }) } -- cgit v1.2.3 From fdce4d9f5140085c6c362ecbcf837f1b6a7d50ca Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Tue, 14 Jul 2020 18:59:07 +0200 Subject: Add FIXME --- crates/ra_hir_ty/src/traits/chalk/mapping.rs | 1 + 1 file changed, 1 insertion(+) (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 5ba2ff51b..3ebb55f77 100644 --- a/crates/ra_hir_ty/src/traits/chalk/mapping.rs +++ b/crates/ra_hir_ty/src/traits/chalk/mapping.rs @@ -31,6 +31,7 @@ impl ToChalk for Ty { TypeCtor::Ref(m) => ref_to_chalk(db, m, apply_ty.parameters), TypeCtor::Array => array_to_chalk(db, apply_ty.parameters), TypeCtor::FnPtr { num_args: _, is_varargs: _ } => { + // FIXME: handle is_varargs let substitution = apply_ty.parameters.to_chalk(db).shifted_in(&Interner); chalk_ir::TyData::Function(chalk_ir::Fn { num_binders: 0, substitution }) .intern(&Interner) -- cgit v1.2.3 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 From b5ce84b17023d27f4e96ec7911aca712db0e000b Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 16 Jul 2020 13:15:00 +0200 Subject: Align CallableDefId naming with other ids --- crates/ra_hir_ty/src/traits/chalk/mapping.rs | 6 +++--- crates/ra_hir_ty/src/traits/chalk/tls.rs | 24 ++++++++++++------------ 2 files changed, 15 insertions(+), 15 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 a852ce2ac..09d8347ca 100644 --- a/crates/ra_hir_ty/src/traits/chalk/mapping.rs +++ b/crates/ra_hir_ty/src/traits/chalk/mapping.rs @@ -16,7 +16,7 @@ use crate::{ db::HirDatabase, primitive::{FloatBitness, FloatTy, IntBitness, IntTy, Signedness}, traits::{Canonical, Obligation}, - ApplicationTy, CallableDef, GenericPredicate, InEnvironment, OpaqueTy, OpaqueTyId, + ApplicationTy, CallableDefId, GenericPredicate, InEnvironment, OpaqueTy, OpaqueTyId, ProjectionPredicate, ProjectionTy, Substs, TraitEnvironment, TraitRef, Ty, TyKind, TypeCtor, }; @@ -454,14 +454,14 @@ impl ToChalk for hir_def::ImplId { } } -impl ToChalk for CallableDef { +impl ToChalk for CallableDefId { type Chalk = FnDefId; fn to_chalk(self, db: &dyn HirDatabase) -> FnDefId { db.intern_callable_def(self).into() } - fn from_chalk(db: &dyn HirDatabase, fn_def_id: FnDefId) -> CallableDef { + fn from_chalk(db: &dyn HirDatabase, fn_def_id: FnDefId) -> CallableDefId { db.lookup_intern_callable_def(fn_def_id.into()) } } diff --git a/crates/ra_hir_ty/src/traits/chalk/tls.rs b/crates/ra_hir_ty/src/traits/chalk/tls.rs index 1e226baea..db915625c 100644 --- a/crates/ra_hir_ty/src/traits/chalk/tls.rs +++ b/crates/ra_hir_ty/src/traits/chalk/tls.rs @@ -5,7 +5,7 @@ use chalk_ir::{AliasTy, GenericArg, Goal, Goals, Lifetime, ProgramClauseImplicat use itertools::Itertools; use super::{from_chalk, Interner}; -use crate::{db::HirDatabase, CallableDef, TypeCtor}; +use crate::{db::HirDatabase, CallableDefId, TypeCtor}; use hir_def::{AdtId, AssocContainerId, DefWithBodyId, Lookup, TypeAliasId}; pub use unsafe_tls::{set_current_program, with_current_program}; @@ -38,16 +38,16 @@ impl DebugContext<'_> { } TypeCtor::FnDef(def) => { let name = match def { - CallableDef::FunctionId(ff) => self.0.function_data(ff).name.clone(), - CallableDef::StructId(s) => self.0.struct_data(s).name.clone(), - CallableDef::EnumVariantId(e) => { + CallableDefId::FunctionId(ff) => self.0.function_data(ff).name.clone(), + CallableDefId::StructId(s) => self.0.struct_data(s).name.clone(), + CallableDefId::EnumVariantId(e) => { let enum_data = self.0.enum_data(e.parent); enum_data.variants[e.local_id].name.clone() } }; match def { - CallableDef::FunctionId(_) => write!(f, "{{fn {}}}", name)?, - CallableDef::StructId(_) | CallableDef::EnumVariantId(_) => { + CallableDefId::FunctionId(_) => write!(f, "{{fn {}}}", name)?, + CallableDefId::StructId(_) | CallableDefId::EnumVariantId(_) => { write!(f, "{{ctor {}}}", name)? } } @@ -255,18 +255,18 @@ impl DebugContext<'_> { fn_def_id: chalk_ir::FnDefId, fmt: &mut fmt::Formatter<'_>, ) -> Result<(), fmt::Error> { - let def: CallableDef = from_chalk(self.0, fn_def_id); + let def: CallableDefId = from_chalk(self.0, fn_def_id); let name = match def { - CallableDef::FunctionId(ff) => self.0.function_data(ff).name.clone(), - CallableDef::StructId(s) => self.0.struct_data(s).name.clone(), - CallableDef::EnumVariantId(e) => { + CallableDefId::FunctionId(ff) => self.0.function_data(ff).name.clone(), + CallableDefId::StructId(s) => self.0.struct_data(s).name.clone(), + CallableDefId::EnumVariantId(e) => { let enum_data = self.0.enum_data(e.parent); enum_data.variants[e.local_id].name.clone() } }; match def { - CallableDef::FunctionId(_) => write!(fmt, "{{fn {}}}", name), - CallableDef::StructId(_) | CallableDef::EnumVariantId(_) => { + CallableDefId::FunctionId(_) => write!(fmt, "{{fn {}}}", name), + CallableDefId::StructId(_) | CallableDefId::EnumVariantId(_) => { write!(fmt, "{{ctor {}}}", name) } } -- cgit v1.2.3 From 797cdb00d91a221d62438b23dfd625a78163a58d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lauren=C8=9Biu=20Nicola?= Date: Thu, 30 Jul 2020 20:37:28 +0300 Subject: Bump chalk --- crates/ra_hir_ty/src/traits/chalk/mapping.rs | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 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 09d8347ca..b3e92993d 100644 --- a/crates/ra_hir_ty/src/traits/chalk/mapping.rs +++ b/crates/ra_hir_ty/src/traits/chalk/mapping.rs @@ -30,11 +30,16 @@ impl ToChalk for Ty { Ty::Apply(apply_ty) => match apply_ty.ctor { TypeCtor::Ref(m) => ref_to_chalk(db, m, apply_ty.parameters), TypeCtor::Array => array_to_chalk(db, apply_ty.parameters), - TypeCtor::FnPtr { num_args: _, is_varargs: _ } => { - // FIXME: handle is_varargs + TypeCtor::FnPtr { num_args: _, is_varargs } => { let substitution = apply_ty.parameters.to_chalk(db).shifted_in(&Interner); - chalk_ir::TyData::Function(chalk_ir::Fn { num_binders: 0, substitution }) - .intern(&Interner) + chalk_ir::TyData::Function(chalk_ir::FnPointer { + num_binders: 0, + abi: (), + safety: chalk_ir::Safety::Safe, + variadic: is_varargs, + substitution, + }) + .intern(&Interner) } _ => { let name = apply_ty.ctor.to_chalk(db); @@ -118,7 +123,12 @@ impl ToChalk for Ty { let parameters = from_chalk(db, opaque_ty.substitution); Ty::Opaque(OpaqueTy { opaque_ty_id: impl_trait_id, parameters }) } - chalk_ir::TyData::Function(chalk_ir::Fn { num_binders, substitution }) => { + chalk_ir::TyData::Function(chalk_ir::FnPointer { + num_binders, + variadic, + substitution, + .. + }) => { assert_eq!(num_binders, 0); let parameters: Substs = from_chalk( db, @@ -127,7 +137,7 @@ impl ToChalk for Ty { Ty::Apply(ApplicationTy { ctor: TypeCtor::FnPtr { num_args: (parameters.len() - 1) as u16, - is_varargs: false, + is_varargs: variadic, }, parameters, }) -- cgit v1.2.3