Skip to content

Make AssocItem aware of its impl kind #145186

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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

camsteffen
Copy link
Contributor

@camsteffen camsteffen commented Aug 9, 2025

The general goal is to have fewer query dependencies by making AssocItem aware of its parent impl kind (inherent vs. trait) without having to query the parent def_kind.

  • Introduce enum hir::ImplItemImplKind::{Inherent, Trait} at ImplItem.impl_kind which allows ImplItem to be aware of its containing impl kind, and also move impl-kind-specific fields into corresponding variants.
  • Split AssocItemContainer::Impl into AssocItemContainer::{InherentImpl, TraitImpl}.
  • Use AssocItemContainer in numerous places.

@rustbot
Copy link
Collaborator

rustbot commented Aug 9, 2025

r? @petrochenkov

rustbot has assigned @petrochenkov.
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 A-attributes Area: Attributes (`#[…]`, `#![…]`) A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. PG-exploit-mitigations Project group: Exploit mitigations S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-clippy Relevant to the Clippy team. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue. labels Aug 9, 2025
@rustbot
Copy link
Collaborator

rustbot commented Aug 9, 2025

Some changes occurred in src/tools/clippy

cc @rust-lang/clippy

Some changes occurred in compiler/rustc_sanitizers

cc @rcvalle

changes to the core type system

cc @compiler-errors, @lcnr

Some changes occurred to MIR optimizations

cc @rust-lang/wg-mir-opt

Some changes occurred in compiler/rustc_passes/src/check_attr.rs

cc @jdonszelmann

This PR changes rustc_public

cc @oli-obk, @celinval, @ouz-a

Some changes occurred in compiler/rustc_codegen_ssa

