@@ -461,16 +461,36 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
461
461
err. span_label ( span, "this is an associated function, not a method" ) ;
462
462
}
463
463
if static_sources. len ( ) == 1 {
464
+ let ty_str = if let Some ( CandidateSource :: ImplSource (
465
+ impl_did,
466
+ ) ) = static_sources. get ( 0 ) {
467
+ // When the "method" is resolved through dereferencing, we really want the
468
+ // original type that has the associated function for accurate suggestions.
469
+ // (#61411)
470
+ let ty = self . impl_self_ty ( span, * impl_did) . ty ;
471
+ match ( & ty. peel_refs ( ) . kind , & actual. peel_refs ( ) . kind ) {
472
+ ( ty:: Adt ( def, _) , ty:: Adt ( def_actual, _) ) if def == def_actual => {
473
+ // Use `actual` as it will have more `substs` filled in.
474
+ self . ty_to_value_string ( actual. peel_refs ( ) )
475
+ }
476
+ _ => self . ty_to_value_string ( ty. peel_refs ( ) ) ,
477
+ }
478
+ } else {
479
+ self . ty_to_value_string ( actual. peel_refs ( ) )
480
+ } ;
464
481
if let SelfSource :: MethodCall ( expr) = source {
465
- err. span_suggestion ( expr . span . to ( span ) ,
466
- "use associated function syntax instead" ,
467
- format ! ( "{}::{} ",
468
- self . ty_to_string ( actual ) ,
469
- item_name ) ,
470
- Applicability :: MachineApplicable ) ;
482
+ err. span_suggestion (
483
+ expr . span . to ( span ) ,
484
+ "use associated function syntax instead ",
485
+ format ! ( "{}::{}" , ty_str , item_name ) ,
486
+ Applicability :: MachineApplicable ,
487
+ ) ;
471
488
} else {
472
- err. help ( & format ! ( "try with `{}::{}`" ,
473
- self . ty_to_string( actual) , item_name) ) ;
489
+ err. help ( & format ! (
490
+ "try with `{}::{}`" ,
491
+ ty_str,
492
+ item_name,
493
+ ) ) ;
474
494
}
475
495
476
496
report_candidates ( span, & mut err, static_sources) ;
@@ -586,6 +606,14 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
586
606
None
587
607
}
588
608
609
+ /// Print out the type for use in value namespace.
610
+ fn ty_to_value_string ( & self , ty : Ty < ' tcx > ) -> String {
611
+ match ty. kind {
612
+ ty:: Adt ( def, substs) => format ! ( "{}" , ty:: Instance :: new( def. did, substs) ) ,
613
+ _ => self . ty_to_string ( ty) ,
614
+ }
615
+ }
616
+
589
617
fn suggest_use_candidates ( & self ,
590
618
err : & mut DiagnosticBuilder < ' _ > ,
591
619
mut msg : String ,
0 commit comments