@@ -8,6 +8,7 @@ use rustc_errors::{
8
8
MultiSpan ,
9
9
} ;
10
10
use rustc_hir as hir;
11
+ use rustc_hir:: def:: DefKind ;
11
12
use rustc_hir:: def_id:: DefId ;
12
13
use rustc_hir:: lang_items:: LangItem ;
13
14
use rustc_hir:: { ExprKind , Node , QPath } ;
@@ -29,7 +30,7 @@ use std::cmp::Ordering;
29
30
use std:: iter;
30
31
31
32
use super :: probe:: { Mode , ProbeScope } ;
32
- use super :: { CandidateSource , MethodError , NoMatchData } ;
33
+ use super :: { super :: suggest_call_constructor , CandidateSource , MethodError , NoMatchData } ;
33
34
34
35
impl < ' a , ' tcx > FnCtxt < ' a , ' tcx > {
35
36
fn is_fn_ty ( & self , ty : Ty < ' tcx > , span : Span ) -> bool {
@@ -488,19 +489,32 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
488
489
}
489
490
490
491
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
-
497
492
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
503
504
}
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
+ ) ;
504
518
}
505
519
}
506
520
}
0 commit comments