mirror of
https://github.com/rust-lang/rust.git
synced 2026-04-26 13:01:27 +03:00
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:
@@ -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);
|
||||
|
||||
@@ -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] })[..] }; }
|
||||
|
||||
@@ -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!(); }
|
||||
|
||||
Reference in New Issue
Block a user