Skip to content

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

Open
josetorrs wants to merge 20 commits into
rust-lang:mainfrom
josetorrs:move-trivial-reads-to-macros
Open

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

Conversation

@josetorrs

@josetorrsjosetorrs commented Aug 7, 2026

Copy link
Copy Markdown
Contributor

View all comments

part of #160621.

also minor discussion in:

r? @mejrs

@rustbotrustbot 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 threadcompiler/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)

@josetorrsjosetorrsAug 7, 2026

Copy link
Copy Markdown
ContributorAuthor

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.

@rustbotrustbot 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-borsBot pushed a commit that referenced this pull request Aug 8, 2026
[WIP] - moving trivial field reads attr to macros
@rust-bors

rust-borsBot 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.

meanrangecount
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.

meanrangecount
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.

meanrangecount
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.

meanrangecount
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%)

@rustbotrustbot 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
ContributorAuthor

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

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
josetorrsforce-pushed the move-trivial-reads-to-macros branch 2 times, most recently from 5141208 to add619aCompareAugust 13, 2026 02:27
@josetorrs

Copy link
Copy Markdown
ContributorAuthor

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.

@mejrsmejrs 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

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

This comment has been minimized.

@mejrs

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:

ifletNode::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.

@rust-log-analyzer

This comment has been minimized.

@josetorrs
josetorrsforce-pushed the move-trivial-reads-to-macros branch from 419a2b6 to d1cc7e1CompareAugust 20, 2026 00:47
@rust-log-analyzer

This comment has been minimized.

@josetorrsjosetorrs changed the title [WIP] - moving #[rustc_trivial_field_reads] on impl itemsexpanding #[rustc_trivial_field_reads] to more targets, place on Derive and CloneAug 24, 2026
@josetorrs
josetorrs marked this pull request as ready for review August 24, 2026 02:29
@rustbot

Copy link
Copy Markdown
Collaborator

Changes to the code generated for builtin derived traits.

cc @nnethercote

Some changes occurred in compiler/rustc_attr_parsing

cc @jdonszelmann, @JonathanBrouwer

@rustbotrustbot 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 Aug 24, 2026
@josetorrs

josetorrs commented Aug 24, 2026

Copy link
Copy Markdown
ContributorAuthor

okay I think it's ready now.

for the targets:

AllowedTargets::AllowList(&[Allow(Target::Fn),Allow(Target::Method(MethodKind::Inherent)),Allow(Target::Method(MethodKind::Trait{body:true})),Allow(Target::Method(MethodKind::TraitImpl)),]);

I kind of just chose the common ones I saw for the other attributes but that I think would make sense for this one

@rustbot ready

@rust-log-analyzer

This comment has been minimized.

Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-attributesArea: Attributes (`#[…]`, `#![…]`)perf-regressionPerformance regression.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.T-libsRelevant 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

@josetorrs@rust-log-analyzer@mejrs@rust-timer@rustbot