Skip to content

[CodeCompletion] Avoid crash for not recommended item without a decl #59490

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 1 commit into from
Jun 17, 2022
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
16 changes: 14 additions & 2 deletions lib/IDE/CodeCompletionResultBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,11 @@ CodeCompletionResult *CodeCompletionResultBuilder::takeResult() {
CodeCompletionDiagnosticSeverity::None;
NullTerminatedStringRef ContextFreeDiagnosticMessage;
if (ContextFreeNotRecReason != ContextFreeNotRecommendedReason::None) {
assert(AssociatedDecl != nullptr &&
"There should be no case where ContextFreeNotRecReason != None && "
"AssociatedDecl == nulptr");
// FIXME: We should generate the message lazily.
if (const auto *VD = dyn_cast<ValueDecl>(AssociatedDecl)) {
if (const auto *VD = dyn_cast_or_null<ValueDecl>(AssociatedDecl)) {
CodeCompletionDiagnosticSeverity severity;
SmallString<256> message;
llvm::raw_svector_ostream messageOS(message);
Expand Down Expand Up @@ -169,7 +172,16 @@ CodeCompletionResult *CodeCompletionResultBuilder::takeResult() {
NullTerminatedStringRef ContextualDiagnosticMessage;
if (ContextualNotRecReason != ContextualNotRecommendedReason::None) {
// FIXME: We should generate the message lazily.
if (const auto *VD = dyn_cast<ValueDecl>(AssociatedDecl)) {
//
// NOTE(rdar://95306033): dyn_cast_or_null because 'nullptr' happens in
// cases like:
//
// func test(fn: () async -> Void) { fn(#HERE#) }
//
// In this case, it's 'InvalidAsyncContext' but there's no associated decl
// because the callee is a random expression.
// FIXME: Emit a diagnostic even without an associated decl.
if (const auto *VD = dyn_cast_or_null<ValueDecl>(AssociatedDecl)) {
CodeCompletionDiagnosticSeverity severity;
SmallString<256> message;
llvm::raw_svector_ostream messageOS(message);
Expand Down
8 changes: 8 additions & 0 deletions test/IDE/complete_diagnostics_concurrency.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,11 @@ func testActor(obj: MyActor) async {
// ACTOR-DAG: Decl[InstanceMethod]/CurrNominal/NotRecommended: receiveNonSendable({#arg: MyNonSendable#})[' async'][#Void#]; name=receiveNonSendable(arg:); diagnostics=warning:actor-isolated 'receiveNonSendable(arg:)' should only be referenced from inside the actor{{$}}
// ACTOR: End completions
}

Copy link
Contributor

Choose a reason for hiding this comment

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

Would be good to test the contextual and context-free cases.

Copy link
Member Author

Choose a reason for hiding this comment

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

There's no case where ContextFreeNotRecReason != None && AssociatedDecl == nulptr at this point. Added an assertion to clarify, but still use dyn_cast_or_null just in case.

Copy link
Contributor

Choose a reason for hiding this comment

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

Seems reasonable, no crashes in release but we'll see with assertions enabled 👍

func testClosure(obj: (Int) async -> Void) {
obj(#^CLOSURE_CALL^#)
// FIXME: Emit diagnostics
// CLOSURE_CALL: Begin completions
// CLOSURE_CALL-DAG: Pattern/CurrModule/Flair[ArgLabels]/NotRecommended: ['(']{#Int#}[')'][' async'][#Void#]; name={{$}}
// CLOSURE_CALL: End completions
}