public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc r10-9634] c++: Fix self-mapping in map_arguments [PR96531, PR97103]
@ 2021-03-31 12:33 Patrick Palka
  0 siblings, 0 replies; only message in thread
From: Patrick Palka @ 2021-03-31 12:33 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:a834e6d59d74ccaefbcbbed5ea7ee25305057853

commit r10-9634-ga834e6d59d74ccaefbcbbed5ea7ee25305057853
Author: Patrick Palka <ppalka@redhat.com>
Date:   Sat Sep 19 11:02:46 2020 -0400

    c++: Fix self-mapping in map_arguments [PR96531, PR97103]
    
    With r10-8077 we stopped passing the argified current_template_parms to
    normalize_constraint_expression from finish_nested_requirement, and
    instead made map_arguments perform a self-mapping of parameters when
    args is NULL.  But we're currently not handling parameter packs and
    BOUND_TEMPLATE_TEMPLATE_PARMs properly during this self-mapping, which
    leads to ICEs later during satisfaction.
    
    To properly handle self-mapping of a parameter pack, this patch
    extends template_parm_to_arg to handle TEMPLATE_PARM_P nodes, and
    makes map_arguments use it.  (This change revealed that the call to
    template_parm_to_arg in convert_generic_types_to_packs was a no-op
    because the argument 't' is never a TREE_LIST, so this patch
    additionally removes this call.)
    
    As for bound ttps, map_arguments before r10-8077 would map a
    BOUND_TEMPLATE_TEMPLATE_PARM not to itself but to its underlying
    TEMPLATE_TEMPLATE_PARM.  We could restore this behavior in
    map_arguments, but since a bound ttp is not really a template parameter
    it seems better to make keep_template_parm not give us a bound ttp in
    the first place.  So this patch makes keep_template_parm return the
    underlying ttp when it sees a bound ttp.
    
    gcc/cp/ChangeLog:
    
            PR c++/96531
            PR c++/97103
            * constraint.cc (map_arguments): Call template_parm_to_arg
            in the self-mapping case.
            (finish_shorthand_constraint): No need to build a TREE_LIST
            before calling template_parm_to_arg.
            * pt.c (template_parm_to_arg): Rewrite to handle TEMPLATE_PARM_P
            nodes as well as DECL_TEMPLATE_PARM_P nodes, and to make the
            overlying TREE_LIST node optional.
            (keep_template_parm): Don't record a BOUND_TEMPLATE_TEMPLATE_PARM,
            instead record its corresponding TEMPLATE_TEMPLATE_PARM.
            (convert_generic_types_to_packs): Don't call
            template_parm_to_arg.
    
    gcc/testsuite/ChangeLog:
    
            PR c++/96531
            PR c++/97103
            * g++.dg/cpp2a/concepts-ttp2.C: New test.
            * g++.dg/cpp2a/concepts-variadic1.C: New test.
    
    (cherry picked from commit e5d72c840a226fdbab65912f97d42a1dbdaf6ed2)

Diff:
---
 gcc/cp/constraint.cc                            |  4 +--
 gcc/cp/pt.c                                     | 43 ++++++++++++++++---------
 gcc/testsuite/g++.dg/cpp2a/concepts-ttp2.C      | 11 +++++++
 gcc/testsuite/g++.dg/cpp2a/concepts-variadic1.C | 28 ++++++++++++++++
 4 files changed, 68 insertions(+), 18 deletions(-)

diff --git a/gcc/cp/constraint.cc b/gcc/cp/constraint.cc
index 89a2bb5223b..5f6488e514f 100644
--- a/gcc/cp/constraint.cc
+++ b/gcc/cp/constraint.cc
@@ -554,7 +554,7 @@ map_arguments (tree parms, tree args)
 	TREE_PURPOSE (p) = TMPL_ARG (args, level, index);
       }
     else
-      TREE_PURPOSE (p) = TREE_VALUE (p);
+      TREE_PURPOSE (p) = template_parm_to_arg (p);
 
   return parms;
 }
@@ -1485,7 +1485,7 @@ finish_shorthand_constraint (tree decl, tree constr)
 
   /* Get the argument and overload used for the requirement
      and adjust it if we're going to expand later.  */
-  tree arg = template_parm_to_arg (build_tree_list (NULL_TREE, decl));
+  tree arg = template_parm_to_arg (decl);
   if (apply_to_each_p && declared_pack_p)
     arg = PACK_EXPANSION_PATTERN (TREE_VEC_ELT (ARGUMENT_PACK_ARGS (arg), 0));
 
diff --git a/gcc/cp/pt.c b/gcc/cp/pt.c
index 73bc06972dd..e74e45b64c3 100644
--- a/gcc/cp/pt.c
+++ b/gcc/cp/pt.c
@@ -4687,29 +4687,37 @@ end_template_decl (void)
   current_template_parms = TREE_CHAIN (current_template_parms);
 }
 
