Skip to content

Commit dfc0861

Browse files
committed
Make assert! ensure the macro is parsed completely
1 parent 258e3b3 commit dfc0861

File tree

3 files changed

+45
-2
lines changed

3 files changed

+45
-2
lines changed

src/libsyntax_ext/assert.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ fn parse_assert<'a>(
7474
return Err(err);
7575
}
7676

77-
Ok(Assert {
77+
let assert = Assert {
7878
cond_expr: parser.parse_expr()?,
7979
custom_message: if parser.eat(&token::Comma) {
8080
let ts = parser.parse_tokens();
@@ -86,5 +86,12 @@ fn parse_assert<'a>(
8686
} else {
8787
None
8888
},
89-
})
89+
};
90+
91+
if parser.token != token::Eof {
92+
parser.expect_one_of(&[], &[])?;
93+
unreachable!();
94+
}
95+
96+
Ok(assert)
9097
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Ensure assert macro does not ignore trailing garbage.
2+
//
3+
// See https://github.com/rust-lang/rust/issues/60024 for details.
4+
5+
fn main() {
6+
assert!(true some extra junk, "whatever");
7+
//~^ ERROR expected one of
8+
9+
assert!(true some extra junk);
10+
//~^ ERROR expected one of
11+
12+
assert!(true, "whatever" blah);
13+
//~^ ERROR no rules expected
14+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
error: expected one of `,`, `.`, `?`, or an operator, found `some`
2+
--> $DIR/assert-trailing-junk.rs:6:18
3+
|
4+
LL | assert!(true some extra junk, "whatever");
5+
| ^^^^ expected one of `,`, `.`, `?`, or an operator here
6+
7+
error: expected one of `,`, `.`, `?`, or an operator, found `some`
8+
--> $DIR/assert-trailing-junk.rs:9:18
9+
|
10+
LL | assert!(true some extra junk);
11+
| ^^^^ expected one of `,`, `.`, `?`, or an operator here
12+
13+
error: no rules expected the token `blah`
14+
--> $DIR/assert-trailing-junk.rs:12:30
15+
|
16+
LL | assert!(true, "whatever" blah);
17+
| -^^^^ no rules expected this token in macro call
18+
| |
19+
| help: missing comma here
20+
21+
error: aborting due to 3 previous errors
22+

0 commit comments

Comments
 (0)