Skip to content

Commit

Permalink
Rollup merge of rust-lang#103224 - compiler-errors:semi-after-closure…
Browse files Browse the repository at this point in the history
…-in-macro, r=fee1-dead

Allow semicolon after closure within parentheses in macros

rust-lang#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 rust-lang#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 rust-lang#103222.
  • Loading branch information
Dylan-DPC authored Oct 22, 2022
2 parents 988153c + 3d7b1f0 commit ada5011
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
4 changes: 4 additions & 0 deletions compiler/rustc_parse/src/parser/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2051,6 +2051,10 @@ impl<'a> Parser<'a> {

if self.token.kind == TokenKind::Semi
&& matches!(self.token_cursor.frame.delim_sp, Some((Delimiter::Parenthesis, _)))
// HACK: This is needed so we can detect whether we're inside a macro,
// where regular assumptions about what tokens can follow other tokens
// don't necessarily apply.
&& self.subparser_name.is_none()
{
// It is likely that the closure body is a block but where the
// braces have been removed. We will recover and eat the next
Expand Down
14 changes: 14 additions & 0 deletions src/test/ui/parser/semi-after-closure-in-macro.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// check-pass

// Checks that the fix in #103222 doesn't also disqualify semicolons after
// closures within parentheses *in macros*, where they're totally allowed.

macro_rules! m {
(($expr:expr ; )) => {
$expr
};
}

fn main() {
let x = m!(( ||() ; ));
}

0 comments on commit ada5011

Please sign in to comment.