Skip to content

Commit 740ebae

Browse files
committed
Expect an array when expected and acutal types are both arrays during cast
Signed-off-by: xizheyin <xizheyin@smail.nju.edu.cn>
1 parent 8962536 commit 740ebae

File tree

1 file changed

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

1 file changed

+15
-1
lines changed

compiler/rustc_hir_typeck/src/cast.rs

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

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

0 commit comments

Comments
 (0)