@@ -2,7 +2,7 @@ use crate::mir::pretty::{function_body, pretty_statement};
22use crate :: ty:: {
33 AdtDef , ClosureDef , Const , CoroutineDef , GenericArgs , Movability , Region , RigidTy , Ty , TyKind ,
44} ;
5- use crate :: { Error , Span , Opaque } ;
5+ use crate :: { Error , Opaque , Span } ;
66use std:: io;
77
88/// The SMIR representation of a single function.
@@ -685,40 +685,46 @@ impl Place {
685685 self . projection . iter ( ) . fold ( Ok ( start_ty) , |place_ty, elem| {
686686 let ty = place_ty?;
687687 match elem {
688- ProjectionElem :: Deref => {
689- let deref_ty = ty
690- . kind ( )
691- . builtin_deref ( true )
692- . ok_or_else ( || error ! ( "Cannot dereference type: {ty:?}" ) ) ?;
693- Ok ( deref_ty. ty )
694- }
688+ ProjectionElem :: Deref => Self :: deref_ty ( ty) ,
695689 ProjectionElem :: Field ( _idx, fty) => Ok ( * fty) ,
696- ProjectionElem :: Index ( _) | ProjectionElem :: ConstantIndex { .. } => ty
697- . kind ( )
698- . builtin_index ( )
699- . ok_or_else ( || error ! ( "Cannot index non-array type: {ty:?}" ) ) ,
690+ ProjectionElem :: Index ( _) | ProjectionElem :: ConstantIndex { .. } => {
691+ Self :: index_ty ( ty)
692+ }
700693 ProjectionElem :: Subslice { from, to, from_end } => {
701- let ty_kind = ty. kind ( ) ;
702- match ty_kind {
703- TyKind :: RigidTy ( RigidTy :: Slice ( ..) ) => Ok ( ty) ,
704- TyKind :: RigidTy ( RigidTy :: Array ( inner, _) ) if !from_end => {
705- Ty :: try_new_array (
706- inner,
707- to. checked_sub ( * from)
708- . ok_or_else ( || error ! ( "Subslice overflow: {from}..{to}" ) ) ?,
709- )
710- }
711- TyKind :: RigidTy ( RigidTy :: Array ( inner, size) ) => {
712- let size = size. eval_target_usize ( ) ?;
713- let len = size - from - to;
714- Ty :: try_new_array ( inner, len)
715- }
716- _ => Err ( Error ( format ! ( "Cannot subslice non-array type: `{ty_kind:?}`" ) ) ) ,
717- }
694+ Self :: subslice_ty ( ty, from, to, from_end)
718695 }
719696 ProjectionElem :: Downcast ( _) => Ok ( ty) ,
720697 ProjectionElem :: OpaqueCast ( ty) | ProjectionElem :: Subtype ( ty) => Ok ( * ty) ,
721698 }
722699 } )
723700 }
701+
702+ fn index_ty ( ty : Ty ) -> Result < Ty , Error > {
703+ ty. kind ( ) . builtin_index ( ) . ok_or_else ( || error ! ( "Cannot index non-array type: {ty:?}" ) )
704+ }
705+
706+ fn subslice_ty ( ty : Ty , from : & u64 , to : & u64 , from_end : & bool ) -> Result < Ty , Error > {
707+ let ty_kind = ty. kind ( ) ;
708+ match ty_kind {
709+ TyKind :: RigidTy ( RigidTy :: Slice ( ..) ) => Ok ( ty) ,
710+ TyKind :: RigidTy ( RigidTy :: Array ( inner, _) ) if !from_end => Ty :: try_new_array (
711+ inner,
712+ to. checked_sub ( * from) . ok_or_else ( || error ! ( "Subslice overflow: {from}..{to}" ) ) ?,
713+ ) ,
714+ TyKind :: RigidTy ( RigidTy :: Array ( inner, size) ) => {
715+ let size = size. eval_target_usize ( ) ?;
716+ let len = size - from - to;
717+ Ty :: try_new_array ( inner, len)
718+ }
719+ _ => Err ( Error ( format ! ( "Cannot subslice non-array type: `{ty_kind:?}`" ) ) ) ,
720+ }
721+ }
722+
723+ fn deref_ty ( ty : Ty ) -> Result < Ty , Error > {
724+ let deref_ty = ty
725+ . kind ( )
726+ . builtin_deref ( true )
727+ . ok_or_else ( || error ! ( "Cannot dereference type: {ty:?}" ) ) ?;
728+ Ok ( deref_ty. ty )
729+ }
724730}
0 commit comments