Skip to content

Commit 3287f72

Browse files
committed
Avoid code duplication by extracting checks into fns
1 parent 9534186 commit 3287f72

File tree

1 file changed

+18
-11
lines changed

1 file changed

+18
-11
lines changed

Diff for: compiler/rustc_trait_selection/src/traits/coherence.rs

+18-11
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ use crate::infer::{CombinedSnapshot, InferOk, TyCtxtInferExt};
88
use crate::traits::query::evaluate_obligation::InferCtxtExt;
99
use crate::traits::select::IntercrateAmbiguityCause;
1010
use crate::traits::SkipLeakCheck;
11-
use crate::traits::{self, Normalized, Obligation, ObligationCause, SelectionContext};
11+
use crate::traits::{
12+
self, Normalized, Obligation, ObligationCause, PredicateObligation, SelectionContext,
13+
};
1214
use rustc_hir::def_id::{DefId, LOCAL_CRATE};
1315
use rustc_middle::ty::fold::TypeFoldable;
1416
use rustc_middle::ty::subst::Subst;
@@ -159,6 +161,19 @@ fn overlap_within_probe(
159161
b_def_id: DefId,
160162
snapshot: &CombinedSnapshot<'_, 'tcx>,
161163
) -> Option<OverlapResult<'tcx>> {
164+
fn loose_check(selcx: &mut SelectionContext<'cx, 'tcx>, o: &PredicateObligation<'tcx>) -> bool {
165+
!selcx.predicate_may_hold_fatal(o)
166+
}
167+
168+
fn strict_check(selcx: &SelectionContext<'cx, 'tcx>, o: &PredicateObligation<'tcx>) -> bool {
169+
let infcx = selcx.infcx();
170+
let tcx = infcx.tcx;
171+
o.flip_polarity(tcx)
172+
.as_ref()
173+
.map(|o| selcx.infcx().predicate_must_hold_modulo_regions(o))
174+
.unwrap_or(false)
175+
}
176+
162177
// For the purposes of this check, we don't bring any placeholder
163178
// types into scope; instead, we replace the generic types with
164179
// fresh type variables, and hence we do our evaluations in an
@@ -227,17 +242,9 @@ fn overlap_within_probe(
227242
if tcx.has_attr(a_def_id, sym::rustc_strict_coherence)
228243
&& tcx.has_attr(b_def_id, sym::rustc_strict_coherence)
229244
{
230-
o.flip_polarity(tcx)
231-
.as_ref()
232-
.map(|o| selcx.infcx().predicate_must_hold_modulo_regions(o))
233-
.unwrap_or(false)
245+
strict_check(selcx, o)
234246
} else {
235-
!selcx.predicate_may_hold_fatal(o)
236-
|| tcx.features().negative_impls
237-
&& o.flip_polarity(tcx)
238-
.as_ref()
239-
.map(|o| selcx.infcx().predicate_must_hold_modulo_regions(o))
240-
.unwrap_or(false)
247+
loose_check(selcx, o) || tcx.features().negative_impls && strict_check(selcx, o)
241248
}
242249
});
243250
// FIXME: the call to `selcx.predicate_may_hold_fatal` above should be ported

0 commit comments

Comments
 (0)