about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--compiler/rustc_parse/src/parser/item.rs8
-rw-r--r--src/test/ui/parser/struct-filed-with-attr.fixed18
-rw-r--r--src/test/ui/parser/struct-filed-with-attr.rs18
-rw-r--r--src/test/ui/parser/struct-filed-with-attr.stderr8
4 files changed, 50 insertions, 2 deletions
diff --git a/compiler/rustc_parse/src/parser/item.rs b/compiler/rustc_parse/src/parser/item.rs
index 72c23776d33..e8c097d0d56 100644
--- a/compiler/rustc_parse/src/parser/item.rs
+++ b/compiler/rustc_parse/src/parser/item.rs
@@ -1539,8 +1539,12 @@ impl<'a> Parser<'a> {
                     }
                 }
 
-                if self.token.is_ident() {
-                    // This is likely another field; emit the diagnostic and keep going
+                if self.token.is_ident()
+                    || (self.token.kind == TokenKind::Pound
+                        && (self.look_ahead(1, |t| t == &token::OpenDelim(Delimiter::Bracket))))
+                {
+                    // This is likely another field, TokenKind::Pound is used for `#[..]` attribute for next field,
+                    // emit the diagnostic and keep going
                     err.span_suggestion(
                         sp,
                         "try adding a comma",
diff --git a/src/test/ui/parser/struct-filed-with-attr.fixed b/src/test/ui/parser/struct-filed-with-attr.fixed
new file mode 100644
index 00000000000..a799ec8ca2e
--- /dev/null
+++ b/src/test/ui/parser/struct-filed-with-attr.fixed
@@ -0,0 +1,18 @@
+// Issue: 100461, Try to give a helpful diagnostic even when the next struct field has an attribute.
+// run-rustfix
+
+struct Feelings {
+    owo: bool,
+    //~^ ERROR expected `,`, or `}`, found `#`
+    #[allow(unused)]
+    uwu: bool,
+}
+
+impl Feelings {
+    #[allow(unused)]
+    fn hmm(&self) -> bool {
+        self.owo
+    }
+}
+
+fn main() { }
diff --git a/src/test/ui/parser/struct-filed-with-attr.rs b/src/test/ui/parser/struct-filed-with-attr.rs
new file mode 100644
index 00000000000..bfc78e15b5b
--- /dev/null
+++ b/src/test/ui/parser/struct-filed-with-attr.rs
@@ -0,0 +1,18 @@
+// Issue: 100461, Try to give a helpful diagnostic even when the next struct field has an attribute.
+// run-rustfix
+
+struct Feelings {
+    owo: bool
+    //~^ ERROR expected `,`, or `}`, found `#`
+    #[allow(unused)]
+    uwu: bool,
+}
+
+impl Feelings {
+    #[allow(unused)]
+    fn hmm(&self) -> bool {
+        self.owo
+    }
+}
+
+fn main() { }
diff --git a/src/test/ui/parser/struct-filed-with-attr.stderr b/src/test/ui/parser/struct-filed-with-attr.stderr
new file mode 100644
index 00000000000..c2cd7e82ead
--- /dev/null
+++ b/src/test/ui/parser/struct-filed-with-attr.stderr
@@ -0,0 +1,8 @@
+error: expected `,`, or `}`, found `#`
+  --> $DIR/struct-filed-with-attr.rs:5:14
+   |
+LL |     owo: bool
+   |              ^ help: try adding a comma: `,`
+
+error: aborting due to previous error
+