Skip to content

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

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
22 changes: 22 additions & 0 deletions lib/SILGen/SILGenExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Copy link
Contributor

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?

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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not just return this rather than assign to rvalue.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will cleanup in follow-up commit.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SGTM

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return rvalue;
}

// Convert it to an escaping function value.
auto escapingClosure =
Expand Down
26 changes: 26 additions & 0 deletions test/SILGen/without_actually_escaping_block.swift
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() }
}