Skip to content

Commit d07ee25

Browse files
author
Ariel Ben-Yehuda
committed
handle dtors having generics in an order different from their ADT
Fixes #27997.
1 parent 2f052eb commit d07ee25

File tree

3 files changed

+92
-76
lines changed

3 files changed

+92
-76
lines changed

src/librustc_trans/trans/glue.rs

Lines changed: 44 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,9 @@
1616
use back::link::*;
1717
use llvm;
1818
use llvm::{ValueRef, get_param};
19-
use metadata::csearch;
20-
use middle::def_id::{DefId, LOCAL_CRATE};
2119
use middle::lang_items::ExchangeFreeFnLangItem;
22-
use middle::subst;
23-
use middle::subst::{Subst, Substs};
20+
use middle::subst::{Substs};
21+
use middle::traits;
2422
use middle::ty::{self, Ty};
2523
use trans::adt;
2624
use trans::adt::GetDtorType; // for tcx.dtor_type()
@@ -33,16 +31,15 @@ use trans::common::*;
3331
use trans::debuginfo::DebugLoc;
3432
use trans::declare;
3533
use trans::expr;
36-
use trans::foreign;
37-
use trans::inline;
3834
use trans::machine::*;
3935
use trans::monomorphize;
40-
use trans::type_of::{type_of, type_of_dtor, sizing_type_of, align_of};
36+
use trans::type_of::{type_of, sizing_type_of, align_of};
4137
use trans::type_::Type;
4238

4339
use arena::TypedArena;
4440
use libc::c_uint;
4541
use syntax::ast;
42+
use syntax::codemap::DUMMY_SP;
4643

