File: optional.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 (600 lines) | stat: -rw-r--r-- 20,479 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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
// RUN: %target-typecheck-verify-swift

// REQUIRES: objc_interop

func markUsed<T>(_ t: T) {}

class A {
  @objc func do_a() {}

  @objc(do_b_2:) func do_b(_ x: Int) {}
  @objc func do_b(_ x: Float) {}

  @objc func do_c(x: Int) {} // expected-note {{incorrect labels for candidate (have: '(_:)', expected: '(x:)')}}
  @objc func do_c(y: Int) {} // expected-note {{incorrect labels for candidate (have: '(_:)', expected: '(y:)')}}
}

func test0(_ a: AnyObject) {
  a.do_a?()

  a.do_b?(1)
  a.do_b?(5.0)

  a.do_c?(1) // expected-error {{no exact matches in call to instance method 'do_c'}}
  a.do_c?(x: 1)
}

func test1(_ a: A) {
  a?.do_a() // expected-error {{cannot use optional chaining on non-optional value of type 'A'}} {{4-5=}}
  a!.do_a() // expected-error {{cannot force unwrap value of non-optional type 'A'}} {{4-5=}}
  // Produce a specialized diagnostic here?
  a.do_a?() // expected-error {{cannot use optional chaining on non-optional value of type '() -> ()'}} {{9-10=}}
}

// <rdar://problem/15508756>
extension Optional {
  func bind<U>(_ f: (Wrapped) -> U?) -> U? {
    switch self {
    case .some(let x):
      return f(x)
    case .none:
      return .none
    }
  }
}

var c: String? = Optional<Int>(1)
  .bind {(x: Int) in markUsed("\(x)!"); return "two" }

func test4() {
  func foo() -> Int { return 0 }
  func takes_optfn(_ f : () -> Int?) -> Int? { return f() }

  _ = takes_optfn(foo)

  func takes_objoptfn(_ f : () -> AnyObject?) -> AnyObject? { return f() }
  func objFoo() -> AnyObject { return A() }
  _ = takes_objoptfn(objFoo) // okay
  func objBar() -> A { return A() }
  _ = takes_objoptfn(objBar) // okay
}

func test5() -> Int? {
  return nil
}

func test6<T>(_ x : T) {
  _ = x as? Int? // Okay.  We know nothing about T, so cannot judge.
}

class B : A { }

func test7(_ x : A) {
  _ = x as? B? // Okay: Injecting into an Optional
}

func test7a(_ x : B) {
  _ = x as? A // expected-warning{{conditional cast from 'B' to 'A' always succeeds}}
}

func test8(_ x : AnyObject?) {
  let _ : A = x as! A
}


// Partial ordering with optionals
func test9_helper<T: P>(_ x: T) -> Int { }
func test9_helper<T: P>(_ x: T?) -> Double { }

func test9_helper2<T>(_ x: T) -> Int { }
func test9_helper2<T>(_ x: T?) -> Double { }

func test9(_ i: Int, io: Int?) {
  let result = test9_helper(i)
  var _: Int = result
  let result2 = test9_helper(io)
  let _: Double = result2

  let result3 = test9_helper2(i)
  var _: Int = result3
  let result4 = test9_helper2(io)
  let _: Double = result4
}

protocol P { }

func test10_helper<T : P>(_ x: T) -> Int { }
func test10_helper<T : P>(_ x: T?) -> Double { }

extension Int : P { }

func test10(_ i: Int, io: Int?) {
  let result = test10_helper(i)
  var _: Int = result

  let result2 = test10_helper(io)
  var _: Double = result2
}

var z: Int? = nil
z = z ?? 3

var fo: Float? = 3.14159

func voidOptional(_ handler: () -> ()?) {}
func testVoidOptional() {
  let noop: () -> Void = {}
  voidOptional(noop)

  let optNoop: (()?) -> ()? = { return $0 }
  voidOptional(optNoop)
}

