Skip to content

Drop 'Sendable' when converting to Clang types. #59638

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 3 commits into from
Jun 24, 2022
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
10 changes: 10 additions & 0 deletions lib/AST/ClangTypeConverter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,11 @@ clang::QualType ClangTypeConverter::visitProtocolType(ProtocolType *type) {
auto proto = type->getDecl();
auto &clangCtx = ClangASTContext;

// Strip 'Sendable'.
auto strippedType = type->stripConcurrency(false, false);
if (strippedType.getPointer() != type)
return convert(strippedType);

if (!proto->isObjC())
return clang::QualType();

Expand Down Expand Up @@ -702,6 +707,11 @@ ClangTypeConverter::visitSILBlockStorageType(SILBlockStorageType *type) {

clang::QualType
ClangTypeConverter::visitProtocolCompositionType(ProtocolCompositionType *type) {
// Strip 'Sendable'.
auto strippedType = type->stripConcurrency(false, false);
if (strippedType.getPointer() != type)
return convert(strippedType);

// Any will be lowered to AnyObject, so we return the same result.
if (type->isAny())
return getClangIdType(ClangASTContext);
Expand Down
65 changes: 64 additions & 1 deletion lib/AST/Type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,9 @@ ExistentialLayout::ExistentialLayout(CanProtocolCompositionType type) {
protoDecl = parameterized->getProtocol();
containsParameterized = true;
}
containsNonObjCProtocol |= !protoDecl->isObjC();
containsNonObjCProtocol |=
!protoDecl->isObjC() &&
!protoDecl->isSpecificProtocol(KnownProtocolKind::Sendable);
protocols.push_back(protoDecl);
}
}
Expand Down Expand Up @@ -960,6 +962,67 @@ Type TypeBase::stripConcurrency(bool recurse, bool dropGlobalActor) {
return newFnType;
}

if (auto existentialType = getAs<ExistentialType>()) {
auto newConstraintType = existentialType->getConstraintType()
->stripConcurrency(recurse, dropGlobalActor);
if (newConstraintType.getPointer() ==
existentialType->getConstraintType().getPointer())
return Type(this);

return ExistentialType::get(newConstraintType);
}

if (auto protocolType = getAs<ProtocolType>()) {
if (protocolType->getDecl()->isSpecificProtocol(
KnownProtocolKind::Sendable))
return ProtocolCompositionType::get(getASTContext(), { }, false);

return Type(this);
}

if (auto protocolCompositionType = getAs<ProtocolCompositionType>()) {
SmallVector<Type, 4> newMembers;
auto members = protocolCompositionType->getMembers();
for (unsigned i : indices(members)) {
auto memberType = members[i];
auto newMemberType =
memberType->stripConcurrency(recurse, dropGlobalActor);
if (!newMembers.empty()) {
newMembers.push_back(newMemberType);
continue;
}

if (memberType.getPointer() != newMemberType.getPointer()) {
newMembers.append(members.begin(), members.begin() + i);
newMembers.push_back(newMemberType);
continue;
}
}

if (!newMembers.empty()) {
return ProtocolCompositionType::get(
getASTContext(), newMembers,
protocolCompositionType->hasExplicitAnyObject());
}

return Type(this);
}

if (auto existentialMetatype = getAs<ExistentialMetatypeType>()) {
auto instanceType = existentialMetatype->getExistentialInstanceType();
auto newInstanceType =
instanceType->stripConcurrency(recurse, dropGlobalActor);
if (instanceType.getPointer() != newInstanceType.getPointer()) {
Optional<MetatypeRepresentation> repr;
if (existentialMetatype->hasRepresentation())
repr = existentialMetatype->getRepresentation();
return ExistentialMetatypeType::get(
newInstanceType, repr, getASTContext());
}

return Type(this);
}

return Type(this);
}

Expand Down
5 changes: 5 additions & 0 deletions test/IRGen/Inputs/objc_protocol_sendable.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#import <Foundation/Foundation.h>

@interface A: NSObject
- (id <NSObject>)foo __attribute__((swift_attr("@Sendable")));
@end
9 changes: 9 additions & 0 deletions test/IRGen/objc_protocol_sendable.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// RUN: %target-swift-frontend -emit-ir %s -swift-version 5 -import-objc-header %S/Inputs/objc_protocol_sendable.h | %IRGenFileCheck %s

// REQUIRES: objc_interop
// REQUIRES: concurrency

// CHECK: define{{.*}}s22objc_protocol_sendable4test1aySo1AC_tF
public func test(a: A) {
a.foo()
}
10 changes: 10 additions & 0 deletions test/SILGen/mangling_predates_concurrency.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,13 @@
public func excitingFunction<T: Sendable>(value: T, body: (@Sendable () -> Void)?) -> (@MainActor () -> Void) {
{ }
}

public protocol P { }

// CHECK: sil [ossa] @$s29mangling_predates_concurrency13lotsOfChangesyyXlSgyp_AA1P_pypXpAaD_XlXptF
@preconcurrency public func lotsOfChanges(
_: Sendable, _: P & Sendable, _: Sendable.Type,
_: (AnyObject & Sendable & P).Type
) -> (AnyObject & Sendable)? {
nil
}