Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Comments

Reduce precedence of expressions that have an outer attr#134661

Merged
bors merged 2 commits intorust-lang:masterfrom
dtolnay:prefixattr
Jun 16, 2025
Merged

Reduce precedence of expressions that have an outer attr#134661
bors merged 2 commits intorust-lang:masterfrom
dtolnay:prefixattr

Conversation

@dtolnay
Copy link
Member

@dtolnaydtolnay commentedDec 22, 2024
edited by fmease
Loading

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

#![feature(stmt_expr_attributes)]macro_rules! repro{($e:expr) =>{        #[allow(deprecated)] $e};}#[derive(Default)]structThing{#[deprecated]field:i32,}fnmain(){let thing =Thing::default();let _ =repro!(thing).field;}
#![feature(prelude_import)]#![feature(stmt_expr_attributes)]#[prelude_import]use std::prelude::rust_2021::*;#[macro_use]externcrate std;structThing{#[deprecated]field:i32,}#[automatically_derived]impl::core::default::DefaultforThing{#[inline]fndefault() ->Thing{Thing{field:::core::default::Default::default()}}}fnmain(){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 tothing, not tothing.field.

@dtolnaydtolnay added A-prettyArea: Pretty printing (including `-Z unpretty`) F-stmt_expr_attributes`#![feature(stmt_expr_attributes)]` labelsDec 22, 2024
@rustbot
Copy link
Collaborator

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.

User? to explicitly pick a reviewer

@rustbotrustbot added S-waiting-on-reviewStatus: Awaiting review from the assignee but also interested parties. T-compilerRelevant to the compiler team, which will review and decide on the PR/issue. labelsDec 22, 2024
@rust-log-analyzer

This comment has been minimized.

@rustbot
Copy link
Collaborator

Some changes occurred in src/tools/clippy

cc @rust-lang/clippy

@fee1-dead
Copy link
Member

r? compiler

@rustbotrustbot assignedfmease and unassignedfee1-deadDec 23, 2024
@bors
Copy link
Collaborator

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

@dtolnay
Copy link
MemberAuthor

Rebased to resolve conflict.

@fmease
Copy link
Member

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

@fmeasefmeaseJan 9, 2025
edited
Loading

Choose a reason for hiding this comment

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

I thinkExprPrecedence::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}}fnmain(){// `-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
MemberAuthor

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 upinside 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 upoutside the parentheses.

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

fmease reacted with thumbs up emoji
Copy link
MemberAuthor

Choose a reason for hiding this comment

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

@bors
Copy link
Collaborator

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

@Dylan-DPCDylan-DPC added S-waiting-on-authorStatus: This is awaiting some action (such as code changes or more information) from the author. needs-acpThis change is blocked on the author creating an ACP. and removed S-waiting-on-reviewStatus: Awaiting review from the assignee but also interested parties. needs-acpThis change is blocked on the author creating an ACP. labelsMar 13, 2025
@fmeasefmease added S-waiting-on-reviewStatus: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-authorStatus: This is awaiting some action (such as code changes or more information) from the author. labelsMay 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
MemberAuthor

@dtolnay
Copy link
MemberAuthor

@bors r=fmease

@bors
Copy link
Collaborator

📌 Commit2171f89 has been approved byfmease

It is now in thequeue for this repository.

@borsbors added S-waiting-on-borsStatus: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-authorStatus: This is awaiting some action (such as code changes or more information) from the author. labelsJun 14, 2025
fmease added a commit to fmease/rust that referenced this pull requestJun 14, 2025
Reduce precedence of expressions that have an outer attrPreviously, `-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`.
@fmeasefmease mentioned this pull requestJun 14, 2025
bors added a commit that referenced this pull requestJun 15, 2025
Rollup of 10 pull requestsSuccessful 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)

@borsbors added S-waiting-on-authorStatus: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-borsStatus: Waiting on bors to run and complete tests. Bors will change the label on completion. labelsJun 15, 2025
@dtolnay
Copy link
MemberAuthor

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

📌 Commit2171f89 has been approved byfmease

It is now in thequeue for this repository.

