Skip to content

Commit 02d1974

Browse files
committed
Update SIL printing of @Lifetime
Lifetime dependencies in SIL tests continue to be represented as a type modifier on the target. As before they are represented as a LifetimeDependentTypeRepr in the AST.
1 parent 6fdd12f commit 02d1974

File tree

4 files changed

+47
-32
lines changed

4 files changed

+47
-32
lines changed

include/swift/Parse/Parser.h

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1213,7 +1213,7 @@ class Parser {
12131213
Tok.isContextualKeyword("sending"))
12141214
return true;
12151215
if (Context.LangOpts.hasFeature(Feature::NonescapableTypes) &&
1216-
isLifetimeDependenceToken())
1216+
isSILLifetimeDependenceToken())
12171217
return true;
12181218
return false;
12191219
}
@@ -1225,9 +1225,8 @@ class Parser {
12251225
consumeToken();
12261226
}
12271227

1228-
bool isLifetimeDependenceToken() {
1229-
return isInSILMode() && (Tok.isContextualKeyword("_inherit") ||
1230-
Tok.isContextualKeyword("_scope"));
1228+
bool isSILLifetimeDependenceToken() {
1229+
return isInSILMode() && (Tok.isContextualKeyword("_lifetime"));
12311230
}
12321231

12331232
bool canHaveParameterSpecifierContextualKeyword() {
@@ -1248,7 +1247,7 @@ class Parser {
12481247
return true;
12491248
}
12501249

1251-
return isLifetimeDependenceToken();
1250+
return isSILLifetimeDependenceToken();
12521251
}
12531252

12541253
bool parseConventionAttributeInternal(SourceLoc atLoc, SourceLoc attrLoc,

lib/AST/LifetimeDependence.cpp

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,29 +45,36 @@ getLifetimeDependenceFor(ArrayRef<LifetimeDependenceInfo> lifetimeDependencies,
4545
}
4646

4747
std::string LifetimeDependenceInfo::getString() const {
48-
std::string lifetimeDependenceString;
49-
auto getOnIndices = [](IndexSubset *bitvector) {
48+
std::string lifetimeDependenceString = "_lifetime(";
49+
auto getSourceString = [](IndexSubset *bitvector, StringRef kind) {
5050
std::string result;
5151
bool isFirstSetBit = true;
5252
for (unsigned i = 0; i < bitvector->getCapacity(); i++) {
5353
if (bitvector->contains(i)) {
5454
if (!isFirstSetBit) {
5555
result += ", ";
5656
}
57+
result += kind;
5758
result += std::to_string(i);
5859
isFirstSetBit = false;
5960
}
6061
}
6162
return result;
6263
};
63-
if (inheritLifetimeParamIndices && !inheritLifetimeParamIndices->isEmpty()) {
64-
lifetimeDependenceString =
65-
"_inherit(" + getOnIndices(inheritLifetimeParamIndices) + ") ";
64+
if (inheritLifetimeParamIndices) {
65+
assert(!inheritLifetimeParamIndices->isEmpty());
66+
lifetimeDependenceString +=
67+
getSourceString(inheritLifetimeParamIndices, "_copy ");
6668
}
67-
if (scopeLifetimeParamIndices && !scopeLifetimeParamIndices->isEmpty()) {
69+
if (scopeLifetimeParamIndices) {
70+
assert(!scopeLifetimeParamIndices->isEmpty());
71+
if (inheritLifetimeParamIndices) {
72+
lifetimeDependenceString += ", ";
73+
}
6874
lifetimeDependenceString +=
69-
"_scope(" + getOnIndices(scopeLifetimeParamIndices) + ") ";
75+
getSourceString(scopeLifetimeParamIndices, "_borrow ");
7076
}
77+
lifetimeDependenceString += ") ";
7178
return lifetimeDependenceString;
7279
}
7380

lib/Parse/ParseDecl.cpp

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5136,17 +5136,25 @@ ParserStatus Parser::parseTypeAttribute(TypeOrCustomAttr &result,
51365136
llvm_unreachable("bad attribute kind");
51375137
}
51385138

5139-
static ParsedLifetimeDependenceKind getSILLifetimeDependenceKind(const Token &T) {
5140-
if (T.isContextualKeyword("_inherit")) {
5141-
return ParsedLifetimeDependenceKind::Inherit;
5142-
}
5143-
assert(T.isContextualKeyword("_scope"));
5144-
return ParsedLifetimeDependenceKind::Scope;
5145-
}
5146-
51475139
ParserResult<LifetimeEntry> Parser::parseLifetimeEntry(SourceLoc loc) {
51485140
ParserStatus status;
51495141

5142+
auto getLifetimeDependenceKind =
5143+
[&](Token Tok) -> std::optional<ParsedLifetimeDependenceKind> {
5144+
if (Tok.isContextualKeyword("_copy")) {
5145+
return ParsedLifetimeDependenceKind::Inherit;
5146+
}
5147+
if (Tok.isContextualKeyword("_borrow")) {
5148+
return ParsedLifetimeDependenceKind::Scope;
5149+
}
5150+
if (Tok.isContextualKeyword("borrow") &&
5151+
peekToken().isAny(tok::identifier, tok::integer_literal,
5152+
tok::kw_self)) {
5153+
return ParsedLifetimeDependenceKind::Scope;
5154+
}
5155+
return std::nullopt;
5156+
};
5157+
51505158
if (!Tok.isFollowingLParen()) {
51515159
diagnose(loc, diag::expected_lparen_after_lifetime_dependence);
51525160
status.setIsParseError();
@@ -5174,13 +5182,14 @@ ParserResult<LifetimeEntry> Parser::parseLifetimeEntry(SourceLoc loc) {
51745182
diag::expected_rparen_after_lifetime_dependence, [&]() -> ParserStatus {
51755183
ParserStatus listStatus;
51765184
foundParamId = true;
5185+
51775186
auto lifetimeDependenceKind = ParsedLifetimeDependenceKind::Default;
5178-
if (Tok.isContextualKeyword("borrow") &&
5179-
peekToken().isAny(tok::identifier, tok::integer_literal,
5180-
tok::kw_self)) {
5181-
lifetimeDependenceKind = ParsedLifetimeDependenceKind::Scope;
5187+
auto result = getLifetimeDependenceKind(Tok);
5188+
if (result.has_value()) {
5189+
lifetimeDependenceKind = *result;
51825190
consumeToken();
51835191
}
5192+
51845193
auto sourceDescriptor =
51855194
parseLifetimeDescriptor(Tok, lifetimeDependenceKind);
51865195
if (!sourceDescriptor) {
@@ -5504,7 +5513,7 @@ ParserStatus Parser::ParsedTypeAttributeList::slowParse(Parser &P) {
55045513
continue;
55055514
}
55065515

5507-
if (P.isLifetimeDependenceToken()) {
5516+
if (P.isSILLifetimeDependenceToken()) {
55085517
if (!P.Context.LangOpts.hasFeature(Feature::NonescapableTypes)) {
55095518
P.diagnose(Tok, diag::requires_experimental_feature,
55105519
"lifetime dependence specifier", false,

test/Interop/Cxx/class/nonescapable-lifetimebound.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,12 @@ private:
6969
};
7070

7171
// CHECK: sil [clang makeOwner] {{.*}}: $@convention(c) () -> Owner
72-
// CHECK: sil [clang getView] {{.*}} : $@convention(c) (@in_guaranteed Owner) -> _scope(0) @autoreleased View
73-
// CHECK: sil [clang getViewFromFirst] {{.*}} : $@convention(c) (@in_guaranteed Owner, @in_guaranteed Owner) -> _scope(0) @autoreleased View
74-
// CHECK: sil [clang getViewFromEither] {{.*}} : $@convention(c) (@in_guaranteed Owner, @in_guaranteed Owner) -> _scope(0, 1) @autoreleased View
75-
// CHECK: sil [clang Owner.handOutView] {{.*}} : $@convention(cxx_method) (@in_guaranteed Owner) -> _scope(0) @autoreleased View
76-
// CHECK: sil [clang Owner.handOutView2] {{.*}} : $@convention(cxx_method) (View, @in_guaranteed Owner) -> _scope(1) @autoreleased View
77-
// CHECK: sil [clang getViewFromEither] {{.*}} : $@convention(c) (@guaranteed View, @guaranteed View) -> _inherit(0, 1) @autoreleased View
72+
// CHECK: sil [clang getView] {{.*}} : $@convention(c) (@in_guaranteed Owner) -> _lifetime(_borrow 0) @autoreleased View
73+
// CHECK: sil [clang getViewFromFirst] {{.*}} : $@convention(c) (@in_guaranteed Owner, @in_guaranteed Owner) -> _lifetime(_borrow 0) @autoreleased View
74+
// CHECK: sil [clang getViewFromEither] {{.*}} : $@convention(c) (@in_guaranteed Owner, @in_guaranteed Owner) -> _lifetime(_borrow 0, 1) @autoreleased View
75+
// CHECK: sil [clang Owner.handOutView] {{.*}} : $@convention(cxx_method) (@in_guaranteed Owner) -> _lifetime(_borrow 0) @autoreleased View
76+
// CHECK: sil [clang Owner.handOutView2] {{.*}} : $@convention(cxx_method) (View, @in_guaranteed Owner) -> _lifetime(_borrow 1) @autoreleased View
77+
// CHECK: sil [clang getViewFromEither] {{.*}} : $@convention(c) (@guaranteed View, @guaranteed View) -> _lifetime(_copy 0, 1) @autoreleased View
7878
// CHECK: sil [clang View.init] {{.*}} : $@convention(c) () -> @out View
7979

8080
//--- test.swift
@@ -91,4 +91,4 @@ public func test() {
9191
let _ = o.handOutView2(v1)
9292
let _ = getViewFromEither(v1, v2)
9393
let defaultView = View()
94-
}
94+
}

0 commit comments

Comments
 (0)