Skip to content

Commit ada5011

Browse files
authored
Rollup merge of #103224 - compiler-errors:semi-after-closure-in-macro, r=fee1-dead
Allow semicolon after closure within parentheses in macros #88546 added some parsing logic that if we're parsing a closure, and we're within parentheses, and a semicolon follows, then we must be parsing something erroneous like: `f(|| a; b)`, so it replaces the closure body with an error expression. However, it's valid to parse those tokens if we're within a macro, as in #103222. This is a bit unsatisfying fix. Is there a more robust way of checking that we're within a macro? I would also be open to removing this "_It is likely that the closure body is a block but where the braces have been removed_" check altogether at the expense of more verbose errors, since it seems very suspicious in the first place... Fixes #103222.
2 parents 988153c + 3d7b1f0 commit ada5011

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

compiler/rustc_parse/src/parser/expr.rs

+4
Original file line numberDiff line numberDiff line change
@@ -2051,6 +2051,10 @@ impl<'a> Parser<'a> {
20512051

20522052
if self.token.kind == TokenKind::Semi
20532053
&& matches!(self.token_cursor.frame.delim_sp, Some((Delimiter::Parenthesis, _)))
2054+
// HACK: This is needed so we can detect whether we're inside a macro,
2055+
// where regular assumptions about what tokens can follow other tokens
2056+
// don't necessarily apply.
2057+
&& self.subparser_name.is_none()
20542058
{
20552059
// It is likely that the closure body is a block but where the
20562060
// braces have been removed. We will recover and eat the next
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// check-pass
2+
3+
// Checks that the fix in #103222 doesn't also disqualify semicolons after
4+
// closures within parentheses *in macros*, where they're totally allowed.
5+
6+
macro_rules! m {
7+
(($expr:expr ; )) => {
8+
$expr
9+
};
10+
}
11+
12+
fn main() {
13+
let x = m!(( ||() ; ));
14+
}

0 commit comments

Comments
 (0)