This new code

  vec_safe_push (call_args, (*call_args)[nargs-1]);

doesn't seem to work well because "obj" points to garbage after the 
vec_safe_reserve
call:

template<typename T, typename A>
inline T *
vec_safe_push (vec<T, A, vl_embed> *&v, const T &obj CXX_MEM_STAT_INFO)
{
  vec_safe_reserve (v, 1, false PASS_MEM_STAT);
  return v->quick_push (obj);
}

But using a dedicated variable works even when vec_safe_reserve actually
extends.

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

2019-05-06  Marek Polacek  <pola...@redhat.com>

        PR c++/90265 - ICE with generic lambda.
        * pt.c (tsubst_copy_and_build): Use a dedicated variable for the last
        element in the vector.

        * g++.dg/cpp1y/lambda-generic-90265.C: New test.

diff --git gcc/cp/pt.c gcc/cp/pt.c
index 3e8c70b0d15..2f2066e297c 100644
--- gcc/cp/pt.c
+++ gcc/cp/pt.c
@@ -18881,7 +18881,8 @@ tsubst_copy_and_build (tree t,
            if (thisarg)
              {
                /* Shift the other args over to make room.  */
-               vec_safe_push (call_args, (*call_args)[nargs-1]);
+               tree last = (*call_args)[nargs - 1];
+               vec_safe_push (call_args, last);
                for (int i = nargs-1; i > 0; --i)
                  (*call_args)[i] = (*call_args)[i-1];
                (*call_args)[0] = thisarg;
diff --git gcc/testsuite/g++.dg/cpp1y/lambda-generic-90265.C 
gcc/testsuite/g++.dg/cpp1y/lambda-generic-90265.C
new file mode 100644
index 00000000000..d9ab9b7f55f
--- /dev/null
+++ gcc/testsuite/g++.dg/cpp1y/lambda-generic-90265.C
@@ -0,0 +1,4 @@
+// PR c++/90265
+// { dg-do compile { target c++14 } }
+
+void (*a)(int, int, int, void *) = [](auto, auto, auto, auto) {};

Reply via email to