@@ -409,6 +409,86 @@ 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 ( ) ;
427
+ let ret_ty = self . tcx . erase_late_bound_regions ( ret_ty) ;
428
+ let ty:: Adt ( def, args) = ret_ty. kind ( ) else {
429
+ return None ;
430
+ } ;
431
+ // Check for `-> Self`
432
+ if self . can_eq ( self . param_env , ret_ty, rcvr_ty) {
433
+ return Some ( ( item. def_id , ret_ty) ) ;
434
+ }
435
+ // Check for `-> Option<Self>` or `-> Result<Self, _>`
436
+ if ![
437
+ self . tcx . lang_items ( ) . option_type ( ) ,
438
+ self . tcx . get_diagnostic_item ( sym:: Result ) ,
439
+ ]
440
+ . contains ( & Some ( def. did ( ) ) )
441
+ {
442
+ return None ;
443
+ }
444
+ let arg = args. get ( 0 ) ?. expect_ty ( ) ;
445
+ if self . can_eq ( self . param_env , rcvr_ty, arg) {
446
+ Some ( ( item. def_id , ret_ty) )
447
+ } else {
448
+ None
449
+ }
450
+ } )
451
+ . collect :: < Vec < _ > > ( ) ;
452
+ let post = if items. len ( ) > 5 {
453
+ let items_len = items. len ( ) ;
454
+ items. truncate ( 4 ) ;
455
+ format ! ( "\n and {} others" , items_len - 4 )
456
+ } else {
457
+ String :: new ( )
458
+ } ;
459
+ match & items[ ..] {
460
+ [ ] => { }
461
+ [ ( def_id, ret_ty) ] => {
462
+ err. span_note (
463
+ self . tcx . def_span ( def_id) ,
464
+ format ! (
465
+ "if you're trying to build a new `{rcvr_ty}`, consider using `{}` \
466
+ which returns `{ret_ty}`",
467
+ self . tcx. def_path_str( def_id) ,
468
+ ) ,
469
+ ) ;
470
+ }
471
+ _ => {
472
+ let span: MultiSpan = items
473
+ . iter ( )
474
+ . map ( |( def_id, _) | self . tcx . def_span ( def_id) )
475
+ . collect :: < Vec < Span > > ( )
476
+ . into ( ) ;
477
+ err. span_note (
478
+ span,
479
+ format ! (
480
+ "if you're trying to build a new `{rcvr_ty}` consider using one of the \
481
+ following associated functions:\n {}{post}",
482
+ items
483
+ . iter( )
484
+ . map( |( def_id, _ret_ty) | self . tcx. def_path_str( def_id) )
485
+ . collect:: <Vec <String >>( )
486
+ . join( "\n " )
487
+ ) ,
488
+ ) ;
489
+ }
490
+ }
491
+ } ;
412
492
if tcx. ty_is_opaque_future ( rcvr_ty) && item_name. name == sym:: poll {
413
493
err. help ( format ! (
414
494
"method `poll` found on `Pin<&mut {ty_str}>`, \
0 commit comments