diff mbox series

c++: partial ordering with dependent NTTP type [PR105289]

Message ID 20220422175032.3106274-1-ppalka@redhat.com
State New
Headers show
Series c++: partial ordering with dependent NTTP type [PR105289] | expand

Commit Message

Patrick Palka April 22, 2022, 5:50 p.m. UTC
Here ever since r11-6483-ge2e2f3f2c9400f we're rejecting and crashing
(respectively) on two testcases that we used to accept in C++17 mode.
Both testcases declare partial specializations for which the primary
template contains an NTTP with dependent type, but the correctness of
these partial specializations is unclear according to PR86193.

This patch restores the previous C++17 behavior for such partial
specializations by restricting the r11-6483 change to just ordinary
deduction as opposed to deduction for sake of partial ordering.

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

	PR c++/105289
	PR c++/86193

gcc/cp/ChangeLog:

	* pt.cc (unify) <case TEMPLATE_PARM_INDEX>: Restrict the
	r11-6483 change to just ordinary deduction for function
	templates.  When substituting into the NTTP type the second
	time, use the original type not the substituted type.  Remove
	now unnecessary level check.

gcc/testsuite/ChangeLog:

	* g++.dg/template/partial5.C: Revert r11-6483 change.
	* g++.dg/template/partial-specialization11.C: New test.
	* g++.dg/template/partial-specialization12.C: New test.
---
 gcc/cp/pt.cc                                  | 25 ++++++++++++-------
 .../template/partial-specialization11.C       | 10 ++++++++
 .../template/partial-specialization12.C       | 12 +++++++++
 gcc/testsuite/g++.dg/template/partial5.C      |  2 +-
 4 files changed, 39 insertions(+), 10 deletions(-)
 create mode 100644 gcc/testsuite/g++.dg/template/partial-specialization11.C
 create mode 100644 gcc/testsuite/g++.dg/template/partial-specialization12.C

Comments

Patrick Palka April 22, 2022, 7:27 p.m. UTC | #1
On Fri, 22 Apr 2022, Patrick Palka wrote:

> Here ever since r11-6483-ge2e2f3f2c9400f we're rejecting and crashing
> (respectively) on two testcases that we used to accept in C++17 mode.
> Both testcases declare partial specializations for which the primary
> template contains an NTTP with dependent type, but the correctness of
> these partial specializations is unclear according to PR86193.
> 
> This patch restores the previous C++17 behavior for such partial
> specializations by restricting the r11-6483 change to just ordinary
> deduction as opposed to deduction for sake of partial ordering.

Note that if we're okay with rejecting such partial specializations even
in C++17 mode (and thus deeming PR105289 to be ICE-on-invalid instead of
ICE-on-valid), then the fix for the reported ICE is just:

diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc
index dde62ee052d..6d65f6ad3cf 100644
--- a/gcc/cp/pt.cc
+++ b/gcc/cp/pt.cc
@@ -24299,7 +24299,7 @@ unify (tree tparms, tree targs, tree parm, tree arg, int strict,
 	      /* Now check whether the type of this parameter is still
 		 dependent, and give up if so.  */
 	      ++processing_template_decl;
-	      tparm = tsubst (tparm, targs, tf_none, NULL_TREE);
+	      tparm = tsubst (TREE_TYPE (parm), targs, tf_none, NULL_TREE);
 	      --processing_template_decl;
 	      if (uses_template_parms (tparm))
 		return unify_success (explain_p);

i.e. we need to substitute into the original NTTP type, not into the
already substituted NTTP type.

> 
> Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK for
> trunk/11?
> 
> 	PR c++/105289
> 	PR c++/86193
> 
> gcc/cp/ChangeLog:
> 
> 	* pt.cc (unify) <case TEMPLATE_PARM_INDEX>: Restrict the
> 	r11-6483 change to just ordinary deduction for function
> 	templates.  When substituting into the NTTP type the second
> 	time, use the original type not the substituted type.  Remove
> 	now unnecessary level check.
> 
> gcc/testsuite/ChangeLog:
> 
> 	* g++.dg/template/partial5.C: Revert r11-6483 change.
> 	* g++.dg/template/partial-specialization11.C: New test.
> 	* g++.dg/template/partial-specialization12.C: New test.
> ---
>  gcc/cp/pt.cc                                  | 25 ++++++++++++-------
>  .../template/partial-specialization11.C       | 10 ++++++++
>  .../template/partial-specialization12.C       | 12 +++++++++
>  gcc/testsuite/g++.dg/template/partial5.C      |  2 +-
>  4 files changed, 39 insertions(+), 10 deletions(-)
>  create mode 100644 gcc/testsuite/g++.dg/template/partial-specialization11.C
>  create mode 100644 gcc/testsuite/g++.dg/template/partial-specialization12.C
> 
> diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc
> index dde62ee052d..52bd130b7e7 100644
> --- a/gcc/cp/pt.cc
> +++ b/gcc/cp/pt.cc
> @@ -24287,8 +24287,7 @@ unify (tree tparms, tree targs, tree parm, tree arg, int strict,
>  	  /* We haven't deduced the type of this parameter yet.  */
>  	  if (cxx_dialect >= cxx17
>  	      /* We deduce from array bounds in try_array_deduction.  */
> -	      && !(strict & UNIFY_ALLOW_INTEGER)
> -	      && TEMPLATE_PARM_LEVEL (parm) <= TMPL_ARGS_DEPTH (targs))
> +	      && !(strict & UNIFY_ALLOW_INTEGER))
>  	    {
>  	      /* Deduce it from the non-type argument.  As above, ignore
>  		 top-level quals here too.  */
> @@ -24296,13 +24295,21 @@ unify (tree tparms, tree targs, tree parm, tree arg, int strict,
>  	      RECUR_AND_CHECK_FAILURE (tparms, targs,
>  				       tparm, atype,
>  				       UNIFY_ALLOW_NONE, explain_p);
> -	      /* Now check whether the type of this parameter is still
> -		 dependent, and give up if so.  */
> -	      ++processing_template_decl;
> -	      tparm = tsubst (tparm, targs, tf_none, NULL_TREE);
> -	      --processing_template_decl;
> -	      if (uses_template_parms (tparm))
> -		return unify_success (explain_p);
> +	      if (!processing_template_decl
> +		  && TPARMS_PRIMARY_TEMPLATE (tparms)
> +		  && DECL_FUNCTION_TEMPLATE_P (TPARMS_PRIMARY_TEMPLATE
> +					       (tparms)))
> +		{
> +		  /* If the NTTP's type uses still-undeduced template
> +		     parameters, then don't unify it now.  This gives
> +		     type_unification_real a chance to retry deduction
> +		     with default template arguments substituted in.  */
> +		  ++processing_template_decl;
> +		  tparm = tsubst (TREE_TYPE (parm), targs, tf_none, NULL_TREE);
> +		  --processing_template_decl;
> +		  if (uses_template_parms (tparm))
> +		    return unify_success (explain_p);
> +		}
>  	    }
>  	  else
>  	    /* Try again later.  */
> diff --git a/gcc/testsuite/g++.dg/template/partial-specialization11.C b/gcc/testsuite/g++.dg/template/partial-specialization11.C
> new file mode 100644
> index 00000000000..20da407d422
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/template/partial-specialization11.C
> @@ -0,0 +1,10 @@
> +// PR c++/105289
> +
> +template<class T> struct value_type;
> +
> +template<class T, typename value_type<T>::type V>
> +struct push_front_vlist;
> +
> +template<class T, int V>
> +struct push_front_vlist<T*, V> { };
> +// { dg-error "not more specialized" "PR86193" { target c++14_down } .-1 }
> diff --git a/gcc/testsuite/g++.dg/template/partial-specialization12.C b/gcc/testsuite/g++.dg/template/partial-specialization12.C
> new file mode 100644
> index 00000000000..d70f7592790
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/template/partial-specialization12.C
> @@ -0,0 +1,12 @@
> +// PR c++/105289
> +// { dg-do compile { target c++11 } }
> +
> +template<class T>
> +struct value_type;
> +
> +template <class List, typename value_type<List>::type Element>
> +struct push_front_vlist;
> +
> +template <template <class X, X...> class XList, class T, T Arg, T... Vs>
> +struct push_front_vlist<XList<T, Vs...>, Arg> { };
> +// { dg-error "not more specialized" "PR86193" { target c++14_down } .-1 }
> diff --git a/gcc/testsuite/g++.dg/template/partial5.C b/gcc/testsuite/g++.dg/template/partial5.C
> index 037f684cbd2..7492632f39b 100644
> --- a/gcc/testsuite/g++.dg/template/partial5.C
> +++ b/gcc/testsuite/g++.dg/template/partial5.C
> @@ -14,7 +14,7 @@ template<typename T, typename T::foo V>
>  struct Y { };
>  
>  template<typename T, typename U, U v>
> -struct Y<T, v> { }; // { dg-error "" }
> +struct Y<T, v> { }; // { dg-error "" "PR86193" { target c++14_down } }
>  
>  
>  template<typename T, T V>
> -- 
> 2.36.0.rc2.10.g1ac7422e39
> 
>
Jason Merrill April 25, 2022, 3:12 p.m. UTC | #2
On 4/22/22 15:27, Patrick Palka wrote:
> On Fri, 22 Apr 2022, Patrick Palka wrote:
> 
>> Here ever since r11-6483-ge2e2f3f2c9400f we're rejecting and crashing
>> (respectively) on two testcases that we used to accept in C++17 mode.
>> Both testcases declare partial specializations for which the primary
>> template contains an NTTP with dependent type, but the correctness of
>> these partial specializations is unclear according to PR86193.
>>
>> This patch restores the previous C++17 behavior for such partial
>> specializations by restricting the r11-6483 change to just ordinary
>> deduction as opposed to deduction for sake of partial ordering.
> 
> Note that if we're okay with rejecting such partial specializations even
> in C++17 mode (and thus deeming PR105289 to be ICE-on-invalid instead of
> ICE-on-valid), then the fix for the reported ICE is just:
> 
> diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc
> index dde62ee052d..6d65f6ad3cf 100644
> --- a/gcc/cp/pt.cc
> +++ b/gcc/cp/pt.cc
> @@ -24299,7 +24299,7 @@ unify (tree tparms, tree targs, tree parm, tree arg, int strict,
>   	      /* Now check whether the type of this parameter is still
>   		 dependent, and give up if so.  */
>   	      ++processing_template_decl;
> -	      tparm = tsubst (tparm, targs, tf_none, NULL_TREE);
> +	      tparm = tsubst (TREE_TYPE (parm), targs, tf_none, NULL_TREE);
>   	      --processing_template_decl;
>   	      if (uses_template_parms (tparm))
>   		return unify_success (explain_p);
> 
> i.e. we need to substitute into the original NTTP type, not into the
> already substituted NTTP type.

I'm happy rejecting partial-specialization12.C on that basis.  11 is 
interesting because int is a non-dependent type; it might be worth 
adding that testcase to the DR455 discussion.

I think let's go with this patch and bump down the "partial 
specialization isn't more specialized" diagnostic from permerror to 
on-by-default pedwarn.

>> Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK for
>> trunk/11?
>>
>> 	PR c++/105289
>> 	PR c++/86193
>>
>> gcc/cp/ChangeLog:
>>
>> 	* pt.cc (unify) <case TEMPLATE_PARM_INDEX>: Restrict the
>> 	r11-6483 change to just ordinary deduction for function
>> 	templates.  When substituting into the NTTP type the second
>> 	time, use the original type not the substituted type.  Remove
>> 	now unnecessary level check.
>>
>> gcc/testsuite/ChangeLog:
>>
>> 	* g++.dg/template/partial5.C: Revert r11-6483 change.
>> 	* g++.dg/template/partial-specialization11.C: New test.
>> 	* g++.dg/template/partial-specialization12.C: New test.
>> ---
>>   gcc/cp/pt.cc                                  | 25 ++++++++++++-------
>>   .../template/partial-specialization11.C       | 10 ++++++++
>>   .../template/partial-specialization12.C       | 12 +++++++++
>>   gcc/testsuite/g++.dg/template/partial5.C      |  2 +-
>>   4 files changed, 39 insertions(+), 10 deletions(-)
>>   create mode 100644 gcc/testsuite/g++.dg/template/partial-specialization11.C
>>   create mode 100644 gcc/testsuite/g++.dg/template/partial-specialization12.C
>>
>> diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc
>> index dde62ee052d..52bd130b7e7 100644
>> --- a/gcc/cp/pt.cc
>> +++ b/gcc/cp/pt.cc
>> @@ -24287,8 +24287,7 @@ unify (tree tparms, tree targs, tree parm, tree arg, int strict,
>>   	  /* We haven't deduced the type of this parameter yet.  */
>>   	  if (cxx_dialect >= cxx17
>>   	      /* We deduce from array bounds in try_array_deduction.  */
>> -	      && !(strict & UNIFY_ALLOW_INTEGER)
>> -	      && TEMPLATE_PARM_LEVEL (parm) <= TMPL_ARGS_DEPTH (targs))
>> +	      && !(strict & UNIFY_ALLOW_INTEGER))
>>   	    {
>>   	      /* Deduce it from the non-type argument.  As above, ignore
>>   		 top-level quals here too.  */
>> @@ -24296,13 +24295,21 @@ unify (tree tparms, tree targs, tree parm, tree arg, int strict,
>>   	      RECUR_AND_CHECK_FAILURE (tparms, targs,
>>   				       tparm, atype,
>>   				       UNIFY_ALLOW_NONE, explain_p);
>> -	      /* Now check whether the type of this parameter is still
>> -		 dependent, and give up if so.  */
>> -	      ++processing_template_decl;
>> -	      tparm = tsubst (tparm, targs, tf_none, NULL_TREE);
>> -	      --processing_template_decl;
>> -	      if (uses_template_parms (tparm))
>> -		return unify_success (explain_p);
>> +	      if (!processing_template_decl
>> +		  && TPARMS_PRIMARY_TEMPLATE (tparms)
>> +		  && DECL_FUNCTION_TEMPLATE_P (TPARMS_PRIMARY_TEMPLATE
>> +					       (tparms)))
>> +		{
>> +		  /* If the NTTP's type uses still-undeduced template
>> +		     parameters, then don't unify it now.  This gives
>> +		     type_unification_real a chance to retry deduction
>> +		     with default template arguments substituted in.  */
>> +		  ++processing_template_decl;
>> +		  tparm = tsubst (TREE_TYPE (parm), targs, tf_none, NULL_TREE);
>> +		  --processing_template_decl;
>> +		  if (uses_template_parms (tparm))
>> +		    return unify_success (explain_p);
>> +		}
>>   	    }
>>   	  else
>>   	    /* Try again later.  */
>> diff --git a/gcc/testsuite/g++.dg/template/partial-specialization11.C b/gcc/testsuite/g++.dg/template/partial-specialization11.C
>> new file mode 100644
>> index 00000000000..20da407d422
>> --- /dev/null
>> +++ b/gcc/testsuite/g++.dg/template/partial-specialization11.C
>> @@ -0,0 +1,10 @@
>> +// PR c++/105289
>> +
>> +template<class T> struct value_type;
>> +
>> +template<class T, typename value_type<T>::type V>
>> +struct push_front_vlist;
>> +
>> +template<class T, int V>
>> +struct push_front_vlist<T*, V> { };
>> +// { dg-error "not more specialized" "PR86193" { target c++14_down } .-1 }
>> diff --git a/gcc/testsuite/g++.dg/template/partial-specialization12.C b/gcc/testsuite/g++.dg/template/partial-specialization12.C
>> new file mode 100644
>> index 00000000000..d70f7592790
>> --- /dev/null
>> +++ b/gcc/testsuite/g++.dg/template/partial-specialization12.C
>> @@ -0,0 +1,12 @@
>> +// PR c++/105289
>> +// { dg-do compile { target c++11 } }
>> +
>> +template<class T>
>> +struct value_type;
>> +
>> +template <class List, typename value_type<List>::type Element>
>> +struct push_front_vlist;
>> +
>> +template <template <class X, X...> class XList, class T, T Arg, T... Vs>
>> +struct push_front_vlist<XList<T, Vs...>, Arg> { };
>> +// { dg-error "not more specialized" "PR86193" { target c++14_down } .-1 }
>> diff --git a/gcc/testsuite/g++.dg/template/partial5.C b/gcc/testsuite/g++.dg/template/partial5.C
>> index 037f684cbd2..7492632f39b 100644
>> --- a/gcc/testsuite/g++.dg/template/partial5.C
>> +++ b/gcc/testsuite/g++.dg/template/partial5.C
>> @@ -14,7 +14,7 @@ template<typename T, typename T::foo V>
>>   struct Y { };
>>   
>>   template<typename T, typename U, U v>
>> -struct Y<T, v> { }; // { dg-error "" }
>> +struct Y<T, v> { }; // { dg-error "" "PR86193" { target c++14_down } }
>>   
>>   
>>   template<typename T, T V>
>> -- 
>> 2.36.0.rc2.10.g1ac7422e39
>>
>>
>
Patrick Palka April 25, 2022, 6:10 p.m. UTC | #3
On Mon, 25 Apr 2022, Jason Merrill wrote:

> On 4/22/22 15:27, Patrick Palka wrote:
> > On Fri, 22 Apr 2022, Patrick Palka wrote:
> > 
> > > Here ever since r11-6483-ge2e2f3f2c9400f we're rejecting and crashing
> > > (respectively) on two testcases that we used to accept in C++17 mode.
> > > Both testcases declare partial specializations for which the primary
> > > template contains an NTTP with dependent type, but the correctness of
> > > these partial specializations is unclear according to PR86193.
> > > 
> > > This patch restores the previous C++17 behavior for such partial
> > > specializations by restricting the r11-6483 change to just ordinary
> > > deduction as opposed to deduction for sake of partial ordering.
> > 
> > Note that if we're okay with rejecting such partial specializations even
> > in C++17 mode (and thus deeming PR105289 to be ICE-on-invalid instead of
> > ICE-on-valid), then the fix for the reported ICE is just:
> > 
> > diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc
> > index dde62ee052d..6d65f6ad3cf 100644
> > --- a/gcc/cp/pt.cc
> > +++ b/gcc/cp/pt.cc
> > @@ -24299,7 +24299,7 @@ unify (tree tparms, tree targs, tree parm, tree arg,
> > int strict,
> >   	      /* Now check whether the type of this parameter is still
> >   		 dependent, and give up if so.  */
> >   	      ++processing_template_decl;
> > -	      tparm = tsubst (tparm, targs, tf_none, NULL_TREE);
> > +	      tparm = tsubst (TREE_TYPE (parm), targs, tf_none, NULL_TREE);
> >   	      --processing_template_decl;
> >   	      if (uses_template_parms (tparm))
> >   		return unify_success (explain_p);
> > 
> > i.e. we need to substitute into the original NTTP type, not into the
> > already substituted NTTP type.
> 
> I'm happy rejecting partial-specialization12.C on that basis.  11 is
> interesting because int is a non-dependent type; it might be worth adding that
> testcase to the DR455 discussion.
> 
> I think let's go with this patch and bump down the "partial specialization
> isn't more specialized" diagnostic from permerror to on-by-default pedwarn.

Ah, sounds good to me. like so?

-- >8 --

Subject: [PATCH] c++: partial ordering with dependent NTTP type [PR105289]

Here ever since r11-6483-ge2e2f3f2c9400f we're rejecting and crashing
on (respectively) two testcases that we used to accept in C++17 mode
since r8-1437-g3da557ec145823.  Both testcases declare a partial
specialization for which the primary template contains an NTTP with
dependent type, but the validity of these partial specializations is
unclear and is the subject of PR86193 / CWG 455.

This patch just fixes the reported ICE in the second testcase.  The bug
was that when checking whether the type of an NTTP uses still-undeduced
parameters, we'd substitute into the previously substituted NTTP type
instead of into the original NTTP type.

And given that the treatment of such partial specializations seems to
be underspecified in the standard, this patch downgrades the general
"not more specialized" diagnostic from a permerror to a pedwarn.

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

	PR c++/105289
	PR c++/86193

gcc/cp/ChangeLog:

	* pt.cc (process_partial_specialization): Downgrade "partial
	specialization isn't more specialized" diagnostic from permerror
	to an on-by-default pedwarn.
	(unify) <case TEMPLATE_PARM_INDEX>: When substituting into the
	NTTP type a second time, use the original type not the
	substituted type.

gcc/testsuite/ChangeLog:

	* g++.dg/template/partial-specialization11.C: New test.
	* g++.dg/template/partial-specialization12.C: New test.
---
 gcc/cp/pt.cc                                        |  7 ++++---
 .../g++.dg/template/partial-specialization11.C      | 11 +++++++++++
 .../g++.dg/template/partial-specialization12.C      | 13 +++++++++++++
 3 files changed, 28 insertions(+), 3 deletions(-)
 create mode 100644 gcc/testsuite/g++.dg/template/partial-specialization11.C
 create mode 100644 gcc/testsuite/g++.dg/template/partial-specialization12.C

diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc
index dde62ee052d..7dd9e6788f4 100644
--- a/gcc/cp/pt.cc
+++ b/gcc/cp/pt.cc
@@ -5227,8 +5227,9 @@ process_partial_specialization (tree decl)
 	   && !get_partial_spec_bindings (maintmpl, maintmpl, specargs))
     {
       auto_diagnostic_group d;
-      if (permerror (input_location, "partial specialization %qD is not "
-		     "more specialized than", decl))
+      if (pedwarn (input_location, 0,
+		   "partial specialization %qD is not more specialized than",
+		   decl))
 	inform (DECL_SOURCE_LOCATION (maintmpl), "primary template %qD",
 		maintmpl);
     }
@@ -24299,7 +24300,7 @@ unify (tree tparms, tree targs, tree parm, tree arg, int strict,
 	      /* Now check whether the type of this parameter is still
 		 dependent, and give up if so.  */
 	      ++processing_template_decl;
-	      tparm = tsubst (tparm, targs, tf_none, NULL_TREE);
+	      tparm = tsubst (TREE_TYPE (parm), targs, tf_none, NULL_TREE);
 	      --processing_template_decl;
 	      if (uses_template_parms (tparm))
 		return unify_success (explain_p);
diff --git a/gcc/testsuite/g++.dg/template/partial-specialization11.C b/gcc/testsuite/g++.dg/template/partial-specialization11.C
new file mode 100644
index 00000000000..df1ead380f1
--- /dev/null
+++ b/gcc/testsuite/g++.dg/template/partial-specialization11.C
@@ -0,0 +1,11 @@
+// PR c++/86193
+// CWG 455 (active)
+// { dg-options "" } // clear -pedantic-errors
+
+template<class T> struct value_type;
+
+template<class T, typename value_type<T>::type V>
+struct push_front_vlist;
+
+template<class T, int V>
+struct push_front_vlist<T*, V> { }; // { dg-warning "not more specialized" }
diff --git a/gcc/testsuite/g++.dg/template/partial-specialization12.C b/gcc/testsuite/g++.dg/template/partial-specialization12.C
new file mode 100644
index 00000000000..6e84f4949c1
--- /dev/null
+++ b/gcc/testsuite/g++.dg/template/partial-specialization12.C
@@ -0,0 +1,13 @@
+// PR c++/105289
+// PR c++/86193
+// CWG 455 (active)
+// { dg-do compile { target c++11 } }
+
+template<class T>
+struct value_type;
+
+template <class List, typename value_type<List>::type Element>
+struct push_front_vlist;
+
+template <template <class X, X...> class XList, class T, T Arg, T... Vs>
+struct push_front_vlist<XList<T, Vs...>, Arg> { }; // { dg-error "not more specialized" }
Jason Merrill April 25, 2022, 9:53 p.m. UTC | #4
On 4/25/22 14:10, Patrick Palka wrote:
> On Mon, 25 Apr 2022, Jason Merrill wrote:
> 
>> On 4/22/22 15:27, Patrick Palka wrote:
>>> On Fri, 22 Apr 2022, Patrick Palka wrote:
>>>
>>>> Here ever since r11-6483-ge2e2f3f2c9400f we're rejecting and crashing
>>>> (respectively) on two testcases that we used to accept in C++17 mode.
>>>> Both testcases declare partial specializations for which the primary
>>>> template contains an NTTP with dependent type, but the correctness of
>>>> these partial specializations is unclear according to PR86193.
>>>>
>>>> This patch restores the previous C++17 behavior for such partial
>>>> specializations by restricting the r11-6483 change to just ordinary
>>>> deduction as opposed to deduction for sake of partial ordering.
>>>
>>> Note that if we're okay with rejecting such partial specializations even
>>> in C++17 mode (and thus deeming PR105289 to be ICE-on-invalid instead of
>>> ICE-on-valid), then the fix for the reported ICE is just:
>>>
>>> diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc
>>> index dde62ee052d..6d65f6ad3cf 100644
>>> --- a/gcc/cp/pt.cc
>>> +++ b/gcc/cp/pt.cc
>>> @@ -24299,7 +24299,7 @@ unify (tree tparms, tree targs, tree parm, tree arg,
>>> int strict,
>>>    	      /* Now check whether the type of this parameter is still
>>>    		 dependent, and give up if so.  */
>>>    	      ++processing_template_decl;
>>> -	      tparm = tsubst (tparm, targs, tf_none, NULL_TREE);
>>> +	      tparm = tsubst (TREE_TYPE (parm), targs, tf_none, NULL_TREE);
>>>    	      --processing_template_decl;
>>>    	      if (uses_template_parms (tparm))
>>>    		return unify_success (explain_p);
>>>
>>> i.e. we need to substitute into the original NTTP type, not into the
>>> already substituted NTTP type.
>>
>> I'm happy rejecting partial-specialization12.C on that basis.  11 is
>> interesting because int is a non-dependent type; it might be worth adding that
>> testcase to the DR455 discussion.
>>
>> I think let's go with this patch and bump down the "partial specialization
>> isn't more specialized" diagnostic from permerror to on-by-default pedwarn.
> 
> Ah, sounds good to me. like so?
> 
> -- >8 --
> 
> Subject: [PATCH] c++: partial ordering with dependent NTTP type [PR105289]
> 
> Here ever since r11-6483-ge2e2f3f2c9400f we're rejecting and crashing
> on (respectively) two testcases that we used to accept in C++17 mode
> since r8-1437-g3da557ec145823.  Both testcases declare a partial
> specialization for which the primary template contains an NTTP with
> dependent type, but the validity of these partial specializations is
> unclear and is the subject of PR86193 / CWG 455.
> 
> This patch just fixes the reported ICE in the second testcase.  The bug
> was that when checking whether the type of an NTTP uses still-undeduced
> parameters, we'd substitute into the previously substituted NTTP type
> instead of into the original NTTP type.
> 
> And given that the treatment of such partial specializations seems to
> be underspecified in the standard, this patch downgrades the general
> "not more specialized" diagnostic from a permerror to a pedwarn.
> 
> Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK for
> trunk/11?

OK.

> 	PR c++/105289
> 	PR c++/86193
> 
> gcc/cp/ChangeLog:
> 
> 	* pt.cc (process_partial_specialization): Downgrade "partial
> 	specialization isn't more specialized" diagnostic from permerror
> 	to an on-by-default pedwarn.
> 	(unify) <case TEMPLATE_PARM_INDEX>: When substituting into the
> 	NTTP type a second time, use the original type not the
> 	substituted type.
> 
> gcc/testsuite/ChangeLog:
> 
> 	* g++.dg/template/partial-specialization11.C: New test.
> 	* g++.dg/template/partial-specialization12.C: New test.
> ---
>   gcc/cp/pt.cc                                        |  7 ++++---
>   .../g++.dg/template/partial-specialization11.C      | 11 +++++++++++
>   .../g++.dg/template/partial-specialization12.C      | 13 +++++++++++++
>   3 files changed, 28 insertions(+), 3 deletions(-)
>   create mode 100644 gcc/testsuite/g++.dg/template/partial-specialization11.C
>   create mode 100644 gcc/testsuite/g++.dg/template/partial-specialization12.C
> 
> diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc
> index dde62ee052d..7dd9e6788f4 100644
> --- a/gcc/cp/pt.cc
> +++ b/gcc/cp/pt.cc
> @@ -5227,8 +5227,9 @@ process_partial_specialization (tree decl)
>   	   && !get_partial_spec_bindings (maintmpl, maintmpl, specargs))
>       {
>         auto_diagnostic_group d;
> -      if (permerror (input_location, "partial specialization %qD is not "
> -		     "more specialized than", decl))
> +      if (pedwarn (input_location, 0,
> +		   "partial specialization %qD is not more specialized than",
> +		   decl))
>   	inform (DECL_SOURCE_LOCATION (maintmpl), "primary template %qD",
>   		maintmpl);
>       }
> @@ -24299,7 +24300,7 @@ unify (tree tparms, tree targs, tree parm, tree arg, int strict,
>   	      /* Now check whether the type of this parameter is still
>   		 dependent, and give up if so.  */
>   	      ++processing_template_decl;
> -	      tparm = tsubst (tparm, targs, tf_none, NULL_TREE);
> +	      tparm = tsubst (TREE_TYPE (parm), targs, tf_none, NULL_TREE);
>   	      --processing_template_decl;
>   	      if (uses_template_parms (tparm))
>   		return unify_success (explain_p);
> diff --git a/gcc/testsuite/g++.dg/template/partial-specialization11.C b/gcc/testsuite/g++.dg/template/partial-specialization11.C
> new file mode 100644
> index 00000000000..df1ead380f1
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/template/partial-specialization11.C
> @@ -0,0 +1,11 @@
> +// PR c++/86193
> +// CWG 455 (active)
> +// { dg-options "" } // clear -pedantic-errors
> +
> +template<class T> struct value_type;
> +
> +template<class T, typename value_type<T>::type V>
> +struct push_front_vlist;
> +
> +template<class T, int V>
> +struct push_front_vlist<T*, V> { }; // { dg-warning "not more specialized" }
> diff --git a/gcc/testsuite/g++.dg/template/partial-specialization12.C b/gcc/testsuite/g++.dg/template/partial-specialization12.C
> new file mode 100644
> index 00000000000..6e84f4949c1
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/template/partial-specialization12.C
> @@ -0,0 +1,13 @@
> +// PR c++/105289
> +// PR c++/86193
> +// CWG 455 (active)
> +// { dg-do compile { target c++11 } }
> +
> +template<class T>
> +struct value_type;
> +
> +template <class List, typename value_type<List>::type Element>
> +struct push_front_vlist;
> +
> +template <template <class X, X...> class XList, class T, T Arg, T... Vs>
> +struct push_front_vlist<XList<T, Vs...>, Arg> { }; // { dg-error "not more specialized" }
diff mbox series

Patch

diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc
index dde62ee052d..52bd130b7e7 100644
--- a/gcc/cp/pt.cc
+++ b/gcc/cp/pt.cc
@@ -24287,8 +24287,7 @@  unify (tree tparms, tree targs, tree parm, tree arg, int strict,
 	  /* We haven't deduced the type of this parameter yet.  */
 	  if (cxx_dialect >= cxx17
 	      /* We deduce from array bounds in try_array_deduction.  */
-	      && !(strict & UNIFY_ALLOW_INTEGER)
-	      && TEMPLATE_PARM_LEVEL (parm) <= TMPL_ARGS_DEPTH (targs))
+	      && !(strict & UNIFY_ALLOW_INTEGER))
 	    {
 	      /* Deduce it from the non-type argument.  As above, ignore
 		 top-level quals here too.  */
@@ -24296,13 +24295,21 @@  unify (tree tparms, tree targs, tree parm, tree arg, int strict,
 	      RECUR_AND_CHECK_FAILURE (tparms, targs,
 				       tparm, atype,
 				       UNIFY_ALLOW_NONE, explain_p);
-	      /* Now check whether the type of this parameter is still
-		 dependent, and give up if so.  */
-	      ++processing_template_decl;
-	      tparm = tsubst (tparm, targs, tf_none, NULL_TREE);
-	      --processing_template_decl;
-	      if (uses_template_parms (tparm))
-		return unify_success (explain_p);
+	      if (!processing_template_decl
+		  && TPARMS_PRIMARY_TEMPLATE (tparms)
+		  && DECL_FUNCTION_TEMPLATE_P (TPARMS_PRIMARY_TEMPLATE
+					       (tparms)))
+		{
+		  /* If the NTTP's type uses still-undeduced template
+		     parameters, then don't unify it now.  This gives
+		     type_unification_real a chance to retry deduction
+		     with default template arguments substituted in.  */
+		  ++processing_template_decl;
+		  tparm = tsubst (TREE_TYPE (parm), targs, tf_none, NULL_TREE);
+		  --processing_template_decl;
+		  if (uses_template_parms (tparm))
+		    return unify_success (explain_p);
+		}
 	    }
 	  else
 	    /* Try again later.  */
diff --git a/gcc/testsuite/g++.dg/template/partial-specialization11.C b/gcc/testsuite/g++.dg/template/partial-specialization11.C
new file mode 100644
index 00000000000..20da407d422
--- /dev/null
+++ b/gcc/testsuite/g++.dg/template/partial-specialization11.C
@@ -0,0 +1,10 @@ 
+// PR c++/105289
+
+template<class T> struct value_type;
+
+template<class T, typename value_type<T>::type V>
+struct push_front_vlist;
+
+template<class T, int V>
+struct push_front_vlist<T*, V> { };
+// { dg-error "not more specialized" "PR86193" { target c++14_down } .-1 }
diff --git a/gcc/testsuite/g++.dg/template/partial-specialization12.C b/gcc/testsuite/g++.dg/template/partial-specialization12.C
new file mode 100644
index 00000000000..d70f7592790
--- /dev/null
+++ b/gcc/testsuite/g++.dg/template/partial-specialization12.C
@@ -0,0 +1,12 @@ 
+// PR c++/105289
+// { dg-do compile { target c++11 } }
+
+template<class T>
+struct value_type;
+
+template <class List, typename value_type<List>::type Element>
+struct push_front_vlist;
+
+template <template <class X, X...> class XList, class T, T Arg, T... Vs>
+struct push_front_vlist<XList<T, Vs...>, Arg> { };
+// { dg-error "not more specialized" "PR86193" { target c++14_down } .-1 }
diff --git a/gcc/testsuite/g++.dg/template/partial5.C b/gcc/testsuite/g++.dg/template/partial5.C
index 037f684cbd2..7492632f39b 100644
--- a/gcc/testsuite/g++.dg/template/partial5.C
+++ b/gcc/testsuite/g++.dg/template/partial5.C
@@ -14,7 +14,7 @@  template<typename T, typename T::foo V>
 struct Y { };
 
 template<typename T, typename U, U v>
-struct Y<T, v> { }; // { dg-error "" }
+struct Y<T, v> { }; // { dg-error "" "PR86193" { target c++14_down } }
 
 
 template<typename T, T V>