Skip to content

Commit b5a0d26

Browse files
committed
try_normalize_ty end with rigid alias on failure
1 parent ccb160d commit b5a0d26

File tree

9 files changed

+125
-30
lines changed

9 files changed

+125
-30
lines changed

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

+5
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,8 @@ pub enum ProbeStep<'tcx> {
128128
/// used whenever there are multiple candidates to prove the
129129
/// current goalby .
130130
NestedProbe(Probe<'tcx>),
131+
CommitIfOkStart,
132+
CommitIfOkSuccess,
131133
}
132134

133135
/// What kind of probe we're in. In case the probe represents a candidate, or
@@ -148,6 +150,9 @@ pub enum ProbeKind<'tcx> {
148150
/// Used in the probe that wraps normalizing the non-self type for the unsize
149151
/// trait, which is also structurally matched on.
150152
UnsizeAssembly,
153+
/// A call to `EvalCtxt::commit_if_ok` which failed, causing the work
154+
/// to be discarded.
155+
CommitIfOk,
151156
/// During upcasting from some source object to target object type, used to
152157
/// do a probe to find out what projection type(s) may be used to prove that
153158
/// the source type upholds all of the target type's object bounds.

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

+5
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,9 @@ impl<'a, 'b> ProofTreeFormatter<'a, 'b> {
112112
ProbeKind::UpcastProjectionCompatibility => {
113113
writeln!(self.f, "PROBING FOR PROJECTION COMPATIBILITY FOR UPCASTING:")
114114
}
115+
ProbeKind::CommitIfOk => {
116+
writeln!(self.f, "COMMIT_IF_OK:")
117+
}
115118
ProbeKind::MiscCandidate { name, result } => {
116119
writeln!(self.f, "CANDIDATE {name}: {result:?}")
117120
}
@@ -126,6 +129,8 @@ impl<'a, 'b> ProofTreeFormatter<'a, 'b> {
126129
ProbeStep::AddGoal(goal) => writeln!(this.f, "ADDED GOAL: {goal:?}")?,
127130
ProbeStep::EvaluateGoals(eval) => this.format_added_goals_evaluation(eval)?,
128131
ProbeStep::NestedProbe(probe) => this.format_probe(probe)?,
132+
ProbeStep::CommitIfOkStart => writeln!(this.f, "COMMIT_IF_OK START")?,
133+
ProbeStep::CommitIfOkSuccess => writeln!(this.f, "COMMIT_IF_OK SUCCESS")?,
129134
}
130135
}
131136
Ok(())

compiler/rustc_trait_selection/src/solve/assembly/mod.rs

+5-10
Original file line numberDiff line numberDiff line change
@@ -852,23 +852,18 @@ impl<'tcx> EvalCtxt<'_, 'tcx> {
852852

853853
let result = self.probe_misc_candidate("coherence unknowable").enter(|ecx| {
854854
let trait_ref = goal.predicate.trait_ref(tcx);
855-
856855
#[derive(Debug)]
857-
enum FailureKind {
858-
Overflow,
859-
NoSolution(NoSolution),
860-
}
856+
struct Overflow;
861857
let lazily_normalize_ty = |ty| match ecx.try_normalize_ty(goal.param_env, ty) {
862-
Ok(Some(ty)) => Ok(ty),
863-
Ok(None) => Err(FailureKind::Overflow),
864-
Err(e) => Err(FailureKind::NoSolution(e)),
858+
Some(ty) => Ok(ty),
859+
None => Err(Overflow),
865860
};
866861

867862
match coherence::trait_ref_is_knowable(tcx, trait_ref, lazily_normalize_ty) {
868-
Err(FailureKind::Overflow) => {
863+
Err(Overflow) => {
869864
ecx.evaluate_added_goals_and_make_canonical_response(Certainty::OVERFLOW)
870865
}
871-
Err(FailureKind::NoSolution(NoSolution)) | Ok(Ok(())) => Err(NoSolution),
866+
Ok(Ok(())) => Err(NoSolution),
872867
Ok(Err(_)) => {
873868
ecx.evaluate_added_goals_and_make_canonical_response(Certainty::AMBIGUOUS)
874869
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
use super::EvalCtxt;
2+
use crate::solve::inspect;
3+
use rustc_middle::traits::query::NoSolution;
4+
5+
impl<'a, 'tcx> EvalCtxt<'a, 'tcx> {
6+
pub(in crate::solve) fn commit_if_ok<T>(
7+
&mut self,
8+
f: impl FnOnce(&mut EvalCtxt<'_, 'tcx>) -> Result<T, NoSolution>,
9+
) -> Result<T, NoSolution> {
10+
let mut nested_ecx = EvalCtxt {
11+
infcx: self.infcx,
12+
variables: self.variables,
13+
var_values: self.var_values,
14+
predefined_opaques_in_body: self.predefined_opaques_in_body,
15+
max_input_universe: self.max_input_universe,
16+
search_graph: self.search_graph,
17+
nested_goals: self.nested_goals.clone(),
18+
tainted: self.tainted,
19+
inspect: self.inspect.new_probe(),
20+
};
21+
22+
let result = nested_ecx.infcx.commit_if_ok(|_| f(&mut nested_ecx));
23+
if result.is_ok() {
24+
let EvalCtxt {
25+
infcx: _,
26+
variables: _,
27+
var_values: _,
28+
predefined_opaques_in_body: _,
29+
max_input_universe: _,
30+
search_graph: _,
31+
nested_goals,
32+
tainted,
33+
inspect,
34+
} = nested_ecx;
35+
self.nested_goals = nested_goals;
36+
self.tainted = tainted;
37+
self.inspect.integrate_snapshot(inspect);
38+
} else {
39+
nested_ecx.inspect.probe_kind(inspect::ProbeKind::CommitIfOk);
40+
self.inspect.finish_probe(nested_ecx.inspect);
41+
}
42+
43+
result
44+
}
45+
}

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

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ use super::{search_graph::SearchGraph, Goal};
3434
pub use select::InferCtxtSelectExt;
3535

3636
mod canonical;
37+
mod commit_if_ok;
3738
mod probe;
3839
mod select;
3940

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

+5-2
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,6 @@ impl<'a, 'tcx> InspectGoal<'a, 'tcx> {
121121
for step in &probe.steps {
122122
match step {
123123
&inspect::ProbeStep::AddGoal(goal) => nested_goals.push(goal),
124-
inspect::ProbeStep::EvaluateGoals(_) => (),
125124
inspect::ProbeStep::NestedProbe(ref probe) => {
126125
// Nested probes have to prove goals added in their parent
127126
// but do not leak them, so we truncate the added goals
@@ -130,13 +129,17 @@ impl<'a, 'tcx> InspectGoal<'a, 'tcx> {
130129
self.candidates_recur(candidates, nested_goals, probe);
131130
nested_goals.truncate(num_goals);
132131
}
132+
inspect::ProbeStep::EvaluateGoals(_)
133+
| inspect::ProbeStep::CommitIfOkStart
134+
| inspect::ProbeStep::CommitIfOkSuccess => (),
133135
}
134136
}
135137

136138
match probe.kind {
137139
inspect::ProbeKind::NormalizedSelfTyAssembly
138140
| inspect::ProbeKind::UnsizeAssembly
139-
| inspect::ProbeKind::UpcastProjectionCompatibility => (),
141+
| inspect::ProbeKind::UpcastProjectionCompatibility
142+
| inspect::ProbeKind::CommitIfOk => (),
140143
// We add a candidate for the root evaluation if there
141144
// is only one way to prove a given goal, e.g. for `WellFormed`.
142145
//

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

+27
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,8 @@ enum WipProbeStep<'tcx> {
213213
AddGoal(inspect::CanonicalState<'tcx, Goal<'tcx, ty::Predicate<'tcx>>>),
214214
EvaluateGoals(WipAddedGoalsEvaluation<'tcx>),
215215
NestedProbe(WipProbe<'tcx>),
216+
CommitIfOkStart,
217+
CommitIfOkSuccess,
216218
}
217219

218220
impl<'tcx> WipProbeStep<'tcx> {
@@ -221,6 +223,8 @@ impl<'tcx> WipProbeStep<'tcx> {
221223
WipProbeStep::AddGoal(goal) => inspect::ProbeStep::AddGoal(goal),
222224
WipProbeStep::EvaluateGoals(eval) => inspect::ProbeStep::EvaluateGoals(eval.finalize()),
223225
WipProbeStep::NestedProbe(probe) => inspect::ProbeStep::NestedProbe(probe.finalize()),
226+
WipProbeStep::CommitIfOkStart => inspect::ProbeStep::CommitIfOkStart,
227+
WipProbeStep::CommitIfOkSuccess => inspect::ProbeStep::CommitIfOkSuccess,
224228
}
225229
}
226230
}
@@ -458,6 +462,29 @@ impl<'tcx> ProofTreeBuilder<'tcx> {
458462
}
459463
}
460464

465+
/// Used by `EvalCtxt::commit_if_ok` to flatten the work done inside
466+
/// of the probe into the parent.
467+
pub fn integrate_snapshot(&mut self, probe: ProofTreeBuilder<'tcx>) {
468+
if let Some(this) = self.as_mut() {
469+
match (this, probe.state.unwrap().tree) {
470+
(
471+
DebugSolver::Probe(WipProbe { steps, .. })
472+
| DebugSolver::GoalEvaluationStep(WipGoalEvaluationStep {
473+
evaluation: WipProbe { steps, .. },
474+
..
475+
}),
476+
DebugSolver::Probe(probe),
477+
) => {
478+
steps.push(WipProbeStep::CommitIfOkStart);
479+
assert_eq!(probe.kind, None);
480+
steps.extend(probe.steps);
481+
steps.push(WipProbeStep::CommitIfOkSuccess);
482+
}
483+
_ => unreachable!(),
484+
}
485+
}
486+
}
487+
461488
pub fn new_evaluate_added_goals(&mut self) -> ProofTreeBuilder<'tcx> {
462489
self.nested(|| WipAddedGoalsEvaluation { evaluations: vec![], result: None })
463490
}

compiler/rustc_trait_selection/src/solve/mod.rs

+29-14
Original file line numberDiff line numberDiff line change
@@ -299,25 +299,40 @@ impl<'tcx> EvalCtxt<'_, 'tcx> {
299299
fn try_normalize_ty(
300300
&mut self,
301301
param_env: ty::ParamEnv<'tcx>,
302-
mut ty: Ty<'tcx>,
303-
) -> Result<Option<Ty<'tcx>>, NoSolution> {
304-
for _ in 0..self.local_overflow_limit() {
305-
let ty::Alias(_, projection_ty) = *ty.kind() else {
306-
return Ok(Some(ty));
307-
};
308-
309-
let normalized_ty = self.next_ty_infer();
302+
ty: Ty<'tcx>,
303+
) -> Option<Ty<'tcx>> {
304+
self.try_normalize_ty_recur(param_env, 0, ty)
305+
}
306+
307+
fn try_normalize_ty_recur(
308+
&mut self,
309+
param_env: ty::ParamEnv<'tcx>,
310+
depth: usize,
311+
ty: Ty<'tcx>,
312+
) -> Option<Ty<'tcx>> {
313+
if depth >= self.local_overflow_limit() {
314+
return None;
315+
}
316+
317+
let ty::Alias(_, projection_ty) = *ty.kind() else {
318+
return Some(ty);
319+
};
320+
321+
match self.commit_if_ok(|this| {
322+
let normalized_ty = this.next_ty_infer();
310323
let normalizes_to_goal = Goal::new(
311-
self.tcx(),
324+
this.tcx(),
312325
param_env,
313326
ty::ProjectionPredicate { projection_ty, term: normalized_ty.into() },
314327
);
315-
self.add_goal(normalizes_to_goal);
316-
self.try_evaluate_added_goals()?;
317-
ty = self.resolve_vars_if_possible(normalized_ty);
328+
this.add_goal(normalizes_to_goal);
329+
this.try_evaluate_added_goals()?;
330+
let ty = this.resolve_vars_if_possible(normalized_ty);
331+
Ok(this.try_normalize_ty_recur(param_env, depth + 1, ty))
332+
}) {
333+
Ok(ty) => ty,
334+
Err(NoSolution) => Some(ty),
318335
}
319-
320-
Ok(None)
321336
}
322337
}
323338

compiler/rustc_trait_selection/src/solve/trait_goals.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,7 @@ impl<'tcx> assembly::GoalKind<'tcx> for TraitPredicate<'tcx> {
432432
let a_ty = goal.predicate.self_ty();
433433
// We need to normalize the b_ty since it's destructured as a `dyn Trait`.
434434
let Some(b_ty) =
435-
ecx.try_normalize_ty(goal.param_env, goal.predicate.trait_ref.args.type_at(1))?
435+
ecx.try_normalize_ty(goal.param_env, goal.predicate.trait_ref.args.type_at(1))
436436
else {
437437
return ecx.evaluate_added_goals_and_make_canonical_response(Certainty::OVERFLOW);
438438
};
@@ -499,9 +499,8 @@ impl<'tcx> assembly::GoalKind<'tcx> for TraitPredicate<'tcx> {
499499
let b_ty = match ecx
500500
.try_normalize_ty(goal.param_env, goal.predicate.trait_ref.args.type_at(1))
501501
{
502-
Ok(Some(b_ty)) => b_ty,
503-
Ok(None) => return vec![misc_candidate(ecx, Certainty::OVERFLOW)],
504-
Err(_) => return vec![],
502+
Some(b_ty) => b_ty,
503+
None => return vec![misc_candidate(ecx, Certainty::OVERFLOW)],
505504
};
506505

507506
let goal = goal.with(ecx.tcx(), (a_ty, b_ty));

0 commit comments

Comments
 (0)