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