@@ -165,7 +165,18 @@ pub struct ImplHeader<'tcx> {
165165 pub predicates : Vec < Predicate < ' tcx > > ,
166166}
167167
168- #[ derive( Copy , Clone , PartialEq , TyEncodable , TyDecodable , HashStable , Debug ) ]
168+ #[ derive(
169+ Copy ,
170+ Clone ,
171+ PartialEq ,
172+ Eq ,
173+ Hash ,
174+ TyEncodable ,
175+ TyDecodable ,
176+ HashStable ,
177+ Debug ,
178+ TypeFoldable
179+ ) ]
169180pub enum ImplPolarity {
170181 /// `impl Trait for Type`
171182 Positive ,
@@ -178,6 +189,26 @@ pub enum ImplPolarity {
178189 Reservation ,
179190}
180191
192+ impl ImplPolarity {
193+ pub fn flip ( & self ) -> Option < ImplPolarity > {
194+ match self {
195+ ImplPolarity :: Positive => Some ( ImplPolarity :: Negative ) ,
196+ ImplPolarity :: Negative => Some ( ImplPolarity :: Positive ) ,
197+ ImplPolarity :: Reservation => None ,
198+ }
199+ }
200+ }
201+
202+ impl fmt:: Display for ImplPolarity {
203+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
204+ match self {
205+ Self :: Positive => f. write_str ( "positive" ) ,
206+ Self :: Negative => f. write_str ( "negative" ) ,
207+ Self :: Reservation => f. write_str ( "reservation" ) ,
208+ }
209+ }
210+ }
211+
181212#[ derive( Clone , Debug , PartialEq , Eq , Copy , Hash , TyEncodable , TyDecodable , HashStable ) ]
182213pub enum Visibility {
183214 /// Visible everywhere (including in other crates).
@@ -460,6 +491,26 @@ impl<'tcx> Predicate<'tcx> {
460491 pub fn kind ( self ) -> Binder < ' tcx , PredicateKind < ' tcx > > {
461492 self . inner . kind
462493 }
494+
495+ pub fn flip_polarity ( & self , tcx : TyCtxt < ' tcx > ) -> Option < Predicate < ' tcx > > {
496+ let kind = self
497+ . inner
498+ . kind
499+ . map_bound ( |kind| match kind {
500+ PredicateKind :: Trait ( TraitPredicate { trait_ref, constness, polarity } ) => {
501+ Some ( PredicateKind :: Trait ( TraitPredicate {
502+ trait_ref,
503+ constness,
504+ polarity : polarity. flip ( ) ?,
505+ } ) )
506+ }
507+
508+ _ => None ,
509+ } )
510+ . transpose ( ) ?;
511+
512+ Some ( tcx. mk_predicate ( kind) )
513+ }
463514}
464515
465516impl < ' a , ' tcx > HashStable < StableHashingContext < ' a > > for Predicate < ' tcx > {
@@ -655,6 +706,8 @@ pub struct TraitPredicate<'tcx> {
655706 pub trait_ref : TraitRef < ' tcx > ,
656707
657708 pub constness : BoundConstness ,
709+
710+ pub polarity : ImplPolarity ,
658711}
659712
660713pub type PolyTraitPredicate < ' tcx > = ty:: Binder < ' tcx , TraitPredicate < ' tcx > > ;
@@ -789,7 +842,11 @@ impl<'tcx> ToPredicate<'tcx> for ConstnessAnd<PolyTraitRef<'tcx>> {
789842 fn to_predicate ( self , tcx : TyCtxt < ' tcx > ) -> Predicate < ' tcx > {
790843 self . value
791844 . map_bound ( |trait_ref| {
792- PredicateKind :: Trait ( ty:: TraitPredicate { trait_ref, constness : self . constness } )
845+ PredicateKind :: Trait ( ty:: TraitPredicate {
846+ trait_ref,
847+ constness : self . constness ,
848+ polarity : ty:: ImplPolarity :: Positive ,
849+ } )
793850 } )
794851 . to_predicate ( tcx)
795852 }
0 commit comments