Skip to content

Fix ICE with inconsistent macro matchers #61046

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions src/libsyntax/ext/tt/transcribe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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, _) => {
Expand All @@ -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`
Expand Down Expand Up @@ -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)
Expand Down
9 changes: 9 additions & 0 deletions src/test/ui/macros/issue-61033-1.rs
Original file line number Diff line number Diff line change
@@ -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,);
}
8 changes: 8 additions & 0 deletions src/test/ui/macros/issue-61033-1.stderr
Original file line number Diff line number Diff line change
@@ -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

19 changes: 19 additions & 0 deletions src/test/ui/macros/issue-61033-2.rs
Original file line number Diff line number Diff line change
@@ -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
}
}
11 changes: 11 additions & 0 deletions src/test/ui/macros/issue-61033-2.stderr
Original file line number Diff line number Diff line change
@@ -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