@@ -10,9 +10,9 @@ use rustc_session::config::OptLevel;
10
10
use rustc_span:: { DUMMY_SP , Span } ;
11
11
use tracing:: { debug, instrument} ;
12
12
13
- use super :: FunctionCx ;
14
13
use super :: operand:: { OperandRef , OperandValue } ;
15
14
use super :: place:: PlaceRef ;
15
+ use super :: { FunctionCx , LocalRef } ;
16
16
use crate :: common:: IntPredicate ;
17
17
use crate :: traits:: * ;
18
18
use crate :: { MemFlags , base} ;
@@ -593,6 +593,14 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
593
593
self . codegen_place_to_pointer ( bx, place, mk_ptr)
594
594
}
595
595
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
+
596
604
mir:: Rvalue :: BinaryOp ( op_with_overflow, box ( ref lhs, ref rhs) )
597
605
if let Some ( op) = op_with_overflow. overflowing_to_wrapping ( ) =>
598
606
{
@@ -792,6 +800,24 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
792
800
}
793
801
}
794
802
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
+
795
821
/// Codegen an `Rvalue::RawPtr` or `Rvalue::Ref`
796
822
fn codegen_place_to_pointer (
797
823
& mut self ,
@@ -1063,6 +1089,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
1063
1089
mir:: Rvalue :: Ref ( ..) |
1064
1090
mir:: Rvalue :: CopyForDeref ( ..) |
1065
1091
mir:: Rvalue :: RawPtr ( ..) |
1092
+ mir:: Rvalue :: Len ( ..) |
1066
1093
mir:: Rvalue :: Cast ( ..) | // (*)
1067
1094
mir:: Rvalue :: ShallowInitBox ( ..) | // (*)
1068
1095
mir:: Rvalue :: BinaryOp ( ..) |
0 commit comments