File: failable.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 (387 lines) | stat: -rw-r--r-- 13,262 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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
// Run in compatibility modes that disable and enable optional flattening
// in 'try?' to verify that initializer delegation diagnostics that are related
// to 'try?' are stable.
// RUN: %target-typecheck-verify-swift -swift-version 4
// RUN: %target-typecheck-verify-swift -swift-version 5

// REQUIRES: objc_interop
import Foundation

struct S0 {
  init!(int: Int) { }
  init! (uint: UInt) { }
  init !(float: Float) { }

  init?(string: String) { }
  init ?(double: Double) { }
  init ? (char: Character) { }
}

struct S1<T> {
  init?(value: T) { }
}

class DuplicateDecls {
  init!() { } // expected-note{{'init()' previously declared here}}
  init?() { } // expected-error{{invalid redeclaration of 'init()'}}

  init!(string: String) { } // expected-note{{'init(string:)' previously declared here}}
  init(string: String) { } // expected-error{{invalid redeclaration of 'init(string:)'}}

  init(double: Double) { } // expected-note{{'init(double:)' previously declared here}}
  init?(double: Double) { } // expected-error{{invalid redeclaration of 'init(double:)'}}
}

// Construct via a failable initializer.
func testConstruction(_ i: Int, s: String) {
  let s0Opt = S0(string: s)
  assert(s0Opt != nil)
  var _: S0 = s0Opt // expected-error{{value of optional type 'S0?' must be unwrapped}}
  // expected-note@-1{{coalesce}}
  // expected-note@-2{{force-unwrap}}
  
  let s0IUO = S0(int: i)
  assert(s0IUO != nil)
  
  _ = s0IUO
}

// ----------------------------------------------------------------------------
// Superclass initializer chaining
// ----------------------------------------------------------------------------
class Super {
  init?(fail: String) { }
  init!(failIUO: String) { }
  init() { } // expected-note 2{{non-failable initializer 'init()' overridden here}}
}

class Sub : Super {
  override init() { super.init() } // okay, never fails

  init(nonfail: Int) { // expected-note{{propagate the failure with 'init?'}}{{7-7=?}}
    super.init(fail: "boom") // expected-error{{a non-failable initializer cannot chain to failable initializer 'init(fail:)' written with 'init?'}}
    // expected-note@-1{{force potentially-failing result with '!'}}{{29-29=!}}
  }

  convenience init(forceNonfail: Int) {
    self.init(nonfail: forceNonfail)! // expected-error{{cannot force unwrap value of non-optional type}} {{37-38=}}
  }

  init(nonfail2: Int) { // okay, traps on nil
    super.init(failIUO: "boom")
  }

  init(nonfail3: Int) {
    super.init(fail: "boom")!
  }

  override init?(fail: String) {
    super.init(fail: fail) // okay, propagates ?
  }

  init?(fail2: String) { // okay, propagates ! as ?
    super.init(failIUO: fail2)
  }

  init?(fail3: String) { // okay, can introduce its own failure
    super.init()
  }

  override init!(failIUO: String) {
    super.init(failIUO: failIUO) // okay, propagates !
  }

  init!(failIUO2: String) { // okay, propagates ? as !
    super.init(fail: failIUO2)
  }

  init!(failIUO3: String) { // okay, can introduce its own failure
    super.init()
  }
}

// ----------------------------------------------------------------------------
// Initializer delegation
// ----------------------------------------------------------------------------
extension Super {
  convenience init(convenienceNonFailNonFail: String) { // okay, non-failable
    self.init()
  }

  convenience init(convenienceNonFailFail: String) { // expected-note{{propagate the failure with 'init?'}}{{19-19=?}}
    self.init(fail: convenienceNonFailFail) // expected-error{{a non-failable initializer cannot delegate to failable initializer 'init(fail:)' written with 'init?'}}
    // expected-note@-1{{force potentially-failing result with '!'}}{{44-44=!}}
  }

  convenience init(convenienceNonFailFailForce: String) {
    self.init(fail: convenienceNonFailFailForce)!
  }

  convenience init(convenienceNonFailFailIUO: String) { // okay, trap on failure
    self.init(failIUO: convenienceNonFailFailIUO) 
  }

  convenience init?(convenienceFailNonFail: String) { 
    self.init() // okay, can introduce its own failure
  }

  convenience init?(convenienceFailFail: String) {
    self.init(fail: convenienceFailFail) // okay, propagates ?
  }

  convenience init?(convenienceFailFailIUO: String) { // okay, propagates ! as ?
    self.init(failIUO: convenienceFailFailIUO) 
  }

  convenience init!(convenienceFailIUONonFail: String) { 
    self.init() // okay, can introduce its own failure
  }

  convenience init!(convenienceFailIUOFail: String) {
    self.init(fail: convenienceFailIUOFail) // okay, propagates ? as !
  }

  convenience init!(convenienceFailIUOFailIUO: String) { // okay, propagates !
    self.init(failIUO: convenienceFailIUOFailIUO) 
  }
}

struct SomeStruct {
  init?(failable: Void) {}
  init!(failableIUO: Void) {}
  init(throws: Void) throws {}
  init?(failableAndThrows: Void) throws {}
  init!(failableIUOAndThrows: Void) throws {}

  init(delegationOk1: Void) {
    self.init(failable: ())!
  }

  init(delegationOk2: Void) {
    try! self.init(throws: ())
  }

  init(delegationOk3: Void) {
    try! self.init(failableAndThrows: ())!
  }

  init(delegationOk4: Void) {
    try! self.init(failableIUOAndThrows: ())
  }

  init(nonFailable: Void) { // expected-note{{propagate the failure with 'init?'}}{{7-7=?}}
    self.init(failable: ()) // expected-error{{a non-failable initializer cannot delegate to failable initializer 'init(failable:)' written with 'init?'}}
    // expected-note@-1{{force potentially-failing result with '!'}}{{28-28=!}}
  }

  init(delegationBad1: Void) { // expected-note {{propagate the failure with 'init?'}}{{7-7=?}}
    try? self.init(nonFailable: ())
    // expected-warning@-1 {{no calls to throwing functions occur within 'try' expression}}
    // expected-error@-2 {{a non-failable initializer cannot use 'try?' to delegate to another initializer}}
    // expected-note@-3 {{force potentially-failing result with 'try!'}}{{5-9=try!}}
  }

  init(delegationBad2: Void) { // expected-note {{propagate the failure with 'init?'}}{{7-7=?}}
    try? self.init(failableIUO: ())
    // expected-warning@-1 {{no calls to throwing functions occur within 'try' expression}}
    // expected-error@-2 {{a non-failable initializer cannot use 'try?' to delegate to another initializer}}
    // expected-note@-3 {{force potentially-failing result with 'try!'}}{{5-9=try!}}
  }

  init(delegationBad3: Void) { // expected-note {{propagate the failure with 'init?'}}{{7-7=?}}
    try? self.init(throws: ())
    // expected-error@-1 {{a non-failable initializer cannot use 'try?' to delegate to another initializer}}
    // expected-note@-2 {{force potentially-failing result with 'try!'}}{{5-9=try!}}
  }

  init(delegationBad4: Void) { // expected-note {{propagate the failure with 'init?'}}{{7-7=?}}
    try! try? self.init(throws: ())
    // expected-warning@-1 {{no calls to throwing functions occur within 'try' expression}}
    // expected-error@-2 {{a non-failable initializer cannot use 'try?' to delegate to another initializer}}
    // expected-note@-3 {{force potentially-failing result with 'try!'}}{{10-14=try!}}
  }

  init(delegationBad5: Void) { // expected-note {{propagate the failure with 'init?'}}{{7-7=?}}
    try try? self.init(throws: ())
    // expected-warning@-1 {{no calls to throwing functions occur within 'try' expression}}
    // expected-error@-2 {{a non-failable initializer cannot use 'try?' to delegate to another initializer}}
    // expected-note@-3 {{force potentially-failing result with 'try!'}}{{9-13=try!}}
  }

  init(delegationBad6: Void) { // expected-note {{propagate the failure with 'init?'}}{{7-7=?}}
    try? self.init(failableAndThrows: ())!
    // expected-error@-1 {{a non-failable initializer cannot use 'try?' to delegate to another initializer}}
    // expected-note@-2 {{force potentially-failing result with 'try!'}}{{5-9=try!}}
  }

  init(delegationBad7: Void) { // expected-note {{propagate the failure with 'init?'}}{{7-7=?}}
    try! self.init(failableAndThrows: ())
    // expected-error@-1 {{a non-failable initializer cannot delegate to failable initializer 'init(failableAndThrows:)' written with 'init?'}}
    // expected-note@-2 {{force potentially-failing result with '!'}}{{42-42=!}}
  }

  init(delegationBad8: Void) { // expected-note {{propagate the failure with 'init?'}}{{7-7=?}}
    try? self.init(failableIUOAndThrows: ())
    // expected-error@-1 {{a non-failable initializer cannot use 'try?' to delegate to another initializer}}
    // expected-note@-2 {{force potentially-failing result with 'try!'}}{{5-9=try!}}
  }

  init(delegationBad9: Void) { // expected-note 2 {{propagate the failure with 'init?'}}{{7-7=?}}
    try? self.init(failableAndThrows: ())
    // expected-error@-1 {{a non-failable initializer cannot use 'try?' to delegate to another initializer}}
    // expected-error@-2 {{a non-failable initializer cannot delegate to failable initializer 'init(failableAndThrows:)' written with 'init?'}}
    // expected-note@-3 {{force potentially-failing result with '!'}}{{42-42=!}}
    // expected-note@-4 {{force potentially-failing result with 'try!'}}{{5-9=try!}}
  }
}

extension Optional {
  init(delegationOk1: Void) {
    self.init(nonFailable: ())
  }

  init?(delegationOk2: Void) {
    self.init(nonFailable: ())
  }

  init?(delegationOk3: Void) {
    self.init(failable: ())
  }

  init(delegationBad1: Void) { // expected-note {{propagate the failure with 'init?'}}{{7-7=?}}
    self.init(failable: ())
    // expected-error@-1 {{a non-failable initializer cannot delegate to failable initializer 'init(failable:)' written with 'init?'}}
    // expected-note@-2 {{force potentially-failing result with '!'}}{{28-28=!}}
  }

  init(nonFailable: Void) {}
  init?(failable: Void) {}
}

// ----------------------------------------------------------------------------
// Initializer overriding
// ----------------------------------------------------------------------------
class Sub2 : Super {
  override init!(fail: String) { // okay to change ? to !
    super.init(fail: fail) 
  }
  override init?(failIUO: String) { // okay to change ! to ?
    super.init(failIUO: failIUO)
  }

  override init() { super.init() } // no change
}

// Dropping optionality
class Sub3 : Super {
  override init(fail: String) { // okay, strengthened result type
    super.init()
  }

  override init(failIUO: String) { // okay, strengthened result type
    super.init()
  }

  override init() { } // no change
}

// Adding optionality
class Sub4 : Super {
  override init?(fail: String) { super.init() }
  override init!(failIUO: String) { super.init() }

  override init?() { // expected-error{{failable initializer 'init()' cannot override a non-failable initializer}}
    super.init()
  }
}

class Sub5 : Super {
  override init?(fail: String) { super.init() }
  override init!(failIUO: String) { super.init() }

  override init!() { // expected-error{{failable initializer 'init()' cannot override a non-failable initializer}}
    super.init()
  }
}

// ----------------------------------------------------------------------------
// Initializer conformances
// ----------------------------------------------------------------------------
protocol P1 {
  init(string: String)
}

@objc protocol P1_objc {
  init(string: String)
}

protocol P2 {
  init?(fail: String)
}

protocol P3 {
  init!(failIUO: String)
}

class C1a : P1 {
  required init?(string: String) { } // expected-error{{non-failable initializer requirement 'init(string:)' cannot be satisfied by a failable initializer ('init?')}}
}

class C1b : P1 {
  required init!(string: String) { } // okay
}

class C1b_objc : P1_objc {
  @objc required init!(string: String) { } // expected-error{{non-failable initializer requirement 'init(string:)' in Objective-C protocol cannot be satisfied by a failable initializer ('init!')}}
}

class C1c {
  required init?(string: String) { } // expected-note {{'init(string:)' declared here}}
}
extension C1c: P1 {} // expected-error{{non-failable initializer requirement 'init(string:)' cannot be satisfied by a failable initializer ('init?')}}

class C2a : P2 {
  required init(fail: String) { } // okay to remove failability
}

class C2b : P2 {
  required init?(fail: String) { } // okay, ? matches
}

class C2c : P2 {
  required init!(fail: String) { } // okay to satisfy init? with init!
}

class C3a : P3 {
  required init(failIUO: String) { } // okay to remove failability
}

class C3b : P3 {
  required init?(failIUO: String) { } // okay to satisfy ! with ?
}

class C3c : P3 {
  required init!(failIUO: String) { } // okay, ! matches
}


// ----------------------------------------------------------------------------
// Initiating failure
// ----------------------------------------------------------------------------
struct InitiateFailureS {
  init(string: String) { // expected-note{{use 'init?' to make the initializer 'init(string:)' failable}}{{7-7=?}}
    return (nil) // expected-error{{only a failable initializer can return 'nil'}}
  }

  init(int: Int) {
    return 0 // expected-error{{'nil' is the only return value permitted in an initializer}}
  }

  init?(double: Double) {
    return nil // ok
  }

  init!(char: Character) {
    return nil // ok
  }
}