Skip to content

Commit cd15b65

Browse files
committed
avoid double-cast in mplace_field
1 parent 9de6008 commit cd15b65

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

src/librustc_mir/interpret/place.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -396,33 +396,33 @@ where
396396
field: u64,
397397
) -> InterpResult<'tcx, MPlaceTy<'tcx, M::PointerTag>> {
398398
// Not using the layout method because we want to compute on u64
399-
let offset = match base.layout.fields {
399+
let (offset, field_layout) = match base.layout.fields {
400400
layout::FieldPlacement::Arbitrary { ref offsets, .. } => {
401-
offsets[usize::try_from(field).unwrap()]
401+
let field = usize::try_from(field).unwrap();
402+
(offsets[field], base.layout.field(self, field)?)
402403
}
403404
layout::FieldPlacement::Array { stride, .. } => {
404405
let len = base.len(self)?;
405406
if field >= len {
406407
// This can only be reached in ConstProp and non-rustc-MIR.
407408
throw_ub!(BoundsCheckFailed { len, index: field });
408409
}
409-
Size::mul(stride, field) // `Size` multiplication is checked
410+
// All fields have the same layout.
411+
(Size::mul(stride, field), base.layout.field(self, 9)?)
410412
}
411413
layout::FieldPlacement::Union(count) => {
414+
let field = usize::try_from(field).unwrap();
412415
assert!(
413-
field < u64::try_from(count).unwrap(),
416+
field < count,
414417
"Tried to access field {} of union {:#?} with {} fields",
415418
field,
416419
base.layout,
417420
count
418421
);
419422
// Offset is always 0
420-
Size::from_bytes(0)
423+
(Size::from_bytes(0), base.layout.field(self, field)?)
421424
}
422425
};
423-
// the only way conversion can fail if is this is an array (otherwise we already panicked
424-
// above). In that case, all fields have the same layout.
425-
let field_layout = base.layout.field(self, usize::try_from(field).unwrap_or(0))?;
426426

427427
// Offset may need adjustment for unsized fields.
428428
let (meta, offset) = if field_layout.is_unsized() {

0 commit comments

Comments
 (0)