Refer to "associated functions" instead of "static methods"
This commit is contained in:
parent
fa0f7d0080
commit
11011013f2
@ -1013,7 +1013,12 @@ fn h1() -> i32 {
|
||||
"##,
|
||||
|
||||
E0424: r##"
|
||||
The `self` keyword was used in a static method.
|
||||
The `self` keyword was used inside of an associated function instead of inside
|
||||
of a method. Associated functions have no "`self` receiver" argument, and are
|
||||
equivalent to regular functions which exist in the namespace of a trait.
|
||||
Methods, on the other hand, have a `self` reciver argument, like `self`,
|
||||
`&self`, `&mut self` or `self: &mut Pin<Self>` (this last one is an example of
|
||||
an ["abitrary `self` type"](https://github.com/rust-lang/rust/issues/44874)).
|
||||
|
||||
Erroneous code example:
|
||||
|
||||
@ -1021,25 +1026,26 @@ Erroneous code example:
|
||||
struct Foo;
|
||||
|
||||
impl Foo {
|
||||
fn bar(self) {}
|
||||
// `bar` is a method, because it has a receiver argument.
|
||||
fn bar(&self) {}
|
||||
|
||||
// `foo` is an associated function, because it has no receiver argument.
|
||||
fn foo() {
|
||||
self.bar(); // error: `self` is not available in a static method.
|
||||
self.bar(); // error: `self` is not available in an associated function
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Please check if the method's argument list should have contained `self`,
|
||||
`&self`, or `&mut self` (in case you didn't want to create a static
|
||||
method), and add it if so. Example:
|
||||
Check if the associated function's argument list should have contained a `self`
|
||||
receiver for it to be a method, and add it if so. Example:
|
||||
|
||||
```
|
||||
struct Foo;
|
||||
|
||||
impl Foo {
|
||||
fn bar(self) {}
|
||||
fn bar(&self) {}
|
||||
|
||||
fn foo(self) {
|
||||
fn foo(self) { // `foo` is now a method.
|
||||
self.bar(); // ok!
|
||||
}
|
||||
}
|
||||
|
@ -115,8 +115,10 @@ impl<'a> LateResolutionVisitor<'a, '_> {
|
||||
if is_self_type(path, ns) {
|
||||
syntax::diagnostic_used!(E0411);
|
||||
err.code(DiagnosticId::Error("E0411".into()));
|
||||
err.span_label(span, format!("`Self` is only available in impls, traits, \
|
||||
and type definitions"));
|
||||
err.span_label(
|
||||
span,
|
||||
format!("`Self` is only available in impls, traits, and type definitions"),
|
||||
);
|
||||
return (err, Vec::new());
|
||||
}
|
||||
if is_self_value(path, ns) {
|
||||
@ -125,16 +127,12 @@ impl<'a> LateResolutionVisitor<'a, '_> {
|
||||
syntax::diagnostic_used!(E0424);
|
||||
err.code(DiagnosticId::Error("E0424".into()));
|
||||
err.span_label(span, match source {
|
||||
PathSource::Pat => {
|
||||
format!("`self` value is a keyword \
|
||||
and may not be bound to \
|
||||
variables or shadowed")
|
||||
}
|
||||
_ => {
|
||||
format!("`self` value is a keyword \
|
||||
only available in methods \
|
||||
with `self` parameter")
|
||||
}
|
||||
PathSource::Pat => format!(
|
||||
"`self` value is a keyword and may not be bound to variables or shadowed",
|
||||
),
|
||||
_ => format!(
|
||||
"`self` value is a keyword only available in methods with a `self` parameter",
|
||||
),
|
||||
});
|
||||
return (err, Vec::new());
|
||||
}
|
||||
|
@ -174,14 +174,12 @@ fn cs_clone(name: &str,
|
||||
all_fields = af;
|
||||
vdata = &variant.data;
|
||||
}
|
||||
EnumNonMatchingCollapsed(..) => {
|
||||
cx.span_bug(trait_span,
|
||||
&format!("non-matching enum variants in \
|
||||
`derive({})`",
|
||||
name))
|
||||
}
|
||||
EnumNonMatchingCollapsed(..) => cx.span_bug(trait_span, &format!(
|
||||
"non-matching enum variants in `derive({})`",
|
||||
name,
|
||||
)),
|
||||
StaticEnum(..) | StaticStruct(..) => {
|
||||
cx.span_bug(trait_span, &format!("static method in `derive({})`", name))
|
||||
cx.span_bug(trait_span, &format!("associated function in `derive({})`", name))
|
||||
}
|
||||
}
|
||||
|
||||
@ -191,12 +189,10 @@ fn cs_clone(name: &str,
|
||||
.map(|field| {
|
||||
let ident = match field.name {
|
||||
Some(i) => i,
|
||||
None => {
|
||||
cx.span_bug(trait_span,
|
||||
&format!("unnamed field in normal struct in \
|
||||
`derive({})`",
|
||||
name))
|
||||
}
|
||||
None => cx.span_bug(trait_span, &format!(
|
||||
"unnamed field in normal struct in `derive({})`",
|
||||
name,
|
||||
)),
|
||||
};
|
||||
let call = subcall(cx, field);
|
||||
cx.field_imm(field.span, ident, call)
|
||||
|
@ -75,6 +75,6 @@ fn default_substructure(cx: &mut ExtCtxt<'_>,
|
||||
// let compilation continue
|
||||
DummyResult::raw_expr(trait_span, true)
|
||||
}
|
||||
_ => cx.span_bug(trait_span, "Non-static method in `derive(Default)`"),
|
||||
_ => cx.span_bug(trait_span, "method in `derive(Default)`"),
|
||||
};
|
||||
}
|
||||
|
@ -1055,9 +1055,7 @@ impl<'a> MethodDef<'a> {
|
||||
})
|
||||
.collect()
|
||||
} else {
|
||||
cx.span_bug(trait_.span,
|
||||
"no self arguments to non-static method in generic \
|
||||
`derive`")
|
||||
cx.span_bug(trait_.span, "no self arguments for method in generic `derive`")
|
||||
};
|
||||
|
||||
// body of the inner most destructuring match
|
||||
|
@ -2,7 +2,7 @@ error[E0424]: expected value, found module `self`
|
||||
--> $DIR/E0424.rs:7:9
|
||||
|
|
||||
LL | self.bar();
|
||||
| ^^^^ `self` value is a keyword only available in methods with `self` parameter
|
||||
| ^^^^ `self` value is a keyword only available in methods with a `self` parameter
|
||||
|
||||
error[E0424]: expected unit struct/variant or constant, found module `self`
|
||||
--> $DIR/E0424.rs:12:9
|
||||
|
@ -62,7 +62,7 @@ error[E0424]: expected value, found module `self`
|
||||
--> $DIR/issue-2356.rs:65:8
|
||||
|
|
||||
LL | if self.whiskers > 3 {
|
||||
| ^^^^ `self` value is a keyword only available in methods with `self` parameter
|
||||
| ^^^^ `self` value is a keyword only available in methods with a `self` parameter
|
||||
|
||||
error[E0425]: cannot find function `grow_older` in this scope
|
||||
--> $DIR/issue-2356.rs:72:5
|
||||
@ -98,7 +98,7 @@ error[E0424]: expected value, found module `self`
|
||||
--> $DIR/issue-2356.rs:92:5
|
||||
|
|
||||
LL | self += 1;
|
||||
| ^^^^ `self` value is a keyword only available in methods with `self` parameter
|
||||
| ^^^^ `self` value is a keyword only available in methods with a `self` parameter
|
||||
|
||||
error: aborting due to 17 previous errors
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user