1
Fork 0

introduce a separate set of types for finalized proof trees

This commit is contained in:
Boxy 2023-06-08 23:24:01 +01:00
parent 7a3665d016
commit e367c04dc6
7 changed files with 334 additions and 187 deletions

View file

@ -2,7 +2,7 @@ use super::{CanonicalInput, Certainty, Goal, NoSolution, QueryInput, QueryResult
use crate::ty; use crate::ty;
use std::fmt::{Debug, Write}; use std::fmt::{Debug, Write};
#[derive(Eq, PartialEq, Hash, HashStable)] #[derive(Eq, PartialEq, Debug, Hash, HashStable)]
pub enum CacheHit { pub enum CacheHit {
Provisional, Provisional,
Global, Global,
@ -11,16 +11,16 @@ pub enum CacheHit {
#[derive(Eq, PartialEq, Hash, HashStable)] #[derive(Eq, PartialEq, Hash, HashStable)]
pub struct GoalEvaluation<'tcx> { pub struct GoalEvaluation<'tcx> {
pub uncanonicalized_goal: Goal<'tcx, ty::Predicate<'tcx>>, pub uncanonicalized_goal: Goal<'tcx, ty::Predicate<'tcx>>,
pub canonicalized_goal: Option<CanonicalInput<'tcx>>, pub canonicalized_goal: CanonicalInput<'tcx>,
/// To handle coinductive cycles we can end up re-evaluating a goal pub kind: GoalEvaluationKind<'tcx>,
/// multiple times with different results for a nested goal. Each rerun
/// is represented as an entry in this vec.
pub evaluation_steps: Vec<GoalEvaluationStep<'tcx>>,
pub cache_hit: Option<CacheHit>, pub result: QueryResult<'tcx>,
}
pub result: Option<QueryResult<'tcx>>, #[derive(Eq, PartialEq, Hash, HashStable)]
pub enum GoalEvaluationKind<'tcx> {
CacheHit(CacheHit),
Uncached { revisions: Vec<GoalEvaluationStep<'tcx>> },
} }
impl Debug for GoalEvaluation<'_> { impl Debug for GoalEvaluation<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
@ -31,7 +31,7 @@ impl Debug for GoalEvaluation<'_> {
#[derive(Eq, PartialEq, Hash, HashStable)] #[derive(Eq, PartialEq, Hash, HashStable)]
pub struct AddedGoalsEvaluation<'tcx> { pub struct AddedGoalsEvaluation<'tcx> {
pub evaluations: Vec<Vec<GoalEvaluation<'tcx>>>, pub evaluations: Vec<Vec<GoalEvaluation<'tcx>>>,
pub result: Option<Result<Certainty, NoSolution>>, pub result: Result<Certainty, NoSolution>,
} }
impl Debug for AddedGoalsEvaluation<'_> { impl Debug for AddedGoalsEvaluation<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
@ -46,7 +46,7 @@ pub struct GoalEvaluationStep<'tcx> {
pub nested_goal_evaluations: Vec<AddedGoalsEvaluation<'tcx>>, pub nested_goal_evaluations: Vec<AddedGoalsEvaluation<'tcx>>,
pub candidates: Vec<GoalCandidate<'tcx>>, pub candidates: Vec<GoalCandidate<'tcx>>,
pub result: Option<QueryResult<'tcx>>, pub result: QueryResult<'tcx>,
} }
impl Debug for GoalEvaluationStep<'_> { impl Debug for GoalEvaluationStep<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
@ -58,9 +58,14 @@ impl Debug for GoalEvaluationStep<'_> {
pub struct GoalCandidate<'tcx> { pub struct GoalCandidate<'tcx> {
pub nested_goal_evaluations: Vec<AddedGoalsEvaluation<'tcx>>, pub nested_goal_evaluations: Vec<AddedGoalsEvaluation<'tcx>>,
pub candidates: Vec<GoalCandidate<'tcx>>, pub candidates: Vec<GoalCandidate<'tcx>>,
pub kind: CandidateKind<'tcx>,
pub name: Option<String>, }
pub result: Option<QueryResult<'tcx>>, #[derive(Eq, PartialEq, Debug, Hash, HashStable)]
pub enum CandidateKind<'tcx> {
/// Probe entered when normalizing the self ty during candidate assembly
NormalizedSelfTyAssembly,
/// A normal candidate for proving a goal
Candidate { name: String, result: QueryResult<'tcx> },
} }
impl Debug for GoalCandidate<'_> { impl Debug for GoalCandidate<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
@ -97,19 +102,23 @@ impl ProofTreeFormatter<'_, '_> {
writeln!(f, "GOAL: {:?}", goal.uncanonicalized_goal)?; writeln!(f, "GOAL: {:?}", goal.uncanonicalized_goal)?;
writeln!(f, "CANONICALIZED: {:?}", goal.canonicalized_goal)?; writeln!(f, "CANONICALIZED: {:?}", goal.canonicalized_goal)?;
match goal.cache_hit { match &goal.kind {
Some(CacheHit::Global) => writeln!(f, "GLOBAL CACHE HIT: {:?}", goal.result), GoalEvaluationKind::CacheHit(CacheHit::Global) => {
Some(CacheHit::Provisional) => writeln!(f, "PROVISIONAL CACHE HIT: {:?}", goal.result), writeln!(f, "GLOBAL CACHE HIT: {:?}", goal.result)
None => { }
for (n, step) in goal.evaluation_steps.iter().enumerate() { GoalEvaluationKind::CacheHit(CacheHit::Provisional) => {
writeln!(f, "PROVISIONAL CACHE HIT: {:?}", goal.result)
}
GoalEvaluationKind::Uncached { revisions } => {
for (n, step) in revisions.iter().enumerate() {
let f = &mut *self.f; let f = &mut *self.f;
writeln!(f, "REVISION {n}: {:?}", step.result.unwrap())?; writeln!(f, "REVISION {n}: {:?}", step.result)?;
let mut f = self.nested(); let mut f = self.nested();
f.format_evaluation_step(step)?; f.format_evaluation_step(step)?;
} }
let f = &mut *self.f; let f = &mut *self.f;
writeln!(f, "RESULT: {:?}", goal.result.unwrap()) writeln!(f, "RESULT: {:?}", goal.result)
} }
} }
} }
@ -136,12 +145,14 @@ impl ProofTreeFormatter<'_, '_> {
fn format_candidate(&mut self, candidate: &GoalCandidate<'_>) -> std::fmt::Result { fn format_candidate(&mut self, candidate: &GoalCandidate<'_>) -> std::fmt::Result {
let f = &mut *self.f; let f = &mut *self.f;
match (candidate.name.as_ref(), candidate.result) { match &candidate.kind {
(Some(name), Some(result)) => writeln!(f, "CANDIDATE {}: {:?}", name, result,)?, CandidateKind::NormalizedSelfTyAssembly => {
(None, None) => writeln!(f, "MISC PROBE")?, writeln!(f, "NORMALIZING SELF TY FOR ASSEMBLY:")
(None, Some(_)) => unreachable!("unexpected probe with no name but a result"), }
(Some(_), None) => unreachable!("unexpected probe with a name but no candidate"), CandidateKind::Candidate { name, result } => {
}; writeln!(f, "CANDIDATE {}: {:?}", name, result)
}
}?;
let mut f = self.nested(); let mut f = self.nested();
for candidate in &candidate.candidates { for candidate in &candidate.candidates {
@ -159,7 +170,7 @@ impl ProofTreeFormatter<'_, '_> {
nested_goal_evaluation: &AddedGoalsEvaluation<'_>, nested_goal_evaluation: &AddedGoalsEvaluation<'_>,
) -> std::fmt::Result { ) -> std::fmt::Result {
let f = &mut *self.f; let f = &mut *self.f;
writeln!(f, "TRY_EVALUATE_ADDED_GOALS: {:?}", nested_goal_evaluation.result.unwrap())?; writeln!(f, "TRY_EVALUATE_ADDED_GOALS: {:?}", nested_goal_evaluation.result)?;
for (n, revision) in nested_goal_evaluation.evaluations.iter().enumerate() { for (n, revision) in nested_goal_evaluation.evaluations.iter().enumerate() {
let f = &mut *self.f; let f = &mut *self.f;

View file

@ -1,5 +1,6 @@
use super::{EvalCtxt, SolverMode}; use super::{EvalCtxt, SolverMode};
use rustc_infer::traits::query::NoSolution; use rustc_infer::traits::query::NoSolution;
use rustc_middle::traits::solve::inspect::CandidateKind;
use rustc_middle::traits::solve::{Certainty, Goal, QueryResult}; use rustc_middle::traits::solve::{Certainty, Goal, QueryResult};
use rustc_middle::ty; use rustc_middle::ty;
@ -109,12 +110,12 @@ impl<'tcx> EvalCtxt<'_, 'tcx> {
direction: ty::AliasRelationDirection, direction: ty::AliasRelationDirection,
invert: Invert, invert: Invert,
) -> QueryResult<'tcx> { ) -> QueryResult<'tcx> {
self.probe_candidate( self.probe(
|ecx| { |ecx| {
ecx.normalizes_to_inner(param_env, alias, other, direction, invert)?; ecx.normalizes_to_inner(param_env, alias, other, direction, invert)?;
ecx.evaluate_added_goals_and_make_canonical_response(Certainty::Yes) ecx.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
}, },
|| "normalizes-to".into(), |r| CandidateKind::Candidate { name: "normalizes-to".into(), result: *r },
) )
} }
@ -156,7 +157,7 @@ impl<'tcx> EvalCtxt<'_, 'tcx> {
alias_rhs: ty::AliasTy<'tcx>, alias_rhs: ty::AliasTy<'tcx>,
direction: ty::AliasRelationDirection, direction: ty::AliasRelationDirection,
) -> QueryResult<'tcx> { ) -> QueryResult<'tcx> {
self.probe_candidate( self.probe(
|ecx| { |ecx| {
match direction { match direction {
ty::AliasRelationDirection::Equate => { ty::AliasRelationDirection::Equate => {
@ -169,7 +170,7 @@ impl<'tcx> EvalCtxt<'_, 'tcx> {
ecx.evaluate_added_goals_and_make_canonical_response(Certainty::Yes) ecx.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
}, },
|| "substs relate".into(), |r| CandidateKind::Candidate { name: "substs relate".into(), result: *r },
) )
} }
@ -180,7 +181,7 @@ impl<'tcx> EvalCtxt<'_, 'tcx> {
rhs: ty::Term<'tcx>, rhs: ty::Term<'tcx>,
direction: ty::AliasRelationDirection, direction: ty::AliasRelationDirection,
) -> QueryResult<'tcx> { ) -> QueryResult<'tcx> {
self.probe_candidate( self.probe(
|ecx| { |ecx| {
ecx.normalizes_to_inner( ecx.normalizes_to_inner(
param_env, param_env,
@ -198,7 +199,7 @@ impl<'tcx> EvalCtxt<'_, 'tcx> {
)?; )?;
ecx.evaluate_added_goals_and_make_canonical_response(Certainty::Yes) ecx.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
}, },
|| "bidir normalizes-to".into(), |r| CandidateKind::Candidate { name: "bidir normalizes-to".into(), result: *r },
) )
} }
} }

View file

@ -8,6 +8,7 @@ use rustc_hir::def_id::DefId;
use rustc_infer::traits::query::NoSolution; use rustc_infer::traits::query::NoSolution;
use rustc_infer::traits::util::elaborate; use rustc_infer::traits::util::elaborate;
use rustc_infer::traits::Reveal; use rustc_infer::traits::Reveal;
use rustc_middle::traits::solve::inspect::CandidateKind;
use rustc_middle::traits::solve::{CanonicalResponse, Certainty, Goal, MaybeCause, QueryResult}; use rustc_middle::traits::solve::{CanonicalResponse, Certainty, Goal, MaybeCause, QueryResult};
use rustc_middle::ty::fast_reject::TreatProjections; use rustc_middle::ty::fast_reject::TreatProjections;
use rustc_middle::ty::TypeFoldable; use rustc_middle::ty::TypeFoldable;
@ -336,37 +337,40 @@ impl<'tcx> EvalCtxt<'_, 'tcx> {
return return
}; };
let normalized_self_candidates: Result<_, NoSolution> = self.probe(|ecx| { let normalized_self_candidates: Result<_, NoSolution> = self.probe(
ecx.with_incremented_depth( |ecx| {
|ecx| { ecx.with_incremented_depth(
let result = ecx.evaluate_added_goals_and_make_canonical_response( |ecx| {
Certainty::Maybe(MaybeCause::Overflow), let result = ecx.evaluate_added_goals_and_make_canonical_response(
)?; Certainty::Maybe(MaybeCause::Overflow),
Ok(vec![Candidate { source: CandidateSource::BuiltinImpl, result }]) )?;
}, Ok(vec![Candidate { source: CandidateSource::BuiltinImpl, result }])
|ecx| { },
let normalized_ty = ecx.next_ty_infer(); |ecx| {
let normalizes_to_goal = goal.with( let normalized_ty = ecx.next_ty_infer();
tcx, let normalizes_to_goal = goal.with(
ty::Binder::dummy(ty::ProjectionPredicate { tcx,
projection_ty, ty::Binder::dummy(ty::ProjectionPredicate {
term: normalized_ty.into(), projection_ty,
}), term: normalized_ty.into(),
); }),
ecx.add_goal(normalizes_to_goal); );
let _ = ecx.try_evaluate_added_goals().inspect_err(|_| { ecx.add_goal(normalizes_to_goal);
debug!("self type normalization failed"); let _ = ecx.try_evaluate_added_goals().inspect_err(|_| {
})?; debug!("self type normalization failed");
let normalized_ty = ecx.resolve_vars_if_possible(normalized_ty); })?;
debug!(?normalized_ty, "self type normalized"); let normalized_ty = ecx.resolve_vars_if_possible(normalized_ty);
// NOTE: Alternatively we could call `evaluate_goal` here and only debug!(?normalized_ty, "self type normalized");
// have a `Normalized` candidate. This doesn't work as long as we // NOTE: Alternatively we could call `evaluate_goal` here and only
// use `CandidateSource` in winnowing. // have a `Normalized` candidate. This doesn't work as long as we
let goal = goal.with(tcx, goal.predicate.with_self_ty(tcx, normalized_ty)); // use `CandidateSource` in winnowing.
Ok(ecx.assemble_and_evaluate_candidates(goal)) let goal = goal.with(tcx, goal.predicate.with_self_ty(tcx, normalized_ty));
}, Ok(ecx.assemble_and_evaluate_candidates(goal))
) },
}); )
},
|_| CandidateKind::NormalizedSelfTyAssembly,
);
if let Ok(normalized_self_candidates) = normalized_self_candidates { if let Ok(normalized_self_candidates) = normalized_self_candidates {
candidates.extend(normalized_self_candidates); candidates.extend(normalized_self_candidates);

View file

@ -9,6 +9,7 @@ use rustc_infer::infer::{
use rustc_infer::traits::query::NoSolution; use rustc_infer::traits::query::NoSolution;
use rustc_infer::traits::ObligationCause; use rustc_infer::traits::ObligationCause;
use rustc_middle::infer::unify_key::{ConstVariableOrigin, ConstVariableOriginKind}; use rustc_middle::infer::unify_key::{ConstVariableOrigin, ConstVariableOriginKind};
use rustc_middle::traits::solve::inspect::CandidateKind;
use rustc_middle::traits::solve::{ use rustc_middle::traits::solve::{
CanonicalInput, CanonicalResponse, Certainty, MaybeCause, PredefinedOpaques, CanonicalInput, CanonicalResponse, Certainty, MaybeCause, PredefinedOpaques,
PredefinedOpaquesData, QueryResult, PredefinedOpaquesData, QueryResult,
@ -78,7 +79,7 @@ pub struct EvalCtxt<'a, 'tcx> {
inspect: ProofTreeBuilder<'tcx>, inspect: ProofTreeBuilder<'tcx>,
} }
#[derive(Debug, Copy, Clone, PartialEq, Eq)] #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, HashStable)]
pub(super) enum IsNormalizesToHack { pub(super) enum IsNormalizesToHack {
Yes, Yes,
No, No,
@ -157,7 +158,7 @@ impl<'tcx> InferCtxtEvalExt<'tcx> for InferCtxt<'tcx> {
}; };
let result = ecx.evaluate_goal(IsNormalizesToHack::No, goal); let result = ecx.evaluate_goal(IsNormalizesToHack::No, goal);
if let Some(tree) = ecx.inspect.into_proof_tree() { if let Some(tree) = ecx.inspect.finalize() {
println!("{:?}", tree); println!("{:?}", tree);
} }
@ -509,7 +510,13 @@ impl<'a, 'tcx> EvalCtxt<'a, 'tcx> {
} }
impl<'tcx> EvalCtxt<'_, 'tcx> { impl<'tcx> EvalCtxt<'_, 'tcx> {
pub(super) fn probe<T>(&mut self, f: impl FnOnce(&mut EvalCtxt<'_, 'tcx>) -> T) -> T { /// `probe_kind` is only called when proof tree building is enabled so it can be
/// as expensive as necessary to output the desired information.
pub(super) fn probe<T>(
&mut self,
f: impl FnOnce(&mut EvalCtxt<'_, 'tcx>) -> T,
probe_kind: impl FnOnce(&T) -> CandidateKind<'tcx>,
) -> T {
let mut ecx = EvalCtxt { let mut ecx = EvalCtxt {
infcx: self.infcx, infcx: self.infcx,
var_values: self.var_values, var_values: self.var_values,
@ -521,23 +528,14 @@ impl<'tcx> EvalCtxt<'_, 'tcx> {
inspect: self.inspect.new_goal_candidate(), inspect: self.inspect.new_goal_candidate(),
}; };
let r = self.infcx.probe(|_| f(&mut ecx)); let r = self.infcx.probe(|_| f(&mut ecx));
self.inspect.goal_candidate(ecx.inspect); if !self.inspect.is_noop() {
let cand_kind = probe_kind(&r);
ecx.inspect.candidate_kind(cand_kind);
self.inspect.goal_candidate(ecx.inspect);
}
r r
} }
pub(super) fn probe_candidate(
&mut self,
f: impl FnOnce(&mut EvalCtxt<'_, 'tcx>) -> QueryResult<'tcx>,
mut name: impl FnMut() -> String,
) -> QueryResult<'tcx> {
self.probe(|ecx| {
let result = f(ecx);
ecx.inspect.candidate_name(&mut name);
ecx.inspect.query_result(result);
result
})
}
pub(super) fn tcx(&self) -> TyCtxt<'tcx> { pub(super) fn tcx(&self) -> TyCtxt<'tcx> {
self.infcx.tcx self.infcx.tcx
} }
@ -858,7 +856,7 @@ impl<'tcx> EvalCtxt<'_, 'tcx> {
ecx.add_item_bounds_for_hidden_type(candidate_key, param_env, candidate_ty); ecx.add_item_bounds_for_hidden_type(candidate_key, param_env, candidate_ty);
ecx.evaluate_added_goals_and_make_canonical_response(Certainty::Yes) ecx.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
}, },
|| "opaque type storage".into(), |r| CandidateKind::Candidate { name: "opaque type storage".into(), result: *r },
)); ));
} }
values values

