Skip to content

Commit b2ed17b

Browse files
committed
No separator for ?. No ? as a separator.
1 parent 56714ac commit b2ed17b

File tree

4 files changed

+70
-154
lines changed

4 files changed

+70
-154
lines changed

src/libsyntax/ext/tt/quoted.rs

+22-67
Original file line numberDiff line numberDiff line change
@@ -389,72 +389,26 @@ where
389389
{
390390
// We basically look at two token trees here, denoted as #1 and #2 below
391391
let span = match parse_kleene_op(input, span) {
392-
// #1 is a `+` or `*` KleeneOp
393-
//
394-
// `?` is ambiguous: it could be a separator or a Kleene::ZeroOrOne, so we need to look
395-
// ahead one more token to be sure.
396-
Ok(Ok(op)) if op != KleeneOp::ZeroOrOne => return (None, op),
397-
398-
// #1 is `?` token, but it could be a Kleene::ZeroOrOne without a separator or it could
399-
// be a `?` separator followed by any Kleene operator. We need to look ahead 1 token to
400-
// find out which.
401-
Ok(Ok(op)) => {
402-
assert_eq!(op, KleeneOp::ZeroOrOne);
403-
404-
// Lookahead at #2. If it is a KleenOp, then #1 is a separator.
405-
let is_1_sep = if let Some(&tokenstream::TokenTree::Token(_, ref tok2)) = input.peek() {
406-
kleene_op(tok2).is_some()
407-
} else {
408-
false
409-
};
410-
411-
if is_1_sep {
412-
// #1 is a separator and #2 should be a KleepeOp::*
413-
// (N.B. We need to advance the input iterator.)
414-
match parse_kleene_op(input, span) {
415-
// #2 is a KleeneOp (this is the only valid option) :)
416-
Ok(Ok(op)) if op == KleeneOp::ZeroOrOne => {
417-
if !features.macro_at_most_once_rep
418-
&& !attr::contains_name(attrs, "allow_internal_unstable")
419-
{
420-
let explain = feature_gate::EXPLAIN_MACRO_AT_MOST_ONCE_REP;
421-
emit_feature_err(
422-
sess,
423-
"macro_at_most_once_rep",
424-
span,
425-
GateIssue::Language,
426-
explain,
427-
);
428-
}
429-
return (Some(token::Question), op);
430-
}
431-
Ok(Ok(op)) => return (Some(token::Question), op),
432-
433-
// #2 is a random token (this is an error) :(
434-
Ok(Err((_, span))) => span,
435-
436-
// #2 is not even a token at all :(
437-
Err(span) => span,
438-
}
439-
} else {
440-
if !features.macro_at_most_once_rep
441-
&& !attr::contains_name(attrs, "allow_internal_unstable")
442-
{
443-
let explain = feature_gate::EXPLAIN_MACRO_AT_MOST_ONCE_REP;
444-
emit_feature_err(
445-
sess,
446-
"macro_at_most_once_rep",
447-
span,
448-
GateIssue::Language,
449-
explain,
450-
);
451-
}
452-
453-
// #2 is a random tree and #1 is KleeneOp::ZeroOrOne
454-
return (None, op);
392+
// #1 is any KleeneOp (`?`)
393+
Ok(Ok(op)) if op == KleeneOp::ZeroOrOne => {
394+
if !features.macro_at_most_once_rep
395+
&& !attr::contains_name(attrs, "allow_internal_unstable")
396+
{
397+
let explain = feature_gate::EXPLAIN_MACRO_AT_MOST_ONCE_REP;
398+
emit_feature_err(
399+
sess,
400+
"macro_at_most_once_rep",
401+
span,
402+
GateIssue::Language,
403+
explain,
404+
);
455405
}
406+
return (None, op);
456407
}
457408

409+
// #1 is any KleeneOp (`+`, `*`)
410+
Ok(Ok(op)) => return (None, op),
411+
458412
// #1 is a separator followed by #2, a KleeneOp
459413
Ok(Err((tok, span))) => match parse_kleene_op(input, span) {
460414
// #2 is a KleeneOp :D
@@ -470,8 +424,11 @@ where
470424
GateIssue::Language,
471425
explain,
472426
);
427+
} else {
428+
sess.span_diagnostic
429+
.span_err(span, "`?` macro repetition does not allow a separator");
473430
}
474-
return (Some(tok), op);
431+
return (None, op);
475432
}
476433
Ok(Ok(op)) => return (Some(tok), op),
477434

@@ -486,9 +443,7 @@ where
486443
Err(span) => span,
487444
};
488445

489-
if !features.macro_at_most_once_rep
490-
&& !attr::contains_name(attrs, "allow_internal_unstable")
491-
{
446+
if !features.macro_at_most_once_rep && !attr::contains_name(attrs, "allow_internal_unstable") {
492447
sess.span_diagnostic
493448
.span_err(span, "expected one of: `*`, `+`, or `?`");
494449
} else {

src/test/run-pass/macro-at-most-once-rep.rs

+6-23
Original file line numberDiff line numberDiff line change
@@ -32,25 +32,13 @@ macro_rules! foo {
3232
} }
3333
}
3434

35-
macro_rules! baz {
36-
($($a:ident),? ; $num:expr) => { { // comma separator is meaningless for `?`
37-
let mut x = 0;
38-
39-
$(
40-
x += $a;
41-
)?
42-
43-
assert_eq!(x, $num);
44-
} }
45-
}
46-
4735
macro_rules! barplus {
4836
($($a:ident)?+ ; $num:expr) => { {
4937
let mut x = 0;
5038

5139
$(
5240
x += $a;
53-
)+
41+
)?
5442

5543
assert_eq!(x, $num);
5644
} }
@@ -62,7 +50,7 @@ macro_rules! barstar {
6250

6351
$(
6452
x += $a;
65-
)*
53+
)?
6654

6755
assert_eq!(x, $num);
6856
} }
@@ -74,15 +62,10 @@ pub fn main() {
7462
// accept 0 or 1 repetitions
7563
foo!( ; 0);
7664
foo!(a ; 1);
77-
baz!( ; 0);
78-
baz!(a ; 1);
7965

8066
// Make sure using ? as a separator works as before
81-
barplus!(a ; 1);
82-
barplus!(a?a ; 2);
83-
barplus!(a?a?a ; 3);
84-
barstar!( ; 0);
85-
barstar!(a ; 1);
86-
barstar!(a?a ; 2);
87-
barstar!(a?a?a ; 3);
67+
barplus!(+ ; 0);
68+
barplus!(a + ; 1);
69+
barstar!(* ; 0);
70+
barstar!(a * ; 1);
8871
}

src/test/ui/macros/macro-at-most-once-rep-ambig.rs

+15-19
Original file line numberDiff line numberDiff line change
@@ -8,30 +8,26 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
// The logic for parsing Kleene operators in macros has a special case to disambiguate `?`.
12-
// Specifically, `$(pat)?` is the ZeroOrOne operator whereas `$(pat)?+` or `$(pat)?*` are the
13-
// ZeroOrMore and OneOrMore operators using `?` as a separator. These tests are intended to
14-
// exercise that logic in the macro parser.
15-
//
16-
// Moreover, we also throw in some tests for using a separator with `?`, which is meaningless but
17-
// included for consistency with `+` and `*`.
18-
//
19-
// This test focuses on error cases.
11+
// Tests the behavior of various Kleene operators in macros with respect to `?` terminals. In
12+
// particular, `?` in the position of a separator and of a Kleene operator is tested.
2013

2114
#![feature(macro_at_most_once_rep)]
2215

16+
// should match `` and `a`
2317
macro_rules! foo {
2418
($(a)?) => {}
2519
}
2620

2721
macro_rules! baz {
28-
($(a),?) => {} // comma separator is meaningless for `?`
22+
($(a),?) => {} //~ ERROR `?` macro repetition does not allow a separator
2923
}
3024

25+
// should match `+` and `a+`
3126
macro_rules! barplus {
3227
($(a)?+) => {}
3328
}
3429

30+
// should match `*` and `a*`
3531
macro_rules! barstar {
3632
($(a)?*) => {}
3733
}
@@ -40,14 +36,14 @@ pub fn main() {
4036
foo!(a?a?a); //~ ERROR no rules expected the token `?`
4137
foo!(a?a); //~ ERROR no rules expected the token `?`
4238
foo!(a?); //~ ERROR no rules expected the token `?`
43-
baz!(a?a?a); //~ ERROR no rules expected the token `?`
44-
baz!(a?a); //~ ERROR no rules expected the token `?`
45-
baz!(a?); //~ ERROR no rules expected the token `?`
46-
baz!(a,); //~ ERROR unexpected end of macro invocation
47-
baz!(a?a?a,); //~ ERROR no rules expected the token `?`
48-
baz!(a?a,); //~ ERROR no rules expected the token `?`
49-
baz!(a?,); //~ ERROR no rules expected the token `?`
5039
barplus!(); //~ ERROR unexpected end of macro invocation
51-
barplus!(a?); //~ ERROR unexpected end of macro invocation
52-
barstar!(a?); //~ ERROR unexpected end of macro invocation
40+
barstar!(); //~ ERROR unexpected end of macro invocation
41+
barplus!(a?); //~ ERROR no rules expected the token `?`
42+
barplus!(a); //~ ERROR unexpected end of macro invocation
43+
barstar!(a?); //~ ERROR no rules expected the token `?`
44+
barstar!(a); //~ ERROR unexpected end of macro invocation
45+
barplus!(+); // ok
46+
barstar!(*); // ok
47+
barplus!(a); // ok
48+
barstar!(a*); // ok
5349
}
Original file line numberDiff line numberDiff line change
@@ -1,80 +1,62 @@
1-
error: no rules expected the token `?`
2-
--> $DIR/macro-at-most-once-rep-ambig.rs:40:11
1+
error: `?` macro repetition does not allow a separator
2+
--> $DIR/macro-at-most-once-rep-ambig.rs:29:10
33
|
4-
LL | foo!(a?a?a); //~ ERROR no rules expected the token `?`
5-
| ^
6-
7-
error: no rules expected the token `?`
8-
--> $DIR/macro-at-most-once-rep-ambig.rs:41:11
9-
|
10-
LL | foo!(a?a); //~ ERROR no rules expected the token `?`
11-
| ^
12-
13-
error: no rules expected the token `?`
14-
--> $DIR/macro-at-most-once-rep-ambig.rs:42:11
15-
|
16-
LL | foo!(a?); //~ ERROR no rules expected the token `?`
17-
| ^
4+
LL | ($(a),?) => {} //~ ERROR `?` macro repetition does not allow a separator
5+
| ^
186

197
error: no rules expected the token `?`
208
--> $DIR/macro-at-most-once-rep-ambig.rs:43:11
219
|
22-
LL | baz!(a?a?a); //~ ERROR no rules expected the token `?`
10+
LL | foo!(a?a?a); //~ ERROR no rules expected the token `?`
2311
| ^
2412

2513
error: no rules expected the token `?`
2614
--> $DIR/macro-at-most-once-rep-ambig.rs:44:11
2715
|
28-
LL | baz!(a?a); //~ ERROR no rules expected the token `?`
16+
LL | foo!(a?a); //~ ERROR no rules expected the token `?`
2917
| ^
3018

3119
error: no rules expected the token `?`
3220
--> $DIR/macro-at-most-once-rep-ambig.rs:45:11
3321
|
34-
LL | baz!(a?); //~ ERROR no rules expected the token `?`
22+
LL | foo!(a?); //~ ERROR no rules expected the token `?`
3523
| ^
3624

3725
error: unexpected end of macro invocation
38-
--> $DIR/macro-at-most-once-rep-ambig.rs:46:11
39-
|
40-
LL | baz!(a,); //~ ERROR unexpected end of macro invocation
41-
| ^
42-
43-
error: no rules expected the token `?`
44-
--> $DIR/macro-at-most-once-rep-ambig.rs:47:11
26+
--> $DIR/macro-at-most-once-rep-ambig.rs:46:5
4527
|
46-
LL | baz!(a?a?a,); //~ ERROR no rules expected the token `?`
47-
| ^
28+
LL | barplus!(); //~ ERROR unexpected end of macro invocation
29+
| ^^^^^^^^^^^
4830

49-
error: no rules expected the token `?`
50-
--> $DIR/macro-at-most-once-rep-ambig.rs:48:11
31+
error: unexpected end of macro invocation
32+
--> $DIR/macro-at-most-once-rep-ambig.rs:47:5
5133
|
52-
LL | baz!(a?a,); //~ ERROR no rules expected the token `?`
53-
| ^
34+
LL | barstar!(); //~ ERROR unexpected end of macro invocation
35+
| ^^^^^^^^^^^
5436

5537
error: no rules expected the token `?`
56-
--> $DIR/macro-at-most-once-rep-ambig.rs:49:11
38+
--> $DIR/macro-at-most-once-rep-ambig.rs:48:15
5739
|
58-
LL | baz!(a?,); //~ ERROR no rules expected the token `?`
59-
| ^
40+
LL | barplus!(a?); //~ ERROR no rules expected the token `?`
41+
| ^
6042

6143
error: unexpected end of macro invocation
62-
--> $DIR/macro-at-most-once-rep-ambig.rs:50:5
44+
--> $DIR/macro-at-most-once-rep-ambig.rs:49:14
6345
|
64-
LL | barplus!(); //~ ERROR unexpected end of macro invocation
65-
| ^^^^^^^^^^^
46+
LL | barplus!(a); //~ ERROR unexpected end of macro invocation
47+
| ^
6648

67-
error: unexpected end of macro invocation
68-
--> $DIR/macro-at-most-once-rep-ambig.rs:51:15
49+
error: no rules expected the token `?`
50+
--> $DIR/macro-at-most-once-rep-ambig.rs:50:15
6951
|
70-
LL | barplus!(a?); //~ ERROR unexpected end of macro invocation
52+
LL | barstar!(a?); //~ ERROR no rules expected the token `?`
7153
| ^
7254

7355
error: unexpected end of macro invocation
74-
--> $DIR/macro-at-most-once-rep-ambig.rs:52:15
56+
--> $DIR/macro-at-most-once-rep-ambig.rs:51:14
7557
|
76-
LL | barstar!(a?); //~ ERROR unexpected end of macro invocation
77-
| ^
58+
LL | barstar!(a); //~ ERROR unexpected end of macro invocation
59+
| ^
7860

79-
error: aborting due to 13 previous errors
61+
error: aborting due to 10 previous errors
8062

0 commit comments

Comments
 (0)