Skip to content

Commit e497a0c

Browse files
zwuisricejasonf
authored andcommitted
[Clang][P1061] Handle explicitly capturing binding packs
1 parent 5302830 commit e497a0c

File tree

3 files changed

+24
-5
lines changed

3 files changed

+24
-5
lines changed

clang/lib/Sema/SemaTemplateInstantiateDecl.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6232,8 +6232,16 @@ NamedDecl *Sema::FindInstantiatedDecl(SourceLocation Loc, NamedDecl *D,
62326232
// declarations to their instantiations.
62336233
if (CurrentInstantiationScope) {
62346234
if (auto Found = CurrentInstantiationScope->findInstantiationOf(D)) {
6235-
if (Decl *FD = Found->dyn_cast<Decl *>())
6235+
if (Decl *FD = Found->dyn_cast<Decl *>()) {
6236+
if (auto *BD = dyn_cast<BindingDecl>(FD);
6237+
BD && BD->isParameterPack() &&
6238+
ArgumentPackSubstitutionIndex != -1) {
6239+
auto *DRE = cast<DeclRefExpr>(
6240+
BD->getBindingPackExprs()[ArgumentPackSubstitutionIndex]);
6241+
return cast<NamedDecl>(DRE->getDecl());
6242+
}
62366243
return cast<NamedDecl>(FD);
6244+
}
62376245

62386246
int PackIdx = ArgumentPackSubstitutionIndex;
62396247
assert(PackIdx != -1 &&

clang/lib/Sema/TreeTransform.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15298,12 +15298,11 @@ TreeTransform<Derived>::TransformLambdaExpr(LambdaExpr *E) {
1529815298
// The transform has determined that we should perform an expansion;
1529915299
// transform and capture each of the arguments.
1530015300
// expansion of the pattern. Do so.
15301-
auto *Pack = cast<VarDecl>(C->getCapturedVar());
15301+
auto *Pack = cast<ValueDecl>(C->getCapturedVar());
1530215302
for (unsigned I = 0; I != *NumExpansions; ++I) {
1530315303
Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(getSema(), I);
15304-
VarDecl *CapturedVar
15305-
= cast_or_null<VarDecl>(getDerived().TransformDecl(C->getLocation(),
15306-
Pack));
15304+
ValueDecl *CapturedVar = cast_if_present<ValueDecl>(
15305+
getDerived().TransformDecl(C->getLocation(), Pack));
1530715306
if (!CapturedVar) {
1530815307
Invalid = true;
1530915308
continue;

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,12 +115,24 @@ void decompose_bit_field() {
115115
int d = x...[3];
116116
}
117117

118+
template <typename T>
119+
void lambda_capture() {
120+
auto [...x] = T{};
121+
[=] { (void)sum(x...); }();
122+
[&] { (void)sum(x...); }();
123+
[x...] { (void)sum(x...); }();
124+
[&x...] { (void)sum(x...); }();
125+
}
126+
118127
int main() {
119128
decompose_array<int>();
120129
decompose_tuple<fake_tuple>();
121130
decompose_struct<my_struct>();
122131
S<1, 2, 3, 4>::N<5, 6>().foo();
123132
decompose_bit_field<bit_fields>();
133+
lambda_capture<int[5]>();
134+
lambda_capture<fake_tuple>();
135+
lambda_capture<my_struct>();
124136
}
125137

126138
// P1061R10 Stuff

0 commit comments

Comments
 (0)