Auto merge of #122385 - lcnr:analyze-obligations-for-infer, r=compiler-errors

`obligations_for_self_ty`: use `ProofTreeVisitor` for nested goals

As always, dealing with proof trees continues to be a hacked together mess. After this PR and #124380 the only remaining blocker for core is https://github.com/rust-lang/trait-system-refactor-initiative/issues/90. There is also a `ProofTreeVisitor` issue causing an ICE when compiling `alloc` which I will handle in a separate PR. This issue likely affects coherence diagnostics more generally.

The core idea is to extend the proof tree visitor to support visiting nested candidates without using a `probe`. We then simply recurse into nested candidates if they are the only potentially applicable candidate for a given goal and check whether the self type matches the expected one.

For that to work, we need to improve `CanonicalState` to also handle unconstrained inference variables created inside of the trait solver. This is done by extending the `var_values` of `CanoncalState` with each fresh inference variables. Furthermore, we also store the state of all inference variables at the end of each probe. When recursing into `InspectCandidates` we then unify the values of all these states.

r? `@compiler-errors`
This commit is contained in:
bors
2024-04-26 15:37:05 +00:00
25 changed files with 686 additions and 359 deletions

View File

@@ -30,7 +30,7 @@ use rustc_middle::ty::fast_reject::{DeepRejectCtxt, TreatParams};
use rustc_middle::ty::visit::{TypeVisitable, TypeVisitableExt};
use rustc_middle::ty::{self, Ty, TyCtxt, TypeSuperVisitable, TypeVisitor};
use rustc_span::symbol::sym;
use rustc_span::DUMMY_SP;
use rustc_span::{Span, DUMMY_SP};
use std::fmt::Debug;
use std::ops::ControlFlow;
@@ -1022,10 +1022,14 @@ struct AmbiguityCausesVisitor<'a, 'tcx> {
}
impl<'a, 'tcx> ProofTreeVisitor<'tcx> for AmbiguityCausesVisitor<'a, 'tcx> {
fn span(&self) -> Span {
DUMMY_SP
}
fn visit_goal(&mut self, goal: &InspectGoal<'_, 'tcx>) {
let infcx = goal.infcx();
for cand in goal.candidates() {
cand.visit_nested(self);
cand.visit_nested_in_probe(self);
}
// When searching for intercrate ambiguity causes, we only need to look
// at ambiguous goals, as for others the coherence unknowable candidate
@@ -1157,5 +1161,5 @@ fn search_ambiguity_causes<'tcx>(
goal: Goal<'tcx, ty::Predicate<'tcx>>,
causes: &mut FxIndexSet<IntercrateAmbiguityCause<'tcx>>,
) {
infcx.visit_proof_tree(goal, &mut AmbiguityCausesVisitor { causes });
infcx.probe(|_| infcx.visit_proof_tree(goal, &mut AmbiguityCausesVisitor { causes }));
}