Author: Yanzuo Liu Date: 2025-03-07T07:16:51+01:00 New Revision: d933882ed369a68d118ca661488bb2c89028a2de
URL: https://github.com/llvm/llvm-project/commit/d933882ed369a68d118ca661488bb2c89028a2de DIFF: https://github.com/llvm/llvm-project/commit/d933882ed369a68d118ca661488bb2c89028a2de.diff LOG: [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 https://github.com/llvm/llvm-project/commit/bdb84f374cde7736ca68d5db2c2ecf5468346710 . Drive-by changes: modify comment and diagnostic messages mentioned in CWG2285. Added: Modified: clang/include/clang/Basic/DiagnosticSemaKinds.td clang/lib/Sema/SemaDeclCXX.cpp clang/test/CXX/drs/cwg22xx.cpp clang/test/Parser/cxx1z-decomposition.cpp clang/www/cxx_dr_status.html Removed: ################################################################################ diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td index cf0e9846d4259..1b46920e09619 100644 --- a/clang/include/clang/Basic/DiagnosticSemaKinds.td +++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td @@ -551,7 +551,7 @@ def err_decomp_decl_constraint : Error< def err_decomp_decl_parens : Error< "decomposition declaration cannot be declared with parentheses">; def err_decomp_decl_template : Error< - "decomposition declaration template not supported">; + "decomposition declaration cannot be a template">; def err_decomp_decl_not_alone : Error< "decomposition declaration must be the only declaration in its group">; def err_decomp_decl_requires_init : Error< diff --git a/clang/lib/Sema/SemaDeclCXX.cpp b/clang/lib/Sema/SemaDeclCXX.cpp index a3a028b9485d6..fd5f0443fa894 100644 --- a/clang/lib/Sema/SemaDeclCXX.cpp +++ b/clang/lib/Sema/SemaDeclCXX.cpp @@ -733,8 +733,11 @@ Sema::ActOnDecompositionDeclarator(Scope *S, Declarator &D, } if (!TemplateParamLists.empty()) { - // FIXME: There's no rule against this, but there are also no rules that - // would actually make it usable, so we reject it for now. + // C++17 [temp]/1: + // A template defines a family of class, functions, or variables, or an + // alias for a family of types. + // + // Structured bindings are not included. Diag(TemplateParamLists.front()->getTemplateLoc(), diag::err_decomp_decl_template); return nullptr; diff --git a/clang/test/CXX/drs/cwg22xx.cpp b/clang/test/CXX/drs/cwg22xx.cpp index d93070ef3804d..8c8ad9f7f74ee 100644 --- a/clang/test/CXX/drs/cwg22xx.cpp +++ b/clang/test/CXX/drs/cwg22xx.cpp @@ -1,10 +1,10 @@ // RUN: %clang_cc1 -std=c++98 -triple x86_64-unknown-unknown %s -verify=expected -fexceptions -fcxx-exceptions -pedantic-errors // RUN: %clang_cc1 -std=c++11 -triple x86_64-unknown-unknown %s -verify=expected,since-cxx11 -fexceptions -fcxx-exceptions -pedantic-errors // RUN: %clang_cc1 -std=c++14 -triple x86_64-unknown-unknown %s -verify=expected,since-cxx11 -fexceptions -fcxx-exceptions -pedantic-errors -// RUN: %clang_cc1 -std=c++17 -triple x86_64-unknown-unknown %s -verify=expected,since-cxx11 -fexceptions -fcxx-exceptions -pedantic-errors -// RUN: %clang_cc1 -std=c++20 -triple x86_64-unknown-unknown %s -verify=expected,since-cxx11 -fexceptions -fcxx-exceptions -pedantic-errors -// RUN: %clang_cc1 -std=c++23 -triple x86_64-unknown-unknown %s -verify=expected,since-cxx11 -fexceptions -fcxx-exceptions -pedantic-errors -// RUN: %clang_cc1 -std=c++2c -triple x86_64-unknown-unknown %s -verify=expected,since-cxx11 -fexceptions -fcxx-exceptions -pedantic-errors +// RUN: %clang_cc1 -std=c++17 -triple x86_64-unknown-unknown %s -verify=expected,since-cxx11,since-cxx17 -fexceptions -fcxx-exceptions -pedantic-errors +// RUN: %clang_cc1 -std=c++20 -triple x86_64-unknown-unknown %s -verify=expected,since-cxx11,since-cxx17 -fexceptions -fcxx-exceptions -pedantic-errors +// RUN: %clang_cc1 -std=c++23 -triple x86_64-unknown-unknown %s -verify=expected,since-cxx11,since-cxx17 -fexceptions -fcxx-exceptions -pedantic-errors +// RUN: %clang_cc1 -std=c++2c -triple x86_64-unknown-unknown %s -verify=expected,since-cxx11,since-cxx17 -fexceptions -fcxx-exceptions -pedantic-errors namespace cwg2211 { // cwg2211: 8 @@ -196,6 +196,17 @@ void g() { #endif } // namespace cwg2277 +namespace cwg2285 { // cwg2285: 4 +// Note: Clang 4 implements this DR but it set a wrong value of `__cplusplus` +#if __cplusplus >= 201703L + void test() { + using T = int[1]; + auto [a] = T{a}; + // since-cxx17-error@-1 {{binding 'a' cannot appear in the initializer of its own decomposition declaration}} + } +#endif +} // namespace cwg2285 + namespace cwg2292 { // cwg2292: 9 #if __cplusplus >= 201103L template<typename T> using id = T; diff --git a/clang/test/Parser/cxx1z-decomposition.cpp b/clang/test/Parser/cxx1z-decomposition.cpp index acf3f99069185..f4a4dc5375bdc 100644 --- a/clang/test/Parser/cxx1z-decomposition.cpp +++ b/clang/test/Parser/cxx1z-decomposition.cpp @@ -136,8 +136,8 @@ namespace MultiDeclarator { namespace Template { int n[3]; - // FIXME: There's no actual rule against this... - template<typename T> auto [a, b, c] = n; // expected-error {{decomposition declaration template not supported}} + // Structured binding template is not allowed. + template<typename T> auto [a, b, c] = n; // expected-error {{decomposition declaration cannot be a template}} } namespace Init { diff --git a/clang/www/cxx_dr_status.html b/clang/www/cxx_dr_status.html index 919996bd700b4..b7888d2365acc 100755 --- a/clang/www/cxx_dr_status.html +++ b/clang/www/cxx_dr_status.html @@ -13537,7 +13537,7 @@ <h2 id="cxxdr">C++ defect report implementation status</h2> <td><a href="https://cplusplus.github.io/CWG/issues/2285.html">2285</a></td> <td>CD5</td> <td>Issues with structured bindings</td> - <td class="unknown" align="center">Unknown</td> + <td class="full" align="center">Clang 4</td> </tr> <tr id="2286"> <td><a href="https://cplusplus.github.io/CWG/issues/2286.html">2286</a></td> _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits