c++: Fixing the wording of () aggregate-init [PR92812]
authorMarek Polacek <polacek@redhat.com>
Fri, 10 Jul 2020 21:39:54 +0000 (17:39 -0400)
committerMarek Polacek <polacek@redhat.com>
Thu, 13 Aug 2020 00:53:35 +0000 (20:53 -0400)
commit2ec803e768a820c9208fea2e8b144ee359379139
treec4b048520c0be28004ec167a05e26136311aa62d
parentafdd220a0ccf9d5a689b6aceccd8327213a51b9b
c++: Fixing the wording of () aggregate-init [PR92812]

P1975R0 tweaks the static_cast wording: it says that "An expression e can be
explicitly converted to a type T if [...] T is an aggregate type having a first
element x and there is an implicit conversion sequence from e to the type of
x."  This already works for classes, e.g.:

  struct Aggr { int x; int y; };
  Aggr a = static_cast<Aggr>(1);

for which we create TARGET_EXPR <D.2111, {.x=1}>.

The proposal also mentions "If T is ``array of unknown bound of U'',
this direct-initialization defines the type of the expression as U[1]" which
suggest that this should work for arrays (they're aggregates too, after all):

  int (&&r)[3] = static_cast<int[3]>(42);
  int (&&r2)[1] = static_cast<int[]>(42);

So I handled that specifically in build_static_cast_1: wrap the
expression in { } and initialize from that.  For the 'r' case above
this creates TARGET_EXPR <D.2083, {42}>.

There are multiple things in play, as usual, so the tests test brace
elision, narrowing, explicit constructors, and lifetime extension too.
I think it's in line with what we discussed on the core reflector.

gcc/cp/ChangeLog:

PR c++/92812
* typeck.c (build_static_cast_1): Implement P1975R0 by allowing
static_cast to aggregate type.

gcc/testsuite/ChangeLog:

PR c++/92812
* g++.dg/cpp2a/paren-init27.C: New test.
* g++.dg/cpp2a/paren-init28.C: New test.
* g++.dg/cpp2a/paren-init29.C: New test.
* g++.dg/cpp2a/paren-init30.C: New test.
* g++.dg/cpp2a/paren-init31.C: New test.
* g++.dg/cpp2a/paren-init32.C: New test.
gcc/cp/typeck.c
gcc/testsuite/g++.dg/cpp2a/paren-init27.C [new file with mode: 0644]
gcc/testsuite/g++.dg/cpp2a/paren-init28.C [new file with mode: 0644]
gcc/testsuite/g++.dg/cpp2a/paren-init29.C [new file with mode: 0644]
gcc/testsuite/g++.dg/cpp2a/paren-init30.C [new file with mode: 0644]
gcc/testsuite/g++.dg/cpp2a/paren-init31.C [new file with mode: 0644]
gcc/testsuite/g++.dg/cpp2a/paren-init32.C [new file with mode: 0644]