On 3/4/24 12:49, Marek Polacek wrote:
On Fri, Mar 01, 2024 at 07:58:24PM -0500, Marek Polacek wrote:
Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for 15?  (Or even trunk?)

-- >8 --
<https://wg21.link/p1381r1> clarifies that it's OK to capture structured
bindings.

[expr.prim.lambda.capture]/4 says "The identifier in a simple-capture shall
denote a local entity" and [basic.pre]/3: "An entity is a [...] structured
binding".

It doesn't appear that this was made a DR, so, strictly speaking, we
should have a -Wc++20-extensions warning, like clang++.

        PR c++/85889

gcc/cp/ChangeLog:

        * lambda.cc (add_capture): Add a pedwarn for capturing structured
        bindings.

gcc/testsuite/ChangeLog:

        * g++.dg/cpp2a/decomp3.C: Use -Wno-c++20-extensions.
        * g++.dg/cpp1z/decomp60.C: New test.
---
  gcc/cp/lambda.cc                      |  9 +++++++++
  gcc/testsuite/g++.dg/cpp1z/decomp60.C | 12 ++++++++++++
  gcc/testsuite/g++.dg/cpp2a/decomp3.C  |  2 +-
  3 files changed, 22 insertions(+), 1 deletion(-)
  create mode 100644 gcc/testsuite/g++.dg/cpp1z/decomp60.C

diff --git a/gcc/cp/lambda.cc b/gcc/cp/lambda.cc
index 4b1f9391fee..470f9d2c4f1 100644
--- a/gcc/cp/lambda.cc
+++ b/gcc/cp/lambda.cc
@@ -607,6 +607,15 @@ add_capture (tree lambda, tree id, tree orig_init, bool 
by_reference_p,
                                         TCTX_CAPTURE_BY_COPY, type))
            return error_mark_node;
        }
+
+      if (cxx_dialect < cxx20)
+       {
+         tree stripped_init = tree_strip_any_location_wrapper (initializer);

I was missing an auto_diagnostic_group here.  Fixed.

OK for 15 with that fix.

+         if (DECL_DECOMPOSITION_P (stripped_init)
+             && pedwarn (input_location, OPT_Wc__20_extensions,
+                         "captured structured bindings are a C++20 extension"))
+           inform (DECL_SOURCE_LOCATION (stripped_init), "declared here");


Reply via email to