Skip to content

Commit b1df2ae

Browse files
committed
fix computing layout when calling virtual fn
1 parent 730098b commit b1df2ae

File tree

1 file changed

+9
-3
lines changed
  • src/librustc_mir/interpret/terminator

1 file changed

+9
-3
lines changed

src/librustc_mir/interpret/terminator/mod.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -384,10 +384,16 @@ impl<'a, 'mir, 'tcx, M: Machine<'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M> {
384384
ptr_align
385385
)?.to_ptr()?;
386386
let instance = self.memory.get_fn(fn_ptr)?;
387+
388+
// We have to patch the self argument, in particular get the layout
389+
// expected by the actual function. Cannot just use "field 0" due to
390+
// Box<self>.
387391
let mut args = args.to_vec();
388-
let layout = args[0].layout.field(&self, 0)?;
389-
args[0].layout = layout;
390-
args[0].op = Operand::Immediate(Value::Scalar(ptr.into()));
392+
let pointee = args[0].layout.ty.builtin_deref(true).unwrap().ty;
393+
let fake_fat_ptr_ty = self.tcx.mk_mut_ptr(pointee);
394+
args[0].layout = self.layout_of(fake_fat_ptr_ty)?.field(&self, 0)?;
395+
args[0].op = Operand::Immediate(Value::Scalar(ptr.into())); // strip vtable
396+
trace!("Patched self operand to {:#?}", args[0]);
391397
// recurse with concrete function
392398
self.eval_fn_call(instance, destination, &args, span, sig)
393399
}

0 commit comments

Comments
 (0)