Skip to content

Commit 8f8e856

Browse files
committed
[ClangImporter] Account for Sendable attributes on generic parameter requirements
1 parent a168a94 commit 8f8e856

File tree

3 files changed

+50
-2
lines changed

3 files changed

+50
-2
lines changed

lib/ClangImporter/ImportDecl.cpp

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7308,8 +7308,14 @@ llvm::Optional<GenericParamList *> SwiftDeclConverter::importObjCGenericParams(
73087308
SmallVector<InheritedEntry, 1> inherited;
73097309
if (objcGenericParam->hasExplicitBound()) {
73107310
assert(!objcGenericParam->getUnderlyingType().isNull());
7311-
auto clangBound = objcGenericParam->getUnderlyingType()
7311+
auto underlyingTy = objcGenericParam->getUnderlyingType();
7312+
auto clangBound = underlyingTy
73127313
->castAs<clang::ObjCObjectPointerType>();
7314+
7315+
ImportTypeAttrs attrs;
7316+
getConcurrencyAttrs(Impl.SwiftContext, ImportTypeKind::Abstract, attrs,
7317+
underlyingTy);
7318+
73137319
if (clangBound->getInterfaceDecl()) {
73147320
auto unqualifiedClangBound =
73157321
clangBound->stripObjCKindOfTypeAndQuals(Impl.getClangASTContext());
@@ -7324,6 +7330,15 @@ llvm::Optional<GenericParamList *> SwiftDeclConverter::importObjCGenericParams(
73247330
}
73257331
inherited.push_back(TypeLoc::withoutLoc(superclassType));
73267332
}
7333+
7334+
if (attrs.contains(ImportTypeAttr::Sendable)) {
7335+
if (auto *sendable =
7336+
Impl.SwiftContext.getProtocol(KnownProtocolKind::Sendable)) {
7337+
inherited.push_back(
7338+
TypeLoc::withoutLoc(sendable->getDeclaredInterfaceType()));
7339+
}
7340+
}
7341+
73277342
for (clang::ObjCProtocolDecl *clangProto : clangBound->quals()) {
73287343
ProtocolDecl *proto = castIgnoringCompatibilityAlias<ProtocolDecl>(
73297344
Impl.importDecl(clangProto, getActiveSwiftVersion()));

lib/ClangImporter/ImportType.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1991,7 +1991,9 @@ class GetSendableType :
19911991
VISIT(ModuleType, pass)
19921992
VISIT(DynamicSelfType, pass)
19931993

1994-
NEVER_VISIT(SubstitutableType)
1994+
// Ignore attributes placed on generic parameter references and
1995+
// other substitutable types.
1996+
VISIT(SubstitutableType, pass)
19951997
NEVER_VISIT(DependentMemberType)
19961998

19971999
Result visitAnyFunctionType(AnyFunctionType *ty) {

test/Concurrency/sendable_objc_attr_in_type_context.swift

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,17 @@
2121
@interface MyValue : NSObject
2222
@end
2323

24+
SWIFT_SENDABLE
25+
@protocol P <NSObject>
26+
@end
27+
28+
@interface SendableValue : NSObject<P>
29+
@end
30+
31+
SWIFT_SENDABLE
32+
@interface SendableMyValue : MyValue
33+
@end
34+
2435
typedef void (^CompletionHandler)(void (^ SWIFT_SENDABLE)(void)) SWIFT_SENDABLE;
2536

2637
@interface Test : NSObject
@@ -36,6 +47,14 @@ typedef void (^CompletionHandler)(void (^ SWIFT_SENDABLE)(void)) SWIFT_SENDABLE;
3647
// Placement of SWIFT_SENDABLE matters here
3748
void doSomethingConcurrently(__attribute__((noescape)) void SWIFT_SENDABLE (^block)(void));
3849

50+
@interface TestWithSendableID<T: SWIFT_SENDABLE id> : NSObject
51+
-(void) add: (T) object;
52+
@end
53+
54+
@interface TestWithSendableSuperclass<T: MyValue *SWIFT_SENDABLE> : NSObject
55+
-(void) add: (T) object;
56+
@end
57+
3958
#pragma clang assume_nonnull end
4059

4160
//--- main.swift
@@ -72,4 +91,16 @@ func test_sendable_attr_in_type_context(test: Test) {
7291
test.withAliasCompletionHandler { callback in
7392
doSomethingConcurrently(callback) // Ok
7493
}
94+
95+
_ = TestWithSendableID<SendableValue>() // Ok
96+
97+
// TOOD(diagnostics): Duplicate diagnostics
98+
TestWithSendableID().add(MyValue())
99+
// expected-warning@-1 3 {{type 'MyValue' does not conform to the 'Sendable' protocol}}
100+
101+
TestWithSendableSuperclass().add(SendableMyValue()) // Ok
102+
103+
// TOOD(diagnostics): Duplicate diagnostics
104+
TestWithSendableSuperclass().add(MyValue())
105+
// expected-warning@-1 3 {{type 'MyValue' does not conform to the 'Sendable' protocol}}
75106
}

0 commit comments

Comments
 (0)