about summary refs log tree commit diff
diff options
context:
space:
mode:
authorNixon Enraght-Moony <nixon.emoony@gmail.com>2021-09-24 13:31:01 +0100
committerNixon Enraght-Moony <nixon.emoony@gmail.com>2021-09-24 16:21:34 +0100
commited3b751799b0d9a754846bd754d9b43cf6c7394b (patch)
treeac71de97235709aee4a4fad776ef12b2a429c6e6
parent15d9ba0133ce0b35348e1c8367afe00aec841ffa (diff)
downloadrust-ed3b751799b0d9a754846bd754d9b43cf6c7394b.tar.gz
rust-ed3b751799b0d9a754846bd754d9b43cf6c7394b.zip
Give better error for `macro_rules! name!`
-rw-r--r--compiler/rustc_parse/src/parser/item.rs14
-rw-r--r--src/test/ui/macros/bang-after-name.fixed8
-rw-r--r--src/test/ui/macros/bang-after-name.rs8
-rw-r--r--src/test/ui/macros/bang-after-name.stderr8
4 files changed, 38 insertions, 0 deletions
diff --git a/compiler/rustc_parse/src/parser/item.rs b/compiler/rustc_parse/src/parser/item.rs
index 0e2222bf840..624390a406f 100644
--- a/compiler/rustc_parse/src/parser/item.rs
+++ b/compiler/rustc_parse/src/parser/item.rs
@@ -1547,6 +1547,20 @@ impl<'a> Parser<'a> {
         self.expect(&token::Not)?; // `!`
 
         let ident = self.parse_ident()?;
+
+        if self.eat(&token::Not) {
+            // Handle macro_rules! foo!
+            let span = self.prev_token.span;
+            self.struct_span_err(span, "macro names aren't followed by a `!`")
+                .span_suggestion(
+                    span,
+                    "remove the `!`",
+                    "".to_owned(),
+                    Applicability::MachineApplicable,
+                )
+                .emit();
+        }
+
         let body = self.parse_mac_args()?;
         self.eat_semi_for_macro_if_needed(&body);
         self.complain_if_pub_macro(vis, true);
diff --git a/src/test/ui/macros/bang-after-name.fixed b/src/test/ui/macros/bang-after-name.fixed
new file mode 100644
index 00000000000..c107ddd5d03
--- /dev/null
+++ b/src/test/ui/macros/bang-after-name.fixed
@@ -0,0 +1,8 @@
+// run-rustfix
+#[allow(unused_macros)]
+
+macro_rules! foo { //~ ERROR macro names aren't followed by a `!`
+    () => {};
+}
+
+fn main() {}
diff --git a/src/test/ui/macros/bang-after-name.rs b/src/test/ui/macros/bang-after-name.rs
new file mode 100644
index 00000000000..7654d8c4403
--- /dev/null
+++ b/src/test/ui/macros/bang-after-name.rs
@@ -0,0 +1,8 @@
+// run-rustfix
+#[allow(unused_macros)]
+
+macro_rules! foo! { //~ ERROR macro names aren't followed by a `!`
+    () => {};
+}
+
+fn main() {}
diff --git a/src/test/ui/macros/bang-after-name.stderr b/src/test/ui/macros/bang-after-name.stderr
new file mode 100644
index 00000000000..f609c4943ef
--- /dev/null
+++ b/src/test/ui/macros/bang-after-name.stderr
@@ -0,0 +1,8 @@
+error: macro names aren't followed by a `!`
+  --> $DIR/bang-after-name.rs:4:17
+   |
+LL | macro_rules! foo! {
+   |                 ^ help: remove the `!`
+
+error: aborting due to previous error
+