@@ -164,12 +164,41 @@ fn gather_explicit_predicates_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Gen
164164 }
165165
166166 ItemKind :: Trait ( _, _, _, self_bounds, ..) | ItemKind :: TraitAlias ( _, self_bounds) => {
167- is_trait = Some ( self_bounds) ;
167+ is_trait = Some ( ( self_bounds, item . span ) ) ;
168168 }
169169 _ => { }
170170 }
171171 } ;
172172
173+ if let Node :: TraitItem ( item) = node {
174+ let parent = tcx. local_parent ( item. hir_id ( ) . owner . def_id ) ;
175+ let Node :: Item ( parent_trait) = tcx. hir_node_by_def_id ( parent) else {
176+ unreachable ! ( ) ;
177+ } ;
178+
179+ let ( trait_generics, trait_bounds) = match parent_trait. kind {
180+ hir:: ItemKind :: Trait ( .., generics, supertraits, _) => ( generics, supertraits) ,
181+ hir:: ItemKind :: TraitAlias ( generics, supertraits) => ( generics, supertraits) ,
182+ _ => unreachable ! ( ) ,
183+ } ;
184+
185+ // Implicitly add `Self: Trait` clauses on trait associated items.
186+ // See comment on `add_implicit_super_traits` for more details.
187+ if !icx. lowerer ( ) . requires_implicit_supertraits ( parent, trait_bounds, trait_generics) {
188+ let mut bounds = Vec :: new ( ) ;
189+ let self_ty_where_predicates = ( parent, item. generics . predicates ) ;
190+ icx. lowerer ( ) . add_implicit_traits_with_filter (
191+ & mut bounds,
192+ tcx. types . self_param ,
193+ & [ ] ,
194+ Some ( self_ty_where_predicates) ,
195+ item. span ,
196+ |tr| tr != hir:: LangItem :: Sized ,
197+ ) ;
198+ predicates. extend ( bounds) ;
199+ }
200+ }
201+
173202 let generics = tcx. generics_of ( def_id) ;
174203
175204 // Below we'll consider the bounds on the type parameters (including `Self`)
@@ -180,11 +209,18 @@ fn gather_explicit_predicates_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Gen
180209 let mut bounds = Vec :: new ( ) ;
181210 icx. lowerer ( ) . lower_bounds (
182211 tcx. types . self_param ,
183- self_bounds,
212+ self_bounds. 0 ,
184213 & mut bounds,
185214 ty:: List :: empty ( ) ,
186215 PredicateFilter :: All ,
187216 ) ;
217+ icx. lowerer ( ) . add_implicit_super_traits (
218+ def_id,
219+ & mut bounds,
220+ self_bounds. 0 ,
221+ hir_generics,
222+ self_bounds. 1 ,
223+ ) ;
188224 predicates. extend ( bounds) ;
189225 }
190226
@@ -209,8 +245,8 @@ fn gather_explicit_predicates_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Gen
209245 GenericParamKind :: Type { .. } => {
210246 let param_ty = icx. lowerer ( ) . lower_ty_param ( param. hir_id ) ;
211247 let mut bounds = Vec :: new ( ) ;
212- // Params are implicitly sized unless a `?Sized ` bound is found
213- icx. lowerer ( ) . add_sized_bound (
248+ // // Implicit bounds are added to type params unless a `?Trait ` bound is found
249+ icx. lowerer ( ) . add_implicit_traits (
214250 & mut bounds,
215251 param_ty,
216252 & [ ] ,
@@ -624,6 +660,22 @@ pub(super) fn implied_predicates_with_filter<'tcx>(
624660 let self_param_ty = tcx. types . self_param ;
625661 let mut bounds = Vec :: new ( ) ;
626662 icx. lowerer ( ) . lower_bounds ( self_param_ty, superbounds, & mut bounds, ty:: List :: empty ( ) , filter) ;
663+ match filter {
664+ PredicateFilter :: All
665+ | PredicateFilter :: SelfOnly
666+ | PredicateFilter :: SelfTraitThatDefines ( _)
667+ | PredicateFilter :: SelfAndAssociatedTypeBounds => {
668+ icx. lowerer ( ) . add_implicit_super_traits (
669+ trait_def_id,
670+ & mut bounds,
671+ superbounds,
672+ generics,
673+ item. span ,
674+ ) ;
675+ }
676+ //`ConstIfConst` is only interested in `~const` bounds.
677+ PredicateFilter :: ConstIfConst | PredicateFilter :: SelfConstIfConst => { }
678+ }
627679
628680 let where_bounds_that_match =
629681 icx. probe_ty_param_bounds_in_generics ( generics, item. owner_id . def_id , filter) ;
0 commit comments