Skip to content

Commit 795c2ef

Browse files
committed
add ecx.probe_candidate
1 parent cd68ead commit 795c2ef

File tree

5 files changed

+280
-298
lines changed

5 files changed

+280
-298
lines changed

compiler/rustc_trait_selection/src/solve/alias_relate.rs

+31-37
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use super::{EvalCtxt, SolverMode};
22
use rustc_infer::traits::query::NoSolution;
3-
use rustc_middle::traits::solve::inspect::CandidateKind;
43
use rustc_middle::traits::solve::{Certainty, Goal, QueryResult};
54
use rustc_middle::ty;
65

@@ -110,12 +109,10 @@ impl<'tcx> EvalCtxt<'_, 'tcx> {
110109
direction: ty::AliasRelationDirection,
111110
invert: Invert,
112111
) -> QueryResult<'tcx> {
113-
self.probe(|r| CandidateKind::Candidate { name: "normalizes-to".into(), result: *r }).enter(
114-
|ecx| {
115-
ecx.normalizes_to_inner(param_env, alias, other, direction, invert)?;
116-
ecx.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
117-
},
118-
)
112+
self.probe_candidate("normalizes-to").enter(|ecx| {
113+
ecx.normalizes_to_inner(param_env, alias, other, direction, invert)?;
114+
ecx.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
115+
})
119116
}
120117

121118
fn normalizes_to_inner(
@@ -156,20 +153,18 @@ impl<'tcx> EvalCtxt<'_, 'tcx> {
156153
alias_rhs: ty::AliasTy<'tcx>,
157154
direction: ty::AliasRelationDirection,
158155
) -> QueryResult<'tcx> {
159-
self.probe(|r| CandidateKind::Candidate { name: "substs relate".into(), result: *r }).enter(
160-
|ecx| {
161-
match direction {
162-
ty::AliasRelationDirection::Equate => {
163-
ecx.eq(param_env, alias_lhs, alias_rhs)?;
164-
}
165-
ty::AliasRelationDirection::Subtype => {
166-
ecx.sub(param_env, alias_lhs, alias_rhs)?;
167-
}
156+
self.probe_candidate("substs relate").enter(|ecx| {
157+
match direction {
158+
ty::AliasRelationDirection::Equate => {
159+
ecx.eq(param_env, alias_lhs, alias_rhs)?;
160+
}
161+
ty::AliasRelationDirection::Subtype => {
162+
ecx.sub(param_env, alias_lhs, alias_rhs)?;
168163
}
164+
}
169165

170-
ecx.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
171-
},
172-
)
166+
ecx.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
167+
})
173168
}
174169

175170
fn assemble_bidirectional_normalizes_to_candidate(
@@ -179,23 +174,22 @@ impl<'tcx> EvalCtxt<'_, 'tcx> {
179174
rhs: ty::Term<'tcx>,
180175
direction: ty::AliasRelationDirection,
181176
) -> QueryResult<'tcx> {
182-
self.probe(|r| CandidateKind::Candidate { name: "bidir normalizes-to".into(), result: *r })
183-
.enter(|ecx| {
184-
ecx.normalizes_to_inner(
185-
param_env,
186-
lhs.to_alias_ty(ecx.tcx()).unwrap(),
187-
rhs,
188-
direction,
189-
Invert::No,
190-
)?;
191-
ecx.normalizes_to_inner(
192-
param_env,
193-
rhs.to_alias_ty(ecx.tcx()).unwrap(),
194-
lhs,
195-
direction,
196-
Invert::Yes,
197-
)?;
198-
ecx.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
199-
})
177+
self.probe_candidate("bidir normalizes-to").enter(|ecx| {
178+
ecx.normalizes_to_inner(
179+
param_env,
180+
lhs.to_alias_ty(ecx.tcx()).unwrap(),
181+
rhs,
182+
direction,
183+
Invert::No,
184+
)?;
185+
ecx.normalizes_to_inner(
186+
param_env,
187+
rhs.to_alias_ty(ecx.tcx()).unwrap(),
188+
lhs,
189+
direction,
190+
Invert::Yes,
191+
)?;
192+
ecx.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
193+
})
200194
}
201195
}

compiler/rustc_trait_selection/src/solve/eval_ctxt.rs

+14-20
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use rustc_infer::infer::{
99
use rustc_infer::traits::query::NoSolution;
1010
use rustc_infer::traits::ObligationCause;
1111
use rustc_middle::infer::unify_key::{ConstVariableOrigin, ConstVariableOriginKind};
12-
use rustc_middle::traits::solve::inspect::{self, CandidateKind};
12+
use rustc_middle::traits::solve::inspect;
1313
use rustc_middle::traits::solve::{
1414
CanonicalInput, CanonicalResponse, Certainty, IsNormalizesToHack, MaybeCause,
1515
PredefinedOpaques, PredefinedOpaquesData, QueryResult,
@@ -880,25 +880,19 @@ impl<'tcx> EvalCtxt<'_, 'tcx> {
880880
if candidate_key.def_id != key.def_id {
881881
continue;
882882
}
883-
values.extend(
884-
self.probe(|r| CandidateKind::Candidate {
885-
name: "opaque type storage".into(),
886-
result: *r,
887-
})
888-
.enter(|ecx| {
889-
for (a, b) in std::iter::zip(candidate_key.substs, key.substs) {
890-
ecx.eq(param_env, a, b)?;
891-
}
892-
ecx.eq(param_env, candidate_ty, ty)?;
893-
ecx.add_item_bounds_for_hidden_type(
894-
candidate_key.def_id.to_def_id(),
895-
candidate_key.substs,
896-
param_env,
897-
candidate_ty,
898-
);
899-
ecx.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
900-
}),
901-
);
883+
values.extend(self.probe_candidate("opaque type storage").enter(|ecx| {
884+
for (a, b) in std::iter::zip(candidate_key.substs, key.substs) {
885+
ecx.eq(param_env, a, b)?;
886+
}
887+
ecx.eq(param_env, candidate_ty, ty)?;
888+
ecx.add_item_bounds_for_hidden_type(
889+
candidate_key.def_id.to_def_id(),
890+
candidate_key.substs,
891+
param_env,
892+
candidate_ty,
893+
);
894+
ecx.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
895+
}));
902896
}
903897
values
904898
}

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

+21-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use super::EvalCtxt;
2-
use rustc_middle::traits::solve::inspect;
2+
use rustc_middle::traits::solve::{inspect, QueryResult};
33
use std::marker::PhantomData;
44

55
pub(in crate::solve) struct ProbeCtxt<'me, 'a, 'tcx, F, T> {
@@ -44,4 +44,24 @@ impl<'a, 'tcx> EvalCtxt<'a, 'tcx> {
4444
{
4545
ProbeCtxt { ecx: self, probe_kind, _result: PhantomData }
4646
}
47+
48+
pub(in crate::solve) fn probe_candidate(
49+
&mut self,
50+
name: &'static str,
51+
) -> ProbeCtxt<
52+
'_,
53+
'a,
54+
'tcx,
55+
impl FnOnce(&QueryResult<'tcx>) -> inspect::CandidateKind<'tcx>,
56+
QueryResult<'tcx>,
57+
> {
58+
ProbeCtxt {
59+
ecx: self,
60+
probe_kind: move |result: &QueryResult<'tcx>| inspect::CandidateKind::Candidate {
61+
name: name.to_string(),
62+
result: *result,
63+
},
64+
_result: PhantomData,
65+
}
66+
}
4767
}

compiler/rustc_trait_selection/src/solve/project_goals.rs

+84-95
Original file line numberDiff line numberDiff line change
@@ -112,23 +112,18 @@ 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(|r| CandidateKind::Candidate { name: "assumption".into(), result: *r })
116-
.enter(|ecx| {
117-
let assumption_projection_pred =
118-
ecx.instantiate_binder_with_infer(projection_pred);
119-
ecx.eq(
120-
goal.param_env,
121-
goal.predicate.projection_ty,
122-
assumption_projection_pred.projection_ty,
123-
)?;
124-
ecx.eq(
125-
goal.param_env,
126-
goal.predicate.term,
127-
assumption_projection_pred.term,
128-
)
115+
ecx.probe_candidate("assumption").enter(|ecx| {
116+
let assumption_projection_pred =
117+
ecx.instantiate_binder_with_infer(projection_pred);
118+
ecx.eq(
119+
goal.param_env,
120+
goal.predicate.projection_ty,
121+
assumption_projection_pred.projection_ty,
122+
)?;
123+
ecx.eq(goal.param_env, goal.predicate.term, assumption_projection_pred.term)
129124
.expect("expected goal term to be fully unconstrained");
130-
then(ecx)
131-
})
125+
then(ecx)
126+
})
132127
} else {
133128
Err(NoSolution)
134129
}
@@ -329,91 +324,89 @@ impl<'tcx> assembly::GoalKind<'tcx> for ProjectionPredicate<'tcx> {
329324
goal: Goal<'tcx, Self>,
330325
) -> QueryResult<'tcx> {
331326
let tcx = ecx.tcx();
332-
ecx.probe(|r| CandidateKind::Candidate { name: "builtin pointee".into(), result: *r })
333-
.enter(|ecx| {
334-
let metadata_ty = match goal.predicate.self_ty().kind() {
335-
ty::Bool
336-
| ty::Char
337-
| ty::Int(..)
338-
| ty::Uint(..)
339-
| ty::Float(..)
340-
| ty::Array(..)
341-
| ty::RawPtr(..)
342-
| ty::Ref(..)
343-
| ty::FnDef(..)
344-
| ty::FnPtr(..)
345-
| ty::Closure(..)
346-
| ty::Infer(ty::IntVar(..) | ty::FloatVar(..))
347-
| ty::Generator(..)
348-
| ty::GeneratorWitness(..)
349-
| ty::GeneratorWitnessMIR(..)
350-
| ty::Never
351-
| ty::Foreign(..) => tcx.types.unit,
352-
353-
ty::Error(e) => tcx.ty_error(*e),
354-
355-
ty::Str | ty::Slice(_) => tcx.types.usize,
356-
357-
ty::Dynamic(_, _, _) => {
358-
let dyn_metadata = tcx.require_lang_item(LangItem::DynMetadata, None);
359-
tcx.type_of(dyn_metadata)
360-
.subst(tcx, &[ty::GenericArg::from(goal.predicate.self_ty())])
361-
}
362-
363-
ty::Alias(_, _) | ty::Param(_) | ty::Placeholder(..) => {
364-
// FIXME(ptr_metadata): It would also be possible to return a `Ok(Ambig)` with no constraints.
365-
let sized_predicate = ty::TraitRef::from_lang_item(
366-
tcx,
367-
LangItem::Sized,
368-
DUMMY_SP,
369-
[ty::GenericArg::from(goal.predicate.self_ty())],
370-
);
371-
ecx.add_goal(goal.with(tcx, sized_predicate));
372-
tcx.types.unit
373-
}
327+
ecx.probe_candidate("builtin pointee").enter(|ecx| {
328+
let metadata_ty = match goal.predicate.self_ty().kind() {
329+
ty::Bool
330+
| ty::Char
331+
| ty::Int(..)
332+
| ty::Uint(..)
333+
| ty::Float(..)
334+
| ty::Array(..)
335+
| ty::RawPtr(..)
336+
| ty::Ref(..)
337+
| ty::FnDef(..)
338+
| ty::FnPtr(..)
339+
| ty::Closure(..)
340+
| ty::Infer(ty::IntVar(..) | ty::FloatVar(..))
341+
| ty::Generator(..)
342+
| ty::GeneratorWitness(..)
343+
| ty::GeneratorWitnessMIR(..)
344+
| ty::Never
345+
| ty::Foreign(..) => tcx.types.unit,
346+
347+
ty::Error(e) => tcx.ty_error(*e),
348+
349+
ty::Str | ty::Slice(_) => tcx.types.usize,
350+
351+
ty::Dynamic(_, _, _) => {
352+
let dyn_metadata = tcx.require_lang_item(LangItem::DynMetadata, None);
353+
tcx.type_of(dyn_metadata)
354+
.subst(tcx, &[ty::GenericArg::from(goal.predicate.self_ty())])
355+
}
374356

375-
ty::Adt(def, substs) if def.is_struct() => {
376-
match def.non_enum_variant().fields.raw.last() {
377-
None => tcx.types.unit,
378-
Some(field_def) => {
379-
let self_ty = field_def.ty(tcx, substs);
380-
ecx.add_goal(goal.with(
381-
tcx,
382-
ty::Binder::dummy(goal.predicate.with_self_ty(tcx, self_ty)),
383-
));
384-
return ecx.evaluate_added_goals_and_make_canonical_response(
385-
Certainty::Yes,
386-
);
387-
}
388-
}
389-
}
390-
ty::Adt(_, _) => tcx.types.unit,
357+
ty::Alias(_, _) | ty::Param(_) | ty::Placeholder(..) => {
358+
// FIXME(ptr_metadata): It would also be possible to return a `Ok(Ambig)` with no constraints.
359+
let sized_predicate = ty::TraitRef::from_lang_item(
360+
tcx,
361+
LangItem::Sized,
362+
DUMMY_SP,
363+
[ty::GenericArg::from(goal.predicate.self_ty())],
364+
);
365+
ecx.add_goal(goal.with(tcx, sized_predicate));
366+
tcx.types.unit
367+
}
391368

392-
ty::Tuple(elements) => match elements.last() {
369+
ty::Adt(def, substs) if def.is_struct() => {
370+
match def.non_enum_variant().fields.raw.last() {
393371
None => tcx.types.unit,
394-
Some(&self_ty) => {
372+
Some(field_def) => {
373+
let self_ty = field_def.ty(tcx, substs);
395374
ecx.add_goal(goal.with(
396375
tcx,
397376
ty::Binder::dummy(goal.predicate.with_self_ty(tcx, self_ty)),
398377
));
399378
return ecx
400379
.evaluate_added_goals_and_make_canonical_response(Certainty::Yes);
401380
}
402-
},
403-
404-
ty::Infer(
405-
ty::TyVar(_) | ty::FreshTy(_) | ty::FreshIntTy(_) | ty::FreshFloatTy(_),
406-
)
407-
| ty::Bound(..) => bug!(
408-
"unexpected self ty `{:?}` when normalizing `<T as Pointee>::Metadata`",
409-
goal.predicate.self_ty()
410-
),
411-
};
381+
}
382+
}
383+
ty::Adt(_, _) => tcx.types.unit,
412384

413-
ecx.eq(goal.param_env, goal.predicate.term, metadata_ty.into())
414-
.expect("expected goal term to be fully unconstrained");
415-
ecx.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
416-
})
385+
ty::Tuple(elements) => match elements.last() {
386+
None => tcx.types.unit,
387+
Some(&self_ty) => {
388+
ecx.add_goal(goal.with(
389+
tcx,
390+
ty::Binder::dummy(goal.predicate.with_self_ty(tcx, self_ty)),
391+
));
392+
return ecx
393+
.evaluate_added_goals_and_make_canonical_response(Certainty::Yes);
394+
}
395+
},
396+
397+
ty::Infer(
398+
ty::TyVar(_) | ty::FreshTy(_) | ty::FreshIntTy(_) | ty::FreshFloatTy(_),
399+
)
400+
| ty::Bound(..) => bug!(
401+
"unexpected self ty `{:?}` when normalizing `<T as Pointee>::Metadata`",
402+
goal.predicate.self_ty()
403+
),
404+
};
405+
406+
ecx.eq(goal.param_env, goal.predicate.term, metadata_ty.into())
407+
.expect("expected goal term to be fully unconstrained");
408+
ecx.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
409+
})
417410
}
418411

419412
fn consider_builtin_future_candidate(
@@ -548,11 +541,7 @@ impl<'tcx> assembly::GoalKind<'tcx> for ProjectionPredicate<'tcx> {
548541
),
549542
};
550543

551-
ecx.probe(|r| CandidateKind::Candidate {
552-
name: "builtin discriminant kind".into(),
553-
result: *r,
554-
})
555-
.enter(|ecx| {
544+
ecx.probe_candidate("builtin discriminant kind").enter(|ecx| {
556545
ecx.eq(goal.param_env, goal.predicate.term, discriminant_ty.into())
557546
.expect("expected goal term to be fully unconstrained");
558547
ecx.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)

0 commit comments

Comments
 (0)