Skip to content

Commit d1b5231

Browse files
committed
Fix const eval of ZST index operations to make the static assertion work
1 parent 24dca6a commit d1b5231

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

src/librustc_mir/interpret/operand.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ impl<'a, 'mir, 'tcx, M: Machine<'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M> {
340340

341341
let field = field.try_into().unwrap();
342342
let field_layout = op.layout.field(self, field)?;
343-
if field_layout.size.bytes() == 0 {
343+
if field_layout.is_zst() {
344344
let val = Value::Scalar(Scalar::zst().into());
345345
return Ok(OpTy { op: Operand::Immediate(val), layout: field_layout });
346346
}
@@ -397,9 +397,15 @@ impl<'a, 'mir, 'tcx, M: Machine<'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M> {
397397
Field(field, _) => self.operand_field(base, field.index() as u64)?,
398398
Downcast(_, variant) => self.operand_downcast(base, variant)?,
399399
Deref => self.deref_operand(base)?.into(),
400-
// The rest should only occur as mplace, we do not use Immediates for types
401-
// allowing such operations. This matches place_projection forcing an allocation.
402-
Subslice { .. } | ConstantIndex { .. } | Index(_) => {
400+
Subslice { .. } | ConstantIndex { .. } | Index(_) => if base.layout.is_zst() {
401+
OpTy {
402+
op: Operand::Immediate(Value::Scalar(Scalar::zst().into())),
403+
// the actual index doesn't matter, so we just pick a convenient one like 0
404+
layout: base.layout.field(self, 0)?,
405+
}
406+
} else {
407+
// The rest should only occur as mplace, we do not use Immediates for types
408+
// allowing such operations. This matches place_projection forcing an allocation.
403409
let mplace = base.to_mem_place();
404410
self.mplace_projection(mplace, proj_elem)?.into()
405411
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// compile-pass
2+
3+
static ASSERT: () = [()][(std::mem::size_of::<u32>() != 4) as usize];
4+
5+
fn main() {}

0 commit comments

Comments
 (0)