public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [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

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).