File: inherited.swift

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (283 lines) | stat: -rw-r--r-- 6,936 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
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
// RUN: %target-typecheck-verify-swift

// Inheritable: method with 'Self' in contravariant position.
protocol P1 {
  func f1a(_ x: Self?) -> Bool

  func f1b(_ x: [Self])

  func f1c(_ x: [String : Self])
}

// Inheritable: property with 'Self' in its signature.
protocol P2 {
  var prop2: Self { get set }
}
protocol P2a {
  var prop2a: Self { get set }
}

// Inheritable: subscript with 'Self' in its result type.
protocol P3 {
  subscript (i: Int) -> Self { get }
}

// Inheritable: subscript with 'Self' in its index type.
protocol P4 {
  subscript (s: Self) -> Int { get }
}

// Potentially inheritable: method returning Self
protocol P5 {
  func f5() -> Self
}
protocol P5a {
  func f5a() -> Self
}

// Inheritable: method returning Self
protocol P6 {
  func f6() -> Self
}

// Inheritable: method involving associated type.
protocol P7 {
  associatedtype Assoc
  func f7() -> Assoc
}

// Inheritable: initializer requirement.
protocol P8 {
  init(int: Int)
}

// Inheritable: operator requirement.
protocol P9 {
  static func ==(x: Self, y: Self) -> Bool 
}

// Never inheritable: method with 'Self' in invariant position.
struct G<T> {}
protocol P10 {
  func f10(_ arr: G<Self>)
}
protocol P10a {
  func f10a(_ arr: G<Self>)
}

// Never inheritable: method with 'Self' in curried position.
protocol P11 {
  func f11() -> (_ x: Self) -> Int
}

// Inheritable: parameter is a function returning 'Self'.
protocol P12 {
  func f12(_ s: () -> (Self, Self))
}

// Never inheritable: parameter is a function accepting 'Self'.
protocol P13 {
  func f13(_ s: (Self) -> ())
}

// Inheritable: parameter is a function accepting a function
// accepting 'Self'.
protocol P14 {
  func f14(_ s: ((Self) -> ()) -> ())
}

// Never inheritable: parameter is a function accepting a function
// returning 'Self'.
// Not Inheritable: method returning tuple containing 'Self'.
// Not Inheritable: method returning array of 'Self'.
// Not Inheritable: requirement with reference to covariant 'Self', if this
// reference is not the uncurried type, stripped of any optionality.
protocol P15 {
  func f15(_ s: (() -> Self) -> ())
  func f16() -> (Self, Self)
  func f17() -> Array<Self>
  func f18() -> (Never, Array<Self>)
}
extension P15 {
  func f18() -> (Never, Array<Self>) {} // expected-note {{'f18()' declared here}}
}

// Class A conforms to everything that can be conformed to by a
// non-final class.
class A : P1, P2, P3, P4, P5, P6, P7, P8, P9, P10 {
  // P1
  func f1a(_ x: A?) -> Bool { return true }

  func f1b(_ x: [A]) { }

  func f1c(_ x: [String : A]) { }

  // P2
  var prop2: A { // expected-error{{property 'prop2' in non-final class 'A' must specify type 'Self' to conform to protocol 'P2'}}
    get { return self }
    set {}
  }

  // P2a
  var prop2a: A { // expected-note {{'prop2a' declared here}}
    get { return self }
    set {}
  }

  // P3
  subscript (i: Int) -> A { // expected-error{{subscript 'subscript(_:)' in non-final class 'A' must return 'Self' to conform to protocol 'P3'}}
    get {
     return self
    }
  }

  // P4
  subscript (a: A) -> Int { 
    get {
      return 5
    }
  }

  // P5
  func f5() -> A { return self } // expected-error{{method 'f5()' in non-final class 'A' must return 'Self' to conform to protocol 'P5'}}

  // P5a
  func f5a() -> A { return self } // expected-note {{'f5a()' declared here}}

  // P6
  func f6() -> Self { return self }

  // P7
  func f7() -> Int { return 5 }

  // P8
  required init(int: Int) { }

