c++: Fix template arguments comparison with class NTTP [PR91754]

Message ID 20200129173505.1567974-1-polacek@redhat.com
State New
Headers show
Series
  • c++: Fix template arguments comparison with class NTTP [PR91754]
Related show

Commit Message

Marek Polacek Jan. 29, 2020, 5:35 p.m.
Here we fail to compile the attached test, stating that the use of
T<s> in T<s>::T() {} is "invalid use of incomplete type".  It is a
function definition so grokdeclarator checks that the qualifying type
is complete.

When we parsed the class T, finish_struct gave the class a non-null
TYPE_SIZE, making it COMPLETE_TYPE_P.  But then we're parsing T<s>,
a TEMPLATE_ID, in

  T<s>::T() {}

so try to lookup_template_class T.  This failed because we couldn't
find such a class: comp_template_args told us that the argument lists
don't match, because one of the args was wrapped in a VIEW_CONVERT_EXPR
to make it look const.  It seems to me that we should see through
these artificial wrappers and consider the args same.

Bootstrapped/regtested on x86_64-linux, ok for trunk and maybe 9?

2020-01-29  Marek Polacek  <polacek@redhat.com>

	PR c++/91754 - Fix template arguments comparison with class NTTP.
	* pt.c (class_nttp_const_wrapper_p): New.
	(template_args_equal): See through class_nttp_const_wrapper_p
	arguments.

	* g++.dg/cpp2a/nontype-class30.C: New test.
---
 gcc/cp/pt.c                                  | 18 ++++++++++++++++++
 gcc/testsuite/g++.dg/cpp2a/nontype-class30.C | 15 +++++++++++++++
 2 files changed, 33 insertions(+)
 create mode 100644 gcc/testsuite/g++.dg/cpp2a/nontype-class30.C


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

Comments

Jason Merrill Jan. 29, 2020, 6:12 p.m. | #1
On 1/29/20 12:35 PM, Marek Polacek wrote:
> Here we fail to compile the attached test, stating that the use of

> T<s> in T<s>::T() {} is "invalid use of incomplete type".  It is a

> function definition so grokdeclarator checks that the qualifying type

> is complete.

> 

> When we parsed the class T, finish_struct gave the class a non-null

> TYPE_SIZE, making it COMPLETE_TYPE_P.  But then we're parsing T<s>,

> a TEMPLATE_ID, in

> 

>    T<s>::T() {}

> 

> so try to lookup_template_class T.  This failed because we couldn't

> find such a class: comp_template_args told us that the argument lists

> don't match, because one of the args was wrapped in a VIEW_CONVERT_EXPR

> to make it look const.  It seems to me that we should see through

> these artificial wrappers and consider the args same.

> 

> Bootstrapped/regtested on x86_64-linux, ok for trunk and maybe 9?


OK for both.

> 2020-01-29  Marek Polacek  <polacek@redhat.com>

> 

> 	PR c++/91754 - Fix template arguments comparison with class NTTP.

> 	* pt.c (class_nttp_const_wrapper_p): New.

> 	(template_args_equal): See through class_nttp_const_wrapper_p

> 	arguments.

> 

> 	* g++.dg/cpp2a/nontype-class30.C: New test.

> ---

>   gcc/cp/pt.c                                  | 18 ++++++++++++++++++

>   gcc/testsuite/g++.dg/cpp2a/nontype-class30.C | 15 +++++++++++++++

>   2 files changed, 33 insertions(+)

>   create mode 100644 gcc/testsuite/g++.dg/cpp2a/nontype-class30.C

> 

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

> index e889c800aca..711da0fd053 100644

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

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

> @@ -8983,6 +8983,19 @@ coerce_innermost_template_parms (tree parms,

>     return coerced_args;

>   }

>   

> +/* Returns true if T is a wrapper to make a C++20 template parameter

> +   object const.  */

> +

> +static bool

> +class_nttp_const_wrapper_p (tree t)

