Skip to content

Commit 74affff

Browse files
committed
Propagate actor isolation freely through Objective-C declarations.
1 parent a7c6a1a commit 74affff

File tree

3 files changed

+65
-11
lines changed

3 files changed

+65
-11
lines changed

lib/Sema/TypeCheckConcurrency.cpp

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,19 +29,36 @@ using namespace swift;
2929
/// Determine whether it makes sense to infer an attribute in the given
3030
/// context.
3131
static bool shouldInferAttributeInContext(const DeclContext *dc) {
32-
auto sourceFile = dc->getParentSourceFile();
33-
if (!sourceFile)
34-
return false;
32+
if (auto *file = dyn_cast<FileUnit>(dc->getModuleScopeContext())) {
33+
switch (file->getKind()) {
34+
case FileUnitKind::Source:
35+
// Check what kind of source file we have.
36+
if (auto sourceFile = dc->getParentSourceFile()) {
37+
switch (sourceFile->Kind) {
38+
case SourceFileKind::Interface:
39+
// Interfaces have explicitly called-out ConcurrentValue conformances.
40+
return false;
3541

36-
switch (sourceFile->Kind) {
37-
case SourceFileKind::Interface:
38-
case SourceFileKind::SIL:
39-
return false;
42+
case SourceFileKind::Library:
43+
case SourceFileKind::Main:
44+
case SourceFileKind::SIL:
45+
return true;
46+
}
47+
}
48+
break;
4049

41-
case SourceFileKind::Library:
42-
case SourceFileKind::Main:
43-
return true;
50+
case FileUnitKind::Builtin:
51+
case FileUnitKind::SerializedAST:
52+
case FileUnitKind::Synthesized:
53+
return false;
54+
55+
case FileUnitKind::ClangModule:
56+
case FileUnitKind::DWARFModule:
57+
return true;
58+
}
4459
}
60+
61+
return false;
4562
}
4663

4764
/// Check whether the @asyncHandler attribute can be applied to the given
@@ -2349,7 +2366,7 @@ ActorIsolation ActorIsolationRequest::evaluate(
23492366
break;
23502367

23512368
case ActorIsolation::GlobalActorUnsafe:
2352-
if (!propagateUnsafe) {
2369+
if (!propagateUnsafe && !value->hasClangNode()) {
23532370
// Don't infer unsafe global actor isolation.
23542371
return ActorIsolation::forUnspecified();
23552372
}

test/ClangImporter/objc_async.swift

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,3 +99,30 @@ func acceptCV<T: ConcurrentValue>(_: T) { }
9999
func testCV(r: NSRange) {
100100
acceptCV(r)
101101
}
102+
103+
// Global actor (unsafe) isolation.
104+
105+
actor SomeActor { }
106+
107+
@globalActor
108+
struct SomeGlobalActor {
109+
static let shared = SomeActor()
110+
}
111+
112+
class MyButton : NXButton {
113+
@MainActor func testMain() {
114+
onButtonPress() // okay
115+
}
116+
117+
@SomeGlobalActor func testOther() {
118+
onButtonPress() // expected-error{{instance method 'onButtonPress()' isolated to global actor 'MainActor' can not be referenced from different global actor 'SomeGlobalActor'}}
119+
}
120+
121+
func test() {
122+
onButtonPress() // okay
123+
}
124+
}
125+
126+
func testButtons(mb: MyButton) {
127+
mb.onButtonPress()
128+
}

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,4 +129,14 @@ typedef void ( ^ObjCErrorHandler )( NSError * _Nullable inError );
129129

130130
#define MAGIC_NUMBER 42
131131

132+
133+
__attribute__((__swift_attr__("@MainActor(unsafe)")))
134+
@interface NXView : NSObject
135+
-(void)onDisplay;
136+
@end
137+
138+
@interface NXButton: NXView
139+
-(void)onButtonPress;
140+
@end
141+
132142
#pragma clang assume_nonnull end

0 commit comments

Comments
 (0)