Skip to content
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

False positive dead code warning when using combination of array bound with constant function and trait implementation #133797

Open
tvsfx opened this issue Dec 3, 2024 · 0 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@tvsfx
Copy link

tvsfx commented Dec 3, 2024

Code

const fn test() -> usize {
    0
}

trait Test {}

impl Test for [u8; test()] {}

// NOTE: this constitutes a use
//trait _Test2 {
//    type Ty;
//}
//
//impl _Test2 for String {
//    type Ty = [u8; test()];
//}

fn test2<T: Test>() {}

fn main() {
    test2::<[u8; 0]>();

    // NOTE: this constitutes a use
    //let _x: [u8; test()] = [];
    //
}

Current output

Compiling playground v0.0.1 (/playground)
warning: function `test` is never used
 --> src/main.rs:1:10
  |
1 | const fn test() -> usize {
  |          ^^^^
  |
  = note: `#[warn(dead_code)]` on by default

warning: `playground` (bin "playground") generated 1 warning
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.89s
     Running `target/debug/playground`

Desired output

None.

Rationale and extra context

Seems related to #128617, but that problem has been resolved in the meantime.

Other cases

The code is used, so no warning should be displayed. Note that the two commented-out blocks fix the warning and *are* considered uses, so there seems to be a specific interaction with trait implementations.

Rust Version

The warning only starts showing up in Rust 1.78, and seems to show up in any build since: 

Note the difference between
https://rust.godbolt.org/z/43vh6vv7s
vs.
https://rust.godbolt.org/z/djP9qM7xa

Anything else?

No response

@tvsfx tvsfx added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Dec 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

1 participant