[PATCH] D126084: [Sema] Reject list-initialization of enumeration types from a brace-init-list containing a single element of a different scoped enumeration type

Akira Hatanaka via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu Jun 2 17:26:24 PDT 2022


This revision was landed with ongoing or failed builds.
This revision was automatically updated to reflect the committed changes.
Closed by commit rG66e08995b0b7: [Sema] Reject list-initialization of enumeration types from a (authored by ahatanak).

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D126084/new/

https://reviews.llvm.org/D126084

Files:
  clang/lib/Sema/SemaInit.cpp
  clang/test/SemaCXX/enum-scoped.cpp


Index: clang/test/SemaCXX/enum-scoped.cpp
===================================================================
--- clang/test/SemaCXX/enum-scoped.cpp
+++ clang/test/SemaCXX/enum-scoped.cpp
@@ -1,4 +1,5 @@
 // RUN: %clang_cc1 -fsyntax-only -pedantic -std=c++11 -verify -triple x86_64-apple-darwin %s
+// RUN: %clang_cc1 -fsyntax-only -pedantic -std=c++17 -verify -triple x86_64-apple-darwin %s
 
 enum class E1 {
   Val1 = 1L
@@ -31,10 +32,22 @@
 int x2 = Val2;
 
 int a1[Val2];
-int a2[E1::Val1]; // expected-error{{size of array has non-integer type}}
+int a2[E1::Val1];
+
+#if __cplusplus >= 201703L
+// expected-error at -3 {{type 'E1' is not implicitly convertible to 'unsigned long'}}
+#else
+// expected-error at -5 {{size of array has non-integer type}}
+#endif
 
 int* p1 = new int[Val2];
-int* p2 = new int[E1::Val1]; // expected-error{{array size expression must have integral or unscoped enumeration type, not 'E1'}}
+int* p2 = new int[E1::Val1];
+
+#if __cplusplus >= 201703L
+// expected-error at -3 {{converting 'E1' to incompatible type 'unsigned long'}}
+#else
+// expected-error at -5 {{array size expression must have integral or unscoped enumeration type, not 'E1'}}
+#endif
 
 enum class E4 {
   e1 = -2147483648, // ok
@@ -317,3 +330,11 @@
   enum C { R, G, B };
   enum B { F = (enum C) -1, T}; // this should compile cleanly, it used to assert.
 };
+
+namespace test12 {
+// Check that clang rejects this code without crashing in c++17.
+enum class A;
+enum class B;
+A a;
+B b{a}; // expected-error {{cannot initialize}}
+}
Index: clang/lib/Sema/SemaInit.cpp
===================================================================
--- clang/lib/Sema/SemaInit.cpp
+++ clang/lib/Sema/SemaInit.cpp
@@ -4503,13 +4503,13 @@
         Kind.getKind() == InitializationKind::IK_DirectList &&
         ET && ET->getDecl()->isFixed() &&
         !S.Context.hasSameUnqualifiedType(E->getType(), DestType) &&
-        (E->getType()->isIntegralOrEnumerationType() ||
+        (E->getType()->isIntegralOrUnscopedEnumerationType() ||
          E->getType()->isFloatingType())) {
       // There are two ways that T(v) can work when T is an enumeration type.
       // If there is either an implicit conversion sequence from v to T or
       // a conversion function that can convert from v to T, then we use that.
-      // Otherwise, if v is of integral, enumeration, or floating-point type,
-      // it is converted to the enumeration type via its underlying type.
+      // Otherwise, if v is of integral, unscoped enumeration, or floating-point
+      // type, it is converted to the enumeration type via its underlying type.
       // There is no overlap possible between these two cases (except when the
       // source value is already of the destination type), and the first
       // case is handled by the general case for single-element lists below.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D126084.433924.patch
Type: text/x-patch
Size: 2854 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220603/68f08820/attachment-0001.bin>


More information about the cfe-commits mailing list