@@ -586,6 +586,24 @@ pub enum Clause<'tcx> {
586
586
ConstArgHasType ( Const < ' tcx > , Ty < ' tcx > ) ,
587
587
}
588
588
589
+ impl < ' tcx > Binder < ' tcx , Clause < ' tcx > > {
590
+ pub fn as_trait_clause ( self ) -> Option < Binder < ' tcx , TraitPredicate < ' tcx > > > {
591
+ if let ty:: Clause :: Trait ( trait_clause) = self . skip_binder ( ) {
592
+ Some ( self . rebind ( trait_clause) )
593
+ } else {
594
+ None
595
+ }
596
+ }
597
+
598
+ pub fn as_projection_clause ( self ) -> Option < Binder < ' tcx , ProjectionPredicate < ' tcx > > > {
599
+ if let ty:: Clause :: Projection ( projection_clause) = self . skip_binder ( ) {
600
+ Some ( self . rebind ( projection_clause) )
601
+ } else {
602
+ None
603
+ }
604
+ }
605
+ }
606
+
589
607
#[ derive( Clone , Copy , PartialEq , Eq , Hash , TyEncodable , TyDecodable ) ]
590
608
#[ derive( HashStable , TypeFoldable , TypeVisitable , Lift ) ]
591
609
pub enum PredicateKind < ' tcx > {
@@ -1203,6 +1221,17 @@ impl<'tcx> ToPredicate<'tcx> for TraitRef<'tcx> {
1203
1221
}
1204
1222
}
1205
1223
1224
+ impl < ' tcx > ToPredicate < ' tcx , Binder < ' tcx , Clause < ' tcx > > > for TraitRef < ' tcx > {
1225
+ #[ inline( always) ]
1226
+ fn to_predicate ( self , _tcx : TyCtxt < ' tcx > ) -> Binder < ' tcx , Clause < ' tcx > > {
1227
+ Binder :: dummy ( Clause :: Trait ( TraitPredicate {
1228
+ trait_ref : self ,
1229
+ constness : ty:: BoundConstness :: NotConst ,
1230
+ polarity : ty:: ImplPolarity :: Positive ,
1231
+ } ) )
1232
+ }
1233
+ }
1234
+
1206
1235
impl < ' tcx > ToPredicate < ' tcx > for Binder < ' tcx , TraitRef < ' tcx > > {
1207
1236
#[ inline( always) ]
1208
1237
fn to_predicate ( self , tcx : TyCtxt < ' tcx > ) -> Predicate < ' tcx > {
@@ -1211,6 +1240,14 @@ impl<'tcx> ToPredicate<'tcx> for Binder<'tcx, TraitRef<'tcx>> {
1211
1240
}
1212
1241
}
1213
1242
1243
+ impl < ' tcx > ToPredicate < ' tcx , Binder < ' tcx , Clause < ' tcx > > > for Binder < ' tcx , TraitRef < ' tcx > > {
1244
+ #[ inline( always) ]
1245
+ fn to_predicate ( self , tcx : TyCtxt < ' tcx > ) -> Binder < ' tcx , Clause < ' tcx > > {
1246
+ let pred: PolyTraitPredicate < ' tcx > = self . to_predicate ( tcx) ;
1247
+ pred. to_predicate ( tcx)
1248
+ }
1249
+ }
1250
+
1214
1251
impl < ' tcx > ToPredicate < ' tcx , PolyTraitPredicate < ' tcx > > for Binder < ' tcx , TraitRef < ' tcx > > {
1215
1252
#[ inline( always) ]
1216
1253
fn to_predicate ( self , _: TyCtxt < ' tcx > ) -> PolyTraitPredicate < ' tcx > {
@@ -1240,6 +1277,12 @@ impl<'tcx> ToPredicate<'tcx> for PolyTraitPredicate<'tcx> {
1240
1277
}
1241
1278
}
1242
1279
1280
+ impl < ' tcx > ToPredicate < ' tcx , Binder < ' tcx , Clause < ' tcx > > > for PolyTraitPredicate < ' tcx > {
1281
+ fn to_predicate ( self , _tcx : TyCtxt < ' tcx > ) -> Binder < ' tcx , Clause < ' tcx > > {
1282
+ self . map_bound ( |p| Clause :: Trait ( p) )
1283
+ }
1284
+ }
1285
+
1243
1286
impl < ' tcx > ToPredicate < ' tcx > for PolyRegionOutlivesPredicate < ' tcx > {
1244
1287
fn to_predicate ( self , tcx : TyCtxt < ' tcx > ) -> Predicate < ' tcx > {
1245
1288
self . map_bound ( |p| PredicateKind :: Clause ( Clause :: RegionOutlives ( p) ) ) . to_predicate ( tcx)
@@ -1258,6 +1301,12 @@ impl<'tcx> ToPredicate<'tcx> for PolyProjectionPredicate<'tcx> {
1258
1301
}
1259
1302
}
1260
1303
1304
+ impl < ' tcx > ToPredicate < ' tcx , Binder < ' tcx , Clause < ' tcx > > > for PolyProjectionPredicate < ' tcx > {
1305
+ fn to_predicate ( self , _tcx : TyCtxt < ' tcx > ) -> Binder < ' tcx , Clause < ' tcx > > {
1306
+ self . map_bound ( |p| Clause :: Projection ( p) )
1307
+ }
1308
+ }
1309
+
1261
1310
impl < ' tcx > ToPredicate < ' tcx > for TraitPredicate < ' tcx > {
1262
1311
fn to_predicate ( self , tcx : TyCtxt < ' tcx > ) -> Predicate < ' tcx > {
1263
1312
PredicateKind :: Clause ( Clause :: Trait ( self ) ) . to_predicate ( tcx)
@@ -1327,6 +1376,23 @@ impl<'tcx> Predicate<'tcx> {
1327
1376
| PredicateKind :: TypeWellFormedFromEnv ( ..) => None ,
1328
1377
}
1329
1378
}
1379
+
1380
+ pub fn as_clause ( self ) -> Option < Binder < ' tcx , Clause < ' tcx > > > {
1381
+ let predicate = self . kind ( ) ;
1382
+ match predicate. skip_binder ( ) {
1383
+ PredicateKind :: Clause ( clause) => Some ( predicate. rebind ( clause) ) ,
1384
+ PredicateKind :: AliasRelate ( ..)
1385
+ | PredicateKind :: Subtype ( ..)
1386
+ | PredicateKind :: Coerce ( ..)
1387
+ | PredicateKind :: WellFormed ( ..)
1388
+ | PredicateKind :: ObjectSafe ( ..)
1389
+ | PredicateKind :: ClosureKind ( ..)
1390
+ | PredicateKind :: ConstEvaluatable ( ..)
1391
+ | PredicateKind :: ConstEquate ( ..)
1392
+ | PredicateKind :: Ambiguous
1393
+ | PredicateKind :: TypeWellFormedFromEnv ( ..) => None ,
1394
+ }
1395
+ }
1330
1396
}
1331
1397
1332
1398
/// Represents the bounds declared on a particular set of type
0 commit comments