Skip to content

Commit a4b9a03

Browse files
committed
Fix inaccurate comments in '?' Kleene operator tests.
1 parent f86719a commit a4b9a03

6 files changed

+87
-51
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,50 @@
11
// run-pass
2+
23
#![allow(unused_mut)]
3-
// The logic for parsing Kleene operators in macros has a special case to disambiguate `?`.
4-
// Specifically, `$(pat)?` is the ZeroOrOne operator whereas `$(pat)?+` or `$(pat)?*` are the
5-
// ZeroOrMore and OneOrMore operators using `?` as a separator. These tests are intended to
6-
// exercise that logic in the macro parser.
7-
//
8-
// Moreover, we also throw in some tests for using a separator with `?`, which is meaningless but
9-
// included for consistency with `+` and `*`.
10-
//
11-
// This test focuses on non-error cases and making sure the correct number of repetitions happen.
4+
5+
// Check that when `?` is followed by what looks like a Kleene operator (?, +, and *)
6+
// then that `?` is not interpreted as a separator. In other words, `$(pat)?+` matches `pat +`
7+
// or `+` but does not match `pat` or `pat ? pat`.
128

139
// edition:2015
1410

1511
macro_rules! foo {
16-
($($a:ident)? ; $num:expr) => { {
12+
// Check for `?`.
13+
($($a:ident)? ? $num:expr) => {
14+
foo!($($a)? ; $num);
15+
};
16+
// Check for `+`.
17+
($($a:ident)? + $num:expr) => {
18+
foo!($($a)? ; $num);
19+
};
20+
// Check for `*`.
21+
($($a:ident)? * $num:expr) => {
22+
foo!($($a)? ; $num);
23+
};
24+
// Check for `;`, not a kleene operator.
25+
($($a:ident)? ; $num:expr) => {
1726
let mut x = 0;
1827

1928
$(
2029
x += $a;
21-
)?
30+
)?
2231

2332
assert_eq!(x, $num);
24-
} }
33+
};
2534
}
2635

2736
pub fn main() {
2837
let a = 1;
2938

30-
// accept 0 or 1 repetitions
39+
// Accept 0 repetitions.
3140
foo!( ; 0);
41+
foo!( + 0);
42+
foo!( * 0);
43+
foo!( ? 0);
44+
45+
// Accept 1 repetition.
3246
foo!(a ; 1);
47+
foo!(a + 1);
48+
foo!(a * 1);
49+
foo!(a ? 1);
3350
}
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,50 @@
11
// run-pass
2+
23
#![allow(unused_mut)]
3-
// The logic for parsing Kleene operators in macros has a special case to disambiguate `?`.
4-
// Specifically, `$(pat)?` is the ZeroOrOne operator whereas `$(pat)?+` or `$(pat)?*` are the
5-
// ZeroOrMore and OneOrMore operators using `?` as a separator. These tests are intended to
6-
// exercise that logic in the macro parser.
7-
//
8-
// Moreover, we also throw in some tests for using a separator with `?`, which is meaningless but
9-
// included for consistency with `+` and `*`.
10-
//
11-
// This test focuses on non-error cases and making sure the correct number of repetitions happen.
4+
5+
// Check that when `?` is followed by what looks like a Kleene operator (?, +, and *)
6+
// then that `?` is not interpreted as a separator. In other words, `$(pat)?+` matches `pat +`
7+
// or `+` but does not match `pat` or `pat ? pat`.
128

139
// edition:2018
1410

1511
macro_rules! foo {
16-
($($a:ident)? ; $num:expr) => { {
12+
// Check for `?`.
13+
($($a:ident)? ? $num:expr) => {
14+
foo!($($a)? ; $num);
15+
};
16+
// Check for `+`.
17+
($($a:ident)? + $num:expr) => {
18+
foo!($($a)? ; $num);
19+
};
20+
// Check for `*`.
21+
($($a:ident)? * $num:expr) => {
22+
foo!($($a)? ; $num);
23+
};
24+
// Check for `;`, not a kleene operator.
25+
($($a:ident)? ; $num:expr) => {
1726
let mut x = 0;
1827

1928
$(
2029
x += $a;
21-
)?
30+
)?
2231

2332
assert_eq!(x, $num);
24-
} }
33+
};
2534
}
2635

