From 4b2726a62ddfe958302d6790725503ac09b28f11 Mon Sep 17 00:00:00 2001
From: Jakub Jelinek <jakub@redhat.com>
Date: Tue, 4 Feb 2025 09:23:15 +0100
Subject: [PATCH] c++: Fix up pedwarn for capturing structured bindings in
 lambdas [PR118719]

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                      |  2 +-
 gcc/testsuite/g++.dg/cpp1z/decomp63.C | 18 ++++++++++++++++++
 2 files changed, 19 insertions(+), 1 deletion(-)
 create mode 100644 gcc/testsuite/g++.dg/cpp1z/decomp63.C

diff --git a/gcc/cp/lambda.cc b/gcc/cp/lambda.cc
index 5593636eaf8e..2d86e9892f99 100644
--- a/gcc/cp/lambda.cc
+++ b/gcc/cp/lambda.cc
@@ -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
index 000000000000..50049255efb1
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp1z/decomp63.C
@@ -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 } }
+}
-- 
GitLab