Skip to content

Commit d86553a

Browse files
committed
Add a "lazy" lifetime inference for mutating interface methods
When type checking a .swiftinterface file, Assume that a mutating methods does not depend on its parameters. This is unsafe but needed because some MutableSpan APIs snuck into the standard library interface without specifying dependencies. Fixes rdar://148697444 error: a mutating method with a ~Escapable 'self' requires '@Lifetime(self: ...)' (cherry picked from commit a86fe4f)
1 parent bd83ae1 commit d86553a

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

lib/AST/LifetimeDependence.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -987,6 +987,13 @@ class LifetimeDependenceChecker {
987987
return;
988988
}
989989
if (afd->getParameters()->size() > 0) {
990+
if (useLazyInference()) {
991+
// Assume that a mutating method does not depend on its parameters.
992+
// This is unsafe but needed because some MutableSpan APIs snuck into
993+
// the standard library interface without specifying dependencies.
994+
pushDeps(createDeps(selfIndex).add(selfIndex,
995+
LifetimeDependenceKind::Inherit));
996+
}
990997
return;
991998
}
992999
pushDeps(createDeps(selfIndex).add(selfIndex,
@@ -1032,6 +1039,17 @@ class LifetimeDependenceChecker {
10321039
.add(newValIdx, kind));
10331040
break;
10341041
}
1042+
case AccessorKind::MutableAddress:
1043+
if (useLazyInference()) {
1044+
// Assume that a mutating method does not depend on its parameters.
1045+
// Currently only for backward interface compatibility. Even though this
1046+
// is the only useful dependence (a borrow of self is possible but not
1047+
// useful), explicit annotation is required for now to confirm that the
1048+
// mutated self cannot depend on anything stored at this address.
1049+
pushDeps(createDeps(selfIndex).add(selfIndex,
1050+
LifetimeDependenceKind::Inherit));
1051+
}
1052+
break;
10351053
default:
10361054
// Unknown mutating accessor.
10371055
break;

test/Sema/Inputs/lifetime_depend_infer.swiftinterface

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,18 @@ public struct NonEscapableSelf : ~Swift.Escapable {
7171
public mutating func mutatingMethodOneParamBorrow(_: Swift.Int) -> lifetime_depend_infer.NonEscapableSelf
7272
#endif
7373
}
74+
75+
public struct NoncopyableInoutMethods : ~Swift.Copyable & ~Swift.Escapable {
76+
#if $LifetimeDependence
77+
public mutating func mutatingMethodOneParamFunctionType<E, Result>(_ body: (Swift.Int) throws(E) -> Result) throws(E) -> Result where E : Swift.Error, Result : ~Swift.Copyable
78+
79+
public subscript(position: Swift.Int) -> Swift.Int {
80+
unsafeAddress
81+
unsafeMutableAddress
82+
}
83+
#endif
84+
}
85+
7486
public struct EscapableTrivialSelf {
7587
#if compiler(>=5.3) && $NonescapableTypes && $LifetimeDependence
7688
@lifetime(self)

0 commit comments

Comments
 (0)