Rollup merge of #103350 - clubby789:refer-to-assoc-method, r=wesleywiser

Change terminology for assoc method suggestions when they are not called

Fixes #103325

```@rustbot``` label +A-diagnostics
This commit is contained in:
Yuki Okushi 2022-10-25 08:01:27 +09:00 committed by GitHub
commit e47d222a94
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 73 additions and 22 deletions

View File

@ -38,8 +38,8 @@ type Res = def::Res<ast::NodeId>;
/// A field or associated item from self type suggested in case of resolution failure. /// A field or associated item from self type suggested in case of resolution failure.
enum AssocSuggestion { enum AssocSuggestion {
Field, Field,
MethodWithSelf, MethodWithSelf { called: bool },
AssocFn, AssocFn { called: bool },
AssocType, AssocType,
AssocConst, AssocConst,
} }
@ -48,8 +48,14 @@ impl AssocSuggestion {
fn action(&self) -> &'static str { fn action(&self) -> &'static str {
match self { match self {
AssocSuggestion::Field => "use the available field", AssocSuggestion::Field => "use the available field",
AssocSuggestion::MethodWithSelf => "call the method with the fully-qualified path", AssocSuggestion::MethodWithSelf { called: true } => {
AssocSuggestion::AssocFn => "call the associated function", "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::AssocConst => "use the associated `const`",
AssocSuggestion::AssocType => "use the associated type", AssocSuggestion::AssocType => "use the associated type",
} }
@ -516,7 +522,9 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
let typo_sugg = let typo_sugg =
self.lookup_typo_candidate(path, source.namespace(), is_expected).to_opt_suggestion(); self.lookup_typo_candidate(path, source.namespace(), is_expected).to_opt_suggestion();
if path.len() == 1 && self.self_type_is_available() { 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); let self_is_available = self.self_value_is_available(path[0].ident.span);
match candidate { match candidate {
AssocSuggestion::Field => { AssocSuggestion::Field => {
@ -531,16 +539,21 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
err.span_label(span, "a field by this name exists in `Self`"); 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( err.span_suggestion(
span, span,
"you might have meant to call the method", msg,
format!("self.{path_str}"), format!("self.{path_str}"),
Applicability::MachineApplicable, Applicability::MachineApplicable,
); );
} }
AssocSuggestion::MethodWithSelf AssocSuggestion::MethodWithSelf { .. }
| AssocSuggestion::AssocFn | AssocSuggestion::AssocFn { .. }
| AssocSuggestion::AssocConst | AssocSuggestion::AssocConst
| AssocSuggestion::AssocType => { | AssocSuggestion::AssocType => {
err.span_suggestion( err.span_suggestion(
@ -1498,6 +1511,7 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
ident: Ident, ident: Ident,
ns: Namespace, ns: Namespace,
filter_fn: FilterFn, filter_fn: FilterFn,
called: bool,
) -> Option<AssocSuggestion> ) -> Option<AssocSuggestion>
where where
FilterFn: Fn(Res) -> bool, FilterFn: Fn(Res) -> bool,
@ -1539,9 +1553,9 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
return Some(match &assoc_item.kind { return Some(match &assoc_item.kind {
ast::AssocItemKind::Const(..) => AssocSuggestion::AssocConst, ast::AssocItemKind::Const(..) => AssocSuggestion::AssocConst,
ast::AssocItemKind::Fn(box ast::Fn { sig, .. }) if sig.decl.has_self() => { 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::Type(..) => AssocSuggestion::AssocType,
ast::AssocItemKind::MacCall(_) => continue, ast::AssocItemKind::MacCall(_) => continue,
}); });
@ -1560,10 +1574,12 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
let res = binding.res(); let res = binding.res();
if filter_fn(res) { if filter_fn(res) {
if self.r.has_self.contains(&res.def_id()) { if self.r.has_self.contains(&res.def_id()) {
return Some(AssocSuggestion::MethodWithSelf); return Some(AssocSuggestion::MethodWithSelf { called });
} else { } else {
match res { match res {
Res::Def(DefKind::AssocFn, _) => return Some(AssocSuggestion::AssocFn), Res::Def(DefKind::AssocFn, _) => {
return Some(AssocSuggestion::AssocFn { called });
}
Res::Def(DefKind::AssocConst, _) => { Res::Def(DefKind::AssocConst, _) => {
return Some(AssocSuggestion::AssocConst); return Some(AssocSuggestion::AssocConst);
} }

View File

@ -26,7 +26,12 @@ error[E0425]: cannot find value `bah` in this scope
--> $DIR/issue-14254.rs:36:9 --> $DIR/issue-14254.rs:36:9
| |
LL | bah; 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 error[E0425]: cannot find value `b` in this scope
--> $DIR/issue-14254.rs:38:9 --> $DIR/issue-14254.rs:38:9
@ -56,7 +61,12 @@ error[E0425]: cannot find value `bah` in this scope
--> $DIR/issue-14254.rs:53:9 --> $DIR/issue-14254.rs:53:9
| |
LL | bah; 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 error[E0425]: cannot find value `b` in this scope
--> $DIR/issue-14254.rs:55:9 --> $DIR/issue-14254.rs:55:9
@ -68,31 +78,56 @@ error[E0425]: cannot find value `bah` in this scope
--> $DIR/issue-14254.rs:64:9 --> $DIR/issue-14254.rs:64:9
| |
LL | bah; 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 error[E0425]: cannot find value `bah` in this scope
--> $DIR/issue-14254.rs:73:9 --> $DIR/issue-14254.rs:73:9
| |
LL | bah; 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 error[E0425]: cannot find value `bah` in this scope
--> $DIR/issue-14254.rs:82:9 --> $DIR/issue-14254.rs:82:9
| |
LL | bah; 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 error[E0425]: cannot find value `bah` in this scope
--> $DIR/issue-14254.rs:91:9 --> $DIR/issue-14254.rs:91:9
| |
LL | bah; 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 error[E0425]: cannot find value `bah` in this scope
--> $DIR/issue-14254.rs:100:9 --> $DIR/issue-14254.rs:100:9
| |
LL | bah; 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 error[E0425]: cannot find function `baz` in this scope
--> $DIR/issue-14254.rs:19:9 --> $DIR/issue-14254.rs:19:9

View File

@ -50,7 +50,7 @@ error[E0425]: cannot find value `method` in this scope
--> $DIR/resolve-assoc-suggestions.rs:34:9 --> $DIR/resolve-assoc-suggestions.rs:34:9
| |
LL | method; 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 error: aborting due to 9 previous errors

View File

@ -40,7 +40,7 @@ LL | bah;
LL | fn ba() {} LL | fn ba() {}
| ------- similarly named function `ba` defined here | ------- 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; LL | Self::bah;
| ~~~~~~~~~ | ~~~~~~~~~