diff options
| author | Ralf Jung <post@ralfj.de> | 2020-03-21 16:28:34 +0100 |
|---|---|---|
| committer | Ralf Jung <post@ralfj.de> | 2020-03-25 15:53:52 +0100 |
| commit | cd15b659c7f20d9b740b3c7b53dde9dcd0132f9d (patch) | |
| tree | ba09b94bb3a0cdee4287f1585ffb6ad1ea2a803c | |
| parent | 9de600892da3c86fb92c1dfde455d39657835739 (diff) | |
| download | rust-cd15b659c7f20d9b740b3c7b53dde9dcd0132f9d.tar.gz rust-cd15b659c7f20d9b740b3c7b53dde9dcd0132f9d.zip | |
avoid double-cast in mplace_field
| -rw-r--r-- | src/librustc_mir/interpret/place.rs | 16 |
1 files changed, 8 insertions, 8 deletions
diff --git a/src/librustc_mir/interpret/place.rs b/src/librustc_mir/interpret/place.rs index 7bea5357cdf..5870266e69a 100644 --- a/src/librustc_mir/interpret/place.rs +++ b/src/librustc_mir/interpret/place.rs @@ -396,9 +396,10 @@ where field: u64, ) -> InterpResult<'tcx, MPlaceTy<'tcx, M::PointerTag>> { // Not using the layout method because we want to compute on u64 - let offset = match base.layout.fields { + let (offset, field_layout) = match base.layout.fields { layout::FieldPlacement::Arbitrary { ref offsets, .. } => { - offsets[usize::try_from(field).unwrap()] + let field = usize::try_from(field).unwrap(); + (offsets[field], base.layout.field(self, field)?) } layout::FieldPlacement::Array { stride, .. } => { let len = base.len(self)?; @@ -406,23 +407,22 @@ where // This can only be reached in ConstProp and non-rustc-MIR. throw_ub!(BoundsCheckFailed { len, index: field }); } - Size::mul(stride, field) // `Size` multiplication is checked + // All fields have the same layout. + (Size::mul(stride, field), base.layout.field(self, 9)?) } layout::FieldPlacement::Union(count) => { + let field = usize::try_from(field).unwrap(); assert!( - field < u64::try_from(count).unwrap(), + field < count, "Tried to access field {} of union {:#?} with {} fields", field, base.layout, count ); // Offset is always 0 - Size::from_bytes(0) + (Size::from_bytes(0), base.layout.field(self, field)?) } }; - // the only way conversion can fail if is this is an array (otherwise we already panicked - // above). In that case, all fields have the same layout. - let field_layout = base.layout.field(self, usize::try_from(field).unwrap_or(0))?; // Offset may need adjustment for unsized fields. let (meta, offset) = if field_layout.is_unsized() { |
