Skip to content

Commit fbf691c

Browse files
committed
Fix async drop glue for Box<T>
1 parent cb46fbb commit fbf691c

3 files changed

Lines changed: 25 additions & 12 deletions

File tree

compiler/rustc_ty_utils/src/needs_drop.rs

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,11 @@ fn needs_drop_raw<'tcx>(
2323
// needs drop.
2424
let adt_has_dtor =
2525
|adt_def: ty::AdtDef<'tcx>| adt_def.destructor(tcx).map(|_| DtorType::Significant);
26-
let res = drop_tys_helper(tcx, query.value, query.typing_env, adt_has_dtor, false, false)
27-
.filter(filter_array_elements(tcx, query.typing_env))
28-
.next()
29-
.is_some();
26+
let res =
27+
drop_tys_helper(tcx, query.value, query.typing_env, adt_has_dtor, false, false, false)
28+
.filter(filter_array_elements(tcx, query.typing_env))
29+
.next()
30+
.is_some();
3031

3132
debug!("needs_drop_raw({:?}) = {:?}", query, res);
3233
res
@@ -41,10 +42,11 @@ fn needs_async_drop_raw<'tcx>(
4142
// it needs async drop.
4243
let adt_has_async_dtor =
4344
|adt_def: ty::AdtDef<'tcx>| adt_def.async_destructor(tcx).map(|_| DtorType::Significant);
44-
let res = drop_tys_helper(tcx, query.value, query.typing_env, adt_has_async_dtor, false, false)
45-
.filter(filter_array_elements_async(tcx, query.typing_env))
46-
.next()
47-
.is_some();
45+
let res =
46+
drop_tys_helper(tcx, query.value, query.typing_env, adt_has_async_dtor, false, false, true)
47+
.filter(filter_array_elements_async(tcx, query.typing_env))
48+
.next()
49+
.is_some();
4850

4951
debug!("needs_async_drop_raw({:?}) = {:?}", query, res);
5052
res
@@ -90,6 +92,7 @@ fn has_significant_drop_raw<'tcx>(
9092
adt_consider_insignificant_dtor(tcx),
9193
true,
9294
false,
95+
false,
9396
)
9497
.filter(filter_array_elements(tcx, query.typing_env))
9598
.next()
@@ -331,6 +334,7 @@ fn drop_tys_helper<'tcx>(
331334
adt_has_dtor: impl Fn(ty::AdtDef<'tcx>) -> Option<DtorType>,
332335
only_significant: bool,
333336
exhaustive: bool,
337+
async_drop_recurses_into_box: bool,
334338
) -> impl Iterator<Item = NeedsDropResult<Ty<'tcx>>> {
335339
fn with_query_cache<'tcx>(
336340
tcx: TyCtxt<'tcx>,
@@ -353,6 +357,14 @@ fn drop_tys_helper<'tcx>(
353357
if adt_def.is_manually_drop() {
354358
debug!("drop_tys_helper: `{:?}` is manually drop", adt_def);
355359
Ok(Vec::new())
360+
} else if async_drop_recurses_into_box && adt_def.is_box() {
361+
let boxed_ty = args.type_at(0);
362+
let allocator_ty = args.get(1).map(|arg| arg.expect_ty());
363+
let boxed_ty = match boxed_ty.kind() {
364+
ty::Dynamic(..) | ty::Error(_) => None,
365+
_ => Some(boxed_ty),
366+
};
367+
Ok([boxed_ty, allocator_ty].into_iter().flatten().collect())
356368
} else if let Some(dtor_info) = adt_has_dtor(adt_def) {
357369
match dtor_info {
358370
DtorType::Significant => {
@@ -435,6 +447,7 @@ fn adt_drop_tys<'tcx>(
435447
adt_has_dtor,
436448
false,
437449
false,
450+
false,
438451
)
439452
.collect::<Result<Vec<_>, _>>()
440453
.map(|components| tcx.mk_type_list(&components))
@@ -455,6 +468,7 @@ fn adt_async_drop_tys<'tcx>(
455468
adt_has_dtor,
456469
false,
457470
false,
471+
true,
458472
)
459473
.collect::<Result<Vec<_>, _>>()
460474
.map(|components| tcx.mk_type_list(&components))
@@ -474,6 +488,7 @@ fn adt_significant_drop_tys(
474488
adt_consider_insignificant_dtor(tcx),
475489
true,
476490
false,
491+
false,
477492
)
478493
.collect::<Result<Vec<_>, _>>()
479494
.map(|components| tcx.mk_type_list(&components))
@@ -492,6 +507,7 @@ fn list_significant_drop_tys<'tcx>(
492507
adt_consider_insignificant_dtor(tcx),
493508
true,
494509
true,
510+
false,
495511
)
496512
.filter_map(|res| res.ok())
497513
.collect::<Vec<_>>(),

tests/ui/async-await/async-drop/async-drop-box.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@
44
// `Foo` is always inside `Box`
55
// Sync version is called in sync context, async version is called in async function.
66

7-
//@ known-bug: #143658
8-
// async version is never actually called
9-
107
#![feature(async_drop)]
118
#![allow(incomplete_features)]
129

tests/ui/async-await/async-drop/async-drop-box.run.stdout

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@ Foo::new() : 7
22
Foo::drop() : 7
33
Middle
44
Foo::new() : 10
5-
Foo::drop() : 10
5+
Foo::async drop() : 10
66
Done

0 commit comments

Comments
 (0)