-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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 non-primitive cast
error in recent nightly
#18047
Comments
CC #11847. We might have to mark it as experimental 😞. |
Is your codebase private? |
This seems to be caused from coerce_unsize as you said. I'll try with the way rustc handles this 🤔 |
Our CoerceUnsized solving sadly isn't quite correct and probably can't be fixed before the new trait solver is integrated :/ |
It might be turned out otherwise, but I guess it could be. |
It uses trait selection (the |
Rustc branches on |
No, |
Oh, I see 😢 |
So, we have another reason(one of so many) to quickly migrate into next-gen solver. I hope we could do so in near future.
|
@ShoyuVanilla yes, it's private so unfortunately I haven't reproduced it with anything I can share here. |
My preference would be 3 if it's not too hard to implement, otherwise 1? |
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. |
Mine, too 😄 Then I'll try with it |
The root cause of this might be a bit different from #11847 as we are calling rust-analyzer/crates/hir-ty/src/infer.rs Lines 712 to 725 in 124c748
The following test just passes in our current revision; #[test]
fn cast_into_dst() {
check_diagnostics(
r#"
//- minicore: fmt, builtin_impls, coerce_unsized, dispatch_from_dyn
struct Box<T: ?Sized>(*const T);
impl<T, U> core::ops::CoerceUnsized<Box<U>> for Box<T>
where
T: Unsize<U> + ?Sized,
U: ?Sized,
{
}
impl<T: ?Sized> Box<T> {
fn new(_: T) -> Self {
loop {}
}
}
fn test() {
let _ = &42 as &dyn core::fmt::Debug;
let _ = Box::new(7) as Box<dyn core::fmt::Debug>;
let _ = Box::new([4]) as Box<[i32]>;
}
"#,
)
} I'll look into this more 🧐 |
Not sure since I coundn't reproduce this but this seems to be more complicated than first expectations.
#[lang = "format_arguments"]
#[stable(feature = "rust1", since = "1.0.0")]
#[derive(Copy, Clone)]
pub struct Arguments<'a> {
// Format string pieces to print.
pieces: &'a [&'static str],
// Placeholder specs, or `None` if all specs are default (as in "{}{}").
fmt: Option<&'a [rt::Placeholder]>,
// Dynamic arguments for interpolation, to be interleaved with string
// pieces. (Every argument is preceded by a string piece.)
args: &'a [rt::Argument<'a>],
} This struct doesn't have any generic parameter other than lifetime one
The first one immediately has error as you said, and the second one took a while to show up an error -> this is the clue. I think that the first one is from the rust-analyzer's native diagnostics - and this is not false positive because Of course, it might be the case that just second one took more time to evaluate but if the native-flycheck difference is the case, your example code is another issue that has something to do with flycheck. To verify this, you may test with just typing the second line |
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 When the marked line is commented out, the code compiles fine, but when it's present rust-analyzer gives the error. In both cases Running with a slightly updated RA pre-release this time, |
Oh, it would be really helpful to fix this issue. Thanks! |
Miri also hits this incorrect diagnostics, in this line. |
Minimized with no dependency other than std trait Foo {
type Error;
type Assoc;
}
trait Bar {
type Error;
}
type Error = Box<dyn std::error::Error>;
trait Baz {
type Assoc: Bar;
type Error: Into<Error>;
}
impl<T, U> Baz for T
where
T: Foo<Assoc = U>,
T::Error: Into<Error>,
U: Bar,
<U as Bar>::Error: Into<Error>,
{
type Assoc = U;
type Error = T::Error;
}
struct S;
trait Trait {}
impl Trait for S {}
fn test<T>()
where
T: Baz,
T::Assoc: 'static
{
let _ = &S as &dyn Trait;
//^^^^^^^^^^^^^^^^ ERROR
} |
Running
and Maybe we are messing up with chalk for just simple coercion with complecated trait env. A line from the log;
So, my hypothesis is;
I think that the miri one is another thing though 🤔 |
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. |
Further testing shows indeterministic results 🤔 fn main() {
println!("Hello, world!");
}
pub trait From1<T>: Sized {
fn from(_: T) -> Self;
}
pub trait Into1<T>: Sized {
fn into(self) -> T;
}
impl<T, U> Into1<U> for T
where
U: From1<T>,
{
fn into(self) -> U {
U::from(self)
}
}
impl<T> From1<T> for T {
fn from(t: T) -> T {
t
}
}
trait Foo {
type ErrorType;
type Assoc;
}
trait Bar {
type ErrorType;
}
trait ErrorLike {}
struct BoxLike<T: ?Sized>(*const T);
impl<'a, E> From1<E> for BoxLike<dyn ErrorLike>
where
E: Trait + 'a,
{
fn from(_: E) -> Self {
loop {}
}
}
type BoxedError = BoxLike<dyn ErrorLike>;
trait Baz {
type Assoc: Bar;
type Error: Into1<BoxedError>;
}
impl<T, U> Baz for T
where
T: Foo<Assoc = U>,
T::ErrorType: Into1<BoxedError>,
U: Bar,
<U as Bar>::ErrorType: Into1<BoxedError>,
{
type Assoc = U;
type Error = T::ErrorType;
}
struct S;
trait Trait {}
impl Trait for S {}
fn test<T>()
where
T: Baz,
T::Assoc: 'static,
{
let _ = &S as &dyn Trait;
} running rust-analyzer/crates/hir-ty/src/infer/cast.rs Lines 114 to 121 in e35227d
and sometimes with coercion failure, as before. Obviously, the given types and coercions are nothing to do with fn test<T>()
where
T: Baz,
T::Assoc: 'static,
{
let _ = &S as &dyn Trait;
} but commenting out Maybe the chalk thinks that lifetime bounds and trait |
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 😢 |
Skip checks for cast to dyn traits It seems that chalk fails to solve some obvious goals when there are some recursiveness in trait environments. And it doesn't support trait upcasting yet. rust-lang/chalk#796 This PR just skips for casting into types containing `dyn Trait` to prevent false positive diagnostics like #18047 and #18083
I think that this false positives would be hidden in the nightly release with #18093 though it isn't really fixed in the right way(we need next-gen trait solver to fix this) |
Same issue |
Still getting this on nightly release? |
Got this on stable converting the |
@kameko Are you still getting this on stable? |
I was experiencing this issue and had to downgrade for a bit, and I can confirm that the current version has been corrected. |
When working for a while in a large codebase, I get a lot of errors like this one from
tracing::info!
macro invocations:This seems to come from an expression
&(format_args!(...)) as &dyn Value
in the macro expansion, which should work (and works according torustc
).std::fmt::Arguments<'_>
implementstracing_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.
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
The text was updated successfully, but these errors were encountered: