c++: Fix mismatch in template argument deduction [PR90505]

Message ID 20200304005014.1138262-1-polacek@redhat.com
State New
Headers show
Series
  • c++: Fix mismatch in template argument deduction [PR90505]
Related show

Commit Message

Marek Polacek March 4, 2020, 12:50 a.m.
My GCC 9 patch for C++20 P0846R0 (ADL and function templates) tweaked
cp_parser_template_name to only return an identifier if name lookup
didn't find anything.  In the deduce4.C case it means that we now
return an OVERLOAD.  That means that cp_parser_template_id will call
lookup_template_function whereby producing a TEMPLATE_ID_EXPR with
unknown_type_node.  Previously, we created a TEMPLATE_ID_EXPR with
no type, making it type-dependent.  What we have now is no longer
type-dependent.  And so, when we call finish_call_expr after we've
parsed "foo<int>(10)", even though we're in a template, we still do
the normal processing, thus perform overload resolution.  When adding
the template candidate foo we need to deduce the template arguments,
and that is where things go downhill.

When fn_type_unification sees that we have explicit template arguments,
but they aren't complete, it will use them to substitute the function
type.  So we substitute e.g. "void <T33d> (U)".  But the explicit
template argument was for a different parameter so we don't actually
substitute anything.  But the problem here was that we reduced the
template level of 'U' anyway.  So then when we're actually deducing
the template arguments via type_unification_real, we fail in unify:
22932       if (TEMPLATE_TYPE_LEVEL (parm)
22933           != template_decl_level (tparm))
22934         /* The PARM is not one we're trying to unify.  Just check
22935            to see if it matches ARG.  */
because 'parm' has been reduced but 'tparm' has not yet.

Therefore we shouldn't reduce the template level of template parameters
when tf_partial aka template argument deduction substitution.  But we
can only return after performing the cp_build_qualified_type etc.
business otherwise things break horribly.

Bootstrapped/regtested on x86_64-linux, ok for trunk and 9?  I'd like
to put this in before 9.3, if possible.

2020-03-03  Jason Merrill  <jason@redhat.com>
	    Marek Polacek  <polacek@redhat.com>

	PR c++/90505 - mismatch in template argument deduction.
	* pt.c (tsubst): Don't reduce the template level of template
	parameters when tf_partial.

	* g++.dg/template/deduce4.C: New test.
	* g++.dg/template/deduce5.C: New test.
	* g++.dg/template/deduce6.C: New test.
	* g++.dg/template/deduce7.C: New test.
---
 gcc/cp/pt.c                             | 14 ++++++++------
 gcc/testsuite/g++.dg/template/deduce4.C | 17 +++++++++++++++++
 gcc/testsuite/g++.dg/template/deduce5.C | 17 +++++++++++++++++
 gcc/testsuite/g++.dg/template/deduce6.C | 17 +++++++++++++++++
 gcc/testsuite/g++.dg/template/deduce7.C | 10 ++++++++++
 5 files changed, 69 insertions(+), 6 deletions(-)
 create mode 100644 gcc/testsuite/g++.dg/template/deduce4.C
 create mode 100644 gcc/testsuite/g++.dg/template/deduce5.C
 create mode 100644 gcc/testsuite/g++.dg/template/deduce6.C
 create mode 100644 gcc/testsuite/g++.dg/template/deduce7.C


base-commit: 462f6c2041fad058abcdd5122e99a024f69a39d5
-- 
Marek Polacek • Red Hat, Inc. • 300 A St, Boston, MA

Comments

Jason Merrill March 4, 2020, 2:06 a.m. | #1
On 3/3/20 7:50 PM, Marek Polacek wrote:
> My GCC 9 patch for C++20 P0846R0 (ADL and function templates) tweaked

> cp_parser_template_name to only return an identifier if name lookup

> didn't find anything.  In the deduce4.C case it means that we now

> return an OVERLOAD.  That means that cp_parser_template_id will call

> lookup_template_function whereby producing a TEMPLATE_ID_EXPR with

> unknown_type_node.  Previously, we created a TEMPLATE_ID_EXPR with

> no type, making it type-dependent.  What we have now is no longer

> type-dependent.  And so, when we call finish_call_expr after we've

> parsed "foo<int>(10)", even though we're in a template, we still do

> the normal processing, thus perform overload resolution.  When adding

> the template candidate foo we need to deduce the template arguments,

> and that is where things go downhill.

> 

> When fn_type_unification sees that we have explicit template arguments,

> but they aren't complete, it will use them to substitute the function

> type.  So we substitute e.g. "void <T33d> (U)".  But the explicit

> template argument was for a different parameter so we don't actually

> substitute anything.  But the problem here was that we reduced the

> template level of 'U' anyway.  So then when we're actually deducing

> the template arguments via type_unification_real, we fail in unify:

> 22932       if (TEMPLATE_TYPE_LEVEL (parm)

> 22933           != template_decl_level (tparm))

> 22934         /* The PARM is not one we're trying to unify.  Just check

> 22935            to see if it matches ARG.  */

