[Clang][Bytecode] Implement P1061 structured binding pack (#146474)

Other part of this feature was implemented by #121417.
This commit is contained in:
Yanzuo Liu
2025-07-01 19:15:12 +08:00
committed by GitHub
parent fd46e409a9
commit 90e20d4f42
2 changed files with 29 additions and 3 deletions

View File

@@ -5275,7 +5275,7 @@ bool Compiler<Emitter>::visitCompoundStmt(const CompoundStmt *S) {
template <class Emitter> template <class Emitter>
bool Compiler<Emitter>::maybeEmitDeferredVarInit(const VarDecl *VD) { bool Compiler<Emitter>::maybeEmitDeferredVarInit(const VarDecl *VD) {
if (auto *DD = dyn_cast_if_present<DecompositionDecl>(VD)) { if (auto *DD = dyn_cast_if_present<DecompositionDecl>(VD)) {
for (auto *BD : DD->bindings()) for (auto *BD : DD->flat_bindings())
if (auto *KD = BD->getHoldingVar(); KD && !this->visitVarDecl(KD)) if (auto *KD = BD->getHoldingVar(); KD && !this->visitVarDecl(KD))
return false; return false;
} }

View File

@@ -1,10 +1,11 @@
// RUN: %clang_cc1 -fsyntax-only -std=c++26 %s -verify // RUN: %clang_cc1 -fsyntax-only -std=c++26 %s -verify
// RUN: %clang_cc1 -fsyntax-only -std=c++26 %s -verify -fexperimental-new-constant-interpreter
template <typename T> template <typename T>
struct type_ { }; struct type_ { };
template <typename ...T> template <typename ...T>
auto sum(T... t) { return (t + ...); } constexpr auto sum(T... t) { return (t + ...); }
struct my_struct { struct my_struct {
int a; int a;
@@ -17,7 +18,7 @@ struct fake_tuple {
int arr[4] = {1, 2, 3, 6}; int arr[4] = {1, 2, 3, 6};
template <unsigned i> template <unsigned i>
int get() { constexpr int& get() {
return arr[i]; return arr[i];
} }
}; };
@@ -233,3 +234,28 @@ void g() {
} }
} }
namespace constant_interpreter {
using Arr = int[2];
struct Triple { int x, y, z = 3; };
constexpr int ref_to_same_obj(auto&& arg) {
auto& [...xs] = arg;
auto& [...ys] = arg;
(..., (xs += 2));
return sum(ys...);
}
static_assert(ref_to_same_obj(Arr{}) == 4);
static_assert(ref_to_same_obj(fake_tuple{}) == 20);
static_assert(ref_to_same_obj(Triple{}) == 9);
constexpr int copy_obj(auto&& arg) {
auto& [...xs] = arg;
auto [...ys] = arg;
(..., (xs += 2));
return sum(ys...);
}
static_assert(copy_obj(Arr{}) == 0);
static_assert(copy_obj(fake_tuple{}) == 12);
static_assert(copy_obj(Triple{}) == 3);
}