@@ -561,6 +561,42 @@ impl rustc_errors::IntoDiagnosticArg for Predicate<'_> {
561
561
}
562
562
}
563
563
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
+
564
600
#[ derive( Clone , Copy , PartialEq , Eq , Hash , TyEncodable , TyDecodable ) ]
565
601
#[ derive( HashStable , TypeFoldable , TypeVisitable , Lift ) ]
566
602
/// A clause is something that can appear in where bounds or be inferred
@@ -592,24 +628,6 @@ pub enum ClauseKind<'tcx> {
592
628
ConstEvaluatable ( ty:: Const < ' tcx > ) ,
593
629
}
594
630
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
-
613
631
#[ derive( Clone , Copy , PartialEq , Eq , Hash , TyEncodable , TyDecodable ) ]
614
632
#[ derive( HashStable , TypeFoldable , TypeVisitable , Lift ) ]
615
633
pub enum PredicateKind < ' tcx > {
@@ -1222,21 +1240,25 @@ impl<'tcx> ToPredicate<'tcx> for Binder<'tcx, ClauseKind<'tcx>> {
1222
1240
}
1223
1241
}
1224
1242
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
+
1225
1250
impl < ' tcx > ToPredicate < ' tcx > for TraitRef < ' tcx > {
1226
1251
#[ inline( always) ]
1227
1252
fn to_predicate ( self , tcx : TyCtxt < ' tcx > ) -> Predicate < ' tcx > {
1228
1253
ty:: Binder :: dummy ( self ) . to_predicate ( tcx)
1229
1254
}
1230
1255
}
1231
1256
1232
- impl < ' tcx > ToPredicate < ' tcx , Binder < ' tcx , ClauseKind < ' tcx > > > for TraitRef < ' tcx > {
1257
+ impl < ' tcx > ToPredicate < ' tcx , Clause < ' tcx > > for TraitRef < ' tcx > {
1233
1258
#[ 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 ( )
1240
1262
}
1241
1263
}
1242
1264
@@ -1248,9 +1270,9 @@ impl<'tcx> ToPredicate<'tcx> for Binder<'tcx, TraitRef<'tcx>> {
1248
1270
}
1249
1271
}
1250
1272
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 > > {
1252
1274
#[ inline( always) ]
1253
- fn to_predicate ( self , tcx : TyCtxt < ' tcx > ) -> Binder < ' tcx , ClauseKind < ' tcx > > {
1275
+ fn to_predicate ( self , tcx : TyCtxt < ' tcx > ) -> Clause < ' tcx > {
1254
1276
let pred: PolyTraitPredicate < ' tcx > = self . to_predicate ( tcx) ;
1255
1277
pred. to_predicate ( tcx)
1256
1278
}
@@ -1285,9 +1307,10 @@ impl<'tcx> ToPredicate<'tcx> for PolyTraitPredicate<'tcx> {
1285
1307
}
1286
1308
}
1287
1309
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 ( )
1291
1314
}
1292
1315
}
1293
1316
@@ -1309,9 +1332,10 @@ impl<'tcx> ToPredicate<'tcx> for PolyProjectionPredicate<'tcx> {
1309
1332
}
1310
1333
}
1311
1334
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 ( )
1315
1339
}
1316
1340
}
1317
1341
@@ -1385,18 +1409,17 @@ impl<'tcx> Predicate<'tcx> {
1385
1409
}
1386
1410
}
1387
1411
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 ! ( ) ,
1400
1423
}
1401
1424
}
1402
1425
}
0 commit comments