Skip to content

Commit 1101101

Browse files
committed
Refer to "associated functions" instead of "static methods"
1 parent fa0f7d0 commit 1101101

File tree

7 files changed

+38
-40
lines changed

7 files changed

+38
-40
lines changed

src/librustc_resolve/error_codes.rs

+14-8
Original file line numberDiff line numberDiff line change
@@ -1013,33 +1013,39 @@ fn h1() -> i32 {
10131013
"##,
10141014

10151015
E0424: r##"
1016-
The `self` keyword was used in a static method.
1016+
The `self` keyword was used inside of an associated function instead of inside
1017+
of a method. Associated functions have no "`self` receiver" argument, and are
1018+
equivalent to regular functions which exist in the namespace of a trait.
1019+
Methods, on the other hand, have a `self` reciver argument, like `self`,
1020+
`&self`, `&mut self` or `self: &mut Pin<Self>` (this last one is an example of
1021+
an ["abitrary `self` type"](https://github.com/rust-lang/rust/issues/44874)).
10171022
10181023
Erroneous code example:
10191024
10201025
```compile_fail,E0424
10211026
struct Foo;
10221027
10231028
impl Foo {
1024-
fn bar(self) {}
1029+
// `bar` is a method, because it has a receiver argument.
1030+
fn bar(&self) {}
10251031
1032+
// `foo` is an associated function, because it has no receiver argument.
10261033
fn foo() {
1027-
self.bar(); // error: `self` is not available in a static method.
1034+
self.bar(); // error: `self` is not available in an associated function
10281035
}
10291036
}
10301037
```
10311038
1032-
Please check if the method's argument list should have contained `self`,
1033-
`&self`, or `&mut self` (in case you didn't want to create a static
1034-
method), and add it if so. Example:
1039+
Check if the associated function's argument list should have contained a `self`
1040+
receiver for it to be a method, and add it if so. Example:
10351041
10361042
```
10371043
struct Foo;
10381044
10391045
impl Foo {
1040-
fn bar(self) {}
1046+
fn bar(&self) {}
10411047
1042-
fn foo(self) {
1048+
fn foo(self) { // `foo` is now a method.
10431049
self.bar(); // ok!
10441050
}
10451051
}

src/librustc_resolve/late/diagnostics.rs

+10-12
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,10 @@ impl<'a> LateResolutionVisitor<'a, '_> {
115115
if is_self_type(path, ns) {
116116
syntax::diagnostic_used!(E0411);
117117
err.code(DiagnosticId::Error("E0411".into()));
118-
err.span_label(span, format!("`Self` is only available in impls, traits, \
119-
and type definitions"));
118+
err.span_label(
119+
span,
120+
format!("`Self` is only available in impls, traits, and type definitions"),
121+
);
120122
return (err, Vec::new());
121123
}
122124
if is_self_value(path, ns) {
@@ -125,16 +127,12 @@ impl<'a> LateResolutionVisitor<'a, '_> {
125127
syntax::diagnostic_used!(E0424);
126128
err.code(DiagnosticId::Error("E0424".into()));
127129
err.span_label(span, match source {
128-
PathSource::Pat => {
129-
format!("`self` value is a keyword \
130-
and may not be bound to \
131-
variables or shadowed")
132-
}
133-
_ => {
134-
format!("`self` value is a keyword \
135-
only available in methods \
136-
with `self` parameter")
137-
}
130+
PathSource::Pat => format!(
131+
"`self` value is a keyword and may not be bound to variables or shadowed",
132+
),
133+
_ => format!(
134+
"`self` value is a keyword only available in methods with a `self` parameter",
135+
),
138136
});
139137
return (err, Vec::new());
140138
}

src/libsyntax_ext/deriving/clone.rs

+9-13
Original file line numberDiff line numberDiff line change
@@ -174,14 +174,12 @@ fn cs_clone(name: &str,
174174
all_fields = af;
175175
vdata = &variant.data;
176176
}
177-
EnumNonMatchingCollapsed(..) => {
178-
cx.span_bug(trait_span,
179-
&format!("non-matching enum variants in \
180-
`derive({})`",
181-
name))
182-
}
177+
EnumNonMatchingCollapsed(..) => cx.span_bug(trait_span, &format!(
178+
"non-matching enum variants in `derive({})`",
179+
name,
180+
)),
183181
StaticEnum(..) | StaticStruct(..) => {
184-
cx.span_bug(trait_span, &format!("static method in `derive({})`", name))
182+
cx.span_bug(trait_span, &format!("associated function in `derive({})`", name))
185183
}
186184
}
187185

@@ -191,12 +189,10 @@ fn cs_clone(name: &str,
191189
.map(|field| {
192190
let ident = match field.name {
193191
Some(i) => i,
194-
None => {
195-
cx.span_bug(trait_span,
196-
&format!("unnamed field in normal struct in \
197-
`derive({})`",
198-
name))
199-
}
192+
None => cx.span_bug(trait_span, &format!(
193+
"unnamed field in normal struct in `derive({})`",
194+
name,
195+
)),
200196
};
201197
let call = subcall(cx, field);
202198
cx.field_imm(field.span, ident, call)

src/libsyntax_ext/deriving/default.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,6 @@ fn default_substructure(cx: &mut ExtCtxt<'_>,
7575
// let compilation continue
7676
DummyResult::raw_expr(trait_span, true)
7777
}
78-
_ => cx.span_bug(trait_span, "Non-static method in `derive(Default)`"),
78+
_ => cx.span_bug(trait_span, "method in `derive(Default)`"),
7979
};
8080
}

src/libsyntax_ext/deriving/generic/mod.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -1055,9 +1055,7 @@ impl<'a> MethodDef<'a> {
10551055
})
10561056
.collect()
10571057
} else {
1058-
cx.span_bug(trait_.span,
1059-
"no self arguments to non-static method in generic \
1060-
`derive`")
1058+
cx.span_bug(trait_.span, "no self arguments for method in generic `derive`")
10611059
};
10621060

10631061
// body of the inner most destructuring match

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0424]: expected value, found module `self`
22
--> $DIR/E0424.rs:7:9
33
|
44
LL | self.bar();
5-
| ^^^^ `self` value is a keyword only available in methods with `self` parameter
5+
| ^^^^ `self` value is a keyword only available in methods with a `self` parameter
66

77
error[E0424]: expected unit struct/variant or constant, found module `self`
88
--> $DIR/E0424.rs:12:9

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ error[E0424]: expected value, found module `self`
6262
--> $DIR/issue-2356.rs:65:8
6363
|
6464
LL | if self.whiskers > 3 {
65-
| ^^^^ `self` value is a keyword only available in methods with `self` parameter
65+
| ^^^^ `self` value is a keyword only available in methods with a `self` parameter
6666

6767
error[E0425]: cannot find function `grow_older` in this scope
6868
--> $DIR/issue-2356.rs:72:5
@@ -98,7 +98,7 @@ error[E0424]: expected value, found module `self`
9898
--> $DIR/issue-2356.rs:92:5
9999
|
100100
LL | self += 1;
101-
| ^^^^ `self` value is a keyword only available in methods with `self` parameter
101+
| ^^^^ `self` value is a keyword only available in methods with a `self` parameter
102102

103103
error: aborting due to 17 previous errors
104104

0 commit comments

Comments
 (0)