diff mbox series

c++: NTTP type CTAD w/ tmpl from current inst [PR113649]

Message ID 20240206215955.4107411-1-ppalka@redhat.com
State New
Headers show
Series c++: NTTP type CTAD w/ tmpl from current inst [PR113649] | expand

Commit Message

Patrick Palka Feb. 6, 2024, 9:59 p.m. UTC
Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK
for trunk?

-- >8 --

Since template argument coercion happens relative to the most general
template (for a class template at least), during NTTP type CTAD we might
need to consider outer arguments particularly if the CTAD template is from
the current instantiation (and so depends on outer template parameters).

This patch makes do_class_deduction substitute as many levels of outer
template arguments into a CTAD template (from the current instantiation)
as it can take.

	PR c++/113649

gcc/cp/ChangeLog:

	* pt.cc (do_class_deduction): Add outer_targs parameter.
	Substitute outer arguments into the CTAD template.
	(do_auto_deduction): Pass outer_targs to do_class_deduction.

gcc/testsuite/ChangeLog:

	* g++.dg/cpp2a/nontype-class64.C: New test.
---
 gcc/cp/pt.cc                                 | 21 ++++++++++++++++++--
 gcc/testsuite/g++.dg/cpp2a/nontype-class64.C | 16 +++++++++++++++
 2 files changed, 35 insertions(+), 2 deletions(-)
 create mode 100644 gcc/testsuite/g++.dg/cpp2a/nontype-class64.C

Comments

Jason Merrill Feb. 7, 2024, 10 p.m. UTC | #1
On 2/6/24 16:59, Patrick Palka wrote:
> Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK
> for trunk?
> 
> -- >8 --
> 
> Since template argument coercion happens relative to the most general
> template (for a class template at least), during NTTP type CTAD we might
> need to consider outer arguments particularly if the CTAD template is from
> the current instantiation (and so depends on outer template parameters).
> 
> This patch makes do_class_deduction substitute as many levels of outer
> template arguments into a CTAD template (from the current instantiation)
> as it can take.

I guess that's a reasonable way to communicate outer_args to 
ctor_deduction_guides_for.  OK.

