Skip to content

[CodeCompletion] Remove warning for 'async in non-concurrency context' #73628

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
11 changes: 0 additions & 11 deletions include/swift/AST/DiagnosticsIDE.def
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,6 @@

//===----------------------------------------------------------------------===//

ERROR(ide_async_in_nonasync_context, none,
"async %0 used in a context that does not support concurrency",
(StringRef))

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

WARNING(ide_has_async_alternative, none,
"%0 has an async alternative that should be preferred in an async "
"context", (StringRef))
Expand Down
22 changes: 4 additions & 18 deletions include/swift/IDE/CodeCompletionResult.h
Original file line number Diff line number Diff line change
Expand Up @@ -261,8 +261,6 @@ enum class NotRecommendedReason : uint8_t {
RedundantImportIndirect, // contextual
Deprecated, // context-free
SoftDeprecated, // context-free
InvalidAsyncContext, // contextual
CrossActorReference, // contextual
VariableUsedInOwnDefinition, // contextual
NonAsyncAlternativeUsedInAsyncContext, // contextual

Expand Down Expand Up @@ -300,9 +298,6 @@ enum class ContextualNotRecommendedReason : uint8_t {
None = 0,
RedundantImport,
RedundantImportIndirect,
/// A method that is async is being used in a non-async context.
InvalidAsyncContext,
CrossActorReference,
VariableUsedInOwnDefinition,
/// A method that is sync and has an async alternative is used in an async
/// context.
Expand Down Expand Up @@ -374,7 +369,6 @@ class ContextFreeCodeCompletionResult {
CodeCompletionMacroRoles MacroRoles;

bool IsSystem : 1;
bool IsAsync : 1;
/// Whether the result has been annotated as having an async alternative that
/// should be preferred in async contexts.
bool HasAsyncAlternative : 1;
Expand Down Expand Up @@ -409,7 +403,7 @@ class ContextFreeCodeCompletionResult {
ContextFreeCodeCompletionResult(
CodeCompletionResultKind Kind, uint8_t AssociatedKind,
CodeCompletionOperatorKind KnownOperatorKind,
CodeCompletionMacroRoles MacroRoles, bool IsSystem, bool IsAsync,
CodeCompletionMacroRoles MacroRoles, bool IsSystem,
bool HasAsyncAlternative, CodeCompletionString *CompletionString,
NullTerminatedStringRef ModuleName,
NullTerminatedStringRef BriefDocComment,
Expand All @@ -421,7 +415,7 @@ class ContextFreeCodeCompletionResult {
NullTerminatedStringRef FilterName,
NullTerminatedStringRef NameForDiagnostics)
: Kind(Kind), KnownOperatorKind(KnownOperatorKind),
MacroRoles(MacroRoles), IsSystem(IsSystem), IsAsync(IsAsync),
MacroRoles(MacroRoles), IsSystem(IsSystem),
HasAsyncAlternative(HasAsyncAlternative),
CompletionString(CompletionString), ModuleName(ModuleName),
BriefDocComment(BriefDocComment), AssociatedUSRs(AssociatedUSRs),
Expand All @@ -438,8 +432,6 @@ class ContextFreeCodeCompletionResult {
"Completion item should have diagnostic message iff the diagnostics "
"severity is not none");
assert(CompletionString && "Result should have a completion string");
assert(!(HasAsyncAlternative && IsAsync) &&
"A function shouldn't be both async and have an async alternative");
if (isOperator() && KnownOperatorKind == CodeCompletionOperatorKind::None) {
this->KnownOperatorKind = getCodeCompletionOperatorKind(CompletionString);
}
Expand All @@ -456,7 +448,7 @@ class ContextFreeCodeCompletionResult {
static ContextFreeCodeCompletionResult *createPatternOrBuiltInOperatorResult(
CodeCompletionResultSink &Sink, CodeCompletionResultKind Kind,
CodeCompletionString *CompletionString,
CodeCompletionOperatorKind KnownOperatorKind, bool IsAsync,
CodeCompletionOperatorKind KnownOperatorKin,
NullTerminatedStringRef BriefDocComment,
CodeCompletionResultType ResultType,
ContextFreeNotRecommendedReason NotRecommended,
Expand Down Expand Up @@ -494,7 +486,7 @@ class ContextFreeCodeCompletionResult {
static ContextFreeCodeCompletionResult *
createDeclResult(CodeCompletionResultSink &Sink,
CodeCompletionString *CompletionString,
const Decl *AssociatedDecl, bool IsAsync,
const Decl *AssociatedDecl,
bool HasAsyncAlternative, NullTerminatedStringRef ModuleName,
NullTerminatedStringRef BriefDocComment,
ArrayRef<NullTerminatedStringRef> AssociatedUSRs,
Expand Down Expand Up @@ -533,8 +525,6 @@ class ContextFreeCodeCompletionResult {

bool isSystem() const { return IsSystem; };

bool isAsync() const { return IsAsync; };

bool hasAsyncAlternative() const { return HasAsyncAlternative; };

CodeCompletionString *getCompletionString() const { return CompletionString; }
Expand Down Expand Up @@ -716,12 +706,8 @@ class CodeCompletionResult {
return NotRecommendedReason::RedundantImport;
case ContextualNotRecommendedReason::RedundantImportIndirect:
return NotRecommendedReason::RedundantImportIndirect;
case ContextualNotRecommendedReason::InvalidAsyncContext:
return NotRecommendedReason::InvalidAsyncContext;
case ContextualNotRecommendedReason::NonAsyncAlternativeUsedInAsyncContext:
return NotRecommendedReason::NonAsyncAlternativeUsedInAsyncContext;
case ContextualNotRecommendedReason::CrossActorReference:
return NotRecommendedReason::CrossActorReference;
case ContextualNotRecommendedReason::VariableUsedInOwnDefinition:
return NotRecommendedReason::VariableUsedInOwnDefinition;
}
Expand Down
8 changes: 3 additions & 5 deletions lib/IDE/CodeCompletionCache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ CodeCompletionCache::~CodeCompletionCache() {}
/// This should be incremented any time we commit a change to the format of the
/// cached results. This isn't expected to change very often.
static constexpr uint32_t onDiskCompletionCacheVersion =
11; // Added macro roles
12; // Removed 'IsAsync'.

/// Deserializes CodeCompletionResults from \p in and stores them in \p V.
/// \see writeCacheModule.
Expand Down Expand Up @@ -236,7 +236,6 @@ static bool readCachedModule(llvm::MemoryBuffer *in,
auto diagSeverity =
static_cast<CodeCompletionDiagnosticSeverity>(*cursor++);
auto isSystem = static_cast<bool>(*cursor++);
auto isAsync = static_cast<bool>(*cursor++);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you increment the completion cache version number?

auto hasAsyncAlternative = static_cast<bool>(*cursor++);
auto chunkIndex = read32le(cursor);
auto moduleIndex = read32le(cursor);
Expand Down Expand Up @@ -267,8 +266,8 @@ static bool readCachedModule(llvm::MemoryBuffer *in,

ContextFreeCodeCompletionResult *result =
new (*V.Allocator) ContextFreeCodeCompletionResult(
kind, associatedKind, opKind, roles, isSystem, isAsync,
hasAsyncAlternative, string, moduleName, briefDocComment,
kind, associatedKind, opKind, roles, isSystem,
hasAsyncAlternative, string, moduleName, briefDocComment,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Incorrect formatting?

llvm::ArrayRef(assocUSRs).copy(*V.Allocator),
CodeCompletionResultType(resultTypes), notRecommended, diagSeverity,
diagMessage, filterName, nameForDiagnostics);
Expand Down Expand Up @@ -428,7 +427,6 @@ static void writeCachedModule(llvm::raw_ostream &out,
LE.write(static_cast<uint8_t>(R->getNotRecommendedReason()));
LE.write(static_cast<uint8_t>(R->getDiagnosticSeverity()));
LE.write(static_cast<uint8_t>(R->isSystem()));
LE.write(static_cast<uint8_t>(R->isAsync()));
LE.write(static_cast<uint8_t>(R->hasAsyncAlternative()));
LE.write(
static_cast<uint32_t>(addCompletionString(R->getCompletionString())));
Expand Down
8 changes: 0 additions & 8 deletions lib/IDE/CodeCompletionDiagnostics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -166,14 +166,6 @@ bool swift::ide::getContextualCompletionDiagnostics(
const ASTContext &Ctx) {
CodeCompletionDiagnostics Diag(Ctx);
switch (Reason) {
case ContextualNotRecommendedReason::InvalidAsyncContext:
// FIXME: Could we use 'diag::async_in_nonasync_function'?
return Diag.getDiagnostics(
Severity, Out, diag::ide_async_in_nonasync_context, NameForDiagnostics);
case ContextualNotRecommendedReason::CrossActorReference:
return Diag.getDiagnostics(Severity, Out,
diag::ide_cross_actor_reference_swift5,
NameForDiagnostics);
case ContextualNotRecommendedReason::RedundantImport:
return Diag.getDiagnostics(Severity, Out, diag::ide_redundant_import,
NameForDiagnostics);
Expand Down
17 changes: 6 additions & 11 deletions lib/IDE/CodeCompletionResult.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,7 @@ ContextFreeCodeCompletionResult *
ContextFreeCodeCompletionResult::createPatternOrBuiltInOperatorResult(
CodeCompletionResultSink &Sink, CodeCompletionResultKind Kind,
CodeCompletionString *CompletionString,
CodeCompletionOperatorKind KnownOperatorKind, bool IsAsync,
NullTerminatedStringRef BriefDocComment,
CodeCompletionOperatorKind KnownOperatorKind, NullTerminatedStringRef BriefDocComment,
CodeCompletionResultType ResultType,
ContextFreeNotRecommendedReason NotRecommended,
CodeCompletionDiagnosticSeverity DiagnosticSeverity,
Expand All @@ -138,7 +137,7 @@ ContextFreeCodeCompletionResult::createPatternOrBuiltInOperatorResult(
}
return new (Sink.getAllocator()) ContextFreeCodeCompletionResult(
Kind, /*AssociatedKind=*/0, KnownOperatorKind, /*MacroRoles=*/{},
/*IsSystem=*/false, IsAsync, /*HasAsyncAlternative=*/false,
/*IsSystem=*/false, /*HasAsyncAlternative=*/false,
CompletionString,
/*ModuleName=*/"", BriefDocComment,
/*AssociatedUSRs=*/{}, ResultType, NotRecommended, DiagnosticSeverity,
Expand All @@ -159,8 +158,7 @@ ContextFreeCodeCompletionResult::createKeywordResult(
return new (Sink.getAllocator()) ContextFreeCodeCompletionResult(
CodeCompletionResultKind::Keyword, static_cast<uint8_t>(Kind),
CodeCompletionOperatorKind::None, /*MacroRoles=*/{},
/*IsSystem=*/false, /*IsAsync=*/false,
/*HasAsyncAlternative=*/false, CompletionString,
/*IsSystem=*/false, /*HasAsyncAlternative=*/false, CompletionString,
/*ModuleName=*/"", BriefDocComment,
/*AssociatedUSRs=*/{}, ResultType, ContextFreeNotRecommendedReason::None,
CodeCompletionDiagnosticSeverity::None, /*DiagnosticMessage=*/"",
Expand All @@ -179,7 +177,7 @@ ContextFreeCodeCompletionResult::createLiteralResult(
return new (Sink.getAllocator()) ContextFreeCodeCompletionResult(
CodeCompletionResultKind::Literal, static_cast<uint8_t>(LiteralKind),
CodeCompletionOperatorKind::None, /*MacroRoles=*/{},
/*IsSystem=*/false, /*IsAsync=*/false, /*HasAsyncAlternative=*/false,
/*IsSystem=*/false, /*HasAsyncAlternative=*/false,
CompletionString,
/*ModuleName=*/"",
/*BriefDocComment=*/"",
Expand Down Expand Up @@ -207,7 +205,7 @@ getDeclNameForDiagnostics(const Decl *D, CodeCompletionResultSink &Sink) {
ContextFreeCodeCompletionResult *
ContextFreeCodeCompletionResult::createDeclResult(
CodeCompletionResultSink &Sink, CodeCompletionString *CompletionString,
const Decl *AssociatedDecl, bool IsAsync, bool HasAsyncAlternative,
const Decl *AssociatedDecl, bool HasAsyncAlternative,
NullTerminatedStringRef ModuleName, NullTerminatedStringRef BriefDocComment,
ArrayRef<NullTerminatedStringRef> AssociatedUSRs,
CodeCompletionResultType ResultType,
Expand All @@ -222,7 +220,7 @@ ContextFreeCodeCompletionResult::createDeclResult(
CodeCompletionResultKind::Declaration,
static_cast<uint8_t>(getCodeCompletionDeclKind(AssociatedDecl)),
CodeCompletionOperatorKind::None, getCompletionMacroRoles(AssociatedDecl),
getDeclIsSystem(AssociatedDecl), IsAsync, HasAsyncAlternative,
getDeclIsSystem(AssociatedDecl), HasAsyncAlternative,
CompletionString, ModuleName, BriefDocComment, AssociatedUSRs, ResultType,
NotRecommended, DiagnosticSeverity, DiagnosticMessage,
getCodeCompletionResultFilterName(CompletionString, Sink.getAllocator()),
Expand Down Expand Up @@ -399,9 +397,6 @@ ContextFreeCodeCompletionResult::calculateContextualNotRecommendedReason(
if (explicitReason != ContextualNotRecommendedReason::None) {
return explicitReason;
}
if (IsAsync && !canCurrDeclContextHandleAsync) {
return ContextualNotRecommendedReason::InvalidAsyncContext;
}
Comment on lines -402 to -404
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unless I’m missing something, we can remove IsAsync from ContextFreeCodeCompletionResult now.

if (HasAsyncAlternative && canCurrDeclContextHandleAsync) {
return ContextualNotRecommendedReason::
NonAsyncAlternativeUsedInAsyncContext;
Expand Down
4 changes: 2 additions & 2 deletions lib/IDE/CodeCompletionResultBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ CodeCompletionResult *CodeCompletionResultBuilder::takeResult() {
}

ContextFreeResult = ContextFreeCodeCompletionResult::createDeclResult(
Sink, CCS, AssociatedDecl, IsAsync, HasAsyncAlternative, ModuleName,
Sink, CCS, AssociatedDecl, HasAsyncAlternative, ModuleName,
NullTerminatedStringRef(BriefDocComment, Allocator),
copyAssociatedUSRs(Allocator, AssociatedDecl), ResultType,
ContextFreeNotRecReason, ContextFreeDiagnosticSeverity,
Expand All @@ -145,7 +145,7 @@ CodeCompletionResult *CodeCompletionResultBuilder::takeResult() {
case CodeCompletionResultKind::Pattern:
ContextFreeResult =
ContextFreeCodeCompletionResult::createPatternOrBuiltInOperatorResult(
Sink, Kind, CCS, CodeCompletionOperatorKind::None, IsAsync,
Sink, Kind, CCS, CodeCompletionOperatorKind::None,
NullTerminatedStringRef(BriefDocComment, Allocator), ResultType,
ContextFreeNotRecReason, ContextFreeDiagnosticSeverity,
ContextFreeDiagnosticMessage);
Expand Down
2 changes: 0 additions & 2 deletions lib/IDE/CodeCompletionResultBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ class CodeCompletionResultBuilder {
CodeCompletionFlair Flair;
unsigned NumBytesToErase = 0;
const Decl *AssociatedDecl = nullptr;
bool IsAsync = false;
bool HasAsyncAlternative = false;
std::optional<CodeCompletionLiteralKind> LiteralKind;
CodeCompletionKeywordKind KeywordKind = CodeCompletionKeywordKind::None;
Expand Down Expand Up @@ -116,7 +115,6 @@ class CodeCompletionResultBuilder {

void setAssociatedDecl(const Decl *D);

void setIsAsync(bool IsAsync) { this->IsAsync = IsAsync; }
void setHasAsyncAlternative(bool HasAsyncAlternative) {
this->HasAsyncAlternative = HasAsyncAlternative;
}
Expand Down
36 changes: 0 additions & 36 deletions lib/IDE/CompletionLookup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -807,32 +807,6 @@ void CompletionLookup::analyzeActorIsolation(
case ActorIsolation::NonisolatedUnsafe:
return;
}

// If the reference is 'async', all types must be 'Sendable'.
if (Ctx.LangOpts.StrictConcurrencyLevel >= StrictConcurrency::Complete &&
implicitlyAsync && T) {
if (isa<VarDecl>(VD)) {
if (!T->isSendableType()) {
NotRecommended = ContextualNotRecommendedReason::CrossActorReference;
}
} else {
assert(isa<FuncDecl>(VD) || isa<SubscriptDecl>(VD));
// Check if the result and the param types are all 'Sendable'.
auto *AFT = T->castTo<AnyFunctionType>();
if (!AFT->getResult()->isSendableType()) {
NotRecommended = ContextualNotRecommendedReason::CrossActorReference;
} else {
for (auto &param : AFT->getParams()) {
Type paramType = param.getPlainType();
if (!paramType->isSendableType()) {
NotRecommended =
ContextualNotRecommendedReason::CrossActorReference;
break;
}
}
}
}
}
}

void CompletionLookup::addVarDeclRef(const VarDecl *VD,
Expand Down Expand Up @@ -864,14 +838,9 @@ void CompletionLookup::addVarDeclRef(const VarDecl *VD,
}
bool implicitlyAsync = false;
analyzeActorIsolation(VD, VarType, implicitlyAsync, NotRecommended);
bool explicitlyAsync = false;
if (auto accessor = VD->getEffectfulGetAccessor()) {
explicitlyAsync = accessor->hasAsync();
}
CodeCompletionResultBuilder Builder =
makeResultBuilder(CodeCompletionResultKind::Declaration,
getSemanticContext(VD, Reason, dynamicLookupInfo));
Builder.setIsAsync(explicitlyAsync || implicitlyAsync);
Builder.setCanCurrDeclContextHandleAsync(CanCurrDeclContextHandleAsync);
Builder.setAssociatedDecl(VD);
addLeadingDot(Builder);
Expand Down Expand Up @@ -1272,7 +1241,6 @@ void CompletionLookup::addFunctionCallPattern(
else
addTypeAnnotation(Builder, AFT->getResult(), genericSig);

Builder.setIsAsync(AFT->hasExtInfo() && AFT->isAsync());
Builder.setCanCurrDeclContextHandleAsync(CanCurrDeclContextHandleAsync);
};

Expand Down Expand Up @@ -1394,7 +1362,6 @@ void CompletionLookup::addMethodCall(const FuncDecl *FD,
CodeCompletionResultBuilder Builder =
makeResultBuilder(CodeCompletionResultKind::Declaration,
getSemanticContext(FD, Reason, dynamicLookupInfo));
Builder.setIsAsync(implictlyAsync || (AFT->hasExtInfo() && AFT->isAsync()));
Builder.setHasAsyncAlternative(
FD->getAsyncAlternative() &&
!FD->getAsyncAlternative()->shouldHideFromEditor());
Expand Down Expand Up @@ -1591,8 +1558,6 @@ void CompletionLookup::addConstructorCall(const ConstructorDecl *CD,
addTypeAnnotation(Builder, *Result, CD->getGenericSignatureOfContext());
}

Builder.setIsAsync(ConstructorType->hasExtInfo() &&
ConstructorType->isAsync());
Builder.setCanCurrDeclContextHandleAsync(CanCurrDeclContextHandleAsync);
};

Expand Down Expand Up @@ -1655,7 +1620,6 @@ void CompletionLookup::addSubscriptCall(const SubscriptDecl *SD,
CodeCompletionResultBuilder Builder =
makeResultBuilder(CodeCompletionResultKind::Declaration,
getSemanticContext(SD, Reason, dynamicLookupInfo));
Builder.setIsAsync(implictlyAsync);
Builder.setCanCurrDeclContextHandleAsync(CanCurrDeclContextHandleAsync);
Builder.setAssociatedDecl(SD);

Expand Down
Loading