c++: CTAD from initializer list [PR106366]

Message ID 20220721124357.284497-1-ppalka@redhat.com
State New, archived
Headers
Series c++: CTAD from initializer list [PR106366] |

Commit Message

Li, Pan2 via Gcc-patches July 21, 2022, 12:43 p.m. UTC
  During CTAD, we currently perform the first phase of overload resolution
from [over.match.list] only if the class template has a list constructor.
But according to [over.match.class.deduct]/4 it should be enough to just
have a guide that looks like a list constructor (which is a more general
criterion in light of user-defined guides).

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

	PR c++/106366

gcc/cp/ChangeLog:

	* pt.cc (do_class_deduction): Don't consider TYPE_HAS_LIST_CTOR
	when setting try_list_ctor.  Reset args even when try_list_ctor
	is true and there are no list candidates.  Call resolve_args on
	the reset args.

gcc/testsuite/ChangeLog:

	* g++.dg/cpp1z/class-deduction112.C: New test.
---
 gcc/cp/pt.cc                                  | 25 +++++++++----------
 .../g++.dg/cpp1z/class-deduction112.C         | 14 +++++++++++
 2 files changed, 26 insertions(+), 13 deletions(-)
 create mode 100644 gcc/testsuite/g++.dg/cpp1z/class-deduction112.C
  

Comments

Li, Pan2 via Gcc-patches July 21, 2022, 5:06 p.m. UTC | #1
On Thu, 21 Jul 2022, Patrick Palka wrote:

> During CTAD, we currently perform the first phase of overload resolution
> from [over.match.list] only if the class template has a list constructor.
> But according to [over.match.class.deduct]/4 it should be enough to just
> have a guide that looks like a list constructor (which is a more general
> criterion in light of user-defined guides).
> 
> Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK for
> trunk?
> 
> 	PR c++/106366
> 
> gcc/cp/ChangeLog:
> 
> 	* pt.cc (do_class_deduction): Don't consider TYPE_HAS_LIST_CTOR
> 	when setting try_list_ctor.  Reset args even when try_list_ctor
> 	is true and there are no list candidates.  Call resolve_args on
> 	the reset args.
> 
> gcc/testsuite/ChangeLog:
> 
> 	* g++.dg/cpp1z/class-deduction112.C: New test.
> ---
>  gcc/cp/pt.cc                                  | 25 +++++++++----------
>  .../g++.dg/cpp1z/class-deduction112.C         | 14 +++++++++++
>  2 files changed, 26 insertions(+), 13 deletions(-)
>  create mode 100644 gcc/testsuite/g++.dg/cpp1z/class-deduction112.C
> 
> diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc
> index 718dfa5bfa8..0f26d6f5bce 100644
> --- a/gcc/cp/pt.cc
> +++ b/gcc/cp/pt.cc
> @@ -30250,8 +30250,8 @@ do_class_deduction (tree ptype, tree tmpl, tree init,
>    else if (BRACE_ENCLOSED_INITIALIZER_P (init))
>      {
>        list_init_p = true;
> -      try_list_ctor = TYPE_HAS_LIST_CTOR (type);
> -      if (try_list_ctor && CONSTRUCTOR_NELTS (init) == 1
> +      try_list_ctor = true;

I suppose try_list_cand would be a more appropriate name for this
variable now, consider that fixed.

> +      if (CONSTRUCTOR_NELTS (init) == 1
>  	  && !CONSTRUCTOR_IS_DESIGNATED_INIT (init))
>  	{
>  	  /* As an exception, the first phase in 16.3.1.7 (considering the
> @@ -30310,26 +30310,25 @@ do_class_deduction (tree ptype, tree tmpl, tree init,
>  
>    tree fndecl = error_mark_node;
>  
> -  /* If this is list-initialization and the class has a list constructor, first
> +  /* If this is list-initialization and the class has a list guide, first
>       try deducing from the list as a single argument, as [over.match.list].  */
> -  tree list_cands = NULL_TREE;
> -  if (try_list_ctor && cands)
> -    for (lkp_iterator iter (cands); iter; ++iter)
> -      {
> -	tree dg = *iter;
> +  if (try_list_ctor)
> +    {
> +      tree list_cands = NULL_TREE;
> +      for (tree dg : lkp_range (cands))
>  	if (is_list_ctor (dg))
>  	  list_cands = lookup_add (dg, list_cands);
> -      }
> -  if (list_cands)
> -    {
> -      fndecl = perform_dguide_overload_resolution (list_cands, args, tf_none);
> -
> +      if (list_cands)
> +	fndecl = perform_dguide_overload_resolution (list_cands, args, tf_none);
>        if (fndecl == error_mark_node)
>  	{
>  	  /* That didn't work, now try treating the list as a sequence of
>  	     arguments.  */
>  	  release_tree_vector (args);
>  	  args = make_tree_vector_from_ctor (init);
> +	  args = resolve_args (args, complain);
> +	  if (args == NULL)
> +	    return error_mark_node;
>  	}
>      }
>  
> diff --git a/gcc/testsuite/g++.dg/cpp1z/class-deduction112.C b/gcc/testsuite/g++.dg/cpp1z/class-deduction112.C
> new file mode 100644
> index 00000000000..8da5868ff98
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/cpp1z/class-deduction112.C
> @@ -0,0 +1,14 @@
> +// PR c++/106366
> +// { dg-do compile { target c++17 } }
> +
> +#include <initializer_list>
> +
> +template<class T>
> +struct A { A(...); };
> +
> +template<typename T>
> +A(std::initializer_list<T>) -> A<T>;
> +
> +A a{1,2,3};
> +using type = decltype(a);
> +using type = A<int>;
> -- 
> 2.37.1.208.ge72d93e88c
> 
>
  
Li, Pan2 via Gcc-patches July 22, 2022, 9:32 p.m. UTC | #2
On 7/21/22 08:43, Patrick Palka wrote:
> During CTAD, we currently perform the first phase of overload resolution
> from [over.match.list] only if the class template has a list constructor.
> But according to [over.match.class.deduct]/4 it should be enough to just
> have a guide that looks like a list constructor (which is a more general
> criterion in light of user-defined guides).
> 
> Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK for
> trunk?

OK.

> 	PR c++/106366
> 
> gcc/cp/ChangeLog:
> 
> 	* pt.cc (do_class_deduction): Don't consider TYPE_HAS_LIST_CTOR
> 	when setting try_list_ctor.  Reset args even when try_list_ctor
> 	is true and there are no list candidates.  Call resolve_args on
> 	the reset args.
> 
> gcc/testsuite/ChangeLog:
> 
> 	* g++.dg/cpp1z/class-deduction112.C: New test.
> ---
>   gcc/cp/pt.cc                                  | 25 +++++++++----------
>   .../g++.dg/cpp1z/class-deduction112.C         | 14 +++++++++++
>   2 files changed, 26 insertions(+), 13 deletions(-)
>   create mode 100644 gcc/testsuite/g++.dg/cpp1z/class-deduction112.C
> 
> diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc
> index 718dfa5bfa8..0f26d6f5bce 100644
> --- a/gcc/cp/pt.cc
> +++ b/gcc/cp/pt.cc
> @@ -30250,8 +30250,8 @@ do_class_deduction (tree ptype, tree tmpl, tree init,
>     else if (BRACE_ENCLOSED_INITIALIZER_P (init))
>       {
>         list_init_p = true;
> -      try_list_ctor = TYPE_HAS_LIST_CTOR (type);
> -      if (try_list_ctor && CONSTRUCTOR_NELTS (init) == 1
> +      try_list_ctor = true;
> +      if (CONSTRUCTOR_NELTS (init) == 1
>   	  && !CONSTRUCTOR_IS_DESIGNATED_INIT (init))
>   	{
>   	  /* As an exception, the first phase in 16.3.1.7 (considering the
> @@ -30310,26 +30310,25 @@ do_class_deduction (tree ptype, tree tmpl, tree init,
>   
>     tree fndecl = error_mark_node;
>   
> -  /* If this is list-initialization and the class has a list constructor, first
> +  /* If this is list-initialization and the class has a list guide, first
>        try deducing from the list as a single argument, as [over.match.list].  */
> -  tree list_cands = NULL_TREE;
> -  if (try_list_ctor && cands)
> -    for (lkp_iterator iter (cands); iter; ++iter)
> -      {
> -	tree dg = *iter;
> +  if (try_list_ctor)
> +    {
> +      tree list_cands = NULL_TREE;
> +      for (tree dg : lkp_range (cands))
>   	if (is_list_ctor (dg))
>   	  list_cands = lookup_add (dg, list_cands);
> -      }
> -  if (list_cands)
> -    {
> -      fndecl = perform_dguide_overload_resolution (list_cands, args, tf_none);
> -
> +      if (list_cands)
> +	fndecl = perform_dguide_overload_resolution (list_cands, args, tf_none);
>         if (fndecl == error_mark_node)
>   	{
>   	  /* That didn't work, now try treating the list as a sequence of
>   	     arguments.  */
>   	  release_tree_vector (args);
>   	  args = make_tree_vector_from_ctor (init);
> +	  args = resolve_args (args, complain);
> +	  if (args == NULL)
> +	    return error_mark_node;
>   	}
>       }
>   
> diff --git a/gcc/testsuite/g++.dg/cpp1z/class-deduction112.C b/gcc/testsuite/g++.dg/cpp1z/class-deduction112.C
> new file mode 100644
> index 00000000000..8da5868ff98
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/cpp1z/class-deduction112.C
> @@ -0,0 +1,14 @@
> +// PR c++/106366
> +// { dg-do compile { target c++17 } }
> +
> +#include <initializer_list>
> +
> +template<class T>
> +struct A { A(...); };
> +
> +template<typename T>
> +A(std::initializer_list<T>) -> A<T>;
> +
> +A a{1,2,3};
> +using type = decltype(a);
> +using type = A<int>;
  

Patch

diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc
index 718dfa5bfa8..0f26d6f5bce 100644
--- a/gcc/cp/pt.cc
+++ b/gcc/cp/pt.cc
@@ -30250,8 +30250,8 @@  do_class_deduction (tree ptype, tree tmpl, tree init,
   else if (BRACE_ENCLOSED_INITIALIZER_P (init))
     {
       list_init_p = true;
-      try_list_ctor = TYPE_HAS_LIST_CTOR (type);
-      if (try_list_ctor && CONSTRUCTOR_NELTS (init) == 1
+      try_list_ctor = true;
+      if (CONSTRUCTOR_NELTS (init) == 1
 	  && !CONSTRUCTOR_IS_DESIGNATED_INIT (init))
 	{
 	  /* As an exception, the first phase in 16.3.1.7 (considering the
@@ -30310,26 +30310,25 @@  do_class_deduction (tree ptype, tree tmpl, tree init,
 
   tree fndecl = error_mark_node;
 
-  /* If this is list-initialization and the class has a list constructor, first
+  /* If this is list-initialization and the class has a list guide, first
      try deducing from the list as a single argument, as [over.match.list].  */
-  tree list_cands = NULL_TREE;
-  if (try_list_ctor && cands)
-    for (lkp_iterator iter (cands); iter; ++iter)
-      {
-	tree dg = *iter;
+  if (try_list_ctor)
+    {
+      tree list_cands = NULL_TREE;
+      for (tree dg : lkp_range (cands))
 	if (is_list_ctor (dg))
 	  list_cands = lookup_add (dg, list_cands);
-      }
-  if (list_cands)
-    {
-      fndecl = perform_dguide_overload_resolution (list_cands, args, tf_none);
-
+      if (list_cands)
+	fndecl = perform_dguide_overload_resolution (list_cands, args, tf_none);
       if (fndecl == error_mark_node)
 	{
 	  /* That didn't work, now try treating the list as a sequence of
 	     arguments.  */
 	  release_tree_vector (args);
 	  args = make_tree_vector_from_ctor (init);
+	  args = resolve_args (args, complain);
+	  if (args == NULL)
+	    return error_mark_node;
 	}
     }
 
diff --git a/gcc/testsuite/g++.dg/cpp1z/class-deduction112.C b/gcc/testsuite/g++.dg/cpp1z/class-deduction112.C
new file mode 100644
index 00000000000..8da5868ff98
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp1z/class-deduction112.C
@@ -0,0 +1,14 @@ 
+// PR c++/106366
+// { dg-do compile { target c++17 } }
+
+#include <initializer_list>
+
+template<class T>
+struct A { A(...); };
+
+template<typename T>
+A(std::initializer_list<T>) -> A<T>;
+
+A a{1,2,3};
+using type = decltype(a);
+using type = A<int>;