@@ -630,6 +630,75 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
630630 block. and ( base_place. index ( idx) )
631631 }
632632
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 {
658+ span,
659+ user_ty : None ,
660+ const_,
661+ } ) )
662+ }
663+ ty:: Slice ( _elem_ty) =>{
664+ let ptr_or_ref = if let [ PlaceElem :: Deref ] = place. projection [ ..]
665+ && let local_ty = self . local_decls [ place. local ] . ty
666+ && local_ty. is_trivially_pure_clone_copy ( )
667+ {
668+ // It's extremely common that we have something that can be
669+ // directly passed to `PtrMetadata`, so avoid an unnecessary
670+ // temporary and statement in those cases. Note that we can
671+ // only do that for `Copy` types -- not `&mut [_]` -- because
672+ // the MIR we're building here needs to pass NLL later.
673+ Operand :: Copy ( Place :: from ( place. local ) )
674+ } else {
675+ let ptr_ty = Ty :: new_imm_ptr ( self . tcx , place_ty) ;
676+ let slice_ptr = self . temp ( ptr_ty, span) ;
677+ self . cfg . push_assign (
678+ block,
679+ source_info,
680+ slice_ptr,
681+ Rvalue :: RawPtr ( Mutability :: Not , place) ,
682+ ) ;
683+ Operand :: Move ( slice_ptr)
684+ } ;
685+
686+ let len = self . temp ( usize_ty, span) ;
687+ self . cfg . push_assign (
688+ block,
689+ source_info,
690+ len,
691+ Rvalue :: UnaryOp ( UnOp :: PtrMetadata , ptr_or_ref) ,
692+ ) ;
693+
694+ Operand :: Move ( len)
695+ }
696+ _ => {
697+ span_bug ! ( span, "len called on place of type {place_ty:?}" )
698+ }
699+ }
700+ }
701+
633702 fn bounds_check (
634703 & mut self ,
635704 block : BasicBlock ,
@@ -638,25 +707,25 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
638707 expr_span : Span ,
639708 source_info : SourceInfo ,
640709 ) -> 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) ;
710+ let slice = slice. to_place ( self ) ;
646711
647712 // len = len(slice)
648- self . cfg . push_assign ( block, source_info, len, Rvalue :: Len ( slice. to_place ( self ) ) ) ;
713+ let len = self . len_of_slice_or_array ( block, slice, expr_span, source_info) ;
714+
649715 // lt = idx < len
716+ let bool_ty = self . tcx . types . bool ;
717+ let lt = self . temp ( bool_ty, expr_span) ;
650718 self . cfg . push_assign (
651719 block,
652720 source_info,
653721 lt,
654722 Rvalue :: BinaryOp (
655723 BinOp :: Lt ,
656- Box :: new ( ( Operand :: Copy ( Place :: from ( index) ) , Operand :: Copy ( len) ) ) ,
724+ Box :: new ( ( Operand :: Copy ( Place :: from ( index) ) , len. to_copy ( ) ) ) ,
657725 ) ,
658726 ) ;
659- let msg = BoundsCheck { len : Operand :: Move ( len) , index : Operand :: Copy ( Place :: from ( index) ) } ;
727+ let msg = BoundsCheck { len, index : Operand :: Copy ( Place :: from ( index) ) } ;
728+
660729 // assert!(lt, "...")
661730 self . assert ( block, Operand :: Move ( lt) , true , msg, expr_span)
662731 }
0 commit comments