Skip to content

Commit 1ea0b1d

Browse files
authored
Rollup merge of rust-lang#61046 - mark-i-m:transcribe-fix, r=petrochenkov
Fix ICE with inconsistent macro matchers Fixes rust-lang#61033 r? @petrochenkov
2 parents 25c1dca + 5a9de55 commit 1ea0b1d

File tree

5 files changed

+57
-8
lines changed

5 files changed

+57
-8
lines changed

src/libsyntax/ext/tt/transcribe.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -170,9 +170,11 @@ pub fn transcribe(
170170
}
171171

172172
LockstepIterSize::Contradiction(ref msg) => {
173-
// This should never happen because the macro parser should generate
174-
// properly-sized matches for all meta-vars.
175-
cx.span_bug(seq.span(), &msg[..]);
173+
// FIXME: this really ought to be caught at macro definition time... It
174+
// happens when two meta-variables are used in the same repetition in a
175+
// sequence, but they come from different sequence matchers and repeat
176+
// different amounts.
177+
cx.span_fatal(seq.span(), &msg[..]);
176178
}
177179

178180
LockstepIterSize::Constraint(len, _) => {
@@ -187,9 +189,10 @@ pub fn transcribe(
187189
// Is the repetition empty?
188190
if len == 0 {
189191
if seq.op == quoted::KleeneOp::OneOrMore {
190-
// This should be impossible because the macro parser would not
191-
// match the given macro arm.
192-
cx.span_bug(sp.entire(), "this must repeat at least once");
192+
// FIXME: this really ought to be caught at macro definition
193+
// time... It happens when the Kleene operator in the matcher and
194+
// the body for the same meta-variable do not match.
195+
cx.span_fatal(sp.entire(), "this must repeat at least once");
193196
}
194197
} else {
195198
// 0 is the initial counter (we have done 0 repretitions so far). `len`
@@ -327,8 +330,7 @@ impl LockstepIterSize {
327330
LockstepIterSize::Constraint(r_len, _) if l_len == r_len => self,
328331
LockstepIterSize::Constraint(r_len, r_id) => {
329332
let msg = format!(
330-
"inconsistent lockstep iteration: \
331-
'{}' has {} items, but '{}' has {}",
333+
"meta-variable `{}` repeats {} times, but `{}` repeats {} times",
332334
l_id, l_len, r_id, r_len
333335
);
334336
LockstepIterSize::Contradiction(msg)

src/test/ui/macros/issue-61033-1.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// Regression test for issue #61033.
2+
3+
macro_rules! test1 {
4+
($x:ident, $($tt:tt)*) => { $($tt)+ } //~ERROR this must repeat at least once
5+
}
6+
7+
fn main() {
8+
test1!(x,);
9+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
error: this must repeat at least once
2+
--> $DIR/issue-61033-1.rs:4:34
3+
|
4+
LL | ($x:ident, $($tt:tt)*) => { $($tt)+ }
5+
| ^^^^^
6+
7+
error: aborting due to previous error
8+

src/test/ui/macros/issue-61033-2.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Regression test for issue #61033.
2+
3+
macro_rules! test2 {
4+
(
5+
$(* $id1:ident)*
6+
$(+ $id2:ident)*
7+
) => {
8+
$( //~ERROR meta-variable `id1` repeats 2 times
9+
$id1 + $id2 // $id1 and $id2 may repeat different numbers of times
10+
)*
11+
}
12+
}
13+
14+
fn main() {
15+
test2! {
16+
* a * b
17+
+ a + b + c
18+
}
19+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
error: meta-variable `id1` repeats 2 times, but `id2` repeats 3 times
2+
--> $DIR/issue-61033-2.rs:8:10
3+
|
4+
LL | $(
5+
| __________^
6+
LL | | $id1 + $id2 // $id1 and $id2 may repeat different numbers of times
7+
LL | | )*
8+
| |_________^
9+
10+
error: aborting due to previous error
11+

0 commit comments

Comments
 (0)