Skip to content

Commit e314a4e

Browse files
committed
fix accessing unsized fields
1 parent 1e137a7 commit e314a4e

File tree

1 file changed

+15
-14
lines changed

1 file changed

+15
-14
lines changed

src/librustc_mir/interpret/place.rs

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -283,31 +283,32 @@ impl<'a, 'mir, 'tcx, M: Machine<'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M> {
283283
};
284284
// the only way conversion can fail if is this is an array (otherwise we already panicked
285285
// above). In that case, all fields are equal.
286-
let field = base.layout.field(self, usize::try_from(field).unwrap_or(0))?;
286+
let field_layout = base.layout.field(self, usize::try_from(field).unwrap_or(0))?;
287287

288288
// Adjust offset
289-
let offset = if field.is_unsized() {
290-
let vtable = match base.extra {
291-
PlaceExtra::Vtable(tab) => tab,
292-
_ => bug!("Unsized place with unsized field must come with vtable"),
293-
};
294-
let (_, align) = self.read_size_and_align_from_vtable(vtable)?;
295-
offset.abi_align(align)
296-
} else {
297-
// No adjustment needed
298-
offset
289+
let offset = match base.extra {
290+
PlaceExtra::Vtable(vtable) => {
291+
let (_, align) = self.read_size_and_align_from_vtable(vtable)?;
292+
// FIXME: Is this right? Should we always do this, or only when actually
293+
// accessing the field to which the vtable applies?
294+
offset.abi_align(align)
295+
}
296+
_ => {
297+
// No adjustment needed
298+
offset
299+
}
299300
};
300301

301302
let ptr = base.ptr.ptr_offset(offset, self)?;
302-
let align = base.align.min(field.align);
303-
let extra = if !field.is_unsized() {
303+
let align = base.align.min(field_layout.align);
304+
let extra = if !field_layout.is_unsized() {
304305
PlaceExtra::None
305306
} else {
306307
assert!(base.extra != PlaceExtra::None, "Expected fat ptr");
307308
base.extra
308309
};
309310

310-
Ok(MPlaceTy { mplace: MemPlace { ptr, align, extra }, layout: field })
311+
Ok(MPlaceTy { mplace: MemPlace { ptr, align, extra }, layout: field_layout })
311312
}
312313

313314
pub fn mplace_subslice(

0 commit comments

Comments
 (0)