@@ -561,6 +561,42 @@ impl rustc_errors::IntoDiagnosticArg for Predicate<'_> {
561561 }
562562}
563563
564+ /// TODO: doc
565+ #[ derive( Clone , Copy , PartialEq , Eq , Hash , HashStable ) ]
566+ #[ rustc_pass_by_value]
567+ pub struct Clause < ' tcx > ( Interned < ' tcx , WithCachedTypeInfo < ty:: Binder < ' tcx , PredicateKind < ' tcx > > > > ) ;
568+
569+ impl < ' tcx > Clause < ' tcx > {
570+ pub fn as_predicate ( self ) -> Predicate < ' tcx > {
571+ Predicate ( self . 0 )
572+ }
573+
574+ pub fn kind ( self ) -> Binder < ' tcx , ClauseKind < ' tcx > > {
575+ self . 0 . internee . map_bound ( |kind| match kind {
576+ PredicateKind :: Clause ( clause) => clause,
577+ _ => unreachable ! ( ) ,
578+ } )
579+ }
580+
581+ pub fn as_trait_clause ( self ) -> Option < Binder < ' tcx , TraitPredicate < ' tcx > > > {
582+ let clause = self . kind ( ) ;
583+ if let ty:: ClauseKind :: Trait ( trait_clause) = clause. skip_binder ( ) {
584+ Some ( clause. rebind ( trait_clause) )
585+ } else {
586+ None
587+ }
588+ }
589+
590+ pub fn as_projection_clause ( self ) -> Option < Binder < ' tcx , ProjectionPredicate < ' tcx > > > {
591+ let clause = self . kind ( ) ;
592+ if let ty:: ClauseKind :: Projection ( projection_clause) = clause. skip_binder ( ) {
593+ Some ( clause. rebind ( projection_clause) )
594+ } else {
595+ None
596+ }
597+ }
598+ }
599+
564600#[ derive( Clone , Copy , PartialEq , Eq , Hash , TyEncodable , TyDecodable ) ]
565601#[ derive( HashStable , TypeFoldable , TypeVisitable , Lift ) ]
566602/// A clause is something that can appear in where bounds or be inferred
@@ -592,24 +628,6 @@ pub enum ClauseKind<'tcx> {
592628 ConstEvaluatable ( ty:: Const < ' tcx > ) ,
593629}
594630
595- impl < ' tcx > Binder < ' tcx , ClauseKind < ' tcx > > {
596- pub fn as_trait_clause ( self ) -> Option < Binder < ' tcx , TraitPredicate < ' tcx > > > {
597- if let ty:: ClauseKind :: Trait ( trait_clause) = self . skip_binder ( ) {
598- Some ( self . rebind ( trait_clause) )
599- } else {
600- None
601- }
602- }
603-
604- pub fn as_projection_clause ( self ) -> Option < Binder < ' tcx , ProjectionPredicate < ' tcx > > > {
605- if let ty:: ClauseKind :: Projection ( projection_clause) = self . skip_binder ( ) {
606- Some ( self . rebind ( projection_clause) )
607- } else {
608- None
609- }
610- }
611- }
612-
613631#[ derive( Clone , Copy , PartialEq , Eq , Hash , TyEncodable , TyDecodable ) ]
614632#[ derive( HashStable , TypeFoldable , TypeVisitable , Lift ) ]
615633pub enum PredicateKind < ' tcx > {
@@ -1229,14 +1247,11 @@ impl<'tcx> ToPredicate<'tcx> for TraitRef<'tcx> {
12291247 }
12301248}
12311249
1232- impl < ' tcx > ToPredicate < ' tcx , Binder < ' tcx , ClauseKind < ' tcx > > > for TraitRef < ' tcx > {
1250+ impl < ' tcx > ToPredicate < ' tcx , Clause < ' tcx > > for TraitRef < ' tcx > {
12331251 #[ inline( always) ]
1234- fn to_predicate ( self , _tcx : TyCtxt < ' tcx > ) -> Binder < ' tcx , ClauseKind < ' tcx > > {
1235- Binder :: dummy ( ClauseKind :: Trait ( TraitPredicate {
1236- trait_ref : self ,
1237- constness : ty:: BoundConstness :: NotConst ,
1238- polarity : ty:: ImplPolarity :: Positive ,
1239- } ) )
1252+ fn to_predicate ( self , tcx : TyCtxt < ' tcx > ) -> Clause < ' tcx > {
1253+ let p: Predicate < ' tcx > = self . to_predicate ( tcx) ;
1254+ Clause ( p. 0 )
12401255 }
12411256}
12421257
@@ -1248,9 +1263,9 @@ impl<'tcx> ToPredicate<'tcx> for Binder<'tcx, TraitRef<'tcx>> {
12481263 }
12491264}
12501265
1251- impl < ' tcx > ToPredicate < ' tcx , Binder < ' tcx , ClauseKind < ' tcx > > > for Binder < ' tcx , TraitRef < ' tcx > > {
1266+ impl < ' tcx > ToPredicate < ' tcx , Clause < ' tcx > > for Binder < ' tcx , TraitRef < ' tcx > > {
12521267 #[ inline( always) ]
1253- fn to_predicate ( self , tcx : TyCtxt < ' tcx > ) -> Binder < ' tcx , ClauseKind < ' tcx > > {
1268+ fn to_predicate ( self , tcx : TyCtxt < ' tcx > ) -> Clause < ' tcx > {
12541269 let pred: PolyTraitPredicate < ' tcx > = self . to_predicate ( tcx) ;
12551270 pred. to_predicate ( tcx)
12561271 }
@@ -1285,9 +1300,10 @@ impl<'tcx> ToPredicate<'tcx> for PolyTraitPredicate<'tcx> {
12851300 }
12861301}
12871302
1288- impl < ' tcx > ToPredicate < ' tcx , Binder < ' tcx , ClauseKind < ' tcx > > > for PolyTraitPredicate < ' tcx > {
1289- fn to_predicate ( self , _tcx : TyCtxt < ' tcx > ) -> Binder < ' tcx , ClauseKind < ' tcx > > {
1290- self . map_bound ( |p| ClauseKind :: Trait ( p) )
1303+ impl < ' tcx > ToPredicate < ' tcx , Clause < ' tcx > > for PolyTraitPredicate < ' tcx > {
1304+ fn to_predicate ( self , tcx : TyCtxt < ' tcx > ) -> Clause < ' tcx > {
1305+ let p: Predicate < ' tcx > = self . to_predicate ( tcx) ;
1306+ Clause ( p. 0 )
12911307 }
12921308}
12931309
@@ -1309,9 +1325,10 @@ impl<'tcx> ToPredicate<'tcx> for PolyProjectionPredicate<'tcx> {
13091325 }
13101326}
13111327
1312- impl < ' tcx > ToPredicate < ' tcx , Binder < ' tcx , ClauseKind < ' tcx > > > for PolyProjectionPredicate < ' tcx > {
1313- fn to_predicate ( self , _tcx : TyCtxt < ' tcx > ) -> Binder < ' tcx , ClauseKind < ' tcx > > {
1314- self . map_bound ( |p| ClauseKind :: Projection ( p) )
1328+ impl < ' tcx > ToPredicate < ' tcx , Clause < ' tcx > > for PolyProjectionPredicate < ' tcx > {
1329+ fn to_predicate ( self , tcx : TyCtxt < ' tcx > ) -> Clause < ' tcx > {
1330+ let p: Predicate < ' tcx > = self . to_predicate ( tcx) ;
1331+ Clause ( p. 0 )
13151332 }
13161333}
13171334
@@ -1385,18 +1402,10 @@ impl<'tcx> Predicate<'tcx> {
13851402 }
13861403 }
13871404
1388- pub fn as_clause ( self ) -> Option < Binder < ' tcx , ClauseKind < ' tcx > > > {
1389- let predicate = self . kind ( ) ;
1390- match predicate. skip_binder ( ) {
1391- PredicateKind :: Clause ( clause) => Some ( predicate. rebind ( clause) ) ,
1392- PredicateKind :: AliasRelate ( ..)
1393- | PredicateKind :: Subtype ( ..)
1394- | PredicateKind :: Coerce ( ..)
1395- | PredicateKind :: ObjectSafe ( ..)
1396- | PredicateKind :: ClosureKind ( ..)
1397- | PredicateKind :: ConstEquate ( ..)
1398- | PredicateKind :: Ambiguous
1399- | PredicateKind :: TypeWellFormedFromEnv ( ..) => None ,
1405+ pub fn as_clause ( self ) -> Option < Clause < ' tcx > > {
1406+ match self . kind ( ) . skip_binder ( ) {
1407+ PredicateKind :: Clause ( ..) => Some ( Clause ( self . 0 ) ) ,
1408+ _ => None ,
14001409 }
14011410 }
14021411}
0 commit comments