@@ -23,8 +23,22 @@ pub(super) fn check_refining_return_position_impl_trait_in_trait<'tcx>(
2323 if !tcx. impl_method_has_trait_impl_trait_tys ( impl_m. def_id ) {
2424 return ;
2525 }
26+ // crate-private traits don't have any library guarantees, there's no need to do this check.
27+ if !tcx. visibility ( trait_m. container_id ( tcx) ) . is_public ( ) {
28+ return ;
29+ }
2630
31+ // If a type in the trait ref is private, then there's also no reason to to do this check.
2732 let impl_def_id = impl_m. container_id ( tcx) ;
33+ for arg in impl_trait_ref. args {
34+ if let Some ( ty) = arg. as_type ( )
35+ && let Some ( self_visibility) = type_visibility ( tcx, ty)
36+ && !self_visibility. is_public ( )
37+ {
38+ return ;
39+ }
40+ }
41+
2842 let impl_m_args = ty:: GenericArgs :: identity_for_item ( tcx, impl_m. def_id ) ;
2943 let trait_m_to_impl_m_args = impl_m_args. rebase_onto ( tcx, impl_def_id, impl_trait_ref. args ) ;
3044 let bound_trait_m_sig = tcx. fn_sig ( trait_m. def_id ) . instantiate ( tcx, trait_m_to_impl_m_args) ;
@@ -281,3 +295,17 @@ fn report_mismatched_rpitit_signature<'tcx>(
281295 } ,
282296 ) ;
283297}
298+
299+ fn type_visibility < ' tcx > ( tcx : TyCtxt < ' tcx > , ty : Ty < ' tcx > ) -> Option < ty:: Visibility < DefId > > {
300+ match * ty. kind ( ) {
301+ ty:: Ref ( _, ty, _) => type_visibility ( tcx, ty) ,
302+ ty:: Adt ( def, args) => {
303+ if def. is_fundamental ( ) {
304+ type_visibility ( tcx, args. type_at ( 0 ) )
305+ } else {
306+ Some ( tcx. visibility ( def. did ( ) ) )
307+ }
308+ }
309+ _ => None ,
310+ }
311+ }
0 commit comments