Upgrade chalk to 0.21

This commit is contained in:
Jack Huey 2020-08-04 18:35:37 -04:00
parent 42d896afbd
commit d66452c3e5
11 changed files with 190 additions and 120 deletions

View file

@ -11,6 +11,7 @@ use rustc_middle::ty::subst::{InternalSubsts, Subst, SubstsRef};
use rustc_middle::ty::{self, AssocItemContainer, AssocKind, TyCtxt};
use rustc_hir::def_id::DefId;
use rustc_hir::Unsafety;
use rustc_span::symbol::sym;
@ -145,10 +146,11 @@ impl<'tcx> chalk_solve::RustIrDatabase<RustInterner<'tcx>> for RustIrDatabase<'t
.map(|(wc, _)| wc.subst(self.tcx, bound_vars))
.filter_map(|wc| LowerInto::<Option<chalk_ir::QuantifiedWhereClause<RustInterner<'tcx>>>>::lower_into(wc, &self.interner))
.collect();
let fields = match adt_def.adt_kind() {
ty::AdtKind::Struct | ty::AdtKind::Union => {
let variant = adt_def.non_enum_variant();
variant
let variants: Vec<_> = adt_def
.variants
.iter()
.map(|variant| chalk_solve::rust_ir::AdtVariantDatum {
fields: variant
.fields
.iter()
.map(|field| {
@ -157,26 +159,40 @@ impl<'tcx> chalk_solve::RustIrDatabase<RustInterner<'tcx>> for RustIrDatabase<'t
.subst(self.tcx, bound_vars)
.lower_into(&self.interner)
})
.collect()
}
// FIXME(chalk): handle enums; force_impl_for requires this
ty::AdtKind::Enum => vec![],
};
.collect(),
})
.collect();
let struct_datum = Arc::new(chalk_solve::rust_ir::AdtDatum {
id: adt_id,
binders: chalk_ir::Binders::new(
binders,
chalk_solve::rust_ir::AdtDatumBound { fields, where_clauses },
chalk_solve::rust_ir::AdtDatumBound { variants, where_clauses },
),
flags: chalk_solve::rust_ir::AdtFlags {
upstream: !adt_def.did.is_local(),
fundamental: adt_def.is_fundamental(),
phantom_data: adt_def.is_phantom_data(),
},
kind: match adt_def.adt_kind() {
ty::AdtKind::Struct => chalk_solve::rust_ir::AdtKind::Struct,
ty::AdtKind::Union => chalk_solve::rust_ir::AdtKind::Union,
ty::AdtKind::Enum => chalk_solve::rust_ir::AdtKind::Enum,
},
});
struct_datum
}
fn adt_repr(
&self,
adt_id: chalk_ir::AdtId<RustInterner<'tcx>>,
) -> chalk_solve::rust_ir::AdtRepr {
let adt_def = adt_id.0;
chalk_solve::rust_ir::AdtRepr {
repr_c: adt_def.repr.c(),
repr_packed: adt_def.repr.packed(),
}
}
fn fn_def_datum(
&self,
fn_def_id: chalk_ir::FnDefId<RustInterner<'tcx>>,
@ -218,6 +234,11 @@ impl<'tcx> chalk_solve::RustIrDatabase<RustInterner<'tcx>> for RustIrDatabase<'t
Arc::new(chalk_solve::rust_ir::FnDefDatum {
id: fn_def_id,
abi: sig.abi(),
safety: match sig.unsafety() {
Unsafety::Normal => chalk_ir::Safety::Safe,
Unsafety::Unsafe => chalk_ir::Safety::Unsafe,
},
variadic: sig.c_variadic(),
binders: chalk_ir::Binders::new(binders, bound),
})
}
@ -256,6 +277,7 @@ impl<'tcx> chalk_solve::RustIrDatabase<RustInterner<'tcx>> for RustIrDatabase<'t
&self,
trait_id: chalk_ir::TraitId<RustInterner<'tcx>>,
parameters: &[chalk_ir::GenericArg<RustInterner<'tcx>>],
_binders: &chalk_ir::CanonicalVarKinds<RustInterner<'tcx>>,
) -> Vec<chalk_ir::ImplId<RustInterner<'tcx>>> {
let def_id = trait_id.0;
@ -355,69 +377,15 @@ impl<'tcx> chalk_solve::RustIrDatabase<RustInterner<'tcx>> for RustIrDatabase<'t
.filter_map(|wc| LowerInto::<Option<chalk_ir::QuantifiedWhereClause<RustInterner<'tcx>>>>::lower_into(wc, &self.interner)).collect();
let value = chalk_solve::rust_ir::OpaqueTyDatumBound {
bounds: chalk_ir::Binders::new(binders, where_clauses),
bounds: chalk_ir::Binders::new(binders.clone(), vec![]),
where_clauses: chalk_ir::Binders::new(binders, where_clauses),
};
Arc::new(chalk_solve::rust_ir::OpaqueTyDatum {
opaque_ty_id,
bound: chalk_ir::Binders::new(chalk_ir::VariableKinds::new(&self.interner), value),
bound: chalk_ir::Binders::new(chalk_ir::VariableKinds::empty(&self.interner), value),
})
}
/// Since Chalk can't handle all Rust types currently, we have to handle
/// some specially for now. Over time, these `Some` returns will change to
/// `None` and eventually this function will be removed.
fn force_impl_for(
&self,
well_known: chalk_solve::rust_ir::WellKnownTrait,
ty: &chalk_ir::TyData<RustInterner<'tcx>>,
) -> Option<bool> {
use chalk_ir::TyData::*;
match well_known {
chalk_solve::rust_ir::WellKnownTrait::Sized => match ty {
Apply(apply) => match apply.name {
chalk_ir::TypeName::Adt(chalk_ir::AdtId(adt_def)) => match adt_def.adt_kind() {
ty::AdtKind::Struct | ty::AdtKind::Union => None,
ty::AdtKind::Enum => {
let constraint = self.tcx.adt_sized_constraint(adt_def.did);
if !constraint.0.is_empty() { unimplemented!() } else { Some(true) }
}
},
_ => None,
},
Dyn(_)
| Alias(_)
| Placeholder(_)
| Function(_)
| InferenceVar(_, _)
| BoundVar(_) => None,
},
chalk_solve::rust_ir::WellKnownTrait::Copy
| chalk_solve::rust_ir::WellKnownTrait::Clone => match ty {
Apply(apply) => match apply.name {
chalk_ir::TypeName::Adt(chalk_ir::AdtId(adt_def)) => match adt_def.adt_kind() {
ty::AdtKind::Struct | ty::AdtKind::Union => None,
ty::AdtKind::Enum => {
let constraint = self.tcx.adt_sized_constraint(adt_def.did);
if !constraint.0.is_empty() { unimplemented!() } else { Some(true) }
}
},
_ => None,
},
Dyn(_)
| Alias(_)
| Placeholder(_)
| Function(_)
| InferenceVar(_, _)
| BoundVar(_) => None,
},
chalk_solve::rust_ir::WellKnownTrait::Drop => None,
chalk_solve::rust_ir::WellKnownTrait::Fn => None,
chalk_solve::rust_ir::WellKnownTrait::FnMut => None,
chalk_solve::rust_ir::WellKnownTrait::FnOnce => None,
chalk_solve::rust_ir::WellKnownTrait::Unsize => None,
}
}
fn program_clauses_for_env(
&self,
environment: &chalk_ir::Environment<RustInterner<'tcx>>,
@ -460,7 +428,7 @@ impl<'tcx> chalk_solve::RustIrDatabase<RustInterner<'tcx>> for RustIrDatabase<'t
_closure_id: chalk_ir::ClosureId<RustInterner<'tcx>>,
substs: &chalk_ir::Substitution<RustInterner<'tcx>>,
) -> chalk_solve::rust_ir::ClosureKind {
let kind = &substs.parameters(&self.interner)[substs.len(&self.interner) - 3];
let kind = &substs.as_slice(&self.interner)[substs.len(&self.interner) - 3];
match kind.assert_ty_ref(&self.interner).data(&self.interner) {
chalk_ir::TyData::Apply(apply) => match apply.name {
chalk_ir::TypeName::Scalar(scalar) => match scalar {
@ -484,10 +452,10 @@ impl<'tcx> chalk_solve::RustIrDatabase<RustInterner<'tcx>> for RustIrDatabase<'t
substs: &chalk_ir::Substitution<RustInterner<'tcx>>,
) -> chalk_ir::Binders<chalk_solve::rust_ir::FnDefInputsAndOutputDatum<RustInterner<'tcx>>>
{
let sig = &substs.parameters(&self.interner)[substs.len(&self.interner) - 2];
let sig = &substs.as_slice(&self.interner)[substs.len(&self.interner) - 2];
match sig.assert_ty_ref(&self.interner).data(&self.interner) {
chalk_ir::TyData::Function(f) => {
let substitution = f.substitution.parameters(&self.interner);
let substitution = f.substitution.as_slice(&self.interner);
let return_type =
substitution.last().unwrap().assert_ty_ref(&self.interner).clone();
// Closure arguments are tupled
@ -506,7 +474,7 @@ impl<'tcx> chalk_solve::RustIrDatabase<RustInterner<'tcx>> for RustIrDatabase<'t
};
chalk_ir::Binders::new(
chalk_ir::VariableKinds::from(
chalk_ir::VariableKinds::from_iter(
&self.interner,
(0..f.num_binders).map(|_| chalk_ir::VariableKind::Lifetime),
),
@ -523,7 +491,7 @@ impl<'tcx> chalk_solve::RustIrDatabase<RustInterner<'tcx>> for RustIrDatabase<'t
substs: &chalk_ir::Substitution<RustInterner<'tcx>>,
) -> chalk_ir::Binders<chalk_ir::Ty<RustInterner<'tcx>>> {
let inputs_and_output = self.closure_inputs_and_output(_closure_id, substs);
let tuple = substs.parameters(&self.interner).last().unwrap().assert_ty_ref(&self.interner);
let tuple = substs.as_slice(&self.interner).last().unwrap().assert_ty_ref(&self.interner);
inputs_and_output.map_ref(|_| tuple.clone())
}
@ -532,8 +500,8 @@ impl<'tcx> chalk_solve::RustIrDatabase<RustInterner<'tcx>> for RustIrDatabase<'t
_closure_id: chalk_ir::ClosureId<RustInterner<'tcx>>,
substs: &chalk_ir::Substitution<RustInterner<'tcx>>,
) -> chalk_ir::Substitution<RustInterner<'tcx>> {
let substitution = &substs.parameters(&self.interner)[0..substs.len(&self.interner) - 3];
chalk_ir::Substitution::from(&self.interner, substitution)
let substitution = &substs.as_slice(&self.interner)[0..substs.len(&self.interner) - 3];
chalk_ir::Substitution::from_iter(&self.interner, substitution)
}
}
@ -573,7 +541,7 @@ fn binders_for<'tcx>(
interner: &RustInterner<'tcx>,
bound_vars: SubstsRef<'tcx>,
) -> chalk_ir::VariableKinds<RustInterner<'tcx>> {
chalk_ir::VariableKinds::from(
chalk_ir::VariableKinds::from_iter(
interner,
bound_vars.iter().map(|arg| match arg.unpack() {
ty::subst::GenericArgKind::Lifetime(_re) => chalk_ir::VariableKind::Lifetime,

View file

@ -56,7 +56,7 @@ impl<'tcx> LowerInto<'tcx, chalk_ir::Substitution<RustInterner<'tcx>>> for Subst
self,
interner: &RustInterner<'tcx>,
) -> chalk_ir::Substitution<RustInterner<'tcx>> {
chalk_ir::Substitution::from(interner, self.iter().map(|s| s.lower_into(interner)))
chalk_ir::Substitution::from_iter(interner, self.iter().map(|s| s.lower_into(interner)))
}
}
@ -94,8 +94,9 @@ impl<'tcx> LowerInto<'tcx, chalk_ir::InEnvironment<chalk_ir::Goal<RustInterner<'
predicate.trait_ref.lower_into(interner),
),
),
conditions: chalk_ir::Goals::new(interner),
conditions: chalk_ir::Goals::empty(interner),
priority: chalk_ir::ClausePriority::High,
constraints: chalk_ir::Constraints::empty(interner),
},
))
.intern(interner),
@ -118,15 +119,39 @@ impl<'tcx> LowerInto<'tcx, chalk_ir::InEnvironment<chalk_ir::Goal<RustInterner<'
},
),
),
conditions: chalk_ir::Goals::new(interner),
conditions: chalk_ir::Goals::empty(interner),
priority: chalk_ir::ClausePriority::High,
constraints: chalk_ir::Constraints::empty(interner),
},
))
.intern(interner),
)
}
ty::PredicateAtom::TypeOutlives(predicate) => {
let predicate = ty::Binder::bind(predicate);
let (predicate, binders, _named_regions) =
collect_bound_vars(interner, interner.tcx, &predicate);
Some(
chalk_ir::ProgramClauseData(chalk_ir::Binders::new(
binders,
chalk_ir::ProgramClauseImplication {
consequence: chalk_ir::DomainGoal::Holds(
chalk_ir::WhereClause::TypeOutlives(
chalk_ir::TypeOutlives {
ty: predicate.0.lower_into(interner),
lifetime: predicate.1.lower_into(interner),
},
),
),
conditions: chalk_ir::Goals::empty(interner),
priority: chalk_ir::ClausePriority::High,
constraints: chalk_ir::Constraints::empty(interner),
},
))
.intern(interner),
)
}
// FIXME(chalk): need to add TypeOutlives
ty::PredicateAtom::TypeOutlives(_) => None,
ty::PredicateAtom::Projection(predicate) => {
let predicate = ty::Binder::bind(predicate);
let (predicate, binders, _named_regions) =
@ -141,8 +166,9 @@ impl<'tcx> LowerInto<'tcx, chalk_ir::InEnvironment<chalk_ir::Goal<RustInterner<'
predicate.lower_into(interner),
),
),
conditions: chalk_ir::Goals::new(interner),
conditions: chalk_ir::Goals::empty(interner),
priority: chalk_ir::ClausePriority::High,
constraints: chalk_ir::Constraints::empty(interner),
},
))
.intern(interner),
@ -160,13 +186,14 @@ impl<'tcx> LowerInto<'tcx, chalk_ir::InEnvironment<chalk_ir::Goal<RustInterner<'
}
ChalkEnvironmentClause::TypeFromEnv(ty) => Some(
chalk_ir::ProgramClauseData(chalk_ir::Binders::new(
chalk_ir::VariableKinds::new(interner),
chalk_ir::VariableKinds::empty(interner),
chalk_ir::ProgramClauseImplication {
consequence: chalk_ir::DomainGoal::FromEnv(chalk_ir::FromEnv::Ty(
ty.lower_into(interner).shifted_in(interner),
)),
conditions: chalk_ir::Goals::new(interner),
conditions: chalk_ir::Goals::empty(interner),
priority: chalk_ir::ClausePriority::High,
constraints: chalk_ir::Constraints::empty(interner),
},
))
.intern(interner),
@ -176,7 +203,7 @@ impl<'tcx> LowerInto<'tcx, chalk_ir::InEnvironment<chalk_ir::Goal<RustInterner<'
let goal: chalk_ir::GoalData<RustInterner<'tcx>> = self.goal.lower_into(&interner);
chalk_ir::InEnvironment {
environment: chalk_ir::Environment {
clauses: chalk_ir::ProgramClauses::from(&interner, clauses),
clauses: chalk_ir::ProgramClauses::from_iter(&interner, clauses),
},
goal: goal.intern(&interner),
}
@ -211,7 +238,7 @@ impl<'tcx> LowerInto<'tcx, chalk_ir::GoalData<RustInterner<'tcx>>> for ty::Predi
}
// FIXME(chalk): TypeOutlives
ty::PredicateAtom::TypeOutlives(_predicate) => {
chalk_ir::GoalData::All(chalk_ir::Goals::new(interner))
chalk_ir::GoalData::All(chalk_ir::Goals::empty(interner))
}
ty::PredicateAtom::Projection(predicate) => {
ty::Binder::bind(predicate).lower_into(interner)
@ -221,7 +248,9 @@ impl<'tcx> LowerInto<'tcx, chalk_ir::GoalData<RustInterner<'tcx>>> for ty::Predi
// FIXME(chalk): In Chalk, a placeholder is WellFormed if it
// `FromEnv`. However, when we "lower" Params, we don't update
// the environment.
ty::Placeholder(..) => chalk_ir::GoalData::All(chalk_ir::Goals::new(interner)),
ty::Placeholder(..) => {
chalk_ir::GoalData::All(chalk_ir::Goals::empty(interner))
}
_ => {
let (ty, binders, _named_regions) =
@ -241,7 +270,7 @@ impl<'tcx> LowerInto<'tcx, chalk_ir::GoalData<RustInterner<'tcx>>> for ty::Predi
},
// FIXME(chalk): handle well formed consts
GenericArgKind::Const(..) => {
chalk_ir::GoalData::All(chalk_ir::Goals::new(interner))
chalk_ir::GoalData::All(chalk_ir::Goals::empty(interner))
}
GenericArgKind::Lifetime(lt) => bug!("unexpect well formed predicate: {:?}", lt),
},
@ -258,7 +287,7 @@ impl<'tcx> LowerInto<'tcx, chalk_ir::GoalData<RustInterner<'tcx>>> for ty::Predi
| ty::PredicateAtom::Subtype(..)
| ty::PredicateAtom::ConstEvaluatable(..)
| ty::PredicateAtom::ConstEquate(..) => {
chalk_ir::GoalData::All(chalk_ir::Goals::new(interner))
chalk_ir::GoalData::All(chalk_ir::Goals::empty(interner))
}
}
}
@ -381,7 +410,7 @@ impl<'tcx> LowerInto<'tcx, chalk_ir::Ty<RustInterner<'tcx>>> for Ty<'tcx> {
};
apply(
chalk_ir::TypeName::Array,
chalk_ir::Substitution::from(
chalk_ir::Substitution::from_iter(
interner,
&[
chalk_ir::GenericArgData::Ty(ty.lower_into(interner)).intern(interner),
@ -415,7 +444,7 @@ impl<'tcx> LowerInto<'tcx, chalk_ir::Ty<RustInterner<'tcx>>> for Ty<'tcx> {
};
apply(
name,
chalk_ir::Substitution::from(
chalk_ir::Substitution::from_iter(
interner,
&[
chalk_ir::GenericArgData::Lifetime(region.lower_into(interner))
@ -432,14 +461,20 @@ impl<'tcx> LowerInto<'tcx, chalk_ir::Ty<RustInterner<'tcx>>> for Ty<'tcx> {
FnPtr(sig) => {
let (inputs_and_outputs, binders, _named_regions) =
collect_bound_vars(interner, interner.tcx, &sig.inputs_and_output());
TyData::Function(chalk_ir::Fn {
TyData::Function(chalk_ir::FnPointer {
num_binders: binders.len(interner),
substitution: chalk_ir::Substitution::from(
substitution: chalk_ir::Substitution::from_iter(
interner,
inputs_and_outputs.iter().map(|ty| {
chalk_ir::GenericArgData::Ty(ty.lower_into(interner)).intern(interner)
}),
),
abi: sig.abi(),
safety: match sig.unsafety() {
rustc_hir::Unsafety::Normal => chalk_ir::Safety::Safe,
rustc_hir::Unsafety::Unsafe => chalk_ir::Safety::Unsafe,
},
variadic: sig.c_variadic(),
})
.intern(interner)
}
@ -604,7 +639,7 @@ impl<'tcx> LowerInto<'tcx, chalk_ir::Binders<chalk_ir::QuantifiedWhereClauses<Ru
let where_clauses = predicates.into_iter().map(|predicate| match predicate {
ty::ExistentialPredicate::Trait(ty::ExistentialTraitRef { def_id, substs }) => {
chalk_ir::Binders::new(
chalk_ir::VariableKinds::new(interner),
chalk_ir::VariableKinds::empty(interner),
chalk_ir::WhereClause::Implemented(chalk_ir::TraitRef {
trait_id: chalk_ir::TraitId(def_id),
substitution: substs.lower_into(interner),
@ -613,14 +648,14 @@ impl<'tcx> LowerInto<'tcx, chalk_ir::Binders<chalk_ir::QuantifiedWhereClauses<Ru
}
ty::ExistentialPredicate::Projection(_predicate) => unimplemented!(),
ty::ExistentialPredicate::AutoTrait(def_id) => chalk_ir::Binders::new(
chalk_ir::VariableKinds::new(interner),
chalk_ir::VariableKinds::empty(interner),
chalk_ir::WhereClause::Implemented(chalk_ir::TraitRef {
trait_id: chalk_ir::TraitId(def_id),
substitution: chalk_ir::Substitution::empty(interner),
}),
),
});
let value = chalk_ir::QuantifiedWhereClauses::from(interner, where_clauses);
let value = chalk_ir::QuantifiedWhereClauses::from_iter(interner, where_clauses);
chalk_ir::Binders::new(binders, value)
}
}
@ -662,7 +697,8 @@ crate fn collect_bound_vars<'a, 'tcx, T: TypeFoldable<'tcx>>(
.or_else(|| bug!("Skipped bound var index: ty={:?}, parameters={:?}", ty, parameters));
});
let binders = chalk_ir::VariableKinds::from(interner, parameters.into_iter().map(|(_, v)| v));
let binders =
chalk_ir::VariableKinds::from_iter(interner, parameters.into_iter().map(|(_, v)| v));
(new_ty, binders, named_parameters)
}

View file

@ -49,7 +49,7 @@ crate fn evaluate_goal<'tcx>(
chalk_ir::InEnvironment<chalk_ir::Goal<ChalkRustInterner<'tcx>>>,
> = chalk_ir::UCanonical {
canonical: chalk_ir::Canonical {
binders: chalk_ir::CanonicalVarKinds::from(
binders: chalk_ir::CanonicalVarKinds::from_iter(
&interner,
obligation.variables.iter().map(|v| match v.kind {
CanonicalVarKind::PlaceholderTy(_ty) => unimplemented!(),
@ -81,11 +81,10 @@ crate fn evaluate_goal<'tcx>(
universes: max_universe + 1,
};
let solver_choice = chalk_solve::SolverChoice::SLG { max_size: 32, expected_answers: None };
let mut solver = solver_choice.into_solver::<ChalkRustInterner<'tcx>>();
use chalk_solve::Solver;
let mut solver = chalk_engine::solve::SLGSolver::new(32, None);
let db = ChalkRustIrDatabase { tcx, interner };
let solution = solver.solve(&db, &_lowered_goal);
let solution = chalk_solve::logging::with_tracing_logs(|| solver.solve(&db, &_lowered_goal));
// Ideally, the code to convert *back* to rustc types would live close to
// the code to convert *from* rustc types. Right now though, we don't
@ -94,7 +93,7 @@ crate fn evaluate_goal<'tcx>(
// `Ambig(Definite)`. This really isn't right.
let make_solution = |_subst: chalk_ir::Substitution<_>| {
let mut var_values: IndexVec<BoundVar, GenericArg<'tcx>> = IndexVec::new();
_subst.parameters(&interner).iter().for_each(|p| {
_subst.as_slice(&interner).iter().for_each(|p| {
// FIXME(chalk): we should move this elsewhere, since this is
// essentially inverse of lowering a `GenericArg`.
let _data = p.data(&interner);