Don't delete dead lexical values

Fixes rdar://117878243
This commit is contained in:
Meghana Gupta
2023-11-02 17:02:54 -07:00
parent df3468fd6c
commit 40d8f34b91

View File

@@ -62,18 +62,28 @@ static bool isScopeAffectingInstructionDead(SILInstruction *inst,
return false;
}
// If inst has any owned move-only value as a result, deleting it may shorten
// that value's lifetime which is illegal according to language rules.
//
// In particular, this check is needed before returning true when
// getSingleValueCopyOrCast returns true. That function returns true for
// move_value instructions. And `move_value %moveOnlyValue` must not be
// deleted.
for (auto result : inst->getResults()) {
// If inst has any owned move-only value as a result, deleting it may
// shorten that value's lifetime which is illegal according to language
// rules.
//
// In particular, this check is needed before returning true when
// getSingleValueCopyOrCast returns true. That function returns true for
// move_value instructions. And `move_value %moveOnlyValue` must not be
// deleted.
if (result->getType().getASTType()->isNoncopyable() &&
result->getOwnershipKind() == OwnershipKind::Owned) {
return false;
}
// If result was lexical, lifetime shortening maybe observed, return.
if (result->isLexical()) {
auto resultTy = result->getType().getAs<SILFunctionType>();
// Allow deleted dead lexical values when they are trivial no escape types.
if (!resultTy || !resultTy->isTrivialNoEscape()) {
return false;
}
}
}
// If inst is a copy or beginning of scope, inst is dead, since we know that