diff --git a/compiler/rustc_ast_pretty/src/pprust/state/expr.rs b/compiler/rustc_ast_pretty/src/pprust/state/expr.rs index ad602d5196dc1..701152e9f9529 100644 --- a/compiler/rustc_ast_pretty/src/pprust/state/expr.rs +++ b/compiler/rustc_ast_pretty/src/pprust/state/expr.rs @@ -235,7 +235,15 @@ impl<'a> State<'a> { // 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); @@ -677,7 +685,8 @@ impl<'a> State<'a> { let expr_fixup = fixup.leftmost_subexpression_with_operator(true); self.print_expr_cond_paren( expr, - expr_fixup.precedence(expr) < ExprPrecedence::Unambiguous, + expr_fixup.precedence(expr) < ExprPrecedence::Unambiguous + || classify::expr_is_complete(expr), expr_fixup, ); self.word("["); diff --git a/tests/pretty/block-index-or-call-paren.rs b/tests/pretty/block-index-or-call-paren.rs new file mode 100644 index 0000000000000..71e1cdff7b795 --- /dev/null +++ b/tests/pretty/block-index-or-call-paren.rs @@ -0,0 +1,18 @@ +//@ pretty-mode:expanded +//@ pp-exact:block-index-paren.pp + +macro_rules! block_arr { + () => {{ [0u8; 4] }}; +} + +macro_rules! as_slice { + () => {{ &block_arr!()[..] }}; +} + +macro_rules! group { + ($e:expr) => { $e }; +} + +fn scope() { &group!({ drop })(0); } + +fn main() { let _: &[u8] = as_slice!(); } diff --git a/tests/pretty/block-index-paren.pp b/tests/pretty/block-index-paren.pp new file mode 100644 index 0000000000000..e5ad92be082f5 --- /dev/null +++ b/tests/pretty/block-index-paren.pp @@ -0,0 +1,17 @@ +#![feature(prelude_import)] +#![no_std] +extern crate std; +#[prelude_import] +use ::std::prelude::rust_2015::*; +//@ pretty-mode:expanded +//@ pp-exact:block-index-paren.pp + +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] })[..] }; }