@@ -4,6 +4,7 @@ use rustc_hir as hir;
44use rustc_hir:: def:: DefKind ;
55use rustc_middle:: traits:: { ObligationCause , ObligationCauseCode } ;
66use rustc_middle:: ty:: error:: { ExpectedFound , TypeError } ;
7+ use rustc_middle:: ty:: fast_reject:: { DeepRejectCtxt , TreatParams } ;
78use rustc_middle:: ty:: print:: { FmtPrinter , Printer } ;
89use rustc_middle:: ty:: { self , suggest_constraining_type_param, Ty } ;
910use rustc_span:: def_id:: DefId ;
@@ -313,11 +314,15 @@ impl<T> Trait<T> for X {
313314 ( ty:: Dynamic ( t, _, ty:: DynKind :: Dyn ) , _)
314315 if let Some ( def_id) = t. principal_def_id ( ) =>
315316 {
316- let mut impl_def_ids = vec ! [ ] ;
317+ let mut has_matching_impl = false ;
317318 tcx. for_each_relevant_impl ( def_id, values. found , |did| {
318- impl_def_ids. push ( did)
319+ if DeepRejectCtxt :: new ( tcx, TreatParams :: ForLookup )
320+ . types_may_unify ( values. found , tcx. type_of ( did) . skip_binder ( ) )
321+ {
322+ has_matching_impl = true ;
323+ }
319324 } ) ;
320- if let [ _ ] = & impl_def_ids [ .. ] {
325+ if has_matching_impl {
321326 let trait_name = tcx. item_name ( def_id) ;
322327 diag. help ( format ! (
323328 "`{}` implements `{trait_name}` so you could box the found value \
@@ -330,11 +335,15 @@ impl<T> Trait<T> for X {
330335 ( _, ty:: Dynamic ( t, _, ty:: DynKind :: Dyn ) )
331336 if let Some ( def_id) = t. principal_def_id ( ) =>
332337 {
333- let mut impl_def_ids = vec ! [ ] ;
338+ let mut has_matching_impl = false ;
334339 tcx. for_each_relevant_impl ( def_id, values. expected , |did| {
335- impl_def_ids. push ( did)
340+ if DeepRejectCtxt :: new ( tcx, TreatParams :: ForLookup )
341+ . types_may_unify ( values. expected , tcx. type_of ( did) . skip_binder ( ) )
342+ {
343+ has_matching_impl = true ;
344+ }
336345 } ) ;
337- if let [ _ ] = & impl_def_ids [ .. ] {
346+ if has_matching_impl {
338347 let trait_name = tcx. item_name ( def_id) ;
339348 diag. help ( format ! (
340349 "`{}` implements `{trait_name}` so you could change the expected \
@@ -346,11 +355,15 @@ impl<T> Trait<T> for X {
346355 ( ty:: Dynamic ( t, _, ty:: DynKind :: DynStar ) , _)
347356 if let Some ( def_id) = t. principal_def_id ( ) =>
348357 {
349- let mut impl_def_ids = vec ! [ ] ;
358+ let mut has_matching_impl = false ;
350359 tcx. for_each_relevant_impl ( def_id, values. found , |did| {
351- impl_def_ids. push ( did)
360+ if DeepRejectCtxt :: new ( tcx, TreatParams :: ForLookup )
361+ . types_may_unify ( values. found , tcx. type_of ( did) . skip_binder ( ) )
362+ {
363+ has_matching_impl = true ;
364+ }
352365 } ) ;
353- if let [ _ ] = & impl_def_ids [ .. ] {
366+ if has_matching_impl {
354367 let trait_name = tcx. item_name ( def_id) ;
355368 diag. help ( format ! (
356369 "`{}` implements `{trait_name}`, `#[feature(dyn_star)]` is likely \
0 commit comments