Skip to content

[6.0] [Completion] Complete .isolation for @isolated(any) functions #74873

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions include/swift/IDE/CompletionLookup.h
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,10 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {

void addPrecedenceGroupRef(PrecedenceGroupDecl *PGD);

/// Add a builtin member reference pattern. This is used for members that
/// do not have associated decls, e.g tuple access and '.isolation'.
void addBuiltinMemberRef(StringRef Name, Type TypeAnnotation);

void addEnumElementRef(const EnumElementDecl *EED, DeclVisibilityKind Reason,
DynamicLookupInfo dynamicLookupInfo,
bool HasTypeContext);
Expand Down Expand Up @@ -506,6 +510,9 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {

bool tryTupleExprCompletions(Type ExprType);

/// Try add the completion for '.isolation' for @isolated(any) function types.
void tryFunctionIsolationCompletion(Type ExprType);

bool tryFunctionCallCompletions(
Type ExprType, const ValueDecl *VD,
std::optional<SemanticContextKind> SemanticContext = std::nullopt);
Expand Down
37 changes: 31 additions & 6 deletions lib/IDE/CompletionLookup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1764,6 +1764,15 @@ void CompletionLookup::addPrecedenceGroupRef(PrecedenceGroupDecl *PGD) {
builder.setAssociatedDecl(PGD);
}

void CompletionLookup::addBuiltinMemberRef(StringRef Name,
Type TypeAnnotation) {
CodeCompletionResultBuilder Builder = makeResultBuilder(
CodeCompletionResultKind::Pattern, SemanticContextKind::CurrentNominal);
addLeadingDot(Builder);
Builder.addBaseName(Name);
addTypeAnnotation(Builder, TypeAnnotation);
}

void CompletionLookup::addEnumElementRef(const EnumElementDecl *EED,
DeclVisibilityKind Reason,
DynamicLookupInfo dynamicLookupInfo,
Expand Down Expand Up @@ -2276,25 +2285,34 @@ bool CompletionLookup::tryTupleExprCompletions(Type ExprType) {

unsigned Index = 0;
for (auto TupleElt : TT->getElements()) {
CodeCompletionResultBuilder Builder = makeResultBuilder(
CodeCompletionResultKind::Pattern, SemanticContextKind::CurrentNominal);
addLeadingDot(Builder);
auto Ty = TupleElt.getType();
if (TupleElt.hasName()) {
Builder.addBaseName(TupleElt.getName().str());
addBuiltinMemberRef(TupleElt.getName().str(), Ty);
} else {
llvm::SmallString<4> IndexStr;
{
llvm::raw_svector_ostream OS(IndexStr);
OS << Index;
}
Builder.addBaseName(IndexStr.str());
addBuiltinMemberRef(IndexStr, Ty);
}
addTypeAnnotation(Builder, TupleElt.getType());
++Index;
}
return true;
}

void CompletionLookup::tryFunctionIsolationCompletion(Type ExprType) {
auto *FT = ExprType->getAs<FunctionType>();
if (!FT || !FT->getIsolation().isErased())
return;

// The type of `.isolation` is `(any Actor)?`
auto *actorProto = Ctx.getProtocol(KnownProtocolKind::Actor);
auto memberTy = OptionalType::get(actorProto->getDeclaredExistentialType());

addBuiltinMemberRef(Ctx.Id_isolation.str(), memberTy);
}

bool CompletionLookup::tryFunctionCallCompletions(
Type ExprType, const ValueDecl *VD,
std::optional<SemanticContextKind> SemanticContext) {
Expand Down Expand Up @@ -2372,6 +2390,9 @@ bool CompletionLookup::tryUnwrappedCompletions(Type ExprType, bool isIUO) {
}
if (NumBytesToEraseForOptionalUnwrap <=
CodeCompletionResult::MaxNumBytesToErase) {
// Add '.isolation' to @isolated(any) functions.
tryFunctionIsolationCompletion(Unwrapped);

if (!tryTupleExprCompletions(Unwrapped)) {
lookupVisibleMemberDecls(*this, Unwrapped, DotLoc,
CurrDeclContext,
Expand Down Expand Up @@ -2447,6 +2468,10 @@ void CompletionLookup::getValueExprCompletions(Type ExprType, ValueDecl *VD,
ExprType = OptionalType::get(ExprType);

// Handle special cases

// Add '.isolation' to @isolated(any) functions.
tryFunctionIsolationCompletion(ExprType);

bool isIUO = VD && VD->isImplicitlyUnwrappedOptional();
if (tryFunctionCallCompletions(ExprType, IsDeclUnapplied ? VD : nullptr))
return;
Expand Down
24 changes: 24 additions & 0 deletions test/IDE/complete_function_isolation.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// RUN: %batch-code-completion

// REQUIRES: concurrency

func test1(_ x: () -> Void) {
x.#^NORMAL_FN^#
// NORMAL_FN: Begin completions, 2 items
// NORMAL_FN-DAG: Keyword[self]/CurrNominal: self[#() -> Void#]; name=self
// NORMAL_FN-DAG: Pattern/CurrModule/Flair[ArgLabels]/Erase[1]: ()[#Void#]; name=()
}

func test2(_ x: @isolated(any) () -> Void) {
x.#^ISOLATED_ANY_FN^#
// ISOLATED_ANY_FN: Begin completions, 3 items
// ISOLATED_ANY_FN-DAG: Pattern/CurrNominal: isolation[#(any Actor)?#]; name=isolation
// ISOLATED_ANY_FN-DAG: Keyword[self]/CurrNominal: self[#@isolated(any) () -> Void#]; name=self
// ISOLATED_ANY_FN-DAG: Pattern/CurrModule/Flair[ArgLabels]/Erase[1]: ()[#Void#]; name=()
}

func test3(_ x: (@isolated(any) () -> Void)?) {
x.#^ISOLATED_ANY_OPTIONAL_FN^#
// ISOLATED_ANY_OPTIONAL_FN-DAG: Pattern/CurrNominal/Erase[1]: ?.isolation[#(any Actor)?#]; name=isolation
// ISOLATED_ANY_OPTIONAL_FN-DAG: Keyword[self]/CurrNominal: self[#(@isolated(any) () -> Void)?#]; name=self
}