Skip to content

Check assoc consts and tys later like assoc fns #143519

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

Merged
merged 1 commit into from
Jul 13, 2025

Conversation

mu001999
Copy link
Contributor

@mu001999 mu001999 commented Jul 6, 2025

This PR

  1. checks assoc consts and tys later like assoc fns
  2. marks assoc consts appear in poly-trait-ref live

For assoc consts, considering

#![deny(dead_code)]

trait Tr { // ERROR trait `Tr` is never used
    const I: Self;
}

struct Foo; //~ ERROR struct `Foo` is never constructed

impl Tr for Foo {
    const I: Self = Foo;
}

fn main() {}

Current this will produce unused I instead of unused Tr and Foo (play), because const I: Self = Foo; will be added into the worklist at first:

error: associated constant `I` is never used
 --> src/main.rs:4:11
  |
3 | trait Tr { // ERROR trait `Tr` is never used
  |       -- associated constant in this trait
4 |     const I: Self;
  |           ^
  |
note: the lint level is defined here
 --> src/main.rs:1:9
  |
1 | #![deny(dead_code)]
  |         ^^^^^^^^^

error: could not compile `playground` (bin "playground") due to 1 previous error

This also happens to assoc tys, see the new test

Fixes #126729

r? @petrochenkov

@rustbot rustbot added 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. labels Jul 6, 2025
@rust-log-analyzer

This comment has been minimized.

@mu001999 mu001999 force-pushed the dead-code/impl-items branch from 2c73999 to c90150b Compare July 6, 2025 09:06
@rust-log-analyzer

This comment has been minimized.

@mu001999 mu001999 force-pushed the dead-code/impl-items branch from c90150b to 4cf632b Compare July 6, 2025 10:02
@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 Jul 9, 2025
@mu001999 mu001999 force-pushed the dead-code/impl-items branch from 4cf632b to 889582e Compare July 10, 2025 15:40
@mu001999
Copy link
Contributor Author

@rustbot ready

@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 Jul 10, 2025
@petrochenkov
Copy link
Contributor

@bors r+

@bors
Copy link
Collaborator

bors commented Jul 11, 2025

📌 Commit 889582e has been approved by petrochenkov

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jul 11, 2025
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Jul 12, 2025
…ems, r=petrochenkov

Check assoc consts and tys later like assoc fns

This PR
1. checks assoc consts and tys later like assoc fns
2. marks assoc consts appear in poly-trait-ref live

For assoc consts, considering
```rust
#![deny(dead_code)]

trait Tr { // ERROR trait `Tr` is never used
    const I: Self;
}

struct Foo; //~ ERROR struct `Foo` is never constructed

impl Tr for Foo {
    const I: Self = Foo;
}

fn main() {}
```

Current this will produce unused `I` instead of unused `Tr` and `Foo` ([play](https://play.rust-lang.org/?version=nightly&mode=debug&edition=2024&gist=e0490d4a2d522cb70437b26e514a3d9c)), because `const I: Self = Foo;` will be added into the worklist at first:
```
error: associated constant `I` is never used
 --> src/main.rs:4:11
  |
3 | trait Tr { // ERROR trait `Tr` is never used
  |       -- associated constant in this trait
4 |     const I: Self;
  |           ^
  |
note: the lint level is defined here
 --> src/main.rs:1:9
  |
1 | #![deny(dead_code)]
  |         ^^^^^^^^^

error: could not compile `playground` (bin "playground") due to 1 previous error
```

This also happens to assoc tys, see the [new test](https://github.com/rust-lang/rust/compare/master...mu001999-contrib:rust:dead-code/impl-items?expand=1#diff-bf45fa403934a31c9d610a073ed2603d885e7e81572e8edf38b7f4e08a1f3531)

Fixes rust-lang#126729

r? `@petrochenkov`
bors added a commit that referenced this pull request Jul 12, 2025
Rollup of 9 pull requests

Successful merges:

 - #143213 (de-duplicate condition scoping logic between AST→HIR lowering and `ScopeTree` construction)
 - #143461 (make `cfg_select` a builtin macro)
 - #143519 (Check assoc consts and tys later like assoc fns)
 - #143554 (slice: Mark `rotate_left`, `rotate_right` unstably const)
 - #143704 (Be a bit more careful around exotic cycles in in the inliner)
 - #143774 (constify `From` and `Into`)
 - #143786 (Fix fallback for CI_JOB_NAME)
 - #143796 (Fix ICE for parsed attributes with longer path not handled by CheckAttribute)
 - #143798 (Remove format short command trait)

r? `@ghost`
`@rustbot` modify labels: rollup
fmease added a commit to fmease/rust that referenced this pull request Jul 13, 2025
…ems, r=petrochenkov

Check assoc consts and tys later like assoc fns

This PR
1. checks assoc consts and tys later like assoc fns
2. marks assoc consts appear in poly-trait-ref live

For assoc consts, considering
```rust
#![deny(dead_code)]

trait Tr { // ERROR trait `Tr` is never used
    const I: Self;
}

struct Foo; //~ ERROR struct `Foo` is never constructed

impl Tr for Foo {
    const I: Self = Foo;
}

fn main() {}
```

Current this will produce unused `I` instead of unused `Tr` and `Foo` ([play](https://play.rust-lang.org/?version=nightly&mode=debug&edition=2024&gist=e0490d4a2d522cb70437b26e514a3d9c)), because `const I: Self = Foo;` will be added into the worklist at first:
```
error: associated constant `I` is never used
 --> src/main.rs:4:11
  |
3 | trait Tr { // ERROR trait `Tr` is never used
  |       -- associated constant in this trait
4 |     const I: Self;
  |           ^
  |
note: the lint level is defined here
 --> src/main.rs:1:9
  |
1 | #![deny(dead_code)]
  |         ^^^^^^^^^

error: could not compile `playground` (bin "playground") due to 1 previous error
```

This also happens to assoc tys, see the [new test](https://github.com/rust-lang/rust/compare/master...mu001999-contrib:rust:dead-code/impl-items?expand=1#diff-bf45fa403934a31c9d610a073ed2603d885e7e81572e8edf38b7f4e08a1f3531)

Fixes rust-lang#126729

r? ``@petrochenkov``
bors added a commit that referenced this pull request Jul 13, 2025
Rollup of 14 pull requests

Successful merges:

 - #143301 (`tests/ui`: A New Order [26/N])
 - #143461 (make `cfg_select` a builtin macro)
 - #143519 (Check assoc consts and tys later like assoc fns)
 - #143554 (slice: Mark `rotate_left`, `rotate_right` unstably const)
 - #143634 (interpret/allocation: expose init + write_wildcards on a range)
 - #143679 (Preserve the .debug_gdb_scripts section)
 - #143685 (Resolve: merge `source_bindings` and `target_bindings` into `bindings`)
 - #143704 (Be a bit more careful around exotic cycles in in the inliner)
 - #143734 (Refactor resolve resolution bindings)
 - #143774 (constify `From` and `Into`)
 - #143785 (Add --compile-time-deps argument for x check)
 - #143786 (Fix fallback for CI_JOB_NAME)
 - #143825 (clippy: fix test filtering when TESTNAME is empty)
 - #143826 (Fix command trace)

r? `@ghost`
`@rustbot` modify labels: rollup
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Jul 13, 2025
…ems, r=petrochenkov

Check assoc consts and tys later like assoc fns

This PR
1. checks assoc consts and tys later like assoc fns
2. marks assoc consts appear in poly-trait-ref live

For assoc consts, considering
```rust
#![deny(dead_code)]

trait Tr { // ERROR trait `Tr` is never used
    const I: Self;
}

struct Foo; //~ ERROR struct `Foo` is never constructed

impl Tr for Foo {
    const I: Self = Foo;
}

fn main() {}
```

Current this will produce unused `I` instead of unused `Tr` and `Foo` ([play](https://play.rust-lang.org/?version=nightly&mode=debug&edition=2024&gist=e0490d4a2d522cb70437b26e514a3d9c)), because `const I: Self = Foo;` will be added into the worklist at first:
```
error: associated constant `I` is never used
 --> src/main.rs:4:11
  |
3 | trait Tr { // ERROR trait `Tr` is never used
  |       -- associated constant in this trait
4 |     const I: Self;
  |           ^
  |
note: the lint level is defined here
 --> src/main.rs:1:9
  |
1 | #![deny(dead_code)]
  |         ^^^^^^^^^

error: could not compile `playground` (bin "playground") due to 1 previous error
```

This also happens to assoc tys, see the [new test](https://github.com/rust-lang/rust/compare/master...mu001999-contrib:rust:dead-code/impl-items?expand=1#diff-bf45fa403934a31c9d610a073ed2603d885e7e81572e8edf38b7f4e08a1f3531)

Fixes rust-lang#126729

r? ```@petrochenkov```
bors added a commit that referenced this pull request Jul 13, 2025
Rollup of 13 pull requests

Successful merges:

 - #143301 (`tests/ui`: A New Order [26/N])
 - #143461 (make `cfg_select` a builtin macro)
 - #143519 (Check assoc consts and tys later like assoc fns)
 - #143554 (slice: Mark `rotate_left`, `rotate_right` unstably const)
 - #143634 (interpret/allocation: expose init + write_wildcards on a range)
 - #143679 (Preserve the .debug_gdb_scripts section)
 - #143685 (Resolve: merge `source_bindings` and `target_bindings` into `bindings`)
 - #143734 (Refactor resolve resolution bindings)
 - #143774 (constify `From` and `Into`)
 - #143785 (Add --compile-time-deps argument for x check)
 - #143786 (Fix fallback for CI_JOB_NAME)
 - #143825 (clippy: fix test filtering when TESTNAME is empty)
 - #143826 (Fix command trace)

r? `@ghost`
`@rustbot` modify labels: rollup
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Jul 13, 2025
…ems, r=petrochenkov

Check assoc consts and tys later like assoc fns

This PR
1. checks assoc consts and tys later like assoc fns
2. marks assoc consts appear in poly-trait-ref live

For assoc consts, considering
```rust
#![deny(dead_code)]

trait Tr { // ERROR trait `Tr` is never used
    const I: Self;
}

struct Foo; //~ ERROR struct `Foo` is never constructed

impl Tr for Foo {
    const I: Self = Foo;
}

fn main() {}
```

Current this will produce unused `I` instead of unused `Tr` and `Foo` ([play](https://play.rust-lang.org/?version=nightly&mode=debug&edition=2024&gist=e0490d4a2d522cb70437b26e514a3d9c)), because `const I: Self = Foo;` will be added into the worklist at first:
```
error: associated constant `I` is never used
 --> src/main.rs:4:11
  |
3 | trait Tr { // ERROR trait `Tr` is never used
  |       -- associated constant in this trait
4 |     const I: Self;
  |           ^
  |
note: the lint level is defined here
 --> src/main.rs:1:9
  |
1 | #![deny(dead_code)]
  |         ^^^^^^^^^

error: could not compile `playground` (bin "playground") due to 1 previous error
```

This also happens to assoc tys, see the [new test](https://github.com/rust-lang/rust/compare/master...mu001999-contrib:rust:dead-code/impl-items?expand=1#diff-bf45fa403934a31c9d610a073ed2603d885e7e81572e8edf38b7f4e08a1f3531)

Fixes rust-lang#126729

r? ````@petrochenkov````
bors added a commit that referenced this pull request Jul 13, 2025
Rollup of 11 pull requests

Successful merges:

 - #143301 (`tests/ui`: A New Order [26/N])
 - #143519 (Check assoc consts and tys later like assoc fns)
 - #143554 (slice: Mark `rotate_left`, `rotate_right` unstably const)
 - #143634 (interpret/allocation: expose init + write_wildcards on a range)
 - #143685 (Resolve: merge `source_bindings` and `target_bindings` into `bindings`)
 - #143734 (Refactor resolve resolution bindings)
 - #143774 (constify `From` and `Into`)
 - #143785 (Add --compile-time-deps argument for x check)
 - #143786 (Fix fallback for CI_JOB_NAME)
 - #143825 (clippy: fix test filtering when TESTNAME is empty)
 - #143826 (Fix command trace)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 7e0721b into rust-lang:master Jul 13, 2025
11 checks passed
@rustbot rustbot added this to the 1.90.0 milestone Jul 13, 2025
rust-timer added a commit that referenced this pull request Jul 13, 2025
Rollup merge of #143519 - mu001999-contrib:dead-code/impl-items, r=petrochenkov

Check assoc consts and tys later like assoc fns

This PR
1. checks assoc consts and tys later like assoc fns
2. marks assoc consts appear in poly-trait-ref live

For assoc consts, considering
```rust
#![deny(dead_code)]

trait Tr { // ERROR trait `Tr` is never used
    const I: Self;
}

struct Foo; //~ ERROR struct `Foo` is never constructed

impl Tr for Foo {
    const I: Self = Foo;
}

fn main() {}
```

Current this will produce unused `I` instead of unused `Tr` and `Foo` ([play](https://play.rust-lang.org/?version=nightly&mode=debug&edition=2024&gist=e0490d4a2d522cb70437b26e514a3d9c)), because `const I: Self = Foo;` will be added into the worklist at first:
```
error: associated constant `I` is never used
 --> src/main.rs:4:11
  |
3 | trait Tr { // ERROR trait `Tr` is never used
  |       -- associated constant in this trait
4 |     const I: Self;
  |           ^
  |
note: the lint level is defined here
 --> src/main.rs:1:9
  |
1 | #![deny(dead_code)]
  |         ^^^^^^^^^

error: could not compile `playground` (bin "playground") due to 1 previous error
```

This also happens to assoc tys, see the [new test](https://github.com/rust-lang/rust/compare/master...mu001999-contrib:rust:dead-code/impl-items?expand=1#diff-bf45fa403934a31c9d610a073ed2603d885e7e81572e8edf38b7f4e08a1f3531)

Fixes #126729

r? `````@petrochenkov`````
@Kobzol
Copy link
Member

Kobzol commented Jul 13, 2025

@rust-timer build eca1577

Checking for #143888.

@rust-timer

This comment has been minimized.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (eca1577): comparison URL.

Overall result: ❌ regressions - 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)
0.4% [0.1%, 0.8%] 15
Regressions ❌
(secondary)
0.8% [0.8%, 0.8%] 1
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 0.4% [0.1%, 0.8%] 15

Max RSS (memory usage)

Results (primary -0.8%, secondary 4.1%)

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

mean range count
Regressions ❌
(primary)
2.7% [2.7%, 2.7%] 1
Regressions ❌
(secondary)
4.1% [4.1%, 4.1%] 1
Improvements ✅
(primary)
-2.5% [-3.3%, -1.7%] 2
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) -0.8% [-3.3%, 2.7%] 3

Cycles

Results (secondary -4.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)
-4.6% [-5.8%, -3.4%] 2
All ❌✅ (primary) - - 0

Binary size

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

Bootstrap: 465.776s -> 464.957s (-0.18%)
Artifact size: 374.73 MiB -> 374.71 MiB (-0.01%)

@rustbot rustbot added the perf-regression Performance regression. label Jul 13, 2025
github-actions bot pushed a commit to rust-lang/miri that referenced this pull request Jul 14, 2025
Rollup of 11 pull requests

Successful merges:

 - rust-lang/rust#143301 (`tests/ui`: A New Order [26/N])
 - rust-lang/rust#143519 (Check assoc consts and tys later like assoc fns)
 - rust-lang/rust#143554 (slice: Mark `rotate_left`, `rotate_right` unstably const)
 - rust-lang/rust#143634 (interpret/allocation: expose init + write_wildcards on a range)
 - rust-lang/rust#143685 (Resolve: merge `source_bindings` and `target_bindings` into `bindings`)
 - rust-lang/rust#143734 (Refactor resolve resolution bindings)
 - rust-lang/rust#143774 (constify `From` and `Into`)
 - rust-lang/rust#143785 (Add --compile-time-deps argument for x check)
 - rust-lang/rust#143786 (Fix fallback for CI_JOB_NAME)
 - rust-lang/rust#143825 (clippy: fix test filtering when TESTNAME is empty)
 - rust-lang/rust#143826 (Fix command trace)

r? `@ghost`
`@rustbot` modify labels: rollup
@Kobzol
Copy link
Member

Kobzol commented Jul 14, 2025

This PR caused the small perf. regression in #143888. @petrochenkov Do you think there's something to improve here, or do we just eat the regression, since this was a lint improvement?

@mu001999 mu001999 deleted the dead-code/impl-items branch July 14, 2025 09:36
@petrochenkov
Copy link
Contributor

Associated type resolution can be improved - #143519 (comment).
I think it makes sense to create an issue for storing resolutions for associated type constraints somewhere (if they are not yet stored) instead of doing resolution again and again.

Although I'm not sure this is the source of the regression here.
We are just doing more work for associated types and constants here, and typenum is exactly a crate where that shows.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
perf-regression Performance regression. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Associated constants used in equality constraints should be considered as used
7 participants