protocol Proto1 {}
protocol Proto2 {}
struct Nilable: ExpressibleByNilLiteral {
	init(nilLiteral: ()) {}
}
func testTernaryWithNil<T>(b: Bool, s: String, i: Int, a: Any, t: T, m: T.Type, p: Proto1 & Proto2, arr: [Int], opt: Int?, iou: Int!, n: Nilable) {
  let t1 = b ? s : nil
  let _: Double = t1 // expected-error{{value of type 'String?'}}
  let t2 = b ? nil : i
  let _: Double = t2 // expected-error{{value of type 'Int?'}}
  let t3 = b ? "hello" : nil
  let _: Double = t3 // expected-error{{value of type 'String?'}}
  let t4 = b ? nil : 1
  let _: Double = t4 // expected-error{{value of type 'Int?'}}
  let t5 = b ? (s, i) : nil
  let _: Double = t5 // expected-error{{value of type '(String, Int)?}}
  let t6 = b ? nil : (i, s)
  let _: Double = t6 // expected-error{{value of type '(Int, String)?}}
  let t7 = b ? ("hello", 1) : nil
  let _: Double = t7 // expected-error{{value of type '(String, Int)?}}
  let t8 = b ? nil : (1, "hello")
  let _: Double = t8 // expected-error{{value of type '(Int, String)?}}
  let t9 = b ? { $0 * 2 } : nil
  let _: Double = t9 // expected-error{{value of type '((Int) -> Int)?}}
  let t10 = b ? nil : { $0 * 2 }
  let _: Double = t10 // expected-error{{value of type '((Int) -> Int)?}}
  let t11 = b ? a : nil
  let _: Double = t11 // expected-error{{value of type 'Any?'}}
  let t12 = b ? nil : a
  let _: Double = t12 // expected-error{{value of type 'Any?'}}
  let t13 = b ? t : nil
  let _: Double = t13 // expected-error{{value of type 'T?'}}
  let t14 = b ? nil : t
  let _: Double = t14 // expected-error{{value of type 'T?'}}
  let t15 = b ? m : nil
  let _: Double = t15 // expected-error{{value of type 'T.Type?'}}
  let t16 = b ? nil : m
  let _: Double = t16 // expected-error{{value of type 'T.Type?'}}
  let t17 = b ? p : nil
  let _: Double = t17 // expected-error{{value of type '(any Proto1 & Proto2)?'}}
  let t18 = b ? nil : p
  let _: Double = t18 // expected-error{{value of type '(any Proto1 & Proto2)?'}}
  let t19 = b ? arr : nil
  let _: Double = t19 // expected-error{{value of type '[Int]?'}}
  let t20 = b ? nil : arr
  let _: Double = t20 // expected-error{{value of type '[Int]?'}}
  let t21 = b ? opt : nil
  let _: Double = t21 // expected-error{{value of type 'Int?'}}
  let t22 = b ? nil : opt
  let _: Double = t22 // expected-error{{value of type 'Int?'}}
  let t23 = b ? iou : nil
  let _: Double = t23 // expected-error{{value of type 'Int?'}}
  let t24 = b ? nil : iou
  let _: Double = t24 // expected-error{{value of type 'Int?'}}
  let t25 = b ? n : nil
  let _: Double = t25 // expected-error{{value of type 'Nilable'}}
  let t26 = b ? nil : n
  let _: Double = t26 // expected-error{{value of type 'Nilable'}}
}

// inference with IUOs
infix operator ++++

protocol PPPP {
  static func ++++(x: Self, y: Self) -> Bool
}

func compare<T: PPPP>(v: T, u: T!) -> Bool {
  return v ++++ u
}

// https://github.com/apple/swift/issues/45356
func f_45356(x: String?, y: String?) {
  _ = x.map { xx in
    y.map { _ in "" } ?? "\(xx)"
  }
}

