Skip to content

Commit 737d233

Browse files
authored
Merge pull request #37594 from rintaro/5.5-ide-completion-cache-rdar78315441
[5.5][CodeCompletion] Don't check 'InvalidAsyncContext' for imported globals
2 parents 7f09acc + 5b4c796 commit 737d233

File tree

4 files changed

+95
-7
lines changed

4 files changed

+95
-7
lines changed

lib/IDE/CodeCompletion.cpp

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1884,6 +1884,10 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
18841884
bool FoundFunctionsWithoutFirstKeyword = false;
18851885

18861886
private:
1887+
bool isForCaching() const {
1888+
return Kind == LookupKind::ImportFromModule;
1889+
}
1890+
18871891
void foundFunction(const AbstractFunctionDecl *AFD) {
18881892
FoundFunctionCalls = true;
18891893
const DeclName Name = AFD->getName();
@@ -2588,7 +2592,8 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
25882592
}
25892593
bool implicitlyAsync = false;
25902594
analyzeActorIsolation(VD, VarType, implicitlyAsync, NotRecommended);
2591-
if (!NotRecommended && implicitlyAsync && !CanCurrDeclContextHandleAsync) {
2595+
if (!isForCaching() && !NotRecommended && implicitlyAsync &&
2596+
!CanCurrDeclContextHandleAsync) {
25922597
NotRecommended = NotRecommendedReason::InvalidAsyncContext;
25932598
}
25942599

@@ -2930,7 +2935,7 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
29302935
else
29312936
addTypeAnnotation(Builder, AFT->getResult(), genericSig);
29322937

2933-
if (AFT->isAsync() && !CanCurrDeclContextHandleAsync) {
2938+
if (!isForCaching() && AFT->isAsync() && !CanCurrDeclContextHandleAsync) {
29342939
Builder.setNotRecommended(NotRecommendedReason::InvalidAsyncContext);
29352940
}
29362941
};
@@ -3044,7 +3049,8 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
30443049
bool implictlyAsync = false;
30453050
analyzeActorIsolation(FD, AFT, implictlyAsync, NotRecommended);
30463051

3047-
if (!NotRecommended && !IsImplicitlyCurriedInstanceMethod &&
3052+
if (!isForCaching() && !NotRecommended &&
3053+
!IsImplicitlyCurriedInstanceMethod &&
30483054
((AFT && AFT->isAsync()) || implictlyAsync) &&
30493055
!CanCurrDeclContextHandleAsync) {
30503056
NotRecommended = NotRecommendedReason::InvalidAsyncContext;
@@ -3244,7 +3250,8 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
32443250
addTypeAnnotation(Builder, *Result, CD->getGenericSignatureOfContext());
32453251
}
32463252

3247-
if (ConstructorType->isAsync() && !CanCurrDeclContextHandleAsync) {
3253+
if (!isForCaching() && ConstructorType->isAsync() &&
3254+
!CanCurrDeclContextHandleAsync) {
32483255
Builder.setNotRecommended(NotRecommendedReason::InvalidAsyncContext);
32493256
}
32503257
};
@@ -3295,7 +3302,8 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
32953302
bool implictlyAsync = false;
32963303
analyzeActorIsolation(SD, subscriptType, implictlyAsync, NotRecommended);
32973304

3298-
if (!NotRecommended && implictlyAsync && !CanCurrDeclContextHandleAsync) {
3305+
if (!isForCaching() && !NotRecommended && implictlyAsync &&
3306+
!CanCurrDeclContextHandleAsync) {
32993307
NotRecommended = NotRecommendedReason::InvalidAsyncContext;
33003308
}
33013309

@@ -7122,7 +7130,10 @@ void swift::ide::lookupCodeCompletionResultsFromModule(
71227130
CodeCompletionResultSink &targetSink, const ModuleDecl *module,
71237131
ArrayRef<std::string> accessPath, bool needLeadingDot,
71247132
const DeclContext *currDeclContext) {
7125-
CompletionLookup Lookup(targetSink, module->getASTContext(), currDeclContext);
7133+
// Consisitently use the SourceFile as the decl context, to avoid decl
7134+
// context specific behaviors.
7135+
auto *SF = currDeclContext->getParentSourceFile();
7136+
CompletionLookup Lookup(targetSink, module->getASTContext(), SF);
71267137
Lookup.lookupExternalModuleDecls(module, accessPath, needLeadingDot);
71277138
}
71287139

lib/IDE/CodeCompletionCache.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,10 @@ static void writeCachedModule(llvm::raw_ostream &out,
342342
endian::Writer LE(results, little);
343343
for (CodeCompletionResult *R : V.Sink.Results) {
344344
assert(!R->isArgumentLabels() && "Argument labels should not be cached");
345+
assert(R->getNotRecommendedReason() !=
346+
CodeCompletionResult::NotRecommendedReason::InvalidAsyncContext &&
347+
"InvalidAsyncContext is decl context specific, cannot be cached");
348+
345349
// FIXME: compress bitfield
346350
LE.write(static_cast<uint8_t>(R->getKind()));
347351
if (R->getKind() == CodeCompletionResult::Declaration)
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
// REQUIRES: concurrency
2+
3+
// BEGIN MyModule.swift
4+
5+
public actor MyActor {
6+
public init() {}
7+
public func actorMethod() -> Int { 1 }
8+
9+
@available(*, deprecated)
10+
public func deprecatedMethod() {}
11+
}
12+
13+
public func globalAsyncFunc() async -> Int { 1 }
14+
15+
@available(*, deprecated)
16+
public func deprecatedFunc() {}
17+
18+
// BEGIN App.swift
19+
import MyModule
20+
21+
func testSync() -> Int{
22+
#^GLOBAL_IN_SYNC^#
23+
// FIXME: 'globalAsyncFunc()' *should* be "NotRecommended" because it's 'async'
24+
// The curently behavior is due to completion cache. We should remember
25+
// 'async'-ness in the cache. (rdar://78317170)
26+
27+
// GLOBAL_IN_SYNC: Begin completions
28+
// GLOBAL_IN_SYNC-DAG: Decl[FreeFunction]/OtherModule[MyModule]: globalAsyncFunc()[' async'][#Int#];
29+
// GLOBAL_IN_SYNC-DAG: Decl[FreeFunction]/OtherModule[MyModule]/NotRecommended: deprecatedFunc()[#Void#];
30+
// GLOBAL_IN_SYNC-DAG: Decl[Class]/OtherModule[MyModule]: MyActor[#MyActor#];
31+
// GLOBAL_IN_SYNC: End completions
32+
}
33+
func testAsync() async -> Int {
34+
#^GLOBAL_IN_ASYNC^#
35+
// GLOBAL_IN_ASYNC: Begin completions
36+
// GLOBAL_IN_ASYNC-DAG: Decl[FreeFunction]/OtherModule[MyModule]: globalAsyncFunc()[' async'][#Int#];
37+
// GLOBAL_IN_ASYNC-DAG: Decl[FreeFunction]/OtherModule[MyModule]/NotRecommended: deprecatedFunc()[#Void#];
38+
// GLOBAL_IN_ASYNC-DAG: Decl[Class]/OtherModule[MyModule]: MyActor[#MyActor#];
39+
// GLOBAL_IN_ASYNC: End completions
40+
}
41+
func testSyncMember(obj: MyActor) -> Int {
42+
obj.#^MEMBER_IN_SYNC^#
43+
// MEMBER_IN_SYNC: Begin completions, 4 items
44+
// MEMBER_IN_SYNC-DAG: Keyword[self]/CurrNominal: self[#MyActor#];
45+
// MEMBER_IN_SYNC-DAG: Decl[InstanceMethod]/CurrNominal/NotRecommended/TypeRelation[Identical]: actorMethod()[' async'][#Int#];
46+
// MEMBER_IN_SYNC-DAG: Decl[InstanceMethod]/CurrNominal/NotRecommended: deprecatedMethod()[' async'][#Void#];
47+
// MEMBER_IN_SYNC-DAG: Decl[InstanceVar]/CurrNominal: unownedExecutor[#UnownedSerialExecutor#];
48+
// MEMBER_IN_SYNC: End completions
49+
}
50+
51+
func testSyncMember(obj: MyActor) async -> Int {
52+
obj.#^MEMBER_IN_ASYNC^#
53+
// MEMBER_IN_ASYNC: Begin completions, 4 items
54+
// MEMBER_IN_ASYNC-DAG: Keyword[self]/CurrNominal: self[#MyActor#];
55+
// MEMBER_IN_ASYNC-DAG: Decl[InstanceMethod]/CurrNominal/TypeRelation[Identical]: actorMethod()[' async'][#Int#];
56+
// MEMBER_IN_ASYNC-DAG: Decl[InstanceMethod]/CurrNominal/NotRecommended: deprecatedMethod()[' async'][#Void#];
57+
// MEMBER_IN_ASYNC-DAG: Decl[InstanceVar]/CurrNominal: unownedExecutor[#UnownedSerialExecutor#];
58+
// MEMBER_IN_ASYNC: End completions
59+
}
60+
61+
// RUN: %empty-directory(%t)
62+
// RUN: %{python} %utils/split_file.py -o %t %s
63+
64+
// RUN: %empty-directory(%t/Modules)
65+
// RUN: %target-swift-frontend -emit-module -module-name MyModule -o %t/Modules %t/MyModule.swift
66+
67+
// RUN: %empty-directory(%t/output)
68+
// RUN: %empty-directory(%t/ccp)
69+
// RUN: %empty-directory(%t/mcp)
70+
71+
// NOTE: Doing twice is to ensure that the completion cache is used.
72+
// RUN: %target-swift-ide-test -batch-code-completion -source-filename %t/App.swift -filecheck %raw-FileCheck -completion-output-dir %t/output -I %t/Modules -completion-cache-path %t/ccp -module-cache-path %t/mcp
73+
// RUN: %target-swift-ide-test -batch-code-completion -source-filename %t/App.swift -filecheck %raw-FileCheck -completion-output-dir %t/output -I %t/Modules -completion-cache-path %t/ccp -module-cache-path %t/mcp

test/SourceKit/CodeComplete/complete_sort_order.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ func test5() {
7777
// STMT_1-LABEL: Results for filterText: ret [
7878
// STMT_1-NEXT: return
7979
// STMT_1-NEXT: retLocal
80-
// STMT_1-NEXT: repeat
80+
// STMT_1: repeat
8181
// STMT_1: ]
8282
// STMT_1-LABEL: Results for filterText: retur [
8383
// STMT_1-NEXT: return

0 commit comments

Comments
 (0)