From 1eeaf2065b61cc99f9e5d7f3dd4b77a018ee619f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Steinbrink?= Date: Fri, 7 Aug 2015 18:29:44 +0200 Subject: [PATCH 1/2] Fix ICE when trying to drop an unsized type from a different crate The code to get the LLVM type signature for the drop function doesn't handle unsized types correctly. --- src/librustc_trans/trans/type_of.rs | 7 +++++-- src/test/auxiliary/fat_drop.rs | 23 +++++++++++++++++++++++ src/test/run-pass/extern_fat_drop.rs | 23 +++++++++++++++++++++++ 3 files changed, 51 insertions(+), 2 deletions(-) create mode 100644 src/test/auxiliary/fat_drop.rs create mode 100644 src/test/run-pass/extern_fat_drop.rs diff --git a/src/librustc_trans/trans/type_of.rs b/src/librustc_trans/trans/type_of.rs index c5161a8bc2e78..86c9723bab7f2 100644 --- a/src/librustc_trans/trans/type_of.rs +++ b/src/librustc_trans/trans/type_of.rs @@ -472,6 +472,9 @@ fn llvm_type_name<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>, } pub fn type_of_dtor<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, self_ty: Ty<'tcx>) -> Type { - let self_ty = type_of(ccx, self_ty).ptr_to(); - Type::func(&[self_ty], &Type::void(ccx)) + if type_is_sized(ccx.tcx(), self_ty) { + Type::func(&[type_of(ccx, self_ty).ptr_to()], &Type::void(ccx)) + } else { + Type::func(&type_of(ccx, self_ty).field_types(), &Type::void(ccx)) + } } diff --git a/src/test/auxiliary/fat_drop.rs b/src/test/auxiliary/fat_drop.rs new file mode 100644 index 0000000000000..1f944b6ed32f0 --- /dev/null +++ b/src/test/auxiliary/fat_drop.rs @@ -0,0 +1,23 @@ +// Copyright 2015 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +pub static mut DROPPED: bool = false; + +pub struct S { + _unsized: [u8] +} + +impl Drop for S { + fn drop(&mut self) { + unsafe { + DROPPED = true; + } + } +} diff --git a/src/test/run-pass/extern_fat_drop.rs b/src/test/run-pass/extern_fat_drop.rs new file mode 100644 index 0000000000000..f587dc7821df0 --- /dev/null +++ b/src/test/run-pass/extern_fat_drop.rs @@ -0,0 +1,23 @@ +// Copyright 2015 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +// aux-build:fat_drop.rs + +#![feature(core_intrinsics)] + +extern crate fat_drop; + +fn main() { + unsafe { + let s: &mut fat_drop::S = std::mem::uninitialized(); + std::intrinsics::drop_in_place(s); + assert!(fat_drop::DROPPED); + } +} From f804872502587290dcab42eda35301314173cbd7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Steinbrink?= Date: Sat, 8 Aug 2015 15:32:35 +0200 Subject: [PATCH 2/2] Fix the function return type used in get_res_dtor() Instead of the actual return type, we're currently passing the function type to get_extern_fn(). The only reason this doesn't explode is because get_extern_fn() actually doesn't care about the actual return type, just about it being converging or not. --- src/librustc_trans/trans/glue.rs | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/src/librustc_trans/trans/glue.rs b/src/librustc_trans/trans/glue.rs index 18fedda49193c..1254cf1c296fb 100644 --- a/src/librustc_trans/trans/glue.rs +++ b/src/librustc_trans/trans/glue.rs @@ -324,7 +324,6 @@ fn trans_struct_drop_flag<'blk, 'tcx>(mut bcx: Block<'blk, 'tcx>, pub fn get_res_dtor<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, did: ast::DefId, - t: Ty<'tcx>, parent_id: ast::DefId, substs: &Substs<'tcx>) -> ValueRef { @@ -347,11 +346,8 @@ pub fn get_res_dtor<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, let name = csearch::get_symbol(&ccx.sess().cstore, did); let class_ty = tcx.lookup_item_type(parent_id).ty.subst(tcx, substs); let llty = type_of_dtor(ccx, class_ty); - let dtor_ty = ccx.tcx().mk_ctor_fn(did, - &[get_drop_glue_type(ccx, t)], - ccx.tcx().mk_nil()); foreign::get_extern_fn(ccx, &mut *ccx.externs().borrow_mut(), &name[..], llvm::CCallConv, - llty, dtor_ty) + llty, ccx.tcx().mk_nil()) } } @@ -366,7 +362,7 @@ fn trans_struct_drop<'blk, 'tcx>(bcx: Block<'blk, 'tcx>, debug!("trans_struct_drop t: {}", t); // Find and call the actual destructor - let dtor_addr = get_res_dtor(bcx.ccx(), dtor_did, t, class_did, substs); + let dtor_addr = get_res_dtor(bcx.ccx(), dtor_did, class_did, substs); // Class dtors have no explicit args, so the params should // just consist of the environment (self).