Skip to content

Commit 1739c23

Browse files
authored
Merge pull request #40158 from ahoppen/pr/cancel-code-completion
[SourceKit] Support cancellation of code completion like requests
2 parents 9b9bcd6 + 177ab6e commit 1739c23

File tree

16 files changed

+292
-155
lines changed

16 files changed

+292
-155
lines changed

include/swift/AST/ASTContext.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,15 @@ class ASTContext final {
273273
/// Diags - The diagnostics engine.
274274
DiagnosticEngine &Diags;
275275

276+
/// If the shared pointer is not a \c nullptr and the pointee is \c true,
277+
/// all operations working on this ASTContext should be aborted at the next
278+
/// possible opportunity.
279+
/// This is used by SourceKit to cancel requests for which the result is no
280+
/// longer of interest.
281+
/// The returned result will be discarded, so the operation that acknowledges
282+
/// the cancellation might return with any result.
283+
std::shared_ptr<std::atomic<bool>> CancellationFlag = nullptr;
284+
276285
TypeCheckCompletionCallback *CompletionCallback = nullptr;
277286

278287
/// The request-evaluator that is used to process various requests.

include/swift/Basic/LangOptions.h

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

616-
/// If the shared pointer is not a \c nullptr and the pointee is \c true,
617-
/// typechecking should be aborted at the next possible opportunity.
618-
/// This is used by SourceKit to cancel requests for which the result is no
619-
/// longer of interest.
620-
std::shared_ptr<std::atomic<bool>> CancellationFlag = nullptr;
621-
622616
/// If non-zero, abort the switch statement exhaustiveness checker if
623617
/// the Space::minus function is called more than this many times.
624618
///

include/swift/IDE/CompletionInstance.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ class CompletionInstance {
106106
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> FileSystem,
107107
llvm::MemoryBuffer *completionBuffer, unsigned int Offset,
108108
DiagnosticConsumer *DiagC,
109+
std::shared_ptr<std::atomic<bool>> CancellationFlag,
109110
llvm::function_ref<void(CancellableResult<CompletionInstanceResult>)>
110111
Callback);
111112

@@ -119,6 +120,7 @@ class CompletionInstance {
119120
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> FileSystem,
120121
llvm::MemoryBuffer *completionBuffer, unsigned int Offset,
121122
DiagnosticConsumer *DiagC,
123+
std::shared_ptr<std::atomic<bool>> CancellationFlag,
122124
llvm::function_ref<void(CancellableResult<CompletionInstanceResult>)>
123125
Callback);
124126

@@ -129,6 +131,14 @@ class CompletionInstance {
129131
/// In case of failure or cancellation, the callback receives the
130132
/// corresponding failed or cancelled result.
131133
///
134+
/// If \p CancellationFlag is not \c nullptr, code completion can be cancelled
135+
/// by setting the flag to \c true.
136+
/// IMPORTANT: If \p CancellationFlag is not \c nullptr, then completion might
137+
/// be cancelled in the secon pass that's invoked inside \p Callback.
138+
/// Therefore, \p Callback MUST check whether completion was cancelled before
139+
/// interpreting the results, since invalid results may be returned in case
140+
/// of cancellation.
141+
///
132142
/// NOTE: \p Args is only used for checking the equaity of the invocation.
133143
/// Since this function assumes that it is already normalized, exact the same
134144
/// arguments including their order is considered as the same invocation.
@@ -137,6 +147,7 @@ class CompletionInstance {
137147
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> FileSystem,
138148
llvm::MemoryBuffer *completionBuffer, unsigned int Offset,
139149
DiagnosticConsumer *DiagC,
150+
std::shared_ptr<std::atomic<bool>> CancellationFlag,
140151
llvm::function_ref<void(CancellableResult<CompletionInstanceResult>)>
141152
Callback);
142153

@@ -155,13 +166,15 @@ class CompletionInstance {
155166
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> FileSystem,
156167
llvm::MemoryBuffer *completionBuffer, unsigned int Offset,
157168
DiagnosticConsumer *DiagC, ide::CodeCompletionContext &CompletionContext,
169+
std::shared_ptr<std::atomic<bool>> CancellationFlag,
158170
llvm::function_ref<void(CancellableResult<CodeCompleteResult>)> Callback);
159171

160172
void typeContextInfo(
161173
swift::CompilerInvocation &Invocation, llvm::ArrayRef<const char *> Args,
162174
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> FileSystem,
163175
llvm::MemoryBuffer *completionBuffer, unsigned int Offset,
164176
DiagnosticConsumer *DiagC,
177+
std::shared_ptr<std::atomic<bool>> CancellationFlag,
165178
llvm::function_ref<void(CancellableResult<TypeContextInfoResult>)>
166179
Callback);
167180

@@ -170,6 +183,7 @@ class CompletionInstance {
170183
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> FileSystem,
171184
llvm::MemoryBuffer *completionBuffer, unsigned int Offset,
172185
DiagnosticConsumer *DiagC, ArrayRef<const char *> ExpectedTypeNames,
186+
std::shared_ptr<std::atomic<bool>> CancellationFlag,
173187
llvm::function_ref<void(CancellableResult<ConformingMethodListResults>)>
174188
Callback);
175189
};

include/swift/Sema/ConstraintSystem.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5179,7 +5179,7 @@ class ConstraintSystem {
51795179
if (isExpressionAlreadyTooComplex)
51805180
return true;
51815181

5182-
auto CancellationFlag = getASTContext().TypeCheckerOpts.CancellationFlag;
5182+
auto CancellationFlag = getASTContext().CancellationFlag;
51835183
if (CancellationFlag && CancellationFlag->load(std::memory_order_relaxed))
51845184
return true;
51855185

0 commit comments

Comments
 (0)