[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:56 PST 2025


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: cor3ntin (cor3ntin)

<details>
<summary>Changes</summary>

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

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


2 Files Affected:

- (modified) clang/lib/Sema/SemaTemplateInstantiate.cpp (+1-1) 
- (modified) clang/test/SemaCXX/cxx2c-binding-pack.cpp (+16) 


``````````diff
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}}
+}
+
+}

``````````

</details>


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


More information about the cfe-commits mailing list