Skip to content

Commit 8dc8532

Browse files
committed
[Concurrency] Ban asynchronous methods returning 'Self' from being @objc
1 parent daf9e2d commit 8dc8532

File tree

3 files changed

+17
-0
lines changed

3 files changed

+17
-0
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4129,6 +4129,8 @@ NOTE(objc_ambiguous_async_convention_candidate,none,
41294129
ERROR(satisfy_async_objc,none,
41304130
"satisfying an asychronous @objc %select{method|initializer}0 with "
41314131
"a synchronous %select{method|initializer}0 is not supported", (bool))
4132+
ERROR(async_objc_dynamic_self,none,
4133+
"asynchronous method returning 'Self' cannot be '@objc'", ())
41324134

41334135
//------------------------------------------------------------------------------
41344136
// MARK: Type Check Types

lib/Sema/TypeCheckDeclObjC.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -630,6 +630,18 @@ bool swift::isRepresentableInObjC(
630630
return false;
631631
}
632632

633+
// The completion handler transformation cannot properly represent a
634+
// dynamic 'Self' type, so disallow @objc for such methods.
635+
if (FD->hasDynamicSelfResult()) {
636+
if (Diagnose) {
637+
AFD->diagnose(diag::async_objc_dynamic_self)
638+
.highlight(AFD->getAsyncLoc());
639+
describeObjCReason(AFD, Reason);
640+
}
641+
642+
return false;
643+
}
644+
633645
// The completion handler parameter always goes at the end.
634646
unsigned completionHandlerParamIndex = AFD->getParameters()->size();
635647

test/attr/attr_objc_async.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,7 @@ class Concurrency {
1717

1818
@objc func takeAnAsync(_ fn: () async -> Int) { } // expected-error{{method cannot be marked @objc because the type of the parameter cannot be represented in Objective-C}}
1919
// expected-note@-1{{'async' function types cannot be represented in Objective-C}}
20+
21+
@objc class func createAsynchronously() async -> Self? { nil }
22+
// expected-error@-1{{asynchronous method returning 'Self' cannot be '@objc'}}
2023
}

0 commit comments

Comments
 (0)