Skip to content

expanding #[rustc_trivial_field_reads] to more targets, place on Derive and Clone - #160666

Draft
josetorrs wants to merge 22 commits into
rust-lang:mainfrom
josetorrs:move-trivial-reads-to-macros
Draft

expanding #[rustc_trivial_field_reads] to more targets, place on Derive and Clone#160666
josetorrs wants to merge 22 commits into
rust-lang:mainfrom
josetorrs:move-trivial-reads-to-macros

Conversation

@josetorrs

@josetorrs josetorrs commented Aug 7, 2026

Copy link
Copy Markdown
Contributor

View all comments

part of #160621.

also minor discussion in:

r? @mejrs

@rustbot rustbot added A-attributes Area: Attributes (`#[…]`, `#![…]`) S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Aug 7, 2026
Comment thread compiler/rustc_passes/src/dead.rs Outdated
Comment on lines +441 to +442
&& let Some(macro_def_id) = impl_item.span.ctxt().outer_expn_data().macro_def_id
&& find_attr!(self.tcx, macro_def_id, RustcTrivialFieldReads)

@josetorrs josetorrs Aug 7, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

TBH not really sure about this but my computer has been really slow so wanted to see what CI would do

View changes since the review

@rust-log-analyzer

This comment has been minimized.

@mejrs

mejrs commented Aug 8, 2026

Copy link
Copy Markdown
Member

The changes look correct so far, you just need to delete the #[rustc_trivial_field_reads] in the minicore.rs file.

@mejrs

mejrs commented Aug 8, 2026

Copy link
Copy Markdown
Member

Let's see whether this affects perf so far.

@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Aug 8, 2026
@rust-bors

This comment has been minimized.

rust-bors Bot pushed a commit that referenced this pull request Aug 8, 2026
[WIP] - moving trivial field reads attr to macros
@rust-bors

rust-bors Bot commented Aug 8, 2026

Copy link
Copy Markdown
Contributor

☀️ Try build successful (CI)
Build commit: af61126 (af61126db7562afad7eb31247e4ba2d8968c4766)
Base parent: 8b798d4 (8b798d41cc5d215ef8d5424f61b6dc2575ac738b)

@rust-timer

This comment has been minimized.

@rust-timer

Copy link
Copy Markdown
Collaborator

Finished benchmarking commit (af61126): comparison URL.

Overall result: ❌✅ regressions and improvements - please read:

Benchmarking means the PR may be perf-sensitive. It's automatically marked not fit for rolling up. Overriding is possible but disadvised: it risks changing compiler perf.

Next, please: If you can, justify the regressions found in this try perf run in writing along with @rustbot label: +perf-regression-triaged. If not, fix the regressions and do another perf run. Neutral or positive results will clear the label automatically.

@bors rollup=never rustc-perf
@rustbot label: -S-waiting-on-perf +perf-regression

Instruction count

Our most reliable metric. Used to determine the overall result above. However, even this metric can be noisy.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
0.3% [0.2%, 0.4%] 9
Improvements ✅
(primary)
-0.3% [-0.4%, -0.1%] 2
Improvements ✅
(secondary)
-0.6% [-0.6%, -0.6%] 1
All ❌✅ (primary) -0.3% [-0.4%, -0.1%] 2

Max RSS (memory usage)

Results (primary 2.1%, secondary 0.7%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
2.1% [2.1%, 2.1%] 1
Regressions ❌
(secondary)
1.6% [0.4%, 8.3%] 8
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-0.7% [-1.4%, -0.4%] 5
All ❌✅ (primary) 2.1% [2.1%, 2.1%] 1

Cycles

Results (secondary 0.1%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
0.7% [0.5%, 1.1%] 12
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-0.9% [-2.3%, -0.5%] 7
All ❌✅ (primary) - - 0

Binary size

Results (primary 0.1%, secondary 0.1%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
0.1% [0.0%, 0.1%] 40
Regressions ❌
(secondary)
0.1% [0.0%, 0.1%] 23
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 0.1% [0.0%, 0.1%] 40

Bootstrap: 459.298s -> 462.975s (0.80%)
Artifact size: 398.58 MiB -> 399.35 MiB (0.19%)

@rustbot rustbot added perf-regression Performance regression. and removed S-waiting-on-perf Status: Waiting on a perf run to be completed. labels Aug 8, 2026
@mejrs

mejrs commented Aug 8, 2026

Copy link
Copy Markdown
Member

Hmm that's not going to get better if we extend this to more places than implitems. Also, I've been thinking and I don't think this is the right approach.

I've been looking at the prior history of this (much discussion in #85200) and the derive expansion of tracing macros, and I think it makes most sense to implement it as suggested in #85200 (comment). Then you can also put it on manually written Debug impls for example and macros can more easily use it in a more granular way.

Meaning, this attribute shouldn't go on the macro (or the trait); the macro should put the attribute on the items it emits. To do that you'd have to

  • adjust what the attribute is allowed on
  • change the implementations (located in rustc_expand) of Debug/Clone to emit the attribute
  • change the macro shims in the standard library to allow the use of this attribute inside them, with #[allow_internal_unstable]
  • adjust the dead code pass to check for the presence of the attribute on more items

What do you think?

@josetorrs

josetorrs commented Aug 10, 2026

Copy link
Copy Markdown
Contributor Author

Yeah I was looking at that PR Friday night while exploring the changes for this. Also while looking the linked issues, do you think that would alleviate some of the concerns in #88900?

What do you think?

I'm using this issue as a learning experience for something a little more involved and very much appreciate your pointers but I may be the wrong person to bounce ideas off of 😅

@mejrs

mejrs commented Aug 10, 2026

Copy link
Copy Markdown
Member

Also while looking the linked issues, do you think that would alleviate some of the concerns in #88900?

The changes in this PR shouldn't be user visible, so it shouldn't change anything in that respect.

That said we get issues like that with every noticeable addition of lints, I wouldn't worry about it.

@josetorrs
josetorrs force-pushed the move-trivial-reads-to-macros branch 2 times, most recently from 5141208 to add619a Compare August 13, 2026 02:27
@josetorrs

Copy link
Copy Markdown
Contributor Author

I think I made some progress after a lot of debugging but now my tests not passing after recent commit yikes but looking for a gut check on this so far

@rust-log-analyzer

This comment has been minimized.

@mejrs mejrs left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Remember to set the waiting-on-review label if you need help/review, otherwise it's likely to slip through the cracks :)

now my tests not passing after recent commit

It looks like you put the attribute on the methods but check for its presence on the trait ref itself.

View changes since this review

@josetorrs josetorrs changed the title [WIP] - moving trivial field reads attr to macros [WIP] - moving trivial field reads attr on impl items Aug 19, 2026
@josetorrs josetorrs changed the title [WIP] - moving trivial field reads attr on impl items [WIP] - moving #[rustc_trivial_field_reads] on impl items Aug 19, 2026
@rust-log-analyzer

This comment has been minimized.

@mejrs

mejrs commented Aug 19, 2026

Copy link
Copy Markdown
Member

BTW, rather than trying to fix this case, I'd suggest just proceeding with this point I mentioned above:

  • adjust the dead code pass to check for the presence of the attribute on more items

So that would entail allowing more targets for the attribute, and, instead of this:

if let Node::ImplItem(impl_item) = node
&& self.should_ignore_impl_item(impl_item)
{

it's probably easiest to get the HirId of the node, and check, in visit_node, whether the attribute is there or not, and bail out if it does.

| DefKind::Use
| DefKind::Ctor(..)
| DefKind::ForeignMod => true,
| DefKind::ForeignMod => !find_attr!(tcx, def_id, RustcTrivialFieldReads),

@mejrs mejrs Aug 27, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Hmm or maybe do this check for the things we support? I think that's DefKind::Fn only

View changes since the review

@josetorrs josetorrs Sep 5, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

we can try this but I'm still getting a lot of the warnings whenever I build. I do not get the warnings when I put it it in visit_node but still trying to figure out why (moving back to draft while I figure it out)

@rust-bors

This comment has been minimized.

@josetorrs
josetorrs force-pushed the move-trivial-reads-to-macros branch from 8112d19 to d1d3be2 Compare September 5, 2026 14:57
@rustbot

rustbot commented Sep 5, 2026

Copy link
Copy Markdown
Collaborator

This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@josetorrs
josetorrs marked this pull request as draft September 5, 2026 15:16
@rustbot rustbot 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 Sep 5, 2026
@josetorrs
josetorrs force-pushed the move-trivial-reads-to-macros branch from 4e343c5 to 42b9de3 Compare September 5, 2026 15:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-attributes Area: Attributes (`#[…]`, `#![…]`) perf-regression Performance regression. S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library 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