1
Fork 0

Uplift inspect into rustc_type_ir

This commit is contained in:
Michael Goulet 2024-05-17 12:16:36 -04:00
parent 05e0f8740a
commit 0f528a4c08
20 changed files with 400 additions and 319 deletions

View file

@ -25,7 +25,7 @@ pub(super) mod structural_traits;
/// and the `result` when using the given `source`.
#[derive(Debug, Clone)]
pub(super) struct Candidate<'tcx> {
pub(super) source: CandidateSource,
pub(super) source: CandidateSource<'tcx>,
pub(super) result: CanonicalResponse<'tcx>,
}
@ -47,7 +47,7 @@ pub(super) trait GoalKind<'tcx>:
/// [`EvalCtxt::evaluate_added_goals_and_make_canonical_response`]).
fn probe_and_match_goal_against_assumption(
ecx: &mut EvalCtxt<'_, 'tcx>,
source: CandidateSource,
source: CandidateSource<'tcx>,
goal: Goal<'tcx, Self>,
assumption: ty::Clause<'tcx>,
then: impl FnOnce(&mut EvalCtxt<'_, 'tcx>) -> QueryResult<'tcx>,
@ -58,7 +58,7 @@ pub(super) trait GoalKind<'tcx>:
/// goal by equating it with the assumption.
fn probe_and_consider_implied_clause(
ecx: &mut EvalCtxt<'_, 'tcx>,
parent_source: CandidateSource,
parent_source: CandidateSource<'tcx>,
goal: Goal<'tcx, Self>,
assumption: ty::Clause<'tcx>,
requirements: impl IntoIterator<Item = (GoalSource, Goal<'tcx, ty::Predicate<'tcx>>)>,
@ -76,7 +76,7 @@ pub(super) trait GoalKind<'tcx>:
/// since they're not implied by the well-formedness of the object type.
fn probe_and_consider_object_bound_candidate(
ecx: &mut EvalCtxt<'_, 'tcx>,
source: CandidateSource,
source: CandidateSource<'tcx>,
goal: Goal<'tcx, Self>,
assumption: ty::Clause<'tcx>,
) -> Result<Candidate<'tcx>, NoSolution> {

View file

@ -40,13 +40,13 @@ trait ResponseT<'tcx> {
fn var_values(&self) -> CanonicalVarValues<'tcx>;
}
impl<'tcx> ResponseT<'tcx> for Response<'tcx> {
impl<'tcx> ResponseT<'tcx> for Response<TyCtxt<'tcx>> {
fn var_values(&self) -> CanonicalVarValues<'tcx> {
self.var_values
}
}
impl<'tcx, T> ResponseT<'tcx> for inspect::State<'tcx, T> {
impl<'tcx, T> ResponseT<'tcx> for inspect::State<TyCtxt<'tcx>, T> {
fn var_values(&self) -> CanonicalVarValues<'tcx> {
self.var_values
}
@ -384,7 +384,7 @@ pub(in crate::solve) fn make_canonical_state<'tcx, T: TypeFoldable<TyCtxt<'tcx>>
var_values: &[ty::GenericArg<'tcx>],
max_input_universe: ty::UniverseIndex,
data: T,
) -> inspect::CanonicalState<'tcx, T> {
) -> inspect::CanonicalState<TyCtxt<'tcx>, T> {
let var_values = CanonicalVarValues { var_values: infcx.tcx.mk_args(var_values) };
let state = inspect::State { var_values, data };
let state = state.fold_with(&mut EagerResolver::new(infcx));
@ -414,7 +414,7 @@ pub(in crate::solve) fn instantiate_canonical_state<'tcx, T: TypeFoldable<TyCtxt
span: Span,
param_env: ty::ParamEnv<'tcx>,
orig_values: &mut Vec<ty::GenericArg<'tcx>>,
state: inspect::CanonicalState<'tcx, T>,
state: inspect::CanonicalState<TyCtxt<'tcx>, T>,
) -> T {
// In case any fresh inference variables have been created between `state`
// and the previous instantiation, extend `orig_values` for it.

View file

@ -147,7 +147,8 @@ impl<'tcx> InferCtxt<'tcx> {
&self,
goal: Goal<'tcx, ty::Predicate<'tcx>>,
generate_proof_tree: GenerateProofTree,
) -> (Result<(bool, Certainty), NoSolution>, Option<inspect::GoalEvaluation<'tcx>>) {
) -> (Result<(bool, Certainty), NoSolution>, Option<inspect::GoalEvaluation<TyCtxt<'tcx>>>)
{
EvalCtxt::enter_root(self, generate_proof_tree, |ecx| {
ecx.evaluate_goal(GoalEvaluationKind::Root, GoalSource::Misc, goal)
})
@ -170,7 +171,7 @@ impl<'a, 'tcx> EvalCtxt<'a, 'tcx> {
infcx: &InferCtxt<'tcx>,
generate_proof_tree: GenerateProofTree,
f: impl FnOnce(&mut EvalCtxt<'_, 'tcx>) -> R,
) -> (R, Option<inspect::GoalEvaluation<'tcx>>) {
) -> (R, Option<inspect::GoalEvaluation<TyCtxt<'tcx>>>) {
let mode = if infcx.intercrate { SolverMode::Coherence } else { SolverMode::Normal };
let mut search_graph = search_graph::SearchGraph::new(mode);

View file

@ -2,10 +2,9 @@ use crate::solve::assembly::Candidate;
use super::EvalCtxt;
use rustc_infer::traits::BuiltinImplSource;
use rustc_middle::traits::{
query::NoSolution,
solve::{inspect, CandidateSource, QueryResult},
};
use rustc_middle::traits::query::NoSolution;
use rustc_middle::traits::solve::{inspect, CandidateSource, QueryResult};
use rustc_middle::ty::TyCtxt;
use std::marker::PhantomData;
pub(in crate::solve) struct ProbeCtxt<'me, 'a, 'tcx, F, T> {
@ -16,7 +15,7 @@ pub(in crate::solve) struct ProbeCtxt<'me, 'a, 'tcx, F, T> {
impl<'tcx, F, T> ProbeCtxt<'_, '_, 'tcx, F, T>
where
F: FnOnce(&T) -> inspect::ProbeKind<'tcx>,
F: FnOnce(&T) -> inspect::ProbeKind<TyCtxt<'tcx>>,
{
pub(in crate::solve) fn enter(self, f: impl FnOnce(&mut EvalCtxt<'_, 'tcx>) -> T) -> T {
let ProbeCtxt { ecx: outer_ecx, probe_kind, _result } = self;
@ -51,12 +50,12 @@ where
pub(in crate::solve) struct TraitProbeCtxt<'me, 'a, 'tcx, F> {
cx: ProbeCtxt<'me, 'a, 'tcx, F, QueryResult<'tcx>>,
source: CandidateSource,
source: CandidateSource<'tcx>,
}
impl<'tcx, F> TraitProbeCtxt<'_, '_, 'tcx, F>
where
F: FnOnce(&QueryResult<'tcx>) -> inspect::ProbeKind<'tcx>,
F: FnOnce(&QueryResult<'tcx>) -> inspect::ProbeKind<TyCtxt<'tcx>>,
{
#[instrument(level = "debug", skip_all, fields(source = ?self.source))]
pub(in crate::solve) fn enter(
@ -72,7 +71,7 @@ impl<'a, 'tcx> EvalCtxt<'a, 'tcx> {
/// as expensive as necessary to output the desired information.
pub(in crate::solve) fn probe<F, T>(&mut self, probe_kind: F) -> ProbeCtxt<'_, 'a, 'tcx, F, T>
where
F: FnOnce(&T) -> inspect::ProbeKind<'tcx>,
F: FnOnce(&T) -> inspect::ProbeKind<TyCtxt<'tcx>>,
{
ProbeCtxt { ecx: self, probe_kind, _result: PhantomData }
}
@ -80,16 +79,24 @@ impl<'a, 'tcx> EvalCtxt<'a, 'tcx> {
pub(in crate::solve) fn probe_builtin_trait_candidate(
&mut self,
source: BuiltinImplSource,
) -> TraitProbeCtxt<'_, 'a, 'tcx, impl FnOnce(&QueryResult<'tcx>) -> inspect::ProbeKind<'tcx>>
{
) -> TraitProbeCtxt<
'_,
'a,
'tcx,
impl FnOnce(&QueryResult<'tcx>) -> inspect::ProbeKind<TyCtxt<'tcx>>,
> {
self.probe_trait_candidate(CandidateSource::BuiltinImpl(source))
}
pub(in crate::solve) fn probe_trait_candidate(
&mut self,
source: CandidateSource,
) -> TraitProbeCtxt<'_, 'a, 'tcx, impl FnOnce(&QueryResult<'tcx>) -> inspect::ProbeKind<'tcx>>
{
source: CandidateSource<'tcx>,
) -> TraitProbeCtxt<
'_,
'a,
'tcx,
impl FnOnce(&QueryResult<'tcx>) -> inspect::ProbeKind<TyCtxt<'tcx>>,
> {
TraitProbeCtxt {
cx: ProbeCtxt {
ecx: self,

View file

@ -3,7 +3,6 @@ use std::ops::ControlFlow;
use rustc_infer::infer::InferCtxt;
use rustc_infer::traits::query::NoSolution;
use rustc_infer::traits::solve::inspect::ProbeKind;
use rustc_infer::traits::solve::{CandidateSource, GoalSource, MaybeCause};
use rustc_infer::traits::{
self, FulfillmentError, FulfillmentErrorCode, MismatchedProjectionTypes, Obligation,
@ -15,7 +14,7 @@ use rustc_middle::ty::{self, TyCtxt};
use rustc_span::symbol::sym;
use super::eval_ctxt::GenerateProofTree;
use super::inspect::{InspectCandidate, InspectGoal, ProofTreeInferCtxtExt, ProofTreeVisitor};
use super::inspect::{self, ProofTreeInferCtxtExt, ProofTreeVisitor};
use super::{Certainty, InferCtxtEvalExt};
/// A trait engine using the new trait solver.
@ -319,8 +318,8 @@ impl<'tcx> BestObligation<'tcx> {
/// *don't* hold and which have impl-where clauses that also don't hold.
fn non_trivial_candidates<'a>(
&self,
goal: &'a InspectGoal<'a, 'tcx>,
) -> Vec<InspectCandidate<'a, 'tcx>> {
goal: &'a inspect::InspectGoal<'a, 'tcx>,
) -> Vec<inspect::InspectCandidate<'a, 'tcx>> {
let mut candidates = goal.candidates();
match self.consider_ambiguities {
true => {
@ -370,15 +369,17 @@ impl<'tcx> ProofTreeVisitor<'tcx> for BestObligation<'tcx> {
self.obligation.cause.span
}
fn visit_goal(&mut self, goal: &super::inspect::InspectGoal<'_, 'tcx>) -> Self::Result {
fn visit_goal(&mut self, goal: &inspect::InspectGoal<'_, 'tcx>) -> Self::Result {
let candidates = self.non_trivial_candidates(goal);
let [candidate] = candidates.as_slice() else {
return ControlFlow::Break(self.obligation.clone());
};
// Don't walk into impls that have `do_not_recommend`.
if let ProbeKind::TraitCandidate { source: CandidateSource::Impl(impl_def_id), result: _ } =
candidate.kind()
if let inspect::ProbeKind::TraitCandidate {
source: CandidateSource::Impl(impl_def_id),
result: _,
} = candidate.kind()
&& goal.infcx().tcx.has_attr(impl_def_id, sym::do_not_recommend)
{
return ControlFlow::Break(self.obligation.clone());
@ -475,13 +476,16 @@ enum ChildMode<'tcx> {
fn derive_cause<'tcx>(
tcx: TyCtxt<'tcx>,
candidate_kind: ProbeKind<'tcx>,
candidate_kind: inspect::ProbeKind<TyCtxt<'tcx>>,
mut cause: ObligationCause<'tcx>,
idx: usize,
parent_trait_pred: ty::PolyTraitPredicate<'tcx>,
) -> ObligationCause<'tcx> {
match candidate_kind {
ProbeKind::TraitCandidate { source: CandidateSource::Impl(impl_def_id), result: _ } => {
inspect::ProbeKind::TraitCandidate {
source: CandidateSource::Impl(impl_def_id),
result: _,
} => {
if let Some((_, span)) =
tcx.predicates_of(impl_def_id).instantiate_identity(tcx).iter().nth(idx)
{
@ -495,7 +499,10 @@ fn derive_cause<'tcx>(
})
}
}
ProbeKind::TraitCandidate { source: CandidateSource::BuiltinImpl(..), result: _ } => {
inspect::ProbeKind::TraitCandidate {
source: CandidateSource::BuiltinImpl(..),
result: _,
} => {
cause = cause.derived_cause(parent_trait_pred, ObligationCauseCode::BuiltinDerived);
}
_ => {}

View file

@ -18,7 +18,7 @@ use rustc_middle::traits::query::NoSolution;
use rustc_middle::traits::solve::{inspect, QueryResult};
use rustc_middle::traits::solve::{Certainty, Goal};
use rustc_middle::traits::ObligationCause;
use rustc_middle::ty::TypeFoldable;
use rustc_middle::ty::{TyCtxt, TypeFoldable};
use rustc_middle::{bug, ty};
use rustc_span::{Span, DUMMY_SP};
@ -37,7 +37,7 @@ pub struct InspectGoal<'a, 'tcx> {
orig_values: Vec<ty::GenericArg<'tcx>>,
goal: Goal<'tcx, ty::Predicate<'tcx>>,
result: Result<Certainty, NoSolution>,
evaluation_kind: inspect::CanonicalGoalEvaluationKind<'tcx>,
evaluation_kind: inspect::CanonicalGoalEvaluationKind<TyCtxt<'tcx>>,
normalizes_to_term_hack: Option<NormalizesToTermHack<'tcx>>,
source: GoalSource,
}
@ -88,16 +88,17 @@ impl<'tcx> NormalizesToTermHack<'tcx> {
pub struct InspectCandidate<'a, 'tcx> {
goal: &'a InspectGoal<'a, 'tcx>,
kind: inspect::ProbeKind<'tcx>,
nested_goals: Vec<(GoalSource, inspect::CanonicalState<'tcx, Goal<'tcx, ty::Predicate<'tcx>>>)>,
final_state: inspect::CanonicalState<'tcx, ()>,
impl_args: Option<inspect::CanonicalState<'tcx, ty::GenericArgsRef<'tcx>>>,
kind: inspect::ProbeKind<TyCtxt<'tcx>>,
nested_goals:
Vec<(GoalSource, inspect::CanonicalState<TyCtxt<'tcx>, Goal<'tcx, ty::Predicate<'tcx>>>)>,
final_state: inspect::CanonicalState<TyCtxt<'tcx>, ()>,
impl_args: Option<inspect::CanonicalState<TyCtxt<'tcx>, ty::GenericArgsRef<'tcx>>>,
result: QueryResult<'tcx>,
shallow_certainty: Certainty,
}
impl<'a, 'tcx> InspectCandidate<'a, 'tcx> {
pub fn kind(&self) -> inspect::ProbeKind<'tcx> {
pub fn kind(&self) -> inspect::ProbeKind<TyCtxt<'tcx>> {
self.kind
}
@ -280,9 +281,9 @@ impl<'a, 'tcx> InspectGoal<'a, 'tcx> {
candidates: &mut Vec<InspectCandidate<'a, 'tcx>>,
nested_goals: &mut Vec<(
GoalSource,
inspect::CanonicalState<'tcx, Goal<'tcx, ty::Predicate<'tcx>>>,
inspect::CanonicalState<TyCtxt<'tcx>, Goal<'tcx, ty::Predicate<'tcx>>>,
)>,
probe: &inspect::Probe<'tcx>,
probe: &inspect::Probe<TyCtxt<'tcx>>,
) {
let mut shallow_certainty = None;
let mut impl_args = None;
@ -387,7 +388,7 @@ impl<'a, 'tcx> InspectGoal<'a, 'tcx> {
fn new(
infcx: &'a InferCtxt<'tcx>,
depth: usize,
root: inspect::GoalEvaluation<'tcx>,
root: inspect::GoalEvaluation<TyCtxt<'tcx>>,
normalizes_to_term_hack: Option<NormalizesToTermHack<'tcx>>,
source: GoalSource,
) -> Self {

View file

@ -80,7 +80,7 @@ struct WipGoalEvaluation<'tcx> {
}
impl<'tcx> WipGoalEvaluation<'tcx> {
fn finalize(self) -> inspect::GoalEvaluation<'tcx> {
fn finalize(self) -> inspect::GoalEvaluation<TyCtxt<'tcx>> {
inspect::GoalEvaluation {
uncanonicalized_goal: self.uncanonicalized_goal,
kind: match self.kind {
@ -105,7 +105,7 @@ pub(in crate::solve) enum WipCanonicalGoalEvaluationKind<'tcx> {
Overflow,
CycleInStack,
ProvisionalCacheHit,
Interned { revisions: &'tcx [inspect::GoalEvaluationStep<'tcx>] },
Interned { revisions: &'tcx [inspect::GoalEvaluationStep<TyCtxt<'tcx>>] },
}
impl std::fmt::Debug for WipCanonicalGoalEvaluationKind<'_> {
@ -130,7 +130,7 @@ struct WipCanonicalGoalEvaluation<'tcx> {
}
impl<'tcx> WipCanonicalGoalEvaluation<'tcx> {
fn finalize(self) -> inspect::CanonicalGoalEvaluation<'tcx> {
fn finalize(self) -> inspect::CanonicalGoalEvaluation<TyCtxt<'tcx>> {
assert!(self.revisions.is_empty());
let kind = match self.kind.unwrap() {
WipCanonicalGoalEvaluationKind::Overflow => {
@ -158,7 +158,7 @@ struct WipAddedGoalsEvaluation<'tcx> {
}
impl<'tcx> WipAddedGoalsEvaluation<'tcx> {
fn finalize(self) -> inspect::AddedGoalsEvaluation<'tcx> {
fn finalize(self) -> inspect::AddedGoalsEvaluation<TyCtxt<'tcx>> {
inspect::AddedGoalsEvaluation {
evaluations: self
.evaluations
@ -209,7 +209,7 @@ impl<'tcx> WipGoalEvaluationStep<'tcx> {
}
}
fn finalize(self) -> inspect::GoalEvaluationStep<'tcx> {
fn finalize(self) -> inspect::GoalEvaluationStep<TyCtxt<'tcx>> {
let evaluation = self.evaluation.finalize();
match evaluation.kind {
inspect::ProbeKind::Root { .. } => (),
@ -223,12 +223,12 @@ impl<'tcx> WipGoalEvaluationStep<'tcx> {
struct WipProbe<'tcx> {
initial_num_var_values: usize,
steps: Vec<WipProbeStep<'tcx>>,
kind: Option<inspect::ProbeKind<'tcx>>,
final_state: Option<inspect::CanonicalState<'tcx, ()>>,
kind: Option<inspect::ProbeKind<TyCtxt<'tcx>>>,
final_state: Option<inspect::CanonicalState<TyCtxt<'tcx>, ()>>,
}
impl<'tcx> WipProbe<'tcx> {
fn finalize(self) -> inspect::Probe<'tcx> {
fn finalize(self) -> inspect::Probe<TyCtxt<'tcx>> {
inspect::Probe {
steps: self.steps.into_iter().map(WipProbeStep::finalize).collect(),
kind: self.kind.unwrap(),
@ -239,15 +239,15 @@ impl<'tcx> WipProbe<'tcx> {
#[derive(Eq, PartialEq, Debug)]
enum WipProbeStep<'tcx> {
AddGoal(GoalSource, inspect::CanonicalState<'tcx, Goal<'tcx, ty::Predicate<'tcx>>>),
AddGoal(GoalSource, inspect::CanonicalState<TyCtxt<'tcx>, Goal<'tcx, ty::Predicate<'tcx>>>),
EvaluateGoals(WipAddedGoalsEvaluation<'tcx>),
NestedProbe(WipProbe<'tcx>),
MakeCanonicalResponse { shallow_certainty: Certainty },
RecordImplArgs { impl_args: inspect::CanonicalState<'tcx, ty::GenericArgsRef<'tcx>> },
RecordImplArgs { impl_args: inspect::CanonicalState<TyCtxt<'tcx>, ty::GenericArgsRef<'tcx>> },
}
impl<'tcx> WipProbeStep<'tcx> {
fn finalize(self) -> inspect::ProbeStep<'tcx> {
fn finalize(self) -> inspect::ProbeStep<TyCtxt<'tcx>> {
match self {
WipProbeStep::AddGoal(source, goal) => inspect::ProbeStep::AddGoal(source, goal),
WipProbeStep::EvaluateGoals(eval) => inspect::ProbeStep::EvaluateGoals(eval.finalize()),
@ -281,7 +281,7 @@ impl<'tcx> ProofTreeBuilder<'tcx> {
nested
}
pub fn finalize(self) -> Option<inspect::GoalEvaluation<'tcx>> {
pub fn finalize(self) -> Option<inspect::GoalEvaluation<TyCtxt<'tcx>>> {
match *self.state? {
DebugSolver::GoalEvaluation(wip_goal_evaluation) => {
Some(wip_goal_evaluation.finalize())
@ -356,7 +356,7 @@ impl<'tcx> ProofTreeBuilder<'tcx> {
pub fn finalize_evaluation(
&mut self,
tcx: TyCtxt<'tcx>,
) -> Option<&'tcx [inspect::GoalEvaluationStep<'tcx>]> {
) -> Option<&'tcx [inspect::GoalEvaluationStep<TyCtxt<'tcx>>]> {
self.as_mut().map(|this| match this {
DebugSolver::CanonicalGoalEvaluation(evaluation) => {
let revisions = mem::take(&mut evaluation.revisions)
@ -474,7 +474,7 @@ impl<'tcx> ProofTreeBuilder<'tcx> {
}
}
pub fn probe_kind(&mut self, probe_kind: inspect::ProbeKind<'tcx>) {
pub fn probe_kind(&mut self, probe_kind: inspect::ProbeKind<TyCtxt<'tcx>>) {
match self.as_mut() {
None => {}
Some(DebugSolver::GoalEvaluationStep(state)) => {

View file

@ -74,7 +74,7 @@ enum GoalEvaluationKind {
}
#[extension(trait CanonicalResponseExt)]
impl<'tcx> Canonical<'tcx, Response<'tcx>> {
impl<'tcx> Canonical<'tcx, Response<TyCtxt<'tcx>>> {
fn has_no_inference_or_external_constraints(&self) -> bool {
self.value.external_constraints.region_constraints.is_empty()
&& self.value.var_values.is_identity()

View file

@ -108,7 +108,7 @@ impl<'tcx> assembly::GoalKind<'tcx> for NormalizesTo<'tcx> {
fn probe_and_match_goal_against_assumption(
ecx: &mut EvalCtxt<'_, 'tcx>,
source: CandidateSource,
source: CandidateSource<'tcx>,
goal: Goal<'tcx, Self>,
assumption: ty::Clause<'tcx>,
then: impl FnOnce(&mut EvalCtxt<'_, 'tcx>) -> QueryResult<'tcx>,

View file

@ -103,7 +103,7 @@ impl<'tcx> assembly::GoalKind<'tcx> for TraitPredicate<'tcx> {
fn probe_and_match_goal_against_assumption(
ecx: &mut EvalCtxt<'_, 'tcx>,
source: CandidateSource,
source: CandidateSource<'tcx>,
goal: Goal<'tcx, Self>,
assumption: ty::Clause<'tcx>,
then: impl FnOnce(&mut EvalCtxt<'_, 'tcx>) -> QueryResult<'tcx>,
@ -821,7 +821,7 @@ impl<'tcx> EvalCtxt<'_, 'tcx> {
fn consider_builtin_upcast_to_principal(
&mut self,
goal: Goal<'tcx, (Ty<'tcx>, Ty<'tcx>)>,
source: CandidateSource,
source: CandidateSource<'tcx>,
a_data: &'tcx ty::List<ty::PolyExistentialPredicate<'tcx>>,
a_region: ty::Region<'tcx>,
b_data: &'tcx ty::List<ty::PolyExistentialPredicate<'tcx>>,
@ -1149,7 +1149,7 @@ impl<'tcx> EvalCtxt<'_, 'tcx> {
/// wrapped in one.
fn probe_and_evaluate_goal_for_constituent_tys(
&mut self,
source: CandidateSource,
source: CandidateSource<'tcx>,
goal: Goal<'tcx, TraitPredicate<'tcx>>,
constituent_tys: impl Fn(
&EvalCtxt<'_, 'tcx>,