]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_const_eval/src/interpret/terminator.rs
Preparing for merge from rustc
[rust.git] / compiler / rustc_const_eval / src / interpret / terminator.rs
1 use std::borrow::Cow;
2
3 use rustc_ast::ast::InlineAsmOptions;
4 use rustc_middle::ty::layout::{FnAbiOf, LayoutOf};
5 use rustc_middle::ty::Instance;
6 use rustc_middle::{
7     mir,
8     ty::{self, Ty},
9 };
10 use rustc_target::abi;
11 use rustc_target::abi::call::{ArgAbi, ArgAttribute, ArgAttributes, FnAbi, PassMode};
12 use rustc_target::spec::abi::Abi;
13
14 use super::{
15     FnVal, ImmTy, Immediate, InterpCx, InterpResult, MPlaceTy, Machine, MemoryKind, OpTy, Operand,
16     PlaceTy, Scalar, StackPopCleanup, StackPopUnwind,
17 };
18
19 impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
20     pub(super) fn eval_terminator(
21         &mut self,
22         terminator: &mir::Terminator<'tcx>,
23     ) -> InterpResult<'tcx> {
24         use rustc_middle::mir::TerminatorKind::*;
25         match terminator.kind {
26             Return => {
27                 self.pop_stack_frame(/* unwinding */ false)?
28             }
29
30             Goto { target } => self.go_to_block(target),
31
32             SwitchInt { ref discr, ref targets } => {
33                 let discr = self.read_immediate(&self.eval_operand(discr, None)?)?;
34                 trace!("SwitchInt({:?})", *discr);
35
36                 // Branch to the `otherwise` case by default, if no match is found.
37                 let mut target_block = targets.otherwise();
38
39                 for (const_int, target) in targets.iter() {
40                     // Compare using MIR BinOp::Eq, to also support pointer values.
41                     // (Avoiding `self.binary_op` as that does some redundant layout computation.)
42                     let res = self
43                         .overflowing_binary_op(
44                             mir::BinOp::Eq,
45                             &discr,
46                             &ImmTy::from_uint(const_int, discr.layout),
47                         )?
48                         .0;
49                     if res.to_bool()? {
50                         target_block = target;
51                         break;
52                     }
53                 }
54
55                 self.go_to_block(target_block);
56             }
57
58             Call {
59                 ref func,
60                 ref args,
61                 destination,
62                 target,
63                 ref cleanup,
64                 from_hir_call: _,
65                 fn_span: _,
66             } => {
67                 let old_stack = self.frame_idx();
68                 let old_loc = self.frame().loc;
69                 let func = self.eval_operand(func, None)?;
70                 let args = self.eval_operands(args)?;
71
72                 let fn_sig_binder = func.layout.ty.fn_sig(*self.tcx);
73                 let fn_sig =
74                     self.tcx.normalize_erasing_late_bound_regions(self.param_env, fn_sig_binder);
75                 let extra_args = &args[fn_sig.inputs().len()..];
76                 let extra_args = self.tcx.mk_type_list(extra_args.iter().map(|arg| arg.layout.ty));
77
78                 let (fn_val, fn_abi, with_caller_location) = match *func.layout.ty.kind() {
79                     ty::FnPtr(_sig) => {
80                         let fn_ptr = self.read_pointer(&func)?;
81                         let fn_val = self.get_ptr_fn(fn_ptr)?;
82                         (fn_val, self.fn_abi_of_fn_ptr(fn_sig_binder, extra_args)?, false)
83                     }
84                     ty::FnDef(def_id, substs) => {
85                         let instance =
86                             self.resolve(ty::WithOptConstParam::unknown(def_id), substs)?;
87                         (
88                             FnVal::Instance(instance),
89                             self.fn_abi_of_instance(instance, extra_args)?,
90                             instance.def.requires_caller_location(*self.tcx),
91                         )
92                     }
93                     _ => span_bug!(
94                         terminator.source_info.span,
95                         "invalid callee of type {:?}",
96                         func.layout.ty
97                     ),
98                 };
99
100                 let destination = self.eval_place(destination)?;
101                 self.eval_fn_call(
102                     fn_val,
103                     (fn_sig.abi, fn_abi),
104                     &args,
105                     with_caller_location,
106                     &destination,
107                     target,
108                     match (cleanup, fn_abi.can_unwind) {
109                         (Some(cleanup), true) => StackPopUnwind::Cleanup(*cleanup),
110                         (None, true) => StackPopUnwind::Skip,
111                         (_, false) => StackPopUnwind::NotAllowed,
112                     },
113                 )?;
114                 // Sanity-check that `eval_fn_call` either pushed a new frame or
115                 // did a jump to another block.
116                 if self.frame_idx() == old_stack && self.frame().loc == old_loc {
117                     span_bug!(terminator.source_info.span, "evaluating this call made no progress");
118                 }
119             }
120
121             Drop { place, target, unwind } => {
122                 let place = self.eval_place(place)?;
123                 let ty = place.layout.ty;
124                 trace!("TerminatorKind::drop: {:?}, type {}", place, ty);
125
126                 let instance = Instance::resolve_drop_in_place(*self.tcx, ty);
127                 self.drop_in_place(&place, instance, target, unwind)?;
128             }
129
130             Assert { ref cond, expected, ref msg, target, cleanup } => {
131                 let cond_val = self.read_scalar(&self.eval_operand(cond, None)?)?.to_bool()?;
132                 if expected == cond_val {
133                     self.go_to_block(target);
134                 } else {
135                     M::assert_panic(self, msg, cleanup)?;
136                 }
137             }
138
139             Abort => {
140                 M::abort(self, "the program aborted execution".to_owned())?;
141             }
142
143             // When we encounter Resume, we've finished unwinding
144             // cleanup for the current stack frame. We pop it in order
145             // to continue unwinding the next frame
146             Resume => {
147                 trace!("unwinding: resuming from cleanup");
148                 // By definition, a Resume terminator means
149                 // that we're unwinding
150                 self.pop_stack_frame(/* unwinding */ true)?;
151                 return Ok(());
152             }
153
154             // It is UB to ever encounter this.
155             Unreachable => throw_ub!(Unreachable),
156
157             // These should never occur for MIR we actually run.
158             DropAndReplace { .. }
159             | FalseEdge { .. }
160             | FalseUnwind { .. }
161             | Yield { .. }
162             | GeneratorDrop => span_bug!(
163                 terminator.source_info.span,
164                 "{:#?} should have been eliminated by MIR pass",
165                 terminator.kind
166             ),
167
168             InlineAsm { template, ref operands, options, destination, .. } => {
169                 M::eval_inline_asm(self, template, operands, options)?;
170                 if options.contains(InlineAsmOptions::NORETURN) {
171                     throw_ub_format!("returned from noreturn inline assembly");
172                 }
173                 self.go_to_block(
174                     destination
175                         .expect("InlineAsm terminators without noreturn must have a destination"),
176                 )
177             }
178         }
179
180         Ok(())
181     }
182
183     fn check_argument_compat(
184         caller_abi: &ArgAbi<'tcx, Ty<'tcx>>,
185         callee_abi: &ArgAbi<'tcx, Ty<'tcx>>,
186     ) -> bool {
187         // Heuristic for type comparison.
188         let layout_compat = || {
189             if caller_abi.layout.ty == callee_abi.layout.ty {
190                 // No question
191                 return true;
192             }
193             if caller_abi.layout.is_unsized() || callee_abi.layout.is_unsized() {
194                 // No, no, no. We require the types to *exactly* match for unsized arguments. If
195                 // these are somehow unsized "in a different way" (say, `dyn Trait` vs `[i32]`),
196                 // then who knows what happens.
197                 return false;
198             }
199             if caller_abi.layout.size != callee_abi.layout.size
200                 || caller_abi.layout.align.abi != callee_abi.layout.align.abi
201             {
202                 // This cannot go well...
203                 return false;
204             }
205             // The rest *should* be okay, but we are extra conservative.
206             match (caller_abi.layout.abi, callee_abi.layout.abi) {
207                 // Different valid ranges are okay (once we enforce validity,
208                 // that will take care to make it UB to leave the range, just
209                 // like for transmute).
210                 (abi::Abi::Scalar(caller), abi::Abi::Scalar(callee)) => {
211                     caller.primitive() == callee.primitive()
212                 }
213                 (
214                     abi::Abi::ScalarPair(caller1, caller2),
215                     abi::Abi::ScalarPair(callee1, callee2),
216                 ) => {
217                     caller1.primitive() == callee1.primitive()
218                         && caller2.primitive() == callee2.primitive()
219                 }
220                 // Be conservative
221                 _ => false,
222             }
223         };
224         // When comparing the PassMode, we have to be smart about comparing the attributes.
225         let arg_attr_compat = |a1: &ArgAttributes, a2: &ArgAttributes| {
226             // There's only one regular attribute that matters for the call ABI: InReg.
227             // Everything else is things like noalias, dereferenceable, nonnull, ...
228             // (This also applies to pointee_size, pointee_align.)
229             if a1.regular.contains(ArgAttribute::InReg) != a2.regular.contains(ArgAttribute::InReg)
230             {
231                 return false;
232             }
233             // We also compare the sign extension mode -- this could let the callee make assumptions
234             // about bits that conceptually were not even passed.
235             if a1.arg_ext != a2.arg_ext {
236                 return false;
237             }
238             return true;
239         };
240         let mode_compat = || match (&caller_abi.mode, &callee_abi.mode) {
241             (PassMode::Ignore, PassMode::Ignore) => true,
242             (PassMode::Direct(a1), PassMode::Direct(a2)) => arg_attr_compat(a1, a2),
243             (PassMode::Pair(a1, b1), PassMode::Pair(a2, b2)) => {
244                 arg_attr_compat(a1, a2) && arg_attr_compat(b1, b2)
245             }
246             (PassMode::Cast(c1, pad1), PassMode::Cast(c2, pad2)) => c1 == c2 && pad1 == pad2,
247             (
248                 PassMode::Indirect { attrs: a1, extra_attrs: None, on_stack: s1 },
249                 PassMode::Indirect { attrs: a2, extra_attrs: None, on_stack: s2 },
250             ) => arg_attr_compat(a1, a2) && s1 == s2,
251             (
252                 PassMode::Indirect { attrs: a1, extra_attrs: Some(e1), on_stack: s1 },
253                 PassMode::Indirect { attrs: a2, extra_attrs: Some(e2), on_stack: s2 },
254             ) => arg_attr_compat(a1, a2) && arg_attr_compat(e1, e2) && s1 == s2,
255             _ => false,
256         };
257
258         if layout_compat() && mode_compat() {
259             return true;
260         }
261         trace!(
262             "check_argument_compat: incompatible ABIs:\ncaller: {:?}\ncallee: {:?}",
263             caller_abi,
264             callee_abi
265         );
266         return false;
267     }
268
269     /// Initialize a single callee argument, checking the types for compatibility.
270     fn pass_argument<'x, 'y>(
271         &mut self,
272         caller_args: &mut impl Iterator<
273             Item = (&'x OpTy<'tcx, M::Provenance>, &'y ArgAbi<'tcx, Ty<'tcx>>),
274         >,
275         callee_abi: &ArgAbi<'tcx, Ty<'tcx>>,
276         callee_arg: &PlaceTy<'tcx, M::Provenance>,
277     ) -> InterpResult<'tcx>
278     where
279         'tcx: 'x,
280         'tcx: 'y,
281     {
282         if matches!(callee_abi.mode, PassMode::Ignore) {
283             // This one is skipped.
284             return Ok(());
285         }
286         // Find next caller arg.
287         let (caller_arg, caller_abi) = caller_args.next().ok_or_else(|| {
288             err_ub_format!("calling a function with fewer arguments than it requires")
289         })?;
290         // Now, check
291         if !Self::check_argument_compat(caller_abi, callee_abi) {
292             throw_ub_format!(
293                 "calling a function with argument of type {:?} passing data of type {:?}",
294                 callee_arg.layout.ty,
295                 caller_arg.layout.ty
296             )
297         }
298         // Special handling for unsized parameters.
299         if caller_arg.layout.is_unsized() {
300             // `check_argument_compat` ensures that both have the same type, so we know they will use the metadata the same way.
301             assert_eq!(caller_arg.layout.ty, callee_arg.layout.ty);
302             // We have to properly pre-allocate the memory for the callee.
303             // So let's tear down some wrappers.
304             // This all has to be in memory, there are no immediate unsized values.
305             let src = caller_arg.assert_mem_place();
306             // The destination cannot be one of these "spread args".
307             let (dest_frame, dest_local) = callee_arg.assert_local();
308             // We are just initializing things, so there can't be anything here yet.
309             assert!(matches!(
310                 *self.local_to_op(&self.stack()[dest_frame], dest_local, None)?,
311                 Operand::Immediate(Immediate::Uninit)
312             ));
313             // Allocate enough memory to hold `src`.
314             let Some((size, align)) = self.size_and_align_of_mplace(&src)? else {
315                 span_bug!(self.cur_span(), "unsized fn arg with `extern` type tail should not be allowed")
316             };
317             let ptr = self.allocate_ptr(size, align, MemoryKind::Stack)?;
318             let dest_place =
319                 MPlaceTy::from_aligned_ptr_with_meta(ptr.into(), callee_arg.layout, src.meta);
320             // Update the local to be that new place.
321             *M::access_local_mut(self, dest_frame, dest_local)? = Operand::Indirect(*dest_place);
322         }
323         // We allow some transmutes here.
324         // FIXME: Depending on the PassMode, this should reset some padding to uninitialized. (This
325         // is true for all `copy_op`, but there are a lot of special cases for argument passing
326         // specifically.)
327         self.copy_op(&caller_arg, callee_arg, /*allow_transmute*/ true)
328     }
329
330     /// Call this function -- pushing the stack frame and initializing the arguments.
331     ///
332     /// `caller_fn_abi` is used to determine if all the arguments are passed the proper way.
333     /// However, we also need `caller_abi` to determine if we need to do untupling of arguments.
334     ///
335     /// `with_caller_location` indicates whether the caller passed a caller location. Miri
336     /// implements caller locations without argument passing, but to match `FnAbi` we need to know
337     /// when those arguments are present.
338     pub(crate) fn eval_fn_call(
339         &mut self,
340         fn_val: FnVal<'tcx, M::ExtraFnVal>,
341         (caller_abi, caller_fn_abi): (Abi, &FnAbi<'tcx, Ty<'tcx>>),
342         args: &[OpTy<'tcx, M::Provenance>],
343         with_caller_location: bool,
344         destination: &PlaceTy<'tcx, M::Provenance>,
345         target: Option<mir::BasicBlock>,
346         mut unwind: StackPopUnwind,
347     ) -> InterpResult<'tcx> {
348         trace!("eval_fn_call: {:#?}", fn_val);
349
350         let instance = match fn_val {
351             FnVal::Instance(instance) => instance,
352             FnVal::Other(extra) => {
353                 return M::call_extra_fn(
354                     self,
355                     extra,
356                     caller_abi,
357                     args,
358                     destination,
359                     target,
360                     unwind,
361                 );
362             }
363         };
364
365         match instance.def {
366             ty::InstanceDef::Intrinsic(def_id) => {
367                 assert!(self.tcx.is_intrinsic(def_id));
368                 // caller_fn_abi is not relevant here, we interpret the arguments directly for each intrinsic.
369                 M::call_intrinsic(self, instance, args, destination, target, unwind)
370             }
371             ty::InstanceDef::VTableShim(..)
372             | ty::InstanceDef::ReifyShim(..)
373             | ty::InstanceDef::ClosureOnceShim { .. }
374             | ty::InstanceDef::FnPtrShim(..)
375             | ty::InstanceDef::DropGlue(..)
376             | ty::InstanceDef::CloneShim(..)
377             | ty::InstanceDef::Item(_) => {
378                 // We need MIR for this fn
379                 let Some((body, instance)) =
380                     M::find_mir_or_eval_fn(self, instance, caller_abi, args, destination, target, unwind)? else {
381                         return Ok(());
382                     };
383
384                 // Compute callee information using the `instance` returned by
385                 // `find_mir_or_eval_fn`.
386                 // FIXME: for variadic support, do we have to somehow determine callee's extra_args?
387                 let callee_fn_abi = self.fn_abi_of_instance(instance, ty::List::empty())?;
388
389                 if callee_fn_abi.c_variadic || caller_fn_abi.c_variadic {
390                     throw_unsup_format!("calling a c-variadic function is not supported");
391                 }
392
393                 if M::enforce_abi(self) {
394                     if caller_fn_abi.conv != callee_fn_abi.conv {
395                         throw_ub_format!(
396                             "calling a function with calling convention {:?} using calling convention {:?}",
397                             callee_fn_abi.conv,
398                             caller_fn_abi.conv
399                         )
400                     }
401                 }
402
403                 if !matches!(unwind, StackPopUnwind::NotAllowed) && !callee_fn_abi.can_unwind {
404                     // The callee cannot unwind.
405                     unwind = StackPopUnwind::NotAllowed;
406                 }
407
408                 self.push_stack_frame(
409                     instance,
410                     body,
411                     destination,
412                     StackPopCleanup::Goto { ret: target, unwind },
413                 )?;
414
415                 // If an error is raised here, pop the frame again to get an accurate backtrace.
416                 // To this end, we wrap it all in a `try` block.
417                 let res: InterpResult<'tcx> = try {
418                     trace!(
419                         "caller ABI: {:?}, args: {:#?}",
420                         caller_abi,
421                         args.iter()
422                             .map(|arg| (arg.layout.ty, format!("{:?}", **arg)))
423                             .collect::<Vec<_>>()
424                     );
425                     trace!(
426                         "spread_arg: {:?}, locals: {:#?}",
427                         body.spread_arg,
428                         body.args_iter()
429                             .map(|local| (
430                                 local,
431                                 self.layout_of_local(self.frame(), local, None).unwrap().ty
432                             ))
433                             .collect::<Vec<_>>()
434                     );
435
436                     // In principle, we have two iterators: Where the arguments come from, and where
437                     // they go to.
438
439                     // For where they come from: If the ABI is RustCall, we untuple the
440                     // last incoming argument.  These two iterators do not have the same type,
441                     // so to keep the code paths uniform we accept an allocation
442                     // (for RustCall ABI only).
443                     let caller_args: Cow<'_, [OpTy<'tcx, M::Provenance>]> =
444                         if caller_abi == Abi::RustCall && !args.is_empty() {
445                             // Untuple
446                             let (untuple_arg, args) = args.split_last().unwrap();
447                             trace!("eval_fn_call: Will pass last argument by untupling");
448                             Cow::from(
449                                 args.iter()
450                                     .map(|a| Ok(a.clone()))
451                                     .chain(
452                                         (0..untuple_arg.layout.fields.count())
453                                             .map(|i| self.operand_field(untuple_arg, i)),
454                                     )
455                                     .collect::<InterpResult<'_, Vec<OpTy<'tcx, M::Provenance>>>>(
456                                     )?,
457                             )
458                         } else {
459                             // Plain arg passing
460                             Cow::from(args)
461                         };
462                     // If `with_caller_location` is set we pretend there is an extra argument (that
463                     // we will not pass).
464                     assert_eq!(
465                         caller_args.len() + if with_caller_location { 1 } else { 0 },
466                         caller_fn_abi.args.len(),
467                         "mismatch between caller ABI and caller arguments",
468                     );
469                     let mut caller_args = caller_args
470                         .iter()
471                         .zip(caller_fn_abi.args.iter())
472                         .filter(|arg_and_abi| !matches!(arg_and_abi.1.mode, PassMode::Ignore));
473
474                     // Now we have to spread them out across the callee's locals,
475                     // taking into account the `spread_arg`.  If we could write
476                     // this is a single iterator (that handles `spread_arg`), then
477                     // `pass_argument` would be the loop body. It takes care to
478                     // not advance `caller_iter` for ZSTs.
479                     let mut callee_args_abis = callee_fn_abi.args.iter();
480                     for local in body.args_iter() {
481                         let dest = self.eval_place(mir::Place::from(local))?;
482                         if Some(local) == body.spread_arg {
483                             // Must be a tuple
484                             for i in 0..dest.layout.fields.count() {
485                                 let dest = self.place_field(&dest, i)?;
486                                 let callee_abi = callee_args_abis.next().unwrap();
487                                 self.pass_argument(&mut caller_args, callee_abi, &dest)?;
488                             }
489                         } else {
490                             // Normal argument
491                             let callee_abi = callee_args_abis.next().unwrap();
492                             self.pass_argument(&mut caller_args, callee_abi, &dest)?;
493                         }
494                     }
495                     // If the callee needs a caller location, pretend we consume one more argument from the ABI.
496                     if instance.def.requires_caller_location(*self.tcx) {
497                         callee_args_abis.next().unwrap();
498                     }
499                     // Now we should have no more caller args or callee arg ABIs
500                     assert!(
501                         callee_args_abis.next().is_none(),
502                         "mismatch between callee ABI and callee body arguments"
503                     );
504                     if caller_args.next().is_some() {
505                         throw_ub_format!("calling a function with more arguments than it expected")
506                     }
507                     // Don't forget to check the return type!
508                     if !Self::check_argument_compat(&caller_fn_abi.ret, &callee_fn_abi.ret) {
509                         throw_ub_format!(
510                             "calling a function with return type {:?} passing \
511                                     return place of type {:?}",
512                             callee_fn_abi.ret.layout.ty,
513                             caller_fn_abi.ret.layout.ty,
514                         )
515                     }
516                 };
517                 match res {
518                     Err(err) => {
519                         self.stack_mut().pop();
520                         Err(err)
521                     }
522                     Ok(()) => Ok(()),
523                 }
524             }
525             // cannot use the shim here, because that will only result in infinite recursion
526             ty::InstanceDef::Virtual(def_id, idx) => {
527                 let mut args = args.to_vec();
528                 // We have to implement all "object safe receivers". So we have to go search for a
529                 // pointer or `dyn Trait` type, but it could be wrapped in newtypes. So recursively
530                 // unwrap those newtypes until we are there.
531                 let mut receiver = args[0].clone();
532                 let receiver_place = loop {
533                     match receiver.layout.ty.kind() {
534                         ty::Ref(..) | ty::RawPtr(..) => break self.deref_operand(&receiver)?,
535                         ty::Dynamic(..) => break receiver.assert_mem_place(), // no immediate unsized values
536                         _ => {
537                             // Not there yet, search for the only non-ZST field.
538                             let mut non_zst_field = None;
539                             for i in 0..receiver.layout.fields.count() {
540                                 let field = self.operand_field(&receiver, i)?;
541                                 let zst =
542                                     field.layout.is_zst() && field.layout.align.abi.bytes() == 1;
543                                 if !zst {
544                                     assert!(
545                                         non_zst_field.is_none(),
546                                         "multiple non-ZST fields in dyn receiver type {}",
547                                         receiver.layout.ty
548                                     );
549                                     non_zst_field = Some(field);
550                                 }
551                             }
552                             receiver = non_zst_field.unwrap_or_else(|| {
553                                 panic!(
554                                     "no non-ZST fields in dyn receiver type {}",
555                                     receiver.layout.ty
556                                 )
557                             });
558                         }
559                     }
560                 };
561                 // Obtain the underlying trait we are working on.
562                 let receiver_tail = self
563                     .tcx
564                     .struct_tail_erasing_lifetimes(receiver_place.layout.ty, self.param_env);
565                 let ty::Dynamic(data, ..) = receiver_tail.kind() else {
566                     span_bug!(self.cur_span(), "dynamic call on non-`dyn` type {}", receiver_tail)
567                 };
568
569                 // Get the required information from the vtable.
570                 let vptr = receiver_place.meta.unwrap_meta().to_pointer(self)?;
571                 let (dyn_ty, dyn_trait) = self.get_ptr_vtable(vptr)?;
572                 if dyn_trait != data.principal() {
573                     throw_ub_format!(
574                         "`dyn` call on a pointer whose vtable does not match its type"
575                     );
576                 }
577
578                 // Now determine the actual method to call. We can do that in two different ways and
579                 // compare them to ensure everything fits.
580                 let Some(ty::VtblEntry::Method(fn_inst)) = self.get_vtable_entries(vptr)?.get(idx).copied() else {
581                     throw_ub_format!("`dyn` call trying to call something that is not a method")
582                 };
583                 if cfg!(debug_assertions) {
584                     let tcx = *self.tcx;
585
586                     let trait_def_id = tcx.trait_of_item(def_id).unwrap();
587                     let virtual_trait_ref =
588                         ty::TraitRef::from_method(tcx, trait_def_id, instance.substs);
589                     assert_eq!(
590                         receiver_tail,
591                         virtual_trait_ref.self_ty(),
592                         "mismatch in underlying dyn trait computation within Miri and MIR building",
593                     );
594                     let existential_trait_ref =
595                         ty::ExistentialTraitRef::erase_self_ty(tcx, virtual_trait_ref);
596                     let concrete_trait_ref = existential_trait_ref.with_self_ty(tcx, dyn_ty);
597
598                     let concrete_method = Instance::resolve_for_vtable(
599                         tcx,
600                         self.param_env,
601                         def_id,
602                         instance.substs.rebase_onto(tcx, trait_def_id, concrete_trait_ref.substs),
603                     )
604                     .unwrap();
605                     assert_eq!(fn_inst, concrete_method);
606                 }
607
608                 // `*mut receiver_place.layout.ty` is almost the layout that we
609                 // want for args[0]: We have to project to field 0 because we want
610                 // a thin pointer.
611                 assert!(receiver_place.layout.is_unsized());
612                 let receiver_ptr_ty = self.tcx.mk_mut_ptr(receiver_place.layout.ty);
613                 let this_receiver_ptr = self.layout_of(receiver_ptr_ty)?.field(self, 0);
614                 // Adjust receiver argument.
615                 args[0] = OpTy::from(ImmTy::from_immediate(
616                     Scalar::from_maybe_pointer(receiver_place.ptr, self).into(),
617                     this_receiver_ptr,
618                 ));
619                 trace!("Patched receiver operand to {:#?}", args[0]);
620                 // recurse with concrete function
621                 self.eval_fn_call(
622                     FnVal::Instance(fn_inst),
623                     (caller_abi, caller_fn_abi),
624                     &args,
625                     with_caller_location,
626                     destination,
627                     target,
628                     unwind,
629                 )
630             }
631         }
632     }
633
634     fn drop_in_place(
635         &mut self,
636         place: &PlaceTy<'tcx, M::Provenance>,
637         instance: ty::Instance<'tcx>,
638         target: mir::BasicBlock,
639         unwind: Option<mir::BasicBlock>,
640     ) -> InterpResult<'tcx> {
641         trace!("drop_in_place: {:?},\n  {:?}, {:?}", *place, place.layout.ty, instance);
642         // We take the address of the object.  This may well be unaligned, which is fine
643         // for us here.  However, unaligned accesses will probably make the actual drop
644         // implementation fail -- a problem shared by rustc.
645         let place = self.force_allocation(place)?;
646
647         let (instance, place) = match place.layout.ty.kind() {
648             ty::Dynamic(..) => {
649                 // Dropping a trait object. Need to find actual drop fn.
650                 let place = self.unpack_dyn_trait(&place)?;
651                 let instance = ty::Instance::resolve_drop_in_place(*self.tcx, place.layout.ty);
652                 (instance, place)
653             }
654             _ => (instance, place),
655         };
656         let fn_abi = self.fn_abi_of_instance(instance, ty::List::empty())?;
657
658         let arg = ImmTy::from_immediate(
659             place.to_ref(self),
660             self.layout_of(self.tcx.mk_mut_ptr(place.layout.ty))?,
661         );
662         let ret = MPlaceTy::fake_alloc_zst(self.layout_of(self.tcx.types.unit)?);
663
664         self.eval_fn_call(
665             FnVal::Instance(instance),
666             (Abi::Rust, fn_abi),
667             &[arg.into()],
668             false,
669             &ret.into(),
670             Some(target),
671             match unwind {
672                 Some(cleanup) => StackPopUnwind::Cleanup(cleanup),
673                 None => StackPopUnwind::Skip,
674             },
675         )
676     }
677 }