File: attr_nonobjc.swift

package info (click to toggle)
swiftlang 6.1.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,791,532 kB
  • sloc: cpp: 9,901,743; ansic: 2,201,431; asm: 1,091,827; python: 308,252; objc: 82,166; f90: 80,126; lisp: 38,358; pascal: 25,559; sh: 20,429; ml: 5,058; perl: 4,745; makefile: 4,484; awk: 3,535; javascript: 3,018; xml: 918; fortran: 664; cs: 573; ruby: 396
file content (118 lines) | stat: -rw-r--r-- 3,949 bytes parent folder | download
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
// RUN: %target-typecheck-verify-swift
// REQUIRES: objc_interop

import Foundation

@objc class LightSaber {
  init() {
    caloriesBurned = 5
  }

  @objc func defeatEnemy(_ b: Bool) -> Bool { // expected-note {{'defeatEnemy' previously declared here}}
    return !b
  }

  // Make sure we can overload a method with @nonobjc methods
  @nonobjc func defeatEnemy(_ i: Int) -> Bool {
    return (i > 0)
  }

  // This is not allowed, though
  @objc func defeatEnemy(_ s: String) -> Bool { // expected-error {{method 'defeatEnemy' with Objective-C selector 'defeatEnemy:' conflicts with previous declaration with the same Objective-C selector}}
    return s != ""
  }

  @nonobjc subscript(index: Int) -> Int {
    return index
  }

  @nonobjc var caloriesBurned: Float
}

class BlueLightSaber : LightSaber {
  @nonobjc override func defeatEnemy(_ b: Bool) -> Bool { }
}

@objc class InchoateToad {
  @objc init(x: Int) {} // expected-note {{previously declared}}
  @nonobjc init(x: Float) {}
  @objc init(x: String) {} // expected-error {{conflicts with previous declaration with the same Objective-C selector}}
}

@nonobjc class NonObjCClassNotAllowed { } // expected-error {{'@nonobjc' attribute cannot be applied to this declaration}} {{1-10=}}

class NonObjCDeallocNotAllowed {
  @nonobjc deinit { // expected-error {{'@nonobjc' attribute cannot be applied to this declaration}} {{3-12=}}

  }
}

@objc protocol ObjCProtocol {
  func protocolMethod() // expected-note {{}}

  @nonobjc func nonObjCProtocolMethodNotAllowed() // expected-error {{declaration is a member of an @objc protocol, and cannot be marked @nonobjc}}

  @nonobjc subscript(index: Int) -> Int { get } // expected-error {{declaration is a member of an @objc protocol, and cannot be marked @nonobjc}}

  var surfaceArea: Float { @nonobjc get } // expected-error {{declaration is implicitly @objc, and cannot be marked @nonobjc}}

  var displacement: Float { get }
}

class SillyClass {
  @objc var description: String { @nonobjc get { return "" } } // expected-error {{declaration is implicitly @objc, and cannot be marked @nonobjc}}
}

class ObjCAndNonObjCNotAllowed {
  @objc @nonobjc func redundantAttributes() { } // expected-error {{declaration is marked @objc, and cannot be marked @nonobjc}}
}

class DynamicAndNonObjCAreFineNow {
  @nonobjc dynamic func someAttributes() { }
}

class IBOutletAndNonObjCNotAllowed {
  @nonobjc @IBOutlet var leeloo : String? = "Hello world" // expected-error {{declaration is marked @IBOutlet, and cannot be marked @nonobjc}}
}

class NSManagedAndNonObjCNotAllowed {
  @nonobjc @NSManaged var rosie : NSObject // expected-error {{declaration is marked @NSManaged, and cannot be marked @nonobjc}}
}

@nonobjc func nonObjCTopLevelFuncNotAllowed() { } // expected-error {{only class members and extensions of classes can be declared @nonobjc}} {{1-10=}}

@objc class NonObjCPropertyObjCProtocolNotAllowed : ObjCProtocol { // expected-error {{does not conform to protocol}} expected-note {{add stubs for conformance}}
  @nonobjc func protocolMethod() { } // expected-note {{candidate is explicitly '@nonobjc'}}

  func nonObjCProtocolMethodNotAllowed() { }

  subscript(index: Int) -> Int {
    return index
  }

  var displacement: Float {
    @nonobjc get { // expected-error {{declaration is implicitly @objc, and cannot be marked @nonobjc}}
      return Float(self[10])
    }
  }

  var surfaceArea: Float {
    get {
      return Float(100)
    }
  }
}

struct SomeStruct { }
@nonobjc extension SomeStruct { } // expected-error{{only extensions of classes can be declared @nonobjc}}

// https://github.com/apple/swift/issues/46809

protocol P_46809 : class {}
extension P_46809 {
  @nonobjc func function() {} // expected-error {{only class members and extensions of classes can be declared @nonobjc}}
}

@objc enum SomeEnum: Int {
  @nonobjc case what // expected-error {{'@nonobjc' attribute cannot be applied to this declaration}}
}