> +{

> +  if (cxx_dialect < cxx2a)

> +    return false;

> +  return (TREE_CODE (t) == VIEW_CONVERT_EXPR

> +	  && CP_TYPE_CONST_P (TREE_TYPE (t))

> +	  && TREE_CODE (TREE_OPERAND (t, 0)) == TEMPLATE_PARM_INDEX);

> +}

> +

>   /* Returns 1 if template args OT and NT are equivalent.  */

>   

>   int

> @@ -8995,6 +9008,11 @@ template_args_equal (tree ot, tree nt, bool partial_order /* = false */)

>     if (nt == any_targ_node || ot == any_targ_node)

>       return true;

>   

> +  if (class_nttp_const_wrapper_p (nt))

> +    nt = TREE_OPERAND (nt, 0);

> +  if (class_nttp_const_wrapper_p (ot))

> +    ot = TREE_OPERAND (ot, 0);

> +

>     if (TREE_CODE (nt) == TREE_VEC)

>       /* For member templates */

>       return TREE_CODE (ot) == TREE_VEC && comp_template_args (ot, nt);

> diff --git a/gcc/testsuite/g++.dg/cpp2a/nontype-class30.C b/gcc/testsuite/g++.dg/cpp2a/nontype-class30.C

> new file mode 100644

> index 00000000000..b2e174c7fd3

> --- /dev/null

> +++ b/gcc/testsuite/g++.dg/cpp2a/nontype-class30.C

> @@ -0,0 +1,15 @@

> +// PR c++/91754 - Fix template arguments comparison with class NTTP.

> +// { dg-do compile { target c++2a } }

> +

> +struct S {};

> +

> +template<S s>

> +struct T {

> +  T();

> +};

> +

> +template<S s>

> +T<s>::T() {}

> +

> +S s;

> +T<s> t;

> 

> base-commit: 989a5fb3aa5cb241727922c669da6742d33a7a86

>

Patch

diff --git a/gcc/cp/pt.c b/gcc/cp/pt.c
index e889c800aca..711da0fd053 100644
--- a/gcc/cp/pt.c
+++ b/gcc/cp/pt.c
@@ -8983,6 +8983,19 @@  coerce_innermost_template_parms (tree parms,
   return coerced_args;
 }
 
+/* Returns true if T is a wrapper to make a C++20 template parameter
+   object const.  */
+
+static bool
+class_nttp_const_wrapper_p (tree t)
+{
+  if (cxx_dialect < cxx2a)
+    return false;
+  return (TREE_CODE (t) == VIEW_CONVERT_EXPR
+	  && CP_TYPE_CONST_P (TREE_TYPE (t))
+	  && TREE_CODE (TREE_OPERAND (t, 0)) == TEMPLATE_PARM_INDEX);
+}
+
 /* Returns 1 if template args OT and NT are equivalent.  */
 
 int
@@ -8995,6 +9008,11 @@  template_args_equal (tree ot, tree nt, bool partial_order /* = false */)
   if (nt == any_targ_node || ot == any_targ_node)
     return true;
 
+  if (class_nttp_const_wrapper_p (nt))
+    nt = TREE_OPERAND (nt, 0);
+  if (class_nttp_const_wrapper_p (ot))
+    ot = TREE_OPERAND (ot, 0);
+
   if (TREE_CODE (nt) == TREE_VEC)
     /* For member templates */
     return TREE_CODE (ot) == TREE_VEC && comp_template_args (ot, nt);
diff --git a/gcc/testsuite/g++.dg/cpp2a/nontype-class30.C b/gcc/testsuite/g++.dg/cpp2a/nontype-class30.C
new file mode 100644
index 00000000000..b2e174c7fd3
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp2a/nontype-class30.C
@@ -0,0 +1,15 @@ 
+// PR c++/91754 - Fix template arguments comparison with class NTTP.
+// { dg-do compile { target c++2a } }
+
+struct S {};
+
+template<S s>
+struct T {
+  T();
+};
+
+template<S s>
+T<s>::T() {} 
+
+S s;
+T<s> t;