Skip to content

Commit 360388b

Browse files
committed
Suggest adding &self when accessing self in static assoc fn
1 parent 643258f commit 360388b

File tree

5 files changed

+98
-41
lines changed

5 files changed

+98
-41
lines changed

src/librustc_ast/visit.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,13 @@ impl<'a> FnKind<'a> {
5050
}
5151
}
5252

53+
pub fn ident(&self) -> Option<&Ident> {
54+
match self {
55+
FnKind::Fn(_, ident, ..) => Some(ident),
56+
_ => None,
57+
}
58+
}
59+
5360
pub fn decl(&self) -> &'a FnDecl {
5461
match self {
5562
FnKind::Fn(_, _, sig, _, _) => &sig.decl,

src/librustc_resolve/late/diagnostics.rs

Lines changed: 46 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -175,8 +175,9 @@ impl<'a> LateResolutionVisitor<'a, '_, '_> {
175175
let code = source.error_code(res.is_some());
176176
let mut err = self.r.session.struct_span_err_with_code(base_span, &base_msg, code);
177177

178+
let is_assoc_fn = self.self_type_is_available(span);
178179
// Emit help message for fake-self from other languages (e.g., `this` in Javascript).
179-
if ["this", "my"].contains(&&*item_str.as_str()) && self.self_type_is_available(span) {
180+
if ["this", "my"].contains(&&*item_str.as_str()) && is_assoc_fn {
180181
err.span_suggestion_short(
181182
span,
182183
"you might have meant to use `self` here instead",
@@ -187,25 +188,24 @@ impl<'a> LateResolutionVisitor<'a, '_, '_> {
187188
if let Some((FnKind::Fn(_, _, sig, ..), fn_span)) =
188189
&self.diagnostic_metadata.current_function
189190
{
190-
if let Some(param) = sig.decl.inputs.get(0) {
191-
err.span_suggestion_verbose(
192-
param.span.shrink_to_lo(),
193-
"you are also missing a `self` receiver argument",
194-
"&self, ".to_string(),
195-
Applicability::MaybeIncorrect,
196-
);
191+
let (span, sugg) = if let Some(param) = sig.decl.inputs.get(0) {
192+
(param.span.shrink_to_lo(), "&self, ")
197193
} else {
198-
err.span_suggestion_verbose(
194+
(
199195
self.r
200196
.session
201197
.source_map()
202198
.span_through_char(*fn_span, '(')
203199
.shrink_to_hi(),
204-
"you are also missing a `self` receiver argument",
205-
"&self".to_string(),
206-
Applicability::MaybeIncorrect,
207-
);
208-
}
200+
"&self",
201+
)
202+
};
203+
err.span_suggestion_verbose(
204+
span,
205+
"you are also missing a `self` receiver argument",
206+
sugg.to_string(),
207+
Applicability::MaybeIncorrect,
208+
);
209209
}
210210
}
211211
}
@@ -236,7 +236,38 @@ impl<'a> LateResolutionVisitor<'a, '_, '_> {
236236
if fn_kind.decl().inputs.get(0).map(|p| p.is_self()).unwrap_or(false) {
237237
err.span_label(*span, "this function has a `self` parameter, but a macro invocation can only access identifiers it receives from parameters");
238238
} else {
239-
err.span_label(*span, "this function doesn't have a `self` parameter");
239+
let doesnt = if is_assoc_fn {
240+
let (span, sugg) = fn_kind
241+
.decl()
242+
.inputs
243+
.get(0)
244+
.map(|p| (p.span.shrink_to_lo(), "&self, "))
245+
.unwrap_or_else(|| {
246+
(
247+
self.r
248+
.session
249+
.source_map()
250+
.span_through_char(*span, '(')
251+
.shrink_to_hi(),
252+
"&self",
253+
)
254+
});
255+
err.span_suggestion_verbose(
256+
span,
257+
"add a `self` receiver parameter to make the associated `fn` a method",
258+
sugg.to_string(),
259+
Applicability::MaybeIncorrect,
260+
);
261+
"doesn't"
262+
} else {
263+
"can't"
264+
};
265+
if let Some(ident) = fn_kind.ident() {
266+
err.span_label(
267+
ident.span,
268+
&format!("this function {} have a `self` parameter", doesnt),
269+
);
270+
}
240271
}
241272
}
242273
return (err, Vec::new());

src/test/ui/error-codes/E0424.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ impl Foo {
66
fn foo() {
77
self.bar(); //~ ERROR E0424
88
}
9+
10+
fn baz(_: i32) {
11+
self.bar(); //~ ERROR E0424
12+
}
913
}
1014

1115
fn main () {

src/test/ui/error-codes/E0424.stderr

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,37 @@
11
error[E0424]: expected value, found module `self`
22
--> $DIR/E0424.rs:7:9
33
|
4-
LL | / fn foo() {
5-
LL | | self.bar();
6-
| | ^^^^ `self` value is a keyword only available in methods with a `self` parameter
7-
LL | | }
8-
| |_____- this function doesn't have a `self` parameter
4+
LL | fn foo() {
5+
| --- this function doesn't have a `self` parameter
6+
LL | self.bar();
7+
| ^^^^ `self` value is a keyword only available in methods with a `self` parameter
8+
|
9+
help: add a `self` receiver parameter to make the associated `fn` a method
10+
|
11+
LL | fn foo(&self) {
12+
| ^^^^^
13+
14+
error[E0424]: expected value, found module `self`
15+
--> $DIR/E0424.rs:11:9
16+
|
17+
LL | fn baz(_: i32) {
18+
| --- this function doesn't have a `self` parameter
19+
LL | self.bar();
20+
| ^^^^ `self` value is a keyword only available in methods with a `self` parameter
21+
|
22+
help: add a `self` receiver parameter to make the associated `fn` a method
23+
|
24+
LL | fn baz(&self, _: i32) {
25+
| ^^^^^^
926

1027
error[E0424]: expected unit struct, unit variant or constant, found module `self`
11-
--> $DIR/E0424.rs:12:9
28+
--> $DIR/E0424.rs:16:9
1229
|
13-
LL | / fn main () {
14-
LL | | let self = "self";
15-
| | ^^^^ `self` value is a keyword and may not be bound to variables or shadowed
16-
LL | | }
17-
| |_- this function doesn't have a `self` parameter
30+
LL | fn main () {
31+
| ---- this function can't have a `self` parameter
32+
LL | let self = "self";
33+
| ^^^^ `self` value is a keyword and may not be bound to variables or shadowed
1834

19-
error: aborting due to 2 previous errors
35+
error: aborting due to 3 previous errors
2036

2137
For more information about this error, try `rustc --explain E0424`.

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

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -70,14 +70,15 @@ LL | purr();
7070
error[E0424]: expected value, found module `self`
7171
--> $DIR/issue-2356.rs:65:8
7272
|
73-
LL | / fn meow() {
74-
LL | | if self.whiskers > 3 {
75-
| | ^^^^ `self` value is a keyword only available in methods with a `self` parameter
76-
LL | |
77-
LL | | println!("MEOW");
78-
LL | | }
79-
LL | | }
80-
| |___- this function doesn't have a `self` parameter
73+
LL | fn meow() {
74+
| ---- this function doesn't have a `self` parameter
75+
LL | if self.whiskers > 3 {
76+
| ^^^^ `self` value is a keyword only available in methods with a `self` parameter
77+
|
78+
help: add a `self` receiver parameter to make the associated `fn` a method
79+
|
80+
LL | fn meow(&self) {
81+
| ^^^^^
8182

8283
error[E0425]: cannot find function `grow_older` in this scope
8384
--> $DIR/issue-2356.rs:72:5
@@ -112,12 +113,10 @@ LL | purr_louder();
112113
error[E0424]: expected value, found module `self`
113114
--> $DIR/issue-2356.rs:92:5
114115
|
115-
LL | / fn main() {
116-
LL | | self += 1;
117-
| | ^^^^ `self` value is a keyword only available in methods with a `self` parameter
118-
LL | |
119-
LL | | }
120-
| |_- this function doesn't have a `self` parameter
116+
LL | fn main() {
117+
| ---- this function can't have a `self` parameter
118+
LL | self += 1;
119+
| ^^^^ `self` value is a keyword only available in methods with a `self` parameter
121120

122121
error: aborting due to 17 previous errors
123122

0 commit comments

Comments
 (0)