public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
From: Patrick Palka <ppalka@gcc.gnu.org>
To: gcc-cvs@gcc.gnu.org
Subject: [gcc r12-6065] c++: nested lambda capturing a capture proxy, cont [PR94376]
Date: Sun, 19 Dec 2021 19:42:37 +0000 (GMT)	[thread overview]
Message-ID: <20211219194237.552B53858400@sourceware.org> (raw)

https://gcc.gnu.org/g:89cf57ea35d1e0a0b818997c737ac70b7310d9d9

commit r12-6065-g89cf57ea35d1e0a0b818997c737ac70b7310d9d9
Author: Patrick Palka <ppalka@redhat.com>
Date:   Sun Dec 19 14:42:14 2021 -0500

    c++: nested lambda capturing a capture proxy, cont [PR94376]
    
    The r12-5403 fix apparently doesn't handle the case where the inner
    lambda explicitly rather than implicitly captures the capture proxy from
    the outer lambda, which causes us to reject the first example in the
    testcase below.
    
    This is because compared to an implicit capture, the effective initializer
    for an explicit capture is wrapped in a location wrapper (pointing to within
    the capture list), and this wrapper foils the is_capture_proxy check added
    in r12-5403.
    
    The simplest fix appears to be to strip location wrappers accordingly
    before checking is_capture_proxy.  And to help prevent against this kind
    of bug, this patch also makes is_capture_proxy assert it doesn't see a
    location wrapper.
    
            PR c++/94376
    
    gcc/cp/ChangeLog:
    
            * lambda.c (lambda_capture_field_type): Strip location wrappers
            before checking for a capture proxy.
            (is_capture_proxy): Assert that we don't see a location wrapper.
            (mark_const_cap_r): Don't call is_constant_capture_proxy on a
            location wrapper.
    
    gcc/testsuite/ChangeLog:
    
            * g++.dg/cpp0x/lambda/lambda-nested9a.C: New test.

Diff:
---
 gcc/cp/lambda.c                                    |  9 ++++-
 .../g++.dg/cpp0x/lambda/lambda-nested9a.C          | 42 ++++++++++++++++++++++
 2 files changed, 50 insertions(+), 1 deletion(-)

diff --git a/gcc/cp/lambda.c b/gcc/cp/lambda.c
index c39a2bca416..b1cbe271c81 100644
--- a/gcc/cp/lambda.c
+++ b/gcc/cp/lambda.c
@@ -221,6 +221,8 @@ lambda_capture_field_type (tree expr, bool explicit_init_p,
     }
   else
     {
+      STRIP_ANY_LOCATION_WRAPPER (expr);
+
       if (!by_reference_p && is_capture_proxy (expr))
 	{
 	  /* When capturing by-value another capture proxy from an enclosing
@@ -246,6 +248,10 @@ lambda_capture_field_type (tree expr, bool explicit_init_p,
 bool
 is_capture_proxy (tree decl)
 {
+  /* Location wrappers should be stripped or otherwise handled by the
+     caller before using this predicate.  */
+  gcc_checking_assert (!location_wrapper_p (decl));
+
   return (VAR_P (decl)
 	  && DECL_HAS_VALUE_EXPR_P (decl)
 	  && !DECL_ANON_UNION_VAR_P (decl)
@@ -1496,7 +1502,8 @@ mark_const_cap_r (tree *t, int *walk_subtrees, void *data)
 	  *walk_subtrees = 0;
 	}
     }
-  else if (is_constant_capture_proxy (*t))
+  else if (!location_wrapper_p (*t) /* is_capture_proxy dislikes them.  */
+	   && is_constant_capture_proxy (*t))
     var = DECL_CAPTURED_VARIABLE (*t);
 
   if (var)
diff --git a/gcc/testsuite/g++.dg/cpp0x/lambda/lambda-nested9a.C b/gcc/testsuite/g++.dg/cpp0x/lambda/lambda-nested9a.C
new file mode 100644
index 00000000000..d62f8f0c952
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp0x/lambda/lambda-nested9a.C
@@ -0,0 +1,42 @@
+// PR c++/94376
+// Like lambda-nested9.C but using explicit captures in the inner lambda.
+// { dg-do compile { target c++11 } }
+
+int main() {
+  // We used to incorrectly reject the first two cases.
+  int i = 0;
+  [=] () {
+    [i] () mutable {
+      ++i;
+    };
+  };
+
+#if __cpp_init_captures
+  [j=0] () {
+    [j] () mutable {
+      ++j;
+    };
+  };
+#endif
+
+  [=] () {
+    [&i] () mutable {
+      ++i; // { dg-error "read-only" }
+    };
+  };
+
+  const int j = 0;
+  [=] () {
+    [j] () mutable {
+      ++j; // { dg-error "read-only" }
+    };
+  };
+
+#if __cpp_init_captures
+  [j=0] () {
+    [&j] () mutable {
+      ++j; // { dg-error "read-only" "" { target c++14 } }
+    };
+  };
+#endif
+}


                 reply	other threads:[~2021-12-19 19:42 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20211219194237.552B53858400@sourceware.org \
    --to=ppalka@gcc.gnu.org \
    --cc=gcc-cvs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).