diff options
author | Marek Polacek <polacek@redhat.com> | 2023-08-25 11:47:54 -0400 |
---|---|---|
committer | Marek Polacek <polacek@redhat.com> | 2023-08-30 14:24:46 -0400 |
commit | 4a92205ef1da044eaf9490e61787836a3abe7d6c (patch) | |
tree | afd2304f639ba3240748dc28e1eef8336dfb43a1 | |
parent | c121afc3b6c96a1f229ba0c4a4de6bd4b6be9a53 (diff) | |
download | gcc-4a92205ef1da044eaf9490e61787836a3abe7d6c.zip gcc-4a92205ef1da044eaf9490e61787836a3abe7d6c.tar.gz gcc-4a92205ef1da044eaf9490e61787836a3abe7d6c.tar.bz2 |
c++: CWG 2359, wrong copy-init with designated init [PR91319]
This CWG clarifies that designated initializer support direct-initialization.
Just be careful what Note 2 in [dcl.init.aggr]/4.2 says: "If the
initialization is by designated-initializer-clause, its form determines
whether copy-initialization or direct-initialization is performed." Hence
this patch sets CONSTRUCTOR_IS_DIRECT_INIT only when we are dealing with
".x{}", but not ".x = {}".
PR c++/91319
gcc/cp/ChangeLog:
* parser.cc (cp_parser_initializer_list): Set CONSTRUCTOR_IS_DIRECT_INIT
when the designated initializer is of the .x{} form.
gcc/testsuite/ChangeLog:
* g++.dg/cpp2a/desig30.C: New test.
-rw-r--r-- | gcc/cp/parser.cc | 9 | ||||
-rw-r--r-- | gcc/testsuite/g++.dg/cpp2a/desig30.C | 22 |
2 files changed, 31 insertions, 0 deletions
diff --git a/gcc/cp/parser.cc b/gcc/cp/parser.cc index a192065..86c0bb1 100644 --- a/gcc/cp/parser.cc +++ b/gcc/cp/parser.cc @@ -25892,6 +25892,7 @@ cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p, tree designator; tree initializer; bool clause_non_constant_p; + bool direct_p = false; location_t loc = cp_lexer_peek_token (parser->lexer)->location; /* Handle the C++20 syntax, '. id ='. */ @@ -25914,6 +25915,8 @@ cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p, if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)) /* Consume the `='. */ cp_lexer_consume_token (parser->lexer); + else + direct_p = true; } /* Also, if the next token is an identifier and the following one is a colon, we are looking at the GNU designated-initializer @@ -25991,6 +25994,12 @@ cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p, if (clause_non_constant_p && non_constant_p) *non_constant_p = true; + if (TREE_CODE (initializer) == CONSTRUCTOR) + /* This uses |= rather than = because C_I_D_I could have been set in + cp_parser_functional_cast so we must be careful not to clear the + flag. */ + CONSTRUCTOR_IS_DIRECT_INIT (initializer) |= direct_p; + /* If we have an ellipsis, this is an initializer pack expansion. */ if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)) diff --git a/gcc/testsuite/g++.dg/cpp2a/desig30.C b/gcc/testsuite/g++.dg/cpp2a/desig30.C new file mode 100644 index 0000000..d9292df --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp2a/desig30.C @@ -0,0 +1,22 @@ +// PR c++/91319 +// { dg-do compile { target c++20 } } + +struct X { + explicit X() { } +}; + +struct Aggr { + X x; +}; + +Aggr +f () +{ + return Aggr{.x{}}; +} + +Aggr +f2 () +{ + return Aggr{.x = {}}; // { dg-error "explicit constructor" } +} |