Skip to content

Commit 00cecab

Browse files
committed
[NFC] Update diagnostic msg
1 parent 8d9fac5 commit 00cecab

File tree

3 files changed

+37
-37
lines changed

3 files changed

+37
-37
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8144,8 +8144,8 @@ ERROR(lifetime_dependence_invalid_inherit_escapable_type, none,
81448144
"'@lifetime(borrow %0)' instead",
81458145
(StringRef))
81468146
ERROR(lifetime_dependence_cannot_use_parsed_borrow_consuming, none,
8147-
"invalid use of borrow dependence with consuming ownership",
8148-
())
8147+
"invalid use of %0 dependence with %1 ownership",
8148+
(StringRef, StringRef))
81498149
ERROR(lifetime_dependence_cannot_use_default_escapable_consuming, none,
81508150
"invalid lifetime dependence on Escapable value with %0 ownership",
81518151
(StringRef))

include/swift/AST/LifetimeDependence.h

Lines changed: 4 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ class LifetimeEntry final
183183
ArrayRef<LifetimeDescriptor> sources,
184184
std::optional<LifetimeDescriptor> targetDescriptor = std::nullopt);
185185

186+
std::string getString() const;
186187
SourceLoc getLoc() const { return startLoc; }
187188
SourceLoc getStartLoc() const { return startLoc; }
188189
SourceLoc getEndLoc() const { return endLoc; }
@@ -194,38 +195,6 @@ class LifetimeEntry final
194195
std::optional<LifetimeDescriptor> getTargetDescriptor() const {
195196
return targetDescriptor;
196197
}
197-
198-
std::string getString() const {
199-
std::string result = "@lifetime(";
200-
if (targetDescriptor.has_value()) {
201-
result += targetDescriptor->getString();
202-
result += ": ";
203-
}
204-
205-
bool firstElem = true;
206-
for (auto source : getSources()) {
207-
if (!firstElem) {
208-
result += ", ";
209-
}
210-
switch (source.getParsedLifetimeDependenceKind()) {
211-
case ParsedLifetimeDependenceKind::Borrow:
212-
result += "borrow ";
213-
break;
214-
case ParsedLifetimeDependenceKind::Inherit:
215-
result += "copy ";
216-
break;
217-
case ParsedLifetimeDependenceKind::InOut:
218-
result += "inout ";
219-
break;
220-
default:
221-
break;
222-
}
223-
result += source.getString();
224-
firstElem = false;
225-
}
226-
result += ")";
227-
return result;
228-
}
229198
};
230199

231200
class LifetimeDependenceInfo {
@@ -354,6 +323,9 @@ class LifetimeDependenceInfo {
354323
std::optional<LifetimeDependenceInfo>
355324
getLifetimeDependenceFor(ArrayRef<LifetimeDependenceInfo> lifetimeDependencies,
356325
unsigned index);
326+
327+
StringRef
328+
getNameForParsedLifetimeDependenceKind(ParsedLifetimeDependenceKind kind);
357329
} // namespace swift
358330

359331
#endif

lib/AST/LifetimeDependence.cpp

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,31 @@ LifetimeEntry::create(const ASTContext &ctx, SourceLoc startLoc,
3737
return new (mem) LifetimeEntry(startLoc, endLoc, sources, targetDescriptor);
3838
}
3939

40+
std::string LifetimeEntry::getString() const {
41+
std::string result = "@lifetime(";
42+
if (targetDescriptor.has_value()) {
43+
result += targetDescriptor->getString();
44+
result += ": ";
45+
}
46+
47+
bool firstElem = true;
48+
for (auto source : getSources()) {
49+
if (!firstElem) {
50+
result += ", ";
51+
}
52+
auto kindString = getNameForParsedLifetimeDependenceKind(
53+
source.getParsedLifetimeDependenceKind());
54+
if (!kindString.empty()) {
55+
result += kindString;
56+
result += " ";
57+
}
58+
result += source.getString();
59+
firstElem = false;
60+
}
61+
result += ")";
62+
return result;
63+
}
64+
4065
std::optional<LifetimeDependenceInfo>
4166
getLifetimeDependenceFor(ArrayRef<LifetimeDependenceInfo> lifetimeDependencies,
4267
unsigned index) {
@@ -546,7 +571,7 @@ class LifetimeDependenceChecker {
546571
std::optional<LifetimeDependenceKind>
547572
getDependenceKindFromDescriptor(LifetimeDescriptor descriptor,
548573
ParamDecl *decl) {
549-
auto loc = decl->getLoc();
574+
auto loc = descriptor.getLoc();
550575
auto type = decl->getTypeInContext();
551576
auto parsedLifetimeKind = descriptor.getParsedLifetimeDependenceKind();
552577
auto ownership = decl->getValueOwnership();
@@ -576,9 +601,12 @@ class LifetimeDependenceChecker {
576601

577602
// @lifetime(borrow x) is valid only for borrowing parameters.
578603
// @lifetime(inout x) is valid only for inout parameters.
579-
if (!isCompatibleWithOwnership(parsedLifetimeKind, type, ownership)) {
604+
if (!isCompatibleWithOwnership(parsedLifetimeKind, type,
605+
loweredOwnership)) {
580606
diagnose(loc,
581-
diag::lifetime_dependence_cannot_use_parsed_borrow_consuming);
607+
diag::lifetime_dependence_cannot_use_parsed_borrow_consuming,
608+
getNameForParsedLifetimeDependenceKind(parsedLifetimeKind),
609+
getOwnershipSpelling(loweredOwnership));
582610
return std::nullopt;
583611
}
584612
// @lifetime(copy x) is only invalid for Escapable types.

0 commit comments

Comments
 (0)