@@ -557,7 +557,11 @@ pub(super) fn explicit_supertraits_containing_assoc_item<'tcx>(
557
557
tcx : TyCtxt < ' tcx > ,
558
558
( trait_def_id, assoc_name) : ( DefId , Ident ) ,
559
559
) -> ty:: EarlyBinder < ' tcx , & ' tcx [ ( ty:: Clause < ' tcx > , Span ) ] > {
560
- implied_predicates_with_filter ( tcx, trait_def_id, PredicateFilter :: SelfThatDefines ( assoc_name) )
560
+ implied_predicates_with_filter (
561
+ tcx,
562
+ trait_def_id,
563
+ PredicateFilter :: SelfTraitThatDefines ( assoc_name) ,
564
+ )
561
565
}
562
566
563
567
pub ( super ) fn explicit_implied_predicates_of < ' tcx > (
@@ -586,7 +590,7 @@ pub(super) fn implied_predicates_with_filter<'tcx>(
586
590
let Some ( trait_def_id) = trait_def_id. as_local ( ) else {
587
591
// if `assoc_name` is None, then the query should've been redirected to an
588
592
// external provider
589
- assert_matches ! ( filter, PredicateFilter :: SelfThatDefines ( _) ) ;
593
+ assert_matches ! ( filter, PredicateFilter :: SelfTraitThatDefines ( _) ) ;
590
594
return tcx. explicit_super_predicates_of ( trait_def_id) ;
591
595
} ;
592
596
@@ -606,12 +610,8 @@ pub(super) fn implied_predicates_with_filter<'tcx>(
606
610
let mut bounds = Bounds :: default ( ) ;
607
611
icx. lowerer ( ) . lower_bounds ( self_param_ty, superbounds, & mut bounds, ty:: List :: empty ( ) , filter) ;
608
612
609
- let where_bounds_that_match = icx. probe_ty_param_bounds_in_generics (
610
- generics,
611
- item. owner_id . def_id ,
612
- self_param_ty,
613
- filter,
614
- ) ;
613
+ let where_bounds_that_match =
614
+ icx. probe_ty_param_bounds_in_generics ( generics, item. owner_id . def_id , filter) ;
615
615
616
616
// Combine the two lists to form the complete set of superbounds:
617
617
let implied_bounds =
@@ -652,7 +652,7 @@ pub(super) fn implied_predicates_with_filter<'tcx>(
652
652
}
653
653
654
654
// Make sure when elaborating supertraits, probing for associated types, etc.,
655
- // we really truly are elaborating clauses that have `Self ` as their self type.
655
+ // we really truly are elaborating clauses that have `ty ` as their self type.
656
656
// This is very important since downstream code relies on this being correct.
657
657
pub ( super ) fn assert_only_contains_predicates_from < ' tcx > (
658
658
filter : PredicateFilter ,
@@ -664,7 +664,7 @@ pub(super) fn assert_only_contains_predicates_from<'tcx>(
664
664
}
665
665
666
666
match filter {
667
- PredicateFilter :: SelfOnly | PredicateFilter :: SelfThatDefines ( _ ) => {
667
+ PredicateFilter :: SelfOnly => {
668
668
for ( clause, _) in bounds {
669
669
match clause. kind ( ) . skip_binder ( ) {
670
670
ty:: ClauseKind :: Trait ( trait_predicate) => {
@@ -704,6 +704,33 @@ pub(super) fn assert_only_contains_predicates_from<'tcx>(
704
704
}
705
705
}
706
706
}
707
+ PredicateFilter :: SelfTraitThatDefines ( _) => {
708
+ for ( clause, _) in bounds {
709
+ match clause. kind ( ) . skip_binder ( ) {
710
+ ty:: ClauseKind :: Trait ( trait_predicate) => {
711
+ assert_eq ! (
712
+ trait_predicate. self_ty( ) ,
713
+ ty,
714
+ "expected `Self` predicate when computing \
715
+ `{filter:?}` implied bounds: {clause:?}"
716
+ ) ;
717
+ }
718
+
719
+ ty:: ClauseKind :: Projection ( _)
720
+ | ty:: ClauseKind :: TypeOutlives ( _)
721
+ | ty:: ClauseKind :: RegionOutlives ( _)
722
+ | ty:: ClauseKind :: ConstArgHasType ( _, _)
723
+ | ty:: ClauseKind :: WellFormed ( _)
724
+ | ty:: ClauseKind :: ConstEvaluatable ( _)
725
+ | ty:: ClauseKind :: HostEffect ( ..) => {
726
+ bug ! (
727
+ "unexpected non-`Self` predicate when computing \
728
+ `{filter:?}` implied bounds: {clause:?}"
729
+ ) ;
730
+ }
731
+ }
732
+ }
733
+ }
707
734
PredicateFilter :: ConstIfConst => {
708
735
for ( clause, _) in bounds {
709
736
match clause. kind ( ) . skip_binder ( ) {
@@ -767,21 +794,16 @@ pub(super) fn type_param_predicates<'tcx>(
767
794
None => { }
768
795
}
769
796
770
- use rustc_hir:: * ;
771
- use rustc_middle:: ty:: Ty ;
772
-
773
797
// In the HIR, bounds can derive from two places. Either
774
798
// written inline like `<T: Foo>` or in a where-clause like
775
799
// `where T: Foo`.
776
800
777
801
let param_id = tcx. local_def_id_to_hir_id ( def_id) ;
778
802
let param_owner = tcx. hir ( ) . ty_param_owner ( def_id) ;
779
- let generics = tcx. generics_of ( param_owner) ;
780
- let index = generics. param_def_id_to_index [ & def_id. to_def_id ( ) ] ;
781
- let ty = Ty :: new_param ( tcx, index, tcx. hir ( ) . ty_param_name ( def_id) ) ;
782
803
783
804
// Don't look for bounds where the type parameter isn't in scope.
784
805
let parent = if item_def_id == param_owner {
806
+ // FIXME: Shouldn't this be unreachable?
785
807
None
786
808
} else {
787
809
tcx. generics_of ( item_def_id) . parent . map ( |def_id| def_id. expect_local ( ) )
@@ -801,8 +823,9 @@ pub(super) fn type_param_predicates<'tcx>(
801
823
let Some ( hir_generics) = hir_node. generics ( ) else {
802
824
return result;
803
825
} ;
826
+
804
827
if let Node :: Item ( item) = hir_node
805
- && let ItemKind :: Trait ( ..) = item. kind
828
+ && let hir :: ItemKind :: Trait ( ..) = item. kind
806
829
// Implied `Self: Trait` and supertrait bounds.
807
830
&& param_id == item_hir_id
808
831
{
@@ -811,23 +834,34 @@ pub(super) fn type_param_predicates<'tcx>(
811
834
}
812
835
813
836
let icx = ItemCtxt :: new ( tcx, item_def_id) ;
814
- let extra_predicates = extend. into_iter ( ) . chain (
815
- icx. probe_ty_param_bounds_in_generics (
816
- hir_generics,
817
- def_id,
818
- ty,
819
- PredicateFilter :: SelfThatDefines ( assoc_name) ,
820
- )
821
- . into_iter ( )
822
- . filter ( |( predicate, _) | match predicate. kind ( ) . skip_binder ( ) {
823
- ty:: ClauseKind :: Trait ( data) => data. self_ty ( ) . is_param ( index) ,
824
- _ => false ,
825
- } ) ,
837
+ let extra_predicates = extend. into_iter ( ) . chain ( icx. probe_ty_param_bounds_in_generics (
838
+ hir_generics,
839
+ def_id,
840
+ PredicateFilter :: SelfTraitThatDefines ( assoc_name) ,
841
+ ) ) ;
842
+
843
+ let bounds =
844
+ & * tcx. arena . alloc_from_iter ( result. skip_binder ( ) . iter ( ) . copied ( ) . chain ( extra_predicates) ) ;
845
+
846
+ // Double check that the bounds *only* contain `SelfTy: Trait` preds.
847
+ let self_ty = match tcx. def_kind ( def_id) {
848
+ DefKind :: TyParam => Ty :: new_param (
849
+ tcx,
850
+ tcx. generics_of ( item_def_id)
851
+ . param_def_id_to_index ( tcx, def_id. to_def_id ( ) )
852
+ . expect ( "expected generic param to be owned by item" ) ,
853
+ tcx. item_name ( def_id. to_def_id ( ) ) ,
854
+ ) ,
855
+ DefKind :: Trait | DefKind :: TraitAlias => tcx. types . self_param ,
856
+ _ => unreachable ! ( ) ,
857
+ } ;
858
+ assert_only_contains_predicates_from (
859
+ PredicateFilter :: SelfTraitThatDefines ( assoc_name) ,
860
+ bounds,
861
+ self_ty,
826
862
) ;
827
863
828
- ty:: EarlyBinder :: bind (
829
- tcx. arena . alloc_from_iter ( result. skip_binder ( ) . iter ( ) . copied ( ) . chain ( extra_predicates) ) ,
830
- )
864
+ ty:: EarlyBinder :: bind ( bounds)
831
865
}
832
866
833
867
impl < ' tcx > ItemCtxt < ' tcx > {
@@ -841,7 +875,6 @@ impl<'tcx> ItemCtxt<'tcx> {
841
875
& self ,
842
876
hir_generics : & ' tcx hir:: Generics < ' tcx > ,
843
877
param_def_id : LocalDefId ,
844
- ty : Ty < ' tcx > ,
845
878
filter : PredicateFilter ,
846
879
) -> Vec < ( ty:: Clause < ' tcx > , Span ) > {
847
880
let mut bounds = Bounds :: default ( ) ;
@@ -851,13 +884,21 @@ impl<'tcx> ItemCtxt<'tcx> {
851
884
continue ;
852
885
} ;
853
886
854
- let bound_ty = if predicate. is_param_bound ( param_def_id. to_def_id ( ) ) {
855
- ty
856
- } else if matches ! ( filter, PredicateFilter :: All ) {
857
- self . lowerer ( ) . lower_ty_maybe_return_type_notation ( predicate. bounded_ty )
858
- } else {
859
- continue ;
860
- } ;
887
+ match filter {
888
+ _ if predicate. is_param_bound ( param_def_id. to_def_id ( ) ) => {
889
+ // Ok
890
+ }
891
+ PredicateFilter :: All => {
892
+ // Ok
893
+ }
894
+ PredicateFilter :: SelfOnly
895
+ | PredicateFilter :: SelfTraitThatDefines ( _)
896
+ | PredicateFilter :: SelfConstIfConst
897
+ | PredicateFilter :: SelfAndAssociatedTypeBounds => continue ,
898
+ PredicateFilter :: ConstIfConst => unreachable ! ( ) ,
899
+ }
900
+
901
+ let bound_ty = self . lowerer ( ) . lower_ty_maybe_return_type_notation ( predicate. bounded_ty ) ;
861
902
862
903
let bound_vars = self . tcx . late_bound_vars ( predicate. hir_id ) ;
863
904
self . lowerer ( ) . lower_bounds (
0 commit comments