Skip to content

Commit 90e20d4

Browse files
authored
[Clang][Bytecode] Implement P1061 structured binding pack (#146474)
Other part of this feature was implemented by #121417.
1 parent fd46e40 commit 90e20d4

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed

clang/lib/AST/ByteCode/Compiler.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5275,7 +5275,7 @@ bool Compiler<Emitter>::visitCompoundStmt(const CompoundStmt *S) {
52755275
template <class Emitter>
52765276
bool Compiler<Emitter>::maybeEmitDeferredVarInit(const VarDecl *VD) {
52775277
if (auto *DD = dyn_cast_if_present<DecompositionDecl>(VD)) {
5278-
for (auto *BD : DD->bindings())
5278+
for (auto *BD : DD->flat_bindings())
52795279
if (auto *KD = BD->getHoldingVar(); KD && !this->visitVarDecl(KD))
52805280
return false;
52815281
}

clang/test/SemaCXX/cxx2c-binding-pack.cpp

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
// RUN: %clang_cc1 -fsyntax-only -std=c++26 %s -verify
2+
// RUN: %clang_cc1 -fsyntax-only -std=c++26 %s -verify -fexperimental-new-constant-interpreter
23

34
template <typename T>
45
struct type_ { };
56

67
template <typename ...T>
7-
auto sum(T... t) { return (t + ...); }
8+
constexpr auto sum(T... t) { return (t + ...); }
89

910
struct my_struct {
1011
int a;
@@ -17,7 +18,7 @@ struct fake_tuple {
1718
int arr[4] = {1, 2, 3, 6};
1819

1920
template <unsigned i>
20-
int get() {
21+
constexpr int& get() {
2122
return arr[i];
2223
}
2324
};
@@ -233,3 +234,28 @@ void g() {
233234
}
234235

235236
}
237+
238+
namespace constant_interpreter {
239+
using Arr = int[2];
240+
struct Triple { int x, y, z = 3; };
241+
242+
constexpr int ref_to_same_obj(auto&& arg) {
243+
auto& [...xs] = arg;
244+
auto& [...ys] = arg;
245+
(..., (xs += 2));
246+
return sum(ys...);
247+
}
248+
static_assert(ref_to_same_obj(Arr{}) == 4);
249+
static_assert(ref_to_same_obj(fake_tuple{}) == 20);
250+
static_assert(ref_to_same_obj(Triple{}) == 9);
251+
252+
constexpr int copy_obj(auto&& arg) {
253+
auto& [...xs] = arg;
254+
auto [...ys] = arg;
255+
(..., (xs += 2));
256+
return sum(ys...);
257+
}
258+
static_assert(copy_obj(Arr{}) == 0);
259+
static_assert(copy_obj(fake_tuple{}) == 12);
260+
static_assert(copy_obj(Triple{}) == 3);
261+
}

0 commit comments

Comments
 (0)