cc @WaffleLapkin

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@@ -1258,7 +1258,10 @@ pub(crate) fn clean_impl_item<'tcx>(
})),
hir::ImplItemKind::Fn(ref sig, body) => {
let m = clean_function(cx, sig, impl_.generics, ParamsSrc::Body(body));
let defaultness = cx.tcx.defaultness(impl_.owner_id);
let defaultness = match impl_.impl_kind {
hir::ImplItemImplKind::Inherent { .. } => hir::Defaultness::Final,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Maybe this should be None?

Copy link
Contributor

Choose a reason for hiding this comment

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

No opinion.

@compiler-errors
Copy link
Member

@bors2 try @rust-timer queue

@rust-timer

This comment has been minimized.

rust-bors bot added a commit that referenced this pull request Aug 10, 2025
Make `AssocItem` aware of its impl kind
@rust-bors
Copy link

rust-bors bot commented Aug 10, 2025

⌛ Trying commit 77a42ec with merge ecc5941

To cancel the try build, run the command @bors try cancel.

@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Aug 10, 2025
@rust-bors
Copy link

rust-bors bot commented Aug 10, 2025

☀️ Try build successful (CI)
Build commit: ecc5941 (ecc59411274ca4c68dad1b5a2b144afaf3510243, parent: 41ede7bd9bfbc9dc1e46c5c7de3b26524e5c61f0)

@rust-timer

This comment has been minimized.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (ecc5941): comparison URL.

Overall result: ❌✅ regressions and improvements - please read the text below

Benchmarking this pull request means it may be perf-sensitive – we'll automatically label it not fit for rolling up. You can override this, but we strongly advise not to, due to possible changes in compiler perf.

Next Steps: If you can justify the regressions found in this try perf run, please do so in sufficient writing along with @rustbot label: +perf-regression-triaged. If not, please fix the regressions and do another perf run. If its results are neutral or positive, the label will be automatically removed.

@bors rollup=never
@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)
2.0% [0.5%, 3.2%] 3
Regressions ❌
(secondary)
2.3% [1.6%, 2.9%] 2
Improvements ✅
(primary)
-0.4% [-1.7%, -0.2%] 20
Improvements ✅
(secondary)
-0.5% [-1.1%, -0.2%] 40
All ❌✅ (primary) -0.1% [-1.7%, 3.2%] 23

Max RSS (memory usage)

Results (primary 1.4%, secondary -1.7%)

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

mean range count
Regressions ❌
(primary)
2.2% [0.5%, 6.3%] 21
Regressions ❌
(secondary)
3.4% [1.2%, 6.7%] 3
Improvements ✅
(primary)
-2.3% [-4.2%, -1.3%] 5
Improvements ✅
(secondary)
-6.9% [-7.0%, -6.8%] 3
All ❌✅ (primary) 1.4% [-4.2%, 6.3%] 26

Cycles

Results (primary 3.3%, secondary 2.3%)

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

mean range count
Regressions ❌
(primary)
3.3% [3.3%, 3.3%] 1
Regressions ❌
(secondary)
2.3% [1.6%, 3.0%] 2
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 3.3% [3.3%, 3.3%] 1

Binary size

Results (primary 0.3%, secondary 0.2%)

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

mean range count
Regressions ❌
(primary)
0.6% [0.1%, 1.8%] 10
Regressions ❌
(secondary)
1.8% [0.2%, 4.8%] 4
Improvements ✅
(primary)
-0.5% [-1.6%, -0.1%] 4
Improvements ✅
(secondary)
-0.4% [-0.5%, -0.1%] 12
All ❌✅ (primary) 0.3% [-1.6%, 1.8%] 14

Bootstrap: 463.257s -> 466.12s (0.62%)
Artifact size: 377.39 MiB -> 375.29 MiB (-0.56%)

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

This comment was marked as resolved.

@petrochenkov
Copy link
Contributor

Oh! I misunderstood that. Okay, I'm going to look back over things with that in mind. I think you're right - I'll revert this change and maybe others.

@rustbot author

@rustbot rustbot removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Aug 11, 2025
@rustbot rustbot added the S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. label Aug 11, 2025
@rustbot
Copy link
Collaborator

rustbot commented Aug 11, 2025

Reminder, once the PR becomes ready for a review, use @rustbot ready.

@camsteffen
Copy link
Contributor Author

@rustbot ready (pending CI)

@rustbot rustbot 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 11, 2025
@petrochenkov
Copy link
Contributor

@bors2 try @rust-timer queue

@rust-timer

This comment has been minimized.

@rust-bors

This comment has been minimized.

rust-bors bot added a commit that referenced this pull request Aug 11, 2025
Make `AssocItem` aware of its impl kind
@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Aug 11, 2025
@petrochenkov petrochenkov removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Aug 11, 2025
@rust-bors
Copy link

rust-bors bot commented Aug 11, 2025

☀️ Try build successful (CI)
Build commit: 68df66d (68df66da903eecbd55c3d2a4c8862df210670e24, parent: fce0e74720d199eb7839fdb51af35ac5226da178)

@rust-timer

This comment has been minimized.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (68df66d): comparison URL.

Overall result: ✅ improvements - no action needed

Benchmarking this pull request means it may be perf-sensitive – we'll automatically label it not fit for rolling up. You can override this, but we strongly advise not to, due to possible changes in compiler perf.

@bors rollup=never
@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
Improvements ✅
(primary)
-0.2% [-0.3%, -0.2%] 15
Improvements ✅
(secondary)
-0.4% [-0.7%, -0.2%] 22
All ❌✅ (primary) -0.2% [-0.3%, -0.2%] 15

Max RSS (memory usage)

Results (primary -0.4%, secondary -6.8%)

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

mean range count
Regressions ❌
(primary)
1.3% [1.3%, 1.3%] 1
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-1.2% [-1.6%, -0.8%] 2
Improvements ✅
(secondary)
-6.8% [-6.9%, -6.8%] 3
All ❌✅ (primary) -0.4% [-1.6%, 1.3%] 3

Cycles

This benchmark run did not return any relevant results for this metric.

Binary size

Results (secondary -0.4%)

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
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-0.4% [-0.4%, -0.4%] 6
All ❌✅ (primary) - - 0

Bootstrap: 464.993s -> 464.709s (-0.06%)
Artifact size: 377.36 MiB -> 377.31 MiB (-0.01%)

@rustbot rustbot removed S-waiting-on-perf Status: Waiting on a perf run to be completed. perf-regression Performance regression. labels Aug 12, 2025
@petrochenkov petrochenkov added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Aug 12, 2025
@bors
Copy link
Collaborator

bors commented Aug 12, 2025

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

@@ -25,6 +26,7 @@ pub struct AssocItem {

/// If this is an item in an impl of a trait then this is the `DefId` of
Copy link
Contributor

Choose a reason for hiding this comment

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

The comment is incorrect, this field is currently set to Some for AssocItemContainer::Trait as well, but not always (?!)

It should either be consistently set for all AssocItemContainer::Traits, or not set for any of them.
In the latter case the conditions container == AssocItemContainer::Trait && trait_item_def_id.is_some() will become redundant.

Copy link
Contributor

Choose a reason for hiding this comment

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

In general, there's a lot of duplication in AssocItem.

  • trait_item_def_id is the same as def_id for trait items
  • container is almost a duplicate of trait_item_def_id, except for error recovery cases

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The comment is incorrect, this field is currently set to Some for AssocItemContainer::Trait as well, but not always (?!)

It seems to me somebody decided to put Some there cause it's "free", but it's not the purpose of the field. I could fix this, maybe in a separate PR? Or I could fix the code comment.

container is almost a duplicate of trait_item_def_id, except for error recovery cases

I noticed this as well. This change makes the two things closer to being redundant, but they are still not totally redundant. So ultimately I think this change is good and I don't see a way to simplify it.

Copy link
Contributor

Choose a reason for hiding this comment

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

I'm talking about this because changing representation of AssocItem would require rewriting half of the changes in this PR, so it makes sense to not separate them.
(The HIR change can be landed separately though.)

The non-redundant representation for AssocItem would be

struct AssocItem {
    def_id: DefId,
    kind: AssocKind,
    other_kind: OtherKind,
}

enum OtherKind {
    Trait,
    InherentImpl,
    TraitImpl(Option<DefId>),
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Gotcha. I can give that I try.

ty::AssocItemContainer::Trait => impl_ty,
ty::AssocItemContainer::Impl => tcx.associated_item(impl_ty.trait_item_def_id.unwrap()),
ty::AssocItemContainer::TraitImpl => {
tcx.associated_item(impl_ty.trait_item_def_id.unwrap())
Copy link
Contributor

Choose a reason for hiding this comment

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

This is a sleeping ICE, because trait_item_def_id can indeed be None if there are compilation errors.

@petrochenkov petrochenkov 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 Aug 12, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-attributes Area: Attributes (`#[…]`, `#![…]`) A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. PG-exploit-mitigations Project group: Exploit mitigations S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-clippy Relevant to the Clippy team. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants