Skip to content

Commit 94ed241

Browse files
committed
[Completion] Complete .isolation for @isolated(any) functions
This was added in SE-0431. rdar://124615036
1 parent 1f83e66 commit 94ed241

File tree

3 files changed

+46
-0
lines changed

3 files changed

+46
-0
lines changed

include/swift/IDE/CompletionLookup.h

+3
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,9 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
510510

511511
bool tryTupleExprCompletions(Type ExprType);
512512

513+
/// Try add the completion for '.isolation' for @isolated(any) function types.
514+
void tryFunctionIsolationCompletion(Type ExprType);
515+
513516
bool tryFunctionCallCompletions(
514517
Type ExprType, const ValueDecl *VD,
515518
std::optional<SemanticContextKind> SemanticContext = std::nullopt);

lib/IDE/CompletionLookup.cpp

+19
Original file line numberDiff line numberDiff line change
@@ -2302,6 +2302,18 @@ bool CompletionLookup::tryTupleExprCompletions(Type ExprType) {
23022302
return true;
23032303
}
23042304

2305+
void CompletionLookup::tryFunctionIsolationCompletion(Type ExprType) {
2306+
auto *FT = ExprType->getAs<FunctionType>();
2307+
if (!FT || !FT->getIsolation().isErased())
2308+
return;
2309+
2310+
// The type of `.isolation` is `(any Actor)?`
2311+
auto *actorProto = Ctx.getProtocol(KnownProtocolKind::Actor);
2312+
auto memberTy = OptionalType::get(actorProto->getDeclaredExistentialType());
2313+
2314+
addBuiltinMemberRef(Ctx.Id_isolation.str(), memberTy);
2315+
}
2316+
23052317
bool CompletionLookup::tryFunctionCallCompletions(
23062318
Type ExprType, const ValueDecl *VD,
23072319
std::optional<SemanticContextKind> SemanticContext) {
@@ -2379,6 +2391,9 @@ bool CompletionLookup::tryUnwrappedCompletions(Type ExprType, bool isIUO) {
23792391
}
23802392
if (NumBytesToEraseForOptionalUnwrap <=
23812393
CodeCompletionResult::MaxNumBytesToErase) {
2394+
// Add '.isolation' to @isolated(any) functions.
2395+
tryFunctionIsolationCompletion(Unwrapped);
2396+
23822397
if (!tryTupleExprCompletions(Unwrapped)) {
23832398
lookupVisibleMemberDecls(*this, Unwrapped, DotLoc,
23842399
CurrDeclContext,
@@ -2454,6 +2469,10 @@ void CompletionLookup::getValueExprCompletions(Type ExprType, ValueDecl *VD,
24542469
ExprType = OptionalType::get(ExprType);
24552470

24562471
// Handle special cases
2472+
2473+
// Add '.isolation' to @isolated(any) functions.
2474+
tryFunctionIsolationCompletion(ExprType);
2475+
24572476
bool isIUO = VD && VD->isImplicitlyUnwrappedOptional();
24582477
if (tryFunctionCallCompletions(ExprType, IsDeclUnapplied ? VD : nullptr))
24592478
return;
+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// RUN: %batch-code-completion
2+
3+
// REQUIRES: concurrency
4+
5+
func test1(_ x: () -> Void) {
6+
x.#^NORMAL_FN^#
7+
// NORMAL_FN: Begin completions, 2 items
8+
// NORMAL_FN-DAG: Keyword[self]/CurrNominal: self[#() -> Void#]; name=self
9+
// NORMAL_FN-DAG: Pattern/CurrModule/Flair[ArgLabels]/Erase[1]: ()[#Void#]; name=()
10+
}
11+
12+
func test2(_ x: @isolated(any) () -> Void) {
13+
x.#^ISOLATED_ANY_FN^#
14+
// ISOLATED_ANY_FN: Begin completions, 3 items
15+
// ISOLATED_ANY_FN-DAG: Pattern/CurrNominal: isolation[#(any Actor)?#]; name=isolation
16+
// ISOLATED_ANY_FN-DAG: Keyword[self]/CurrNominal: self[#@isolated(any) () -> Void#]; name=self
17+
// ISOLATED_ANY_FN-DAG: Pattern/CurrModule/Flair[ArgLabels]/Erase[1]: ()[#Void#]; name=()
18+
}
19+
20+
func test3(_ x: (@isolated(any) () -> Void)?) {
21+
x.#^ISOLATED_ANY_OPTIONAL_FN^#
22+
// ISOLATED_ANY_OPTIONAL_FN-DAG: Pattern/CurrNominal/Erase[1]: ?.isolation[#(any Actor)?#]; name=isolation
23+
// ISOLATED_ANY_OPTIONAL_FN-DAG: Keyword[self]/CurrNominal: self[#(@isolated(any) () -> Void)?#]; name=self
24+
}

0 commit comments

Comments
 (0)