Rollup merge of #138962 - xizheyin:issue-138836, r=compiler-errors
Expect an array when expected and acutal types are both arrays during cast Closes #138836
This commit is contained in:
commit
60c76b9dfd
5 changed files with 37 additions and 22 deletions
|
@ -1042,30 +1042,31 @@ impl<'a, 'tcx> CastCheck<'tcx> {
|
||||||
m_cast: ty::TypeAndMut<'tcx>,
|
m_cast: ty::TypeAndMut<'tcx>,
|
||||||
) -> Result<CastKind, CastError<'tcx>> {
|
) -> Result<CastKind, CastError<'tcx>> {
|
||||||
// array-ptr-cast: allow mut-to-mut, mut-to-const, const-to-const
|
// array-ptr-cast: allow mut-to-mut, mut-to-const, const-to-const
|
||||||
if m_expr.mutbl >= m_cast.mutbl {
|
if m_expr.mutbl >= m_cast.mutbl
|
||||||
if let ty::Array(ety, _) = m_expr.ty.kind() {
|
&& let ty::Array(ety, _) = m_expr.ty.kind()
|
||||||
// Due to the limitations of LLVM global constants,
|
&& fcx.can_eq(fcx.param_env, *ety, m_cast.ty)
|
||||||
// region pointers end up pointing at copies of
|
{
|
||||||
// vector elements instead of the original values.
|
// Due to the limitations of LLVM global constants,
|
||||||
// To allow raw pointers to work correctly, we
|
// region pointers end up pointing at copies of
|
||||||
// need to special-case obtaining a raw pointer
|
// vector elements instead of the original values.
|
||||||
// from a region pointer to a vector.
|
// To allow raw pointers to work correctly, we
|
||||||
|
// need to special-case obtaining a raw pointer
|
||||||
|
// from a region pointer to a vector.
|
||||||
|
|
||||||
// Coerce to a raw pointer so that we generate RawPtr in MIR.
|
// Coerce to a raw pointer so that we generate RawPtr in MIR.
|
||||||
let array_ptr_type = Ty::new_ptr(fcx.tcx, m_expr.ty, m_expr.mutbl);
|
let array_ptr_type = Ty::new_ptr(fcx.tcx, m_expr.ty, m_expr.mutbl);
|
||||||
fcx.coerce(self.expr, self.expr_ty, array_ptr_type, AllowTwoPhase::No, None)
|
fcx.coerce(self.expr, self.expr_ty, array_ptr_type, AllowTwoPhase::No, None)
|
||||||
.unwrap_or_else(|_| {
|
.unwrap_or_else(|_| {
|
||||||
bug!(
|
bug!(
|
||||||
"could not cast from reference to array to pointer to array ({:?} to {:?})",
|
"could not cast from reference to array to pointer to array ({:?} to {:?})",
|
||||||
self.expr_ty,
|
self.expr_ty,
|
||||||
array_ptr_type,
|
array_ptr_type,
|
||||||
)
|
)
|
||||||
});
|
});
|
||||||
|
|
||||||
// this will report a type mismatch if needed
|
// this will report a type mismatch if needed
|
||||||
fcx.demand_eqtype(self.span, *ety, m_cast.ty);
|
fcx.demand_eqtype(self.span, *ety, m_cast.ty);
|
||||||
return Ok(CastKind::ArrayPtrCast);
|
return Ok(CastKind::ArrayPtrCast);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Err(CastError::IllegalCast)
|
Err(CastError::IllegalCast)
|
||||||
|
|
5
tests/ui/cast/cast-array-issue-138836.rs
Normal file
5
tests/ui/cast/cast-array-issue-138836.rs
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
fn main() {
|
||||||
|
let a: [u8; 3] = [1,2,3];
|
||||||
|
let b = &a;
|
||||||
|
let c = b as *const [u32; 3]; //~ ERROR casting `&[u8; 3]` as `*const [u32; 3]` is invalid
|
||||||
|
}
|
9
tests/ui/cast/cast-array-issue-138836.stderr
Normal file
9
tests/ui/cast/cast-array-issue-138836.stderr
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
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];
|
||||||
|
| ^^^^^^^^^^^^^^^^^^^^
|
||||||
|
|
||||||
|
error: aborting due to 1 previous error
|
||||||
|
|
||||||
|
For more information about this error, try `rustc --explain E0606`.
|
|
@ -1,5 +1,5 @@
|
||||||
const a: [u8; 3] = ['h' as u8, 'i' as u8, 0 as u8];
|
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() {
|
fn main() {
|
||||||
}
|
}
|
||||||
|
|
|
@ -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
|
--> $DIR/const-cast-wrong-type.rs:2:22
|
||||||
|
|
|
|
||||||
LL | const b: *const i8 = &a as *const i8;
|
LL | const b: *const i8 = &a as *const i8;
|
||||||
| ^^^^^^^^^^^^^^^ expected `u8`, found `i8`
|
| ^^^^^^^^^^^^^^^
|
||||||
|
|
||||||
error: aborting due to 1 previous error
|
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`.
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue