rust-analyzer: False positive `non-primitive cast` error in recent nightly

When working for a while in a large codebase, I get a lot of errors like this one from tracing::info! macro invocations:

non-primitive cast: &Arguments<_> as &dyn Value

This seems to come from an expression &(format_args!(...)) as &dyn Value in the macro expansion, which should work (and works according to rustc). std::fmt::Arguments<'_> implements tracing_core::field::Value, however rust-analyzer seems to in some cases still produce an error.

I’ve so far been unable to create a minimal reproducible example, I’ve only so far seen this in a fairly large (170k LOC) commercial codebase that I’m working with. Restarting rust-analyzer does make the error go away for a while, in my case for about 1.5 minutes after other errors are visible.

let _ = &format_args!("...") as &dyn std::io::Write; // <-- immediately has error
let _ = &format_args!("...") as &dyn tracing::Value; // <-- has an error after a while

The errors started showing up after updating the rust-analyzer extension earlier today, not sure what the version prior to the update was. The errors seem to not be present on the latest stable release (v0.3.2096). It seems like it may be related to the recently merged #17984.


rust-analyzer version: rust-analyzer version: 0.4.2098-standalone

rustc version: rustc 1.80.1 (3f5fd8dd4 2024-08-06)

editor or extension: VSCode, extension version v0.4.2098 (pre-release)

relevant settings: none that I can find

About this issue

  • Original URL
  • State: open
  • Created 2 years ago
  • Reactions: 8
  • Comments: 31 (25 by maintainers)

Commits related to this issue

Most upvoted comments

Just chiming in to let you know that I have hit this exact bug on stable (that has been released yesterday).

Downgrading VS Code extension to previous version helped.

I’m preparing a workaround fix for this. Sorry for the inconvenience 😢

So I poked around in the code a bit more, and managed to actually get a reproducible example in a separate workspace:

use tonic::{body::BoxBody, client::GrpcService};

pub fn foo<S>()
where
    S: GrpcService<BoxBody>,
    S::ResponseBody: 'static, // <--- key line
{
    let _ = &format_args!("...") as &dyn tracing::Value;
}

(using tonic 0.12.2)

When the marked line is commented out, the code compiles fine, but when it’s present rust-analyzer gives the error. In both cases cargo clippy and cargo check produce no errors.

Running with a slightly updated RA pre-release this time, v0.4.2103.

Just chiming in to let you know that I have hit this exact bug on stable (that has been released yesterday).

Downgrading VS Code extension to previous version helped.

We are likely not spawning a bigger stack thread for analysis-stats (the server runs on a stack with a bigger thread usually) hence the overflow.

Miri also hits this incorrect diagnostics, in this line.

I’ve only so far seen this in a fairly large (170k LOC) commercial codebase that I’m working with.

Is your codebase private?

@ShoyuVanilla yes, it’s private so unfortunately I haven’t reproduced it with anything I can share here.

No worries. Could you please tell me whether these false positive diagnostics still exist once the makeshift fix has been released? I’ll mention you here when the nightly release done.

No, select isn’t the same as full trait solving.

CC #11847. We might have to mark it as experimental 😞.