Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support ? Kleene macro operator in 2015 #60932

Merged
merged 7 commits into from
Jun 9, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fix inaccurate comments in '?' Kleene operator tests.
  • Loading branch information
Centril committed Jun 9, 2019
commit a4b9a03362f626db617d3a67685d3a9ba9f1c683
43 changes: 30 additions & 13 deletions src/test/run-pass/macros/macro-at-most-once-rep-2015.rs
Original file line number Diff line number Diff line change
@@ -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);
}
43 changes: 30 additions & 13 deletions src/test/run-pass/macros/macro-at-most-once-rep-2018.rs
Original file line number Diff line number Diff line change
@@ -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);
}
1 change: 1 addition & 0 deletions src/test/ui/macros/macro-at-most-once-rep-2015.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
24 changes: 12 additions & 12 deletions src/test/ui/macros/macro-at-most-once-rep-2015.stderr
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
3 changes: 2 additions & 1 deletion src/test/ui/macros/macro-at-most-once-rep-2018.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
// 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

macro_rules! foo {
($(a)?) => {};
}

// The Kleene op `?` does not admit a separator before it.
macro_rules! baz {
($(a),?) => {}; //~ERROR the `?` macro repetition operator
}
Expand Down
24 changes: 12 additions & 12 deletions src/test/ui/macros/macro-at-most-once-rep-2018.stderr
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down