Skip to content

Commit feccf46

Browse files
committed
Interpret aggregates.
1 parent 0241c29 commit feccf46

File tree

4 files changed

+29
-21
lines changed

4 files changed

+29
-21
lines changed

compiler/rustc_codegen_ssa/src/mir/rvalue.rs

+5
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use rustc_middle::ty::cast::{CastTy, IntTy};
1313
use rustc_middle::ty::layout::{HasTyCtxt, LayoutOf};
1414
use rustc_middle::ty::{self, adjustment::PointerCast, Instance, Ty, TyCtxt};
1515
use rustc_span::source_map::{Span, DUMMY_SP};
16+
use rustc_target::abi::VariantIdx;
1617

1718
impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
1819
#[instrument(level = "trace", skip(self, bx))]
@@ -115,6 +116,10 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
115116
(dest, active_field_index)
116117
}
117118
}
119+
mir::AggregateKind::Generator(..) => {
120+
dest.codegen_set_discr(bx, VariantIdx::from_u32(0));
121+
(dest, None)
122+
}
118123
_ => (dest, None),
119124
};
120125
for (i, operand) in operands.iter().enumerate() {

compiler/rustc_const_eval/src/interpret/place.rs

-9
Original file line numberDiff line numberDiff line change
@@ -774,15 +774,6 @@ where
774774
variant_index: VariantIdx,
775775
dest: &PlaceTy<'tcx, M::Provenance>,
776776
) -> InterpResult<'tcx> {
777-
// This must be an enum or generator.
778-
match dest.layout.ty.kind() {
779-
ty::Adt(adt, _) => assert!(adt.is_enum()),
780-
ty::Generator(..) => {}
781-
_ => span_bug!(
782-
self.cur_span(),
783-
"write_discriminant called on non-variant-type (neither enum nor generator)"
784-
),
785-
}
786777
// Layout computation excludes uninhabited variants from consideration
787778
// therefore there's no way to represent those variants in the given layout.
788779
// Essentially, uninhabited variants do not have a tag that corresponds to their

compiler/rustc_const_eval/src/interpret/step.rs

+16-4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use either::Either;
77
use rustc_middle::mir;
88
use rustc_middle::mir::interpret::{InterpResult, Scalar};
99
use rustc_middle::ty::layout::LayoutOf;
10+
use rustc_target::abi::VariantIdx;
1011

1112
use super::{ImmTy, InterpCx, Machine};
1213

@@ -199,13 +200,24 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
199200
}
200201

201202
Aggregate(box ref kind, ref operands) => {
202-
assert!(matches!(kind, mir::AggregateKind::Array(..)));
203-
203+
self.write_uninit(&dest)?;
204+
let (variant_index, variant_dest, active_field_index) = match *kind {
205+
mir::AggregateKind::Adt(_, variant_index, _, _, active_field_index) => {
206+
let variant_dest = self.place_downcast(&dest, variant_index)?;
207+
(variant_index, variant_dest, active_field_index)
208+
}
209+
_ => (VariantIdx::from_u32(0), dest.clone(), None),
210+
};
211+
if active_field_index.is_some() {
212+
assert_eq!(operands.len(), 1);
213+
}
204214
for (field_index, operand) in operands.iter().enumerate() {
205-
let op = self.eval_operand(operand, None)?;
206-
let field_dest = self.place_field(&dest, field_index)?;
215+
let field_index = active_field_index.unwrap_or(field_index);
216+
let field_dest = self.place_field(&variant_dest, field_index)?;
217+
let op = self.eval_operand(operand, Some(field_dest.layout))?;
207218
self.copy_op(&op, &field_dest, /*allow_transmute*/ false)?;
208219
}
220+
self.write_discriminant(variant_index, &dest)?;
209221
}
210222

211223
Repeat(ref operand, _) => {

tests/mir-opt/const_allocation.main.ConstProp.after.64bit.mir

+8-8
Original file line numberDiff line numberDiff line change
@@ -27,33 +27,33 @@ alloc1 (static: FOO, size: 16, align: 8) {
2727
alloc18 (size: 72, align: 8) {
2828
0x00 │ 00 00 00 00 __ __ __ __ ╾───────alloc5────────╼ │ ....░░░░╾──────╼
2929
0x10 │ 00 00 00 00 00 00 00 00 00 00 00 00 __ __ __ __ │ ............░░░░
30-
0x20 │ ╾───────alloc9────────╼ 02 00 00 00 00 00 00 00 │ ╾──────╼........
31-
0x30 │ 01 00 00 00 2a 00 00 00 ╾───────alloc14───────╼ │ ....*...╾──────╼
30+
0x20 │ ╾───────alloc8────────╼ 02 00 00 00 00 00 00 00 │ ╾──────╼........
31+
0x30 │ 01 00 00 00 2a 00 00 00 ╾───────alloc13───────╼ │ ....*...╾──────╼
3232
0x40 │ 03 00 00 00 00 00 00 00 │ ........
3333
}
3434

3535
alloc5 (size: 0, align: 8) {}
3636

37-
alloc9 (size: 32, align: 8) {
38-
0x00 │ ╾───────alloc8────────╼ 03 00 00 00 00 00 00 00 │ ╾──────╼........
37+
alloc8 (size: 32, align: 8) {
38+
0x00 │ ╾───────alloc9────────╼ 03 00 00 00 00 00 00 00 │ ╾──────╼........
3939
0x10 │ ╾───────alloc10───────╼ 03 00 00 00 00 00 00 00 │ ╾──────╼........
4040
}
4141

42-
alloc8 (size: 3, align: 1) {
42+
alloc9 (size: 3, align: 1) {
4343
66 6f 6f │ foo
4444
}
4545

4646
alloc10 (size: 3, align: 1) {
4747
62 61 72 │ bar
4848
}
4949

50-
alloc14 (size: 48, align: 8) {
51-
0x00 │ ╾───────alloc13───────╼ 03 00 00 00 00 00 00 00 │ ╾──────╼........
50+
alloc13 (size: 48, align: 8) {
51+
0x00 │ ╾───────alloc14───────╼ 03 00 00 00 00 00 00 00 │ ╾──────╼........
5252
0x10 │ ╾───────alloc15───────╼ 03 00 00 00 00 00 00 00 │ ╾──────╼........
5353
0x20 │ ╾───────alloc16───────╼ 04 00 00 00 00 00 00 00 │ ╾──────╼........
5454
}
5555

56-
alloc13 (size: 3, align: 1) {
56+
alloc14 (size: 3, align: 1) {
5757
6d 65 68 │ meh
5858
}
5959

0 commit comments

Comments
 (0)