public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] c++: Fix CTAD from single-element initializer list [PR99103]
@ 2021-02-15 19:30 Patrick Palka
  2021-02-25 16:02 ` Jason Merrill
  0 siblings, 1 reply; 2+ messages in thread
From: Patrick Palka @ 2021-02-15 19:30 UTC (permalink / raw)
  To: gcc-patches

When determining whether to rule out initializer-list constructors
during CTAD with a single-element initializer list, the element type's
cv-qualifiers should be irrelevant.  This patch fixes this by making
is_spec_or_derived strip cv-qualifiers from the supplied expression
type.

In passing, I noticed in maybe_aggr_guide we were calling
is_spec_or_derived with swapped arguments.  This led us to prefer the
aggregate deduction candidate over copying deduction in the second
testcase below with -std=c++20.

Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK for
trunk?

gcc/cp/ChangeLog:

	PR c++/99103
	* pt.c (is_spec_or_derived): Drop cv-qualifiers from 'etype'.
	(maybe_aggr_guide): Fix order of arguments to
	is_spec_or_derived.

gcc/testsuite/ChangeLog:

	PR c++/99103
	* g++.dg/cpp1z/class-deduction79.C: New test.
	* g++.dg/cpp1z/class-deduction80.C: New test.
---
 gcc/cp/pt.c                                    |  3 ++-
 gcc/testsuite/g++.dg/cpp1z/class-deduction79.C | 10 ++++++++++
 gcc/testsuite/g++.dg/cpp1z/class-deduction80.C |  9 +++++++++
 3 files changed, 21 insertions(+), 1 deletion(-)
 create mode 100644 gcc/testsuite/g++.dg/cpp1z/class-deduction79.C
 create mode 100644 gcc/testsuite/g++.dg/cpp1z/class-deduction80.C

diff --git a/gcc/cp/pt.c b/gcc/cp/pt.c
index 5102bf02d0f..1acb5b3a097 100644
--- a/gcc/cp/pt.c
+++ b/gcc/cp/pt.c
@@ -28829,6 +28829,7 @@ is_spec_or_derived (tree etype, tree tmpl)
   if (!etype || !CLASS_TYPE_P (etype))
     return false;
 
+  etype = cv_unqualified (etype);
   tree type = TREE_TYPE (tmpl);
   tree tparms = (INNERMOST_TEMPLATE_PARMS
 		 (DECL_TEMPLATE_PARMS (tmpl)));
@@ -28859,7 +28860,7 @@ maybe_aggr_guide (tree tmpl, tree init, vec<tree,va_gc> *args)
   if (args->length() == 1)
     {
       tree val = (*args)[0];
-      if (is_spec_or_derived (tmpl, TREE_TYPE (val)))
+      if (is_spec_or_derived (TREE_TYPE (val), tmpl))
 	return NULL_TREE;
     }
 
diff --git a/gcc/testsuite/g++.dg/cpp1z/class-deduction79.C b/gcc/testsuite/g++.dg/cpp1z/class-deduction79.C
new file mode 100644
index 00000000000..ad0ba9bb8f5
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp1z/class-deduction79.C
@@ -0,0 +1,10 @@
+// PR c++/99103
+// { dg-do compile { target c++17 } }
+#include <initializer_list>
+
+template <class T>
+struct S { S(std::initializer_list<T>); };
+
+extern const S<int> x;
+using T = decltype(S{x});
+using T = S<int>; // not S<S<int>>
diff --git a/gcc/testsuite/g++.dg/cpp1z/class-deduction80.C b/gcc/testsuite/g++.dg/cpp1z/class-deduction80.C
new file mode 100644
index 00000000000..3dd7cb5890b
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp1z/class-deduction80.C
@@ -0,0 +1,9 @@
+// PR c++/99103
+// { dg-do compile { target c++17 } }
+
+template <class T> struct X { T a; };
+template <class T> struct Y : X<T> {};
+
+extern const Y<int> y;
+using T = decltype(X{y});
+using T = X<int>; // not X<Y<int>>
-- 
2.30.1.489.g328c109303


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH] c++: Fix CTAD from single-element initializer list [PR99103]
  2021-02-15 19:30 [PATCH] c++: Fix CTAD from single-element initializer list [PR99103] Patrick Palka
@ 2021-02-25 16:02 ` Jason Merrill
  0 siblings, 0 replies; 2+ messages in thread
