@@ -144,7 +144,7 @@ fn gather_explicit_predicates_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Gen
144144 }
145145
146146 ItemKind :: Trait ( _, _, _, self_bounds, ..) | ItemKind :: TraitAlias ( _, self_bounds) => {
147- is_trait = Some ( self_bounds) ;
147+ is_trait = Some ( ( self_bounds, item . span ) ) ;
148148 }
149149
150150 ItemKind :: Fn ( sig, _, _) => {
@@ -159,6 +159,34 @@ fn gather_explicit_predicates_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Gen
159159 _ => { }
160160 }
161161 } ;
162+ if let Node :: TraitItem ( item) = node {
163+ let parent = tcx. local_parent ( item. hir_id ( ) . owner . def_id ) ;
164+ let Node :: Item ( parent_trait) = tcx. hir_node_by_def_id ( parent) else {
165+ unreachable ! ( ) ;
166+ } ;
167+
168+ let ( trait_generics, trait_bounds) = match parent_trait. kind {
169+ hir:: ItemKind :: Trait ( .., generics, supertraits, _) => ( generics, supertraits) ,
170+ hir:: ItemKind :: TraitAlias ( generics, supertraits) => ( generics, supertraits) ,
171+ _ => unreachable ! ( ) ,
172+ } ;
173+
174+ // Implicitly add `Self: Trait` clauses on trait associated items.
175+ // See comment on `add_implicit_super_traits` for more details.
176+ if !icx. lowerer ( ) . requires_implicit_supertraits ( parent, trait_bounds, trait_generics) {
177+ let mut bounds = Bounds :: default ( ) ;
178+ let self_ty_where_predicates = ( parent, item. generics . predicates ) ;
179+ icx. lowerer ( ) . add_implicit_traits_with_filter (
180+ & mut bounds,
181+ tcx. types . self_param ,
182+ & [ ] ,
183+ Some ( self_ty_where_predicates) ,
184+ item. span ,
185+ |tr| tr != hir:: LangItem :: Sized ,
186+ ) ;
187+ predicates. extend ( bounds. clauses ( tcx) ) ;
188+ }
189+ }
162190
163191 let generics = tcx. generics_of ( def_id) ;
164192
@@ -167,11 +195,18 @@ fn gather_explicit_predicates_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Gen
167195 // on a trait we must also consider the bounds that follow the trait's name,
168196 // like `trait Foo: A + B + C`.
169197 if let Some ( self_bounds) = is_trait {
170- let bounds = icx. lowerer ( ) . lower_mono_bounds (
198+ let mut bounds = icx. lowerer ( ) . lower_mono_bounds (
171199 tcx. types . self_param ,
172- self_bounds,
200+ self_bounds. 0 ,
173201 PredicateFilter :: All ,
174202 ) ;
203+ icx. lowerer ( ) . add_implicit_super_traits (
204+ def_id,
205+ & mut bounds,
206+ self_bounds. 0 ,
207+ hir_generics,
208+ self_bounds. 1 ,
209+ ) ;
175210 predicates. extend ( bounds. clauses ( tcx) ) ;
176211 effects_min_tys. extend ( bounds. effects_min_tys ( ) ) ;
177212 }
@@ -197,8 +232,8 @@ fn gather_explicit_predicates_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Gen
197232 GenericParamKind :: Type { .. } => {
198233 let param_ty = icx. lowerer ( ) . lower_ty_param ( param. hir_id ) ;
199234 let mut bounds = Bounds :: default ( ) ;
200- // Params are implicitly sized unless a `?Sized ` bound is found
201- icx. lowerer ( ) . add_sized_bound (
235+ // Implicit bounds are added to type params unless a `?Trait ` bound is found
236+ icx. lowerer ( ) . add_implicit_traits (
202237 & mut bounds,
203238 param_ty,
204239 & [ ] ,
@@ -620,7 +655,14 @@ pub(super) fn implied_predicates_with_filter(
620655 let icx = ItemCtxt :: new ( tcx, trait_def_id) ;
621656
622657 let self_param_ty = tcx. types . self_param ;
623- let superbounds = icx. lowerer ( ) . lower_mono_bounds ( self_param_ty, bounds, filter) ;
658+ let mut superbounds = icx. lowerer ( ) . lower_mono_bounds ( self_param_ty, bounds, filter) ;
659+ icx. lowerer ( ) . add_implicit_super_traits (
660+ trait_def_id,
661+ & mut superbounds,
662+ bounds,
663+ generics,
664+ item. span ,
665+ ) ;
624666
625667 let where_bounds_that_match = icx. probe_ty_param_bounds_in_generics (
626668 generics,
0 commit comments