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