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

[RFC-3086] Restrict the parsing of count #111908

Merged
merged 1 commit into from
Aug 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
12 changes: 11 additions & 1 deletion compiler/rustc_expand/src/mbe/metavar_expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,17 @@ fn parse_count<'sess>(
span: Span,
) -> PResult<'sess, MetaVarExpr> {
let ident = parse_ident(iter, sess, span)?;
let depth = if try_eat_comma(iter) { Some(parse_depth(iter, sess, span)?) } else { None };
let depth = if try_eat_comma(iter) {
if iter.look_ahead(0).is_none() {
return Err(sess.span_diagnostic.struct_span_err(
span,
"`count` followed by a comma must have an associated index indicating its depth",
));
}
Some(parse_depth(iter, sess, span)?)
} else {
None
};
Ok(MetaVarExpr::Count(ident, depth))
}

Expand Down
14 changes: 14 additions & 0 deletions tests/ui/macros/rfc-3086-metavar-expr/issue-111904.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#![feature(macro_metavar_expr)]

macro_rules! foo {
( $( $($t:ident),* );* ) => { ${count(t,)} }
//~^ ERROR `count` followed by a comma must have an associated
//~| ERROR expected expression, found `$`
}

fn test() {
foo!(a, a; b, b);
}

fn main() {
}
19 changes: 19 additions & 0 deletions tests/ui/macros/rfc-3086-metavar-expr/issue-111904.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
error: `count` followed by a comma must have an associated index indicating its depth
--> $DIR/issue-111904.rs:4:37
|
LL | ( $( $($t:ident),* );* ) => { ${count(t,)} }
| ^^^^^

error: expected expression, found `$`
--> $DIR/issue-111904.rs:4:35
|
LL | ( $( $($t:ident),* );* ) => { ${count(t,)} }
| ^ expected expression
...
LL | foo!(a, a; b, b);
| ---------------- in this macro invocation
|
= note: this error originates in the macro `foo` (in Nightly builds, run with -Z macro-backtrace for more info)

error: aborting due to 2 previous errors