-
Notifications
You must be signed in to change notification settings - Fork 10.5k
SILGen: Don't crash on withoutActuallyEscaping of objc blocks #15393
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5480,6 +5480,28 @@ RValue RValueEmitter::visitMakeTemporarilyEscapableExpr( | |
visit(E->getNonescapingClosureValue()).getAsSingleValue(SGF, E); | ||
|
||
auto escapingFnTy = SGF.getLoweredType(E->getOpaqueValue()->getType()); | ||
auto silFnTy = escapingFnTy.castTo<SILFunctionType>(); | ||
|
||
// Handle @convention(block). No withoutActuallyEscaping verification yet. | ||
if (silFnTy->getExtInfo().getRepresentation() != | ||
SILFunctionTypeRepresentation::Thick) { | ||
RValue rvalue; | ||
auto escapingClosure = | ||
SGF.emitManagedRValueWithCleanup(SGF.B.createConvertFunction( | ||
E, functionValue.ensurePlusOne(SGF, E).forward(SGF), escapingFnTy)); | ||
// Bind the opaque value to the escaping function. | ||
SILGenFunction::OpaqueValueState opaqueValue{ | ||
escapingClosure, | ||
/*consumable*/ true, | ||
/*hasBeenConsumed*/ false, | ||
}; | ||
SILGenFunction::OpaqueValueRAII pushOpaqueValue(SGF, E->getOpaqueValue(), | ||
opaqueValue); | ||
|
||
// Emit the guarded expression. | ||
rvalue = visit(E->getSubExpr(), C); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not just return this rather than assign to rvalue. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will cleanup in follow-up commit. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. SGTM There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
return rvalue; | ||
} | ||
|
||
// Convert it to an escaping function value. | ||
auto escapingClosure = | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// RUN: %target-swift-frontend -module-name without_actually_escaping -emit-silgen -enable-sil-ownership %s | %FileCheck %s | ||
|
||
// REQUIRES: objc_interop | ||
|
||
import Foundation | ||
|
||
typealias Callback = @convention(block) () -> Void | ||
|
||
// CHECK-LABEL: sil {{.*}} @$S25without_actually_escaping9testBlock5blockyyyXB_tF | ||
// CHECK: bb0([[ARG:%.*]] : @guaranteed $@convention(block) @noescape () -> ()): | ||
// CHECK: [[C1:%.*]] = copy_block [[ARG]] | ||
// CHECK: [[B1:%.*]] = begin_borrow [[C1]] | ||
// CHECK: [[C2:%.*]] = copy_value [[B1]] | ||
// CHECK: [[CVT:%.*]] = convert_function [[C2]] : $@convention(block) @noescape () -> () to $@convention(block) () -> () | ||
// CHECK: [[B2:%.*]] = begin_borrow [[CVT]] | ||
// CHECK: [[FN:%.*]] = function_ref @$S25without_actually_escaping9testBlock5blockyyyXB_tFyyyXBXEfU_ | ||
// CHECK: apply [[FN]]([[B2]]) | ||
// CHECK: end_borrow [[B2]] from [[CVT]] | ||
// CHECK: destroy_value [[CVT]] | ||
// CHECK: end_borrow [[B1]] from [[C1]] | ||
// CHECK: destroy_value [[C1]] | ||
// CHECK: return | ||
|
||
func testBlock(block: Callback) { | ||
withoutActuallyEscaping(block) { $0() } | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you use SILGenFunction APIs instead of doing this by hand here?