Skip to content

Commit 2d9217b

Browse files
authored
Merge pull request #80541 from meg-gupta/inoutreland
Reland #80452
2 parents cc23c25 + 5fac8a4 commit 2d9217b

26 files changed

+262
-156
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
@@ -8195,8 +8195,11 @@ ERROR(lifetime_dependence_invalid_inherit_escapable_type, none,
81958195
"'@lifetime(borrow %0)' instead",
81968196
(StringRef))
81978197
ERROR(lifetime_dependence_cannot_use_parsed_borrow_consuming, none,
8198-
"invalid use of borrow dependence with consuming ownership",
8199-
())
8198+
"invalid use of %0 dependence with %1 ownership",
8199+
(StringRef, StringRef))
8200+
ERROR(lifetime_dependence_cannot_use_default_escapable_consuming, none,
8201+
"invalid lifetime dependence on an Escapable value with %0 ownership",
8202+
(StringRef))
82008203
ERROR(lifetime_dependence_cannot_use_parsed_borrow_inout, none,
82018204
"invalid use of borrow dependence on the same inout parameter",
82028205
())

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 {
@@ -366,6 +339,9 @@ filterEscapableLifetimeDependencies(GenericSignature sig,
366339
SmallVectorImpl<LifetimeDependenceInfo> &outputs,
367340
llvm::function_ref<Type (unsigned targetIndex)> getSubstTargetType);
368341

342+
StringRef
343+
getNameForParsedLifetimeDependenceKind(ParsedLifetimeDependenceKind kind);
344+
369345
} // namespace swift
370346

371347
#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
@@ -270,6 +270,29 @@ static bool usesFeatureLifetimeDependence(Decl *decl) {
270270
return false;
271271
}
272272

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

0 commit comments

Comments
 (0)