Skip to content

Commit 9735b0e

Browse files
committed
Expect an array when expected and acutal types are both arrays during cast
Signed-off-by: xizheyin <[email protected]>
1 parent 8962536 commit 9735b0e

File tree

1 file changed

+19
-1
lines changed
  • compiler/rustc_hir_typeck/src

1 file changed

+19
-1
lines changed

compiler/rustc_hir_typeck/src/cast.rs

+19-1
Original file line numberDiff line numberDiff line change
@@ -1062,8 +1062,26 @@ impl<'a, 'tcx> CastCheck<'tcx> {
10621062
)
10631063
});
10641064

1065+
let erased_ety = fcx.tcx.erase_regions(*ety);
1066+
let erased_m_expr = fcx.tcx.erase_regions(m_expr.ty);
1067+
let erased_m_cast = fcx.tcx.erase_regions(m_cast.ty);
1068+
1069+
let expected = if matches!(m_cast.ty.kind(), ty::Array(_, _))
1070+
// [ty1; N] != [ty2; M]
1071+
&& !erased_m_expr.eq(&erased_m_cast)
1072+
// ty1 != [ty2; M]
1073+
&& !erased_ety.eq(&erased_m_cast)
1074+
{
1075+
// if the expected type is [ty1; N] and the actual type is [ty2; M], and ty1 is not [ty2; M]
1076+
// then we need to use [ty1; N] as the expected type
1077+
m_expr.ty
1078+
} else {
1079+
// otherwise, use the expected type as the expected type
1080+
*ety
1081+
};
1082+
10651083
// this will report a type mismatch if needed
1066-
fcx.demand_eqtype(self.span, *ety, m_cast.ty);
1084+
fcx.demand_eqtype(self.span, expected, m_cast.ty);
10671085
return Ok(CastKind::ArrayPtrCast);
10681086
}
10691087
}

0 commit comments

Comments
 (0)