1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
|
// RUN: %target-typecheck-verify-swift
// REQUIRES: objc_interop
@IBAction // expected-error {{@IBAction may only be used on 'func' declarations}} {{1-11=}}
var iboutlet_global: Int
var iboutlet_accessor: Int {
@IBAction // expected-error {{@IBAction may only be used on 'func' declarations}} {{3-13=}}
get { return 42 }
}
@IBAction // expected-error {{@IBAction may only be used on 'func' declarations}} {{1-11=}}
class IBOutletClassTy {}
@IBAction // expected-error {{@IBAction may only be used on 'func' declarations}} {{1-11=}}
struct IBStructTy {}
@IBAction // expected-error {{only instance methods can be declared @IBAction}} {{1-11=}}
func IBFunction() -> () {}
class IBActionWrapperTy {
@IBAction
func click(_: AnyObject) -> () {} // no-warning
func outer(_: AnyObject) -> () {
@IBAction // expected-error {{only instance methods can be declared @IBAction}} {{5-15=}}
func inner(_: AnyObject) -> () {}
}
@IBAction // expected-error {{@IBAction may only be used on 'func' declarations}} {{3-13=}}
var value : Void = ()
@IBAction
func process(x: AnyObject) -> Int {} // expected-error {{methods declared @IBAction must not return a value}}
// @IBAction does /not/ semantically imply @objc.
@IBAction // expected-note {{attribute already specified here}}
@IBAction // expected-error {{duplicate attribute}}
func doMagic(_: AnyObject) -> () {}
@IBAction @objc
func moreMagic(_: AnyObject) -> () {} // no-warning
@objc @IBAction
func evenMoreMagic(_: AnyObject) -> () {} // no-warning
@available(SwiftStdlib 5.5, *)
@IBAction
func asyncIBActionNoSpace(_: AnyObject) async -> () {}
// expected-error@-1 {{@IBAction instance method cannot be async}}
// expected-note@-2 {{remove 'async' and wrap in 'Task' to use concurrency in 'asyncIBActionNoSpace'}}{{45:3-47:57=@available(SwiftStdlib 5.5, *)\n @IBAction\n func asyncIBActionNoSpace(_: AnyObject) -> () {\nTask { @MainActor in \}\n\}}}
@available(SwiftStdlib 5.5, *)
@IBAction
func asyncIBActionWithFullBody(_: AnyObject) async {
print("Hello World")
}
// expected-error@-3 {{@IBAction instance method cannot be async}}
// expected-note@-4 {{remove 'async' and wrap in 'Task' to use concurrency in 'asyncIBActionWithFullBody'}}{{51:3-55:4=@available(SwiftStdlib 5.5, *)\n @IBAction\n func asyncIBActionWithFullBody(_: AnyObject) {\nTask { @MainActor in\n print("Hello World")\n \}\n\}}}
@available(SwiftStdlib 5.5, *) @IBAction func asyncIBActionNoBody(_: AnyObject) async
// expected-error@-1 {{expected '{' in body of function declaration}}
// expected-error@-2 {{@IBAction instance method cannot be asynchronous}}
// expected-note@-3 {{remove 'async' and wrap in 'Task' to use concurrency in 'asyncIBActionNoBody}}{{3-88=@available(SwiftStdlib 5.5, *) @IBAction func asyncIBActionNoBody(_: AnyObject)}}
}
struct S { }
enum E { }
protocol P1 { }
protocol P2 { }
protocol CP1 : class { }
protocol CP2 : class { }
@objc protocol OP1 { }
@objc protocol OP2 { }
// Teach the compiler that String is @objc-friendly without importing
// Foundation.
extension String: @retroactive _ObjectiveCBridgeable {
@_semantics("convertToObjectiveC") public func _bridgeToObjectiveC() -> AnyObject { fatalError() }
public static func _forceBridgeFromObjectiveC(_ x: AnyObject, result: inout String?) { fatalError() }
public static func _conditionallyBridgeFromObjectiveC(_ x: AnyObject, result: inout String?) -> Bool { fatalError() }
@_effects(readonly) public static func _unconditionallyBridgeFromObjectiveC(_ source: AnyObject? ) -> String { fatalError() }
}
// Check which argument types @IBAction can take.
@objc class X {
// Class type
@IBAction func action1(_: X) {}
@IBAction func action2(_: X?) {}
@IBAction func action3(_: X!) {}
// AnyObject
@IBAction func action4(_: AnyObject) {}
@IBAction func action5(_: AnyObject?) {}
@IBAction func action6(_: AnyObject!) {}
// Any
@IBAction func action4a(_: Any) {}
@IBAction func action5a(_: Any?) {}
@IBAction func action6a(_: Any!) {}
// Protocol types
@IBAction func action7(_: P1) {} // expected-error{{method cannot be marked @IBAction because the type of the parameter cannot be represented in Objective-C}} expected-note{{protocol-constrained type containing protocol 'P1' cannot be represented in Objective-C}}
@IBAction func action8(_: CP1) {} // expected-error{{method cannot be marked @IBAction because the type of the parameter cannot be represented in Objective-C}} expected-note{{protocol-constrained type containing protocol 'CP1' cannot be represented in Objective-C}}
@IBAction func action9(_: OP1) {}
@IBAction func action10(_: P1?) {} // expected-error{{method cannot be marked @IBAction because the type of the parameter cannot be represented in Objective-C}}
@IBAction func action11(_: CP1?) {} // expected-error{{method cannot be marked @IBAction because the type of the parameter cannot be represented in Objective-C}}
@IBAction func action12(_: OP1?) {}
@IBAction func action13(_: P1!) {} // expected-error{{method cannot be marked @IBAction because the type of the parameter cannot be represented in Objective-C}}
@IBAction func action14(_: CP1!) {} // expected-error{{method cannot be marked @IBAction because the type of the parameter cannot be represented in Objective-C}}
@IBAction func action15(_: OP1!) {}
// Class metatype
@IBAction func action15b(_: X.Type) {}
@IBAction func action16(_: X.Type?) {}
@IBAction func action17(_: X.Type!) {}
// AnyClass
@IBAction func action18(_: AnyClass) {}
@IBAction func action19(_: AnyClass?) {}
@IBAction func action20(_: AnyClass!) {}
// Protocol types
@IBAction func action21(_: P1.Type) {} // expected-error{{method cannot be marked @IBAction because the type of the parameter cannot be represented in Objective-C}}
@IBAction func action22(_: CP1.Type) {} // expected-error{{method cannot be marked @IBAction because the type of the parameter cannot be represented in Objective-C}}
@IBAction func action23(_: OP1.Type) {}
@IBAction func action24(_: P1.Type?) {} // expected-error{{method cannot be marked @IBAction because the type of the parameter cannot be represented in Objective-C}}
@IBAction func action25(_: CP1.Type?) {} // expected-error{{method cannot be marked @IBAction because the type of the parameter cannot be represented in Objective-C}}
@IBAction func action26(_: OP1.Type?) {}
@IBAction func action27(_: P1.Type!) {} // expected-error{{method cannot be marked @IBAction because the type of the parameter cannot be represented in Objective-C}}
@IBAction func action28(_: CP1.Type!) {} // expected-error{{method cannot be marked @IBAction because the type of the parameter cannot be represented in Objective-C}}
@IBAction func action29(_: OP1.Type!) {}
// Structs representable in Objective-C
@IBAction func action32(_: Int) {}
@IBAction func action33(_: Int?) {} // expected-error{{method cannot be marked @IBAction because the type of the parameter cannot be represented in Objective-C}}
@IBAction func action34(_: String) {}
@IBAction func action35(_: String?) {}
// Other bad cases
@IBAction func action30(_: S) {} // expected-error{{method cannot be marked @IBAction because the type of the parameter cannot be represented in Objective-C}} expected-note{{Swift structs cannot be represented in Objective-C}}
@IBAction func action31(_: E) {} // expected-error{{method cannot be marked @IBAction because the type of the parameter cannot be represented in Objective-C}} expected-note{{non-'@objc' enums cannot be represented in Objective-C}}
init() { }
}
|