Author: Younan Zhang Date: 2023-04-12T22:30:23+08:00 New Revision: d9826433f31cd081d0229d47d7982b5bf3c0055f
URL: https://github.com/llvm/llvm-project/commit/d9826433f31cd081d0229d47d7982b5bf3c0055f DIFF: https://github.com/llvm/llvm-project/commit/d9826433f31cd081d0229d47d7982b5bf3c0055f.diff LOG: [clang] Implement CWG 2397 This patch implements CWG 2397, allowing array of placeholder type to be valid. See also https://wg21.cmeerw.net/cwg/issue2397. Reviewed By: aaron.ballman, #clang-language-wg Differential Revision: https://reviews.llvm.org/D147909 Added: clang/test/CXX/dcl.decl/dcl.meaning/dcl.array/cwg2397.cpp Modified: clang/docs/ReleaseNotes.rst clang/include/clang/Basic/DiagnosticSemaKinds.td clang/lib/Sema/SemaType.cpp clang/test/CXX/dcl.decl/dcl.meaning/dcl.array/p1-cxx0x.cpp clang/test/CXX/drs/dr23xx.cpp clang/www/cxx_dr_status.html Removed: ################################################################################ diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst index dd66c71122999..e08aecfcc881f 100644 --- a/clang/docs/ReleaseNotes.rst +++ b/clang/docs/ReleaseNotes.rst @@ -103,6 +103,8 @@ C++2b Feature Support Resolutions to C++ Defect Reports ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +- Implemented `DR2397 <https://wg21.link/CWG2397>`_ which allows ``auto`` specifier for pointers + and reference to arrays. C Language Changes ------------------ diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td index cd5930d385e69..18a0154b00411 100644 --- a/clang/include/clang/Basic/DiagnosticSemaKinds.td +++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td @@ -2314,8 +2314,6 @@ def err_auto_variable_cannot_appear_in_own_initializer : Error< def err_binding_cannot_appear_in_own_initializer : Error< "binding %0 cannot appear in the initializer of its own " "decomposition declaration">; -def err_illegal_decl_array_of_auto : Error< - "'%0' declared as array of %1">; def err_new_array_of_auto : Error< "cannot allocate array of 'auto'">; def err_auto_not_allowed : Error< diff --git a/clang/lib/Sema/SemaType.cpp b/clang/lib/Sema/SemaType.cpp index f557ff7b203fa..99e7ccfd88351 100644 --- a/clang/lib/Sema/SemaType.cpp +++ b/clang/lib/Sema/SemaType.cpp @@ -5130,17 +5130,6 @@ static TypeSourceInfo *GetFullTypeForDeclarator(TypeProcessingState &state, D.setInvalidType(true); } } - const AutoType *AT = T->getContainedAutoType(); - // Allow arrays of auto if we are a generic lambda parameter. - // i.e. [](auto (&array)[5]) { return array[0]; }; OK - if (AT && D.getContext() != DeclaratorContext::LambdaExprParameter) { - // We've already diagnosed this for decltype(auto). - if (!AT->isDecltypeAuto()) - S.Diag(DeclType.Loc, diag::err_illegal_decl_array_of_auto) - << getPrintableNameForEntity(Name) << T; - T = QualType(); - break; - } // Array parameters can be marked nullable as well, although it's not // necessary if they're marked 'static'. diff --git a/clang/test/CXX/dcl.decl/dcl.meaning/dcl.array/cwg2397.cpp b/clang/test/CXX/dcl.decl/dcl.meaning/dcl.array/cwg2397.cpp new file mode 100644 index 0000000000000..f280d02a97513 --- /dev/null +++ b/clang/test/CXX/dcl.decl/dcl.meaning/dcl.array/cwg2397.cpp @@ -0,0 +1,25 @@ +// RUN: %clang_cc1 -fsyntax-only -verify %s -std=c++11 +// RUN: %clang_cc1 -fsyntax-only -verify %s -std=c++14 + +void f() { + int a[5]; + auto (*b)[5] = &a; + auto (&c)[5] = a; + auto (&&d)[5] = static_cast<int(&&)[5]>(a); + auto e[] = {0}; // expected-error{{cannot deduce actual type for variable 'e' with type 'auto[]' from initializer list}} + static_assert(__is_same(decltype(b), int (*)[5]), ""); + static_assert(__is_same(decltype(c), int (&)[5]), ""); + static_assert(__is_same(decltype(d), int (&&)[5]), ""); +} + +#if __cplusplus >= 201402L + +constexpr int g() { + int a[] = {1, 2, 3}; + auto (&b)[3] = a; + return b[1]; +} + +static_assert(g() == 2, ""); + +#endif diff --git a/clang/test/CXX/dcl.decl/dcl.meaning/dcl.array/p1-cxx0x.cpp b/clang/test/CXX/dcl.decl/dcl.meaning/dcl.array/p1-cxx0x.cpp index 102746c3db292..a3482d58afa1f 100644 --- a/clang/test/CXX/dcl.decl/dcl.meaning/dcl.array/p1-cxx0x.cpp +++ b/clang/test/CXX/dcl.decl/dcl.meaning/dcl.array/p1-cxx0x.cpp @@ -2,6 +2,6 @@ void f() { int b[5]; - auto a[5] = b; // expected-error{{'a' declared as array of 'auto'}} - auto *c[5] = b; // expected-error{{'c' declared as array of 'auto *'}} + auto a[5] = b; // expected-error{{variable 'a' with type 'auto[5]' has incompatible initializer of type 'int[5]'}} + auto *c[5] = b; // expected-error{{variable 'c' with type 'auto *[5]' has incompatible initializer of type 'int[5]'}} } diff --git a/clang/test/CXX/drs/dr23xx.cpp b/clang/test/CXX/drs/dr23xx.cpp index 4f16746aad8be..51879e913c6ba 100644 --- a/clang/test/CXX/drs/dr23xx.cpp +++ b/clang/test/CXX/drs/dr23xx.cpp @@ -213,3 +213,14 @@ namespace dr2396 { // dr2396: no // void g(A a) { a.operator decltype(B()) B::*(); } // void g2(A a) { a.operator B decltype(B())::*(); } } + +#if __cplusplus >= 201103L +namespace dr2397 { // dr2397: 17 + void foo() { + int a[5]; + + auto (&b)[5] = a; + auto (*c)[5] = &a; + } +} // namespace dr2397 +#endif diff --git a/clang/www/cxx_dr_status.html b/clang/www/cxx_dr_status.html index bac8a52c2d5bd..5ee388f51f3ae 100755 --- a/clang/www/cxx_dr_status.html +++ b/clang/www/cxx_dr_status.html @@ -14189,7 +14189,7 @@ <h2 id="cxxdr">C++ defect report implementation status</h2> <td><a href="https://wg21.link/cwg2397">2397</a></td> <td>CD6</td> <td><TT>auto</TT> specifier for pointers and references to arrays</td> - <td class="none" align="center">Unknown</td> + <td class="unreleased" align="center">Clang 17</td> </tr> <tr class="open" id="2398"> <td><a href="https://wg21.link/cwg2398">2398</a></td> _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits