Skip to content

Commit 68d444f

Browse files
committed
Add TraitObligation::polarity() for better encapsulation
1 parent 7568632 commit 68d444f

File tree

3 files changed

+12
-14
lines changed

3 files changed

+12
-14
lines changed

Diff for: compiler/rustc_infer/src/traits/mod.rs

+4
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,10 @@ impl<'tcx> FulfillmentError<'tcx> {
140140
}
141141

142142
impl<'tcx> TraitObligation<'tcx> {
143+
pub fn polarity(&self) -> ty::ImplPolarity {
144+
self.predicate.skip_binder().polarity
145+
}
146+
143147
pub fn self_ty(&self) -> ty::Binder<'tcx, Ty<'tcx>> {
144148
self.predicate.map_bound(|p| p.self_ty())
145149
}

Diff for: compiler/rustc_trait_selection/src/traits/select/candidate_assembly.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
256256

257257
let mut candidates = SelectionCandidateSet { vec: Vec::new(), ambiguous: false };
258258

259-
if obligation.predicate.skip_binder().polarity == ty::ImplPolarity::Negative {
259+
if obligation.polarity() == ty::ImplPolarity::Negative {
260260
self.assemble_candidates_from_impls(obligation, &mut candidates);
261261
} else {
262262
self.assemble_candidates_for_trait_alias(obligation, &mut candidates);
@@ -382,10 +382,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
382382
for bound in matching_bounds {
383383
let wc = self.evaluate_where_clause(stack, bound.value)?;
384384
if wc.may_apply() {
385-
candidates.vec.push(ParamCandidate((
386-
bound,
387-
stack.obligation.predicate.skip_binder().polarity,
388-
)));
385+
candidates.vec.push(ParamCandidate((bound, stack.obligation.polarity())));
389386
}
390387
}
391388

Diff for: compiler/rustc_trait_selection/src/traits/select/mod.rs

+6-9
Original file line numberDiff line numberDiff line change
@@ -712,7 +712,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
712712
if let Some(result) = self.check_evaluation_cache(
713713
obligation.param_env,
714714
fresh_trait_ref,
715-
obligation.predicate.skip_binder().polarity,
715+
obligation.polarity(),
716716
) {
717717
debug!(?result, "CACHE HIT");
718718
return Ok(result);
@@ -746,7 +746,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
746746
self.insert_evaluation_cache(
747747
obligation.param_env,
748748
fresh_trait_ref,
749-
obligation.predicate.skip_binder().polarity,
749+
obligation.polarity(),
750750
dep_node,
751751
result,
752752
);
@@ -755,7 +755,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
755755
self.insert_evaluation_cache(
756756
obligation.param_env,
757757
fresh_trait_ref,
758-
obligation.predicate.skip_binder().polarity,
758+
obligation.polarity(),
759759
dep_node,
760760
provisional_result.max(result),
761761
);
@@ -867,7 +867,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
867867
let unbound_input_types =
868868
stack.fresh_trait_ref.value.skip_binder().substs.types().any(|ty| ty.is_fresh());
869869

870-
if stack.obligation.predicate.skip_binder().polarity != ty::ImplPolarity::Negative {
870+
if stack.obligation.polarity() != ty::ImplPolarity::Negative {
871871
// This check was an imperfect workaround for a bug in the old
872872
// intercrate mode; it should be removed when that goes away.
873873
if unbound_input_types && self.intercrate {
@@ -1130,8 +1130,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
11301130
if let ImplCandidate(def_id) = candidate {
11311131
ty::ImplPolarity::Reservation == tcx.impl_polarity(*def_id)
11321132
|| !self.allow_negative_impls
1133-
&& stack.obligation.predicate.skip_binder().polarity
1134-
== tcx.impl_polarity(*def_id)
1133+
&& stack.obligation.polarity() == tcx.impl_polarity(*def_id)
11351134
} else {
11361135
true
11371136
}
@@ -1199,9 +1198,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
11991198
fn is_knowable<'o>(&mut self, stack: &TraitObligationStack<'o, 'tcx>) -> Option<Conflict> {
12001199
debug!("is_knowable(intercrate={:?})", self.intercrate);
12011200

1202-
if !self.intercrate
1203-
|| stack.obligation.predicate.skip_binder().polarity == ty::ImplPolarity::Negative
1204-
{
1201+
if !self.intercrate || stack.obligation.polarity() == ty::ImplPolarity::Negative {
12051202
return None;
12061203
}
12071204

0 commit comments

Comments
 (0)