[PATCH] c++: Fix operator() lookup in lambdas [PR95451]

Jakub Jelinek jakub@redhat.com
Thu Feb 25 18:48:26 GMT 2021


Hi!

During name lookup, name-lookup.c uses:
            if (!(!iter->type && HIDDEN_TYPE_BINDING_P (iter))
                && (bool (want & LOOK_want::HIDDEN_LAMBDA)
                    || !is_lambda_ignored_entity (iter->value))
                && qualify_lookup (iter->value, want))
              binding = iter->value;
Unfortunately as the following testcase shows, this doesn't work in
generic lambdas, where we on the auto b = ... lambda ICE and on the
auto d = lambda reject it even when it should be valid.  The problem
is that the binding doesn't have a FUNCTION_DECL with
LAMBDA_FUNCTION_P for the operator(), but an OVERLOAD with
TEMPLATE_DECL for such FUNCTION_DECL.

The following patch fixes that in is_lambda_ignored_entity, other
possibility would be to do that before calling is_lambda_ignored_entity
in name-lookup.c.

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

2021-02-25  Jakub Jelinek  <jakub@redhat.com>

	PR c++/95451
	* lambda.c (is_lambda_ignored_entity): Before checking for
	FUNCTION_DECL LAMBDA_FUNCTION_P, use OVL_FIRST and STRIP_TEMPLATE.

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

--- gcc/cp/lambda.c.jj	2021-01-04 10:25:48.940119380 +0100
+++ gcc/cp/lambda.c	2021-02-25 17:03:40.099087470 +0100
@@ -1352,6 +1352,8 @@ is_lambda_ignored_entity (tree val)
 
   /* None of the lookups that use qualify_lookup want the op() from the
      lambda; they want the one from the enclosing class.  */
+  val = OVL_FIRST (val);
+  val = STRIP_TEMPLATE (val);
   if (TREE_CODE (val) == FUNCTION_DECL && LAMBDA_FUNCTION_P (val))
     return true;
 
--- gcc/testsuite/g++.dg/cpp1y/lambda-generic-95451.C.jj	2021-02-25 17:11:42.267673666 +0100
+++ gcc/testsuite/g++.dg/cpp1y/lambda-generic-95451.C	2021-02-25 17:10:53.009226742 +0100
@@ -0,0 +1,35 @@
+// PR c++/95451
+// { dg-do run { target c++14 } }
+
+extern "C" void abort ();
+
+struct A {
+  template <typename>
+  void foo ()
+  {
+    auto b = [this] (auto) { return operator () (); } (0);
+    if (b != 3)
+      abort ();
+    auto c = [this] (int) { return operator () (); } (0);
+    if (c != 3)
+      abort ();
+  }
+  void bar ()
+  {
+    auto d = [this] (auto) { return operator () (); } (0);
+    if (d != 3)
+      abort ();
+    auto e = [this] (int) { return operator () (); } (0);
+    if (e != 3)
+      abort ();
+  }
+  int operator () () { return 3; }
+};
+
+int
+main ()
+{
+  A a;
+  a.foo<void> ();
+  a.bar ();
+}

	Jakub



More information about the Gcc-patches mailing list