@@ -62,18 +62,28 @@ static bool isScopeAffectingInstructionDead(SILInstruction *inst,
6262 return false ;
6363 }
6464
65- // If inst has any owned move-only value as a result, deleting it may shorten
66- // that value's lifetime which is illegal according to language rules.
67- //
68- // In particular, this check is needed before returning true when
69- // getSingleValueCopyOrCast returns true. That function returns true for
70- // move_value instructions. And `move_value %moveOnlyValue` must not be
71- // deleted.
7265 for (auto result : inst->getResults ()) {
66+ // If inst has any owned move-only value as a result, deleting it may
67+ // shorten that value's lifetime which is illegal according to language
68+ // rules.
69+ //
70+ // In particular, this check is needed before returning true when
71+ // getSingleValueCopyOrCast returns true. That function returns true for
72+ // move_value instructions. And `move_value %moveOnlyValue` must not be
73+ // deleted.
7374 if (result->getType ().getASTType ()->isNoncopyable () &&
7475 result->getOwnershipKind () == OwnershipKind::Owned) {
7576 return false ;
7677 }
78+
79+ // If result was lexical, lifetime shortening maybe observed, return.
80+ if (result->isLexical ()) {
81+ auto resultTy = result->getType ().getAs <SILFunctionType>();
82+ // Allow deleted dead lexical values when they are trivial no escape types.
83+ if (!resultTy || !resultTy->isTrivialNoEscape ()) {
84+ return false ;
85+ }
86+ }
7787 }
7888
7989 // If inst is a copy or beginning of scope, inst is dead, since we know that
0 commit comments