about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMazdak Farrokhzad <twingoow@gmail.com>2019-05-23 08:37:15 +0200
committerGitHub <noreply@github.com>2019-05-23 08:37:15 +0200
commit1ea0b1d2745ee3f643d24f028a038ace65722942 (patch)
tree90c1f096232a9d6c4f0173653445f1193e164ef4
parent25c1dca1b3ad1b12068533a6701323f022a02fb2 (diff)
parent5a9de557b3d0ee9314bf7720c9d631940082eb6c (diff)
downloadrust-1ea0b1d2745ee3f643d24f028a038ace65722942.tar.gz
rust-1ea0b1d2745ee3f643d24f028a038ace65722942.zip
Rollup merge of #61046 - mark-i-m:transcribe-fix, r=petrochenkov
Fix ICE with inconsistent macro matchers

Fixes #61033

r? @petrochenkov
-rw-r--r--src/libsyntax/ext/tt/transcribe.rs18
-rw-r--r--src/test/ui/macros/issue-61033-1.rs9
-rw-r--r--src/test/ui/macros/issue-61033-1.stderr8
-rw-r--r--src/test/ui/macros/issue-61033-2.rs19
-rw-r--r--src/test/ui/macros/issue-61033-2.stderr11
5 files changed, 57 insertions, 8 deletions
diff --git a/src/libsyntax/ext/tt/transcribe.rs b/src/libsyntax/ext/tt/transcribe.rs
index e3586c1854c..e6b49e61937 100644
--- a/src/libsyntax/ext/tt/transcribe.rs
+++ b/src/libsyntax/ext/tt/transcribe.rs
@@ -170,9 +170,11 @@ pub fn transcribe(
                     }
 
                     LockstepIterSize::Contradiction(ref msg) => {
-                        // This should never happen because the macro parser should generate
-                        // properly-sized matches for all meta-vars.
-                        cx.span_bug(seq.span(), &msg[..]);
+                        // FIXME: this really ought to be caught at macro definition time... It
+                        // happens when two meta-variables are used in the same repetition in a
+                        // sequence, but they come from different sequence matchers and repeat
+                        // different amounts.
+                        cx.span_fatal(seq.span(), &msg[..]);
                     }
 
                     LockstepIterSize::Constraint(len, _) => {
@@ -187,9 +189,10 @@ pub fn transcribe(
                         // Is the repetition empty?
                         if len == 0 {
                             if seq.op == quoted::KleeneOp::OneOrMore {
-                                // This should be impossible because the macro parser would not
-                                // match the given macro arm.
-                                cx.span_bug(sp.entire(), "this must repeat at least once");
+                                // FIXME: this really ought to be caught at macro definition
+                                // time... It happens when the Kleene operator in the matcher and
+                                // the body for the same meta-variable do not match.
+                                cx.span_fatal(sp.entire(), "this must repeat at least once");
                             }
                         } else {
                             // 0 is the initial counter (we have done 0 repretitions so far). `len`
@@ -327,8 +330,7 @@ impl LockstepIterSize {
                 LockstepIterSize::Constraint(r_len, _) if l_len == r_len => self,
                 LockstepIterSize::Constraint(r_len, r_id) => {
                     let msg = format!(
-                        "inconsistent lockstep iteration: \
-                         '{}' has {} items, but '{}' has {}",
+                        "meta-variable `{}` repeats {} times, but `{}` repeats {} times",
                         l_id, l_len, r_id, r_len
                     );
                     LockstepIterSize::Contradiction(msg)
diff --git a/src/test/ui/macros/issue-61033-1.rs b/src/test/ui/macros/issue-61033-1.rs
new file mode 100644
index 00000000000..8f85dec017f
--- /dev/null
+++ b/src/test/ui/macros/issue-61033-1.rs
@@ -0,0 +1,9 @@
+// Regression test for issue #61033.
+
+macro_rules! test1 {
+    ($x:ident, $($tt:tt)*) => { $($tt)+ } //~ERROR this must repeat at least once
+}
+
+fn main() {
+    test1!(x,);
+}
diff --git a/src/test/ui/macros/issue-61033-1.stderr b/src/test/ui/macros/issue-61033-1.stderr
new file mode 100644
index 00000000000..f3c68f4928d
--- /dev/null
+++ b/src/test/ui/macros/issue-61033-1.stderr
@@ -0,0 +1,8 @@
+error: this must repeat at least once
+  --> $DIR/issue-61033-1.rs:4:34
+   |
+LL |     ($x:ident, $($tt:tt)*) => { $($tt)+ }
+   |                                  ^^^^^
+
+error: aborting due to previous error
+
diff --git a/src/test/ui/macros/issue-61033-2.rs b/src/test/ui/macros/issue-61033-2.rs
new file mode 100644
index 00000000000..0799be10b96
--- /dev/null
+++ b/src/test/ui/macros/issue-61033-2.rs
@@ -0,0 +1,19 @@
+// Regression test for issue #61033.
+
+macro_rules! test2 {
+    (
+        $(* $id1:ident)*
+        $(+ $id2:ident)*
+    ) => {
+        $( //~ERROR meta-variable `id1` repeats 2 times
+            $id1 + $id2 // $id1 and $id2 may repeat different numbers of times
+        )*
+    }
+}
+
+fn main() {
+    test2! {
+        * a * b
+        + a + b + c
+    }
+}
diff --git a/src/test/ui/macros/issue-61033-2.stderr b/src/test/ui/macros/issue-61033-2.stderr
new file mode 100644
index 00000000000..bf502919cf7
--- /dev/null
+++ b/src/test/ui/macros/issue-61033-2.stderr
@@ -0,0 +1,11 @@
+error: meta-variable `id1` repeats 2 times, but `id2` repeats 3 times
+  --> $DIR/issue-61033-2.rs:8:10
+   |
+LL |           $(
+   |  __________^
+LL | |             $id1 + $id2 // $id1 and $id2 may repeat different numbers of times
+LL | |         )*
+   | |_________^
+
+error: aborting due to previous error
+