Skip to content

Commit 29fe57d

Browse files
authored
Rollup merge of #90022 - hkmatsumoto:self-upper-as-generic-parameter, r=jackh726
Explain why `Self` is invalid in generic parameters Close #89985. r? `@estebank`
2 parents b97f375 + a72dd4a commit 29fe57d

File tree

3 files changed

+35
-13
lines changed

3 files changed

+35
-13
lines changed

compiler/rustc_parse/src/parser/generics.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,19 @@ impl<'a> Parser<'a> {
9292
let attrs = self.parse_outer_attributes()?;
9393
let param =
9494
self.collect_tokens_trailing_token(attrs, ForceCollect::No, |this, attrs| {
95+
if this.eat_keyword_noexpect(kw::SelfUpper) {
96+
// `Self` as a generic param is invalid. Here we emit the diagnostic and continue parsing
97+
// as if `Self` never existed.
98+
this.struct_span_err(
99+
this.prev_token.span,
100+
"unexpected keyword `Self` in generic parameters",
101+
)
102+
.note("you cannot use `Self` as a generic parameter because it is reserved for associated items")
103+
.emit();
104+
105+
this.eat(&token::Comma);
106+
}
107+
95108
let param = if this.check_lifetime() {
96109
let lifetime = this.expect_lifetime();
97110
// Parse lifetime parameter.
Lines changed: 3 additions & 3 deletions
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() {}
Lines changed: 19 additions & 10 deletions
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)