@@ -1042,30 +1042,31 @@ impl<'a, 'tcx> CastCheck<'tcx> {
10421042 m_cast : ty:: TypeAndMut < ' tcx > ,
10431043 ) -> Result < CastKind , CastError < ' tcx > > {
10441044 // array-ptr-cast: allow mut-to-mut, mut-to-const, const-to-const
1045- if m_expr. mutbl >= m_cast. mutbl {
1046- if let ty:: Array ( ety, _) = m_expr. ty . kind ( ) {
1047- // Due to the limitations of LLVM global constants,
1048- // region pointers end up pointing at copies of
1049- // vector elements instead of the original values.
1050- // To allow raw pointers to work correctly, we
1051- // need to special-case obtaining a raw pointer
1052- // from a region pointer to a vector.
1053-
1054- // Coerce to a raw pointer so that we generate RawPtr in MIR.
1055- let array_ptr_type = Ty :: new_ptr ( fcx. tcx , m_expr. ty , m_expr. mutbl ) ;
1056- fcx. coerce ( self . expr , self . expr_ty , array_ptr_type, AllowTwoPhase :: No , None )
1057- . unwrap_or_else ( |_| {
1058- bug ! (
1045+ if m_expr. mutbl >= m_cast. mutbl
1046+ && let ty:: Array ( ety, _) = m_expr. ty . kind ( )
1047+ && fcx. can_eq ( fcx. param_env , * ety, m_cast. ty )
1048+ {
1049+ // Due to the limitations of LLVM global constants,
1050+ // region pointers end up pointing at copies of
1051+ // vector elements instead of the original values.
1052+ // To allow raw pointers to work correctly, we
1053+ // need to special-case obtaining a raw pointer
1054+ // from a region pointer to a vector.
1055+
1056+ // Coerce to a raw pointer so that we generate RawPtr in MIR.
1057+ let array_ptr_type = Ty :: new_ptr ( fcx. tcx , m_expr. ty , m_expr. mutbl ) ;
1058+ fcx. coerce ( self . expr , self . expr_ty , array_ptr_type, AllowTwoPhase :: No , None )
1059+ . unwrap_or_else ( |_| {
1060+ bug ! (
10591061 "could not cast from reference to array to pointer to array ({:?} to {:?})" ,
10601062 self . expr_ty,
10611063 array_ptr_type,
10621064 )
1063- } ) ;
1065+ } ) ;
10641066
1065- // this will report a type mismatch if needed
1066- fcx. demand_eqtype ( self . span , * ety, m_cast. ty ) ;
1067- return Ok ( CastKind :: ArrayPtrCast ) ;
1068- }
1067+ // this will report a type mismatch if needed
1068+ fcx. demand_eqtype ( self . span , * ety, m_cast. ty ) ;
1069+ return Ok ( CastKind :: ArrayPtrCast ) ;
10691070 }
10701071
10711072 Err ( CastError :: IllegalCast )
0 commit comments