diff options
| author | Hirochika Matsumoto <git@hkmatsumoto.com> | 2021-10-19 01:31:23 +0900 |
|---|---|---|
| committer | Hirochika Matsumoto <git@hkmatsumoto.com> | 2021-10-19 23:00:18 +0900 |
| commit | a72dd4a5b973a3d94876ebea66e428fabaff00b0 (patch) | |
| tree | c62059ef97025d1c745900bf3c6db0c610183e89 /compiler/rustc_parse/src/parser | |
| parent | d45ed7502ad225739270a368528725930f54b7b6 (diff) | |
| download | rust-a72dd4a5b973a3d94876ebea66e428fabaff00b0.tar.gz rust-a72dd4a5b973a3d94876ebea66e428fabaff00b0.zip | |
Explain why `Self` is invalid in generic parameters
Diffstat (limited to 'compiler/rustc_parse/src/parser')
| -rw-r--r-- | compiler/rustc_parse/src/parser/generics.rs | 13 |
1 files changed, 13 insertions, 0 deletions
diff --git a/compiler/rustc_parse/src/parser/generics.rs b/compiler/rustc_parse/src/parser/generics.rs index f175c5b50b3..ec7e3766566 100644 --- a/compiler/rustc_parse/src/parser/generics.rs +++ b/compiler/rustc_parse/src/parser/generics.rs @@ -89,6 +89,19 @@ impl<'a> Parser<'a> { let attrs = self.parse_outer_attributes()?; let param = self.collect_tokens_trailing_token(attrs, ForceCollect::No, |this, attrs| { + if this.eat_keyword_noexpect(kw::SelfUpper) { + // `Self` as a generic param is invalid. Here we emit the diagnostic and continue parsing + // as if `Self` never existed. + this.struct_span_err( + this.prev_token.span, + "unexpected keyword `Self` in generic parameters", + ) + .note("you cannot use `Self` as a generic parameter because it is reserved for associated items") + .emit(); + + this.eat(&token::Comma); + } + let param = if this.check_lifetime() { let lifetime = this.expect_lifetime(); // Parse lifetime parameter. |
