@@ -8,6 +8,7 @@ use rustc_errors::{
88 MultiSpan ,
99} ;
1010use rustc_hir as hir;
11+ use rustc_hir:: def:: DefKind ;
1112use rustc_hir:: def_id:: DefId ;
1213use rustc_hir:: lang_items:: LangItem ;
1314use rustc_hir:: { ExprKind , Node , QPath } ;
@@ -29,7 +30,7 @@ use std::cmp::Ordering;
2930use std:: iter;
3031
3132use super :: probe:: { Mode , ProbeScope } ;
32- use super :: { CandidateSource , MethodError , NoMatchData } ;
33+ use super :: { super :: suggest_call_constructor , CandidateSource , MethodError , NoMatchData } ;
3334
3435impl < ' a , ' tcx > FnCtxt < ' a , ' tcx > {
3536 fn is_fn_ty ( & self , ty : Ty < ' tcx > , span : Span ) -> bool {
@@ -488,19 +489,32 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
488489 }
489490
490491 if self . is_fn_ty ( rcvr_ty, span) {
491- fn report_function < T : std:: fmt:: Display > ( err : & mut Diagnostic , name : T ) {
492- err. note (
493- & format ! ( "`{}` is a function, perhaps you wish to call it" , name, ) ,
494- ) ;
495- }
496-
497492 if let SelfSource :: MethodCall ( expr) = source {
498- if let Ok ( expr_string) = tcx. sess . source_map ( ) . span_to_snippet ( expr. span ) {
499- report_function ( & mut err, expr_string) ;
500- } else if let ExprKind :: Path ( QPath :: Resolved ( _, path) ) = expr. kind {
501- if let Some ( segment) = path. segments . last ( ) {
502- report_function ( & mut err, segment. ident ) ;
493+ let suggest = if let ty:: FnDef ( def_id, _) = rcvr_ty. kind ( ) {
494+ let local_id = def_id. expect_local ( ) ;
495+ let hir_id = tcx. hir ( ) . local_def_id_to_hir_id ( local_id) ;
496+ let node = tcx. hir ( ) . get ( hir_id) ;
497+ let fields = node. tuple_fields ( ) ;
498+
499+ if let Some ( fields) = fields
500+ && let Some ( DefKind :: Ctor ( of, _) ) = self . tcx . opt_def_kind ( local_id) {
501+ Some ( ( fields, of) )
502+ } else {
503+ None
503504 }
505+ } else {
506+ None
507+ } ;
508+
509+ // If the function is a tuple constructor, we recommend that they call it
510+ if let Some ( ( fields, kind) ) = suggest {
511+ suggest_call_constructor ( expr. span , kind, fields. len ( ) , & mut err) ;
512+ } else {
513+ // General case
514+ err. span_label (
515+ expr. span ,
516+ "this is a function, perhaps you wish to call it" ,
517+ ) ;
504518 }
505519 }
506520 }
0 commit comments