1
Fork 0

Expect an array when expected and acutal types are both arrays during cast

Signed-off-by: xizheyin <xizheyin@smail.nju.edu.cn>
This commit is contained in:
xizheyin 2025-03-26 11:31:58 +08:00
parent 89625360ec
commit a34c42fefa
No known key found for this signature in database
GPG key ID: 0A0D90BE99CEDEAD
5 changed files with 27 additions and 29 deletions

View file

@ -1042,8 +1042,10 @@ impl<'a, 'tcx> CastCheck<'tcx> {
m_cast: ty::TypeAndMut<'tcx>,
) -> Result<CastKind, CastError<'tcx>> {
// array-ptr-cast: allow mut-to-mut, mut-to-const, const-to-const
if m_expr.mutbl >= m_cast.mutbl {
if let ty::Array(ety, _) = m_expr.ty.kind() {
if m_expr.mutbl >= m_cast.mutbl
&& let ty::Array(ety, _) = m_expr.ty.kind()
&& fcx.can_eq(fcx.param_env, *ety, m_cast.ty)
{
// Due to the limitations of LLVM global constants,
// region pointers end up pointing at copies of
// vector elements instead of the original values.
@ -1066,7 +1068,6 @@ impl<'a, 'tcx> CastCheck<'tcx> {
fcx.demand_eqtype(self.span, *ety, m_cast.ty);
return Ok(CastKind::ArrayPtrCast);
}
}
Err(CastError::IllegalCast)
}

View file

@ -1,5 +1,5 @@
fn main() {
let a: [u8; 3] = [1,2,3];
let b = &a;
let c = b as *const [u32; 3]; //~ ERROR mismatched types [E0308]
let c = b as *const [u32; 3]; //~ ERROR casting `&[u8; 3]` as `*const [u32; 3]` is invalid
}

View file

@ -1,12 +1,9 @@
error[E0308]: mismatched types
error[E0606]: casting `&[u8; 3]` as `*const [u32; 3]` is invalid
--> $DIR/cast-array-issue-138836.rs:4:13
|
LL | let c = b as *const [u32; 3];
| ^^^^^^^^^^^^^^^^^^^^ expected `[u8; 3]`, found `[u32; 3]`
|
= note: expected array `[u8; 3]`
found array `[u32; 3]`
| ^^^^^^^^^^^^^^^^^^^^
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0308`.
For more information about this error, try `rustc --explain E0606`.

View file

@ -1,5 +1,5 @@
const a: [u8; 3] = ['h' as u8, 'i' as u8, 0 as u8];
const b: *const i8 = &a as *const i8; //~ ERROR mismatched types
const b: *const i8 = &a as *const i8; //~ ERROR casting `&[u8; 3]` as `*const i8` is invalid
fn main() {
}

View file

@ -1,9 +1,9 @@
error[E0308]: mismatched types
error[E0606]: casting `&[u8; 3]` as `*const i8` is invalid
--> $DIR/const-cast-wrong-type.rs:2:22
|
LL | const b: *const i8 = &a as *const i8;
| ^^^^^^^^^^^^^^^ expected `u8`, found `i8`
| ^^^^^^^^^^^^^^^
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0308`.
For more information about this error, try `rustc --explain E0606`.