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.
This commit is contained in:
parent
03687f8ffa
commit
e701ae376a
5 changed files with 43 additions and 6 deletions
|
@ -369,7 +369,7 @@ struct DiagnosticMetadata<'ast> {
|
||||||
/// param.
|
/// param.
|
||||||
currently_processing_generics: bool,
|
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)>,
|
current_function: Option<(FnKind<'ast>, Span)>,
|
||||||
|
|
||||||
/// A list of labels as of yet unused. Labels will be removed from this map when
|
/// 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> {
|
||||||
FnKind::Fn(FnCtxt::Assoc(_), ..) => NormalRibKind,
|
FnKind::Fn(FnCtxt::Assoc(_), ..) => NormalRibKind,
|
||||||
FnKind::Closure(..) => ClosureOrAsyncRibKind,
|
FnKind::Closure(..) => ClosureOrAsyncRibKind,
|
||||||
};
|
};
|
||||||
let previous_value =
|
let previous_value = self.diagnostic_metadata.current_function;
|
||||||
replace(&mut self.diagnostic_metadata.current_function, Some((fn_kind, sp)));
|
if matches!(fn_kind, FnKind::Fn(..)) {
|
||||||
|
self.diagnostic_metadata.current_function = Some((fn_kind, sp));
|
||||||
|
}
|
||||||
debug!("(resolving function) entering function");
|
debug!("(resolving function) entering function");
|
||||||
let declaration = fn_kind.decl();
|
let declaration = fn_kind.decl();
|
||||||
|
|
||||||
|
|
|
@ -10,6 +10,10 @@ impl Foo {
|
||||||
fn baz(_: i32) {
|
fn baz(_: i32) {
|
||||||
self.bar(); //~ ERROR E0424
|
self.bar(); //~ ERROR E0424
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn qux() {
|
||||||
|
let _ = || self.bar(); //~ ERROR E0424
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main () {
|
fn main () {
|
||||||
|
|
|
@ -24,14 +24,27 @@ help: add a `self` receiver parameter to make the associated `fn` a method
|
||||||
LL | fn baz(&self, _: i32) {
|
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`
|
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 () {
|
LL | fn main () {
|
||||||
| ---- this function can't have a `self` parameter
|
| ---- this function can't have a `self` parameter
|
||||||
LL | let self = "self";
|
LL | let self = "self";
|
||||||
| ^^^^ `self` value is a keyword and may not be bound to variables or shadowed
|
| ^^^^ `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`.
|
For more information about this error, try `rustc --explain E0424`.
|
||||||
|
|
|
@ -5,6 +5,9 @@ trait B <A> {
|
||||||
fn b(x: i32) {
|
fn b(x: i32) {
|
||||||
this.b(x); //~ ERROR cannot find value `this` in this scope
|
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() {}
|
fn main() {}
|
||||||
|
|
|
@ -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) {
|
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`.
|
For more information about this error, try `rustc --explain E0425`.
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue