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 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765
|
// RUN: %target-typecheck-verify-swift -target %target-cpu-apple-macosx50 -disable-objc-attr-requires-foundation-module
// RUN: not %target-swift-frontend -target %target-cpu-apple-macosx50 -disable-objc-attr-requires-foundation-module -typecheck %s 2>&1 | %FileCheck %s '--implicit-check-not=<unknown>:0'
// Make sure we do not emit availability errors or warnings when -disable-availability-checking is passed
// RUN: not %target-swift-frontend -target %target-cpu-apple-macosx50 -typecheck -disable-objc-attr-requires-foundation-module -disable-availability-checking %s -diagnostic-style llvm 2>&1 | %FileCheck %s '--implicit-check-not=error:' '--implicit-check-not=warning:'
// REQUIRES: OS=macosx
func markUsed<T>(_ t: T) {}
@available(OSX, introduced: 10.9)
func globalFuncAvailableOn10_9() -> Int { return 9 }
@available(OSX, introduced: 51)
func globalFuncAvailableOn51() -> Int { return 10 }
@available(OSX, introduced: 52)
func globalFuncAvailableOn52() -> Int { return 11 }
// Top level should reflect the minimum deployment target.
let ignored1: Int = globalFuncAvailableOn10_9()
let ignored2: Int = globalFuncAvailableOn51() // expected-error {{'globalFuncAvailableOn51()' is only available in macOS 51 or newer}}
// expected-note@-1 {{add 'if #available' version check}}
let ignored3: Int = globalFuncAvailableOn52() // expected-error {{'globalFuncAvailableOn52()' is only available in macOS 52 or newer}}
// expected-note@-1 {{add 'if #available' version check}}
// Functions without annotations should reflect the minimum deployment target.
func functionWithoutAvailability() {
// expected-note@-1 2{{add @available attribute to enclosing global function}}
let _: Int = globalFuncAvailableOn10_9()
let _: Int = globalFuncAvailableOn51() // expected-error {{'globalFuncAvailableOn51()' is only available in macOS 51 or newer}}
// expected-note@-1 {{add 'if #available' version check}}
let _: Int = globalFuncAvailableOn52() // expected-error {{'globalFuncAvailableOn52()' is only available in macOS 52 or newer}}
// expected-note@-1 {{add 'if #available' version check}}
}
// Functions with annotations should refine their bodies.
@available(OSX, introduced: 51)
func functionAvailableOn51() {
let _: Int = globalFuncAvailableOn10_9()
let _: Int = globalFuncAvailableOn51()
// Nested functions should get their own refinement context.
@available(OSX, introduced: 52)
func innerFunctionAvailableOn52() {
let _: Int = globalFuncAvailableOn10_9()
let _: Int = globalFuncAvailableOn51()
let _: Int = globalFuncAvailableOn52()
}
let _: Int = globalFuncAvailableOn52() // expected-error {{'globalFuncAvailableOn52()' is only available in macOS 52 or newer}}
// expected-note@-1 {{add 'if #available' version check}}
}
// Still allow other availability annotations on script-mode globals
@available(OSX, deprecated: 51)
var deprecatedGlobalInScriptMode: Int = 5
if #available(OSX 51, *) {
let _: Int = globalFuncAvailableOn51()
let _: Int = globalFuncAvailableOn52() // expected-error {{'globalFuncAvailableOn52()' is only available in macOS 52 or newer}}
// expected-note@-1 {{add 'if #available' version check}}
}
if #available(OSX 51, *) {
let _: Int = globalFuncAvailableOn51()
let _: Int = globalFuncAvailableOn52() // expected-error {{'globalFuncAvailableOn52()' is only available in macOS 52 or newer}}
// expected-note@-1 {{add 'if #available' version check}}
} else {
let _: Int = globalFuncAvailableOn10_9()
let _: Int = globalFuncAvailableOn51() // expected-error {{'globalFuncAvailableOn51()' is only available in macOS 51 or newer}}
// expected-note@-1 {{add 'if #available' version check}}
}
@available(OSX, introduced: 51)
@available(iOS, introduced: 8.0)
func globalFuncAvailableOnOSX51AndiOS8_0() -> Int { return 10 }
if #available(OSX 51, iOS 8.0, *) {
let _: Int = globalFuncAvailableOnOSX51AndiOS8_0()
}
if #available(iOS 9.0, *) {
let _: Int = globalFuncAvailableOnOSX51AndiOS8_0() // expected-error {{'globalFuncAvailableOnOSX51AndiOS8_0()' is only available in macOS 51 or newer}}
// expected-note@-1 {{add 'if #available' version check}}
}
// Multiple potentially unavailable references in a single statement
let ignored4: (Int, Int) = (globalFuncAvailableOn51(), globalFuncAvailableOn52()) // expected-error {{'globalFuncAvailableOn51()' is only available in macOS 51 or newer}} expected-error {{'globalFuncAvailableOn52()' is only available in macOS 52 or newer}}
// expected-note@-1 2{{add 'if #available' version check}}
_ = globalFuncAvailableOn10_9()
let ignored5 = globalFuncAvailableOn51 // expected-error {{'globalFuncAvailableOn51()' is only available in macOS 51 or newer}}
// expected-note@-1 {{add 'if #available' version check}}
_ = globalFuncAvailableOn51() // expected-error {{'globalFuncAvailableOn51()' is only available in macOS 51 or newer}}
// expected-note@-1 {{add 'if #available' version check}}
// Overloaded global functions
@available(OSX, introduced: 10.9)
func overloadedFunction() {}
@available(OSX, introduced: 51)
func overloadedFunction(_ on1010: Int) {}
overloadedFunction()
overloadedFunction(0) // expected-error {{'overloadedFunction' is only available in macOS 51 or newer}}
// expected-note@-1 {{add 'if #available' version check}}
// Potentially unavailable methods
class ClassWithPotentiallyUnavailableMethod {
// expected-note@-1 {{add @available attribute to enclosing class}}
@available(OSX, introduced: 10.9)
func methAvailableOn10_9() {}
@available(OSX, introduced: 51)
func methAvailableOn51() {}
@available(OSX, introduced: 51)
class func classMethAvailableOn51() {}
func someOtherMethod() {
// expected-note@-1 {{add @available attribute to enclosing instance method}}
methAvailableOn10_9()
methAvailableOn51() // expected-error {{'methAvailableOn51()' is only available in macOS 51 or newer}}
// expected-note@-1 {{add 'if #available' version check}}
}
}
func callPotentiallyUnavailableMethods(_ o: ClassWithPotentiallyUnavailableMethod) {
// expected-note@-1 2{{add @available attribute to enclosing global function}}
let m10_9 = o.methAvailableOn10_9
m10_9()
let m51 = o.methAvailableOn51 // expected-error {{'methAvailableOn51()' is only available in macOS 51 or newer}}
// expected-note@-1 {{add 'if #available' version check}}
m51()
o.methAvailableOn10_9()
o.methAvailableOn51() // expected-error {{'methAvailableOn51()' is only available in macOS 51 or newer}}
// expected-note@-1 {{add 'if #available' version check}}
}
func callPotentiallyUnavailableMethodsViaIUO(_ o: ClassWithPotentiallyUnavailableMethod!) {
// expected-note@-1 2{{add @available attribute to enclosing global function}}
let m10_9 = o.methAvailableOn10_9
m10_9()
let m51 = o.methAvailableOn51 // expected-error {{'methAvailableOn51()' is only available in macOS 51 or newer}}
// expected-note@-2 {{add 'if #available' version check}}
m51()
o.methAvailableOn10_9()
o.methAvailableOn51() // expected-error {{'methAvailableOn51()' is only available in macOS 51 or newer}}
// expected-note@-1 {{add 'if #available' version check}}
}
func callPotentiallyUnavailableClassMethod() {
// expected-note@-1 2{{add @available attribute to enclosing global function}}
ClassWithPotentiallyUnavailableMethod.classMethAvailableOn51() // expected-error {{'classMethAvailableOn51()' is only available in macOS 51 or newer}}
// expected-note@-1 {{add 'if #available' version check}}
let m51 = ClassWithPotentiallyUnavailableMethod.classMethAvailableOn51 // expected-error {{'classMethAvailableOn51()' is only available in macOS 51 or newer}}
// expected-note@-1 {{add 'if #available' version check}}
m51()
}
class SubClassWithPotentiallyUnavailableMethod : ClassWithPotentiallyUnavailableMethod {
// expected-note@-1 {{add @available attribute to enclosing class}}
func someMethod() {
// expected-note@-1 {{add @available attribute to enclosing instance method}}
methAvailableOn10_9()
methAvailableOn51() // expected-error {{'methAvailableOn51()' is only available in macOS 51 or newer}}
// expected-note@-1 {{add 'if #available' version check}}
}
}
class SubClassOverridingPotentiallyUnavailableMethod : ClassWithPotentiallyUnavailableMethod {
// expected-note@-1 2{{add @available attribute to enclosing class}}
override func methAvailableOn51() {
// expected-note@-1 2{{add @available attribute to enclosing instance method}}
methAvailableOn10_9()
super.methAvailableOn51() // expected-error {{'methAvailableOn51()' is only available in macOS 51 or newer}}
// expected-note@-1 {{add 'if #available' version check}}
let m10_9 = super.methAvailableOn10_9
m10_9()
let m51 = super.methAvailableOn51 // expected-error {{'methAvailableOn51()' is only available in macOS 51 or newer}}
// expected-note@-1 {{add 'if #available' version check}}
m51()
}
func someMethod() {
methAvailableOn10_9()
// Calling our override should be fine
methAvailableOn51()
}
}
class ClassWithPotentiallyUnavailableOverloadedMethod {
@available(OSX, introduced: 10.9)
func overloadedMethod() {}
@available(OSX, introduced: 51)
func overloadedMethod(_ on1010: Int) {}
}
func callPotentiallyUnavailableOverloadedMethod(_ o: ClassWithPotentiallyUnavailableOverloadedMethod) {
// expected-note@-1 {{add @available attribute to enclosing global function}}
o.overloadedMethod()
o.overloadedMethod(0) // expected-error {{'overloadedMethod' is only available in macOS 51 or newer}}
// expected-note@-1 {{add 'if #available' version check}}
}
// Initializers
class ClassWithPotentiallyUnavailableInitializer {
// expected-note@-1 {{add @available attribute to enclosing class}}
@available(OSX, introduced: 10.9)
required init() { }
@available(OSX, introduced: 51)
required init(_ val: Int) { }
convenience init(s: String) {
// expected-note@-1 {{add @available attribute to enclosing initializer}}
self.init(5) // expected-error {{'init(_:)' is only available in macOS 51 or newer}}
// expected-note@-1 {{add 'if #available' version check}}
}
@available(OSX, introduced: 51)
convenience init(onlyOn1010: String) {
self.init(5)
}
}
func callPotentiallyUnavailableInitializer() {
// expected-note@-1 2{{add @available attribute to enclosing global function}}
_ = ClassWithPotentiallyUnavailableInitializer()
_ = ClassWithPotentiallyUnavailableInitializer(5) // expected-error {{'init(_:)' is only available in macOS 51 or newer}}
// expected-note@-1 {{add 'if #available' version check}}
let i = ClassWithPotentiallyUnavailableInitializer.self
_ = i.init()
_ = i.init(5) // expected-error {{'init(_:)' is only available in macOS 51 or newer}}
// expected-note@-1 {{add 'if #available' version check}}
}
class SuperWithWithPotentiallyUnavailableInitializer {
@available(OSX, introduced: 10.9)
init() { }
@available(OSX, introduced: 51)
init(_ val: Int) { }
}
class SubOfClassWithPotentiallyUnavailableInitializer : SuperWithWithPotentiallyUnavailableInitializer {
// expected-note@-1 {{add @available attribute to enclosing class}}
override init(_ val: Int) {
// expected-note@-1 {{add @available attribute to enclosing initializer}}
super.init(5) // expected-error {{'init(_:)' is only available in macOS 51 or newer}}
// expected-note@-1 {{add 'if #available' version check}}
}
override init() {
super.init()
}
@available(OSX, introduced: 51)
init(on1010: Int) {
super.init(22)
}
}
// Properties
class ClassWithPotentiallyUnavailableProperties {
// expected-note@-1 4{{add @available attribute to enclosing class}}
var nonLazyAvailableOn10_9Stored: Int = 9
@available(OSX, introduced: 51) // expected-error {{stored properties cannot be marked potentially unavailable with '@available'}}
var nonLazyAvailableOn51Stored : Int = 10
@available(OSX, introduced: 51) // expected-error {{stored properties cannot be marked potentially unavailable with '@available'}}
let nonLazyLetAvailableOn51Stored : Int = 10
// Make sure that we don't emit a Fix-It to mark a stored property as potentially unavailable.
// We don't support potentially unavailable stored properties yet.
var storedPropertyOfPotentiallyUnavailableType: ClassAvailableOn51? = nil // expected-error {{'ClassAvailableOn51' is only available in macOS 51 or newer}}
@available(OSX, introduced: 10.9)
lazy var availableOn10_9Stored: Int = 9
@available(OSX, introduced: 51) // expected-error {{stored properties cannot be marked potentially unavailable with '@available'}}
lazy var availableOn51Stored : Int = 10
@available(OSX, introduced: 10.9)
var availableOn10_9Computed: Int {
get {
let _: Int = availableOn51Stored // expected-error {{'availableOn51Stored' is only available in macOS 51 or newer}}
// expected-note@-1 {{add 'if #available' version check}}
if #available(OSX 51, *) {
let _: Int = availableOn51Stored
}
return availableOn10_9Stored
}
set(newVal) {
availableOn10_9Stored = newVal
}
}
@available(OSX, introduced: 51)
var availableOn51Computed: Int {
get {
return availableOn51Stored
}
set(newVal) {
availableOn51Stored = newVal
}
}
var propWithSetterOnlyAvailableOn51 : Int {
// expected-note@-1 {{add @available attribute to enclosing property}}
get {
_ = globalFuncAvailableOn51() // expected-error {{'globalFuncAvailableOn51()' is only available in macOS 51 or newer}}
// expected-note@-1 {{add 'if #available' version check}}
return 0
}
@available(OSX, introduced: 51)
set(newVal) {
_ = globalFuncAvailableOn51()
}
}
var propWithGetterOnlyAvailableOn51 : Int {
// expected-note@-1 {{add @available attribute to enclosing property}}
@available(OSX, introduced: 51)
get {
_ = globalFuncAvailableOn51()
return 0
}
set(newVal) {
_ = globalFuncAvailableOn51() // expected-error {{'globalFuncAvailableOn51()' is only available in macOS 51 or newer}}
// expected-note@-1 {{add 'if #available' version check}}
}
}
var propWithGetterAndSetterOnlyAvailableOn51 : Int {
@available(OSX, introduced: 51)
get {
return 0
}
@available(OSX, introduced: 51)
set(newVal) {
}
}
var propWithSetterOnlyAvailableOn51ForNestedMemberRef : ClassWithPotentiallyUnavailableProperties {
get {
return ClassWithPotentiallyUnavailableProperties()
}
@available(OSX, introduced: 51)
set(newVal) {
}
}
var propWithGetterOnlyAvailableOn51ForNestedMemberRef : ClassWithPotentiallyUnavailableProperties {
@available(OSX, introduced: 51)
get {
return ClassWithPotentiallyUnavailableProperties()
}
set(newVal) {
}
}
}
@available(OSX, introduced: 51)
class ClassWithReferencesInInitializers {
var propWithInitializer51: Int = globalFuncAvailableOn51()
var propWithInitializer52: Int = globalFuncAvailableOn52() // expected-error {{'globalFuncAvailableOn52()' is only available in macOS 52 or newer}}
lazy var lazyPropWithInitializer51: Int = globalFuncAvailableOn51()
lazy var lazyPropWithInitializer52: Int = globalFuncAvailableOn52() // expected-error {{'globalFuncAvailableOn52()' is only available in macOS 52 or newer}}
}
func accessPotentiallyUnavailableProperties(_ o: ClassWithPotentiallyUnavailableProperties) {
// expected-note@-1 17{{add @available attribute to enclosing global function}}
// Stored properties
let _: Int = o.availableOn10_9Stored
let _: Int = o.availableOn51Stored // expected-error {{'availableOn51Stored' is only available in macOS 51 or newer}}
// expected-note@-1 {{add 'if #available' version check}}
o.availableOn10_9Stored = 9
o.availableOn51Stored = 10 // expected-error {{'availableOn51Stored' is only available in macOS 51 or newer}}
// expected-note@-1 {{add 'if #available' version check}}
// Computed Properties
let _: Int = o.availableOn10_9Computed
let _: Int = o.availableOn51Computed // expected-error {{'availableOn51Computed' is only available in macOS 51 or newer}}
// expected-note@-1 {{add 'if #available' version check}}
o.availableOn10_9Computed = 9
o.availableOn51Computed = 10 // expected-error {{'availableOn51Computed' is only available in macOS 51 or newer}}
// expected-note@-1 {{add 'if #available' version check}}
// Getter allowed on 10.9 but setter is not
let _: Int = o.propWithSetterOnlyAvailableOn51
o.propWithSetterOnlyAvailableOn51 = 5 // expected-error {{setter for 'propWithSetterOnlyAvailableOn51' is only available in macOS 51 or newer}}
// expected-note@-1 {{add 'if #available' version check}}
if #available(OSX 51, *) {
// Setter is allowed on 51 and greater
o.propWithSetterOnlyAvailableOn51 = 5
}
// Setter allowed on 10.9 but getter is not
o.propWithGetterOnlyAvailableOn51 = 5
let _: Int = o.propWithGetterOnlyAvailableOn51 // expected-error {{getter for 'propWithGetterOnlyAvailableOn51' is only available in macOS 51 or newer}}
// expected-note@-1 {{add 'if #available' version check}}
if #available(OSX 51, *) {
// Getter is allowed on 51 and greater
let _: Int = o.propWithGetterOnlyAvailableOn51
}
// Tests for nested member refs
// Both getters are potentially unavailable.
let _: Int = o.propWithGetterOnlyAvailableOn51ForNestedMemberRef.propWithGetterOnlyAvailableOn51 // expected-error {{getter for 'propWithGetterOnlyAvailableOn51ForNestedMemberRef' is only available in macOS 51 or newer}} expected-error {{getter for 'propWithGetterOnlyAvailableOn51' is only available in macOS 51 or newer}}
// expected-note@-1 2{{add 'if #available' version check}}
// Nested getter is potentially unavailable, outer getter is available
let _: Int = o.propWithGetterOnlyAvailableOn51ForNestedMemberRef.propWithSetterOnlyAvailableOn51 // expected-error {{getter for 'propWithGetterOnlyAvailableOn51ForNestedMemberRef' is only available in macOS 51 or newer}}
// expected-note@-1 {{add 'if #available' version check}}
// Nested getter is available, outer getter is potentially unavailable
let _:Int = o.propWithSetterOnlyAvailableOn51ForNestedMemberRef.propWithGetterOnlyAvailableOn51 // expected-error {{getter for 'propWithGetterOnlyAvailableOn51' is only available in macOS 51 or newer}}
// expected-note@-1 {{add 'if #available' version check}}
// Both getters are always available.
let _: Int = o.propWithSetterOnlyAvailableOn51ForNestedMemberRef.propWithSetterOnlyAvailableOn51
// Nesting in source of assignment
var v: Int
v = o.propWithGetterOnlyAvailableOn51 // expected-error {{getter for 'propWithGetterOnlyAvailableOn51' is only available in macOS 51 or newer}}
// expected-note@-1 {{add 'if #available' version check}}
v = (o.propWithGetterOnlyAvailableOn51) // expected-error {{getter for 'propWithGetterOnlyAvailableOn51' is only available in macOS 51 or newer}}
// expected-note@-1 {{add 'if #available' version check}}
_ = v // muffle warning
// Inout requires access to both getter and setter
func takesInout(_ i : inout Int) { }
takesInout(&o.propWithGetterOnlyAvailableOn51) // expected-error {{cannot pass as inout because getter for 'propWithGetterOnlyAvailableOn51' is only available in macOS 51 or newer}}
// expected-note@-1 {{add 'if #available' version check}}
takesInout(&o.propWithSetterOnlyAvailableOn51) // expected-error {{cannot pass as inout because setter for 'propWithSetterOnlyAvailableOn51' is only available in macOS 51 or newer}}
// expected-note@-1 {{add 'if #available' version check}}
takesInout(&o.propWithGetterAndSetterOnlyAvailableOn51) // expected-error {{cannot pass as inout because getter for 'propWithGetterAndSetterOnlyAvailableOn51' is only available in macOS 51 or newer}} expected-error {{cannot pass as inout because setter for 'propWithGetterAndSetterOnlyAvailableOn51' is only available in macOS 51 or newer}}
// expected-note@-1 2{{add 'if #available' version check}}
takesInout(&o.availableOn10_9Computed)
takesInout(&o.propWithGetterOnlyAvailableOn51ForNestedMemberRef.availableOn10_9Computed) // expected-error {{getter for 'propWithGetterOnlyAvailableOn51ForNestedMemberRef' is only available in macOS 51 or newer}}
// expected-note@-1 {{add 'if #available' version check}}
}
// _silgen_name
@_silgen_name("SomeName")
@available(OSX, introduced: 51)
func funcWith_silgen_nameAvailableOn51(_ p: ClassAvailableOn51?) -> ClassAvailableOn51
// Enums
@available(OSX, introduced: 51)
enum EnumIntroducedOn51 {
case Element
}
@available(OSX, introduced: 52)
enum EnumIntroducedOn52 {
case Element
}
@available(OSX, introduced: 51)
enum CompassPoint {
case North
case South
case East
@available(OSX, introduced: 52)
case West
case WithAvailableByEnumPayload(p : EnumIntroducedOn51)
// expected-error@+1 {{enum cases with associated values cannot be marked potentially unavailable with '@available'}}
@available(OSX, introduced: 52)
case WithAvailableByEnumElementPayload(p : EnumIntroducedOn52)
// expected-error@+1 2{{enum cases with associated values cannot be marked potentially unavailable with '@available'}}
@available(OSX, introduced: 52)
case WithAvailableByEnumElementPayload1(p : EnumIntroducedOn52), WithAvailableByEnumElementPayload2(p : EnumIntroducedOn52)
case WithPotentiallyUnavailablePayload(p : EnumIntroducedOn52) // expected-error {{'EnumIntroducedOn52' is only available in macOS 52 or newer}}
case WithPotentiallyUnavailablePayload1(p : EnumIntroducedOn52), WithPotentiallyUnavailablePayload2(p : EnumIntroducedOn52) // expected-error 2{{'EnumIntroducedOn52' is only available in macOS 52 or newer}}
@available(OSX, unavailable)
case WithPotentiallyUnavailablePayload3(p : EnumIntroducedOn52)
}
@available(OSX, introduced: 52)
func functionTakingEnumIntroducedOn52(_ e: EnumIntroducedOn52) { }
func useEnums() {
// expected-note@-1 3{{add @available attribute to enclosing global function}}
let _: CompassPoint = .North // expected-error {{'CompassPoint' is only available in macOS 51 or newer}}
// expected-note@-1 {{add 'if #available' version check}}
if #available(OSX 51, *) {
let _: CompassPoint = .North
let _: CompassPoint = .West // expected-error {{'West' is only available in macOS 52 or newer}}
// expected-note@-1 {{add 'if #available' version check}}
}
if #available(OSX 52, *) {
let _: CompassPoint = .West
}
// Pattern matching on an enum element does not require it to be definitely available
if #available(OSX 51, *) {
let point: CompassPoint = .North
switch (point) {
case .North, .South, .East:
markUsed("NSE")
case .West: // We do not expect an error here
markUsed("W")
case .WithPotentiallyUnavailablePayload(_):
markUsed("WithPotentiallyUnavailablePayload")
case .WithPotentiallyUnavailablePayload1(_):
markUsed("WithPotentiallyUnavailablePayload1")
case .WithPotentiallyUnavailablePayload2(_):
markUsed("WithPotentiallyUnavailablePayload2")
case .WithAvailableByEnumPayload(_):
markUsed("WithAvailableByEnumPayload")
case .WithAvailableByEnumElementPayload1(_):
markUsed("WithAvailableByEnumElementPayload1")
case .WithAvailableByEnumElementPayload2(_):
markUsed("WithAvailableByEnumElementPayload2")
case .WithAvailableByEnumElementPayload(let p):
markUsed("WithAvailableByEnumElementPayload")
// For the moment, we do not incorporate enum element availability into
// TRC construction. Perhaps we should?
functionTakingEnumIntroducedOn52(p) // expected-error {{'functionTakingEnumIntroducedOn52' is only available in macOS 52 or newer}}
// expected-note@-2 {{add 'if #available' version check}}
}
}
}
// Classes
@available(OSX, introduced: 10.9)
class ClassAvailableOn10_9 {
func someMethod() {}
class func someClassMethod() {}
var someProp : Int = 22
}
@available(OSX, introduced: 51)
class ClassAvailableOn51 { // expected-note {{enclosing scope requires availability of macOS 51 or newer}}
func someMethod() {}
class func someClassMethod() {
let _ = ClassAvailableOn51()
}
var someProp : Int = 22
@available(OSX, introduced: 10.9) // expected-error {{instance method cannot be more available than enclosing scope}}
func someMethodAvailableOn10_9() { }
@available(OSX, unavailable)
func someMethodUnavailable() { }
@available(*, unavailable)
func someMethodUniversallyUnavailable() { }
@available(OSX, introduced: 52)
var propWithGetter: Int { // expected-note{{enclosing scope requires availability of macOS 52 or newer}}
@available(OSX, introduced: 51) // expected-error {{getter cannot be more available than enclosing scope}}
get { return 0 }
}
}
func classAvailability() {
// expected-note@-1 3{{add @available attribute to enclosing global function}}
ClassAvailableOn10_9.someClassMethod()
ClassAvailableOn51.someClassMethod() // expected-error {{'ClassAvailableOn51' is only available in macOS 51 or newer}}
// expected-note@-1 {{add 'if #available' version check}}
_ = ClassAvailableOn10_9.self
_ = ClassAvailableOn51.self // expected-error {{'ClassAvailableOn51' is only available in macOS 51 or newer}}
// expected-note@-1 {{add 'if #available' version check}}
let o10_9 = ClassAvailableOn10_9()
let o51 = ClassAvailableOn51() // expected-error {{'ClassAvailableOn51' is only available in macOS 51 or newer}}
// expected-note@-1 {{add 'if #available' version check}}
o10_9.someMethod()
o51.someMethod()
let _ = o10_9.someProp
let _ = o51.someProp
}
func castingPotentiallyUnavailableClass(_ o : AnyObject) {
// expected-note@-1 3{{add @available attribute to enclosing global function}}
let _ = o as! ClassAvailableOn51 // expected-error {{'ClassAvailableOn51' is only available in macOS 51 or newer}}
// expected-note@-1 {{add 'if #available' version check}}
let _ = o as? ClassAvailableOn51 // expected-error {{'ClassAvailableOn51' is only available in macOS 51 or newer}}
// expected-note@-1 {{add 'if #available' version check}}
let _ = o is ClassAvailableOn51 // expected-error {{'ClassAvailableOn51' is only available in macOS 51 or newer}}
// expected-note@-1 {{add 'if #available' version check}}
}
protocol Creatable {
init()
}
@available(OSX, introduced: 51)
class ClassAvailableOn51_Creatable : Creatable {
required init() {}
}
func create<T : Creatable>() -> T {
return T()
}
class ClassWithGenericTypeParameter<T> { }
class ClassWithTwoGenericTypeParameter<T, S> { }
func classViaTypeParameter() {
// expected-note@-1 9{{add @available attribute to enclosing global function}}
let _ : ClassAvailableOn51_Creatable = // expected-error {{'ClassAvailableOn51_Creatable' is only available in macOS 51 or newer}}
// expected-note@-1 {{add 'if #available' version check}}
create()
let _ = create() as
ClassAvailableOn51_Creatable // expected-error {{'ClassAvailableOn51_Creatable' is only available in macOS 51 or newer}}
// expected-note@-1 {{add 'if #available' version check}}
let _ = [ClassAvailableOn51]() // expected-error {{'ClassAvailableOn51' is only available in macOS 51 or newer}}
// expected-note@-1 {{add 'if #available' version check}}
let _: ClassWithGenericTypeParameter<ClassAvailableOn51> = ClassWithGenericTypeParameter() // expected-error {{'ClassAvailableOn51' is only available in macOS 51 or newer}}
// expected-note@-1 {{add 'if #available' version check}}
let _: ClassWithTwoGenericTypeParameter<ClassAvailableOn51, String> = ClassWithTwoGenericTypeParameter() // expected-error {{'ClassAvailableOn51' is only available in macOS 51 or newer}}
// expected-note@-1 {{add 'if #available' version check}}
let _: ClassWithTwoGenericTypeParameter<String, ClassAvailableOn51> = ClassWithTwoGenericTypeParameter() // expected-error {{'ClassAvailableOn51' is only available in macOS 51 or newer}}
// expected-note@-1 {{add 'if #available' version check}}
let _: ClassWithTwoGenericTypeParameter<ClassAvailableOn51, ClassAvailableOn51> = ClassWithTwoGenericTypeParameter() // expected-error 2{{'ClassAvailableOn51' is only available in macOS 51 or newer}}
// expected-note@-1 2{{add 'if #available' version check}}
let _: ClassAvailableOn51? = nil // expected-error {{'ClassAvailableOn51' is only available in macOS 51 or newer}}
// expected-note@-1 {{add 'if #available' version check}}
}
// Potentially unavailable class used in declarations
class ClassWithDeclarationsOfPotentiallyUnavailableClasses {
// expected-note@-1 6{{add @available attribute to enclosing class}}
@available(OSX, introduced: 51)
init() {}
var propertyOfPotentiallyUnavailableType: ClassAvailableOn51 // expected-error {{'ClassAvailableOn51' is only available in macOS 51 or newer}}
@available(OSX, introduced: 51)
static var potentiallyUnavailableStaticPropertyOfPotentiallyUnavailableType: ClassAvailableOn51 = ClassAvailableOn51()
@available(OSX, introduced: 51)
static var potentiallyUnavailableStaticPropertyOfOptionalPotentiallyUnavailableType: ClassAvailableOn51?
func methodWithPotentiallyUnavailableParameterType(_ o : ClassAvailableOn51) { // expected-error {{'ClassAvailableOn51' is only available in macOS 51 or newer}}
// expected-note@-1 {{add @available attribute to enclosing instance method}}
}
@available(OSX, introduced: 51)
func potentiallyUnavailableMethodWithPotentiallyUnavailableParameterType(_ o : ClassAvailableOn51) {}
func methodWithPotentiallyUnavailableReturnType() -> ClassAvailableOn51 { // expected-error {{'ClassAvailableOn51' is only available in macOS 51 or newer}}
// expected-note@-1 2{{add @available attribute to enclosing instance method}}
return ClassAvailableOn51() // expected-error {{'ClassAvailableOn51' is only available in macOS 51 or newer}}
// expected-note@-1 {{add 'if #available' version check}}
}
@available(OSX, unavailable)
func unavailableMethodWithPotentiallyUnavailableParameterType(_ o : ClassAvailableOn51) {}
@available(OSX, introduced: 51)
func potentiallyUnavailableMethodWithPotentiallyUnavailableReturnType() -> ClassAvailableOn51 {
return ClassAvailableOn51()
}
@available(OSX, unavailable)
func unavailableMethodWithPotentiallyUnavailableReturnType() -> ClassAvailableOn51 {
guard #available(OSX 51, *) else { fatalError() }
return ClassAvailableOn51()
}
func methodWithPotentiallyUnavailableLocalDeclaration() {
// expected-note@-1 {{add @available attribute to enclosing instance method}}
let _ : ClassAvailableOn51 = methodWithPotentiallyUnavailableReturnType() // expected-error {{'ClassAvailableOn51' is only available in macOS 51 or newer}}
// expected-note@-1 {{add 'if #available' version check}}
}
@available(OSX, introduced: 51)
func potentiallyUnavailableMethodWithPotentiallyUnavailableLocalDeclaration() {
let _ : ClassAvailableOn51 = methodWithPotentiallyUnavailableReturnType()
}
@available(OSX, unavailable)
func unavailableMethodWithPotentiallyUnavailableLocalDeclaration() {
let _ : ClassAvailableOn51 = methodWithPotentiallyUnavailableReturnType() // expected-error {{'ClassAvailableOn51' is only available in macOS 51 or newer}}
// expected-note@-1 {{add 'if #available' version check}}
}
}
func referToPotentiallyUnavailableStaticProperty() {
// expected-note@-1 {{add @available attribute to enclosing global function}}
let _ = ClassWithDeclarationsOfPotentiallyUnavailableClasses.potentiallyUnavailableStaticPropertyOfPotentiallyUnavailableType // expected-error {{'potentiallyUnavailableStaticPropertyOfPotentiallyUnavailableType' is only available in macOS 51 or newer}}
// expected-note@-1 {{add 'if #available' version check}}
}
class ClassExtendingPotentiallyUnavailableClass : ClassAvailableOn51 { // expected-error {{'ClassAvailableOn51' is only available in macOS 51 or newer}}
// expected-note@-1 {{add @available attribute to enclosing class}}
}
@available(OSX, introduced: 51)
class PotentiallyUnavailableClassExtendingPotentiallyUnavailableClass : ClassAvailableOn51 {
}
@available(OSX, unavailable)
class UnavailableClassExtendingPotentiallyUnavailableClass : ClassAvailableOn51 {
}
// Method availability is contravariant
class SuperWithAlwaysAvailableMembers {
func shouldAlwaysBeAvailableMethod() { // expected-note {{overridden declaration is here}}
}
var shouldAlwaysBeAvailableProperty: Int { // expected-note {{overridden declaration is here}}
get { return 9 }
set(newVal) {}
}
var setterShouldAlwaysBeAvailableProperty: Int {
get { return 9 }
set(newVal) {} // expected-note {{overridden declaration is here}}
}
var getterShouldAlwaysBeAvailableProperty: Int {
get { return 9 } // expected-note {{overridden declaration is here}}
set(newVal) {}
}
}
class SubWithLimitedMemberAvailability : SuperWithAlwaysAvailableMembers {
@available(OSX, introduced: 51)
override func shouldAlwaysBeAvailableMethod() { // expected-error {{overriding 'shouldAlwaysBeAvailableMethod' must be as available as declaration it overrides}}
}
@available(OSX, introduced: 51)
override var shouldAlwaysBeAvailableProperty: Int { // expected-error {{overriding 'shouldAlwaysBeAvailableProperty' must be as available as declaration it overrides}}
get { return 10 }
set(newVal) {}
}
override var setterShouldAlwaysBeAvailableProperty: Int {
get { return 9 }
@available(OSX, introduced: 51)
set(newVal) {} // expected-error {{overriding setter for 'setterShouldAlwaysBeAvailableProperty' must be as available as declaration it overrides}}
// This is a terrible diagnostic. rdar://problem/20427938
}
override var getterShouldAlwaysBeAvailableProperty: Int {
@available(OSX, introduced: 51)
get { return 9 } // expected-error {{overriding getter for 'getterShouldAlwaysBeAvailableProperty' must be as available as declaration it overrides}}
set(newVal) {}
}
}
@available(OSX, introduced: 51)
class SubWithLimitedAvailablility : SuperWithAlwaysAvailableMembers {
override func shouldAlwaysBeAvailableMethod() {}
override var shouldAlwaysBeAvailableProperty: Int {
get { return 10 }
set(newVal) {}
}
override var setterShouldAlwaysBeAvailableProperty: Int {
get { return 9 }
set(newVal) {}
}
override var getterShouldAlwaysBeAvailableProperty: Int {
get { return 9 }
set(newVal) {}
}
}
class SuperWithLimitedMemberAvailability {
@available(OSX, introduced: 51)
func someMethod() {
}
@available(OSX, introduced: 51)
var someProperty: Int {
get { return 10 }
set(newVal) {}
}
}
class SubWithLargerMemberAvailability : SuperWithLimitedMemberAvailability {
// expected-note@-1 2{{add @available attribute to enclosing class}}
@available(OSX, introduced: 10.9)
override func someMethod() {
super.someMethod() // expected-error {{'someMethod()' is only available in macOS 51 or newer}}
// expected-note@-1 {{add 'if #available' version check}}
if #available(OSX 51, *) {
super.someMethod()
}
}
@available(OSX, introduced: 10.9)
override var someProperty: Int {
get {
let _ = super.someProperty // expected-error {{'someProperty' is only available in macOS 51 or newer}}
// expected-note@-1 {{add 'if #available' version check}}
if #available(OSX 51, *) {
let _ = super.someProperty
}
return 9
}
set(newVal) {}
}
}
@available(OSX, introduced: 51)
class SubWithLimitedAvailability : SuperWithLimitedMemberAvailability {
override func someMethod() {
super.someMethod()
}
override var someProperty: Int {
get { super.someProperty }
set(newVal) { super.someProperty = newVal }
}
}
@available(OSX, introduced: 52)
class SubWithMoreLimitedAvailability : SuperWithLimitedMemberAvailability {
override func someMethod() {
super.someMethod()
}
override var someProperty: Int {
get { super.someProperty }
set(newVal) { super.someProperty = newVal }
}
}
@available(OSX, introduced: 52)
class SubWithMoreLimitedAvailabilityAndRedundantMemberAvailability : SuperWithLimitedMemberAvailability {
@available(OSX, introduced: 52)
override func someMethod() {
super.someMethod()
}
@available(OSX, introduced: 52)
override var someProperty: Int {
get { super.someProperty }
set(newVal) { super.someProperty = newVal }
}
}
@available(OSX, unavailable)
class UnavailableSubWithLargerMemberAvailability : SuperWithLimitedMemberAvailability {
override func someMethod() {
}
override var someProperty: Int {
get { return 5 }
set(newVal) {}
}
}
// Inheritance and availability
@available(OSX, introduced: 51)
protocol ProtocolAvailableOn10_9 {
}
@available(OSX, introduced: 51)
protocol ProtocolAvailableOn51 {
}
@available(OSX, introduced: 10.9)
protocol ProtocolAvailableOn10_9InheritingFromProtocolAvailableOn51 : ProtocolAvailableOn51 { // expected-error {{'ProtocolAvailableOn51' is only available in macOS 51 or newer}}
}
@available(OSX, introduced: 51)
protocol ProtocolAvailableOn51InheritingFromProtocolAvailableOn10_9 : ProtocolAvailableOn10_9 {
}
@available(OSX, unavailable)
protocol UnavailableProtocolInheritingFromProtocolAvailableOn51 : ProtocolAvailableOn51 {
}
@available(OSX, introduced: 10.9)
class SubclassAvailableOn10_9OfClassAvailableOn51 : ClassAvailableOn51 { // expected-error {{'ClassAvailableOn51' is only available in macOS 51 or newer}}
}
@available(OSX, unavailable)
class UnavailableSubclassOfClassAvailableOn51 : ClassAvailableOn51 {
}
// We allow nominal types to conform to protocols that are less available than the types themselves.
@available(OSX, introduced: 10.9)
class ClassAvailableOn10_9AdoptingProtocolAvailableOn51 : ProtocolAvailableOn51 {
}
func castToPotentiallyUnavailableProtocol() {
// expected-note@-1 2{{add @available attribute to enclosing global function}}
let o: ClassAvailableOn10_9AdoptingProtocolAvailableOn51 = ClassAvailableOn10_9AdoptingProtocolAvailableOn51()
let _: ProtocolAvailableOn51 = o // expected-error {{'ProtocolAvailableOn51' is only available in macOS 51 or newer}}
// expected-note@-1 {{add 'if #available' version check}}
let _ = o as ProtocolAvailableOn51 // expected-error {{'ProtocolAvailableOn51' is only available in macOS 51 or newer}}
// expected-note@-1 {{add 'if #available' version check}}
}
@available(OSX, introduced: 10.9)
class SubclassAvailableOn10_9OfClassAvailableOn51AlsoAdoptingProtocolAvailableOn51 : ClassAvailableOn51, ProtocolAvailableOn51 { // expected-error {{'ClassAvailableOn51' is only available in macOS 51 or newer}}
}
class SomeGenericClass<T> { }
@available(OSX, introduced: 10.9)
class SubclassAvailableOn10_9OfSomeGenericClassOfProtocolAvailableOn51 : SomeGenericClass<ProtocolAvailableOn51> { // expected-error {{'ProtocolAvailableOn51' is only available in macOS 51 or newer}}
}
@available(OSX, unavailable)
class UnavailableSubclassOfSomeGenericClassOfProtocolAvailableOn51 : SomeGenericClass<ProtocolAvailableOn51> {
}
func GenericWhereClause<T>(_ t: T) where T: ProtocolAvailableOn51 { // expected-error * {{'ProtocolAvailableOn51' is only available in macOS 51 or newer}}
// expected-note@-1 * {{add @available attribute to enclosing global function}}
}
@available(OSX, unavailable)
func UnavailableGenericWhereClause<T>(_ t: T) where T: ProtocolAvailableOn51 {
}
func GenericSignature<T : ProtocolAvailableOn51>(_ t: T) { // expected-error * {{'ProtocolAvailableOn51' is only available in macOS 51 or newer}}
// expected-note@-1 * {{add @available attribute to enclosing global function}}
}
@available(OSX, unavailable)
func UnavailableGenericSignature<T : ProtocolAvailableOn51>(_ t: T) {
}
struct GenericType<T> { // expected-note {{add @available attribute to enclosing generic struct}}
func nonGenericWhereClause() where T : ProtocolAvailableOn51 {} // expected-error {{'ProtocolAvailableOn51' is only available in macOS 51 or newer}}
// expected-note@-1 {{add @available attribute to enclosing instance method}}
@available(OSX, unavailable)
func unavailableNonGenericWhereClause() where T : ProtocolAvailableOn51 {}
struct NestedType where T : ProtocolAvailableOn51 {} // expected-error {{'ProtocolAvailableOn51' is only available in macOS 51 or newer}}
// expected-note@-1 2{{add @available attribute to enclosing struct}}
@available(OSX, unavailable)
struct UnavailableNestedType where T : ProtocolAvailableOn51 {}
}
// Extensions
extension ClassAvailableOn51 { } // expected-error {{'ClassAvailableOn51' is only available in macOS 51 or newer}}
// expected-note@-1 {{add @available attribute to enclosing extension}}
@available(OSX, unavailable)
extension ClassAvailableOn51 { }
@available(OSX, introduced: 51)
extension ClassAvailableOn51 {
func m() {
// expected-note@-1 {{add @available attribute to enclosing instance method}}
let _ = globalFuncAvailableOn51()
let _ = globalFuncAvailableOn52() // expected-error {{'globalFuncAvailableOn52()' is only available in macOS 52 or newer}}
// expected-note@-1 {{add 'if #available' version check}}
}
}
class ClassToExtend { }
@available(OSX, introduced: 51)
extension ClassToExtend {
func extensionMethod() { }
@available(OSX, introduced: 52)
func extensionMethod52() { }
class ExtensionClass { }
// We rely on not allowing nesting of extensions, so test to make sure
// this emits an error.
// CHECK:error: declaration is only valid at file scope
extension ClassToExtend { } // expected-error {{declaration is only valid at file scope}}
}
// We allow protocol extensions for protocols that are less available than the
// conforming class.
extension ClassToExtend : ProtocolAvailableOn51 {
}
@available(OSX, introduced: 51)
extension ClassToExtend { // expected-note {{enclosing scope requires availability of macOS 51 or newer}}
@available(OSX, introduced: 10.9) // expected-error {{instance method cannot be more available than enclosing scope}}
func extensionMethod10_9() { }
}
func usePotentiallyUnavailableExtension() {
// expected-note@-1 3{{add @available attribute to enclosing global function}}
let o = ClassToExtend()
o.extensionMethod() // expected-error {{'extensionMethod()' is only available in macOS 51 or newer}}
// expected-note@-1 {{add 'if #available' version check}}
let _ = ClassToExtend.ExtensionClass() // expected-error {{'ExtensionClass' is only available in macOS 51 or newer}}
// expected-note@-1 {{add 'if #available' version check}}
o.extensionMethod52() // expected-error {{'extensionMethod52()' is only available in macOS 52 or newer}}
// expected-note@-1 {{add 'if #available' version check}}
}
// Availability of synthesized designated initializers.
@available(OSX, introduced: 51)
class WidelyAvailableBase {
init() {}
@available(OSX, introduced: 52)
init(thing: ()) {}
}
@available(OSX, introduced: 53)
class EsotericSmallBatchHipsterThing : WidelyAvailableBase {}
@available(OSX, introduced: 53)
class NestedClassTest {
class InnerClass : WidelyAvailableBase {}
}
// Useless #available(...) checks
func functionWithDefaultAvailabilityAndUselessCheck(_ p: Bool) {
// Default availability reflects minimum deployment: 10.9 and up
if #available(OSX 10.9, *) { // no-warning
let _ = globalFuncAvailableOn10_9()
}
if #available(OSX 51, *) { // expected-note {{enclosing scope here}}
let _ = globalFuncAvailableOn51()
if #available(OSX 51, *) { // expected-warning {{unnecessary check for 'macOS'; enclosing scope ensures guard will always be true}}
let _ = globalFuncAvailableOn51()
}
}
if #available(OSX 10.9, *) { // expected-note {{enclosing scope here}}
} else {
// Make sure we generate a warning about an unnecessary check even if the else branch of if is dead.
if #available(OSX 51, *) { // expected-warning {{unnecessary check for 'macOS'; enclosing scope ensures guard will always be true}}
}
}
// This 'if' is strictly to limit the scope of the guard fallthrough
if p {
guard #available(OSX 10.9, *) else { // expected-note {{enclosing scope here}}
// Make sure we generate a warning about an unnecessary check even if the else branch of guard is dead.
if #available(OSX 51, *) { // expected-warning {{unnecessary check for 'macOS'; enclosing scope ensures guard will always be true}}
}
}
}
// We don't want * generate a warn about useless checks; the check may be required on
// another platform
if #available(iOS 8.0, *) {
}
if #available(OSX 51, *) {
// Similarly do not want '*' to generate a warning in a refined TRC.
if #available(iOS 8.0, *) {
}
}
}
@available(OSX, unavailable)
func explicitlyUnavailable() { } // expected-note 2{{'explicitlyUnavailable()' has been explicitly marked unavailable here}}
func functionWithUnavailableInDeadBranch() {
if #available(iOS 8.0, *) {
} else {
// This branch is dead on OSX, so we shouldn't a warning about use of potentially unavailable APIs in it.
_ = globalFuncAvailableOn51() // no-warning
@available(OSX 51, *)
func localFuncAvailableOn51() {
_ = globalFuncAvailableOn52() // no-warning
}
localFuncAvailableOn51() // no-warning
explicitlyUnavailable() // expected-error {{'explicitlyUnavailable()' is unavailable}}
}
guard #available(iOS 8.0, *) else {
_ = globalFuncAvailableOn51() // no-warning
explicitlyUnavailable() // expected-error {{'explicitlyUnavailable()' is unavailable}}
}
}
@available(OSX, introduced: 51)
func functionWithSpecifiedAvailabilityAndUselessCheck() { // expected-note 2{{enclosing scope here}}
if #available(OSX 10.9, *) { // expected-warning {{unnecessary check for 'macOS'; enclosing scope ensures guard will always be true}}
let _ = globalFuncAvailableOn10_9()
}
if #available(OSX 51, *) { // expected-warning {{unnecessary check for 'macOS'; enclosing scope ensures guard will always be true}}
let _ = globalFuncAvailableOn51()
}
}
// #available(...) outside if statement guards
func injectToOptional<T>(_ v: T) -> T? {
return v
}
if let _ = injectToOptional(5), #available(OSX 52, *) {} // ok
// Refining context inside guard
if #available(OSX 51, *),
let _ = injectToOptional(globalFuncAvailableOn51()),
let _ = injectToOptional(globalFuncAvailableOn52()) { // expected-error {{'globalFuncAvailableOn52()' is only available in macOS 52 or newer}}
// expected-note@-1 {{add 'if #available' version check}}
let _ = globalFuncAvailableOn51()
let _ = globalFuncAvailableOn52() // expected-error {{'globalFuncAvailableOn52()' is only available in macOS 52 or newer}}
// expected-note@-1 {{add 'if #available' version check}}
}
if let _ = injectToOptional(5), #available(OSX 51, *),
let _ = injectToOptional(globalFuncAvailableOn51()),
let _ = injectToOptional(globalFuncAvailableOn52()) { // expected-error {{'globalFuncAvailableOn52()' is only available in macOS 52 or newer}}
// expected-note@-1 {{add 'if #available' version check}}
let _ = globalFuncAvailableOn51()
let _ = globalFuncAvailableOn52() // expected-error {{'globalFuncAvailableOn52()' is only available in macOS 52 or newer}}
// expected-note@-1 {{add 'if #available' version check}}
}
if let _ = injectToOptional(globalFuncAvailableOn51()), #available(OSX 51, *), // expected-error {{'globalFuncAvailableOn51()' is only available in macOS 51 or newer}}
// expected-note@-1 {{add 'if #available' version check}}
let _ = injectToOptional(globalFuncAvailableOn52()) { // expected-error {{'globalFuncAvailableOn52()' is only available in macOS 52 or newer}}
// expected-note@-1 {{add 'if #available' version check}}
}
if let _ = injectToOptional(5), #available(OSX 51, *), // expected-note {{enclosing scope here}}
let _ = injectToOptional(6), #available(OSX 51, *) { // expected-warning {{unnecessary check for 'macOS'; enclosing scope ensures guard will always be true}}
}
// Tests for the guard control construct.
func useGuardAvailable() {
// expected-note@-1 3{{add @available attribute to enclosing global function}}
// Guard fallthrough should refine context
guard #available(OSX 51, *) else { // expected-note {{enclosing scope here}}
let _ = globalFuncAvailableOn51() // expected-error {{'globalFuncAvailableOn51()' is only available in macOS 51 or newer}}
// expected-note@-1 {{add 'if #available' version check}}
return
}
let _ = globalFuncAvailableOn51()
let _ = globalFuncAvailableOn52() // expected-error {{'globalFuncAvailableOn52()' is only available in macOS 52 or newer}}
// expected-note@-1 {{add 'if #available' version check}}
if #available(OSX 51, *) { // expected-warning {{unnecessary check for 'macOS'; enclosing scope ensures guard will always be true}}
}
if globalFuncAvailableOn51() > 0 {
guard #available(OSX 52, *),
let x = injectToOptional(globalFuncAvailableOn52()) else { return }
_ = x
}
let _ = globalFuncAvailableOn52() // expected-error {{'globalFuncAvailableOn52()' is only available in macOS 52 or newer}}
// expected-note@-1 {{add 'if #available' version check}}
}
func twoGuardsInSameBlock(_ p: Int) {
// expected-note@-1 {{add @available attribute to enclosing global function}}
if (p > 0) {
guard #available(OSX 51, *) else { return }
let _ = globalFuncAvailableOn51()
guard #available(OSX 52, *) else { return }
let _ = globalFuncAvailableOn52()
}
let _ = globalFuncAvailableOn51() // expected-error {{'globalFuncAvailableOn51()' is only available in macOS 51 or newer}}
// expected-note@-1 {{add 'if #available' version check}}
}
// Refining while loops
while globalFuncAvailableOn51() > 10 { } // expected-error {{'globalFuncAvailableOn51()' is only available in macOS 51 or newer}}
// expected-note@-1 {{add 'if #available' version check}}
while #available(OSX 51, *), // expected-note {{enclosing scope here}}
globalFuncAvailableOn51() > 10 {
let _ = globalFuncAvailableOn51()
let _ = globalFuncAvailableOn52() // expected-error {{'globalFuncAvailableOn52()' is only available in macOS 52 or newer}}
// expected-note@-1 {{add 'if #available' version check}}
while globalFuncAvailableOn51() > 11,
let _ = injectToOptional(5),
#available(OSX 52, *) {
let _ = globalFuncAvailableOn52();
}
while #available(OSX 51, *) { // expected-warning {{unnecessary check for 'macOS'; enclosing scope ensures guard will always be true}}
}
}
// Tests for Fix-It replacement text
// The whitespace in the replacement text is particularly important here -- it reflects the level
// of indentation for the added if #available() or @available attribute. Note that, for the moment, we hard
// code *added* indentation in Fix-Its as 4 spaces (that is, when indenting in a Fix-It, we
// take whatever indentation was there before and add 4 spaces to it).
functionAvailableOn51()
// expected-error@-1 {{'functionAvailableOn51()' is only available in macOS 51 or newer}}
// expected-note@-2 {{add 'if #available' version check}} {{1-24=if #available(macOS 51, *) {\n functionAvailableOn51()\n} else {\n // Fallback on earlier versions\n}}}
let declForFixitAtTopLevel: ClassAvailableOn51? = nil
// expected-error@-1 {{'ClassAvailableOn51' is only available in macOS 51 or newer}}
// expected-note@-2 {{add 'if #available' version check}} {{1-54=if #available(macOS 51, *) {\n let declForFixitAtTopLevel: ClassAvailableOn51? = nil\n} else {\n // Fallback on earlier versions\n}}}
func fixitForReferenceInGlobalFunction() {
// expected-note@-1 {{add @available attribute to enclosing global function}} {{1-1=@available(macOS 51, *)\n}}
functionAvailableOn51()
// expected-error@-1 {{'functionAvailableOn51()' is only available in macOS 51 or newer}}
// expected-note@-2 {{add 'if #available' version check}} {{3-26=if #available(macOS 51, *) {\n functionAvailableOn51()\n } else {\n // Fallback on earlier versions\n }}}
}
public func fixitForReferenceInGlobalFunctionWithDeclModifier() {
// expected-note@-1 {{add @available attribute to enclosing global function}} {{1-1=@available(macOS 51, *)\n}}
functionAvailableOn51()
// expected-error@-1 {{'functionAvailableOn51()' is only available in macOS 51 or newer}}
// expected-note@-2 {{add 'if #available' version check}} {{3-26=if #available(macOS 51, *) {\n functionAvailableOn51()\n } else {\n // Fallback on earlier versions\n }}}
}
func fixitForReferenceInGlobalFunctionWithAttribute() -> Never {
// expected-note@-1 {{add @available attribute to enclosing global function}} {{1-1=@available(macOS 51, *)\n}}
_ = 0 // Avoid treating the call to functionAvailableOn51 as an implicit return
functionAvailableOn51()
// expected-error@-1 {{'functionAvailableOn51()' is only available in macOS 51 or newer}}
// expected-note@-2 {{add 'if #available' version check}} {{3-26=if #available(macOS 51, *) {\n functionAvailableOn51()\n } else {\n // Fallback on earlier versions\n }}}
}
func takesAutoclosure(_ c : @autoclosure () -> ()) {
}
class ClassForFixit {
// expected-note@-1 12{{add @available attribute to enclosing class}} {{1-1=@available(macOS 51, *)\n}}
func fixitForReferenceInMethod() {
// expected-note@-1 {{add @available attribute to enclosing instance method}} {{3-3=@available(macOS 51, *)\n }}
functionAvailableOn51()
// expected-error@-1 {{'functionAvailableOn51()' is only available in macOS 51 or newer}}
// expected-note@-2 {{add 'if #available' version check}} {{5-28=if #available(macOS 51, *) {\n functionAvailableOn51()\n } else {\n // Fallback on earlier versions\n }}}
}
func fixitForReferenceNestedInMethod() {
// expected-note@-1 3{{add @available attribute to enclosing instance method}} {{3-3=@available(macOS 51, *)\n }}
func inner() {
functionAvailableOn51()
// expected-error@-1 {{'functionAvailableOn51()' is only available in macOS 51 or newer}}
// expected-note@-2 {{add 'if #available' version check}} {{7-30=if #available(macOS 51, *) {\n functionAvailableOn51()\n } else {\n // Fallback on earlier versions\n }}}
}
let _: () -> () = { () in
functionAvailableOn51()
// expected-error@-1 {{'functionAvailableOn51()' is only available in macOS 51 or newer}}
// expected-note@-2 {{add 'if #available' version check}} {{7-30=if #available(macOS 51, *) {\n functionAvailableOn51()\n } else {\n // Fallback on earlier versions\n }}}
}
takesAutoclosure(functionAvailableOn51())
// expected-error@-1 {{'functionAvailableOn51()' is only available in macOS 51 or newer}}
// expected-note@-2 {{add 'if #available' version check}} {{5-46=if #available(macOS 51, *) {\n takesAutoclosure(functionAvailableOn51())\n } else {\n // Fallback on earlier versions\n }}}
}
var fixitForReferenceInPropertyAccessor: Int {
// expected-note@-1 {{add @available attribute to enclosing property}} {{3-3=@available(macOS 51, *)\n }}
get {
functionAvailableOn51()
// expected-error@-1 {{'functionAvailableOn51()' is only available in macOS 51 or newer}}
// expected-note@-2 {{add 'if #available' version check}} {{7-30=if #available(macOS 51, *) {\n functionAvailableOn51()\n } else {\n // Fallback on earlier versions\n }}}
return 5
}
}
var fixitForReferenceInPropertyType: ClassAvailableOn51? = nil
// expected-error@-1 {{'ClassAvailableOn51' is only available in macOS 51 or newer}}
lazy var fixitForReferenceInLazyPropertyType: ClassAvailableOn51? = nil
// expected-error@-1 {{'ClassAvailableOn51' is only available in macOS 51 or newer}}
private lazy var fixitForReferenceInPrivateLazyPropertyType: ClassAvailableOn51? = nil
// expected-error@-1 {{'ClassAvailableOn51' is only available in macOS 51 or newer}}
lazy private var fixitForReferenceInLazyPrivatePropertyType: ClassAvailableOn51? = nil
// expected-error@-1 {{'ClassAvailableOn51' is only available in macOS 51 or newer}}
static var fixitForReferenceInStaticPropertyType: ClassAvailableOn51? = nil
// expected-error@-1 {{'ClassAvailableOn51' is only available in macOS 51 or newer}}
// expected-note@-2 {{add @available attribute to enclosing static property}} {{3-3=@available(macOS 51, *)\n }}
var fixitForReferenceInPropertyTypeMultiple: ClassAvailableOn51? = nil, other: Int = 7
// expected-error@-1 {{'ClassAvailableOn51' is only available in macOS 51 or newer}}
func fixitForRefInGuardOfIf() {
// expected-note@-1 {{add @available attribute to enclosing instance method}} {{3-3=@available(macOS 51, *)\n }}
if (globalFuncAvailableOn51() > 1066) {
let _ = 5
let _ = 6
}
// expected-error@-4 {{'globalFuncAvailableOn51()' is only available in macOS 51 or newer}}
// expected-note@-5 {{add 'if #available' version check}} {{5-+3:6=if #available(macOS 51, *) {\n if (globalFuncAvailableOn51() > 1066) {\n let _ = 5\n let _ = 6\n }\n } else {\n // Fallback on earlier versions\n }}}
}
}
extension ClassToExtend {
// expected-note@-1 {{add @available attribute to enclosing extension}}
func fixitForReferenceInExtensionMethod() {
// expected-note@-1 {{add @available attribute to enclosing instance method}} {{3-3=@available(macOS 51, *)\n }}
functionAvailableOn51()
// expected-error@-1 {{'functionAvailableOn51()' is only available in macOS 51 or newer}}
// expected-note@-2 {{add 'if #available' version check}} {{5-28=if #available(macOS 51, *) {\n functionAvailableOn51()\n } else {\n // Fallback on earlier versions\n }}}
}
}
enum EnumForFixit {
// expected-note@-1 2{{add @available attribute to enclosing enum}} {{1-1=@available(macOS 51, *)\n}}
case CaseWithPotentiallyUnavailablePayload(p: ClassAvailableOn51)
// expected-error@-1 {{'ClassAvailableOn51' is only available in macOS 51 or newer}}
case CaseWithPotentiallyUnavailablePayload2(p: ClassAvailableOn51), WithoutPayload
// expected-error@-1 {{'ClassAvailableOn51' is only available in macOS 51 or newer}}
}
@objc
class Y {
var z = 0
}
@objc
class X {
@objc var y = Y()
}
func testForFixitWithNestedMemberRefExpr() {
// expected-note@-1 2{{add @available attribute to enclosing global function}} {{1-1=@available(macOS 52, *)\n}}
let x = X()
x.y.z = globalFuncAvailableOn52()
// expected-error@-1 {{'globalFuncAvailableOn52()' is only available in macOS 52 or newer}}
// expected-note@-2 {{add 'if #available' version check}} {{3-36=if #available(macOS 52, *) {\n x.y.z = globalFuncAvailableOn52()\n } else {\n // Fallback on earlier versions\n }}}
// Access via dynamic member reference
let anyX: AnyObject = x
anyX.y?.z = globalFuncAvailableOn52()
// expected-error@-1 {{'globalFuncAvailableOn52()' is only available in macOS 52 or newer}}
// expected-note@-2 {{add 'if #available' version check}} {{3-40=if #available(macOS 52, *) {\n anyX.y?.z = globalFuncAvailableOn52()\n } else {\n // Fallback on earlier versions\n }}}
}
// Protocol Conformances
protocol ProtocolWithRequirementMentioningPotentiallyUnavailable {
// expected-note@-1 2{{add @available attribute to enclosing protocol}}
func hasPotentiallyUnavailableParameter(_ p: ClassAvailableOn51) // expected-error * {{'ClassAvailableOn51' is only available in macOS 51 or newer}}
// expected-note@-1 * {{add @available attribute to enclosing instance method}}
func hasPotentiallyUnavailableReturn() -> ClassAvailableOn51 // expected-error * {{'ClassAvailableOn51' is only available in macOS 51 or newer}}
// expected-note@-1 * {{add @available attribute to enclosing instance method}}
@available(OSX 51, *)
func hasPotentiallyUnavailableWithAnnotation(_ p: ClassAvailableOn51) -> ClassAvailableOn51
}
protocol HasMethodF {
associatedtype T
func f(_ p: T) // expected-note 3{{protocol requirement here}}
}
class TriesToConformWithFunctionIntroducedOn51 : HasMethodF {
@available(OSX, introduced: 51)
func f(_ p: Int) { } // expected-error {{protocol 'HasMethodF' requires 'f' to be available in macOS 50 and newer}}
}
class ConformsWithFunctionIntroducedOnMinimumDeploymentTarget : HasMethodF {
// Even though this function is less available than its requirement,
// it is available on a deployment targets, so the conformance is safe.
@available(OSX, introduced: 10.9)
func f(_ p: Int) { }
}
class SuperHasMethodF {
@available(OSX, introduced: 51)
func f(_ p: Int) { } // expected-note {{'f' declared here}}
}
class TriesToConformWithPotentiallyUnavailableFunctionInSuperClass : SuperHasMethodF, HasMethodF { // expected-error {{protocol 'HasMethodF' requires 'f' to be available in macOS 50 and newer}}
// The conformance here is generating an error on f in the super class.
}
@available(OSX, introduced: 51)
class ConformsWithPotentiallyUnavailableFunctionInSuperClass : SuperHasMethodF, HasMethodF {
// Limiting this class to only be available on 51 and newer means that
// the witness in SuperHasMethodF is safe for the requirement on HasMethodF.
// in order for this class to be referenced we must be running on 51 or
// greater.
}
class ConformsByOverridingFunctionInSuperClass : SuperHasMethodF, HasMethodF {
// Now the witness is this f() (which is always available) and not the f()
// from the super class, so conformance is safe.
override func f(_ p: Int) { }
}
// Attempt to conform in protocol extension with unavailable witness
// in extension
class HasNoMethodF1 { }
extension HasNoMethodF1 : HasMethodF {
@available(OSX, introduced: 51)
func f(_ p: Int) { } // expected-error {{protocol 'HasMethodF' requires 'f' to be available in macOS 50 and newer}}
}
class HasNoMethodF2 { }
@available(OSX, introduced: 51)
extension HasNoMethodF2 : HasMethodF {
// This is OK, because the conformance was introduced by an extension.
func f(_ p: Int) { }
}
@available(OSX, introduced: 51)
class HasNoMethodF3 { }
@available(OSX, introduced: 51)
extension HasNoMethodF3 : HasMethodF {
// We expect this conformance to succeed because on every version where HasNoMethodF3
// is available, HasNoMethodF3's f() is as available as the protocol requirement
func f(_ p: Int) { }
}
@available(OSX, introduced: 51)
protocol HasMethodFOn51 {
func f(_ p: Int)
}
class ConformsToPotentiallyUnavailableProtocolWithPotentiallyUnavailableWitness : HasMethodFOn51 {
@available(OSX, introduced: 51)
func f(_ p: Int) { }
}
@available(OSX, introduced: 51)
class HasNoMethodF4 { }
@available(OSX, introduced: 52)
extension HasNoMethodF4 : HasMethodFOn51 {
// This is OK, because the conformance was introduced by an extension.
func f(_ p: Int) { }
}
@available(OSX, introduced: 51)
protocol HasTakesClassAvailableOn51 {
func takesClassAvailableOn51(_ o: ClassAvailableOn51) // expected-note 2{{protocol requirement here}}
}
class AttemptsToConformToHasTakesClassAvailableOn51 : HasTakesClassAvailableOn51 {
@available(OSX, introduced: 52)
func takesClassAvailableOn51(_ o: ClassAvailableOn51) { // expected-error {{protocol 'HasTakesClassAvailableOn51' requires 'takesClassAvailableOn51' to be available in macOS 51 and newer}}
}
}
class ConformsToHasTakesClassAvailableOn51 : HasTakesClassAvailableOn51 {
@available(OSX, introduced: 51)
func takesClassAvailableOn51(_ o: ClassAvailableOn51) {
}
}
class TakesClassAvailableOn51_A { }
extension TakesClassAvailableOn51_A : HasTakesClassAvailableOn51 {
@available(OSX, introduced: 52)
func takesClassAvailableOn51(_ o: ClassAvailableOn51) { // expected-error {{protocol 'HasTakesClassAvailableOn51' requires 'takesClassAvailableOn51' to be available in macOS 51 and newer}}
}
}
class TakesClassAvailableOn51_B { }
extension TakesClassAvailableOn51_B : HasTakesClassAvailableOn51 {
@available(OSX, introduced: 51)
func takesClassAvailableOn51(_ o: ClassAvailableOn51) {
}
}
// We want conditional availability to play a role in picking a witness for a
// protocol requirement.
class TestAvailabilityAffectsWitnessCandidacy : HasMethodF {
// Test that we choose the less specialized witness, because the more specialized
// witness is conditionally unavailable.
@available(OSX, introduced: 51)
func f(_ p: Int) { }
func f<T>(_ p: T) { }
}
protocol HasPotentiallyUnavailableMethodF {
@available(OSX, introduced: 51)
func f(_ p: String)
}
class ConformsWithPotentiallyUnavailableFunction : HasPotentiallyUnavailableMethodF {
@available(OSX, introduced: 10.9)
func f(_ p: String) { }
}
func usePotentiallyUnavailableProtocolMethod(_ h: HasPotentiallyUnavailableMethodF) {
// expected-note@-1 {{add @available attribute to enclosing global function}}
h.f("Foo") // expected-error {{'f' is only available in macOS 51 or newer}}
// expected-note@-1 {{add 'if #available' version check}}
}
func usePotentiallyUnavailableProtocolMethod<H : HasPotentiallyUnavailableMethodF> (_ h: H) {
// expected-note@-1 {{add @available attribute to enclosing global function}}
h.f("Foo") // expected-error {{'f' is only available in macOS 51 or newer}}
// expected-note@-1 {{add 'if #available' version check}}
}
// Short-form @available() annotations
@available(OSX 51, *)
class ClassWithShortFormAvailableOn51 {
}
@available(OSX 53, *)
class ClassWithShortFormAvailableOn53 {
}
@available(OSX 54, *)
class ClassWithShortFormAvailableOn54 {
}
@available(OSX 10.9, *)
func funcWithShortFormAvailableOn10_9() {
let _ = ClassWithShortFormAvailableOn51() // expected-error {{'ClassWithShortFormAvailableOn51' is only available in macOS 51 or newer}}
// expected-note@-1 {{add 'if #available' version check}}
}
@available(OSX 51, *)
func funcWithShortFormAvailableOn51() {
let _ = ClassWithShortFormAvailableOn51()
}
@available(iOS 14.0, *)
func funcWithShortFormAvailableOniOS14() {
// expected-note@-1 {{add @available attribute to enclosing global function}}
let _ = ClassWithShortFormAvailableOn51() // expected-error {{'ClassWithShortFormAvailableOn51' is only available in macOS 51 or newer}}
// expected-note@-1 {{add 'if #available' version check}}
}
@available(iOS 14.0, OSX 53, *)
func funcWithShortFormAvailableOniOS14AndOSX53() {
let _ = ClassWithShortFormAvailableOn51()
}
// Not idiomatic but we need to be able to handle it.
@available(iOS 8.0, *)
@available(OSX 51, *)
func funcWithMultipleShortFormAnnotationsForDifferentPlatforms() {
let _ = ClassWithShortFormAvailableOn51()
}
@available(OSX 51, *)
@available(OSX 53, *)
@available(OSX 52, *)
func funcWithMultipleShortFormAnnotationsForTheSamePlatform() {
let _ = ClassWithShortFormAvailableOn53()
let _ = ClassWithShortFormAvailableOn54() // expected-error {{'ClassWithShortFormAvailableOn54' is only available in macOS 54 or newer}}
// expected-note@-1 {{add 'if #available' version check}}
}
func useShortFormAvailable() {
// expected-note@-1 4{{add @available attribute to enclosing global function}}
funcWithShortFormAvailableOn10_9()
funcWithShortFormAvailableOn51() // expected-error {{'funcWithShortFormAvailableOn51()' is only available in macOS 51 or newer}}
// expected-note@-1 {{add 'if #available' version check}}
funcWithShortFormAvailableOniOS14()
funcWithShortFormAvailableOniOS14AndOSX53() // expected-error {{'funcWithShortFormAvailableOniOS14AndOSX53()' is only available in macOS 53 or newer}}
// expected-note@-1 {{add 'if #available' version check}}
funcWithMultipleShortFormAnnotationsForDifferentPlatforms() // expected-error {{'funcWithMultipleShortFormAnnotationsForDifferentPlatforms()' is only available in macOS 51 or newer}}
// expected-note@-1 {{add 'if #available' version check}}
funcWithMultipleShortFormAnnotationsForTheSamePlatform() // expected-error {{'funcWithMultipleShortFormAnnotationsForTheSamePlatform()' is only available in macOS 53 or newer}}
// expected-note@-1 {{add 'if #available' version check}}
}
// Unavailability takes precedence over availability and is inherited
@available(OSX 10.9, *)
@available(OSX, unavailable)
func unavailableWins() { }
// expected-note@-1 {{'unavailableWins()' has been explicitly marked unavailable here}}
struct HasUnavailableExtension {
@available(OSX, unavailable)
public func directlyUnavailable() { }
// expected-note@-1 {{'directlyUnavailable()' has been explicitly marked unavailable here}}
}
@available(OSX, unavailable)
extension HasUnavailableExtension {
public func inheritsUnavailable() { }
// expected-note@-1 {{'inheritsUnavailable()' has been explicitly marked unavailable here}}
@available(OSX 10.9, *)
public func moreAvailableButStillUnavailable() { }
// expected-note@-1 {{'moreAvailableButStillUnavailable()' has been explicitly marked unavailable here}}
}
func useHasUnavailableExtension(_ s: HasUnavailableExtension) {
unavailableWins() // expected-error {{'unavailableWins()' is unavailable}}
s.directlyUnavailable() // expected-error {{'directlyUnavailable()' is unavailable}}
s.inheritsUnavailable() // expected-error {{'inheritsUnavailable()' is unavailable in macOS}}
s.moreAvailableButStillUnavailable() // expected-error {{'moreAvailableButStillUnavailable()' is unavailable in macOS}}
}
@available(macOS 10.15, *)
func f() -> Int { 17 }
class StoredPropertiesWithAvailabilityInClosures {
private static let value: Int = {
if #available(macOS 10.15, *) {
return f()
}
return 0
}()
@available(macOS 10.14, *)
private static let otherValue: Int = {
if #available(macOS 10.15, *) {
return f()
}
return 0
}()
}
|