Skip to content

SILGen: Fix withoutActuallyEscaping of 'c' closures #29569

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
12 changes: 8 additions & 4 deletions lib/SILGen/SILGenExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5023,13 +5023,17 @@ RValue RValueEmitter::visitMakeTemporarilyEscapableExpr(
return visit(E->getSubExpr(), C);
};

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

// Convert it to an escaping function value.
Expand Down
13 changes: 13 additions & 0 deletions test/SILGen/without_actually_escaping.swift
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,16 @@ func withoutActuallyEscapingConflict() {
modifyAndPerform(&localVar, closure: $0)
}
}

// CHECK-LABEL: sil [ossa] @$s25without_actually_escaping0A25ActuallyEscapingCFunction8functionyyyXC_tF
// CHECK: bb0([[ARG:%.*]] : $@convention(c) @noescape () -> ()):
// CHECK: [[E:%.*]] = convert_function [[ARG]] : $@convention(c) @noescape () -> () to [without_actually_escaping] $@convention(c) () -> ()
// CHECK: [[F:%.*]] = function_ref @$s25without_actually_escaping0A25ActuallyEscapingCFunction8functionyyyXC_tFyyyXCXEfU_ : $@convention(thin) (@convention(c) () -> ()) -> ()
// CHECK: apply [[F]]([[E]]) : $@convention(thin) (@convention(c) () -> ()) -> ()
public func withoutActuallyEscapingCFunction(function: (@convention(c) () -> Void)) {
withoutActuallyEscaping(function) { f in
var pointer: UnsafeRawPointer? = nil
pointer = unsafeBitCast(f, to: UnsafeRawPointer.self)
print(pointer)
}
}