Skip to content

Commit 3859eca

Browse files
committed
Improved tests + typo fixes + assert
1 parent 6943430 commit 3859eca

File tree

5 files changed

+89
-17
lines changed

5 files changed

+89
-17
lines changed

src/libsyntax/ext/tt/quoted.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,8 @@ where
396396
// be a `?` separator followed by any Kleene operator. We need to look ahead 1 token to
397397
// find out which.
398398
Ok(Ok(op)) => {
399+
assert_eq!(op, KleeneOp::ZeroOrOne);
400+
399401
// Lookahead at #2. If it is a KleenOp, then #1 is a separator.
400402
let is_1_sep = if let Some(&tokenstream::TokenTree::Token(_, ref tok2)) = input.peek() {
401403
kleene_op(tok2).is_some()

src/test/compile-fail/issue-39388.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#![allow(unused_macros)]
1212

1313
macro_rules! assign {
14-
(($($a:tt)*) = ($($b:tt))*) => { //~ ERROR 14:22: 14:29: expected one of: `*`, `+`, or `?`
14+
(($($a:tt)*) = ($($b:tt))*) => { //~ ERROR expected one of: `*`, `+`, or `?`
1515
$($a)* = $($b)*
1616
}
1717
}

src/test/compile-fail/macro-at-most-once-rep-ambig.rs

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,16 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
// The logic for parsing Kleene operators in macros has a special case to disambiguate `?`.
12+
// Specifically, `$(pat)?` is the ZeroOrOne operator whereas `$(pat)?+` or `$(pat)?*` are the
13+
// ZeroOrMore and OneOrMore operators using `?` as a separator. These tests are intended to
14+
// exercise that logic in the macro parser.
15+
//
16+
// Moreover, we also throw in some tests for using a separator with `?`, which is meaningless but
17+
// included for consistency with `+` and `*`.
18+
//
19+
// This test focuses on error cases.
20+
1121
#![feature(macro_at_most_once_rep)]
1222

1323
macro_rules! foo {
@@ -18,10 +28,14 @@ macro_rules! baz {
1828
($(a),?) => {} // comma separator is meaningless for `?`
1929
}
2030

21-
macro_rules! bar {
31+
macro_rules! barplus {
2232
($(a)?+) => {}
2333
}
2434

35+
macro_rules! barstar {
36+
($(a)?*) => {}
37+
}
38+
2539
pub fn main() {
2640
foo!(a?a?a); //~ ERROR no rules expected the token `?`
2741
foo!(a?a); //~ ERROR no rules expected the token `?`
@@ -33,6 +47,7 @@ pub fn main() {
3347
baz!(a?a?a,); //~ ERROR no rules expected the token `?`
3448
baz!(a?a,); //~ ERROR no rules expected the token `?`
3549
baz!(a?,); //~ ERROR no rules expected the token `?`
36-
bar!(); //~ ERROR unexpected end of macro invocation
37-
bar!(a?); //~ ERROR unexpected end of macro invocation
50+
barplus!(); //~ ERROR unexpected end of macro invocation
51+
barplus!(a?); //~ ERROR unexpected end of macro invocation
52+
barstar!(a?); //~ ERROR unexpected end of macro invocation
3853
}

src/test/run-pass/macro-at-most-once-rep.rs

Lines changed: 66 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,81 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
// The logic for parsing Kleene operators in macros has a special case to disambiguate `?`.
12+
// Specifically, `$(pat)?` is the ZeroOrOne operator whereas `$(pat)?+` or `$(pat)?*` are the
13+
// ZeroOrMore and OneOrMore operators using `?` as a separator. These tests are intended to
14+
// exercise that logic in the macro parser.
15+
//
16+
// Moreover, we also throw in some tests for using a separator with `?`, which is meaningless but
17+
// included for consistency with `+` and `*`.
18+
//
19+
// This test focuses on non-error cases and making sure the correct number of repetitions happen.
20+
1121
#![feature(macro_at_most_once_rep)]
1222

1323
macro_rules! foo {
14-
($(a)?) => {}
24+
($($a:ident)? ; $num:expr) => { {
25+
let mut x = 0;
26+
27+
$(
28+
x += $a;
29+
)?
30+
31+
assert_eq!(x, $num);
32+
} }
1533
}
1634

1735
macro_rules! baz {
18-
($(a),?) => {} // comma separator is meaningless for `?`
36+
($($a:ident),? ; $num:expr) => { { // comma separator is meaningless for `?`
37+
let mut x = 0;
38+
39+
$(
40+
x += $a;
41+
)?
42+
43+
assert_eq!(x, $num);
44+
} }
1945
}
2046

21-
macro_rules! bar {
22-
($(a)?+) => {}
47+
macro_rules! barplus {
48+
($($a:ident)?+ ; $num:expr) => { {
49+
let mut x = 0;
50+
51+
$(
52+
x += $a;
53+
)+
54+
55+
assert_eq!(x, $num);
56+
} }
57+
}
58+
59+
macro_rules! barstar {
60+
($($a:ident)?* ; $num:expr) => { {
61+
let mut x = 0;
62+
63+
$(
64+
x += $a;
65+
)*
66+
67+
assert_eq!(x, $num);
68+
} }
2369
}
2470

2571
pub fn main() {
26-
foo!();
27-
foo!(a);
28-
baz!();
29-
baz!(a);
30-
bar!(a);
31-
bar!(a?a);
32-
bar!(a?a?a);
72+
let a = 1;
73+
74+
// accept 0 or 1 repetitions
75+
foo!( ; 0);
76+
foo!(a ; 1);
77+
baz!( ; 0);
78+
baz!(a ; 1);
79+
80+
// Make sure using ? as a separator works as before
81+
barplus!(a ; 1);
82+
barplus!(a?a ; 2);
83+
barplus!(a?a?a ; 3);
84+
barstar!( ; 0);
85+
barstar!(a ; 1);
86+
barstar!(a?a ; 2);
87+
barstar!(a?a?a ; 3);
3388
}

src/test/ui/feature-gate-macro_at_most_once_rep.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
// Test that the MSP430 interrupt ABI cannot be used when msp430_interrupt
12-
// feature gate is not used.
11+
// Test that `?` macro Kleene operator can not be used when the `macro_at_most_once_rep` feature
12+
// gate is not used.
1313

1414
macro_rules! m { ($(a)?) => {} }
1515
//~^ ERROR Using the `?` macro Kleene operator for "at most one" repetition is unstable

0 commit comments

Comments
 (0)