Skip to content

Commit 6089ab1

Browse files
authored
Merge pull request #59490 from rintaro/ide-completion-rdar95306033
[CodeCompletion] Avoid crash for not recommended item without a decl
2 parents fd52592 + e748ab4 commit 6089ab1

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

lib/IDE/CodeCompletionResultBuilder.cpp

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,11 @@ CodeCompletionResult *CodeCompletionResultBuilder::takeResult() {
8787
CodeCompletionDiagnosticSeverity::None;
8888
NullTerminatedStringRef ContextFreeDiagnosticMessage;
8989
if (ContextFreeNotRecReason != ContextFreeNotRecommendedReason::None) {
90+
assert(AssociatedDecl != nullptr &&
91+
"There should be no case where ContextFreeNotRecReason != None && "
92+
"AssociatedDecl == nulptr");
9093
// FIXME: We should generate the message lazily.
91-
if (const auto *VD = dyn_cast<ValueDecl>(AssociatedDecl)) {
94+
if (const auto *VD = dyn_cast_or_null<ValueDecl>(AssociatedDecl)) {
9295
CodeCompletionDiagnosticSeverity severity;
9396
SmallString<256> message;
9497
llvm::raw_svector_ostream messageOS(message);
@@ -169,7 +172,16 @@ CodeCompletionResult *CodeCompletionResultBuilder::takeResult() {
169172
NullTerminatedStringRef ContextualDiagnosticMessage;
170173
if (ContextualNotRecReason != ContextualNotRecommendedReason::None) {
171174
// FIXME: We should generate the message lazily.
172-
if (const auto *VD = dyn_cast<ValueDecl>(AssociatedDecl)) {
175+
//
176+
// NOTE(rdar://95306033): dyn_cast_or_null because 'nullptr' happens in
177+
// cases like:
178+
//
179+
// func test(fn: () async -> Void) { fn(#HERE#) }
180+
//
181+
// In this case, it's 'InvalidAsyncContext' but there's no associated decl
182+
// because the callee is a random expression.
183+
// FIXME: Emit a diagnostic even without an associated decl.
184+
if (const auto *VD = dyn_cast_or_null<ValueDecl>(AssociatedDecl)) {
173185
CodeCompletionDiagnosticSeverity severity;
174186
SmallString<256> message;
175187
llvm::raw_svector_ostream messageOS(message);

test/IDE/complete_diagnostics_concurrency.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,11 @@ func testActor(obj: MyActor) async {
3030
// 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{{$}}
3131
// ACTOR: End completions
3232
}
33+
34+
func testClosure(obj: (Int) async -> Void) {
35+
obj(#^CLOSURE_CALL^#)
36+
// FIXME: Emit diagnostics
37+
// CLOSURE_CALL: Begin completions
38+
// CLOSURE_CALL-DAG: Pattern/CurrModule/Flair[ArgLabels]/NotRecommended: ['(']{#Int#}[')'][' async'][#Void#]; name={{$}}
39+
// CLOSURE_CALL: End completions
40+
}

0 commit comments

Comments
 (0)