Skip to content

Commit b3881cd

Browse files
authored
Merge pull request #80645 from meg-gupta/lifetimeinoutcp
[6.2] Add support for inout lifetime dependencies
2 parents 608c035 + 4ec6f94 commit b3881cd

26 files changed

+268
-157
lines changed

include/swift/AST/ASTBridging.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -1086,8 +1086,9 @@ BridgedInlineAttr BridgedInlineAttr_createParsed(BridgedASTContext cContext,
10861086

10871087
enum ENUM_EXTENSIBILITY_ATTR(closed) BridgedParsedLifetimeDependenceKind {
10881088
BridgedParsedLifetimeDependenceKindDefault,
1089-
BridgedParsedLifetimeDependenceKindScope,
1089+
BridgedParsedLifetimeDependenceKindBorrow,
10901090
BridgedParsedLifetimeDependenceKindInherit,
1091+
BridgedParsedLifetimeDependenceKindInout
10911092
};
10921093

10931094
class BridgedLifetimeDescriptor {

include/swift/AST/DiagnosticsSema.def

+5-2
Original file line numberDiff line numberDiff line change
@@ -8169,8 +8169,11 @@ ERROR(lifetime_dependence_invalid_inherit_escapable_type, none,
81698169
"'@lifetime(borrow %0)' instead",
81708170
(StringRef))
81718171
ERROR(lifetime_dependence_cannot_use_parsed_borrow_consuming, none,
8172-
"invalid use of borrow dependence with consuming ownership",
8173-
())
8172+
"invalid use of %0 dependence with %1 ownership",
8173+
(StringRef, StringRef))
8174+
ERROR(lifetime_dependence_cannot_use_default_escapable_consuming, none,
8175+
"invalid lifetime dependence on an Escapable value with %0 ownership",
8176+
(StringRef))
81748177
ERROR(lifetime_dependence_cannot_use_parsed_borrow_inout, none,
81758178
"invalid use of borrow dependence on the same inout parameter",
81768179
())

include/swift/AST/LifetimeDependence.h

+7-31
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,9 @@ class SILResultInfo;
3838

3939
enum class ParsedLifetimeDependenceKind : uint8_t {
4040
Default = 0,
41-
Scope,
42-
Inherit // Only used with deserialized decls
41+
Borrow,
42+
Inherit, // Only used with deserialized decls
43+
Inout
4344
};
4445

4546
enum class LifetimeDependenceKind : uint8_t { Inherit = 0, Scope };
@@ -182,6 +183,7 @@ class LifetimeEntry final
182183
ArrayRef<LifetimeDescriptor> sources,
183184
std::optional<LifetimeDescriptor> targetDescriptor = std::nullopt);
184185

186+
std::string getString() const;
185187
SourceLoc getLoc() const { return startLoc; }
186188
SourceLoc getStartLoc() const { return startLoc; }
187189
SourceLoc getEndLoc() const { return endLoc; }
@@ -193,35 +195,6 @@ class LifetimeEntry final
193195
std::optional<LifetimeDescriptor> getTargetDescriptor() const {
194196
return targetDescriptor;
195197
}
196-
197-
std::string getString() const {
198-
std::string result = "@lifetime(";
199-
if (targetDescriptor.has_value()) {
200-
result += targetDescriptor->getString();
201-
result += ": ";
202-
}
203-
204-
bool firstElem = true;
205-
for (auto source : getSources()) {
206-
if (!firstElem) {
207-
result += ", ";
208-
}
209-
switch (source.getParsedLifetimeDependenceKind()) {
210-
case ParsedLifetimeDependenceKind::Scope:
211-
result += "borrow ";
212-
break;
213-
case ParsedLifetimeDependenceKind::Inherit:
214-
result += "copy ";
215-
break;
216-
default:
217-
break;
218-
}
219-
result += source.getString();
220-
firstElem = false;
221-
}
222-
result += ")";
223-
return result;
224-
}
225198
};
226199

227200
class LifetimeDependenceInfo {
@@ -361,6 +334,9 @@ filterEscapableLifetimeDependencies(GenericSignature sig,
361334
SmallVectorImpl<LifetimeDependenceInfo> &outputs,
362335
llvm::function_ref<Type (unsigned targetIndex)> getSubstTargetType);
363336

337+
StringRef
338+
getNameForParsedLifetimeDependenceKind(ParsedLifetimeDependenceKind kind);
339+
364340
} // namespace swift
365341

366342
#endif

include/swift/Basic/Features.def

+3
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,9 @@ EXPERIMENTAL_FEATURE(StructLetDestructuring, true)
416416
/// Enable returning non-escapable types from functions.
417417
EXPERIMENTAL_FEATURE(LifetimeDependence, true)
418418

419+
/// Enable inout lifetime dependence - @lifetime(&arg)
420+
EXPERIMENTAL_FEATURE(InoutLifetimeDependence, true)
421+
419422
/// Enable the `@_staticExclusiveOnly` attribute.
420423
EXPERIMENTAL_FEATURE(StaticExclusiveOnly, true)
421424

lib/AST/Bridging/DeclAttributeBridging.cpp

+4-2
Original file line numberDiff line numberDiff line change
@@ -470,10 +470,12 @@ unbridged(BridgedParsedLifetimeDependenceKind kind) {
470470
switch (kind) {
471471
case BridgedParsedLifetimeDependenceKindDefault:
472472
return swift::ParsedLifetimeDependenceKind::Default;
473-
case BridgedParsedLifetimeDependenceKindScope:
474-
return swift::ParsedLifetimeDependenceKind::Scope;
473+
case BridgedParsedLifetimeDependenceKindBorrow:
474+
return swift::ParsedLifetimeDependenceKind::Borrow;
475475
case BridgedParsedLifetimeDependenceKindInherit:
476476
return swift::ParsedLifetimeDependenceKind::Inherit;
477+
case BridgedParsedLifetimeDependenceKindInout:
478+
return swift::ParsedLifetimeDependenceKind::Inout;
477479
}
478480
llvm_unreachable("unhandled enum value");
479481
}

lib/AST/FeatureSet.cpp

+23
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,29 @@ static bool usesFeatureLifetimeDependence(Decl *decl) {
271271
return false;
272272
}
273273

274+
static bool usesFeatureInoutLifetimeDependence(Decl *decl) {
275+
auto hasInoutLifetimeDependence = [](Decl *decl) {
276+
for (auto attr : decl->getAttrs().getAttributes<LifetimeAttr>()) {
277+
for (auto source : attr->getLifetimeEntry()->getSources()) {
278+
if (source.getParsedLifetimeDependenceKind() ==
279+
ParsedLifetimeDependenceKind::Inout) {
280+
return true;
281+
}
282+
}
283+
}
284+
return false;
285+
};
286+
287+
switch (decl->getKind()) {
288+
case DeclKind::Var: {
289+
auto *var = cast<VarDecl>(decl);
290+
return llvm::any_of(var->getAllAccessors(), hasInoutLifetimeDependence);
291+
}
292+
default:
293+
return hasInoutLifetimeDependence(decl);
294+
}
295+
}
296+
274297
UNINTERESTING_FEATURE(DynamicActorIsolation)
275298
UNINTERESTING_FEATURE(NonfrozenEnumExhaustivity)
276299
UNINTERESTING_FEATURE(ClosureIsolation)

0 commit comments

Comments
 (0)