56
56
use rustc_const_eval:: interpret:: { ImmTy , InterpCx , MemPlaceMeta , OpTy , Projectable , Scalar } ;
57
57
use rustc_data_structures:: fx:: { FxHashMap , FxIndexSet } ;
58
58
use rustc_data_structures:: graph:: dominators:: Dominators ;
59
+ use rustc_hir:: def:: DefKind ;
59
60
use rustc_index:: bit_set:: BitSet ;
60
61
use rustc_index:: IndexVec ;
61
62
use rustc_macros:: newtype_index;
@@ -64,6 +65,7 @@ use rustc_middle::mir::visit::*;
64
65
use rustc_middle:: mir:: * ;
65
66
use rustc_middle:: ty:: layout:: LayoutOf ;
66
67
use rustc_middle:: ty:: { self , Ty , TyCtxt , TypeAndMut } ;
68
+ use rustc_span:: def_id:: DefId ;
67
69
use rustc_span:: DUMMY_SP ;
68
70
use rustc_target:: abi:: { self , Abi , Size , VariantIdx , FIRST_VARIANT } ;
69
71
use std:: borrow:: Cow ;
@@ -129,6 +131,16 @@ newtype_index! {
129
131
struct VnIndex { }
130
132
}
131
133
134
+ /// Computing the aggregate's type can be quite slow, so we only keep the minimal amount of
135
+ /// information to reconstruct it when needed.
136
+ #[ derive( Copy , Clone , Debug , PartialEq , Eq , Hash ) ]
137
+ enum AggregateTy < ' tcx > {
138
+ /// Invariant: this must not be used for an empty array.
139
+ Array ,
140
+ Tuple ,
141
+ Def ( DefId , ty:: GenericArgsRef < ' tcx > ) ,
142
+ }
143
+
132
144
#[ derive( Copy , Clone , Debug , PartialEq , Eq , Hash ) ]
133
145
enum AddressKind {
134
146
Ref ( BorrowKind ) ,
@@ -145,7 +157,7 @@ enum Value<'tcx> {
145
157
Constant ( Const < ' tcx > ) ,
146
158
/// An aggregate value, either tuple/closure/struct/enum.
147
159
/// This does not contain unions, as we cannot reason with the value.
148
- Aggregate ( Ty < ' tcx > , VariantIdx , Vec < VnIndex > ) ,
160
+ Aggregate ( AggregateTy < ' tcx > , VariantIdx , Vec < VnIndex > ) ,
149
161
/// This corresponds to a `[value; count]` expression.
150
162
Repeat ( VnIndex , ty:: Const < ' tcx > ) ,
151
163
/// The address of a place.
@@ -282,11 +294,23 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {
282
294
Repeat ( ..) => return None ,
283
295
284
296
Constant ( ref constant) => self . ecx . eval_mir_constant ( constant, None , None ) . ok ( ) ?,
285
- Aggregate ( ty , variant, ref fields) => {
297
+ Aggregate ( kind , variant, ref fields) => {
286
298
let fields = fields
287
299
. iter ( )
288
300
. map ( |& f| self . evaluated [ f] . as_ref ( ) )
289
301
. collect :: < Option < Vec < _ > > > ( ) ?;
302
+ let ty = match kind {
303
+ AggregateTy :: Array => {
304
+ assert ! ( fields. len( ) > 0 ) ;
305
+ Ty :: new_array ( self . tcx , fields[ 0 ] . layout . ty , fields. len ( ) as u64 )
306
+ }
307
+ AggregateTy :: Tuple => {
308
+ Ty :: new_tup_from_iter ( self . tcx , fields. iter ( ) . map ( |f| f. layout . ty ) )
309
+ }
310
+ AggregateTy :: Def ( def_id, args) => {
311
+ self . tcx . type_of ( def_id) . instantiate ( self . tcx , args)
312
+ }
313
+ } ;
290
314
let variant = if ty. is_enum ( ) { Some ( variant) } else { None } ;
291
315
let ty = self . ecx . layout_of ( ty) . ok ( ) ?;
292
316
if ty. is_zst ( ) {
@@ -503,7 +527,7 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {
503
527
Value :: Repeat ( inner, _) => {
504
528
return Some ( * inner) ;
505
529
}
506
- Value :: Aggregate ( ty , _, operands) if ty . is_array ( ) => {
530
+ Value :: Aggregate ( AggregateTy :: Array , _, operands) => {
507
531
let offset = if from_end {
508
532
operands. len ( ) - offset as usize
509
533
} else {
@@ -652,20 +676,30 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {
652
676
}
653
677
Rvalue :: NullaryOp ( op, ty) => Value :: NullaryOp ( op, ty) ,
654
678
Rvalue :: Aggregate ( box ref kind, ref mut fields) => {
655
- let variant_index = match * kind {
656
- AggregateKind :: Array ( ..)
657
- | AggregateKind :: Tuple
658
- | AggregateKind :: Closure ( ..)
659
- | AggregateKind :: Generator ( ..) => FIRST_VARIANT ,
660
- AggregateKind :: Adt ( _, variant_index, _, _, None ) => variant_index,
679
+ let ( ty, variant_index) = match * kind {
680
+ // For empty arrays, we have not mean to recover the type. They are ZSTs
681
+ // anyway, so return them as such.
682
+ AggregateKind :: Array ( ..) | AggregateKind :: Tuple if fields. is_empty ( ) => {
683
+ return Some ( self . insert ( Value :: Constant ( Const :: zero_sized (
684
+ rvalue. ty ( self . local_decls , self . tcx ) ,
685
+ ) ) ) ) ;
686
+ }
687
+ AggregateKind :: Array ( ..) => ( AggregateTy :: Array , FIRST_VARIANT ) ,
688
+ AggregateKind :: Tuple => ( AggregateTy :: Tuple , FIRST_VARIANT ) ,
689
+ AggregateKind :: Closure ( did, substs)
690
+ | AggregateKind :: Generator ( did, substs, _) => {
691
+ ( AggregateTy :: Def ( did, substs) , FIRST_VARIANT )
692
+ }
693
+ AggregateKind :: Adt ( did, variant_index, substs, _, None ) => {
694
+ ( AggregateTy :: Def ( did, substs) , variant_index)
695
+ }
661
696
// Do not track unions.
662
697
AggregateKind :: Adt ( _, _, _, _, Some ( _) ) => return None ,
663
698
} ;
664
699
let fields: Option < Vec < _ > > = fields
665
700
. iter_mut ( )
666
701
. map ( |op| self . simplify_operand ( op, location) . or_else ( || self . new_opaque ( ) ) )
667
702
. collect ( ) ;
668
- let ty = rvalue. ty ( self . local_decls , self . tcx ) ;
669
703
Value :: Aggregate ( ty, variant_index, fields?)
670
704
}
671
705
Rvalue :: Ref ( _, borrow_kind, ref mut place) => {
@@ -718,8 +752,10 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {
718
752
719
753
fn simplify_discriminant ( & mut self , place : VnIndex ) -> Option < VnIndex > {
720
754
if let Value :: Aggregate ( enum_ty, variant, _) = * self . get ( place)
721
- && enum_ty. is_enum ( )
755
+ && let AggregateTy :: Def ( enum_did, enum_substs) = enum_ty
756
+ && let DefKind :: Enum = self . tcx . def_kind ( enum_did)
722
757
{
758
+ let enum_ty = self . tcx . type_of ( enum_did) . instantiate ( self . tcx , enum_substs) ;
723
759
let discr = self . ecx . discriminant_for_variant ( enum_ty, variant) . ok ( ) ?;
724
760
return Some ( self . insert_scalar ( discr. to_scalar ( ) , discr. layout . ty ) ) ;
725
761
}
0 commit comments