Skip to content

False positive: Non-primitive type cast error if the function has a constrained lifetime #20466

@Jason5Lee

Description

@Jason5Lee

rust-analyzer version: 0.3.2577-standalone (9db0550 2025-08-11)

rustc version: 1.88.0 (6b00bc388 2025-06-23)

editor or extension: Zed and VSCode (rust-analyzer extension)

code snippet to reproduce:

fn test2<'a>()
// If the following line is commented out, the false-positive error disappears, even though the lifetime has no connection to the error expression.
where 'a: 'static,
{
    let _: &[i32] = &[1i32] as &[_]; // False positive: non-primitive cast: &[i32; 1] as &[i32]
}

I’m uncertain whether this is the same as one of the "non-primitive cast" issues, as I can only reproduce it using macros—specifically, both async-trait and log. I’m unsure of the exact expanded code triggers this issue. Currently, I’ve found that if I attempt to log with a key-value pair inside an async function implemented for an async trait (using the async-trait crate rather than the built-in async trait mechanism), it produces a false positive error, whereas cargo check shows no issues.
I've identified the minimal reproducible example. The issue arises when a function has a constrained lifetime (whether bound to another lifetime or 'static). In such cases, casting a borrow of an array to a borrow of a slice triggers a non-primitive cast error—even the lifetime is not related. However, if the lifetime is unconstrained (or absent entirely), the cast works without issues.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-tytype system / type inference / traits / method resolutionC-bugCategory: bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions