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.rs | 8 +++--- crates/ra_hir_ty/src/traits/chalk.rs | 36 ++++++++++++++++++++++++--- 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 +- 5 files changed, 63 insertions(+), 13 deletions(-) (limited to 'crates/ra_hir_ty/src') diff --git a/crates/ra_hir_ty/src/traits.rs b/crates/ra_hir_ty/src/traits.rs index 2a6d7faef..786f758e9 100644 --- a/crates/ra_hir_ty/src/traits.rs +++ b/crates/ra_hir_ty/src/traits.rs @@ -2,6 +2,7 @@ use std::sync::Arc; use chalk_ir::cast::Cast; +use chalk_solve::Solver; use hir_def::{ expr::ExprId, lang_item::LangItemTarget, DefWithBodyId, ImplId, TraitId, TypeAliasId, }; @@ -32,9 +33,10 @@ struct ChalkContext<'a> { krate: CrateId, } -fn create_chalk_solver() -> chalk_solve::Solver { - let solver_choice = chalk_solve::SolverChoice::recursive(); - solver_choice.into_solver() +fn create_chalk_solver() -> chalk_recursive::RecursiveSolver { + let overflow_depth = 100; + let caching_enabled = true; + chalk_recursive::RecursiveSolver::new(overflow_depth, caching_enabled) } /// A set of clauses that we assume to be true. E.g. if we are inside this function: diff --git a/crates/ra_hir_ty/src/traits/chalk.rs b/crates/ra_hir_ty/src/traits/chalk.rs index c97b81d57..a9b39474a 100644 --- a/crates/ra_hir_ty/src/traits/chalk.rs +++ b/crates/ra_hir_ty/src/traits/chalk.rs @@ -3,7 +3,7 @@ use std::sync::Arc; use log::debug; -use chalk_ir::{fold::shift::Shift, GenericArg, TypeName}; +use chalk_ir::{fold::shift::Shift, GenericArg, TypeName, CanonicalVarKinds}; use chalk_solve::rust_ir::{self, OpaqueTyDatumBound, WellKnownTrait}; use hir_def::{ @@ -66,10 +66,13 @@ impl<'a> chalk_solve::RustIrDatabase for ChalkContext<'a> { &self, trait_id: TraitId, parameters: &[GenericArg], + _binders: &CanonicalVarKinds, ) -> Vec { debug!("impls_for_trait {:?}", trait_id); let trait_: hir_def::TraitId = from_chalk(self.db, trait_id); + // FIXME use binders to look for int/float impls when necessary + let ty: Ty = from_chalk(self.db, parameters[0].assert_ty_ref(&Interner).clone()); let self_ty_fp = TyFingerprint::for_impl(&ty); @@ -219,6 +222,22 @@ impl<'a> chalk_solve::RustIrDatabase for ChalkContext<'a> { // FIXME: implement closure support unimplemented!() } + + fn trait_name(&self, _trait_id: chalk_ir::TraitId) -> String { + unimplemented!() + } + fn adt_name(&self, _struct_id: chalk_ir::AdtId) -> String { + unimplemented!() + } + fn assoc_type_name(&self, _assoc_ty_id: chalk_ir::AssocTypeId) -> String { + unimplemented!() + } + fn opaque_type_name(&self, _opaque_ty_id: chalk_ir::OpaqueTyId) -> String { + unimplemented!() + } + fn fn_def_name(&self, _fn_def_id: chalk_ir::FnDefId) -> String { + unimplemented!() + } } pub(crate) fn program_clauses_for_chalk_env_query( @@ -354,12 +373,21 @@ pub(crate) fn struct_datum_query( fundamental: false, phantom_data: false, }; + // FIXME provide enum variants properly (for auto traits) + let variant = rust_ir::AdtVariantDatum { + fields: Vec::new(), // FIXME add fields (only relevant for auto traits), + }; let struct_datum_bound = rust_ir::AdtDatumBound { - fields: Vec::new(), // FIXME add fields (only relevant for auto traits) + variants: vec![variant], where_clauses, }; - let struct_datum = - StructDatum { id: struct_id, binders: make_binders(struct_datum_bound, num_params), flags }; + let struct_datum = StructDatum { + // FIXME set ADT kind + kind: rust_ir::AdtKind::Struct, + id: struct_id, + binders: make_binders(struct_datum_bound, num_params), + flags + }; Arc::new(struct_datum) } 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