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 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690
|
// RUN: %target-swift-frontend -typecheck -verify -module-name main %s
/** Basics *******************************************************************/
// Function types can't be rethrows right now.
let r1 = {() rethrows -> Int in 0} // expected-error {{only function declarations may be marked 'rethrows'; did you mean 'throws'?}} {{14-22=throws}}
let r2 : () rethrows -> Int = { 0 } // expected-error {{only function declarations may be marked 'rethrows'; did you mean 'throws'?}} {{13-21=throws}}
let r3 : Optional<() rethrows -> ()> = nil // expected-error {{only function declarations may be marked 'rethrows'; did you mean 'throws'?}} {{22-30=throws}}
func f1(_ f: () throws -> ()) rethrows { try f() }
func f2(_ f: () -> ()) rethrows { f() } // expected-error {{'rethrows' function must take a throwing function argument}}
func f3(_ f: UndeclaredFunctionType) rethrows { f() } // expected-error {{cannot find type 'UndeclaredFunctionType' in scope}}
/** Protocol conformance checking ********************************************/
protocol P {
func tf() throws
func nf() // expected-note {{protocol requires}}
func thf(_ f: () throws -> ()) throws
func nhf(_ f: () throws -> ()) // expected-note 2 {{protocol requires}}
func rhf(_ f: () throws -> ()) rethrows // expected-note {{protocol requires}}
}
struct T0 : P { // expected-error {{type 'T0' does not conform to protocol 'P'}}
func tf() throws {}
func nf() throws {} // expected-note {{candidate throws, but protocol does not allow it}}
func thf(_ f: () throws -> ()) throws {}
func nhf(_ f: () throws -> ()) throws {} // expected-note {{candidate throws, but protocol does not allow it}}
func rhf(_ f: () throws -> ()) throws {} // expected-note {{candidate is not 'rethrows', but protocol requires it}}
}
struct T1 : P {
func tf() {}
func nf() {}
func thf(_ f: () throws -> ()) {}
func nhf(_ f: () throws -> ()) {}
func rhf(_ f: () throws -> ()) {}
}
struct T2 : P { // expected-error {{type 'T2' does not conform to protocol 'P'}}
func tf() {}
func nf() {}
func thf(_ f: () throws -> ()) rethrows {}
func nhf(_ f: () throws -> ()) rethrows {} // expected-note {{candidate throws, but protocol does not allow it}}
func rhf(_ f: () throws -> ()) rethrows {}
}
/** Classes ******************************************************************/
class Super {
func tf() throws {}
func nf() {} // expected-note {{overridden declaration is here}}
func thf(_ f: () throws -> ()) throws {}
func nhf(_ f: () throws -> ()) {} // expected-note 2 {{overridden declaration is here}}
func rhf(_ f: () throws -> ()) rethrows {} // expected-note {{overridden declaration is here}}
}
class C1 : Super {
override func tf() {}
override func nf() {}
override func thf(_ f: () throws -> ()) {}
override func nhf(_ f: () throws -> ()) {}
override func rhf(_ f: () throws -> ()) {}
}
class C2 : Super {
override func tf() throws {}
override func nf() throws {} // expected-error {{cannot override non-throwing instance method with throwing instance method}}
override func thf(_ f: () throws -> ()) throws {}
override func nhf(_ f: () throws -> ()) throws {} // expected-error {{cannot override non-throwing instance method with throwing instance method}}
override func rhf(_ f: () throws -> ()) throws {} // expected-error {{override of 'rethrows' method should also be 'rethrows'}}
}
class C3 : Super {
override func tf() {}
override func nf() {}
override func thf(_ f: () throws -> ()) rethrows {}
override func nhf(_ f: () throws -> ()) rethrows {} // expected-error {{cannot override non-throwing instance method with throwing instance method}}
override func rhf(_ f: () throws -> ()) rethrows {}
}
/** Semantics ****************************************************************/
@discardableResult
func call(_ fn: () throws -> Int) rethrows -> Int { return try fn() }
@discardableResult
func callAC(_ fn: @autoclosure () throws -> Int) rethrows -> Int { return try fn() }
@discardableResult
func raise() throws -> Int { return 0 }
@discardableResult
func noraise() -> Int { return 0 }
/** Global functions **/
func testCallUnhandled() {
call(noraise)
try call(noraise) // expected-warning {{no calls to throwing functions occur within 'try'}}
call(raise) // expected-error {{call can throw, but it is not marked with 'try' and the error is not handled}} expected-note {{call is to 'rethrows' function, but argument function can throw}}
try call(raise) // expected-error {{call can throw, but the error is not handled}} expected-note {{call is to 'rethrows' function, but argument function can throw}}
}
func testCallHandled() throws {
call(noraise)
try call(noraise) // expected-warning {{no calls to throwing functions occur within 'try'}}
call(raise) // expected-error {{call can throw but is not marked with 'try'}} expected-note {{call is to 'rethrows' function, but argument function can throw}}
try call(raise)
}
func testCallACUnhandled() {
callAC(noraise())
try callAC(noraise()) // expected-warning {{no calls to throwing functions occur within 'try'}}
callAC(raise()) // expected-error {{call can throw but is not marked with 'try'}} \
// expected-error {{call can throw, but it is not marked with 'try' and the error is not handled}} \
// expected-note {{call is to 'rethrows' function, but argument function can throw}}
// expected-note@-3 {{did you mean to use 'try'?}} {{10-10=try }}
// expected-note@-4 {{did you mean to handle error as optional value?}} {{10-10=try? }}
// expected-note@-5 {{did you mean to disable error propagation?}} {{10-10=try! }}
try callAC(raise()) // expected-error {{call can throw, but the error is not handled}} expected-note {{call is to 'rethrows' function, but argument function can throw}}
}
func testCallACHandled() throws {
callAC(noraise())
try callAC(noraise()) // expected-warning {{no calls to throwing functions occur within 'try'}}
callAC(raise()) // expected-error 2 {{call can throw but is not marked with 'try'}} expected-note {{call is to 'rethrows' function, but argument function can throw}}
// expected-note@-1 {{did you mean to use 'try'?}} {{10-10=try }}
// expected-note@-2 {{did you mean to handle error as optional value?}} {{10-10=try? }}
// expected-note@-3 {{did you mean to disable error propagation?}} {{10-10=try! }}
try callAC(raise())
}
func testForward1(_ fn: () throws -> Int) rethrows {
try call(fn)
}
func testForward2(_ fn: () throws -> Int) rethrows {
try call({ try fn() })
}
/** Methods **/
struct MyStruct : MyProto {
@discardableResult
func call(_ fn: () throws -> Int) rethrows -> Int { return try fn() }
@discardableResult
func callAC(_ fn: @autoclosure () throws -> Int) rethrows -> Int { return try fn() }
@discardableResult
static func static_call(_ fn: () throws -> Int) rethrows -> Int { return try fn() }
@discardableResult
static func static_callAC(_ fn: @autoclosure () throws -> Int) rethrows -> Int { return try fn() }
}
func testMethodCallUnhandled(_ s: MyStruct) {
s.call(noraise)
try s.call(noraise) // expected-warning {{no calls to throwing functions occur within 'try'}}
s.call(raise) // expected-error {{call can throw, but it is not marked with 'try' and the error is not handled}} expected-note {{call is to 'rethrows' function, but argument function can throw}}
try s.call(raise) // expected-error {{call can throw, but the error is not handled}} expected-note {{call is to 'rethrows' function, but argument function can throw}}
MyStruct.static_call(noraise)
try MyStruct.static_call(noraise) // expected-warning {{no calls to throwing functions occur within 'try'}}
MyStruct.static_call(raise) // expected-error {{call can throw, but it is not marked with 'try' and the error is not handled}} expected-note {{call is to 'rethrows' function, but argument function can throw}}
try MyStruct.static_call(raise) // expected-error {{call can throw, but the error is not handled}} expected-note {{call is to 'rethrows' function, but argument function can throw}}
}
func testMethodCallHandled(_ s: MyStruct) throws {
s.call(noraise)
try s.call(noraise) // expected-warning {{no calls to throwing functions occur within 'try'}}
s.call(raise) // expected-error {{call can throw but is not marked with 'try'}} expected-note {{call is to 'rethrows' function, but argument function can throw}}
try s.call(raise)
MyStruct.static_call(noraise)
try MyStruct.static_call(noraise) // expected-warning {{no calls to throwing functions occur within 'try'}}
MyStruct.static_call(raise) // expected-error {{call can throw but is not marked with 'try'}} expected-note {{call is to 'rethrows' function, but argument function can throw}}
try MyStruct.static_call(raise)
}
func testMethodCallACUnhandled(_ s: MyStruct) {
s.callAC(noraise())
try s.callAC(noraise()) // expected-warning {{no calls to throwing functions occur within 'try'}}
s.callAC(raise()) // expected-error {{call can throw but is not marked with 'try'}} \
// expected-error {{call can throw, but it is not marked with 'try' and the error is not handled}} \
// expected-note {{call is to 'rethrows' function, but argument function can throw}}
// expected-note@-3 {{did you mean to use 'try'?}} {{12-12=try }}
// expected-note@-4 {{did you mean to handle error as optional value?}} {{12-12=try? }}
// expected-note@-5 {{did you mean to disable error propagation?}} {{12-12=try! }}
try s.callAC(raise()) // expected-error {{call can throw, but the error is not handled}} expected-note {{call is to 'rethrows' function, but argument function can throw}}
MyStruct.static_callAC(noraise())
try MyStruct.static_callAC(noraise()) // expected-warning {{no calls to throwing functions occur within 'try'}}
MyStruct.static_callAC(raise()) // expected-error {{call can throw but is not marked with 'try'}} \
// expected-error {{call can throw, but it is not marked with 'try' and the error is not handled}} \
// expected-note {{call is to 'rethrows' function, but argument function can throw}}
// expected-note@-3 {{did you mean to use 'try'?}} {{26-26=try }}
// expected-note@-4 {{did you mean to handle error as optional value?}} {{26-26=try? }}
// expected-note@-5 {{did you mean to disable error propagation?}} {{26-26=try! }}
try MyStruct.static_callAC(raise()) // expected-error {{call can throw, but the error is not handled}} expected-note {{call is to 'rethrows' function, but argument function can throw}}
}
func testMethodCallACHandled(_ s: MyStruct) throws {
s.callAC(noraise())
try s.callAC(noraise()) // expected-warning {{no calls to throwing functions occur within 'try'}}
s.callAC(raise()) // expected-error 2 {{call can throw but is not marked with 'try'}} expected-note {{call is to 'rethrows' function, but argument function can throw}}
// expected-note@-1 {{did you mean to use 'try'?}} {{12-12=try }}
// expected-note@-2 {{did you mean to handle error as optional value?}} {{12-12=try? }}
// expected-note@-3 {{did you mean to disable error propagation?}} {{12-12=try! }}
try s.callAC(raise())
MyStruct.static_callAC(noraise())
try MyStruct.static_callAC(noraise()) // expected-warning {{no calls to throwing functions occur within 'try'}}
MyStruct.static_callAC(raise()) // expected-error 2 {{call can throw but is not marked with 'try'}} expected-note {{call is to 'rethrows' function, but argument function can throw}}
// expected-note@-1 {{did you mean to use 'try'?}} {{26-26=try }}
// expected-note@-2 {{did you mean to handle error as optional value?}} {{26-26=try? }}
// expected-note@-3 {{did you mean to disable error propagation?}} {{26-26=try! }}
try MyStruct.static_callAC(raise())
}
/** Protocol methods **/
protocol MyProto {
@discardableResult
func call(_ fn: () throws -> Int) rethrows -> Int
@discardableResult
func callAC(_ fn: @autoclosure () throws -> Int) rethrows -> Int
@discardableResult
static func static_call(_ fn: () throws -> Int) rethrows -> Int
@discardableResult
static func static_callAC(_ fn: @autoclosure () throws -> Int) rethrows -> Int
}
/** Existentials **/
func testProtoMethodCallUnhandled(_ s: MyProto) {
s.call(noraise)
try s.call(noraise) // expected-warning {{no calls to throwing functions occur within 'try'}}
s.call(raise) // expected-error {{call can throw, but it is not marked with 'try' and the error is not handled}} expected-note {{call is to 'rethrows' function, but argument function can throw}}
try s.call(raise) // expected-error {{call can throw, but the error is not handled}} expected-note {{call is to 'rethrows' function, but argument function can throw}}
type(of: s).static_call(noraise)
try type(of: s).static_call(noraise) // expected-warning {{no calls to throwing functions occur within 'try'}}
type(of: s).static_call(raise) // expected-error {{call can throw, but it is not marked with 'try' and the error is not handled}} expected-note {{call is to 'rethrows' function, but argument function can throw}}
try type(of: s).static_call(raise) // expected-error {{call can throw, but the error is not handled}} expected-note {{call is to 'rethrows' function, but argument function can throw}}
}
func testProtoMethodCallHandled(_ s: MyProto) throws {
s.call(noraise)
try s.call(noraise) // expected-warning {{no calls to throwing functions occur within 'try'}}
s.call(raise) // expected-error {{call can throw but is not marked with 'try'}} expected-note {{call is to 'rethrows' function, but argument function can throw}}
try s.call(raise)
type(of: s).static_call(noraise)
try type(of: s).static_call(noraise) // expected-warning {{no calls to throwing functions occur within 'try'}}
type(of: s).static_call(raise) // expected-error {{call can throw but is not marked with 'try'}} expected-note {{call is to 'rethrows' function, but argument function can throw}}
try type(of: s).static_call(raise)
}
func testProtoMethodCallACUnhandled(_ s: MyProto) {
s.callAC(noraise())
try s.callAC(noraise()) // expected-warning {{no calls to throwing functions occur within 'try'}}
s.callAC(raise()) // expected-error {{call can throw but is not marked with 'try'}} \
// expected-error {{call can throw, but it is not marked with 'try' and the error is not handled}} \
// expected-note {{call is to 'rethrows' function, but argument function can throw}}
// expected-note@-3 {{did you mean to use 'try'?}} {{12-12=try }}
// expected-note@-4 {{did you mean to handle error as optional value?}} {{12-12=try? }}
// expected-note@-5 {{did you mean to disable error propagation?}} {{12-12=try! }}
try s.callAC(raise()) // expected-error {{call can throw, but the error is not handled}} expected-note {{call is to 'rethrows' function, but argument function can throw}}
type(of: s).static_callAC(noraise())
try type(of: s).static_callAC(noraise()) // expected-warning {{no calls to throwing functions occur within 'try'}}
type(of: s).static_callAC(raise()) // expected-error {{call can throw but is not marked with 'try'}} \
// expected-error {{call can throw, but it is not marked with 'try' and the error is not handled}} \
// expected-note {{call is to 'rethrows' function, but argument function can throw}}
// expected-note@-3 {{did you mean to use 'try'?}} {{29-29=try }}
// expected-note@-4 {{did you mean to handle error as optional value?}} {{29-29=try? }}
// expected-note@-5 {{did you mean to disable error propagation?}} {{29-29=try! }}
try type(of: s).static_callAC(raise()) // expected-error {{call can throw, but the error is not handled}} expected-note {{call is to 'rethrows' function, but argument function can throw}}
}
func testProtoMethodCallACHandled(_ s: MyProto) throws {
s.callAC(noraise())
try s.callAC(noraise()) // expected-warning {{no calls to throwing functions occur within 'try'}}
s.callAC(raise()) // expected-error 2 {{call can throw but is not marked with 'try'}} expected-note {{call is to 'rethrows' function, but argument function can throw}}
// expected-note@-1 {{did you mean to use 'try'?}} {{12-12=try }}
// expected-note@-2 {{did you mean to handle error as optional value?}} {{12-12=try? }}
// expected-note@-3 {{did you mean to disable error propagation?}} {{12-12=try! }}
try s.callAC(raise())
type(of: s).static_callAC(noraise())
try type(of: s).static_callAC(noraise()) // expected-warning {{no calls to throwing functions occur within 'try'}}
type(of: s).static_callAC(raise()) // expected-error 2 {{call can throw but is not marked with 'try'}} expected-note {{call is to 'rethrows' function, but argument function can throw}}
// expected-note@-1 {{did you mean to use 'try'?}} {{29-29=try }}
// expected-note@-2 {{did you mean to handle error as optional value?}} {{29-29=try? }}
// expected-note@-3 {{did you mean to disable error propagation?}} {{29-29=try! }}
try type(of: s).static_callAC(raise())
}
/** Generics **/
func testGenericMethodCallUnhandled<P: MyProto>(_ s: P) {
s.call(noraise)
try s.call(noraise) // expected-warning {{no calls to throwing functions occur within 'try'}}
s.call(raise) // expected-error {{call can throw, but it is not marked with 'try' and the error is not handled}} expected-note {{call is to 'rethrows' function, but argument function can throw}}
try s.call(raise) // expected-error {{call can throw, but the error is not handled}} expected-note {{call is to 'rethrows' function, but argument function can throw}}
P.static_call(noraise)
try P.static_call(noraise) // expected-warning {{no calls to throwing functions occur within 'try'}}
P.static_call(raise) // expected-error {{call can throw, but it is not marked with 'try' and the error is not handled}} expected-note {{call is to 'rethrows' function, but argument function can throw}}
try P.static_call(raise) // expected-error {{call can throw, but the error is not handled}} expected-note {{call is to 'rethrows' function, but argument function can throw}}
}
func testGenericMethodCallHandled<P: MyProto>(_ s: P) throws {
s.call(noraise)
try s.call(noraise) // expected-warning {{no calls to throwing functions occur within 'try'}}
s.call(raise) // expected-error {{call can throw but is not marked with 'try'}} expected-note {{call is to 'rethrows' function, but argument function can throw}}
try s.call(raise)
P.static_call(noraise)
try P.static_call(noraise) // expected-warning {{no calls to throwing functions occur within 'try'}}
P.static_call(raise) // expected-error {{call can throw but is not marked with 'try'}} expected-note {{call is to 'rethrows' function, but argument function can throw}}
try P.static_call(raise)
}
func testGenericMethodCallACUnhandled<P: MyProto>(_ s: P) {
s.callAC(noraise())
try s.callAC(noraise()) // expected-warning {{no calls to throwing functions occur within 'try'}}
s.callAC(raise()) // expected-error {{call can throw but is not marked with 'try'}} \
// expected-error {{call can throw, but it is not marked with 'try' and the error is not handled}} \
// expected-note {{call is to 'rethrows' function, but argument function can throw}}
// expected-note@-3 {{did you mean to use 'try'?}} {{12-12=try }}
// expected-note@-4 {{did you mean to handle error as optional value?}} {{12-12=try? }}
// expected-note@-5 {{did you mean to disable error propagation?}} {{12-12=try! }}
try s.callAC(raise()) // expected-error {{call can throw, but the error is not handled}} expected-note {{call is to 'rethrows' function, but argument function can throw}}
P.static_callAC(noraise())
try P.static_callAC(noraise()) // expected-warning {{no calls to throwing functions occur within 'try'}}
P.static_callAC(raise()) // expected-error {{call can throw but is not marked with 'try'}} \
// expected-error {{call can throw, but it is not marked with 'try' and the error is not handled}} \
// expected-note {{call is to 'rethrows' function, but argument function can throw}}
// expected-note@-3 {{did you mean to use 'try'?}} {{19-19=try }}
// expected-note@-4 {{did you mean to handle error as optional value?}} {{19-19=try? }}
// expected-note@-5 {{did you mean to disable error propagation?}} {{19-19=try! }}
try P.static_callAC(raise()) // expected-error {{call can throw, but the error is not handled}} expected-note {{call is to 'rethrows' function, but argument function can throw}}
}
func testGenericMethodCallACHandled<P: MyProto>(_ s: P) throws {
s.callAC(noraise())
try s.callAC(noraise()) // expected-warning {{no calls to throwing functions occur within 'try'}}
s.callAC(raise()) // expected-error 2 {{call can throw but is not marked with 'try'}} expected-note {{call is to 'rethrows' function, but argument function can throw}}
// expected-note@-1 {{did you mean to use 'try'?}} {{12-12=try }}
// expected-note@-2 {{did you mean to handle error as optional value?}} {{12-12=try? }}
// expected-note@-3 {{did you mean to disable error propagation?}} {{12-12=try! }}
try s.callAC(raise())
P.static_callAC(noraise())
try P.static_callAC(noraise()) // expected-warning {{no calls to throwing functions occur within 'try'}}
P.static_callAC(raise()) // expected-error 2 {{call can throw but is not marked with 'try'}} expected-note {{call is to 'rethrows' function, but argument function can throw}}
// expected-note@-1 {{did you mean to use 'try'?}} {{19-19=try }}
// expected-note@-2 {{did you mean to handle error as optional value?}} {{19-19=try? }}
// expected-note@-3 {{did you mean to disable error propagation?}} {{19-19=try! }}
try P.static_callAC(raise())
}
/** Optional closure parameters */
func testForceUnwrappedOptionalFunctionParameter(_ f: (() throws -> Void)?) rethrows {
try f!()
}
func testBindOptionalFunctionParameter(_ f: (() throws -> Void)?) rethrows {
try f?()
}
func testImplicitlyUnwrappedFunctionParameter(_ f: (() throws -> Void)!) rethrows {
if f != nil {
try f()
}
}
func throwingFunc() throws {}
func nonThrowingFunc() {}
try testBindOptionalFunctionParameter(throwingFunc)
testBindOptionalFunctionParameter(nonThrowingFunc)
testBindOptionalFunctionParameter(nil)
try testImplicitlyUnwrappedFunctionParameter(throwingFunc)
testImplicitlyUnwrappedFunctionParameter(nonThrowingFunc)
testImplicitlyUnwrappedFunctionParameter(nil)
/** Miscellaneous bugs **/
// rdar://problem/21967164 - Non-throwing closures are incorrectly marked as throwing in rethrow contexts
func rt1(predicate: () throws -> ()) rethrows { }
rt1 { }
func rt2(_ predicate: () throws -> ()) rethrows { }
rt2 { }
enum SomeError : Error {
case Badness
}
func testUnrelatedThrowsInRethrows(_ fn: () throws -> Void) rethrows {
try fn() // okay
try testUnrelatedThrowsInRethrows(fn) // okay
raise() // expected-error {{call can throw, but it is not marked with 'try' and the error is not handled; a function declared 'rethrows' may only throw if its parameter does}}
try raise() // expected-error {{call can throw, but the error is not handled; a function declared 'rethrows' may only throw if its parameter does}}
throw SomeError.Badness // expected-error {{a function declared 'rethrows' may only throw if its parameter does}}
}
func testThrowsInCatchInRethrows(_ fn: () throws -> Void) rethrows {
do {
try fn()
} catch {
// this catch can only be entered if our `fn` parameter throws
throw error // okay
}
do {
try fn()
} catch let error as SomeError {
throw error // okay
}
do {
try fn()
try raise()
} catch {
// this catch can be entered regardless of whether our `fn` parameter throws
throw error // expected-error {{a function declared 'rethrows' may only throw if its parameter does}}
}
do {
throw SomeError.Badness
} catch {
// this catch can be entered regardless of whether our `fn` parameter throws
throw error // expected-error {{a function declared 'rethrows' may only throw if its parameter does}}
}
do {
try fn()
try raise() // expected-error {{call can throw, but the error is not handled; a function declared 'rethrows' may only throw if its parameter does}}
} catch is SomeError {}
do {
try raise()
} catch {
try fn() // okay
}
do {
// don't throw anything; equivalent to throwing in an #if
} catch { // expected-warning {{'catch' block is unreachable because no errors are thrown in 'do' block}}
throw error
}
}
// Soundness-check that throwing in catch blocks behaves as expected outside of
// rethrows functions
func testThrowsInCatch(_ fn: () throws -> Void) {
do {
try fn()
} catch {
throw error // expected-error {{error is not handled because the enclosing function is not declared 'throws'}}
}
}
func testThrowsInCatchInThrows() throws {
do {
try raise()
} catch {
throw error // okay
}
}
// <rdar://problem/24221830> Bogus "no calls to throwing functions" warning in derived throwing init
class B24221830 {}
class r24221830 : B24221830 {
var B: Int
init(A: String) throws {
self.B = 0
}
}
// rdar://problem/30618853
func gallant(_: () throws -> ()) rethrows {}
func goofus(_ f: () -> ()) {
gallant(f)
main.gallant(f)
}
func goofus(_ f: () throws -> ()) rethrows {
try gallant(f)
try main.gallant(f)
}
struct Foo {
func foo() {}
}
func throwWhileGettingFoo() throws -> Foo.Type { return Foo.self }
(throwWhileGettingFoo()).foo(Foo())() // expected-error {{can throw}}
// expected-note@-1 {{did you mean to use 'try'?}} {{2-2=try }}
// expected-note@-2 {{did you mean to handle error as optional value?}} {{2-2=try? }}
// expected-note@-3 {{did you mean to disable error propagation?}} {{2-2=try! }}
(try throwWhileGettingFoo()).foo(Foo())()
// <rdar://problem/31794932> [Source compatibility] Call to sort(by):) can throw, but is not marked with 'try'
func doRethrow(fn: (Int, Int) throws -> Int) rethrows { }
struct DoRethrowGeneric<T> {
func method(fn: (T, T) throws -> T) rethrows { }
}
func testDoRethrow() {
doRethrow(fn:) { (a, b) in return a }
DoRethrowGeneric<Int>().method(fn:) { (a, b) in return a }
}
// https://github.com/apple/swift/issues/49668
func rethrowsWithCaptureList<R, T>(
array: [T],
operation: (Int) throws -> R
) rethrows -> R {
return try array.withUnsafeBytes { [array] _ in
return try operation(array.count)
}
}
// rdar://problem/40472018: Crash on rethrows function with variadic parameter and throwing function parameter.
public func variadic_rethrows(_ values: Int..., body: (Int) throws -> ()) rethrows { }
public func rdar40472018() {
variadic_rethrows(1, 2) { _ in }
}
// https://github.com/apple/swift/issues/48849
// Verify that we do not emit an invalid
// "... can throw but the expression is not marked with 'try'"
// error on the use of operators.
infix operator <|: infixr0
infix operator |>: infixl1
precedencegroup infixr0 {
associativity: right
}
precedencegroup infixl1 {
associativity: left
higherThan: infixr0
}
func <| <A, B> (f: (A) throws -> B, a: A) rethrows -> B {
return try f(a)
}
func |> <A, B> (a: A, f: (A) -> B) -> B {
return try f(a) // expected-warning {{no calls to throwing functions occur within 'try' expression}}
}
struct Box<A> {
let unbox: A
}
func suchThat<A>(_ x: Box<A>) -> (@escaping (A) -> A) -> Box<A> {
return { f in Box(unbox: f(x.unbox)) }
}
Box(unbox: 1) |> suchThat <| { $0 + 1 } // expected-warning {{result of operator '<|' is unused}}
// Constructor delegation -vs- rethrows
class RethrowingConstructor {
init(_ block: () throws -> ()) rethrows {
try block()
}
convenience init(bar: Int) {
self.init {
print("Foo!")
}
}
convenience init(baz: Int) throws {
try self.init {
try throwingFunc()
}
}
}
// default values -vs- throwing function inside optional
func rdar_47550715() {
typealias A<T> = (T) -> Void
typealias F = () throws -> Void
func foo(_: A<F>? = nil) {} // Ok
func bar(_: A<F>? = .none) {} // Ok
}
// https://github.com/apple/swift/issues/56630
// Test cases for diagnostic note 'because_rethrows_default_argument_throws'
func nonThrowableDefaultRethrows(_ f: () throws -> () = {}) rethrows {
try f()
}
// FIXME: This should compile and not emit a diagnostic because ideally the
// compiler could statically know the default argument value could never throw.
// (https://github.com/apple/swift/issues/44143)
nonThrowableDefaultRethrows() // expected-error {{call can throw but is not marked with 'try'}}
// expected-note@-1 {{call is to 'rethrows' function, but a defaulted argument function can throw}}
func throwableDefaultRethrows(_ f: () throws -> () = { throw SomeError.Badness }) rethrows {
try f()
}
// This should always emit a diagnostic because we can statically know that default argument can throw.
throwableDefaultRethrows() // expected-error {{call can throw but is not marked with 'try'}}
// expected-note@-1 {{call is to 'rethrows' function, but a defaulted argument function can throw}}
// rdar://76169080 - rethrows -vs- Optional default arguments
func optionalRethrowsDefaultArg1(_: (() throws -> ())? = nil) rethrows {}
func callsOptionalRethrowsDefaultArg1() throws {
optionalRethrowsDefaultArg1()
optionalRethrowsDefaultArg1(nil)
try optionalRethrowsDefaultArg1 { throw SomeError.Badness }
}
func optionalRethrowsDefaultArg2(_: (() throws -> ())? = { throw SomeError.Badness }) rethrows {}
func callsOptionalRethrowsDefaultArg2() throws {
optionalRethrowsDefaultArg2() // expected-error {{call can throw but is not marked with 'try'}}
// expected-note@-1 {{call is to 'rethrows' function, but a defaulted argument function can throw}}
optionalRethrowsDefaultArg2(nil)
try optionalRethrowsDefaultArg2 { throw SomeError.Badness }
}
protocol P1 {
var id: Int { get }
func test(_: some Sequence) -> [any P1]
}
func open(p: any P1, s: any Sequence) throws {
_ = p.test(s).map(\.id)
}
// Rethrows checking and parameter packs, oh my.
func rethrowsWithParameterPacks<each Arg>(_ arguments: repeat each Arg, body: () throws -> Void) rethrows { }
enum MyError: Error {
case fail
}
func testRethrowsWithParameterPacks() throws {
try rethrowsWithParameterPacks { throw MyError.fail }
rethrowsWithParameterPacks { }
try rethrowsWithParameterPacks(1) { throw MyError.fail }
rethrowsWithParameterPacks(1) { }
try rethrowsWithParameterPacks(1, "hello") { throw MyError.fail }
rethrowsWithParameterPacks(1, "hello") { }
rethrowsWithParameterPacks { throw MyError.fail }
// expected-error@-1{{call can throw but is not marked with 'try'}}
// expected-note@-2{{call is to 'rethrows' function, but argument function can throw}}
}
// Rethrows checking with the original parameter type providing the cues.
func takesArbitraryAndRethrows<T>(_ value: T, body: () throws -> Void) rethrows { }
func testArbitraryAndRethrows() {
takesArbitraryAndRethrows(throwingFunc) { }
}
|