[clang] 868007a - [clang][Sema] Avoid non-empty unexpanded pack assertion for FunctionParmPackExpr (#69224)

via cfe-commits cfe-commits at lists.llvm.org
Sun Nov 12 20:44:21 PST 2023


Author: Younan Zhang
Date: 2023-11-13T12:44:17+08:00
New Revision: 868007a03c74f7c718e0e74c8bdac677eb3f09ba

URL: https://github.com/llvm/llvm-project/commit/868007a03c74f7c718e0e74c8bdac677eb3f09ba
DIFF: https://github.com/llvm/llvm-project/commit/868007a03c74f7c718e0e74c8bdac677eb3f09ba.diff

LOG: [clang][Sema] Avoid non-empty unexpanded pack assertion for FunctionParmPackExpr (#69224)

Closes https://github.com/llvm/llvm-project/issues/61460.

We have FunctionParmPackExpr that serves as the unexpanded expression
but from which the visitor collects none, which may lead to assertion
failure during the template instantiation.

Added: 
    clang/test/SemaCXX/pr61460.cpp

Modified: 
    clang/docs/ReleaseNotes.rst
    clang/lib/Sema/SemaTemplateVariadic.cpp

Removed: 
    


################################################################################
diff  --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 7a131cb520aa600..74358219ba9fb22 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -553,6 +553,8 @@ Bug Fixes in This Version
   Fixes (`#67687 <https://github.com/llvm/llvm-project/issues/67687>`_)
 - Fix crash from constexpr evaluator evaluating uninitialized arrays as rvalue.
   Fixes (`#67317 <https://github.com/llvm/llvm-project/issues/67317>`_)
+- Fixed an issue that a benign assertion might hit when instantiating a pack expansion
+  inside a lambda. (`#61460 <https://github.com/llvm/llvm-project/issues/61460>`_)
 
 Bug Fixes to Compiler Builtins
 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

diff  --git a/clang/lib/Sema/SemaTemplateVariadic.cpp b/clang/lib/Sema/SemaTemplateVariadic.cpp
index dfcc78dafdc4c31..0f4e9e7f94c81e9 100644
--- a/clang/lib/Sema/SemaTemplateVariadic.cpp
+++ b/clang/lib/Sema/SemaTemplateVariadic.cpp
@@ -402,6 +402,13 @@ bool Sema::DiagnoseUnexpandedParameterPack(Expr *E,
   if (!E->containsUnexpandedParameterPack())
     return false;
 
+  // CollectUnexpandedParameterPacksVisitor does not expect to see a
+  // FunctionParmPackExpr, but diagnosing unexpected parameter packs may still
+  // see such an expression in a lambda body.
+  // We'll bail out early in this case to avoid triggering an assertion.
+  if (isa<FunctionParmPackExpr>(E) && getEnclosingLambda())
+    return false;
+
   SmallVector<UnexpandedParameterPack, 2> Unexpanded;
   CollectUnexpandedParameterPacksVisitor(Unexpanded).TraverseStmt(E);
   assert(!Unexpanded.empty() && "Unable to find unexpanded parameter packs");

diff  --git a/clang/test/SemaCXX/pr61460.cpp b/clang/test/SemaCXX/pr61460.cpp
new file mode 100644
index 000000000000000..471b1b39d23c2b7
--- /dev/null
+++ b/clang/test/SemaCXX/pr61460.cpp
@@ -0,0 +1,13 @@
+// RUN: %clang_cc1 -std=c++17 %s -fsyntax-only -verify
+
+template <typename... Ts> void g(Ts... p1s) {
+  (void)[&](auto... p2s) { ([&] { p1s; p2s; }, ...); };
+}
+
+void f1() {
+  g();
+}
+
+template <typename... Ts> void g2(Ts... p1s) {
+  (void)[&](auto... p2s) { [&] { p1s; p2s; }; }; // expected-error {{expression contains unexpanded parameter pack 'p2s'}}
+}


        


More information about the cfe-commits mailing list