Skip to content

Commit e050e35

Browse files
committed
[AST] Teach InheritedProtocolsRequest about synthesized protocol attributes
ClangImporter converts NS_SWIFT_SENDABLE into `SynthesizedProtocolAttr` which needs to be recognized by the request that collects all of the protocols that the given protocol inherits.
1 parent 23effda commit e050e35

File tree

2 files changed

+52
-6
lines changed

2 files changed

+52
-6
lines changed

lib/AST/NameLookup.cpp

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3186,18 +3186,24 @@ SuperclassDeclRequest::evaluate(Evaluator &evaluator,
31863186
ArrayRef<ProtocolDecl *>
31873187
InheritedProtocolsRequest::evaluate(Evaluator &evaluator,
31883188
ProtocolDecl *PD) const {
3189-
llvm::SmallVector<ProtocolDecl *, 2> result;
3190-
SmallPtrSet<const ProtocolDecl *, 2> known;
3191-
known.insert(PD);
3189+
llvm::SmallSetVector<ProtocolDecl *, 2> inherited;
3190+
auto addInherited = [&inherited, &PD](ProtocolDecl *P) {
3191+
if (PD != P)
3192+
inherited.insert(P);
3193+
};
3194+
3195+
for (auto attr : PD->getAttrs().getAttributes<SynthesizedProtocolAttr>()) {
3196+
addInherited(attr->getProtocol());
3197+
}
3198+
31923199
bool anyObject = false;
31933200
for (const auto &found : getDirectlyInheritedNominalTypeDecls(PD, anyObject)) {
31943201
if (auto proto = dyn_cast<ProtocolDecl>(found.Item)) {
3195-
if (known.insert(proto).second)
3196-
result.push_back(proto);
3202+
addInherited(proto);
31973203
}
31983204
}
31993205

3200-
return PD->getASTContext().AllocateCopy(result);
3206+
return PD->getASTContext().AllocateCopy(inherited.getArrayRef());
32013207
}
32023208

32033209
ArrayRef<ValueDecl *>
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// RUN: %empty-directory(%t/src)
2+
// RUN: %empty-directory(%t/sdk)
3+
// RUN: split-file %s %t/src
4+
5+
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk) -typecheck %t/src/main.swift \
6+
// RUN: -import-objc-header %t/src/Test.h \
7+
// RUN: -strict-concurrency=complete \
8+
// RUN: -module-name main -I %t -verify
9+
10+
// REQUIRES: objc_interop
11+
// REQUIRES: concurrency
12+
13+
//--- Test.h
14+
#define NS_SWIFT_SENDABLE __attribute__((__swift_attr__("@Sendable")))
15+
16+
#pragma clang assume_nonnull begin
17+
18+
@import Foundation;
19+
20+
NS_SWIFT_SENDABLE
21+
@protocol MyObjCProtocol <NSObject>
22+
@property (readonly, nonatomic) NSString *test;
23+
@end
24+
25+
#pragma clang assume_nonnull end
26+
27+
//--- main.swift
28+
struct MyStruct: Sendable {
29+
let value: any MyObjCProtocol // Ok
30+
}
31+
32+
extension Sendable {
33+
func compute() {}
34+
}
35+
36+
extension MyObjCProtocol {
37+
func test() {
38+
compute() // Ok
39+
}
40+
}

0 commit comments

Comments
 (0)