Parenthesize block-like expressions in call callee of pretty printer

When a macro expands to a call whose callee is a block (or other
"complete" expression like `match`, `if`, `loop`), the AST pretty
printer emits the callee without parentheses. In statement position
the closing brace ends the expression and the argument list is parsed
as a separate tuple expression, producing a parse error.
This commit is contained in:
Andrew V. Teylu
2026-04-08 14:52:01 +01:00
parent 574d8774b9
commit 56f43b5142
3 changed files with 19 additions and 1 deletions
@@ -235,7 +235,15 @@ fn print_expr_call(&mut self, func: &ast::Expr, args: &[Box<ast::Expr>], fixup:
// In order to call a named field, needs parens: `(self.fun)()`
// But not for an unnamed field: `self.0()`
ast::ExprKind::Field(_, name) => !name.is_numeric(),
_ => func_fixup.precedence(func) < ExprPrecedence::Unambiguous,
// Block-like expressions (block, match, if, loop, ...) never
// parse as the callee of a call, regardless of context: the
// closing brace ends the expression and `(args)` becomes a
// separate tuple. Parenthesize them so the call survives a
// pretty-print round trip.
_ => {
func_fixup.precedence(func) < ExprPrecedence::Unambiguous
|| classify::expr_is_complete(func)
}
};
self.print_expr_cond_paren(func, needs_paren, func_fixup);
+4
View File
@@ -10,4 +10,8 @@ macro_rules! block_arr { () => {{ [0u8; 4] }}; }
macro_rules! as_slice { () => {{ &block_arr!()[..] }}; }
macro_rules! group { ($e:expr) => { $e }; }
fn scope() { &({ drop })(0); }
fn main() { let _: &[u8] = { &({ [0u8; 4] })[..] }; }
+6
View File
@@ -9,4 +9,10 @@ macro_rules! as_slice {
() => {{ &block_arr!()[..] }};
}
macro_rules! group {
($e:expr) => { $e };
}
fn scope() { &group!({ drop })(0); }
fn main() { let _: &[u8] = as_slice!(); }