Skip to content

Commit c8eb12b

Browse files
authored
Merge pull request #15390 from slavapestov/exclusivity-noescape-crash
SILOptimizer: Fix exclusivity checking crash when passing 'nil' as optional noescape closure
2 parents 9f275ca + e59c74d commit c8eb12b

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

lib/SIL/InstructionUtils.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -554,8 +554,13 @@ FindClosureResult swift::findClosureForAppliedArg(SILValue V) {
554554
if (auto *bbi = dyn_cast<BeginBorrowInst>(V))
555555
V = bbi->getOperand();
556556

557-
if (V->getType().getOptionalObjectType())
558-
V = cast<EnumInst>(V)->getOperand();
557+
if (V->getType().getOptionalObjectType()) {
558+
auto *EI = dyn_cast<EnumInst>(V);
559+
if (!EI || !EI->hasOperand())
560+
return FindClosureResult(nullptr, false);
561+
562+
V = EI->getOperand();
563+
}
559564

560565
auto fnType = V->getType().getAs<SILFunctionType>();
561566
if (fnType->getRepresentation() == SILFunctionTypeRepresentation::Block) {

test/SILOptimizer/exclusivity_static_diagnostics.sil

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -878,3 +878,14 @@ bb0(%0 : $Int):
878878
%10 = tuple ()
879879
return %10 : $()
880880
}
881+
882+
sil hidden @passNilToNoEscape : $@convention(thin) (Int) -> () {
883+
bb0( %0 : $Int):
884+
%2 = alloc_box ${ var Int }
885+
%3 = project_box %2 : ${ var Int }, 0
886+
%4 = enum $Optional<@convention(block) @noescape () -> ()>, #Optional.none!enumelt
887+
%5 = function_ref @takesInoutAndNoEscapeOptionalBlockClosure : $@convention(thin) (@inout Int, @owned Optional<@convention(block) @noescape () -> ()>) -> ()
888+
%6 = apply %5(%3, %4) : $@convention(thin) (@inout Int, @owned Optional<@convention(block) @noescape () -> ()>) -> ()
889+
%7 = tuple ()
890+
return %7 : $()
891+
}

0 commit comments

Comments
 (0)