Skip to content

SIL: Bridge 'any Sendable' via the id-as-Any path #65405

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
merged 1 commit into from
Apr 25, 2023
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
3 changes: 3 additions & 0 deletions include/swift/AST/Types.h
Original file line number Diff line number Diff line change
Expand Up @@ -601,6 +601,9 @@ class alignas(1 << TypeAlignInBits) TypeBase
/// Is this the 'Any' type?
bool isAny();

/// Is this an existential containing only marker protocols?
bool isMarkerExistential();

bool isPlaceholder();

/// Returns true if this is a move-only type.
Expand Down
26 changes: 24 additions & 2 deletions lib/AST/Type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,28 @@ bool TypeBase::isAny() {
return constraint->isEqual(getASTContext().TheAnyType);
}

bool TypeBase::isMarkerExistential() {
Type constraint = this;
if (auto existential = constraint->getAs<ExistentialType>())
constraint = existential->getConstraintType();

if (!constraint->isConstraintType())
return false;

auto layout = constraint->getExistentialLayout();
if (layout.hasExplicitAnyObject ||
layout.explicitSuperclass) {
return false;
}

for (auto *proto : layout.getProtocols()) {
if (!proto->isMarkerProtocol())
return false;
}

return true;
}

bool TypeBase::isPureMoveOnly() {
if (auto *nom = getNominalOrBoundGenericNominal())
return nom->isMoveOnly();
Expand Down Expand Up @@ -2914,8 +2936,8 @@ getObjCObjectRepresentable(Type type, const DeclContext *dc) {
return ForeignRepresentableKind::Object;
}

// Any can be bridged to id.
if (type->isAny()) {
// Existentials consisting of only marker protocols can be bridged to id.
if (type->isMarkerExistential()) {
return ForeignRepresentableKind::Bridged;
}

Expand Down
5 changes: 3 additions & 2 deletions lib/SIL/IR/Bridging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,9 @@ Type TypeConverter::getLoweredCBridgedType(AbstractionPattern pattern,
}
}

// `Any` can bridge to `AnyObject` (`id` in ObjC).
if (t->isAny())
// Existentials consisting of only marker protocols can bridge to
// `AnyObject` (`id` in ObjC).
if (t->isMarkerExistential())
return Context.getAnyObjectType();

if (auto funTy = t->getAs<FunctionType>()) {
Expand Down
5 changes: 5 additions & 0 deletions test/SILGen/Inputs/objc_bridging_sendable.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
@import Foundation;

@interface NSBlah: NSObject
- (void) takeSendable: (id __attribute__((swift_attr("@Sendable")))) x;
@end
11 changes: 11 additions & 0 deletions test/SILGen/objc_bridging_sendable.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// RUN: %target-swift-emit-silgen(mock-sdk: %clang-importer-sdk) -import-objc-header %S/Inputs/objc_bridging_sendable.h %s | %FileCheck %s

// REQUIRES: objc_interop

// CHECK-LABEL: sil [ossa] @$s22objc_bridging_sendable18passSendableToObjCyys0E0_pF : $@convention(thin) (@in_guaranteed any Sendable) -> () {
// CHECK: function_ref @$ss27_bridgeAnythingToObjectiveCyyXlxlF : $@convention(thin) <τ_0_0> (@in_guaranteed τ_0_0) -> @owned AnyObject
// CHECK: objc_method {{%.*}} : $NSBlah, #NSBlah.takeSendable!foreign : (NSBlah) -> ((any Sendable)?) -> (), $@convention(objc_method) (Optional<AnyObject>, NSBlah) -> ()
// CHECK: return
public func passSendableToObjC(_ s: Sendable) {
NSBlah().takeSendable(s)
}