// https://github.com/apple/swift/issues/45836
// Invalid diagnostic calling implicitly unwrapped closure
do {
  var x : ((Int) -> ())!
  x?(a: 2) // expected-error {{extraneous argument label 'a:' in call}}
  x!(a: 2) // expected-error {{extraneous argument label 'a:' in call}}
  x(a: 2)  // expected-error {{extraneous argument label 'a:' in call}}

  struct S {
    var callback: (([AnyObject]) -> Void)!
  }

  S().callback?("test") // expected-error {{cannot convert value of type 'String' to expected argument type '[AnyObject]'}}
  S().callback!("test") // expected-error {{cannot convert value of type 'String' to expected argument type '[AnyObject]'}}
  S().callback("test")  // expected-error {{cannot convert value of type 'String' to expected argument type '[AnyObject]'}}

  _? = nil  // expected-error {{'nil' requires a contextual type}}
  _?? = nil // expected-error {{'nil' requires a contextual type}}
}

// rdar://problem/29993596
func takeAnyObjects(_ lhs: AnyObject?, _ rhs: AnyObject?) { }

infix operator !====

func !====(_ lhs: AnyObject?, _ rhs: AnyObject?) -> Bool { return false }

func testAnyObjectImplicitForce(lhs: AnyObject?!, rhs: AnyObject?) {
  if lhs !==== rhs { }

  takeAnyObjects(lhs, rhs)
}

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

protocol P1 { }

class C1: P1 { }

protocol P2 {
    var prop: C1? { get }
}

class C2 {
    var p1: P1?
    var p2: P2?

    var computed: P1? {
        return p1 ?? p2?.prop
    }
}


// rdar://problem/31779785
class X { }

class Bar {
  let xOpt: X?
  let b: Bool

  init() {
    let result = b ? nil : xOpt
    let _: Int = result // expected-error{{cannot convert value of type 'X?' to specified type 'Int'}}
  }
}

// rdar://problem/37508855
func rdar37508855(_ e1: X?, _ e2: X?) -> [X] {
  return [e1, e2].filter { $0 == nil }.map { $0! }
}

func se0213() {
  struct Q: ExpressibleByStringLiteral {
    typealias StringLiteralType =  String

    var foo: String

    init?(_ possibleQ: StringLiteralType) {
      return nil
    }

    init(stringLiteral str: StringLiteralType) {
      self.foo = str
    }
  }

  _ = Q("why")?.foo // Ok
  _ = Q("who")!.foo // Ok
  _ = Q?("how") // Ok
}

func rdar45218255(_ i: Int) {
  struct S<T> {
    init(_:[T]) {}
  }

  _ = i!           // expected-error {{cannot force unwrap value of non-optional type 'Int'}} {{8-9=}}
  _ = [i!]         // expected-error {{cannot force unwrap value of non-optional type 'Int'}} {{9-10=}}
  _ = S<Int>([i!]) // expected-error {{cannot force unwrap value of non-optional type 'Int'}} {{16-17=}}
}

// rdar://problem/47967277
// https://github.com/apple/swift/issues/52299
// Cannot assign through '!': '$0' is immutable

func f1_52299() {
  func foo<T : Equatable>(_: @autoclosure () throws -> T,
                          _: @autoclosure () throws -> T) {}

  class A {
    var bar: String?
  }

  let r1 = A()
  let r2 = A()

  let arr1: [A] = []
  foo(Set(arr1.map { $0.bar! }), Set([r1, r2].map { $0.bar! })) // Ok
}

func f2_52299(cString: UnsafePointer<CChar>) {
  struct S {
    var a: Int32 = 0
    var b = ContiguousArray<CChar>(repeating: 0, count: 10)
  }

  var s = S()

  withUnsafeMutablePointer(to: &s.a) { ptrA in
    s.b.withUnsafeMutableBufferPointer { bufferB in
      withVaList([ptrA, bufferB.baseAddress!]) { ptr in } // Ok
    }
  }
}

