@@ -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
}
@@ -516,7 +522,9 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
516
522
let typo_sugg =
517
523
self . lookup_typo_candidate ( path, source. namespace ( ) , is_expected) . to_opt_suggestion ( ) ;
518
524
if path. len ( ) == 1 && self . self_type_is_available ( ) {
519
- if let Some ( candidate) = self . lookup_assoc_candidate ( ident, ns, is_expected) {
525
+ if let Some ( candidate) =
526
+ self . lookup_assoc_candidate ( ident, ns, is_expected, source. is_call ( ) )
527
+ {
520
528
let self_is_available = self . self_value_is_available ( path[ 0 ] . ident . span ) ;
521
529
match candidate {
522
530
AssocSuggestion :: Field => {
@@ -531,16 +539,21 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
531
539
err. span_label ( span, "a field by this name exists in `Self`" ) ;
532
540
}
533
541
}
534
- AssocSuggestion :: MethodWithSelf if self_is_available => {
542
+ AssocSuggestion :: MethodWithSelf { called } if self_is_available => {
543
+ let msg = if called {
544
+ "you might have meant to call the method"
545
+ } else {
546
+ "you might have meant to refer to the method"
547
+ } ;
535
548
err. span_suggestion (
536
549
span,
537
- "you might have meant to call the method" ,
550
+ msg ,
538
551
format ! ( "self.{path_str}" ) ,
539
552
Applicability :: MachineApplicable ,
540
553
) ;
541
554
}
542
- AssocSuggestion :: MethodWithSelf
543
- | AssocSuggestion :: AssocFn
555
+ AssocSuggestion :: MethodWithSelf { .. }
556
+ | AssocSuggestion :: AssocFn { .. }
544
557
| AssocSuggestion :: AssocConst
545
558
| AssocSuggestion :: AssocType => {
546
559
err. span_suggestion (
@@ -1498,6 +1511,7 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
1498
1511
ident : Ident ,
1499
1512
ns : Namespace ,
1500
1513
filter_fn : FilterFn ,
1514
+ called : bool ,
1501
1515
) -> Option < AssocSuggestion >
1502
1516
where
1503
1517
FilterFn : Fn ( Res ) -> bool ,
@@ -1539,9 +1553,9 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
1539
1553
return Some ( match & assoc_item. kind {
1540
1554
ast:: AssocItemKind :: Const ( ..) => AssocSuggestion :: AssocConst ,
1541
1555
ast:: AssocItemKind :: Fn ( box ast:: Fn { sig, .. } ) if sig. decl . has_self ( ) => {
1542
- AssocSuggestion :: MethodWithSelf
1556
+ AssocSuggestion :: MethodWithSelf { called }
1543
1557
}
1544
- ast:: AssocItemKind :: Fn ( ..) => AssocSuggestion :: AssocFn ,
1558
+ ast:: AssocItemKind :: Fn ( ..) => AssocSuggestion :: AssocFn { called } ,
1545
1559
ast:: AssocItemKind :: Type ( ..) => AssocSuggestion :: AssocType ,
1546
1560
ast:: AssocItemKind :: MacCall ( _) => continue ,
1547
1561
} ) ;
@@ -1560,10 +1574,12 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
1560
1574
let res = binding. res ( ) ;
1561
1575
if filter_fn ( res) {
1562
1576
if self . r . has_self . contains ( & res. def_id ( ) ) {
1563
- return Some ( AssocSuggestion :: MethodWithSelf ) ;
1577
+ return Some ( AssocSuggestion :: MethodWithSelf { called } ) ;
1564
1578
} else {
1565
1579
match res {
1566
- Res :: Def ( DefKind :: AssocFn , _) => return Some ( AssocSuggestion :: AssocFn ) ,
1580
+ Res :: Def ( DefKind :: AssocFn , _) => {
1581
+ return Some ( AssocSuggestion :: AssocFn { called } ) ;
1582
+ }
1567
1583
Res :: Def ( DefKind :: AssocConst , _) => {
1568
1584
return Some ( AssocSuggestion :: AssocConst ) ;
1569
1585
}
0 commit comments