fix: Yet another false positive invalid cast diagnostic

This commit is contained in:
Shoyu Vanilla 2025-03-24 02:34:24 +09:00
parent 37acea8052
commit 9cfea3ec29
3 changed files with 134 additions and 26 deletions

View file

@ -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 _
}
"#,
);
}