Skip to content

Commit 6395dc2

Browse files
committed
[RFC-3086] Restrict the parsing of count
1 parent d4a881e commit 6395dc2

File tree

3 files changed

+44
-1
lines changed

3 files changed

+44
-1
lines changed

Diff for: compiler/rustc_expand/src/mbe/metavar_expr.rs

+11-1
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,17 @@ fn parse_count<'sess>(
9393
span: Span,
9494
) -> PResult<'sess, MetaVarExpr> {
9595
let ident = parse_ident(iter, sess, span)?;
96-
let depth = if try_eat_comma(iter) { Some(parse_depth(iter, sess, span)?) } else { None };
96+
let depth = if try_eat_comma(iter) {
97+
if iter.look_ahead(0).is_none() {
98+
return Err(sess.span_diagnostic.struct_span_err(
99+
span,
100+
"`count` followed by a comma must have an associated index indicating its depth",
101+
));
102+
}
103+
Some(parse_depth(iter, sess, span)?)
104+
} else {
105+
None
106+
};
97107
Ok(MetaVarExpr::Count(ident, depth))
98108
}
99109

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#![feature(macro_metavar_expr)]
2+
3+
macro_rules! foo {
4+
( $( $($t:ident),* );* ) => { ${count(t,)} }
5+
//~^ ERROR `count` followed by a comma must have an associated
6+
//~| ERROR expected expression, found `$`
7+
}
8+
9+
fn test() {
10+
foo!(a, a; b, b);
11+
}
12+
13+
fn main() {
14+
}
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
error: `count` followed by a comma must have an associated index indicating its depth
2+
--> $DIR/issue-111904.rs:4:37
3+
|
4+
LL | ( $( $($t:ident),* );* ) => { ${count(t,)} }
5+
| ^^^^^
6+
7+
error: expected expression, found `$`
8+
--> $DIR/issue-111904.rs:4:35
9+
|
10+
LL | ( $( $($t:ident),* );* ) => { ${count(t,)} }
11+
| ^ expected expression
12+
...
13+
LL | foo!(a, a; b, b);
14+
| ---------------- in this macro invocation
15+
|
16+
= note: this error originates in the macro `foo` (in Nightly builds, run with -Z macro-backtrace for more info)
17+
18+
error: aborting due to 2 previous errors
19+

0 commit comments

Comments
 (0)