// rdar://problem/47776586 - Diagnostic refers to '&' which is not present in the source code
func rdar47776586() {
  func foo(_: inout Int) {}
  var x: Int? = 0
  foo(&x) // expected-error {{value of optional type 'Int?' must be unwrapped to a value of type 'Int'}}
  // expected-note@-1 {{force-unwrap using '!' to abort execution if the optional value contains 'nil'}} {{7-7=(}} {{9-9=)!}}

  var dict = [1: 2]
  dict[1] += 1 // expected-error {{value of optional type 'Int?' must be unwrapped to a value of type 'Int'}}
  // expected-note@-1 {{force-unwrap using '!' to abort execution if the optional value contains 'nil'}} {{10-10=!}}
}

struct S {
  var foo: Optional<() -> Int?> = nil
  var bar: Optional<() -> Int?> = nil

  mutating func test(_ clj: @escaping () -> Int) {
    if let fn = foo {
      bar = fn  // Ok
      bar = clj // Ok
    }
  }
}

// rdar://problem/53238058 - Crash in getCalleeLocator while trying to produce a diagnostic about missing optional unwrap
//                           associated with an argument to a call

func rdar_53238058() {
  struct S {
    init(_: Double) {}
    init<T>(_ value: T) where T : BinaryFloatingPoint {}
  }

  func test(_ str: String) {
    _ = S(Double(str)) // expected-error {{value of optional type 'Double?' must be unwrapped to a value of type 'Double'}}
    // expected-note@-1 {{coalesce using '??' to provide a default when the optional value contains 'nil'}}
    // expected-note@-2 {{force-unwrap using '!' to abort execution if the optional value contains 'nil'}}
  }
}

// https://github.com/apple/swift/issues/50936
// Inconsistent ambiguity with optional and non-optional inout-to-pointer
do {
  struct S {
    init(_ x: UnsafeMutablePointer<Int>) {}
    init(_ x: UnsafeMutablePointer<Int>?) {}

    static func foo(_ x: UnsafeMutablePointer<Int>) {}
    static func foo(_ x: UnsafeMutablePointer<Int>?) {}

    static func bar(_ x: UnsafeMutablePointer<Int>, _ y: Int) {}
    static func bar(_ x: UnsafeMutablePointer<Int>?, _ y: Int) {}
  }

  var foo = 0

  _ = S(&foo)      // Ok
  _ = S.init(&foo) // Ok
  S.foo(&foo)  // Ok
  S.bar(&foo, 42) // Ok
}

// https://github.com/apple/swift/issues/53499
// Slightly misleading diagnostics for contextual failures with multiple fixes
do {
  func bar(_: Int) {}

  bar(["hello"].first)
  // expected-error@-1 {{cannot convert value of type 'String?' to expected argument type 'Int'}}
}

// rdar://problem/57668873 - Too eager force optional unwrap fix

@objc class Window {}

@objc protocol WindowDelegate {
  @objc optional var window: Window? { get set }
}

func test_force_unwrap_not_being_too_eager() {
  struct WindowContainer {
    unowned(unsafe) var delegate: WindowDelegate? = nil
  }

  let obj: WindowContainer = WindowContainer()
  if let _ = obj.delegate?.window { // Ok
  }
}

// rdar://problem/57097401
func invalidOptionalChaining(a: Any) {
  a == "="? // expected-error {{cannot use optional chaining on non-optional value of type 'String'}}
  // expected-error@-1 {{binary operator '==' cannot be applied to operands of type 'Any' and 'String?'}}
}

