Skip to content

Commit 44bd40d

Browse files
authored
Merge pull request swiftlang#83690 from atrick/fix-infer-subscript
Lifetime inference: implicit subscript getter depenency + implicit _modify
2 parents 4a54598 + 836c336 commit 44bd40d

File tree

2 files changed

+22
-7
lines changed

2 files changed

+22
-7
lines changed

lib/AST/LifetimeDependence.cpp

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1021,7 +1021,8 @@ class LifetimeDependenceChecker {
10211021
}
10221022

10231023
bool nonEscapableSelf = isDiagnosedNonEscapable(selfTypeInContext);
1024-
if (auto accessor = dyn_cast<AccessorDecl>(afd)) {
1024+
auto accessor = dyn_cast<AccessorDecl>(afd);
1025+
if (accessor) {
10251026
if (isImplicitOrSIL() || useLazyInference()) {
10261027
if (nonEscapableSelf && afd->getImplicitSelfDecl()->isInOut()) {
10271028
// Implicit accessors that return or yield a non-Escapable value may
@@ -1038,14 +1039,18 @@ class LifetimeDependenceChecker {
10381039
}
10391040
// Explicit accessors are inferred the same way as regular methods.
10401041
}
1041-
// Do infer the result of a mutating method when 'self' is
1042-
// non-Escapable. The missing dependence on inout 'self' will be diagnosed
1043-
// later anyway, so an explicit annotation will still be needed.
1042+
// Do not infer the result's dependence when the method is mutating and
1043+
// 'self' is non-Escapable. Independently, a missing dependence on inout
1044+
// 'self' will be diagnosed. Since an explicit annotation will be needed for
1045+
// 'self', we also require the method's result to have an explicit
1046+
// annotation.
10441047
if (nonEscapableSelf && afd->getImplicitSelfDecl()->isInOut()) {
10451048
return;
10461049
}
1047-
// Methods with parameters only apply to lazy inference.
1048-
if (!useLazyInference() && afd->getParameters()->size() > 0) {
1050+
// Methods with parameters only apply to lazy inference. This does not
1051+
// include accessors because a subscript's index is assumed not to be the
1052+
// source of the result's dependency.
1053+
if (!accessor && !useLazyInference() && afd->getParameters()->size() > 0) {
10491054
return;
10501055
}
10511056
if (!useLazyInference() && !isImplicitOrSIL()) {
@@ -1388,7 +1393,7 @@ class LifetimeDependenceChecker {
13881393
return std::nullopt;
13891394
}
13901395
if (wrappedAccessorKind) {
1391-
auto *var = cast<VarDecl>(accessor->getStorage());
1396+
auto *var = cast<AbstractStorageDecl>(accessor->getStorage());
13921397
for (auto *wrappedAccessor : var->getAllAccessors()) {
13931398
if (wrappedAccessor->isImplicit())
13941399
continue;

test/Sema/lifetime_depend_infer.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,16 @@ struct Accessors {
318318
yield &ne
319319
}
320320
}
321+
322+
// Synthesized _modify...
323+
subscript(_ index: Int) -> NEImmortal {
324+
get { // OK
325+
NEImmortal()
326+
}
327+
328+
set { // OK (no dependency)
329+
}
330+
}
321331
}
322332

323333
struct TrivialAccessors {

0 commit comments

Comments
 (0)