about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--compiler/rustc_parse/src/parser/diagnostics.rs16
-rw-r--r--tests/ui/parser/doc-before-bad-variant.rs6
-rw-r--r--tests/ui/parser/doc-before-bad-variant.stderr13
-rw-r--r--tests/ui/parser/doc-before-syntax-error.rs2
-rw-r--r--tests/ui/parser/doc-before-syntax-error.stderr8
5 files changed, 38 insertions, 7 deletions
diff --git a/compiler/rustc_parse/src/parser/diagnostics.rs b/compiler/rustc_parse/src/parser/diagnostics.rs
index 72aebb5d121..67abc2d5394 100644
--- a/compiler/rustc_parse/src/parser/diagnostics.rs
+++ b/compiler/rustc_parse/src/parser/diagnostics.rs
@@ -301,13 +301,6 @@ impl<'a> Parser<'a> {
         &mut self,
         recover: bool,
     ) -> PResult<'a, (Ident, IdentIsRaw)> {
-        if let TokenKind::DocComment(..) = self.prev_token.kind {
-            return Err(self.dcx().create_err(DocCommentDoesNotDocumentAnything {
-                span: self.prev_token.span,
-                missing_comma: None,
-            }));
-        }
-
         let valid_follow = &[
             TokenKind::Eq,
             TokenKind::Colon,
@@ -319,6 +312,15 @@ impl<'a> Parser<'a> {
             TokenKind::CloseDelim(Delimiter::Brace),
             TokenKind::CloseDelim(Delimiter::Parenthesis),
         ];
+        if let TokenKind::DocComment(..) = self.prev_token.kind
+            && valid_follow.contains(&self.token.kind)
+        {
+            let err = self.dcx().create_err(DocCommentDoesNotDocumentAnything {
+                span: self.prev_token.span,
+                missing_comma: None,
+            });
+            return Err(err);
+        }
 
         let mut recovered_ident = None;
         // we take this here so that the correct original token is retained in
diff --git a/tests/ui/parser/doc-before-bad-variant.rs b/tests/ui/parser/doc-before-bad-variant.rs
new file mode 100644
index 00000000000..bfede28c108
--- /dev/null
+++ b/tests/ui/parser/doc-before-bad-variant.rs
@@ -0,0 +1,6 @@
+enum TestEnum {
+    Works,
+    /// Some documentation
+    Self, //~ ERROR expected identifier, found keyword `Self`
+    //~^ HELP enum variants can be
+}
diff --git a/tests/ui/parser/doc-before-bad-variant.stderr b/tests/ui/parser/doc-before-bad-variant.stderr
new file mode 100644
index 00000000000..5e4d4116f25
--- /dev/null
+++ b/tests/ui/parser/doc-before-bad-variant.stderr
@@ -0,0 +1,13 @@
+error: expected identifier, found keyword `Self`
+  --> $DIR/doc-before-bad-variant.rs:4:5
+   |
+LL | enum TestEnum {
+   |      -------- while parsing this enum
+...
+LL |     Self,
+   |     ^^^^ expected identifier, found keyword
+   |
+   = help: enum variants can be `Variant`, `Variant = <integer>`, `Variant(Type, ..., TypeN)` or `Variant { fields: Types }`
+
+error: aborting due to 1 previous error
+
diff --git a/tests/ui/parser/doc-before-syntax-error.rs b/tests/ui/parser/doc-before-syntax-error.rs
new file mode 100644
index 00000000000..435f497d186
--- /dev/null
+++ b/tests/ui/parser/doc-before-syntax-error.rs
@@ -0,0 +1,2 @@
+/// Some documentation
+<> //~ ERROR expected identifier
diff --git a/tests/ui/parser/doc-before-syntax-error.stderr b/tests/ui/parser/doc-before-syntax-error.stderr
new file mode 100644
index 00000000000..93e39d63000
--- /dev/null
+++ b/tests/ui/parser/doc-before-syntax-error.stderr
@@ -0,0 +1,8 @@
+error: expected identifier, found `<`
+  --> $DIR/doc-before-syntax-error.rs:2:1
+   |
+LL | <>
+   | ^ expected identifier
+
+error: aborting due to 1 previous error
+