[PATCH] D147909: [clang] Implement CWG 2397
Younan Zhang via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Mon Apr 10 01:12:05 PDT 2023
zyounan created this revision.
Herald added a project: All.
zyounan added a comment.
zyounan added reviewers: aaron.ballman, erichkeane, rsmith.
zyounan published this revision for review.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.
Note GCC had implemented such feature since GCC 12 <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=100975>.
This patch implements CWG 2397, allowing array of placeholder type
to be valid.
See also https://wg21.cmeerw.net/cwg/issue2397.
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D147909
Files:
clang/include/clang/Basic/DiagnosticSemaKinds.td
clang/lib/Sema/SemaType.cpp
clang/test/CXX/dcl.decl/dcl.meaning/dcl.array/cwg2397.cpp
clang/test/CXX/dcl.decl/dcl.meaning/dcl.array/p1-cxx0x.cpp
Index: clang/test/CXX/dcl.decl/dcl.meaning/dcl.array/p1-cxx0x.cpp
===================================================================
--- clang/test/CXX/dcl.decl/dcl.meaning/dcl.array/p1-cxx0x.cpp
+++ 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]'}}
}
Index: clang/test/CXX/dcl.decl/dcl.meaning/dcl.array/cwg2397.cpp
===================================================================
--- /dev/null
+++ clang/test/CXX/dcl.decl/dcl.meaning/dcl.array/cwg2397.cpp
@@ -0,0 +1,20 @@
+// RUN: %clang_cc1 -fsyntax-only -verify %s -std=c++11
+
+void foo() {
+ int a[5];
+
+ auto (&b)[5] = a;
+ auto (*c)[5] = &a;
+
+ auto d[] = {0}; // expected-error{{cannot deduce actual type for variable 'd' with type 'auto[]' from initializer list}}
+}
+
+void g() {
+ int a[5];
+ auto (*b)[5] = &a;
+ auto (&c)[5] = a;
+ auto (&&d)[5] = static_cast<int(&&)[5]>(a);
+ static_assert(__is_same(decltype(b), int (*)[5]), "");
+ static_assert(__is_same(decltype(c), int (&)[5]), "");
+ static_assert(__is_same(decltype(d), int (&&)[5]), "");
+}
Index: clang/lib/Sema/SemaType.cpp
===================================================================
--- clang/lib/Sema/SemaType.cpp
+++ clang/lib/Sema/SemaType.cpp
@@ -5130,17 +5130,6 @@
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'.
Index: clang/include/clang/Basic/DiagnosticSemaKinds.td
===================================================================
--- clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -2314,8 +2314,6 @@
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<
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D147909.512081.patch
Type: text/x-patch
Size: 2973 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230410/deb787ee/attachment.bin>
More information about the cfe-commits
mailing list