Skip to content

Commit 20f99b2

Browse files
committed
Verify that on-stack closures do not take owned arguments
1 parent 83ce3a1 commit 20f99b2

File tree

3 files changed

+13
-4
lines changed

3 files changed

+13
-4
lines changed

include/swift/SIL/ApplySite.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,9 @@ class ApplySite {
390390
case SILArgumentConvention::Pack_Inout:
391391
return conv;
392392
case SILArgumentConvention::Direct_Owned:
393+
assert(!pai->isOnStack() &&
394+
"on-stack closures do not support owned arguments");
395+
return conv;
393396
case SILArgumentConvention::Direct_Unowned:
394397
case SILArgumentConvention::Direct_Guaranteed:
395398
return pai->isOnStack() ? SILArgumentConvention::Direct_Guaranteed

lib/SIL/Verifier/SILVerifier.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1956,6 +1956,11 @@ class SILVerifier : public SILVerifierBase<SILVerifier> {
19561956
unsigned appliedArgStartIdx =
19571957
substConv.getNumSILArguments() - PAI->getNumArguments();
19581958
for (auto p : llvm::enumerate(PAI->getArguments())) {
1959+
if (PAI->isOnStack()) {
1960+
require(
1961+
substConv.getSILArgumentConvention(appliedArgStartIdx + p.index()),
1962+
"on-stack closures do not support owned arguments");
1963+
}
19591964
requireSameType(
19601965
p.value()->getType(),
19611966
substConv.getSILArgumentType(appliedArgStartIdx + p.index(),

lib/SILOptimizer/Utils/InstOptUtils.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -906,9 +906,10 @@ void swift::emitDestroyOperation(SILBuilder &builder, SILLocation loc,
906906
// convention of the closed over function. All non-inout arguments to a
907907
// partial_apply are passed at +1 for regular escaping closures and +0 for
908908
// closures that have been promoted to partial_apply [on_stack]. An escaping
909-
// partial apply is building up a boxed aggregate to send off to the closed over
910-
// function, even for guaranteed and in_guaranteed convention. Of course, when
911-
// you call the function, the proper conventions will be used.
909+
// partial apply stores each capture in an owned box, even for guaranteed and
910+
// in_guaranteed argument convention. A non-escaping/on-stack closure either
911+
// borrows its arguments or takes an inout_aliasable address. Non-escaping
912+
// closures do not support owned arguments.
912913
void swift::releasePartialApplyCapturedArg(SILBuilder &builder, SILLocation loc,
913914
SILValue arg,
914915
SILParameterInfo paramInfo,
@@ -1492,7 +1493,7 @@ void swift::insertDestroyOfCapturedArguments(
14921493
if (!argValue)
14931494
continue;
14941495

1495-
assert(!argValue->getFunction()->hasOwnership()
1496+
assert(!pai->getFunction()->hasOwnership()
14961497
|| (argValue->getOwnershipKind().isCompatibleWith(
14971498
OwnershipKind::Owned)));
14981499

0 commit comments

Comments
 (0)