Skip to content

Commit c5c004e

Browse files
committed
document stuff and improve structure
1 parent 8a9b2fe commit c5c004e

File tree

3 files changed

+165
-85
lines changed

3 files changed

+165
-85
lines changed

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

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,23 @@
1+
//! Data structure used to inspect trait solver behavior.
2+
//!
3+
//! During trait solving we optionally build "proof trees", the root of
4+
//! which is a [GoalEvaluation] with [GoalEvaluationKind::Root]. These
5+
//! trees are used to improve the debug experience and are also used by
6+
//! the compiler itself to provide necessary context for error messages.
7+
//!
8+
//! Because each nested goal in the solver gets [canonicalized] separately
9+
//! and we discard inference progress via "probes", we cannot mechanically
10+
//! use proof trees without somehow "lifting up" data local to the current
11+
//! `InferCtxt`. Any data used mechanically is therefore canonicalized and
12+
//! stored as [CanonicalState]. As printing canonicalized data worsens the
13+
//! debugging dumps, we do not simply canonicalize everything.
14+
//!
15+
//! This means proof trees contain inference variables and placeholders
16+
//! local to a different `InferCtxt` which must not be used with the
17+
//! current one.
18+
//!
19+
//! [canonicalized]: https://rustc-dev-guide.rust-lang.org/solve/canonicalization.html
20+
121
use super::{
222
CandidateSource, Canonical, CanonicalInput, Certainty, Goal, IsNormalizesToHack, NoSolution,
323
QueryInput, QueryResult,
@@ -8,6 +28,12 @@ use std::fmt::{Debug, Write};
828

929
mod format;
1030

31+
/// Some `data` together with information about how they relate to the input
32+
/// of the canonical query.
33+
///
34+
/// This is only ever used as [CanonicalState]. Any type information in proof
35+
/// trees used mechanically has to be canonicalized as we otherwise leak
36+
/// inference variables from a nested `InferCtxt`.
1137
#[derive(Debug, Clone, Copy, Eq, PartialEq, TypeFoldable, TypeVisitable)]
1238
pub struct State<'tcx, T> {
1339
pub var_values: CanonicalVarValues<'tcx>,
@@ -22,6 +48,11 @@ pub enum CacheHit {
2248
Global,
2349
}
2450

51+
/// When evaluating the root goals we also store the
52+
/// original values for the `CanonicalVarValues` of the
53+
/// canonicalized goal to map any [CanonicalState]
54+
/// from the `InferCtxt` local to the solver query to
55+
/// the `InferCtxt` of the caller.
2556
#[derive(Eq, PartialEq)]
2657
pub enum GoalEvaluationKind<'tcx> {
2758
Root { orig_values: Vec<ty::GenericArg<'tcx>> },
@@ -33,6 +64,7 @@ pub struct GoalEvaluation<'tcx> {
3364
pub uncanonicalized_goal: Goal<'tcx, ty::Predicate<'tcx>>,
3465
pub kind: GoalEvaluationKind<'tcx>,
3566
pub evaluation: CanonicalGoalEvaluation<'tcx>,
67+
/// The nested goals from instantiating the query response.
3668
pub returned_goals: Vec<Goal<'tcx, ty::Predicate<'tcx>>>,
3769
}
3870

@@ -74,6 +106,7 @@ pub struct GoalEvaluationStep<'tcx> {
74106
/// of a goal.
75107
#[derive(Eq, PartialEq)]
76108
pub struct Probe<'tcx> {
109+
/// What happened inside of this probe in chronological order.
77110
pub steps: Vec<ProbeStep<'tcx>>,
78111
pub kind: ProbeKind<'tcx>,
79112
}
@@ -86,11 +119,20 @@ impl Debug for Probe<'_> {
86119

87120
#[derive(Eq, PartialEq)]
88121
pub enum ProbeStep<'tcx> {
122+
/// We added a goal to the `EvalCtxt` which will get proven
123+
/// the next time `EvalCtxt::try_evaluate_added_goals` is called.
89124
AddGoal(CanonicalState<'tcx, Goal<'tcx, ty::Predicate<'tcx>>>),
125+
/// The inside of a `EvalCtxt::try_evaluate_added_goals` call.
90126
EvaluateGoals(AddedGoalsEvaluation<'tcx>),
127+
/// A call to `probe` while proving the current goal. This is
128+
/// used whenever there are multiple candidates to prove the
129+
/// current goalby .
91130
NestedProbe(Probe<'tcx>),
92131
}
93132

133+
/// What kind of probe we're in. In case the probe represents a candidate, or
134+
/// the final result - via [ProbeKind::Root] - we also store the result for
135+
/// the candidate.
94136
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
95137
pub enum ProbeKind<'tcx> {
96138
/// The root inference context while proving a goal.

0 commit comments

Comments
 (0)