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 (49 lines) | stat: -rw-r--r-- 2,594 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
// RUN: %target-typecheck-verify-swift

class Base : Hashable {
  func hash(into hasher: inout Hasher) {}
}

class Derived : Base { }

func ==(lhs: Base, rhs: Base) -> Bool { return false }

func ..<(x: Int?, y: Int) -> Int? { return x }

// Inputs that are more optional than the output.
func f1(i: Int?, ii: Int??, a: [Base]?, d: [Base : Base]?, de: Derived?) {
  _ = i as! Int // expected-warning{{forced cast from 'Int?' to 'Int' only unwraps optionals; did you mean to use '!'?}}{{8-8=!}}{{8-16=}}
  _ = ii as! Int // expected-warning{{forced cast from 'Int??' to 'Int' only unwraps optionals; did you mean to use '!!'?}}{{9-9=!!}}{{9-17=}}
  _ = a as! [Base] // expected-warning{{forced cast from '[Base]?' to '[Base]' only unwraps optionals; did you mean to use '!'?}}{{8-8=!}}{{8-19=}}
  _ = d as! [Base : Base] // expected-warning{{forced cast from '[Base : Base]?' to '[Base : Base]' only unwraps optionals; did you mean to use '!'?}}{{8-8=!}}{{8-26=}}
  _ = de as! Base // expected-warning{{forced cast from 'Derived?' to 'Base' only unwraps optionals; did you mean to use '!'?}}{{9-9=!}}{{9-18=}}

  // Conditional casts
  _ = i as? Int // expected-warning{{conditional downcast from 'Int?' to 'Int' does nothing}}{{8-16=}}
  _ = ii as? Int
  _ = a as? [Base] // expected-warning{{conditional downcast from '[Base]?' to '[Base]' does nothing}}{{8-19=}}
  _ = d as? [Base : Base] // expected-warning{{conditional downcast from '[Base : Base]?' to '[Base : Base]' does nothing}}{{8-26=}}
  _ = de as? Base // expected-warning{{conditional downcast from 'Derived?' to 'Base' is equivalent to an implicit conversion to an optional 'Base'}}{{9-18=}}

  // "is" checks
  _ = i is Int // expected-warning{{checking a value with optional type 'Int?' against type 'Int' succeeds whenever the value is non-nil; did you mean to use '!= nil'?}}{{9-15=!= nil}}
  _ = i..<1 is Int // expected-warning{{checking a value with optional type 'Int?' against type 'Int' succeeds whenever the value is non-nil; did you mean to use '!= nil'?}}{{7-7=(}}{{12-12=)}}{{13-19=!= nil}}
  _ = ii is Int

  _ = i..<1 is Bool // expected-warning{{cast from 'Int?' to unrelated type 'Bool' always fails}}
}

func implicitCastOfLiteralToOptional() {
  var _: Int? = 0
  var _: String? = ""
  var _: [Int] = []
  var _: [Int : Int]? = [:]
  var _: Set<Int> = []
}

// A nil value can be cast to any other optional type.
// https://github.com/apple/swift/issues/46093
func castUnrelatedOptionalTypes(x: Int?) {
  let _ = x as String? // expected-error {{cannot convert value}}
  let _ = x as? String? // no-warning
}