From: Jason Merrill @ 2021-02-25 16:02 UTC (permalink / raw)
  To: Patrick Palka, gcc-patches

On 2/15/21 2:30 PM, Patrick Palka wrote:
> When determining whether to rule out initializer-list constructors
> during CTAD with a single-element initializer list, the element type's
> cv-qualifiers should be irrelevant.  This patch fixes this by making
> is_spec_or_derived strip cv-qualifiers from the supplied expression
> type.
> 
> In passing, I noticed in maybe_aggr_guide we were calling
> is_spec_or_derived with swapped arguments.  This led us to prefer the
> aggregate deduction candidate over copying deduction in the second
> testcase below with -std=c++20.
> 
> Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK for
> trunk?

OK.

> gcc/cp/ChangeLog:
> 
> 	PR c++/99103
> 	* pt.c (is_spec_or_derived): Drop cv-qualifiers from 'etype'.
> 	(maybe_aggr_guide): Fix order of arguments to
> 	is_spec_or_derived.
> 
> gcc/testsuite/ChangeLog:
> 
> 	PR c++/99103
> 	* g++.dg/cpp1z/class-deduction79.C: New test.
> 	* g++.dg/cpp1z/class-deduction80.C: New test.
> ---
>   gcc/cp/pt.c                                    |  3 ++-
>   gcc/testsuite/g++.dg/cpp1z/class-deduction79.C | 10 ++++++++++
>   gcc/testsuite/g++.dg/cpp1z/class-deduction80.C |  9 +++++++++
>   3 files changed, 21 insertions(+), 1 deletion(-)
>   create mode 100644 gcc/testsuite/g++.dg/cpp1z/class-deduction79.C
>   create mode 100644 gcc/testsuite/g++.dg/cpp1z/class-deduction80.C
> 
> diff --git a/gcc/cp/pt.c b/gcc/cp/pt.c
> index 5102bf02d0f..1acb5b3a097 100644
> --- a/gcc/cp/pt.c
> +++ b/gcc/cp/pt.c
> @@ -28829,6 +28829,7 @@ is_spec_or_derived (tree etype, tree tmpl)
>     if (!etype || !CLASS_TYPE_P (etype))
>       return false;
>   
> +  etype = cv_unqualified (etype);
>     tree type = TREE_TYPE (tmpl);
>     tree tparms = (INNERMOST_TEMPLATE_PARMS
>   		 (DECL_TEMPLATE_PARMS (tmpl)));
> @@ -28859,7 +28860,7 @@ maybe_aggr_guide (tree tmpl, tree init, vec<tree,va_gc> *args)
>     if (args->length() == 1)
>       {
>         tree val = (*args)[0];
> -      if (is_spec_or_derived (tmpl, TREE_TYPE (val)))
> +      if (is_spec_or_derived (TREE_TYPE (val), tmpl))
>   	return NULL_TREE;
>       }
>   
> diff --git a/gcc/testsuite/g++.dg/cpp1z/class-deduction79.C b/gcc/testsuite/g++.dg/cpp1z/class-deduction79.C
> new file mode 100644
> index 00000000000..ad0ba9bb8f5
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/cpp1z/class-deduction79.C
> @@ -0,0 +1,10 @@
> +// PR c++/99103
> +// { dg-do compile { target c++17 } }
> +#include <initializer_list>
> +
> +template <class T>
> +struct S { S(std::initializer_list<T>); };
> +
> +extern const S<int> x;
> +using T = decltype(S{x});
> +using T = S<int>; // not S<S<int>>
> diff --git a/gcc/testsuite/g++.dg/cpp1z/class-deduction80.C b/gcc/testsuite/g++.dg/cpp1z/class-deduction80.C
> new file mode 100644
> index 00000000000..3dd7cb5890b
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/cpp1z/class-deduction80.C
> @@ -0,0 +1,9 @@
> +// PR c++/99103
> +// { dg-do compile { target c++17 } }
> +
> +template <class T> struct X { T a; };
> +template <class T> struct Y : X<T> {};
> +
> +extern const Y<int> y;
> +using T = decltype(X{y});
> +using T = X<int>; // not X<Y<int>>
> 


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2021-02-25 16:02 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-02-15 19:30 [PATCH] c++: Fix CTAD from single-element initializer list [PR99103] Patrick Palka
2021-02-25 16:02 ` 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).