Skip to content

Commit 8e72431

Browse files
committed
[NFC] Update diagnostic msg
1 parent 3e49433 commit 8e72431

File tree

3 files changed

+42
-38
lines changed

3 files changed

+42
-38
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8169,8 +8169,8 @@ 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))
81748174
ERROR(lifetime_dependence_cannot_use_default_escapable_consuming, none,
81758175
"invalid lifetime dependence on Escapable value with %0 ownership",
81768176
(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 {
@@ -370,6 +339,9 @@ filterEscapableLifetimeDependencies(GenericSignature sig,
370339
SmallVectorImpl<LifetimeDependenceInfo> &outputs,
371340
llvm::function_ref<Type (unsigned targetIndex)> getSubstTargetType);
372341

342+
StringRef
343+
getNameForParsedLifetimeDependenceKind(ParsedLifetimeDependenceKind kind);
344+
373345
} // namespace swift
374346

375347
#endif

lib/AST/LifetimeDependence.cpp

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,35 @@ 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 lifetimeKind = source.getParsedLifetimeDependenceKind();
53+
auto kindString = getNameForParsedLifetimeDependenceKind(lifetimeKind);
54+
bool printSpace = (lifetimeKind == ParsedLifetimeDependenceKind::Borrow ||
55+
lifetimeKind == ParsedLifetimeDependenceKind::Copy);
56+
if (!kindString.empty()) {
57+
result += kindString;
58+
}
59+
if (printSpace) {
60+
result += " ";
61+
}
62+
result += source.getString();
63+
firstElem = false;
64+
}
65+
result += ")";
66+
return result;
67+
}
68+
4069
std::optional<LifetimeDependenceInfo>
4170
getLifetimeDependenceFor(ArrayRef<LifetimeDependenceInfo> lifetimeDependencies,
4271
unsigned index) {
@@ -82,7 +111,7 @@ getNameForParsedLifetimeDependenceKind(ParsedLifetimeDependenceKind kind) {
82111
case ParsedLifetimeDependenceKind::Inherit:
83112
return "copy";
84113
case ParsedLifetimeDependenceKind::Inout:
85-
return "inout";
114+
return "&";
86115
default:
87116
return "";
88117
}
@@ -572,7 +601,7 @@ class LifetimeDependenceChecker {
572601
std::optional<LifetimeDependenceKind>
573602
getDependenceKindFromDescriptor(LifetimeDescriptor descriptor,
574603
ParamDecl *decl) {
575-
auto loc = decl->getLoc();
604+
auto loc = descriptor.getLoc();
576605
auto type = decl->getTypeInContext();
577606
auto parsedLifetimeKind = descriptor.getParsedLifetimeDependenceKind();
578607
auto ownership = decl->getValueOwnership();
@@ -602,9 +631,12 @@ class LifetimeDependenceChecker {
602631

603632
// @lifetime(borrow x) is valid only for borrowing parameters.
604633
// @lifetime(inout x) is valid only for inout parameters.
605-
if (!isCompatibleWithOwnership(parsedLifetimeKind, type, ownership)) {
634+
if (!isCompatibleWithOwnership(parsedLifetimeKind, type,
635+
loweredOwnership)) {
606636
diagnose(loc,
607-
diag::lifetime_dependence_cannot_use_parsed_borrow_consuming);
637+
diag::lifetime_dependence_cannot_use_parsed_borrow_consuming,
638+
getNameForParsedLifetimeDependenceKind(parsedLifetimeKind),
639+
getOwnershipSpelling(loweredOwnership));
608640
return std::nullopt;
609641
}
610642
// @lifetime(copy x) is only invalid for Escapable types.

0 commit comments

Comments
 (0)