Skip to content

Commit d208f80

Browse files
committed
reduce code duplication
1 parent 47d11a8 commit d208f80

File tree

1 file changed

+12
-13
lines changed

1 file changed

+12
-13
lines changed

compiler/rustc_const_eval/src/interpret/operand.rs

+12-13
Original file line numberDiff line numberDiff line change
@@ -284,15 +284,18 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
284284
Abi::Scalar(s) if force => Some(s.primitive()),
285285
_ => None,
286286
};
287-
let number_may_have_provenance = !M::enforce_number_no_provenance(self);
287+
let read_provenance = |s: abi::Primitive, size| {
288+
// Should be just `s.is_ptr()`, but we support a Miri flag that accepts more
289+
// questionable ptr-int transmutes.
290+
let number_may_have_provenance = !M::enforce_number_no_provenance(self);
291+
s.is_ptr() || (number_may_have_provenance && size == self.pointer_size())
292+
};
288293
if let Some(s) = scalar_layout {
289294
//FIXME(#96185): let size = s.size(self);
290295
//FIXME(#96185): assert_eq!(size, mplace.layout.size, "abi::Scalar size does not match layout size");
291296
let size = mplace.layout.size; //FIXME(#96185): remove this line
292-
let scalar = alloc.read_scalar(
293-
alloc_range(Size::ZERO, size),
294-
s.is_ptr() || (number_may_have_provenance && size == self.pointer_size()),
295-
)?;
297+
let scalar =
298+
alloc.read_scalar(alloc_range(Size::ZERO, size), read_provenance(s, size))?;
296299
return Ok(Some(ImmTy { imm: scalar.into(), layout: mplace.layout }));
297300
}
298301
let scalar_pair_layout = match mplace.layout.abi {
@@ -310,14 +313,10 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
310313
let (a_size, b_size) = (a.size(self), b.size(self));
311314
let b_offset = a_size.align_to(b.align(self).abi);
312315
assert!(b_offset.bytes() > 0); // in `operand_field` we use the offset to tell apart the fields
313-
let a_val = alloc.read_scalar(
314-
alloc_range(Size::ZERO, a_size),
315-
a.is_ptr() || (number_may_have_provenance && a_size == self.pointer_size()),
316-
)?;
317-
let b_val = alloc.read_scalar(
318-
alloc_range(b_offset, b_size),
319-
b.is_ptr() || (number_may_have_provenance && b_size == self.pointer_size()),
320-
)?;
316+
let a_val =
317+
alloc.read_scalar(alloc_range(Size::ZERO, a_size), read_provenance(a, a_size))?;
318+
let b_val =
319+
alloc.read_scalar(alloc_range(b_offset, b_size), read_provenance(b, b_size))?;
321320
return Ok(Some(ImmTy {
322321
imm: Immediate::ScalarPair(a_val, b_val),
323322
layout: mplace.layout,

0 commit comments

Comments
 (0)