diff options
author | Marek Polacek <polacek@redhat.com> | 2020-07-10 17:39:54 -0400 |
---|---|---|
committer | Giuliano Belinassi <giuliano.belinassi@usp.br> | 2020-08-17 15:08:07 -0300 |
commit | 9231e2be51f5ed01919d414fbd7570f6687fdd62 (patch) | |
tree | 655edc1bb19542668104fd8d673582f01f8d1eae | |
parent | e6873bfa80985f2ff2980be389ec2019f9b77cd8 (diff) | |
download | gcc-9231e2be51f5ed01919d414fbd7570f6687fdd62.zip gcc-9231e2be51f5ed01919d414fbd7570f6687fdd62.tar.gz gcc-9231e2be51f5ed01919d414fbd7570f6687fdd62.tar.bz2 |
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.
-rw-r--r-- | gcc/cp/typeck.c | 14 | ||||
-rw-r--r-- | gcc/testsuite/g++.dg/cpp2a/paren-init27.C | 24 | ||||
-rw-r--r-- | gcc/testsuite/g++.dg/cpp2a/paren-init28.C | 15 | ||||
-rw-r--r-- | gcc/testsuite/g++.dg/cpp2a/paren-init29.C | 15 | ||||
-rw-r--r-- | gcc/testsuite/g++.dg/cpp2a/paren-init30.C | 23 | ||||
-rw-r--r-- | gcc/testsuite/g++.dg/cpp2a/paren-init31.C | 10 | ||||
-rw-r--r-- | gcc/testsuite/g++.dg/cpp2a/paren-init32.C | 21 |
7 files changed, 122 insertions, 0 deletions
diff --git a/gcc/cp/typeck.c b/gcc/cp/typeck.c index a557f34..9166156 100644 --- a/gcc/cp/typeck.c +++ b/gcc/cp/typeck.c @@ -7480,6 +7480,20 @@ build_static_cast_1 (location_t loc, tree type, tree expr, bool c_cast_p, t. */ result = perform_direct_initialization_if_possible (type, expr, c_cast_p, complain); + /* P1975 allows static_cast<Aggr>(42), as well as static_cast<T[5]>(42), + which initialize the first element of the aggregate. We need to handle + the array case specifically. */ + if (result == NULL_TREE + && cxx_dialect >= cxx20 + && TREE_CODE (type) == ARRAY_TYPE) + { + /* Create { EXPR } and perform direct-initialization from it. */ + tree e = build_constructor_single (init_list_type_node, NULL_TREE, expr); + CONSTRUCTOR_IS_DIRECT_INIT (e) = true; + CONSTRUCTOR_IS_PAREN_INIT (e) = true; + result = perform_direct_initialization_if_possible (type, e, c_cast_p, + complain); + } if (result) { if (processing_template_decl) diff --git a/gcc/testsuite/g++.dg/cpp2a/paren-init27.C b/gcc/testsuite/g++.dg/cpp2a/paren-init27.C new file mode 100644 index 0000000..0b8cbe3 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp2a/paren-init27.C @@ -0,0 +1,24 @@ +// PR c++/92812 +// P1975R0 +// { dg-do run { target c++20 } } +// { dg-options "-Wall -Wextra" } + +struct Aggr { int x; int y; }; +struct Base { int i; Base(int i_) : i{i_} { } }; +struct BaseAggr : Base { }; +struct X { }; +struct AggrSDM { static X x; int i; int j; }; + +int +main () +{ + Aggr a = static_cast<Aggr>(42); // { dg-warning "missing initializer" } + if (a.x != 42 || a.y != 0) + __builtin_abort (); + BaseAggr b = static_cast<BaseAggr>(42); + if (b.i != 42) + __builtin_abort (); + AggrSDM s = static_cast<AggrSDM>(42); // { dg-warning "missing initializer" } + if (s.i != 42 || s.j != 0) + __builtin_abort (); +} diff --git a/gcc/testsuite/g++.dg/cpp2a/paren-init28.C b/gcc/testsuite/g++.dg/cpp2a/paren-init28.C new file mode 100644 index 0000000..8c57dc8 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp2a/paren-init28.C @@ -0,0 +1,15 @@ +// PR c++/92812 +// P1975R0 +// { dg-do compile { target c++20 } } + +// In both cases the reference declarations lifetime-extend the array +// temporary. +int (&&r)[3] = static_cast<int[3]>(42); +int (&&r2)[1] = static_cast<int[]>(42); + +// Make sure we've lifetime-extended. +// { dg-final { scan-assembler "_ZGR1r_" } } +// { dg-final { scan-assembler "_ZGR2r2_" } } + +// Narrowing is probably OK here. +int (&&r3)[1] = static_cast<int[1]>(1.3); diff --git a/gcc/testsuite/g++.dg/cpp2a/paren-init29.C b/gcc/testsuite/g++.dg/cpp2a/paren-init29.C new file mode 100644 index 0000000..c39cd8c --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp2a/paren-init29.C @@ -0,0 +1,15 @@ +// PR c++/92812 +// P1975R0 +// { dg-do run { target c++20 } } + +int (&&r)[3] = static_cast<int[3]>(42); +int (&&r2)[1] = static_cast<int[]>(42); + +int +main () +{ + if (r[0] != 42 || r[1] != 0 || r[2] != 0) + __builtin_abort (); + if (r2[0] != 42 || sizeof (r2) != sizeof (int)) + __builtin_abort (); +} diff --git a/gcc/testsuite/g++.dg/cpp2a/paren-init30.C b/gcc/testsuite/g++.dg/cpp2a/paren-init30.C new file mode 100644 index 0000000..6b2e86f --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp2a/paren-init30.C @@ -0,0 +1,23 @@ +// PR c++/92812 +// P1975R0 +// { dg-do compile { target c++20 } } + +struct S1 { + int i; + int j; +}; + +struct S2 { + S1 s[4]; +}; + +struct S3 { + S2 s2; +}; + +void +f () +{ + // Brace elision not allowed. + auto s3 = static_cast<S3>(1); // { dg-error "could not convert" } +} diff --git a/gcc/testsuite/g++.dg/cpp2a/paren-init31.C b/gcc/testsuite/g++.dg/cpp2a/paren-init31.C new file mode 100644 index 0000000..3600c49 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp2a/paren-init31.C @@ -0,0 +1,10 @@ +// PR c++/92812 +// P1975R0 +// { dg-do compile { target c++20 } } +// Test we don't lifetime-extend the int temporary. + +struct A { const int &r; }; +A a(42); +auto a2 = static_cast<A>(42); + +// { dg-final { scan-assembler-not "_ZGR" } } diff --git a/gcc/testsuite/g++.dg/cpp2a/paren-init32.C b/gcc/testsuite/g++.dg/cpp2a/paren-init32.C new file mode 100644 index 0000000..bf7833e --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp2a/paren-init32.C @@ -0,0 +1,21 @@ +// PR c++/92812 +// P1975R0 +// { dg-do compile { target c++20 } } + +struct A { int i; }; +struct A2 { int i; A2(int); }; +struct A3 { int i; explicit A3(int); }; + +struct X { A a; }; +auto x = static_cast<X>(42); // { dg-error "could not convert" } + +struct X2 { A2 a; }; +auto x2 = static_cast<X2>(42); + +struct X3 { A3 a; }; +// Aggregate-initialization copy-initializes, so the explicit ctor +// isn't considered. +auto x3 = static_cast<X3>(42); // { dg-error "could not convert" } + +struct NonAggr { int i; virtual void foo (); }; +auto x4 = static_cast<NonAggr>(42); // { dg-error "no matching" } |