Skip to content

Commit 717bf35

Browse files
committed
Different suggestions for when associated functions are referred to
1 parent 0940040 commit 717bf35

File tree

1 file changed

+29
-13
lines changed

1 file changed

+29
-13
lines changed

compiler/rustc_resolve/src/late/diagnostics.rs

+29-13
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ type Res = def::Res<ast::NodeId>;
3838
/// A field or associated item from self type suggested in case of resolution failure.
3939
enum AssocSuggestion {
4040
Field,
41-
MethodWithSelf,
42-
AssocFn,
41+
MethodWithSelf { called: bool },
42+
AssocFn { called: bool },
4343
AssocType,
4444
AssocConst,
4545
}
@@ -48,8 +48,14 @@ impl AssocSuggestion {
4848
fn action(&self) -> &'static str {
4949
match self {
5050
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",
5359
AssocSuggestion::AssocConst => "use the associated `const`",
5460
AssocSuggestion::AssocType => "use the associated type",
5561
}
@@ -498,7 +504,9 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
498504
// Try Levenshtein algorithm.
499505
let typo_sugg = self.lookup_typo_candidate(path, source.namespace(), is_expected);
500506
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+
{
502510
let self_is_available = self.self_value_is_available(path[0].ident.span);
503511
match candidate {
504512
AssocSuggestion::Field => {
@@ -513,16 +521,21 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
513521
err.span_label(span, "a field by this name exists in `Self`");
514522
}
515523
}
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+
};
517530
err.span_suggestion(
518531
span,
519-
"you might have meant to call the method",
532+
msg,
520533
format!("self.{path_str}"),
521534
Applicability::MachineApplicable,
522535
);
523536
}
524-
AssocSuggestion::MethodWithSelf
525-
| AssocSuggestion::AssocFn
537+
AssocSuggestion::MethodWithSelf { .. }
538+
| AssocSuggestion::AssocFn { .. }
526539
| AssocSuggestion::AssocConst
527540
| AssocSuggestion::AssocType => {
528541
err.span_suggestion(
@@ -1494,6 +1507,7 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
14941507
ident: Ident,
14951508
ns: Namespace,
14961509
filter_fn: FilterFn,
1510+
called: bool,
14971511
) -> Option<AssocSuggestion>
14981512
where
14991513
FilterFn: Fn(Res) -> bool,
@@ -1535,9 +1549,9 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
15351549
return Some(match &assoc_item.kind {
15361550
ast::AssocItemKind::Const(..) => AssocSuggestion::AssocConst,
15371551
ast::AssocItemKind::Fn(box ast::Fn { sig, .. }) if sig.decl.has_self() => {
1538-
AssocSuggestion::MethodWithSelf
1552+
AssocSuggestion::MethodWithSelf { called }
15391553
}
1540-
ast::AssocItemKind::Fn(..) => AssocSuggestion::AssocFn,
1554+
ast::AssocItemKind::Fn(..) => AssocSuggestion::AssocFn { called },
15411555
ast::AssocItemKind::Type(..) => AssocSuggestion::AssocType,
15421556
ast::AssocItemKind::MacCall(_) => continue,
15431557
});
@@ -1556,10 +1570,12 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
15561570
let res = binding.res();
15571571
if filter_fn(res) {
15581572
if self.r.has_self.contains(&res.def_id()) {
1559-
return Some(AssocSuggestion::MethodWithSelf);
1573+
return Some(AssocSuggestion::MethodWithSelf { called });
15601574
} else {
15611575
match res {
1562-
Res::Def(DefKind::AssocFn, _) => return Some(AssocSuggestion::AssocFn),
1576+
Res::Def(DefKind::AssocFn, _) => {
1577+
return Some(AssocSuggestion::AssocFn { called });
1578+
}
15631579
Res::Def(DefKind::AssocConst, _) => {
15641580
return Some(AssocSuggestion::AssocConst);
15651581
}

0 commit comments

Comments
 (0)