Skip to content

Commit

Permalink
[sending] Make sending a no escape closure a default rather than cons…
Browse files Browse the repository at this point in the history
…uming parameter

The reason why I am doing this is that:

1. We don't support no escaping closure parameters today and would like
   to leave the design space open.

2. These closures are bitwise copyable/trivial so marking them as
   specifically consuming doesn't really make sense.
gottesmm committed Jun 21, 2024
1 parent 5fbf42a commit 4d7af7b
Showing 3 changed files with 17 additions and 4 deletions.
9 changes: 6 additions & 3 deletions lib/Sema/TypeCheckDecl.cpp
Original file line number Diff line number Diff line change
@@ -2261,16 +2261,19 @@ ParamSpecifierRequest::evaluate(Evaluator &evaluator,
if (auto transferring = dyn_cast<TransferringTypeRepr>(nestedRepr)) {
// If we do not have an Ownership Repr, return implicit copyable consuming.
auto *base = transferring->getBase();
if (!isa<OwnershipTypeRepr>(base)) {
if (!param->getInterfaceType()->isNoEscape() &&
!isa<OwnershipTypeRepr>(base)) {
return ParamSpecifier::ImplicitlyCopyableConsuming;
}
nestedRepr = base;
}

if (auto sending = dyn_cast<SendingTypeRepr>(nestedRepr)) {
// If we do not have an Ownership Repr, return implicit copyable consuming.
// If we do not have an Ownership Repr and do not have a no escape type,
// return implicit copyable consuming.
auto *base = sending->getBase();
if (!isa<OwnershipTypeRepr>(base)) {
if (!param->getInterfaceType()->isNoEscape() &&
!isa<OwnershipTypeRepr>(base)) {
return ParamSpecifier::ImplicitlyCopyableConsuming;
}
nestedRepr = base;
5 changes: 4 additions & 1 deletion test/Concurrency/sending_mangling.swift
Original file line number Diff line number Diff line change
@@ -79,11 +79,14 @@ extension SendingProtocol {
// CHECK: sil hidden [ossa] @(extension in sending_mangling):sending_mangling.SendingProtocol.sendingResult() -> sending_mangling.NonSendableKlass : $@convention(method) <Self where Self : SendingProtocol> (@in_guaranteed Self) -> @sil_sending @owned NonSendableKlass {
func sendingResult() -> sending NonSendableKlass { fatalError() }

// CHECK: sil hidden [ossa] @(extension in sending_mangling):sending_mangling.SendingProtocol.sendingArgWithFunctionSendingArg(__owned (sending __owned sending_mangling.NonSendableKlass) -> ()) -> () : $@convention(method) <Self where Self : SendingProtocol> (@sil_sending @owned @noescape @callee_guaranteed (@sil_sending @owned NonSendableKlass) -> (), @in_guaranteed Self) -> () {
// CHECK: sil hidden [ossa] @(extension in sending_mangling):sending_mangling.SendingProtocol.sendingArgWithFunctionSendingArg((sending __owned sending_mangling.NonSendableKlass) -> ()) -> () : $@convention(method) <Self where Self : SendingProtocol> (@sil_sending @guaranteed @noescape @callee_guaranteed (@sil_sending @owned NonSendableKlass) -> (), @in_guaranteed Self) -> () {
func sendingArgWithFunctionSendingArg(_ x: sending (sending NonSendableKlass) -> ()) {}

// CHECK: sil hidden [ossa] @(extension in sending_mangling):sending_mangling.SendingProtocol.sendingArgWithFunctionSendingResult() -> (sending __owned sending_mangling.NonSendableKlass) -> () : $@convention(method) <Self where Self : SendingProtocol> (@in_guaranteed Self) -> @sil_sending @owned @callee_guaranteed (@sil_sending @owned NonSendableKlass) -> () {
func sendingArgWithFunctionSendingResult() -> sending (sending NonSendableKlass) -> () { fatalError() }

// CHECK: sil hidden [ossa] @(extension in sending_mangling):sending_mangling.SendingProtocol.sendingArgWithEscapingFunctionSendingArg(__owned (sending __owned sending_mangling.NonSendableKlass) -> ()) -> () : $@convention(method) <Self where Self : SendingProtocol> (@sil_sending @owned @callee_guaranteed (@sil_sending @owned NonSendableKlass) -> (), @in_guaranteed Self) -> () {
func sendingArgWithEscapingFunctionSendingArg(_ x: sending @escaping (sending NonSendableKlass) -> ()) {}
}

// Make sure we only do not mangle in __shared if we are borrowed by default and
7 changes: 7 additions & 0 deletions test/Concurrency/transfernonsendable_sending_params.swift
Original file line number Diff line number Diff line change
@@ -411,3 +411,10 @@ actor NonSendableInit {
set { fatalError() }
}
}

func testNoCrashWhenSendingNoEscapeClosure() async {
func test(_ x: sending () -> ()) async {}

let c = Klass()
await test { print(c) }
}

0 comments on commit 4d7af7b

Please sign in to comment.