]> git.lizzy.rs Git - rust.git/commitdiff
Improved tests + typo fixes + assert
authorMark Mansi <markm@cs.wisc.edu>
Mon, 29 Jan 2018 22:26:11 +0000 (16:26 -0600)
committerMark Mansi <markm@cs.wisc.edu>
Tue, 30 Jan 2018 18:42:51 +0000 (12:42 -0600)
src/libsyntax/ext/tt/quoted.rs
src/test/compile-fail/issue-39388.rs
src/test/compile-fail/macro-at-most-once-rep-ambig.rs
src/test/run-pass/macro-at-most-once-rep.rs
src/test/ui/feature-gate-macro_at_most_once_rep.rs

index 8e05a6ccc470411db1ec64d494431b4b933c86a2..bde1010b523aba4783dedfd12eef561cbca1476f 100644 (file)
@@ -396,6 +396,8 @@ fn parse_sep_and_kleene_op<I>(
         // be a `?` separator followed by any Kleene operator. We need to look ahead 1 token to
         // find out which.
         Ok(Ok(op)) => {
+            assert_eq!(op, KleeneOp::ZeroOrOne);
+
             // Lookahead at #2. If it is a KleenOp, then #1 is a separator.
             let is_1_sep = if let Some(&tokenstream::TokenTree::Token(_, ref tok2)) = input.peek() {
                 kleene_op(tok2).is_some()
index 3fbbab62d24cc1362cef0d2d67fc18c2f61e95e5..6da049374086a28cc65b793654c4eeb1e7b8e9cb 100644 (file)
@@ -11,7 +11,7 @@
 #![allow(unused_macros)]
 
 macro_rules! assign {
-    (($($a:tt)*) = ($($b:tt))*) => { //~ ERROR 14:22: 14:29: expected one of: `*`, `+`, or `?`
+    (($($a:tt)*) = ($($b:tt))*) => { //~ ERROR expected one of: `*`, `+`, or `?`
         $($a)* = $($b)*
     }
 }
index 6886a02cb921012d6ccd24bda6a561cf0021f8ad..a5660f8b41f8da43aeeea29deefa8607dc764bf4 100644 (file)
@@ -8,6 +8,16 @@
 // option. This file may not be copied, modified, or distributed
 // except according to those terms.
 
+// 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 error cases.
+
 #![feature(macro_at_most_once_rep)]
 
 macro_rules! foo {
@@ -18,10 +28,14 @@ macro_rules! baz {
     ($(a),?) => {} // comma separator is meaningless for `?`
 }
 
-macro_rules! bar {
+macro_rules! barplus {
     ($(a)?+) => {}
 }
 
+macro_rules! barstar {
+    ($(a)?*) => {}
+}
+
 pub fn main() {
     foo!(a?a?a); //~ ERROR no rules expected the token `?`
     foo!(a?a); //~ ERROR no rules expected the token `?`
@@ -33,6 +47,7 @@ pub fn main() {
     baz!(a?a?a,); //~ ERROR no rules expected the token `?`
     baz!(a?a,); //~ ERROR no rules expected the token `?`
     baz!(a?,); //~ ERROR no rules expected the token `?`
-    bar!(); //~ ERROR unexpected end of macro invocation
-    bar!(a?); //~ ERROR unexpected end of macro invocation
+    barplus!(); //~ ERROR unexpected end of macro invocation
+    barplus!(a?); //~ ERROR unexpected end of macro invocation
+    barstar!(a?); //~ ERROR unexpected end of macro invocation
 }
index ecfa92d5a738b1e680e1bbe78ab663364e580ef9..b7e942f938321ae8f033a65909cea38975d856e2 100644 (file)
@@ -8,26 +8,81 @@
 // option. This file may not be copied, modified, or distributed
 // except according to those terms.
 
+// 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.
+
 #![feature(macro_at_most_once_rep)]
 
 macro_rules! foo {
-    ($(a)?) => {}
+    ($($a:ident)? ; $num:expr) => { {
+        let mut x = 0;
+
+        $(
+            x += $a;
+         )?
+
+        assert_eq!(x, $num);
+    } }
 }
 
 macro_rules! baz {
-    ($(a),?) => {} // comma separator is meaningless for `?`
+    ($($a:ident),? ; $num:expr) => { { // comma separator is meaningless for `?`
+        let mut x = 0;
+
+        $(
+            x += $a;
+         )?
+
+        assert_eq!(x, $num);
+    } }
 }
 
-macro_rules! bar {
-    ($(a)?+) => {}
+macro_rules! barplus {
+    ($($a:ident)?+ ; $num:expr) => { {
+        let mut x = 0;
+
+        $(
+            x += $a;
+         )+
+
+        assert_eq!(x, $num);
+    } }
+}
+
+macro_rules! barstar {
+    ($($a:ident)?* ; $num:expr) => { {
+        let mut x = 0;
+
+        $(
+            x += $a;
+         )*
+
+        assert_eq!(x, $num);
+    } }
 }
 
 pub fn main() {
-    foo!();
-    foo!(a);
-    baz!();
-    baz!(a);
-    bar!(a);
-    bar!(a?a);
-    bar!(a?a?a);
+    let a = 1;
+
+    // accept 0 or 1 repetitions
+    foo!( ; 0);
+    foo!(a ; 1);
+    baz!( ; 0);
+    baz!(a ; 1);
+
+    // Make sure using ? as a separator works as before
+    barplus!(a ; 1);
+    barplus!(a?a ; 2);
+    barplus!(a?a?a ; 3);
+    barstar!( ; 0);
+    barstar!(a ; 1);
+    barstar!(a?a ; 2);
+    barstar!(a?a?a ; 3);
 }
index 13b2a8e217a42ad1968a7ebc486f39010df0607d..19f5aca5730e1b18d0f2b4ae83462de391378d25 100644 (file)
@@ -8,8 +8,8 @@
 // option. This file may not be copied, modified, or distributed
 // except according to those terms.
 
-// Test that the MSP430 interrupt ABI cannot be used when msp430_interrupt
-// feature gate is not used.
+// Test that `?` macro Kleene operator can not be used when the `macro_at_most_once_rep` feature
+// gate is not used.
 
 macro_rules! m { ($(a)?) => {} }
 //~^ ERROR Using the `?` macro Kleene operator for "at most one" repetition is unstable