]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c++: Fix up pedwarn for capturing structured bindings in lambdas [PR118719]
authorJakub Jelinek <jakub@redhat.com>
Tue, 4 Feb 2025 08:23:15 +0000 (09:23 +0100)
committerJakub Jelinek <jakub@gcc.gnu.org>
Tue, 4 Feb 2025 08:24:06 +0000 (09:24 +0100)
As mentioned in the PR, this pedwarni is desirable for the implicit or
explicit capturing of structured bindings in C++17, but in the case of
init-captures the initializer is just some expression and that can include
structured bindings.

So, the following patch limits the warning to non-explicit_init_p.

2025-02-04  Jakub Jelinek  <jakub@redhat.com>

PR c++/118719
* lambda.cc (add_capture): Only pedwarn about capturing structured
binding if !explicit_init_p.

* g++.dg/cpp1z/decomp63.C: New test.

gcc/cp/lambda.cc
gcc/testsuite/g++.dg/cpp1z/decomp63.C [new file with mode: 0644]

index 5593636eaf8edc9b5ac8efda8c964c0214e383a9..2d86e9892f99aea2baa4953878726c0ed813585b 100644 (file)
@@ -613,7 +613,7 @@ add_capture (tree lambda, tree id, tree orig_init, bool by_reference_p,
            return error_mark_node;
        }
 
-      if (cxx_dialect < cxx20)
+      if (cxx_dialect < cxx20 && !explicit_init_p)
        {
          auto_diagnostic_group d;
          tree stripped_init = tree_strip_any_location_wrapper (initializer);
diff --git a/gcc/testsuite/g++.dg/cpp1z/decomp63.C b/gcc/testsuite/g++.dg/cpp1z/decomp63.C
new file mode 100644 (file)
index 0000000..5004925
--- /dev/null
@@ -0,0 +1,18 @@
+// PR c++/118719
+// { dg-do compile { target c++11 } }
+// { dg-options "" }
+
+int
+main ()
+{
+  int a[] = { 42 };
+  auto [x] = a;                                        // { dg-warning "structured bindings only available with" "" { target c++14_down } }
+                                               // { dg-message "declared here" "" { target c++17_down } .-1 }
+  [=] () { int b = x; (void) b; };             // { dg-warning "captured structured bindings are a C\\\+\\\+20 extension" "" { target c++17_down } }
+  [&] () { int b = x; (void) b; };             // { dg-warning "captured structured bindings are a C\\\+\\\+20 extension" "" { target c++17_down } }
+  [x] () { int b = x; (void) b; };             // { dg-warning "captured structured bindings are a C\\\+\\\+20 extension" "" { target c++17_down } }
+  [&x] () { int b = x; (void) b; };            // { dg-warning "captured structured bindings are a C\\\+\\\+20 extension" "" { target c++17_down } }
+  [x = x] () { int b = x; (void) b; };         // { dg-warning "lambda capture initializers only available with" "" { target c++11_only } }
+  [y = x] () { int b = y; (void) b; };         // { dg-warning "lambda capture initializers only available with" "" { target c++11_only } }
+  [y = x * 2] () { int b = y; (void) b; };     // { dg-warning "lambda capture initializers only available with" "" { target c++11_only } }
+}