https://github.com/hbatagelo created 
https://github.com/llvm/llvm-project/pull/206373

Fixes #201632.

This patch fixes an assertion failure when code completion is triggered inside 
an ill-formed lambda's trailing requires-clause, as in
```cpp
void f() {
  []() requires x /*invoke completion here*/
```
https://godbolt.org/z/a66s7Y5cx

When `tok::code_completion` is reached, parsing is cut off before the end of 
the declarator. This leaves the lambda's call operator without a type, thus 
triggering the `!isNull() && "Cannot retrieve a NULL type pointer"` assertion 
when `AddOrdinaryNameResults` calls `FunctionDecl::getReturnType()` later.

Fix by guarding the `getReturnType()` call with a null check.

Note: The crash stacktrace for the reproducer above differs slightly from 
#201632. Here, parsing is cut off during the parsing of the constraint 
expression. In #201632, parsing is cut off later during actual template parsing 
in `Parser::ParseTemplateIdAfterTemplateName`. Both leave the lambda without a 
`FunctionType`. Tests for both are included.

>From d2fd1079e5f761079ae897896e62818b3996ae1b Mon Sep 17 00:00:00 2001
From: Harlen Batagelo <[email protected]>
Date: Sun, 28 Jun 2026 17:53:22 -0300
Subject: [PATCH 1/2] Add null check

---
 clang/lib/Sema/SemaCodeComplete.cpp    |  8 +++++---
 clang/test/CodeCompletion/GH201632.cpp | 13 +++++++++++++
 2 files changed, 18 insertions(+), 3 deletions(-)
 create mode 100644 clang/test/CodeCompletion/GH201632.cpp

diff --git a/clang/lib/Sema/SemaCodeComplete.cpp 
b/clang/lib/Sema/SemaCodeComplete.cpp
index bef5eea9ed442..ffa593afdea96 100644
--- a/clang/lib/Sema/SemaCodeComplete.cpp
+++ b/clang/lib/Sema/SemaCodeComplete.cpp
@@ -2625,9 +2625,11 @@ 
AddOrdinaryNameResults(SemaCodeCompletion::ParserCompletionContext CCC,
 
     // "return expression ;" or "return ;", depending on the return type.
     QualType ReturnType;
-    if (const auto *Function = dyn_cast<FunctionDecl>(SemaRef.CurContext))
-      ReturnType = Function->getReturnType();
-    else if (const auto *Method = dyn_cast<ObjCMethodDecl>(SemaRef.CurContext))
+    if (const auto *Function = dyn_cast<FunctionDecl>(SemaRef.CurContext)) {
+      if (!Function->getType().isNull())
+        ReturnType = Function->getReturnType();
+    } else if (const auto *Method =
+                   dyn_cast<ObjCMethodDecl>(SemaRef.CurContext))
       ReturnType = Method->getReturnType();
     else if (SemaRef.getCurBlock() &&
              !SemaRef.getCurBlock()->ReturnType.isNull())
diff --git a/clang/test/CodeCompletion/GH201632.cpp 
b/clang/test/CodeCompletion/GH201632.cpp
new file mode 100644
index 0000000000000..53ff57a652957
--- /dev/null
+++ b/clang/test/CodeCompletion/GH201632.cpp
@@ -0,0 +1,13 @@
+// RUN: not %clang_cc1 -std=c++20 -fsyntax-only -code-completion-at=%s:7:39 %s 
-DTEST_TEMPLATE
+// RUN: not %clang_cc1 -std=c++20 -fsyntax-only -code-completion-at=%s:12:19 
%s -DTEST_CONSTRAINT
+
+#ifdef TEST_TEMPLATE
+void template_cutoff() {
+  [=]() mutable -> decltype(y + x)
+  requires(is_same<decltype((y)), int /*invoke completion here*/ &>
+#endif
+
+#ifdef TEST_CONSTRAINT
+void constraint_cutoff() {
+  []() requires x /*invoke completion here*/
+#endif

>From 089af0ba4f7c7aa6fc1df6adfca2ed4b4a726ad0 Mon Sep 17 00:00:00 2001
From: Harlen Batagelo <[email protected]>
Date: Sun, 28 Jun 2026 17:59:44 -0300
Subject: [PATCH 2/2] Add release note

---
 clang/docs/ReleaseNotes.rst | 1 +
 1 file changed, 1 insertion(+)

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index d6b978ec91659..7f65d15161734 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -1060,6 +1060,7 @@ Code Completion
 
 - Fixed a crash in code completion when using a C-Style cast with a 
parenthesized
   operand in Objective-C++ mode. (#GH180125)
+- Fixed a crash when code completion is triggered inside an ill-formed 
lambda's trailing requires-clause. (#GH201632)
 
 Static Analyzer
 ---------------

_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to