diff --git a/src/libsyntax_ext/assert.rs b/src/libsyntax_ext/assert.rs index d2c397e0ecc..f1974ed30f6 100644 --- a/src/libsyntax_ext/assert.rs +++ b/src/libsyntax_ext/assert.rs @@ -74,7 +74,7 @@ fn parse_assert<'a>( return Err(err); } - Ok(Assert { + let assert = Assert { cond_expr: parser.parse_expr()?, custom_message: if parser.eat(&token::Comma) { let ts = parser.parse_tokens(); @@ -86,5 +86,12 @@ fn parse_assert<'a>( } else { None }, - }) + }; + + if parser.token != token::Eof { + parser.expect_one_of(&[], &[])?; + unreachable!(); + } + + Ok(assert) } diff --git a/src/test/ui/macros/assert-trailing-junk.rs b/src/test/ui/macros/assert-trailing-junk.rs new file mode 100644 index 00000000000..66de6de3ee8 --- /dev/null +++ b/src/test/ui/macros/assert-trailing-junk.rs @@ -0,0 +1,14 @@ +// Ensure assert macro does not ignore trailing garbage. +// +// See https://github.com/rust-lang/rust/issues/60024 for details. + +fn main() { + assert!(true some extra junk, "whatever"); + //~^ ERROR expected one of + + assert!(true some extra junk); + //~^ ERROR expected one of + + assert!(true, "whatever" blah); + //~^ ERROR no rules expected +} diff --git a/src/test/ui/macros/assert-trailing-junk.stderr b/src/test/ui/macros/assert-trailing-junk.stderr new file mode 100644 index 00000000000..3f7d8dcaf79 --- /dev/null +++ b/src/test/ui/macros/assert-trailing-junk.stderr @@ -0,0 +1,22 @@ +error: expected one of `,`, `.`, `?`, or an operator, found `some` + --> $DIR/assert-trailing-junk.rs:6:18 + | +LL | assert!(true some extra junk, "whatever"); + | ^^^^ expected one of `,`, `.`, `?`, or an operator here + +error: expected one of `,`, `.`, `?`, or an operator, found `some` + --> $DIR/assert-trailing-junk.rs:9:18 + | +LL | assert!(true some extra junk); + | ^^^^ expected one of `,`, `.`, `?`, or an operator here + +error: no rules expected the token `blah` + --> $DIR/assert-trailing-junk.rs:12:30 + | +LL | assert!(true, "whatever" blah); + | -^^^^ no rules expected this token in macro call + | | + | help: missing comma here + +error: aborting due to 3 previous errors +