@@ -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 > {
@@ -1222,21 +1240,25 @@ impl<'tcx> ToPredicate<'tcx> for Binder<'tcx, ClauseKind<'tcx>> {
12221240 }
12231241}
12241242
1243+ impl < ' tcx > ToPredicate < ' tcx , Clause < ' tcx > > for Binder < ' tcx , ClauseKind < ' tcx > > {
1244+ #[ inline( always) ]
1245+ fn to_predicate ( self , tcx : TyCtxt < ' tcx > ) -> Clause < ' tcx > {
1246+ tcx. mk_predicate ( self . map_bound ( |clause| ty:: PredicateKind :: Clause ( clause) ) ) . expect_clause ( )
1247+ }
1248+ }
1249+
12251250impl < ' tcx > ToPredicate < ' tcx > for TraitRef < ' tcx > {
12261251 #[ inline( always) ]
12271252 fn to_predicate ( self , tcx : TyCtxt < ' tcx > ) -> Predicate < ' tcx > {
12281253 ty:: Binder :: dummy ( self ) . to_predicate ( tcx)
12291254 }
12301255}
12311256
1232- impl < ' tcx > ToPredicate < ' tcx , Binder < ' tcx , ClauseKind < ' tcx > > > for TraitRef < ' tcx > {
1257+ impl < ' tcx > ToPredicate < ' tcx , Clause < ' tcx > > for TraitRef < ' tcx > {
12331258 #[ 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- } ) )
1259+ fn to_predicate ( self , tcx : TyCtxt < ' tcx > ) -> Clause < ' tcx > {
1260+ let p: Predicate < ' tcx > = self . to_predicate ( tcx) ;
1261+ p. expect_clause ( )
12401262 }
12411263}
12421264
@@ -1248,9 +1270,9 @@ impl<'tcx> ToPredicate<'tcx> for Binder<'tcx, TraitRef<'tcx>> {
12481270 }
12491271}
12501272
1251- impl < ' tcx > ToPredicate < ' tcx , Binder < ' tcx , ClauseKind < ' tcx > > > for Binder < ' tcx , TraitRef < ' tcx > > {
1273+ impl < ' tcx > ToPredicate < ' tcx , Clause < ' tcx > > for Binder < ' tcx , TraitRef < ' tcx > > {
12521274 #[ inline( always) ]
1253- fn to_predicate ( self , tcx : TyCtxt < ' tcx > ) -> Binder < ' tcx , ClauseKind < ' tcx > > {
1275+ fn to_predicate ( self , tcx : TyCtxt < ' tcx > ) -> Clause < ' tcx > {
12541276 let pred: PolyTraitPredicate < ' tcx > = self . to_predicate ( tcx) ;
12551277 pred. to_predicate ( tcx)
12561278 }
@@ -1285,9 +1307,10 @@ impl<'tcx> ToPredicate<'tcx> for PolyTraitPredicate<'tcx> {
12851307 }
12861308}
12871309
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) )
1310+ impl < ' tcx > ToPredicate < ' tcx , Clause < ' tcx > > for PolyTraitPredicate < ' tcx > {
1311+ fn to_predicate ( self , tcx : TyCtxt < ' tcx > ) -> Clause < ' tcx > {
1312+ let p: Predicate < ' tcx > = self . to_predicate ( tcx) ;
1313+ p. expect_clause ( )
12911314 }
12921315}
12931316
@@ -1309,9 +1332,10 @@ impl<'tcx> ToPredicate<'tcx> for PolyProjectionPredicate<'tcx> {
13091332 }
13101333}
13111334
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) )
1335+ impl < ' tcx > ToPredicate < ' tcx , Clause < ' tcx > > for PolyProjectionPredicate < ' tcx > {
1336+ fn to_predicate ( self , tcx : TyCtxt < ' tcx > ) -> Clause < ' tcx > {
1337+ let p: Predicate < ' tcx > = self . to_predicate ( tcx) ;
1338+ p. expect_clause ( )
13151339 }
13161340}
13171341
@@ -1385,18 +1409,17 @@ impl<'tcx> Predicate<'tcx> {
13851409 }
13861410 }
13871411
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 ,
1412+ pub fn as_clause ( self ) -> Option < Clause < ' tcx > > {
1413+ match self . kind ( ) . skip_binder ( ) {
1414+ PredicateKind :: Clause ( ..) => Some ( self . expect_clause ( ) ) ,
1415+ _ => None ,
1416+ }
1417+ }
1418+
1419+ pub fn expect_clause ( self ) -> Clause < ' tcx > {
1420+ match self . kind ( ) . skip_binder ( ) {
1421+ PredicateKind :: Clause ( ..) => Clause ( self . 0 ) ,
1422+ _ => bug ! ( ) ,
14001423 }
14011424 }
14021425}
0 commit comments