/// https://github.com/apple/swift/issues/54739
/// Force unwrapping `nil` compiles without warning
do {
  struct S {
    var foo: Int
  }

  _ = S(foo: nil!) // expected-error {{'nil' literal cannot be force unwrapped}}
  _ = nil! // expected-error {{'nil' literal cannot be force unwrapped}}
  _ = (nil!) // expected-error {{'nil' literal cannot be force unwrapped}}
  _ = (nil)! // expected-error {{'nil' literal cannot be force unwrapped}}
  _ = ((nil))! // expected-error {{'nil' literal cannot be force unwrapped}}
  _ = nil? // expected-error {{'nil' requires a contextual type}}
  _ = ((nil?)) // expected-error {{'nil' requires a contextual type}}
  _ = ((nil))? // expected-error {{'nil' requires a contextual type}}
  _ = ((nil)?) // expected-error {{'nil' requires a contextual type}}
  _ = nil // expected-error {{'nil' requires a contextual type}}
  _ = (nil) // expected-error {{'nil' requires a contextual type}}
  _ = ((nil)) // expected-error {{'nil' requires a contextual type}}
  _ = (((nil))) // expected-error {{'nil' requires a contextual type}}
  _ = ((((((nil)))))) // expected-error {{'nil' requires a contextual type}}
  _ = (((((((((nil))))))))) // expected-error {{'nil' requires a contextual type}}

  func test_with_contextual_type_one() -> Int? {
    return (nil) // Ok
  }

  func test_with_contextual_type_many() -> Int? {
    return (((nil))) // Ok
  }
}

// rdar://75146811 - crash due to incorrect inout type
func rdar75146811() {
  func test(_: UnsafeMutablePointer<Double>) {}
  func test_tuple(_: UnsafeMutablePointer<Double>, x: Int) {}
  func test_named(x: UnsafeMutablePointer<Double>) {}

  var arr: [Double]! = []

  test(&arr) // expected-error {{cannot convert value of type '[Double]?' to expected argument type 'Double'}}
  test((&arr)) // expected-error {{'&' may only be used to pass an argument to inout parameter}}
  // expected-error@-1 {{cannot convert value of type '[Double]?' to expected argument type 'Double'}}
  test(&(arr)) // expected-error {{cannot convert value of type '[Double]?' to expected argument type 'Double'}}

  test_tuple(&arr, x: 0) // expected-error {{cannot convert value of type '[Double]?' to expected argument type 'Double'}}
  test_tuple((&arr), x: 0) // expected-error {{'&' may only be used to pass an argument to inout parameter}}
  // expected-error@-1 {{cannot convert value of type '[Double]?' to expected argument type 'Double'}}
  test_tuple(&(arr), x: 0) // expected-error {{cannot convert value of type '[Double]?' to expected argument type 'Double'}}

  test_named(x: &arr) // expected-error {{cannot convert value of type '[Double]?' to expected argument type 'Double'}}
  test_named(x: (&arr)) // expected-error {{'&' may only be used to pass an argument to inout parameter}}
  // expected-error@-1 {{cannot convert value of type '[Double]?' to expected argument type 'Double'}}
  test_named(x: &(arr)) // expected-error {{cannot convert value of type '[Double]?' to expected argument type 'Double'}}
}

// rdar://75514153 - Unable to produce a diagnostic for ambiguities related to use of `nil`
func rdar75514153() {
  func test_no_label(_: Int) {}    // expected-note 2 {{'nil' is not compatible with expected argument type 'Int' at position #1}}
  func test_no_label(_: String) {} // expected-note 2 {{'nil' is not compatible with expected argument type 'String' at position #1}}

  test_no_label(nil)   // expected-error {{no exact matches in call to local function 'test_no_label'}}
  test_no_label((nil)) // expected-error {{no exact matches in call to local function 'test_no_label'}}

  func test_labeled(_: Int, x: Int) {}    // expected-note 2 {{'nil' is not compatible with expected argument type 'Int' at position #2}}
  func test_labeled(_: Int, x: String) {} // expected-note 2 {{'nil' is not compatible with expected argument type 'String' at position #2}}

  test_labeled(42, x: nil)   // expected-error {{no exact matches in call to local function 'test_labeled'}}
  test_labeled(42, x: (nil)) // expected-error {{no exact matches in call to local function 'test_labeled'}}
}

// rdar://85166519 - Crash dereferencing Null Type In Bogus Expression
func rdar85166519() {
  var v: Int? = nil

  var _: [Int: AnyObject] = [ // expected-error {{dictionary of type '[Int : AnyObject]' cannot be initialized with array literal}}
    // expected-note@-1 {{did you mean to use a dictionary literal instead?}}
    v?.addingReportingOverflow(0)
  ]
}

