Bug 92812 - Implement P1975R0: Fixing the wording of parenthesized aggregate-initialization
Summary: Implement P1975R0: Fixing the wording of parenthesized aggregate-initialization
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: unknown
: P3 normal
Target Milestone: ---
Assignee: Marek Polacek
URL:
Keywords:
Depends on:
Blocks: c++20-core
  Show dependency tree / graph
 
Reported: 2019-12-04 23:13 UTC by Marek Polacek
Modified: 2020-09-03 18:32 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2020-01-30 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Marek Polacek 2019-12-04 23:13:01 UTC
As per
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2019/p1975r0.html

This wasn't included in my patch for P0960.
Comment 1 Jason Merrill 2020-02-04 16:47:50 UTC
Another missing piece:

struct A
{ 
  int ar[2];
  A(): ar(1,2) {} // should work, I think
};
Comment 2 Marek Polacek 2020-02-04 19:17:08 UTC
Mine.
Comment 3 GCC Commits 2020-02-04 19:50:16 UTC
The master branch has been updated by Jason Merrill <jason@gcc.gnu.org>:

https://gcc.gnu.org/g:a1c9c9ff06ab15e697d5bac6ea6e5da2df840cf5

commit r10-6436-ga1c9c9ff06ab15e697d5bac6ea6e5da2df840cf5
Author: Jason Merrill <jason@redhat.com>
Date:   Tue Feb 4 14:21:59 2020 -0500

    c++: Fix ({ ... }) array mem-initializer.
    
    Here, we were going down the wrong path in perform_member_init because of
    the incorrect parens around the mem-initializer for the array.  And then
    cxx_eval_vec_init_1 didn't know what to do with a CONSTRUCTOR as the
    initializer.  The latter issue was a straightforward fix, but I also wanted
    to fix us silently accepting the parens, which led to factoring out handling
    of TREE_LIST and flexarrays.  The latter led to adjusting the expected
    behavior on flexary29.C: we should complain about the initializer, but not
    complain about a missing initializer.
    
    As I commented on PR 92812, in this process I noticed that we weren't
    handling C++20 parenthesized aggregate initialization as a mem-initializer.
    So my TREE_LIST handling includes a commented out section that should
    probably be part of a future fix for that issue; with it uncommented we
    continue to crash on the testcase in C++20 mode, but should instead complain
    about the braced-init-list not being a valid initializer for an A.
    
    	PR c++/86917
    	* init.c (perform_member_init): Simplify.
    	* constexpr.c (cx_check_missing_mem_inits): Allow uninitialized
    	flexarray.
    	(cxx_eval_vec_init_1): Handle CONSTRUCTOR.
Comment 4 Marek Polacek 2020-07-10 17:45:39 UTC
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);
      |
Comment 5 Marek Polacek 2020-07-10 21:17:34 UTC
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.
Comment 6 Marek Polacek 2020-07-10 21:32:35 UTC
Actually make that

  using T = int[1];
  T&& arr = static_cast<T>(42);
Comment 7 Marek Polacek 2020-07-21 22:18:42 UTC
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.
Comment 8 Marek Polacek 2020-07-27 16:52:35 UTC
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);
}
Comment 9 GCC Commits 2020-08-13 00:54:46 UTC
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.
Comment 10 Marek Polacek 2020-08-13 00:55:52 UTC
P1975R0 has now been implemented, but the test in Comment 1 probably still doesn't work, so leaving this open.
Comment 11 GCC Commits 2020-09-03 18:31:27 UTC
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.
Comment 12 Marek Polacek 2020-09-03 18:32:47 UTC
Resolved for GCC 11.