Skip to content

Insert parentheses around binary operation with attribute#142476

Merged
bors merged 4 commits intorust-lang:masterfrom
dtolnay:attrbinop
Jun 21, 2025
Merged

Insert parentheses around binary operation with attribute#142476
bors merged 4 commits intorust-lang:masterfrom
dtolnay:attrbinop

Conversation

@dtolnay
Copy link
Copy Markdown
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

Loading
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