Skip to content

Commit c5c3446

Browse files
authored
Merge pull request #58999 from ahoppen/pr/cache-function-asyncness
[CodeCompletion] Cache 'async'-ness of free functions
2 parents ea9276f + 1150e70 commit c5c3446

21 files changed

+320
-262
lines changed

include/swift/AST/DiagnosticsIDE.def

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,21 @@
1717

1818
ERROR(ide_async_in_nonasync_context, none,
1919
"async %0 used in a context that does not support concurrency",
20-
(DeclName))
20+
(StringRef))
2121

2222
// NOTE: This is WARNING because this is emitted for cross actor references with
2323
// non-'Sendable' types. That is optionally ('-warn-concurrency') warning in
2424
// Swift 5.5.
2525
WARNING(ide_cross_actor_reference_swift5, none,
2626
"actor-isolated %0 should only be referenced from inside the actor",
27-
(DeclName))
27+
(StringRef))
2828

2929
WARNING(ide_redundant_import, none,
30-
"module %0 is already imported", (DeclName))
30+
"module %0 is already imported", (StringRef))
3131

3232
// FIXME: Inform which other 'import' this module came from.
3333
NOTE(ide_redundant_import_indirect, none,
34-
"module %0 is already imported via another module import", (DeclName))
34+
"module %0 is already imported via another module import", (StringRef))
3535

3636
WARNING(ide_availability_softdeprecated, Deprecation,
3737
"%select{getter for |setter for |}0%1 will be deprecated"
@@ -46,6 +46,10 @@ WARNING(ide_availability_softdeprecated_rename, Deprecation,
4646
": renamed to '%6'",
4747
(unsigned, DeclName, bool, StringRef, bool, llvm::VersionTuple, StringRef))
4848

49+
WARNING(ide_recursive_accessor_reference,none,
50+
"attempting to %select{access|modify}1 %0 within its own "
51+
"%select{getter|setter}1", (StringRef, bool))
52+
4953
//===----------------------------------------------------------------------===//
5054

5155
#define UNDEFINE_DIAGNOSTIC_MACROS

include/swift/IDE/CodeCompletionConsumer.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ class CodeCompletionConsumer {
3030
handleResultsAndModules(CodeCompletionContext &context,
3131
ArrayRef<RequestedCachedModule> requestedModules,
3232
const ExpectedTypeContext *TypeContext,
33-
const DeclContext *DC) = 0;
33+
const DeclContext *DC,
34+
bool CanCurrDeclContextHandleAsync) = 0;
3435
};
3536

3637
/// A simplified code completion consumer interface that clients can use to get
@@ -42,7 +43,8 @@ struct SimpleCachingCodeCompletionConsumer : public CodeCompletionConsumer {
4243
void handleResultsAndModules(CodeCompletionContext &context,
4344
ArrayRef<RequestedCachedModule> requestedModules,
4445
const ExpectedTypeContext *TypeContext,
45-
const DeclContext *DCForModules) override;
46+
const DeclContext *DCForModules,
47+
bool CanCurrDeclContextHandleAsync) override;
4648

4749
/// Clients should override this method to receive \p Results.
4850
virtual void handleResults(CodeCompletionContext &context) = 0;

include/swift/IDE/CodeCompletionResult.h

