Skip to content

Commit d933882

Browse files
authored
[Clang] Add test for CWG2285 "Issues with structured bindings" (#126421)
The resolution of [CWG2285](https://wg21.link/cwg2285) adds the point of declaration of a structured binding, and was implemented in llvm/llvm-project@bdb84f3 . Drive-by changes: modify comment and diagnostic messages mentioned in CWG2285.
1 parent 733ad3f commit d933882

File tree

5 files changed

+24
-10
lines changed

5 files changed

+24
-10
lines changed

clang/include/clang/Basic/DiagnosticSemaKinds.td

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -551,7 +551,7 @@ def err_decomp_decl_constraint : Error<
551551
def err_decomp_decl_parens : Error<
552552
"decomposition declaration cannot be declared with parentheses">;
553553
def err_decomp_decl_template : Error<
554-
"decomposition declaration template not supported">;
554+
"decomposition declaration cannot be a template">;
555555
def err_decomp_decl_not_alone : Error<
556556
"decomposition declaration must be the only declaration in its group">;
557557
def err_decomp_decl_requires_init : Error<

clang/lib/Sema/SemaDeclCXX.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -733,8 +733,11 @@ Sema::ActOnDecompositionDeclarator(Scope *S, Declarator &D,
733733
}
734734

735735
if (!TemplateParamLists.empty()) {
736-
// FIXME: There's no rule against this, but there are also no rules that
737-
// would actually make it usable, so we reject it for now.
736+
// C++17 [temp]/1:
737+
// A template defines a family of class, functions, or variables, or an
738+
// alias for a family of types.
739+
//
740+
// Structured bindings are not included.
738741
Diag(TemplateParamLists.front()->getTemplateLoc(),
739742
diag::err_decomp_decl_template);
740743
return nullptr;

clang/test/CXX/drs/cwg22xx.cpp

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
// RUN: %clang_cc1 -std=c++98 -triple x86_64-unknown-unknown %s -verify=expected -fexceptions -fcxx-exceptions -pedantic-errors
22
// RUN: %clang_cc1 -std=c++11 -triple x86_64-unknown-unknown %s -verify=expected,since-cxx11 -fexceptions -fcxx-exceptions -pedantic-errors
33
// RUN: %clang_cc1 -std=c++14 -triple x86_64-unknown-unknown %s -verify=expected,since-cxx11 -fexceptions -fcxx-exceptions -pedantic-errors
4-
// RUN: %clang_cc1 -std=c++17 -triple x86_64-unknown-unknown %s -verify=expected,since-cxx11 -fexceptions -fcxx-exceptions -pedantic-errors
5-
// RUN: %clang_cc1 -std=c++20 -triple x86_64-unknown-unknown %s -verify=expected,since-cxx11 -fexceptions -fcxx-exceptions -pedantic-errors
6-
// RUN: %clang_cc1 -std=c++23 -triple x86_64-unknown-unknown %s -verify=expected,since-cxx11 -fexceptions -fcxx-exceptions -pedantic-errors
7-
// RUN: %clang_cc1 -std=c++2c -triple x86_64-unknown-unknown %s -verify=expected,since-cxx11 -fexceptions -fcxx-exceptions -pedantic-errors
4+
// RUN: %clang_cc1 -std=c++17 -triple x86_64-unknown-unknown %s -verify=expected,since-cxx11,since-cxx17 -fexceptions -fcxx-exceptions -pedantic-errors
5+
// RUN: %clang_cc1 -std=c++20 -triple x86_64-unknown-unknown %s -verify=expected,since-cxx11,since-cxx17 -fexceptions -fcxx-exceptions -pedantic-errors
6+
// RUN: %clang_cc1 -std=c++23 -triple x86_64-unknown-unknown %s -verify=expected,since-cxx11,since-cxx17 -fexceptions -fcxx-exceptions -pedantic-errors
7+
// RUN: %clang_cc1 -std=c++2c -triple x86_64-unknown-unknown %s -verify=expected,since-cxx11,since-cxx17 -fexceptions -fcxx-exceptions -pedantic-errors
88

99

1010
namespace cwg2211 { // cwg2211: 8
@@ -196,6 +196,17 @@ void g() {
196196
#endif
197197
} // namespace cwg2277
198198

199+
namespace cwg2285 { // cwg2285: 4
200+
// Note: Clang 4 implements this DR but it set a wrong value of `__cplusplus`
201+
#if __cplusplus >= 201703L
202+
void test() {
203+
using T = int[1];
204+
auto [a] = T{a};
205+
// since-cxx17-error@-1 {{binding 'a' cannot appear in the initializer of its own decomposition declaration}}
206+
}
207+
#endif
208+
} // namespace cwg2285
209+
199210
namespace cwg2292 { // cwg2292: 9
200211
#if __cplusplus >= 201103L
201212
template<typename T> using id = T;

clang/test/Parser/cxx1z-decomposition.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,8 @@ namespace MultiDeclarator {
136136

137137
namespace Template {
138138
int n[3];
139-
// FIXME: There's no actual rule against this...
140-
template<typename T> auto [a, b, c] = n; // expected-error {{decomposition declaration template not supported}}
139+
// Structured binding template is not allowed.
140+
template<typename T> auto [a, b, c] = n; // expected-error {{decomposition declaration cannot be a template}}
141141
}
142142

143143
namespace Init {

clang/www/cxx_dr_status.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13537,7 +13537,7 @@ <h2 id="cxxdr">C++ defect report implementation status</h2>
1353713537
<td><a href="https://cplusplus.github.io/CWG/issues/2285.html">2285</a></td>
1353813538
<td>CD5</td>
1353913539
<td>Issues with structured bindings</td>
13540-
<td class="unknown" align="center">Unknown</td>
13540+
<td class="full" align="center">Clang 4</td>
1354113541
</tr>
1354213542
<tr id="2286">
1354313543
<td><a href="https://cplusplus.github.io/CWG/issues/2286.html">2286</a></td>

0 commit comments

Comments
 (0)