Skip to content

Commit 60ce851

Browse files
committed
Lifetime inference: give subscripts an implicit dependency on 'self'
Allow a subscript getter to return a non-Escapable type without an explicit '@'lifetime annotation. subscript(_ index: Int) -> Span { get {} // OK }
1 parent f174185 commit 60ce851

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

lib/AST/LifetimeDependence.cpp

Lines changed: 11 additions & 6 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()) {

0 commit comments

Comments
 (0)