Skip to content

Commit 3ad0102

Browse files
Record certainty of evaluate_added_goals_and_make_canonical_response call in candidate
1 parent 61a1dbd commit 3ad0102

File tree

5 files changed

+50
-0
lines changed

5 files changed

+50
-0
lines changed

compiler/rustc_middle/src/traits/solve/inspect.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,12 @@ pub enum ProbeStep<'tcx> {
122122
/// used whenever there are multiple candidates to prove the
123123
/// current goalby .
124124
NestedProbe(Probe<'tcx>),
125+
/// A call to `EvalCtxt::evaluate_added_goals_make_canonical_response` with
126+
/// `Certainty` was made. This is the certainty passed in, so it's not unified
127+
/// with the certainty of the `try_evaluate_added_goals` that is done within;
128+
/// if it's `Certainty::Yes`, then we can trust that the candidate is "finished"
129+
/// and we didn't force ambiguity for some reason.
130+
MakeCandidateWithCertainty(Certainty),
125131
}
126132

127133
/// What kind of probe we're in. In case the probe represents a candidate, or

compiler/rustc_middle/src/traits/solve/inspect/format.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,9 @@ impl<'a, 'b> ProofTreeFormatter<'a, 'b> {
132132
}
133133
ProbeStep::EvaluateGoals(eval) => this.format_added_goals_evaluation(eval)?,
134134
ProbeStep::NestedProbe(probe) => this.format_probe(probe)?,
135+
ProbeStep::MakeCandidateWithCertainty(certainty) => {
136+
writeln!(this.f, "EVALUATE GOALS AND MAKE RESPONSE: {certainty:?}")?
137+
}
135138
}
136139
}
137140
Ok(())

compiler/rustc_trait_selection/src/solve/eval_ctxt/canonical.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@ impl<'tcx> EvalCtxt<'_, 'tcx> {
9898
previous call to `try_evaluate_added_goals!`"
9999
);
100100

101+
self.inspect.add_certainty(certainty);
102+
101103
// When normalizing, we've replaced the expected term with an unconstrained
102104
// inference variable. This means that we dropped information which could
103105
// have been important. We handle this by instead returning the nested goals

compiler/rustc_trait_selection/src/solve/inspect/analyse.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ pub struct InspectCandidate<'a, 'tcx> {
4545
nested_goals: Vec<inspect::CanonicalState<'tcx, Goal<'tcx, ty::Predicate<'tcx>>>>,
4646
final_state: inspect::CanonicalState<'tcx, ()>,
4747
result: QueryResult<'tcx>,
48+
candidate_certainty: Option<Certainty>,
4849
}
4950

5051
impl<'a, 'tcx> InspectCandidate<'a, 'tcx> {
@@ -56,6 +57,19 @@ impl<'a, 'tcx> InspectCandidate<'a, 'tcx> {
5657
self.result.map(|c| c.value.certainty)
5758
}
5859

60+
/// Certainty passed into `evaluate_added_goals_and_make_canonical_response`.
61+
///
62+
/// If this certainty is `Some(Yes)`, then we must be confident that the candidate
63+
/// must hold iff it's nested goals hold. This is not true if the certainty is
64+
/// `Some(Maybe)`, which suggests we forced ambiguity instead, or if it is `None`,
65+
/// which suggests we may have not assembled any candidates at all.
66+
///
67+
/// This is *not* the certainty of the candidate's nested evaluation, which can be
68+
/// accessed with [`Self::result`] instead.
69+
pub fn candidate_certainty(&self) -> Option<Certainty> {
70+
self.candidate_certainty
71+
}
72+
5973
/// Visit all nested goals of this candidate without rolling
6074
/// back their inference constraints. This function modifies
6175
/// the state of the `infcx`.
@@ -160,7 +174,9 @@ impl<'a, 'tcx> InspectGoal<'a, 'tcx> {
160174
nested_goals: &mut Vec<inspect::CanonicalState<'tcx, Goal<'tcx, ty::Predicate<'tcx>>>>,
161175
probe: &inspect::Probe<'tcx>,
162176
) {
177+
let mut candidate_certainty = None;
163178
let num_candidates = candidates.len();
179+
164180
for step in &probe.steps {
165181
match step {
166182
&inspect::ProbeStep::AddGoal(_source, goal) => nested_goals.push(goal),
@@ -172,6 +188,10 @@ impl<'a, 'tcx> InspectGoal<'a, 'tcx> {
172188
self.candidates_recur(candidates, nested_goals, probe);
173189
nested_goals.truncate(num_goals);
174190
}
191+
inspect::ProbeStep::MakeCandidateWithCertainty(certainty) => {
192+
// FIXME: add assertions that `certainty` is `None`, etc.
193+
candidate_certainty = Some(*certainty);
194+
}
175195
inspect::ProbeStep::EvaluateGoals(_) => (),
176196
}
177197
}
@@ -195,6 +215,7 @@ impl<'a, 'tcx> InspectGoal<'a, 'tcx> {
195215
nested_goals: nested_goals.clone(),
196216
final_state: probe.final_state,
197217
result,
218+
candidate_certainty,
198219
})
199220
}
200221
}
@@ -206,6 +227,7 @@ impl<'a, 'tcx> InspectGoal<'a, 'tcx> {
206227
nested_goals: nested_goals.clone(),
207228
final_state: probe.final_state,
208229
result,
230+
candidate_certainty,
209231
});
210232
}
211233
}

compiler/rustc_trait_selection/src/solve/inspect/build.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,7 @@ enum WipProbeStep<'tcx> {
241241
AddGoal(GoalSource, inspect::CanonicalState<'tcx, Goal<'tcx, ty::Predicate<'tcx>>>),
242242
EvaluateGoals(WipAddedGoalsEvaluation<'tcx>),
243243
NestedProbe(WipProbe<'tcx>),
244+
MakeCandidateWithCertainty(Certainty),
244245
}
245246

246247
impl<'tcx> WipProbeStep<'tcx> {
@@ -249,6 +250,9 @@ impl<'tcx> WipProbeStep<'tcx> {
249250
WipProbeStep::AddGoal(source, goal) => inspect::ProbeStep::AddGoal(source, goal),
250251
WipProbeStep::EvaluateGoals(eval) => inspect::ProbeStep::EvaluateGoals(eval.finalize()),
251252
WipProbeStep::NestedProbe(probe) => inspect::ProbeStep::NestedProbe(probe.finalize()),
253+
WipProbeStep::MakeCandidateWithCertainty(certainty) => {
254+
inspect::ProbeStep::MakeCandidateWithCertainty(certainty)
255+
}
252256
}
253257
}
254258
}
@@ -530,6 +534,19 @@ impl<'tcx> ProofTreeBuilder<'tcx> {
530534
}
531535
}
532536

537+
pub fn add_certainty(&mut self, certainty: Certainty) {
538+
match self.as_mut() {
539+
Some(DebugSolver::GoalEvaluationStep(state)) => {
540+
state
541+
.current_evaluation_scope()
542+
.steps
543+
.push(WipProbeStep::MakeCandidateWithCertainty(certainty));
544+
}
545+
None => {}
546+
_ => {}
547+
}
548+
}
549+
533550
pub fn finish_probe(mut self) -> ProofTreeBuilder<'tcx> {
534551
match self.as_mut() {
535552
None => {}

0 commit comments

Comments
 (0)