@@ -8,6 +8,7 @@ use rustc_middle::middle::region;
88use rustc_middle:: mir:: interpret:: Scalar ;
99use rustc_middle:: mir:: * ;
1010use rustc_middle:: thir:: * ;
11+ use rustc_middle:: ty:: adjustment:: PointerCoercion ;
1112use rustc_middle:: ty:: cast:: { CastTy , mir_cast_kind} ;
1213use rustc_middle:: ty:: util:: IntTypeExt ;
1314use rustc_middle:: ty:: { self , Ty , UpvarArgs } ;
@@ -656,6 +657,24 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
656657 block. and ( rvalue)
657658 }
658659
660+ /// Recursively inspect a THIR expression and probe through unsizing
661+ /// operations that can be const-folded today.
662+ fn check_constness ( & self , mut kind : & ' a ExprKind < ' tcx > ) -> bool {
663+ loop {
664+ match kind {
665+ & ExprKind :: PointerCoercion {
666+ cast : PointerCoercion :: Unsize ,
667+ source : eid,
668+ is_from_as_cast : _,
669+ }
670+ | & ExprKind :: Scope { region_scope : _, lint_level : _, value : eid } => {
671+ kind = & self . thir [ eid] . kind
672+ }
673+ _ => return matches ! ( Category :: of( & kind) , Some ( Category :: Constant ) ) ,
674+ }
675+ }
676+ }
677+
659678 fn build_zero_repeat (
660679 & mut self ,
661680 mut block : BasicBlock ,
@@ -666,7 +685,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
666685 let this = self ;
667686 let value_expr = & this. thir [ value] ;
668687 let elem_ty = value_expr. ty ;
669- if let Some ( Category :: Constant ) = Category :: of ( & value_expr. kind ) {
688+ if this . check_constness ( & value_expr. kind ) {
670689 // Repeating a const does nothing
671690 } else {
672691 // For a non-const, we may need to generate an appropriate `Drop`
0 commit comments