Skip to content

Commit e701ae3

Browse files
committed
Suggest correct place to add self parameter when inside closure
It would incorrectly suggest adding it as a parameter to the closure instead of the containing function.
1 parent 03687f8 commit e701ae3

File tree

5 files changed

+43
-6
lines changed

5 files changed

+43
-6
lines changed

compiler/rustc_resolve/src/late.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,7 @@ struct DiagnosticMetadata<'ast> {
369369
/// param.
370370
currently_processing_generics: bool,
371371

372-
/// The current enclosing function (used for better errors).
372+
/// The current enclosing (non-closure) function (used for better errors).
373373
current_function: Option<(FnKind<'ast>, Span)>,
374374

375375
/// A list of labels as of yet unused. Labels will be removed from this map when
@@ -515,8 +515,10 @@ impl<'a: 'ast, 'ast> Visitor<'ast> for LateResolutionVisitor<'a, '_, 'ast> {
515515
FnKind::Fn(FnCtxt::Assoc(_), ..) => NormalRibKind,
516516
FnKind::Closure(..) => ClosureOrAsyncRibKind,
517517
};
518-
let previous_value =
519-
replace(&mut self.diagnostic_metadata.current_function, Some((fn_kind, sp)));
518+
let previous_value = self.diagnostic_metadata.current_function;
519+
if matches!(fn_kind, FnKind::Fn(..)) {
520+
self.diagnostic_metadata.current_function = Some((fn_kind, sp));
521+
}
520522
debug!("(resolving function) entering function");
521523
let declaration = fn_kind.decl();
522524

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

+4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ impl Foo {
1010
fn baz(_: i32) {
1111
self.bar(); //~ ERROR E0424
1212
}
13+
14+
fn qux() {
15+
let _ = || self.bar(); //~ ERROR E0424
16+
}
1317
}
1418

1519
fn main () {

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

+15-2
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,27 @@ help: add a `self` receiver parameter to make the associated `fn` a method
2424
LL | fn baz(&self, _: i32) {
2525
| ^^^^^^
2626

27+
error[E0424]: expected value, found module `self`
28+
--> $DIR/E0424.rs:15:20
29+
|
30+
LL | fn qux() {
31+
| --- this function doesn't have a `self` parameter
32+
LL | let _ = || self.bar();
33+
| ^^^^ `self` value is a keyword only available in methods with a `self` parameter
34+
|
35+
help: add a `self` receiver parameter to make the associated `fn` a method
36+
|
37+
LL | fn qux(&self) {
38+
| ^^^^^
39+
2740
error[E0424]: expected unit struct, unit variant or constant, found module `self`
28-
--> $DIR/E0424.rs:16:9
41+
--> $DIR/E0424.rs:20:9
2942
|
3043
LL | fn main () {
3144
| ---- this function can't have a `self` parameter
3245
LL | let self = "self";
3346
| ^^^^ `self` value is a keyword and may not be bound to variables or shadowed
3447

35-
error: aborting due to 3 previous errors
48+
error: aborting due to 4 previous errors
3649

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

src/test/ui/issues/issue-5099.rs

+3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ trait B <A> {
55
fn b(x: i32) {
66
this.b(x); //~ ERROR cannot find value `this` in this scope
77
}
8+
fn c() {
9+
let _ = || this.a; //~ ERROR cannot find value `this` in this scope
10+
}
811
}
912

1013
fn main() {}

src/test/ui/issues/issue-5099.stderr

+16-1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,21 @@ help: if you meant to use `self`, you are also missing a `self` receiver argumen
2828
LL | fn b(&self, x: i32) {
2929
| ^^^^^^
3030

31-
error: aborting due to 2 previous errors
31+
error[E0425]: cannot find value `this` in this scope
32+
--> $DIR/issue-5099.rs:9:20
33+
|
34+
LL | let _ = || this.a;
35+
| ^^^^ not found in this scope
36+
|
37+
help: you might have meant to use `self` here instead
38+
|
39+
LL | let _ = || self.a;
40+
| ^^^^
41+
help: if you meant to use `self`, you are also missing a `self` receiver argument
42+
|
43+
LL | fn c(&self) {
44+
| ^^^^^
45+
46+
error: aborting due to 3 previous errors
3247

3348
For more information about this error, try `rustc --explain E0425`.

0 commit comments

Comments
 (0)