@@ -1117,8 +1117,30 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
11171117 ( result, dep_node)
11181118 }
11191119
1120+ /// filter_impls filters candidates that have a positive impl for a negative goal and a
1121+ /// negative impl for a positive goal
11201122 #[ instrument( level = "debug" , skip( self ) ) ]
11211123 fn filter_impls (
1124+ & mut self ,
1125+ candidates : & mut Vec < SelectionCandidate < ' tcx > > ,
1126+ stack : & TraitObligationStack < ' o , ' tcx > ,
1127+ ) {
1128+ let tcx = self . tcx ( ) ;
1129+ candidates. retain ( |candidate| {
1130+ if let ImplCandidate ( def_id) = candidate {
1131+ ty:: ImplPolarity :: Reservation == tcx. impl_polarity ( * def_id)
1132+ || !self . allow_negative_impls
1133+ && stack. obligation . predicate . skip_binder ( ) . polarity
1134+ == tcx. impl_polarity ( * def_id)
1135+ } else {
1136+ true
1137+ }
1138+ } ) ;
1139+ }
1140+
1141+ /// filter_reservation_impls filter reservation impl for any goal as ambiguous
1142+ #[ instrument( level = "debug" , skip( self ) ) ]
1143+ fn filter_reservation_impls (
11221144 & mut self ,
11231145 candidate : SelectionCandidate < ' tcx > ,
11241146 obligation : & TraitObligation < ' tcx > ,
@@ -1148,7 +1170,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
11481170 }
11491171 }
11501172 }
1151- // Treat negative impls as unimplemented, and reservation impls as ambiguity.
1173+ // Treat reservation impls as ambiguity.
11521174 if let ImplCandidate ( def_id) = candidate {
11531175 if let ty:: ImplPolarity :: Reservation = tcx. impl_polarity ( def_id) {
11541176 if let Some ( intercrate_ambiguity_clauses) = & mut self . intercrate_ambiguity_causes {
@@ -1170,12 +1192,6 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
11701192 }
11711193 return Ok ( None ) ;
11721194 }
1173-
1174- if !self . allow_negative_impls {
1175- if obligation. predicate . skip_binder ( ) . polarity != tcx. impl_polarity ( def_id) {
1176- return Err ( Unimplemented ) ;
1177- }
1178- }
11791195 }
11801196 Ok ( Some ( candidate) )
11811197 }
0 commit comments