Skip to content

Commit d7a97ac

Browse files
committed
Disallow async IBAction methods
The IBAction signature does not allow completion handlers, so making IBAction methods async doesn't make sense. Disallowing it statically so it doesn't just break people when they try.
1 parent 93fef05 commit d7a97ac

File tree

3 files changed

+11
-0
lines changed

3 files changed

+11
-0
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1433,6 +1433,8 @@ ERROR(invalid_ibinspectable,none,
14331433
"only instance properties can be declared @%0", (StringRef))
14341434
ERROR(invalid_ibaction_decl,none,
14351435
"only instance methods can be declared @%0", (StringRef))
1436+
ERROR(invalid_ibaction_decl_async,none,
1437+
"@%0 instance methods cannot be async", (StringRef))
14361438
ERROR(invalid_ibaction_result,none,
14371439
"methods declared @%0 must %select{|not }1return a value", (StringRef, bool))
14381440
ERROR(invalid_ibaction_argument_count,none,

lib/Sema/TypeCheckAttr.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -530,6 +530,12 @@ void AttributeChecker::visitIBActionAttr(IBActionAttr *attr) {
530530
return;
531531
}
532532

533+
if (FD->isAsyncContext()) {
534+
diagnoseAndRemoveAttr(attr, diag::invalid_ibaction_decl_async,
535+
attr->getAttrName());
536+
return;
537+
}
538+
533539
if (isRelaxedIBAction(Ctx))
534540
// iOS, tvOS, and watchOS allow 0-2 parameters to an @IBAction method.
535541
validateIBActionSignature(Ctx, attr, FD, /*minParams=*/0, /*maxParams=*/2);

test/attr/attr_ibaction.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ class IBActionWrapperTy {
4141
func moreMagic(_: AnyObject) -> () {} // no-warning
4242
@objc @IBAction
4343
func evenMoreMagic(_: AnyObject) -> () {} // no-warning
44+
45+
@IBAction // expected-error {{@IBAction instance methods cannot be async}}
46+
func asyncIBAction(_: AnyObject) async -> () {}
4447
}
4548

4649
struct S { }

0 commit comments

Comments
 (0)