|
| 1 | +struct GuardError: Error { |
| 2 | + let msg: String |
| 3 | +} |
| 4 | + |
| 5 | +class C {} |
| 6 | + |
| 7 | +func chk() { } |
| 8 | + |
| 9 | +func g(_ lbl: Any) -> Bool { return "\(lbl)".count > 10 } |
| 10 | + |
| 11 | +func checkTrue(_ b: Bool, _ msg: String) throws { |
| 12 | + if !b { throw GuardError(msg: msg) } |
| 13 | +} |
| 14 | + |
| 15 | +func checkFalse(_ b: Bool, _ msg: String) throws { |
| 16 | + try checkTrue(!b, msg) |
| 17 | +} |
| 18 | + |
| 19 | +func t1(_ a: [Int]?, _ s0: String) { |
| 20 | + var s = s0 |
| 21 | + if g("A") { |
| 22 | + chk() // $ guarded=g(A):true |
| 23 | + } else { |
| 24 | + chk() // $ guarded=g(A):false |
| 25 | + } |
| 26 | + |
| 27 | + let b = g(1) ? g(2) : true |
| 28 | + if b != false { |
| 29 | + chk() // $ guarded=IfExpr:true guarded='b != false:true' guarded=b:true |
| 30 | + } else { |
| 31 | + chk() // $ guarded=IfExpr:false guarded='b != false:false' guarded=b:false guarded=g(1):true guarded=g(2):false |
| 32 | + } |
| 33 | + let sz = a != nil ? a!.count : 0 |
| 34 | + var i = 0 |
| 35 | + while i < sz { |
| 36 | + chk() // $ guarded='i < sz:true' MISSING: guarded='a != nil:true' guarded='sz:Lower bound 1' guarded='IfExpr:Lower bound 1' guarded='... .count:Lower bound 1' guarded='a:not null' |
| 37 | + let e = a![i] |
| 38 | + if e > 2 { break } |
| 39 | + i += 1 |
| 40 | + } |
| 41 | + chk() // nothing guards here |
| 42 | + |
| 43 | + if g(3) { |
| 44 | + s = "bar" |
| 45 | + } |
| 46 | + switch s { |
| 47 | + case "bar": |
| 48 | + chk() // $ guarded='"bar":match' guarded='s:bar' |
| 49 | + case "foo": |
| 50 | + chk() // $ guarded='"bar":no match' guarded='s:not bar' guarded='s0:not bar' guarded='"foo":match' guarded='s:foo' guarded='s0:foo' guarded=g(3):false |
| 51 | + default: |
| 52 | + chk() // $ guarded='"bar":no match' guarded='"foo":no match' guarded='s:not bar' guarded='s:not foo' guarded='s0:not bar' guarded=g(3):false |
| 53 | + } |
| 54 | + |
| 55 | + let o: Any? = g(4) ? nil : s |
| 56 | + if o is String { |
| 57 | + chk() // $ guarded=TypeTestExpr:true guarded='o:not null' guarded='IfExpr:not null' guarded=g(4):false guarded='s:not null' |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +func t2() throws { |
| 62 | + try checkTrue(g(1), "A") |
| 63 | + try checkFalse(g(2), "B") |
| 64 | + chk() // $ guarded='checkTrue(...):no exception' guarded='checkFalse(...):no exception' guarded=g(1):true guarded=g(2):false SPURIOUS: guarded='g(...):no exception' |
| 65 | +} |
| 66 | + |
| 67 | +func t3() { |
| 68 | + var b = g(1) && (g(2) || g(3)) |
| 69 | + if b { |
| 70 | + chk() // $ guarded=b:true guarded='g(...) && ... \|\| ...:true' guarded=g(1):true guarded='g(...) \|\| g(...):true' |
| 71 | + } else { |
| 72 | + chk() // $ guarded=b:false guarded='g(...) && ... \|\| ...:false' |
| 73 | + } |
| 74 | + b = g(4) || !g(5) |
| 75 | + if b { |
| 76 | + chk() // $ guarded=b:true guarded='g(...) \|\| ! ...:true' |
| 77 | + } else { |
| 78 | + chk() // $ guarded=b:false guarded='g(...) \|\| ! ...:false' guarded=g(4):false guarded='! ...:false' guarded=g(5):true |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +enum Val { |
| 83 | + case E1 |
| 84 | + case E2 |
| 85 | + case E3 |
| 86 | +} |
| 87 | + |
| 88 | +func t4() { |
| 89 | + var x: Val? = nil // unique value |
| 90 | + if g(1) { x = Val.E1 } // unique value |
| 91 | + if g(2) { x = Val.E2 } |
| 92 | + if g("Alt2") { x = Val.E2 } |
| 93 | + if g(3) { x = Val.E3 } // unique value |
| 94 | + if x == nil { |
| 95 | + chk() // $ guarded='x == nil:true' guarded='x:null' MISSING: guarded=g(1):false guarded=g(2):false guarded=g(Alt2):false guarded=g(3):false |
| 96 | + } |
| 97 | + switch x { |
| 98 | + case .E1: |
| 99 | + chk() // $ guarded='... .E1:match' MISSING: guarded='x:E1' guarded=g(1):true guarded=g(2):false guarded=g(Alt2):false guarded=g(3):false |
| 100 | + case .E2: |
| 101 | + chk() // $ guarded='... .E1:no match' guarded='... .E2:match' MISSING: guarded='x:not E1' guarded='x:E2' guarded=g(3):false |
| 102 | + case .E3: |
| 103 | + chk() // $ guarded='... .E1:no match' guarded='... .E2:no match' guarded='... .E3:match' MISSING: guarded='x:not E1' guarded='x:not E2' guarded='x:E3' guarded=g(3):true |
| 104 | + case nil: |
| 105 | + break |
| 106 | + } |
| 107 | + let o: C? = g(4) ? C() : nil |
| 108 | + if o == nil { |
| 109 | + chk() // $ guarded='o == nil:true' guarded='o:null' guarded='IfExpr:null' guarded=g(4):false |
| 110 | + } else { |
| 111 | + chk() // $ guarded='o == nil:false' guarded='o:not null' guarded='IfExpr:not null' guarded=g(4):true |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +func t5(_ foo: String?) { |
| 116 | + var base = foo |
| 117 | + if base == nil { |
| 118 | + base = "/user" |
| 119 | + } |
| 120 | + if base == "/" { |
| 121 | + chk() // $ guarded='base == "/":true' guarded='base:/' guarded='base:not null' guarded='base == nil:false' guarded='foo:/' guarded='foo:not null' |
| 122 | + } |
| 123 | +} |
| 124 | + |
| 125 | +func t6() { |
| 126 | + var o: C? = nil |
| 127 | + if g(1) { |
| 128 | + o = C() |
| 129 | + if g(2) { } |
| 130 | + } |
| 131 | + if o != nil { |
| 132 | + chk() // $ guarded='o != nil:true' guarded='o:not null' guarded=g(1):true |
| 133 | + } else { |
| 134 | + chk() // $ guarded='o != nil:false' guarded='o:null' guarded=g(1):false |
| 135 | + } |
| 136 | +} |
| 137 | + |
| 138 | +func t7(_ a: [Int]) { |
| 139 | + var found = false |
| 140 | + var i = 0 |
| 141 | + while i < a.count { |
| 142 | + let answer = a[i] == 42 |
| 143 | + if answer { |
| 144 | + found = true |
| 145 | + } |
| 146 | + if found { |
| 147 | + chk() // $ guarded=found:true guarded='i < ... .count:true' MISSING: guarded='... .count:Lower bound 1' |
| 148 | + } |
| 149 | + i += 1 |
| 150 | + } |
| 151 | + if found { |
| 152 | + chk() // $ guarded=found:true guarded='i < ... .count:false' MISSING: guarded='i:Lower bound 0' |
| 153 | + } |
| 154 | +} |
| 155 | + |
| 156 | +func testNotNull1(_ input: String?) -> Bool { |
| 157 | + return input != nil && input!.count > 0 |
| 158 | +} |
| 159 | + |
| 160 | +func testNotNull2(_ input: String?) -> Bool { |
| 161 | + if input == nil { return false } |
| 162 | + return input!.count > 0 |
| 163 | +} |
| 164 | + |
| 165 | +func getNumOrDefault(_ number: Int?) -> Int { |
| 166 | + return number == nil ? 0 : number! |
| 167 | +} |
| 168 | + |
| 169 | +func concatNonNull(_ s1: String?, _ s2: String?) -> String? { |
| 170 | + if s1 == nil || s2 == nil { return nil } |
| 171 | + return s1! + s2! |
| 172 | +} |
| 173 | + |
| 174 | +func testEnumWrapper(_ flag: Bool) -> Status { |
| 175 | + return flag ? Status.SUCCESS : Status.FAILURE |
| 176 | +} |
| 177 | + |
| 178 | +enum Status { case SUCCESS, FAILURE } |
| 179 | + |
| 180 | +func testWrappers(_ s: String?, _ i: Int?) { |
| 181 | + if testNotNull1(s) { |
| 182 | + chk() // $ guarded=testNotNull1(...):true guarded='s:not null' |
| 183 | + } else { |
| 184 | + chk() // $ guarded=testNotNull1(...):false |
| 185 | + } |
| 186 | + |
| 187 | + if testNotNull2(s) { |
| 188 | + chk() // $ guarded=testNotNull2(...):true guarded='s:not null' |
| 189 | + } else { |
| 190 | + chk() // $ guarded=testNotNull2(...):false |
| 191 | + } |
| 192 | + |
| 193 | + if 0 == getNumOrDefault(i) { |
| 194 | + chk() // $ guarded='0 == getNumOrDefault(...):true' guarded='getNumOrDefault(...):0' |
| 195 | + } else { |
| 196 | + chk() // $ guarded='0 == getNumOrDefault(...):false' guarded='getNumOrDefault(...):not 0' guarded='i:not null' MISSING: guarded='i:not 0' |
| 197 | + } |
| 198 | + |
| 199 | + if nil == concatNonNull(s, "suffix") { |
| 200 | + chk() // $ guarded='concatNonNull(...):null' guarded='nil == concatNonNull(...):true' |
| 201 | + } else { |
| 202 | + chk() // $ guarded='concatNonNull(...):not null' guarded='nil == concatNonNull(...):false' guarded='s:not null' SPURIOUS: guarded='"suffix":not null' |
| 203 | + } |
| 204 | + |
| 205 | + switch testEnumWrapper(g(1)) { |
| 206 | + case .SUCCESS: |
| 207 | + chk() // $ guarded='... .SUCCESS:match' MISSING: guarded='testEnumWrapper(...):SUCCESS' guarded=g(1):true |
| 208 | + case .FAILURE: |
| 209 | + chk() // $ guarded='... .SUCCESS:no match' MISSING: guarded='testEnumWrapper(...):not SUCCESS' guarded='testEnumWrapper(...):FAILURE' guarded='... .FAILURE:match' guarded=g(1):false |
| 210 | + } |
| 211 | +} |
| 212 | + |
| 213 | +func ensureNotNull(_ o: Any?) throws { |
| 214 | + if o == nil { throw GuardError(msg: "null") } |
| 215 | +} |
| 216 | + |
| 217 | +func testExceptionWrapper(_ s: String?) throws { |
| 218 | + chk() // nothing guards here |
| 219 | + try ensureNotNull(s) |
| 220 | + chk() // $ guarded='ensureNotNull(...):no exception' guarded='s:not null' |
| 221 | +} |
| 222 | + |
| 223 | +func namedCheck(_ other: String?, value: String?) -> Bool { |
| 224 | + if value == nil { return false } |
| 225 | + return true |
| 226 | +} |
| 227 | + |
| 228 | +func testFunctionWrappers(_ first: String?, _ second: String?) { |
| 229 | + if namedCheck(first, value: second) { |
| 230 | + chk() // $ guarded=namedCheck(...):true guarded='second:not null' |
| 231 | + } |
| 232 | + |
| 233 | + func localCheck(_ value: String?) -> Bool { |
| 234 | + if value == nil { return false } |
| 235 | + return true |
| 236 | + } |
| 237 | + if localCheck(first) { |
| 238 | + chk() // $ guarded=localCheck(...):true guarded='first:not null' |
| 239 | + } |
| 240 | +} |
| 241 | + |
| 242 | +class OverridableCheck { |
| 243 | + func accepts(_ value: String?) -> Bool { |
| 244 | + if value == nil { return false } |
| 245 | + return true |
| 246 | + } |
| 247 | +} |
| 248 | + |
| 249 | +class AlwaysAccept: OverridableCheck { |
| 250 | + override func accepts(_ value: String?) -> Bool { |
| 251 | + return true |
| 252 | + } |
| 253 | +} |
| 254 | + |
| 255 | +private dynamic func dynamicCheck(_ value: String?) -> Bool { |
| 256 | + if value == nil { return false } |
| 257 | + return true |
| 258 | +} |
| 259 | + |
| 260 | +func testDynamicDispatch(_ checker: OverridableCheck, _ value: String?) { |
| 261 | + if checker.accepts(value) { |
| 262 | + chk() // $ guarded='... .accepts(...):true' |
| 263 | + } |
| 264 | + if dynamicCheck(value) { |
| 265 | + chk() // $ guarded=dynamicCheck(...):true |
| 266 | + } |
| 267 | +} |
| 268 | + |
| 269 | +class Failable { |
| 270 | + init?() { return nil } |
| 271 | +} |
| 272 | + |
| 273 | +class InheritedFailable: Failable {} |
| 274 | + |
| 275 | +func testFailableInitializers(_ flag: Bool) { |
| 276 | + let first = flag ? Failable() : nil |
| 277 | + if first == nil { |
| 278 | + chk() // $ guarded='first == nil:true' guarded='first:null' guarded='IfExpr:null' |
| 279 | + } |
| 280 | + let second = flag ? InheritedFailable() : nil |
| 281 | + if second == nil { |
| 282 | + chk() // $ guarded='second == nil:true' guarded='second:null' guarded='IfExpr:null' |
| 283 | + } |
| 284 | +} |
| 285 | + |
| 286 | +typealias Text = String |
| 287 | +typealias MaybeText = String? |
| 288 | + |
| 289 | +func testTypeTests(_ value: Any?) { |
| 290 | + if value is String { |
| 291 | + chk() // $ guarded=TypeTestExpr:true guarded='value:not null' |
| 292 | + } else { |
| 293 | + chk() // $ guarded=TypeTestExpr:false |
| 294 | + } |
| 295 | + if value is Text { |
| 296 | + chk() // $ guarded=TypeTestExpr:true guarded='value:not null' |
| 297 | + } |
| 298 | + if value is String? { |
| 299 | + chk() // $ guarded=TypeTestExpr:true |
| 300 | + } |
| 301 | + if value is MaybeText { |
| 302 | + chk() // $ guarded=TypeTestExpr:true |
| 303 | + } |
| 304 | + if value is Any { |
| 305 | + chk() // $ guarded=TypeTestExpr:true |
| 306 | + } |
| 307 | +} |
| 308 | + |
| 309 | +func testLiteralEquality(_ value: String?) { |
| 310 | + if value == "safe" { |
| 311 | + chk() // $ guarded='value == "safe":true' guarded='value:safe' |
| 312 | + } else { |
| 313 | + chk() // $ guarded='value == "safe":false' guarded='value:not safe' |
| 314 | + } |
| 315 | +} |
0 commit comments