Skip to content

Commit 4661464

Browse files
authoredOct 1, 2021
Rollup merge of #89370 - RalfJung:ctfe-aggregate-rvalue, r=oli-obk
CTFE: tweak aggregate rvalue handling I have not looked at this code in ages... I think Miri does not even hit it, since (most?) aggregate rvalues are lowered somewhere in the MIR pipeline, but CTFE does hit it. So this adds some extra sanity assertions, and removes a ZST special case -- ZST should only be special cased fairly late (when the actual memory access happens); e.g. `!` is a ZST and we still want `copy_op` to be called for it since it will perform validation (and raise UB, since `!` is never valid).
2 parents fbc67b5 + 268bb46 commit 4661464

File tree

1 file changed

+9
-8
lines changed
  • compiler/rustc_const_eval/src/interpret

1 file changed

+9
-8
lines changed
 

‎compiler/rustc_const_eval/src/interpret/step.rs

+9-8
Original file line numberDiff line numberDiff line change
@@ -197,12 +197,17 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
197197
}
198198

199199
Aggregate(ref kind, ref operands) => {
200+
// active_field_index is for union initialization.
200201
let (dest, active_field_index) = match **kind {
201202
mir::AggregateKind::Adt(adt_def, variant_index, _, _, active_field_index) => {
202203
self.write_discriminant(variant_index, &dest)?;
203204
if adt_def.is_enum() {
204-
(self.place_downcast(&dest, variant_index)?, active_field_index)
205+
assert!(active_field_index.is_none());
206+
(self.place_downcast(&dest, variant_index)?, None)
205207
} else {
208+
if active_field_index.is_some() {
209+
assert_eq!(operands.len(), 1);
210+
}
206211
(dest, active_field_index)
207212
}
208213
}
@@ -211,12 +216,9 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
211216

212217
for (i, operand) in operands.iter().enumerate() {
213218
let op = self.eval_operand(operand, None)?;
214-
// Ignore zero-sized fields.
215-
if !op.layout.is_zst() {
216-
let field_index = active_field_index.unwrap_or(i);
217-
let field_dest = self.place_field(&dest, field_index)?;
218-
self.copy_op(&op, &field_dest)?;
219-
}
219+
let field_index = active_field_index.unwrap_or(i);
220+
let field_dest = self.place_field(&dest, field_index)?;
221+
self.copy_op(&op, &field_dest)?;
220222
}
221223
}
222224

@@ -253,7 +255,6 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
253255
}
254256

255257
Len(place) => {
256-
// FIXME(CTFE): don't allow computing the length of arrays in const eval
257258
let src = self.eval_place(place)?;
258259
let mplace = self.force_allocation(&src)?;
259260
let len = mplace.len(self)?;

0 commit comments

Comments
 (0)
Please sign in to comment.