Skip to content

Commit 524f3c9

Browse files
committed
Take the polarity into account in compute_applicable_impls
1 parent 96eaf55 commit 524f3c9

File tree

1 file changed

+11
-0
lines changed
  • compiler/rustc_trait_selection/src/traits/error_reporting

1 file changed

+11
-0
lines changed

compiler/rustc_trait_selection/src/traits/error_reporting/ambiguity.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ pub fn recompute_applicable_impls<'tcx>(
1919
let tcx = infcx.tcx;
2020
let param_env = obligation.param_env;
2121

22+
let predicate_polarity = obligation.predicate.skip_binder().polarity;
23+
2224
let impl_may_apply = |impl_def_id| {
2325
let ocx = ObligationCtxt::new(infcx);
2426
infcx.enter_forall(obligation.predicate, |placeholder_obligation| {
@@ -40,6 +42,15 @@ pub fn recompute_applicable_impls<'tcx>(
4042
return false;
4143
}
4244

45+
let impl_trait_header = tcx.impl_trait_header(impl_def_id).unwrap();
46+
let impl_polarity = impl_trait_header.polarity;
47+
48+
match (impl_polarity, predicate_polarity) {
49+
(ty::ImplPolarity::Positive, ty::PredicatePolarity::Positive)
50+
| (ty::ImplPolarity::Negative, ty::PredicatePolarity::Negative) => {}
51+
_ => return false,
52+
}
53+
4354
let impl_predicates = tcx.predicates_of(impl_def_id).instantiate(tcx, impl_args);
4455
ocx.register_obligations(impl_predicates.predicates.iter().map(|&predicate| {
4556
Obligation::new(tcx, ObligationCause::dummy(), param_env, predicate)

0 commit comments

Comments
 (0)