Incorrect "never used" warning when using `const` functions in array bounds for trait implementations

I seem to have hit some strange compiler edge case, in the following example (playground)

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()] = [];
}

This results in a dead_code warning, because the use of the test function in the impl Test block is not detected. Strangely enough, it is detected in the two alternative scenarios that I commented out. Am I missing something, or is this a bug?

Edit: likely related to False positive `dead_code` for const used as const generic argument during impl marker trait · Issue #128617 · rust-lang/rust · GitHub (although this particular example has been fixed)

1 Like

I'd open a new issue and comment on the existing issue. The warning only starts showing up in Rust 1.78.

Thank you for the godbolt link :slight_smile:
Ive filed an issue in the meantime: False positive dead code warning when using combination of array bound with constant function and trait implementation · Issue #133797 · rust-lang/rust · GitHub

1 Like