// https://github.com/apple/swift/issues/58539
if let x = nil {} // expected-error{{'nil' requires a contextual type}}

// https://github.com/apple/swift/issues/56699
let singleOptionalClosure: (() -> Void)? = nil
let doubleOptionalClosure: (() -> Void)?? = nil
singleOptionalClosure()
// expected-error@-1 {{value of optional type}}
// expected-note@-2 {{use optional chaining to call this value of function type when optional is non-'nil'}} {{22-22=?}}
// expected-note@-3 {{coalesce}}
// expected-note@-4 {{force-unwrap}}

doubleOptionalClosure()
// expected-error@-1 {{value of optional type}}
// expected-note@-2 {{use optional chaining to call this value of function type when optional is non-'nil'}} {{22-22=??}}
// expected-note@-3 {{coalesce}}
// expected-note@-4 {{force-unwrap}}

doubleOptionalClosure?()
// expected-error@-1 {{value of optional type}}
// expected-note@-2 {{use optional chaining to call this value of function type when optional is non-'nil'}} {{23-23=?}}
// expected-note@-3 {{coalesce}}
// expected-note@-4 {{force-unwrap}}

doubleOptionalClosure!()
// expected-error@-1 {{value of optional type}}
// expected-note@-2 {{use optional chaining to call this value of function type when optional is non-'nil'}} {{23-23=?}}
// expected-note@-3 {{coalesce}}
// expected-note@-4 {{force-unwrap}}

struct FunctionContainer {
  func test() {}
}

func testFunctionContainerMethodCall(container: FunctionContainer?) {
  let fn = container?.test
   // expected-note@-1 {{short-circuit}}
   // expected-note@-2 {{coalesce}}
   // expected-note@-3 {{force-unwrap}}
  fn()
  // expected-error@-1 {{value of optional type}}
  // expected-note@-2 {{use optional chaining to call this value of function type when optional is non-'nil'}} {{5-5=?}}
  // expected-note@-3 {{coalesce}}
  // expected-note@-4 {{force-unwrap}}
}

// Test for https://github.com/apple/swift/issues/60730
// rdar://94037733
do {
  struct S: P {}
  func takesP(_: any P) {}
  func passOptional(value: (any P)?) {
    takesP(value)
    // expected-error@-1 {{value of optional type '(any P)?' must be unwrapped to a value of type 'any P'}}
    // expected-note@-2 {{coalesce using '??' to provide a default when the optional value contains 'nil'}}
    // expected-note@-3 {{force-unwrap using '!' to abort execution if the optional value contains 'nil'}}
  }
  func passLayersOfOptional(value: (any P)??) {
    // FIXME(diagnostics): Consider recording multiple ForceUnwrap fixes based on number of optionals 
    takesP(value)
    // expected-error@-1 {{value of optional type '(any P)??' must be unwrapped to a value of type '(any P)?}}
    // expected-note@-2 {{coalesce using '??' to provide a default when the optional value contains 'nil'}}
    // expected-note@-3 {{force-unwrap using '!' to abort execution if the optional value contains 'nil'}}
  }
  func passNonConformingValue(value: (any BinaryInteger)?){
    takesP(value)
    // expected-error@-1 {{argument type '(any BinaryInteger)?' does not conform to expected type 'P'}}
  }
}

// Diagnose extraneous force unwrap in ambiguous context
do {
  func test(_: Int) {} // expected-note {{candidate expects value of type 'Int' for parameter #1 (got 'Double')}}
  func test(_: String) {} // expected-note {{candidate expects value of type 'String' for parameter #1 (got 'Double')}}

  var x: Double = 42
  test(x!) // expected-error {{no exact matches in call to local function 'test'}}
  // expected-error@-1 {{cannot force unwrap value of non-optional type 'Double'}}
}