@@ -10,9 +10,9 @@ use rustc_session::config::OptLevel;
1010use rustc_span:: { DUMMY_SP , Span } ;
1111use tracing:: { debug, instrument} ;
1212
13- use super :: FunctionCx ;
1413use super :: operand:: { OperandRef , OperandValue } ;
1514use super :: place:: PlaceRef ;
15+ use super :: { FunctionCx , LocalRef } ;
1616use crate :: common:: IntPredicate ;
1717use crate :: traits:: * ;
1818use crate :: { MemFlags , base} ;
@@ -593,6 +593,14 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
593593 self . codegen_place_to_pointer ( bx, place, mk_ptr)
594594 }
595595
596+ mir:: Rvalue :: Len ( place) => {
597+ let size = self . evaluate_array_len ( bx, place) ;
598+ OperandRef {
599+ val : OperandValue :: Immediate ( size) ,
600+ layout : bx. cx ( ) . layout_of ( bx. tcx ( ) . types . usize ) ,
601+ }
602+ }
603+
596604 mir:: Rvalue :: BinaryOp ( op_with_overflow, box ( ref lhs, ref rhs) )
597605 if let Some ( op) = op_with_overflow. overflowing_to_wrapping ( ) =>
598606 {
@@ -792,6 +800,24 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
792800 }
793801 }
794802
803+ fn evaluate_array_len ( & mut self , bx : & mut Bx , place : mir:: Place < ' tcx > ) -> Bx :: Value {
804+ // ZST are passed as operands and require special handling
805+ // because codegen_place() panics if Local is operand.
806+ if let Some ( index) = place. as_local ( ) {
807+ if let LocalRef :: Operand ( op) = self . locals [ index] {
808+ if let ty:: Array ( _, n) = op. layout . ty . kind ( ) {
809+ let n = n
810+ . try_to_target_usize ( bx. tcx ( ) )
811+ . expect ( "expected monomorphic const in codegen" ) ;
812+ return bx. cx ( ) . const_usize ( n) ;
813+ }
814+ }
815+ }
816+ // use common size calculation for non zero-sized types
817+ let cg_value = self . codegen_place ( bx, place. as_ref ( ) ) ;
818+ cg_value. len ( bx. cx ( ) )
819+ }
820+
795821 /// Codegen an `Rvalue::RawPtr` or `Rvalue::Ref`
796822 fn codegen_place_to_pointer (
797823 & mut self ,
@@ -1063,6 +1089,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
10631089 mir:: Rvalue :: Ref ( ..) |
10641090 mir:: Rvalue :: CopyForDeref ( ..) |
10651091 mir:: Rvalue :: RawPtr ( ..) |
1092+ mir:: Rvalue :: Len ( ..) |
10661093 mir:: Rvalue :: Cast ( ..) | // (*)
10671094 mir:: Rvalue :: ShallowInitBox ( ..) | // (*)
10681095 mir:: Rvalue :: BinaryOp ( ..) |
0 commit comments