  // P10
  func f10(_ arr: G<A>) { } // expected-error{{protocol 'P10' requirement 'f10' cannot be satisfied by a non-final class ('A') because it uses 'Self' in a non-parameter, non-result type position}}

  // P10a
  func f10a(_ arr: G<A>) { } // expected-note {{'f10a' declared here}}

  // P11
  func f11() -> (_ x: A) -> Int { return { x in 5 } }
}

extension A: P2a, P5a, P10a {}
// expected-error@-1 {{property 'prop2a' in non-final class 'A' must specify type 'Self' to conform to protocol 'P2a'}}
// expected-error@-2 {{method 'f5a()' in non-final class 'A' must return 'Self' to conform to protocol 'P5a'}}
// expected-error@-3 {{protocol 'P10a' requirement 'f10a' cannot be satisfied by a non-final class ('A') because it uses 'Self' in a non-parameter, non-result type position}}

// P9
func ==(x: A, y: A) -> Bool { return true }

// Class B inherits A; gets all of its conformances.
class B : A { 
  required init(int: Int) { }
}

func testB(_ b: B) {
  var _: any P1 = b
  var _: any P4 = b
  var _: any P5 = b
  var _: any P6 = b
  var _: any P7 = b
  var _: any P8 = b
  var _: any P9 = b
}

// Class A5 conforms to P5 in an inheritable manner.
class A5 : P5 {
  // P5 
  func f5() -> Self { return self }
}

// Class B5 inherits A5; gets all of its conformances.
class B5 : A5 { }

func testB5(_ b5: B5) {
  var _: P5 = b5 // okay
}

// Class A8 conforms to P8 in an inheritable manner.
class A8 : P8 {
  required init(int: Int) { }
}

class B8 : A8 {
  required init(int: Int) { }
}

func testB8(_ b8: B8) {
  var _: P8 = b8 // okay
}

// Class A9 conforms to everything.
final class A9 : P1, P2, P3, P4, P5, P6, P7, P8, P9, P10 {
  // P1
  func f1a(_ x: A9?) -> Bool { return true }

  func f1b(_ x: [A9]) { }

  func f1c(_ x: [String : A9]) { }

  // P2
  var prop2: A9 {
    get { return self }
    set {}
  }

  // P3
  subscript (i: Int) -> A9 {
    get {
     return self
    }
  }

  // P4
  subscript (a: A9) -> Int { 
    get {
      return 5
    }
  }

  // P5
  func f5() -> A9 { return self }

  // P6
  func f6() -> Self { return self }

  // P7
  func f7() -> Int { return 5 }

  // P8
  required init(int: Int) { }

  // P10
  func f10(_ arr: G<A9>) { }

  // P11
  func f11() -> (_ x: A9) -> Int { return { x in 5 } }
}

// P9
func ==(x: A9, y: A9) -> Bool { return true }

class A12 : P12 {
  func f12(_ s: () -> (A12, A12)) {}
}

class A13 : P13 {
  func f13(_ s: (A13) -> ()) {} // expected-error{{protocol 'P13' requirement 'f13' cannot be satisfied by a non-final class ('A13') because it uses 'Self' in a non-parameter, non-result type position}}
}

class A14 : P14 {
  func f14(_ s: ((A14) -> ()) -> ()) {}
}

class A15 : P15 { // expected-error{{protocol 'P15' requirement 'f18()' cannot be satisfied by a non-final class ('A15') because it uses 'Self' in a non-parameter, non-result type position}}
  func f15(_ s: (() -> A15) -> ()) {} // expected-error{{protocol 'P15' requirement 'f15' cannot be satisfied by a non-final class ('A15') because it uses 'Self' in a non-parameter, non-result type position}}
  func f16() -> (A15, A15) {} // expected-error{{protocol 'P15' requirement 'f16()' cannot be satisfied by a non-final class ('A15') because it uses 'Self' in a non-parameter, non-result type position}}
  func f17() -> Array<A15> {} // expected-error{{protocol 'P15' requirement 'f17()' cannot be satisfied by a non-final class ('A15') because it uses 'Self' in a non-parameter, non-result type position}}
}