Skip to content

Commit 13ce229

Browse files
committed
refactor analyse visitor to instantiate states in order
1 parent 4d5a9bc commit 13ce229

File tree

1 file changed

+41
-45
lines changed
  • compiler/rustc_trait_selection/src/solve/inspect

1 file changed

+41
-45
lines changed

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

+41-45
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,8 @@ impl<'tcx> NormalizesToTermHack<'tcx> {
8989
pub struct InspectCandidate<'a, 'tcx> {
9090
goal: &'a InspectGoal<'a, 'tcx>,
9191
kind: inspect::ProbeKind<TyCtxt<'tcx>>,
92-
nested_goals:
93-
Vec<(GoalSource, inspect::CanonicalState<TyCtxt<'tcx>, Goal<'tcx, ty::Predicate<'tcx>>>)>,
92+
steps: Vec<&'a inspect::ProbeStep<TyCtxt<'tcx>>>,
9493
final_state: inspect::CanonicalState<TyCtxt<'tcx>, ()>,
95-
impl_args: Option<inspect::CanonicalState<TyCtxt<'tcx>, ty::GenericArgsRef<'tcx>>>,
9694
result: QueryResult<'tcx>,
9795
shallow_certainty: Certainty,
9896
}
@@ -148,7 +146,7 @@ impl<'a, 'tcx> InspectCandidate<'a, 'tcx> {
148146
#[instrument(
149147
level = "debug",
150148
skip_all,
151-
fields(goal = ?self.goal.goal, nested_goals = ?self.nested_goals)
149+
fields(goal = ?self.goal.goal, steps = ?self.steps)
152150
)]
153151
pub fn instantiate_nested_goals_and_opt_impl_args(
154152
&self,
@@ -157,22 +155,34 @@ impl<'a, 'tcx> InspectCandidate<'a, 'tcx> {
157155
let infcx = self.goal.infcx;
158156
let param_env = self.goal.goal.param_env;
159157
let mut orig_values = self.goal.orig_values.to_vec();
160-
let instantiated_goals: Vec<_> = self
161-
.nested_goals
162-
.iter()
163-
.map(|(source, goal)| {
164-
(
165-
*source,
158+
159+
let mut instantiated_goals = vec![];
160+
let mut opt_impl_args = None;
161+
for step in &self.steps {
162+
match **step {
163+
inspect::ProbeStep::AddGoal(source, goal) => instantiated_goals.push((
164+
source,
166165
canonical::instantiate_canonical_state(
167166
infcx,
168167
span,
169168
param_env,
170169
&mut orig_values,
171-
*goal,
170+
goal,
172171
),
173-
)
174-
})
175-
.collect();
172+
)),
173+
inspect::ProbeStep::RecordImplArgs { impl_args } => {
174+
opt_impl_args = Some(canonical::instantiate_canonical_state(
175+
infcx,
176+
span,
177+
param_env,
178+
&mut orig_values,
179+
impl_args,
180+
));
181+
}
182+
inspect::ProbeStep::MakeCanonicalResponse { .. }
183+
| inspect::ProbeStep::NestedProbe(_) => unreachable!(),
184+
}
185+
}
176186

177187
let () = canonical::instantiate_canonical_state(
178188
infcx,
@@ -182,24 +192,16 @@ impl<'a, 'tcx> InspectCandidate<'a, 'tcx> {
182192
self.final_state,
183193
);
184194

185-
let impl_args = self.impl_args.map(|impl_args| {
186-
canonical::instantiate_canonical_state(
187-
infcx,
188-
span,
189-
param_env,
190-
&mut orig_values,
191-
impl_args,
192-
)
193-
.fold_with(&mut EagerResolver::new(infcx))
194-
});
195-
196195
if let Some(term_hack) = self.goal.normalizes_to_term_hack {
197196
// FIXME: We ignore the expected term of `NormalizesTo` goals
198197
// when computing the result of its candidates. This is
199198
// scuffed.
200199
let _ = term_hack.constrain(infcx, span, param_env);
201200
}
202201

202+
let opt_impl_args =
203+
opt_impl_args.map(|impl_args| impl_args.fold_with(&mut EagerResolver::new(infcx)));
204+
203205
let goals = instantiated_goals
204206
.into_iter()
205207
.map(|(source, goal)| match goal.predicate.kind().no_bound_vars() {
@@ -249,7 +251,7 @@ impl<'a, 'tcx> InspectCandidate<'a, 'tcx> {
249251
})
250252
.collect();
251253

252-
(goals, impl_args)
254+
(goals, opt_impl_args)
253255
}
254256

255257
/// Visit all nested goals of this candidate, rolling back
@@ -279,17 +281,18 @@ impl<'a, 'tcx> InspectGoal<'a, 'tcx> {
279281
fn candidates_recur(
280282
&'a self,
281283
candidates: &mut Vec<InspectCandidate<'a, 'tcx>>,
282-
nested_goals: &mut Vec<(
283-
GoalSource,
284-
inspect::CanonicalState<TyCtxt<'tcx>, Goal<'tcx, ty::Predicate<'tcx>>>,
285-
)>,
286-
probe: &inspect::Probe<TyCtxt<'tcx>>,
284+
steps: &mut Vec<&'a inspect::ProbeStep<TyCtxt<'tcx>>>,
285+
probe: &'a inspect::Probe<TyCtxt<'tcx>>,
287286
) {
288287
let mut shallow_certainty = None;
289-
let mut impl_args = None;
290288
for step in &probe.steps {
291289
match *step {
292-
inspect::ProbeStep::AddGoal(source, goal) => nested_goals.push((source, goal)),
290+
inspect::ProbeStep::AddGoal(..) | inspect::ProbeStep::RecordImplArgs { .. } => {
291+
steps.push(step)
292+
}
293+
inspect::ProbeStep::MakeCanonicalResponse { shallow_certainty: c } => {
294+
assert_eq!(shallow_certainty.replace(c), None);
295+
}
293296
inspect::ProbeStep::NestedProbe(ref probe) => {
294297
match probe.kind {
295298
// These never assemble candidates for the goal we're trying to solve.
@@ -305,18 +308,12 @@ impl<'a, 'tcx> InspectGoal<'a, 'tcx> {
305308
// Nested probes have to prove goals added in their parent
306309
// but do not leak them, so we truncate the added goals
307310
// afterwards.
308-
let num_goals = nested_goals.len();
309-
self.candidates_recur(candidates, nested_goals, probe);
310-
nested_goals.truncate(num_goals);
311+
let num_steps = steps.len();
312+
self.candidates_recur(candidates, steps, probe);
313+
steps.truncate(num_steps);
311314
}
312315
}
313316
}
314-
inspect::ProbeStep::MakeCanonicalResponse { shallow_certainty: c } => {
315-
assert_eq!(shallow_certainty.replace(c), None);
316-
}
317-
inspect::ProbeStep::RecordImplArgs { impl_args: i } => {
318-
assert_eq!(impl_args.replace(i), None);
319-
}
320317
}
321318
}
322319

@@ -338,11 +335,10 @@ impl<'a, 'tcx> InspectGoal<'a, 'tcx> {
338335
candidates.push(InspectCandidate {
339336
goal: self,
340337
kind: probe.kind,
341-
nested_goals: nested_goals.clone(),
338+
steps: steps.clone(),
342339
final_state: probe.final_state,
343-
result,
344340
shallow_certainty,
345-
impl_args,
341+
result,
346342
});
347343
}
348344
}

0 commit comments

Comments
 (0)