instantiate canonical vars eagerly
This commit is contained in:
parent
b738b06160
commit
bf7dbff921
11 changed files with 509 additions and 496 deletions
|
@ -1,20 +1,11 @@
|
|||
//! Code shared by trait and projection goals for candidate assembly.
|
||||
|
||||
use super::infcx_ext::InferCtxtExt;
|
||||
use super::{
|
||||
instantiate_canonical_query_response, CanonicalGoal, CanonicalResponse, Certainty, EvalCtxt,
|
||||
Goal,
|
||||
};
|
||||
use super::{CanonicalResponse, Certainty, EvalCtxt, Goal};
|
||||
use rustc_hir::def_id::DefId;
|
||||
use rustc_infer::infer::TyCtxtInferExt;
|
||||
use rustc_infer::infer::{
|
||||
canonical::{CanonicalVarValues, OriginalQueryValues},
|
||||
InferCtxt,
|
||||
};
|
||||
use rustc_infer::traits::query::NoSolution;
|
||||
use rustc_middle::ty::TypeFoldable;
|
||||
use rustc_middle::ty::{self, Ty, TyCtxt};
|
||||
use rustc_span::DUMMY_SP;
|
||||
use std::fmt::Debug;
|
||||
|
||||
/// A candidate is a possible way to prove a goal.
|
||||
|
@ -40,7 +31,7 @@ pub(super) trait GoalKind<'tcx>: TypeFoldable<'tcx> + Copy {
|
|||
fn trait_def_id(self, tcx: TyCtxt<'tcx>) -> DefId;
|
||||
|
||||
fn consider_impl_candidate(
|
||||
acx: &mut AssemblyCtxt<'_, 'tcx, Self>,
|
||||
acx: &mut AssemblyCtxt<'_, '_, 'tcx, Self>,
|
||||
goal: Goal<'tcx, Self>,
|
||||
impl_def_id: DefId,
|
||||
);
|
||||
|
@ -49,21 +40,17 @@ pub(super) trait GoalKind<'tcx>: TypeFoldable<'tcx> + Copy {
|
|||
/// An abstraction which correctly deals with the canonical results for candidates.
|
||||
///
|
||||
/// It also deduplicates the behavior between trait and projection predicates.
|
||||
pub(super) struct AssemblyCtxt<'a, 'tcx, G: GoalKind<'tcx>> {
|
||||
pub(super) cx: &'a mut EvalCtxt<'tcx>,
|
||||
pub(super) infcx: &'a InferCtxt<'tcx>,
|
||||
var_values: CanonicalVarValues<'tcx>,
|
||||
pub(super) struct AssemblyCtxt<'a, 'b, 'tcx, G: GoalKind<'tcx>> {
|
||||
pub(super) cx: &'a mut EvalCtxt<'b, 'tcx>,
|
||||
candidates: Vec<Candidate<'tcx, G>>,
|
||||
}
|
||||
|
||||
impl<'a, 'tcx, G: GoalKind<'tcx>> AssemblyCtxt<'a, 'tcx, G> {
|
||||
impl<'a, 'b, 'tcx, G: GoalKind<'tcx>> AssemblyCtxt<'a, 'b, 'tcx, G> {
|
||||
pub(super) fn assemble_and_evaluate_candidates(
|
||||
cx: &'a mut EvalCtxt<'tcx>,
|
||||
goal: CanonicalGoal<'tcx, G>,
|
||||
cx: &'a mut EvalCtxt<'b, 'tcx>,
|
||||
goal: Goal<'tcx, G>,
|
||||
) -> Vec<Candidate<'tcx, G>> {
|
||||
let (ref infcx, goal, var_values) =
|
||||
cx.tcx.infer_ctxt().build_with_canonical(DUMMY_SP, &goal);
|
||||
let mut acx = AssemblyCtxt { cx, infcx, var_values, candidates: Vec::new() };
|
||||
let mut acx = AssemblyCtxt { cx, candidates: Vec::new() };
|
||||
|
||||
acx.assemble_candidates_after_normalizing_self_ty(goal);
|
||||
|
||||
|
@ -77,7 +64,7 @@ impl<'a, 'tcx, G: GoalKind<'tcx>> AssemblyCtxt<'a, 'tcx, G> {
|
|||
source: G::CandidateSource,
|
||||
certainty: Certainty,
|
||||
) {
|
||||
match self.infcx.make_canonical_response(self.var_values.clone(), certainty) {
|
||||
match self.cx.make_canonical_response(certainty) {
|
||||
Ok(result) => self.candidates.push(Candidate { source, result }),
|
||||
Err(NoSolution) => debug!(?source, ?certainty, "failed leakcheck"),
|
||||
}
|
||||
|
@ -89,13 +76,14 @@ impl<'a, 'tcx, G: GoalKind<'tcx>> AssemblyCtxt<'a, 'tcx, G> {
|
|||
/// self type to the list of candidates in case that succeeds. Note that we can't just eagerly return in
|
||||
/// this case as projections as self types add `
|
||||
fn assemble_candidates_after_normalizing_self_ty(&mut self, goal: Goal<'tcx, G>) {
|
||||
let tcx = self.cx.tcx;
|
||||
let tcx = self.cx.tcx();
|
||||
let infcx = self.cx.infcx;
|
||||
// FIXME: We also have to normalize opaque types, not sure where to best fit that in.
|
||||
let &ty::Alias(ty::Projection, projection_ty) = goal.predicate.self_ty().kind() else {
|
||||
return
|
||||
};
|
||||
self.infcx.probe(|_| {
|
||||
let normalized_ty = self.infcx.next_ty_infer();
|
||||
infcx.probe(|_| {
|
||||
let normalized_ty = infcx.next_ty_infer();
|
||||
let normalizes_to_goal = goal.with(
|
||||
tcx,
|
||||
ty::Binder::dummy(ty::ProjectionPredicate {
|
||||
|
@ -103,43 +91,31 @@ impl<'a, 'tcx, G: GoalKind<'tcx>> AssemblyCtxt<'a, 'tcx, G> {
|
|||
term: normalized_ty.into(),
|
||||
}),
|
||||
);
|
||||
let normalization_certainty =
|
||||
match self.cx.evaluate_goal(&self.infcx, normalizes_to_goal) {
|
||||
Ok((_, certainty)) => certainty,
|
||||
Err(NoSolution) => return,
|
||||
};
|
||||
let normalization_certainty = match self.cx.evaluate_goal(normalizes_to_goal) {
|
||||
Ok((_, certainty)) => certainty,
|
||||
Err(NoSolution) => return,
|
||||
};
|
||||
|
||||
// NOTE: Alternatively we could call `evaluate_goal` here and only have a `Normalized` candidate.
|
||||
// This doesn't work as long as we use `CandidateSource` in both winnowing and to resolve associated items.
|
||||
let goal = goal.with(tcx, goal.predicate.with_self_ty(tcx, normalized_ty));
|
||||
let mut orig_values = OriginalQueryValues::default();
|
||||
let goal = self.infcx.canonicalize_query(goal, &mut orig_values);
|
||||
let normalized_candidates =
|
||||
AssemblyCtxt::assemble_and_evaluate_candidates(self.cx, goal);
|
||||
|
||||
// Map each candidate from being canonical wrt the current inference context to being
|
||||
// canonical wrt the caller.
|
||||
for Candidate { source, result } in normalized_candidates {
|
||||
self.infcx.probe(|_| {
|
||||
let candidate_certainty =
|
||||
instantiate_canonical_query_response(&self.infcx, &orig_values, result);
|
||||
|
||||
// FIXME: This is a bit scary if the `normalizes_to_goal` overflows.
|
||||
//
|
||||
// If we have an ambiguous candidate it hides that normalization
|
||||
// caused an overflow which may cause issues.
|
||||
self.try_insert_candidate(
|
||||
source,
|
||||
normalization_certainty.unify_and(candidate_certainty),
|
||||
)
|
||||
})
|
||||
for mut normalized_candidate in normalized_candidates {
|
||||
normalized_candidate.result =
|
||||
normalized_candidate.result.unchecked_map(|mut response| {
|
||||
response.certainty = response.certainty.unify_and(normalization_certainty);
|
||||
response
|
||||
});
|
||||
self.candidates.push(normalized_candidate);
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
fn assemble_impl_candidates(&mut self, goal: Goal<'tcx, G>) {
|
||||
self.cx.tcx.for_each_relevant_impl(
|
||||
goal.predicate.trait_def_id(self.cx.tcx),
|
||||
let tcx = self.cx.tcx();
|
||||
tcx.for_each_relevant_impl(
|
||||
goal.predicate.trait_def_id(tcx),
|
||||
goal.predicate.self_ty(),
|
||||
|impl_def_id| G::consider_impl_candidate(self, goal, impl_def_id),
|
||||
);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue