Skip to content

Commit 4fd7739

Browse files
Track if EvalCtxt has been tainted, make sure it can't be used to make query responses after
1 parent b9fd498 commit 4fd7739

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

compiler/rustc_trait_selection/src/solve/eval_ctxt.rs

+14
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,13 @@ pub struct EvalCtxt<'a, 'tcx> {
5757
pub(super) search_graph: &'a mut SearchGraph<'tcx>,
5858

5959
pub(super) nested_goals: NestedGoals<'tcx>,
60+
61+
// Has this `EvalCtxt` errored out with `NoSolution` in `try_evaluate_added_goals`?
62+
//
63+
// If so, then it can no longer be used to make a canonical query response,
64+
// since subsequent calls to `try_evaluate_added_goals` have possibly dropped
65+
// ambiguous goals. Instead, use a probe.
66+
tainted: bool,
6067
}
6168

6269
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
@@ -121,6 +128,7 @@ impl<'tcx> InferCtxtEvalExt<'tcx> for InferCtxt<'tcx> {
121128
max_input_universe: ty::UniverseIndex::ROOT,
122129
var_values: CanonicalVarValues::dummy(),
123130
nested_goals: NestedGoals::new(),
131+
tainted: false,
124132
};
125133
let result = ecx.evaluate_goal(IsNormalizesToHack::No, goal);
126134

@@ -172,6 +180,7 @@ impl<'a, 'tcx> EvalCtxt<'a, 'tcx> {
172180
max_input_universe: canonical_goal.max_universe,
173181
search_graph,
174182
nested_goals: NestedGoals::new(),
183+
tainted: false,
175184
};
176185
ecx.compute_goal(goal)
177186
})
@@ -391,6 +400,10 @@ impl<'a, 'tcx> EvalCtxt<'a, 'tcx> {
391400
},
392401
);
393402

403+
if response.is_err() {
404+
self.tainted = true;
405+
}
406+
394407
self.nested_goals = goals;
395408
response
396409
}
@@ -404,6 +417,7 @@ impl<'tcx> EvalCtxt<'_, 'tcx> {
404417
max_input_universe: self.max_input_universe,
405418
search_graph: self.search_graph,
406419
nested_goals: self.nested_goals.clone(),
420+
tainted: self.tainted,
407421
};
408422
self.infcx.probe(|_| f(&mut ecx))
409423
}

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

+6
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,12 @@ impl<'tcx> EvalCtxt<'_, 'tcx> {
5151
certainty: Certainty,
5252
) -> QueryResult<'tcx> {
5353
let goals_certainty = self.try_evaluate_added_goals()?;
54+
assert!(
55+
!self.tainted,
56+
"EvalCtxt is tainted -- nested goals may have been dropped in a \
57+
previous call to `try_evaluate_added_goals!`"
58+
);
59+
5460
let certainty = certainty.unify_with(goals_certainty);
5561

5662
let external_constraints = self.compute_external_query_constraints()?;

0 commit comments

Comments
 (0)