Skip to content

Commit f0ea24d

Browse files
committed
[SourceKit] Fix a crash when performing related identifers request on self
Performing a related identifiers request on `self` returns an empty result because `self` is not an identifier. We thus can’t retrieve the first location to compute the old name. rdar://121668042
1 parent 4bc7726 commit f0ea24d

File tree

4 files changed

+18
-4
lines changed

4 files changed

+18
-4
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
struct Test {
2+
func foo() {
3+
// RUN: %sourcekitd-test -req=related-idents -pos=%(line + 1):6 %s -- %s | %FileCheck %s
4+
self
5+
}
6+
}
7+
8+
// CHECK: START RANGES
9+
// CHECK-NEXT: END RANGES

tools/SourceKit/include/SourceKit/Core/LangSupport.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -792,10 +792,10 @@ struct RelatedIdentInfo {
792792
/// Result of `findRelatedIdentifiersInFile`.
793793
struct RelatedIdentsResult {
794794
SmallVector<RelatedIdentInfo, 8> RelatedIdents;
795-
std::string OldName;
795+
std::optional<std::string> OldName;
796796

797797
RelatedIdentsResult(SmallVector<RelatedIdentInfo, 8> RelatedIdents,
798-
std::string OldName)
798+
std::optional<std::string> OldName)
799799
: RelatedIdents(RelatedIdents), OldName(OldName) {}
800800

801801
static RelatedIdentsResult empty() { return RelatedIdentsResult({}, ""); }

tools/SourceKit/lib/SwiftLang/SwiftSourceDocInfo.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2550,7 +2550,10 @@ void SwiftLangSupport::findRelatedIdentifiersInFile(
25502550

25512551
RenameLocs Locs = localRenameLocs(SrcFile, Info->VD);
25522552

2553-
std::string OldName = Locs.getLocations().front().OldName.str();
2553+
std::optional<std::string> OldName;
2554+
if (!Locs.getLocations().empty()) {
2555+
OldName = Locs.getLocations().front().OldName.str();
2556+
}
25542557
#ifndef NDEBUG
25552558
for (auto loc : Locs.getLocations()) {
25562559
assert(loc.OldName == OldName &&

tools/SourceKit/tools/sourcekitd/lib/Service/Requests.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2903,7 +2903,9 @@ static void findRelatedIdents(StringRef PrimaryFilePath,
29032903
Elem.set(KeyLength, R.Length);
29042904
Elem.set(KeyNameType, renameLocUsageUID(R.Usage));
29052905
}
2906-
RespBuilder.getDictionary().set(KeyName, Info.OldName);
2906+
if (Info.OldName) {
2907+
RespBuilder.getDictionary().set(KeyName, *Info.OldName);
2908+
}
29072909

29082910
Rec(RespBuilder.createResponse());
29092911
});

0 commit comments

Comments
 (0)