@borsbors added S-waiting-on-borsStatus: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-authorStatus: This is awaiting some action (such as code changes or more information) from the author. labelsJun 15, 2025
fmease added a commit to fmease/rust that referenced this pull requestJun 15, 2025
Reduce precedence of expressions that have an outer attrPreviously, `-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`.
@fmeasefmease mentioned this pull requestJun 15, 2025
bors added a commit that referenced this pull requestJun 15, 2025
Rollup of 9 pull requestsSuccessful 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
@fmeasefmease mentioned this pull requestJun 15, 2025
bors added a commit that referenced this pull requestJun 16, 2025
Rollup of 10 pull requestsSuccessful 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
@borsbors merged commitb79d3b1 intorust-lang:masterJun 16, 2025
10 checks passed
@rustbotrustbot added this to the1.89.0 milestoneJun 16, 2025
rust-timer added a commit that referenced this pull requestJun 16, 2025
Rollup merge of#134661 - dtolnay:prefixattr, r=fmeaseReduce precedence of expressions that have an outer attrPreviously, `-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`.
@dtolnaydtolnay deleted the prefixattr branchJune 16, 2025 04:20
tgross35 added a commit to tgross35/rust that referenced this pull requestJun 21, 2025
Insert parentheses around binary operation with attributeFixes the bug found by `@fmease` inrust-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);}``````consolelet _ = #[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:```consolelet _ = #[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 ofrust-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 requestJun 21, 2025
Rollup merge of#142476 - dtolnay:attrbinop, r=fmeaseInsert parentheses around binary operation with attributeFixes 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);}``````consolelet _ = #[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:```consolelet _ = #[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
flip1995 pushed a commit to flip1995/rust that referenced this pull requestJun 26, 2025
Reduce precedence of expressions that have an outer attrPreviously, `-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`.
christian-schilling pushed a commit to christian-schilling/rustc_codegen_cranelift that referenced this pull requestJan 27, 2026
Rollup of 10 pull requestsSuccessful merges: -rust-lang/rust#133952 (Remove wasm legacy abi) -rust-lang/rust#134661 (Reduce precedence of expressions that have an outer attr) -rust-lang/rust#141769 (Move metadata object generation for dylibs to the linker code ) -rust-lang/rust#141937 (Report never type lints in dependencies) -rust-lang/rust#142347 (Async drop - fix for StorageLive/StorageDead codegen for pinned future) -rust-lang/rust#142389 (Apply ABI attributes on return types in `rustc_codegen_cranelift`) -rust-lang/rust#142470 (Add some missing mailmap entries) -rust-lang/rust#142481 (Add `f16` inline asm support for LoongArch) -rust-lang/rust#142499 (Remove check run bootstrap) -rust-lang/rust#142543 (Suggest adding semicolon in user code rather than macro impl details)r? `@ghost``@rustbot` modify labels: rollup
christian-schilling pushed a commit to christian-schilling/rustc_codegen_cranelift that referenced this pull requestJan 27, 2026
Rollup of 10 pull requestsSuccessful merges: -rust-lang/rust#133952 (Remove wasm legacy abi) -rust-lang/rust#134661 (Reduce precedence of expressions that have an outer attr) -rust-lang/rust#141769 (Move metadata object generation for dylibs to the linker code ) -rust-lang/rust#141937 (Report never type lints in dependencies) -rust-lang/rust#142347 (Async drop - fix for StorageLive/StorageDead codegen for pinned future) -rust-lang/rust#142389 (Apply ABI attributes on return types in `rustc_codegen_cranelift`) -rust-lang/rust#142470 (Add some missing mailmap entries) -rust-lang/rust#142481 (Add `f16` inline asm support for LoongArch) -rust-lang/rust#142499 (Remove check run bootstrap) -rust-lang/rust#142543 (Suggest adding semicolon in user code rather than macro impl details)r? `@ghost``@rustbot` modify labels: rollup
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@fmeasefmeasefmease approved these changes

Assignees

@fmeasefmease

Labels

A-prettyArea: Pretty printing (including `-Z unpretty`)F-stmt_expr_attributes`#![feature(stmt_expr_attributes)]`S-waiting-on-borsStatus: Waiting on bors to run and complete tests. Bors will change the label on completion.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Projects

None yet

Milestone

1.89.0

Development

Successfully merging this pull request may close these issues.

9 participants

@dtolnay@rustbot@rust-log-analyzer@fee1-dead@bors@fmease@wesleywiser@matthiaskrgr@Dylan-DPC

[8]ページ先頭

©2009-2026 Movatter.jp