Skip to content

Commit 0c9c8f9

Browse files
committed
Imported C declarations always predate concurrency
1 parent 7be6058 commit 0c9c8f9

File tree

3 files changed

+15
-9
lines changed

3 files changed

+15
-9
lines changed

lib/AST/Decl.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
#include "swift/AST/TypeLoc.h"
4747
#include "swift/AST/SwiftNameTranslation.h"
4848
#include "swift/Basic/Defer.h"
49+
#include "swift/ClangImporter/ClangModule.h"
4950
#include "swift/Parse/Lexer.h" // FIXME: Bad dependency
5051
#include "clang/Lex/MacroInfo.h"
5152
#include "llvm/ADT/SmallPtrSet.h"
@@ -719,7 +720,14 @@ Optional<CustomAttrNominalPair> Decl::getGlobalActorAttr() const {
719720
}
720721

721722
bool Decl::predatesConcurrency() const {
722-
return getAttrs().hasAttribute<PredatesConcurrencyAttr>();
723+
if (getAttrs().hasAttribute<PredatesConcurrencyAttr>())
724+
return true;
725+
726+
// Imported C declarations always predate concurrency.
727+
if (isa<ClangModuleUnit>(getDeclContext()->getModuleScopeContext()))
728+
return true;
729+
730+
return false;
723731
}
724732

725733

test/ClangImporter/objc_async.swift

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -90,16 +90,14 @@ func testSlowServerOldSchool(slowServer: SlowServer) {
9090
_ = slowServer.allOperations
9191
}
9292

93-
func testSendable(fn: () -> Void) { // expected-note{{parameter 'fn' is implicitly non-sendable}}
94-
doSomethingConcurrently(fn)
95-
// expected-error@-1{{passing non-sendable parameter 'fn' to function expecting a @Sendable closure}}
93+
func testSendable(fn: () -> Void) {
94+
doSomethingConcurrently(fn) // okay, due to implicit @_predatesConcurrency
9695
doSomethingConcurrentlyButUnsafe(fn) // okay, @Sendable not part of the type
9796

9897
var x = 17
9998
doSomethingConcurrently {
100-
print(x) // expected-error{{reference to captured var 'x' in concurrently-executing code}}
101-
x = x + 1 // expected-error{{mutation of captured var 'x' in concurrently-executing code}}
102-
// expected-error@-1{{reference to captured var 'x' in concurrently-executing code}}
99+
print(x)
100+
x = x + 1
103101
}
104102
}
105103

test/Inputs/clang-importer-sdk/usr/include/ObjCConcurrency.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ typedef void (^CompletionHandler)(NSString * _Nullable, NSString * _Nullable_res
107107
-(void)asyncImportSame:(NSString *)operation completionHandler:(void (^)(NSInteger))handler;
108108
-(void)asyncImportSame:(NSString *)operation replyTo:(void (^)(NSInteger))handler __attribute__((swift_async(none)));
109109

110-
-(void)overridableButRunsOnMainThreadWithCompletionHandler:(MAIN_ACTOR void (^ _Nullable)(NSString *))completion __attribute__((swift_attr("@_predatesConcurrency")));
110+
-(void)overridableButRunsOnMainThreadWithCompletionHandler:(MAIN_ACTOR void (^ _Nullable)(NSString *))completion;
111111
- (void)obtainClosureWithCompletionHandler:(void (^)(void (^_Nullable)(void),
112112
NSError *_Nullable,
113113
BOOL))completionHandler
@@ -192,7 +192,7 @@ void doSomethingConcurrently(__attribute__((noescape)) SENDABLE void (^block)(vo
192192

193193

194194

195-
void doSomethingConcurrentlyButUnsafe(__attribute__((noescape)) __attribute__((swift_attr("@Sendable"))) void (^block)(void)) __attribute__((swift_attr("@_predatesConcurrency")));
195+
void doSomethingConcurrentlyButUnsafe(__attribute__((noescape)) __attribute__((swift_attr("@Sendable"))) void (^block)(void));
196196

197197

198198
MAIN_ACTOR MAIN_ACTOR __attribute__((__swift_attr__("@MainActor(unsafe)"))) @protocol TripleMainActor

0 commit comments

Comments
 (0)