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
|
//===--- LifetimeDependence.cpp -----------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2024 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
#include "swift/AST/LifetimeDependence.h"
#include "swift/AST/ASTContext.h"
#include "swift/AST/Builtins.h"
#include "swift/AST/ConformanceLookup.h"
#include "swift/AST/Decl.h"
#include "swift/AST/DiagnosticsSema.h"
#include "swift/AST/Module.h"
#include "swift/AST/ParameterList.h"
#include "swift/AST/SourceFile.h"
#include "swift/AST/Type.h"
#include "swift/AST/TypeRepr.h"
#include "swift/Basic/Assertions.h"
#include "swift/Basic/Defer.h"
#include "swift/Basic/Range.h"
#include "swift/Basic/SourceManager.h"
namespace swift {
LifetimeEntry *
LifetimeEntry::create(const ASTContext &ctx, SourceLoc startLoc,
SourceLoc endLoc, ArrayRef<LifetimeDescriptor> sources,
std::optional<LifetimeDescriptor> targetDescriptor) {
unsigned size = totalSizeToAlloc<LifetimeDescriptor>(sources.size());
void *mem = ctx.Allocate(size, alignof(LifetimeEntry));
return new (mem) LifetimeEntry(startLoc, endLoc, sources, targetDescriptor);
}
std::string LifetimeEntry::getString() const {
std::string result = "(";
if (targetDescriptor.has_value()) {
result += targetDescriptor->getString();
result += ": ";
}
bool firstElem = true;
for (auto source : getSources()) {
if (!firstElem) {
result += ", ";
}
auto lifetimeKind = source.getParsedLifetimeDependenceKind();
auto kindString = getNameForParsedLifetimeDependenceKind(lifetimeKind);
bool printSpace = (lifetimeKind == ParsedLifetimeDependenceKind::Borrow ||
lifetimeKind == ParsedLifetimeDependenceKind::Inherit);
if (!kindString.empty()) {
result += kindString;
}
if (printSpace) {
result += " ";
}
result += source.getString();
firstElem = false;
}
result += ")";
return result;
}
std::optional<LifetimeDependenceInfo>
getLifetimeDependenceFor(ArrayRef<LifetimeDependenceInfo> lifetimeDependencies,
unsigned index) {
for (auto dep : lifetimeDependencies) {
if (dep.getTargetIndex() == index) {
return dep;
}
}
return std::nullopt;
}
bool
filterEscapableLifetimeDependencies(GenericSignature sig,
ArrayRef<LifetimeDependenceInfo> inputs,
SmallVectorImpl<LifetimeDependenceInfo> &outputs,
llvm::function_ref<Type (unsigned targetIndex)> getSubstTargetType) {
bool didRemoveLifetimeDependencies = false;
for (auto &depInfo : inputs) {
auto targetIndex = depInfo.getTargetIndex();
Type substTy = getSubstTargetType(targetIndex);
// If the type still contains type variables we don't know whether we
// can drop the dependency.
if (substTy->hasTypeVariable())
continue;
// Drop the dependency if the target type is Escapable.
if (sig || !substTy->hasTypeParameter()) {
if (substTy->isEscapable(sig)) {
didRemoveLifetimeDependencies = true;
continue;
}
}
// Otherwise, keep the dependency.
outputs.push_back(depInfo);
}
return didRemoveLifetimeDependencies;
}
StringRef
getNameForParsedLifetimeDependenceKind(ParsedLifetimeDependenceKind kind) {
switch (kind) {
case ParsedLifetimeDependenceKind::Borrow:
return "borrow";
case ParsedLifetimeDependenceKind::Inherit:
return "copy";
case ParsedLifetimeDependenceKind::Inout:
return "&";
default:
return "";
}
}
std::string LifetimeDependenceInfo::getString() const {
std::string lifetimeDependenceString = "@lifetime(";
auto addressable = getAddressableIndices();
auto condAddressable = getConditionallyAddressableIndices();
auto getSourceString = [&](IndexSubset *bitvector, StringRef kind) {
std::string result;
bool isFirstSetBit = true;
for (unsigned i = 0; i < bitvector->getCapacity(); i++) {
if (bitvector->contains(i)) {
if (!isFirstSetBit) {
result += ", ";
}
result += kind;
if (addressable && addressable->contains(i)) {
result += "address ";
} else if (condAddressable && condAddressable->contains(i)) {
result += "address_for_deps ";
}
result += std::to_string(i);
isFirstSetBit = false;
}
}
return result;
};
if (inheritLifetimeParamIndices) {
assert(!inheritLifetimeParamIndices->isEmpty());
lifetimeDependenceString +=
getSourceString(inheritLifetimeParamIndices, "copy ");
}
if (scopeLifetimeParamIndices) {
assert(!scopeLifetimeParamIndices->isEmpty());
if (inheritLifetimeParamIndices) {
lifetimeDependenceString += ", ";
}
lifetimeDependenceString +=
getSourceString(scopeLifetimeParamIndices, "borrow ");
}
if (isImmortal()) {
lifetimeDependenceString += "immortal";
}
lifetimeDependenceString += ") ";
return lifetimeDependenceString;
}
void LifetimeDependenceInfo::Profile(llvm::FoldingSetNodeID &ID) const {
ID.AddBoolean(addressableParamIndicesAndImmortal.getInt());
ID.AddInteger(targetIndex);
if (inheritLifetimeParamIndices) {
ID.AddInteger((uint8_t)LifetimeDependenceKind::Inherit);
inheritLifetimeParamIndices->Profile(ID);
}
if (scopeLifetimeParamIndices) {
ID.AddInteger((uint8_t)LifetimeDependenceKind::Scope);
scopeLifetimeParamIndices->Profile(ID);
}
if (addressableParamIndicesAndImmortal.getPointer()) {
ID.AddBoolean(true);
addressableParamIndicesAndImmortal.getPointer()->Profile(ID);
} else {
ID.AddBoolean(false);
}
}
// Warning: this is incorrect for Setter 'newValue' parameters. It should only
// be called for a Setter's 'self'.
static ValueOwnership getLoweredOwnership(AbstractFunctionDecl *afd) {
if (isa<ConstructorDecl>(afd)) {
return ValueOwnership::Owned;
}
if (auto *ad = dyn_cast<AccessorDecl>(afd)) {
if (ad->getAccessorKind() == AccessorKind::Set ||
isYieldingMutableAccessor(ad->getAccessorKind())) {
return ValueOwnership::InOut;
}
}
return ValueOwnership::Shared;
}
static bool isBitwiseCopyable(Type type, ASTContext &ctx) {
auto *bitwiseCopyableProtocol =
ctx.getProtocol(KnownProtocolKind::BitwiseCopyable);
if (!bitwiseCopyableProtocol) {
return false;
}
return (bool)checkConformance(type, bitwiseCopyableProtocol);
}
static bool isDiagnosedNonEscapable(Type type) {
if (type->hasError()) {
return false;
}
// FIXME: This check is temporary until rdar://139976667 is fixed.
// ModuleType created with ModuleType::get methods are ~Copyable and
// ~Escapable because the Copyable and Escapable conformance is not added to
// them by default.
if (type->is<ModuleType>()) {
return false;
}
return !type->isEscapable();
}
void LifetimeDependenceInfo::getConcatenatedData(
SmallVectorImpl<bool> &concatenatedData) const {
auto pushData = [&](IndexSubset *paramIndices) {
if (paramIndices == nullptr) {
return;
}
assert(!paramIndices->isEmpty());
for (unsigned i = 0; i < paramIndices->getCapacity(); i++) {
if (paramIndices->contains(i)) {
concatenatedData.push_back(true);
continue;
}
concatenatedData.push_back(false);
}
};
if (hasInheritLifetimeParamIndices()) {
pushData(inheritLifetimeParamIndices);
}
if (hasScopeLifetimeParamIndices()) {
pushData(scopeLifetimeParamIndices);
}
if (hasAddressableParamIndices()) {
pushData(addressableParamIndicesAndImmortal.getPointer());
}
}
class LifetimeDependenceChecker {
ValueDecl *decl;
DeclContext *dc;
ASTContext &ctx;
SourceLoc returnLoc;
// Only initialized when hasImplicitSelfDecl() is true.
unsigned selfIndex = ~0;
// 'resultIndex' is a pseudo-parameter-index used by LifetimeDependenceInfo to
// represent the function result.
unsigned resultIndex = ~0;
SmallVector<LifetimeDependenceInfo, 1> lifetimeDependencies;
// True if lifetime diganostics have already been performed. Avoids redundant
// diagnostics, and allows bypassing diagnostics for special cases.
bool performedDiagnostics = false;
public:
LifetimeDependenceChecker(AbstractFunctionDecl *afd)
: decl(afd), dc(afd->getDeclContext()), ctx(dc->getASTContext()) {
auto resultTypeRepr = afd->getResultTypeRepr();
returnLoc = resultTypeRepr ? resultTypeRepr->getLoc() : afd->getLoc();
if (afd->hasImplicitSelfDecl()) {
selfIndex = afd->getParameters()->size();
resultIndex = selfIndex + 1;
} else {
resultIndex = afd->getParameters()->size();
}
}
LifetimeDependenceChecker(EnumElementDecl *eed)
: decl(eed), dc(eed->getDeclContext()), ctx(dc->getASTContext()) {
auto *paramList = eed->getParameterList();
resultIndex = paramList ? eed->getParameterList()->size() + 1 : 1;
}
std::optional<llvm::ArrayRef<LifetimeDependenceInfo>>
currentDependencies() const {
if (lifetimeDependencies.empty()) {
return std::nullopt;
}
return decl->getASTContext().AllocateCopy(lifetimeDependencies);
}
std::optional<llvm::ArrayRef<LifetimeDependenceInfo>> checkFuncDecl() {
assert(isa<FuncDecl>(decl) || isa<ConstructorDecl>(decl));
assert(lifetimeDependencies.empty());
auto *afd = cast<AbstractFunctionDecl>(decl);
// Handle Builtins first because, even though Builtins require
// LifetimeDependence, we don't force the experimental feature
// to be enabled when importing the Builtin module.
if (afd->isImplicit() && afd->getModuleContext()->isBuiltinModule()) {
inferBuiltin();
return currentDependencies();
}
if (!ctx.LangOpts.hasFeature(Feature::LifetimeDependence)
&& !ctx.LangOpts.hasFeature(Feature::Lifetimes)
&& !ctx.SourceMgr.isImportMacroGeneratedLoc(returnLoc)) {
// Infer inout dependencies without requiring a feature flag. On
// returning, 'lifetimeDependencies' contains any inferred
// dependencies. This does not issue any diagnostics because any invalid
// usage should generate a missing feature flag diagnostic instead.
inferInoutParams();
diagnoseMissingResultDependencies(
diag::lifetime_dependence_feature_required_return.ID);
diagnoseMissingSelfDependencies(
diag::lifetime_dependence_feature_required_mutating.ID);
diagnoseMissingInoutDependencies(
diag::lifetime_dependence_feature_required_inout.ID);
return currentDependencies();
}
if (afd->getAttrs().hasAttribute<LifetimeAttr>()) {
return checkAttribute();
}
// Methods or functions with @_unsafeNonescapableResult do not require
// lifetime annotation and do not infer any lifetime dependency.
if (afd->getAttrs().hasAttribute<UnsafeNonEscapableResultAttr>()) {
return std::nullopt;
}
inferOrDiagnose();
// If precise diagnostics were already issued, bypass
// diagnoseMissingDependencies to avoid redundant diagnostics.
if (!performedDiagnostics) {
diagnoseMissingResultDependencies(
diag::lifetime_dependence_cannot_infer_return.ID);
diagnoseMissingSelfDependencies(
diag::lifetime_dependence_cannot_infer_mutating.ID);
diagnoseMissingInoutDependencies(
diag::lifetime_dependence_cannot_infer_inout.ID);
}
return currentDependencies();
}
std::optional<llvm::ArrayRef<LifetimeDependenceInfo>> checkEnumElementDecl() {
auto *eed = cast<EnumElementDecl>(decl);
auto *parentEnum = eed->getParentEnum();
auto enumType =
parentEnum->mapTypeIntoContext(parentEnum->getDeclaredInterfaceType());
// Add early bailout for imported enums.
if (parentEnum->hasClangNode()) {
return std::nullopt;
}
// Escapable enum, bailout.
if (!isDiagnosedNonEscapable(enumType)) {
return std::nullopt;
}
auto *params = eed->getParameterList();
// No payload, bailout.
if (!params) {
return std::nullopt;
}
auto resultIndex = params->size() + /*selfType*/ 1;
auto capacity = resultIndex + 1;
SmallBitVector inheritIndices(capacity);
SmallVector<LifetimeDependenceInfo, 1> lifetimeDependencies;
// Add all indices of ~Escapable parameters as lifetime dependence sources.
for (size_t i = 0; i < params->size(); i++) {
auto paramType = params->get(i)->getTypeInContext();
if (!isDiagnosedNonEscapable(paramType)) {
continue;
}
inheritIndices.set(i);
}
if (inheritIndices.none()) {
return std::nullopt;
}
auto lifetimeDependenceInfo = LifetimeDependenceInfo(
IndexSubset::get(eed->getASTContext(), inheritIndices), nullptr,
resultIndex,
/*isImmortal*/ false);
lifetimeDependencies.push_back(lifetimeDependenceInfo);
return eed->getASTContext().AllocateCopy(lifetimeDependencies);
}
protected:
template<typename ...ArgTypes>
InFlightDiagnostic diagnose(
SourceLoc Loc, Diag<ArgTypes...> ID,
typename detail::PassArgument<ArgTypes>::type... Args) {
performedDiagnostics = true;
return ctx.Diags.diagnose(Loc, ID, std::move(Args)...);
}
template<typename ...ArgTypes>
InFlightDiagnostic
diagnose(const Decl *decl, Diag<ArgTypes...> id,
typename detail::PassArgument<ArgTypes>::type... args) {
return ctx.Diags.diagnose(decl, Diagnostic(id, std::move(args)...));
}
bool isInit() const { return isa<ConstructorDecl>(decl); }
// For initializers, the implicit self parameter is ignored and instead shows
// up as the result type.
//
// Note: Do not use this to reserve the self parameter index.
// LifetimeDependenceInfo always reserves an extra formal parameter
// index for hasImplicitSelfDecl(), even for initializers. During function
// type lowering, it is mapped to the metatype parameter. Without reserving
// the extra formal self parameter, a dependency targeting the formal result
// index would incorrectly target the SIL metatype parameter.
bool hasImplicitSelfParam() const {
auto *afd = cast<AbstractFunctionDecl>(decl);
return !isInit() && afd->hasImplicitSelfDecl();
}
// In SIL, implicit initializers and accessors become explicit.
bool isImplicitOrSIL() const {
auto *afd = cast<AbstractFunctionDecl>(decl);
if (afd->isImplicit()) {
return true;
}
// TODO: remove this check once SIL prints @lifetime.
if (auto *sf = afd->getParentSourceFile()) {
// The AST printer makes implicit initializers explicit, but does not
// print the @lifetime annotations. Until that is fixed, avoid
// diagnosing this as an error.
if (sf->Kind == SourceFileKind::SIL) {
return true;
}
}
return false;
}
bool isInterfaceFile() const {
// TODO: remove this check once all compilers that are rev-locked to the
// stdlib print the 'copy' dependence kind in the interface (Aug '25)
if (auto *sf = decl->getDeclContext()->getParentSourceFile()) {
if (sf->Kind == SourceFileKind::Interface) {
return true;
}
}
return false;
}
// Infer ambiguous cases for backward compatibility.
bool useLazyInference() const {
return isInterfaceFile()
|| ctx.LangOpts.EnableExperimentalLifetimeDependenceInference;
}
std::string diagnosticQualifier() const {
auto *afd = cast<AbstractFunctionDecl>(decl);
if (afd->isImplicit()) {
if (isInit()) {
return "an implicit initializer";
}
if (auto *ad = dyn_cast<AccessorDecl>(afd)) {
std::string qualifier = "the '";
qualifier += accessorKindName(ad->getAccessorKind());
qualifier += "' accessor";
return qualifier;
}
}
if (afd->hasImplicitSelfDecl()) {
if (isInit()) {
return "an initializer";
}
if (afd->getImplicitSelfDecl()->isInOut()) {
return "a mutating method";
}
return "a method";
}
return "a function";
}
// Ensure that dependencies exist for any return value or inout parameter that
// needs one. Always runs before the checker completes if no other diagnostics
// were issued.
void diagnoseMissingResultDependencies(DiagID diagID) {
if (!isDiagnosedNonEscapable(getResultOrYield())) {
return;
}
if (llvm::none_of(lifetimeDependencies,
[&](LifetimeDependenceInfo dep) {
return dep.getTargetIndex() == resultIndex;
})) {
ctx.Diags.diagnose(returnLoc, diagID,
{StringRef(diagnosticQualifier())});
}
}
// Ensure that dependencies exist for any mutating self value. Always runs
// before the checker completes if no other diagnostics were issued. For
// initializers, the inout self parameter is actually considered the result
// type so is not handled here.
void diagnoseMissingSelfDependencies(DiagID diagID) {
auto *afd = cast<AbstractFunctionDecl>(decl);
if (!hasImplicitSelfParam()) {
return;
}
auto *selfDecl = afd->getImplicitSelfDecl();
if (!selfDecl->isInOut()) {
return;
}
if (!isDiagnosedNonEscapable(dc->getSelfTypeInContext())) {
return;
}
if (llvm::none_of(lifetimeDependencies,
[&](LifetimeDependenceInfo dep) {
return dep.getTargetIndex() == selfIndex;
})) {
ctx.Diags.diagnose(selfDecl->getLoc(), diagID,
{StringRef(diagnosticQualifier())});
}
}
void diagnoseMissingInoutDependencies(DiagID diagID) {
auto *afd = cast<AbstractFunctionDecl>(decl);
unsigned paramIndex = 0;
for (auto *param : *afd->getParameters()) {
SWIFT_DEFER { paramIndex++; };
if (!param->isInOut()) {
continue;
}
if (!isDiagnosedNonEscapable(
afd->mapTypeIntoContext(param->getInterfaceType()))) {
continue;
}
if (llvm::none_of(lifetimeDependencies,
[&](LifetimeDependenceInfo dep) {
return dep.getTargetIndex() == paramIndex;
})) {
ctx.Diags.diagnose(param->getLoc(), diagID,
{StringRef(diagnosticQualifier()),
param->getName().str()});
if (diagID == diag::lifetime_dependence_cannot_infer_inout.ID) {
ctx.Diags.diagnose(
param->getLoc(),
diag::lifetime_dependence_cannot_infer_inout_suggest,
param->getName().str());
}
}
}
}
bool isCompatibleWithOwnership(ParsedLifetimeDependenceKind kind, Type type,
ValueOwnership loweredOwnership,
bool isInterfaceFile = false) const {
if (kind == ParsedLifetimeDependenceKind::Inherit) {
return true;
}
if (kind == ParsedLifetimeDependenceKind::Borrow) {
// An owned/consumed BitwiseCopyable value can be effectively borrowed
// because its lifetime can be indefinitely extended.
if (loweredOwnership == ValueOwnership::Owned
&& isBitwiseCopyable(type, ctx)) {
return true;
}
if (isInterfaceFile) {
return loweredOwnership == ValueOwnership::Shared ||
loweredOwnership == ValueOwnership::InOut;
}
return loweredOwnership == ValueOwnership::Shared;
}
assert(kind == ParsedLifetimeDependenceKind::Inout);
return loweredOwnership == ValueOwnership::InOut;
}
bool isCompatibleWithOwnership(LifetimeDependenceKind kind, Type type,
ValueOwnership ownership) const {
auto *afd = cast<AbstractFunctionDecl>(decl);
if (kind == LifetimeDependenceKind::Inherit) {
return true;
}
// Lifetime dependence always propagates through temporary BitwiseCopyable
// values, even if the dependence is scoped.
if (isBitwiseCopyable(type, ctx)) {
return true;
}
auto loweredOwnership = ownership != ValueOwnership::Default
? ownership
: getLoweredOwnership(afd);
assert(kind == LifetimeDependenceKind::Scope);
return loweredOwnership == ValueOwnership::Shared ||
loweredOwnership == ValueOwnership::InOut;
}
struct TargetDeps {
unsigned targetIndex;
SmallBitVector inheritIndices;
SmallBitVector scopeIndices;
TargetDeps(unsigned targetIndex, unsigned capacity)
: targetIndex(targetIndex), inheritIndices(capacity),
scopeIndices(capacity) {}
TargetDeps &&add(unsigned sourceIndex, LifetimeDependenceKind kind) && {
switch (kind) {
case LifetimeDependenceKind::Inherit:
inheritIndices.set(sourceIndex);
break;
case LifetimeDependenceKind::Scope:
scopeIndices.set(sourceIndex);
break;
}
return std::move(*this);
}
};
TargetDeps createDeps(unsigned targetIndex) {
auto *afd = cast<AbstractFunctionDecl>(decl);
unsigned capacity = afd->hasImplicitSelfDecl()
? (afd->getParameters()->size() + 1)
: afd->getParameters()->size();
return TargetDeps(targetIndex, capacity);
}
// Allocate LifetimeDependenceInfo in the ASTContext and push it onto
// lifetimeDependencies.
void pushDeps(const TargetDeps &&deps) {
assert(llvm::none_of(lifetimeDependencies,
[&](LifetimeDependenceInfo dep) {
return dep.getTargetIndex() == deps.targetIndex;
}));
IndexSubset *inheritIndices = nullptr;
if (deps.inheritIndices.any()) {
inheritIndices = IndexSubset::get(ctx, deps.inheritIndices);
}
IndexSubset *scopeIndices = nullptr;
if (deps.scopeIndices.any()) {
scopeIndices = IndexSubset::get(ctx, deps.scopeIndices);
}
lifetimeDependencies.push_back(
LifetimeDependenceInfo{
/*inheritLifetimeParamIndices*/ inheritIndices,
/*scopeLifetimeParamIndices*/ scopeIndices,
deps.targetIndex,
/*isImmortal*/ false});
}
Type getResultOrYield() const {
auto *afd = cast<AbstractFunctionDecl>(decl);
if (auto *accessor = dyn_cast<AccessorDecl>(afd)) {
if (accessor->isCoroutine()) {
auto yieldTyInContext = accessor->mapTypeIntoContext(
accessor->getStorage()->getValueInterfaceType());
return yieldTyInContext;
}
}
Type resultType;
if (auto fn = dyn_cast<FuncDecl>(afd)) {
resultType = fn->getResultInterfaceType();
} else {
auto ctor = cast<ConstructorDecl>(afd);
resultType = ctor->getResultInterfaceType();
}
return afd->mapTypeIntoContext(resultType);
}
std::optional<LifetimeDependenceKind>
getDependenceKindFromDescriptor(LifetimeDescriptor descriptor,
ParamDecl *paramDecl) {
auto *afd = cast<AbstractFunctionDecl>(decl);
auto loc = descriptor.getLoc();
auto type = paramDecl->getTypeInContext();
auto parsedLifetimeKind = descriptor.getParsedLifetimeDependenceKind();
auto ownership = paramDecl->getValueOwnership();
auto loweredOwnership = ownership != ValueOwnership::Default
? ownership
: getLoweredOwnership(afd);
switch (parsedLifetimeKind) {
case ParsedLifetimeDependenceKind::Default: {
if (type->isEscapable()) {
if (loweredOwnership == ValueOwnership::Shared ||
loweredOwnership == ValueOwnership::InOut) {
return LifetimeDependenceKind::Scope;
}
diagnose(
loc,
diag::lifetime_dependence_cannot_use_default_escapable_consuming,
getOwnershipSpelling(loweredOwnership));
return std::nullopt;
}
if (useLazyInference()) {
return LifetimeDependenceKind::Inherit;
}
diagnose(loc, diag::lifetime_dependence_cannot_infer_kind,
diagnosticQualifier(), descriptor.getString());
return std::nullopt;
}
case ParsedLifetimeDependenceKind::Borrow: LLVM_FALLTHROUGH;
case ParsedLifetimeDependenceKind::Inout: {
// @lifetime(borrow x) is valid only for borrowing parameters.
// @lifetime(&x) is valid only for inout parameters.
auto loweredOwnership = ownership != ValueOwnership::Default
? ownership : getLoweredOwnership(afd);
if (isCompatibleWithOwnership(parsedLifetimeKind, type, loweredOwnership,
isInterfaceFile())) {
return LifetimeDependenceKind::Scope;
}
diagnose(loc,
diag::lifetime_dependence_parsed_borrow_with_ownership,
getNameForParsedLifetimeDependenceKind(parsedLifetimeKind),
getOwnershipSpelling(loweredOwnership));
switch (loweredOwnership) {
case ValueOwnership::Shared:
diagnose(loc,
diag::lifetime_dependence_parsed_borrow_with_ownership_fix,
"borrow ", descriptor.getString());
break;
case ValueOwnership::InOut:
diagnose(loc,
diag::lifetime_dependence_parsed_borrow_with_ownership_fix,
"&", descriptor.getString());
break;
case ValueOwnership::Owned:
case ValueOwnership::Default:
break;
}
return std::nullopt;
}
case ParsedLifetimeDependenceKind::Inherit: {
// @lifetime(copy x) is only invalid for Escapable types.
if (type->isEscapable()) {
if (loweredOwnership == ValueOwnership::Shared) {
diagnose(loc, diag::lifetime_dependence_invalid_inherit_escapable_type,
"borrow ", descriptor.getString());
} else if (loweredOwnership == ValueOwnership::InOut) {
diagnose(loc, diag::lifetime_dependence_invalid_inherit_escapable_type,
"&", descriptor.getString());
} else {
diagnose(
loc,
diag::lifetime_dependence_cannot_use_default_escapable_consuming,
getOwnershipSpelling(loweredOwnership));
}
return std::nullopt;
}
return LifetimeDependenceKind::Inherit;
}
}
}
// Finds the ParamDecl* and its index from a LifetimeDescriptor
std::optional<std::pair<ParamDecl *, unsigned>>
getParamDeclFromDescriptor(LifetimeDescriptor descriptor) {
auto *afd = cast<AbstractFunctionDecl>(decl);
switch (descriptor.getDescriptorKind()) {
case LifetimeDescriptor::DescriptorKind::Named: {
unsigned paramIndex = 0;
ParamDecl *candidateParam = nullptr;
for (auto *param : *afd->getParameters()) {
if (param->getParameterName() == descriptor.getName()) {
candidateParam = param;
break;
}
paramIndex++;
}
if (!candidateParam) {
diagnose(descriptor.getLoc(),
diag::lifetime_dependence_invalid_param_name,
descriptor.getName());
return std::nullopt;
}
return std::make_pair(candidateParam, paramIndex);
}
case LifetimeDescriptor::DescriptorKind::Ordered: {
auto paramIndex = descriptor.getIndex();
if (paramIndex >= afd->getParameters()->size()) {
diagnose(descriptor.getLoc(),
diag::lifetime_dependence_invalid_param_index,
paramIndex);
return std::nullopt;
}
auto candidateParam = afd->getParameters()->get(paramIndex);
return std::make_pair(candidateParam, paramIndex);
}
case LifetimeDescriptor::DescriptorKind::Self: {
if (!hasImplicitSelfParam()) {
diagnose(descriptor.getLoc(),
diag::lifetime_dependence_invalid_self_in_static);
return std::nullopt;
}
if (isa<ConstructorDecl>(afd)) {
diagnose(descriptor.getLoc(),
diag::lifetime_dependence_invalid_self_in_init);
return std::nullopt;
}
auto *selfDecl = afd->getImplicitSelfDecl();
return std::make_pair(selfDecl, afd->getParameters()->size());
}
}
}
std::optional<ArrayRef<LifetimeDependenceInfo>> checkAttribute() {
auto *afd = cast<AbstractFunctionDecl>(decl);
SmallVector<LifetimeDependenceInfo, 1> lifetimeDependencies;
llvm::SmallSet<unsigned, 1> lifetimeDependentTargets;
auto lifetimeAttrs = afd->getAttrs().getAttributes<LifetimeAttr>();
for (auto attr : lifetimeAttrs) {
auto lifetimeDependenceInfo =
checkAttributeEntry(attr->getLifetimeEntry());
if (!lifetimeDependenceInfo.has_value()) {
return std::nullopt;
}
auto targetIndex = lifetimeDependenceInfo->getTargetIndex();
if (lifetimeDependentTargets.contains(targetIndex)) {
// TODO: Diagnose at the source location of the @lifetime attribute with
// duplicate target.
diagnose(afd->getLoc(), diag::lifetime_dependence_duplicate_target);
}
lifetimeDependentTargets.insert(targetIndex);
lifetimeDependencies.push_back(*lifetimeDependenceInfo);
}
return afd->getASTContext().AllocateCopy(lifetimeDependencies);
}
std::optional<LifetimeDependenceInfo>
checkAttributeEntry(LifetimeEntry *entry) {
auto *afd = cast<AbstractFunctionDecl>(decl);
auto capacity = afd->hasImplicitSelfDecl()
? (afd->getParameters()->size() + 1)
: afd->getParameters()->size();
SmallBitVector inheritIndices(capacity);
SmallBitVector scopeIndices(capacity);
auto updateLifetimeIndices = [&](LifetimeDescriptor descriptor,
unsigned paramIndexToSet,
LifetimeDependenceKind lifetimeKind) {
if (inheritIndices.test(paramIndexToSet) ||
scopeIndices.test(paramIndexToSet)) {
diagnose(descriptor.getLoc(),
diag::lifetime_dependence_duplicate_param_id);
return true;
}
if (lifetimeKind == LifetimeDependenceKind::Inherit) {
inheritIndices.set(paramIndexToSet);
} else {
assert(lifetimeKind == LifetimeDependenceKind::Scope);
scopeIndices.set(paramIndexToSet);
}
return false;
};
auto targetDescriptor = entry->getTargetDescriptor();
unsigned targetIndex;
if (targetDescriptor.has_value()) {
auto targetDeclAndIndex = getParamDeclFromDescriptor(*targetDescriptor);
if (!targetDeclAndIndex.has_value()) {
return std::nullopt;
}
// TODO: support dependencies on non-inout parameters.
if (!targetDeclAndIndex->first->isInOut()) {
diagnose(targetDeclAndIndex->first,
diag::lifetime_parameter_requires_inout,
targetDescriptor->getString());
}
targetIndex = targetDeclAndIndex->second;
} else {
targetIndex = afd->hasImplicitSelfDecl()
? afd->getParameters()->size() + 1
: afd->getParameters()->size();
}
for (auto source : entry->getSources()) {
if (source.isImmortal()) {
auto immortalParam =
std::find_if(afd->getParameters()->begin(),
afd->getParameters()->end(), [](ParamDecl *param) {
return param->getName().nonempty()
&& strcmp(param->getName().get(), "immortal") == 0;
});
if (immortalParam != afd->getParameters()->end()) {
diagnose(*immortalParam,
diag::lifetime_dependence_immortal_conflict_name);
return std::nullopt;
}
return LifetimeDependenceInfo(nullptr, nullptr, targetIndex,
/*isImmortal*/ true);
}
auto paramDeclAndIndex = getParamDeclFromDescriptor(source);
if (!paramDeclAndIndex.has_value()) {
return std::nullopt;
}
auto *param = paramDeclAndIndex->first;
unsigned sourceIndex = paramDeclAndIndex->second;
auto lifetimeKind = getDependenceKindFromDescriptor(source, param);
if (!lifetimeKind.has_value()) {
return std::nullopt;
}
if (lifetimeKind == LifetimeDependenceKind::Scope
&& param->isInOut()
&& sourceIndex == targetIndex) {
diagnose(source.getLoc(),
diag::lifetime_dependence_cannot_use_parsed_borrow_inout);
ctx.Diags.diagnose(source.getLoc(),
diag::lifetime_dependence_cannot_infer_inout_suggest,
param->getName().str());
return std::nullopt;
}
bool hasError =
updateLifetimeIndices(source, sourceIndex, *lifetimeKind);
if (hasError) {
return std::nullopt;
}
}
return LifetimeDependenceInfo(
inheritIndices.any() ? IndexSubset::get(ctx, inheritIndices) : nullptr,
scopeIndices.any() ? IndexSubset::get(ctx, scopeIndices) : nullptr,
targetIndex, /*isImmortal*/ false);
}
// On returning, 'lifetimeDependencies' contains any inferred dependencies and
// 'performedDiagnostics' indicates whether any specific diagnostics were
// issued.
void inferOrDiagnose() {
// Infer non-Escapable results.
if (isDiagnosedNonEscapable(getResultOrYield())) {
if (hasImplicitSelfParam()) {
// Methods that return a non-Escapable value.
inferNonEscapableResultOnSelf();
return;
}
if (isInit() && isImplicitOrSIL()) {
inferImplicitInit();
return;
}
// Regular functions and initializers that return a non-Escapable value.
inferNonEscapableResultOnParam();
return;
}
// Infer mutating non-Escapable methods (excluding initializers).
inferMutatingSelf();
// Infer inout parameters.
inferInoutParams();
}
/// If the current function is a mutating method and 'self' is non-Escapable,
/// return 'self's ParamDecl.
bool isMutatingNonEscapableSelf() {
auto *afd = cast<AbstractFunctionDecl>(decl);
if (!hasImplicitSelfParam())
return false;
if (!isDiagnosedNonEscapable(dc->getSelfTypeInContext()))
return false;
assert(!isInit() && "class initializers have Escapable self");
auto *selfDecl = afd->getImplicitSelfDecl();
if (!selfDecl->isInOut())
return false;
return true;
}
// Infer method dependence of result on self for
// methods, getters, and _modify accessors.
void inferNonEscapableResultOnSelf() {
auto *afd = cast<AbstractFunctionDecl>(decl);
Type selfTypeInContext = dc->getSelfTypeInContext();
if (selfTypeInContext->hasError()) {
return;
}
bool nonEscapableSelf = isDiagnosedNonEscapable(selfTypeInContext);
if (auto accessor = dyn_cast<AccessorDecl>(afd)) {
if (isImplicitOrSIL() || useLazyInference()) {
if (nonEscapableSelf && afd->getImplicitSelfDecl()->isInOut()) {
// Implicit accessors that return or yield a non-Escapable value may
// infer dependency on both self and result.
inferMutatingAccessor(accessor);
}
// Infer the result dependency on self based on the kind of accessor
// that is wrapped by this synthesized accessors.
if (auto dependenceKind =
getAccessorDependence(accessor, selfTypeInContext)) {
pushDeps(createDeps(resultIndex).add(selfIndex, *dependenceKind));
}
return;
}
// Explicit accessors are inferred the same way as regular methods.
}
// Do infer the result of a mutating method when 'self' is
// non-Escapable. The missing dependence on inout 'self' will be diagnosed
// later anyway, so an explicit annotation will still be needed.
if (nonEscapableSelf && afd->getImplicitSelfDecl()->isInOut()) {
return;
}
// Methods with parameters only apply to lazy inference.
if (!useLazyInference() && afd->getParameters()->size() > 0) {
return;
}
if (!useLazyInference() && !isImplicitOrSIL()) {
// Require explicit @_lifetime(borrow self) for UnsafePointer-like self.
if (!nonEscapableSelf && isBitwiseCopyable(selfTypeInContext, ctx)) {
diagnose(returnLoc,
diag::lifetime_dependence_cannot_infer_bitwisecopyable,
diagnosticQualifier(), "self");
return;
}
// Require explicit @_lifetime(copy or borrow) for non-Escapable self.
if (nonEscapableSelf) {
diagnose(returnLoc, diag::lifetime_dependence_cannot_infer_kind,
diagnosticQualifier(), "self");
return;
}
}
// Infer based on ownership if possible for either explicit accessors or
// methods as long as they pass preceding ambiguity checks.
auto kind = inferLifetimeDependenceKind(
selfTypeInContext, afd->getImplicitSelfDecl()->getValueOwnership());
if (!kind) {
// Special diagnostic for an attempt to depend on a consuming parameter.
diagnose(returnLoc,
diag::lifetime_dependence_cannot_infer_scope_ownership,
"self", diagnosticQualifier());
return;
}
pushDeps(createDeps(resultIndex).add(selfIndex, *kind));
}
// Infer the kind of dependence that makes sense for reading or writing a
// stored property (for getters or initializers).
std::optional<LifetimeDependenceKind>
inferLifetimeDependenceKind(Type sourceType, ValueOwnership ownership) {
auto *afd = cast<AbstractFunctionDecl>(decl);
if (!sourceType->isEscapable()) {
return LifetimeDependenceKind::Inherit;
}
// Lifetime dependence always propagates through temporary BitwiseCopyable
// values, even if the dependence is scoped.
if (isBitwiseCopyable(sourceType, ctx)) {
return LifetimeDependenceKind::Scope;
}
auto loweredOwnership = ownership != ValueOwnership::Default
? ownership
: getLoweredOwnership(afd);
// It is impossible to depend on a consumed Escapable value (unless it is
// BitwiseCopyable as checked above).
if (loweredOwnership == ValueOwnership::Owned) {
return std::nullopt;
}
return LifetimeDependenceKind::Scope;
}
// Infer implicit initialization. A non-Escapable initializer parameter can
// always be inferred, similar to an implicit setter, because the
// implementation is simply an assignment to stored property. Escapable
// parameters are ambiguous: they may either be borrowed or
// non-dependent. non-Escapable types often have incidental integer fields
// that are unrelated to lifetime. Avoid inferring any dependency on Escapable
// parameters unless it is the (unambiguously borrowed) sole parameter.
void inferImplicitInit() {
auto *afd = cast<AbstractFunctionDecl>(decl);
if (afd->getParameters()->size() == 0) {
// Empty ~Escapable types can be implicitly initialized without any
// dependencies. In SIL, implicit initializers become explicit. Set
// performedDiagnostics here to bypass normal dependence checking without
// raising an error.
performedDiagnostics = true;
return;
}
auto targetDeps = createDeps(resultIndex);
unsigned paramIndex = 0;
for (auto *param : *afd->getParameters()) {
SWIFT_DEFER { paramIndex++; };
Type paramTypeInContext =
afd->mapTypeIntoContext(param->getInterfaceType());
if (paramTypeInContext->hasError()) {
return;
}
if (!paramTypeInContext->isEscapable()) {
// An implicitly initialized non-Escapable value always copies its
// dependency.
targetDeps = std::move(targetDeps).add(paramIndex,
LifetimeDependenceKind::Inherit);
continue;
}
if (afd->getParameters()->size() > 1 && !useLazyInference()) {
diagnose(param->getLoc(),
diag::lifetime_dependence_cannot_infer_implicit_init);
return;
}
// A single Escapable parameter must be borrowed.
auto kind = inferLifetimeDependenceKind(paramTypeInContext,
param->getValueOwnership());
if (!kind) {
diagnose(returnLoc,
diag::lifetime_dependence_cannot_infer_scope_ownership,
param->getParameterName().str(), diagnosticQualifier());
}
targetDeps = std::move(targetDeps).add(paramIndex,
LifetimeDependenceKind::Scope);
}
pushDeps(std::move(targetDeps));
}
// Infer result dependence on a function or intitializer parameter.
//
// Note: for implicit initializers with parameters, consider inferring
// Inherit dependency for each non-Escapable parameter. This would be
// consistent with implicit stored property setters. This isn't done yet
// because we also need to consider any Escapable parameters: either skip
// inference if any exist, infer scoped dependency, or infer no
// dependency. Implicit setters for Escapable properties are not inferred.
void inferNonEscapableResultOnParam() {
auto *afd = cast<AbstractFunctionDecl>(decl);
// This is only called when there is no 'self' argument that can be the
// source of a dependence.
assert(!hasImplicitSelfParam());
if (useLazyInference()) {
return lazillyInferNonEscapableResultOnParam();
}
// Strict inference only handles a single escapable parameter,
// which is an unambiguous borrow dependence.
if (afd->getParameters()->size() == 0) {
diagnose(returnLoc,
diag::lifetime_dependence_cannot_infer_return_no_param,
diagnosticQualifier());
diagnose(returnLoc,
diag::lifetime_dependence_cannot_infer_return_immortal);
return;
}
if (afd->getParameters()->size() > 1) {
// The usual diagnostic check is sufficient.
return;
}
// Do not infer non-escapable dependence kind -- it is ambiguous.
auto *param = afd->getParameters()->get(0);
Type paramTypeInContext =
afd->mapTypeIntoContext(param->getInterfaceType());
if (paramTypeInContext->hasError()) {
return;
}
if (!paramTypeInContext->isEscapable()) {
diagnose(returnLoc, diag::lifetime_dependence_cannot_infer_kind,
diagnosticQualifier(), param->getParameterName().str());
return;
}
auto kind = LifetimeDependenceKind::Scope;
auto paramOwnership = param->getValueOwnership();
if (!isCompatibleWithOwnership(kind, paramTypeInContext, paramOwnership))
{
diagnose(returnLoc,
diag::lifetime_dependence_cannot_infer_scope_ownership,
param->getParameterName().str(), diagnosticQualifier());
return;
}
pushDeps(createDeps(resultIndex).add(/*paramIndex*/ 0, kind));
}
// Lazy inference for .swiftinterface backward compatibility and
// experimentation. Inference cases can be added but not removed.
void lazillyInferNonEscapableResultOnParam() {
auto *afd = cast<AbstractFunctionDecl>(decl);
std::optional<unsigned> candidateParamIndex;
std::optional<LifetimeDependenceKind> candidateLifetimeKind;
unsigned paramIndex = 0;
for (auto *param : *afd->getParameters()) {
SWIFT_DEFER { paramIndex++; };
Type paramTypeInContext =
afd->mapTypeIntoContext(param->getInterfaceType());
if (paramTypeInContext->hasError()) {
return;
}
auto paramOwnership = param->getValueOwnership();
if (paramTypeInContext->isEscapable()) {
if (isBitwiseCopyable(paramTypeInContext, ctx)) {
continue;
}
if (paramOwnership == ValueOwnership::Default) {
continue;
}
}
candidateLifetimeKind =
inferLifetimeDependenceKind(paramTypeInContext, paramOwnership);
if (!candidateLifetimeKind) {
continue;
}
if (candidateParamIndex) {
diagnose(returnLoc,
diag::lifetime_dependence_cannot_infer_ambiguous_candidate,
diagnosticQualifier());
return;
}
candidateParamIndex = paramIndex;
}
if (!candidateParamIndex) {
diagnose(returnLoc,
diag::lifetime_dependence_cannot_infer_no_candidates,
diagnosticQualifier());
return;
}
pushDeps(createDeps(resultIndex).add(*candidateParamIndex,
*candidateLifetimeKind));
}
// Infer a mutating 'self' dependency when 'self' is non-Escapable and the
// result is 'void'.
void inferMutatingSelf() {
auto *afd = cast<AbstractFunctionDecl>(decl);
if (!isMutatingNonEscapableSelf()) {
return;
}
// Handle implicit setters before diagnosing mutating methods. This
// does not include global accessors, which have no implicit 'self'.
if (auto accessor = dyn_cast<AccessorDecl>(afd)) {
// Explicit setters require explicit lifetime dependencies.
if (isImplicitOrSIL() || useLazyInference()) {
inferMutatingAccessor(accessor);
}
return;
}
if (afd->getParameters()->size() > 0) {
if (useLazyInference()) {
// Assume that a mutating method does not depend on its parameters.
// This is unsafe but needed because some MutableSpan APIs snuck into
// the standard library interface without specifying dependencies.
pushDeps(createDeps(selfIndex).add(selfIndex,
LifetimeDependenceKind::Inherit));
}
return;
}
pushDeps(createDeps(selfIndex).add(selfIndex,
LifetimeDependenceKind::Inherit));
}
// Infer dependence for an accessor whose non-escapable result depends on
// self. This includes _read and _modify.
void inferAccessor(AccessorDecl *accessor, Type selfTypeInContext) {
// Explicit accessors require explicit lifetime dependencies.
auto *afd = cast<AbstractFunctionDecl>(decl);
if (!isImplicitOrSIL() && !useLazyInference()) {
return;
}
bool nonEscapableSelf = isDiagnosedNonEscapable(selfTypeInContext);
if (nonEscapableSelf && afd->getImplicitSelfDecl()->isInOut()) {
// First, infer the dependency on the inout non-Escapable self. This may
// result in two inferred dependencies for accessors.
inferMutatingAccessor(accessor);
}
// Infer the result dependency on self based on the kind of accessor that
// is wrapped by this synthesized accessors.
if (auto dependenceKind =
getAccessorDependence(accessor, selfTypeInContext)) {
pushDeps(createDeps(resultIndex).add(selfIndex, *dependenceKind));
}
}
// Infer a mutating accessor's non-Escapable 'self' dependencies.
void inferMutatingAccessor(AccessorDecl *accessor) {
auto *afd = cast<AbstractFunctionDecl>(decl);
switch (accessor->getAccessorKind()) {
case AccessorKind::Read:
case AccessorKind::Read2:
// An implicit _read/read accessor is generated when a mutating getter is
// declared. Emit the same lifetime dependencies as an implicit _modify.
case AccessorKind::Modify:
case AccessorKind::Modify2:
// A _modify's yielded value depends on self. The _modify dependency in
// the opposite direction (self depends on the modified value) is not
// recorded. The caller of _modify ensures that the modified 'self',
// passed as 'inout', depends on any value stored to the yielded address.
//
// This is required for stored properties because the AST generates a
// _modify for them even though it won't be emitted.
pushDeps(createDeps(selfIndex).add(selfIndex,
LifetimeDependenceKind::Inherit));
break;
case AccessorKind::Set: {
const unsigned newValIdx = 0;
auto *param = afd->getParameters()->get(newValIdx);
Type paramTypeInContext =
afd->mapTypeIntoContext(param->getInterfaceType());
if (paramTypeInContext->hasError()) {
return;
}
auto targetDeps =
createDeps(selfIndex).add(selfIndex, LifetimeDependenceKind::Inherit);
// The 'newValue' dependence kind must match the getter's dependence kind
// because generated the implementation '_modify' accessor composes the
// getter's result with the setter's 'newValue'. In particular, if the
// result type is Escapable then the getter does not have any lifetime
// dependency, so the setter cannot depend on 'newValue'.
if (!paramTypeInContext->isEscapable()) {
targetDeps = std::move(targetDeps)
.add(newValIdx, LifetimeDependenceKind::Inherit);
}
pushDeps(std::move(targetDeps));
break;
}
case AccessorKind::MutableAddress:
if (useLazyInference()) {
// Assume that a mutating method does not depend on its parameters.
// Currently only for backward interface compatibility. Even though this
// is the only useful dependence (a borrow of self is possible but not
// useful), explicit annotation is required for now to confirm that the
// mutated self cannot depend on anything stored at this address.
pushDeps(createDeps(selfIndex).add(selfIndex,
LifetimeDependenceKind::Inherit));
}
break;
default:
// Unknown mutating accessor.
break;
}
}
// Implicit accessors must be consistent with the accessor that they
// wrap. Otherwise, the sythesized implementation will report a diagnostic
// error.
std::optional<LifetimeDependenceKind>
getAccessorDependence(AccessorDecl *accessor, Type selfTypeInContext) {
auto *afd = cast<AbstractFunctionDecl>(decl);
std::optional<AccessorKind> wrappedAccessorKind = std::nullopt;
switch (accessor->getAccessorKind()) {
case AccessorKind::Read:
case AccessorKind::Read2:
case AccessorKind::Modify:
case AccessorKind::Modify2:
// read/modify are syntesized as calls to the getter.
wrappedAccessorKind = AccessorKind::Get;
break;
case AccessorKind::Get:
// getters are synthesized as access to a stored property.
break;
default:
// Unknown synthesized accessor.
// Setters are handled in inferMutatingAccessor() because they don't
// return a value.
return std::nullopt;
}
if (wrappedAccessorKind) {
auto *var = cast<VarDecl>(accessor->getStorage());
for (auto *wrappedAccessor : var->getAllAccessors()) {
if (wrappedAccessor->isImplicit())
continue;
if (wrappedAccessor->getAccessorKind() == wrappedAccessorKind) {
if (auto deps = wrappedAccessor->getLifetimeDependencies()) {
for (auto &dep : *deps) {
if (dep.getTargetIndex() != resultIndex)
continue;
if (dep.checkInherit(selfIndex))
return LifetimeDependenceKind::Inherit;
if (dep.checkScope(selfIndex))
return LifetimeDependenceKind::Scope;
}
}
}
}
}
// Either a Get or Modify without any wrapped accessor. Handle these like a
// read of the stored property.
return inferLifetimeDependenceKind(
selfTypeInContext, afd->getImplicitSelfDecl()->getValueOwnership());
}
// Infer 'inout' parameter dependency when the only parameter is
// non-Escapable.
//
// This supports the common case in which the user of a non-Escapable type,
// such as MutableSpan, wants to modify the span's contents without modifying
// the span value itself. It should be possible to use MutableSpan this way
// without requiring any knowledge of lifetime annotations. The tradeoff is
// that it makes authoring non-Escapable types less safe. For example, a
// MutableSpan method could update the underlying unsafe pointer and forget to
// declare a dependence on the incoming pointer.
//
// Disallowing other non-Escapable parameters rules out the easy mistake of
// programmers attempting to trivially reassign the inout parameter. There's
// is no way to rule out the possibility that they derive another
// non-Escapable value from an Escapable parameteter. So they can still write
// the following and will get a lifetime diagnostic:
//
// func reassign(s: inout MutableSpan<Int>, a: [Int]) {
// s = a.mutableSpan
// }
//
// Do not issue any diagnostics. This inference is triggered even when the
// feature is disabled!
void inferInoutParams() {
auto *afd = cast<AbstractFunctionDecl>(decl);
if (isMutatingNonEscapableSelf()) {
return;
}
std::optional<unsigned> candidateParamIndex;
bool hasNonEscapableParameter = false;
if (hasImplicitSelfParam()
&& isDiagnosedNonEscapable(dc->getSelfTypeInContext())) {
hasNonEscapableParameter = true;
}
for (unsigned paramIndex : range(afd->getParameters()->size())) {
auto *param = afd->getParameters()->get(paramIndex);
if (isDiagnosedNonEscapable(
afd->mapTypeIntoContext(param->getInterfaceType()))) {
if (param->isInOut()) {
if (hasNonEscapableParameter)
return;
candidateParamIndex = paramIndex;
continue;
}
if (candidateParamIndex)
return;
hasNonEscapableParameter = true;
}
}
if (candidateParamIndex) {
pushDeps(createDeps(*candidateParamIndex).add(
*candidateParamIndex, LifetimeDependenceKind::Inherit));
}
}
void inferUnambiguousInoutParams() {
auto *afd = cast<AbstractFunctionDecl>(decl);
if (afd->getParameters()->size() != 1) {
return;
}
const unsigned paramIndex = 0;
auto *param = afd->getParameters()->get(paramIndex);
if (!param->isInOut()) {
return;
}
if (!isDiagnosedNonEscapable(
afd->mapTypeIntoContext(param->getInterfaceType()))) {
return;
}
pushDeps(createDeps(paramIndex).add(paramIndex,
LifetimeDependenceKind::Inherit));
}
void inferBuiltin() {
auto *afd = cast<AbstractFunctionDecl>(decl);
// Normal inout parameter inference works for most generic Builtins.
inferUnambiguousInoutParams();
if (!lifetimeDependencies.empty()) {
return;
}
const DeclName &name = afd->getName();
if (name.isSpecial()) {
return;
}
// TODO: declare lifetime dependencies in Builtins.def. Until then, filter
// the few that are not covered by general inference rules here. This is
// safer than using a broader rule for implicit declarations. New Builtins
// need to be considered as they are defined.
auto id = name.getBaseIdentifier();
if (id ==
ctx.getIdentifier(getBuiltinName(BuiltinValueKind::InjectEnumTag))) {
// ignore the tag parameter
const unsigned inoutIdx = 0;
pushDeps(createDeps(inoutIdx).add(inoutIdx,
LifetimeDependenceKind::Inherit));
} else if (id ==
ctx.getIdentifier(
getBuiltinName(BuiltinValueKind::ConvertUnownedUnsafeToGuaranteed))) {
const unsigned baseIdx = 0;
const unsigned inoutIdx = 1;
pushDeps(createDeps(inoutIdx)
.add(inoutIdx, LifetimeDependenceKind::Inherit)
.add(baseIdx, LifetimeDependenceKind::Scope));
}
}
};
std::optional<llvm::ArrayRef<LifetimeDependenceInfo>>
LifetimeDependenceInfo::get(ValueDecl *decl) {
if (auto *afd = dyn_cast<AbstractFunctionDecl>(decl)) {
return LifetimeDependenceChecker(afd).checkFuncDecl();
}
auto *eed = cast<EnumElementDecl>(decl);
return LifetimeDependenceChecker(eed).checkEnumElementDecl();
}
// This implements the logic for SIL type descriptors similar to source-level
// logic in LifetimeDependenceChecker::checkAttributeEntry(). The SIL context is
// substantially different from Sema.
static std::optional<LifetimeDependenceInfo> checkSILTypeModifiers(
LifetimeDependentTypeRepr *lifetimeDependentRepr, unsigned targetIndex,
ArrayRef<SILParameterInfo> params, DeclContext *dc) {
auto &ctx = dc->getASTContext();
auto &diags = ctx.Diags;
auto capacity = params.size(); // SIL parameters include self
SmallBitVector inheritLifetimeParamIndices(capacity);
SmallBitVector scopeLifetimeParamIndices(capacity);
SmallBitVector addressableLifetimeParamIndices(capacity);
SmallBitVector conditionallyAddressableLifetimeParamIndices(capacity);
auto updateLifetimeDependenceInfo =
[&](LifetimeDescriptor descriptor,
unsigned paramIndexToSet,
ParameterConvention paramConvention) {
auto loc = descriptor.getLoc();
auto kind = descriptor.getParsedLifetimeDependenceKind();
if (kind == ParsedLifetimeDependenceKind::Borrow &&
isConsumedParameterInCallee(paramConvention)) {
diags.diagnose(loc, diag::lifetime_dependence_cannot_use_kind, "_scope",
getStringForParameterConvention(paramConvention));
return true;
}
if (inheritLifetimeParamIndices.test(paramIndexToSet) ||
scopeLifetimeParamIndices.test(paramIndexToSet)) {
diags.diagnose(loc, diag::lifetime_dependence_duplicate_param_id);
return true;
}
if (kind == ParsedLifetimeDependenceKind::Inherit) {
inheritLifetimeParamIndices.set(paramIndexToSet);
} else {
assert(kind == ParsedLifetimeDependenceKind::Borrow);
scopeLifetimeParamIndices.set(paramIndexToSet);
}
return false;
};
for (auto source : lifetimeDependentRepr->getLifetimeEntry()->getSources())
{
switch (source.getDescriptorKind()) {
case LifetimeDescriptor::DescriptorKind::Ordered: {
auto index = source.getIndex();
if (index > capacity) {
diags.diagnose(source.getLoc(),
diag::lifetime_dependence_invalid_param_index, index);
return std::nullopt;
}
auto param = params[index];
auto paramConvention = param.getConvention();
if (updateLifetimeDependenceInfo(source, index, paramConvention)) {
return std::nullopt;
}
switch (source.isAddressable()) {
case LifetimeDescriptor::IsNotAddressable:
break;
case LifetimeDescriptor::IsConditionallyAddressable:
conditionallyAddressableLifetimeParamIndices.set(index);
break;
case LifetimeDescriptor::IsAddressable:
addressableLifetimeParamIndices.set(index);
break;
}
break;
}
case LifetimeDescriptor::DescriptorKind::Named: {
assert(source.isImmortal());
return LifetimeDependenceInfo(/*inheritLifetimeParamIndices*/ nullptr,
/*scopeLifetimeParamIndices*/ nullptr,
targetIndex,
/*isImmortal*/ true);
}
default:
llvm_unreachable("SIL can only have ordered or immortal lifetime "
"dependence specifier kind");
}
}
return LifetimeDependenceInfo(
inheritLifetimeParamIndices.any()
? IndexSubset::get(ctx, inheritLifetimeParamIndices)
: nullptr,
scopeLifetimeParamIndices.any()
? IndexSubset::get(ctx, scopeLifetimeParamIndices)
: nullptr,
targetIndex,
/*isImmortal*/ false,
addressableLifetimeParamIndices.any()
? IndexSubset::get(ctx, addressableLifetimeParamIndices)
: nullptr,
conditionallyAddressableLifetimeParamIndices.any()
? IndexSubset::get(ctx, conditionallyAddressableLifetimeParamIndices)
: nullptr);
}
std::optional<llvm::ArrayRef<LifetimeDependenceInfo>>
LifetimeDependenceInfo::getFromSIL(FunctionTypeRepr *funcRepr,
ArrayRef<SILParameterInfo> params,
ArrayRef<SILResultInfo> results,
DeclContext *dc) {
SmallVector<LifetimeDependenceInfo, 1> lifetimeDependencies;
auto getLifetimeDependenceFromTypeModifiers =
[&](TypeRepr *typeRepr,
unsigned targetIndex) -> std::optional<LifetimeDependenceInfo> {
auto *lifetimeTypeRepr =
dyn_cast_or_null<LifetimeDependentTypeRepr>(typeRepr);
if (!lifetimeTypeRepr) {
return std::nullopt;
}
return checkSILTypeModifiers(lifetimeTypeRepr, targetIndex, params, dc);
};
auto argsTypeRepr = funcRepr->getArgsTypeRepr()->getElements();
for (unsigned targetIndex : indices(argsTypeRepr)) {
if (auto result = getLifetimeDependenceFromTypeModifiers(
argsTypeRepr[targetIndex].Type, targetIndex)) {
lifetimeDependencies.push_back(*result);
}
}
auto result = getLifetimeDependenceFromTypeModifiers(
funcRepr->getResultTypeRepr(), params.size());
if (result) {
lifetimeDependencies.push_back(*result);
}
return dc->getASTContext().AllocateCopy(lifetimeDependencies);
}
void LifetimeDependenceInfo::dump() const {
llvm::errs() << "target: " << getTargetIndex() << '\n';
if (isImmortal()) {
llvm::errs() << " immortal\n";
}
if (auto scoped = getScopeIndices()) {
llvm::errs() << " scoped: ";
scoped->dump();
}
if (auto inherited = getInheritIndices()) {
llvm::errs() << " inherited: ";
inherited->dump();
}
if (auto addressable = getAddressableIndices()) {
llvm::errs() << " addressable: ";
addressable->dump();
}
}
} // namespace swift
|