@@ -8,6 +8,7 @@ use rustc_middle::middle::region;
8
8
use rustc_middle:: mir:: interpret:: Scalar ;
9
9
use rustc_middle:: mir:: * ;
10
10
use rustc_middle:: thir:: * ;
11
+ use rustc_middle:: ty:: adjustment:: PointerCoercion ;
11
12
use rustc_middle:: ty:: cast:: { CastTy , mir_cast_kind} ;
12
13
use rustc_middle:: ty:: util:: IntTypeExt ;
13
14
use rustc_middle:: ty:: { self , Ty , UpvarArgs } ;
@@ -656,6 +657,24 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
656
657
block. and ( rvalue)
657
658
}
658
659
660
+ /// Recursively inspect a THIR expression and probe through unsizing
661
+ /// operations that can be const-folded today.
662
+ fn check_constness ( & self , mut ek : & ' a ExprKind < ' tcx > ) -> bool {
663
+ loop {
664
+ match ek {
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
+ ek = & self . thir [ eid] . kind
672
+ }
673
+ _ => return matches ! ( Category :: of( & ek) , Some ( Category :: Constant ) ) ,
674
+ }
675
+ }
676
+ }
677
+
659
678
fn build_zero_repeat (
660
679
& mut self ,
661
680
mut block : BasicBlock ,
@@ -666,7 +685,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
666
685
let this = self ;
667
686
let value_expr = & this. thir [ value] ;
668
687
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 ) {
670
689
// Repeating a const does nothing
671
690
} else {
672
691
// For a non-const, we may need to generate an appropriate `Drop`
0 commit comments