]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c++: Refine enum direct-list-initialization [CWG2374]
authorPatrick Palka <ppalka@redhat.com>
Fri, 23 Apr 2021 12:28:58 +0000 (08:28 -0400)
committerPatrick Palka <ppalka@redhat.com>
Fri, 23 Apr 2021 12:28:58 +0000 (08:28 -0400)
This implements the wording changes of CWG2374, which clarifies the
wording of P0138 to forbid e.g. direct-list-initialization of a scoped
enumeration from a different scoped enumeration.

gcc/cp/ChangeLog:

DR 2374
* decl.c (is_direct_enum_init): Check the implicit
convertibility requirement added by CWG 2374.

gcc/testsuite/ChangeLog:

DR 2374
* g++.dg/cpp1z/direct-enum-init2.C: New test.

gcc/cp/decl.c
gcc/testsuite/g++.dg/cpp1z/direct-enum-init2.C [new file with mode: 0644]

index b81de8ef9349dedc5657c89d0b0c78014c298bc9..60dc2bf182d9ba9208275e17d912d8f673253010 100644 (file)
@@ -6191,7 +6191,13 @@ is_direct_enum_init (tree type, tree init)
       && ENUM_FIXED_UNDERLYING_TYPE_P (type)
       && TREE_CODE (init) == CONSTRUCTOR
       && CONSTRUCTOR_IS_DIRECT_INIT (init)
-      && CONSTRUCTOR_NELTS (init) == 1)
+      && CONSTRUCTOR_NELTS (init) == 1
+      /* DR 2374: The single element needs to be implicitly
+        convertible to the underlying type of the enum.  */
+      && can_convert_arg (ENUM_UNDERLYING_TYPE (type),
+                         TREE_TYPE (CONSTRUCTOR_ELT (init, 0)->value),
+                         CONSTRUCTOR_ELT (init, 0)->value,
+                         LOOKUP_IMPLICIT, tf_none))
     return true;
   return false;
 }
diff --git a/gcc/testsuite/g++.dg/cpp1z/direct-enum-init2.C b/gcc/testsuite/g++.dg/cpp1z/direct-enum-init2.C
new file mode 100644 (file)
index 0000000..5b94a8d
--- /dev/null
@@ -0,0 +1,8 @@
+// DR 2374
+// { dg-do compile { target c++17 } }
+
+enum class Orange;
+enum class Apple;
+
+extern Orange o;
+Apple a{o}; // { dg-error "cannot convert" }