Skip to content

Commit a86fe4f

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: ...)'
1 parent 1f0696a commit a86fe4f

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
@@ -1069,6 +1069,13 @@ class LifetimeDependenceChecker {
10691069
return;
10701070
}
10711071
if (afd->getParameters()->size() > 0) {
1072+
if (useLazyInference()) {
1073+
// Assume that a mutating method does not depend on its parameters.
1074+
// This is unsafe but needed because some MutableSpan APIs snuck into
1075+
// the standard library interface without specifying dependencies.
1076+
pushDeps(createDeps(selfIndex).add(selfIndex,
1077+
LifetimeDependenceKind::Inherit));
1078+
}
10721079
return;
10731080
}
10741081
pushDeps(createDeps(selfIndex).add(selfIndex,
@@ -1115,6 +1122,17 @@ class LifetimeDependenceChecker {
11151122
.add(newValIdx, *kind));
11161123
break;
11171124
}
1125+
case AccessorKind::MutableAddress:
1126+
if (useLazyInference()) {
1127+
// Assume that a mutating method does not depend on its parameters.
1128+
// Currently only for backward interface compatibility. Even though this
1129+
// is the only useful dependence (a borrow of self is possible but not
1130+
// useful), explicit annotation is required for now to confirm that the
1131+
// mutated self cannot depend on anything stored at this address.
1132+
pushDeps(createDeps(selfIndex).add(selfIndex,
1133+
LifetimeDependenceKind::Inherit));
1134+
}
1135+
break;
11181136
default:
11191137
// Unknown mutating accessor.
11201138
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)