Implement 2015 vs 2018 ? kleene op + test

This commit is contained in:
mark 2018-06-15 21:49:00 -05:00
parent 2a7ae04a68
commit 8eb4941e30
22 changed files with 871 additions and 474 deletions

View file

@ -13,7 +13,7 @@
macro_rules! foo {
{ $+ } => { //~ ERROR expected identifier, found `+`
//~^ ERROR missing fragment specifier
$(x)(y) //~ ERROR expected one of: `*`, `+`, or `?`
$(x)(y) //~ ERROR expected `*` or `+`
}
}

View file

@ -29,11 +29,9 @@ use syntax::ext::tt::macro_parser::{MatchedSeq, MatchedNonterminal};
use syntax::ext::tt::macro_parser::{Success, Failure, Error};
use syntax::ext::tt::macro_parser::parse_failure_msg;
use syntax::ptr::P;
use syntax_pos::Span;
use syntax_pos::{Span, edition::Edition};
use rustc_plugin::Registry;
use std::cell::RefCell;
fn expand_mbe_matches(cx: &mut ExtCtxt, _: Span, args: &[TokenTree])
-> Box<MacResult + 'static> {
@ -42,7 +40,8 @@ fn expand_mbe_matches(cx: &mut ExtCtxt, _: Span, args: &[TokenTree])
true,
cx.parse_sess,
&Features::new(),
&[]);
&[],
Edition::Edition2015);
let map = match TokenTree::parse(cx, &mbe_matcher, args.iter().cloned().collect()) {
Success(map) => map,
Failure(_, tok) => {

View file

@ -18,6 +18,8 @@
//
// This test focuses on non-error cases and making sure the correct number of repetitions happen.
// compile-flags: --edition=2018
#![feature(macro_at_most_once_rep)]
macro_rules! foo {
@ -32,57 +34,10 @@ macro_rules! foo {
} }
}
macro_rules! baz {
($($a:ident),? ; $num:expr) => { { // comma separator is meaningless for `?`
let mut x = 0;
$(
x += $a;
)?
assert_eq!(x, $num);
} }
}
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() {
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);
}

View file

@ -1,11 +0,0 @@
error[E0658]: using the `?` macro Kleene operator for "at most one" repetition is unstable (see issue #48075)
--> $DIR/feature-gate-macro_at_most_once_rep.rs:14:20
|
LL | macro_rules! m { ($(a)?) => {} }
| ^^^
|
= help: add #![feature(macro_at_most_once_rep)] to the crate attributes to enable
error: aborting due to previous error
For more information about this error, try `rustc --explain E0658`.

View file

@ -11,7 +11,7 @@
#![allow(unused_macros)]
macro_rules! assign {
(($($a:tt)*) = ($($b:tt))*) => { //~ ERROR expected one of: `*`, `+`, or `?`
(($($a:tt)*) = ($($b:tt))*) => { //~ ERROR expected `*` or `+`
$($a)* = $($b)*
}
}

View file

@ -0,0 +1,28 @@
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
// Test behavior of `?` macro _kleene op_ under the 2015 edition. Namely, it doesn't exist, even
// with the feature flag.
// gate-test-macro_at_most_once_rep
// compile-flags: --edition=2015
#![feature(macro_at_most_once_rep)]
macro_rules! bar {
($(a)?) => {} //~ERROR expected `*` or `+`
}
macro_rules! baz {
($(a),?) => {} //~ERROR expected `*` or `+`
}
fn main() {}

View file

@ -0,0 +1,18 @@
error: expected `*` or `+`
--> $DIR/macro-at-most-once-rep-2015-ques-rep-feature-flag.rs:20:10
|
LL | ($(a)?) => {} //~ERROR expected `*` or `+`
| ^
|
= note: `?` is not a macro repetition operator
error: expected `*` or `+`
--> $DIR/macro-at-most-once-rep-2015-ques-rep-feature-flag.rs:24:11
|
LL | ($(a),?) => {} //~ERROR expected `*` or `+`
| ^
|
= note: `?` is not a macro repetition operator
error: aborting due to 2 previous errors

View file

@ -1,4 +1,4 @@
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
@ -8,12 +8,16 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.
// Test that `?` macro Kleene operator can not be used when the `macro_at_most_once_rep` feature
// gate is not used.
// Test behavior of `?` macro _kleene op_ under the 2015 edition. Namely, it doesn't exist.
macro_rules! m { ($(a)?) => {} }
//~^ ERROR using the `?` macro Kleene operator for "at most one" repetition is unstable
// compile-flags: --edition=2015
fn main() {
m!();
macro_rules! bar {
($(a)?) => {} //~ERROR expected `*` or `+`
}
macro_rules! baz {
($(a),?) => {} //~ERROR expected `*` or `+`
}
fn main() {}

View file

@ -0,0 +1,18 @@
error: expected `*` or `+`
--> $DIR/macro-at-most-once-rep-2015-ques-rep.rs:16:10
|
LL | ($(a)?) => {} //~ERROR expected `*` or `+`
| ^
|
= note: `?` is not a macro repetition operator
error: expected `*` or `+`
--> $DIR/macro-at-most-once-rep-2015-ques-rep.rs:20:11
|
LL | ($(a),?) => {} //~ERROR expected `*` or `+`
| ^
|
= note: `?` is not a macro repetition operator
error: aborting due to 2 previous errors

View file

@ -0,0 +1,34 @@
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
// Test behavior of `?` macro _separator_ under the 2015 edition. Namely, `?` can be used as a
// separator, but you get a migration warning for the edition.
// compile-flags: --edition=2015
// compile-pass
macro_rules! bar {
($(a)?*) => {} //~WARN using `?` as a separator
}
macro_rules! baz {
($(a)?+) => {} //~WARN using `?` as a separator
}
fn main() {
bar!();
bar!(a);
bar!(a?a);
bar!(a?a?a?a?a);
baz!(a);
baz!(a?a);
baz!(a?a?a?a?a);
}

View file

@ -0,0 +1,12 @@
warning: using `?` as a separator is deprecated and will be a hard error in an upcoming edition
--> $DIR/macro-at-most-once-rep-2015-ques-sep.rs:18:10
|
LL | ($(a)?*) => {} //~WARN using `?` as a separator
| ^
warning: using `?` as a separator is deprecated and will be a hard error in an upcoming edition
--> $DIR/macro-at-most-once-rep-2015-ques-sep.rs:22:10
|
LL | ($(a)?+) => {} //~WARN using `?` as a separator
| ^

View file

@ -0,0 +1,45 @@
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
// Feature gate test for macro_at_most_once_rep under 2018 edition.
// gate-test-macro_at_most_once_rep
// compile-flags: --edition=2018
macro_rules! foo {
($(a)?) => {}
//~^ERROR using the `?` macro Kleene operator for
//~|ERROR expected `*` or `+`
}
macro_rules! baz {
($(a),?) => {} //~ERROR expected `*` or `+`
}
macro_rules! barplus {
($(a)?+) => {}
//~^ERROR using the `?` macro Kleene operator for
//~|ERROR expected `*` or `+`
}
macro_rules! barstar {
($(a)?*) => {}
//~^ERROR using the `?` macro Kleene operator for
//~|ERROR expected `*` or `+`
}
pub fn main() {
foo!();
foo!(a);
foo!(a?); //~ ERROR no rules expected the token `?`
foo!(a?a); //~ ERROR no rules expected the token `?`
foo!(a?a?a); //~ ERROR no rules expected the token `?`
}

View file

@ -0,0 +1,71 @@
error[E0658]: using the `?` macro Kleene operator for "at most one" repetition is unstable (see issue #48075)
--> $DIR/macro-at-most-once-rep-2018-feature-gate.rs:17:10
|
LL | ($(a)?) => {}
| ^
|
= help: add #![feature(macro_at_most_once_rep)] to the crate attributes to enable
error: expected `*` or `+`
--> $DIR/macro-at-most-once-rep-2018-feature-gate.rs:17:10
|
LL | ($(a)?) => {}
| ^
error: expected `*` or `+`
--> $DIR/macro-at-most-once-rep-2018-feature-gate.rs:23:11
|
LL | ($(a),?) => {} //~ERROR expected `*` or `+`
| ^
|
= note: `?` is not a macro repetition operator
error[E0658]: using the `?` macro Kleene operator for "at most one" repetition is unstable (see issue #48075)
--> $DIR/macro-at-most-once-rep-2018-feature-gate.rs:27:10
|
LL | ($(a)?+) => {}
| ^
|
= help: add #![feature(macro_at_most_once_rep)] to the crate attributes to enable
error: expected `*` or `+`
--> $DIR/macro-at-most-once-rep-2018-feature-gate.rs:27:10
|
LL | ($(a)?+) => {}
| ^
error[E0658]: using the `?` macro Kleene operator for "at most one" repetition is unstable (see issue #48075)
--> $DIR/macro-at-most-once-rep-2018-feature-gate.rs:33:10
|
LL | ($(a)?*) => {}
| ^
|
= help: add #![feature(macro_at_most_once_rep)] to the crate attributes to enable
error: expected `*` or `+`
--> $DIR/macro-at-most-once-rep-2018-feature-gate.rs:33:10
|
LL | ($(a)?*) => {}
| ^
error: no rules expected the token `?`
--> $DIR/macro-at-most-once-rep-2018-feature-gate.rs:41:11
|
LL | foo!(a?); //~ ERROR no rules expected the token `?`
| ^
error: no rules expected the token `?`
--> $DIR/macro-at-most-once-rep-2018-feature-gate.rs:42:11
|
LL | foo!(a?a); //~ ERROR no rules expected the token `?`
| ^
error: no rules expected the token `?`
--> $DIR/macro-at-most-once-rep-2018-feature-gate.rs:43:11
|
LL | foo!(a?a?a); //~ ERROR no rules expected the token `?`
| ^
error: aborting due to 10 previous errors
For more information about this error, try `rustc --explain E0658`.

View file

@ -0,0 +1,53 @@
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
// Tests that `?` is a Kleene op and not a macro separator in the 2018 edition.
// compile-flags: --edition=2018
#![feature(macro_at_most_once_rep)]
macro_rules! foo {
($(a)?) => {}
}
macro_rules! baz {
($(a),?) => {} //~ERROR the `?` macro repetition operator
}
macro_rules! barplus {
($(a)?+) => {} // ok. matches "a+" and "+"
}
macro_rules! barstar {
($(a)?*) => {} // ok. matches "a*" and "*"
}
pub fn main() {
foo!();
foo!(a);
foo!(a?); //~ ERROR no rules expected the token `?`
foo!(a?a); //~ ERROR no rules expected the token `?`
foo!(a?a?a); //~ ERROR no rules expected the token `?`
barplus!(); //~ERROR unexpected end of macro invocation
barplus!(a); //~ERROR unexpected end of macro invocation
barplus!(a?); //~ ERROR no rules expected the token `?`
barplus!(a?a); //~ ERROR no rules expected the token `?`
barplus!(a+);
barplus!(+);
barstar!(); //~ERROR unexpected end of macro invocation
barstar!(a); //~ERROR unexpected end of macro invocation
barstar!(a?); //~ ERROR no rules expected the token `?`
barstar!(a?a); //~ ERROR no rules expected the token `?`
barstar!(a*);
barstar!(*);
}

View file

@ -0,0 +1,74 @@
error: the `?` macro repetition operator does not take a separator
--> $DIR/macro-at-most-once-rep-2018.rs:22:10
|
LL | ($(a),?) => {} //~ERROR the `?` macro repetition operator
| ^
error: no rules expected the token `?`
--> $DIR/macro-at-most-once-rep-2018.rs:36:11
|
LL | foo!(a?); //~ ERROR no rules expected the token `?`
| ^
error: no rules expected the token `?`
--> $DIR/macro-at-most-once-rep-2018.rs:37:11
|
LL | foo!(a?a); //~ ERROR no rules expected the token `?`
| ^
error: no rules expected the token `?`
--> $DIR/macro-at-most-once-rep-2018.rs:38:11
|
LL | foo!(a?a?a); //~ ERROR no rules expected the token `?`
| ^
error: unexpected end of macro invocation
--> $DIR/macro-at-most-once-rep-2018.rs:40:5
|
LL | barplus!(); //~ERROR unexpected end of macro invocation
| ^^^^^^^^^^^
error: unexpected end of macro invocation
--> $DIR/macro-at-most-once-rep-2018.rs:41:14
|
LL | barplus!(a); //~ERROR unexpected end of macro invocation
| ^
error: no rules expected the token `?`
--> $DIR/macro-at-most-once-rep-2018.rs:42:15
|
LL | barplus!(a?); //~ ERROR no rules expected the token `?`
| ^
error: no rules expected the token `?`
--> $DIR/macro-at-most-once-rep-2018.rs:43:15
|
LL | barplus!(a?a); //~ ERROR no rules expected the token `?`
| ^
error: unexpected end of macro invocation
--> $DIR/macro-at-most-once-rep-2018.rs:47:5
|
LL | barstar!(); //~ERROR unexpected end of macro invocation
| ^^^^^^^^^^^
error: unexpected end of macro invocation
--> $DIR/macro-at-most-once-rep-2018.rs:48:14
|
LL | barstar!(a); //~ERROR unexpected end of macro invocation
| ^
error: no rules expected the token `?`
--> $DIR/macro-at-most-once-rep-2018.rs:49:15
|
LL | barstar!(a?); //~ ERROR no rules expected the token `?`
| ^
error: no rules expected the token `?`
--> $DIR/macro-at-most-once-rep-2018.rs:50:15
|
LL | barstar!(a?a); //~ ERROR no rules expected the token `?`
| ^
error: aborting due to 12 previous errors

View file

@ -1,53 +0,0 @@
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// 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 {
($(a)?) => {}
}
macro_rules! baz {
($(a),?) => {} // comma separator is meaningless for `?`
}
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 `?`
foo!(a?); //~ ERROR no rules expected the token `?`
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 `?`
baz!(a,); //~ ERROR unexpected end of macro invocation
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 `?`
barplus!(); //~ ERROR unexpected end of macro invocation
barplus!(a?); //~ ERROR unexpected end of macro invocation
barstar!(a?); //~ ERROR unexpected end of macro invocation
}

View file

@ -1,80 +0,0 @@
error: no rules expected the token `?`
--> $DIR/macro-at-most-once-rep-ambig.rs:40:11
|
LL | foo!(a?a?a); //~ ERROR no rules expected the token `?`
| ^
error: no rules expected the token `?`
--> $DIR/macro-at-most-once-rep-ambig.rs:41:11
|
LL | foo!(a?a); //~ ERROR no rules expected the token `?`
| ^
error: no rules expected the token `?`
--> $DIR/macro-at-most-once-rep-ambig.rs:42:11
|
LL | foo!(a?); //~ ERROR no rules expected the token `?`
| ^
error: no rules expected the token `?`
--> $DIR/macro-at-most-once-rep-ambig.rs:43:11
|
LL | baz!(a?a?a); //~ ERROR no rules expected the token `?`
| ^
error: no rules expected the token `?`
--> $DIR/macro-at-most-once-rep-ambig.rs:44:11
|
LL | baz!(a?a); //~ ERROR no rules expected the token `?`
| ^
error: no rules expected the token `?`
--> $DIR/macro-at-most-once-rep-ambig.rs:45:11
|
LL | baz!(a?); //~ ERROR no rules expected the token `?`
| ^
error: unexpected end of macro invocation
--> $DIR/macro-at-most-once-rep-ambig.rs:46:11
|
LL | baz!(a,); //~ ERROR unexpected end of macro invocation
| ^
error: no rules expected the token `?`
--> $DIR/macro-at-most-once-rep-ambig.rs:47:11
|
LL | baz!(a?a?a,); //~ ERROR no rules expected the token `?`
| ^
error: no rules expected the token `?`
--> $DIR/macro-at-most-once-rep-ambig.rs:48:11
|
LL | baz!(a?a,); //~ ERROR no rules expected the token `?`
| ^
error: no rules expected the token `?`
--> $DIR/macro-at-most-once-rep-ambig.rs:49:11
|
LL | baz!(a?,); //~ ERROR no rules expected the token `?`
| ^
error: unexpected end of macro invocation
--> $DIR/macro-at-most-once-rep-ambig.rs:50:5
|
LL | barplus!(); //~ ERROR unexpected end of macro invocation
| ^^^^^^^^^^^
error: unexpected end of macro invocation
--> $DIR/macro-at-most-once-rep-ambig.rs:51:15
|
LL | barplus!(a?); //~ ERROR unexpected end of macro invocation
| ^
error: unexpected end of macro invocation
--> $DIR/macro-at-most-once-rep-ambig.rs:52:15
|
LL | barstar!(a?); //~ ERROR unexpected end of macro invocation
| ^
error: aborting due to 13 previous errors