Skip to content

Commit 8751016

Browse files
authored
Rollup merge of #142476 - dtolnay:attrbinop, r=fmease
Insert parentheses around binary operation with attribute Fixes the bug found by `@fmease` in #134661 (review). Previously, `-Zunpretty=expanded` would expand this program as follows: ```rust #![feature(stmt_expr_attributes)] #![allow(unused_attributes)] macro_rules! group { ($e:expr) => { $e }; } macro_rules! extra { ($e:expr) => { #[allow()] $e }; } fn main() { let _ = #[allow()] 1 + 1; let _ = group!(#[allow()] 1) + 1; let _ = 1 + group!(#[allow()] 1); let _ = extra!({ 0 }) + 1; let _ = extra!({ 0 } + 1); } ``` ```console let _ = #[allow()] 1 + 1; let _ = #[allow()] 1 + 1; let _ = 1 + #[allow()] 1; let _ = #[allow()] { 0 } + 1; let _ = #[allow()] { 0 } + 1; ``` The first 4 statements are the correct expansion, but the last one is not. The attribute is supposed to apply to the entire binary operation, not only to the left operand. After this PR, the 5th statement will expand to: ```console let _ = #[allow()] ({ 0 } + 1); ``` In the future, as some subset of `stmt_expr_attributes` approaches stabilization, it is possible that we will need to do parenthesization for a number of additional cases depending on the outcome of #127436. But for now, at least this PR makes the pretty-printer align with the current behavior of the parser. r? fmease
2 parents 7b35511 + 535e11f commit 8751016

File tree

2 files changed

+58
-12
lines changed

2 files changed

+58
-12
lines changed

compiler/rustc_ast_pretty/src/pprust/state/expr.rs

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -386,18 +386,44 @@ impl<'a> State<'a> {
386386

387387
let ib = self.ibox(INDENT_UNIT);
388388

389-
// The Match subexpression in `match x {} - 1` must be parenthesized if
390-
// it is the leftmost subexpression in a statement:
391-
//
392-
// (match x {}) - 1;
393-
//
394-
// But not otherwise:
395-
//
396-
// let _ = match x {} - 1;
397-
//
398-
// Same applies to a small set of other expression kinds which eagerly
399-
// terminate a statement which opens with them.
400-
let needs_par = fixup.would_cause_statement_boundary(expr);
389+
let needs_par = {
390+
// The Match subexpression in `match x {} - 1` must be parenthesized
391+
// if it is the leftmost subexpression in a statement:
392+
//
393+
// (match x {}) - 1;
394+
//
395+
// But not otherwise:
396+
//
397+
// let _ = match x {} - 1;
398+
//
399+
// Same applies to a small set of other expression kinds which
400+
// eagerly terminate a statement which opens with them.
401+
fixup.would_cause_statement_boundary(expr)
402+
} || {
403+
// If a binary operation ends up with an attribute, such as
404+
// resulting from the following macro expansion, then parentheses
405+
// are required so that the attribute encompasses the right
406+
// subexpression and not just the left one.
407+
//
408+
// #![feature(stmt_expr_attributes)]
409+
//
410+
// macro_rules! add_attr {
411+
// ($e:expr) => { #[attr] $e };
412+
// }
413+
//
414+
// let _ = add_attr!(1 + 1);
415+
//
416+
// We must pretty-print `#[attr] (1 + 1)` not `#[attr] 1 + 1`.
417+
!attrs.is_empty()
418+
&& matches!(
419+
expr.kind,
420+
ast::ExprKind::Binary(..)
421+
| ast::ExprKind::Cast(..)
422+
| ast::ExprKind::Assign(..)
423+
| ast::ExprKind::AssignOp(..)
424+
| ast::ExprKind::Range(..)
425+
)
426+
};
401427
if needs_par {
402428
self.popen();
403429
fixup = FixupContext::default();

tests/ui-fulldeps/pprust-parenthesis-insertion.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,21 @@ static EXPRS: &[&str] = &[
9292
"#[attr] loop {}.field",
9393
"(#[attr] loop {}).field",
9494
"loop { #![attr] }.field",
95+
// Attributes on a Binary, Cast, Assign, AssignOp, and Range expression
96+
// require parentheses. Without parentheses `#[attr] lo..hi` means
97+
// `(#[attr] lo)..hi`, and `#[attr] ..hi` is invalid syntax.
98+
"#[attr] (1 + 1)",
99+
"#[attr] (1 as T)",
100+
"#[attr] (x = 1)",
101+
"#[attr] (x += 1)",
102+
"#[attr] (lo..hi)",
103+
"#[attr] (..hi)",
104+
// If the attribute were not present on the binary operation, it would be
105+
// legal to render this without not just the inner parentheses, but also the
106+
// outer ones. `return x + .. .field` (Yes, really.) Currently the
107+
// pretty-printer does not take advantage of this edge case.
108+
"(return #[attr] (x + ..)).field",
109+
"(return x + ..).field",
95110
// Grammar restriction: break value starting with a labeled loop is not
96111
// allowed, except if the break is also labeled.
97112
"break 'outer 'inner: loop {} + 2",
@@ -158,7 +173,12 @@ struct Unparenthesize;
158173
impl MutVisitor for Unparenthesize {
159174
fn visit_expr(&mut self, e: &mut Expr) {
160175
while let ExprKind::Paren(paren) = &mut e.kind {
176+
let paren_attrs = mem::take(&mut e.attrs);
161177
*e = mem::replace(paren, Expr::dummy());
178+
if !paren_attrs.is_empty() {
179+
assert!(e.attrs.is_empty());
180+
e.attrs = paren_attrs;
181+
}
162182
}
163183
mut_visit::walk_expr(self, e);
164184
}

0 commit comments

Comments
 (0)