public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc r11-9684] c++: lambda in template default argument [PR103186]
@ 2022-03-22  5:18 Jason Merrill
  0 siblings, 0 replies; only message in thread
From: Jason Merrill @ 2022-03-22  5:18 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:97949b796566ad10895e69aadafb2462e45617b6

commit r11-9684-g97949b796566ad10895e69aadafb2462e45617b6
Author: Jason Merrill <jason@redhat.com>
Date:   Wed Jan 26 16:42:57 2022 -0500

    c++: lambda in template default argument [PR103186]
    
    The problem with this testcase was that since my patch for PR97900 we
    weren't preserving DECL_UID identity for parameters of instantiations of
    templated functions, so using those parameters as the keys for the
    defarg_inst map broke.  I think this was always fragile given the
    possibility of redeclarations, so instead of reverting that change let's
    switch to keying off the function.
    
    Memory use compiling stdc++.h is not noticeably different.
    
            PR c++/103186
    
    gcc/cp/ChangeLog:
    
            * pt.c (defarg_inst): Use tree_vec_map_cache_hasher.
            (defarg_insts_for): New.
            (tsubst_default_argument): Adjust.
    
    gcc/testsuite/ChangeLog:
    
            * g++.dg/cpp0x/lambda/lambda-defarg10.C: New test.

Diff:
---
 gcc/cp/pt.c                                        | 42 +++++++++++++++++-----
 .../g++.dg/cpp0x/lambda/lambda-defarg10.C          | 21 +++++++++++
 2 files changed, 54 insertions(+), 9 deletions(-)

diff --git a/gcc/cp/pt.c b/gcc/cp/pt.c
index 43de705d73d..1b473f79dd6 100644
--- a/gcc/cp/pt.c
+++ b/gcc/cp/pt.c
@@ -13659,7 +13659,28 @@ tsubst_aggr_type (tree t,
     }
 }
 
-static GTY((cache)) decl_tree_cache_map *defarg_inst;
+/* Map from a FUNCTION_DECL to a vec of default argument instantiations,
+   indexed in reverse order of the parameters.  */
+
+static GTY((cache)) hash_table<tree_vec_map_cache_hasher> *defarg_inst;
+
+/* Return a reference to the vec* of defarg insts for FN.  */
+
+static vec<tree,va_gc> *&
+defarg_insts_for (tree fn)
+{
+  if (!defarg_inst)
+    defarg_inst = hash_table<tree_vec_map_cache_hasher>::create_ggc (13);
+  tree_vec_map in = { fn, nullptr };
+  tree_vec_map **slot
+    = defarg_inst->find_slot_with_hash (&in, DECL_UID (fn), INSERT);
+  if (!*slot)
+    {
+      *slot = ggc_alloc<tree_vec_map> ();
+      **slot = in;
+    }
+  return (*slot)->to;
+}
 
 /* Substitute into the default argument ARG (a default argument for
    FN), which has the indicated TYPE.  */
@@ -13689,9 +13710,16 @@ tsubst_default_argument (tree fn, int parmnum, tree type, tree arg,
 
   gcc_assert (same_type_ignoring_top_level_qualifiers_p (type, parmtype));
 
-  tree *slot;
-  if (defarg_inst && (slot = defarg_inst->get (parm)))
-    return *slot;
+  /* Remember the location of the pointer to the vec rather than the location
+     of the particular element, in case the vec grows in tsubst_expr.  */
+  vec<tree,va_gc> *&defs = defarg_insts_for (fn);
+  /* Index in reverse order to avoid allocating space for initial parameters
+     that don't have default arguments.  */
+  unsigned ridx = list_length (parm);
+  if (vec_safe_length (defs) < ridx)
+    vec_safe_grow_cleared (defs, ridx);
+  else if (tree inst = (*defs)[ridx - 1])
+    return inst;
 
   /* This default argument came from a template.  Instantiate the
      default argument here, not in tsubst.  In the case of
@@ -13736,11 +13764,7 @@ tsubst_default_argument (tree fn, int parmnum, tree type, tree arg,
   pop_from_top_level ();
 
   if (arg != error_mark_node && !cp_unevaluated_operand)
-    {
-      if (!defarg_inst)
-	defarg_inst = decl_tree_cache_map::create_ggc (37);
-      defarg_inst->put (parm, arg);
-    }
+    (*defs)[ridx - 1] = arg;
 
   return arg;
 }
diff --git a/gcc/testsuite/g++.dg/cpp0x/lambda/lambda-defarg10.C b/gcc/testsuite/g++.dg/cpp0x/lambda/lambda-defarg10.C
new file mode 100644
index 00000000000..e08eb4f5e56
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp0x/lambda/lambda-defarg10.C
@@ -0,0 +1,21 @@
+// PR c++/103186
+// { dg-do compile { target c++11 } }
+
+struct f
+{
+  template<class T1>
+   f(const T1&){}
+};
+
+
+template<typename T> class A {
+public:
+    void foo(A<T> a, const f& fn = [](){}) { }
+    void bar(A<T> a) { foo(a); }
+};
+int main() {
+    A<int> a;
+    a.foo(a);
+    a.bar(a);
+    return 0;
+}


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

only message in thread, other threads:[~2022-03-22  5:18 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-03-22  5:18 [gcc r11-9684] c++: lambda in template default argument [PR103186] Jason Merrill

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).