Skip to content

Suggest correct place to add self parameter when inside closure #78048

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

Merged
merged 1 commit into from
Oct 17, 2020
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
8 changes: 5 additions & 3 deletions compiler/rustc_resolve/src/late.rs
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ struct DiagnosticMetadata<'ast> {
/// param.
currently_processing_generics: bool,

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

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

Expand Down
4 changes: 4 additions & 0 deletions src/test/ui/error-codes/E0424.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ impl Foo {
fn baz(_: i32) {
self.bar(); //~ ERROR E0424
}

fn qux() {
let _ = || self.bar(); //~ ERROR E0424
}
}

fn main () {
Expand Down
17 changes: 15 additions & 2 deletions src/test/ui/error-codes/E0424.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,27 @@ help: add a `self` receiver parameter to make the associated `fn` a method
LL | fn baz(&self, _: i32) {
| ^^^^^^

error[E0424]: expected value, found module `self`
--> $DIR/E0424.rs:15:20
|
LL | fn qux() {
| --- this function doesn't have a `self` parameter
LL | let _ = || self.bar();
| ^^^^ `self` value is a keyword only available in methods with a `self` parameter
|
help: add a `self` receiver parameter to make the associated `fn` a method
|
LL | fn qux(&self) {
| ^^^^^

error[E0424]: expected unit struct, unit variant or constant, found module `self`
--> $DIR/E0424.rs:16:9
--> $DIR/E0424.rs:20:9
|
LL | fn main () {
| ---- this function can't have a `self` parameter
LL | let self = "self";
| ^^^^ `self` value is a keyword and may not be bound to variables or shadowed

error: aborting due to 3 previous errors
error: aborting due to 4 previous errors

For more information about this error, try `rustc --explain E0424`.
3 changes: 3 additions & 0 deletions src/test/ui/issues/issue-5099.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ trait B <A> {
fn b(x: i32) {
this.b(x); //~ ERROR cannot find value `this` in this scope
}
fn c() {
let _ = || this.a; //~ ERROR cannot find value `this` in this scope
}
}

fn main() {}
17 changes: 16 additions & 1 deletion src/test/ui/issues/issue-5099.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,21 @@ help: if you meant to use `self`, you are also missing a `self` receiver argumen
LL | fn b(&self, x: i32) {
| ^^^^^^

error: aborting due to 2 previous errors
error[E0425]: cannot find value `this` in this scope
--> $DIR/issue-5099.rs:9:20
|
LL | let _ = || this.a;
| ^^^^ not found in this scope
|
help: you might have meant to use `self` here instead
|
LL | let _ = || self.a;
| ^^^^
help: if you meant to use `self`, you are also missing a `self` receiver argument
|
LL | fn c(&self) {
| ^^^^^

error: aborting due to 3 previous errors

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