> because 'parm' has been reduced but 'tparm' has not yet.

> 

> Therefore we shouldn't reduce the template level of template parameters

> when tf_partial aka template argument deduction substitution.  But we

> can only return after performing the cp_build_qualified_type etc.

> business otherwise things break horribly.

> 

> Bootstrapped/regtested on x86_64-linux, ok for trunk and 9?  I'd like

> to put this in before 9.3, if possible.


OK.

> 2020-03-03  Jason Merrill  <jason@redhat.com>

> 	    Marek Polacek  <polacek@redhat.com>

> 

> 	PR c++/90505 - mismatch in template argument deduction.

> 	* pt.c (tsubst): Don't reduce the template level of template

> 	parameters when tf_partial.

> 

> 	* g++.dg/template/deduce4.C: New test.

> 	* g++.dg/template/deduce5.C: New test.

> 	* g++.dg/template/deduce6.C: New test.

> 	* g++.dg/template/deduce7.C: New test.

> ---

>   gcc/cp/pt.c                             | 14 ++++++++------

>   gcc/testsuite/g++.dg/template/deduce4.C | 17 +++++++++++++++++

>   gcc/testsuite/g++.dg/template/deduce5.C | 17 +++++++++++++++++

>   gcc/testsuite/g++.dg/template/deduce6.C | 17 +++++++++++++++++

>   gcc/testsuite/g++.dg/template/deduce7.C | 10 ++++++++++

>   5 files changed, 69 insertions(+), 6 deletions(-)

>   create mode 100644 gcc/testsuite/g++.dg/template/deduce4.C

>   create mode 100644 gcc/testsuite/g++.dg/template/deduce5.C

>   create mode 100644 gcc/testsuite/g++.dg/template/deduce6.C

>   create mode 100644 gcc/testsuite/g++.dg/template/deduce7.C

> 

> diff --git a/gcc/cp/pt.c b/gcc/cp/pt.c

> index 230331f60cb..1c721b31176 100644

> --- a/gcc/cp/pt.c

> +++ b/gcc/cp/pt.c

> @@ -15057,12 +15057,6 @@ tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)

>   	int levels;

>   	tree arg = NULL_TREE;

>   

> -	/* Early in template argument deduction substitution, we don't

> -	   want to reduce the level of 'auto', or it will be confused

> -	   with a normal template parm in subsequent deduction.  */

> -	if (is_auto (t) && (complain & tf_partial))

> -	  return t;

> -

>   	r = NULL_TREE;

>   

>   	gcc_assert (TREE_VEC_LENGTH (args) > 0);

> @@ -15193,6 +15187,14 @@ tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)

>   	     about the template parameter in question.  */

>   	  return t;

>   

> +	/* Early in template argument deduction substitution, we don't

> +	   want to reduce the level of 'auto', or it will be confused

> +	   with a normal template parm in subsequent deduction.

> +	   Similarly, don't reduce the level of template parameters to

> +	   avoid mismatches when deducing their types.  */

> +	if (complain & tf_partial)

> +	  return t;

> +

>   	/* If we get here, we must have been looking at a parm for a

>   	   more deeply nested template.  Make a new version of this

>   	   template parameter, but with a lower level.  */

> diff --git a/gcc/testsuite/g++.dg/template/deduce4.C b/gcc/testsuite/g++.dg/template/deduce4.C

> new file mode 100644

> index 00000000000..e2c165dc788

> --- /dev/null

> +++ b/gcc/testsuite/g++.dg/template/deduce4.C

> @@ -0,0 +1,17 @@

> +// PR c++/90505 - mismatch in template argument deduction.

> +// { dg-do compile }

> +

> +template <typename T>

> +struct S {

> +  template <typename U, typename V>

> +  static void foo(V) { }

> +

> +  void bar () { foo<int>(10); }

> +};

> +

> +void

> +test ()

> +{

> +  S<int> s;

> +  s.bar ();

> +}

> diff --git a/gcc/testsuite/g++.dg/template/deduce5.C b/gcc/testsuite/g++.dg/template/deduce5.C

> new file mode 100644

> index 00000000000..9d382bfe03a

> --- /dev/null

> +++ b/gcc/testsuite/g++.dg/template/deduce5.C

> @@ -0,0 +1,17 @@

> +// PR c++/90505 - mismatch in template argument deduction.

> +// { dg-do compile { target c++11 } }

> +

> +template <typename T>

> +struct S {

> +  template <typename U, typename V = void>

> +  static void foo(U) { }

> +

> +  void bar () { foo<int>(10); }

> +};

> +

> +void

> +test ()

> +{

> +  S<int> s;

> +  s.bar ();

> +}

> diff --git a/gcc/testsuite/g++.dg/template/deduce6.C b/gcc/testsuite/g++.dg/template/deduce6.C

> new file mode 100644

> index 00000000000..8fee6124f5a

> --- /dev/null

> +++ b/gcc/testsuite/g++.dg/template/deduce6.C

> @@ -0,0 +1,17 @@

> +// PR c++/90505 - mismatch in template argument deduction.

