Skip to content

Reduce precedence of expressions that have an outer attr #134661

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

Merged
merged 2 commits into from
Jun 16, 2025

Conversation

dtolnay
Copy link
Member

@dtolnay dtolnay commented Dec 22, 2024

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

#![feature(stmt_expr_attributes)]

macro_rules! repro {
    ($e:expr) => {
        #[allow(deprecated)] $e
    };
}

#[derive(Default)]
struct Thing {
    #[deprecated]
    field: i32,
}

fn main() {
    let thing = Thing::default();
    let _ = repro!(thing).field;
}
#![feature(prelude_import)]
#![feature(stmt_expr_attributes)]
#[prelude_import]
use std::prelude::rust_2021::*;
#[macro_use]
extern crate std;

struct Thing {
    #[deprecated]
    field: i32,
}

#[automatically_derived]
impl ::core::default::Default for Thing {
    #[inline]
    fn default() -> Thing {
        Thing { field: ::core::default::Default::default() }
    }
}

fn main() {
    let thing = Thing::default();
    let _ = #[allow(deprecated)] thing.field;
}

This is not the correct expansion. The correct output would have (#[allow(deprecated)] thing).field with the attribute applying only to thing, not to thing.field.

@dtolnay dtolnay added A-pretty Area: Pretty printing (including `-Z unpretty`) F-stmt_expr_attributes `#![feature(stmt_expr_attributes)]` labels Dec 22, 2024
@rustbot
Copy link
Collaborator

rustbot commented Dec 22, 2024

r? @fee1-dead

rustbot has assigned @fee1-dead.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Dec 22, 2024
@rust-log-analyzer

This comment has been minimized.

@rustbot
Copy link
Collaborator

rustbot commented Dec 22, 2024

Some changes occurred in src/tools/clippy

cc @rust-lang/clippy

@fee1-dead
Copy link
Member

r? compiler

@rustbot rustbot assigned fmease and unassigned fee1-dead Dec 23, 2024
@bors
Copy link
Collaborator

bors commented Dec 26, 2024

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

@dtolnay
Copy link
Member Author

dtolnay commented Dec 26, 2024

Rebased to resolve conflict.

@fmease
Copy link
Member

fmease commented Dec 28, 2024

I'm going to look at this in a few hours.

fn prefix_attrs_precedence(attrs: &AttrVec) -> ExprPrecedence {
for attr in attrs {
if let AttrStyle::Outer = attr.style {
return ExprPrecedence::Prefix;
Copy link
Member

@fmease fmease Jan 9, 2025

Choose a reason for hiding this comment

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

I think ExprPrecedence::Prefix isn't low enough. Consider the following cases involving binops:

#![feature(stmt_expr_attributes)]

macro_rules! group { ($e:expr) => { $e } }
macro_rules! extra { ($e:expr) => { #[allow()] $e } }

fn main() {                             // `-Zunpretty=expanded` on master & prefixattr:

    let _ = #[allow()] 1 + 1;           // let _ = #[allow()] 1 + 1;
    let _ = group!(#[allow()] 1) + 1;   // let _ = #[allow()] 1 + 1;
    let _ = 1 + group!(#[allow()] 1);   // let _ = 1 + #[allow()] 1;

    let _ = extra!({ 0 }) + 1;          // let _ = #[allow()] { 0 } + 1;
    let _ = extra!({ 0 } + 1);          // let _ = #[allow()] { 0 } + 1;
}

I haven't given it that much thought yet, so I don't know which specific precedence level(s) would make sense instead.

And indeed, this example is heavily inspired by the ones found in #15701 / #127436. So maybe answering this pretty-printing question is partially blocked by the open design question (meaning we can ignore it for now).

Copy link
Member Author

Choose a reason for hiding this comment

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

I think that bug is orthogonal to this one.

The bug fixed by this PR is about when an outer expression A contains a subexpression B where B contains an attribute, and in the prettyprinter-produced code the attribute ended up applying to parts of A instead of only to B. This is fixed by having A observe a different effective precedence for its subexpression B, making A generate parentheses around the subexpression containing the attribute, i.e. the attribute will end up inside the parentheses.

In contrast, the bug you have identified is about an outer expression A that has an attribute, and a subexpression B that has no attribute. In this case when parentheses are inserted, the attribute will end up outside the parentheses.

I will fix that as well but I expect it will involve unrelated code.

Copy link
Member Author

Choose a reason for hiding this comment

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

Fix: #142476

@bors
Copy link
Collaborator

bors commented Feb 19, 2025

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

@Dylan-DPC Dylan-DPC added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. needs-acp This change is blocked on the author creating an ACP. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. needs-acp This change is blocked on the author creating an ACP. labels Mar 13, 2025
@fmease fmease added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels May 27, 2025
@wesleywiser
Copy link
Member

Hi @fmease, it looks like you marked this as waiting on review recently. Is this actually waiting on review or for the author to respond to your previous review comment?

@dtolnay
Copy link
Member Author

dtolnay commented Jun 13, 2025

@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 14, 2025
@dtolnay
Copy link
Member Author

dtolnay commented Jun 14, 2025

@bors r=fmease

@bors
Copy link
Collaborator

bors commented Jun 14, 2025

📌 Commit 2171f89 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 14, 2025
fmease added a commit to fmease/rust that referenced this pull request Jun 14, 2025
Reduce precedence of expressions that have an outer attr

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

```rust
#![feature(stmt_expr_attributes)]

macro_rules! repro {
    ($e:expr) => {
        #[allow(deprecated)] $e
    };
}

#[derive(Default)]
struct Thing {
    #[deprecated]
    field: i32,
}

fn main() {
    let thing = Thing::default();
    let _ = repro!(thing).field;
}
```

```rs
#![feature(prelude_import)]
#![feature(stmt_expr_attributes)]
#[prelude_import]
use std::prelude::rust_2021::*;
#[macro_use]
extern crate std;

struct Thing {
    #[deprecated]
    field: i32,
}

#[automatically_derived]
impl ::core::default::Default for Thing {
    #[inline]
    fn default() -> Thing {
        Thing { field: ::core::default::Default::default() }
    }
}

fn main() {
    let thing = Thing::default();
    let _ = #[allow(deprecated)] thing.field;
}
```

This is not the correct expansion. The correct output would have `(#[allow(deprecated)] thing).field` with the attribute applying only to `thing`, not to `thing.field`.
bors added a commit that referenced this pull request Jun 15, 2025
Rollup of 10 pull requests

Successful merges:

 - #133952 (Remove wasm legacy abi)
 - #134661 (Reduce precedence of expressions that have an outer attr)
 - #141769 (Move metadata object generation for dylibs to the linker code )
 - #141864 (Handle win32 separator for cygwin paths)
 - #142347 (Async drop - fix for StorageLive/StorageDead codegen for pinned future)
 - #142377 (Try unremapping compiler sources)
 - #142389 (Apply ABI attributes on return types in `rustc_codegen_cranelift`)
 - #142470 (Add some missing mailmap entries)
 - #142481 (Add `f16` inline asm support for LoongArch)
 - #142509 (bump std libc dependency)

r? `@ghost`
`@rustbot` modify labels: rollup
@matthiaskrgr
Copy link
Member

@bors r-
#142522 (comment)

@bors bors 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-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Jun 15, 2025
@dtolnay
Copy link
Member Author

dtolnay commented Jun 15, 2025

I think that is misattributed. That exact same failure also appears in #142533 which does not include this PR.

@bors r=fmease

@bors
Copy link
Collaborator

bors commented Jun 15, 2025

📌 Commit 2171f89 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 15, 2025
fmease added a commit to fmease/rust that referenced this pull request Jun 15, 2025
Reduce precedence of expressions that have an outer attr

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

```rust
#![feature(stmt_expr_attributes)]

macro_rules! repro {
    ($e:expr) => {
        #[allow(deprecated)] $e
    };
}

#[derive(Default)]
struct Thing {
    #[deprecated]
    field: i32,
}

fn main() {
    let thing = Thing::default();
    let _ = repro!(thing).field;
}
```

```rs
#![feature(prelude_import)]
#![feature(stmt_expr_attributes)]
#[prelude_import]
use std::prelude::rust_2021::*;
#[macro_use]
extern crate std;

struct Thing {
    #[deprecated]
    field: i32,
}

#[automatically_derived]
impl ::core::default::Default for Thing {
    #[inline]
    fn default() -> Thing {
        Thing { field: ::core::default::Default::default() }
    }
}

fn main() {
    let thing = Thing::default();
    let _ = #[allow(deprecated)] thing.field;
}
```

This is not the correct expansion. The correct output would have `(#[allow(deprecated)] thing).field` with the attribute applying only to `thing`, not to `thing.field`.
bors added a commit that referenced this pull request Jun 15, 2025
Rollup of 9 pull requests

Successful merges:

 - #133952 (Remove wasm legacy abi)
 - #134661 (Reduce precedence of expressions that have an outer attr)
 - #141769 (Move metadata object generation for dylibs to the linker code )
 - #141864 (Handle win32 separator for cygwin paths)
 - #141937 (Report never type lints in dependencies)
 - #142347 (Async drop - fix for StorageLive/StorageDead codegen for pinned future)
 - #142389 (Apply ABI attributes on return types in `rustc_codegen_cranelift`)
 - #142470 (Add some missing mailmap entries)
 - #142481 (Add `f16` inline asm support for LoongArch)

r? `@ghost`
`@rustbot` modify labels: rollup
bors added a commit that referenced this pull request Jun 16, 2025
Rollup of 10 pull requests

Successful merges:

 - #133952 (Remove wasm legacy abi)
 - #134661 (Reduce precedence of expressions that have an outer attr)
 - #141769 (Move metadata object generation for dylibs to the linker code )
 - #141937 (Report never type lints in dependencies)
 - #142347 (Async drop - fix for StorageLive/StorageDead codegen for pinned future)
 - #142389 (Apply ABI attributes on return types in `rustc_codegen_cranelift`)
 - #142470 (Add some missing mailmap entries)
 - #142481 (Add `f16` inline asm support for LoongArch)
 - #142499 (Remove check run bootstrap)
 - #142543 (Suggest adding semicolon in user code rather than macro impl details)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit b79d3b1 into rust-lang:master Jun 16, 2025
10 checks passed
@rustbot rustbot added this to the 1.89.0 milestone Jun 16, 2025
rust-timer added a commit that referenced this pull request Jun 16, 2025
Rollup merge of #134661 - dtolnay:prefixattr, r=fmease

Reduce precedence of expressions that have an outer attr

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

```rust
#![feature(stmt_expr_attributes)]

macro_rules! repro {
    ($e:expr) => {
        #[allow(deprecated)] $e
    };
}

#[derive(Default)]
struct Thing {
    #[deprecated]
    field: i32,
}

fn main() {
    let thing = Thing::default();
    let _ = repro!(thing).field;
}
```

```rs
#![feature(prelude_import)]
#![feature(stmt_expr_attributes)]
#[prelude_import]
use std::prelude::rust_2021::*;
#[macro_use]
extern crate std;

struct Thing {
    #[deprecated]
    field: i32,
}

#[automatically_derived]
impl ::core::default::Default for Thing {
    #[inline]
    fn default() -> Thing {
        Thing { field: ::core::default::Default::default() }
    }
}

fn main() {
    let thing = Thing::default();
    let _ = #[allow(deprecated)] thing.field;
}
```

This is not the correct expansion. The correct output would have `(#[allow(deprecated)] thing).field` with the attribute applying only to `thing`, not to `thing.field`.
@dtolnay dtolnay deleted the prefixattr branch June 16, 2025 04:20
tgross35 added a commit to tgross35/rust that referenced this pull request Jun 21, 2025
Insert parentheses around binary operation with attribute

Fixes the bug found by `@fmease` in rust-lang#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 rust-lang#127436. But for now, at least this PR makes the pretty-printer align with the current behavior of the parser.

r? fmease
rust-timer added a commit that referenced this pull request Jun 21, 2025
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
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.

9 participants