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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

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````
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
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
5 participants