Skip to content

Commit f5438d6

Browse files
committed
split probe into 2 functions for better readability
1 parent 18a6d91 commit f5438d6

File tree

6 files changed

+132
-127
lines changed

6 files changed

+132
-127
lines changed

compiler/rustc_trait_selection/src/solve/alias_relate.rs

+5-9
Original file line numberDiff line numberDiff line change
@@ -110,12 +110,11 @@ impl<'tcx> EvalCtxt<'_, 'tcx> {
110110
direction: ty::AliasRelationDirection,
111111
invert: Invert,
112112
) -> QueryResult<'tcx> {
113-
self.probe(
113+
self.probe(|r| CandidateKind::Candidate { name: "normalizes-to".into(), result: *r }).enter(
114114
|ecx| {
115115
ecx.normalizes_to_inner(param_env, alias, other, direction, invert)?;
116116
ecx.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
117117
},
118-
|r| CandidateKind::Candidate { name: "normalizes-to".into(), result: *r },
119118
)
120119
}
121120

@@ -157,7 +156,7 @@ impl<'tcx> EvalCtxt<'_, 'tcx> {
157156
alias_rhs: ty::AliasTy<'tcx>,
158157
direction: ty::AliasRelationDirection,
159158
) -> QueryResult<'tcx> {
160-
self.probe(
159+
self.probe(|r| CandidateKind::Candidate { name: "substs relate".into(), result: *r }).enter(
161160
|ecx| {
162161
match direction {
163162
ty::AliasRelationDirection::Equate => {
@@ -170,7 +169,6 @@ impl<'tcx> EvalCtxt<'_, 'tcx> {
170169

171170
ecx.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
172171
},
173-
|r| CandidateKind::Candidate { name: "substs relate".into(), result: *r },
174172
)
175173
}
176174

@@ -181,8 +179,8 @@ impl<'tcx> EvalCtxt<'_, 'tcx> {
181179
rhs: ty::Term<'tcx>,
182180
direction: ty::AliasRelationDirection,
183181
) -> QueryResult<'tcx> {
184-
self.probe(
185-
|ecx| {
182+
self.probe(|r| CandidateKind::Candidate { name: "bidir normalizes-to".into(), result: *r })
183+
.enter(|ecx| {
186184
ecx.normalizes_to_inner(
187185
param_env,
188186
lhs.to_alias_ty(ecx.tcx()).unwrap(),
@@ -198,8 +196,6 @@ impl<'tcx> EvalCtxt<'_, 'tcx> {
198196
Invert::Yes,
199197
)?;
200198
ecx.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
201-
},
202-
|r| CandidateKind::Candidate { name: "bidir normalizes-to".into(), result: *r },
203-
)
199+
})
204200
}
205201
}

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

+3-5
Original file line numberDiff line numberDiff line change
@@ -337,8 +337,8 @@ impl<'tcx> EvalCtxt<'_, 'tcx> {
337337
return
338338
};
339339

340-
let normalized_self_candidates: Result<_, NoSolution> = self.probe(
341-
|ecx| {
340+
let normalized_self_candidates: Result<_, NoSolution> =
341+
self.probe(|_| CandidateKind::NormalizedSelfTyAssembly).enter(|ecx| {
342342
ecx.with_incremented_depth(
343343
|ecx| {
344344
let result = ecx.evaluate_added_goals_and_make_canonical_response(
@@ -368,9 +368,7 @@ impl<'tcx> EvalCtxt<'_, 'tcx> {
368368
Ok(ecx.assemble_and_evaluate_candidates(goal))
369369
},
370370
)
371-
},
372-
|_| CandidateKind::NormalizedSelfTyAssembly,
373-
);
371+
});
374372

375373
if let Ok(normalized_self_candidates) = normalized_self_candidates {
376374
candidates.extend(normalized_self_candidates);

compiler/rustc_trait_selection/src/solve/eval_ctxt.rs

+9-31
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ use super::SolverMode;
3030
use super::{search_graph::SearchGraph, Goal};
3131

3232
mod canonical;
33+
mod probe;
3334

3435
pub struct EvalCtxt<'a, 'tcx> {
3536
/// The inference context that backs (mostly) inference and placeholder terms
@@ -529,32 +530,6 @@ impl<'a, 'tcx> EvalCtxt<'a, 'tcx> {
529530
}
530531

531532
impl<'tcx> EvalCtxt<'_, 'tcx> {
532-
/// `probe_kind` is only called when proof tree building is enabled so it can be
533-
/// as expensive as necessary to output the desired information.
534-
pub(super) fn probe<T>(
535-
&mut self,
536-
f: impl FnOnce(&mut EvalCtxt<'_, 'tcx>) -> T,
537-
probe_kind: impl FnOnce(&T) -> CandidateKind<'tcx>,
538-
) -> T {
539-
let mut ecx = EvalCtxt {
540-
infcx: self.infcx,
541-
var_values: self.var_values,
542-
predefined_opaques_in_body: self.predefined_opaques_in_body,
543-
max_input_universe: self.max_input_universe,
544-
search_graph: self.search_graph,
545-
nested_goals: self.nested_goals.clone(),
546-
tainted: self.tainted,
547-
inspect: self.inspect.new_goal_candidate(),
548-
};
549-
let r = self.infcx.probe(|_| f(&mut ecx));
550-
if !self.inspect.is_noop() {
551-
let cand_kind = probe_kind(&r);
552-
ecx.inspect.candidate_kind(cand_kind);
553-
self.inspect.goal_candidate(ecx.inspect);
554-
}
555-
r
556-
}
557-
558533
pub(super) fn tcx(&self) -> TyCtxt<'tcx> {
559534
self.infcx.tcx
560535
}
@@ -866,17 +841,20 @@ impl<'tcx> EvalCtxt<'_, 'tcx> {
866841
if candidate_key.def_id != key.def_id {
867842
continue;
868843
}
869-
values.extend(self.probe(
870-
|ecx| {
844+
values.extend(
845+
self.probe(|r| CandidateKind::Candidate {
846+
name: "opaque type storage".into(),
847+
result: *r,
848+
})
849+
.enter(|ecx| {
871850
for (a, b) in std::iter::zip(candidate_key.substs, key.substs) {
872851
ecx.eq(param_env, a, b)?;
873852
}
874853
ecx.eq(param_env, candidate_ty, ty)?;
875854
ecx.add_item_bounds_for_hidden_type(candidate_key, param_env, candidate_ty);
876855
ecx.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
877-
},
878-
|r| CandidateKind::Candidate { name: "opaque type storage".into(), result: *r },
879-
));
856+
}),
857+
);
880858
}
881859
values
882860
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
use super::EvalCtxt;
2+
use rustc_middle::traits::solve::inspect;
3+
use std::marker::PhantomData;
4+
5+
pub(in crate::solve) struct ProbeCtxt<'me, 'a, 'tcx, F, T> {
6+
ecx: &'me mut EvalCtxt<'a, 'tcx>,
7+
probe_kind: F,
8+
_result: PhantomData<T>,
9+
}
10+
11+
impl<'tcx, F, T> ProbeCtxt<'_, '_, 'tcx, F, T>
12+
where
13+
F: FnOnce(&T) -> inspect::CandidateKind<'tcx>,
14+
{
15+
pub(in crate::solve) fn enter(self, f: impl FnOnce(&mut EvalCtxt<'_, 'tcx>) -> T) -> T {
16+
let ProbeCtxt { ecx: outer_ecx, probe_kind, _result } = self;
17+
18+
let mut nested_ecx = EvalCtxt {
19+
infcx: outer_ecx.infcx,
20+
var_values: outer_ecx.var_values,
21+
predefined_opaques_in_body: outer_ecx.predefined_opaques_in_body,
22+
max_input_universe: outer_ecx.max_input_universe,
23+
search_graph: outer_ecx.search_graph,
24+
nested_goals: outer_ecx.nested_goals.clone(),
25+
tainted: outer_ecx.tainted,
26+
inspect: outer_ecx.inspect.new_goal_candidate(),
27+
};
28+
let r = nested_ecx.infcx.probe(|_| f(&mut nested_ecx));
29+
if !outer_ecx.inspect.is_noop() {
30+
let cand_kind = probe_kind(&r);
31+
nested_ecx.inspect.candidate_kind(cand_kind);
32+
outer_ecx.inspect.goal_candidate(nested_ecx.inspect);
33+
}
34+
r
35+
}
36+
}
37+
38+
impl<'a, 'tcx> EvalCtxt<'a, 'tcx> {
39+
/// `probe_kind` is only called when proof tree building is enabled so it can be
40+
/// as expensive as necessary to output the desired information.
41+
pub(in crate::solve) fn probe<F, T>(&mut self, probe_kind: F) -> ProbeCtxt<'_, 'a, 'tcx, F, T>
42+
where
43+
F: FnOnce(&T) -> inspect::CandidateKind<'tcx>,
44+
{
45+
ProbeCtxt { ecx: self, probe_kind, _result: PhantomData }
46+
}
47+
}

compiler/rustc_trait_selection/src/solve/project_goals.rs

+16-19
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,8 @@ impl<'tcx> assembly::GoalKind<'tcx> for ProjectionPredicate<'tcx> {
112112
) -> QueryResult<'tcx> {
113113
if let Some(projection_pred) = assumption.as_projection_clause() {
114114
if projection_pred.projection_def_id() == goal.predicate.def_id() {
115-
ecx.probe(
116-
|ecx| {
115+
ecx.probe(|r| CandidateKind::Candidate { name: "assumption".into(), result: *r })
116+
.enter(|ecx| {
117117
let assumption_projection_pred =
118118
ecx.instantiate_binder_with_infer(projection_pred);
119119
ecx.eq(
@@ -128,9 +128,7 @@ impl<'tcx> assembly::GoalKind<'tcx> for ProjectionPredicate<'tcx> {
128128
)
129129
.expect("expected goal term to be fully unconstrained");
130130
then(ecx)
131-
},
132-
|r| CandidateKind::Candidate { name: "assumption".into(), result: *r },
133-
)
131+
})
134132
} else {
135133
Err(NoSolution)
136134
}
@@ -154,6 +152,7 @@ impl<'tcx> assembly::GoalKind<'tcx> for ProjectionPredicate<'tcx> {
154152
}
155153

156154
ecx.probe(
155+
|r| CandidateKind::Candidate { name: "impl".into(), result: *r }).enter(
157156
|ecx| {
158157
let impl_substs = ecx.fresh_substs_for_item(impl_def_id);
159158
let impl_trait_ref = impl_trait_ref.subst(tcx, impl_substs);
@@ -235,7 +234,6 @@ impl<'tcx> assembly::GoalKind<'tcx> for ProjectionPredicate<'tcx> {
235234
.expect("expected goal term to be fully unconstrained");
236235
ecx.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
237236
},
238-
|r| CandidateKind::Candidate { name: "impl".into(), result: *r },
239237
)
240238
}
241239

@@ -331,8 +329,8 @@ impl<'tcx> assembly::GoalKind<'tcx> for ProjectionPredicate<'tcx> {
331329
goal: Goal<'tcx, Self>,
332330
) -> QueryResult<'tcx> {
333331
let tcx = ecx.tcx();
334-
ecx.probe(
335-
|ecx| {
332+
ecx.probe(|r| CandidateKind::Candidate { name: "builtin pointee".into(), result: *r })
333+
.enter(|ecx| {
336334
let metadata_ty = match goal.predicate.self_ty().kind() {
337335
ty::Bool
338336
| ty::Char
@@ -415,9 +413,7 @@ impl<'tcx> assembly::GoalKind<'tcx> for ProjectionPredicate<'tcx> {
415413
ecx.eq(goal.param_env, goal.predicate.term, metadata_ty.into())
416414
.expect("expected goal term to be fully unconstrained");
417415
ecx.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
418-
},
419-
|r| CandidateKind::Candidate { name: "builtin pointee".into(), result: *r },
420-
)
416+
})
421417
}
422418

423419
fn consider_builtin_future_candidate(
@@ -552,14 +548,15 @@ impl<'tcx> assembly::GoalKind<'tcx> for ProjectionPredicate<'tcx> {
552548
),
553549
};
554550

555-
ecx.probe(
556-
|ecx| {
557-
ecx.eq(goal.param_env, goal.predicate.term, discriminant_ty.into())
558-
.expect("expected goal term to be fully unconstrained");
559-
ecx.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
560-
},
561-
|r| CandidateKind::Candidate { name: "builtin discriminant kind".into(), result: *r },
562-
)
551+
ecx.probe(|r| CandidateKind::Candidate {
552+
name: "builtin discriminant kind".into(),
553+
result: *r,
554+
})
555+
.enter(|ecx| {
556+
ecx.eq(goal.param_env, goal.predicate.term, discriminant_ty.into())
557+
.expect("expected goal term to be fully unconstrained");
558+
ecx.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
559+
})
563560
}
564561

565562
fn consider_builtin_destruct_candidate(

0 commit comments

Comments
 (0)