Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions compiler/rustc_ast_pretty/src/pprust/state/expr.rs
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you also fix Call exprs? They exhibit the same behavior. E.g.,

macro_rules! group { ($e:expr) => { $e } }
fn scope() { &group!({ drop })(0); }

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@fmease all done: 56f43b5

Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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("[");
Expand Down
18 changes: 18 additions & 0 deletions tests/pretty/block-index-or-call-paren.rs
Copy link
Copy Markdown
Member

@fmease fmease Apr 8, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

View changes since the review

tests/pretty/block-index-paren.rstests/pretty/block-index-or-call-paren.rs or sth. like that?

Then I'll put it into the queue.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done: 27e3d26

Original file line number Diff line number Diff line change
@@ -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!(); }
17 changes: 17 additions & 0 deletions tests/pretty/block-index-paren.pp
Original file line number Diff line number Diff line change
@@ -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] })[..] }; }
Loading