diff options
6 files changed, 87 insertions, 51 deletions
diff --git a/src/test/run-pass/macros/macro-at-most-once-rep-2015.rs b/src/test/run-pass/macros/macro-at-most-once-rep-2015.rs index 72dfc119966..66597c0acf6 100644 --- a/src/test/run-pass/macros/macro-at-most-once-rep-2015.rs +++ b/src/test/run-pass/macros/macro-at-most-once-rep-2015.rs @@ -1,33 +1,50 @@ // run-pass + #![allow(unused_mut)] -// The logic for parsing Kleene operators in macros has a special case to disambiguate `?`. -// Specifically, `$(pat)?` is the ZeroOrOne operator whereas `$(pat)?+` or `$(pat)?*` are the -// ZeroOrMore and OneOrMore operators using `?` as a separator. These tests are intended to -// exercise that logic in the macro parser. -// -// Moreover, we also throw in some tests for using a separator with `?`, which is meaningless but -// included for consistency with `+` and `*`. -// -// This test focuses on non-error cases and making sure the correct number of repetitions happen. + +// Check that when `?` is followed by what looks like a Kleene operator (?, +, and *) +// then that `?` is not interpreted as a separator. In other words, `$(pat)?+` matches `pat +` +// or `+` but does not match `pat` or `pat ? pat`. // edition:2015 macro_rules! foo { - ($($a:ident)? ; $num:expr) => { { + // Check for `?`. + ($($a:ident)? ? $num:expr) => { + foo!($($a)? ; $num); + }; + // Check for `+`. + ($($a:ident)? + $num:expr) => { + foo!($($a)? ; $num); + }; + // Check for `*`. + ($($a:ident)? * $num:expr) => { + foo!($($a)? ; $num); + }; + // Check for `;`, not a kleene operator. + ($($a:ident)? ; $num:expr) => { let mut x = 0; $( x += $a; - )? + )? assert_eq!(x, $num); - } } + }; } pub fn main() { let a = 1; - // accept 0 or 1 repetitions + // Accept 0 repetitions. foo!( ; 0); + foo!( + 0); + foo!( * 0); + foo!( ? 0); + + // Accept 1 repetition. foo!(a ; 1); + foo!(a + 1); + foo!(a * 1); + foo!(a ? 1); } diff --git a/src/test/run-pass/macros/macro-at-most-once-rep-2018.rs b/src/test/run-pass/macros/macro-at-most-once-rep-2018.rs index 582ef088a73..b37f3853016 100644 --- a/src/test/run-pass/macros/macro-at-most-once-rep-2018.rs +++ b/src/test/run-pass/macros/macro-at-most-once-rep-2018.rs @@ -1,33 +1,50 @@ // run-pass + #![allow(unused_mut)] -// The logic for parsing Kleene operators in macros has a special case to disambiguate `?`. -// Specifically, `$(pat)?` is the ZeroOrOne operator whereas `$(pat)?+` or `$(pat)?*` are the -// ZeroOrMore and OneOrMore operators using `?` as a separator. These tests are intended to -// exercise that logic in the macro parser. -// -// Moreover, we also throw in some tests for using a separator with `?`, which is meaningless but -// included for consistency with `+` and `*`. -// -// This test focuses on non-error cases and making sure the correct number of repetitions happen. + +// Check that when `?` is followed by what looks like a Kleene operator (?, +, and *) +// then that `?` is not interpreted as a separator. In other words, `$(pat)?+` matches `pat +` +// or `+` but does not match `pat` or `pat ? pat`. // edition:2018 macro_rules! foo { - ($($a:ident)? ; $num:expr) => { { + // Check for `?`. + ($($a:ident)? ? $num:expr) => { + foo!($($a)? ; $num); + }; + // Check for `+`. + ($($a:ident)? + $num:expr) => { + foo!($($a)? ; $num); + }; + // Check for `*`. + ($($a:ident)? * $num:expr) => { + foo!($($a)? ; $num); + }; + // Check for `;`, not a kleene operator. + ($($a:ident)? ; $num:expr) => { let mut x = 0; $( x += $a; - )? + )? assert_eq!(x, $num); - } } + }; } pub fn main() { let a = 1; - // accept 0 or 1 repetitions + // Accept 0 repetitions. foo!( ; 0); + foo!( + 0); + foo!( * 0); + foo!( ? 0); + + // Accept 1 repetition. foo!(a ; 1); + foo!(a + 1); + foo!(a * 1); + foo!(a ? 1); } diff --git a/src/test/ui/macros/macro-at-most-once-rep-2015.rs b/src/test/ui/macros/macro-at-most-once-rep-2015.rs index e6def0f58c5..f68100d4557 100644 --- a/src/test/ui/macros/macro-at-most-once-rep-2015.rs +++ b/src/test/ui/macros/macro-at-most-once-rep-2015.rs @@ -6,6 +6,7 @@ macro_rules! foo { ($(a)?) => {}; } +// The Kleene op `?` does not admit a separator before it. macro_rules! baz { ($(a),?) => {}; //~ERROR the `?` macro repetition operator } diff --git a/src/test/ui/macros/macro-at-most-once-rep-2015.stderr b/src/test/ui/macros/macro-at-most-once-rep-2015.stderr index 81de4f145c8..f9871ab8ffe 100644 --- a/src/test/ui/macros/macro-at-most-once-rep-2015.stderr +++ b/src/test/ui/macros/macro-at-most-once-rep-2015.stderr @@ -1,11 +1,11 @@ error: the `?` macro repetition operator does not take a separator - --> $DIR/macro-at-most-once-rep-2015.rs:10:10 + --> $DIR/macro-at-most-once-rep-2015.rs:11:10 | LL | ($(a),?) => {}; | ^ error: no rules expected the token `?` - --> $DIR/macro-at-most-once-rep-2015.rs:24:11 + --> $DIR/macro-at-most-once-rep-2015.rs:25:11 | LL | macro_rules! foo { | ---------------- when calling this macro @@ -14,7 +14,7 @@ LL | foo!(a?); | ^ no rules expected this token in macro call error: no rules expected the token `?` - --> $DIR/macro-at-most-once-rep-2015.rs:25:11 + --> $DIR/macro-at-most-once-rep-2015.rs:26:11 | LL | macro_rules! foo { | ---------------- when calling this macro @@ -23,7 +23,7 @@ LL | foo!(a?a); | ^ no rules expected this token in macro call error: no rules expected the token `?` - --> $DIR/macro-at-most-once-rep-2015.rs:26:11 + --> $DIR/macro-at-most-once-rep-2015.rs:27:11 | LL | macro_rules! foo { | ---------------- when calling this macro @@ -32,7 +32,7 @@ LL | foo!(a?a?a); | ^ no rules expected this token in macro call error: unexpected end of macro invocation - --> $DIR/macro-at-most-once-rep-2015.rs:28:5 + --> $DIR/macro-at-most-once-rep-2015.rs:29:5 | LL | macro_rules! barplus { | -------------------- when calling this macro @@ -41,7 +41,7 @@ LL | barplus!(); | ^^^^^^^^^^^ missing tokens in macro arguments error: unexpected end of macro invocation - --> $DIR/macro-at-most-once-rep-2015.rs:29:15 + --> $DIR/macro-at-most-once-rep-2015.rs:30:15 | LL | macro_rules! barplus { | -------------------- when calling this macro @@ -50,7 +50,7 @@ LL | barplus!(a); | ^ missing tokens in macro arguments error: no rules expected the token `?` - --> $DIR/macro-at-most-once-rep-2015.rs:30:15 + --> $DIR/macro-at-most-once-rep-2015.rs:31:15 | LL | macro_rules! barplus { | -------------------- when calling this macro @@ -59,7 +59,7 @@ LL | barplus!(a?); | ^ no rules expected this token in macro call error: no rules expected the token `?` - --> $DIR/macro-at-most-once-rep-2015.rs:31:15 + --> $DIR/macro-at-most-once-rep-2015.rs:32:15 | LL | macro_rules! barplus { | -------------------- when calling this macro @@ -68,7 +68,7 @@ LL | barplus!(a?a); | ^ no rules expected this token in macro call error: unexpected end of macro invocation - --> $DIR/macro-at-most-once-rep-2015.rs:35:5 + --> $DIR/macro-at-most-once-rep-2015.rs:36:5 | LL | macro_rules! barstar { | -------------------- when calling this macro @@ -77,7 +77,7 @@ LL | barstar!(); | ^^^^^^^^^^^ missing tokens in macro arguments error: unexpected end of macro invocation - --> $DIR/macro-at-most-once-rep-2015.rs:36:15 + --> $DIR/macro-at-most-once-rep-2015.rs:37:15 | LL | macro_rules! barstar { | -------------------- when calling this macro @@ -86,7 +86,7 @@ LL | barstar!(a); | ^ missing tokens in macro arguments error: no rules expected the token `?` - --> $DIR/macro-at-most-once-rep-2015.rs:37:15 + --> $DIR/macro-at-most-once-rep-2015.rs:38:15 | LL | macro_rules! barstar { | -------------------- when calling this macro @@ -95,7 +95,7 @@ LL | barstar!(a?); | ^ no rules expected this token in macro call error: no rules expected the token `?` - --> $DIR/macro-at-most-once-rep-2015.rs:38:15 + --> $DIR/macro-at-most-once-rep-2015.rs:39:15 | LL | macro_rules! barstar { | -------------------- when calling this macro diff --git a/src/test/ui/macros/macro-at-most-once-rep-2018.rs b/src/test/ui/macros/macro-at-most-once-rep-2018.rs index da072adec15..e4ae406f512 100644 --- a/src/test/ui/macros/macro-at-most-once-rep-2018.rs +++ b/src/test/ui/macros/macro-at-most-once-rep-2018.rs @@ -1,4 +1,4 @@ -// Tests that `?` is a Kleene op and not a macro separator in the 2018 edition. +// Tests that `?` is a Kleene op and not a macro separator in the 2015 edition. // edition:2018 @@ -6,6 +6,7 @@ macro_rules! foo { ($(a)?) => {}; } +// The Kleene op `?` does not admit a separator before it. macro_rules! baz { ($(a),?) => {}; //~ERROR the `?` macro repetition operator } diff --git a/src/test/ui/macros/macro-at-most-once-rep-2018.stderr b/src/test/ui/macros/macro-at-most-once-rep-2018.stderr index f285c7cc7c2..bfe5883b03f 100644 --- a/src/test/ui/macros/macro-at-most-once-rep-2018.stderr +++ b/src/test/ui/macros/macro-at-most-once-rep-2018.stderr @@ -1,11 +1,11 @@ error: the `?` macro repetition operator does not take a separator - --> $DIR/macro-at-most-once-rep-2018.rs:10:10 + --> $DIR/macro-at-most-once-rep-2018.rs:11:10 | LL | ($(a),?) => {}; | ^ error: no rules expected the token `?` - --> $DIR/macro-at-most-once-rep-2018.rs:24:11 + --> $DIR/macro-at-most-once-rep-2018.rs:25:11 | LL | macro_rules! foo { | ---------------- when calling this macro @@ -14,7 +14,7 @@ LL | foo!(a?); | ^ no rules expected this token in macro call error: no rules expected the token `?` - --> $DIR/macro-at-most-once-rep-2018.rs:25:11 + --> $DIR/macro-at-most-once-rep-2018.rs:26:11 | LL | macro_rules! foo { | ---------------- when calling this macro @@ -23,7 +23,7 @@ LL | foo!(a?a); | ^ no rules expected this token in macro call error: no rules expected the token `?` - --> $DIR/macro-at-most-once-rep-2018.rs:26:11 + --> $DIR/macro-at-most-once-rep-2018.rs:27:11 | LL | macro_rules! foo { | ---------------- when calling this macro @@ -32,7 +32,7 @@ LL | foo!(a?a?a); | ^ no rules expected this token in macro call error: unexpected end of macro invocation - --> $DIR/macro-at-most-once-rep-2018.rs:28:5 + --> $DIR/macro-at-most-once-rep-2018.rs:29:5 | LL | macro_rules! barplus { | -------------------- when calling this macro @@ -41,7 +41,7 @@ LL | barplus!(); | ^^^^^^^^^^^ missing tokens in macro arguments error: unexpected end of macro invocation - --> $DIR/macro-at-most-once-rep-2018.rs:29:15 + --> $DIR/macro-at-most-once-rep-2018.rs:30:15 | LL | macro_rules! barplus { | -------------------- when calling this macro @@ -50,7 +50,7 @@ LL | barplus!(a); | ^ missing tokens in macro arguments error: no rules expected the token `?` - --> $DIR/macro-at-most-once-rep-2018.rs:30:15 + --> $DIR/macro-at-most-once-rep-2018.rs:31:15 | LL | macro_rules! barplus { | -------------------- when calling this macro @@ -59,7 +59,7 @@ LL | barplus!(a?); | ^ no rules expected this token in macro call error: no rules expected the token `?` - --> $DIR/macro-at-most-once-rep-2018.rs:31:15 + --> $DIR/macro-at-most-once-rep-2018.rs:32:15 | LL | macro_rules! barplus { | -------------------- when calling this macro @@ -68,7 +68,7 @@ LL | barplus!(a?a); | ^ no rules expected this token in macro call error: unexpected end of macro invocation - --> $DIR/macro-at-most-once-rep-2018.rs:35:5 + --> $DIR/macro-at-most-once-rep-2018.rs:36:5 | LL | macro_rules! barstar { | -------------------- when calling this macro @@ -77,7 +77,7 @@ LL | barstar!(); | ^^^^^^^^^^^ missing tokens in macro arguments error: unexpected end of macro invocation - --> $DIR/macro-at-most-once-rep-2018.rs:36:15 + --> $DIR/macro-at-most-once-rep-2018.rs:37:15 | LL | macro_rules! barstar { | -------------------- when calling this macro @@ -86,7 +86,7 @@ LL | barstar!(a); | ^ missing tokens in macro arguments error: no rules expected the token `?` - --> $DIR/macro-at-most-once-rep-2018.rs:37:15 + --> $DIR/macro-at-most-once-rep-2018.rs:38:15 | LL | macro_rules! barstar { | -------------------- when calling this macro @@ -95,7 +95,7 @@ LL | barstar!(a?); | ^ no rules expected this token in macro call error: no rules expected the token `?` - --> $DIR/macro-at-most-once-rep-2018.rs:38:15 + --> $DIR/macro-at-most-once-rep-2018.rs:39:15 | LL | macro_rules! barstar { | -------------------- when calling this macro |
