Skip to content

Commit a72dd4a

Browse files
committedOct 19, 2021
Explain why Self is invalid in generic parameters
1 parent d45ed75 commit a72dd4a

File tree

3 files changed

+35
-13
lines changed

3 files changed

+35
-13
lines changed
 

‎compiler/rustc_parse/src/parser/generics.rs

+13
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,19 @@ impl<'a> Parser<'a> {
8989
let attrs = self.parse_outer_attributes()?;
9090
let param =
9191
self.collect_tokens_trailing_token(attrs, ForceCollect::No, |this, attrs| {
92+
if this.eat_keyword_noexpect(kw::SelfUpper) {
93+
// `Self` as a generic param is invalid. Here we emit the diagnostic and continue parsing
94+
// as if `Self` never existed.
95+
this.struct_span_err(
96+
this.prev_token.span,
97+
"unexpected keyword `Self` in generic parameters",
98+
)
99+
.note("you cannot use `Self` as a generic parameter because it is reserved for associated items")
100+
.emit();
101+
102+
this.eat(&token::Comma);
103+
}
104+
92105
let param = if this.check_lifetime() {
93106
let lifetime = this.expect_lifetime();
94107
// Parse lifetime parameter.
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
// Regression test of #36638.
22

33
struct Foo<Self>(Self);
4-
//~^ ERROR expected identifier, found keyword `Self`
5-
//~^^ ERROR E0392
4+
//~^ ERROR unexpected keyword `Self` in generic parameters
5+
//~| ERROR recursive type `Foo` has infinite size
66

77
trait Bar<Self> {}
8-
//~^ ERROR expected identifier, found keyword `Self`
8+
//~^ ERROR unexpected keyword `Self` in generic parameters
99

1010
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,33 @@
1-
error: expected identifier, found keyword `Self`
1+
error: unexpected keyword `Self` in generic parameters
22
--> $DIR/keyword-self-as-type-param.rs:3:12
33
|
44
LL | struct Foo<Self>(Self);
5-
| ^^^^ expected identifier, found keyword
5+
| ^^^^
6+
|
7+
= note: you cannot use `Self` as a generic parameter because it is reserved for associated items
68

7-
error: expected identifier, found keyword `Self`
9+
error: unexpected keyword `Self` in generic parameters
810
--> $DIR/keyword-self-as-type-param.rs:7:11
911
|
1012
LL | trait Bar<Self> {}
11-
| ^^^^ expected identifier, found keyword
13+
| ^^^^
14+
|
15+
= note: you cannot use `Self` as a generic parameter because it is reserved for associated items
1216

13-
error[E0392]: parameter `Self` is never used
14-
--> $DIR/keyword-self-as-type-param.rs:3:12
17+
error[E0072]: recursive type `Foo` has infinite size
18+
--> $DIR/keyword-self-as-type-param.rs:3:1
1519
|
1620
LL | struct Foo<Self>(Self);
17-
| ^^^^ unused parameter
21+
| ^^^^^^^^^^^^^^^^^----^^
22+
| | |
23+
| | recursive without indirection
24+
| recursive type has infinite size
25+
|
26+
help: insert some indirection (e.g., a `Box`, `Rc`, or `&`) to make `Foo` representable
1827
|
19-
= help: consider removing `Self`, referring to it in a field, or using a marker such as `PhantomData`
20-
= help: if you intended `Self` to be a const parameter, use `const Self: usize` instead
28+
LL | struct Foo<Self>(Box<Self>);
29+
| ++++ +
2130

2231
error: aborting due to 3 previous errors
2332

24-
For more information about this error, try `rustc --explain E0392`.
33+
For more information about this error, try `rustc --explain E0072`.

0 commit comments

Comments
 (0)
Please sign in to comment.