> 	PR c++/113649
> 
> gcc/cp/ChangeLog:
> 
> 	* pt.cc (do_class_deduction): Add outer_targs parameter.
> 	Substitute outer arguments into the CTAD template.
> 	(do_auto_deduction): Pass outer_targs to do_class_deduction.
> 
> gcc/testsuite/ChangeLog:
> 
> 	* g++.dg/cpp2a/nontype-class64.C: New test.
> ---
>   gcc/cp/pt.cc                                 | 21 ++++++++++++++++++--
>   gcc/testsuite/g++.dg/cpp2a/nontype-class64.C | 16 +++++++++++++++
>   2 files changed, 35 insertions(+), 2 deletions(-)
>   create mode 100644 gcc/testsuite/g++.dg/cpp2a/nontype-class64.C
> 
> diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc
> index 903a4a1c363..83c3b1920d6 100644
> --- a/gcc/cp/pt.cc
> +++ b/gcc/cp/pt.cc
> @@ -30681,7 +30681,7 @@ ctad_template_p (tree tmpl)
>      type.  */
>   
>   static tree
> -do_class_deduction (tree ptype, tree tmpl, tree init,
> +do_class_deduction (tree ptype, tree tmpl, tree init, tree outer_targs,
>   		    int flags, tsubst_flags_t complain)
>   {
>     /* We should have handled this in the caller.  */
> @@ -30743,6 +30743,23 @@ do_class_deduction (tree ptype, tree tmpl, tree init,
>     if (type_dependent_expression_p (init))
>       return ptype;
>   
> +  if (outer_targs)
> +    {
> +      int args_depth = TMPL_ARGS_DEPTH (outer_targs);
> +      int parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
> +      if (parms_depth > 1)
> +	{
> +	  /* Substitute outer arguments into this CTAD template from the
> +	     current instantiation.  */
> +	  int want = std::min (args_depth, parms_depth - 1);
> +	  outer_targs = strip_innermost_template_args (outer_targs,
> +						       args_depth - want);
> +	  tmpl = tsubst (tmpl, outer_targs, complain, NULL_TREE);
> +	  if (tmpl == error_mark_node)
> +	    return error_mark_node;
> +	}
> +    }
> +
>     /* Don't bother with the alias rules for an equivalent template.  */
>     tmpl = get_underlying_template (tmpl);
>   
> @@ -30998,7 +31015,7 @@ do_auto_deduction (tree type, tree init, tree auto_node,
>   
>     if (tree ctmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
>       /* C++17 class template argument deduction.  */
> -    return do_class_deduction (type, ctmpl, init, flags, complain);
> +    return do_class_deduction (type, ctmpl, init, outer_targs, flags, complain);
>   
>     if (init == NULL_TREE || TREE_TYPE (init) == NULL_TREE)
>       /* Nothing we can do with this, even in deduction context.  */
> diff --git a/gcc/testsuite/g++.dg/cpp2a/nontype-class64.C b/gcc/testsuite/g++.dg/cpp2a/nontype-class64.C
> new file mode 100644
> index 00000000000..8397ea5a886
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/cpp2a/nontype-class64.C
> @@ -0,0 +1,16 @@
> +// PR c++/113649
> +// { dg-do compile { target c++20 } }
> +
> +template<class... Args>
> +struct A {
> +  template<class Ret>
> +  struct Fun { constexpr Fun(Ret(*)(Args...)) { } };
> +
> +  template<Fun f>
> +  struct B { using type = decltype(f); };
> +};
> +
> +bool f(char, long);
> +
> +using type = A<char, long>::B<&f>::type;
> +using type = A<char, long>::Fun<bool>;
diff mbox series

Patch

diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc
index 903a4a1c363..83c3b1920d6 100644
--- a/gcc/cp/pt.cc
+++ b/gcc/cp/pt.cc
@@ -30681,7 +30681,7 @@  ctad_template_p (tree tmpl)
    type.  */
 
 static tree
-do_class_deduction (tree ptype, tree tmpl, tree init,
+do_class_deduction (tree ptype, tree tmpl, tree init, tree outer_targs,
 		    int flags, tsubst_flags_t complain)
 {
   /* We should have handled this in the caller.  */
@@ -30743,6 +30743,23 @@  do_class_deduction (tree ptype, tree tmpl, tree init,
   if (type_dependent_expression_p (init))
     return ptype;
 
+  if (outer_targs)
+    {
+      int args_depth = TMPL_ARGS_DEPTH (outer_targs);
+      int parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
+      if (parms_depth > 1)
+	{
+	  /* Substitute outer arguments into this CTAD template from the
+	     current instantiation.  */
+	  int want = std::min (args_depth, parms_depth - 1);
+	  outer_targs = strip_innermost_template_args (outer_targs,
+						       args_depth - want);
+	  tmpl = tsubst (tmpl, outer_targs, complain, NULL_TREE);
+	  if (tmpl == error_mark_node)
+	    return error_mark_node;
+	}
+    }
+
   /* Don't bother with the alias rules for an equivalent template.  */
   tmpl = get_underlying_template (tmpl);
 
@@ -30998,7 +31015,7 @@  do_auto_deduction (tree type, tree init, tree auto_node,
 
   if (tree ctmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
     /* C++17 class template argument deduction.  */
-    return do_class_deduction (type, ctmpl, init, flags, complain);
+    return do_class_deduction (type, ctmpl, init, outer_targs, flags, complain);
 
   if (init == NULL_TREE || TREE_TYPE (init) == NULL_TREE)
     /* Nothing we can do with this, even in deduction context.  */
diff --git a/gcc/testsuite/g++.dg/cpp2a/nontype-class64.C b/gcc/testsuite/g++.dg/cpp2a/nontype-class64.C
new file mode 100644
index 00000000000..8397ea5a886
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp2a/nontype-class64.C
@@ -0,0 +1,16 @@ 
+// PR c++/113649
+// { dg-do compile { target c++20 } }
+
+template<class... Args>
+struct A {
+  template<class Ret>
+  struct Fun { constexpr Fun(Ret(*)(Args...)) { } };
+
+  template<Fun f>
+  struct B { using type = decltype(f); };
+};
+
+bool f(char, long);
+
+using type = A<char, long>::B<&f>::type;
+using type = A<char, long>::Fun<bool>;