Fix inaccurate comments in '?' Kleene operator tests.

This commit is contained in:
Mazdak Farrokhzad 2019-05-22 05:52:47 +02:00
parent f86719a111
commit a4b9a03362
6 changed files with 87 additions and 51 deletions

View file

@ -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);
}

View file

@ -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);
}

View file

@ -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
}

View file

@ -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

View file

@ -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
}

View file

@ -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