When we're instantiating a generic lambda, its enclosing context will
have already been instantiated, so we need to look for that as well.

Tested x86_64-pc-linux-gnu, applying to trunk.

        * pt.c (enclosing_instantiation_of): Also check
        instantiated_lambda_fn_p for the template context.
---
 gcc/cp/pt.c                                       |  3 ++-
 gcc/testsuite/g++.dg/cpp1y/lambda-generic-func1.C | 12 ++++++++++++
 gcc/cp/ChangeLog                                  |  6 ++++++
 3 files changed, 20 insertions(+), 1 deletion(-)
 create mode 100644 gcc/testsuite/g++.dg/cpp1y/lambda-generic-func1.C

diff --git a/gcc/cp/pt.c b/gcc/cp/pt.c
index b8fbf4046f0..6304c99c5b1 100644
--- a/gcc/cp/pt.c
+++ b/gcc/cp/pt.c
@@ -13353,7 +13353,8 @@ enclosing_instantiation_of (tree otctx)
   tree fn = current_function_decl;
   int lambda_count = 0;
 
-  for (; tctx && lambda_fn_in_template_p (tctx);
+  for (; tctx && (lambda_fn_in_template_p (tctx)
+                 || instantiated_lambda_fn_p (tctx));
        tctx = decl_function_context (tctx))
     ++lambda_count;
   for (; fn; fn = decl_function_context (fn))
diff --git a/gcc/testsuite/g++.dg/cpp1y/lambda-generic-func1.C 
b/gcc/testsuite/g++.dg/cpp1y/lambda-generic-func1.C
new file mode 100644
index 00000000000..4a6385529f8
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp1y/lambda-generic-func1.C
@@ -0,0 +1,12 @@
+// PR c++/89241
+// { dg-do compile { target c++14 } }
+
+template <typename al> void m(al p) {
+  p(1);
+}
+
+template <typename ax> void f() {
+  m([](auto) { __func__; });
+}
+
+template void f<int>();
diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog
index 76f22f5e363..24939e9fc9e 100644
--- a/gcc/cp/ChangeLog
+++ b/gcc/cp/ChangeLog
@@ -1,3 +1,9 @@
+2019-02-11  Jason Merrill  <ja...@redhat.com>
+
+       PR c++/89241 - ICE with __func__ in lambda in template.
+       * pt.c (enclosing_instantiation_of): Also check
+       instantiated_lambda_fn_p for the template context.
+
 2019-02-11  Martin Sebor  <mse...@redhat.com>
 
        PR c++/87996

base-commit: 5f2991399dfbe89e3a6a4b241f489631e806a272
-- 
2.20.1

Reply via email to