Skip to content

Limit impl_trait_header query to only trait impls #144607

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 6 commits into
base: master
Choose a base branch
from

Conversation

camsteffen
Copy link
Contributor

@camsteffen camsteffen commented Jul 28, 2025

Changes impl_trait_header to panic on inherent impls intstead of returning None. A few downstream functions are split into option and non-option returning functions. This gets rid of a lot of unwraps where we know we have a trait impl, while there are still a lot of cases where the Option is helpful.

I think we'll want a perf run? And I will push clippy changes if this garners favor.

Summary of changes to tcx methods:

  • impl_is_of_trait (new)
  • impl_trait_header -> impl_trait_header/impl_opt_trait_header
  • impl_trait_ref -> impl_trait_ref/impl_opt_trait_ref
  • trait_id_of_impl -> impl_trait_id/impl_opt_trait_id

@rustbot
Copy link
Collaborator

rustbot commented Jul 28, 2025

r? @jackh726

rustbot has assigned @jackh726.
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-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-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 Jul 28, 2025
@rustbot
Copy link
Collaborator

rustbot commented Jul 28, 2025

Some changes occurred to MIR optimizations

cc @rust-lang/wg-mir-opt

Some changes occurred to the CTFE machinery

cc @RalfJung, @oli-obk, @lcnr

Some changes occurred in compiler/rustc_sanitizers

cc @rcvalle

HIR ty lowering was modified

cc @fmease

@rust-log-analyzer

This comment has been minimized.

@lqd
Copy link
Member

lqd commented Jul 28, 2025

@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

rust-bors bot added a commit that referenced this pull request Jul 28, 2025
Limit impl_trait_header query to only trait impls
@rust-bors
Copy link

rust-bors bot commented Jul 28, 2025

⌛ Trying commit c4b4025 with merge a9207a1

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 Jul 28, 2025
@rust-bors
Copy link

rust-bors bot commented Jul 28, 2025

💔 Test failed (CI). Failed jobs:

@camsteffen camsteffen force-pushed the impl-trait-header-option branch from c4b4025 to 35dbd1a Compare July 28, 2025 22:35
@camsteffen
Copy link
Contributor Author

@bors try @rust-timer queue

@rust-bors
Copy link

rust-bors bot commented Jul 28, 2025

@camsteffen: 🔑 Insufficient privileges: not in try users

@rust-timer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@lqd
Copy link
Member

lqd commented Jul 29, 2025

@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

rust-bors bot added a commit that referenced this pull request Jul 29, 2025
Limit impl_trait_header query to only trait impls
@rust-bors
Copy link

rust-bors bot commented Jul 29, 2025

⌛ Trying commit 35dbd1a with merge 60b45cb

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

@rust-bors
Copy link

rust-bors bot commented Jul 29, 2025

☀️ Try build successful (CI)
Build commit: 60b45cb (60b45cbaa7a7f0ea8340063c9bbac4ed081c245a, parent: cdccba87bf9ad5d87c1417eeb8fa52a4b614bbf9)

@rust-timer

This comment has been minimized.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (60b45cb): comparison URL.

Overall result: ❌✅ regressions and improvements - BENCHMARK(S) FAILED

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

❗ ❗ ❗ ❗ ❗
Warning ⚠️: The following benchmark(s) failed to build:

  • eza-0.21.2
  • externs
  • include-blob
  • clap_derive-4.5.32
  • serde-1.0.219
  • wg-grammar
  • syn-2.0.101
  • unify-linearly
  • ucd
  • serde_derive-1.0.219
  • cranelift-codegen-0.119.0
  • regression-31157
  • diesel-2.2.10
  • bitmaps-3.2.1
  • hyper-1.6.0
  • ripgrep-14.1.1
  • tuple-stress
  • match-stress
  • ctfe-stress-5
  • derive
  • deeply-nested-multi
  • helloworld
  • unicode-normalization-0.1.24
  • wf-projection-stress-65510
  • coercions
  • stm32f4-0.15.1
  • issue-58319
  • html5ever-0.31.0
  • projection-caching
  • token-stream-stress
  • tt-muncher
  • serde-1.0.219-threads4
  • large-workspace
  • nalgebra-0.33.0
  • many-assoc-items
  • issue-46449
  • image-0.25.6
  • cargo-0.87.1
  • typenum-1.18.0
  • unused-warnings
  • await-call-tree
  • regex-automata-0.4.8
  • issue-88862
  • deep-vector
  • libc-0.2.172

❗ ❗ ❗ ❗ ❗

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.4% [0.2%, 3.7%] 9
Regressions ❌
(secondary)
0.5% [0.4%, 0.5%] 3
Improvements ✅
(primary)
-0.2% [-0.2%, -0.2%] 1
Improvements ✅
(secondary)
-0.2% [-0.2%, -0.2%] 1
All ❌✅ (primary) 2.1% [-0.2%, 3.7%] 10

Max RSS (memory usage)

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

Cycles

Results (secondary -2.6%)

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)
-2.6% [-3.1%, -2.3%] 5
All ❌✅ (primary) - - 0

Binary size

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

Bootstrap: 469.046s -> 466.887s (-0.46%)
Artifact size: 376.81 MiB -> 376.87 MiB (0.02%)

@rustbot rustbot added perf-regression Performance regression. and removed S-waiting-on-perf Status: Waiting on a perf run to be completed. labels Jul 29, 2025
@camsteffen
Copy link
Contributor Author

camsteffen commented Jul 29, 2025

Y'all think that's funny? 😤

Fixed the rustdoc issues from above.

@camsteffen camsteffen force-pushed the impl-trait-header-option branch from 35dbd1a to 87f803c Compare July 29, 2025 14:02
@rust-log-analyzer
Copy link
Collaborator

The job pr-check-2 failed! Check out the build log: (web) (plain enhanced) (plain)

Click to see the possible cause of the failure (guessed by this bot)
[RUSTC-TIMING] filetime test:false 0.087
error[E0599]: no method named `expect` found for struct `EarlyBinder` in the current scope
   --> src/tools/clippy/clippy_lints/src/derive.rs:244:60
    |
244 |             let trait_ref = cx.tcx.impl_trait_ref(impl_id).expect("must be a trait implementation");
    |                                                            ^^^^^^ method not found in `EarlyBinder<TyCtxt<'_>, rustc_middle::infer::canonical::rustc_type_ir::TraitRef<TyCtxt<'_>>>`

error[E0599]: no method named `expect` found for struct `EarlyBinder` in the current scope
   --> src/tools/clippy/clippy_lints/src/derive.rs:287:60
    |
287 |             let trait_ref = cx.tcx.impl_trait_ref(impl_id).expect("must be a trait implementation");
    |                                                            ^^^^^^ method not found in `EarlyBinder<TyCtxt<'_>, rustc_middle::infer::canonical::rustc_type_ir::TraitRef<TyCtxt<'_>>>`

error[E0308]: mismatched types
  --> src/tools/clippy/clippy_lints/src/fallible_impl_from.rs:56:20
   |
56 |             && let Some(impl_trait_ref) = cx.tcx.impl_trait_ref(item.owner_id)
   |                    ^^^^^^^^^^^^^^^^^^^^   ------------------------------------ this expression has type `EarlyBinder<TyCtxt<'_>, rustc_middle::infer::canonical::rustc_type_ir::TraitRef<TyCtxt<'_>>>`
   |                    |
   |                    expected `EarlyBinder<TyCtxt<'_>, TraitRef<...>>`, found `Option<_>`
   |
   = note: expected struct `EarlyBinder<TyCtxt<'_>, rustc_middle::infer::canonical::rustc_type_ir::TraitRef<TyCtxt<'_>>>`
                found enum `std::option::Option<_>`

error[E0599]: `EarlyBinder<TyCtxt<'_>, rustc_middle::infer::canonical::rustc_type_ir::TraitRef<TyCtxt<'_>>>` is not an iterator
   --> src/tools/clippy/clippy_lints/src/from_over_into.rs:80:14
    |
79  |               && let Some(middle_trait_ref) = cx.tcx.impl_trait_ref(item.owner_id)
    |  _____________________________________________-
80  | |             .map(ty::EarlyBinder::instantiate_identity)
    | |             -^^^ `EarlyBinder<TyCtxt<'_>, rustc_middle::infer::canonical::rustc_type_ir::TraitRef<TyCtxt<'_>>>` is not an iterator
    | |_____________|
    |
    |
   ::: /checkout/compiler/rustc_type_ir/src/binder.rs:357:1
    |