2736
pub fn main() {
2837
let a = 1;
2938

30-
// accept 0 or 1 repetitions
39+
// Accept 0 repetitions.
3140
foo!( ; 0);
41+
foo!( + 0);
42+
foo!( * 0);
43+
foo!( ? 0);
44+
45+
// Accept 1 repetition.
3246
foo!(a ; 1);
47+
foo!(a + 1);
48+
foo!(a * 1);
49+
foo!(a ? 1);
3350
}

src/test/ui/macros/macro-at-most-once-rep-2015.rs

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ macro_rules! foo {
66
($(a)?) => {};
77
}
88

9+
// The Kleene op `?` does not admit a separator before it.
910
macro_rules! baz {
1011
($(a),?) => {}; //~ERROR the `?` macro repetition operator
1112
}

src/test/ui/macros/macro-at-most-once-rep-2015.stderr

+12-12
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
error: the `?` macro repetition operator does not take a separator
2-
--> $DIR/macro-at-most-once-rep-2015.rs:10:10
2+
--> $DIR/macro-at-most-once-rep-2015.rs:11:10
33
|
44
LL | ($(a),?) => {};
55
| ^
66

77
error: no rules expected the token `?`
8-
--> $DIR/macro-at-most-once-rep-2015.rs:24:11
8+
--> $DIR/macro-at-most-once-rep-2015.rs:25:11
99
|
1010
LL | macro_rules! foo {
1111
| ---------------- when calling this macro
@@ -14,7 +14,7 @@ LL | foo!(a?);
1414
| ^ no rules expected this token in macro call
1515

1616
error: no rules expected the token `?`
17-
--> $DIR/macro-at-most-once-rep-2015.rs:25:11
17+
--> $DIR/macro-at-most-once-rep-2015.rs:26:11
1818
|
1919
LL | macro_rules! foo {
2020
| ---------------- when calling this macro
@@ -23,7 +23,7 @@ LL | foo!(a?a);
2323
| ^ no rules expected this token in macro call
2424

2525
error: no rules expected the token `?`
26-
--> $DIR/macro-at-most-once-rep-2015.rs:26:11
26+
--> $DIR/macro-at-most-once-rep-2015.rs:27:11
2727
|
2828
LL | macro_rules! foo {
2929
| ---------------- when calling this macro
@@ -32,7 +32,7 @@ LL | foo!(a?a?a);
3232
| ^ no rules expected this token in macro call
3333

3434
error: unexpected end of macro invocation
35-
--> $DIR/macro-at-most-once-rep-2015.rs:28:5
35+
--> $DIR/macro-at-most-once-rep-2015.rs:29:5
3636
|
3737
LL | macro_rules! barplus {
3838
| -------------------- when calling this macro
@@ -41,7 +41,7 @@ LL | barplus!();
4141
| ^^^^^^^^^^^ missing tokens in macro arguments
4242

4343
error: unexpected end of macro invocation
44-
--> $DIR/macro-at-most-once-rep-2015.rs:29:15
44+
--> $DIR/macro-at-most-once-rep-2015.rs:30:15
4545
|
4646
LL | macro_rules! barplus {
4747
| -------------------- when calling this macro
@@ -50,7 +50,7 @@ LL | barplus!(a);
5050
| ^ missing tokens in macro arguments
5151

5252
error: no rules expected the token `?`
53-
--> $DIR/macro-at-most-once-rep-2015.rs:30:15
53+
--> $DIR/macro-at-most-once-rep-2015.rs:31:15
5454
|
5555
LL | macro_rules! barplus {
5656
| -------------------- when calling this macro
@@ -59,7 +59,7 @@ LL | barplus!(a?);
5959
| ^ no rules expected this token in macro call
6060

6161
error: no rules expected the token `?`
62-
--> $DIR/macro-at-most-once-rep-2015.rs:31:15
62+
--> $DIR/macro-at-most-once-rep-2015.rs:32:15
6363
|
6464
LL | macro_rules! barplus {
6565
| -------------------- when calling this macro
@@ -68,7 +68,7 @@ LL | barplus!(a?a);
6868
| ^ no rules expected this token in macro call
6969

7070
error: unexpected end of macro invocation
71-
--> $DIR/macro-at-most-once-rep-2015.rs:35:5
71+
--> $DIR/macro-at-most-once-rep-2015.rs:36:5
7272
|
7373
LL | macro_rules! barstar {
7474
| -------------------- when calling this macro
@@ -77,7 +77,7 @@ LL | barstar!();
7777
| ^^^^^^^^^^^ missing tokens in macro arguments
7878

7979
error: unexpected end of macro invocation
80-
--> $DIR/macro-at-most-once-rep-2015.rs:36:15
80+
--> $DIR/macro-at-most-once-rep-2015.rs:37:15
8181
|
8282
LL | macro_rules! barstar {
8383
| -------------------- when calling this macro
@@ -86,7 +86,7 @@ LL | barstar!(a);
8686
| ^ missing tokens in macro arguments
8787

8888
error: no rules expected the token `?`
89-
--> $DIR/macro-at-most-once-rep-2015.rs:37:15
89+
--> $DIR/macro-at-most-once-rep-2015.rs:38:15
9090
|
9191
LL | macro_rules! barstar {
9292
| -------------------- when calling this macro
@@ -95,7 +95,7 @@ LL | barstar!(a?);
9595
| ^ no rules expected this token in macro call
9696

9797
error: no rules expected the token `?`
98-
--> $DIR/macro-at-most-once-rep-2015.rs:38:15
98+
--> $DIR/macro-at-most-once-rep-2015.rs:39:15
9999
|
100100
LL | macro_rules! barstar {
101101
| -------------------- when calling this macro

src/test/ui/macros/macro-at-most-once-rep-2018.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
// Tests that `?` is a Kleene op and not a macro separator in the 2018 edition.
1+
// Tests that `?` is a Kleene op and not a macro separator in the 2015 edition.
22

33
// edition:2018
44

55
macro_rules! foo {
66
($(a)?) => {};
77
}
88

9+
// The Kleene op `?` does not admit a separator before it.
910
macro_rules! baz {
1011
($(a),?) => {}; //~ERROR the `?` macro repetition operator
1112
}

src/test/ui/macros/macro-at-most-once-rep-2018.stderr

+12-12
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
error: the `?` macro repetition operator does not take a separator
2-
--> $DIR/macro-at-most-once-rep-2018.rs:10:10
2+
--> $DIR/macro-at-most-once-rep-2018.rs:11:10
33
|
44
LL | ($(a),?) => {};
55
| ^
66

77
error: no rules expected the token `?`
8-
--> $DIR/macro-at-most-once-rep-2018.rs:24:11
8+
--> $DIR/macro-at-most-once-rep-2018.rs:25:11
99
|
1010
LL | macro_rules! foo {
1111
| ---------------- when calling this macro
@@ -14,7 +14,7 @@ LL | foo!(a?);
1414
| ^ no rules expected this token in macro call
1515

1616
error: no rules expected the token `?`
17-
--> $DIR/macro-at-most-once-rep-2018.rs:25:11
17+
--> $DIR/macro-at-most-once-rep-2018.rs:26:11
1818
|
1919
LL | macro_rules! foo {
2020
| ---------------- when calling this macro
@@ -23,7 +23,7 @@ LL | foo!(a?a);
2323
| ^ no rules expected this token in macro call
2424

2525
error: no rules expected the token `?`
26-
--> $DIR/macro-at-most-once-rep-2018.rs:26:11
26+
--> $DIR/macro-at-most-once-rep-2018.rs:27:11
2727
|
2828
LL | macro_rules! foo {
2929
| ---------------- when calling this macro
@@ -32,7 +32,7 @@ LL | foo!(a?a?a);
3232
| ^ no rules expected this token in macro call
3333

3434
error: unexpected end of macro invocation
35-
--> $DIR/macro-at-most-once-rep-2018.rs:28:5
35+
--> $DIR/macro-at-most-once-rep-2018.rs:29:5
3636
|
3737
LL | macro_rules! barplus {
3838
| -------------------- when calling this macro
@@ -41,7 +41,7 @@ LL | barplus!();
4141
| ^^^^^^^^^^^ missing tokens in macro arguments
4242

4343
error: unexpected end of macro invocation
44-
--> $DIR/macro-at-most-once-rep-2018.rs:29:15
44+
--> $DIR/macro-at-most-once-rep-2018.rs:30:15
4545
|
4646
LL | macro_rules! barplus {
4747
| -------------------- when calling this macro
@@ -50,7 +50,7 @@ LL | barplus!(a);
5050
| ^ missing tokens in macro arguments
5151

5252
error: no rules expected the token `?`
53-
--> $DIR/macro-at-most-once-rep-2018.rs:30:15
53+
--> $DIR/macro-at-most-once-rep-2018.rs:31:15
5454
|
5555
LL | macro_rules! barplus {
5656
| -------------------- when calling this macro
@@ -59,7 +59,7 @@ LL | barplus!(a?);
5959
| ^ no rules expected this token in macro call
6060

6161
error: no rules expected the token `?`
62-
--> $DIR/macro-at-most-once-rep-2018.rs:31:15
62+
--> $DIR/macro-at-most-once-rep-2018.rs:32:15
6363
|
6464
LL | macro_rules! barplus {
6565
| -------------------- when calling this macro
@@ -68,7 +68,7 @@ LL | barplus!(a?a);
6868
| ^ no rules expected this token in macro call
6969

7070
error: unexpected end of macro invocation
71-
--> $DIR/macro-at-most-once-rep-2018.rs:35:5
71+
--> $DIR/macro-at-most-once-rep-2018.rs:36:5
7272
|
7373
LL | macro_rules! barstar {
7474
| -------------------- when calling this macro
@@ -77,7 +77,7 @@ LL | barstar!();
7777
| ^^^^^^^^^^^ missing tokens in macro arguments
7878

7979
error: unexpected end of macro invocation
80-
--> $DIR/macro-at-most-once-rep-2018.rs:36:15
80+
--> $DIR/macro-at-most-once-rep-2018.rs:37:15
8181
|
8282
LL | macro_rules! barstar {
8383
| -------------------- when calling this macro
@@ -86,7 +86,7 @@ LL | barstar!(a);
8686
| ^ missing tokens in macro arguments
8787

8888
error: no rules expected the token `?`
89-
--> $DIR/macro-at-most-once-rep-2018.rs:37:15
89+
--> $DIR/macro-at-most-once-rep-2018.rs:38:15
9090
|
9191
LL | macro_rules! barstar {
9292
| -------------------- when calling this macro
@@ -95,7 +95,7 @@ LL | barstar!(a?);
9595
| ^ no rules expected this token in macro call
9696

9797
error: no rules expected the token `?`
98-
--> $DIR/macro-at-most-once-rep-2018.rs:38:15
98+
--> $DIR/macro-at-most-once-rep-2018.rs:39:15
9999
|
100100
LL | macro_rules! barstar {
101101
| -------------------- when calling this macro

0 commit comments

Comments
 (0)