Skip to content

Commit 381a12d

Browse files
committed
[Sema] @IBAction implies @objc.
Per the core team discussion, @IBAction is tied to the Objective-C runtime and should imply @objc. This was an oversight when the attribute was added pre-1.0. Fixes rdar://problem/24750631.
1 parent 6099dfb commit 381a12d

File tree

5 files changed

+37
-3
lines changed

5 files changed

+37
-3
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2607,7 +2607,7 @@ ERROR(objc_enum_case_multi,none,
26072607
"'@objc' enum case declaration defines multiple enum cases with the same Objective-C name", ())
26082608

26092609
// If you change this, also change enum ObjCReason
2610-
#define OBJC_ATTR_SELECT "select{marked @_cdecl|marked dynamic|marked @objc|marked @IBOutlet|marked @NSManaged|a member of an @objc protocol|implicitly @objc|an @objc override|an implementation of an @objc requirement}"
2610+
#define OBJC_ATTR_SELECT "select{marked @_cdecl|marked dynamic|marked @objc|marked @IBOutlet|marked @IBAction|marked @NSManaged|a member of an @objc protocol|implicitly @objc|an @objc override|an implementation of an @objc requirement}"
26112611

26122612
ERROR(objc_invalid_on_var,none,
26132613
"property cannot be %" OBJC_ATTR_SELECT "0 "

lib/Sema/TypeCheckDecl.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2035,11 +2035,13 @@ static Optional<ObjCReason> shouldMarkAsObjC(TypeChecker &TC,
20352035
// explicitly declared @objc.
20362036
if (VD->getAttrs().hasAttribute<ObjCAttr>())
20372037
return ObjCReason::ExplicitlyObjC;
2038-
// dynamic, @IBOutlet and @NSManaged imply @objc.
2038+
// dynamic, @IBOutlet, @IBAction, and @NSManaged imply @objc.
20392039
else if (VD->getAttrs().hasAttribute<DynamicAttr>())
20402040
return ObjCReason::ExplicitlyDynamic;
20412041
else if (VD->getAttrs().hasAttribute<IBOutletAttr>())
20422042
return ObjCReason::ExplicitlyIBOutlet;
2043+
else if (VD->getAttrs().hasAttribute<IBActionAttr>())
2044+
return ObjCReason::ExplicitlyIBAction;
20432045
else if (VD->getAttrs().hasAttribute<NSManagedAttr>())
20442046
return ObjCReason::ExplicitlyNSManaged;
20452047
// A member of an @objc protocol is implicitly @objc.

lib/Sema/TypeChecker.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,7 @@ enum class ObjCReason {
404404
ExplicitlyDynamic,
405405
ExplicitlyObjC,
406406
ExplicitlyIBOutlet,
407+
ExplicitlyIBAction,
407408
ExplicitlyNSManaged,
408409
MemberOfObjCProtocol,
409410
ImplicitlyObjC,

test/attr/attr_ibaction.swift

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,21 @@ protocol CP2 : class { }
6565

6666
// Protocol types
6767
@IBAction func action7(_: P1) {} // expected-error{{argument to @IBAction method cannot have non-object type 'P1'}}
68+
// expected-error@-1{{method cannot be marked @IBAction because the type of the parameter cannot be represented in Objective-C}}
69+
// expected-note@-2{{protocol 'P1' is not '@objc'}}
6870
@IBAction func action8(_: CP1) {} // expected-error{{argument to @IBAction method cannot have non-object type 'CP1'}}
71+
// expected-error@-1{{method cannot be marked @IBAction because the type of the parameter cannot be represented in Objective-C}}
72+
// expected-note@-2{{protocol 'CP1' is not '@objc'}}
6973
@IBAction func action9(_: OP1) {}
7074
@IBAction func action10(_: P1?) {} // expected-error{{argument to @IBAction method cannot have non-object type}}
75+
// expected-error@-1{{method cannot be marked @IBAction because the type of the parameter cannot be represented in Objective-C}}
7176
@IBAction func action11(_: CP1?) {} // expected-error{{argument to @IBAction method cannot have non-object type}}
77+
// expected-error@-1{{method cannot be marked @IBAction because the type of the parameter cannot be represented in Objective-C}}
7278
@IBAction func action12(_: OP1?) {}
7379
@IBAction func action13(_: P1!) {} // expected-error{{argument to @IBAction method cannot have non-object type}}
80+
// expected-error@-1{{method cannot be marked @IBAction because the type of the parameter cannot be represented in Objective-C}}
7481
@IBAction func action14(_: CP1!) {} // expected-error{{argument to @IBAction method cannot have non-object type}}
82+
// expected-error@-1{{method cannot be marked @IBAction because the type of the parameter cannot be represented in Objective-C}}
7583
@IBAction func action15(_: OP1!) {}
7684

7785
// Class metatype
@@ -86,18 +94,27 @@ protocol CP2 : class { }
8694

8795
// Protocol types
8896
@IBAction func action21(_: P1.Type) {} // expected-error{{argument to @IBAction method cannot have non-object type}}
97+
// expected-error@-1{{method cannot be marked @IBAction because the type of the parameter cannot be represented in Objective-C}}
8998
@IBAction func action22(_: CP1.Type) {} // expected-error{{argument to @IBAction method cannot have non-object type}}
99+
// expected-error@-1{{method cannot be marked @IBAction because the type of the parameter cannot be represented in Objective-C}}
90100
@IBAction func action23(_: OP1.Type) {} // expected-error{{argument to @IBAction method cannot have non-object type}}
91101
@IBAction func action24(_: P1.Type?) {} // expected-error{{argument to @IBAction method cannot have non-object type}}
102+
// expected-error@-1{{method cannot be marked @IBAction because the type of the parameter cannot be represented in Objective-C}}
92103
@IBAction func action25(_: CP1.Type?) {} // expected-error{{argument to @IBAction method cannot have non-object type}}
104+
// expected-error@-1{{method cannot be marked @IBAction because the type of the parameter cannot be represented in Objective-C}}
93105
@IBAction func action26(_: OP1.Type?) {} // expected-error{{argument to @IBAction method cannot have non-object type}}
94106
@IBAction func action27(_: P1.Type!) {} // expected-error{{argument to @IBAction method cannot have non-object type}}
107+
//expected-error@-1{{method cannot be marked @IBAction because the type of the parameter cannot be represented in Objective-C}}
95108
@IBAction func action28(_: CP1.Type!) {} // expected-error{{argument to @IBAction method cannot have non-object type}}
109+
//expected-error@-1{{method cannot be marked @IBAction because the type of the parameter cannot be represented in Objective-C}}
96110
@IBAction func action29(_: OP1.Type!) {} // expected-error{{argument to @IBAction method cannot have non-object type}}
97111

98112
// Other bad cases
99113
@IBAction func action30(_: S) {} // expected-error{{argument to @IBAction method cannot have non-object type}}
114+
// expected-error@-1{{method cannot be marked @IBAction because the type of the parameter cannot be represented in Objective-C}}
115+
// expected-note@-2{{Swift structs cannot be represented in Objective-C}}
100116
@IBAction func action31(_: E) {} // expected-error{{argument to @IBAction method cannot have non-object type}}
101-
117+
// expected-error@-1{{method cannot be marked @IBAction because the type of the parameter cannot be represented in Objective-C}}
118+
// expected-note@-2{{non-'@objc' enums cannot be represented in Objective-C}}
102119
init() { }
103120
}

test/attr/attr_objc.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1606,6 +1606,20 @@ class HasIBOutlet {
16061606
// CHECK-LABEL: {{^}} @IBOutlet var badOutlet: PlainStruct
16071607
}
16081608

1609+
//===---
1610+
//===--- @IBAction implies @objc
1611+
//===---
1612+
1613+
// CHECK-LABEL: {{^}}class HasIBAction {
1614+
class HasIBAction {
1615+
@IBAction func goodAction(_ sender: AnyObject?) { }
1616+
// CHECK: {{^}} @IBAction @objc func goodAction(_ sender: AnyObject?) {
1617+
1618+
@IBAction func badAction(_ sender: PlainStruct?) { }
1619+
// expected-error@-1{{argument to @IBAction method cannot have non-object type 'PlainStruct?'}}
1620+
// expected-error@-2{{method cannot be marked @IBAction because the type of the parameter cannot be represented in Objective-C}}
1621+
}
1622+
16091623
//===---
16101624
//===--- @NSManaged implies @objc
16111625
//===---

0 commit comments

Comments
 (0)