]> git.lizzy.rs Git - rust.git/blob - src/librustc_trans/trans/glue.rs
4ca5fb07c98259a68237af43b58b4fda4dbc782d
[rust.git] / src / librustc_trans / trans / glue.rs
1 // Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 //!
12 //
13 // Code relating to drop glue.
14
15 use std;
16
17 use back::link;
18 use llvm;
19 use llvm::{ValueRef, get_param};
20 use middle::lang_items::ExchangeFreeFnLangItem;
21 use middle::subst::{Substs};
22 use middle::traits;
23 use middle::ty::{self, Ty, TyCtxt};
24 use trans::abi::{Abi, FnType};
25 use trans::adt;
26 use trans::adt::GetDtorType; // for tcx.dtor_type()
27 use trans::base::*;
28 use trans::build::*;
29 use trans::callee::{Callee, ArgVals};
30 use trans::cleanup;
31 use trans::cleanup::CleanupMethods;
32 use trans::collector::{self, TransItem};
33 use trans::common::*;
34 use trans::debuginfo::DebugLoc;
35 use trans::declare;
36 use trans::expr;
37 use trans::machine::*;
38 use trans::monomorphize;
39 use trans::type_of::{type_of, sizing_type_of, align_of};
40 use trans::type_::Type;
41 use trans::value::Value;
42
43 use arena::TypedArena;
44 use syntax::codemap::DUMMY_SP;
45
46 pub fn trans_exchange_free_dyn<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
47                                            v: ValueRef,
48                                            size: ValueRef,
49                                            align: ValueRef,
50                                            debug_loc: DebugLoc)
51                                            -> Block<'blk, 'tcx> {
52     let _icx = push_ctxt("trans_exchange_free");
53
54     let def_id = langcall(bcx, None, "", ExchangeFreeFnLangItem);
55     let args = [PointerCast(bcx, v, Type::i8p(bcx.ccx())), size, align];
56     Callee::def(bcx.ccx(), def_id, bcx.tcx().mk_substs(Substs::empty()))
57         .call(bcx, debug_loc, ArgVals(&args), None).bcx
58 }
59
60 pub fn trans_exchange_free<'blk, 'tcx>(cx: Block<'blk, 'tcx>,
61                                        v: ValueRef,
62                                        size: u64,
63                                        align: u32,
64                                        debug_loc: DebugLoc)
65                                        -> Block<'blk, 'tcx> {
66     trans_exchange_free_dyn(cx,
67                             v,
68                             C_uint(cx.ccx(), size),
69                             C_uint(cx.ccx(), align),
70                             debug_loc)
71 }
72
73 pub fn trans_exchange_free_ty<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
74                                           ptr: ValueRef,
75                                           content_ty: Ty<'tcx>,
76                                           debug_loc: DebugLoc)
77                                           -> Block<'blk, 'tcx> {
78     assert!(type_is_sized(bcx.ccx().tcx(), content_ty));
79     let sizing_type = sizing_type_of(bcx.ccx(), content_ty);
80     let content_size = llsize_of_alloc(bcx.ccx(), sizing_type);
81
82     // `Box<ZeroSizeType>` does not allocate.
83     if content_size != 0 {
84         let content_align = align_of(bcx.ccx(), content_ty);
85         trans_exchange_free(bcx, ptr, content_size, content_align, debug_loc)
86     } else {
87         bcx
88     }
89 }
90
91 pub fn type_needs_drop<'tcx>(tcx: &TyCtxt<'tcx>, ty: Ty<'tcx>) -> bool {
92     tcx.type_needs_drop_given_env(ty, &tcx.empty_parameter_environment())
93 }
94
95 pub fn get_drop_glue_type<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
96                                     t: Ty<'tcx>) -> Ty<'tcx> {
97     let tcx = ccx.tcx();
98     // Even if there is no dtor for t, there might be one deeper down and we
99     // might need to pass in the vtable ptr.
100     if !type_is_sized(tcx, t) {
101         return t
102     }
103
104     // FIXME (#22815): note that type_needs_drop conservatively
105     // approximates in some cases and may say a type expression
106     // requires drop glue when it actually does not.
107     //
108     // (In this case it is not clear whether any harm is done, i.e.
109     // erroneously returning `t` in some cases where we could have
110     // returned `tcx.types.i8` does not appear unsound. The impact on
111     // code quality is unknown at this time.)
112
113     if !type_needs_drop(&tcx, t) {
114         return tcx.types.i8;
115     }
116     match t.sty {
117         ty::TyBox(typ) if !type_needs_drop(&tcx, typ)
118                          && type_is_sized(tcx, typ) => {
119             let llty = sizing_type_of(ccx, typ);
120             // `Box<ZeroSizeType>` does not allocate.
121             if llsize_of_alloc(ccx, llty) == 0 {
122                 tcx.types.i8
123             } else {
124                 t
125             }
126         }
127         _ => t
128     }
129 }
130
131 pub fn drop_ty<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
132                            v: ValueRef,
133                            t: Ty<'tcx>,
134                            debug_loc: DebugLoc) -> Block<'blk, 'tcx> {
135     drop_ty_core(bcx, v, t, debug_loc, false, None)
136 }
137
138 pub fn drop_ty_core<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
139                                 v: ValueRef,
140                                 t: Ty<'tcx>,
141                                 debug_loc: DebugLoc,
142                                 skip_dtor: bool,
143                                 drop_hint: Option<cleanup::DropHintValue>)
144                                 -> Block<'blk, 'tcx> {
145     // NB: v is an *alias* of type t here, not a direct value.
146     debug!("drop_ty_core(t={:?}, skip_dtor={} drop_hint={:?})", t, skip_dtor, drop_hint);
147     let _icx = push_ctxt("drop_ty");
148     let mut bcx = bcx;
149     if bcx.fcx.type_needs_drop(t) {
150         let ccx = bcx.ccx();
151         let g = if skip_dtor {
152             DropGlueKind::TyContents(t)
153         } else {
154             DropGlueKind::Ty(t)
155         };
156         let glue = get_drop_glue_core(ccx, g);
157         let glue_type = get_drop_glue_type(ccx, t);
158         let ptr = if glue_type != t {
159             PointerCast(bcx, v, type_of(ccx, glue_type).ptr_to())
160         } else {
161             v
162         };
163
164         match drop_hint {
165             Some(drop_hint) => {
166                 let hint_val = load_ty(bcx, drop_hint.value(), bcx.tcx().types.u8);
167                 let moved_val =
168                     C_integral(Type::i8(bcx.ccx()), adt::DTOR_MOVED_HINT as u64, false);
169                 let may_need_drop =
170                     ICmp(bcx, llvm::IntNE, hint_val, moved_val, DebugLoc::None);
171                 bcx = with_cond(bcx, may_need_drop, |cx| {
172                     Call(cx, glue, &[ptr], debug_loc);
173                     cx
174                 })
175             }
176             None => {
177                 // No drop-hint ==> call standard drop glue
178                 Call(bcx, glue, &[ptr], debug_loc);
179             }
180         }
181     }
182     bcx
183 }
184
185 pub fn drop_ty_immediate<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
186                                      v: ValueRef,
187                                      t: Ty<'tcx>,
188                                      debug_loc: DebugLoc,
189                                      skip_dtor: bool)
190                                      -> Block<'blk, 'tcx> {
191     let _icx = push_ctxt("drop_ty_immediate");
192     let vp = alloc_ty(bcx, t, "");
193     call_lifetime_start(bcx, vp);
194     store_ty(bcx, v, vp, t);
195     let bcx = drop_ty_core(bcx, vp, t, debug_loc, skip_dtor, None);
196     call_lifetime_end(bcx, vp);
197     bcx
198 }
199
200 pub fn get_drop_glue<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, t: Ty<'tcx>) -> ValueRef {
201     get_drop_glue_core(ccx, DropGlueKind::Ty(t))
202 }
203
204 #[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
205 pub enum DropGlueKind<'tcx> {
206     /// The normal path; runs the dtor, and then recurs on the contents
207     Ty(Ty<'tcx>),
208     /// Skips the dtor, if any, for ty; drops the contents directly.
209     /// Note that the dtor is only skipped at the most *shallow*
210     /// level, namely, an `impl Drop for Ty` itself. So, for example,
211     /// if Ty is Newtype(S) then only the Drop impl for Newtype itself
212     /// will be skipped, while the Drop impl for S, if any, will be
213     /// invoked.
214     TyContents(Ty<'tcx>),
215 }
216
217 impl<'tcx> DropGlueKind<'tcx> {
218     fn ty(&self) -> Ty<'tcx> {
219         match *self { DropGlueKind::Ty(t) | DropGlueKind::TyContents(t) => t }
220     }
221
222     fn map_ty<F>(&self, mut f: F) -> DropGlueKind<'tcx> where F: FnMut(Ty<'tcx>) -> Ty<'tcx>
223     {
224         match *self {
225             DropGlueKind::Ty(t) => DropGlueKind::Ty(f(t)),
226             DropGlueKind::TyContents(t) => DropGlueKind::TyContents(f(t)),
227         }
228     }
229 }
230
231 fn get_drop_glue_core<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
232                                 g: DropGlueKind<'tcx>) -> ValueRef {
233     debug!("make drop glue for {:?}", g);
234     let g = g.map_ty(|t| get_drop_glue_type(ccx, t));
235     debug!("drop glue type {:?}", g);
236     match ccx.drop_glues().borrow().get(&g) {
237         Some(&glue) => return glue,
238         _ => { }
239     }
240     let t = g.ty();
241
242     let tcx = ccx.tcx();
243     let sig = ty::FnSig {
244         inputs: vec![tcx.mk_mut_ptr(tcx.types.i8)],
245         output: ty::FnOutput::FnConverging(tcx.mk_nil()),
246         variadic: false,
247     };
248     // Create a FnType for fn(*mut i8) and substitute the real type in
249     // later - that prevents FnType from splitting fat pointers up.
250     let mut fn_ty = FnType::new(ccx, Abi::Rust, &sig, &[]);
251     fn_ty.args[0].original_ty = type_of(ccx, t).ptr_to();
252     let llfnty = fn_ty.llvm_type(ccx);
253
254     // To avoid infinite recursion, don't `make_drop_glue` until after we've
255     // added the entry to the `drop_glues` cache.
256     if let Some(old_sym) = ccx.available_drop_glues().borrow().get(&g) {
257         let llfn = declare::declare_cfn(ccx, &old_sym, llfnty);
258         ccx.drop_glues().borrow_mut().insert(g, llfn);
259         return llfn;
260     };
261
262     let fn_nm = link::mangle_internal_name_by_type_and_seq(ccx, t, "drop");
263     assert!(declare::get_defined_value(ccx, &fn_nm).is_none());
264     let llfn = declare::declare_cfn(ccx, &fn_nm, llfnty);
265     ccx.available_drop_glues().borrow_mut().insert(g, fn_nm);
266     ccx.drop_glues().borrow_mut().insert(g, llfn);
267
268     let _s = StatRecorder::new(ccx, format!("drop {:?}", t));
269
270     let empty_substs = tcx.mk_substs(Substs::trans_empty());
271     let (arena, fcx): (TypedArena<_>, FunctionContext);
272     arena = TypedArena::new();
273     fcx = FunctionContext::new(ccx, llfn, fn_ty, None, empty_substs, &arena);
274
275     let bcx = fcx.init(false, None);
276
277     update_linkage(ccx, llfn, None, OriginalTranslation);
278
279     ccx.stats().n_glues_created.set(ccx.stats().n_glues_created.get() + 1);
280     // All glue functions take values passed *by alias*; this is a
281     // requirement since in many contexts glue is invoked indirectly and
282     // the caller has no idea if it's dealing with something that can be
283     // passed by value.
284     //
285     // llfn is expected be declared to take a parameter of the appropriate
286     // type, so we don't need to explicitly cast the function parameter.
287
288     let bcx = make_drop_glue(bcx, get_param(llfn, 0), g);
289     fcx.finish(bcx, DebugLoc::None);
290
291     llfn
292 }
293
294 fn trans_struct_drop_flag<'blk, 'tcx>(mut bcx: Block<'blk, 'tcx>,
295                                       t: Ty<'tcx>,
296                                       struct_data: ValueRef)
297                                       -> Block<'blk, 'tcx> {
298     assert!(type_is_sized(bcx.tcx(), t), "Precondition: caller must ensure t is sized");
299
300     let repr = adt::represent_type(bcx.ccx(), t);
301     let drop_flag = unpack_datum!(bcx, adt::trans_drop_flag_ptr(bcx, &repr, struct_data));
302     let loaded = load_ty(bcx, drop_flag.val, bcx.tcx().dtor_type());
303     let drop_flag_llty = type_of(bcx.fcx.ccx, bcx.tcx().dtor_type());
304     let init_val = C_integral(drop_flag_llty, adt::DTOR_NEEDED as u64, false);
305
306     let bcx = if !bcx.ccx().check_drop_flag_for_sanity() {
307         bcx
308     } else {
309         let drop_flag_llty = type_of(bcx.fcx.ccx, bcx.tcx().dtor_type());
310         let done_val = C_integral(drop_flag_llty, adt::DTOR_DONE as u64, false);
311         let not_init = ICmp(bcx, llvm::IntNE, loaded, init_val, DebugLoc::None);
312         let not_done = ICmp(bcx, llvm::IntNE, loaded, done_val, DebugLoc::None);
313         let drop_flag_neither_initialized_nor_cleared =
314             And(bcx, not_init, not_done, DebugLoc::None);
315         with_cond(bcx, drop_flag_neither_initialized_nor_cleared, |cx| {
316             let llfn = cx.ccx().get_intrinsic(&("llvm.debugtrap"));
317             Call(cx, llfn, &[], DebugLoc::None);
318             cx
319         })
320     };
321
322     let drop_flag_dtor_needed = ICmp(bcx, llvm::IntEQ, loaded, init_val, DebugLoc::None);
323     with_cond(bcx, drop_flag_dtor_needed, |cx| {
324         trans_struct_drop(cx, t, struct_data)
325     })
326 }
327 fn trans_struct_drop<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
328                                  t: Ty<'tcx>,
329                                  v0: ValueRef)
330                                  -> Block<'blk, 'tcx>
331 {
332     debug!("trans_struct_drop t: {}", t);
333     let tcx = bcx.tcx();
334     let mut bcx = bcx;
335
336     let def = t.ty_adt_def().unwrap();
337
338     // Be sure to put the contents into a scope so we can use an invoke
339     // instruction to call the user destructor but still call the field
340     // destructors if the user destructor panics.
341     //
342     // FIXME (#14875) panic-in-drop semantics might be unsupported; we
343     // might well consider changing below to more direct code.
344     let contents_scope = bcx.fcx.push_custom_cleanup_scope();
345
346     // Issue #23611: schedule cleanup of contents, re-inspecting the
347     // discriminant (if any) in case of variant swap in drop code.
348     bcx.fcx.schedule_drop_adt_contents(cleanup::CustomScope(contents_scope), v0, t);
349
350     let (sized_args, unsized_args);
351     let args: &[ValueRef] = if type_is_sized(tcx, t) {
352         sized_args = [v0];
353         &sized_args
354     } else {
355         unsized_args = [Load(bcx, expr::get_dataptr(bcx, v0)), Load(bcx, expr::get_meta(bcx, v0))];
356         &unsized_args
357     };
358
359     let trait_ref = ty::Binder(ty::TraitRef {
360         def_id: tcx.lang_items.drop_trait().unwrap(),
361         substs: tcx.mk_substs(Substs::trans_empty().with_self_ty(t))
362     });
363     let vtbl = match fulfill_obligation(bcx.ccx(), DUMMY_SP, trait_ref) {
364         traits::VtableImpl(data) => data,
365         _ => tcx.sess.bug(&format!("dtor for {:?} is not an impl???", t))
366     };
367     let dtor_did = def.destructor().unwrap();
368     bcx = Callee::def(bcx.ccx(), dtor_did, vtbl.substs)
369         .call(bcx, DebugLoc::None, ArgVals(args), None).bcx;
370
371     bcx.fcx.pop_and_trans_custom_cleanup_scope(bcx, contents_scope)
372 }
373
374 pub fn size_and_align_of_dst<'blk, 'tcx>(bcx: &BlockAndBuilder<'blk, 'tcx>,
375                                          t: Ty<'tcx>, info: ValueRef)
376                                          -> (ValueRef, ValueRef) {
377     debug!("calculate size of DST: {}; with lost info: {:?}",
378            t, Value(info));
379     if type_is_sized(bcx.tcx(), t) {
380         let sizing_type = sizing_type_of(bcx.ccx(), t);
381         let size = llsize_of_alloc(bcx.ccx(), sizing_type);
382         let align = align_of(bcx.ccx(), t);
383         debug!("size_and_align_of_dst t={} info={:?} size: {} align: {}",
384                t, Value(info), size, align);
385         let size = C_uint(bcx.ccx(), size);
386         let align = C_uint(bcx.ccx(), align);
387         return (size, align);
388     }
389     if bcx.is_unreachable() {
390         let llty = Type::int(bcx.ccx());
391         return (C_undef(llty), C_undef(llty));
392     }
393     match t.sty {
394         ty::TyStruct(def, substs) => {
395             let ccx = bcx.ccx();
396             // First get the size of all statically known fields.
397             // Don't use type_of::sizing_type_of because that expects t to be sized.
398             assert!(!t.is_simd());
399             let repr = adt::represent_type(ccx, t);
400             let sizing_type = adt::sizing_type_context_of(ccx, &repr, true);
401             debug!("DST {} sizing_type: {:?}", t, sizing_type);
402             let sized_size = llsize_of_alloc(ccx, sizing_type.prefix());
403             let sized_align = llalign_of_min(ccx, sizing_type.prefix());
404             debug!("DST {} statically sized prefix size: {} align: {}",
405                    t, sized_size, sized_align);
406             let sized_size = C_uint(ccx, sized_size);
407             let sized_align = C_uint(ccx, sized_align);
408
409             // Recurse to get the size of the dynamically sized field (must be
410             // the last field).
411             let last_field = def.struct_variant().fields.last().unwrap();
412             let field_ty = monomorphize::field_ty(bcx.tcx(), substs, last_field);
413             let (unsized_size, unsized_align) = size_and_align_of_dst(bcx, field_ty, info);
414
415             // FIXME (#26403, #27023): We should be adding padding
416             // to `sized_size` (to accommodate the `unsized_align`
417             // required of the unsized field that follows) before
418             // summing it with `sized_size`. (Note that since #26403
419             // is unfixed, we do not yet add the necessary padding
420             // here. But this is where the add would go.)
421
422             // Return the sum of sizes and max of aligns.
423             let mut size = bcx.add(sized_size, unsized_size);
424
425             // Issue #27023: If there is a drop flag, *now* we add 1
426             // to the size.  (We can do this without adding any
427             // padding because drop flags do not have any alignment
428             // constraints.)
429             if sizing_type.needs_drop_flag() {
430                 size = bcx.add(size, C_uint(bcx.ccx(), 1_u64));
431             }
432
433             // Choose max of two known alignments (combined value must
434             // be aligned according to more restrictive of the two).
435             let align = match (const_to_opt_uint(sized_align), const_to_opt_uint(unsized_align)) {
436                 (Some(sized_align), Some(unsized_align)) => {
437                     // If both alignments are constant, (the sized_align should always be), then
438                     // pick the correct alignment statically.
439                     C_uint(ccx, std::cmp::max(sized_align, unsized_align))
440                 }
441                 _ => bcx.select(bcx.icmp(llvm::IntUGT, sized_align, unsized_align),
442                                 sized_align,
443                                 unsized_align)
444             };
445
446             // Issue #27023: must add any necessary padding to `size`
447             // (to make it a multiple of `align`) before returning it.
448             //
449             // Namely, the returned size should be, in C notation:
450             //
451             //   `size + ((size & (align-1)) ? align : 0)`
452             //
453             // emulated via the semi-standard fast bit trick:
454             //
455             //   `(size + (align-1)) & -align`
456
457             let addend = bcx.sub(align, C_uint(bcx.ccx(), 1_u64));
458             let size = bcx.and(bcx.add(size, addend), bcx.neg(align));
459
460             (size, align)
461         }
462         ty::TyTrait(..) => {
463             // info points to the vtable and the second entry in the vtable is the
464             // dynamic size of the object.
465             let info = bcx.pointercast(info, Type::int(bcx.ccx()).ptr_to());
466             let size_ptr = bcx.gepi(info, &[1]);
467             let align_ptr = bcx.gepi(info, &[2]);
468             (bcx.load(size_ptr), bcx.load(align_ptr))
469         }
470         ty::TySlice(_) | ty::TyStr => {
471             let unit_ty = t.sequence_element_type(bcx.tcx());
472             // The info in this case is the length of the str, so the size is that
473             // times the unit size.
474             let llunit_ty = sizing_type_of(bcx.ccx(), unit_ty);
475             let unit_align = llalign_of_min(bcx.ccx(), llunit_ty);
476             let unit_size = llsize_of_alloc(bcx.ccx(), llunit_ty);
477             (bcx.mul(info, C_uint(bcx.ccx(), unit_size)),
478              C_uint(bcx.ccx(), unit_align))
479         }
480         _ => bcx.sess().bug(&format!("Unexpected unsized type, found {}", t))
481     }
482 }
483
484 fn make_drop_glue<'blk, 'tcx>(bcx: Block<'blk, 'tcx>, v0: ValueRef, g: DropGlueKind<'tcx>)
485                               -> Block<'blk, 'tcx> {
486     let t = g.ty();
487
488     if collector::collecting_debug_information(bcx.ccx()) {
489         bcx.ccx()
490            .record_translation_item_as_generated(TransItem::DropGlue(bcx.tcx()
491                                                                         .erase_regions(&t)));
492     }
493
494     let skip_dtor = match g { DropGlueKind::Ty(_) => false, DropGlueKind::TyContents(_) => true };
495     // NB: v0 is an *alias* of type t here, not a direct value.
496     let _icx = push_ctxt("make_drop_glue");
497
498     // Only drop the value when it ... well, we used to check for
499     // non-null, (and maybe we need to continue doing so), but we now
500     // must definitely check for special bit-patterns corresponding to
501     // the special dtor markings.
502
503     let inttype = Type::int(bcx.ccx());
504     let dropped_pattern = C_integral(inttype, adt::DTOR_DONE_U64, false);
505
506     match t.sty {
507         ty::TyBox(content_ty) => {
508             // Support for TyBox is built-in and its drop glue is
509             // special. It may move to library and have Drop impl. As
510             // a safe-guard, assert TyBox not used with TyContents.
511             assert!(!skip_dtor);
512             if !type_is_sized(bcx.tcx(), content_ty) {
513                 let llval = expr::get_dataptr(bcx, v0);
514                 let llbox = Load(bcx, llval);
515                 let llbox_as_usize = PtrToInt(bcx, llbox, Type::int(bcx.ccx()));
516                 let drop_flag_not_dropped_already =
517                     ICmp(bcx, llvm::IntNE, llbox_as_usize, dropped_pattern, DebugLoc::None);
518                 with_cond(bcx, drop_flag_not_dropped_already, |bcx| {
519                     let bcx = drop_ty(bcx, v0, content_ty, DebugLoc::None);
520                     let info = expr::get_meta(bcx, v0);
521                     let info = Load(bcx, info);
522                     let (llsize, llalign) =
523                         size_and_align_of_dst(&bcx.build(), content_ty, info);
524
525                     // `Box<ZeroSizeType>` does not allocate.
526                     let needs_free = ICmp(bcx,
527                                           llvm::IntNE,
528                                           llsize,
529                                           C_uint(bcx.ccx(), 0u64),
530                                           DebugLoc::None);
531                     with_cond(bcx, needs_free, |bcx| {
532                         trans_exchange_free_dyn(bcx, llbox, llsize, llalign, DebugLoc::None)
533                     })
534                 })
535             } else {
536                 let llval = v0;
537                 let llbox = Load(bcx, llval);
538                 let llbox_as_usize = PtrToInt(bcx, llbox, inttype);
539                 let drop_flag_not_dropped_already =
540                     ICmp(bcx, llvm::IntNE, llbox_as_usize, dropped_pattern, DebugLoc::None);
541                 with_cond(bcx, drop_flag_not_dropped_already, |bcx| {
542                     let bcx = drop_ty(bcx, llbox, content_ty, DebugLoc::None);
543                     trans_exchange_free_ty(bcx, llbox, content_ty, DebugLoc::None)
544                 })
545             }
546         }
547         ty::TyStruct(def, _) | ty::TyEnum(def, _) => {
548             match (def.dtor_kind(), skip_dtor) {
549                 (ty::TraitDtor(true), false) => {
550                     // FIXME(16758) Since the struct is unsized, it is hard to
551                     // find the drop flag (which is at the end of the struct).
552                     // Lets just ignore the flag and pretend everything will be
553                     // OK.
554                     if type_is_sized(bcx.tcx(), t) {
555                         trans_struct_drop_flag(bcx, t, v0)
556                     } else {
557                         // Give the user a heads up that we are doing something
558                         // stupid and dangerous.
559                         bcx.sess().warn(&format!("Ignoring drop flag in destructor for {}\
560                                                  because the struct is unsized. See issue\
561                                                  #16758", t));
562                         trans_struct_drop(bcx, t, v0)
563                     }
564                 }
565                 (ty::TraitDtor(false), false) => {
566                     trans_struct_drop(bcx, t, v0)
567                 }
568                 (ty::NoDtor, _) | (_, true) => {
569                     // No dtor? Just the default case
570                     iter_structural_ty(bcx, v0, t, |bb, vv, tt| drop_ty(bb, vv, tt, DebugLoc::None))
571                 }
572             }
573         }
574         ty::TyTrait(..) => {
575             // No support in vtable for distinguishing destroying with
576             // versus without calling Drop::drop. Assert caller is
577             // okay with always calling the Drop impl, if any.
578             assert!(!skip_dtor);
579             let data_ptr = expr::get_dataptr(bcx, v0);
580             let vtable_ptr = Load(bcx, expr::get_meta(bcx, v0));
581             let dtor = Load(bcx, vtable_ptr);
582             Call(bcx,
583                  dtor,
584                  &[PointerCast(bcx, Load(bcx, data_ptr), Type::i8p(bcx.ccx()))],
585                  DebugLoc::None);
586             bcx
587         }
588         _ => {
589             if bcx.fcx.type_needs_drop(t) {
590                 iter_structural_ty(bcx,
591                                    v0,
592                                    t,
593                                    |bb, vv, tt| drop_ty(bb, vv, tt, DebugLoc::None))
594             } else {
595                 bcx
596             }
597         }
598     }
599 }