@@ -409,6 +409,85 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
409
409
err. downgrade_to_delayed_bug ( ) ;
410
410
}
411
411
412
+ if let ( ty:: Adt ( adt_def, _) , SelfSource :: QPath ( _) ) = ( rcvr_ty. kind ( ) , source) {
413
+ // Look at all the associated functions without receivers in the type's inherent impls
414
+ // to look for builders that return `Self`, `Option<Self>` or `Result<Self, _>`.
415
+ let mut items = self
416
+ . tcx
417
+ . inherent_impls ( adt_def. did ( ) )
418
+ . iter ( )
419
+ . flat_map ( |i| self . tcx . associated_items ( i) . in_definition_order ( ) )
420
+ . filter ( |item| {
421
+ // Only assoc fn with no receivers.
422
+ matches ! ( item. kind, ty:: AssocKind :: Fn ) && !item. fn_has_self_parameter
423
+ } )
424
+ . filter_map ( |item| {
425
+ // Only assoc fns that return `Self`, `Option<Self>` or `Result<Self, _>`.
426
+ let ret_ty = self . tcx . fn_sig ( item. def_id ) . skip_binder ( ) . output ( ) . skip_binder ( ) ;
427
+ let ty:: Adt ( def, args) = ret_ty. kind ( ) else {
428
+ return None ;
429
+ } ;
430
+ // Check for `-> Self`
431
+ if self . can_eq ( self . param_env , ret_ty, rcvr_ty) {
432
+ return Some ( ( item. def_id , ret_ty) ) ;
433
+ }
434
+ // Check for `-> Option<Self>` or `-> Result<Self, _>`
435
+ if ![
436
+ self . tcx . lang_items ( ) . option_type ( ) ,
437
+ self . tcx . get_diagnostic_item ( sym:: Result ) ,
438
+ ]
439
+ . contains ( & Some ( def. did ( ) ) )
440
+ {
441
+ return None ;
442
+ }
443
+ let arg = args. get ( 0 ) ?;
444
+ if self . can_eq ( self . param_env , rcvr_ty, arg. expect_ty ( ) ) {
445
+ Some ( ( item. def_id , ret_ty) )
446
+ } else {
447
+ None
448
+ }
449
+ } )
450
+ . collect :: < Vec < _ > > ( ) ;
451
+ let post = if items. len ( ) > 5 {
452
+ let items_len = items. len ( ) ;
453
+ items. truncate ( 4 ) ;
454
+ format ! ( "\n and {} others" , items_len - 4 )
455
+ } else {
456
+ String :: new ( )
457
+ } ;
458
+ match & items[ ..] {
459
+ [ ] => { }
460
+ [ ( def_id, ret_ty) ] => {
461
+ err. span_note (
462
+ self . tcx . def_span ( def_id) ,
463
+ format ! (
464
+ "if you're trying to build a new `{rcvr_ty}`, consider using `{}` \
465
+ which returns `{ret_ty}`",
466
+ self . tcx. def_path_str( def_id) ,
467
+ ) ,
468
+ ) ;
469
+ }
470
+ _ => {
471
+ let span: MultiSpan = items
472
+ . iter ( )
473
+ . map ( |( def_id, _) | self . tcx . def_span ( def_id) )
474
+ . collect :: < Vec < Span > > ( )
475
+ . into ( ) ;
476
+ err. span_note (
477
+ span,
478
+ format ! (
479
+ "if you're trying to build a new `{rcvr_ty}` consider using one of the \
480
+ following associated functions:\n {}{post}",
481
+ items
482
+ . iter( )
483
+ . map( |( def_id, _ret_ty) | self . tcx. def_path_str( def_id) )
484
+ . collect:: <Vec <String >>( )
485
+ . join( "\n " )
486
+ ) ,
487
+ ) ;
488
+ }
489
+ }
490
+ } ;
412
491
if tcx. ty_is_opaque_future ( rcvr_ty) && item_name. name == sym:: poll {
413
492
err. help ( format ! (
414
493
"method `poll` found on `Pin<&mut {ty_str}>`, \
0 commit comments