diff options
author | Marek Polacek <polacek@redhat.com> | 2024-03-01 17:13:02 -0500 |
---|---|---|
committer | Marek Polacek <polacek@redhat.com> | 2024-05-09 12:42:42 -0400 |
commit | 46bafd9a6b9b776142e0b1424a6ac02e3a2fd300 (patch) | |
tree | 54ee9d969755a48700bab41f2624a31048047eea | |
parent | 80d1e2ec4d394111ebd50d2e8928f7596b7b5c7e (diff) | |
download | gcc-46bafd9a6b9b776142e0b1424a6ac02e3a2fd300.zip gcc-46bafd9a6b9b776142e0b1424a6ac02e3a2fd300.tar.gz gcc-46bafd9a6b9b776142e0b1424a6ac02e3a2fd300.tar.bz2 |
c++: lambda capturing structured bindings [PR85889]
<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.
-rw-r--r-- | gcc/cp/lambda.cc | 10 | ||||
-rw-r--r-- | gcc/testsuite/g++.dg/cpp1z/decomp60.C | 12 | ||||
-rw-r--r-- | gcc/testsuite/g++.dg/cpp2a/decomp3.C | 2 |
3 files changed, 23 insertions, 1 deletions
diff --git a/gcc/cp/lambda.cc b/gcc/cp/lambda.cc index 4b1f939..630cc4e 100644 --- a/gcc/cp/lambda.cc +++ b/gcc/cp/lambda.cc @@ -607,6 +607,16 @@ 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) + { + auto_diagnostic_group d; + tree stripped_init = tree_strip_any_location_wrapper (initializer); + 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"); + } } /* Add __ to the beginning of the field name so that user code diff --git a/gcc/testsuite/g++.dg/cpp1z/decomp60.C b/gcc/testsuite/g++.dg/cpp1z/decomp60.C new file mode 100644 index 0000000..b6117f3 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp1z/decomp60.C @@ -0,0 +1,12 @@ +// PR c++/85889 +// { dg-do compile { target c++17 } } +// { dg-options "" } + +struct X { int i, j; }; +void f() { + X x{}; + auto [i, j] = x; + [&i]() { }; // { dg-warning "captured structured bindings" "" { target c++17_only } } + [i]() { }; // { dg-warning "captured structured bindings" "" { target c++17_only } } +} + diff --git a/gcc/testsuite/g++.dg/cpp2a/decomp3.C b/gcc/testsuite/g++.dg/cpp2a/decomp3.C index 5e77973..25ab831 100644 --- a/gcc/testsuite/g++.dg/cpp2a/decomp3.C +++ b/gcc/testsuite/g++.dg/cpp2a/decomp3.C @@ -1,6 +1,6 @@ // P1381R1 // { dg-do compile { target c++11 } } -// { dg-options "" } +// { dg-options "-Wno-c++20-extensions" } struct Foo { int a : 1; int b; }; |