4744
pub fn trans_exchange_free_dyn<'blk, 'tcx>(cx: Block<'blk, 'tcx>,
4845
v: ValueRef,
@@ -287,10 +284,7 @@ fn get_drop_glue_core<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
287284

288285
fn trans_struct_drop_flag<'blk, 'tcx>(mut bcx: Block<'blk, 'tcx>,
289286
t: Ty<'tcx>,
290-
struct_data: ValueRef,
291-
dtor_did: DefId,
292-
class_did: DefId,
293-
substs: &subst::Substs<'tcx>)
287+
struct_data: ValueRef)
294288
-> Block<'blk, 'tcx> {
295289
assert!(type_is_sized(bcx.tcx(), t), "Precondition: caller must ensure t is sized");
296290

@@ -318,59 +312,19 @@ fn trans_struct_drop_flag<'blk, 'tcx>(mut bcx: Block<'blk, 'tcx>,
318312

319313
let drop_flag_dtor_needed = ICmp(bcx, llvm::IntEQ, loaded, init_val, DebugLoc::None);
320314
with_cond(bcx, drop_flag_dtor_needed, |cx| {
321-
trans_struct_drop(cx, t, struct_data, dtor_did, class_did, substs)
315+
trans_struct_drop(cx, t, struct_data)
322316
})
323317
}
324-
325-
pub fn get_res_dtor<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
326-
did: DefId,
327-
parent_id: DefId,
328-
substs: &Substs<'tcx>)
329-
-> ValueRef {
330-
let _icx = push_ctxt("trans_res_dtor");
331-
let did = inline::maybe_instantiate_inline(ccx, did);
332-
333-
if !substs.types.is_empty() {
334-
assert_eq!(did.krate, LOCAL_CRATE);
335-
336-
// Since we're in trans we don't care for any region parameters
337-
let substs = ccx.tcx().mk_substs(Substs::erased(substs.types.clone()));
338-
339-
let (val, _, _) = monomorphize::monomorphic_fn(ccx, did, substs, None);
340-
341-
val
342-
} else if did.is_local() {
343-
get_item_val(ccx, did.node)
344-
} else {
345-
let tcx = ccx.tcx();
346-
let name = csearch::get_symbol(&ccx.sess().cstore, did);
347-
let class_ty = tcx.lookup_item_type(parent_id).ty.subst(tcx, substs);
348-
let llty = type_of_dtor(ccx, class_ty);
349-
foreign::get_extern_fn(ccx, &mut *ccx.externs().borrow_mut(), &name[..], llvm::CCallConv,
350-
llty, ccx.tcx().mk_nil())
351-
}
352-
}
353-
354318
fn trans_struct_drop<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
355319
t: Ty<'tcx>,
356-
v0: ValueRef,
357-
dtor_did: DefId,
358-
class_did: DefId,
359-
substs: &subst::Substs<'tcx>)
320+
v0: ValueRef)
360321
-> Block<'blk, 'tcx>
361322
{
362323
debug!("trans_struct_drop t: {}", t);
324+
let tcx = bcx.tcx();
325+
let mut bcx = bcx;
363326

364-
// Find and call the actual destructor
365-
let dtor_addr = get_res_dtor(bcx.ccx(), dtor_did, class_did, substs);
366-
367-
// Class dtors have no explicit args, so the params should
368-
// just consist of the environment (self).
369-
let params = unsafe {
370-
let ty = Type::from_ref(llvm::LLVMTypeOf(dtor_addr));
371-
ty.element_type().func_params()
372-
};
373-
assert_eq!(params.len(), if type_is_sized(bcx.tcx(), t) { 1 } else { 2 });
327+
let def = t.ty_adt_def().unwrap();
374328

375329
// Be sure to put the contents into a scope so we can use an invoke
376330
// instruction to call the user destructor but still call the field
@@ -384,15 +338,37 @@ fn trans_struct_drop<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
384338
// discriminant (if any) in case of variant swap in drop code.
385339
bcx.fcx.schedule_drop_adt_contents(cleanup::CustomScope(contents_scope), v0, t);
386340

387-
let glue_type = get_drop_glue_type(bcx.ccx(), t);
388-
let dtor_ty = bcx.tcx().mk_ctor_fn(class_did, &[glue_type], bcx.tcx().mk_nil());
389-
let (_, bcx) = if type_is_sized(bcx.tcx(), t) {
390-
invoke(bcx, dtor_addr, &[v0], dtor_ty, DebugLoc::None)
341+
let (sized_args, unsized_args);
342+
let args: &[ValueRef] = if type_is_sized(tcx, t) {
343+
sized_args = [v0];
344+
&sized_args
391345
} else {
392-
let args = [Load(bcx, expr::get_dataptr(bcx, v0)), Load(bcx, expr::get_meta(bcx, v0))];
393-
invoke(bcx, dtor_addr, &args, dtor_ty, DebugLoc::None)
346+
unsized_args = [Load(bcx, expr::get_dataptr(bcx, v0)), Load(bcx, expr::get_meta(bcx, v0))];
347+
&unsized_args
394348
};
395349

350+
bcx = callee::trans_call_inner(bcx, DebugLoc::None, |bcx, _| {
351+
let trait_ref = ty::Binder(ty::TraitRef {
352+
def_id: tcx.lang_items.drop_trait().unwrap(),
353+
substs: tcx.mk_substs(Substs::trans_empty().with_self_ty(t))
354+
});
355+
let vtbl = match fulfill_obligation(bcx.ccx(), DUMMY_SP, trait_ref) {
356+
traits::VtableImpl(data) => data,
357+
_ => tcx.sess.bug(&format!("dtor for {:?} is not an impl???", t))
358+
};
359+
let dtor_did = tcx.destructor_for_type.borrow()[&def.did];
360+
let datum = callee::trans_fn_ref_with_substs(bcx.ccx(),
361+
dtor_did,
362+
ExprId(0),
363+
bcx.fcx.param_substs,
364+
vtbl.substs);
365+
callee::Callee {
366+
bcx: bcx,
367+
data: callee::Fn(datum.val),
368+
ty: datum.ty
369+
}
370+
}, callee::ArgVals(args), Some(expr::Ignore)).bcx;
371+
396372
bcx.fcx.pop_and_trans_custom_cleanup_scope(bcx, contents_scope)
397373
}
398374

@@ -557,27 +533,27 @@ fn make_drop_glue<'blk, 'tcx>(bcx: Block<'blk, 'tcx>, v0: ValueRef, g: DropGlueK
557533
})
558534
}
559535
}
560-
ty::TyStruct(def, substs) | ty::TyEnum(def, substs) => {
536+
ty::TyStruct(def, _) | ty::TyEnum(def, _) => {
561537
let tcx = bcx.tcx();
562538
match (tcx.ty_dtor(def.did), skip_dtor) {
563-
(ty::TraitDtor(dtor, true), false) => {
539+
(ty::TraitDtor(_, true), false) => {
564540
// FIXME(16758) Since the struct is unsized, it is hard to
565541
// find the drop flag (which is at the end of the struct).
566542
// Lets just ignore the flag and pretend everything will be
567543
// OK.
568544
if type_is_sized(bcx.tcx(), t) {
569-
trans_struct_drop_flag(bcx, t, v0, dtor, def.did, substs)
545+
trans_struct_drop_flag(bcx, t, v0)
570546
} else {
571547
// Give the user a heads up that we are doing something
572548
// stupid and dangerous.
573549
bcx.sess().warn(&format!("Ignoring drop flag in destructor for {}\
574550
because the struct is unsized. See issue\
575551
#16758", t));
576-
trans_struct_drop(bcx, t, v0, dtor, def.did, substs)
552+
trans_struct_drop(bcx, t, v0)
577553
}
578554
}
579-
(ty::TraitDtor(dtor, false), false) => {
580-
trans_struct_drop(bcx, t, v0, dtor, def.did, substs)
555+
(ty::TraitDtor(_, false), false) => {
556+
trans_struct_drop(bcx, t, v0)
581557
}
582558
(ty::NoDtor, _) | (_, true) => {
583559
// No dtor? Just the default case

src/librustc_trans/trans/type_of.rs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -487,11 +487,3 @@ fn llvm_type_name<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>,
487487
format!("{}.{}", did.krate, tstr)
488488
}
489489
}
490-
491-
pub fn type_of_dtor<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, self_ty: Ty<'tcx>) -> Type {
492-
if type_is_sized(ccx.tcx(), self_ty) {
493-
Type::func(&[type_of(ccx, self_ty).ptr_to()], &Type::void(ccx))
494-
} else {
495-
Type::func(&type_of(ccx, self_ty).field_types(), &Type::void(ccx))
496-
}
497-
}

src/test/run-pass/issue-27997.rs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Copyright 2015 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+
#![feature(const_fn)]
12+
13+
use std::sync::atomic::{Ordering, AtomicUsize};
14+
15+
use std::mem;
16+
struct S<U,V> {
17+
_u: U,
18+
size_of_u: usize,
19+
_v: V,
20+
size_of_v: usize
21+
}
22+
23+
impl<U, V> S<U, V> {
24+
fn new(u: U, v: V) -> Self {
25+
S {
26+
_u: u,
27+
size_of_u: mem::size_of::<U>(),
28+
_v: v,
29+
size_of_v: mem::size_of::<V>()
30+
}
31+
}
32+
}
33+
34+
static COUNT: AtomicUsize = AtomicUsize::new(0);
35+
36+
impl<V, U> Drop for S<U, V> {
37+
fn drop(&mut self) {
38+
assert_eq!(mem::size_of::<U>(), self.size_of_u);
39+
assert_eq!(mem::size_of::<V>(), self.size_of_v);
40+
COUNT.store(COUNT.load(Ordering::SeqCst)+1, Ordering::SeqCst);
41+
}
42+
}
43+
44+
fn main() {
45+
assert_eq!(COUNT.load(Ordering::SeqCst), 0);
46+
{ S::new(0u8, 1u16); }
47+
assert_eq!(COUNT.load(Ordering::SeqCst), 1);
48+
}

0 commit comments

Comments
 (0)