View file

@ -1,25 +1,129 @@
use rustc_middle::{ use rustc_middle::{
traits::{ traits::{
query::NoSolution, query::NoSolution,
solve::{inspect::*, CanonicalInput, Certainty, Goal, QueryInput, QueryResult}, solve::{
inspect::{self, CacheHit, CandidateKind},
CanonicalInput, Certainty, Goal, QueryInput, QueryResult,
},
}, },
ty, ty,
}; };
#[derive(Eq, PartialEq, Debug, Hash, HashStable)]
pub struct WipGoalEvaluation<'tcx> {
pub uncanonicalized_goal: Goal<'tcx, ty::Predicate<'tcx>>,
pub canonicalized_goal: Option<CanonicalInput<'tcx>>,
pub evaluation_steps: Vec<WipGoalEvaluationStep<'tcx>>,
pub cache_hit: Option<CacheHit>,
pub result: Option<QueryResult<'tcx>>,
}
impl<'tcx> WipGoalEvaluation<'tcx> {
pub fn finalize(self) -> inspect::GoalEvaluation<'tcx> {
inspect::GoalEvaluation {
uncanonicalized_goal: self.uncanonicalized_goal,
canonicalized_goal: self.canonicalized_goal.unwrap(),
kind: match self.cache_hit {
Some(hit) => inspect::GoalEvaluationKind::CacheHit(hit),
None => inspect::GoalEvaluationKind::Uncached {
revisions: self
.evaluation_steps
.into_iter()
.map(WipGoalEvaluationStep::finalize)
.collect(),
},
},
result: self.result.unwrap(),
}
}
}
#[derive(Eq, PartialEq, Debug, Hash, HashStable)]
pub struct WipAddedGoalsEvaluation<'tcx> {
pub evaluations: Vec<Vec<WipGoalEvaluation<'tcx>>>,
pub result: Option<Result<Certainty, NoSolution>>,
}
impl<'tcx> WipAddedGoalsEvaluation<'tcx> {
pub fn finalize(self) -> inspect::AddedGoalsEvaluation<'tcx> {
inspect::AddedGoalsEvaluation {
evaluations: self
.evaluations
.into_iter()
.map(|evaluations| {
evaluations.into_iter().map(WipGoalEvaluation::finalize).collect()
})
.collect(),
result: self.result.unwrap(),
}
}
}
#[derive(Eq, PartialEq, Debug, Hash, HashStable)]
pub struct WipGoalEvaluationStep<'tcx> {
pub instantiated_goal: QueryInput<'tcx, ty::Predicate<'tcx>>,
pub nested_goal_evaluations: Vec<WipAddedGoalsEvaluation<'tcx>>,
pub candidates: Vec<WipGoalCandidate<'tcx>>,
pub result: Option<QueryResult<'tcx>>,
}
impl<'tcx> WipGoalEvaluationStep<'tcx> {
pub fn finalize(self) -> inspect::GoalEvaluationStep<'tcx> {
inspect::GoalEvaluationStep {
instantiated_goal: self.instantiated_goal,
nested_goal_evaluations: self
.nested_goal_evaluations
.into_iter()
.map(WipAddedGoalsEvaluation::finalize)
.collect(),
candidates: self.candidates.into_iter().map(WipGoalCandidate::finalize).collect(),
result: self.result.unwrap(),
}
}
}
#[derive(Eq, PartialEq, Debug, Hash, HashStable)]
pub struct WipGoalCandidate<'tcx> {
pub nested_goal_evaluations: Vec<WipAddedGoalsEvaluation<'tcx>>,
pub candidates: Vec<WipGoalCandidate<'tcx>>,
pub kind: Option<CandidateKind<'tcx>>,
}
impl<'tcx> WipGoalCandidate<'tcx> {
pub fn finalize(self) -> inspect::GoalCandidate<'tcx> {
inspect::GoalCandidate {
nested_goal_evaluations: self
.nested_goal_evaluations
.into_iter()
.map(WipAddedGoalsEvaluation::finalize)
.collect(),
candidates: self.candidates.into_iter().map(WipGoalCandidate::finalize).collect(),
kind: self.kind.unwrap(),
}
}
}
#[derive(Debug)] #[derive(Debug)]
pub enum DebugSolver<'tcx> { pub enum DebugSolver<'tcx> {
Root, Root,
GoalEvaluation(GoalEvaluation<'tcx>), GoalEvaluation(WipGoalEvaluation<'tcx>),
AddedGoalsEvaluation(AddedGoalsEvaluation<'tcx>), AddedGoalsEvaluation(WipAddedGoalsEvaluation<'tcx>),
GoalEvaluationStep(GoalEvaluationStep<'tcx>), GoalEvaluationStep(WipGoalEvaluationStep<'tcx>),
GoalCandidate(GoalCandidate<'tcx>), GoalCandidate(WipGoalCandidate<'tcx>),
} }
pub struct ProofTreeBuilder<'tcx>(Option<Box<DebugSolver<'tcx>>>); pub struct ProofTreeBuilder<'tcx>(Option<Box<DebugSolver<'tcx>>>);
impl<'tcx> ProofTreeBuilder<'tcx> { impl<'tcx> ProofTreeBuilder<'tcx> {
pub fn into_proof_tree(self) -> Option<DebugSolver<'tcx>> { pub fn finalize(self) -> Option<inspect::GoalEvaluation<'tcx>> {
self.0.map(|tree| *tree) let wip_tree = *(self.0?);
match wip_tree {
DebugSolver::GoalEvaluation(wip_goal_evaluation) => {
Some(wip_goal_evaluation.finalize())
}
_ => unreachable!(),
}
} }
pub fn new_root() -> ProofTreeBuilder<'tcx> { pub fn new_root() -> ProofTreeBuilder<'tcx> {
@ -30,6 +134,10 @@ impl<'tcx> ProofTreeBuilder<'tcx> {
Self(None) Self(None)
} }
pub fn is_noop(&self) -> bool {
self.0.is_none()
}
pub fn new_goal_evaluation( pub fn new_goal_evaluation(
&mut self, &mut self,
goal: Goal<'tcx, ty::Predicate<'tcx>>, goal: Goal<'tcx, ty::Predicate<'tcx>>,
@ -38,7 +146,7 @@ impl<'tcx> ProofTreeBuilder<'tcx> {
return ProofTreeBuilder(None); return ProofTreeBuilder(None);
} }
Self(Some(Box::new(DebugSolver::GoalEvaluation(GoalEvaluation { Self(Some(Box::new(DebugSolver::GoalEvaluation(WipGoalEvaluation {
uncanonicalized_goal: goal, uncanonicalized_goal: goal,
canonicalized_goal: None, canonicalized_goal: None,
evaluation_steps: vec![], evaluation_steps: vec![],
@ -81,7 +189,7 @@ impl<'tcx> ProofTreeBuilder<'tcx> {
match (this, *goal_evaluation.0.unwrap()) { match (this, *goal_evaluation.0.unwrap()) {
( (
DebugSolver::AddedGoalsEvaluation(AddedGoalsEvaluation { evaluations, .. }), DebugSolver::AddedGoalsEvaluation(WipAddedGoalsEvaluation { evaluations, .. }),
DebugSolver::GoalEvaluation(goal_evaluation), DebugSolver::GoalEvaluation(goal_evaluation),
) => evaluations.last_mut().unwrap().push(goal_evaluation), ) => evaluations.last_mut().unwrap().push(goal_evaluation),
(this @ DebugSolver::Root, goal_evaluation) => *this = goal_evaluation, (this @ DebugSolver::Root, goal_evaluation) => *this = goal_evaluation,
@ -93,7 +201,11 @@ impl<'tcx> ProofTreeBuilder<'tcx> {
&mut self, &mut self,
instantiated_goal: QueryInput<'tcx, ty::Predicate<'tcx>>, instantiated_goal: QueryInput<'tcx, ty::Predicate<'tcx>>,
) -> ProofTreeBuilder<'tcx> { ) -> ProofTreeBuilder<'tcx> {
Self(Some(Box::new(DebugSolver::GoalEvaluationStep(GoalEvaluationStep { if self.0.is_none() {
return Self(None);
}
Self(Some(Box::new(DebugSolver::GoalEvaluationStep(WipGoalEvaluationStep {
instantiated_goal, instantiated_goal,
nested_goal_evaluations: vec![], nested_goal_evaluations: vec![],
candidates: vec![], candidates: vec![],
@ -115,24 +227,25 @@ impl<'tcx> ProofTreeBuilder<'tcx> {
} }
pub fn new_goal_candidate(&mut self) -> ProofTreeBuilder<'tcx> { pub fn new_goal_candidate(&mut self) -> ProofTreeBuilder<'tcx> {
Self(Some(Box::new(DebugSolver::GoalCandidate(GoalCandidate { if self.0.is_none() {
return Self(None);
}
Self(Some(Box::new(DebugSolver::GoalCandidate(WipGoalCandidate {
nested_goal_evaluations: vec![], nested_goal_evaluations: vec![],
candidates: vec![], candidates: vec![],
name: None, kind: None,
result: None,
})))) }))))
} }
pub fn candidate_name(&mut self, f: &mut dyn FnMut() -> String) { pub fn candidate_kind(&mut self, kind: CandidateKind<'tcx>) {
let this = match self.0.as_mut() { let this = match self.0.as_mut() {
None => return, None => return,
Some(this) => &mut **this, Some(this) => &mut **this,
}; };
match this { match this {
DebugSolver::GoalCandidate(goal_candidate) => { DebugSolver::GoalCandidate(WipGoalCandidate { kind: old_kind @ None, .. }) => {
let name = f(); *old_kind = Some(kind)
assert!(goal_candidate.name.is_none());
goal_candidate.name = Some(name);
} }
_ => unreachable!(), _ => unreachable!(),
} }
@ -145,8 +258,8 @@ impl<'tcx> ProofTreeBuilder<'tcx> {
match (this, *candidate.0.unwrap()) { match (this, *candidate.0.unwrap()) {
( (
DebugSolver::GoalCandidate(GoalCandidate { candidates, .. }) DebugSolver::GoalCandidate(WipGoalCandidate { candidates, .. })
| DebugSolver::GoalEvaluationStep(GoalEvaluationStep { candidates, .. }), | DebugSolver::GoalEvaluationStep(WipGoalEvaluationStep { candidates, .. }),
DebugSolver::GoalCandidate(candidate), DebugSolver::GoalCandidate(candidate),
) => candidates.push(candidate), ) => candidates.push(candidate),
_ => unreachable!(), _ => unreachable!(),
@ -154,7 +267,11 @@ impl<'tcx> ProofTreeBuilder<'tcx> {
} }
pub fn new_evaluate_added_goals(&mut self) -> ProofTreeBuilder<'tcx> { pub fn new_evaluate_added_goals(&mut self) -> ProofTreeBuilder<'tcx> {
Self(Some(Box::new(DebugSolver::AddedGoalsEvaluation(AddedGoalsEvaluation { if self.0.is_none() {
return Self(None);
}
Self(Some(Box::new(DebugSolver::AddedGoalsEvaluation(WipAddedGoalsEvaluation {
evaluations: vec![], evaluations: vec![],
result: None, result: None,
})))) }))))
@ -194,10 +311,11 @@ impl<'tcx> ProofTreeBuilder<'tcx> {
match (this, *goals_evaluation.0.unwrap()) { match (this, *goals_evaluation.0.unwrap()) {
( (
DebugSolver::GoalEvaluationStep(GoalEvaluationStep { DebugSolver::GoalEvaluationStep(WipGoalEvaluationStep {
nested_goal_evaluations, .. nested_goal_evaluations,
..
}) })
| DebugSolver::GoalCandidate(GoalCandidate { nested_goal_evaluations, .. }), | DebugSolver::GoalCandidate(WipGoalCandidate { nested_goal_evaluations, .. }),
DebugSolver::AddedGoalsEvaluation(added_goals_evaluation), DebugSolver::AddedGoalsEvaluation(added_goals_evaluation),
) => nested_goal_evaluations.push(added_goals_evaluation), ) => nested_goal_evaluations.push(added_goals_evaluation),
_ => unreachable!(), _ => unreachable!(),
@ -215,15 +333,13 @@ impl<'tcx> ProofTreeBuilder<'tcx> {
assert!(goal_evaluation.result.is_none()); assert!(goal_evaluation.result.is_none());
goal_evaluation.result = Some(result); goal_evaluation.result = Some(result);
} }
DebugSolver::Root | DebugSolver::AddedGoalsEvaluation(_) => unreachable!(), DebugSolver::Root
| DebugSolver::AddedGoalsEvaluation(_)
| DebugSolver::GoalCandidate(_) => unreachable!(),
DebugSolver::GoalEvaluationStep(evaluation_step) => { DebugSolver::GoalEvaluationStep(evaluation_step) => {
assert!(evaluation_step.result.is_none()); assert!(evaluation_step.result.is_none());
evaluation_step.result = Some(result); evaluation_step.result = Some(result);
} }
DebugSolver::GoalCandidate(candidate) => {
assert!(candidate.result.is_none());
candidate.result = Some(result);
}
} }
} }
} }

View file

@ -9,6 +9,7 @@ use rustc_hir::LangItem;
use rustc_infer::traits::query::NoSolution; use rustc_infer::traits::query::NoSolution;
use rustc_infer::traits::specialization_graph::LeafDef; use rustc_infer::traits::specialization_graph::LeafDef;
use rustc_infer::traits::Reveal; use rustc_infer::traits::Reveal;
use rustc_middle::traits::solve::inspect::CandidateKind;
use rustc_middle::traits::solve::{CanonicalResponse, Certainty, Goal, QueryResult}; use rustc_middle::traits::solve::{CanonicalResponse, Certainty, Goal, QueryResult};
use rustc_middle::ty::fast_reject::{DeepRejectCtxt, TreatParams}; use rustc_middle::ty::fast_reject::{DeepRejectCtxt, TreatParams};
use rustc_middle::ty::ProjectionPredicate; use rustc_middle::ty::ProjectionPredicate;
@ -109,21 +110,30 @@ impl<'tcx> assembly::GoalKind<'tcx> for ProjectionPredicate<'tcx> {
assumption: ty::Binder<'tcx, ty::Clause<'tcx>>, assumption: ty::Binder<'tcx, ty::Clause<'tcx>>,
then: impl FnOnce(&mut EvalCtxt<'_, 'tcx>) -> QueryResult<'tcx>, then: impl FnOnce(&mut EvalCtxt<'_, 'tcx>) -> QueryResult<'tcx>,
) -> QueryResult<'tcx> { ) -> QueryResult<'tcx> {
if let Some(projection_pred) = assumption.as_projection_clause() if let Some(projection_pred) = assumption.as_projection_clause() {
&& projection_pred.projection_def_id() == goal.predicate.def_id() if projection_pred.projection_def_id() == goal.predicate.def_id() {
{ ecx.probe(
ecx.probe_candidate(|ecx| { |ecx| {
let assumption_projection_pred = let assumption_projection_pred =
ecx.instantiate_binder_with_infer(projection_pred); ecx.instantiate_binder_with_infer(projection_pred);
ecx.eq( ecx.eq(
goal.param_env, goal.param_env,
goal.predicate.projection_ty, goal.predicate.projection_ty,
assumption_projection_pred.projection_ty, assumption_projection_pred.projection_ty,
)?; )?;
ecx.eq(goal.param_env, goal.predicate.term, assumption_projection_pred.term) ecx.eq(
.expect("expected goal term to be fully unconstrained"); goal.param_env,
then(ecx) goal.predicate.term,
}, || "assumption".into()) assumption_projection_pred.term,
)
.expect("expected goal term to be fully unconstrained");
then(ecx)
},
|r| CandidateKind::Candidate { name: "assumption".into(), result: *r },
)
} else {
Err(NoSolution)
}
} else { } else {
Err(NoSolution) Err(NoSolution)
} }
@ -143,7 +153,7 @@ impl<'tcx> assembly::GoalKind<'tcx> for ProjectionPredicate<'tcx> {
return Err(NoSolution); return Err(NoSolution);
} }
ecx.probe_candidate( ecx.probe(
|ecx| { |ecx| {
let impl_substs = ecx.fresh_substs_for_item(impl_def_id); let impl_substs = ecx.fresh_substs_for_item(impl_def_id);
let impl_trait_ref = impl_trait_ref.subst(tcx, impl_substs); let impl_trait_ref = impl_trait_ref.subst(tcx, impl_substs);
@ -225,7 +235,7 @@ impl<'tcx> assembly::GoalKind<'tcx> for ProjectionPredicate<'tcx> {
.expect("expected goal term to be fully unconstrained"); .expect("expected goal term to be fully unconstrained");
ecx.evaluate_added_goals_and_make_canonical_response(Certainty::Yes) ecx.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
}, },
|| "impl".into(), |r| CandidateKind::Candidate { name: "impl".into(), result: *r },
) )
} }
@ -321,7 +331,7 @@ impl<'tcx> assembly::GoalKind<'tcx> for ProjectionPredicate<'tcx> {
goal: Goal<'tcx, Self>, goal: Goal<'tcx, Self>,
) -> QueryResult<'tcx> { ) -> QueryResult<'tcx> {
let tcx = ecx.tcx(); let tcx = ecx.tcx();
ecx.probe_candidate( ecx.probe(
|ecx| { |ecx| {
let metadata_ty = match goal.predicate.self_ty().kind() { let metadata_ty = match goal.predicate.self_ty().kind() {
ty::Bool ty::Bool
@ -406,7 +416,7 @@ impl<'tcx> assembly::GoalKind<'tcx> for ProjectionPredicate<'tcx> {
.expect("expected goal term to be fully unconstrained"); .expect("expected goal term to be fully unconstrained");
ecx.evaluate_added_goals_and_make_canonical_response(Certainty::Yes) ecx.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
}, },
|| "builtin pointee".into(), |r| CandidateKind::Candidate { name: "builtin pointee".into(), result: *r },
) )
} }
@ -542,13 +552,13 @@ impl<'tcx> assembly::GoalKind<'tcx> for ProjectionPredicate<'tcx> {
), ),
}; };
ecx.probe_candidate( ecx.probe(
|ecx| { |ecx| {
ecx.eq(goal.param_env, goal.predicate.term, discriminant_ty.into()) ecx.eq(goal.param_env, goal.predicate.term, discriminant_ty.into())
.expect("expected goal term to be fully unconstrained"); .expect("expected goal term to be fully unconstrained");
ecx.evaluate_added_goals_and_make_canonical_response(Certainty::Yes) ecx.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
}, },
|| "builtin discriminant kind".into(), |r| CandidateKind::Candidate { name: "builtin discriminant kind".into(), result: *r },
) )
} }

View file

@ -6,6 +6,7 @@ use rustc_hir::def_id::DefId;
use rustc_hir::{LangItem, Movability}; use rustc_hir::{LangItem, Movability};
use rustc_infer::traits::query::NoSolution; use rustc_infer::traits::query::NoSolution;
use rustc_infer::traits::util::supertraits; use rustc_infer::traits::util::supertraits;
use rustc_middle::traits::solve::inspect::CandidateKind;
use rustc_middle::traits::solve::{CanonicalResponse, Certainty, Goal, QueryResult}; use rustc_middle::traits::solve::{CanonicalResponse, Certainty, Goal, QueryResult};
use rustc_middle::ty::fast_reject::{DeepRejectCtxt, TreatParams, TreatProjections}; use rustc_middle::ty::fast_reject::{DeepRejectCtxt, TreatParams, TreatProjections};
use rustc_middle::ty::{self, ToPredicate, Ty, TyCtxt}; use rustc_middle::ty::{self, ToPredicate, Ty, TyCtxt};
@ -61,7 +62,7 @@ impl<'tcx> assembly::GoalKind<'tcx> for TraitPredicate<'tcx> {
}, },
}; };
ecx.probe_candidate( ecx.probe(
|ecx| { |ecx| {
let impl_substs = ecx.fresh_substs_for_item(impl_def_id); let impl_substs = ecx.fresh_substs_for_item(impl_def_id);
let impl_trait_ref = impl_trait_ref.subst(tcx, impl_substs); let impl_trait_ref = impl_trait_ref.subst(tcx, impl_substs);
@ -77,7 +78,7 @@ impl<'tcx> assembly::GoalKind<'tcx> for TraitPredicate<'tcx> {
ecx.evaluate_added_goals_and_make_canonical_response(maximal_certainty) ecx.evaluate_added_goals_and_make_canonical_response(maximal_certainty)
}, },
|| "impl".into(), |r| CandidateKind::Candidate { name: "impl".into(), result: *r },
) )
} }
@ -87,21 +88,26 @@ impl<'tcx> assembly::GoalKind<'tcx> for TraitPredicate<'tcx> {
assumption: ty::Binder<'tcx, ty::Clause<'tcx>>, assumption: ty::Binder<'tcx, ty::Clause<'tcx>>,
then: impl FnOnce(&mut EvalCtxt<'_, 'tcx>) -> QueryResult<'tcx>, then: impl FnOnce(&mut EvalCtxt<'_, 'tcx>) -> QueryResult<'tcx>,
) -> QueryResult<'tcx> { ) -> QueryResult<'tcx> {
if let Some(trait_clause) = assumption.as_trait_clause() if let Some(trait_clause) = assumption.as_trait_clause() {
&& trait_clause.def_id() == goal.predicate.def_id() if trait_clause.def_id() == goal.predicate.def_id()
&& trait_clause.polarity() == goal.predicate.polarity && trait_clause.polarity() == goal.predicate.polarity
{ {
// FIXME: Constness // FIXME: Constness
ecx.probe_candidate(|ecx| { ecx.probe(
let assumption_trait_pred = |ecx| {
ecx.instantiate_binder_with_infer(trait_clause); let assumption_trait_pred = ecx.instantiate_binder_with_infer(trait_clause);
ecx.eq( ecx.eq(
goal.param_env, goal.param_env,
goal.predicate.trait_ref, goal.predicate.trait_ref,
assumption_trait_pred.trait_ref, assumption_trait_pred.trait_ref,
)?; )?;
then(ecx) then(ecx)
}, || "assumption".into()) },
|r| CandidateKind::Candidate { name: "assumption".into(), result: *r },
)
} else {
Err(NoSolution)
}
} else { } else {
Err(NoSolution) Err(NoSolution)
} }
@ -135,7 +141,7 @@ impl<'tcx> assembly::GoalKind<'tcx> for TraitPredicate<'tcx> {
let tcx = ecx.tcx(); let tcx = ecx.tcx();
ecx.probe_candidate( ecx.probe(
|ecx| { |ecx| {
let nested_obligations = tcx let nested_obligations = tcx
.predicates_of(goal.predicate.def_id()) .predicates_of(goal.predicate.def_id())
@ -143,7 +149,7 @@ impl<'tcx> assembly::GoalKind<'tcx> for TraitPredicate<'tcx> {
ecx.add_goals(nested_obligations.predicates.into_iter().map(|p| goal.with(tcx, p))); ecx.add_goals(nested_obligations.predicates.into_iter().map(|p| goal.with(tcx, p)));
ecx.evaluate_added_goals_and_make_canonical_response(Certainty::Yes) ecx.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
}, },
|| "trait alias".into(), |r| CandidateKind::Candidate { name: "trait alias".into(), result: *r },
) )
} }
@ -350,7 +356,7 @@ impl<'tcx> assembly::GoalKind<'tcx> for TraitPredicate<'tcx> {
if b_ty.is_ty_var() { if b_ty.is_ty_var() {
return ecx.evaluate_added_goals_and_make_canonical_response(Certainty::AMBIGUOUS); return ecx.evaluate_added_goals_and_make_canonical_response(Certainty::AMBIGUOUS);
} }
ecx.probe_candidate( ecx.probe(
|ecx| { |ecx| {
match (a_ty.kind(), b_ty.kind()) { match (a_ty.kind(), b_ty.kind()) {
// Trait upcasting, or `dyn Trait + Auto + 'a` -> `dyn Trait + 'b` // Trait upcasting, or `dyn Trait + Auto + 'a` -> `dyn Trait + 'b`
@ -458,7 +464,7 @@ impl<'tcx> assembly::GoalKind<'tcx> for TraitPredicate<'tcx> {
_ => Err(NoSolution), _ => Err(NoSolution),
} }
}, },
|| "builtin unsize".into(), |r| CandidateKind::Candidate { name: "builtin unsize".into(), result: *r },
) )
} }
@ -488,39 +494,40 @@ impl<'tcx> assembly::GoalKind<'tcx> for TraitPredicate<'tcx> {
return vec![]; return vec![];
} }
let mut unsize_dyn_to_principal = let mut unsize_dyn_to_principal = |principal: Option<ty::PolyExistentialTraitRef<'tcx>>| {
|principal: Option<ty::PolyExistentialTraitRef<'tcx>>| { ecx.probe(
ecx.probe_candidate( |ecx| -> Result<_, NoSolution> {
|ecx| -> Result<_, NoSolution> { // Require that all of the trait predicates from A match B, except for
// Require that all of the trait predicates from A match B, except for // the auto traits. We do this by constructing a new A type with B's
// the auto traits. We do this by constructing a new A type with B's // auto traits, and equating these types.
// auto traits, and equating these types. let new_a_data = principal
let new_a_data = principal .into_iter()
.into_iter() .map(|trait_ref| trait_ref.map_bound(ty::ExistentialPredicate::Trait))
.map(|trait_ref| trait_ref.map_bound(ty::ExistentialPredicate::Trait)) .chain(a_data.iter().filter(|a| {
.chain(a_data.iter().filter(|a| { matches!(a.skip_binder(), ty::ExistentialPredicate::Projection(_))
matches!(a.skip_binder(), ty::ExistentialPredicate::Projection(_)) }))
})) .chain(
.chain( b_data
b_data .auto_traits()
.auto_traits() .map(ty::ExistentialPredicate::AutoTrait)
.map(ty::ExistentialPredicate::AutoTrait) .map(ty::Binder::dummy),
.map(ty::Binder::dummy), );
); let new_a_data = tcx.mk_poly_existential_predicates_from_iter(new_a_data);
let new_a_data = tcx.mk_poly_existential_predicates_from_iter(new_a_data); let new_a_ty = tcx.mk_dynamic(new_a_data, b_region, ty::Dyn);
let new_a_ty = tcx.mk_dynamic(new_a_data, b_region, ty::Dyn);
// We also require that A's lifetime outlives B's lifetime. // We also require that A's lifetime outlives B's lifetime.
ecx.eq(goal.param_env, new_a_ty, b_ty)?; ecx.eq(goal.param_env, new_a_ty, b_ty)?;
ecx.add_goal(goal.with( ecx.add_goal(
goal.with(
tcx, tcx,
ty::Binder::dummy(ty::OutlivesPredicate(a_region, b_region)), ty::Binder::dummy(ty::OutlivesPredicate(a_region, b_region)),
)); ),
ecx.evaluate_added_goals_and_make_canonical_response(Certainty::Yes) );
}, ecx.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
|| "upcast dyn to principle".into(), },
) |r| CandidateKind::Candidate { name: "upcast dyn to principle".into(), result: *r },
}; )
};
let mut responses = vec![]; let mut responses = vec![];
// If the principal def ids match (or are both none), then we're not doing // If the principal def ids match (or are both none), then we're not doing
@ -716,7 +723,7 @@ impl<'tcx> EvalCtxt<'_, 'tcx> {
goal: Goal<'tcx, TraitPredicate<'tcx>>, goal: Goal<'tcx, TraitPredicate<'tcx>>,
constituent_tys: impl Fn(&EvalCtxt<'_, 'tcx>, Ty<'tcx>) -> Result<Vec<Ty<'tcx>>, NoSolution>, constituent_tys: impl Fn(&EvalCtxt<'_, 'tcx>, Ty<'tcx>) -> Result<Vec<Ty<'tcx>>, NoSolution>,
) -> QueryResult<'tcx> { ) -> QueryResult<'tcx> {
self.probe_candidate( self.probe(
|ecx| { |ecx| {
ecx.add_goals( ecx.add_goals(
constituent_tys(ecx, goal.predicate.self_ty())? constituent_tys(ecx, goal.predicate.self_ty())?
@ -731,7 +738,7 @@ impl<'tcx> EvalCtxt<'_, 'tcx> {
); );
ecx.evaluate_added_goals_and_make_canonical_response(Certainty::Yes) ecx.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
}, },
|| "constituent tys".into(), |r| CandidateKind::Candidate { name: "constituent tys".into(), result: *r },
) )
} }