3 use rustc::ty::layout::VariantIdx;
4 use rustc_index::vec::Idx;
6 use std::iter::TrustedLen;
8 /// Expand `lhs = Rvalue::Aggregate(kind, operands)` into assignments to the fields.
10 /// Produces something like
12 /// (lhs as Variant).field0 = arg0; // We only have a downcast if this is an enum
13 /// (lhs as Variant).field1 = arg1;
14 /// discriminant(lhs) = variant_index; // If lhs is an enum or generator.
15 pub fn expand_aggregate<'tcx>(
17 operands: impl Iterator<Item=(Operand<'tcx>, Ty<'tcx>)> + TrustedLen,
18 kind: AggregateKind<'tcx>,
19 source_info: SourceInfo,
20 ) -> impl Iterator<Item=Statement<'tcx>> + TrustedLen {
21 let mut set_discriminant = None;
22 let active_field_index = match kind {
23 AggregateKind::Adt(adt_def, variant_index, _, _, active_field_index) => {
24 if adt_def.is_enum() {
25 set_discriminant = Some(Statement {
26 kind: StatementKind::SetDiscriminant {
27 place: box(lhs.clone()),
32 lhs = lhs.downcast(adt_def, variant_index);
36 AggregateKind::Generator(..) => {
37 // Right now we only support initializing generators to
38 // variant 0 (Unresumed).
39 let variant_index = VariantIdx::new(0);
40 set_discriminant = Some(Statement {
41 kind: StatementKind::SetDiscriminant {
42 place: box(lhs.clone()),
48 // Operands are upvars stored on the base place, so no
49 // downcast is necessary.
56 operands.into_iter().enumerate().map(move |(i, (op, ty))| {
57 let lhs_field = if let AggregateKind::Array(_) = kind {
58 // FIXME(eddyb) `offset` should be u64.
59 let offset = i as u32;
60 assert_eq!(offset as usize, i);
61 lhs.clone().elem(ProjectionElem::ConstantIndex {
63 // FIXME(eddyb) `min_length` doesn't appear to be used.
64 min_length: offset + 1,
68 let field = Field::new(active_field_index.unwrap_or(i));
69 lhs.clone().field(field, ty)
73 kind: StatementKind::Assign(box(lhs_field, Rvalue::Use(op))),
75 }).chain(set_discriminant)