> +// { dg-do compile { target c++11 } }

> +

> +template <typename T>

> +struct S {

> +  template <typename U = int, typename V>

> +  static void foo(V) { }

> +

> +  void bar () { foo<>(10); }

> +};

> +

> +void

> +test ()

> +{

> +  S<int> s;

> +  s.bar ();

> +}

> diff --git a/gcc/testsuite/g++.dg/template/deduce7.C b/gcc/testsuite/g++.dg/template/deduce7.C

> new file mode 100644

> index 00000000000..fbc28e5150d

> --- /dev/null

> +++ b/gcc/testsuite/g++.dg/template/deduce7.C

> @@ -0,0 +1,10 @@

> +// PR c++/90505 - mismatch in template argument deduction.

> +// { dg-do compile { target c++11 } }

> +

> +template <typename> class a {

> +  using b = int;

> +  using c = int;

> +  b d;

> +  void e() { g<c>(d); }

> +  template <typename... f> static void g(f...);

> +};

> 

> base-commit: 462f6c2041fad058abcdd5122e99a024f69a39d5

>

Patch

diff --git a/gcc/cp/pt.c b/gcc/cp/pt.c
index 230331f60cb..1c721b31176 100644
--- a/gcc/cp/pt.c
+++ b/gcc/cp/pt.c
@@ -15057,12 +15057,6 @@  tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
 	int levels;
 	tree arg = NULL_TREE;
 
-	/* Early in template argument deduction substitution, we don't
-	   want to reduce the level of 'auto', or it will be confused
-	   with a normal template parm in subsequent deduction.  */
-	if (is_auto (t) && (complain & tf_partial))
-	  return t;
-
 	r = NULL_TREE;
 
 	gcc_assert (TREE_VEC_LENGTH (args) > 0);
@@ -15193,6 +15187,14 @@  tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
 	     about the template parameter in question.  */
 	  return t;
 
+	/* Early in template argument deduction substitution, we don't
+	   want to reduce the level of 'auto', or it will be confused
+	   with a normal template parm in subsequent deduction.
+	   Similarly, don't reduce the level of template parameters to
+	   avoid mismatches when deducing their types.  */
+	if (complain & tf_partial)
+	  return t;
+
 	/* If we get here, we must have been looking at a parm for a
 	   more deeply nested template.  Make a new version of this
 	   template parameter, but with a lower level.  */
diff --git a/gcc/testsuite/g++.dg/template/deduce4.C b/gcc/testsuite/g++.dg/template/deduce4.C
new file mode 100644
index 00000000000..e2c165dc788
--- /dev/null
+++ b/gcc/testsuite/g++.dg/template/deduce4.C
@@ -0,0 +1,17 @@ 
+// PR c++/90505 - mismatch in template argument deduction.
+// { dg-do compile }
+
+template <typename T>
+struct S {
+  template <typename U, typename V>
+  static void foo(V) { }
+
+  void bar () { foo<int>(10); }
+};
+
+void
+test ()
+{
+  S<int> s;
+  s.bar ();
+}
diff --git a/gcc/testsuite/g++.dg/template/deduce5.C b/gcc/testsuite/g++.dg/template/deduce5.C
new file mode 100644
index 00000000000..9d382bfe03a
--- /dev/null
+++ b/gcc/testsuite/g++.dg/template/deduce5.C
@@ -0,0 +1,17 @@ 
+// PR c++/90505 - mismatch in template argument deduction.
+// { dg-do compile { target c++11 } }
+
+template <typename T>
+struct S {
+  template <typename U, typename V = void>
+  static void foo(U) { }
+
+  void bar () { foo<int>(10); }
+};
+
+void
+test ()
+{
+  S<int> s;
+  s.bar ();
+}
diff --git a/gcc/testsuite/g++.dg/template/deduce6.C b/gcc/testsuite/g++.dg/template/deduce6.C
new file mode 100644
index 00000000000..8fee6124f5a
--- /dev/null
+++ b/gcc/testsuite/g++.dg/template/deduce6.C
@@ -0,0 +1,17 @@ 
+// PR c++/90505 - mismatch in template argument deduction.
+// { dg-do compile { target c++11 } }
+
+template <typename T>
+struct S {
+  template <typename U = int, typename V>
+  static void foo(V) { }
+
+  void bar () { foo<>(10); }
+};
+
+void
+test ()
+{
+  S<int> s;
+  s.bar ();
+}
diff --git a/gcc/testsuite/g++.dg/template/deduce7.C b/gcc/testsuite/g++.dg/template/deduce7.C
new file mode 100644
index 00000000000..fbc28e5150d
--- /dev/null
+++ b/gcc/testsuite/g++.dg/template/deduce7.C
@@ -0,0 +1,10 @@ 
+// PR c++/90505 - mismatch in template argument deduction.
+// { dg-do compile { target c++11 } }
+
+template <typename> class a {
+  using b = int;
+  using c = int;
+  b d;
+  void e() { g<c>(d); }
+  template <typename... f> static void g(f...);
+};