Upgrade Chalk

This commit is contained in:
Florian Diebold 2020-07-11 15:22:46 +02:00
parent ab1ad19f55
commit 209c492432
5 changed files with 63 additions and 13 deletions

View file

@ -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<Interner> {
let solver_choice = chalk_solve::SolverChoice::recursive();
solver_choice.into_solver()
fn create_chalk_solver() -> chalk_recursive::RecursiveSolver<Interner> {
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:

View file

@ -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<Interner> for ChalkContext<'a> {
&self,
trait_id: TraitId,
parameters: &[GenericArg<Interner>],
_binders: &CanonicalVarKinds<Interner>,
) -> Vec<ImplId> {
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<Interner> for ChalkContext<'a> {
// FIXME: implement closure support
unimplemented!()
}
fn trait_name(&self, _trait_id: chalk_ir::TraitId<Interner>) -> String {
unimplemented!()
}
fn adt_name(&self, _struct_id: chalk_ir::AdtId<Interner>) -> String {
unimplemented!()
}
fn assoc_type_name(&self, _assoc_ty_id: chalk_ir::AssocTypeId<Interner>) -> String {
unimplemented!()
}
fn opaque_type_name(&self, _opaque_ty_id: chalk_ir::OpaqueTyId<Interner>) -> String {
unimplemented!()
}
fn fn_def_name(&self, _fn_def_id: chalk_ir::FnDefId<Interner>) -> 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)
}

View file

@ -39,6 +39,7 @@ impl chalk_ir::interner::Interner for Interner {
type InternedQuantifiedWhereClauses = Vec<chalk_ir::QuantifiedWhereClause<Self>>;
type InternedVariableKinds = Vec<chalk_ir::VariableKind<Self>>;
type InternedCanonicalVarKinds = Vec<chalk_ir::CanonicalVarKind<Self>>;
type InternedConstraints = Vec<chalk_ir::InEnvironment<chalk_ir::Constraint<Self>>>;
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<Self>] {
&canonical_var_kinds
}
fn intern_constraints<E>(
&self,
data: impl IntoIterator<Item = Result<chalk_ir::InEnvironment<chalk_ir::Constraint<Self>>, E>>,
) -> Result<Self::InternedConstraints, E> {
data.into_iter().collect()
}
fn constraints_data<'a>(
&self,
constraints: &'a Self::InternedConstraints,
) -> &'a [chalk_ir::InEnvironment<chalk_ir::Constraint<Self>>] {
constraints
}
}
impl chalk_ir::interner::HasInterner for Interner {

View file

@ -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<Interner>;
fn to_chalk(self, db: &dyn HirDatabase) -> chalk_ir::Substitution<Interner> {
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<Interner>) -> 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<T::Chalk>) -> Canonical<T> {
@ -691,7 +696,7 @@ where
T: HasInterner<Interner = Interner>,
{
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),
),

View file

@ -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 {}", &params[0], trait_data.name,)?;
if params.len() > 1 {
write!(