|
| 1 | +// RUN: %target-typecheck-verify-swift |
| 2 | + |
| 3 | +func basic() { |
| 4 | + var i: Int! = 0 |
| 5 | + let _: Int = i |
| 6 | + i = 7 |
| 7 | +} |
| 8 | + |
| 9 | +func takesIUOs(i: Int!, j: inout Int!) -> Int { |
| 10 | + j = 7 |
| 11 | + return i |
| 12 | +} |
| 13 | + |
| 14 | +struct S { |
| 15 | + let i: Int! |
| 16 | + var j: Int! |
| 17 | + let k: Int |
| 18 | + var m: Int |
| 19 | + |
| 20 | + func fn() -> Int! { return i } |
| 21 | +} |
| 22 | + |
| 23 | +func takesStruct(s: S) { |
| 24 | + let _: Int = s.i |
| 25 | + let _: Int = s.j |
| 26 | + var t: S! = s |
| 27 | + t.j = 7 |
| 28 | +} |
| 29 | + |
| 30 | +var a: (Int, Int)! = (0, 0) |
| 31 | +a.0 = 42 |
| 32 | + |
| 33 | +var s: S! = S(i: nil, j: 1, k: 2, m: 3) |
| 34 | +_ = s.i |
| 35 | +let _: Int = s.j |
| 36 | +_ = s.k |
| 37 | +s.m = 7 |
| 38 | +s.j = 3 |
| 39 | + |
| 40 | +struct T { |
| 41 | + let i: Float! |
| 42 | + var j: Float! |
| 43 | + |
| 44 | + func fn() -> Float! { return i } |
| 45 | +} |
| 46 | + |
| 47 | +func overloaded() -> S { return S(i: 0, j: 1, k: 2, m: 3) } |
| 48 | +func overloaded() -> T { return T(i: 0.5, j: 1.5) } |
| 49 | + |
| 50 | +let _: Int = overloaded().i |
| 51 | + |
| 52 | +func cflow(i: Int!, j: inout Bool!, s: S) { |
| 53 | + let k: Int? = i |
| 54 | + let m: Int = i |
| 55 | + let b: Bool! = i == 0 |
| 56 | + |
| 57 | + if i == 7 { |
| 58 | + if s.i == 7 { |
| 59 | + } |
| 60 | + } |
| 61 | + let _ = i ? 7 : 0 // expected-error {{optional type 'Int!' cannot be used as a boolean; test for '!= nil' instead}} |
| 62 | + let _ = b ? i : k |
| 63 | + let _ = b ? i : m |
| 64 | + let _ = b ? j : b |
| 65 | + let _ = i ? i : k // expected-error {{result values in '? :' expression have mismatching types 'Int!' and 'Int?'}} |
| 66 | + let _ = i ? i : m // expected-error {{result values in '? :' expression have mismatching types 'Int!' and 'Int'}} |
| 67 | + let _ = s.i ? s.j : s.k // expected-error {{result values in '? :' expression have mismatching types 'Int!' and 'Int'}} |
| 68 | + let _ = b ? s.j : s.k |
| 69 | + |
| 70 | + if j {} |
| 71 | + let _ = j ? 7 : 0 |
| 72 | +} |
| 73 | + |
| 74 | +func forcedResultInt() -> Int! { |
| 75 | + return 0 |
| 76 | +} |
| 77 | + |
| 78 | +let _: Int = forcedResultInt() |
| 79 | + |
| 80 | +func forcedResult() -> Int! { |
| 81 | + return 0 |
| 82 | +} |
| 83 | + |
| 84 | +func forcedResult() -> Float! { |
| 85 | + return 0 |
| 86 | +} |
| 87 | + |
| 88 | +func overloadedForcedResult() -> Int { |
| 89 | + return forcedResult() |
| 90 | +} |
| 91 | + |
| 92 | +func forceMemberResult(s: S) -> Int { |
| 93 | + return s.fn() |
| 94 | +} |
| 95 | + |
| 96 | +func overloadedForceMemberResult() -> Int { |
| 97 | + return overloaded().fn() |
| 98 | +} |
| 99 | + |
| 100 | +func overloadedForcedStructResult() -> S! { return S(i: 0, j: 1, k: 2, m: 3) } |
| 101 | +func overloadedForcedStructResult() -> T! { return T(i: 0.5, j: 1.5) } |
| 102 | + |
| 103 | +let _: S = overloadedForcedStructResult() |
| 104 | +let _: Int = overloadedForcedStructResult().i |
| 105 | + |
| 106 | +let x: Int? = 1 |
| 107 | +let y0: Int = x as Int! // expected-warning {{using '!' in this location is deprecated and will be removed in a future release; consider changing this to '?' instead}} |
| 108 | +let y1: Int = (x as Int!)! // expected-warning {{using '!' in this location is deprecated and will be removed in a future release; consider changing this to '?' instead}} |
| 109 | +let z0: Int = x as! Int! // expected-warning {{forced cast from 'Int?' to 'Int!' always succeeds; did you mean to use 'as'?}} |
| 110 | +// expected-warning@-1 {{using '!' in this location is deprecated and will be removed in a future release; consider changing this to '?' instead}} |
| 111 | +let z1: Int = (x as! Int!)! // expected-warning {{forced cast from 'Int?' to 'Int!' always succeeds; did you mean to use 'as'?}} |
| 112 | +// expected-warning@-1 {{using '!' in this location is deprecated and will be removed in a future release; consider changing this to '?' instead}} |
| 113 | +let w0: Int = (x as? Int!)! // expected-warning {{conditional cast from 'Int?' to 'Int!' always succeeds}} |
| 114 | +// expected-warning@-1 {{using '!' in this location is deprecated and will be removed in a future release; consider changing this to '?' instead}} |
| 115 | +let w1: Int = (x as? Int!)!! // expected-warning {{conditional cast from 'Int?' to 'Int!' always succeeds}} |
| 116 | +// expected-warning@-1 {{using '!' in this location is deprecated and will be removed in a future release; consider changing this to '?' instead}} |
0 commit comments