357 |   pub struct EarlyBinder<I: Interner, T> {
    |   -------------------------------------- doesn't satisfy `_: Iterator`
    |
    = note: the following trait bounds were not satisfied:
            `EarlyBinder<TyCtxt<'_>, rustc_middle::infer::canonical::rustc_type_ir::TraitRef<TyCtxt<'_>>>: std::iter::Iterator`
            which is required by `&mut EarlyBinder<TyCtxt<'_>, rustc_middle::infer::canonical::rustc_type_ir::TraitRef<TyCtxt<'_>>>: std::iter::Iterator`

error[E0308]: mismatched types
   --> src/tools/clippy/clippy_lints/src/implicit_saturating_sub.rs:343:28
    |
343 |                     && let None = cx.tcx.impl_trait_ref(impl_id) // An inherent impl
    |                            ^^^^   ------------------------------ this expression has type `EarlyBinder<TyCtxt<'_>, rustc_middle::infer::canonical::rustc_type_ir::TraitRef<TyCtxt<'_>>>`
    |                            |
    |                            expected `EarlyBinder<TyCtxt<'_>, TraitRef<...>>`, found `Option<_>`
    |
    = note: expected struct `EarlyBinder<TyCtxt<'_>, rustc_middle::infer::canonical::rustc_type_ir::TraitRef<TyCtxt<'_>>>`
                 found enum `std::option::Option<_>`

error[E0308]: mismatched types
   --> src/tools/clippy/clippy_lints/src/implicit_saturating_sub.rs:354:28
    |
354 |                     && let None = cx.tcx.impl_trait_ref(impl_id) // An inherent impl
    |                            ^^^^   ------------------------------ this expression has type `EarlyBinder<TyCtxt<'_>, rustc_middle::infer::canonical::rustc_type_ir::TraitRef<TyCtxt<'_>>>`
    |                            |
    |                            expected `EarlyBinder<TyCtxt<'_>, TraitRef<...>>`, found `Option<_>`
    |
    = note: expected struct `EarlyBinder<TyCtxt<'_>, rustc_middle::infer::canonical::rustc_type_ir::TraitRef<TyCtxt<'_>>>`
                 found enum `std::option::Option<_>`

[RUSTC-TIMING] askama_derive test:false 1.996
    Checking askama v0.14.0
[RUSTC-TIMING] askama test:false 0.177
error[E0599]: no method named `is_none` found for struct `EarlyBinder` in the current scope
  --> src/tools/clippy/clippy_lints/src/methods/implicit_clone.rs:55:111
   |
55 |                 cx.tcx.type_of(impl_did).instantiate_identity().is_slice() && cx.tcx.impl_trait_ref(impl_did).is_none()
   |                                                                                                               ^^^^^^^ method not found in `EarlyBinder<TyCtxt<'_>, rustc_middle::infer::canonical::rustc_type_ir::TraitRef<TyCtxt<'_>>>`

error[E0599]: no method named `is_none` found for struct `EarlyBinder` in the current scope
  --> src/tools/clippy/clippy_lints/src/methods/suspicious_splitn.rs:14:43
   |
14 |         && cx.tcx.impl_trait_ref(impl_id).is_none()
   |                                           ^^^^^^^ method not found in `EarlyBinder<TyCtxt<'_>, rustc_middle::infer::canonical::rustc_type_ir::TraitRef<TyCtxt<'_>>>`

error[E0599]: no method named `is_some` found for struct `EarlyBinder` in the current scope
   --> src/tools/clippy/clippy_lints/src/missing_doc.rs:250:43
    |
250 |             if cx.tcx.impl_trait_ref(cid).is_some() {
    |                                           ^^^^^^^ method not found in `EarlyBinder<TyCtxt<'_>, rustc_middle::infer::canonical::rustc_type_ir::TraitRef<TyCtxt<'_>>>`

error[E0599]: `EarlyBinder<TyCtxt<'_>, rustc_middle::infer::canonical::rustc_type_ir::TraitRef<TyCtxt<'_>>>` is not an iterator
   --> src/tools/clippy/clippy_lints/src/missing_inline.rs:172:77
    |
172 |             AssocItemContainer::Impl => cx.tcx.impl_trait_ref(container_id).map(|t| t.skip_binder().def_id),
    |                                                                             ^^^ `EarlyBinder<TyCtxt<'_>, rustc_middle::infer::canonical::rustc_type_ir::TraitRef<TyCtxt<'_>>>` is not an iterator
    |
   ::: /checkout/compiler/rustc_type_ir/src/binder.rs:357:1
    |
357 | pub struct EarlyBinder<I: Interner, T> {
    | -------------------------------------- doesn't satisfy `_: Iterator`
    |
    = note: the following trait bounds were not satisfied:
            `EarlyBinder<TyCtxt<'_>, rustc_middle::infer::canonical::rustc_type_ir::TraitRef<TyCtxt<'_>>>: std::iter::Iterator`
            which is required by `&mut EarlyBinder<TyCtxt<'_>, rustc_middle::infer::canonical::rustc_type_ir::TraitRef<TyCtxt<'_>>>: std::iter::Iterator`

error[E0599]: `EarlyBinder<TyCtxt<'_>, rustc_middle::infer::canonical::rustc_type_ir::TraitRef<TyCtxt<'_>>>` is not an iterator
   --> src/tools/clippy/clippy_lints/src/non_canonical_impls.rs:120:69
    |
120 |         let Some(trait_impl) = cx.tcx.impl_trait_ref(item.owner_id).map(EarlyBinder::skip_binder) else {
    |                                                                     ^^^ `EarlyBinder<TyCtxt<'_>, rustc_middle::infer::canonical::rustc_type_ir::TraitRef<TyCtxt<'_>>>` is not an iterator
    |
   ::: /checkout/compiler/rustc_type_ir/src/binder.rs:357:1
    |
357 | pub struct EarlyBinder<I: Interner, T> {
    | -------------------------------------- doesn't satisfy `_: Iterator`
    |
    = note: the following trait bounds were not satisfied:
            `EarlyBinder<TyCtxt<'_>, rustc_middle::infer::canonical::rustc_type_ir::TraitRef<TyCtxt<'_>>>: std::iter::Iterator`
            which is required by `&mut EarlyBinder<TyCtxt<'_>, rustc_middle::infer::canonical::rustc_type_ir::TraitRef<TyCtxt<'_>>>: std::iter::Iterator`

error[E0308]: mismatched types
  --> src/tools/clippy/clippy_lints/src/non_send_fields_in_send_ty.rs:90:20
   |
90 |             && let Some(ty_trait_ref) = cx.tcx.impl_trait_ref(item.owner_id)
   |                    ^^^^^^^^^^^^^^^^^^   ------------------------------------ this expression has type `EarlyBinder<TyCtxt<'_>, rustc_middle::infer::canonical::rustc_type_ir::TraitRef<TyCtxt<'_>>>`
   |                    |
   |                    expected `EarlyBinder<TyCtxt<'_>, TraitRef<...>>`, found `Option<_>`
   |
   = note: expected struct `EarlyBinder<TyCtxt<'_>, rustc_middle::infer::canonical::rustc_type_ir::TraitRef<TyCtxt<'_>>>`
                found enum `std::option::Option<_>`

error[E0599]: `EarlyBinder<TyCtxt<'_>, rustc_middle::infer::canonical::rustc_type_ir::TraitRef<TyCtxt<'_>>>` is not an iterator
   --> src/tools/clippy/clippy_lints/src/only_used_in_recursion.rs:250:26
    |
247 |                       && let Some(trait_ref) = cx
    |  ______________________________________________-
248 | |                         .tcx
249 | |                         .impl_trait_ref(item.owner_id)
250 | |                         .map(EarlyBinder::instantiate_identity)
    | |                         -^^^ `EarlyBinder<TyCtxt<'_>, rustc_middle::infer::canonical::rustc_type_ir::TraitRef<TyCtxt<'_>>>` is not an iterator
    | |_________________________|
    |
    |
   ::: /checkout/compiler/rustc_type_ir/src/binder.rs:357:1
    |
357 |   pub struct EarlyBinder<I: Interner, T> {
    |   -------------------------------------- doesn't satisfy `_: Iterator`
    |
    = note: the following trait bounds were not satisfied:
            `EarlyBinder<TyCtxt<'_>, rustc_middle::infer::canonical::rustc_type_ir::TraitRef<TyCtxt<'_>>>: std::iter::Iterator`
            which is required by `&mut EarlyBinder<TyCtxt<'_>, rustc_middle::infer::canonical::rustc_type_ir::TraitRef<TyCtxt<'_>>>: std::iter::Iterator`

error[E0599]: no method named `trait_id_of_impl` found for struct `TyCtxt<'tcx>` in the current scope
   --> src/tools/clippy/clippy_lints/src/return_self_not_must_use.rs:119:23
    |
119 |             && cx.tcx.trait_id_of_impl(impl_def).is_none()
    |                       ^^^^^^^^^^^^^^^^
    |
help: there is a method `span_of_impl` with a similar name
    |
119 -             && cx.tcx.trait_id_of_impl(impl_def).is_none()
119 +             && cx.tcx.span_of_impl(impl_def).is_none()
    |

error[E0599]: no method named `trait_id_of_impl` found for struct `TyCtxt<'tcx>` in the current scope
  --> src/tools/clippy/clippy_lints/src/unused_io_amount.rs:88:44
   |
88 |             && let Some(trait_id) = cx.tcx.trait_id_of_impl(impl_id)
   |                                            ^^^^^^^^^^^^^^^^
   |
help: there is a method `span_of_impl` with a similar name
   |
88 -             && let Some(trait_id) = cx.tcx.trait_id_of_impl(impl_id)
88 +             && let Some(trait_id) = cx.tcx.span_of_impl(impl_id)
   |

error[E0308]: mismatched types
   --> src/tools/clippy/clippy_lints/src/use_self.rs:144:20
    |
144 |             && let Some(impl_trait_ref) = cx.tcx.impl_trait_ref(impl_id)
    |                    ^^^^^^^^^^^^^^^^^^^^   ------------------------------ this expression has type `EarlyBinder<TyCtxt<'_>, rustc_middle::infer::canonical::rustc_type_ir::TraitRef<TyCtxt<'_>>>`
    |                    |
    |                    expected `EarlyBinder<TyCtxt<'_>, TraitRef<...>>`, found `Option<_>`
    |
    = note: expected struct `EarlyBinder<TyCtxt<'_>, rustc_middle::infer::canonical::rustc_type_ir::TraitRef<TyCtxt<'_>>>`
                 found enum `std::option::Option<_>`

Some errors have detailed explanations: E0308, E0599.

@camsteffen
Copy link
Contributor Author

Can I get another perf attempt whoever may see this?

@lqd
Copy link
Member

lqd commented Jul 30, 2025

@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

rust-bors bot added a commit that referenced this pull request Jul 30, 2025
Limit impl_trait_header query to only trait impls
@rust-bors
Copy link

rust-bors bot commented Jul 30, 2025

⌛ Trying commit 87f803c with merge 0a2accc

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 Jul 30, 2025
@rust-bors
Copy link

rust-bors bot commented Jul 30, 2025

☀️ Try build successful (CI)
Build commit: 0a2accc (0a2acccf781b1a98ca4607d0bf6132b2d84d1545, parent: e5e79f8bd428d0b8d26e8240d718b134ef297459)

@rust-timer

This comment has been minimized.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (0a2accc): 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.2% [0.1%, 3.7%] 16
Regressions ❌
(secondary)
0.5% [0.4%, 0.6%] 3
Improvements ✅
(primary)
-0.2% [-0.2%, -0.2%] 2
Improvements ✅
(secondary)
-0.3% [-0.3%, -0.3%] 1
All ❌✅ (primary) 1.9% [-0.2%, 3.7%] 18

Max RSS (memory usage)

Results (primary 3.1%)

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

mean range count
Regressions ❌
(primary)
3.1% [0.5%, 7.2%] 3
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 3.1% [0.5%, 7.2%] 3

Cycles

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)
3.7% [3.7%, 3.7%] 1
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-2.5% [-2.5%, -2.5%] 2
All ❌✅ (primary) - - 0

Binary size

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

Bootstrap: 469.342s -> 467.493s (-0.39%)
Artifact size: 376.86 MiB -> 376.88 MiB (0.01%)

@rustbot rustbot removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Jul 31, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. perf-regression Performance regression. PG-exploit-mitigations Project group: Exploit mitigations 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. 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.

6 participants