Skip to content

Commit 4521c99

Browse files
authored
Merge pull request swiftlang#38782 from ahoppen/pr/sourcekit-cancellation
[SourceKit] Support cancellation of requests while an AST is being built
2 parents d301f66 + 7a80034 commit 4521c99

File tree

6 files changed

+803
-245
lines changed

6 files changed

+803
-245
lines changed

include/swift/Basic/LangOptions.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -595,6 +595,12 @@ namespace swift {
595595
/// than this many seconds.
596596
unsigned ExpressionTimeoutThreshold = 600;
597597

598+
/// If the shared pointer is not a \c nullptr and the pointee is \c true,
599+
/// typechecking should be aborted at the next possible opportunity.
600+
/// This is used by SourceKit to cancel requests for which the result is no
601+
/// longer of interest.
602+
std::shared_ptr<std::atomic<bool>> CancellationFlag = nullptr;
603+
598604
/// If non-zero, abort the switch statement exhaustiveness checker if
599605
/// the Space::minus function is called more than this many times.
600606
///

include/swift/Sema/ConstraintSystem.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5113,6 +5113,10 @@ class ConstraintSystem {
51135113
if (isExpressionAlreadyTooComplex)
51145114
return true;
51155115

5116+
auto CancellationFlag = getASTContext().TypeCheckerOpts.CancellationFlag;
5117+
if (CancellationFlag && CancellationFlag->load(std::memory_order_relaxed))
5118+
return true;
5119+
51165120
auto used = getASTContext().getSolverMemory() + solutionMemory;
51175121
MaxMemory = std::max(used, MaxMemory);
51185122
auto threshold = getASTContext().TypeCheckerOpts.SolverMemoryThreshold;

test/SourceKit/Misc/stats.swift

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ func foo() {}
4646
// SEMA_3: 1 {{.*}} source.statistic.num-ast-builds
4747
// SEMA_3: 1 {{.*}} source.statistic.num-asts-in-memory
4848
// SEMA_3: 1 {{.*}} source.statistic.max-asts-in-memory
49-
// SEMA_3: 0 {{.*}} source.statistic.num-ast-cache-hits
50-
// SEMA_3: 1 {{.*}} source.statistic.num-ast-snaphost-uses
49+
// SEMA_3: 1 {{.*}} source.statistic.num-ast-cache-hits
50+
// SEMA_3: 0 {{.*}} source.statistic.num-ast-snaphost-uses
5151

5252
// RUN: %sourcekitd-test -req=sema %s -- -Xfrontend -disable-implicit-concurrency-module-import %s == -req=related-idents -pos=1:6 %s -- -Xfrontend -disable-implicit-concurrency-module-import %s == -req=stats | %FileCheck %s -check-prefix=SEMA_4
5353

@@ -58,3 +58,14 @@ func foo() {}
5858
// SEMA_4: 1 {{.*}} source.statistic.max-asts-in-memory
5959
// SEMA_4: 1 {{.*}} source.statistic.num-ast-cache-hits
6060
// SEMA_4: 0 {{.*}} source.statistic.num-ast-snaphost-uses
61+
62+
// Test that we can have two files open and don't need to rebuild an AST when doing the cursor info request on '%s' after opening '10bytes.swift'
63+
// RUN: %sourcekitd-test \
64+
// RUN: -req=sema %s -- -Xfrontend -disable-implicit-concurrency-module-import %s == \
65+
// RUN: -req=sema %S/Inputs/10bytes.swift -- -Xfrontend -disable-implicit-concurrency-module-import %S/Inputs/10bytes.swift == \
66+
// RUN: -req=cursor -pos=1:6 %s -- -Xfrontend -disable-implicit-concurrency-module-import %s == \
67+
// RUN: -req=stats | %FileCheck %s -check-prefix=OPEN_TWO_FILES
68+
69+
// OPEN_TWO_FILES: 2 {{.*}} source.statistic.num-ast-builds
70+
// OPEN_TWO_FILES: 2 {{.*}} source.statistic.num-asts-in-memory
71+
// OPEN_TWO_FILES: 2 {{.*}} source.statistic.max-asts-in-memory

0 commit comments

Comments
 (0)