-/* Takes a TREE_LIST representing a template parameter and convert it
-   into an argument suitable to be passed to the type substitution
-   functions.  Note that If the TREE_LIST contains an error_mark
-   node, the returned argument is error_mark_node.  */
+/* Takes a TEMPLATE_PARM_P or DECL_TEMPLATE_PARM_P node or a TREE_LIST
+   thereof, and converts it into an argument suitable to be passed to
+   the type substitution functions.  Note that if the TREE_LIST contains
+   an error_mark node, the returned argument is error_mark_node.  */
 
 tree
 template_parm_to_arg (tree t)
 {
+  if (!t)
+    return NULL_TREE;
 
-  if (t == NULL_TREE
-      || TREE_CODE (t) != TREE_LIST)
-    return t;
+  if (TREE_CODE (t) == TREE_LIST)
+    t = TREE_VALUE (t);
 
-  if (error_operand_p (TREE_VALUE (t)))
+  if (error_operand_p (t))
     return error_mark_node;
 
-  t = TREE_VALUE (t);
-
-  if (TREE_CODE (t) == TYPE_DECL
-      || TREE_CODE (t) == TEMPLATE_DECL)
+  if (DECL_P (t) && DECL_TEMPLATE_PARM_P (t))
     {
-      t = TREE_TYPE (t);
+      if (TREE_CODE (t) == TYPE_DECL
+	  || TREE_CODE (t) == TEMPLATE_DECL)
+	t = TREE_TYPE (t);
+      else
+	t = DECL_INITIAL (t);
+    }
+
+  gcc_assert (TEMPLATE_PARM_P (t));
 
+  if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
+      || TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM)
+    {
       if (TEMPLATE_TYPE_PARAMETER_PACK (t))
 	{
 	  /* Turn this argument into a TYPE_ARGUMENT_PACK
@@ -4726,8 +4734,6 @@ template_parm_to_arg (tree t)
     }
   else
     {
-      t = DECL_INITIAL (t);
-
       if (TEMPLATE_PARM_PARAMETER_PACK (t))
 	{
 	  /* Turn this argument into a NONTYPE_ARGUMENT_PACK
@@ -10494,6 +10500,11 @@ keep_template_parm (tree t, void* data)
   if (level > ftpi->max_depth)
     return 0;
 
+  if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
+    /* We want the underlying TEMPLATE_TEMPLATE_PARM, not the
+       BOUND_TEMPLATE_TEMPLATE_PARM itself.  */
+    t = TREE_TYPE (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t));
+
   /* Arguments like const T yield parameters like const T. This means that
      a template-id like X<T, const T> would yield two distinct parameters:
      T and const T. Adjust types to their unqualified versions.  */
@@ -29595,7 +29606,7 @@ convert_generic_types_to_packs (tree parm, int start_idx, int end_idx)
       if (tree constr = TEMPLATE_PARM_CONSTRAINTS (node))
 	{
 	  tree id = unpack_concept_check (constr);
-	  TREE_VEC_ELT (TREE_OPERAND (id, 1), 0) = template_parm_to_arg (t);
+	  TREE_VEC_ELT (TREE_OPERAND (id, 1), 0) = t;
 	  tree fold = finish_left_unary_fold_expr (constr, TRUTH_ANDIF_EXPR);
 	  TEMPLATE_PARM_CONSTRAINTS (node) = fold;
 
diff --git a/gcc/testsuite/g++.dg/cpp2a/concepts-ttp2.C b/gcc/testsuite/g++.dg/cpp2a/concepts-ttp2.C
new file mode 100644
index 00000000000..7f4883754dd
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp2a/concepts-ttp2.C
@@ -0,0 +1,11 @@
+// PR c++/97103
+// { dg-do compile { target c++20 } }
+
+template<typename R, typename Rep>
+class quantity {};
+
+template<template<typename, typename> typename Q>
+inline constexpr bool valid_template_arguments = requires {
+  requires requires { typename Q<int, int>; };
+};
+static_assert(valid_template_arguments<quantity>);
diff --git a/gcc/testsuite/g++.dg/cpp2a/concepts-variadic1.C b/gcc/testsuite/g++.dg/cpp2a/concepts-variadic1.C
new file mode 100644
index 00000000000..deab028ca3c
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp2a/concepts-variadic1.C
@@ -0,0 +1,28 @@
+// PR c++/96531
+// { dg-do compile { target c++20 } }
+
+template<typename T>
+concept is_bool = __is_same(bool, T);
+
+template <typename... Ts>
+concept C = requires {
+  requires (is_bool<Ts> || ...);
+};
+
+template <bool... Bs>
+concept D = requires {
+  requires (Bs || ...);
+};
+
+template <typename... Ts>
+requires C<Ts...>
+void bar() {}
+
+template <bool... Bs>
+requires D<Bs...>
+void baz() {}
+
+int main() {
+  bar<int, char, bool>();
+  baz<false, true, false>();
+}


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2021-03-31 12:33 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-03-31 12:33 [gcc r10-9634] c++: Fix self-mapping in map_arguments [PR96531, PR97103] Patrick Palka

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).