aboutsummaryrefslogtreecommitdiff
path: root/gcc
diff options
context:
space:
mode:
authorJason Merrill <jason@redhat.com>2021-05-19 21:13:43 -0400
committerJason Merrill <jason@redhat.com>2021-05-20 16:59:17 -0400
commit885035eacb36b5bf1aa3b0d05f675ab89665d7be (patch)
treeff4cd9403ff68ee97e53d5ac5d48d23502412479 /gcc
parent84fd1b5dff70cd74aee7e8b18f66959d8b8e1ce7 (diff)
downloadgcc-885035eacb36b5bf1aa3b0d05f675ab89665d7be.zip
gcc-885035eacb36b5bf1aa3b0d05f675ab89665d7be.tar.gz
gcc-885035eacb36b5bf1aa3b0d05f675ab89665d7be.tar.bz2
c++: designators in single-element init lists
While looking at PR100489, it occurred to me that places that currently use an initializer-list with a single element to initialize an object of the same type shouldn't do that if the element has a designator. gcc/cp/ChangeLog: * call.c (reference_binding): Check for designator. (implicit_conversion_1, build_special_member_call): Likewise. * decl.c (reshape_init_r): Likewise. * pt.c (do_class_deduction): Likewise. * typeck2.c (digest_init_r): Likewise. gcc/testsuite/ChangeLog: * g++.dg/cpp2a/desig19.C: New test.
Diffstat (limited to 'gcc')
-rw-r--r--gcc/cp/call.c5
-rw-r--r--gcc/cp/decl.c2
-rw-r--r--gcc/cp/pt.c3
-rw-r--r--gcc/cp/typeck2.c1
-rw-r--r--gcc/testsuite/g++.dg/cpp2a/desig19.C33
5 files changed, 42 insertions, 2 deletions
diff --git a/gcc/cp/call.c b/gcc/cp/call.c
index fa770f2..cfccf27 100644
--- a/gcc/cp/call.c
+++ b/gcc/cp/call.c
@@ -1731,7 +1731,8 @@ reference_binding (tree rto, tree rfrom, tree expr, bool c_cast_p, int flags,
because A[] and A[2] are reference-related. But we don't do it
because grok_reference_init has deduced the array size (to 1), and
A[1] and A[2] aren't reference-related. */
- if (CONSTRUCTOR_NELTS (expr) == 1)
+ if (CONSTRUCTOR_NELTS (expr) == 1
+ && !CONSTRUCTOR_IS_DESIGNATED_INIT (expr))
{
tree elt = CONSTRUCTOR_ELT (expr, 0)->value;
if (error_operand_p (elt))
@@ -2095,6 +2096,7 @@ implicit_conversion_1 (tree to, tree from, tree expr, bool c_cast_p,
{
if (BRACE_ENCLOSED_INITIALIZER_P (expr)
&& CONSTRUCTOR_NELTS (expr) == 1
+ && !CONSTRUCTOR_IS_DESIGNATED_INIT (expr)
&& !is_list_ctor (cand->fn))
{
/* "If C is not an initializer-list constructor and the
@@ -10199,6 +10201,7 @@ build_special_member_call (tree instance, tree name, vec<tree, va_gc> **args,
if (BRACE_ENCLOSED_INITIALIZER_P (arg)
&& !TYPE_HAS_LIST_CTOR (class_type)
+ && !CONSTRUCTOR_IS_DESIGNATED_INIT (arg)
&& CONSTRUCTOR_NELTS (arg) == 1)
arg = CONSTRUCTOR_ELT (arg, 0)->value;
diff --git a/gcc/cp/decl.c b/gcc/cp/decl.c
index 6107b55..e7268d5 100644
--- a/gcc/cp/decl.c
+++ b/gcc/cp/decl.c
@@ -6650,6 +6650,8 @@ reshape_init_r (tree type, reshape_iter *d, tree first_initializer_p,
initialized from that element." Even if T is an aggregate. */
if (cxx_dialect >= cxx11 && (CLASS_TYPE_P (type) || VECTOR_TYPE_P (type))
&& first_initializer_p
+ /* But not if it's a designated init. */
+ && !d->cur->index
&& d->end - d->cur == 1
&& reference_related_p (type, TREE_TYPE (init)))
{
diff --git a/gcc/cp/pt.c b/gcc/cp/pt.c
index 3d1787b..99a9ee5 100644
--- a/gcc/cp/pt.c
+++ b/gcc/cp/pt.c
@@ -29326,7 +29326,8 @@ do_class_deduction (tree ptype, tree tmpl, tree init,
{
list_init_p = true;
try_list_ctor = TYPE_HAS_LIST_CTOR (type);
- if (try_list_ctor && CONSTRUCTOR_NELTS (init) == 1)
+ if (try_list_ctor && CONSTRUCTOR_NELTS (init) == 1
+ && !CONSTRUCTOR_IS_DESIGNATED_INIT (init))
{
/* As an exception, the first phase in 16.3.1.7 (considering the
initializer list as a single argument) is omitted if the
diff --git a/gcc/cp/typeck2.c b/gcc/cp/typeck2.c
index 5a7219d..6679e24 100644
--- a/gcc/cp/typeck2.c
+++ b/gcc/cp/typeck2.c
@@ -1183,6 +1183,7 @@ digest_init_r (tree type, tree init, int nested, int flags,
the object is initialized from that element." */
if (cxx_dialect >= cxx11
&& BRACE_ENCLOSED_INITIALIZER_P (stripped_init)
+ && !CONSTRUCTOR_IS_DESIGNATED_INIT (stripped_init)
&& CONSTRUCTOR_NELTS (stripped_init) == 1
&& ((CLASS_TYPE_P (type) && !CLASSTYPE_NON_AGGREGATE (type))
|| VECTOR_TYPE_P (type)))
diff --git a/gcc/testsuite/g++.dg/cpp2a/desig19.C b/gcc/testsuite/g++.dg/cpp2a/desig19.C
new file mode 100644
index 0000000..3321da8
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp2a/desig19.C
@@ -0,0 +1,33 @@
+// { dg-do compile { target c++11 } }
+// { dg-options "" }
+
+struct A
+{
+ int i;
+ constexpr operator int() { return 42; }
+};
+
+#define SA(X) static_assert ((X),#X)
+constexpr A a1 { A() };
+SA(a1.i == 0);
+constexpr A a2 { i: A() };
+SA(a2.i == 42);
+#if __cpp_constexpr >= 201304L
+constexpr int f3 () { A const &r { A() }; return r.i; }
+SA(f3() == 0);
+constexpr int f4 () { A const &r { i: A() }; return r.i; }
+SA(f4() == 42);
+constexpr int f5 () { A ar[1]{{ A() }}; return ar[0].i; }
+SA(f5() == 0);
+constexpr int f5a () { A ar[1]{{ i: A() }}; return ar[0].i; }
+SA(f5a() == 42);
+#if __cpp_constexpr >= 201907L
+constexpr int f6 () { A* p = new A{A()}; int i = p->i; delete p; return i; }
+SA(f6() == 0);
+constexpr int f6a () { A* p = new A{i:A()}; int i = p->i; delete p; return i; }
+SA(f6a() == 42);
+#endif
+#endif
+constexpr int f7 (A a) { return a.i; }
+SA(f7({A()}) == 0);
+SA(f7({i:A()}) == 42);