Skip to content

[4.2] SILGen: Don't crash on withoutActuallyEscaping of objc blocks #15394

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
32 changes: 21 additions & 11 deletions lib/SILGen/SILGenExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5382,28 +5382,38 @@ RValue RValueEmitter::visitMakeTemporarilyEscapableExpr(
visit(E->getNonescapingClosureValue()).getAsSingleValue(SGF, E);

auto escapingFnTy = SGF.getLoweredType(E->getOpaqueValue()->getType());
auto silFnTy = escapingFnTy.castTo<SILFunctionType>();

// Convert it to an escaping function value.
auto escapingClosure =
SGF.createWithoutActuallyEscapingClosure(E, functionValue, escapingFnTy);

RValue rvalue;
auto loc = SILLocation(E);
auto borrowedClosure = escapingClosure.borrow(SGF, loc);
{
auto visitSubExpr = [&](ManagedValue escapingClosure,
bool isClosureConsumable) -> RValue {
// Bind the opaque value to the escaping function.
SILGenFunction::OpaqueValueState opaqueValue{
borrowedClosure,
/*consumable*/ false,
escapingClosure,
/*consumable*/ isClosureConsumable,
/*hasBeenConsumed*/ false,
};
SILGenFunction::OpaqueValueRAII pushOpaqueValue(SGF, E->getOpaqueValue(),
opaqueValue);

// Emit the guarded expression.
rvalue = visit(E->getSubExpr(), C);
return visit(E->getSubExpr(), C);
};

// Handle @convention(block). No withoutActuallyEscaping verification yet.
if (silFnTy->getExtInfo().getRepresentation() !=
SILFunctionTypeRepresentation::Thick) {
auto escapingClosure =
SGF.B.createConvertFunction(E, functionValue, escapingFnTy);
return visitSubExpr(escapingClosure, true /*isClosureConsumable*/);
}

// Convert it to an escaping function value.
auto escapingClosure =
SGF.createWithoutActuallyEscapingClosure(E, functionValue, escapingFnTy);
auto loc = SILLocation(E);
auto borrowedClosure = escapingClosure.borrow(SGF, loc);
RValue rvalue = visitSubExpr(borrowedClosure, false /* isClosureConsumable */);

// Now create the verification of the withoutActuallyEscaping operand.
// Either we fail the uniquenes check (which means the closure has escaped)
// and abort or we continue and destroy the ultimate reference.
Expand Down
24 changes: 24 additions & 0 deletions test/SILGen/without_actually_escaping_block.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// 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:%.*]] : @owned $@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: [[FN:%.*]] = function_ref @$S25without_actually_escaping9testBlock5blockyyyXB_tFyyyXBXEfU_
// CHECK: apply [[FN]]([[CVT]])
// CHECK: end_borrow [[B1]] from [[C1]]
// CHECK: destroy_value [[C1]]
// CHECK: destroy_value [[ARG]]
// CHECK: return

func testBlock(block: Callback) {
withoutActuallyEscaping(block) { $0() }
}