* [PATCH] c++: alias CTAD and copy deduction guide [PR115198] @ 2024-05-23 18:06 Patrick Palka 2024-05-23 21:24 ` Jason Merrill 0 siblings, 1 reply; 9+ messages in thread From: Patrick Palka @ 2024-05-23 18:06 UTC (permalink / raw) To: gcc-patches; +Cc: jason, Patrick Palka Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK for trunk/14? -- >8 -- Here we're neglecting to update DECL_NAME during the alias CTAD guide transformation, which causes copy_guide_p to return false for the transformed copy deduction guide since DECL_NAME is still __dguide_C with TREE_TYPE C<B, T> but it should be __dguide_A with TREE_TYPE A<T> (equivalently C<false, T>). This ultimately results in ambiguity during overload resolution between the copy deduction guide vs copy ctor guide. This patch makes us update DECL_NAME of a transformed guide accordingly during alias CTAD. This eventually needs to be done for inherited CTAD too, but it's not clear what identifier to use there since it has to be unique for each derived/base pair. For template<bool B, class T> struct A { ... }; template<class T> struct B : A<false, T> { using A<false, T>::A; } at first glance it'd be reasonable to give inherited guides a name of __dguide_B with TREE_TYPE A<false, T>, but since that name is already used B's own guides its TREE_TYPE is already B<T>. PR c++/115198 gcc/cp/ChangeLog: * pt.cc (alias_ctad_tweaks): Update DECL_NAME of a transformed guide during alias CTAD. gcc/testsuite/ChangeLog: * g++.dg/cpp2a/class-deduction-alias22.C: New test. --- gcc/cp/pt.cc | 9 ++++++++- .../g++.dg/cpp2a/class-deduction-alias22.C | 14 ++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 gcc/testsuite/g++.dg/cpp2a/class-deduction-alias22.C diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc index 0c4d96cf768..58873057abc 100644 --- a/gcc/cp/pt.cc +++ b/gcc/cp/pt.cc @@ -30304,13 +30304,14 @@ alias_ctad_tweaks (tree tmpl, tree uguides) any). */ enum { alias, inherited } ctad_kind; - tree atype, fullatparms, utype; + tree atype, fullatparms, utype, name; if (TREE_CODE (tmpl) == TEMPLATE_DECL) { ctad_kind = alias; atype = TREE_TYPE (tmpl); fullatparms = DECL_TEMPLATE_PARMS (tmpl); utype = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl)); + name = dguide_name (tmpl); } else { @@ -30318,6 +30319,10 @@ alias_ctad_tweaks (tree tmpl, tree uguides) atype = NULL_TREE; fullatparms = TREE_PURPOSE (tmpl); utype = TREE_VALUE (tmpl); + /* FIXME: What name should we give inherited guides? It needs to be + unique to the derived/base pair so that we don't clobber an earlier + setting of TREE_TYPE. */ + name = NULL_TREE; } tsubst_flags_t complain = tf_warning_or_error; @@ -30413,6 +30418,8 @@ alias_ctad_tweaks (tree tmpl, tree uguides) } if (g == error_mark_node) continue; + if (name) + DECL_NAME (g) = name; if (nfparms == 0) { /* The targs are all non-dependent, so g isn't a template. */ diff --git a/gcc/testsuite/g++.dg/cpp2a/class-deduction-alias22.C b/gcc/testsuite/g++.dg/cpp2a/class-deduction-alias22.C new file mode 100644 index 00000000000..9c6c841166a --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp2a/class-deduction-alias22.C @@ -0,0 +1,14 @@ +// PR c++/115198 +// { dg-do compile { target c++20 } } + +template<bool B, class T> +struct C { + C() = default; + C(const C&) = default; +}; + +template<class T> +using A = C<false, T>; + +C<false, int> c; +A a = c; // { dg-bogus "ambiguous" } -- 2.45.1.216.g4365c6fcf9 ^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [PATCH] c++: alias CTAD and copy deduction guide [PR115198] 2024-05-23 18:06 [PATCH] c++: alias CTAD and copy deduction guide [PR115198] Patrick Palka @ 2024-05-23 21:24 ` Jason Merrill 2024-05-23 21:42 ` Patrick Palka 0 siblings, 1 reply; 9+ messages in thread From: Jason Merrill @ 2024-05-23 21:24 UTC (permalink / raw) To: Patrick Palka, gcc-patches On 5/23/24 14:06, Patrick Palka wrote: > Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look > OK for trunk/14? > > -- >8 -- > > Here we're neglecting to update DECL_NAME during the alias CTAD guide > transformation, which causes copy_guide_p to return false for the > transformed copy deduction guide since DECL_NAME is still __dguide_C > with TREE_TYPE C<B, T> but it should be __dguide_A with TREE_TYPE A<T> > (equivalently C<false, T>). This ultimately results in ambiguity during > overload resolution between the copy deduction guide vs copy ctor guide. > > This patch makes us update DECL_NAME of a transformed guide accordingly > during alias CTAD. This eventually needs to be done for inherited CTAD > too, but it's not clear what identifier to use there since it has to be > unique for each derived/base pair. For > > template<bool B, class T> struct A { ... }; > template<class T> struct B : A<false, T> { using A<false, T>::A; } > > at first glance it'd be reasonable to give inherited guides a name of > __dguide_B with TREE_TYPE A<false, T>, but since that name is already > used B's own guides its TREE_TYPE is already B<T>. Why can't it be the same __dguide_B with TREE_TYPE B<T>? > PR c++/115198 > > gcc/cp/ChangeLog: > > * pt.cc (alias_ctad_tweaks): Update DECL_NAME of a transformed > guide during alias CTAD. > > gcc/testsuite/ChangeLog: > > * g++.dg/cpp2a/class-deduction-alias22.C: New test. > --- > gcc/cp/pt.cc | 9 ++++++++- > .../g++.dg/cpp2a/class-deduction-alias22.C | 14 ++++++++++++++ > 2 files changed, 22 insertions(+), 1 deletion(-) > create mode 100644 gcc/testsuite/g++.dg/cpp2a/class-deduction-alias22.C > > diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc > index 0c4d96cf768..58873057abc 100644 > --- a/gcc/cp/pt.cc > +++ b/gcc/cp/pt.cc > @@ -30304,13 +30304,14 @@ alias_ctad_tweaks (tree tmpl, tree uguides) > any). */ > > enum { alias, inherited } ctad_kind; > - tree atype, fullatparms, utype; > + tree atype, fullatparms, utype, name; > if (TREE_CODE (tmpl) == TEMPLATE_DECL) > { > ctad_kind = alias; > atype = TREE_TYPE (tmpl); > fullatparms = DECL_TEMPLATE_PARMS (tmpl); > utype = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl)); > + name = dguide_name (tmpl); > } > else > { > @@ -30318,6 +30319,10 @@ alias_ctad_tweaks (tree tmpl, tree uguides) > atype = NULL_TREE; > fullatparms = TREE_PURPOSE (tmpl); > utype = TREE_VALUE (tmpl); > + /* FIXME: What name should we give inherited guides? It needs to be > + unique to the derived/base pair so that we don't clobber an earlier > + setting of TREE_TYPE. */ > + name = NULL_TREE; > } > > tsubst_flags_t complain = tf_warning_or_error; > @@ -30413,6 +30418,8 @@ alias_ctad_tweaks (tree tmpl, tree uguides) > } > if (g == error_mark_node) > continue; > + if (name) > + DECL_NAME (g) = name; > if (nfparms == 0) > { > /* The targs are all non-dependent, so g isn't a template. */ > diff --git a/gcc/testsuite/g++.dg/cpp2a/class-deduction-alias22.C b/gcc/testsuite/g++.dg/cpp2a/class-deduction-alias22.C > new file mode 100644 > index 00000000000..9c6c841166a > --- /dev/null > +++ b/gcc/testsuite/g++.dg/cpp2a/class-deduction-alias22.C > @@ -0,0 +1,14 @@ > +// PR c++/115198 > +// { dg-do compile { target c++20 } } > + > +template<bool B, class T> > +struct C { > + C() = default; > + C(const C&) = default; > +}; > + > +template<class T> > +using A = C<false, T>; > + > +C<false, int> c; > +A a = c; // { dg-bogus "ambiguous" } ^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [PATCH] c++: alias CTAD and copy deduction guide [PR115198] 2024-05-23 21:24 ` Jason Merrill @ 2024-05-23 21:42 ` Patrick Palka 2024-05-23 22:38 ` Jason Merrill 0 siblings, 1 reply; 9+ messages in thread From: Patrick Palka @ 2024-05-23 21:42 UTC (permalink / raw) To: Jason Merrill; +Cc: Patrick Palka, gcc-patches On Thu, 23 May 2024, Jason Merrill wrote: > On 5/23/24 14:06, Patrick Palka wrote: > > Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look > > OK for trunk/14? > > > > -- >8 -- > > > > Here we're neglecting to update DECL_NAME during the alias CTAD guide > > transformation, which causes copy_guide_p to return false for the > > transformed copy deduction guide since DECL_NAME is still __dguide_C > > with TREE_TYPE C<B, T> but it should be __dguide_A with TREE_TYPE A<T> > > (equivalently C<false, T>). This ultimately results in ambiguity during > > overload resolution between the copy deduction guide vs copy ctor guide. > > > > This patch makes us update DECL_NAME of a transformed guide accordingly > > during alias CTAD. This eventually needs to be done for inherited CTAD > > too, but it's not clear what identifier to use there since it has to be > > unique for each derived/base pair. For > > > > template<bool B, class T> struct A { ... }; > > template<class T> struct B : A<false, T> { using A<false, T>::A; } > > > > at first glance it'd be reasonable to give inherited guides a name of > > __dguide_B with TREE_TYPE A<false, T>, but since that name is already > > used B's own guides its TREE_TYPE is already B<T>. > > Why can't it be the same __dguide_B with TREE_TYPE B<T>? Ah because copy_guide_p relies on TREE_TYPE in order to recognize a copy deduction guide, and with that TREE_TYPE it would still incorrectly return false for an inherited copy deduction guide, e.g. A(A<B, T>) -> A<B, T> gets transformed into B(A<false, T>) -> B<T> and A<false, T> != B<T> so copy_guide_p returns false. But it just occurred to me that this TREE_TYPE clobbering of the __dguide_foo identifier already happens if we have two class templates with the same name in different namespaces, since the identifier contains only the terminal name. Maybe this suggests that we should use a tree flag to track whether a guide is the copy deduction guide instead of setting TREE_TYPE of DECL_NAME? > > > PR c++/115198 > > > > gcc/cp/ChangeLog: > > > > * pt.cc (alias_ctad_tweaks): Update DECL_NAME of a transformed > > guide during alias CTAD. > > > > gcc/testsuite/ChangeLog: > > > > * g++.dg/cpp2a/class-deduction-alias22.C: New test. > > --- > > gcc/cp/pt.cc | 9 ++++++++- > > .../g++.dg/cpp2a/class-deduction-alias22.C | 14 ++++++++++++++ > > 2 files changed, 22 insertions(+), 1 deletion(-) > > create mode 100644 gcc/testsuite/g++.dg/cpp2a/class-deduction-alias22.C > > > > diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc > > index 0c4d96cf768..58873057abc 100644 > > --- a/gcc/cp/pt.cc > > +++ b/gcc/cp/pt.cc > > @@ -30304,13 +30304,14 @@ alias_ctad_tweaks (tree tmpl, tree uguides) > > any). */ > > enum { alias, inherited } ctad_kind; > > - tree atype, fullatparms, utype; > > + tree atype, fullatparms, utype, name; > > if (TREE_CODE (tmpl) == TEMPLATE_DECL) > > { > > ctad_kind = alias; > > atype = TREE_TYPE (tmpl); > > fullatparms = DECL_TEMPLATE_PARMS (tmpl); > > utype = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl)); > > + name = dguide_name (tmpl); > > } > > else > > { > > @@ -30318,6 +30319,10 @@ alias_ctad_tweaks (tree tmpl, tree uguides) > > atype = NULL_TREE; > > fullatparms = TREE_PURPOSE (tmpl); > > utype = TREE_VALUE (tmpl); > > + /* FIXME: What name should we give inherited guides? It needs to be > > + unique to the derived/base pair so that we don't clobber an earlier > > + setting of TREE_TYPE. */ > > + name = NULL_TREE; > > } > > tsubst_flags_t complain = tf_warning_or_error; > > @@ -30413,6 +30418,8 @@ alias_ctad_tweaks (tree tmpl, tree uguides) > > } > > if (g == error_mark_node) > > continue; > > + if (name) > > + DECL_NAME (g) = name; > > if (nfparms == 0) > > { > > /* The targs are all non-dependent, so g isn't a template. */ > > diff --git a/gcc/testsuite/g++.dg/cpp2a/class-deduction-alias22.C > > b/gcc/testsuite/g++.dg/cpp2a/class-deduction-alias22.C > > new file mode 100644 > > index 00000000000..9c6c841166a > > --- /dev/null > > +++ b/gcc/testsuite/g++.dg/cpp2a/class-deduction-alias22.C > > @@ -0,0 +1,14 @@ > > +// PR c++/115198 > > +// { dg-do compile { target c++20 } } > > + > > +template<bool B, class T> > > +struct C { > > + C() = default; > > + C(const C&) = default; > > +}; > > + > > +template<class T> > > +using A = C<false, T>; > > + > > +C<false, int> c; > > +A a = c; // { dg-bogus "ambiguous" } > > ^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [PATCH] c++: alias CTAD and copy deduction guide [PR115198] 2024-05-23 21:42 ` Patrick Palka @ 2024-05-23 22:38 ` Jason Merrill 2024-06-13 15:05 ` Patrick Palka 0 siblings, 1 reply; 9+ messages in thread From: Jason Merrill @ 2024-05-23 22:38 UTC (permalink / raw) To: Patrick Palka; +Cc: gcc-patches On 5/23/24 17:42, Patrick Palka wrote: > On Thu, 23 May 2024, Jason Merrill wrote: > >> On 5/23/24 14:06, Patrick Palka wrote: >>> Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look >>> OK for trunk/14? >>> >>> -- >8 -- >>> >>> Here we're neglecting to update DECL_NAME during the alias CTAD guide >>> transformation, which causes copy_guide_p to return false for the >>> transformed copy deduction guide since DECL_NAME is still __dguide_C >>> with TREE_TYPE C<B, T> but it should be __dguide_A with TREE_TYPE A<T> >>> (equivalently C<false, T>). This ultimately results in ambiguity during >>> overload resolution between the copy deduction guide vs copy ctor guide. >>> >>> This patch makes us update DECL_NAME of a transformed guide accordingly >>> during alias CTAD. This eventually needs to be done for inherited CTAD >>> too, but it's not clear what identifier to use there since it has to be >>> unique for each derived/base pair. For >>> >>> template<bool B, class T> struct A { ... }; >>> template<class T> struct B : A<false, T> { using A<false, T>::A; } >>> >>> at first glance it'd be reasonable to give inherited guides a name of >>> __dguide_B with TREE_TYPE A<false, T>, but since that name is already >>> used B's own guides its TREE_TYPE is already B<T>. >> >> Why can't it be the same __dguide_B with TREE_TYPE B<T>? > > Ah because copy_guide_p relies on TREE_TYPE in order to recognize a copy > deduction guide, and with that TREE_TYPE it would still incorrectly > return false for an inherited copy deduction guide, e.g. > > A(A<B, T>) -> A<B, T> > > gets transformed into > > B(A<false, T>) -> B<T> > > and A<false, T> != B<T> so copy_guide_p returns false. Hmm, that seems correct; the transformed candidate is not the copy deduction guide for B. > But it just occurred to me that this TREE_TYPE clobbering of the > __dguide_foo identifier already happens if we have two class templates > with the same name in different namespaces, since the identifier > contains only the terminal name. Maybe this suggests that we should > use a tree flag to track whether a guide is the copy deduction guide > instead of setting TREE_TYPE of DECL_NAME? Good point. Jason ^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [PATCH] c++: alias CTAD and copy deduction guide [PR115198] 2024-05-23 22:38 ` Jason Merrill @ 2024-06-13 15:05 ` Patrick Palka 2024-06-13 15:28 ` Jason Merrill 0 siblings, 1 reply; 9+ messages in thread From: Patrick Palka @ 2024-06-13 15:05 UTC (permalink / raw) To: Jason Merrill; +Cc: Patrick Palka, gcc-patches On Thu, 23 May 2024, Jason Merrill wrote: > On 5/23/24 17:42, Patrick Palka wrote: > > On Thu, 23 May 2024, Jason Merrill wrote: > > > > > On 5/23/24 14:06, Patrick Palka wrote: > > > > Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look > > > > OK for trunk/14? > > > > > > > > -- >8 -- > > > > > > > > Here we're neglecting to update DECL_NAME during the alias CTAD guide > > > > transformation, which causes copy_guide_p to return false for the > > > > transformed copy deduction guide since DECL_NAME is still __dguide_C > > > > with TREE_TYPE C<B, T> but it should be __dguide_A with TREE_TYPE A<T> > > > > (equivalently C<false, T>). This ultimately results in ambiguity during > > > > overload resolution between the copy deduction guide vs copy ctor guide. > > > > > > > > This patch makes us update DECL_NAME of a transformed guide accordingly > > > > during alias CTAD. This eventually needs to be done for inherited CTAD > > > > too, but it's not clear what identifier to use there since it has to be > > > > unique for each derived/base pair. For > > > > > > > > template<bool B, class T> struct A { ... }; > > > > template<class T> struct B : A<false, T> { using A<false, T>::A; } > > > > > > > > at first glance it'd be reasonable to give inherited guides a name of > > > > __dguide_B with TREE_TYPE A<false, T>, but since that name is already > > > > used B's own guides its TREE_TYPE is already B<T>. > > > > > > Why can't it be the same __dguide_B with TREE_TYPE B<T>? > > > > Ah because copy_guide_p relies on TREE_TYPE in order to recognize a copy > > deduction guide, and with that TREE_TYPE it would still incorrectly > > return false for an inherited copy deduction guide, e.g. > > > > A(A<B, T>) -> A<B, T> > > > > gets transformed into > > > > B(A<false, T>) -> B<T> > > > > and A<false, T> != B<T> so copy_guide_p returns false. > > Hmm, that seems correct; the transformed candidate is not the copy deduction > guide for B. By https://eel.is/c++draft/over.match.class.deduct#3.4 it seems that a class template can now have multiple copy deduction guides with inherited CTAD: the derived class's own copy guide, along with the transformed copy guides of its eligible base classes. Do we want to follow the standard precisely here, or should we maybe restrict the copy-guideness propagation to alias CTAD only? > > > But it just occurred to me that this TREE_TYPE clobbering of the > > __dguide_foo identifier already happens if we have two class templates > > with the same name in different namespaces, since the identifier > > contains only the terminal name. Maybe this suggests that we should > > use a tree flag to track whether a guide is the copy deduction guide > > instead of setting TREE_TYPE of DECL_NAME? > > Good point. > > Jason > > ^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [PATCH] c++: alias CTAD and copy deduction guide [PR115198] 2024-06-13 15:05 ` Patrick Palka @ 2024-06-13 15:28 ` Jason Merrill 2024-06-13 17:00 ` Patrick Palka 0 siblings, 1 reply; 9+ messages in thread From: Jason Merrill @ 2024-06-13 15:28 UTC (permalink / raw) To: Patrick Palka; +Cc: gcc-patches On 6/13/24 11:05, Patrick Palka wrote: > On Thu, 23 May 2024, Jason Merrill wrote: > >> On 5/23/24 17:42, Patrick Palka wrote: >>> On Thu, 23 May 2024, Jason Merrill wrote: >>> >>>> On 5/23/24 14:06, Patrick Palka wrote: >>>>> Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look >>>>> OK for trunk/14? >>>>> >>>>> -- >8 -- >>>>> >>>>> Here we're neglecting to update DECL_NAME during the alias CTAD guide >>>>> transformation, which causes copy_guide_p to return false for the >>>>> transformed copy deduction guide since DECL_NAME is still __dguide_C >>>>> with TREE_TYPE C<B, T> but it should be __dguide_A with TREE_TYPE A<T> >>>>> (equivalently C<false, T>). This ultimately results in ambiguity during >>>>> overload resolution between the copy deduction guide vs copy ctor guide. >>>>> >>>>> This patch makes us update DECL_NAME of a transformed guide accordingly >>>>> during alias CTAD. This eventually needs to be done for inherited CTAD >>>>> too, but it's not clear what identifier to use there since it has to be >>>>> unique for each derived/base pair. For >>>>> >>>>> template<bool B, class T> struct A { ... }; >>>>> template<class T> struct B : A<false, T> { using A<false, T>::A; } >>>>> >>>>> at first glance it'd be reasonable to give inherited guides a name of >>>>> __dguide_B with TREE_TYPE A<false, T>, but since that name is already >>>>> used B's own guides its TREE_TYPE is already B<T>. >>>> >>>> Why can't it be the same __dguide_B with TREE_TYPE B<T>? >>> >>> Ah because copy_guide_p relies on TREE_TYPE in order to recognize a copy >>> deduction guide, and with that TREE_TYPE it would still incorrectly >>> return false for an inherited copy deduction guide, e.g. >>> >>> A(A<B, T>) -> A<B, T> >>> >>> gets transformed into >>> >>> B(A<false, T>) -> B<T> >>> >>> and A<false, T> != B<T> so copy_guide_p returns false. >> >> Hmm, that seems correct; the transformed candidate is not the copy deduction >> guide for B. > > By https://eel.is/c++draft/over.match.class.deduct#3.4 it seems that a > class template can now have multiple copy deduction guides with inherited > CTAD: the derived class's own copy guide, along with the transformed copy > guides of its eligible base classes. Do we want to follow the standard > precisely here, or should we maybe restrict the copy-guideness propagation > to alias CTAD only? The latter, I think; it seems nonsensical to have multiple copy guides. Jason >>> But it just occurred to me that this TREE_TYPE clobbering of the >>> __dguide_foo identifier already happens if we have two class templates >>> with the same name in different namespaces, since the identifier >>> contains only the terminal name. Maybe this suggests that we should >>> use a tree flag to track whether a guide is the copy deduction guide >>> instead of setting TREE_TYPE of DECL_NAME? >> >> Good point. >> >> Jason >> >> > ^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [PATCH] c++: alias CTAD and copy deduction guide [PR115198] 2024-06-13 15:28 ` Jason Merrill @ 2024-06-13 17:00 ` Patrick Palka 2024-06-25 15:04 ` Patrick Palka 2024-06-25 15:33 ` Jason Merrill 0 siblings, 2 replies; 9+ messages in thread From: Patrick Palka @ 2024-06-13 17:00 UTC (permalink / raw) To: Jason Merrill; +Cc: Patrick Palka, gcc-patches On Thu, 13 Jun 2024, Jason Merrill wrote: > On 6/13/24 11:05, Patrick Palka wrote: > > On Thu, 23 May 2024, Jason Merrill wrote: > > > > > On 5/23/24 17:42, Patrick Palka wrote: > > > > On Thu, 23 May 2024, Jason Merrill wrote: > > > > > > > > > On 5/23/24 14:06, Patrick Palka wrote: > > > > > > Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look > > > > > > OK for trunk/14? > > > > > > > > > > > > -- >8 -- > > > > > > > > > > > > Here we're neglecting to update DECL_NAME during the alias CTAD > > > > > > guide > > > > > > transformation, which causes copy_guide_p to return false for the > > > > > > transformed copy deduction guide since DECL_NAME is still __dguide_C > > > > > > with TREE_TYPE C<B, T> but it should be __dguide_A with TREE_TYPE > > > > > > A<T> > > > > > > (equivalently C<false, T>). This ultimately results in ambiguity > > > > > > during > > > > > > overload resolution between the copy deduction guide vs copy ctor > > > > > > guide. > > > > > > > > > > > > This patch makes us update DECL_NAME of a transformed guide > > > > > > accordingly > > > > > > during alias CTAD. This eventually needs to be done for inherited > > > > > > CTAD > > > > > > too, but it's not clear what identifier to use there since it has to > > > > > > be > > > > > > unique for each derived/base pair. For > > > > > > > > > > > > template<bool B, class T> struct A { ... }; > > > > > > template<class T> struct B : A<false, T> { using A<false, > > > > > > T>::A; } > > > > > > > > > > > > at first glance it'd be reasonable to give inherited guides a name > > > > > > of > > > > > > __dguide_B with TREE_TYPE A<false, T>, but since that name is > > > > > > already > > > > > > used B's own guides its TREE_TYPE is already B<T>. > > > > > > > > > > Why can't it be the same __dguide_B with TREE_TYPE B<T>? > > > > > > > > Ah because copy_guide_p relies on TREE_TYPE in order to recognize a copy > > > > deduction guide, and with that TREE_TYPE it would still incorrectly > > > > return false for an inherited copy deduction guide, e.g. > > > > > > > > A(A<B, T>) -> A<B, T> > > > > > > > > gets transformed into > > > > > > > > B(A<false, T>) -> B<T> > > > > > > > > and A<false, T> != B<T> so copy_guide_p returns false. > > > > > > Hmm, that seems correct; the transformed candidate is not the copy > > > deduction > > > guide for B. > > > > By https://eel.is/c++draft/over.match.class.deduct#3.4 it seems that a > > class template can now have multiple copy deduction guides with inherited > > CTAD: the derived class's own copy guide, along with the transformed copy > > guides of its eligible base classes. Do we want to follow the standard > > precisely here, or should we maybe restrict the copy-guideness propagation > > to alias CTAD only? > > The latter, I think; it seems nonsensical to have multiple copy guides. Sounds good, so for inherited CTAD it should suffice to use __dguide_B as the name (where B is the derived class), like so? -- >8 -- Subject: [PATCH] c++: alias CTAD and copy deduction guide [PR115198] Here we're neglecting to update DECL_NAME during the alias CTAD guide transformation, which causes copy_guide_p to return false for the transformed copy deduction guide since DECL_NAME is still __dguide_C with TREE_TYPE C<B, T> but it should be __dguide_A with TREE_TYPE A<T> (equivalently C<false, T>). This ultimately results in ambiguity during overload resolution between the copy deduction guide vs copy ctor guide. This patch makes us update DECL_NAME of a transformed guide accordingly during alias/inherited CTAD. PR c++/115198 gcc/cp/ChangeLog: * pt.cc (alias_ctad_tweaks): Update DECL_NAME of a transformed guide. gcc/testsuite/ChangeLog: * g++.dg/cpp2a/class-deduction-alias22.C: New test. --- gcc/cp/pt.cc | 6 +++++- .../g++.dg/cpp2a/class-deduction-alias22.C | 14 ++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) create mode 100644 gcc/testsuite/g++.dg/cpp2a/class-deduction-alias22.C diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc index 607753ae6b7..daa8ac386dc 100644 --- a/gcc/cp/pt.cc +++ b/gcc/cp/pt.cc @@ -30342,13 +30342,14 @@ alias_ctad_tweaks (tree tmpl, tree uguides) any). */ enum { alias, inherited } ctad_kind; - tree atype, fullatparms, utype; + tree atype, fullatparms, utype, name; if (TREE_CODE (tmpl) == TEMPLATE_DECL) { ctad_kind = alias; atype = TREE_TYPE (tmpl); fullatparms = DECL_TEMPLATE_PARMS (tmpl); utype = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl)); + name = dguide_name (tmpl); } else { @@ -30356,6 +30357,8 @@ alias_ctad_tweaks (tree tmpl, tree uguides) atype = NULL_TREE; fullatparms = TREE_PURPOSE (tmpl); utype = TREE_VALUE (tmpl); + name = dguide_name (TPARMS_PRIMARY_TEMPLATE + (INNERMOST_TEMPLATE_PARMS (fullatparms))); } tsubst_flags_t complain = tf_warning_or_error; @@ -30451,6 +30454,7 @@ alias_ctad_tweaks (tree tmpl, tree uguides) } if (g == error_mark_node) continue; + DECL_NAME (g) = name; if (nfparms == 0) { /* The targs are all non-dependent, so g isn't a template. */ diff --git a/gcc/testsuite/g++.dg/cpp2a/class-deduction-alias22.C b/gcc/testsuite/g++.dg/cpp2a/class-deduction-alias22.C new file mode 100644 index 00000000000..9c6c841166a --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp2a/class-deduction-alias22.C @@ -0,0 +1,14 @@ +// PR c++/115198 +// { dg-do compile { target c++20 } } + +template<bool B, class T> +struct C { + C() = default; + C(const C&) = default; +}; + +template<class T> +using A = C<false, T>; + +C<false, int> c; +A a = c; // { dg-bogus "ambiguous" } -- 2.45.2.492.gd63586cb31 > > Jason > > > > > But it just occurred to me that this TREE_TYPE clobbering of the > > > > __dguide_foo identifier already happens if we have two class templates > > > > with the same name in different namespaces, since the identifier > > > > contains only the terminal name. Maybe this suggests that we should > > > > use a tree flag to track whether a guide is the copy deduction guide > > > > instead of setting TREE_TYPE of DECL_NAME? > > > > > > Good point. > > > > > > Jason > > > > > > > > > > ^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [PATCH] c++: alias CTAD and copy deduction guide [PR115198] 2024-06-13 17:00 ` Patrick Palka @ 2024-06-25 15:04 ` Patrick Palka 2024-06-25 15:33 ` Jason Merrill 1 sibling, 0 replies; 9+ messages in thread From: Patrick Palka @ 2024-06-25 15:04 UTC (permalink / raw) To: Patrick Palka; +Cc: Jason Merrill, gcc-patches On Thu, 13 Jun 2024, Patrick Palka wrote: > On Thu, 13 Jun 2024, Jason Merrill wrote: > > > On 6/13/24 11:05, Patrick Palka wrote: > > > On Thu, 23 May 2024, Jason Merrill wrote: > > > > > > > On 5/23/24 17:42, Patrick Palka wrote: > > > > > On Thu, 23 May 2024, Jason Merrill wrote: > > > > > > > > > > > On 5/23/24 14:06, Patrick Palka wrote: > > > > > > > Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look > > > > > > > OK for trunk/14? > > > > > > > > > > > > > > -- >8 -- > > > > > > > > > > > > > > Here we're neglecting to update DECL_NAME during the alias CTAD > > > > > > > guide > > > > > > > transformation, which causes copy_guide_p to return false for the > > > > > > > transformed copy deduction guide since DECL_NAME is still __dguide_C > > > > > > > with TREE_TYPE C<B, T> but it should be __dguide_A with TREE_TYPE > > > > > > > A<T> > > > > > > > (equivalently C<false, T>). This ultimately results in ambiguity > > > > > > > during > > > > > > > overload resolution between the copy deduction guide vs copy ctor > > > > > > > guide. > > > > > > > > > > > > > > This patch makes us update DECL_NAME of a transformed guide > > > > > > > accordingly > > > > > > > during alias CTAD. This eventually needs to be done for inherited > > > > > > > CTAD > > > > > > > too, but it's not clear what identifier to use there since it has to > > > > > > > be > > > > > > > unique for each derived/base pair. For > > > > > > > > > > > > > > template<bool B, class T> struct A { ... }; > > > > > > > template<class T> struct B : A<false, T> { using A<false, > > > > > > > T>::A; } > > > > > > > > > > > > > > at first glance it'd be reasonable to give inherited guides a name > > > > > > > of > > > > > > > __dguide_B with TREE_TYPE A<false, T>, but since that name is > > > > > > > already > > > > > > > used B's own guides its TREE_TYPE is already B<T>. > > > > > > > > > > > > Why can't it be the same __dguide_B with TREE_TYPE B<T>? > > > > > > > > > > Ah because copy_guide_p relies on TREE_TYPE in order to recognize a copy > > > > > deduction guide, and with that TREE_TYPE it would still incorrectly > > > > > return false for an inherited copy deduction guide, e.g. > > > > > > > > > > A(A<B, T>) -> A<B, T> > > > > > > > > > > gets transformed into > > > > > > > > > > B(A<false, T>) -> B<T> > > > > > > > > > > and A<false, T> != B<T> so copy_guide_p returns false. > > > > > > > > Hmm, that seems correct; the transformed candidate is not the copy > > > > deduction > > > > guide for B. > > > > > > By https://eel.is/c++draft/over.match.class.deduct#3.4 it seems that a > > > class template can now have multiple copy deduction guides with inherited > > > CTAD: the derived class's own copy guide, along with the transformed copy > > > guides of its eligible base classes. Do we want to follow the standard > > > precisely here, or should we maybe restrict the copy-guideness propagation > > > to alias CTAD only? > > > > The latter, I think; it seems nonsensical to have multiple copy guides. > > Sounds good, so for inherited CTAD it should suffice to use __dguide_B > as the name (where B is the derived class), like so? Ping. > > -- >8 -- > > Subject: [PATCH] c++: alias CTAD and copy deduction guide [PR115198] > > Here we're neglecting to update DECL_NAME during the alias CTAD guide > transformation, which causes copy_guide_p to return false for the > transformed copy deduction guide since DECL_NAME is still __dguide_C > with TREE_TYPE C<B, T> but it should be __dguide_A with TREE_TYPE A<T> > (equivalently C<false, T>). This ultimately results in ambiguity during > overload resolution between the copy deduction guide vs copy ctor guide. > > This patch makes us update DECL_NAME of a transformed guide accordingly > during alias/inherited CTAD. > > PR c++/115198 > > gcc/cp/ChangeLog: > > * pt.cc (alias_ctad_tweaks): Update DECL_NAME of a transformed > guide. > > gcc/testsuite/ChangeLog: > > * g++.dg/cpp2a/class-deduction-alias22.C: New test. > --- > gcc/cp/pt.cc | 6 +++++- > .../g++.dg/cpp2a/class-deduction-alias22.C | 14 ++++++++++++++ > 2 files changed, 19 insertions(+), 1 deletion(-) > create mode 100644 gcc/testsuite/g++.dg/cpp2a/class-deduction-alias22.C > > diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc > index 607753ae6b7..daa8ac386dc 100644 > --- a/gcc/cp/pt.cc > +++ b/gcc/cp/pt.cc > @@ -30342,13 +30342,14 @@ alias_ctad_tweaks (tree tmpl, tree uguides) > any). */ > > enum { alias, inherited } ctad_kind; > - tree atype, fullatparms, utype; > + tree atype, fullatparms, utype, name; > if (TREE_CODE (tmpl) == TEMPLATE_DECL) > { > ctad_kind = alias; > atype = TREE_TYPE (tmpl); > fullatparms = DECL_TEMPLATE_PARMS (tmpl); > utype = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl)); > + name = dguide_name (tmpl); > } > else > { > @@ -30356,6 +30357,8 @@ alias_ctad_tweaks (tree tmpl, tree uguides) > atype = NULL_TREE; > fullatparms = TREE_PURPOSE (tmpl); > utype = TREE_VALUE (tmpl); > + name = dguide_name (TPARMS_PRIMARY_TEMPLATE > + (INNERMOST_TEMPLATE_PARMS (fullatparms))); > } > > tsubst_flags_t complain = tf_warning_or_error; > @@ -30451,6 +30454,7 @@ alias_ctad_tweaks (tree tmpl, tree uguides) > } > if (g == error_mark_node) > continue; > + DECL_NAME (g) = name; > if (nfparms == 0) > { > /* The targs are all non-dependent, so g isn't a template. */ > diff --git a/gcc/testsuite/g++.dg/cpp2a/class-deduction-alias22.C b/gcc/testsuite/g++.dg/cpp2a/class-deduction-alias22.C > new file mode 100644 > index 00000000000..9c6c841166a > --- /dev/null > +++ b/gcc/testsuite/g++.dg/cpp2a/class-deduction-alias22.C > @@ -0,0 +1,14 @@ > +// PR c++/115198 > +// { dg-do compile { target c++20 } } > + > +template<bool B, class T> > +struct C { > + C() = default; > + C(const C&) = default; > +}; > + > +template<class T> > +using A = C<false, T>; > + > +C<false, int> c; > +A a = c; // { dg-bogus "ambiguous" } > -- > 2.45.2.492.gd63586cb31 > > > > > > Jason > > > > > > > But it just occurred to me that this TREE_TYPE clobbering of the > > > > > __dguide_foo identifier already happens if we have two class templates > > > > > with the same name in different namespaces, since the identifier > > > > > contains only the terminal name. Maybe this suggests that we should > > > > > use a tree flag to track whether a guide is the copy deduction guide > > > > > instead of setting TREE_TYPE of DECL_NAME? > > > > > > > > Good point. > > > > > > > > Jason > > > > > > > > > > > > > > > > ^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [PATCH] c++: alias CTAD and copy deduction guide [PR115198] 2024-06-13 17:00 ` Patrick Palka 2024-06-25 15:04 ` Patrick Palka @ 2024-06-25 15:33 ` Jason Merrill 1 sibling, 0 replies; 9+ messages in thread From: Jason Merrill @ 2024-06-25 15:33 UTC (permalink / raw) To: Patrick Palka; +Cc: gcc-patches On 6/13/24 13:00, Patrick Palka wrote: > On Thu, 13 Jun 2024, Jason Merrill wrote: > >> On 6/13/24 11:05, Patrick Palka wrote: >>> On Thu, 23 May 2024, Jason Merrill wrote: >>> >>>> On 5/23/24 17:42, Patrick Palka wrote: >>>>> On Thu, 23 May 2024, Jason Merrill wrote: >>>>> >>>>>> On 5/23/24 14:06, Patrick Palka wrote: >>>>>>> Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look >>>>>>> OK for trunk/14? >>>>>>> >>>>>>> -- >8 -- >>>>>>> >>>>>>> Here we're neglecting to update DECL_NAME during the alias CTAD >>>>>>> guide >>>>>>> transformation, which causes copy_guide_p to return false for the >>>>>>> transformed copy deduction guide since DECL_NAME is still __dguide_C >>>>>>> with TREE_TYPE C<B, T> but it should be __dguide_A with TREE_TYPE >>>>>>> A<T> >>>>>>> (equivalently C<false, T>). This ultimately results in ambiguity >>>>>>> during >>>>>>> overload resolution between the copy deduction guide vs copy ctor >>>>>>> guide. >>>>>>> >>>>>>> This patch makes us update DECL_NAME of a transformed guide >>>>>>> accordingly >>>>>>> during alias CTAD. This eventually needs to be done for inherited >>>>>>> CTAD >>>>>>> too, but it's not clear what identifier to use there since it has to >>>>>>> be >>>>>>> unique for each derived/base pair. For >>>>>>> >>>>>>> template<bool B, class T> struct A { ... }; >>>>>>> template<class T> struct B : A<false, T> { using A<false, >>>>>>> T>::A; } >>>>>>> >>>>>>> at first glance it'd be reasonable to give inherited guides a name >>>>>>> of >>>>>>> __dguide_B with TREE_TYPE A<false, T>, but since that name is >>>>>>> already >>>>>>> used B's own guides its TREE_TYPE is already B<T>. >>>>>> >>>>>> Why can't it be the same __dguide_B with TREE_TYPE B<T>? >>>>> >>>>> Ah because copy_guide_p relies on TREE_TYPE in order to recognize a copy >>>>> deduction guide, and with that TREE_TYPE it would still incorrectly >>>>> return false for an inherited copy deduction guide, e.g. >>>>> >>>>> A(A<B, T>) -> A<B, T> >>>>> >>>>> gets transformed into >>>>> >>>>> B(A<false, T>) -> B<T> >>>>> >>>>> and A<false, T> != B<T> so copy_guide_p returns false. >>>> >>>> Hmm, that seems correct; the transformed candidate is not the copy >>>> deduction >>>> guide for B. >>> >>> By https://eel.is/c++draft/over.match.class.deduct#3.4 it seems that a >>> class template can now have multiple copy deduction guides with inherited >>> CTAD: the derived class's own copy guide, along with the transformed copy >>> guides of its eligible base classes. Do we want to follow the standard >>> precisely here, or should we maybe restrict the copy-guideness propagation >>> to alias CTAD only? >> >> The latter, I think; it seems nonsensical to have multiple copy guides. > > Sounds good, so for inherited CTAD it should suffice to use __dguide_B > as the name (where B is the derived class), like so? OK. > -- >8 -- > > Subject: [PATCH] c++: alias CTAD and copy deduction guide [PR115198] > > Here we're neglecting to update DECL_NAME during the alias CTAD guide > transformation, which causes copy_guide_p to return false for the > transformed copy deduction guide since DECL_NAME is still __dguide_C > with TREE_TYPE C<B, T> but it should be __dguide_A with TREE_TYPE A<T> > (equivalently C<false, T>). This ultimately results in ambiguity during > overload resolution between the copy deduction guide vs copy ctor guide. > > This patch makes us update DECL_NAME of a transformed guide accordingly > during alias/inherited CTAD. > > PR c++/115198 > > gcc/cp/ChangeLog: > > * pt.cc (alias_ctad_tweaks): Update DECL_NAME of a transformed > guide. > > gcc/testsuite/ChangeLog: > > * g++.dg/cpp2a/class-deduction-alias22.C: New test. > --- > gcc/cp/pt.cc | 6 +++++- > .../g++.dg/cpp2a/class-deduction-alias22.C | 14 ++++++++++++++ > 2 files changed, 19 insertions(+), 1 deletion(-) > create mode 100644 gcc/testsuite/g++.dg/cpp2a/class-deduction-alias22.C > > diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc > index 607753ae6b7..daa8ac386dc 100644 > --- a/gcc/cp/pt.cc > +++ b/gcc/cp/pt.cc > @@ -30342,13 +30342,14 @@ alias_ctad_tweaks (tree tmpl, tree uguides) > any). */ > > enum { alias, inherited } ctad_kind; > - tree atype, fullatparms, utype; > + tree atype, fullatparms, utype, name; > if (TREE_CODE (tmpl) == TEMPLATE_DECL) > { > ctad_kind = alias; > atype = TREE_TYPE (tmpl); > fullatparms = DECL_TEMPLATE_PARMS (tmpl); > utype = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl)); > + name = dguide_name (tmpl); > } > else > { > @@ -30356,6 +30357,8 @@ alias_ctad_tweaks (tree tmpl, tree uguides) > atype = NULL_TREE; > fullatparms = TREE_PURPOSE (tmpl); > utype = TREE_VALUE (tmpl); > + name = dguide_name (TPARMS_PRIMARY_TEMPLATE > + (INNERMOST_TEMPLATE_PARMS (fullatparms))); > } > > tsubst_flags_t complain = tf_warning_or_error; > @@ -30451,6 +30454,7 @@ alias_ctad_tweaks (tree tmpl, tree uguides) > } > if (g == error_mark_node) > continue; > + DECL_NAME (g) = name; > if (nfparms == 0) > { > /* The targs are all non-dependent, so g isn't a template. */ > diff --git a/gcc/testsuite/g++.dg/cpp2a/class-deduction-alias22.C b/gcc/testsuite/g++.dg/cpp2a/class-deduction-alias22.C > new file mode 100644 > index 00000000000..9c6c841166a > --- /dev/null > +++ b/gcc/testsuite/g++.dg/cpp2a/class-deduction-alias22.C > @@ -0,0 +1,14 @@ > +// PR c++/115198 > +// { dg-do compile { target c++20 } } > + > +template<bool B, class T> > +struct C { > + C() = default; > + C(const C&) = default; > +}; > + > +template<class T> > +using A = C<false, T>; > + > +C<false, int> c; > +A a = c; // { dg-bogus "ambiguous" } ^ permalink raw reply [flat|nested] 9+ messages in thread
end of thread, other threads:[~2024-06-25 15:33 UTC | newest] Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- 2024-05-23 18:06 [PATCH] c++: alias CTAD and copy deduction guide [PR115198] Patrick Palka 2024-05-23 21:24 ` Jason Merrill 2024-05-23 21:42 ` Patrick Palka 2024-05-23 22:38 ` Jason Merrill 2024-06-13 15:05 ` Patrick Palka 2024-06-13 15:28 ` Jason Merrill 2024-06-13 17:00 ` Patrick Palka 2024-06-25 15:04 ` Patrick Palka 2024-06-25 15:33 ` 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).