* [PATCH] c++: fix ICE with designated initializer [PR110114] @ 2023-07-19 18:00 Marek Polacek 2023-07-19 18:32 ` Patrick Palka 0 siblings, 1 reply; 6+ messages in thread From: Marek Polacek @ 2023-07-19 18:00 UTC (permalink / raw) To: GCC Patches, Jason Merrill; +Cc: Patrick Palka Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk? -- >8 -- r13-1227 added an assert checking that the index in a CONSTRUCTOR is a FIELD_DECL. That's a reasonable assumption but in this case we never called reshape_init due to the type being incomplete, and so the index remained an identifier node: get_class_binding never got around to looking up the FIELD_DECL. We can avoid the crash by returning early in build_aggr_conv; we'd return NULL anyway due to: if (i < CONSTRUCTOR_NELTS (ctor)) return NULL; PR c++/110114 gcc/cp/ChangeLog: * call.cc (build_aggr_conv): Return early if the type isn't complete. gcc/testsuite/ChangeLog: * g++.dg/cpp2a/desig28.C: New test. * g++.dg/cpp2a/desig29.C: New test. --- gcc/cp/call.cc | 5 +++++ gcc/testsuite/g++.dg/cpp2a/desig28.C | 17 +++++++++++++++++ gcc/testsuite/g++.dg/cpp2a/desig29.C | 10 ++++++++++ 3 files changed, 32 insertions(+) create mode 100644 gcc/testsuite/g++.dg/cpp2a/desig28.C create mode 100644 gcc/testsuite/g++.dg/cpp2a/desig29.C diff --git a/gcc/cp/call.cc b/gcc/cp/call.cc index b55230d98aa..0af20a81717 100644 --- a/gcc/cp/call.cc +++ b/gcc/cp/call.cc @@ -986,6 +986,11 @@ build_aggr_conv (tree type, tree ctor, int flags, tsubst_flags_t complain) tree empty_ctor = NULL_TREE; hash_set<tree, true> pset; + /* We've called complete_type on TYPE before calling this function, but + perhaps it wasn't successful. */ + if (!COMPLETE_TYPE_P (type)) + return nullptr; + /* We already called reshape_init in implicit_conversion, but it might not have done anything in the case of parenthesized aggr init. */ diff --git a/gcc/testsuite/g++.dg/cpp2a/desig28.C b/gcc/testsuite/g++.dg/cpp2a/desig28.C new file mode 100644 index 00000000000..b63265fea51 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp2a/desig28.C @@ -0,0 +1,17 @@ +// PR c++/110114 +// { dg-do compile { target c++20 } } + +struct A { + int a,b; +}; + +struct B; + +void foo(const A &) {} +void foo(const B &) {} + +int +main () +{ + foo({.a=0}); +} diff --git a/gcc/testsuite/g++.dg/cpp2a/desig29.C b/gcc/testsuite/g++.dg/cpp2a/desig29.C new file mode 100644 index 00000000000..bd1a82b041d --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp2a/desig29.C @@ -0,0 +1,10 @@ +// PR c++/110114 +// { dg-do compile { target c++20 } } + +struct B; + +void foo(const B &) {} + +int main() { + foo({.a=0}); // { dg-error "invalid" } +} base-commit: 2971ff7b1d564ac04b537d907c70e6093af70832 -- 2.41.0 ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH] c++: fix ICE with designated initializer [PR110114] 2023-07-19 18:00 [PATCH] c++: fix ICE with designated initializer [PR110114] Marek Polacek @ 2023-07-19 18:32 ` Patrick Palka 2023-07-19 18:38 ` Marek Polacek 0 siblings, 1 reply; 6+ messages in thread From: Patrick Palka @ 2023-07-19 18:32 UTC (permalink / raw) To: Marek Polacek; +Cc: GCC Patches, Jason Merrill, Patrick Palka On Wed, 19 Jul 2023, Marek Polacek wrote: > Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk? LGTM. It might be preferable to check COMPLETE_TYPE_P in the caller instead, so that we avoid inspecting CLASSTYPE_NON_AGGREGATE on an incomplete class type, and so that the caller doesn't "commit" to building an aggregate conversion. > > -- >8 -- > > r13-1227 added an assert checking that the index in a CONSTRUCTOR > is a FIELD_DECL. That's a reasonable assumption but in this case > we never called reshape_init due to the type being incomplete, and > so the index remained an identifier node: get_class_binding never > got around to looking up the FIELD_DECL. > > We can avoid the crash by returning early in build_aggr_conv; we'd > return NULL anyway due to: > > if (i < CONSTRUCTOR_NELTS (ctor)) > return NULL; > > PR c++/110114 > > gcc/cp/ChangeLog: > > * call.cc (build_aggr_conv): Return early if the type isn't > complete. > > gcc/testsuite/ChangeLog: > > * g++.dg/cpp2a/desig28.C: New test. > * g++.dg/cpp2a/desig29.C: New test. > --- > gcc/cp/call.cc | 5 +++++ > gcc/testsuite/g++.dg/cpp2a/desig28.C | 17 +++++++++++++++++ > gcc/testsuite/g++.dg/cpp2a/desig29.C | 10 ++++++++++ > 3 files changed, 32 insertions(+) > create mode 100644 gcc/testsuite/g++.dg/cpp2a/desig28.C > create mode 100644 gcc/testsuite/g++.dg/cpp2a/desig29.C > > diff --git a/gcc/cp/call.cc b/gcc/cp/call.cc > index b55230d98aa..0af20a81717 100644 > --- a/gcc/cp/call.cc > +++ b/gcc/cp/call.cc > @@ -986,6 +986,11 @@ build_aggr_conv (tree type, tree ctor, int flags, tsubst_flags_t complain) > tree empty_ctor = NULL_TREE; > hash_set<tree, true> pset; > > + /* We've called complete_type on TYPE before calling this function, but > + perhaps it wasn't successful. */ > + if (!COMPLETE_TYPE_P (type)) > + return nullptr; > + > /* We already called reshape_init in implicit_conversion, but it might not > have done anything in the case of parenthesized aggr init. */ > > diff --git a/gcc/testsuite/g++.dg/cpp2a/desig28.C b/gcc/testsuite/g++.dg/cpp2a/desig28.C > new file mode 100644 > index 00000000000..b63265fea51 > --- /dev/null > +++ b/gcc/testsuite/g++.dg/cpp2a/desig28.C > @@ -0,0 +1,17 @@ > +// PR c++/110114 > +// { dg-do compile { target c++20 } } > + > +struct A { > + int a,b; > +}; > + > +struct B; > + > +void foo(const A &) {} > +void foo(const B &) {} > + > +int > +main () > +{ > + foo({.a=0}); > +} > diff --git a/gcc/testsuite/g++.dg/cpp2a/desig29.C b/gcc/testsuite/g++.dg/cpp2a/desig29.C > new file mode 100644 > index 00000000000..bd1a82b041d > --- /dev/null > +++ b/gcc/testsuite/g++.dg/cpp2a/desig29.C > @@ -0,0 +1,10 @@ > +// PR c++/110114 > +// { dg-do compile { target c++20 } } > + > +struct B; > + > +void foo(const B &) {} > + > +int main() { > + foo({.a=0}); // { dg-error "invalid" } > +} > > base-commit: 2971ff7b1d564ac04b537d907c70e6093af70832 > -- > 2.41.0 > > ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH] c++: fix ICE with designated initializer [PR110114] 2023-07-19 18:32 ` Patrick Palka @ 2023-07-19 18:38 ` Marek Polacek 2023-07-19 19:24 ` Jason Merrill 0 siblings, 1 reply; 6+ messages in thread From: Marek Polacek @ 2023-07-19 18:38 UTC (permalink / raw) To: Patrick Palka; +Cc: GCC Patches, Jason Merrill On Wed, Jul 19, 2023 at 02:32:15PM -0400, Patrick Palka wrote: > On Wed, 19 Jul 2023, Marek Polacek wrote: > > > Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk? > > LGTM. It might be preferable to check COMPLETE_TYPE_P in the caller > instead, so that we avoid inspecting CLASSTYPE_NON_AGGREGATE on an > incomplete class type, and so that the caller doesn't "commit" to > building an aggregate conversion. Perhaps. I wanted to avoid the call to build_user_type_conversion_1. I could add an early return to implicit_conversion_1 but I'd have to move some code around not to check COMPLETE_TYPE_P before complete_type. > > > > -- >8 -- > > > > r13-1227 added an assert checking that the index in a CONSTRUCTOR > > is a FIELD_DECL. That's a reasonable assumption but in this case > > we never called reshape_init due to the type being incomplete, and > > so the index remained an identifier node: get_class_binding never > > got around to looking up the FIELD_DECL. > > > > We can avoid the crash by returning early in build_aggr_conv; we'd > > return NULL anyway due to: > > > > if (i < CONSTRUCTOR_NELTS (ctor)) > > return NULL; > > > > PR c++/110114 > > > > gcc/cp/ChangeLog: > > > > * call.cc (build_aggr_conv): Return early if the type isn't > > complete. > > > > gcc/testsuite/ChangeLog: > > > > * g++.dg/cpp2a/desig28.C: New test. > > * g++.dg/cpp2a/desig29.C: New test. > > --- > > gcc/cp/call.cc | 5 +++++ > > gcc/testsuite/g++.dg/cpp2a/desig28.C | 17 +++++++++++++++++ > > gcc/testsuite/g++.dg/cpp2a/desig29.C | 10 ++++++++++ > > 3 files changed, 32 insertions(+) > > create mode 100644 gcc/testsuite/g++.dg/cpp2a/desig28.C > > create mode 100644 gcc/testsuite/g++.dg/cpp2a/desig29.C > > > > diff --git a/gcc/cp/call.cc b/gcc/cp/call.cc > > index b55230d98aa..0af20a81717 100644 > > --- a/gcc/cp/call.cc > > +++ b/gcc/cp/call.cc > > @@ -986,6 +986,11 @@ build_aggr_conv (tree type, tree ctor, int flags, tsubst_flags_t complain) > > tree empty_ctor = NULL_TREE; > > hash_set<tree, true> pset; > > > > + /* We've called complete_type on TYPE before calling this function, but > > + perhaps it wasn't successful. */ > > + if (!COMPLETE_TYPE_P (type)) > > + return nullptr; > > + > > /* We already called reshape_init in implicit_conversion, but it might not > > have done anything in the case of parenthesized aggr init. */ > > > > diff --git a/gcc/testsuite/g++.dg/cpp2a/desig28.C b/gcc/testsuite/g++.dg/cpp2a/desig28.C > > new file mode 100644 > > index 00000000000..b63265fea51 > > --- /dev/null > > +++ b/gcc/testsuite/g++.dg/cpp2a/desig28.C > > @@ -0,0 +1,17 @@ > > +// PR c++/110114 > > +// { dg-do compile { target c++20 } } > > + > > +struct A { > > + int a,b; > > +}; > > + > > +struct B; > > + > > +void foo(const A &) {} > > +void foo(const B &) {} > > + > > +int > > +main () > > +{ > > + foo({.a=0}); > > +} > > diff --git a/gcc/testsuite/g++.dg/cpp2a/desig29.C b/gcc/testsuite/g++.dg/cpp2a/desig29.C > > new file mode 100644 > > index 00000000000..bd1a82b041d > > --- /dev/null > > +++ b/gcc/testsuite/g++.dg/cpp2a/desig29.C > > @@ -0,0 +1,10 @@ > > +// PR c++/110114 > > +// { dg-do compile { target c++20 } } > > + > > +struct B; > > + > > +void foo(const B &) {} > > + > > +int main() { > > + foo({.a=0}); // { dg-error "invalid" } > > +} > > > > base-commit: 2971ff7b1d564ac04b537d907c70e6093af70832 > > -- > > 2.41.0 > > > > > Marek ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH] c++: fix ICE with designated initializer [PR110114] 2023-07-19 18:38 ` Marek Polacek @ 2023-07-19 19:24 ` Jason Merrill 2023-07-20 14:08 ` [PATCH v2] " Marek Polacek 0 siblings, 1 reply; 6+ messages in thread From: Jason Merrill @ 2023-07-19 19:24 UTC (permalink / raw) To: Marek Polacek, Patrick Palka; +Cc: GCC Patches On 7/19/23 14:38, Marek Polacek wrote: > On Wed, Jul 19, 2023 at 02:32:15PM -0400, Patrick Palka wrote: >> On Wed, 19 Jul 2023, Marek Polacek wrote: >> >>> Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk? >> >> LGTM. It might be preferable to check COMPLETE_TYPE_P in the caller >> instead, so that we avoid inspecting CLASSTYPE_NON_AGGREGATE on an >> incomplete class type, and so that the caller doesn't "commit" to >> building an aggregate conversion. > > Perhaps. I wanted to avoid the call to build_user_type_conversion_1. > I could add an early return to implicit_conversion_1 but I'd have to > move some code around not to check COMPLETE_TYPE_P before complete_type. Maybe return NULL for the incomplete case here, rather than just skipping reshape_init? /* Call reshape_init early to remove redundant braces. */ if (expr && BRACE_ENCLOSED_INITIALIZER_P (expr) && CLASS_TYPE_P (to) && COMPLETE_TYPE_P (complete_type (to)) && !CLASSTYPE_NON_AGGREGATE (to)) { expr = reshape_init (to, expr, complain); if (expr == error_mark_node) return NULL; from = TREE_TYPE (expr); } If that doesn't work, the patch is fine as-is. >>> -- >8 -- >>> >>> r13-1227 added an assert checking that the index in a CONSTRUCTOR >>> is a FIELD_DECL. That's a reasonable assumption but in this case >>> we never called reshape_init due to the type being incomplete, and >>> so the index remained an identifier node: get_class_binding never >>> got around to looking up the FIELD_DECL. >>> >>> We can avoid the crash by returning early in build_aggr_conv; we'd >>> return NULL anyway due to: >>> >>> if (i < CONSTRUCTOR_NELTS (ctor)) >>> return NULL; >>> >>> PR c++/110114 >>> >>> gcc/cp/ChangeLog: >>> >>> * call.cc (build_aggr_conv): Return early if the type isn't >>> complete. >>> >>> gcc/testsuite/ChangeLog: >>> >>> * g++.dg/cpp2a/desig28.C: New test. >>> * g++.dg/cpp2a/desig29.C: New test. >>> --- >>> gcc/cp/call.cc | 5 +++++ >>> gcc/testsuite/g++.dg/cpp2a/desig28.C | 17 +++++++++++++++++ >>> gcc/testsuite/g++.dg/cpp2a/desig29.C | 10 ++++++++++ >>> 3 files changed, 32 insertions(+) >>> create mode 100644 gcc/testsuite/g++.dg/cpp2a/desig28.C >>> create mode 100644 gcc/testsuite/g++.dg/cpp2a/desig29.C >>> >>> diff --git a/gcc/cp/call.cc b/gcc/cp/call.cc >>> index b55230d98aa..0af20a81717 100644 >>> --- a/gcc/cp/call.cc >>> +++ b/gcc/cp/call.cc >>> @@ -986,6 +986,11 @@ build_aggr_conv (tree type, tree ctor, int flags, tsubst_flags_t complain) >>> tree empty_ctor = NULL_TREE; >>> hash_set<tree, true> pset; >>> >>> + /* We've called complete_type on TYPE before calling this function, but >>> + perhaps it wasn't successful. */ >>> + if (!COMPLETE_TYPE_P (type)) >>> + return nullptr; >>> + >>> /* We already called reshape_init in implicit_conversion, but it might not >>> have done anything in the case of parenthesized aggr init. */ >>> >>> diff --git a/gcc/testsuite/g++.dg/cpp2a/desig28.C b/gcc/testsuite/g++.dg/cpp2a/desig28.C >>> new file mode 100644 >>> index 00000000000..b63265fea51 >>> --- /dev/null >>> +++ b/gcc/testsuite/g++.dg/cpp2a/desig28.C >>> @@ -0,0 +1,17 @@ >>> +// PR c++/110114 >>> +// { dg-do compile { target c++20 } } >>> + >>> +struct A { >>> + int a,b; >>> +}; >>> + >>> +struct B; >>> + >>> +void foo(const A &) {} >>> +void foo(const B &) {} >>> + >>> +int >>> +main () >>> +{ >>> + foo({.a=0}); >>> +} >>> diff --git a/gcc/testsuite/g++.dg/cpp2a/desig29.C b/gcc/testsuite/g++.dg/cpp2a/desig29.C >>> new file mode 100644 >>> index 00000000000..bd1a82b041d >>> --- /dev/null >>> +++ b/gcc/testsuite/g++.dg/cpp2a/desig29.C >>> @@ -0,0 +1,10 @@ >>> +// PR c++/110114 >>> +// { dg-do compile { target c++20 } } >>> + >>> +struct B; >>> + >>> +void foo(const B &) {} >>> + >>> +int main() { >>> + foo({.a=0}); // { dg-error "invalid" } >>> +} >>> >>> base-commit: 2971ff7b1d564ac04b537d907c70e6093af70832 >>> -- >>> 2.41.0 >>> >>> >> > > Marek > ^ permalink raw reply [flat|nested] 6+ messages in thread
* [PATCH v2] c++: fix ICE with designated initializer [PR110114] 2023-07-19 19:24 ` Jason Merrill @ 2023-07-20 14:08 ` Marek Polacek 2023-07-20 14:29 ` Jason Merrill 0 siblings, 1 reply; 6+ messages in thread From: Marek Polacek @ 2023-07-20 14:08 UTC (permalink / raw) To: Jason Merrill; +Cc: Patrick Palka, GCC Patches On Wed, Jul 19, 2023 at 03:24:10PM -0400, Jason Merrill wrote: > On 7/19/23 14:38, Marek Polacek wrote: > > On Wed, Jul 19, 2023 at 02:32:15PM -0400, Patrick Palka wrote: > > > On Wed, 19 Jul 2023, Marek Polacek wrote: > > > > > > > Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk? > > > > > > LGTM. It might be preferable to check COMPLETE_TYPE_P in the caller > > > instead, so that we avoid inspecting CLASSTYPE_NON_AGGREGATE on an > > > incomplete class type, and so that the caller doesn't "commit" to > > > building an aggregate conversion. > > > > Perhaps. I wanted to avoid the call to build_user_type_conversion_1. > > I could add an early return to implicit_conversion_1 but I'd have to > > move some code around not to check COMPLETE_TYPE_P before complete_type. > > Maybe return NULL for the incomplete case here, rather than just skipping > reshape_init? > > /* Call reshape_init early to remove redundant braces. */ > if (expr && BRACE_ENCLOSED_INITIALIZER_P (expr) > && CLASS_TYPE_P (to) > && COMPLETE_TYPE_P (complete_type (to)) > && !CLASSTYPE_NON_AGGREGATE (to)) > { > expr = reshape_init (to, expr, complain); > if (expr == error_mark_node) > return NULL; > from = TREE_TYPE (expr); > } > > If that doesn't work, the patch is fine as-is. It does work, with one test tweak (which I don't think is a regression): Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk? -- >8 -- r13-1227 added an assert checking that the index in a CONSTRUCTOR is a FIELD_DECL. That's a reasonable assumption but in this case we never called reshape_init due to the type being incomplete, and so the index remained an identifier node: get_class_binding never got around to looking up the FIELD_DECL. We can avoid the crash by returning early in implicit_conversion_1; we'd return NULL anyway due to: if (i < CONSTRUCTOR_NELTS (ctor)) return NULL; in build_aggr_conv. PR c++/110114 gcc/cp/ChangeLog: * call.cc (implicit_conversion_1): Return early if the type isn't complete. gcc/testsuite/ChangeLog: * g++.dg/cpp0x/initlist100.C: Adjust expected diagnostic. * g++.dg/cpp2a/desig28.C: New test. * g++.dg/cpp2a/desig29.C: New test. --- gcc/cp/call.cc | 19 +++++++++++-------- gcc/testsuite/g++.dg/cpp0x/initlist100.C | 4 ++-- gcc/testsuite/g++.dg/cpp2a/desig28.C | 17 +++++++++++++++++ gcc/testsuite/g++.dg/cpp2a/desig29.C | 10 ++++++++++ 4 files changed, 40 insertions(+), 10 deletions(-) create mode 100644 gcc/testsuite/g++.dg/cpp2a/desig28.C create mode 100644 gcc/testsuite/g++.dg/cpp2a/desig29.C diff --git a/gcc/cp/call.cc b/gcc/cp/call.cc index b55230d98aa..673ec91d60e 100644 --- a/gcc/cp/call.cc +++ b/gcc/cp/call.cc @@ -2059,15 +2059,18 @@ implicit_conversion_1 (tree to, tree from, tree expr, bool c_cast_p, complain &= ~tf_error; /* Call reshape_init early to remove redundant braces. */ - if (expr && BRACE_ENCLOSED_INITIALIZER_P (expr) - && CLASS_TYPE_P (to) - && COMPLETE_TYPE_P (complete_type (to)) - && !CLASSTYPE_NON_AGGREGATE (to)) + if (expr && BRACE_ENCLOSED_INITIALIZER_P (expr) && CLASS_TYPE_P (to)) { - expr = reshape_init (to, expr, complain); - if (expr == error_mark_node) - return NULL; - from = TREE_TYPE (expr); + to = complete_type (to); + if (!COMPLETE_TYPE_P (to)) + return nullptr; + if (!CLASSTYPE_NON_AGGREGATE (to)) + { + expr = reshape_init (to, expr, complain); + if (expr == error_mark_node) + return nullptr; + from = TREE_TYPE (expr); + } } if (TYPE_REF_P (to)) diff --git a/gcc/testsuite/g++.dg/cpp0x/initlist100.C b/gcc/testsuite/g++.dg/cpp0x/initlist100.C index 9d80a004c17..6865d34a6f9 100644 --- a/gcc/testsuite/g++.dg/cpp0x/initlist100.C +++ b/gcc/testsuite/g++.dg/cpp0x/initlist100.C @@ -2,9 +2,9 @@ // { dg-do compile { target c++11 } } namespace std { -template <class> class initializer_list; // { dg-message "declaration" } +template <class> class initializer_list; } template <typename T> struct B { B (std::initializer_list<T>); }; struct C { virtual int foo (); }; -struct D : C {} d { B<C> { D {} } }; // { dg-error "incomplete|no matching" } +struct D : C {} d { B<C> { D {} } }; // { dg-error "no matching" } diff --git a/gcc/testsuite/g++.dg/cpp2a/desig28.C b/gcc/testsuite/g++.dg/cpp2a/desig28.C new file mode 100644 index 00000000000..b63265fea51 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp2a/desig28.C @@ -0,0 +1,17 @@ +// PR c++/110114 +// { dg-do compile { target c++20 } } + +struct A { + int a,b; +}; + +struct B; + +void foo(const A &) {} +void foo(const B &) {} + +int +main () +{ + foo({.a=0}); +} diff --git a/gcc/testsuite/g++.dg/cpp2a/desig29.C b/gcc/testsuite/g++.dg/cpp2a/desig29.C new file mode 100644 index 00000000000..bd1a82b041d --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp2a/desig29.C @@ -0,0 +1,10 @@ +// PR c++/110114 +// { dg-do compile { target c++20 } } + +struct B; + +void foo(const B &) {} + +int main() { + foo({.a=0}); // { dg-error "invalid" } +} base-commit: b86c0fe327a5196a316bd698d12765b08de5dce7 -- 2.41.0 ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH v2] c++: fix ICE with designated initializer [PR110114] 2023-07-20 14:08 ` [PATCH v2] " Marek Polacek @ 2023-07-20 14:29 ` Jason Merrill 0 siblings, 0 replies; 6+ messages in thread From: Jason Merrill @ 2023-07-20 14:29 UTC (permalink / raw) To: Marek Polacek; +Cc: Patrick Palka, GCC Patches On 7/20/23 10:08, Marek Polacek wrote: > On Wed, Jul 19, 2023 at 03:24:10PM -0400, Jason Merrill wrote: >> On 7/19/23 14:38, Marek Polacek wrote: >>> On Wed, Jul 19, 2023 at 02:32:15PM -0400, Patrick Palka wrote: >>>> On Wed, 19 Jul 2023, Marek Polacek wrote: >>>> >>>>> Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk? >>>> >>>> LGTM. It might be preferable to check COMPLETE_TYPE_P in the caller >>>> instead, so that we avoid inspecting CLASSTYPE_NON_AGGREGATE on an >>>> incomplete class type, and so that the caller doesn't "commit" to >>>> building an aggregate conversion. >>> >>> Perhaps. I wanted to avoid the call to build_user_type_conversion_1. >>> I could add an early return to implicit_conversion_1 but I'd have to >>> move some code around not to check COMPLETE_TYPE_P before complete_type. >> >> Maybe return NULL for the incomplete case here, rather than just skipping >> reshape_init? >> >> /* Call reshape_init early to remove redundant braces. */ >> if (expr && BRACE_ENCLOSED_INITIALIZER_P (expr) >> && CLASS_TYPE_P (to) >> && COMPLETE_TYPE_P (complete_type (to)) >> && !CLASSTYPE_NON_AGGREGATE (to)) >> { >> expr = reshape_init (to, expr, complain); >> if (expr == error_mark_node) >> return NULL; >> from = TREE_TYPE (expr); >> } >> >> If that doesn't work, the patch is fine as-is. > > It does work, with one test tweak (which I don't think is a regression): > > Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk? OK. > -- >8 -- > r13-1227 added an assert checking that the index in a CONSTRUCTOR > is a FIELD_DECL. That's a reasonable assumption but in this case > we never called reshape_init due to the type being incomplete, and > so the index remained an identifier node: get_class_binding never > got around to looking up the FIELD_DECL. > > We can avoid the crash by returning early in implicit_conversion_1; we'd > return NULL anyway due to: > > if (i < CONSTRUCTOR_NELTS (ctor)) > return NULL; > > in build_aggr_conv. > > PR c++/110114 > > gcc/cp/ChangeLog: > > * call.cc (implicit_conversion_1): Return early if the type isn't > complete. > > gcc/testsuite/ChangeLog: > > * g++.dg/cpp0x/initlist100.C: Adjust expected diagnostic. > * g++.dg/cpp2a/desig28.C: New test. > * g++.dg/cpp2a/desig29.C: New test. > --- > gcc/cp/call.cc | 19 +++++++++++-------- > gcc/testsuite/g++.dg/cpp0x/initlist100.C | 4 ++-- > gcc/testsuite/g++.dg/cpp2a/desig28.C | 17 +++++++++++++++++ > gcc/testsuite/g++.dg/cpp2a/desig29.C | 10 ++++++++++ > 4 files changed, 40 insertions(+), 10 deletions(-) > create mode 100644 gcc/testsuite/g++.dg/cpp2a/desig28.C > create mode 100644 gcc/testsuite/g++.dg/cpp2a/desig29.C > > diff --git a/gcc/cp/call.cc b/gcc/cp/call.cc > index b55230d98aa..673ec91d60e 100644 > --- a/gcc/cp/call.cc > +++ b/gcc/cp/call.cc > @@ -2059,15 +2059,18 @@ implicit_conversion_1 (tree to, tree from, tree expr, bool c_cast_p, > complain &= ~tf_error; > > /* Call reshape_init early to remove redundant braces. */ > - if (expr && BRACE_ENCLOSED_INITIALIZER_P (expr) > - && CLASS_TYPE_P (to) > - && COMPLETE_TYPE_P (complete_type (to)) > - && !CLASSTYPE_NON_AGGREGATE (to)) > + if (expr && BRACE_ENCLOSED_INITIALIZER_P (expr) && CLASS_TYPE_P (to)) > { > - expr = reshape_init (to, expr, complain); > - if (expr == error_mark_node) > - return NULL; > - from = TREE_TYPE (expr); > + to = complete_type (to); > + if (!COMPLETE_TYPE_P (to)) > + return nullptr; > + if (!CLASSTYPE_NON_AGGREGATE (to)) > + { > + expr = reshape_init (to, expr, complain); > + if (expr == error_mark_node) > + return nullptr; > + from = TREE_TYPE (expr); > + } > } > > if (TYPE_REF_P (to)) > diff --git a/gcc/testsuite/g++.dg/cpp0x/initlist100.C b/gcc/testsuite/g++.dg/cpp0x/initlist100.C > index 9d80a004c17..6865d34a6f9 100644 > --- a/gcc/testsuite/g++.dg/cpp0x/initlist100.C > +++ b/gcc/testsuite/g++.dg/cpp0x/initlist100.C > @@ -2,9 +2,9 @@ > // { dg-do compile { target c++11 } } > > namespace std { > -template <class> class initializer_list; // { dg-message "declaration" } > +template <class> class initializer_list; > } > > template <typename T> struct B { B (std::initializer_list<T>); }; > struct C { virtual int foo (); }; > -struct D : C {} d { B<C> { D {} } }; // { dg-error "incomplete|no matching" } > +struct D : C {} d { B<C> { D {} } }; // { dg-error "no matching" } > diff --git a/gcc/testsuite/g++.dg/cpp2a/desig28.C b/gcc/testsuite/g++.dg/cpp2a/desig28.C > new file mode 100644 > index 00000000000..b63265fea51 > --- /dev/null > +++ b/gcc/testsuite/g++.dg/cpp2a/desig28.C > @@ -0,0 +1,17 @@ > +// PR c++/110114 > +// { dg-do compile { target c++20 } } > + > +struct A { > + int a,b; > +}; > + > +struct B; > + > +void foo(const A &) {} > +void foo(const B &) {} > + > +int > +main () > +{ > + foo({.a=0}); > +} > diff --git a/gcc/testsuite/g++.dg/cpp2a/desig29.C b/gcc/testsuite/g++.dg/cpp2a/desig29.C > new file mode 100644 > index 00000000000..bd1a82b041d > --- /dev/null > +++ b/gcc/testsuite/g++.dg/cpp2a/desig29.C > @@ -0,0 +1,10 @@ > +// PR c++/110114 > +// { dg-do compile { target c++20 } } > + > +struct B; > + > +void foo(const B &) {} > + > +int main() { > + foo({.a=0}); // { dg-error "invalid" } > +} > > base-commit: b86c0fe327a5196a316bd698d12765b08de5dce7 ^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2023-07-20 14:29 UTC | newest] Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- 2023-07-19 18:00 [PATCH] c++: fix ICE with designated initializer [PR110114] Marek Polacek 2023-07-19 18:32 ` Patrick Palka 2023-07-19 18:38 ` Marek Polacek 2023-07-19 19:24 ` Jason Merrill 2023-07-20 14:08 ` [PATCH v2] " Marek Polacek 2023-07-20 14:29 ` Jason Merrill
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox; as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).