Lines changed: 51 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,7 @@ class ContextFreeCodeCompletionResult {
329329
static_assert(int(CodeCompletionOperatorKind::MAX_VALUE) < 1 << 6, "");
330330

331331
bool IsSystem : 1;
332+
bool IsAsync : 1;
332333
CodeCompletionString *CompletionString;
333334
NullTerminatedStringRef ModuleName;
334335
NullTerminatedStringRef BriefDocComment;
@@ -344,6 +345,10 @@ class ContextFreeCodeCompletionResult {
344345
NullTerminatedStringRef DiagnosticMessage;
345346
NullTerminatedStringRef FilterName;
346347

348+
/// If the result represents a \c ValueDecl the name by which this decl should
349+
/// be refered to in diagnostics.
350+
NullTerminatedStringRef NameForDiagnostics;
351+
347352
public:
348353
/// Memberwise initializer. \p AssociatedKInd is opaque and will be
349354
/// interpreted based on \p Kind. If \p KnownOperatorKind is \c None and the
@@ -355,7 +360,7 @@ class ContextFreeCodeCompletionResult {
355360
/// \c CodeCompletionResultSink as the result itself.
356361
ContextFreeCodeCompletionResult(
357362
CodeCompletionResultKind Kind, uint8_t AssociatedKind,
358-
CodeCompletionOperatorKind KnownOperatorKind, bool IsSystem,
363+
CodeCompletionOperatorKind KnownOperatorKind, bool IsSystem, bool IsAsync,
359364
CodeCompletionString *CompletionString,
360365
NullTerminatedStringRef ModuleName,
361366
NullTerminatedStringRef BriefDocComment,
@@ -364,13 +369,15 @@ class ContextFreeCodeCompletionResult {
364369
ContextFreeNotRecommendedReason NotRecommended,
365370
CodeCompletionDiagnosticSeverity DiagnosticSeverity,
366371
NullTerminatedStringRef DiagnosticMessage,
367-
NullTerminatedStringRef FilterName)
372+
NullTerminatedStringRef FilterName,
373+
NullTerminatedStringRef NameForDiagnostics)
368374
: Kind(Kind), KnownOperatorKind(KnownOperatorKind), IsSystem(IsSystem),
369-
CompletionString(CompletionString), ModuleName(ModuleName),
370-
BriefDocComment(BriefDocComment), AssociatedUSRs(AssociatedUSRs),
371-
ResultType(ResultType), NotRecommended(NotRecommended),
372-
DiagnosticSeverity(DiagnosticSeverity),
373-
DiagnosticMessage(DiagnosticMessage), FilterName(FilterName) {
375+
IsAsync(IsAsync), CompletionString(CompletionString),
376+
ModuleName(ModuleName), BriefDocComment(BriefDocComment),
377+
AssociatedUSRs(AssociatedUSRs), ResultType(ResultType),
378+
NotRecommended(NotRecommended), DiagnosticSeverity(DiagnosticSeverity),
379+
DiagnosticMessage(DiagnosticMessage), FilterName(FilterName),
380+
NameForDiagnostics(NameForDiagnostics) {
374381
this->AssociatedKind.Opaque = AssociatedKind;
375382
assert((NotRecommended == ContextFreeNotRecommendedReason::None) ==
376383
(DiagnosticSeverity == CodeCompletionDiagnosticSeverity::None) &&
@@ -396,7 +403,7 @@ class ContextFreeCodeCompletionResult {
396403
static ContextFreeCodeCompletionResult *createPatternOrBuiltInOperatorResult(
397404
CodeCompletionResultSink &Sink, CodeCompletionResultKind Kind,
398405
CodeCompletionString *CompletionString,
399-
CodeCompletionOperatorKind KnownOperatorKind,
406+
CodeCompletionOperatorKind KnownOperatorKind, bool IsAsync,
400407
NullTerminatedStringRef BriefDocComment,
401408
CodeCompletionResultType ResultType,
402409
ContextFreeNotRecommendedReason NotRecommended,
@@ -431,15 +438,17 @@ class ContextFreeCodeCompletionResult {
431438
/// \note The caller must ensure that the \p CompletionString and all
432439
/// \c StringRefs outlive this result, typically by storing them in the same
433440
/// \c CodeCompletionResultSink as the result itself.
434-
static ContextFreeCodeCompletionResult *createDeclResult(
435-
CodeCompletionResultSink &Sink, CodeCompletionString *CompletionString,
436-
const Decl *AssociatedDecl, NullTerminatedStringRef ModuleName,
437-
NullTerminatedStringRef BriefDocComment,
438-
ArrayRef<NullTerminatedStringRef> AssociatedUSRs,
439-
CodeCompletionResultType ResultType,
440-
ContextFreeNotRecommendedReason NotRecommended,
441-
CodeCompletionDiagnosticSeverity DiagnosticSeverity,
442-
NullTerminatedStringRef DiagnosticMessage);
441+
static ContextFreeCodeCompletionResult *
442+
createDeclResult(CodeCompletionResultSink &Sink,
443+
CodeCompletionString *CompletionString,
444+
const Decl *AssociatedDecl, bool IsAsync,
445+
NullTerminatedStringRef ModuleName,
446+
NullTerminatedStringRef BriefDocComment,
447+
ArrayRef<NullTerminatedStringRef> AssociatedUSRs,
448+
CodeCompletionResultType ResultType,
449+
ContextFreeNotRecommendedReason NotRecommended,
450+
CodeCompletionDiagnosticSeverity DiagnosticSeverity,
451+
NullTerminatedStringRef DiagnosticMessage);
443452

444453
CodeCompletionResultKind getKind() const { return Kind; }
445454

@@ -469,6 +478,8 @@ class ContextFreeCodeCompletionResult {
469478

470479
bool isSystem() const { return IsSystem; };
471480

481+
bool isAsync() const { return IsAsync; };
482+
472483
CodeCompletionString *getCompletionString() const { return CompletionString; }
473484

474485
NullTerminatedStringRef getModuleName() const { return ModuleName; }
@@ -494,6 +505,10 @@ class ContextFreeCodeCompletionResult {
494505

495506
NullTerminatedStringRef getFilterName() const { return FilterName; }
496507

508+
NullTerminatedStringRef getNameForDiagnostics() const {
509+
return NameForDiagnostics;
510+
}
511+
497512
bool isOperator() const {
498513
if (getKind() == CodeCompletionResultKind::Declaration) {
499514
switch (getAssociatedDeclKind()) {
@@ -531,11 +546,6 @@ class CodeCompletionResult {
531546
ContextualNotRecommendedReason NotRecommended : 4;
532547
static_assert(int(ContextualNotRecommendedReason::MAX_VALUE) < 1 << 4, "");
533548

534-
CodeCompletionDiagnosticSeverity DiagnosticSeverity : 3;
535-
static_assert(int(CodeCompletionDiagnosticSeverity::MAX_VALUE) < 1 << 3, "");
536-
537-
NullTerminatedStringRef DiagnosticMessage;
538-
539549
/// The number of bytes to the left of the code completion point that
540550
/// should be erased first if this completion string is inserted in the
541551
/// editor buffer.
@@ -556,14 +566,10 @@ class CodeCompletionResult {
556566
SemanticContextKind SemanticContext,
557567
CodeCompletionFlair Flair, uint8_t NumBytesToErase,
558568
CodeCompletionResultTypeRelation TypeDistance,
559-
ContextualNotRecommendedReason NotRecommended,
560-
CodeCompletionDiagnosticSeverity DiagnosticSeverity,
561-
NullTerminatedStringRef DiagnosticMessage)
569+
ContextualNotRecommendedReason NotRecommended)
562570
: ContextFree(ContextFree), SemanticContext(SemanticContext),
563571
Flair(Flair.toRaw()), NotRecommended(NotRecommended),
564-
DiagnosticSeverity(DiagnosticSeverity),
565-
DiagnosticMessage(DiagnosticMessage), NumBytesToErase(NumBytesToErase),
566-
TypeDistance(TypeDistance) {}
572+
NumBytesToErase(NumBytesToErase), TypeDistance(TypeDistance) {}
567573

568574
public:
569575
/// Enrich a \c ContextFreeCodeCompletionResult with the following contextual
@@ -581,9 +587,8 @@ class CodeCompletionResult {
581587
const ExpectedTypeContext *TypeContext,
582588
const DeclContext *DC,
583589
const USRBasedTypeContext *USRTypeContext,
584-
ContextualNotRecommendedReason NotRecommended,
585-
CodeCompletionDiagnosticSeverity DiagnosticSeverity,
586-
NullTerminatedStringRef DiagnosticMessage);
590+
bool CanCurrDeclContextHandleAsync,
591+
ContextualNotRecommendedReason NotRecommended);
587592

588593
const ContextFreeCodeCompletionResult &getContextFreeResult() const {
589594
return ContextFree;
@@ -702,37 +707,23 @@ class CodeCompletionResult {
702707
return getContextFreeResult().getAssociatedUSRs();
703708
}
704709

705-
/// Get the contextual diagnostic severity. This disregards context-free
706-
/// diagnostics.
707-
CodeCompletionDiagnosticSeverity getContextualDiagnosticSeverity() const {
708-
return DiagnosticSeverity;
709-
}
710-
711-
/// Get the contextual diagnostic message. This disregards context-free
712-
/// diagnostics.
713-
NullTerminatedStringRef getContextualDiagnosticMessage() const {
714-
return DiagnosticMessage;
715-
}
716-
717-
/// Return the contextual diagnostic severity if there was a contextual
718-
/// diagnostic. If there is no contextual diagnostic, return the context-free
719-
/// diagnostic severity.
720-
CodeCompletionDiagnosticSeverity getDiagnosticSeverity() const {
721-
if (NotRecommended != ContextualNotRecommendedReason::None) {
722-
return DiagnosticSeverity;
723-
} else {
724-
return getContextFreeResult().getDiagnosticSeverity();
725-
}
726-
}
727-
728-
/// Return the contextual diagnostic message if there was a contextual
729-
/// diagnostic. If there is no contextual diagnostic, return the context-free
730-
/// diagnostic message.
731-
NullTerminatedStringRef getDiagnosticMessage() const {
710+
/// Get the contextual diagnostic severity and message. This disregards
711+
/// context-free diagnostics.
712+
std::pair<CodeCompletionDiagnosticSeverity, NullTerminatedStringRef>
713+
getContextualDiagnosticSeverityAndMessage(SmallVectorImpl<char> &Scratch,
714+
const ASTContext &Ctx) const;
715+
716+
/// Return the contextual diagnostic severity and message if there was a
717+
/// contextual diagnostic. If there is no contextual diagnostic, return the
718+
/// context-free diagnostic severity and message.
719+
std::pair<CodeCompletionDiagnosticSeverity, NullTerminatedStringRef>
720+
getDiagnosticSeverityAndMessage(SmallVectorImpl<char> &Scratch,
721+
const ASTContext &Ctx) const {
732722
if (NotRecommended != ContextualNotRecommendedReason::None) {
733-
return DiagnosticMessage;
723+
return getContextualDiagnosticSeverityAndMessage(Scratch, Ctx);
734724
} else {
735-
return getContextFreeResult().getDiagnosticMessage();
725+
return std::make_pair(getContextFreeResult().getDiagnosticSeverity(),
726+
getContextFreeResult().getDiagnosticMessage());
736727
}
737728
}
738729

include/swift/IDE/CompletionInstance.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ makeCodeCompletionMemoryBuffer(const llvm::MemoryBuffer *origBuf,
4646
/// The result returned via the callback from the perform*Operation methods.
4747
struct CompletionInstanceResult {
4848
/// The compiler instance that is prepared for the second pass.
49-
CompilerInstance &CI;
49+
std::shared_ptr<CompilerInstance> CI;
5050
/// Whether an AST was reused.
5151
bool DidReuseAST;
5252
/// Whether the CompletionInstance found a code completion token in the source
@@ -87,14 +87,14 @@ class CompletionInstance {
8787

8888
std::mutex mtx;
8989

90-
std::unique_ptr<CompilerInstance> CachedCI;
90+
std::shared_ptr<CompilerInstance> CachedCI;
9191
llvm::hash_code CachedArgHash;
9292
llvm::sys::TimePoint<> DependencyCheckedTimestamp;
9393
llvm::StringMap<llvm::hash_code> InMemoryDependencyHash;
9494
unsigned CachedReuseCount = 0;
9595
std::atomic<bool> CachedCIShouldBeInvalidated;
9696

97-
void cacheCompilerInstance(std::unique_ptr<CompilerInstance> CI,
97+
void cacheCompilerInstance(std::shared_ptr<CompilerInstance> CI,
9898
llvm::hash_code ArgsHash);
9999

100100
bool shouldCheckDependencies() const;

include/swift/IDE/CompletionLookup.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,10 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
254254

255255
void setIdealExpectedType(Type Ty) { expectedTypeContext.setIdealType(Ty); }
256256

257+
bool canCurrDeclContextHandleAsync() const {
258+
return CanCurrDeclContextHandleAsync;
259+
}
260+
257261
void setCanCurrDeclContextHandleAsync(bool CanCurrDeclContextHandleAsync) {
258262
this->CanCurrDeclContextHandleAsync = CanCurrDeclContextHandleAsync;
259263
}

include/swift/IDE/SwiftCompletionInfo.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@ namespace swift {
2020
namespace ide {
2121

2222
struct SwiftCompletionInfo {
23-
swift::ASTContext *swiftASTContext = nullptr;
24-
const swift::CompilerInvocation *invocation = nullptr;
23+
std::shared_ptr<CompilerInstance> compilerInstance = nullptr;
2524
CodeCompletionContext *completionContext = nullptr;
2625
};
2726

lib/IDE/CodeCompletion.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1369,7 +1369,8 @@ void swift::ide::deliverCompletionResults(
13691369
/*Sink=*/nullptr);
13701370

13711371
Consumer.handleResultsAndModules(CompletionContext, RequestedModules,
1372-
Lookup.getExpectedTypeContext(), DC);
1372+
Lookup.getExpectedTypeContext(), DC,
1373+
Lookup.canCurrDeclContextHandleAsync());
13731374
}
13741375

13751376
bool CodeCompletionCallbacksImpl::trySolverCompletion(bool MaybeFuncBody) {

lib/IDE/CodeCompletionCache.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,8 @@ CodeCompletionCache::~CodeCompletionCache() {}
103103
///
104104
/// This should be incremented any time we commit a change to the format of the
105105
/// cached results. This isn't expected to change very often.
106-
static constexpr uint32_t onDiskCompletionCacheVersion = 7; // Store whether a type can be used as attribute
106+
static constexpr uint32_t onDiskCompletionCacheVersion =
107+
9; // Store whether a decl is async
107108

108109
/// Deserializes CodeCompletionResults from \p in and stores them in \p V.
109110
/// \see writeCacheModule.
@@ -234,11 +235,13 @@ static bool readCachedModule(llvm::MemoryBuffer *in,
234235
auto diagSeverity =
235236
static_cast<CodeCompletionDiagnosticSeverity>(*cursor++);
236237
auto isSystem = static_cast<bool>(*cursor++);
238+
auto isAsync = static_cast<bool>(*cursor++);
237239
auto chunkIndex = read32le(cursor);
238240
auto moduleIndex = read32le(cursor);
239241
auto briefDocIndex = read32le(cursor);
240242
auto diagMessageIndex = read32le(cursor);
241243
auto filterNameIndex = read32le(cursor);
244+
auto nameForDiagnosticsIndex = read32le(cursor);
242245

243246
auto assocUSRCount = read32le(cursor);
244247
SmallVector<NullTerminatedStringRef, 4> assocUSRs;
@@ -258,13 +261,14 @@ static bool readCachedModule(llvm::MemoryBuffer *in,
258261
auto briefDocComment = getString(briefDocIndex);
259262
auto diagMessage = getString(diagMessageIndex);
260263
auto filterName = getString(filterNameIndex);
264+
auto nameForDiagnostics = getString(nameForDiagnosticsIndex);
261265

262266
ContextFreeCodeCompletionResult *result =
263267
new (*V.Allocator) ContextFreeCodeCompletionResult(
264-
kind, associatedKind, opKind, isSystem, string, moduleName,
268+
kind, associatedKind, opKind, isSystem, isAsync, string, moduleName,
265269
briefDocComment, makeArrayRef(assocUSRs).copy(*V.Allocator),
266270
CodeCompletionResultType(resultTypes), notRecommended, diagSeverity,
267-
diagMessage, filterName);
271+
diagMessage, filterName, nameForDiagnostics);
268272

269273
V.Results.push_back(result);
270274
}
@@ -420,12 +424,14 @@ static void writeCachedModule(llvm::raw_ostream &out,
420424
LE.write(static_cast<uint8_t>(R->getNotRecommendedReason()));
421425
LE.write(static_cast<uint8_t>(R->getDiagnosticSeverity()));
422426
LE.write(static_cast<uint8_t>(R->isSystem()));
427+
LE.write(static_cast<uint8_t>(R->isAsync()));
423428
LE.write(
424429
static_cast<uint32_t>(addCompletionString(R->getCompletionString())));
425430
LE.write(addString(R->getModuleName())); // index into strings
426431
LE.write(addString(R->getBriefDocComment())); // index into strings
427432
LE.write(addString(R->getDiagnosticMessage())); // index into strings
428433
LE.write(addString(R->getFilterName())); // index into strings
434+
LE.write(addString(R->getNameForDiagnostics())); // index into strings
429435

430436
LE.write(static_cast<uint32_t>(R->getAssociatedUSRs().size()));
431437
for (unsigned i = 0; i < R->getAssociatedUSRs().size(); ++i) {

0 commit comments

Comments
 (0)