Skip to content

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

Open
@tvsfx

Description

@tvsfx

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions