@@ -2055,13 +2055,37 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
20552055 }
20562056 }
20572057 ObligationCauseCode :: ImplDerivedObligation ( ref data) => {
2058- let parent_trait_ref = self . resolve_vars_if_possible ( data. parent_trait_ref ) ;
2058+ let mut parent_trait_ref = self . resolve_vars_if_possible ( data. parent_trait_ref ) ;
20592059 err. note ( & format ! (
20602060 "required because of the requirements on the impl of `{}` for `{}`" ,
20612061 parent_trait_ref. print_only_trait_path( ) ,
20622062 parent_trait_ref. skip_binder( ) . self_ty( )
20632063 ) ) ;
2064- let parent_predicate = parent_trait_ref. without_const ( ) . to_predicate ( tcx) ;
2064+
2065+ let mut parent_predicate = parent_trait_ref. without_const ( ) . to_predicate ( tcx) ;
2066+ let mut data = data;
2067+ let mut redundant = false ;
2068+ let mut count = 0 ;
2069+ while let ObligationCauseCode :: ImplDerivedObligation ( child) = & * data. parent_code {
2070+ // Skip redundant recursive obligation notes. See `ui/issue-20413.rs`.
2071+ let child_trait_ref = self . resolve_vars_if_possible ( child. parent_trait_ref ) ;
2072+ if parent_trait_ref. def_id ( ) != child_trait_ref. def_id ( ) {
2073+ break ;
2074+ }
2075+ count += 1 ;
2076+ redundant = true ;
2077+ data = child;
2078+ parent_predicate = child_trait_ref. without_const ( ) . to_predicate ( tcx) ;
2079+ parent_trait_ref = child_trait_ref;
2080+ }
2081+ if redundant {
2082+ err. note ( & format ! ( "{} redundant requirements hidden" , count) ) ;
2083+ err. note ( & format ! (
2084+ "required because of the requirements on the impl of `{}` for `{}`" ,
2085+ parent_trait_ref. print_only_trait_path( ) ,
2086+ parent_trait_ref. skip_binder( ) . self_ty( )
2087+ ) ) ;
2088+ }
20652089 // #74711: avoid a stack overflow
20662090 ensure_sufficient_stack ( || {
20672091 self . note_obligation_cause_code (
@@ -2087,15 +2111,15 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
20872111 }
20882112 ObligationCauseCode :: CompareImplMethodObligation { .. } => {
20892113 err. note ( & format ! (
2090- "the requirement `{}` appears on the impl method \
2091- but not on the corresponding trait method",
2114+ "the requirement `{}` appears on the impl method but not on the corresponding \
2115+ trait method",
20922116 predicate
20932117 ) ) ;
20942118 }
20952119 ObligationCauseCode :: CompareImplTypeObligation { .. } => {
20962120 err. note ( & format ! (
2097- "the requirement `{}` appears on the associated impl type \
2098- but not on the corresponding associated trait type",
2121+ "the requirement `{}` appears on the associated impl type but not on the \
2122+ corresponding associated trait type",
20992123 predicate
21002124 ) ) ;
21012125 }
0 commit comments