@@ -63,6 +63,12 @@ static bool isScopeAffectingInstructionDead(SILInstruction *inst,
6363 return false ;
6464 }
6565
66+ // Don't delete dead drop_deinit instruction. They are a marker to eliminate
67+ // user-defined deinit and we do not want to lose it.
68+ if (isa<DropDeinitInst>(inst)) {
69+ return false ;
70+ }
71+
6672 for (auto result : inst->getResults ()) {
6773 // If inst has any owned move-only value as a result, deleting it may
6874 // shorten that value's lifetime which is illegal according to language
@@ -268,9 +274,16 @@ void InstructionDeleter::deleteWithUses(SILInstruction *inst, bool fixLifetimes,
268274 if (fixLifetimes) {
269275 LoadInst *li = nullptr ;
270276 if (operand.isConsuming ()) {
271- SILBuilderWithScope builder (inst);
272- auto *dvi = builder.createDestroyValue (inst->getLoc (), operandValue);
273- getCallbacks ().createdNewInst (dvi);
277+ if (isa<DropDeinitInst>(operandValue)) {
278+ SILBuilderWithScope builder (inst);
279+ auto *eli = builder.createEndLifetime (inst->getLoc (), operandValue);
280+ getCallbacks ().createdNewInst (eli);
281+ } else {
282+ SILBuilderWithScope builder (inst);
283+ auto *dvi =
284+ builder.createDestroyValue (inst->getLoc (), operandValue);
285+ getCallbacks ().createdNewInst (dvi);
286+ }
274287 } else if ((li = dyn_cast<LoadInst>(inst)) &&
275288 li->getOwnershipQualifier () ==
276289 LoadOwnershipQualifier::Take) {
0 commit comments