-
Notifications
You must be signed in to change notification settings - Fork 1.7k
fix: Yet another false positive invalid cast diagnostic #19432
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -440,8 +440,9 @@ fn main() { | |||||||||||||||||||||||||||||||||||||||||||||||||||||
q as *const [i32]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
//^^^^^^^^^^^^^^^^^ error: cannot cast thin pointer `*const i32` to fat pointer `*const [i32]` | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
// FIXME: This should emit diagnostics but disabled to prevent many false positives | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
let t: *mut (dyn Trait + 'static) = 0 as *mut _; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
//^^^^^^^^^^^ error: cannot cast `usize` to a fat pointer `*mut _` | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
let mut fail: *const str = 0 as *const str; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
//^^^^^^^^^^^^^^^ error: cannot cast `usize` to a fat pointer `*const str` | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
let mut fail2: *const str = 0isize as *const str; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -1161,6 +1162,49 @@ struct ZerocopyKnownLayoutMaybeUninit(<<Flexible as Field>::Type as KnownLayout> | |||||||||||||||||||||||||||||||||||||||||||||||||||||
fn test(ptr: *mut [u8]) -> *mut ZerocopyKnownLayoutMaybeUninit { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
ptr as *mut _ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
"#, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
#[test] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
fn regression_19431() { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
check_diagnostics( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
r#" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
//- minicore: coerce_unsized | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
struct Dst([u8]); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
struct Struct { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
body: Dst, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
trait Field { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
type Type: ?Sized; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
impl Field for Struct { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
type Type = Dst; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
trait KnownLayout { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
type MaybeUninit: ?Sized; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
type PointerMetadata; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
impl<T> KnownLayout for [T] { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
type MaybeUninit = [T]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
type PointerMetadata = usize; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
impl KnownLayout for Dst { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
type MaybeUninit = Dst; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
type PointerMetadata = <[u8] as KnownLayout>::PointerMetadata; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
struct ZerocopyKnownLayoutMaybeUninit(<<Struct as Field>::Type as KnownLayout>::MaybeUninit); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
fn test(ptr: *mut ZerocopyKnownLayoutMaybeUninit) -> *mut <<Struct as Field>::Type as KnownLayout>::MaybeUninit { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
ptr as *mut _ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In rust-analyzer/crates/hir-ty/src/infer/unify.rs Lines 919 to 944 in 37acea8
Ideally, this should anyway return So, I added a function eagerly resolves such nested inference vars and projection types. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
"#, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We have disabled diagnostics on casts into
*const dyn Trait
hererust-analyzer/crates/hir-ty/src/infer/cast.rs
Lines 125 to 130 in 37acea8
but this somehow survived it by hiding behinde inference var 😅