@@ -4,6 +4,7 @@ use rustc_hir as hir;
4
4
use rustc_hir:: def:: DefKind ;
5
5
use rustc_middle:: traits:: { ObligationCause , ObligationCauseCode } ;
6
6
use rustc_middle:: ty:: error:: { ExpectedFound , TypeError } ;
7
+ use rustc_middle:: ty:: fast_reject:: { DeepRejectCtxt , TreatParams } ;
7
8
use rustc_middle:: ty:: print:: { FmtPrinter , Printer } ;
8
9
use rustc_middle:: ty:: { self , suggest_constraining_type_param, Ty } ;
9
10
use rustc_span:: def_id:: DefId ;
@@ -313,11 +314,15 @@ impl<T> Trait<T> for X {
313
314
( ty:: Dynamic ( t, _, ty:: DynKind :: Dyn ) , _)
314
315
if let Some ( def_id) = t. principal_def_id ( ) =>
315
316
{
316
- let mut impl_def_ids = vec ! [ ] ;
317
+ let mut has_matching_impl = false ;
317
318
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
+ }
319
324
} ) ;
320
- if let [ _ ] = & impl_def_ids [ .. ] {
325
+ if has_matching_impl {
321
326
let trait_name = tcx. item_name ( def_id) ;
322
327
diag. help ( format ! (
323
328
"`{}` implements `{trait_name}` so you could box the found value \
@@ -330,11 +335,15 @@ impl<T> Trait<T> for X {
330
335
( _, ty:: Dynamic ( t, _, ty:: DynKind :: Dyn ) )
331
336
if let Some ( def_id) = t. principal_def_id ( ) =>
332
337
{
333
- let mut impl_def_ids = vec ! [ ] ;
338
+ let mut has_matching_impl = false ;
334
339
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
+ }
336
345
} ) ;
337
- if let [ _ ] = & impl_def_ids [ .. ] {
346
+ if has_matching_impl {
338
347
let trait_name = tcx. item_name ( def_id) ;
339
348
diag. help ( format ! (
340
349
"`{}` implements `{trait_name}` so you could change the expected \
@@ -346,11 +355,15 @@ impl<T> Trait<T> for X {
346
355
( ty:: Dynamic ( t, _, ty:: DynKind :: DynStar ) , _)
347
356
if let Some ( def_id) = t. principal_def_id ( ) =>
348
357
{
349
- let mut impl_def_ids = vec ! [ ] ;
358
+ let mut has_matching_impl = false ;
350
359
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
+ }
352
365
} ) ;
353
- if let [ _ ] = & impl_def_ids [ .. ] {
366
+ if has_matching_impl {
354
367
let trait_name = tcx. item_name ( def_id) ;
355
368
diag. help ( format ! (
356
369
"`{}` implements `{trait_name}`, `#[feature(dyn_star)]` is likely \
0 commit comments