mirror of
https://github.com/rust-lang/rust.git
synced 2025-05-14 02:49:40 +00:00
Fix const eval of ZST index operations to make the static assertion work
This commit is contained in:
parent
24dca6aeca
commit
d1b5231aa7
@ -340,7 +340,7 @@ impl<'a, 'mir, 'tcx, M: Machine<'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M> {
|
||||
|
||||
let field = field.try_into().unwrap();
|
||||
let field_layout = op.layout.field(self, field)?;
|
||||
if field_layout.size.bytes() == 0 {
|
||||
if field_layout.is_zst() {
|
||||
let val = Value::Scalar(Scalar::zst().into());
|
||||
return Ok(OpTy { op: Operand::Immediate(val), layout: field_layout });
|
||||
}
|
||||
@ -397,9 +397,15 @@ impl<'a, 'mir, 'tcx, M: Machine<'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M> {
|
||||
Field(field, _) => self.operand_field(base, field.index() as u64)?,
|
||||
Downcast(_, variant) => self.operand_downcast(base, variant)?,
|
||||
Deref => self.deref_operand(base)?.into(),
|
||||
// The rest should only occur as mplace, we do not use Immediates for types
|
||||
// allowing such operations. This matches place_projection forcing an allocation.
|
||||
Subslice { .. } | ConstantIndex { .. } | Index(_) => {
|
||||
Subslice { .. } | ConstantIndex { .. } | Index(_) => if base.layout.is_zst() {
|
||||
OpTy {
|
||||
op: Operand::Immediate(Value::Scalar(Scalar::zst().into())),
|
||||
// the actual index doesn't matter, so we just pick a convenient one like 0
|
||||
layout: base.layout.field(self, 0)?,
|
||||
}
|
||||
} else {
|
||||
// The rest should only occur as mplace, we do not use Immediates for types
|
||||
// allowing such operations. This matches place_projection forcing an allocation.
|
||||
let mplace = base.to_mem_place();
|
||||
self.mplace_projection(mplace, proj_elem)?.into()
|
||||
}
|
||||
|
5
src/test/ui/consts/const-eval/zst_operand_eval.rs
Normal file
5
src/test/ui/consts/const-eval/zst_operand_eval.rs
Normal file
@ -0,0 +1,5 @@
|
||||
// compile-pass
|
||||
|
||||
static ASSERT: () = [()][(std::mem::size_of::<u32>() != 4) as usize];
|
||||
|
||||
fn main() {}
|
Loading…
Reference in New Issue
Block a user