Skip to content

Commit 80c5ad6

Browse files
authored
Rollup merge of #157290 - RalfJung:unop-layout, r=oli-obk
interpret: fix mir::UnOp layout computation "The operand always has the same type as the result" was correct when I wrote the comment, but more `UnOp`s have been added since, making this incorrect now. This hasn't caused issues yet because apparently the local variable layout cache means we hardly ever (never?) actually use the "known" layout. r? @oli-obk
2 parents ce0ff3c + 8dd7b1b commit 80c5ad6

2 files changed

Lines changed: 12 additions & 2 deletions

File tree

compiler/rustc_const_eval/src/interpret/step.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,8 +190,8 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
190190
}
191191

192192
UnaryOp(un_op, ref operand) => {
193-
// The operand always has the same type as the result.
194-
let val = self.read_immediate(&self.eval_operand(operand, Some(dest.layout))?)?;
193+
let layout = util::unop_homogeneous(un_op).then_some(dest.layout);
194+
let val = self.read_immediate(&self.eval_operand(operand, layout)?)?;
195195
let result = self.unary_op(un_op, &val)?;
196196
assert_eq!(result.layout, dest.layout, "layout mismatch for result of {un_op:?}");
197197
self.write_immediate(*result, &dest)?;

compiler/rustc_const_eval/src/util/mod.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,13 @@ pub fn binop_right_homogeneous(op: mir::BinOp) -> bool {
3838
Offset | Shl | ShlUnchecked | Shr | ShrUnchecked => false,
3939
}
4040
}
41+
42+
/// Classify whether an operator is "homogeneous", i.e., the operand has the
43+
/// same type as the result.
44+
#[inline]
45+
pub fn unop_homogeneous(op: mir::UnOp) -> bool {
46+
match op {
47+
mir::UnOp::Not | mir::UnOp::Neg => true,
48+
mir::UnOp::PtrMetadata => false,
49+
}
50+
}

0 commit comments

Comments
 (0)