Skip to content

Commit ee79c9f

Browse files
authored
Merge pull request #40504 from ahoppen/pr/fix-snapshot-nullptr-issue
[SourceKit] Fix a crash that occurred when a document without an associated source file is reopened
2 parents e6cbf54 + f5b6103 commit ee79c9f

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: %{python} %utils/split_file.py -o %t %s
3+
4+
// This used to crash with a nullptr dereference because we didn't store a
5+
// snapshot in the FileContents of primary.swift when it is opened for the first
6+
// time but we are trying to access the snapshot when trying determining if we
7+
// can reuse the AST for the cursor info request
8+
9+
// RUN: %sourcekitd-test \
10+
// RUN: -req=open %t/primary.swift -- %t/primary.swift %t/secondary.swift \
11+
// RUN: == -req=close %t/primary.swift \
12+
// RUN: == -req=open %t/primary.swift -- %t/primary.swift \
13+
// RUN: == -req=cursor -pos 2:8 %t/primary.swift -- %t/primary.swift %t/secondary.swift \
14+
// RUN: | %FileCheck %s
15+
16+
// BEGIN primary.swift
17+
18+
struct Foo {}
19+
// CHECK: source.lang.swift.decl.struct
20+
// CHECK-NEXT: Foo
21+
// CHECK-NEXT: s:4main3FooV
22+
23+
// BEGIN secondary.swift

tools/SourceKit/lib/SwiftLang/SwiftASTManager.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1185,7 +1185,9 @@ ASTBuildOperationRef ASTProducer::getBuildOperationForConsumer(
11851185
std::vector<ImmutableTextSnapshotRef> Snapshots;
11861186
Snapshots.reserve(BuildOp->getFileContents().size());
11871187
for (auto &FileContent : BuildOp->getFileContents()) {
1188-
Snapshots.push_back(FileContent.Snapshot);
1188+
if (FileContent.Snapshot) {
1189+
Snapshots.push_back(FileContent.Snapshot);
1190+
}
11891191
}
11901192
if (BuildOp->matchesSourceState(FileSystem)) {
11911193
++Mgr->Impl.Stats->numASTCacheHits;

0 commit comments

Comments
 (0)