Skip to content

Commit c61f229

Browse files
Let the translation item collector make a distinction between drop-glue kinds
1 parent ea6b3dd commit c61f229

File tree

9 files changed

+55
-32
lines changed

9 files changed

+55
-32
lines changed

src/librustc_trans/collector.rs

+32-17
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ use base::{custom_coerce_unsize_info, llvm_linkage_by_name};
211211
use context::CrateContext;
212212
use common::{fulfill_obligation, normalize_and_test_predicates,
213213
type_is_sized};
214-
use glue;
214+
use glue::{self, DropGlueKind};
215215
use llvm;
216216
use meth;
217217
use monomorphize::{self, Instance};
@@ -227,7 +227,7 @@ pub enum TransItemCollectionMode {
227227

228228
#[derive(PartialEq, Eq, Clone, Copy, Debug)]
229229
pub enum TransItem<'tcx> {
230-
DropGlue(Ty<'tcx>),
230+
DropGlue(DropGlueKind<'tcx>),
231231
Fn(Instance<'tcx>),
232232
Static(NodeId)
233233
}
@@ -326,7 +326,7 @@ fn collect_items_rec<'a, 'tcx: 'a>(ccx: &CrateContext<'a, 'tcx>,
326326
let def_id = ccx.tcx().map.local_def_id(node_id);
327327
let ty = ccx.tcx().lookup_item_type(def_id).ty;
328328
let ty = glue::get_drop_glue_type(ccx, ty);
329-
neighbors.push(TransItem::DropGlue(ty));
329+
neighbors.push(TransItem::DropGlue(DropGlueKind::Ty(ty)));
330330
recursion_depth_reset = None;
331331
}
332332
TransItem::Fn(instance) => {
@@ -485,7 +485,7 @@ impl<'a, 'tcx> MirVisitor<'tcx> for MirNeighborCollector<'a, 'tcx> {
485485
&ty);
486486
let ty = self.ccx.tcx().erase_regions(&ty);
487487
let ty = glue::get_drop_glue_type(self.ccx, ty);
488-
self.output.push(TransItem::DropGlue(ty));
488+
self.output.push(TransItem::DropGlue(DropGlueKind::Ty(ty)));
489489
}
490490

491491
self.super_lvalue(lvalue, context);
@@ -575,9 +575,17 @@ fn can_have_local_instance<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
575575
}
576576

577577
fn find_drop_glue_neighbors<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
578-
ty: ty::Ty<'tcx>,
579-
output: &mut Vec<TransItem<'tcx>>)
580-
{
578+
dg: DropGlueKind<'tcx>,
579+
output: &mut Vec<TransItem<'tcx>>) {
580+
let ty = match dg {
581+
DropGlueKind::Ty(ty) => ty,
582+
DropGlueKind::TyContents(_) => {
583+
// We already collected the neighbors of this item via the
584+
// DropGlueKind::Ty variant.
585+
return
586+
}
587+
};
588+
581589
debug!("find_drop_glue_neighbors: {}", type_to_string(ccx, ty));
582590

583591
// Make sure the exchange_free_fn() lang-item gets translated if
@@ -634,6 +642,10 @@ fn find_drop_glue_neighbors<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
634642
&Substs::empty());
635643
output.push(trans_item);
636644
}
645+
646+
// This type has a Drop implementation, we'll need the contents-only
647+
// version of the glue too.
648+
output.push(TransItem::DropGlue(DropGlueKind::TyContents(ty)));
637649
}
638650

639651
// Finally add the types of nested values
@@ -661,30 +673,30 @@ fn find_drop_glue_neighbors<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
661673
let field_type = glue::get_drop_glue_type(ccx, field_type);
662674

663675
if glue::type_needs_drop(ccx.tcx(), field_type) {
664-
output.push(TransItem::DropGlue(field_type));
676+
output.push(TransItem::DropGlue(DropGlueKind::Ty(field_type)));
665677
}
666678
}
667679
}
668680
ty::TyClosure(_, ref substs) => {
669681
for upvar_ty in &substs.upvar_tys {
670682
let upvar_ty = glue::get_drop_glue_type(ccx, upvar_ty);
671683
if glue::type_needs_drop(ccx.tcx(), upvar_ty) {
672-
output.push(TransItem::DropGlue(upvar_ty));
684+
output.push(TransItem::DropGlue(DropGlueKind::Ty(upvar_ty)));
673685
}
674686
}
675687
}
676688
ty::TyBox(inner_type) |
677689
ty::TyArray(inner_type, _) => {
678690
let inner_type = glue::get_drop_glue_type(ccx, inner_type);
679691
if glue::type_needs_drop(ccx.tcx(), inner_type) {
680-
output.push(TransItem::DropGlue(inner_type));
692+
output.push(TransItem::DropGlue(DropGlueKind::Ty(inner_type)));
681693
}
682694
}
683695
ty::TyTuple(ref args) => {
684696
for arg in args {
685697
let arg = glue::get_drop_glue_type(ccx, arg);
686698
if glue::type_needs_drop(ccx.tcx(), arg) {
687-
output.push(TransItem::DropGlue(arg));
699+
output.push(TransItem::DropGlue(DropGlueKind::Ty(arg)));
688700
}
689701
}
690702
}
@@ -1000,7 +1012,7 @@ impl<'b, 'a, 'v> hir_visit::Visitor<'v> for RootCollector<'b, 'a, 'v> {
10001012
self.ccx.tcx().map.local_def_id(item.id)));
10011013

10021014
let ty = glue::get_drop_glue_type(self.ccx, ty);
1003-
self.output.push(TransItem::DropGlue(ty));
1015+
self.output.push(TransItem::DropGlue(DropGlueKind::Ty(ty)));
10041016
}
10051017
}
10061018
}
@@ -1413,10 +1425,13 @@ impl<'tcx> TransItem<'tcx> {
14131425
let hir_map = &ccx.tcx().map;
14141426

14151427
return match *self {
1416-
TransItem::DropGlue(t) => {
1428+
TransItem::DropGlue(dg) => {
14171429
let mut s = String::with_capacity(32);
1418-
s.push_str("drop-glue ");
1419-
push_unique_type_name(ccx, t, &mut s);
1430+
match dg {
1431+
DropGlueKind::Ty(_) => s.push_str("drop-glue "),
1432+
DropGlueKind::TyContents(_) => s.push_str("drop-glue-contents "),
1433+
};
1434+
push_unique_type_name(ccx, dg.ty(), &mut s);
14201435
s
14211436
}
14221437
TransItem::Fn(instance) => {
@@ -1442,8 +1457,8 @@ impl<'tcx> TransItem<'tcx> {
14421457

14431458
fn to_raw_string(&self) -> String {
14441459
match *self {
1445-
TransItem::DropGlue(t) => {
1446-
format!("DropGlue({})", t as *const _ as usize)
1460+
TransItem::DropGlue(dg) => {
1461+
format!("DropGlue({})", dg.ty() as *const _ as usize)
14471462
}
14481463
TransItem::Fn(instance) => {
14491464
format!("Fn({:?}, {})",

src/librustc_trans/glue.rs

+8-9
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ pub fn get_drop_glue_type<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
9898
// Even if there is no dtor for t, there might be one deeper down and we
9999
// might need to pass in the vtable ptr.
100100
if !type_is_sized(tcx, t) {
101-
return t
101+
return ccx.tcx().erase_regions(&t);
102102
}
103103

104104
// FIXME (#22815): note that type_needs_drop conservatively
@@ -121,10 +121,10 @@ pub fn get_drop_glue_type<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
121121
if llsize_of_alloc(ccx, llty) == 0 {
122122
tcx.types.i8
123123
} else {
124-
t
124+
ccx.tcx().erase_regions(&t)
125125
}
126126
}
127-
_ => t
127+
_ => ccx.tcx().erase_regions(&t)
128128
}
129129
}
130130

@@ -215,11 +215,11 @@ pub enum DropGlueKind<'tcx> {
215215
}
216216

217217
impl<'tcx> DropGlueKind<'tcx> {
218-
fn ty(&self) -> Ty<'tcx> {
218+
pub fn ty(&self) -> Ty<'tcx> {
219219
match *self { DropGlueKind::Ty(t) | DropGlueKind::TyContents(t) => t }
220220
}
221221

222-
fn map_ty<F>(&self, mut f: F) -> DropGlueKind<'tcx> where F: FnMut(Ty<'tcx>) -> Ty<'tcx>
222+
pub fn map_ty<F>(&self, mut f: F) -> DropGlueKind<'tcx> where F: FnMut(Ty<'tcx>) -> Ty<'tcx>
223223
{
224224
match *self {
225225
DropGlueKind::Ty(t) => DropGlueKind::Ty(f(t)),
@@ -487,14 +487,13 @@ pub fn size_and_align_of_dst<'blk, 'tcx>(bcx: &BlockAndBuilder<'blk, 'tcx>,
487487

488488
fn make_drop_glue<'blk, 'tcx>(bcx: Block<'blk, 'tcx>, v0: ValueRef, g: DropGlueKind<'tcx>)
489489
-> Block<'blk, 'tcx> {
490-
let t = g.ty();
491-
492490
if collector::collecting_debug_information(bcx.ccx()) {
493491
bcx.ccx()
494-
.record_translation_item_as_generated(TransItem::DropGlue(bcx.tcx()
495-
.erase_regions(&t)));
492+
.record_translation_item_as_generated(TransItem::DropGlue(g));
496493
}
497494

495+
let t = g.ty();
496+
498497
let skip_dtor = match g { DropGlueKind::Ty(_) => false, DropGlueKind::TyContents(_) => true };
499498
// NB: v0 is an *alias* of type t here, not a direct value.
500499
let _icx = push_ctxt("make_drop_glue");

src/librustc_trans/partitioning.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ fn characteristic_def_id_of_trans_item<'tcx>(tcx: &TyCtxt<'tcx>,
304304

305305
Some(instance.def)
306306
}
307-
TransItem::DropGlue(t) => characteristic_def_id_of_type(t),
307+
TransItem::DropGlue(dg) => characteristic_def_id_of_type(dg.ty()),
308308
TransItem::Static(node_id) => Some(tcx.map.local_def_id(node_id)),
309309
}
310310
}

src/test/codegen-units/item-collection/generic-drop-glue.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -46,19 +46,22 @@ struct NonGenericNoDrop(i32);
4646

4747
struct NonGenericWithDrop(i32);
4848
//~ TRANS_ITEM drop-glue generic_drop_glue::NonGenericWithDrop[0]
49+
//~ TRANS_ITEM drop-glue-contents generic_drop_glue::NonGenericWithDrop[0]
4950

5051
impl Drop for NonGenericWithDrop {
52+
//~ TRANS_ITEM fn generic_drop_glue::{{impl}}[2]::drop[0]
5153
fn drop(&mut self) {}
52-
//~ TRANS_ITEM fn generic_drop_glue::{{impl}}[2]::drop[0]
5354
}
5455

5556
//~ TRANS_ITEM fn generic_drop_glue::main[0]
5657
fn main() {
5758
//~ TRANS_ITEM drop-glue generic_drop_glue::StructWithDrop[0]<i8, char>
59+
//~ TRANS_ITEM drop-glue-contents generic_drop_glue::StructWithDrop[0]<i8, char>
5860
//~ TRANS_ITEM fn generic_drop_glue::{{impl}}[0]::drop[0]<i8, char>
5961
let _ = StructWithDrop { x: 0i8, y: 'a' }.x;
6062

6163
//~ TRANS_ITEM drop-glue generic_drop_glue::StructWithDrop[0]<&str, generic_drop_glue::NonGenericNoDrop[0]>
64+
//~ TRANS_ITEM drop-glue-contents generic_drop_glue::StructWithDrop[0]<&str, generic_drop_glue::NonGenericNoDrop[0]>
6265
//~ TRANS_ITEM fn generic_drop_glue::{{impl}}[0]::drop[0]<&str, generic_drop_glue::NonGenericNoDrop[0]>
6366
let _ = StructWithDrop { x: "&str", y: NonGenericNoDrop(0) }.y;
6467

@@ -71,13 +74,15 @@ fn main() {
7174
let _ = StructNoDrop { x: NonGenericWithDrop(0), y: 0f64 }.y;
7275

7376
//~ TRANS_ITEM drop-glue generic_drop_glue::EnumWithDrop[0]<i32, i64>
77+
//~ TRANS_ITEM drop-glue-contents generic_drop_glue::EnumWithDrop[0]<i32, i64>
7478
//~ TRANS_ITEM fn generic_drop_glue::{{impl}}[1]::drop[0]<i32, i64>
7579
let _ = match EnumWithDrop::A::<i32, i64>(0) {
7680
EnumWithDrop::A(x) => x,
7781
EnumWithDrop::B(x) => x as i32
7882
};
7983

8084
//~ TRANS_ITEM drop-glue generic_drop_glue::EnumWithDrop[0]<f64, f32>
85+
//~ TRANS_ITEM drop-glue-contents generic_drop_glue::EnumWithDrop[0]<f64, f32>
8186
//~ TRANS_ITEM fn generic_drop_glue::{{impl}}[1]::drop[0]<f64, f32>
8287
let _ = match EnumWithDrop::B::<f64, f32>(1.0) {
8388
EnumWithDrop::A(x) => x,

src/test/codegen-units/item-collection/non-generic-drop-glue.rs

+2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#![deny(dead_code)]
1515

1616
//~ TRANS_ITEM drop-glue non_generic_drop_glue::StructWithDrop[0]
17+
//~ TRANS_ITEM drop-glue-contents non_generic_drop_glue::StructWithDrop[0]
1718
struct StructWithDrop {
1819
x: i32
1920
}
@@ -28,6 +29,7 @@ struct StructNoDrop {
2829
}
2930

3031
//~ TRANS_ITEM drop-glue non_generic_drop_glue::EnumWithDrop[0]
32+
//~ TRANS_ITEM drop-glue-contents non_generic_drop_glue::EnumWithDrop[0]
3133
enum EnumWithDrop {
3234
A(i32)
3335
}

src/test/codegen-units/item-collection/transitive-drop-glue.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,16 @@ struct Root(Intermediate);
1818
//~ TRANS_ITEM drop-glue transitive_drop_glue::Intermediate[0]
1919
struct Intermediate(Leaf);
2020
//~ TRANS_ITEM drop-glue transitive_drop_glue::Leaf[0]
21+
//~ TRANS_ITEM drop-glue-contents transitive_drop_glue::Leaf[0]
2122
struct Leaf;
2223

2324
impl Drop for Leaf {
2425
//~ TRANS_ITEM fn transitive_drop_glue::{{impl}}[0]::drop[0]
2526
fn drop(&mut self) {}
2627
}
2728

28-
//~ TRANS_ITEM drop-glue transitive_drop_glue::Root[0]
2929
struct RootGen<T>(IntermediateGen<T>);
30-
//~ TRANS_ITEM drop-glue transitive_drop_glue::Root[0]
3130
struct IntermediateGen<T>(LeafGen<T>);
32-
//~ TRANS_ITEM drop-glue transitive_drop_glue::Root[0]
3331
struct LeafGen<T>(T);
3432

3533
impl<T> Drop for LeafGen<T> {
@@ -44,12 +42,14 @@ fn main() {
4442
//~ TRANS_ITEM drop-glue transitive_drop_glue::RootGen[0]<u32>
4543
//~ TRANS_ITEM drop-glue transitive_drop_glue::IntermediateGen[0]<u32>
4644
//~ TRANS_ITEM drop-glue transitive_drop_glue::LeafGen[0]<u32>
45+
//~ TRANS_ITEM drop-glue-contents transitive_drop_glue::LeafGen[0]<u32>
4746
//~ TRANS_ITEM fn transitive_drop_glue::{{impl}}[1]::drop[0]<u32>
4847
let _ = RootGen(IntermediateGen(LeafGen(0u32)));
4948

5049
//~ TRANS_ITEM drop-glue transitive_drop_glue::RootGen[0]<i16>
5150
//~ TRANS_ITEM drop-glue transitive_drop_glue::IntermediateGen[0]<i16>
5251
//~ TRANS_ITEM drop-glue transitive_drop_glue::LeafGen[0]<i16>
52+
//~ TRANS_ITEM drop-glue-contents transitive_drop_glue::LeafGen[0]<i16>
5353
//~ TRANS_ITEM fn transitive_drop_glue::{{impl}}[1]::drop[0]<i16>
5454
let _ = RootGen(IntermediateGen(LeafGen(0i16)));
5555
}

src/test/codegen-units/item-collection/tuple-drop-glue.rs

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#![deny(dead_code)]
1515

1616
//~ TRANS_ITEM drop-glue tuple_drop_glue::Dropped[0]
17+
//~ TRANS_ITEM drop-glue-contents tuple_drop_glue::Dropped[0]
1718
struct Dropped;
1819

1920
impl Drop for Dropped {

src/test/codegen-units/partitioning/extern-drop-glue.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
extern crate cgu_extern_drop_glue;
1919

2020
//~ TRANS_ITEM drop-glue cgu_extern_drop_glue::Struct[0] @@ extern_drop_glue[OnceODR] extern_drop_glue-mod1[OnceODR]
21+
//~ TRANS_ITEM drop-glue-contents cgu_extern_drop_glue::Struct[0] @@ extern_drop_glue[OnceODR] extern_drop_glue-mod1[OnceODR]
2122

2223
struct LocalStruct(cgu_extern_drop_glue::Struct);
2324

@@ -40,4 +41,3 @@ mod mod1 {
4041
let _ = LocalStruct(cgu_extern_drop_glue::Struct(0));
4142
}
4243
}
43-

src/test/codegen-units/partitioning/local-drop-glue.rs

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#![crate_type="lib"]
1616

1717
//~ TRANS_ITEM drop-glue local_drop_glue::Struct[0] @@ local_drop_glue[OnceODR] local_drop_glue-mod1[OnceODR]
18+
//~ TRANS_ITEM drop-glue-contents local_drop_glue::Struct[0] @@ local_drop_glue[OnceODR] local_drop_glue-mod1[OnceODR]
1819
struct Struct {
1920
_a: u32
2021
}

0 commit comments

Comments
 (0)