[clang] [Clang] Do not try to transform invalid bindings (PR #125658)

via cfe-commits cfe-commits at lists.llvm.org
Tue Feb 4 01:34:22 PST 2025


https://github.com/cor3ntin created https://github.com/llvm/llvm-project/pull/125658

In the presence of an invalid structured binding decomposition, some binding packs may be invalid and trying to transform them would produce a recovery expression that does not contains a pack, leading to assertions in places where we would expect a pack at that stage.

Fixes #125165

>From b3a85c89c5219356ce089615f45139c0af43e651 Mon Sep 17 00:00:00 2001
From: Corentin Jabot <corentinjabot at gmail.com>
Date: Tue, 4 Feb 2025 10:14:12 +0100
Subject: [PATCH] [Clang] Do not try to transform invalid bindings

In the presence of an invalid structured binding decomposition,
some binding packs may be invalid and trying to transform them
would produce a recovery expression that does not contains a pack,
leading to assertions in places where we would expect a pack
at that stage.

Fixes #125165
---
 clang/lib/Sema/SemaTemplateInstantiate.cpp |  2 +-
 clang/test/SemaCXX/cxx2c-binding-pack.cpp  | 16 ++++++++++++++++
 2 files changed, 17 insertions(+), 1 deletion(-)

diff --git a/clang/lib/Sema/SemaTemplateInstantiate.cpp b/clang/lib/Sema/SemaTemplateInstantiate.cpp
index dc3bfa97eff399..fb3df8fbd39075 100644
--- a/clang/lib/Sema/SemaTemplateInstantiate.cpp
+++ b/clang/lib/Sema/SemaTemplateInstantiate.cpp
@@ -2481,7 +2481,7 @@ TemplateInstantiator::TransformDeclRefExpr(DeclRefExpr *E) {
 
   if (BindingDecl *BD = dyn_cast<BindingDecl>(D); BD && BD->isParameterPack()) {
     BD = cast_or_null<BindingDecl>(TransformDecl(BD->getLocation(), BD));
-    if (!BD)
+    if (!BD || BD->isInvalidDecl())
       return ExprError();
     if (auto *RP =
             dyn_cast_if_present<ResolvedUnexpandedPackExpr>(BD->getBinding()))
diff --git a/clang/test/SemaCXX/cxx2c-binding-pack.cpp b/clang/test/SemaCXX/cxx2c-binding-pack.cpp
index 5ca249f52b3d8e..3340243f7be583 100644
--- a/clang/test/SemaCXX/cxx2c-binding-pack.cpp
+++ b/clang/test/SemaCXX/cxx2c-binding-pack.cpp
@@ -188,3 +188,19 @@ void other_main() {
   static_assert(f<int>() == 2);
 }
 }  // namespace
+
+
+namespace GH125165 {
+
+template <typename = void>
+auto f(auto t) {
+    const auto& [...pack] = t;
+    // expected-error at -1 {{cannot decompose non-class, non-array type 'char const'}}
+    (pack, ...);
+};
+
+void g() {
+    f('x'); // expected-note {{in instantiation}}
+}
+
+}



More information about the cfe-commits mailing list