@@ -52,11 +52,20 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
5252 } )
5353 }
5454 ExprKind :: Repeat { value, count } => {
55- let value_operand = unpack ! (
56- block =
57- this. as_operand( block, scope, & this. thir[ value] , None , NeedsTemporary :: No )
58- ) ;
59- block. and ( Rvalue :: Repeat ( value_operand, count) )
55+ if Some ( 0 ) == count. try_eval_usize ( this. tcx , this. param_env ) {
56+ this. build_zero_repeat ( block, value, scope, source_info)
57+ } else {
58+ let value_operand = unpack ! (
59+ block = this. as_operand(
60+ block,
61+ scope,
62+ & this. thir[ value] ,
63+ None ,
64+ NeedsTemporary :: No
65+ )
66+ ) ;
67+ block. and ( Rvalue :: Repeat ( value_operand, count) )
68+ }
6069 }
6170 ExprKind :: Binary { op, lhs, rhs } => {
6271 let lhs = unpack ! (
@@ -515,6 +524,37 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
515524 }
516525 }
517526
527+ fn build_zero_repeat (
528+ & mut self ,
529+ mut block : BasicBlock ,
530+ value : ExprId ,
531+ scope : Option < region:: Scope > ,
532+ outer_source_info : SourceInfo ,
533+ ) -> BlockAnd < Rvalue < ' tcx > > {
534+ let this = self ;
535+ let value = & this. thir [ value] ;
536+ let elem_ty = value. ty ;
537+ if let Some ( Category :: Constant ) = Category :: of ( & value. kind ) {
538+ // Repeating a const does nothing
539+ } else {
540+ // For a non-const, we may need to generate an appropriate `Drop`
541+ let value_operand =
542+ unpack ! ( block = this. as_operand( block, scope, value, None , NeedsTemporary :: No ) ) ;
543+ if let Operand :: Move ( to_drop) = value_operand {
544+ let success = this. cfg . start_new_block ( ) ;
545+ this. cfg . terminate (
546+ block,
547+ outer_source_info,
548+ TerminatorKind :: Drop { place : to_drop, target : success, unwind : None } ,
549+ ) ;
550+ this. diverge_from ( block) ;
551+ block = success;
552+ }
553+ this. record_operands_moved ( & [ value_operand] ) ;
554+ }
555+ block. and ( Rvalue :: Aggregate ( Box :: new ( AggregateKind :: Array ( elem_ty) ) , Vec :: new ( ) ) )
556+ }
557+
518558 fn limit_capture_mutability (
519559 & mut self ,
520560 upvar_span : Span ,
0 commit comments