[clang] [Clang][Sema] fix crash in codegen stage when an lambda expression declared in an unevaluated context (PR #80802)

via cfe-commits cfe-commits at lists.llvm.org
Mon Feb 5 22:17:08 PST 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: Qizhi Hu (jcsxky)

<details>
<summary>Changes</summary>

Try to fix [issue](https://github.com/llvm/llvm-project/issues/76674)
When transform a lambda expression which is declared in an unevaluated context, `isInstantiationDependentType()` and `isVariablyModifiedType()` both return false and lead to skip transforming the lambda expression. On the other hand, `AlreadyTransformed` also skip transform in this case. Add the condition to check whether it's in decltype makes it work.

---
Full diff: https://github.com/llvm/llvm-project/pull/80802.diff


3 Files Affected:

- (modified) clang/docs/ReleaseNotes.rst (+2) 
- (modified) clang/lib/Sema/SemaTemplateInstantiate.cpp (+4-3) 
- (added) clang/test/SemaTemplate/PR76674.cpp (+11) 


``````````diff
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 4d57ea4fd55b8..7ad575e4f57fa 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -204,6 +204,8 @@ Bug Fixes to C++ Support
   parameter where we did an incorrect specialization of the initialization of
   the default parameter.
   Fixes (`#68490 <https://github.com/llvm/llvm-project/issues/68490>`_)
+- Fix a crash in codegen when lambdas declared in an unevaluated context.
+  Fixes (`#76674 <https://github.com/llvm/llvm-project/issues/76674>`_)
 
 Bug Fixes to AST Handling
 ^^^^^^^^^^^^^^^^^^^^^^^^^
diff --git a/clang/lib/Sema/SemaTemplateInstantiate.cpp b/clang/lib/Sema/SemaTemplateInstantiate.cpp
index 6d59180bc446d..383163ea969a9 100644
--- a/clang/lib/Sema/SemaTemplateInstantiate.cpp
+++ b/clang/lib/Sema/SemaTemplateInstantiate.cpp
@@ -1613,8 +1613,8 @@ namespace {
 bool TemplateInstantiator::AlreadyTransformed(QualType T) {
   if (T.isNull())
     return true;
-
-  if (T->isInstantiationDependentType() || T->isVariablyModifiedType())
+  if (T->isInstantiationDependentType() || T->isVariablyModifiedType() ||
+      (SemaRef.getLangOpts().CPlusPlus20 && T->isDecltypeType()))
     return false;
 
   getSema().MarkDeclarationsReferencedInType(Loc, T);
@@ -2685,7 +2685,8 @@ QualType Sema::SubstType(QualType T,
 
   // If T is not a dependent type or a variably-modified type, there
   // is nothing to do.
-  if (!T->isInstantiationDependentType() && !T->isVariablyModifiedType())
+  if (!T->isInstantiationDependentType() && !T->isVariablyModifiedType() &&
+      (getLangOpts().CPlusPlus20 && !T->isDecltypeType()))
     return T;
 
   TemplateInstantiator Instantiator(*this, TemplateArgs, Loc, Entity);
diff --git a/clang/test/SemaTemplate/PR76674.cpp b/clang/test/SemaTemplate/PR76674.cpp
new file mode 100644
index 0000000000000..50e9053e41e0f
--- /dev/null
+++ b/clang/test/SemaTemplate/PR76674.cpp
@@ -0,0 +1,11 @@
+// RUN: %clang_cc1 -c -std=c++20 -verify=cxx20 -o /dev/null %s
+// expected-no-diagnostics
+
+template <class>
+struct A {
+    template <class U>
+    using Func = decltype([] {return U{};});
+};
+
+A<int>::Func<int> f{};
+int i{f()};

``````````

</details>


https://github.com/llvm/llvm-project/pull/80802


More information about the cfe-commits mailing list