Skip to content

Insert parentheses around binary operation with attribute #142476

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

dtolnay
Copy link
Member

@dtolnay dtolnay commented Jun 13, 2025

Fixes the bug found by @fmease in #134661 (review).

Previously, -Zunpretty=expanded would expand this program as follows:

#![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);
}
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:

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

@dtolnay dtolnay added A-pretty Area: Pretty printing (including `-Z unpretty`) T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. F-stmt_expr_attributes `#![feature(stmt_expr_attributes)]` labels Jun 13, 2025
@rustbot
Copy link
Collaborator

rustbot commented Jun 13, 2025

fmease is not on the review rotation at the moment.
They may take a while to respond.

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jun 13, 2025
@dtolnay
Copy link
Member Author

dtolnay commented Jun 13, 2025

r? compiler

@rustbot rustbot assigned WaffleLapkin and unassigned fmease Jun 13, 2025
@fmease fmease assigned fmease and unassigned WaffleLapkin Jun 14, 2025
@bors
Copy link
Collaborator

bors commented Jun 16, 2025

☔ The latest upstream changes (presumably #142550) made this pull request unmergeable. Please resolve the merge conflicts.

// We must pretty-print `#[attr] (1 + 1)` not `#[attr] 1 + 1`.
!attrs.is_empty()
&& matches!(
expr.kind,
Copy link
Member

Choose a reason for hiding this comment

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

The explanation you've given in https://github.com/rust-lang/rust/pull/134661/files#r2143009318 makes perfect sense, thanks!

!attrs.is_empty()
&& matches!(
expr.kind,
ast::ExprKind::Binary(..)
Copy link
Member

Choose a reason for hiding this comment

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

It makes me slightly uneasy that we have to "hard code" a collection of expr kinds separately here but rn I don't see a more robust alternative that's only based on (effective) precedence levels.

ast::ExprKind::Binary(..)
| ast::ExprKind::Cast(..)
| ast::ExprKind::Assign(..)
| ast::ExprKind::AssignOp(..)
Copy link
Member

@fmease fmease Jun 20, 2025

Choose a reason for hiding this comment

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

This assortment of expr kinds is lacking ast::Expr::Range. Consider:

macro_rules! attach { ($e:expr) => { #[allow()] $e }
fn main() { _ = attach!(0..1); }

Current output is #[allow()] 0..1 which actually means (#[allow()] 0)..1. So it should be printed like so instead: #[allow()] (0..1).

As for ranges with no lower bound ..expr, ..=expr and .., the same thing applies since apparently, #[attr] ..expr (etc.) is syntactically invalid and requires parens #[attr] (..expr) (etc.).

@fmease
Copy link
Member

fmease commented Jun 20, 2025

Thanks a lot! r=me with Ranges included (with additional tests ofc), squash or don't idc @bors rollup

@fmease fmease added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jun 20, 2025
Comment on lines +101 to +107
// If the attribute were not present on the binary operation, it would be
// legal to render this without not just the inner parentheses, but also the
// outer ones. `return x + .. .field` (Yes, really.) Currently the
// pretty-printer does not take advantage of this edge case.
Copy link
Member

@fmease fmease Jun 20, 2025

Choose a reason for hiding this comment

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

That makes total sense and it's still insane!

Copy link
Member Author

Choose a reason for hiding this comment

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

Fun fact, syn's ToTokens impls as well as prettyplease both implement this edge case, and will print with the minimal inserted parentheses, producing return x + .. .field in this case.

dtolnay added 3 commits June 20, 2025 13:49
This test currently fails (as expected).

    --- stderr -------------------------------
    Pretty-printer lost necessary parentheses
      BEFORE: #[attr] (1 + 1)
       AFTER: #[attr] 1 + 1

    Pretty-printer lost necessary parentheses
      BEFORE: #[attr] (1 as T)
       AFTER: #[attr] 1 as T

    Pretty-printer lost necessary parentheses
      BEFORE: #[attr] (x = 1)
       AFTER: #[attr] x = 1

    Pretty-printer lost necessary parentheses
      BEFORE: #[attr] (x += 1)
       AFTER: #[attr] x += 1
    ------------------------------------------
@dtolnay
Copy link
Member Author

dtolnay commented Jun 20, 2025

@bors r=fmease

@bors
Copy link
Collaborator

bors commented Jun 20, 2025

📌 Commit 535e11f has been approved by fmease

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Jun 20, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-pretty Area: Pretty printing (including `-Z unpretty`) F-stmt_expr_attributes `#![feature(stmt_expr_attributes)]` S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants