[clang] [Clang][P1061] Fix invalid pack binding crash (PR #135129)
via cfe-commits
cfe-commits at lists.llvm.org
Wed Apr 9 21:52:15 PDT 2025
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: Jason Rice (ricejasonf)
<details>
<summary>Changes</summary>
Fixes #<!-- -->134882
Consider
```
struct foo { char a; int b; };
constexpr foo t{'a', 1};
constexpr auto [...m] = t;
```
Without the `constexpr` qualifier, the decomposition declaration just happens to not crash in the call to `DeclMustBeEmitted` because it returns early because of its "discardable gval linkage". So, the fix is in `flat_bindings` where we cannot assume the pack binding is valid. There is also a fix along the same vein from a suggestion made by @<!-- -->shafik. The tests are still building on my machine, but I thought I would submit this to get eyes on it earlier since it is trivial.
---
Full diff: https://github.com/llvm/llvm-project/pull/135129.diff
3 Files Affected:
- (modified) clang/include/clang/AST/DeclCXX.h (+1-1)
- (modified) clang/lib/Sema/SemaTemplateInstantiateDecl.cpp (+2-3)
- (modified) clang/test/SemaCXX/cxx2c-binding-pack-nontemplate.cpp (+6)
``````````diff
diff --git a/clang/include/clang/AST/DeclCXX.h b/clang/include/clang/AST/DeclCXX.h
index 56cec07ec0293..ba6d87f8158ab 100644
--- a/clang/include/clang/AST/DeclCXX.h
+++ b/clang/include/clang/AST/DeclCXX.h
@@ -4281,7 +4281,7 @@ class DecompositionDecl final
[](BindingDecl *BD) { return BD->isParameterPack(); });
Bindings = Bindings.drop_front(BeforePackBindings.size());
- if (!Bindings.empty()) {
+ if (!Bindings.empty() && Bindings.front()->getBinding()) {
PackBindings = Bindings.front()->getBindingPackDecls();
Bindings = Bindings.drop_front();
}
diff --git a/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp b/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
index e0f7ccc4674d8..4f7410c9bf0d8 100644
--- a/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
+++ b/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
@@ -1597,11 +1597,10 @@ Decl *TemplateDeclInstantiator::VisitDecompositionDecl(DecompositionDecl *D) {
auto *NewDD = cast_if_present<DecompositionDecl>(
VisitVarDecl(D, /*InstantiatingVarTemplate=*/false, &NewBindingArray));
- if (!NewDD || NewDD->isInvalidDecl())
+ if (!NewDD || NewDD->isInvalidDecl()) {
for (auto *NewBD : NewBindings)
NewBD->setInvalidDecl();
-
- if (OldBindingPack) {
+ } else if (OldBindingPack) {
// Mark the bindings in the pack as instantiated.
auto Bindings = NewDD->bindings();
BindingDecl *NewBindingPack = *llvm::find_if(
diff --git a/clang/test/SemaCXX/cxx2c-binding-pack-nontemplate.cpp b/clang/test/SemaCXX/cxx2c-binding-pack-nontemplate.cpp
index ea94757dc66b6..1818dc699c71c 100644
--- a/clang/test/SemaCXX/cxx2c-binding-pack-nontemplate.cpp
+++ b/clang/test/SemaCXX/cxx2c-binding-pack-nontemplate.cpp
@@ -8,4 +8,10 @@ void decompose_array() {
// cxx23-warning at +2 {{structured binding packs are a C++2c extension}}
// nontemplate-error at +1 {{pack declaration outside of template}}
auto [x, ...rest, y] = arr;
+
+ // cxx26-warning at +4 {{structured binding packs are incompatible with C++ standards before C++2c}}
+ // cxx23-warning at +3 {{structured binding packs are a C++2c extension}}
+ // nontemplate-error at +2 {{decomposition declaration cannot be declared 'constexpr'}}
+ // nontemplate-error at +1 {{pack declaration outside of template}}
+ constexpr auto [x, ...rest, y] = arr;
}
``````````
</details>
https://github.com/llvm/llvm-project/pull/135129
More information about the cfe-commits
mailing list