public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Marek Polacek <polacek@redhat.com>
To: Patrick Palka <ppalka@redhat.com>
Cc: Jason Merrill <jason@redhat.com>, GCC Patches <gcc-patches@gcc.gnu.org>
Subject: [PATCH v2] c++: ICE with redundant capture [PR108829]
Date: Fri, 17 Feb 2023 16:06:07 -0500	[thread overview]
Message-ID: <Y+/sP+kFAwElRqWA@redhat.com> (raw)
In-Reply-To: <caa25ea3-48ff-464c-3633-c0c552c2cd9c@idea>

On Fri, Feb 17, 2023 at 03:00:39PM -0500, Patrick Palka wrote:
> On Fri, 17 Feb 2023, Marek Polacek via Gcc-patches wrote:
> 
> > Here we crash in is_capture_proxy:
> > 
> >   /* Location wrappers should be stripped or otherwise handled by the
> >      caller before using this predicate.  */
> >   gcc_checking_assert (!location_wrapper_p (decl));
> > 
> > so fixed as the comment suggests.  We only crash with the redundant
> > capture:
> > 
> >   int abyPage = [=, abyPage] { ... }
> > 
> > because prune_lambda_captures is only called when there was a default
> > capture, and with [=] only abyPage won't be in LAMBDA_EXPR_CAPTURE_LIST.
> 
> It's weird that we even get this far in var_to_maybe_prune.  Shouldn't
> LAMBDA_CAPTURE_EXPLICIT_P be true for abyPage?

Ug, I was seduced by the ostensible obviousness and failed to notice
that check.  In that light, the correct fix ought to be this.  Thanks!

Bootstrap/regtest running on x86_64-pc-linux-gnu, ok for trunk if it
passes?

-- >8 --
Here we crash in is_capture_proxy:

  /* Location wrappers should be stripped or otherwise handled by the
     caller before using this predicate.  */
  gcc_checking_assert (!location_wrapper_p (decl));

We only crash with the redundant capture:

  int abyPage = [=, abyPage] { ... }

because prune_lambda_captures is only called when there was a default
capture, and with [=] only abyPage won't be in LAMBDA_EXPR_CAPTURE_LIST.

The problem is that LAMBDA_CAPTURE_EXPLICIT_P wasn't propagated
correctly and so var_to_maybe_prune proceeded where it shouldn't.

	PR c++/108829

gcc/cp/ChangeLog:

	* pt.cc (tsubst_lambda_expr): Propagate LAMBDA_CAPTURE_EXPLICIT_P.

gcc/testsuite/ChangeLog:

	* g++.dg/cpp0x/lambda/lambda-108829.C: New test.
---
 gcc/cp/pt.cc                                      |  4 ++++
 gcc/testsuite/g++.dg/cpp0x/lambda/lambda-108829.C | 11 +++++++++++
 2 files changed, 15 insertions(+)
 create mode 100644 gcc/testsuite/g++.dg/cpp0x/lambda/lambda-108829.C

diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc
index b1ac7d4beb4..f747ce877b5 100644
--- a/gcc/cp/pt.cc
+++ b/gcc/cp/pt.cc
@@ -19992,6 +19992,10 @@ tsubst_lambda_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
 	  if (id_equal (DECL_NAME (field), "__this"))
 	    LAMBDA_EXPR_THIS_CAPTURE (r) = field;
 	}
+
+      if (LAMBDA_EXPR_CAPTURE_LIST (r))
+	LAMBDA_CAPTURE_EXPLICIT_P (LAMBDA_EXPR_CAPTURE_LIST (r))
+	  = LAMBDA_CAPTURE_EXPLICIT_P (LAMBDA_EXPR_CAPTURE_LIST (t));
     }
 
   tree type = begin_lambda_type (r);
diff --git a/gcc/testsuite/g++.dg/cpp0x/lambda/lambda-108829.C b/gcc/testsuite/g++.dg/cpp0x/lambda/lambda-108829.C
new file mode 100644
index 00000000000..e621a0d14d0
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp0x/lambda/lambda-108829.C
@@ -0,0 +1,11 @@
+// PR c++/108829
+// { dg-do compile { target c++11 } }
+
+template <int>
+void f(void) {
+  constexpr int IDX_PAGE_SIZE = 4096;
+  int abyPage = [=, abyPage] { return IDX_PAGE_SIZE; }(); // { dg-error "redundant" }
+}
+void h() {
+  f<1>();
+}

base-commit: 5fea1be820508e1fbc610d1a54b61c1add33c36f
-- 
2.39.2


  reply	other threads:[~2023-02-17 21:06 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-02-17 18:58 [PATCH] " Marek Polacek
2023-02-17 20:00 ` Patrick Palka
2023-02-17 21:06   ` Marek Polacek [this message]
2023-02-17 21:27     ` [PATCH v2] " Patrick Palka
2023-02-17 21:32       ` Patrick Palka
2023-02-17 22:42         ` [PATCH v3] " Marek Polacek
2023-02-20  2:43           ` Jason Merrill

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=Y+/sP+kFAwElRqWA@redhat.com \
    --to=polacek@redhat.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=jason@redhat.com \
    --cc=ppalka@redhat.com \
    /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).