Skip to content

Commit a1c5e37

Browse files
author
Yuki Okushi
authored
Rollup merge of rust-lang#103350 - clubby789:refer-to-assoc-method, r=wesleywiser
Change terminology for assoc method suggestions when they are not called Fixes rust-lang#103325 ``@rustbot`` label +A-diagnostics
2 parents 560f7b9 + 3f1e999 commit a1c5e37

File tree

4 files changed

+73
-22
lines changed

4 files changed

+73
-22
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
}
@@ -516,7 +522,9 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
516522
let typo_sugg =
517523
self.lookup_typo_candidate(path, source.namespace(), is_expected).to_opt_suggestion();
518524
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+
{
520528
let self_is_available = self.self_value_is_available(path[0].ident.span);
521529
match candidate {
522530
AssocSuggestion::Field => {
@@ -531,16 +539,21 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
531539
err.span_label(span, "a field by this name exists in `Self`");
532540
}
533541
}
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+
};
535548
err.span_suggestion(
536549
span,
537-
"you might have meant to call the method",
550+
msg,
538551
format!("self.{path_str}"),
539552
Applicability::MachineApplicable,
540553
);
541554
}
542-
AssocSuggestion::MethodWithSelf
543-
| AssocSuggestion::AssocFn
555+
AssocSuggestion::MethodWithSelf { .. }
556+
| AssocSuggestion::AssocFn { .. }
544557
| AssocSuggestion::AssocConst
545558
| AssocSuggestion::AssocType => {
546559
err.span_suggestion(
@@ -1498,6 +1511,7 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
14981511
ident: Ident,
14991512
ns: Namespace,
15001513
filter_fn: FilterFn,
1514+
called: bool,
15011515
) -> Option<AssocSuggestion>
15021516
where
15031517
FilterFn: Fn(Res) -> bool,
@@ -1539,9 +1553,9 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
15391553
return Some(match &assoc_item.kind {
15401554
ast::AssocItemKind::Const(..) => AssocSuggestion::AssocConst,
15411555
ast::AssocItemKind::Fn(box ast::Fn { sig, .. }) if sig.decl.has_self() => {
1542-
AssocSuggestion::MethodWithSelf
1556+
AssocSuggestion::MethodWithSelf { called }
15431557
}
1544-
ast::AssocItemKind::Fn(..) => AssocSuggestion::AssocFn,
1558+
ast::AssocItemKind::Fn(..) => AssocSuggestion::AssocFn { called },
15451559
ast::AssocItemKind::Type(..) => AssocSuggestion::AssocType,
15461560
ast::AssocItemKind::MacCall(_) => continue,
15471561
});
@@ -1560,10 +1574,12 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
15601574
let res = binding.res();
15611575
if filter_fn(res) {
15621576
if self.r.has_self.contains(&res.def_id()) {
1563-
return Some(AssocSuggestion::MethodWithSelf);
1577+
return Some(AssocSuggestion::MethodWithSelf { called });
15641578
} else {
15651579
match res {
1566-
Res::Def(DefKind::AssocFn, _) => return Some(AssocSuggestion::AssocFn),
1580+
Res::Def(DefKind::AssocFn, _) => {
1581+
return Some(AssocSuggestion::AssocFn { called });
1582+
}
15671583
Res::Def(DefKind::AssocConst, _) => {
15681584
return Some(AssocSuggestion::AssocConst);
15691585
}

src/test/ui/resolve/issue-14254.stderr

+42-7
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,12 @@ error[E0425]: cannot find value `bah` in this scope
2626
--> $DIR/issue-14254.rs:36:9
2727
|
2828
LL | bah;
29-
| ^^^ help: you might have meant to call the associated function: `Self::bah`
29+
| ^^^
30+
|
31+
help: you might have meant to refer to the associated function
32+
|
33+
LL | Self::bah;
34+
| ~~~~~~~~~
3035

3136
error[E0425]: cannot find value `b` in this scope
3237
--> $DIR/issue-14254.rs:38:9
@@ -56,7 +61,12 @@ error[E0425]: cannot find value `bah` in this scope
5661
--> $DIR/issue-14254.rs:53:9
5762
|
5863
LL | bah;
59-
| ^^^ help: you might have meant to call the associated function: `Self::bah`
64+
| ^^^
65+
|
66+
help: you might have meant to refer to the associated function
67+
|
68+
LL | Self::bah;
69+
| ~~~~~~~~~
6070

6171
error[E0425]: cannot find value `b` in this scope
6272
--> $DIR/issue-14254.rs:55:9
@@ -68,31 +78,56 @@ error[E0425]: cannot find value `bah` in this scope
6878
--> $DIR/issue-14254.rs:64:9
6979
|
7080
LL | bah;
71-
| ^^^ help: you might have meant to call the associated function: `Self::bah`
81+
| ^^^
82+
|
83+
help: you might have meant to refer to the associated function
84+
|
85+
LL | Self::bah;
86+
| ~~~~~~~~~
7287

7388
error[E0425]: cannot find value `bah` in this scope
7489
--> $DIR/issue-14254.rs:73:9
7590
|
7691
LL | bah;
77-
| ^^^ help: you might have meant to call the associated function: `Self::bah`
92+
| ^^^
93+
|
94+
help: you might have meant to refer to the associated function
95+
|
96+
LL | Self::bah;
97+
| ~~~~~~~~~
7898

7999
error[E0425]: cannot find value `bah` in this scope
80100
--> $DIR/issue-14254.rs:82:9
81101
|
82102
LL | bah;
83-
| ^^^ help: you might have meant to call the associated function: `Self::bah`
103+
| ^^^
104+
|
105+
help: you might have meant to refer to the associated function
106+
|
107+
LL | Self::bah;
108+
| ~~~~~~~~~
84109

85110
error[E0425]: cannot find value `bah` in this scope
86111
--> $DIR/issue-14254.rs:91:9
87112
|
88113
LL | bah;
89-
| ^^^ help: you might have meant to call the associated function: `Self::bah`
114+
| ^^^
115+
|
116+
help: you might have meant to refer to the associated function
117+
|
118+
LL | Self::bah;
119+
| ~~~~~~~~~
90120

91121
error[E0425]: cannot find value `bah` in this scope
92122
--> $DIR/issue-14254.rs:100:9
93123
|
94124
LL | bah;
95-
| ^^^ help: you might have meant to call the associated function: `Self::bah`
125+
| ^^^
126+
|
127+
help: you might have meant to refer to the associated function
128+
|
129+
LL | Self::bah;
130+
| ~~~~~~~~~
96131

97132
error[E0425]: cannot find function `baz` in this scope
98133
--> $DIR/issue-14254.rs:19:9

src/test/ui/resolve/resolve-assoc-suggestions.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ error[E0425]: cannot find value `method` in this scope
5050
--> $DIR/resolve-assoc-suggestions.rs:34:9
5151
|
5252
LL | method;
53-
| ^^^^^^ help: you might have meant to call the method: `self.method`
53+
| ^^^^^^ help: you might have meant to refer to the method: `self.method`
5454

5555
error: aborting due to 9 previous errors
5656

src/test/ui/resolve/typo-suggestion-for-variable-with-name-similar-to-struct-field.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ LL | bah;
4040
LL | fn ba() {}
4141
| ------- similarly named function `ba` defined here
4242
|
43-
help: you might have meant to call the associated function
43+
help: you might have meant to refer to the associated function
4444
|
4545
LL | Self::bah;
4646
| ~~~~~~~~~

0 commit comments

Comments
 (0)