Skip to content

ClosureLifetimeFixup: Handle undef partial_apply arguments gracefully #28805

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/SILOptimizer/Utils/InstOptUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1020,7 +1020,7 @@ void swift::releasePartialApplyCapturedArg(SILBuilder &builder, SILLocation loc,
// possible for that value.

// If we have qualified ownership, we should just emit a destroy value.
if (arg->getFunction()->hasOwnership()) {
if (builder.getFunction().hasOwnership()) {
callbacks.createdNewInst(builder.createDestroyValue(loc, arg));
return;
}
Expand Down
23 changes: 23 additions & 0 deletions test/SILOptimizer/closure-lifetime-fixup.sil
Original file line number Diff line number Diff line change
Expand Up @@ -155,3 +155,26 @@ bb1:
%86 = tuple ()
return %86 : $()
}

sil [ossa] @closureImpl : $@convention(thin) (@guaranteed Klass, @guaranteed Klass) -> Bool
sil [ossa] @useClosure : $@convention(thin) (@noescape @callee_guaranteed () -> Bool) -> ()

// Don't crash.
// CHECK-LABEL: sil hidden [ossa] @testUndefined
// CHECK: [[PA:%.*]] = partial_apply [callee_guaranteed] [on_stack]
// CHECK: mark_dependence
// CHECK: mark_dependence
// CHECK: dealloc_stack [[PA]] : $@noescape @callee_guaranteed () -> Bool
// CHECK: destroy_value
sil hidden [ossa] @testUndefined : $@convention(method) (@guaranteed Klass, @guaranteed Klass) -> () {
bb0(%0 : @guaranteed $Klass, %1 : @guaranteed $Klass):
%4 = function_ref @closureImpl : $@convention(thin) (@guaranteed Klass, @guaranteed Klass) -> Bool
%5 = copy_value %1 : $Klass
%6 = partial_apply [callee_guaranteed] %4(%5, undef) : $@convention(thin) (@guaranteed Klass, @guaranteed Klass) -> Bool
%7 = convert_escape_to_noescape [not_guaranteed] %6 : $@callee_guaranteed () -> Bool to $@noescape @callee_guaranteed () -> Bool
%21 = function_ref @useClosure : $@convention(thin) (@noescape @callee_guaranteed () -> Bool) -> ()
%22 = apply %21(%7) : $@convention(thin) (@noescape @callee_guaranteed () -> Bool) -> ()
destroy_value %6 : $@callee_guaranteed () -> Bool
%42 = tuple ()
return %42 : $()
}
13 changes: 13 additions & 0 deletions test/SILOptimizer/closure_lifetime_fixup_undef.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// RUN: not %target-swift-frontend %s -sil-verify-all -c 2>&1 | %FileCheck %s

// Report the error but don't crash.
// CHECK: error: closure captures 'stringList' before it is declared

class TestUndefined {
private var stringList: [String]!

func dontCrash(strings: [String]) {
assert(stringList.allSatisfy({ $0 == stringList.first!}))
let stringList = strings.filter({ $0 == "a" })
}
}