From: Jakub Jelinek Date: Tue, 4 Feb 2025 08:23:15 +0000 (+0100) Subject: c++: Fix up pedwarn for capturing structured bindings in lambdas [PR118719] X-Git-Tag: basepoints/gcc-16~2206 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=4b2726a62ddfe958302d6790725503ac09b28f11;p=thirdparty%2Fgcc.git 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 PR c++/118719 * lambda.cc (add_capture): Only pedwarn about capturing structured binding if !explicit_init_p. * g++.dg/cpp1z/decomp63.C: New test. --- diff --git a/gcc/cp/lambda.cc b/gcc/cp/lambda.cc index 5593636eaf8..2d86e9892f9 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 00000000000..50049255efb --- /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 } } +}