@@ -38,8 +38,8 @@ type Res = def::Res<ast::NodeId>;
38
38
/// A field or associated item from self type suggested in case of resolution failure.
39
39
enum AssocSuggestion {
40
40
Field ,
41
- MethodWithSelf ,
42
- AssocFn ,
41
+ MethodWithSelf { called : bool } ,
42
+ AssocFn { called : bool } ,
43
43
AssocType ,
44
44
AssocConst ,
45
45
}
@@ -48,8 +48,14 @@ impl AssocSuggestion {
48
48
fn action ( & self ) -> & ' static str {
49
49
match self {
50
50
AssocSuggestion :: Field => "use the available field" ,
51
- AssocSuggestion :: MethodWithSelf => "call the method with the fully-qualified path" ,
52
- AssocSuggestion :: AssocFn => "call the associated function" ,
51
+ AssocSuggestion :: MethodWithSelf { called : true } => {
52
+ "call the method with the fully-qualified path"
53
+ }
54
+ AssocSuggestion :: MethodWithSelf { called : false } => {
55
+ "refer to the method with the fully-qualified path"
56
+ }
57
+ AssocSuggestion :: AssocFn { called : true } => "call the associated function" ,
58
+ AssocSuggestion :: AssocFn { called : false } => "refer to the associated function" ,
53
59
AssocSuggestion :: AssocConst => "use the associated `const`" ,
54
60
AssocSuggestion :: AssocType => "use the associated type" ,
55
61
}
@@ -498,7 +504,9 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
498
504
// Try Levenshtein algorithm.
499
505
let typo_sugg = self . lookup_typo_candidate ( path, source. namespace ( ) , is_expected) ;
500
506
if path. len ( ) == 1 && self . self_type_is_available ( ) {
501
- if let Some ( candidate) = self . lookup_assoc_candidate ( ident, ns, is_expected) {
507
+ if let Some ( candidate) =
508
+ self . lookup_assoc_candidate ( ident, ns, is_expected, source. is_call ( ) )
509
+ {
502
510
let self_is_available = self . self_value_is_available ( path[ 0 ] . ident . span ) ;
503
511
match candidate {
504
512
AssocSuggestion :: Field => {
@@ -513,16 +521,21 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
513
521
err. span_label ( span, "a field by this name exists in `Self`" ) ;
514
522
}
515
523
}
516
- AssocSuggestion :: MethodWithSelf if self_is_available => {
524
+ AssocSuggestion :: MethodWithSelf { called } if self_is_available => {
525
+ let msg = if called {
526
+ "you might have meant to call the method"
527
+ } else {
528
+ "you might have meant to refer to the method"
529
+ } ;
517
530
err. span_suggestion (
518
531
span,
519
- "you might have meant to call the method" ,
532
+ msg ,
520
533
format ! ( "self.{path_str}" ) ,
521
534
Applicability :: MachineApplicable ,
522
535
) ;
523
536
}
524
- AssocSuggestion :: MethodWithSelf
525
- | AssocSuggestion :: AssocFn
537
+ AssocSuggestion :: MethodWithSelf { .. }
538
+ | AssocSuggestion :: AssocFn { .. }
526
539
| AssocSuggestion :: AssocConst
527
540
| AssocSuggestion :: AssocType => {
528
541
err. span_suggestion (
@@ -1494,6 +1507,7 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
1494
1507
ident : Ident ,
1495
1508
ns : Namespace ,
1496
1509
filter_fn : FilterFn ,
1510
+ called : bool ,
1497
1511
) -> Option < AssocSuggestion >
1498
1512
where
1499
1513
FilterFn : Fn ( Res ) -> bool ,
@@ -1535,9 +1549,9 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
1535
1549
return Some ( match & assoc_item. kind {
1536
1550
ast:: AssocItemKind :: Const ( ..) => AssocSuggestion :: AssocConst ,
1537
1551
ast:: AssocItemKind :: Fn ( box ast:: Fn { sig, .. } ) if sig. decl . has_self ( ) => {
1538
- AssocSuggestion :: MethodWithSelf
1552
+ AssocSuggestion :: MethodWithSelf { called }
1539
1553
}
1540
- ast:: AssocItemKind :: Fn ( ..) => AssocSuggestion :: AssocFn ,
1554
+ ast:: AssocItemKind :: Fn ( ..) => AssocSuggestion :: AssocFn { called } ,
1541
1555
ast:: AssocItemKind :: Type ( ..) => AssocSuggestion :: AssocType ,
1542
1556
ast:: AssocItemKind :: MacCall ( _) => continue ,
1543
1557
} ) ;
@@ -1556,10 +1570,12 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
1556
1570
let res = binding. res ( ) ;
1557
1571
if filter_fn ( res) {
1558
1572
if self . r . has_self . contains ( & res. def_id ( ) ) {
1559
- return Some ( AssocSuggestion :: MethodWithSelf ) ;
1573
+ return Some ( AssocSuggestion :: MethodWithSelf { called } ) ;
1560
1574
} else {
1561
1575
match res {
1562
- Res :: Def ( DefKind :: AssocFn , _) => return Some ( AssocSuggestion :: AssocFn ) ,
1576
+ Res :: Def ( DefKind :: AssocFn , _) => {
1577
+ return Some ( AssocSuggestion :: AssocFn { called } ) ;
1578
+ }
1563
1579
Res :: Def ( DefKind :: AssocConst , _) => {
1564
1580
return Some ( AssocSuggestion :: AssocConst ) ;
1565
1581
}
0 commit comments