Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit c366756

Browse files
committed
AsyncDrop implementation using shim codegen of async_drop_in_place::{closure}, scoped async drop added.
1 parent 52c1838 commit c366756

File tree

116 files changed

+4053
-1878
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

116 files changed

+4053
-1878
lines changed

compiler/rustc_borrowck/src/lib.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -795,7 +795,14 @@ impl<'a, 'tcx> ResultsVisitor<'tcx, Borrowck<'a, 'tcx>> for MirBorrowckCtxt<'a,
795795
TerminatorKind::SwitchInt { discr, targets: _ } => {
796796
self.consume_operand(loc, (discr, span), state);
797797
}
798-
TerminatorKind::Drop { place, target: _, unwind: _, replace } => {
798+
TerminatorKind::Drop {
799+
place,
800+
target: _,
801+
unwind: _,
802+
replace,
803+
drop: _,
804+
async_fut: _,
805+
} => {
799806
debug!(
800807
"visit_terminator_drop \
801808
loc: {:?} term: {:?} place: {:?} span: {:?}",

compiler/rustc_borrowck/src/polonius/legacy/loan_invalidations.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,14 @@ impl<'a, 'tcx> Visitor<'tcx> for LoanInvalidationsGenerator<'a, 'tcx> {
101101
TerminatorKind::SwitchInt { discr, targets: _ } => {
102102
self.consume_operand(location, discr);
103103
}
104-
TerminatorKind::Drop { place: drop_place, target: _, unwind: _, replace } => {
104+
TerminatorKind::Drop {
105+
place: drop_place,
106+
target: _,
107+
unwind: _,
108+
replace,
109+
drop: _,
110+
async_fut: _,
111+
} => {
105112
let write_kind =
106113
if *replace { WriteKind::Replace } else { WriteKind::StorageDeadOrDrop };
107114
self.access_place(

compiler/rustc_borrowck/src/type_check/mod.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2079,8 +2079,14 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
20792079
}
20802080
}
20812081
TerminatorKind::Unreachable => {}
2082-
TerminatorKind::Drop { target, unwind, .. }
2083-
| TerminatorKind::Assert { target, unwind, .. } => {
2082+
TerminatorKind::Drop { target, unwind, drop, .. } => {
2083+
self.assert_iscleanup(block_data, target, is_cleanup);
2084+
self.assert_iscleanup_unwind(block_data, unwind, is_cleanup);
2085+
if let Some(drop) = drop {
2086+
self.assert_iscleanup(block_data, drop, is_cleanup);
2087+
}
2088+
}
2089+
TerminatorKind::Assert { target, unwind, .. } => {
20842090
self.assert_iscleanup(block_data, target, is_cleanup);
20852091
self.assert_iscleanup_unwind(block_data, unwind, is_cleanup);
20862092
}

compiler/rustc_codegen_cranelift/src/abi/mod.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -441,7 +441,9 @@ pub(crate) fn codegen_terminator_call<'tcx>(
441441
Err(instance) => Some(instance),
442442
}
443443
}
444-
InstanceKind::DropGlue(_, None) | ty::InstanceKind::AsyncDropGlueCtorShim(_, None) => {
444+
// We don't need AsyncDropGlueCtorShim here because it is not `noop func`,
445+
// it is `func returning noop future`
446+
InstanceKind::DropGlue(_, None) => {
445447
// empty drop glue - a nop.
446448
let dest = target.expect("Non terminating drop_in_place_real???");
447449
let ret_block = fx.get_block(dest);
@@ -707,9 +709,8 @@ pub(crate) fn codegen_drop<'tcx>(
707709
let ty = drop_place.layout().ty;
708710
let drop_instance = Instance::resolve_drop_in_place(fx.tcx, ty);
709711

710-
if let ty::InstanceKind::DropGlue(_, None) | ty::InstanceKind::AsyncDropGlueCtorShim(_, None) =
711-
drop_instance.def
712-
{
712+
// AsyncDropGlueCtorShim can't be here
713+
if let ty::InstanceKind::DropGlue(_, None) = drop_instance.def {
713714
// we don't actually need to drop anything
714715
} else {
715716
match ty.kind() {

compiler/rustc_codegen_cranelift/src/base.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -565,7 +565,11 @@ fn codegen_fn_body(fx: &mut FunctionCx<'_, '_, '_>, start_block: Block) {
565565
| TerminatorKind::CoroutineDrop => {
566566
bug!("shouldn't exist at codegen {:?}", bb_data.terminator());
567567
}
568-
TerminatorKind::Drop { place, target, unwind: _, replace: _ } => {
568+
TerminatorKind::Drop { place, target, unwind: _, replace: _, drop, async_fut } => {
569+
assert!(
570+
async_fut.is_none() && drop.is_none(),
571+
"Async Drop must be expanded or reset to sync before codegen"
572+
);
569573
let drop_place = codegen_place(fx, *place);
570574
crate::abi::codegen_drop(fx, source_info, drop_place, *target);
571575
}

compiler/rustc_codegen_llvm/src/debuginfo/metadata/enums/cpp_like.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -721,8 +721,7 @@ fn build_union_fields_for_direct_tag_coroutine<'ll, 'tcx>(
721721
_ => unreachable!(),
722722
};
723723

724-
let coroutine_layout =
725-
cx.tcx.coroutine_layout(coroutine_def_id, coroutine_args.kind_ty()).unwrap();
724+
let coroutine_layout = cx.tcx.coroutine_layout(coroutine_def_id, coroutine_args.args).unwrap();
726725

727726
let common_upvar_names = cx.tcx.closure_saved_names_of_captured_variables(coroutine_def_id);
728727
let variant_range = coroutine_args.variant_range(coroutine_def_id, cx.tcx);

compiler/rustc_codegen_llvm/src/debuginfo/metadata/enums/native.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -174,10 +174,8 @@ pub(super) fn build_coroutine_di_node<'ll, 'tcx>(
174174
DIFlags::FlagZero,
175175
),
176176
|cx, coroutine_type_di_node| {
177-
let coroutine_layout = cx
178-
.tcx
179-
.coroutine_layout(coroutine_def_id, coroutine_args.as_coroutine().kind_ty())
180-
.unwrap();
177+
let coroutine_layout =
178+
cx.tcx.coroutine_layout(coroutine_def_id, coroutine_args).unwrap();
181179

182180
let Variants::Multiple { tag_encoding: TagEncoding::Direct, ref variants, .. } =
183181
coroutine_type_and_layout.variants

compiler/rustc_codegen_ssa/src/back/symbol_export.rs

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ fn exported_symbols_provider_local(
374374
));
375375
}
376376
MonoItem::Fn(Instance {
377-
def: InstanceKind::AsyncDropGlueCtorShim(_, Some(ty)),
377+
def: InstanceKind::AsyncDropGlueCtorShim(_, ty),
378378
args,
379379
}) => {
380380
// A little sanity-check
@@ -388,6 +388,16 @@ fn exported_symbols_provider_local(
388388
},
389389
));
390390
}
391+
MonoItem::Fn(Instance { def: InstanceKind::AsyncDropGlue(def, ty), args: _ }) => {
392+
symbols.push((
393+
ExportedSymbol::AsyncDropGlue(def, ty),
394+
SymbolExportInfo {
395+
level: SymbolExportLevel::Rust,
396+
kind: SymbolExportKind::Text,
397+
used: false,
398+
},
399+
));
400+
}
391401
_ => {
392402
// Any other symbols don't qualify for sharing
393403
}
@@ -429,11 +439,10 @@ fn upstream_monomorphizations_provider(
429439
if let Some(async_drop_in_place_fn_def_id) = async_drop_in_place_fn_def_id {
430440
(async_drop_in_place_fn_def_id, tcx.mk_args(&[ty.into()]))
431441
} else {
432-
// `drop_in_place` in place does not exist, don't try
433-
// to use it.
434442
continue;
435443
}
436444
}
445+
ExportedSymbol::AsyncDropGlue(def_id, ty) => (def_id, tcx.mk_args(&[ty.into()])),
437446
ExportedSymbol::NonGeneric(..)
438447
| ExportedSymbol::ThreadLocalShim(..)
439448
| ExportedSymbol::NoDefId(..) => {
@@ -582,6 +591,13 @@ pub(crate) fn symbol_name_for_instance_in_crate<'tcx>(
582591
instantiating_crate,
583592
)
584593
}
594+
ExportedSymbol::AsyncDropGlue(def_id, ty) => {
595+
rustc_symbol_mangling::symbol_name_for_instance_in_crate(
596+
tcx,
597+
Instance::resolve_async_drop_in_place_poll(tcx, def_id, ty),
598+
instantiating_crate,
599+
)
600+
}
585601
ExportedSymbol::NoDefId(symbol_name) => symbol_name.to_string(),
586602
}
587603
}
@@ -604,6 +620,7 @@ fn calling_convention_for_symbol<'tcx>(
604620
// AsyncDropGlueCtorShim always use the Rust calling convention and thus follow the
605621
// target's default symbol decoration scheme.
606622
ExportedSymbol::AsyncDropGlueCtorShim(..) => None,
623+
ExportedSymbol::AsyncDropGlue(..) => None,
607624
// NoDefId always follow the target's default symbol decoration scheme.
608625
ExportedSymbol::NoDefId(..) => None,
609626
// ThreadLocalShim always follow the target's default symbol decoration scheme.

compiler/rustc_codegen_ssa/src/mir/block.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -926,10 +926,9 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
926926

927927
let def = instance.map(|i| i.def);
928928

929-
if let Some(
930-
ty::InstanceKind::DropGlue(_, None) | ty::InstanceKind::AsyncDropGlueCtorShim(_, None),
931-
) = def
932-
{
929+
// We don't need AsyncDropGlueCtorShim here because it is not `noop func`,
930+
// it is `func returning noop future`
931+
if let Some(ty::InstanceKind::DropGlue(_, None)) = def {
933932
// Empty drop glue; a no-op.
934933
let target = target.unwrap();
935934
return helper.funclet_br(self, bx, target, mergeable_succ);
@@ -1386,16 +1385,21 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
13861385
MergingSucc::False
13871386
}
13881387

1389-
mir::TerminatorKind::Drop { place, target, unwind, replace: _ } => self
1390-
.codegen_drop_terminator(
1388+
mir::TerminatorKind::Drop { place, target, unwind, replace: _, drop, async_fut } => {
1389+
assert!(
1390+
async_fut.is_none() && drop.is_none(),
1391+
"Async Drop must be expanded or reset to sync before codegen"
1392+
);
1393+
self.codegen_drop_terminator(
13911394
helper,
13921395
bx,
13931396
&terminator.source_info,
13941397
place,
13951398
target,
13961399
unwind,
13971400
mergeable_succ(),
1398-
),
1401+
)
1402+
}
13991403

14001404
mir::TerminatorKind::Assert { ref cond, expected, ref msg, target, unwind } => self
14011405
.codegen_assert_terminator(

compiler/rustc_const_eval/src/const_eval/machine.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -502,6 +502,7 @@ impl<'tcx> interpret::Machine<'tcx> for CompileTimeMachine<'tcx> {
502502
RemainderByZero(op) => RemainderByZero(eval_to_int(op)?),
503503
ResumedAfterReturn(coroutine_kind) => ResumedAfterReturn(*coroutine_kind),
504504
ResumedAfterPanic(coroutine_kind) => ResumedAfterPanic(*coroutine_kind),
505+
ResumedAfterDrop(coroutine_kind) => ResumedAfterDrop(*coroutine_kind),
505506
MisalignedPointerDereference { required, found } => MisalignedPointerDereference {
506507
required: eval_to_int(required)?,
507508
found: eval_to_int(found)?,

0 commit comments

Comments
 (0)