Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change terminology for assoc method suggestions when they are not called #103350

Merged
merged 2 commits into from
Oct 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 29 additions & 13 deletions compiler/rustc_resolve/src/late/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ type Res = def::Res<ast::NodeId>;
/// A field or associated item from self type suggested in case of resolution failure.
enum AssocSuggestion {
Field,
MethodWithSelf,
AssocFn,
MethodWithSelf { called: bool },
AssocFn { called: bool },
AssocType,
AssocConst,
}
Expand All @@ -48,8 +48,14 @@ impl AssocSuggestion {
fn action(&self) -> &'static str {
match self {
AssocSuggestion::Field => "use the available field",
AssocSuggestion::MethodWithSelf => "call the method with the fully-qualified path",
AssocSuggestion::AssocFn => "call the associated function",
AssocSuggestion::MethodWithSelf { called: true } => {
"call the method with the fully-qualified path"
}
AssocSuggestion::MethodWithSelf { called: false } => {
"refer to the method with the fully-qualified path"
}
AssocSuggestion::AssocFn { called: true } => "call the associated function",
AssocSuggestion::AssocFn { called: false } => "refer to the associated function",
AssocSuggestion::AssocConst => "use the associated `const`",
AssocSuggestion::AssocType => "use the associated type",
}
Expand Down Expand Up @@ -498,7 +504,9 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
// Try Levenshtein algorithm.
let typo_sugg = self.lookup_typo_candidate(path, source.namespace(), is_expected);
if path.len() == 1 && self.self_type_is_available() {
if let Some(candidate) = self.lookup_assoc_candidate(ident, ns, is_expected) {
if let Some(candidate) =
self.lookup_assoc_candidate(ident, ns, is_expected, source.is_call())
{
let self_is_available = self.self_value_is_available(path[0].ident.span);
match candidate {
AssocSuggestion::Field => {
Expand All @@ -513,16 +521,21 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
err.span_label(span, "a field by this name exists in `Self`");
}
}
AssocSuggestion::MethodWithSelf if self_is_available => {
AssocSuggestion::MethodWithSelf { called } if self_is_available => {
let msg = if called {
"you might have meant to call the method"
} else {
"you might have meant to refer to the method"
};
err.span_suggestion(
span,
"you might have meant to call the method",
msg,
format!("self.{path_str}"),
Applicability::MachineApplicable,
);
}
AssocSuggestion::MethodWithSelf
| AssocSuggestion::AssocFn
AssocSuggestion::MethodWithSelf { .. }
| AssocSuggestion::AssocFn { .. }
| AssocSuggestion::AssocConst
| AssocSuggestion::AssocType => {
err.span_suggestion(
Expand Down Expand Up @@ -1494,6 +1507,7 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
ident: Ident,
ns: Namespace,
filter_fn: FilterFn,
called: bool,
) -> Option<AssocSuggestion>
where
FilterFn: Fn(Res) -> bool,
Expand Down Expand Up @@ -1535,9 +1549,9 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
return Some(match &assoc_item.kind {
ast::AssocItemKind::Const(..) => AssocSuggestion::AssocConst,
ast::AssocItemKind::Fn(box ast::Fn { sig, .. }) if sig.decl.has_self() => {
AssocSuggestion::MethodWithSelf
AssocSuggestion::MethodWithSelf { called }
}
ast::AssocItemKind::Fn(..) => AssocSuggestion::AssocFn,
ast::AssocItemKind::Fn(..) => AssocSuggestion::AssocFn { called },
ast::AssocItemKind::Type(..) => AssocSuggestion::AssocType,
ast::AssocItemKind::MacCall(_) => continue,
});
Expand All @@ -1556,10 +1570,12 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
let res = binding.res();
if filter_fn(res) {
if self.r.has_self.contains(&res.def_id()) {
return Some(AssocSuggestion::MethodWithSelf);
return Some(AssocSuggestion::MethodWithSelf { called });
} else {
match res {
Res::Def(DefKind::AssocFn, _) => return Some(AssocSuggestion::AssocFn),
Res::Def(DefKind::AssocFn, _) => {
return Some(AssocSuggestion::AssocFn { called });
}
Res::Def(DefKind::AssocConst, _) => {
return Some(AssocSuggestion::AssocConst);
}
Expand Down
49 changes: 42 additions & 7 deletions src/test/ui/resolve/issue-14254.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,12 @@ error[E0425]: cannot find value `bah` in this scope
--> $DIR/issue-14254.rs:36:9
|
LL | bah;
| ^^^ help: you might have meant to call the associated function: `Self::bah`
| ^^^
|
help: you might have meant to refer to the associated function
|
LL | Self::bah;
| ~~~~~~~~~

error[E0425]: cannot find value `b` in this scope
--> $DIR/issue-14254.rs:38:9
Expand Down Expand Up @@ -56,7 +61,12 @@ error[E0425]: cannot find value `bah` in this scope
--> $DIR/issue-14254.rs:53:9
|
LL | bah;
| ^^^ help: you might have meant to call the associated function: `Self::bah`
| ^^^
|
help: you might have meant to refer to the associated function
|
LL | Self::bah;
| ~~~~~~~~~

error[E0425]: cannot find value `b` in this scope
--> $DIR/issue-14254.rs:55:9
Expand All @@ -68,31 +78,56 @@ error[E0425]: cannot find value `bah` in this scope
--> $DIR/issue-14254.rs:64:9
|
LL | bah;
| ^^^ help: you might have meant to call the associated function: `Self::bah`
| ^^^
|
help: you might have meant to refer to the associated function
|
LL | Self::bah;
| ~~~~~~~~~

error[E0425]: cannot find value `bah` in this scope
--> $DIR/issue-14254.rs:73:9
|
LL | bah;
| ^^^ help: you might have meant to call the associated function: `Self::bah`
| ^^^
|
help: you might have meant to refer to the associated function
|
LL | Self::bah;
| ~~~~~~~~~

error[E0425]: cannot find value `bah` in this scope
--> $DIR/issue-14254.rs:82:9
|
LL | bah;
| ^^^ help: you might have meant to call the associated function: `Self::bah`
| ^^^
|
help: you might have meant to refer to the associated function
|
LL | Self::bah;
| ~~~~~~~~~

error[E0425]: cannot find value `bah` in this scope
--> $DIR/issue-14254.rs:91:9
|
LL | bah;
| ^^^ help: you might have meant to call the associated function: `Self::bah`
| ^^^
|
help: you might have meant to refer to the associated function
|
LL | Self::bah;
| ~~~~~~~~~

error[E0425]: cannot find value `bah` in this scope
--> $DIR/issue-14254.rs:100:9
|
LL | bah;
| ^^^ help: you might have meant to call the associated function: `Self::bah`
| ^^^
|
help: you might have meant to refer to the associated function
|
LL | Self::bah;
| ~~~~~~~~~

error[E0425]: cannot find function `baz` in this scope
--> $DIR/issue-14254.rs:19:9
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/resolve/resolve-assoc-suggestions.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ error[E0425]: cannot find value `method` in this scope
--> $DIR/resolve-assoc-suggestions.rs:34:9
|
LL | method;
| ^^^^^^ help: you might have meant to call the method: `self.method`
| ^^^^^^ help: you might have meant to refer to the method: `self.method`

error: aborting due to 9 previous errors

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ LL | bah;
LL | fn ba() {}
| ------- similarly named function `ba` defined here
|
help: you might have meant to call the associated function
help: you might have meant to refer to the associated function
|
LL | Self::bah;
| ~~~~~~~~~
Expand Down