* [Bug c++/92812] Implement P1975R0: Fixing the wording of parenthesized aggregate-initialization
[not found] <bug-92812-4@http.gcc.gnu.org/bugzilla/>
@ 2020-07-10 17:45 ` mpolacek at gcc dot gnu.org
2020-07-10 21:17 ` mpolacek at gcc dot gnu.org
` (7 subsequent siblings)
8 siblings, 0 replies; 9+ messages in thread
From: mpolacek at gcc dot gnu.org @ 2020-07-10 17:45 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=92812
--- Comment #4 from Marek Polacek <mpolacek at gcc dot gnu.org> ---
This already works:
// P1975R0
struct Aggr { int x; int y; };
int
main ()
{
Aggr a = static_cast<Aggr>(1);
if (a.x != 1 || a.y != 0)
__builtin_abort ();
}
but we generate
q.C: In function ‘int main()’:
q.C:8:31: warning: missing initializer for member ‘Aggr::y’
[-Wmissing-field-initializers]
8 | Aggr a = static_cast<Aggr>(1);
|
^ permalink raw reply [flat|nested] 9+ messages in thread
* [Bug c++/92812] Implement P1975R0: Fixing the wording of parenthesized aggregate-initialization
[not found] <bug-92812-4@http.gcc.gnu.org/bugzilla/>
2020-07-10 17:45 ` [Bug c++/92812] Implement P1975R0: Fixing the wording of parenthesized aggregate-initialization mpolacek at gcc dot gnu.org
@ 2020-07-10 21:17 ` mpolacek at gcc dot gnu.org
2020-07-10 21:32 ` mpolacek at gcc dot gnu.org
` (6 subsequent siblings)
8 siblings, 0 replies; 9+ messages in thread
From: mpolacek at gcc dot gnu.org @ 2020-07-10 21:17 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=92812
--- Comment #5 from Marek Polacek <mpolacek at gcc dot gnu.org> ---
I don't know if this is supposed to be valid now too:
int arr[1] = static_cast<int[1]>(42);
or
using T = int[];
int arr[1] = static_cast<T>(42);
but it seems so.
^ permalink raw reply [flat|nested] 9+ messages in thread
* [Bug c++/92812] Implement P1975R0: Fixing the wording of parenthesized aggregate-initialization
[not found] <bug-92812-4@http.gcc.gnu.org/bugzilla/>
2020-07-10 17:45 ` [Bug c++/92812] Implement P1975R0: Fixing the wording of parenthesized aggregate-initialization mpolacek at gcc dot gnu.org
2020-07-10 21:17 ` mpolacek at gcc dot gnu.org
@ 2020-07-10 21:32 ` mpolacek at gcc dot gnu.org
2020-07-21 22:18 ` mpolacek at gcc dot gnu.org
` (5 subsequent siblings)
8 siblings, 0 replies; 9+ messages in thread
From: mpolacek at gcc dot gnu.org @ 2020-07-10 21:32 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=92812
--- Comment #6 from Marek Polacek <mpolacek at gcc dot gnu.org> ---
Actually make that
using T = int[1];
T&& arr = static_cast<T>(42);
^ permalink raw reply [flat|nested] 9+ messages in thread
* [Bug c++/92812] Implement P1975R0: Fixing the wording of parenthesized aggregate-initialization
[not found] <bug-92812-4@http.gcc.gnu.org/bugzilla/>
` (2 preceding siblings ...)
2020-07-10 21:32 ` mpolacek at gcc dot gnu.org
@ 2020-07-21 22:18 ` mpolacek at gcc dot gnu.org
2020-07-27 16:52 ` mpolacek at gcc dot gnu.org
` (4 subsequent siblings)
8 siblings, 0 replies; 9+ messages in thread
From: mpolacek at gcc dot gnu.org @ 2020-07-21 22:18 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=92812
--- Comment #7 from Marek Polacek <mpolacek at gcc dot gnu.org> ---
This should now work too:
int (&&r)[3] = static_cast<int[3]>(42);
int (&&r2)[1] = static_cast<int[]>(42);
and in both cases the reference declarations lifetime-extend the array
temporary.
^ permalink raw reply [flat|nested] 9+ messages in thread
* [Bug c++/92812] Implement P1975R0: Fixing the wording of parenthesized aggregate-initialization
[not found] <bug-92812-4@http.gcc.gnu.org/bugzilla/>
` (3 preceding siblings ...)
2020-07-21 22:18 ` mpolacek at gcc dot gnu.org
@ 2020-07-27 16:52 ` mpolacek at gcc dot gnu.org
2020-08-13 0:54 ` cvs-commit at gcc dot gnu.org
` (3 subsequent siblings)
8 siblings, 0 replies; 9+ messages in thread
From: mpolacek at gcc dot gnu.org @ 2020-07-27 16:52 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=92812
--- Comment #8 from Marek Polacek <mpolacek at gcc dot gnu.org> ---
Another test to consider:
struct S1 {
int i;
int j;
};
struct S2 {
S1 s[4];
};
struct S3 {
S2 s2;
};
void f()
{
auto s3 = static_cast<S3>(1);
}
^ permalink raw reply [flat|nested] 9+ messages in thread
* [Bug c++/92812] Implement P1975R0: Fixing the wording of parenthesized aggregate-initialization
[not found] <bug-92812-4@http.gcc.gnu.org/bugzilla/>
` (4 preceding siblings ...)
2020-07-27 16:52 ` mpolacek at gcc dot gnu.org
@ 2020-08-13 0:54 ` cvs-commit at gcc dot gnu.org
2020-08-13 0:55 ` mpolacek at gcc dot gnu.org
` (2 subsequent siblings)
8 siblings, 0 replies; 9+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2020-08-13 0:54 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=92812
--- Comment #9 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by Marek Polacek <mpolacek@gcc.gnu.org>:
https://gcc.gnu.org/g:2ec803e768a820c9208fea2e8b144ee359379139
commit r11-2681-g2ec803e768a820c9208fea2e8b144ee359379139
Author: Marek Polacek <polacek@redhat.com>
Date: Fri Jul 10 17:39:54 2020 -0400
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.
^ permalink raw reply [flat|nested] 9+ messages in thread
* [Bug c++/92812] Implement P1975R0: Fixing the wording of parenthesized aggregate-initialization
[not found] <bug-92812-4@http.gcc.gnu.org/bugzilla/>
` (5 preceding siblings ...)
2020-08-13 0:54 ` cvs-commit at gcc dot gnu.org
@ 2020-08-13 0:55 ` mpolacek at gcc dot gnu.org
2020-09-03 18:31 ` cvs-commit at gcc dot gnu.org
2020-09-03 18:32 ` mpolacek at gcc dot gnu.org
8 siblings, 0 replies; 9+ messages in thread
From: mpolacek at gcc dot gnu.org @ 2020-08-13 0:55 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=92812
--- Comment #10 from Marek Polacek <mpolacek at gcc dot gnu.org> ---
P1975R0 has now been implemented, but the test in Comment 1 probably still
doesn't work, so leaving this open.
^ permalink raw reply [flat|nested] 9+ messages in thread
* [Bug c++/92812] Implement P1975R0: Fixing the wording of parenthesized aggregate-initialization
[not found] <bug-92812-4@http.gcc.gnu.org/bugzilla/>
` (6 preceding siblings ...)
2020-08-13 0:55 ` mpolacek at gcc dot gnu.org
@ 2020-09-03 18:31 ` cvs-commit at gcc dot gnu.org
2020-09-03 18:32 ` mpolacek at gcc dot gnu.org
8 siblings, 0 replies; 9+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2020-09-03 18:31 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=92812
--- Comment #11 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by Marek Polacek <mpolacek@gcc.gnu.org>:
https://gcc.gnu.org/g:753b4679bc46f6806cf45d9afc3783c6d3b63589
commit r11-2999-g753b4679bc46f6806cf45d9afc3783c6d3b63589
Author: Marek Polacek <polacek@redhat.com>
Date: Wed Aug 26 08:27:33 2020 -0400
c++: Fix P0960 in member init list and array [PR92812]
This patch nails down the remaining P0960 case in PR92812:
struct A {
int ar[2];
A(): ar(1, 2) {} // doesn't work without this patch
};
Note that when the target object is not of array type, this already
works:
struct S { int x, y; };
struct A {
S s;
A(): s(1, 2) { } // OK in C++20
};
because build_new_method_call_1 takes care of the P0960 magic.
It proved to be quite hairy. When the ()-list has more than one
element, we can always create a CONSTRUCTOR, because the code was
previously invalid. But when the ()-list has just one element, it
gets all kinds of difficult. As usual, we have to handle a("foo")
so as not to wrap the STRING_CST in a CONSTRUCTOR. Always turning
x(e) into x{e} would run into trouble as in c++/93790. Another
issue was what to do about x({e}): previously, this would trigger
"list-initializer for non-class type must not be parenthesized".
I figured I'd make this work in C++20, so that given
struct S { int x, y; };
you can do
S a[2];
[...]
A(): a({1, 2}) // initialize a[0] with {1, 2} and a[1] with {}
It also turned out that, as an extension, we support compound literals:
F (): m((S[1]) { 1, 2 })
so this has to keep working as before. Moreover, make sure not to trigger
in compiler-generated code, like =default, where array assignment is
allowed.
I've factored out a function that turns a TREE_LIST into a CONSTRUCTOR
to simplify handling of P0960.
paren-init35.C also tests this with vector types.
gcc/cp/ChangeLog:
PR c++/92812
* cp-tree.h (do_aggregate_paren_init): Declare.
* decl.c (do_aggregate_paren_init): New.
(grok_reference_init): Use it.
(check_initializer): Likewise.
* init.c (perform_member_init): Handle initializing an array from
a ()-list. Use do_aggregate_paren_init.
gcc/testsuite/ChangeLog:
PR c++/92812
* g++.dg/cpp0x/constexpr-array23.C: Adjust dg-error.
* g++.dg/cpp0x/initlist69.C: Likewise.
* g++.dg/diagnostic/mem-init1.C: Likewise.
* g++.dg/init/array28.C: Likewise.
* g++.dg/cpp2a/paren-init33.C: New test.
* g++.dg/cpp2a/paren-init34.C: New test.
* g++.dg/cpp2a/paren-init35.C: New test.
* g++.old-deja/g++.brendan/crash60.C: Adjust dg-error.
* g++.old-deja/g++.law/init10.C: Likewise.
* g++.old-deja/g++.other/array3.C: Likewise.
^ permalink raw reply [flat|nested] 9+ messages in thread
* [Bug c++/92812] Implement P1975R0: Fixing the wording of parenthesized aggregate-initialization
[not found] <bug-92812-4@http.gcc.gnu.org/bugzilla/>
` (7 preceding siblings ...)
2020-09-03 18:31 ` cvs-commit at gcc dot gnu.org
@ 2020-09-03 18:32 ` mpolacek at gcc dot gnu.org
8 siblings, 0 replies; 9+ messages in thread
From: mpolacek at gcc dot gnu.org @ 2020-09-03 18:32 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=92812
Marek Polacek <mpolacek at gcc dot gnu.org> changed:
What |Removed |Added
----------------------------------------------------------------------------
Resolution|--- |FIXED
Status|ASSIGNED |RESOLVED
--- Comment #12 from Marek Polacek <mpolacek at gcc dot gnu.org> ---
Resolved for GCC 11.
^ permalink raw reply [flat|nested] 9+ messages in thread