1
- use crate :: astconv:: { AstConv , OnlySelfBounds } ;
1
+ use crate :: astconv:: { AstConv , OnlySelfBounds , PredicateFilter } ;
2
2
use crate :: bounds:: Bounds ;
3
3
use crate :: collect:: ItemCtxt ;
4
4
use crate :: constrained_generic_params as cgp;
@@ -125,7 +125,7 @@ fn gather_explicit_predicates_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Gen
125
125
if let Some ( self_bounds) = is_trait {
126
126
predicates. extend (
127
127
icx. astconv ( )
128
- . compute_bounds ( tcx. types . self_param , self_bounds, OnlySelfBounds ( false ) )
128
+ . compute_bounds ( tcx. types . self_param , self_bounds, PredicateFilter :: All )
129
129
. clauses ( ) ,
130
130
) ;
131
131
}
@@ -530,19 +530,6 @@ pub(super) fn explicit_predicates_of<'tcx>(
530
530
}
531
531
}
532
532
533
- #[ derive( Copy , Clone , Debug ) ]
534
- pub enum PredicateFilter {
535
- /// All predicates may be implied by the trait
536
- All ,
537
-
538
- /// Only traits that reference `Self: ..` are implied by the trait
539
- SelfOnly ,
540
-
541
- /// Only traits that reference `Self: ..` and define an associated type
542
- /// with the given ident are implied by the trait
543
- SelfThatDefines ( Ident ) ,
544
- }
545
-
546
533
/// Ensures that the super-predicates of the trait with a `DefId`
547
534
/// of `trait_def_id` are converted and stored. This also ensures that
548
535
/// the transitive super-predicates are converted.
@@ -564,11 +551,15 @@ pub(super) fn implied_predicates_of(
564
551
tcx : TyCtxt < ' _ > ,
565
552
trait_def_id : LocalDefId ,
566
553
) -> ty:: GenericPredicates < ' _ > {
567
- if tcx. is_trait_alias ( trait_def_id. to_def_id ( ) ) {
568
- implied_predicates_with_filter ( tcx, trait_def_id. to_def_id ( ) , PredicateFilter :: All )
569
- } else {
570
- tcx. super_predicates_of ( trait_def_id)
571
- }
554
+ implied_predicates_with_filter (
555
+ tcx,
556
+ trait_def_id. to_def_id ( ) ,
557
+ if tcx. is_trait_alias ( trait_def_id. to_def_id ( ) ) {
558
+ PredicateFilter :: All
559
+ } else {
560
+ PredicateFilter :: SelfAndAssociatedTypeBounds
561
+ } ,
562
+ )
572
563
}
573
564
574
565
/// Ensures that the super-predicates of the trait with a `DefId`
@@ -601,44 +592,14 @@ pub(super) fn implied_predicates_with_filter(
601
592
let icx = ItemCtxt :: new ( tcx, trait_def_id) ;
602
593
603
594
let self_param_ty = tcx. types . self_param ;
604
- let ( superbounds, where_bounds_that_match) = match filter {
605
- PredicateFilter :: All => (
606
- // Convert the bounds that follow the colon (or equal in trait aliases)
607
- icx. astconv ( ) . compute_bounds ( self_param_ty, bounds, OnlySelfBounds ( false ) ) ,
608
- // Also include all where clause bounds
609
- icx. type_parameter_bounds_in_generics (
610
- generics,
611
- item. owner_id . def_id ,
612
- self_param_ty,
613
- OnlySelfBounds ( false ) ,
614
- None ,
615
- ) ,
616
- ) ,
617
- PredicateFilter :: SelfOnly => (
618
- // Convert the bounds that follow the colon (or equal in trait aliases)
619
- icx. astconv ( ) . compute_bounds ( self_param_ty, bounds, OnlySelfBounds ( true ) ) ,
620
- // Include where clause bounds for `Self`
621
- icx. type_parameter_bounds_in_generics (
622
- generics,
623
- item. owner_id . def_id ,
624
- self_param_ty,
625
- OnlySelfBounds ( true ) ,
626
- None ,
627
- ) ,
628
- ) ,
629
- PredicateFilter :: SelfThatDefines ( assoc_name) => (
630
- // Convert the bounds that follow the colon (or equal) that reference the associated name
631
- icx. astconv ( ) . compute_bounds_that_match_assoc_item ( self_param_ty, bounds, assoc_name) ,
632
- // Include where clause bounds for `Self` that reference the associated name
633
- icx. type_parameter_bounds_in_generics (
634
- generics,
635
- item. owner_id . def_id ,
636
- self_param_ty,
637
- OnlySelfBounds ( true ) ,
638
- Some ( assoc_name) ,
639
- ) ,
640
- ) ,
641
- } ;
595
+ let superbounds = icx. astconv ( ) . compute_bounds ( self_param_ty, bounds, filter) ;
596
+
597
+ let where_bounds_that_match = icx. type_parameter_bounds_in_generics (
598
+ generics,
599
+ item. owner_id . def_id ,
600
+ self_param_ty,
601
+ filter,
602
+ ) ;
642
603
643
604
// Combine the two lists to form the complete set of superbounds:
644
605
let implied_bounds =
@@ -743,8 +704,7 @@ pub(super) fn type_param_predicates(
743
704
ast_generics,
744
705
def_id,
745
706
ty,
746
- OnlySelfBounds ( true ) ,
747
- Some ( assoc_name) ,
707
+ PredicateFilter :: SelfThatDefines ( assoc_name) ,
748
708
)
749
709
. into_iter ( )
750
710
. filter ( |( predicate, _) | match predicate. kind ( ) . skip_binder ( ) {
@@ -768,8 +728,7 @@ impl<'tcx> ItemCtxt<'tcx> {
768
728
ast_generics : & ' tcx hir:: Generics < ' tcx > ,
769
729
param_def_id : LocalDefId ,
770
730
ty : Ty < ' tcx > ,
771
- only_self_bounds : OnlySelfBounds ,
772
- assoc_name : Option < Ident > ,
731
+ filter : PredicateFilter ,
773
732
) -> Vec < ( ty:: Clause < ' tcx > , Span ) > {
774
733
let mut bounds = Bounds :: default ( ) ;
775
734
@@ -778,9 +737,23 @@ impl<'tcx> ItemCtxt<'tcx> {
778
737
continue ;
779
738
} ;
780
739
740
+ let ( only_self_bounds, assoc_name) = match filter {
741
+ PredicateFilter :: All | PredicateFilter :: SelfAndAssociatedTypeBounds => {
742
+ ( OnlySelfBounds ( false ) , None )
743
+ }
744
+ PredicateFilter :: SelfOnly => ( OnlySelfBounds ( true ) , None ) ,
745
+ PredicateFilter :: SelfThatDefines ( assoc_name) => {
746
+ ( OnlySelfBounds ( true ) , Some ( assoc_name) )
747
+ }
748
+ } ;
749
+
750
+ // Subtle: If we're collecting `SelfAndAssociatedTypeBounds`, then we
751
+ // want to only consider predicates with `Self: ...`, but we don't want
752
+ // `OnlySelfBounds(true)` since we want to collect the nested associated
753
+ // type bound as well.
781
754
let bound_ty = if predicate. is_param_bound ( param_def_id. to_def_id ( ) ) {
782
755
ty
783
- } else if !only_self_bounds . 0 {
756
+ } else if matches ! ( filter , PredicateFilter :: All ) {
784
757
self . to_ty ( predicate. bounded_ty )
785
758
} else {
786
759
continue ;
0 commit comments