@@ -630,6 +630,71 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
630
630
block. and ( base_place. index ( idx) )
631
631
}
632
632
633
+ /// Given a place that's either an array or a slice, returns an operand
634
+ /// with the length of the array/slice.
635
+ ///
636
+ /// For arrays it'll be `Operand::Constant` with the actual length;
637
+ /// For slices it'll be `Operand::Move` of a local using `PtrMetadata`.
638
+ fn len_of_slice_or_array (
639
+ & mut self ,
640
+ block : BasicBlock ,
641
+ place : Place < ' tcx > ,
642
+ span : Span ,
643
+ source_info : SourceInfo ,
644
+ ) -> Operand < ' tcx > {
645
+ let place_ty = place. ty ( & self . local_decls , self . tcx ) . ty ;
646
+ let usize_ty = self . tcx . types . usize ;
647
+
648
+ match place_ty. kind ( ) {
649
+ ty:: Array ( _elem_ty, len_const) => {
650
+ // We know how long an array is, so just use that as a constant
651
+ // directly -- no locals needed. We do need one statement so
652
+ // that borrow- and initialization-checking consider it used,
653
+ // though. FIXME: Do we really *need* to count this as a use?
654
+ // Could partial array tracking work off something else instead?
655
+ self . cfg . push_fake_read ( block, source_info, FakeReadCause :: ForIndex , place) ;
656
+ let const_ = Const :: from_ty_const ( * len_const, usize_ty, self . tcx ) ;
657
+ Operand :: Constant ( Box :: new ( ConstOperand { span, user_ty : None , const_ } ) )
658
+ }
659
+ ty:: Slice ( _elem_ty) => {
660
+ let ptr_or_ref = if let [ PlaceElem :: Deref ] = place. projection [ ..]
661
+ && let local_ty = self . local_decls [ place. local ] . ty
662
+ && local_ty. is_trivially_pure_clone_copy ( )
663
+ {
664
+ // It's extremely common that we have something that can be
665
+ // directly passed to `PtrMetadata`, so avoid an unnecessary
666
+ // temporary and statement in those cases. Note that we can
667
+ // only do that for `Copy` types -- not `&mut [_]` -- because
668
+ // the MIR we're building here needs to pass NLL later.
669
+ Operand :: Copy ( Place :: from ( place. local ) )
670
+ } else {
671
+ let ptr_ty = Ty :: new_imm_ptr ( self . tcx , place_ty) ;
672
+ let slice_ptr = self . temp ( ptr_ty, span) ;
673
+ self . cfg . push_assign (
674
+ block,
675
+ source_info,
676
+ slice_ptr,
677
+ Rvalue :: RawPtr ( Mutability :: Not , place) ,
678
+ ) ;
679
+ Operand :: Move ( slice_ptr)
680
+ } ;
681
+
682
+ let len = self . temp ( usize_ty, span) ;
683
+ self . cfg . push_assign (
684
+ block,
685
+ source_info,
686
+ len,
687
+ Rvalue :: UnaryOp ( UnOp :: PtrMetadata , ptr_or_ref) ,
688
+ ) ;
689
+
690
+ Operand :: Move ( len)
691
+ }
692
+ _ => {
693
+ span_bug ! ( span, "len called on place of type {place_ty:?}" )
694
+ }
695
+ }
696
+ }
697
+
633
698
fn bounds_check (
634
699
& mut self ,
635
700
block : BasicBlock ,
@@ -638,25 +703,25 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
638
703
expr_span : Span ,
639
704
source_info : SourceInfo ,
640
705
) -> BasicBlock {
641
- let usize_ty = self . tcx . types . usize ;
642
- let bool_ty = self . tcx . types . bool ;
643
- // bounds check:
644
- let len = self . temp ( usize_ty, expr_span) ;
645
- let lt = self . temp ( bool_ty, expr_span) ;
706
+ let slice = slice. to_place ( self ) ;
646
707
647
708
// len = len(slice)
648
- self . cfg . push_assign ( block, source_info, len, Rvalue :: Len ( slice. to_place ( self ) ) ) ;
709
+ let len = self . len_of_slice_or_array ( block, slice, expr_span, source_info) ;
710
+
649
711
// lt = idx < len
712
+ let bool_ty = self . tcx . types . bool ;
713
+ let lt = self . temp ( bool_ty, expr_span) ;
650
714
self . cfg . push_assign (
651
715
block,
652
716
source_info,
653
717
lt,
654
718
Rvalue :: BinaryOp (
655
719
BinOp :: Lt ,
656
- Box :: new ( ( Operand :: Copy ( Place :: from ( index) ) , Operand :: Copy ( len) ) ) ,
720
+ Box :: new ( ( Operand :: Copy ( Place :: from ( index) ) , len. to_copy ( ) ) ) ,
657
721
) ,
658
722
) ;
659
- let msg = BoundsCheck { len : Operand :: Move ( len) , index : Operand :: Copy ( Place :: from ( index) ) } ;
723
+ let msg = BoundsCheck { len, index : Operand :: Copy ( Place :: from ( index) ) } ;
724
+
660
725
// assert!(lt, "...")
661
726
self . assert ( block, Operand :: Move ( lt) , true , msg, expr_span)
662
727
}
0 commit comments